1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy /* 22*eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*eda14cbcSMatt Macy * Copyright (c) 2012, 2019 by Delphix. All rights reserved. 24*eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25*eda14cbcSMatt Macy */ 26*eda14cbcSMatt Macy 27*eda14cbcSMatt Macy #include <sys/zfs_context.h> 28*eda14cbcSMatt Macy #include <sys/dbuf.h> 29*eda14cbcSMatt Macy #include <sys/dnode.h> 30*eda14cbcSMatt Macy #include <sys/dmu.h> 31*eda14cbcSMatt Macy #include <sys/dmu_impl.h> 32*eda14cbcSMatt Macy #include <sys/dmu_tx.h> 33*eda14cbcSMatt Macy #include <sys/dmu_objset.h> 34*eda14cbcSMatt Macy #include <sys/dsl_dir.h> 35*eda14cbcSMatt Macy #include <sys/dsl_dataset.h> 36*eda14cbcSMatt Macy #include <sys/spa.h> 37*eda14cbcSMatt Macy #include <sys/zio.h> 38*eda14cbcSMatt Macy #include <sys/dmu_zfetch.h> 39*eda14cbcSMatt Macy #include <sys/range_tree.h> 40*eda14cbcSMatt Macy #include <sys/trace_zfs.h> 41*eda14cbcSMatt Macy #include <sys/zfs_project.h> 42*eda14cbcSMatt Macy 43*eda14cbcSMatt Macy dnode_stats_t dnode_stats = { 44*eda14cbcSMatt Macy { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64 }, 45*eda14cbcSMatt Macy { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64 }, 46*eda14cbcSMatt Macy { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64 }, 47*eda14cbcSMatt Macy { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64 }, 48*eda14cbcSMatt Macy { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64 }, 49*eda14cbcSMatt Macy { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64 }, 50*eda14cbcSMatt Macy { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64 }, 51*eda14cbcSMatt Macy { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64 }, 52*eda14cbcSMatt Macy { "dnode_hold_free_hits", KSTAT_DATA_UINT64 }, 53*eda14cbcSMatt Macy { "dnode_hold_free_misses", KSTAT_DATA_UINT64 }, 54*eda14cbcSMatt Macy { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64 }, 55*eda14cbcSMatt Macy { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64 }, 56*eda14cbcSMatt Macy { "dnode_hold_free_overflow", KSTAT_DATA_UINT64 }, 57*eda14cbcSMatt Macy { "dnode_hold_free_refcount", KSTAT_DATA_UINT64 }, 58*eda14cbcSMatt Macy { "dnode_free_interior_lock_retry", KSTAT_DATA_UINT64 }, 59*eda14cbcSMatt Macy { "dnode_allocate", KSTAT_DATA_UINT64 }, 60*eda14cbcSMatt Macy { "dnode_reallocate", KSTAT_DATA_UINT64 }, 61*eda14cbcSMatt Macy { "dnode_buf_evict", KSTAT_DATA_UINT64 }, 62*eda14cbcSMatt Macy { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64 }, 63*eda14cbcSMatt Macy { "dnode_alloc_race", KSTAT_DATA_UINT64 }, 64*eda14cbcSMatt Macy { "dnode_alloc_next_block", KSTAT_DATA_UINT64 }, 65*eda14cbcSMatt Macy { "dnode_move_invalid", KSTAT_DATA_UINT64 }, 66*eda14cbcSMatt Macy { "dnode_move_recheck1", KSTAT_DATA_UINT64 }, 67*eda14cbcSMatt Macy { "dnode_move_recheck2", KSTAT_DATA_UINT64 }, 68*eda14cbcSMatt Macy { "dnode_move_special", KSTAT_DATA_UINT64 }, 69*eda14cbcSMatt Macy { "dnode_move_handle", KSTAT_DATA_UINT64 }, 70*eda14cbcSMatt Macy { "dnode_move_rwlock", KSTAT_DATA_UINT64 }, 71*eda14cbcSMatt Macy { "dnode_move_active", KSTAT_DATA_UINT64 }, 72*eda14cbcSMatt Macy }; 73*eda14cbcSMatt Macy 74*eda14cbcSMatt Macy static kstat_t *dnode_ksp; 75*eda14cbcSMatt Macy static kmem_cache_t *dnode_cache; 76*eda14cbcSMatt Macy 77*eda14cbcSMatt Macy static dnode_phys_t dnode_phys_zero __maybe_unused; 78*eda14cbcSMatt Macy 79*eda14cbcSMatt Macy int zfs_default_bs = SPA_MINBLOCKSHIFT; 80*eda14cbcSMatt Macy int zfs_default_ibs = DN_MAX_INDBLKSHIFT; 81*eda14cbcSMatt Macy 82*eda14cbcSMatt Macy #ifdef _KERNEL 83*eda14cbcSMatt Macy static kmem_cbrc_t dnode_move(void *, void *, size_t, void *); 84*eda14cbcSMatt Macy #endif /* _KERNEL */ 85*eda14cbcSMatt Macy 86*eda14cbcSMatt Macy static int 87*eda14cbcSMatt Macy dbuf_compare(const void *x1, const void *x2) 88*eda14cbcSMatt Macy { 89*eda14cbcSMatt Macy const dmu_buf_impl_t *d1 = x1; 90*eda14cbcSMatt Macy const dmu_buf_impl_t *d2 = x2; 91*eda14cbcSMatt Macy 92*eda14cbcSMatt Macy int cmp = TREE_CMP(d1->db_level, d2->db_level); 93*eda14cbcSMatt Macy if (likely(cmp)) 94*eda14cbcSMatt Macy return (cmp); 95*eda14cbcSMatt Macy 96*eda14cbcSMatt Macy cmp = TREE_CMP(d1->db_blkid, d2->db_blkid); 97*eda14cbcSMatt Macy if (likely(cmp)) 98*eda14cbcSMatt Macy return (cmp); 99*eda14cbcSMatt Macy 100*eda14cbcSMatt Macy if (d1->db_state == DB_SEARCH) { 101*eda14cbcSMatt Macy ASSERT3S(d2->db_state, !=, DB_SEARCH); 102*eda14cbcSMatt Macy return (-1); 103*eda14cbcSMatt Macy } else if (d2->db_state == DB_SEARCH) { 104*eda14cbcSMatt Macy ASSERT3S(d1->db_state, !=, DB_SEARCH); 105*eda14cbcSMatt Macy return (1); 106*eda14cbcSMatt Macy } 107*eda14cbcSMatt Macy 108*eda14cbcSMatt Macy return (TREE_PCMP(d1, d2)); 109*eda14cbcSMatt Macy } 110*eda14cbcSMatt Macy 111*eda14cbcSMatt Macy /* ARGSUSED */ 112*eda14cbcSMatt Macy static int 113*eda14cbcSMatt Macy dnode_cons(void *arg, void *unused, int kmflag) 114*eda14cbcSMatt Macy { 115*eda14cbcSMatt Macy dnode_t *dn = arg; 116*eda14cbcSMatt Macy int i; 117*eda14cbcSMatt Macy 118*eda14cbcSMatt Macy rw_init(&dn->dn_struct_rwlock, NULL, RW_NOLOCKDEP, NULL); 119*eda14cbcSMatt Macy mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL); 120*eda14cbcSMatt Macy mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL); 121*eda14cbcSMatt Macy cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL); 122*eda14cbcSMatt Macy cv_init(&dn->dn_nodnholds, NULL, CV_DEFAULT, NULL); 123*eda14cbcSMatt Macy 124*eda14cbcSMatt Macy /* 125*eda14cbcSMatt Macy * Every dbuf has a reference, and dropping a tracked reference is 126*eda14cbcSMatt Macy * O(number of references), so don't track dn_holds. 127*eda14cbcSMatt Macy */ 128*eda14cbcSMatt Macy zfs_refcount_create_untracked(&dn->dn_holds); 129*eda14cbcSMatt Macy zfs_refcount_create(&dn->dn_tx_holds); 130*eda14cbcSMatt Macy list_link_init(&dn->dn_link); 131*eda14cbcSMatt Macy 132*eda14cbcSMatt Macy bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr)); 133*eda14cbcSMatt Macy bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels)); 134*eda14cbcSMatt Macy bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift)); 135*eda14cbcSMatt Macy bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype)); 136*eda14cbcSMatt Macy bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk)); 137*eda14cbcSMatt Macy bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen)); 138*eda14cbcSMatt Macy bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz)); 139*eda14cbcSMatt Macy bzero(&dn->dn_next_maxblkid[0], sizeof (dn->dn_next_maxblkid)); 140*eda14cbcSMatt Macy 141*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 142*eda14cbcSMatt Macy multilist_link_init(&dn->dn_dirty_link[i]); 143*eda14cbcSMatt Macy dn->dn_free_ranges[i] = NULL; 144*eda14cbcSMatt Macy list_create(&dn->dn_dirty_records[i], 145*eda14cbcSMatt Macy sizeof (dbuf_dirty_record_t), 146*eda14cbcSMatt Macy offsetof(dbuf_dirty_record_t, dr_dirty_node)); 147*eda14cbcSMatt Macy } 148*eda14cbcSMatt Macy 149*eda14cbcSMatt Macy dn->dn_allocated_txg = 0; 150*eda14cbcSMatt Macy dn->dn_free_txg = 0; 151*eda14cbcSMatt Macy dn->dn_assigned_txg = 0; 152*eda14cbcSMatt Macy dn->dn_dirty_txg = 0; 153*eda14cbcSMatt Macy dn->dn_dirtyctx = 0; 154*eda14cbcSMatt Macy dn->dn_dirtyctx_firstset = NULL; 155*eda14cbcSMatt Macy dn->dn_bonus = NULL; 156*eda14cbcSMatt Macy dn->dn_have_spill = B_FALSE; 157*eda14cbcSMatt Macy dn->dn_zio = NULL; 158*eda14cbcSMatt Macy dn->dn_oldused = 0; 159*eda14cbcSMatt Macy dn->dn_oldflags = 0; 160*eda14cbcSMatt Macy dn->dn_olduid = 0; 161*eda14cbcSMatt Macy dn->dn_oldgid = 0; 162*eda14cbcSMatt Macy dn->dn_oldprojid = ZFS_DEFAULT_PROJID; 163*eda14cbcSMatt Macy dn->dn_newuid = 0; 164*eda14cbcSMatt Macy dn->dn_newgid = 0; 165*eda14cbcSMatt Macy dn->dn_newprojid = ZFS_DEFAULT_PROJID; 166*eda14cbcSMatt Macy dn->dn_id_flags = 0; 167*eda14cbcSMatt Macy 168*eda14cbcSMatt Macy dn->dn_dbufs_count = 0; 169*eda14cbcSMatt Macy avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t), 170*eda14cbcSMatt Macy offsetof(dmu_buf_impl_t, db_link)); 171*eda14cbcSMatt Macy 172*eda14cbcSMatt Macy dn->dn_moved = 0; 173*eda14cbcSMatt Macy return (0); 174*eda14cbcSMatt Macy } 175*eda14cbcSMatt Macy 176*eda14cbcSMatt Macy /* ARGSUSED */ 177*eda14cbcSMatt Macy static void 178*eda14cbcSMatt Macy dnode_dest(void *arg, void *unused) 179*eda14cbcSMatt Macy { 180*eda14cbcSMatt Macy int i; 181*eda14cbcSMatt Macy dnode_t *dn = arg; 182*eda14cbcSMatt Macy 183*eda14cbcSMatt Macy rw_destroy(&dn->dn_struct_rwlock); 184*eda14cbcSMatt Macy mutex_destroy(&dn->dn_mtx); 185*eda14cbcSMatt Macy mutex_destroy(&dn->dn_dbufs_mtx); 186*eda14cbcSMatt Macy cv_destroy(&dn->dn_notxholds); 187*eda14cbcSMatt Macy cv_destroy(&dn->dn_nodnholds); 188*eda14cbcSMatt Macy zfs_refcount_destroy(&dn->dn_holds); 189*eda14cbcSMatt Macy zfs_refcount_destroy(&dn->dn_tx_holds); 190*eda14cbcSMatt Macy ASSERT(!list_link_active(&dn->dn_link)); 191*eda14cbcSMatt Macy 192*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 193*eda14cbcSMatt Macy ASSERT(!multilist_link_active(&dn->dn_dirty_link[i])); 194*eda14cbcSMatt Macy ASSERT3P(dn->dn_free_ranges[i], ==, NULL); 195*eda14cbcSMatt Macy list_destroy(&dn->dn_dirty_records[i]); 196*eda14cbcSMatt Macy ASSERT0(dn->dn_next_nblkptr[i]); 197*eda14cbcSMatt Macy ASSERT0(dn->dn_next_nlevels[i]); 198*eda14cbcSMatt Macy ASSERT0(dn->dn_next_indblkshift[i]); 199*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonustype[i]); 200*eda14cbcSMatt Macy ASSERT0(dn->dn_rm_spillblk[i]); 201*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonuslen[i]); 202*eda14cbcSMatt Macy ASSERT0(dn->dn_next_blksz[i]); 203*eda14cbcSMatt Macy ASSERT0(dn->dn_next_maxblkid[i]); 204*eda14cbcSMatt Macy } 205*eda14cbcSMatt Macy 206*eda14cbcSMatt Macy ASSERT0(dn->dn_allocated_txg); 207*eda14cbcSMatt Macy ASSERT0(dn->dn_free_txg); 208*eda14cbcSMatt Macy ASSERT0(dn->dn_assigned_txg); 209*eda14cbcSMatt Macy ASSERT0(dn->dn_dirty_txg); 210*eda14cbcSMatt Macy ASSERT0(dn->dn_dirtyctx); 211*eda14cbcSMatt Macy ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL); 212*eda14cbcSMatt Macy ASSERT3P(dn->dn_bonus, ==, NULL); 213*eda14cbcSMatt Macy ASSERT(!dn->dn_have_spill); 214*eda14cbcSMatt Macy ASSERT3P(dn->dn_zio, ==, NULL); 215*eda14cbcSMatt Macy ASSERT0(dn->dn_oldused); 216*eda14cbcSMatt Macy ASSERT0(dn->dn_oldflags); 217*eda14cbcSMatt Macy ASSERT0(dn->dn_olduid); 218*eda14cbcSMatt Macy ASSERT0(dn->dn_oldgid); 219*eda14cbcSMatt Macy ASSERT0(dn->dn_oldprojid); 220*eda14cbcSMatt Macy ASSERT0(dn->dn_newuid); 221*eda14cbcSMatt Macy ASSERT0(dn->dn_newgid); 222*eda14cbcSMatt Macy ASSERT0(dn->dn_newprojid); 223*eda14cbcSMatt Macy ASSERT0(dn->dn_id_flags); 224*eda14cbcSMatt Macy 225*eda14cbcSMatt Macy ASSERT0(dn->dn_dbufs_count); 226*eda14cbcSMatt Macy avl_destroy(&dn->dn_dbufs); 227*eda14cbcSMatt Macy } 228*eda14cbcSMatt Macy 229*eda14cbcSMatt Macy void 230*eda14cbcSMatt Macy dnode_init(void) 231*eda14cbcSMatt Macy { 232*eda14cbcSMatt Macy ASSERT(dnode_cache == NULL); 233*eda14cbcSMatt Macy dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t), 234*eda14cbcSMatt Macy 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 235*eda14cbcSMatt Macy kmem_cache_set_move(dnode_cache, dnode_move); 236*eda14cbcSMatt Macy 237*eda14cbcSMatt Macy dnode_ksp = kstat_create("zfs", 0, "dnodestats", "misc", 238*eda14cbcSMatt Macy KSTAT_TYPE_NAMED, sizeof (dnode_stats) / sizeof (kstat_named_t), 239*eda14cbcSMatt Macy KSTAT_FLAG_VIRTUAL); 240*eda14cbcSMatt Macy if (dnode_ksp != NULL) { 241*eda14cbcSMatt Macy dnode_ksp->ks_data = &dnode_stats; 242*eda14cbcSMatt Macy kstat_install(dnode_ksp); 243*eda14cbcSMatt Macy } 244*eda14cbcSMatt Macy } 245*eda14cbcSMatt Macy 246*eda14cbcSMatt Macy void 247*eda14cbcSMatt Macy dnode_fini(void) 248*eda14cbcSMatt Macy { 249*eda14cbcSMatt Macy if (dnode_ksp != NULL) { 250*eda14cbcSMatt Macy kstat_delete(dnode_ksp); 251*eda14cbcSMatt Macy dnode_ksp = NULL; 252*eda14cbcSMatt Macy } 253*eda14cbcSMatt Macy 254*eda14cbcSMatt Macy kmem_cache_destroy(dnode_cache); 255*eda14cbcSMatt Macy dnode_cache = NULL; 256*eda14cbcSMatt Macy } 257*eda14cbcSMatt Macy 258*eda14cbcSMatt Macy 259*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 260*eda14cbcSMatt Macy void 261*eda14cbcSMatt Macy dnode_verify(dnode_t *dn) 262*eda14cbcSMatt Macy { 263*eda14cbcSMatt Macy int drop_struct_lock = FALSE; 264*eda14cbcSMatt Macy 265*eda14cbcSMatt Macy ASSERT(dn->dn_phys); 266*eda14cbcSMatt Macy ASSERT(dn->dn_objset); 267*eda14cbcSMatt Macy ASSERT(dn->dn_handle->dnh_dnode == dn); 268*eda14cbcSMatt Macy 269*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type)); 270*eda14cbcSMatt Macy 271*eda14cbcSMatt Macy if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 272*eda14cbcSMatt Macy return; 273*eda14cbcSMatt Macy 274*eda14cbcSMatt Macy if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 275*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 276*eda14cbcSMatt Macy drop_struct_lock = TRUE; 277*eda14cbcSMatt Macy } 278*eda14cbcSMatt Macy if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 279*eda14cbcSMatt Macy int i; 280*eda14cbcSMatt Macy int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 281*eda14cbcSMatt Macy ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 282*eda14cbcSMatt Macy if (dn->dn_datablkshift) { 283*eda14cbcSMatt Macy ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 284*eda14cbcSMatt Macy ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 285*eda14cbcSMatt Macy ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 286*eda14cbcSMatt Macy } 287*eda14cbcSMatt Macy ASSERT3U(dn->dn_nlevels, <=, 30); 288*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(dn->dn_type)); 289*eda14cbcSMatt Macy ASSERT3U(dn->dn_nblkptr, >=, 1); 290*eda14cbcSMatt Macy ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 291*eda14cbcSMatt Macy ASSERT3U(dn->dn_bonuslen, <=, max_bonuslen); 292*eda14cbcSMatt Macy ASSERT3U(dn->dn_datablksz, ==, 293*eda14cbcSMatt Macy dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 294*eda14cbcSMatt Macy ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 295*eda14cbcSMatt Macy ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 296*eda14cbcSMatt Macy dn->dn_bonuslen, <=, max_bonuslen); 297*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 298*eda14cbcSMatt Macy ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 299*eda14cbcSMatt Macy } 300*eda14cbcSMatt Macy } 301*eda14cbcSMatt Macy if (dn->dn_phys->dn_type != DMU_OT_NONE) 302*eda14cbcSMatt Macy ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 303*eda14cbcSMatt Macy ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL); 304*eda14cbcSMatt Macy if (dn->dn_dbuf != NULL) { 305*eda14cbcSMatt Macy ASSERT3P(dn->dn_phys, ==, 306*eda14cbcSMatt Macy (dnode_phys_t *)dn->dn_dbuf->db.db_data + 307*eda14cbcSMatt Macy (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 308*eda14cbcSMatt Macy } 309*eda14cbcSMatt Macy if (drop_struct_lock) 310*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 311*eda14cbcSMatt Macy } 312*eda14cbcSMatt Macy #endif 313*eda14cbcSMatt Macy 314*eda14cbcSMatt Macy void 315*eda14cbcSMatt Macy dnode_byteswap(dnode_phys_t *dnp) 316*eda14cbcSMatt Macy { 317*eda14cbcSMatt Macy uint64_t *buf64 = (void*)&dnp->dn_blkptr; 318*eda14cbcSMatt Macy int i; 319*eda14cbcSMatt Macy 320*eda14cbcSMatt Macy if (dnp->dn_type == DMU_OT_NONE) { 321*eda14cbcSMatt Macy bzero(dnp, sizeof (dnode_phys_t)); 322*eda14cbcSMatt Macy return; 323*eda14cbcSMatt Macy } 324*eda14cbcSMatt Macy 325*eda14cbcSMatt Macy dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 326*eda14cbcSMatt Macy dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 327*eda14cbcSMatt Macy dnp->dn_extra_slots = BSWAP_8(dnp->dn_extra_slots); 328*eda14cbcSMatt Macy dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 329*eda14cbcSMatt Macy dnp->dn_used = BSWAP_64(dnp->dn_used); 330*eda14cbcSMatt Macy 331*eda14cbcSMatt Macy /* 332*eda14cbcSMatt Macy * dn_nblkptr is only one byte, so it's OK to read it in either 333*eda14cbcSMatt Macy * byte order. We can't read dn_bouslen. 334*eda14cbcSMatt Macy */ 335*eda14cbcSMatt Macy ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 336*eda14cbcSMatt Macy ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 337*eda14cbcSMatt Macy for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 338*eda14cbcSMatt Macy buf64[i] = BSWAP_64(buf64[i]); 339*eda14cbcSMatt Macy 340*eda14cbcSMatt Macy /* 341*eda14cbcSMatt Macy * OK to check dn_bonuslen for zero, because it won't matter if 342*eda14cbcSMatt Macy * we have the wrong byte order. This is necessary because the 343*eda14cbcSMatt Macy * dnode dnode is smaller than a regular dnode. 344*eda14cbcSMatt Macy */ 345*eda14cbcSMatt Macy if (dnp->dn_bonuslen != 0) { 346*eda14cbcSMatt Macy /* 347*eda14cbcSMatt Macy * Note that the bonus length calculated here may be 348*eda14cbcSMatt Macy * longer than the actual bonus buffer. This is because 349*eda14cbcSMatt Macy * we always put the bonus buffer after the last block 350*eda14cbcSMatt Macy * pointer (instead of packing it against the end of the 351*eda14cbcSMatt Macy * dnode buffer). 352*eda14cbcSMatt Macy */ 353*eda14cbcSMatt Macy int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 354*eda14cbcSMatt Macy int slots = dnp->dn_extra_slots + 1; 355*eda14cbcSMatt Macy size_t len = DN_SLOTS_TO_BONUSLEN(slots) - off; 356*eda14cbcSMatt Macy dmu_object_byteswap_t byteswap; 357*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype)); 358*eda14cbcSMatt Macy byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype); 359*eda14cbcSMatt Macy dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len); 360*eda14cbcSMatt Macy } 361*eda14cbcSMatt Macy 362*eda14cbcSMatt Macy /* Swap SPILL block if we have one */ 363*eda14cbcSMatt Macy if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) 364*eda14cbcSMatt Macy byteswap_uint64_array(DN_SPILL_BLKPTR(dnp), sizeof (blkptr_t)); 365*eda14cbcSMatt Macy } 366*eda14cbcSMatt Macy 367*eda14cbcSMatt Macy void 368*eda14cbcSMatt Macy dnode_buf_byteswap(void *vbuf, size_t size) 369*eda14cbcSMatt Macy { 370*eda14cbcSMatt Macy int i = 0; 371*eda14cbcSMatt Macy 372*eda14cbcSMatt Macy ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 373*eda14cbcSMatt Macy ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 374*eda14cbcSMatt Macy 375*eda14cbcSMatt Macy while (i < size) { 376*eda14cbcSMatt Macy dnode_phys_t *dnp = (void *)(((char *)vbuf) + i); 377*eda14cbcSMatt Macy dnode_byteswap(dnp); 378*eda14cbcSMatt Macy 379*eda14cbcSMatt Macy i += DNODE_MIN_SIZE; 380*eda14cbcSMatt Macy if (dnp->dn_type != DMU_OT_NONE) 381*eda14cbcSMatt Macy i += dnp->dn_extra_slots * DNODE_MIN_SIZE; 382*eda14cbcSMatt Macy } 383*eda14cbcSMatt Macy } 384*eda14cbcSMatt Macy 385*eda14cbcSMatt Macy void 386*eda14cbcSMatt Macy dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx) 387*eda14cbcSMatt Macy { 388*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1); 389*eda14cbcSMatt Macy 390*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 391*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 392*eda14cbcSMatt Macy ASSERT3U(newsize, <=, DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) - 393*eda14cbcSMatt Macy (dn->dn_nblkptr-1) * sizeof (blkptr_t)); 394*eda14cbcSMatt Macy 395*eda14cbcSMatt Macy if (newsize < dn->dn_bonuslen) { 396*eda14cbcSMatt Macy /* clear any data after the end of the new size */ 397*eda14cbcSMatt Macy size_t diff = dn->dn_bonuslen - newsize; 398*eda14cbcSMatt Macy char *data_end = ((char *)dn->dn_bonus->db.db_data) + newsize; 399*eda14cbcSMatt Macy bzero(data_end, diff); 400*eda14cbcSMatt Macy } 401*eda14cbcSMatt Macy 402*eda14cbcSMatt Macy dn->dn_bonuslen = newsize; 403*eda14cbcSMatt Macy if (newsize == 0) 404*eda14cbcSMatt Macy dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN; 405*eda14cbcSMatt Macy else 406*eda14cbcSMatt Macy dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 407*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 408*eda14cbcSMatt Macy } 409*eda14cbcSMatt Macy 410*eda14cbcSMatt Macy void 411*eda14cbcSMatt Macy dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx) 412*eda14cbcSMatt Macy { 413*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1); 414*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 415*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 416*eda14cbcSMatt Macy dn->dn_bonustype = newtype; 417*eda14cbcSMatt Macy dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype; 418*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 419*eda14cbcSMatt Macy } 420*eda14cbcSMatt Macy 421*eda14cbcSMatt Macy void 422*eda14cbcSMatt Macy dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx) 423*eda14cbcSMatt Macy { 424*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1); 425*eda14cbcSMatt Macy ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 426*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 427*eda14cbcSMatt Macy dn->dn_rm_spillblk[tx->tx_txg & TXG_MASK] = DN_KILL_SPILLBLK; 428*eda14cbcSMatt Macy dn->dn_have_spill = B_FALSE; 429*eda14cbcSMatt Macy } 430*eda14cbcSMatt Macy 431*eda14cbcSMatt Macy static void 432*eda14cbcSMatt Macy dnode_setdblksz(dnode_t *dn, int size) 433*eda14cbcSMatt Macy { 434*eda14cbcSMatt Macy ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE)); 435*eda14cbcSMatt Macy ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 436*eda14cbcSMatt Macy ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 437*eda14cbcSMatt Macy ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 438*eda14cbcSMatt Macy 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 439*eda14cbcSMatt Macy dn->dn_datablksz = size; 440*eda14cbcSMatt Macy dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 441*eda14cbcSMatt Macy dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0; 442*eda14cbcSMatt Macy } 443*eda14cbcSMatt Macy 444*eda14cbcSMatt Macy static dnode_t * 445*eda14cbcSMatt Macy dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 446*eda14cbcSMatt Macy uint64_t object, dnode_handle_t *dnh) 447*eda14cbcSMatt Macy { 448*eda14cbcSMatt Macy dnode_t *dn; 449*eda14cbcSMatt Macy 450*eda14cbcSMatt Macy dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 451*eda14cbcSMatt Macy dn->dn_moved = 0; 452*eda14cbcSMatt Macy 453*eda14cbcSMatt Macy /* 454*eda14cbcSMatt Macy * Defer setting dn_objset until the dnode is ready to be a candidate 455*eda14cbcSMatt Macy * for the dnode_move() callback. 456*eda14cbcSMatt Macy */ 457*eda14cbcSMatt Macy dn->dn_object = object; 458*eda14cbcSMatt Macy dn->dn_dbuf = db; 459*eda14cbcSMatt Macy dn->dn_handle = dnh; 460*eda14cbcSMatt Macy dn->dn_phys = dnp; 461*eda14cbcSMatt Macy 462*eda14cbcSMatt Macy if (dnp->dn_datablkszsec) { 463*eda14cbcSMatt Macy dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 464*eda14cbcSMatt Macy } else { 465*eda14cbcSMatt Macy dn->dn_datablksz = 0; 466*eda14cbcSMatt Macy dn->dn_datablkszsec = 0; 467*eda14cbcSMatt Macy dn->dn_datablkshift = 0; 468*eda14cbcSMatt Macy } 469*eda14cbcSMatt Macy dn->dn_indblkshift = dnp->dn_indblkshift; 470*eda14cbcSMatt Macy dn->dn_nlevels = dnp->dn_nlevels; 471*eda14cbcSMatt Macy dn->dn_type = dnp->dn_type; 472*eda14cbcSMatt Macy dn->dn_nblkptr = dnp->dn_nblkptr; 473*eda14cbcSMatt Macy dn->dn_checksum = dnp->dn_checksum; 474*eda14cbcSMatt Macy dn->dn_compress = dnp->dn_compress; 475*eda14cbcSMatt Macy dn->dn_bonustype = dnp->dn_bonustype; 476*eda14cbcSMatt Macy dn->dn_bonuslen = dnp->dn_bonuslen; 477*eda14cbcSMatt Macy dn->dn_num_slots = dnp->dn_extra_slots + 1; 478*eda14cbcSMatt Macy dn->dn_maxblkid = dnp->dn_maxblkid; 479*eda14cbcSMatt Macy dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0); 480*eda14cbcSMatt Macy dn->dn_id_flags = 0; 481*eda14cbcSMatt Macy 482*eda14cbcSMatt Macy dmu_zfetch_init(&dn->dn_zfetch, dn); 483*eda14cbcSMatt Macy 484*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type)); 485*eda14cbcSMatt Macy ASSERT(zrl_is_locked(&dnh->dnh_zrlock)); 486*eda14cbcSMatt Macy ASSERT(!DN_SLOT_IS_PTR(dnh->dnh_dnode)); 487*eda14cbcSMatt Macy 488*eda14cbcSMatt Macy mutex_enter(&os->os_lock); 489*eda14cbcSMatt Macy 490*eda14cbcSMatt Macy /* 491*eda14cbcSMatt Macy * Exclude special dnodes from os_dnodes so an empty os_dnodes 492*eda14cbcSMatt Macy * signifies that the special dnodes have no references from 493*eda14cbcSMatt Macy * their children (the entries in os_dnodes). This allows 494*eda14cbcSMatt Macy * dnode_destroy() to easily determine if the last child has 495*eda14cbcSMatt Macy * been removed and then complete eviction of the objset. 496*eda14cbcSMatt Macy */ 497*eda14cbcSMatt Macy if (!DMU_OBJECT_IS_SPECIAL(object)) 498*eda14cbcSMatt Macy list_insert_head(&os->os_dnodes, dn); 499*eda14cbcSMatt Macy membar_producer(); 500*eda14cbcSMatt Macy 501*eda14cbcSMatt Macy /* 502*eda14cbcSMatt Macy * Everything else must be valid before assigning dn_objset 503*eda14cbcSMatt Macy * makes the dnode eligible for dnode_move(). 504*eda14cbcSMatt Macy */ 505*eda14cbcSMatt Macy dn->dn_objset = os; 506*eda14cbcSMatt Macy 507*eda14cbcSMatt Macy dnh->dnh_dnode = dn; 508*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 509*eda14cbcSMatt Macy 510*eda14cbcSMatt Macy arc_space_consume(sizeof (dnode_t), ARC_SPACE_DNODE); 511*eda14cbcSMatt Macy 512*eda14cbcSMatt Macy return (dn); 513*eda14cbcSMatt Macy } 514*eda14cbcSMatt Macy 515*eda14cbcSMatt Macy /* 516*eda14cbcSMatt Macy * Caller must be holding the dnode handle, which is released upon return. 517*eda14cbcSMatt Macy */ 518*eda14cbcSMatt Macy static void 519*eda14cbcSMatt Macy dnode_destroy(dnode_t *dn) 520*eda14cbcSMatt Macy { 521*eda14cbcSMatt Macy objset_t *os = dn->dn_objset; 522*eda14cbcSMatt Macy boolean_t complete_os_eviction = B_FALSE; 523*eda14cbcSMatt Macy 524*eda14cbcSMatt Macy ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0); 525*eda14cbcSMatt Macy 526*eda14cbcSMatt Macy mutex_enter(&os->os_lock); 527*eda14cbcSMatt Macy POINTER_INVALIDATE(&dn->dn_objset); 528*eda14cbcSMatt Macy if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 529*eda14cbcSMatt Macy list_remove(&os->os_dnodes, dn); 530*eda14cbcSMatt Macy complete_os_eviction = 531*eda14cbcSMatt Macy list_is_empty(&os->os_dnodes) && 532*eda14cbcSMatt Macy list_link_active(&os->os_evicting_node); 533*eda14cbcSMatt Macy } 534*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 535*eda14cbcSMatt Macy 536*eda14cbcSMatt Macy /* the dnode can no longer move, so we can release the handle */ 537*eda14cbcSMatt Macy if (!zrl_is_locked(&dn->dn_handle->dnh_zrlock)) 538*eda14cbcSMatt Macy zrl_remove(&dn->dn_handle->dnh_zrlock); 539*eda14cbcSMatt Macy 540*eda14cbcSMatt Macy dn->dn_allocated_txg = 0; 541*eda14cbcSMatt Macy dn->dn_free_txg = 0; 542*eda14cbcSMatt Macy dn->dn_assigned_txg = 0; 543*eda14cbcSMatt Macy dn->dn_dirty_txg = 0; 544*eda14cbcSMatt Macy 545*eda14cbcSMatt Macy dn->dn_dirtyctx = 0; 546*eda14cbcSMatt Macy dn->dn_dirtyctx_firstset = NULL; 547*eda14cbcSMatt Macy if (dn->dn_bonus != NULL) { 548*eda14cbcSMatt Macy mutex_enter(&dn->dn_bonus->db_mtx); 549*eda14cbcSMatt Macy dbuf_destroy(dn->dn_bonus); 550*eda14cbcSMatt Macy dn->dn_bonus = NULL; 551*eda14cbcSMatt Macy } 552*eda14cbcSMatt Macy dn->dn_zio = NULL; 553*eda14cbcSMatt Macy 554*eda14cbcSMatt Macy dn->dn_have_spill = B_FALSE; 555*eda14cbcSMatt Macy dn->dn_oldused = 0; 556*eda14cbcSMatt Macy dn->dn_oldflags = 0; 557*eda14cbcSMatt Macy dn->dn_olduid = 0; 558*eda14cbcSMatt Macy dn->dn_oldgid = 0; 559*eda14cbcSMatt Macy dn->dn_oldprojid = ZFS_DEFAULT_PROJID; 560*eda14cbcSMatt Macy dn->dn_newuid = 0; 561*eda14cbcSMatt Macy dn->dn_newgid = 0; 562*eda14cbcSMatt Macy dn->dn_newprojid = ZFS_DEFAULT_PROJID; 563*eda14cbcSMatt Macy dn->dn_id_flags = 0; 564*eda14cbcSMatt Macy 565*eda14cbcSMatt Macy dmu_zfetch_fini(&dn->dn_zfetch); 566*eda14cbcSMatt Macy kmem_cache_free(dnode_cache, dn); 567*eda14cbcSMatt Macy arc_space_return(sizeof (dnode_t), ARC_SPACE_DNODE); 568*eda14cbcSMatt Macy 569*eda14cbcSMatt Macy if (complete_os_eviction) 570*eda14cbcSMatt Macy dmu_objset_evict_done(os); 571*eda14cbcSMatt Macy } 572*eda14cbcSMatt Macy 573*eda14cbcSMatt Macy void 574*eda14cbcSMatt Macy dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 575*eda14cbcSMatt Macy dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx) 576*eda14cbcSMatt Macy { 577*eda14cbcSMatt Macy int i; 578*eda14cbcSMatt Macy 579*eda14cbcSMatt Macy ASSERT3U(dn_slots, >, 0); 580*eda14cbcSMatt Macy ASSERT3U(dn_slots << DNODE_SHIFT, <=, 581*eda14cbcSMatt Macy spa_maxdnodesize(dmu_objset_spa(dn->dn_objset))); 582*eda14cbcSMatt Macy ASSERT3U(blocksize, <=, 583*eda14cbcSMatt Macy spa_maxblocksize(dmu_objset_spa(dn->dn_objset))); 584*eda14cbcSMatt Macy if (blocksize == 0) 585*eda14cbcSMatt Macy blocksize = 1 << zfs_default_bs; 586*eda14cbcSMatt Macy else 587*eda14cbcSMatt Macy blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 588*eda14cbcSMatt Macy 589*eda14cbcSMatt Macy if (ibs == 0) 590*eda14cbcSMatt Macy ibs = zfs_default_ibs; 591*eda14cbcSMatt Macy 592*eda14cbcSMatt Macy ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 593*eda14cbcSMatt Macy 594*eda14cbcSMatt Macy dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n", 595*eda14cbcSMatt Macy dn->dn_objset, dn->dn_object, tx->tx_txg, blocksize, ibs, dn_slots); 596*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_allocate); 597*eda14cbcSMatt Macy 598*eda14cbcSMatt Macy ASSERT(dn->dn_type == DMU_OT_NONE); 599*eda14cbcSMatt Macy ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 600*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 601*eda14cbcSMatt Macy ASSERT(ot != DMU_OT_NONE); 602*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(ot)); 603*eda14cbcSMatt Macy ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 604*eda14cbcSMatt Macy (bonustype == DMU_OT_SA && bonuslen == 0) || 605*eda14cbcSMatt Macy (bonustype != DMU_OT_NONE && bonuslen != 0)); 606*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(bonustype)); 607*eda14cbcSMatt Macy ASSERT3U(bonuslen, <=, DN_SLOTS_TO_BONUSLEN(dn_slots)); 608*eda14cbcSMatt Macy ASSERT(dn->dn_type == DMU_OT_NONE); 609*eda14cbcSMatt Macy ASSERT0(dn->dn_maxblkid); 610*eda14cbcSMatt Macy ASSERT0(dn->dn_allocated_txg); 611*eda14cbcSMatt Macy ASSERT0(dn->dn_assigned_txg); 612*eda14cbcSMatt Macy ASSERT0(dn->dn_dirty_txg); 613*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&dn->dn_tx_holds)); 614*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&dn->dn_holds), <=, 1); 615*eda14cbcSMatt Macy ASSERT(avl_is_empty(&dn->dn_dbufs)); 616*eda14cbcSMatt Macy 617*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 618*eda14cbcSMatt Macy ASSERT0(dn->dn_next_nblkptr[i]); 619*eda14cbcSMatt Macy ASSERT0(dn->dn_next_nlevels[i]); 620*eda14cbcSMatt Macy ASSERT0(dn->dn_next_indblkshift[i]); 621*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonuslen[i]); 622*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonustype[i]); 623*eda14cbcSMatt Macy ASSERT0(dn->dn_rm_spillblk[i]); 624*eda14cbcSMatt Macy ASSERT0(dn->dn_next_blksz[i]); 625*eda14cbcSMatt Macy ASSERT0(dn->dn_next_maxblkid[i]); 626*eda14cbcSMatt Macy ASSERT(!multilist_link_active(&dn->dn_dirty_link[i])); 627*eda14cbcSMatt Macy ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL); 628*eda14cbcSMatt Macy ASSERT3P(dn->dn_free_ranges[i], ==, NULL); 629*eda14cbcSMatt Macy } 630*eda14cbcSMatt Macy 631*eda14cbcSMatt Macy dn->dn_type = ot; 632*eda14cbcSMatt Macy dnode_setdblksz(dn, blocksize); 633*eda14cbcSMatt Macy dn->dn_indblkshift = ibs; 634*eda14cbcSMatt Macy dn->dn_nlevels = 1; 635*eda14cbcSMatt Macy dn->dn_num_slots = dn_slots; 636*eda14cbcSMatt Macy if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */ 637*eda14cbcSMatt Macy dn->dn_nblkptr = 1; 638*eda14cbcSMatt Macy else { 639*eda14cbcSMatt Macy dn->dn_nblkptr = MIN(DN_MAX_NBLKPTR, 640*eda14cbcSMatt Macy 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >> 641*eda14cbcSMatt Macy SPA_BLKPTRSHIFT)); 642*eda14cbcSMatt Macy } 643*eda14cbcSMatt Macy 644*eda14cbcSMatt Macy dn->dn_bonustype = bonustype; 645*eda14cbcSMatt Macy dn->dn_bonuslen = bonuslen; 646*eda14cbcSMatt Macy dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 647*eda14cbcSMatt Macy dn->dn_compress = ZIO_COMPRESS_INHERIT; 648*eda14cbcSMatt Macy dn->dn_dirtyctx = 0; 649*eda14cbcSMatt Macy 650*eda14cbcSMatt Macy dn->dn_free_txg = 0; 651*eda14cbcSMatt Macy dn->dn_dirtyctx_firstset = NULL; 652*eda14cbcSMatt Macy 653*eda14cbcSMatt Macy dn->dn_allocated_txg = tx->tx_txg; 654*eda14cbcSMatt Macy dn->dn_id_flags = 0; 655*eda14cbcSMatt Macy 656*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 657*eda14cbcSMatt Macy dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs; 658*eda14cbcSMatt Macy dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 659*eda14cbcSMatt Macy dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype; 660*eda14cbcSMatt Macy dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz; 661*eda14cbcSMatt Macy } 662*eda14cbcSMatt Macy 663*eda14cbcSMatt Macy void 664*eda14cbcSMatt Macy dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 665*eda14cbcSMatt Macy dmu_object_type_t bonustype, int bonuslen, int dn_slots, 666*eda14cbcSMatt Macy boolean_t keep_spill, dmu_tx_t *tx) 667*eda14cbcSMatt Macy { 668*eda14cbcSMatt Macy int nblkptr; 669*eda14cbcSMatt Macy 670*eda14cbcSMatt Macy ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 671*eda14cbcSMatt Macy ASSERT3U(blocksize, <=, 672*eda14cbcSMatt Macy spa_maxblocksize(dmu_objset_spa(dn->dn_objset))); 673*eda14cbcSMatt Macy ASSERT0(blocksize % SPA_MINBLOCKSIZE); 674*eda14cbcSMatt Macy ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 675*eda14cbcSMatt Macy ASSERT(tx->tx_txg != 0); 676*eda14cbcSMatt Macy ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 677*eda14cbcSMatt Macy (bonustype != DMU_OT_NONE && bonuslen != 0) || 678*eda14cbcSMatt Macy (bonustype == DMU_OT_SA && bonuslen == 0)); 679*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(bonustype)); 680*eda14cbcSMatt Macy ASSERT3U(bonuslen, <=, 681*eda14cbcSMatt Macy DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn->dn_objset)))); 682*eda14cbcSMatt Macy ASSERT3U(bonuslen, <=, DN_BONUS_SIZE(dn_slots << DNODE_SHIFT)); 683*eda14cbcSMatt Macy 684*eda14cbcSMatt Macy dnode_free_interior_slots(dn); 685*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_reallocate); 686*eda14cbcSMatt Macy 687*eda14cbcSMatt Macy /* clean up any unreferenced dbufs */ 688*eda14cbcSMatt Macy dnode_evict_dbufs(dn); 689*eda14cbcSMatt Macy 690*eda14cbcSMatt Macy dn->dn_id_flags = 0; 691*eda14cbcSMatt Macy 692*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 693*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 694*eda14cbcSMatt Macy if (dn->dn_datablksz != blocksize) { 695*eda14cbcSMatt Macy /* change blocksize */ 696*eda14cbcSMatt Macy ASSERT0(dn->dn_maxblkid); 697*eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || 698*eda14cbcSMatt Macy dnode_block_freed(dn, 0)); 699*eda14cbcSMatt Macy 700*eda14cbcSMatt Macy dnode_setdblksz(dn, blocksize); 701*eda14cbcSMatt Macy dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = blocksize; 702*eda14cbcSMatt Macy } 703*eda14cbcSMatt Macy if (dn->dn_bonuslen != bonuslen) 704*eda14cbcSMatt Macy dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = bonuslen; 705*eda14cbcSMatt Macy 706*eda14cbcSMatt Macy if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */ 707*eda14cbcSMatt Macy nblkptr = 1; 708*eda14cbcSMatt Macy else 709*eda14cbcSMatt Macy nblkptr = MIN(DN_MAX_NBLKPTR, 710*eda14cbcSMatt Macy 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >> 711*eda14cbcSMatt Macy SPA_BLKPTRSHIFT)); 712*eda14cbcSMatt Macy if (dn->dn_bonustype != bonustype) 713*eda14cbcSMatt Macy dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = bonustype; 714*eda14cbcSMatt Macy if (dn->dn_nblkptr != nblkptr) 715*eda14cbcSMatt Macy dn->dn_next_nblkptr[tx->tx_txg & TXG_MASK] = nblkptr; 716*eda14cbcSMatt Macy if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR && !keep_spill) { 717*eda14cbcSMatt Macy dbuf_rm_spill(dn, tx); 718*eda14cbcSMatt Macy dnode_rm_spill(dn, tx); 719*eda14cbcSMatt Macy } 720*eda14cbcSMatt Macy 721*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 722*eda14cbcSMatt Macy 723*eda14cbcSMatt Macy /* change type */ 724*eda14cbcSMatt Macy dn->dn_type = ot; 725*eda14cbcSMatt Macy 726*eda14cbcSMatt Macy /* change bonus size and type */ 727*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 728*eda14cbcSMatt Macy dn->dn_bonustype = bonustype; 729*eda14cbcSMatt Macy dn->dn_bonuslen = bonuslen; 730*eda14cbcSMatt Macy dn->dn_num_slots = dn_slots; 731*eda14cbcSMatt Macy dn->dn_nblkptr = nblkptr; 732*eda14cbcSMatt Macy dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 733*eda14cbcSMatt Macy dn->dn_compress = ZIO_COMPRESS_INHERIT; 734*eda14cbcSMatt Macy ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 735*eda14cbcSMatt Macy 736*eda14cbcSMatt Macy /* fix up the bonus db_size */ 737*eda14cbcSMatt Macy if (dn->dn_bonus) { 738*eda14cbcSMatt Macy dn->dn_bonus->db.db_size = 739*eda14cbcSMatt Macy DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) - 740*eda14cbcSMatt Macy (dn->dn_nblkptr-1) * sizeof (blkptr_t); 741*eda14cbcSMatt Macy ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size); 742*eda14cbcSMatt Macy } 743*eda14cbcSMatt Macy 744*eda14cbcSMatt Macy dn->dn_allocated_txg = tx->tx_txg; 745*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 746*eda14cbcSMatt Macy } 747*eda14cbcSMatt Macy 748*eda14cbcSMatt Macy #ifdef _KERNEL 749*eda14cbcSMatt Macy static void 750*eda14cbcSMatt Macy dnode_move_impl(dnode_t *odn, dnode_t *ndn) 751*eda14cbcSMatt Macy { 752*eda14cbcSMatt Macy int i; 753*eda14cbcSMatt Macy 754*eda14cbcSMatt Macy ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock)); 755*eda14cbcSMatt Macy ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx)); 756*eda14cbcSMatt Macy ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx)); 757*eda14cbcSMatt Macy ASSERT(!MUTEX_HELD(&odn->dn_zfetch.zf_lock)); 758*eda14cbcSMatt Macy 759*eda14cbcSMatt Macy /* Copy fields. */ 760*eda14cbcSMatt Macy ndn->dn_objset = odn->dn_objset; 761*eda14cbcSMatt Macy ndn->dn_object = odn->dn_object; 762*eda14cbcSMatt Macy ndn->dn_dbuf = odn->dn_dbuf; 763*eda14cbcSMatt Macy ndn->dn_handle = odn->dn_handle; 764*eda14cbcSMatt Macy ndn->dn_phys = odn->dn_phys; 765*eda14cbcSMatt Macy ndn->dn_type = odn->dn_type; 766*eda14cbcSMatt Macy ndn->dn_bonuslen = odn->dn_bonuslen; 767*eda14cbcSMatt Macy ndn->dn_bonustype = odn->dn_bonustype; 768*eda14cbcSMatt Macy ndn->dn_nblkptr = odn->dn_nblkptr; 769*eda14cbcSMatt Macy ndn->dn_checksum = odn->dn_checksum; 770*eda14cbcSMatt Macy ndn->dn_compress = odn->dn_compress; 771*eda14cbcSMatt Macy ndn->dn_nlevels = odn->dn_nlevels; 772*eda14cbcSMatt Macy ndn->dn_indblkshift = odn->dn_indblkshift; 773*eda14cbcSMatt Macy ndn->dn_datablkshift = odn->dn_datablkshift; 774*eda14cbcSMatt Macy ndn->dn_datablkszsec = odn->dn_datablkszsec; 775*eda14cbcSMatt Macy ndn->dn_datablksz = odn->dn_datablksz; 776*eda14cbcSMatt Macy ndn->dn_maxblkid = odn->dn_maxblkid; 777*eda14cbcSMatt Macy ndn->dn_num_slots = odn->dn_num_slots; 778*eda14cbcSMatt Macy bcopy(&odn->dn_next_type[0], &ndn->dn_next_type[0], 779*eda14cbcSMatt Macy sizeof (odn->dn_next_type)); 780*eda14cbcSMatt Macy bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0], 781*eda14cbcSMatt Macy sizeof (odn->dn_next_nblkptr)); 782*eda14cbcSMatt Macy bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0], 783*eda14cbcSMatt Macy sizeof (odn->dn_next_nlevels)); 784*eda14cbcSMatt Macy bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0], 785*eda14cbcSMatt Macy sizeof (odn->dn_next_indblkshift)); 786*eda14cbcSMatt Macy bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0], 787*eda14cbcSMatt Macy sizeof (odn->dn_next_bonustype)); 788*eda14cbcSMatt Macy bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0], 789*eda14cbcSMatt Macy sizeof (odn->dn_rm_spillblk)); 790*eda14cbcSMatt Macy bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0], 791*eda14cbcSMatt Macy sizeof (odn->dn_next_bonuslen)); 792*eda14cbcSMatt Macy bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0], 793*eda14cbcSMatt Macy sizeof (odn->dn_next_blksz)); 794*eda14cbcSMatt Macy bcopy(&odn->dn_next_maxblkid[0], &ndn->dn_next_maxblkid[0], 795*eda14cbcSMatt Macy sizeof (odn->dn_next_maxblkid)); 796*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 797*eda14cbcSMatt Macy list_move_tail(&ndn->dn_dirty_records[i], 798*eda14cbcSMatt Macy &odn->dn_dirty_records[i]); 799*eda14cbcSMatt Macy } 800*eda14cbcSMatt Macy bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0], 801*eda14cbcSMatt Macy sizeof (odn->dn_free_ranges)); 802*eda14cbcSMatt Macy ndn->dn_allocated_txg = odn->dn_allocated_txg; 803*eda14cbcSMatt Macy ndn->dn_free_txg = odn->dn_free_txg; 804*eda14cbcSMatt Macy ndn->dn_assigned_txg = odn->dn_assigned_txg; 805*eda14cbcSMatt Macy ndn->dn_dirty_txg = odn->dn_dirty_txg; 806*eda14cbcSMatt Macy ndn->dn_dirtyctx = odn->dn_dirtyctx; 807*eda14cbcSMatt Macy ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset; 808*eda14cbcSMatt Macy ASSERT(zfs_refcount_count(&odn->dn_tx_holds) == 0); 809*eda14cbcSMatt Macy zfs_refcount_transfer(&ndn->dn_holds, &odn->dn_holds); 810*eda14cbcSMatt Macy ASSERT(avl_is_empty(&ndn->dn_dbufs)); 811*eda14cbcSMatt Macy avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs); 812*eda14cbcSMatt Macy ndn->dn_dbufs_count = odn->dn_dbufs_count; 813*eda14cbcSMatt Macy ndn->dn_bonus = odn->dn_bonus; 814*eda14cbcSMatt Macy ndn->dn_have_spill = odn->dn_have_spill; 815*eda14cbcSMatt Macy ndn->dn_zio = odn->dn_zio; 816*eda14cbcSMatt Macy ndn->dn_oldused = odn->dn_oldused; 817*eda14cbcSMatt Macy ndn->dn_oldflags = odn->dn_oldflags; 818*eda14cbcSMatt Macy ndn->dn_olduid = odn->dn_olduid; 819*eda14cbcSMatt Macy ndn->dn_oldgid = odn->dn_oldgid; 820*eda14cbcSMatt Macy ndn->dn_oldprojid = odn->dn_oldprojid; 821*eda14cbcSMatt Macy ndn->dn_newuid = odn->dn_newuid; 822*eda14cbcSMatt Macy ndn->dn_newgid = odn->dn_newgid; 823*eda14cbcSMatt Macy ndn->dn_newprojid = odn->dn_newprojid; 824*eda14cbcSMatt Macy ndn->dn_id_flags = odn->dn_id_flags; 825*eda14cbcSMatt Macy dmu_zfetch_init(&ndn->dn_zfetch, NULL); 826*eda14cbcSMatt Macy list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream); 827*eda14cbcSMatt Macy ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode; 828*eda14cbcSMatt Macy 829*eda14cbcSMatt Macy /* 830*eda14cbcSMatt Macy * Update back pointers. Updating the handle fixes the back pointer of 831*eda14cbcSMatt Macy * every descendant dbuf as well as the bonus dbuf. 832*eda14cbcSMatt Macy */ 833*eda14cbcSMatt Macy ASSERT(ndn->dn_handle->dnh_dnode == odn); 834*eda14cbcSMatt Macy ndn->dn_handle->dnh_dnode = ndn; 835*eda14cbcSMatt Macy if (ndn->dn_zfetch.zf_dnode == odn) { 836*eda14cbcSMatt Macy ndn->dn_zfetch.zf_dnode = ndn; 837*eda14cbcSMatt Macy } 838*eda14cbcSMatt Macy 839*eda14cbcSMatt Macy /* 840*eda14cbcSMatt Macy * Invalidate the original dnode by clearing all of its back pointers. 841*eda14cbcSMatt Macy */ 842*eda14cbcSMatt Macy odn->dn_dbuf = NULL; 843*eda14cbcSMatt Macy odn->dn_handle = NULL; 844*eda14cbcSMatt Macy avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t), 845*eda14cbcSMatt Macy offsetof(dmu_buf_impl_t, db_link)); 846*eda14cbcSMatt Macy odn->dn_dbufs_count = 0; 847*eda14cbcSMatt Macy odn->dn_bonus = NULL; 848*eda14cbcSMatt Macy dmu_zfetch_fini(&odn->dn_zfetch); 849*eda14cbcSMatt Macy 850*eda14cbcSMatt Macy /* 851*eda14cbcSMatt Macy * Set the low bit of the objset pointer to ensure that dnode_move() 852*eda14cbcSMatt Macy * recognizes the dnode as invalid in any subsequent callback. 853*eda14cbcSMatt Macy */ 854*eda14cbcSMatt Macy POINTER_INVALIDATE(&odn->dn_objset); 855*eda14cbcSMatt Macy 856*eda14cbcSMatt Macy /* 857*eda14cbcSMatt Macy * Satisfy the destructor. 858*eda14cbcSMatt Macy */ 859*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 860*eda14cbcSMatt Macy list_create(&odn->dn_dirty_records[i], 861*eda14cbcSMatt Macy sizeof (dbuf_dirty_record_t), 862*eda14cbcSMatt Macy offsetof(dbuf_dirty_record_t, dr_dirty_node)); 863*eda14cbcSMatt Macy odn->dn_free_ranges[i] = NULL; 864*eda14cbcSMatt Macy odn->dn_next_nlevels[i] = 0; 865*eda14cbcSMatt Macy odn->dn_next_indblkshift[i] = 0; 866*eda14cbcSMatt Macy odn->dn_next_bonustype[i] = 0; 867*eda14cbcSMatt Macy odn->dn_rm_spillblk[i] = 0; 868*eda14cbcSMatt Macy odn->dn_next_bonuslen[i] = 0; 869*eda14cbcSMatt Macy odn->dn_next_blksz[i] = 0; 870*eda14cbcSMatt Macy } 871*eda14cbcSMatt Macy odn->dn_allocated_txg = 0; 872*eda14cbcSMatt Macy odn->dn_free_txg = 0; 873*eda14cbcSMatt Macy odn->dn_assigned_txg = 0; 874*eda14cbcSMatt Macy odn->dn_dirty_txg = 0; 875*eda14cbcSMatt Macy odn->dn_dirtyctx = 0; 876*eda14cbcSMatt Macy odn->dn_dirtyctx_firstset = NULL; 877*eda14cbcSMatt Macy odn->dn_have_spill = B_FALSE; 878*eda14cbcSMatt Macy odn->dn_zio = NULL; 879*eda14cbcSMatt Macy odn->dn_oldused = 0; 880*eda14cbcSMatt Macy odn->dn_oldflags = 0; 881*eda14cbcSMatt Macy odn->dn_olduid = 0; 882*eda14cbcSMatt Macy odn->dn_oldgid = 0; 883*eda14cbcSMatt Macy odn->dn_oldprojid = ZFS_DEFAULT_PROJID; 884*eda14cbcSMatt Macy odn->dn_newuid = 0; 885*eda14cbcSMatt Macy odn->dn_newgid = 0; 886*eda14cbcSMatt Macy odn->dn_newprojid = ZFS_DEFAULT_PROJID; 887*eda14cbcSMatt Macy odn->dn_id_flags = 0; 888*eda14cbcSMatt Macy 889*eda14cbcSMatt Macy /* 890*eda14cbcSMatt Macy * Mark the dnode. 891*eda14cbcSMatt Macy */ 892*eda14cbcSMatt Macy ndn->dn_moved = 1; 893*eda14cbcSMatt Macy odn->dn_moved = (uint8_t)-1; 894*eda14cbcSMatt Macy } 895*eda14cbcSMatt Macy 896*eda14cbcSMatt Macy /*ARGSUSED*/ 897*eda14cbcSMatt Macy static kmem_cbrc_t 898*eda14cbcSMatt Macy dnode_move(void *buf, void *newbuf, size_t size, void *arg) 899*eda14cbcSMatt Macy { 900*eda14cbcSMatt Macy dnode_t *odn = buf, *ndn = newbuf; 901*eda14cbcSMatt Macy objset_t *os; 902*eda14cbcSMatt Macy int64_t refcount; 903*eda14cbcSMatt Macy uint32_t dbufs; 904*eda14cbcSMatt Macy 905*eda14cbcSMatt Macy /* 906*eda14cbcSMatt Macy * The dnode is on the objset's list of known dnodes if the objset 907*eda14cbcSMatt Macy * pointer is valid. We set the low bit of the objset pointer when 908*eda14cbcSMatt Macy * freeing the dnode to invalidate it, and the memory patterns written 909*eda14cbcSMatt Macy * by kmem (baddcafe and deadbeef) set at least one of the two low bits. 910*eda14cbcSMatt Macy * A newly created dnode sets the objset pointer last of all to indicate 911*eda14cbcSMatt Macy * that the dnode is known and in a valid state to be moved by this 912*eda14cbcSMatt Macy * function. 913*eda14cbcSMatt Macy */ 914*eda14cbcSMatt Macy os = odn->dn_objset; 915*eda14cbcSMatt Macy if (!POINTER_IS_VALID(os)) { 916*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_invalid); 917*eda14cbcSMatt Macy return (KMEM_CBRC_DONT_KNOW); 918*eda14cbcSMatt Macy } 919*eda14cbcSMatt Macy 920*eda14cbcSMatt Macy /* 921*eda14cbcSMatt Macy * Ensure that the objset does not go away during the move. 922*eda14cbcSMatt Macy */ 923*eda14cbcSMatt Macy rw_enter(&os_lock, RW_WRITER); 924*eda14cbcSMatt Macy if (os != odn->dn_objset) { 925*eda14cbcSMatt Macy rw_exit(&os_lock); 926*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_recheck1); 927*eda14cbcSMatt Macy return (KMEM_CBRC_DONT_KNOW); 928*eda14cbcSMatt Macy } 929*eda14cbcSMatt Macy 930*eda14cbcSMatt Macy /* 931*eda14cbcSMatt Macy * If the dnode is still valid, then so is the objset. We know that no 932*eda14cbcSMatt Macy * valid objset can be freed while we hold os_lock, so we can safely 933*eda14cbcSMatt Macy * ensure that the objset remains in use. 934*eda14cbcSMatt Macy */ 935*eda14cbcSMatt Macy mutex_enter(&os->os_lock); 936*eda14cbcSMatt Macy 937*eda14cbcSMatt Macy /* 938*eda14cbcSMatt Macy * Recheck the objset pointer in case the dnode was removed just before 939*eda14cbcSMatt Macy * acquiring the lock. 940*eda14cbcSMatt Macy */ 941*eda14cbcSMatt Macy if (os != odn->dn_objset) { 942*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 943*eda14cbcSMatt Macy rw_exit(&os_lock); 944*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_recheck2); 945*eda14cbcSMatt Macy return (KMEM_CBRC_DONT_KNOW); 946*eda14cbcSMatt Macy } 947*eda14cbcSMatt Macy 948*eda14cbcSMatt Macy /* 949*eda14cbcSMatt Macy * At this point we know that as long as we hold os->os_lock, the dnode 950*eda14cbcSMatt Macy * cannot be freed and fields within the dnode can be safely accessed. 951*eda14cbcSMatt Macy * The objset listing this dnode cannot go away as long as this dnode is 952*eda14cbcSMatt Macy * on its list. 953*eda14cbcSMatt Macy */ 954*eda14cbcSMatt Macy rw_exit(&os_lock); 955*eda14cbcSMatt Macy if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) { 956*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 957*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_special); 958*eda14cbcSMatt Macy return (KMEM_CBRC_NO); 959*eda14cbcSMatt Macy } 960*eda14cbcSMatt Macy ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */ 961*eda14cbcSMatt Macy 962*eda14cbcSMatt Macy /* 963*eda14cbcSMatt Macy * Lock the dnode handle to prevent the dnode from obtaining any new 964*eda14cbcSMatt Macy * holds. This also prevents the descendant dbufs and the bonus dbuf 965*eda14cbcSMatt Macy * from accessing the dnode, so that we can discount their holds. The 966*eda14cbcSMatt Macy * handle is safe to access because we know that while the dnode cannot 967*eda14cbcSMatt Macy * go away, neither can its handle. Once we hold dnh_zrlock, we can 968*eda14cbcSMatt Macy * safely move any dnode referenced only by dbufs. 969*eda14cbcSMatt Macy */ 970*eda14cbcSMatt Macy if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) { 971*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 972*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_handle); 973*eda14cbcSMatt Macy return (KMEM_CBRC_LATER); 974*eda14cbcSMatt Macy } 975*eda14cbcSMatt Macy 976*eda14cbcSMatt Macy /* 977*eda14cbcSMatt Macy * Ensure a consistent view of the dnode's holds and the dnode's dbufs. 978*eda14cbcSMatt Macy * We need to guarantee that there is a hold for every dbuf in order to 979*eda14cbcSMatt Macy * determine whether the dnode is actively referenced. Falsely matching 980*eda14cbcSMatt Macy * a dbuf to an active hold would lead to an unsafe move. It's possible 981*eda14cbcSMatt Macy * that a thread already having an active dnode hold is about to add a 982*eda14cbcSMatt Macy * dbuf, and we can't compare hold and dbuf counts while the add is in 983*eda14cbcSMatt Macy * progress. 984*eda14cbcSMatt Macy */ 985*eda14cbcSMatt Macy if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) { 986*eda14cbcSMatt Macy zrl_exit(&odn->dn_handle->dnh_zrlock); 987*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 988*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_rwlock); 989*eda14cbcSMatt Macy return (KMEM_CBRC_LATER); 990*eda14cbcSMatt Macy } 991*eda14cbcSMatt Macy 992*eda14cbcSMatt Macy /* 993*eda14cbcSMatt Macy * A dbuf may be removed (evicted) without an active dnode hold. In that 994*eda14cbcSMatt Macy * case, the dbuf count is decremented under the handle lock before the 995*eda14cbcSMatt Macy * dbuf's hold is released. This order ensures that if we count the hold 996*eda14cbcSMatt Macy * after the dbuf is removed but before its hold is released, we will 997*eda14cbcSMatt Macy * treat the unmatched hold as active and exit safely. If we count the 998*eda14cbcSMatt Macy * hold before the dbuf is removed, the hold is discounted, and the 999*eda14cbcSMatt Macy * removal is blocked until the move completes. 1000*eda14cbcSMatt Macy */ 1001*eda14cbcSMatt Macy refcount = zfs_refcount_count(&odn->dn_holds); 1002*eda14cbcSMatt Macy ASSERT(refcount >= 0); 1003*eda14cbcSMatt Macy dbufs = DN_DBUFS_COUNT(odn); 1004*eda14cbcSMatt Macy 1005*eda14cbcSMatt Macy /* We can't have more dbufs than dnode holds. */ 1006*eda14cbcSMatt Macy ASSERT3U(dbufs, <=, refcount); 1007*eda14cbcSMatt Macy DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount, 1008*eda14cbcSMatt Macy uint32_t, dbufs); 1009*eda14cbcSMatt Macy 1010*eda14cbcSMatt Macy if (refcount > dbufs) { 1011*eda14cbcSMatt Macy rw_exit(&odn->dn_struct_rwlock); 1012*eda14cbcSMatt Macy zrl_exit(&odn->dn_handle->dnh_zrlock); 1013*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 1014*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_move_active); 1015*eda14cbcSMatt Macy return (KMEM_CBRC_LATER); 1016*eda14cbcSMatt Macy } 1017*eda14cbcSMatt Macy 1018*eda14cbcSMatt Macy rw_exit(&odn->dn_struct_rwlock); 1019*eda14cbcSMatt Macy 1020*eda14cbcSMatt Macy /* 1021*eda14cbcSMatt Macy * At this point we know that anyone with a hold on the dnode is not 1022*eda14cbcSMatt Macy * actively referencing it. The dnode is known and in a valid state to 1023*eda14cbcSMatt Macy * move. We're holding the locks needed to execute the critical section. 1024*eda14cbcSMatt Macy */ 1025*eda14cbcSMatt Macy dnode_move_impl(odn, ndn); 1026*eda14cbcSMatt Macy 1027*eda14cbcSMatt Macy list_link_replace(&odn->dn_link, &ndn->dn_link); 1028*eda14cbcSMatt Macy /* If the dnode was safe to move, the refcount cannot have changed. */ 1029*eda14cbcSMatt Macy ASSERT(refcount == zfs_refcount_count(&ndn->dn_holds)); 1030*eda14cbcSMatt Macy ASSERT(dbufs == DN_DBUFS_COUNT(ndn)); 1031*eda14cbcSMatt Macy zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */ 1032*eda14cbcSMatt Macy mutex_exit(&os->os_lock); 1033*eda14cbcSMatt Macy 1034*eda14cbcSMatt Macy return (KMEM_CBRC_YES); 1035*eda14cbcSMatt Macy } 1036*eda14cbcSMatt Macy #endif /* _KERNEL */ 1037*eda14cbcSMatt Macy 1038*eda14cbcSMatt Macy static void 1039*eda14cbcSMatt Macy dnode_slots_hold(dnode_children_t *children, int idx, int slots) 1040*eda14cbcSMatt Macy { 1041*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1042*eda14cbcSMatt Macy 1043*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1044*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1045*eda14cbcSMatt Macy zrl_add(&dnh->dnh_zrlock); 1046*eda14cbcSMatt Macy } 1047*eda14cbcSMatt Macy } 1048*eda14cbcSMatt Macy 1049*eda14cbcSMatt Macy static void 1050*eda14cbcSMatt Macy dnode_slots_rele(dnode_children_t *children, int idx, int slots) 1051*eda14cbcSMatt Macy { 1052*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1053*eda14cbcSMatt Macy 1054*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1055*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1056*eda14cbcSMatt Macy 1057*eda14cbcSMatt Macy if (zrl_is_locked(&dnh->dnh_zrlock)) 1058*eda14cbcSMatt Macy zrl_exit(&dnh->dnh_zrlock); 1059*eda14cbcSMatt Macy else 1060*eda14cbcSMatt Macy zrl_remove(&dnh->dnh_zrlock); 1061*eda14cbcSMatt Macy } 1062*eda14cbcSMatt Macy } 1063*eda14cbcSMatt Macy 1064*eda14cbcSMatt Macy static int 1065*eda14cbcSMatt Macy dnode_slots_tryenter(dnode_children_t *children, int idx, int slots) 1066*eda14cbcSMatt Macy { 1067*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1068*eda14cbcSMatt Macy 1069*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1070*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1071*eda14cbcSMatt Macy 1072*eda14cbcSMatt Macy if (!zrl_tryenter(&dnh->dnh_zrlock)) { 1073*eda14cbcSMatt Macy for (int j = idx; j < i; j++) { 1074*eda14cbcSMatt Macy dnh = &children->dnc_children[j]; 1075*eda14cbcSMatt Macy zrl_exit(&dnh->dnh_zrlock); 1076*eda14cbcSMatt Macy } 1077*eda14cbcSMatt Macy 1078*eda14cbcSMatt Macy return (0); 1079*eda14cbcSMatt Macy } 1080*eda14cbcSMatt Macy } 1081*eda14cbcSMatt Macy 1082*eda14cbcSMatt Macy return (1); 1083*eda14cbcSMatt Macy } 1084*eda14cbcSMatt Macy 1085*eda14cbcSMatt Macy static void 1086*eda14cbcSMatt Macy dnode_set_slots(dnode_children_t *children, int idx, int slots, void *ptr) 1087*eda14cbcSMatt Macy { 1088*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1089*eda14cbcSMatt Macy 1090*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1091*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1092*eda14cbcSMatt Macy dnh->dnh_dnode = ptr; 1093*eda14cbcSMatt Macy } 1094*eda14cbcSMatt Macy } 1095*eda14cbcSMatt Macy 1096*eda14cbcSMatt Macy static boolean_t 1097*eda14cbcSMatt Macy dnode_check_slots_free(dnode_children_t *children, int idx, int slots) 1098*eda14cbcSMatt Macy { 1099*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1100*eda14cbcSMatt Macy 1101*eda14cbcSMatt Macy /* 1102*eda14cbcSMatt Macy * If all dnode slots are either already free or 1103*eda14cbcSMatt Macy * evictable return B_TRUE. 1104*eda14cbcSMatt Macy */ 1105*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1106*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1107*eda14cbcSMatt Macy dnode_t *dn = dnh->dnh_dnode; 1108*eda14cbcSMatt Macy 1109*eda14cbcSMatt Macy if (dn == DN_SLOT_FREE) { 1110*eda14cbcSMatt Macy continue; 1111*eda14cbcSMatt Macy } else if (DN_SLOT_IS_PTR(dn)) { 1112*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1113*eda14cbcSMatt Macy boolean_t can_free = (dn->dn_type == DMU_OT_NONE && 1114*eda14cbcSMatt Macy zfs_refcount_is_zero(&dn->dn_holds) && 1115*eda14cbcSMatt Macy !DNODE_IS_DIRTY(dn)); 1116*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1117*eda14cbcSMatt Macy 1118*eda14cbcSMatt Macy if (!can_free) 1119*eda14cbcSMatt Macy return (B_FALSE); 1120*eda14cbcSMatt Macy else 1121*eda14cbcSMatt Macy continue; 1122*eda14cbcSMatt Macy } else { 1123*eda14cbcSMatt Macy return (B_FALSE); 1124*eda14cbcSMatt Macy } 1125*eda14cbcSMatt Macy } 1126*eda14cbcSMatt Macy 1127*eda14cbcSMatt Macy return (B_TRUE); 1128*eda14cbcSMatt Macy } 1129*eda14cbcSMatt Macy 1130*eda14cbcSMatt Macy static void 1131*eda14cbcSMatt Macy dnode_reclaim_slots(dnode_children_t *children, int idx, int slots) 1132*eda14cbcSMatt Macy { 1133*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1134*eda14cbcSMatt Macy 1135*eda14cbcSMatt Macy for (int i = idx; i < idx + slots; i++) { 1136*eda14cbcSMatt Macy dnode_handle_t *dnh = &children->dnc_children[i]; 1137*eda14cbcSMatt Macy 1138*eda14cbcSMatt Macy ASSERT(zrl_is_locked(&dnh->dnh_zrlock)); 1139*eda14cbcSMatt Macy 1140*eda14cbcSMatt Macy if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) { 1141*eda14cbcSMatt Macy ASSERT3S(dnh->dnh_dnode->dn_type, ==, DMU_OT_NONE); 1142*eda14cbcSMatt Macy dnode_destroy(dnh->dnh_dnode); 1143*eda14cbcSMatt Macy dnh->dnh_dnode = DN_SLOT_FREE; 1144*eda14cbcSMatt Macy } 1145*eda14cbcSMatt Macy } 1146*eda14cbcSMatt Macy } 1147*eda14cbcSMatt Macy 1148*eda14cbcSMatt Macy void 1149*eda14cbcSMatt Macy dnode_free_interior_slots(dnode_t *dn) 1150*eda14cbcSMatt Macy { 1151*eda14cbcSMatt Macy dnode_children_t *children = dmu_buf_get_user(&dn->dn_dbuf->db); 1152*eda14cbcSMatt Macy int epb = dn->dn_dbuf->db.db_size >> DNODE_SHIFT; 1153*eda14cbcSMatt Macy int idx = (dn->dn_object & (epb - 1)) + 1; 1154*eda14cbcSMatt Macy int slots = dn->dn_num_slots - 1; 1155*eda14cbcSMatt Macy 1156*eda14cbcSMatt Macy if (slots == 0) 1157*eda14cbcSMatt Macy return; 1158*eda14cbcSMatt Macy 1159*eda14cbcSMatt Macy ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK); 1160*eda14cbcSMatt Macy 1161*eda14cbcSMatt Macy while (!dnode_slots_tryenter(children, idx, slots)) { 1162*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_free_interior_lock_retry); 1163*eda14cbcSMatt Macy cond_resched(); 1164*eda14cbcSMatt Macy } 1165*eda14cbcSMatt Macy 1166*eda14cbcSMatt Macy dnode_set_slots(children, idx, slots, DN_SLOT_FREE); 1167*eda14cbcSMatt Macy dnode_slots_rele(children, idx, slots); 1168*eda14cbcSMatt Macy } 1169*eda14cbcSMatt Macy 1170*eda14cbcSMatt Macy void 1171*eda14cbcSMatt Macy dnode_special_close(dnode_handle_t *dnh) 1172*eda14cbcSMatt Macy { 1173*eda14cbcSMatt Macy dnode_t *dn = dnh->dnh_dnode; 1174*eda14cbcSMatt Macy 1175*eda14cbcSMatt Macy /* 1176*eda14cbcSMatt Macy * Ensure dnode_rele_and_unlock() has released dn_mtx, after final 1177*eda14cbcSMatt Macy * zfs_refcount_remove() 1178*eda14cbcSMatt Macy */ 1179*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1180*eda14cbcSMatt Macy if (zfs_refcount_count(&dn->dn_holds) > 0) 1181*eda14cbcSMatt Macy cv_wait(&dn->dn_nodnholds, &dn->dn_mtx); 1182*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1183*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 0); 1184*eda14cbcSMatt Macy 1185*eda14cbcSMatt Macy ASSERT(dn->dn_dbuf == NULL || 1186*eda14cbcSMatt Macy dmu_buf_get_user(&dn->dn_dbuf->db) == NULL); 1187*eda14cbcSMatt Macy zrl_add(&dnh->dnh_zrlock); 1188*eda14cbcSMatt Macy dnode_destroy(dn); /* implicit zrl_remove() */ 1189*eda14cbcSMatt Macy zrl_destroy(&dnh->dnh_zrlock); 1190*eda14cbcSMatt Macy dnh->dnh_dnode = NULL; 1191*eda14cbcSMatt Macy } 1192*eda14cbcSMatt Macy 1193*eda14cbcSMatt Macy void 1194*eda14cbcSMatt Macy dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object, 1195*eda14cbcSMatt Macy dnode_handle_t *dnh) 1196*eda14cbcSMatt Macy { 1197*eda14cbcSMatt Macy dnode_t *dn; 1198*eda14cbcSMatt Macy 1199*eda14cbcSMatt Macy zrl_init(&dnh->dnh_zrlock); 1200*eda14cbcSMatt Macy zrl_tryenter(&dnh->dnh_zrlock); 1201*eda14cbcSMatt Macy 1202*eda14cbcSMatt Macy dn = dnode_create(os, dnp, NULL, object, dnh); 1203*eda14cbcSMatt Macy DNODE_VERIFY(dn); 1204*eda14cbcSMatt Macy 1205*eda14cbcSMatt Macy zrl_exit(&dnh->dnh_zrlock); 1206*eda14cbcSMatt Macy } 1207*eda14cbcSMatt Macy 1208*eda14cbcSMatt Macy static void 1209*eda14cbcSMatt Macy dnode_buf_evict_async(void *dbu) 1210*eda14cbcSMatt Macy { 1211*eda14cbcSMatt Macy dnode_children_t *dnc = dbu; 1212*eda14cbcSMatt Macy 1213*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_buf_evict); 1214*eda14cbcSMatt Macy 1215*eda14cbcSMatt Macy for (int i = 0; i < dnc->dnc_count; i++) { 1216*eda14cbcSMatt Macy dnode_handle_t *dnh = &dnc->dnc_children[i]; 1217*eda14cbcSMatt Macy dnode_t *dn; 1218*eda14cbcSMatt Macy 1219*eda14cbcSMatt Macy /* 1220*eda14cbcSMatt Macy * The dnode handle lock guards against the dnode moving to 1221*eda14cbcSMatt Macy * another valid address, so there is no need here to guard 1222*eda14cbcSMatt Macy * against changes to or from NULL. 1223*eda14cbcSMatt Macy */ 1224*eda14cbcSMatt Macy if (!DN_SLOT_IS_PTR(dnh->dnh_dnode)) { 1225*eda14cbcSMatt Macy zrl_destroy(&dnh->dnh_zrlock); 1226*eda14cbcSMatt Macy dnh->dnh_dnode = DN_SLOT_UNINIT; 1227*eda14cbcSMatt Macy continue; 1228*eda14cbcSMatt Macy } 1229*eda14cbcSMatt Macy 1230*eda14cbcSMatt Macy zrl_add(&dnh->dnh_zrlock); 1231*eda14cbcSMatt Macy dn = dnh->dnh_dnode; 1232*eda14cbcSMatt Macy /* 1233*eda14cbcSMatt Macy * If there are holds on this dnode, then there should 1234*eda14cbcSMatt Macy * be holds on the dnode's containing dbuf as well; thus 1235*eda14cbcSMatt Macy * it wouldn't be eligible for eviction and this function 1236*eda14cbcSMatt Macy * would not have been called. 1237*eda14cbcSMatt Macy */ 1238*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&dn->dn_holds)); 1239*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&dn->dn_tx_holds)); 1240*eda14cbcSMatt Macy 1241*eda14cbcSMatt Macy dnode_destroy(dn); /* implicit zrl_remove() for first slot */ 1242*eda14cbcSMatt Macy zrl_destroy(&dnh->dnh_zrlock); 1243*eda14cbcSMatt Macy dnh->dnh_dnode = DN_SLOT_UNINIT; 1244*eda14cbcSMatt Macy } 1245*eda14cbcSMatt Macy kmem_free(dnc, sizeof (dnode_children_t) + 1246*eda14cbcSMatt Macy dnc->dnc_count * sizeof (dnode_handle_t)); 1247*eda14cbcSMatt Macy } 1248*eda14cbcSMatt Macy 1249*eda14cbcSMatt Macy /* 1250*eda14cbcSMatt Macy * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used 1251*eda14cbcSMatt Macy * to ensure the hole at the specified object offset is large enough to 1252*eda14cbcSMatt Macy * hold the dnode being created. The slots parameter is also used to ensure 1253*eda14cbcSMatt Macy * a dnode does not span multiple dnode blocks. In both of these cases, if 1254*eda14cbcSMatt Macy * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases 1255*eda14cbcSMatt Macy * are only possible when using DNODE_MUST_BE_FREE. 1256*eda14cbcSMatt Macy * 1257*eda14cbcSMatt Macy * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0. 1258*eda14cbcSMatt Macy * dnode_hold_impl() will check if the requested dnode is already consumed 1259*eda14cbcSMatt Macy * as an extra dnode slot by an large dnode, in which case it returns 1260*eda14cbcSMatt Macy * ENOENT. 1261*eda14cbcSMatt Macy * 1262*eda14cbcSMatt Macy * If the DNODE_DRY_RUN flag is set, we don't actually hold the dnode, just 1263*eda14cbcSMatt Macy * return whether the hold would succeed or not. tag and dnp should set to 1264*eda14cbcSMatt Macy * NULL in this case. 1265*eda14cbcSMatt Macy * 1266*eda14cbcSMatt Macy * errors: 1267*eda14cbcSMatt Macy * EINVAL - Invalid object number or flags. 1268*eda14cbcSMatt Macy * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE) 1269*eda14cbcSMatt Macy * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE) 1270*eda14cbcSMatt Macy * - Refers to a freeing dnode (DNODE_MUST_BE_FREE) 1271*eda14cbcSMatt Macy * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED) 1272*eda14cbcSMatt Macy * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED) 1273*eda14cbcSMatt Macy * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED) 1274*eda14cbcSMatt Macy * EIO - I/O error when reading the meta dnode dbuf. 1275*eda14cbcSMatt Macy * 1276*eda14cbcSMatt Macy * succeeds even for free dnodes. 1277*eda14cbcSMatt Macy */ 1278*eda14cbcSMatt Macy int 1279*eda14cbcSMatt Macy dnode_hold_impl(objset_t *os, uint64_t object, int flag, int slots, 1280*eda14cbcSMatt Macy void *tag, dnode_t **dnp) 1281*eda14cbcSMatt Macy { 1282*eda14cbcSMatt Macy int epb, idx, err; 1283*eda14cbcSMatt Macy int drop_struct_lock = FALSE; 1284*eda14cbcSMatt Macy int type; 1285*eda14cbcSMatt Macy uint64_t blk; 1286*eda14cbcSMatt Macy dnode_t *mdn, *dn; 1287*eda14cbcSMatt Macy dmu_buf_impl_t *db; 1288*eda14cbcSMatt Macy dnode_children_t *dnc; 1289*eda14cbcSMatt Macy dnode_phys_t *dn_block; 1290*eda14cbcSMatt Macy dnode_handle_t *dnh; 1291*eda14cbcSMatt Macy 1292*eda14cbcSMatt Macy ASSERT(!(flag & DNODE_MUST_BE_ALLOCATED) || (slots == 0)); 1293*eda14cbcSMatt Macy ASSERT(!(flag & DNODE_MUST_BE_FREE) || (slots > 0)); 1294*eda14cbcSMatt Macy IMPLY(flag & DNODE_DRY_RUN, (tag == NULL) && (dnp == NULL)); 1295*eda14cbcSMatt Macy 1296*eda14cbcSMatt Macy /* 1297*eda14cbcSMatt Macy * If you are holding the spa config lock as writer, you shouldn't 1298*eda14cbcSMatt Macy * be asking the DMU to do *anything* unless it's the root pool 1299*eda14cbcSMatt Macy * which may require us to read from the root filesystem while 1300*eda14cbcSMatt Macy * holding some (not all) of the locks as writer. 1301*eda14cbcSMatt Macy */ 1302*eda14cbcSMatt Macy ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 || 1303*eda14cbcSMatt Macy (spa_is_root(os->os_spa) && 1304*eda14cbcSMatt Macy spa_config_held(os->os_spa, SCL_STATE, RW_WRITER))); 1305*eda14cbcSMatt Macy 1306*eda14cbcSMatt Macy ASSERT((flag & DNODE_MUST_BE_ALLOCATED) || (flag & DNODE_MUST_BE_FREE)); 1307*eda14cbcSMatt Macy 1308*eda14cbcSMatt Macy if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT || 1309*eda14cbcSMatt Macy object == DMU_PROJECTUSED_OBJECT) { 1310*eda14cbcSMatt Macy if (object == DMU_USERUSED_OBJECT) 1311*eda14cbcSMatt Macy dn = DMU_USERUSED_DNODE(os); 1312*eda14cbcSMatt Macy else if (object == DMU_GROUPUSED_OBJECT) 1313*eda14cbcSMatt Macy dn = DMU_GROUPUSED_DNODE(os); 1314*eda14cbcSMatt Macy else 1315*eda14cbcSMatt Macy dn = DMU_PROJECTUSED_DNODE(os); 1316*eda14cbcSMatt Macy if (dn == NULL) 1317*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 1318*eda14cbcSMatt Macy type = dn->dn_type; 1319*eda14cbcSMatt Macy if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) 1320*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 1321*eda14cbcSMatt Macy if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE) 1322*eda14cbcSMatt Macy return (SET_ERROR(EEXIST)); 1323*eda14cbcSMatt Macy DNODE_VERIFY(dn); 1324*eda14cbcSMatt Macy /* Don't actually hold if dry run, just return 0 */ 1325*eda14cbcSMatt Macy if (!(flag & DNODE_DRY_RUN)) { 1326*eda14cbcSMatt Macy (void) zfs_refcount_add(&dn->dn_holds, tag); 1327*eda14cbcSMatt Macy *dnp = dn; 1328*eda14cbcSMatt Macy } 1329*eda14cbcSMatt Macy return (0); 1330*eda14cbcSMatt Macy } 1331*eda14cbcSMatt Macy 1332*eda14cbcSMatt Macy if (object == 0 || object >= DN_MAX_OBJECT) 1333*eda14cbcSMatt Macy return (SET_ERROR(EINVAL)); 1334*eda14cbcSMatt Macy 1335*eda14cbcSMatt Macy mdn = DMU_META_DNODE(os); 1336*eda14cbcSMatt Macy ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT); 1337*eda14cbcSMatt Macy 1338*eda14cbcSMatt Macy DNODE_VERIFY(mdn); 1339*eda14cbcSMatt Macy 1340*eda14cbcSMatt Macy if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 1341*eda14cbcSMatt Macy rw_enter(&mdn->dn_struct_rwlock, RW_READER); 1342*eda14cbcSMatt Macy drop_struct_lock = TRUE; 1343*eda14cbcSMatt Macy } 1344*eda14cbcSMatt Macy 1345*eda14cbcSMatt Macy blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t)); 1346*eda14cbcSMatt Macy db = dbuf_hold(mdn, blk, FTAG); 1347*eda14cbcSMatt Macy if (drop_struct_lock) 1348*eda14cbcSMatt Macy rw_exit(&mdn->dn_struct_rwlock); 1349*eda14cbcSMatt Macy if (db == NULL) { 1350*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_dbuf_hold); 1351*eda14cbcSMatt Macy return (SET_ERROR(EIO)); 1352*eda14cbcSMatt Macy } 1353*eda14cbcSMatt Macy 1354*eda14cbcSMatt Macy /* 1355*eda14cbcSMatt Macy * We do not need to decrypt to read the dnode so it doesn't matter 1356*eda14cbcSMatt Macy * if we get the encrypted or decrypted version. 1357*eda14cbcSMatt Macy */ 1358*eda14cbcSMatt Macy err = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_NO_DECRYPT); 1359*eda14cbcSMatt Macy if (err) { 1360*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_dbuf_read); 1361*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1362*eda14cbcSMatt Macy return (err); 1363*eda14cbcSMatt Macy } 1364*eda14cbcSMatt Macy 1365*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 1366*eda14cbcSMatt Macy epb = db->db.db_size >> DNODE_SHIFT; 1367*eda14cbcSMatt Macy 1368*eda14cbcSMatt Macy idx = object & (epb - 1); 1369*eda14cbcSMatt Macy dn_block = (dnode_phys_t *)db->db.db_data; 1370*eda14cbcSMatt Macy 1371*eda14cbcSMatt Macy ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE); 1372*eda14cbcSMatt Macy dnc = dmu_buf_get_user(&db->db); 1373*eda14cbcSMatt Macy dnh = NULL; 1374*eda14cbcSMatt Macy if (dnc == NULL) { 1375*eda14cbcSMatt Macy dnode_children_t *winner; 1376*eda14cbcSMatt Macy int skip = 0; 1377*eda14cbcSMatt Macy 1378*eda14cbcSMatt Macy dnc = kmem_zalloc(sizeof (dnode_children_t) + 1379*eda14cbcSMatt Macy epb * sizeof (dnode_handle_t), KM_SLEEP); 1380*eda14cbcSMatt Macy dnc->dnc_count = epb; 1381*eda14cbcSMatt Macy dnh = &dnc->dnc_children[0]; 1382*eda14cbcSMatt Macy 1383*eda14cbcSMatt Macy /* Initialize dnode slot status from dnode_phys_t */ 1384*eda14cbcSMatt Macy for (int i = 0; i < epb; i++) { 1385*eda14cbcSMatt Macy zrl_init(&dnh[i].dnh_zrlock); 1386*eda14cbcSMatt Macy 1387*eda14cbcSMatt Macy if (skip) { 1388*eda14cbcSMatt Macy skip--; 1389*eda14cbcSMatt Macy continue; 1390*eda14cbcSMatt Macy } 1391*eda14cbcSMatt Macy 1392*eda14cbcSMatt Macy if (dn_block[i].dn_type != DMU_OT_NONE) { 1393*eda14cbcSMatt Macy int interior = dn_block[i].dn_extra_slots; 1394*eda14cbcSMatt Macy 1395*eda14cbcSMatt Macy dnode_set_slots(dnc, i, 1, DN_SLOT_ALLOCATED); 1396*eda14cbcSMatt Macy dnode_set_slots(dnc, i + 1, interior, 1397*eda14cbcSMatt Macy DN_SLOT_INTERIOR); 1398*eda14cbcSMatt Macy skip = interior; 1399*eda14cbcSMatt Macy } else { 1400*eda14cbcSMatt Macy dnh[i].dnh_dnode = DN_SLOT_FREE; 1401*eda14cbcSMatt Macy skip = 0; 1402*eda14cbcSMatt Macy } 1403*eda14cbcSMatt Macy } 1404*eda14cbcSMatt Macy 1405*eda14cbcSMatt Macy dmu_buf_init_user(&dnc->dnc_dbu, NULL, 1406*eda14cbcSMatt Macy dnode_buf_evict_async, NULL); 1407*eda14cbcSMatt Macy winner = dmu_buf_set_user(&db->db, &dnc->dnc_dbu); 1408*eda14cbcSMatt Macy if (winner != NULL) { 1409*eda14cbcSMatt Macy 1410*eda14cbcSMatt Macy for (int i = 0; i < epb; i++) 1411*eda14cbcSMatt Macy zrl_destroy(&dnh[i].dnh_zrlock); 1412*eda14cbcSMatt Macy 1413*eda14cbcSMatt Macy kmem_free(dnc, sizeof (dnode_children_t) + 1414*eda14cbcSMatt Macy epb * sizeof (dnode_handle_t)); 1415*eda14cbcSMatt Macy dnc = winner; 1416*eda14cbcSMatt Macy } 1417*eda14cbcSMatt Macy } 1418*eda14cbcSMatt Macy 1419*eda14cbcSMatt Macy ASSERT(dnc->dnc_count == epb); 1420*eda14cbcSMatt Macy 1421*eda14cbcSMatt Macy if (flag & DNODE_MUST_BE_ALLOCATED) { 1422*eda14cbcSMatt Macy slots = 1; 1423*eda14cbcSMatt Macy 1424*eda14cbcSMatt Macy dnode_slots_hold(dnc, idx, slots); 1425*eda14cbcSMatt Macy dnh = &dnc->dnc_children[idx]; 1426*eda14cbcSMatt Macy 1427*eda14cbcSMatt Macy if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) { 1428*eda14cbcSMatt Macy dn = dnh->dnh_dnode; 1429*eda14cbcSMatt Macy } else if (dnh->dnh_dnode == DN_SLOT_INTERIOR) { 1430*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_interior); 1431*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1432*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1433*eda14cbcSMatt Macy return (SET_ERROR(EEXIST)); 1434*eda14cbcSMatt Macy } else if (dnh->dnh_dnode != DN_SLOT_ALLOCATED) { 1435*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_misses); 1436*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1437*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1438*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 1439*eda14cbcSMatt Macy } else { 1440*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1441*eda14cbcSMatt Macy while (!dnode_slots_tryenter(dnc, idx, slots)) { 1442*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry); 1443*eda14cbcSMatt Macy cond_resched(); 1444*eda14cbcSMatt Macy } 1445*eda14cbcSMatt Macy 1446*eda14cbcSMatt Macy /* 1447*eda14cbcSMatt Macy * Someone else won the race and called dnode_create() 1448*eda14cbcSMatt Macy * after we checked DN_SLOT_IS_PTR() above but before 1449*eda14cbcSMatt Macy * we acquired the lock. 1450*eda14cbcSMatt Macy */ 1451*eda14cbcSMatt Macy if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) { 1452*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses); 1453*eda14cbcSMatt Macy dn = dnh->dnh_dnode; 1454*eda14cbcSMatt Macy } else { 1455*eda14cbcSMatt Macy dn = dnode_create(os, dn_block + idx, db, 1456*eda14cbcSMatt Macy object, dnh); 1457*eda14cbcSMatt Macy } 1458*eda14cbcSMatt Macy } 1459*eda14cbcSMatt Macy 1460*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1461*eda14cbcSMatt Macy if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg != 0) { 1462*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_type_none); 1463*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1464*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1465*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1466*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 1467*eda14cbcSMatt Macy } 1468*eda14cbcSMatt Macy 1469*eda14cbcSMatt Macy /* Don't actually hold if dry run, just return 0 */ 1470*eda14cbcSMatt Macy if (flag & DNODE_DRY_RUN) { 1471*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1472*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1473*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1474*eda14cbcSMatt Macy return (0); 1475*eda14cbcSMatt Macy } 1476*eda14cbcSMatt Macy 1477*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_alloc_hits); 1478*eda14cbcSMatt Macy } else if (flag & DNODE_MUST_BE_FREE) { 1479*eda14cbcSMatt Macy 1480*eda14cbcSMatt Macy if (idx + slots - 1 >= DNODES_PER_BLOCK) { 1481*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_overflow); 1482*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1483*eda14cbcSMatt Macy return (SET_ERROR(ENOSPC)); 1484*eda14cbcSMatt Macy } 1485*eda14cbcSMatt Macy 1486*eda14cbcSMatt Macy dnode_slots_hold(dnc, idx, slots); 1487*eda14cbcSMatt Macy 1488*eda14cbcSMatt Macy if (!dnode_check_slots_free(dnc, idx, slots)) { 1489*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_misses); 1490*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1491*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1492*eda14cbcSMatt Macy return (SET_ERROR(ENOSPC)); 1493*eda14cbcSMatt Macy } 1494*eda14cbcSMatt Macy 1495*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1496*eda14cbcSMatt Macy while (!dnode_slots_tryenter(dnc, idx, slots)) { 1497*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_lock_retry); 1498*eda14cbcSMatt Macy cond_resched(); 1499*eda14cbcSMatt Macy } 1500*eda14cbcSMatt Macy 1501*eda14cbcSMatt Macy if (!dnode_check_slots_free(dnc, idx, slots)) { 1502*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_lock_misses); 1503*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1504*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1505*eda14cbcSMatt Macy return (SET_ERROR(ENOSPC)); 1506*eda14cbcSMatt Macy } 1507*eda14cbcSMatt Macy 1508*eda14cbcSMatt Macy /* 1509*eda14cbcSMatt Macy * Allocated but otherwise free dnodes which would 1510*eda14cbcSMatt Macy * be in the interior of a multi-slot dnodes need 1511*eda14cbcSMatt Macy * to be freed. Single slot dnodes can be safely 1512*eda14cbcSMatt Macy * re-purposed as a performance optimization. 1513*eda14cbcSMatt Macy */ 1514*eda14cbcSMatt Macy if (slots > 1) 1515*eda14cbcSMatt Macy dnode_reclaim_slots(dnc, idx + 1, slots - 1); 1516*eda14cbcSMatt Macy 1517*eda14cbcSMatt Macy dnh = &dnc->dnc_children[idx]; 1518*eda14cbcSMatt Macy if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) { 1519*eda14cbcSMatt Macy dn = dnh->dnh_dnode; 1520*eda14cbcSMatt Macy } else { 1521*eda14cbcSMatt Macy dn = dnode_create(os, dn_block + idx, db, 1522*eda14cbcSMatt Macy object, dnh); 1523*eda14cbcSMatt Macy } 1524*eda14cbcSMatt Macy 1525*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1526*eda14cbcSMatt Macy if (!zfs_refcount_is_zero(&dn->dn_holds) || dn->dn_free_txg) { 1527*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_refcount); 1528*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1529*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1530*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1531*eda14cbcSMatt Macy return (SET_ERROR(EEXIST)); 1532*eda14cbcSMatt Macy } 1533*eda14cbcSMatt Macy 1534*eda14cbcSMatt Macy /* Don't actually hold if dry run, just return 0 */ 1535*eda14cbcSMatt Macy if (flag & DNODE_DRY_RUN) { 1536*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1537*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1538*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1539*eda14cbcSMatt Macy return (0); 1540*eda14cbcSMatt Macy } 1541*eda14cbcSMatt Macy 1542*eda14cbcSMatt Macy dnode_set_slots(dnc, idx + 1, slots - 1, DN_SLOT_INTERIOR); 1543*eda14cbcSMatt Macy DNODE_STAT_BUMP(dnode_hold_free_hits); 1544*eda14cbcSMatt Macy } else { 1545*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1546*eda14cbcSMatt Macy return (SET_ERROR(EINVAL)); 1547*eda14cbcSMatt Macy } 1548*eda14cbcSMatt Macy 1549*eda14cbcSMatt Macy ASSERT0(dn->dn_free_txg); 1550*eda14cbcSMatt Macy 1551*eda14cbcSMatt Macy if (zfs_refcount_add(&dn->dn_holds, tag) == 1) 1552*eda14cbcSMatt Macy dbuf_add_ref(db, dnh); 1553*eda14cbcSMatt Macy 1554*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1555*eda14cbcSMatt Macy 1556*eda14cbcSMatt Macy /* Now we can rely on the hold to prevent the dnode from moving. */ 1557*eda14cbcSMatt Macy dnode_slots_rele(dnc, idx, slots); 1558*eda14cbcSMatt Macy 1559*eda14cbcSMatt Macy DNODE_VERIFY(dn); 1560*eda14cbcSMatt Macy ASSERT3P(dnp, !=, NULL); 1561*eda14cbcSMatt Macy ASSERT3P(dn->dn_dbuf, ==, db); 1562*eda14cbcSMatt Macy ASSERT3U(dn->dn_object, ==, object); 1563*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1564*eda14cbcSMatt Macy 1565*eda14cbcSMatt Macy *dnp = dn; 1566*eda14cbcSMatt Macy return (0); 1567*eda14cbcSMatt Macy } 1568*eda14cbcSMatt Macy 1569*eda14cbcSMatt Macy /* 1570*eda14cbcSMatt Macy * Return held dnode if the object is allocated, NULL if not. 1571*eda14cbcSMatt Macy */ 1572*eda14cbcSMatt Macy int 1573*eda14cbcSMatt Macy dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp) 1574*eda14cbcSMatt Macy { 1575*eda14cbcSMatt Macy return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0, tag, 1576*eda14cbcSMatt Macy dnp)); 1577*eda14cbcSMatt Macy } 1578*eda14cbcSMatt Macy 1579*eda14cbcSMatt Macy /* 1580*eda14cbcSMatt Macy * Can only add a reference if there is already at least one 1581*eda14cbcSMatt Macy * reference on the dnode. Returns FALSE if unable to add a 1582*eda14cbcSMatt Macy * new reference. 1583*eda14cbcSMatt Macy */ 1584*eda14cbcSMatt Macy boolean_t 1585*eda14cbcSMatt Macy dnode_add_ref(dnode_t *dn, void *tag) 1586*eda14cbcSMatt Macy { 1587*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1588*eda14cbcSMatt Macy if (zfs_refcount_is_zero(&dn->dn_holds)) { 1589*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1590*eda14cbcSMatt Macy return (FALSE); 1591*eda14cbcSMatt Macy } 1592*eda14cbcSMatt Macy VERIFY(1 < zfs_refcount_add(&dn->dn_holds, tag)); 1593*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1594*eda14cbcSMatt Macy return (TRUE); 1595*eda14cbcSMatt Macy } 1596*eda14cbcSMatt Macy 1597*eda14cbcSMatt Macy void 1598*eda14cbcSMatt Macy dnode_rele(dnode_t *dn, void *tag) 1599*eda14cbcSMatt Macy { 1600*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1601*eda14cbcSMatt Macy dnode_rele_and_unlock(dn, tag, B_FALSE); 1602*eda14cbcSMatt Macy } 1603*eda14cbcSMatt Macy 1604*eda14cbcSMatt Macy void 1605*eda14cbcSMatt Macy dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting) 1606*eda14cbcSMatt Macy { 1607*eda14cbcSMatt Macy uint64_t refs; 1608*eda14cbcSMatt Macy /* Get while the hold prevents the dnode from moving. */ 1609*eda14cbcSMatt Macy dmu_buf_impl_t *db = dn->dn_dbuf; 1610*eda14cbcSMatt Macy dnode_handle_t *dnh = dn->dn_handle; 1611*eda14cbcSMatt Macy 1612*eda14cbcSMatt Macy refs = zfs_refcount_remove(&dn->dn_holds, tag); 1613*eda14cbcSMatt Macy if (refs == 0) 1614*eda14cbcSMatt Macy cv_broadcast(&dn->dn_nodnholds); 1615*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1616*eda14cbcSMatt Macy /* dnode could get destroyed at this point, so don't use it anymore */ 1617*eda14cbcSMatt Macy 1618*eda14cbcSMatt Macy /* 1619*eda14cbcSMatt Macy * It's unsafe to release the last hold on a dnode by dnode_rele() or 1620*eda14cbcSMatt Macy * indirectly by dbuf_rele() while relying on the dnode handle to 1621*eda14cbcSMatt Macy * prevent the dnode from moving, since releasing the last hold could 1622*eda14cbcSMatt Macy * result in the dnode's parent dbuf evicting its dnode handles. For 1623*eda14cbcSMatt Macy * that reason anyone calling dnode_rele() or dbuf_rele() without some 1624*eda14cbcSMatt Macy * other direct or indirect hold on the dnode must first drop the dnode 1625*eda14cbcSMatt Macy * handle. 1626*eda14cbcSMatt Macy */ 1627*eda14cbcSMatt Macy ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread); 1628*eda14cbcSMatt Macy 1629*eda14cbcSMatt Macy /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 1630*eda14cbcSMatt Macy if (refs == 0 && db != NULL) { 1631*eda14cbcSMatt Macy /* 1632*eda14cbcSMatt Macy * Another thread could add a hold to the dnode handle in 1633*eda14cbcSMatt Macy * dnode_hold_impl() while holding the parent dbuf. Since the 1634*eda14cbcSMatt Macy * hold on the parent dbuf prevents the handle from being 1635*eda14cbcSMatt Macy * destroyed, the hold on the handle is OK. We can't yet assert 1636*eda14cbcSMatt Macy * that the handle has zero references, but that will be 1637*eda14cbcSMatt Macy * asserted anyway when the handle gets destroyed. 1638*eda14cbcSMatt Macy */ 1639*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1640*eda14cbcSMatt Macy dbuf_rele_and_unlock(db, dnh, evicting); 1641*eda14cbcSMatt Macy } 1642*eda14cbcSMatt Macy } 1643*eda14cbcSMatt Macy 1644*eda14cbcSMatt Macy /* 1645*eda14cbcSMatt Macy * Test whether we can create a dnode at the specified location. 1646*eda14cbcSMatt Macy */ 1647*eda14cbcSMatt Macy int 1648*eda14cbcSMatt Macy dnode_try_claim(objset_t *os, uint64_t object, int slots) 1649*eda14cbcSMatt Macy { 1650*eda14cbcSMatt Macy return (dnode_hold_impl(os, object, DNODE_MUST_BE_FREE | DNODE_DRY_RUN, 1651*eda14cbcSMatt Macy slots, NULL, NULL)); 1652*eda14cbcSMatt Macy } 1653*eda14cbcSMatt Macy 1654*eda14cbcSMatt Macy void 1655*eda14cbcSMatt Macy dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 1656*eda14cbcSMatt Macy { 1657*eda14cbcSMatt Macy objset_t *os = dn->dn_objset; 1658*eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 1659*eda14cbcSMatt Macy 1660*eda14cbcSMatt Macy if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 1661*eda14cbcSMatt Macy dsl_dataset_dirty(os->os_dsl_dataset, tx); 1662*eda14cbcSMatt Macy return; 1663*eda14cbcSMatt Macy } 1664*eda14cbcSMatt Macy 1665*eda14cbcSMatt Macy DNODE_VERIFY(dn); 1666*eda14cbcSMatt Macy 1667*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 1668*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1669*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 1670*eda14cbcSMatt Macy ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); 1671*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1672*eda14cbcSMatt Macy #endif 1673*eda14cbcSMatt Macy 1674*eda14cbcSMatt Macy /* 1675*eda14cbcSMatt Macy * Determine old uid/gid when necessary 1676*eda14cbcSMatt Macy */ 1677*eda14cbcSMatt Macy dmu_objset_userquota_get_ids(dn, B_TRUE, tx); 1678*eda14cbcSMatt Macy 1679*eda14cbcSMatt Macy multilist_t *dirtylist = os->os_dirty_dnodes[txg & TXG_MASK]; 1680*eda14cbcSMatt Macy multilist_sublist_t *mls = multilist_sublist_lock_obj(dirtylist, dn); 1681*eda14cbcSMatt Macy 1682*eda14cbcSMatt Macy /* 1683*eda14cbcSMatt Macy * If we are already marked dirty, we're done. 1684*eda14cbcSMatt Macy */ 1685*eda14cbcSMatt Macy if (multilist_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 1686*eda14cbcSMatt Macy multilist_sublist_unlock(mls); 1687*eda14cbcSMatt Macy return; 1688*eda14cbcSMatt Macy } 1689*eda14cbcSMatt Macy 1690*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&dn->dn_holds) || 1691*eda14cbcSMatt Macy !avl_is_empty(&dn->dn_dbufs)); 1692*eda14cbcSMatt Macy ASSERT(dn->dn_datablksz != 0); 1693*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonuslen[txg & TXG_MASK]); 1694*eda14cbcSMatt Macy ASSERT0(dn->dn_next_blksz[txg & TXG_MASK]); 1695*eda14cbcSMatt Macy ASSERT0(dn->dn_next_bonustype[txg & TXG_MASK]); 1696*eda14cbcSMatt Macy 1697*eda14cbcSMatt Macy dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 1698*eda14cbcSMatt Macy dn->dn_object, txg); 1699*eda14cbcSMatt Macy 1700*eda14cbcSMatt Macy multilist_sublist_insert_head(mls, dn); 1701*eda14cbcSMatt Macy 1702*eda14cbcSMatt Macy multilist_sublist_unlock(mls); 1703*eda14cbcSMatt Macy 1704*eda14cbcSMatt Macy /* 1705*eda14cbcSMatt Macy * The dnode maintains a hold on its containing dbuf as 1706*eda14cbcSMatt Macy * long as there are holds on it. Each instantiated child 1707*eda14cbcSMatt Macy * dbuf maintains a hold on the dnode. When the last child 1708*eda14cbcSMatt Macy * drops its hold, the dnode will drop its hold on the 1709*eda14cbcSMatt Macy * containing dbuf. We add a "dirty hold" here so that the 1710*eda14cbcSMatt Macy * dnode will hang around after we finish processing its 1711*eda14cbcSMatt Macy * children. 1712*eda14cbcSMatt Macy */ 1713*eda14cbcSMatt Macy VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg)); 1714*eda14cbcSMatt Macy 1715*eda14cbcSMatt Macy (void) dbuf_dirty(dn->dn_dbuf, tx); 1716*eda14cbcSMatt Macy 1717*eda14cbcSMatt Macy dsl_dataset_dirty(os->os_dsl_dataset, tx); 1718*eda14cbcSMatt Macy } 1719*eda14cbcSMatt Macy 1720*eda14cbcSMatt Macy void 1721*eda14cbcSMatt Macy dnode_free(dnode_t *dn, dmu_tx_t *tx) 1722*eda14cbcSMatt Macy { 1723*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1724*eda14cbcSMatt Macy if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 1725*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1726*eda14cbcSMatt Macy return; 1727*eda14cbcSMatt Macy } 1728*eda14cbcSMatt Macy dn->dn_free_txg = tx->tx_txg; 1729*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1730*eda14cbcSMatt Macy 1731*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 1732*eda14cbcSMatt Macy } 1733*eda14cbcSMatt Macy 1734*eda14cbcSMatt Macy /* 1735*eda14cbcSMatt Macy * Try to change the block size for the indicated dnode. This can only 1736*eda14cbcSMatt Macy * succeed if there are no blocks allocated or dirty beyond first block 1737*eda14cbcSMatt Macy */ 1738*eda14cbcSMatt Macy int 1739*eda14cbcSMatt Macy dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 1740*eda14cbcSMatt Macy { 1741*eda14cbcSMatt Macy dmu_buf_impl_t *db; 1742*eda14cbcSMatt Macy int err; 1743*eda14cbcSMatt Macy 1744*eda14cbcSMatt Macy ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset))); 1745*eda14cbcSMatt Macy if (size == 0) 1746*eda14cbcSMatt Macy size = SPA_MINBLOCKSIZE; 1747*eda14cbcSMatt Macy else 1748*eda14cbcSMatt Macy size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 1749*eda14cbcSMatt Macy 1750*eda14cbcSMatt Macy if (ibs == dn->dn_indblkshift) 1751*eda14cbcSMatt Macy ibs = 0; 1752*eda14cbcSMatt Macy 1753*eda14cbcSMatt Macy if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0) 1754*eda14cbcSMatt Macy return (0); 1755*eda14cbcSMatt Macy 1756*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1757*eda14cbcSMatt Macy 1758*eda14cbcSMatt Macy /* Check for any allocated blocks beyond the first */ 1759*eda14cbcSMatt Macy if (dn->dn_maxblkid != 0) 1760*eda14cbcSMatt Macy goto fail; 1761*eda14cbcSMatt Macy 1762*eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 1763*eda14cbcSMatt Macy for (db = avl_first(&dn->dn_dbufs); db != NULL; 1764*eda14cbcSMatt Macy db = AVL_NEXT(&dn->dn_dbufs, db)) { 1765*eda14cbcSMatt Macy if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID && 1766*eda14cbcSMatt Macy db->db_blkid != DMU_SPILL_BLKID) { 1767*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 1768*eda14cbcSMatt Macy goto fail; 1769*eda14cbcSMatt Macy } 1770*eda14cbcSMatt Macy } 1771*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 1772*eda14cbcSMatt Macy 1773*eda14cbcSMatt Macy if (ibs && dn->dn_nlevels != 1) 1774*eda14cbcSMatt Macy goto fail; 1775*eda14cbcSMatt Macy 1776*eda14cbcSMatt Macy /* resize the old block */ 1777*eda14cbcSMatt Macy err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db); 1778*eda14cbcSMatt Macy if (err == 0) { 1779*eda14cbcSMatt Macy dbuf_new_size(db, size, tx); 1780*eda14cbcSMatt Macy } else if (err != ENOENT) { 1781*eda14cbcSMatt Macy goto fail; 1782*eda14cbcSMatt Macy } 1783*eda14cbcSMatt Macy 1784*eda14cbcSMatt Macy dnode_setdblksz(dn, size); 1785*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 1786*eda14cbcSMatt Macy dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 1787*eda14cbcSMatt Macy if (ibs) { 1788*eda14cbcSMatt Macy dn->dn_indblkshift = ibs; 1789*eda14cbcSMatt Macy dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 1790*eda14cbcSMatt Macy } 1791*eda14cbcSMatt Macy /* release after we have fixed the blocksize in the dnode */ 1792*eda14cbcSMatt Macy if (db) 1793*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1794*eda14cbcSMatt Macy 1795*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 1796*eda14cbcSMatt Macy return (0); 1797*eda14cbcSMatt Macy 1798*eda14cbcSMatt Macy fail: 1799*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 1800*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 1801*eda14cbcSMatt Macy } 1802*eda14cbcSMatt Macy 1803*eda14cbcSMatt Macy static void 1804*eda14cbcSMatt Macy dnode_set_nlevels_impl(dnode_t *dn, int new_nlevels, dmu_tx_t *tx) 1805*eda14cbcSMatt Macy { 1806*eda14cbcSMatt Macy uint64_t txgoff = tx->tx_txg & TXG_MASK; 1807*eda14cbcSMatt Macy int old_nlevels = dn->dn_nlevels; 1808*eda14cbcSMatt Macy dmu_buf_impl_t *db; 1809*eda14cbcSMatt Macy list_t *list; 1810*eda14cbcSMatt Macy dbuf_dirty_record_t *new, *dr, *dr_next; 1811*eda14cbcSMatt Macy 1812*eda14cbcSMatt Macy ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 1813*eda14cbcSMatt Macy 1814*eda14cbcSMatt Macy dn->dn_nlevels = new_nlevels; 1815*eda14cbcSMatt Macy 1816*eda14cbcSMatt Macy ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 1817*eda14cbcSMatt Macy dn->dn_next_nlevels[txgoff] = new_nlevels; 1818*eda14cbcSMatt Macy 1819*eda14cbcSMatt Macy /* dirty the left indirects */ 1820*eda14cbcSMatt Macy db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 1821*eda14cbcSMatt Macy ASSERT(db != NULL); 1822*eda14cbcSMatt Macy new = dbuf_dirty(db, tx); 1823*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1824*eda14cbcSMatt Macy 1825*eda14cbcSMatt Macy /* transfer the dirty records to the new indirect */ 1826*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 1827*eda14cbcSMatt Macy mutex_enter(&new->dt.di.dr_mtx); 1828*eda14cbcSMatt Macy list = &dn->dn_dirty_records[txgoff]; 1829*eda14cbcSMatt Macy for (dr = list_head(list); dr; dr = dr_next) { 1830*eda14cbcSMatt Macy dr_next = list_next(&dn->dn_dirty_records[txgoff], dr); 1831*eda14cbcSMatt Macy if (dr->dr_dbuf->db_level != new_nlevels-1 && 1832*eda14cbcSMatt Macy dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID && 1833*eda14cbcSMatt Macy dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) { 1834*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf->db_level == old_nlevels-1); 1835*eda14cbcSMatt Macy list_remove(&dn->dn_dirty_records[txgoff], dr); 1836*eda14cbcSMatt Macy list_insert_tail(&new->dt.di.dr_children, dr); 1837*eda14cbcSMatt Macy dr->dr_parent = new; 1838*eda14cbcSMatt Macy } 1839*eda14cbcSMatt Macy } 1840*eda14cbcSMatt Macy mutex_exit(&new->dt.di.dr_mtx); 1841*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 1842*eda14cbcSMatt Macy } 1843*eda14cbcSMatt Macy 1844*eda14cbcSMatt Macy int 1845*eda14cbcSMatt Macy dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx) 1846*eda14cbcSMatt Macy { 1847*eda14cbcSMatt Macy int ret = 0; 1848*eda14cbcSMatt Macy 1849*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1850*eda14cbcSMatt Macy 1851*eda14cbcSMatt Macy if (dn->dn_nlevels == nlevels) { 1852*eda14cbcSMatt Macy ret = 0; 1853*eda14cbcSMatt Macy goto out; 1854*eda14cbcSMatt Macy } else if (nlevels < dn->dn_nlevels) { 1855*eda14cbcSMatt Macy ret = SET_ERROR(EINVAL); 1856*eda14cbcSMatt Macy goto out; 1857*eda14cbcSMatt Macy } 1858*eda14cbcSMatt Macy 1859*eda14cbcSMatt Macy dnode_set_nlevels_impl(dn, nlevels, tx); 1860*eda14cbcSMatt Macy 1861*eda14cbcSMatt Macy out: 1862*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 1863*eda14cbcSMatt Macy return (ret); 1864*eda14cbcSMatt Macy } 1865*eda14cbcSMatt Macy 1866*eda14cbcSMatt Macy /* read-holding callers must not rely on the lock being continuously held */ 1867*eda14cbcSMatt Macy void 1868*eda14cbcSMatt Macy dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read, 1869*eda14cbcSMatt Macy boolean_t force) 1870*eda14cbcSMatt Macy { 1871*eda14cbcSMatt Macy int epbs, new_nlevels; 1872*eda14cbcSMatt Macy uint64_t sz; 1873*eda14cbcSMatt Macy 1874*eda14cbcSMatt Macy ASSERT(blkid != DMU_BONUS_BLKID); 1875*eda14cbcSMatt Macy 1876*eda14cbcSMatt Macy ASSERT(have_read ? 1877*eda14cbcSMatt Macy RW_READ_HELD(&dn->dn_struct_rwlock) : 1878*eda14cbcSMatt Macy RW_WRITE_HELD(&dn->dn_struct_rwlock)); 1879*eda14cbcSMatt Macy 1880*eda14cbcSMatt Macy /* 1881*eda14cbcSMatt Macy * if we have a read-lock, check to see if we need to do any work 1882*eda14cbcSMatt Macy * before upgrading to a write-lock. 1883*eda14cbcSMatt Macy */ 1884*eda14cbcSMatt Macy if (have_read) { 1885*eda14cbcSMatt Macy if (blkid <= dn->dn_maxblkid) 1886*eda14cbcSMatt Macy return; 1887*eda14cbcSMatt Macy 1888*eda14cbcSMatt Macy if (!rw_tryupgrade(&dn->dn_struct_rwlock)) { 1889*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 1890*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1891*eda14cbcSMatt Macy } 1892*eda14cbcSMatt Macy } 1893*eda14cbcSMatt Macy 1894*eda14cbcSMatt Macy /* 1895*eda14cbcSMatt Macy * Raw sends (indicated by the force flag) require that we take the 1896*eda14cbcSMatt Macy * given blkid even if the value is lower than the current value. 1897*eda14cbcSMatt Macy */ 1898*eda14cbcSMatt Macy if (!force && blkid <= dn->dn_maxblkid) 1899*eda14cbcSMatt Macy goto out; 1900*eda14cbcSMatt Macy 1901*eda14cbcSMatt Macy /* 1902*eda14cbcSMatt Macy * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff] 1903*eda14cbcSMatt Macy * to indicate that this field is set. This allows us to set the 1904*eda14cbcSMatt Macy * maxblkid to 0 on an existing object in dnode_sync(). 1905*eda14cbcSMatt Macy */ 1906*eda14cbcSMatt Macy dn->dn_maxblkid = blkid; 1907*eda14cbcSMatt Macy dn->dn_next_maxblkid[tx->tx_txg & TXG_MASK] = 1908*eda14cbcSMatt Macy blkid | DMU_NEXT_MAXBLKID_SET; 1909*eda14cbcSMatt Macy 1910*eda14cbcSMatt Macy /* 1911*eda14cbcSMatt Macy * Compute the number of levels necessary to support the new maxblkid. 1912*eda14cbcSMatt Macy * Raw sends will ensure nlevels is set correctly for us. 1913*eda14cbcSMatt Macy */ 1914*eda14cbcSMatt Macy new_nlevels = 1; 1915*eda14cbcSMatt Macy epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1916*eda14cbcSMatt Macy for (sz = dn->dn_nblkptr; 1917*eda14cbcSMatt Macy sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 1918*eda14cbcSMatt Macy new_nlevels++; 1919*eda14cbcSMatt Macy 1920*eda14cbcSMatt Macy ASSERT3U(new_nlevels, <=, DN_MAX_LEVELS); 1921*eda14cbcSMatt Macy 1922*eda14cbcSMatt Macy if (!force) { 1923*eda14cbcSMatt Macy if (new_nlevels > dn->dn_nlevels) 1924*eda14cbcSMatt Macy dnode_set_nlevels_impl(dn, new_nlevels, tx); 1925*eda14cbcSMatt Macy } else { 1926*eda14cbcSMatt Macy ASSERT3U(dn->dn_nlevels, >=, new_nlevels); 1927*eda14cbcSMatt Macy } 1928*eda14cbcSMatt Macy 1929*eda14cbcSMatt Macy out: 1930*eda14cbcSMatt Macy if (have_read) 1931*eda14cbcSMatt Macy rw_downgrade(&dn->dn_struct_rwlock); 1932*eda14cbcSMatt Macy } 1933*eda14cbcSMatt Macy 1934*eda14cbcSMatt Macy static void 1935*eda14cbcSMatt Macy dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx) 1936*eda14cbcSMatt Macy { 1937*eda14cbcSMatt Macy dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG); 1938*eda14cbcSMatt Macy if (db != NULL) { 1939*eda14cbcSMatt Macy dmu_buf_will_dirty(&db->db, tx); 1940*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 1941*eda14cbcSMatt Macy } 1942*eda14cbcSMatt Macy } 1943*eda14cbcSMatt Macy 1944*eda14cbcSMatt Macy /* 1945*eda14cbcSMatt Macy * Dirty all the in-core level-1 dbufs in the range specified by start_blkid 1946*eda14cbcSMatt Macy * and end_blkid. 1947*eda14cbcSMatt Macy */ 1948*eda14cbcSMatt Macy static void 1949*eda14cbcSMatt Macy dnode_dirty_l1range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid, 1950*eda14cbcSMatt Macy dmu_tx_t *tx) 1951*eda14cbcSMatt Macy { 1952*eda14cbcSMatt Macy dmu_buf_impl_t db_search; 1953*eda14cbcSMatt Macy dmu_buf_impl_t *db; 1954*eda14cbcSMatt Macy avl_index_t where; 1955*eda14cbcSMatt Macy 1956*eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 1957*eda14cbcSMatt Macy 1958*eda14cbcSMatt Macy db_search.db_level = 1; 1959*eda14cbcSMatt Macy db_search.db_blkid = start_blkid + 1; 1960*eda14cbcSMatt Macy db_search.db_state = DB_SEARCH; 1961*eda14cbcSMatt Macy for (;;) { 1962*eda14cbcSMatt Macy 1963*eda14cbcSMatt Macy db = avl_find(&dn->dn_dbufs, &db_search, &where); 1964*eda14cbcSMatt Macy if (db == NULL) 1965*eda14cbcSMatt Macy db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 1966*eda14cbcSMatt Macy 1967*eda14cbcSMatt Macy if (db == NULL || db->db_level != 1 || 1968*eda14cbcSMatt Macy db->db_blkid >= end_blkid) { 1969*eda14cbcSMatt Macy break; 1970*eda14cbcSMatt Macy } 1971*eda14cbcSMatt Macy 1972*eda14cbcSMatt Macy /* 1973*eda14cbcSMatt Macy * Setup the next blkid we want to search for. 1974*eda14cbcSMatt Macy */ 1975*eda14cbcSMatt Macy db_search.db_blkid = db->db_blkid + 1; 1976*eda14cbcSMatt Macy ASSERT3U(db->db_blkid, >=, start_blkid); 1977*eda14cbcSMatt Macy 1978*eda14cbcSMatt Macy /* 1979*eda14cbcSMatt Macy * If the dbuf transitions to DB_EVICTING while we're trying 1980*eda14cbcSMatt Macy * to dirty it, then we will be unable to discover it in 1981*eda14cbcSMatt Macy * the dbuf hash table. This will result in a call to 1982*eda14cbcSMatt Macy * dbuf_create() which needs to acquire the dn_dbufs_mtx 1983*eda14cbcSMatt Macy * lock. To avoid a deadlock, we drop the lock before 1984*eda14cbcSMatt Macy * dirtying the level-1 dbuf. 1985*eda14cbcSMatt Macy */ 1986*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 1987*eda14cbcSMatt Macy dnode_dirty_l1(dn, db->db_blkid, tx); 1988*eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 1989*eda14cbcSMatt Macy } 1990*eda14cbcSMatt Macy 1991*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 1992*eda14cbcSMatt Macy /* 1993*eda14cbcSMatt Macy * Walk all the in-core level-1 dbufs and verify they have been dirtied. 1994*eda14cbcSMatt Macy */ 1995*eda14cbcSMatt Macy db_search.db_level = 1; 1996*eda14cbcSMatt Macy db_search.db_blkid = start_blkid + 1; 1997*eda14cbcSMatt Macy db_search.db_state = DB_SEARCH; 1998*eda14cbcSMatt Macy db = avl_find(&dn->dn_dbufs, &db_search, &where); 1999*eda14cbcSMatt Macy if (db == NULL) 2000*eda14cbcSMatt Macy db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 2001*eda14cbcSMatt Macy for (; db != NULL; db = AVL_NEXT(&dn->dn_dbufs, db)) { 2002*eda14cbcSMatt Macy if (db->db_level != 1 || db->db_blkid >= end_blkid) 2003*eda14cbcSMatt Macy break; 2004*eda14cbcSMatt Macy if (db->db_state != DB_EVICTING) 2005*eda14cbcSMatt Macy ASSERT(db->db_dirtycnt > 0); 2006*eda14cbcSMatt Macy } 2007*eda14cbcSMatt Macy #endif 2008*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 2009*eda14cbcSMatt Macy } 2010*eda14cbcSMatt Macy 2011*eda14cbcSMatt Macy void 2012*eda14cbcSMatt Macy dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, void *tag) 2013*eda14cbcSMatt Macy { 2014*eda14cbcSMatt Macy /* 2015*eda14cbcSMatt Macy * Don't set dirtyctx to SYNC if we're just modifying this as we 2016*eda14cbcSMatt Macy * initialize the objset. 2017*eda14cbcSMatt Macy */ 2018*eda14cbcSMatt Macy if (dn->dn_dirtyctx == DN_UNDIRTIED) { 2019*eda14cbcSMatt Macy dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 2020*eda14cbcSMatt Macy 2021*eda14cbcSMatt Macy if (ds != NULL) { 2022*eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, tag); 2023*eda14cbcSMatt Macy } 2024*eda14cbcSMatt Macy if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) { 2025*eda14cbcSMatt Macy if (dmu_tx_is_syncing(tx)) 2026*eda14cbcSMatt Macy dn->dn_dirtyctx = DN_DIRTY_SYNC; 2027*eda14cbcSMatt Macy else 2028*eda14cbcSMatt Macy dn->dn_dirtyctx = DN_DIRTY_OPEN; 2029*eda14cbcSMatt Macy dn->dn_dirtyctx_firstset = tag; 2030*eda14cbcSMatt Macy } 2031*eda14cbcSMatt Macy if (ds != NULL) { 2032*eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, tag); 2033*eda14cbcSMatt Macy } 2034*eda14cbcSMatt Macy } 2035*eda14cbcSMatt Macy } 2036*eda14cbcSMatt Macy 2037*eda14cbcSMatt Macy void 2038*eda14cbcSMatt Macy dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 2039*eda14cbcSMatt Macy { 2040*eda14cbcSMatt Macy dmu_buf_impl_t *db; 2041*eda14cbcSMatt Macy uint64_t blkoff, blkid, nblks; 2042*eda14cbcSMatt Macy int blksz, blkshift, head, tail; 2043*eda14cbcSMatt Macy int trunc = FALSE; 2044*eda14cbcSMatt Macy int epbs; 2045*eda14cbcSMatt Macy 2046*eda14cbcSMatt Macy blksz = dn->dn_datablksz; 2047*eda14cbcSMatt Macy blkshift = dn->dn_datablkshift; 2048*eda14cbcSMatt Macy epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2049*eda14cbcSMatt Macy 2050*eda14cbcSMatt Macy if (len == DMU_OBJECT_END) { 2051*eda14cbcSMatt Macy len = UINT64_MAX - off; 2052*eda14cbcSMatt Macy trunc = TRUE; 2053*eda14cbcSMatt Macy } 2054*eda14cbcSMatt Macy 2055*eda14cbcSMatt Macy /* 2056*eda14cbcSMatt Macy * First, block align the region to free: 2057*eda14cbcSMatt Macy */ 2058*eda14cbcSMatt Macy if (ISP2(blksz)) { 2059*eda14cbcSMatt Macy head = P2NPHASE(off, blksz); 2060*eda14cbcSMatt Macy blkoff = P2PHASE(off, blksz); 2061*eda14cbcSMatt Macy if ((off >> blkshift) > dn->dn_maxblkid) 2062*eda14cbcSMatt Macy return; 2063*eda14cbcSMatt Macy } else { 2064*eda14cbcSMatt Macy ASSERT(dn->dn_maxblkid == 0); 2065*eda14cbcSMatt Macy if (off == 0 && len >= blksz) { 2066*eda14cbcSMatt Macy /* 2067*eda14cbcSMatt Macy * Freeing the whole block; fast-track this request. 2068*eda14cbcSMatt Macy */ 2069*eda14cbcSMatt Macy blkid = 0; 2070*eda14cbcSMatt Macy nblks = 1; 2071*eda14cbcSMatt Macy if (dn->dn_nlevels > 1) { 2072*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2073*eda14cbcSMatt Macy dnode_dirty_l1(dn, 0, tx); 2074*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2075*eda14cbcSMatt Macy } 2076*eda14cbcSMatt Macy goto done; 2077*eda14cbcSMatt Macy } else if (off >= blksz) { 2078*eda14cbcSMatt Macy /* Freeing past end-of-data */ 2079*eda14cbcSMatt Macy return; 2080*eda14cbcSMatt Macy } else { 2081*eda14cbcSMatt Macy /* Freeing part of the block. */ 2082*eda14cbcSMatt Macy head = blksz - off; 2083*eda14cbcSMatt Macy ASSERT3U(head, >, 0); 2084*eda14cbcSMatt Macy } 2085*eda14cbcSMatt Macy blkoff = off; 2086*eda14cbcSMatt Macy } 2087*eda14cbcSMatt Macy /* zero out any partial block data at the start of the range */ 2088*eda14cbcSMatt Macy if (head) { 2089*eda14cbcSMatt Macy int res; 2090*eda14cbcSMatt Macy ASSERT3U(blkoff + head, ==, blksz); 2091*eda14cbcSMatt Macy if (len < head) 2092*eda14cbcSMatt Macy head = len; 2093*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 2094*eda14cbcSMatt Macy res = dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off), 2095*eda14cbcSMatt Macy TRUE, FALSE, FTAG, &db); 2096*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2097*eda14cbcSMatt Macy if (res == 0) { 2098*eda14cbcSMatt Macy caddr_t data; 2099*eda14cbcSMatt Macy boolean_t dirty; 2100*eda14cbcSMatt Macy 2101*eda14cbcSMatt Macy db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, 2102*eda14cbcSMatt Macy FTAG); 2103*eda14cbcSMatt Macy /* don't dirty if it isn't on disk and isn't dirty */ 2104*eda14cbcSMatt Macy dirty = !list_is_empty(&db->db_dirty_records) || 2105*eda14cbcSMatt Macy (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr)); 2106*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, FTAG); 2107*eda14cbcSMatt Macy if (dirty) { 2108*eda14cbcSMatt Macy dmu_buf_will_dirty(&db->db, tx); 2109*eda14cbcSMatt Macy data = db->db.db_data; 2110*eda14cbcSMatt Macy bzero(data + blkoff, head); 2111*eda14cbcSMatt Macy } 2112*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 2113*eda14cbcSMatt Macy } 2114*eda14cbcSMatt Macy off += head; 2115*eda14cbcSMatt Macy len -= head; 2116*eda14cbcSMatt Macy } 2117*eda14cbcSMatt Macy 2118*eda14cbcSMatt Macy /* If the range was less than one block, we're done */ 2119*eda14cbcSMatt Macy if (len == 0) 2120*eda14cbcSMatt Macy return; 2121*eda14cbcSMatt Macy 2122*eda14cbcSMatt Macy /* If the remaining range is past end of file, we're done */ 2123*eda14cbcSMatt Macy if ((off >> blkshift) > dn->dn_maxblkid) 2124*eda14cbcSMatt Macy return; 2125*eda14cbcSMatt Macy 2126*eda14cbcSMatt Macy ASSERT(ISP2(blksz)); 2127*eda14cbcSMatt Macy if (trunc) 2128*eda14cbcSMatt Macy tail = 0; 2129*eda14cbcSMatt Macy else 2130*eda14cbcSMatt Macy tail = P2PHASE(len, blksz); 2131*eda14cbcSMatt Macy 2132*eda14cbcSMatt Macy ASSERT0(P2PHASE(off, blksz)); 2133*eda14cbcSMatt Macy /* zero out any partial block data at the end of the range */ 2134*eda14cbcSMatt Macy if (tail) { 2135*eda14cbcSMatt Macy int res; 2136*eda14cbcSMatt Macy if (len < tail) 2137*eda14cbcSMatt Macy tail = len; 2138*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 2139*eda14cbcSMatt Macy res = dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len), 2140*eda14cbcSMatt Macy TRUE, FALSE, FTAG, &db); 2141*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2142*eda14cbcSMatt Macy if (res == 0) { 2143*eda14cbcSMatt Macy boolean_t dirty; 2144*eda14cbcSMatt Macy /* don't dirty if not on disk and not dirty */ 2145*eda14cbcSMatt Macy db_lock_type_t type = dmu_buf_lock_parent(db, RW_READER, 2146*eda14cbcSMatt Macy FTAG); 2147*eda14cbcSMatt Macy dirty = !list_is_empty(&db->db_dirty_records) || 2148*eda14cbcSMatt Macy (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr)); 2149*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, type, FTAG); 2150*eda14cbcSMatt Macy if (dirty) { 2151*eda14cbcSMatt Macy dmu_buf_will_dirty(&db->db, tx); 2152*eda14cbcSMatt Macy bzero(db->db.db_data, tail); 2153*eda14cbcSMatt Macy } 2154*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 2155*eda14cbcSMatt Macy } 2156*eda14cbcSMatt Macy len -= tail; 2157*eda14cbcSMatt Macy } 2158*eda14cbcSMatt Macy 2159*eda14cbcSMatt Macy /* If the range did not include a full block, we are done */ 2160*eda14cbcSMatt Macy if (len == 0) 2161*eda14cbcSMatt Macy return; 2162*eda14cbcSMatt Macy 2163*eda14cbcSMatt Macy ASSERT(IS_P2ALIGNED(off, blksz)); 2164*eda14cbcSMatt Macy ASSERT(trunc || IS_P2ALIGNED(len, blksz)); 2165*eda14cbcSMatt Macy blkid = off >> blkshift; 2166*eda14cbcSMatt Macy nblks = len >> blkshift; 2167*eda14cbcSMatt Macy if (trunc) 2168*eda14cbcSMatt Macy nblks += 1; 2169*eda14cbcSMatt Macy 2170*eda14cbcSMatt Macy /* 2171*eda14cbcSMatt Macy * Dirty all the indirect blocks in this range. Note that only 2172*eda14cbcSMatt Macy * the first and last indirect blocks can actually be written 2173*eda14cbcSMatt Macy * (if they were partially freed) -- they must be dirtied, even if 2174*eda14cbcSMatt Macy * they do not exist on disk yet. The interior blocks will 2175*eda14cbcSMatt Macy * be freed by free_children(), so they will not actually be written. 2176*eda14cbcSMatt Macy * Even though these interior blocks will not be written, we 2177*eda14cbcSMatt Macy * dirty them for two reasons: 2178*eda14cbcSMatt Macy * 2179*eda14cbcSMatt Macy * - It ensures that the indirect blocks remain in memory until 2180*eda14cbcSMatt Macy * syncing context. (They have already been prefetched by 2181*eda14cbcSMatt Macy * dmu_tx_hold_free(), so we don't have to worry about reading 2182*eda14cbcSMatt Macy * them serially here.) 2183*eda14cbcSMatt Macy * 2184*eda14cbcSMatt Macy * - The dirty space accounting will put pressure on the txg sync 2185*eda14cbcSMatt Macy * mechanism to begin syncing, and to delay transactions if there 2186*eda14cbcSMatt Macy * is a large amount of freeing. Even though these indirect 2187*eda14cbcSMatt Macy * blocks will not be written, we could need to write the same 2188*eda14cbcSMatt Macy * amount of space if we copy the freed BPs into deadlists. 2189*eda14cbcSMatt Macy */ 2190*eda14cbcSMatt Macy if (dn->dn_nlevels > 1) { 2191*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2192*eda14cbcSMatt Macy uint64_t first, last; 2193*eda14cbcSMatt Macy 2194*eda14cbcSMatt Macy first = blkid >> epbs; 2195*eda14cbcSMatt Macy dnode_dirty_l1(dn, first, tx); 2196*eda14cbcSMatt Macy if (trunc) 2197*eda14cbcSMatt Macy last = dn->dn_maxblkid >> epbs; 2198*eda14cbcSMatt Macy else 2199*eda14cbcSMatt Macy last = (blkid + nblks - 1) >> epbs; 2200*eda14cbcSMatt Macy if (last != first) 2201*eda14cbcSMatt Macy dnode_dirty_l1(dn, last, tx); 2202*eda14cbcSMatt Macy 2203*eda14cbcSMatt Macy dnode_dirty_l1range(dn, first, last, tx); 2204*eda14cbcSMatt Macy 2205*eda14cbcSMatt Macy int shift = dn->dn_datablkshift + dn->dn_indblkshift - 2206*eda14cbcSMatt Macy SPA_BLKPTRSHIFT; 2207*eda14cbcSMatt Macy for (uint64_t i = first + 1; i < last; i++) { 2208*eda14cbcSMatt Macy /* 2209*eda14cbcSMatt Macy * Set i to the blockid of the next non-hole 2210*eda14cbcSMatt Macy * level-1 indirect block at or after i. Note 2211*eda14cbcSMatt Macy * that dnode_next_offset() operates in terms of 2212*eda14cbcSMatt Macy * level-0-equivalent bytes. 2213*eda14cbcSMatt Macy */ 2214*eda14cbcSMatt Macy uint64_t ibyte = i << shift; 2215*eda14cbcSMatt Macy int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK, 2216*eda14cbcSMatt Macy &ibyte, 2, 1, 0); 2217*eda14cbcSMatt Macy i = ibyte >> shift; 2218*eda14cbcSMatt Macy if (i >= last) 2219*eda14cbcSMatt Macy break; 2220*eda14cbcSMatt Macy 2221*eda14cbcSMatt Macy /* 2222*eda14cbcSMatt Macy * Normally we should not see an error, either 2223*eda14cbcSMatt Macy * from dnode_next_offset() or dbuf_hold_level() 2224*eda14cbcSMatt Macy * (except for ESRCH from dnode_next_offset). 2225*eda14cbcSMatt Macy * If there is an i/o error, then when we read 2226*eda14cbcSMatt Macy * this block in syncing context, it will use 2227*eda14cbcSMatt Macy * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according 2228*eda14cbcSMatt Macy * to the "failmode" property. dnode_next_offset() 2229*eda14cbcSMatt Macy * doesn't have a flag to indicate MUSTSUCCEED. 2230*eda14cbcSMatt Macy */ 2231*eda14cbcSMatt Macy if (err != 0) 2232*eda14cbcSMatt Macy break; 2233*eda14cbcSMatt Macy 2234*eda14cbcSMatt Macy dnode_dirty_l1(dn, i, tx); 2235*eda14cbcSMatt Macy } 2236*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2237*eda14cbcSMatt Macy } 2238*eda14cbcSMatt Macy 2239*eda14cbcSMatt Macy done: 2240*eda14cbcSMatt Macy /* 2241*eda14cbcSMatt Macy * Add this range to the dnode range list. 2242*eda14cbcSMatt Macy * We will finish up this free operation in the syncing phase. 2243*eda14cbcSMatt Macy */ 2244*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2245*eda14cbcSMatt Macy { 2246*eda14cbcSMatt Macy int txgoff = tx->tx_txg & TXG_MASK; 2247*eda14cbcSMatt Macy if (dn->dn_free_ranges[txgoff] == NULL) { 2248*eda14cbcSMatt Macy dn->dn_free_ranges[txgoff] = range_tree_create(NULL, 2249*eda14cbcSMatt Macy RANGE_SEG64, NULL, 0, 0); 2250*eda14cbcSMatt Macy } 2251*eda14cbcSMatt Macy range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks); 2252*eda14cbcSMatt Macy range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks); 2253*eda14cbcSMatt Macy } 2254*eda14cbcSMatt Macy dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 2255*eda14cbcSMatt Macy blkid, nblks, tx->tx_txg); 2256*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2257*eda14cbcSMatt Macy 2258*eda14cbcSMatt Macy dbuf_free_range(dn, blkid, blkid + nblks - 1, tx); 2259*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 2260*eda14cbcSMatt Macy } 2261*eda14cbcSMatt Macy 2262*eda14cbcSMatt Macy static boolean_t 2263*eda14cbcSMatt Macy dnode_spill_freed(dnode_t *dn) 2264*eda14cbcSMatt Macy { 2265*eda14cbcSMatt Macy int i; 2266*eda14cbcSMatt Macy 2267*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2268*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 2269*eda14cbcSMatt Macy if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK) 2270*eda14cbcSMatt Macy break; 2271*eda14cbcSMatt Macy } 2272*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2273*eda14cbcSMatt Macy return (i < TXG_SIZE); 2274*eda14cbcSMatt Macy } 2275*eda14cbcSMatt Macy 2276*eda14cbcSMatt Macy /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 2277*eda14cbcSMatt Macy uint64_t 2278*eda14cbcSMatt Macy dnode_block_freed(dnode_t *dn, uint64_t blkid) 2279*eda14cbcSMatt Macy { 2280*eda14cbcSMatt Macy void *dp = spa_get_dsl(dn->dn_objset->os_spa); 2281*eda14cbcSMatt Macy int i; 2282*eda14cbcSMatt Macy 2283*eda14cbcSMatt Macy if (blkid == DMU_BONUS_BLKID) 2284*eda14cbcSMatt Macy return (FALSE); 2285*eda14cbcSMatt Macy 2286*eda14cbcSMatt Macy /* 2287*eda14cbcSMatt Macy * If we're in the process of opening the pool, dp will not be 2288*eda14cbcSMatt Macy * set yet, but there shouldn't be anything dirty. 2289*eda14cbcSMatt Macy */ 2290*eda14cbcSMatt Macy if (dp == NULL) 2291*eda14cbcSMatt Macy return (FALSE); 2292*eda14cbcSMatt Macy 2293*eda14cbcSMatt Macy if (dn->dn_free_txg) 2294*eda14cbcSMatt Macy return (TRUE); 2295*eda14cbcSMatt Macy 2296*eda14cbcSMatt Macy if (blkid == DMU_SPILL_BLKID) 2297*eda14cbcSMatt Macy return (dnode_spill_freed(dn)); 2298*eda14cbcSMatt Macy 2299*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2300*eda14cbcSMatt Macy for (i = 0; i < TXG_SIZE; i++) { 2301*eda14cbcSMatt Macy if (dn->dn_free_ranges[i] != NULL && 2302*eda14cbcSMatt Macy range_tree_contains(dn->dn_free_ranges[i], blkid, 1)) 2303*eda14cbcSMatt Macy break; 2304*eda14cbcSMatt Macy } 2305*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2306*eda14cbcSMatt Macy return (i < TXG_SIZE); 2307*eda14cbcSMatt Macy } 2308*eda14cbcSMatt Macy 2309*eda14cbcSMatt Macy /* call from syncing context when we actually write/free space for this dnode */ 2310*eda14cbcSMatt Macy void 2311*eda14cbcSMatt Macy dnode_diduse_space(dnode_t *dn, int64_t delta) 2312*eda14cbcSMatt Macy { 2313*eda14cbcSMatt Macy uint64_t space; 2314*eda14cbcSMatt Macy dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n", 2315*eda14cbcSMatt Macy dn, dn->dn_phys, 2316*eda14cbcSMatt Macy (u_longlong_t)dn->dn_phys->dn_used, 2317*eda14cbcSMatt Macy (longlong_t)delta); 2318*eda14cbcSMatt Macy 2319*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2320*eda14cbcSMatt Macy space = DN_USED_BYTES(dn->dn_phys); 2321*eda14cbcSMatt Macy if (delta > 0) { 2322*eda14cbcSMatt Macy ASSERT3U(space + delta, >=, space); /* no overflow */ 2323*eda14cbcSMatt Macy } else { 2324*eda14cbcSMatt Macy ASSERT3U(space, >=, -delta); /* no underflow */ 2325*eda14cbcSMatt Macy } 2326*eda14cbcSMatt Macy space += delta; 2327*eda14cbcSMatt Macy if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) { 2328*eda14cbcSMatt Macy ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0); 2329*eda14cbcSMatt Macy ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT)); 2330*eda14cbcSMatt Macy dn->dn_phys->dn_used = space >> DEV_BSHIFT; 2331*eda14cbcSMatt Macy } else { 2332*eda14cbcSMatt Macy dn->dn_phys->dn_used = space; 2333*eda14cbcSMatt Macy dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES; 2334*eda14cbcSMatt Macy } 2335*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2336*eda14cbcSMatt Macy } 2337*eda14cbcSMatt Macy 2338*eda14cbcSMatt Macy /* 2339*eda14cbcSMatt Macy * Scans a block at the indicated "level" looking for a hole or data, 2340*eda14cbcSMatt Macy * depending on 'flags'. 2341*eda14cbcSMatt Macy * 2342*eda14cbcSMatt Macy * If level > 0, then we are scanning an indirect block looking at its 2343*eda14cbcSMatt Macy * pointers. If level == 0, then we are looking at a block of dnodes. 2344*eda14cbcSMatt Macy * 2345*eda14cbcSMatt Macy * If we don't find what we are looking for in the block, we return ESRCH. 2346*eda14cbcSMatt Macy * Otherwise, return with *offset pointing to the beginning (if searching 2347*eda14cbcSMatt Macy * forwards) or end (if searching backwards) of the range covered by the 2348*eda14cbcSMatt Macy * block pointer we matched on (or dnode). 2349*eda14cbcSMatt Macy * 2350*eda14cbcSMatt Macy * The basic search algorithm used below by dnode_next_offset() is to 2351*eda14cbcSMatt Macy * use this function to search up the block tree (widen the search) until 2352*eda14cbcSMatt Macy * we find something (i.e., we don't return ESRCH) and then search back 2353*eda14cbcSMatt Macy * down the tree (narrow the search) until we reach our original search 2354*eda14cbcSMatt Macy * level. 2355*eda14cbcSMatt Macy */ 2356*eda14cbcSMatt Macy static int 2357*eda14cbcSMatt Macy dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset, 2358*eda14cbcSMatt Macy int lvl, uint64_t blkfill, uint64_t txg) 2359*eda14cbcSMatt Macy { 2360*eda14cbcSMatt Macy dmu_buf_impl_t *db = NULL; 2361*eda14cbcSMatt Macy void *data = NULL; 2362*eda14cbcSMatt Macy uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 2363*eda14cbcSMatt Macy uint64_t epb = 1ULL << epbs; 2364*eda14cbcSMatt Macy uint64_t minfill, maxfill; 2365*eda14cbcSMatt Macy boolean_t hole; 2366*eda14cbcSMatt Macy int i, inc, error, span; 2367*eda14cbcSMatt Macy 2368*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2369*eda14cbcSMatt Macy 2370*eda14cbcSMatt Macy hole = ((flags & DNODE_FIND_HOLE) != 0); 2371*eda14cbcSMatt Macy inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1; 2372*eda14cbcSMatt Macy ASSERT(txg == 0 || !hole); 2373*eda14cbcSMatt Macy 2374*eda14cbcSMatt Macy if (lvl == dn->dn_phys->dn_nlevels) { 2375*eda14cbcSMatt Macy error = 0; 2376*eda14cbcSMatt Macy epb = dn->dn_phys->dn_nblkptr; 2377*eda14cbcSMatt Macy data = dn->dn_phys->dn_blkptr; 2378*eda14cbcSMatt Macy } else { 2379*eda14cbcSMatt Macy uint64_t blkid = dbuf_whichblock(dn, lvl, *offset); 2380*eda14cbcSMatt Macy error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db); 2381*eda14cbcSMatt Macy if (error) { 2382*eda14cbcSMatt Macy if (error != ENOENT) 2383*eda14cbcSMatt Macy return (error); 2384*eda14cbcSMatt Macy if (hole) 2385*eda14cbcSMatt Macy return (0); 2386*eda14cbcSMatt Macy /* 2387*eda14cbcSMatt Macy * This can only happen when we are searching up 2388*eda14cbcSMatt Macy * the block tree for data. We don't really need to 2389*eda14cbcSMatt Macy * adjust the offset, as we will just end up looking 2390*eda14cbcSMatt Macy * at the pointer to this block in its parent, and its 2391*eda14cbcSMatt Macy * going to be unallocated, so we will skip over it. 2392*eda14cbcSMatt Macy */ 2393*eda14cbcSMatt Macy return (SET_ERROR(ESRCH)); 2394*eda14cbcSMatt Macy } 2395*eda14cbcSMatt Macy error = dbuf_read(db, NULL, 2396*eda14cbcSMatt Macy DB_RF_CANFAIL | DB_RF_HAVESTRUCT | DB_RF_NO_DECRYPT); 2397*eda14cbcSMatt Macy if (error) { 2398*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 2399*eda14cbcSMatt Macy return (error); 2400*eda14cbcSMatt Macy } 2401*eda14cbcSMatt Macy data = db->db.db_data; 2402*eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_READER); 2403*eda14cbcSMatt Macy } 2404*eda14cbcSMatt Macy 2405*eda14cbcSMatt Macy if (db != NULL && txg != 0 && (db->db_blkptr == NULL || 2406*eda14cbcSMatt Macy db->db_blkptr->blk_birth <= txg || 2407*eda14cbcSMatt Macy BP_IS_HOLE(db->db_blkptr))) { 2408*eda14cbcSMatt Macy /* 2409*eda14cbcSMatt Macy * This can only happen when we are searching up the tree 2410*eda14cbcSMatt Macy * and these conditions mean that we need to keep climbing. 2411*eda14cbcSMatt Macy */ 2412*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2413*eda14cbcSMatt Macy } else if (lvl == 0) { 2414*eda14cbcSMatt Macy dnode_phys_t *dnp = data; 2415*eda14cbcSMatt Macy 2416*eda14cbcSMatt Macy ASSERT(dn->dn_type == DMU_OT_DNODE); 2417*eda14cbcSMatt Macy ASSERT(!(flags & DNODE_FIND_BACKWARDS)); 2418*eda14cbcSMatt Macy 2419*eda14cbcSMatt Macy for (i = (*offset >> DNODE_SHIFT) & (blkfill - 1); 2420*eda14cbcSMatt Macy i < blkfill; i += dnp[i].dn_extra_slots + 1) { 2421*eda14cbcSMatt Macy if ((dnp[i].dn_type == DMU_OT_NONE) == hole) 2422*eda14cbcSMatt Macy break; 2423*eda14cbcSMatt Macy } 2424*eda14cbcSMatt Macy 2425*eda14cbcSMatt Macy if (i == blkfill) 2426*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2427*eda14cbcSMatt Macy 2428*eda14cbcSMatt Macy *offset = (*offset & ~(DNODE_BLOCK_SIZE - 1)) + 2429*eda14cbcSMatt Macy (i << DNODE_SHIFT); 2430*eda14cbcSMatt Macy } else { 2431*eda14cbcSMatt Macy blkptr_t *bp = data; 2432*eda14cbcSMatt Macy uint64_t start = *offset; 2433*eda14cbcSMatt Macy span = (lvl - 1) * epbs + dn->dn_datablkshift; 2434*eda14cbcSMatt Macy minfill = 0; 2435*eda14cbcSMatt Macy maxfill = blkfill << ((lvl - 1) * epbs); 2436*eda14cbcSMatt Macy 2437*eda14cbcSMatt Macy if (hole) 2438*eda14cbcSMatt Macy maxfill--; 2439*eda14cbcSMatt Macy else 2440*eda14cbcSMatt Macy minfill++; 2441*eda14cbcSMatt Macy 2442*eda14cbcSMatt Macy if (span >= 8 * sizeof (*offset)) { 2443*eda14cbcSMatt Macy /* This only happens on the highest indirection level */ 2444*eda14cbcSMatt Macy ASSERT3U((lvl - 1), ==, dn->dn_phys->dn_nlevels - 1); 2445*eda14cbcSMatt Macy *offset = 0; 2446*eda14cbcSMatt Macy } else { 2447*eda14cbcSMatt Macy *offset = *offset >> span; 2448*eda14cbcSMatt Macy } 2449*eda14cbcSMatt Macy 2450*eda14cbcSMatt Macy for (i = BF64_GET(*offset, 0, epbs); 2451*eda14cbcSMatt Macy i >= 0 && i < epb; i += inc) { 2452*eda14cbcSMatt Macy if (BP_GET_FILL(&bp[i]) >= minfill && 2453*eda14cbcSMatt Macy BP_GET_FILL(&bp[i]) <= maxfill && 2454*eda14cbcSMatt Macy (hole || bp[i].blk_birth > txg)) 2455*eda14cbcSMatt Macy break; 2456*eda14cbcSMatt Macy if (inc > 0 || *offset > 0) 2457*eda14cbcSMatt Macy *offset += inc; 2458*eda14cbcSMatt Macy } 2459*eda14cbcSMatt Macy 2460*eda14cbcSMatt Macy if (span >= 8 * sizeof (*offset)) { 2461*eda14cbcSMatt Macy *offset = start; 2462*eda14cbcSMatt Macy } else { 2463*eda14cbcSMatt Macy *offset = *offset << span; 2464*eda14cbcSMatt Macy } 2465*eda14cbcSMatt Macy 2466*eda14cbcSMatt Macy if (inc < 0) { 2467*eda14cbcSMatt Macy /* traversing backwards; position offset at the end */ 2468*eda14cbcSMatt Macy ASSERT3U(*offset, <=, start); 2469*eda14cbcSMatt Macy *offset = MIN(*offset + (1ULL << span) - 1, start); 2470*eda14cbcSMatt Macy } else if (*offset < start) { 2471*eda14cbcSMatt Macy *offset = start; 2472*eda14cbcSMatt Macy } 2473*eda14cbcSMatt Macy if (i < 0 || i >= epb) 2474*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2475*eda14cbcSMatt Macy } 2476*eda14cbcSMatt Macy 2477*eda14cbcSMatt Macy if (db != NULL) { 2478*eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 2479*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 2480*eda14cbcSMatt Macy } 2481*eda14cbcSMatt Macy 2482*eda14cbcSMatt Macy return (error); 2483*eda14cbcSMatt Macy } 2484*eda14cbcSMatt Macy 2485*eda14cbcSMatt Macy /* 2486*eda14cbcSMatt Macy * Find the next hole, data, or sparse region at or after *offset. 2487*eda14cbcSMatt Macy * The value 'blkfill' tells us how many items we expect to find 2488*eda14cbcSMatt Macy * in an L0 data block; this value is 1 for normal objects, 2489*eda14cbcSMatt Macy * DNODES_PER_BLOCK for the meta dnode, and some fraction of 2490*eda14cbcSMatt Macy * DNODES_PER_BLOCK when searching for sparse regions thereof. 2491*eda14cbcSMatt Macy * 2492*eda14cbcSMatt Macy * Examples: 2493*eda14cbcSMatt Macy * 2494*eda14cbcSMatt Macy * dnode_next_offset(dn, flags, offset, 1, 1, 0); 2495*eda14cbcSMatt Macy * Finds the next/previous hole/data in a file. 2496*eda14cbcSMatt Macy * Used in dmu_offset_next(). 2497*eda14cbcSMatt Macy * 2498*eda14cbcSMatt Macy * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg); 2499*eda14cbcSMatt Macy * Finds the next free/allocated dnode an objset's meta-dnode. 2500*eda14cbcSMatt Macy * Only finds objects that have new contents since txg (ie. 2501*eda14cbcSMatt Macy * bonus buffer changes and content removal are ignored). 2502*eda14cbcSMatt Macy * Used in dmu_object_next(). 2503*eda14cbcSMatt Macy * 2504*eda14cbcSMatt Macy * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0); 2505*eda14cbcSMatt Macy * Finds the next L2 meta-dnode bp that's at most 1/4 full. 2506*eda14cbcSMatt Macy * Used in dmu_object_alloc(). 2507*eda14cbcSMatt Macy */ 2508*eda14cbcSMatt Macy int 2509*eda14cbcSMatt Macy dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset, 2510*eda14cbcSMatt Macy int minlvl, uint64_t blkfill, uint64_t txg) 2511*eda14cbcSMatt Macy { 2512*eda14cbcSMatt Macy uint64_t initial_offset = *offset; 2513*eda14cbcSMatt Macy int lvl, maxlvl; 2514*eda14cbcSMatt Macy int error = 0; 2515*eda14cbcSMatt Macy 2516*eda14cbcSMatt Macy if (!(flags & DNODE_FIND_HAVELOCK)) 2517*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 2518*eda14cbcSMatt Macy 2519*eda14cbcSMatt Macy if (dn->dn_phys->dn_nlevels == 0) { 2520*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2521*eda14cbcSMatt Macy goto out; 2522*eda14cbcSMatt Macy } 2523*eda14cbcSMatt Macy 2524*eda14cbcSMatt Macy if (dn->dn_datablkshift == 0) { 2525*eda14cbcSMatt Macy if (*offset < dn->dn_datablksz) { 2526*eda14cbcSMatt Macy if (flags & DNODE_FIND_HOLE) 2527*eda14cbcSMatt Macy *offset = dn->dn_datablksz; 2528*eda14cbcSMatt Macy } else { 2529*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2530*eda14cbcSMatt Macy } 2531*eda14cbcSMatt Macy goto out; 2532*eda14cbcSMatt Macy } 2533*eda14cbcSMatt Macy 2534*eda14cbcSMatt Macy maxlvl = dn->dn_phys->dn_nlevels; 2535*eda14cbcSMatt Macy 2536*eda14cbcSMatt Macy for (lvl = minlvl; lvl <= maxlvl; lvl++) { 2537*eda14cbcSMatt Macy error = dnode_next_offset_level(dn, 2538*eda14cbcSMatt Macy flags, offset, lvl, blkfill, txg); 2539*eda14cbcSMatt Macy if (error != ESRCH) 2540*eda14cbcSMatt Macy break; 2541*eda14cbcSMatt Macy } 2542*eda14cbcSMatt Macy 2543*eda14cbcSMatt Macy while (error == 0 && --lvl >= minlvl) { 2544*eda14cbcSMatt Macy error = dnode_next_offset_level(dn, 2545*eda14cbcSMatt Macy flags, offset, lvl, blkfill, txg); 2546*eda14cbcSMatt Macy } 2547*eda14cbcSMatt Macy 2548*eda14cbcSMatt Macy /* 2549*eda14cbcSMatt Macy * There's always a "virtual hole" at the end of the object, even 2550*eda14cbcSMatt Macy * if all BP's which physically exist are non-holes. 2551*eda14cbcSMatt Macy */ 2552*eda14cbcSMatt Macy if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 && 2553*eda14cbcSMatt Macy minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) { 2554*eda14cbcSMatt Macy error = 0; 2555*eda14cbcSMatt Macy } 2556*eda14cbcSMatt Macy 2557*eda14cbcSMatt Macy if (error == 0 && (flags & DNODE_FIND_BACKWARDS ? 2558*eda14cbcSMatt Macy initial_offset < *offset : initial_offset > *offset)) 2559*eda14cbcSMatt Macy error = SET_ERROR(ESRCH); 2560*eda14cbcSMatt Macy out: 2561*eda14cbcSMatt Macy if (!(flags & DNODE_FIND_HAVELOCK)) 2562*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2563*eda14cbcSMatt Macy 2564*eda14cbcSMatt Macy return (error); 2565*eda14cbcSMatt Macy } 2566*eda14cbcSMatt Macy 2567*eda14cbcSMatt Macy #if defined(_KERNEL) 2568*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_hold); 2569*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_rele); 2570*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_set_nlevels); 2571*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_set_blksz); 2572*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_free_range); 2573*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_evict_dbufs); 2574*eda14cbcSMatt Macy EXPORT_SYMBOL(dnode_evict_bonus); 2575*eda14cbcSMatt Macy #endif 2576