1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 223444Sek110237 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/dbuf.h> 30789Sahrens #include <sys/dnode.h> 31789Sahrens #include <sys/dmu.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/dmu_objset.h> 34789Sahrens #include <sys/dsl_dataset.h> 35789Sahrens #include <sys/spa.h> 36789Sahrens 37789Sahrens static void 38789Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 39789Sahrens { 40789Sahrens dmu_buf_impl_t *db; 41*3547Smaybee int txgoff = tx->tx_txg & TXG_MASK; 42*3547Smaybee int nblkptr = dn->dn_phys->dn_nblkptr; 43*3547Smaybee int old_toplvl = dn->dn_phys->dn_nlevels - 1; 44*3547Smaybee int new_level = dn->dn_next_nlevels[txgoff]; 45789Sahrens int i; 46789Sahrens 47*3547Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 48*3547Smaybee 49*3547Smaybee /* this dnode can't be paged out because it's dirty */ 50789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 51789Sahrens ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 52*3547Smaybee ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); 53789Sahrens 54789Sahrens db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 551544Seschrock ASSERT(db != NULL); 56789Sahrens 57*3547Smaybee dn->dn_phys->dn_nlevels = new_level; 58789Sahrens dprintf("os=%p obj=%llu, increase to %d\n", 59789Sahrens dn->dn_objset, dn->dn_object, 60789Sahrens dn->dn_phys->dn_nlevels); 61789Sahrens 62*3547Smaybee /* check for existing blkptrs in the dnode */ 63*3547Smaybee for (i = 0; i < nblkptr; i++) 64*3547Smaybee if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 65*3547Smaybee break; 66*3547Smaybee if (i != nblkptr) { 67*3547Smaybee /* transfer dnode's block pointers to new indirect block */ 68*3547Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 69*3547Smaybee ASSERT(db->db.db_data); 70*3547Smaybee ASSERT(arc_released(db->db_buf)); 71*3547Smaybee ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 72*3547Smaybee bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 73*3547Smaybee sizeof (blkptr_t) * nblkptr); 74*3547Smaybee arc_buf_freeze(db->db_buf); 75*3547Smaybee } 76*3547Smaybee 77789Sahrens /* set dbuf's parent pointers to new indirect buf */ 78*3547Smaybee for (i = 0; i < nblkptr; i++) { 79*3547Smaybee dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); 80*3547Smaybee 81789Sahrens if (child == NULL) 82789Sahrens continue; 83*3547Smaybee ASSERT3P(child->db_dnode, ==, dn); 84*3547Smaybee if (child->db_parent && child->db_parent != dn->dn_dbuf) { 85*3547Smaybee ASSERT(child->db_parent->db_level == db->db_level); 86*3547Smaybee ASSERT(child->db_blkptr != 87*3547Smaybee &dn->dn_phys->dn_blkptr[child->db_blkid]); 88789Sahrens mutex_exit(&child->db_mtx); 89789Sahrens continue; 90789Sahrens } 91*3547Smaybee ASSERT(child->db_parent == NULL || 92*3547Smaybee child->db_parent == dn->dn_dbuf); 93789Sahrens 94*3547Smaybee child->db_parent = db; 95*3547Smaybee dbuf_add_ref(db, child); 96*3547Smaybee if (db->db.db_data) 97*3547Smaybee child->db_blkptr = (blkptr_t *)db->db.db_data + i; 98*3547Smaybee else 99*3547Smaybee child->db_blkptr = NULL; 100*3547Smaybee dprintf_dbuf_bp(child, child->db_blkptr, 101*3547Smaybee "changed db_blkptr to new indirect %s", ""); 102789Sahrens 103789Sahrens mutex_exit(&child->db_mtx); 104789Sahrens } 105789Sahrens 106*3547Smaybee bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 107789Sahrens 1081544Seschrock dbuf_rele(db, FTAG); 109*3547Smaybee 110*3547Smaybee rw_exit(&dn->dn_struct_rwlock); 111789Sahrens } 112789Sahrens 113789Sahrens static void 114789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 115789Sahrens { 116789Sahrens objset_impl_t *os = dn->dn_objset; 117789Sahrens uint64_t bytesfreed = 0; 118789Sahrens int i; 119789Sahrens 120789Sahrens dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num); 121789Sahrens 122789Sahrens for (i = 0; i < num; i++, bp++) { 123789Sahrens if (BP_IS_HOLE(bp)) 124789Sahrens continue; 125789Sahrens 1262082Seschrock bytesfreed += bp_get_dasize(os->os_spa, bp); 1272082Seschrock ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 128*3547Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, bp, dn->dn_zio, tx); 129*3547Smaybee bzero(bp, sizeof (blkptr_t)); 130789Sahrens } 131789Sahrens dnode_diduse_space(dn, -bytesfreed); 132789Sahrens } 133789Sahrens 134873Sek110237 #ifdef ZFS_DEBUG 135789Sahrens static void 136789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 137789Sahrens { 138789Sahrens int off, num; 139789Sahrens int i, err, epbs; 140789Sahrens uint64_t txg = tx->tx_txg; 141789Sahrens 142789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 143789Sahrens off = start - (db->db_blkid * 1<<epbs); 144789Sahrens num = end - start + 1; 145789Sahrens 146789Sahrens ASSERT3U(off, >=, 0); 147789Sahrens ASSERT3U(num, >=, 0); 148789Sahrens ASSERT3U(db->db_level, >, 0); 149789Sahrens ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift); 150789Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 151789Sahrens ASSERT(db->db_blkptr != NULL); 152789Sahrens 153789Sahrens for (i = off; i < off+num; i++) { 154789Sahrens uint64_t *buf; 155*3547Smaybee dmu_buf_impl_t *child; 156*3547Smaybee dbuf_dirty_record_t *dr; 157789Sahrens int j; 158789Sahrens 159789Sahrens ASSERT(db->db_level == 1); 160789Sahrens 161789Sahrens rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 162789Sahrens err = dbuf_hold_impl(db->db_dnode, db->db_level-1, 163789Sahrens (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 164789Sahrens rw_exit(&db->db_dnode->dn_struct_rwlock); 165789Sahrens if (err == ENOENT) 166789Sahrens continue; 167789Sahrens ASSERT(err == 0); 168789Sahrens ASSERT(child->db_level == 0); 169*3547Smaybee dr = child->db_last_dirty; 170*3547Smaybee while (dr && dr->dr_txg > txg) 171*3547Smaybee dr = dr->dr_next; 172*3547Smaybee ASSERT(dr == NULL || dr->dr_txg == txg); 173789Sahrens 174*3547Smaybee /* data_old better be zeroed */ 175*3547Smaybee if (dr) { 176*3547Smaybee buf = dr->dt.dl.dr_data->b_data; 177789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 178789Sahrens if (buf[j] != 0) { 179789Sahrens panic("freed data not zero: " 180789Sahrens "child=%p i=%d off=%d num=%d\n", 181789Sahrens child, i, off, num); 182789Sahrens } 183789Sahrens } 184789Sahrens } 185789Sahrens 186789Sahrens /* 187789Sahrens * db_data better be zeroed unless it's dirty in a 188789Sahrens * future txg. 189789Sahrens */ 190789Sahrens mutex_enter(&child->db_mtx); 191789Sahrens buf = child->db.db_data; 192789Sahrens if (buf != NULL && child->db_state != DB_FILL && 193*3547Smaybee child->db_last_dirty == NULL) { 194789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 195789Sahrens if (buf[j] != 0) { 196789Sahrens panic("freed data not zero: " 197789Sahrens "child=%p i=%d off=%d num=%d\n", 198789Sahrens child, i, off, num); 199789Sahrens } 200789Sahrens } 201789Sahrens } 202789Sahrens mutex_exit(&child->db_mtx); 203789Sahrens 2041544Seschrock dbuf_rele(child, FTAG); 205789Sahrens } 206873Sek110237 } 207789Sahrens #endif 208789Sahrens 209789Sahrens static int 210789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 211789Sahrens dmu_tx_t *tx) 212789Sahrens { 213789Sahrens dnode_t *dn = db->db_dnode; 214789Sahrens blkptr_t *bp; 215789Sahrens dmu_buf_impl_t *subdb; 216789Sahrens uint64_t start, end, dbstart, dbend, i; 217789Sahrens int epbs, shift, err; 218789Sahrens int all = TRUE; 219789Sahrens 2201544Seschrock (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 221789Sahrens arc_release(db->db_buf, db); 222789Sahrens bp = (blkptr_t *)db->db.db_data; 223789Sahrens 224789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 225789Sahrens shift = (db->db_level - 1) * epbs; 226789Sahrens dbstart = db->db_blkid << epbs; 227789Sahrens start = blkid >> shift; 228789Sahrens if (dbstart < start) { 229789Sahrens bp += start - dbstart; 230789Sahrens all = FALSE; 231789Sahrens } else { 232789Sahrens start = dbstart; 233789Sahrens } 234789Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 235789Sahrens end = (blkid + nblks - 1) >> shift; 236789Sahrens if (dbend <= end) 237789Sahrens end = dbend; 238789Sahrens else if (all) 239789Sahrens all = trunc; 240789Sahrens ASSERT3U(start, <=, end); 241789Sahrens 242789Sahrens if (db->db_level == 1) { 243873Sek110237 FREE_VERIFY(db, start, end, tx); 244789Sahrens free_blocks(dn, bp, end-start+1, tx); 2453093Sahrens arc_buf_freeze(db->db_buf); 246*3547Smaybee ASSERT(all || db->db_last_dirty); 247789Sahrens return (all); 248789Sahrens } 249789Sahrens 250789Sahrens for (i = start; i <= end; i++, bp++) { 251789Sahrens if (BP_IS_HOLE(bp)) 252789Sahrens continue; 253789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 254789Sahrens err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 255789Sahrens ASSERT3U(err, ==, 0); 256789Sahrens rw_exit(&dn->dn_struct_rwlock); 257789Sahrens 258789Sahrens if (free_children(subdb, blkid, nblks, trunc, tx)) { 259789Sahrens ASSERT3P(subdb->db_blkptr, ==, bp); 260789Sahrens free_blocks(dn, bp, 1, tx); 2611163Smaybee } else { 2621163Smaybee all = FALSE; 263789Sahrens } 2641544Seschrock dbuf_rele(subdb, FTAG); 265789Sahrens } 2663093Sahrens arc_buf_freeze(db->db_buf); 267789Sahrens #ifdef ZFS_DEBUG 268789Sahrens bp -= (end-start)+1; 269789Sahrens for (i = start; i <= end; i++, bp++) { 270789Sahrens if (i == start && blkid != 0) 271789Sahrens continue; 272789Sahrens else if (i == end && !trunc) 273789Sahrens continue; 274789Sahrens ASSERT3U(bp->blk_birth, ==, 0); 275789Sahrens } 276789Sahrens #endif 277*3547Smaybee ASSERT(all || db->db_last_dirty); 278789Sahrens return (all); 279789Sahrens } 280789Sahrens 281789Sahrens /* 282789Sahrens * free_range: Traverse the indicated range of the provided file 283789Sahrens * and "free" all the blocks contained there. 284789Sahrens */ 285789Sahrens static void 286789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 287789Sahrens { 288789Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 289789Sahrens dmu_buf_impl_t *db; 290789Sahrens int trunc, start, end, shift, i, err; 291789Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 292789Sahrens 293789Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 294789Sahrens return; 295789Sahrens 296789Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 297789Sahrens trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 298789Sahrens if (trunc) 299789Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 300789Sahrens 301789Sahrens /* There are no indirect blocks in the object */ 302789Sahrens if (dnlevel == 1) { 303789Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 304789Sahrens /* this range was never made persistent */ 305789Sahrens return; 306789Sahrens } 307789Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 308789Sahrens free_blocks(dn, bp + blkid, nblks, tx); 309789Sahrens if (trunc) { 310789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 311789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 312789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 313789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 314789Sahrens dn->dn_phys->dn_maxblkid == 0 || 3153025Sahrens dnode_next_offset(dn, FALSE, &off, 3163025Sahrens 1, 1, 0) != 0); 317789Sahrens } 318789Sahrens return; 319789Sahrens } 320789Sahrens 321789Sahrens shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 322789Sahrens start = blkid >> shift; 323789Sahrens ASSERT(start < dn->dn_phys->dn_nblkptr); 324789Sahrens end = (blkid + nblks - 1) >> shift; 325789Sahrens bp += start; 326789Sahrens for (i = start; i <= end; i++, bp++) { 327789Sahrens if (BP_IS_HOLE(bp)) 328789Sahrens continue; 329789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 330789Sahrens err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 331789Sahrens ASSERT3U(err, ==, 0); 332789Sahrens rw_exit(&dn->dn_struct_rwlock); 333789Sahrens 334789Sahrens if (free_children(db, blkid, nblks, trunc, tx)) { 335789Sahrens ASSERT3P(db->db_blkptr, ==, bp); 336789Sahrens free_blocks(dn, bp, 1, tx); 337789Sahrens } 3381544Seschrock dbuf_rele(db, FTAG); 339789Sahrens } 340789Sahrens if (trunc) { 341789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 342789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 343789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 344789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 345789Sahrens dn->dn_phys->dn_maxblkid == 0 || 3463025Sahrens dnode_next_offset(dn, FALSE, &off, 1, 1, 0) != 0); 347789Sahrens } 348789Sahrens } 349789Sahrens 3501544Seschrock /* 3511544Seschrock * Try to kick all the dnodes dbufs out of the cache... 3521544Seschrock */ 3531646Sperrin int 3541646Sperrin dnode_evict_dbufs(dnode_t *dn, int try) 3551544Seschrock { 3561596Sahrens int progress; 3571596Sahrens int pass = 0; 3581596Sahrens 3591596Sahrens do { 3601602Smaybee dmu_buf_impl_t *db, marker; 3611596Sahrens int evicting = FALSE; 3621544Seschrock 3631596Sahrens progress = FALSE; 3641596Sahrens mutex_enter(&dn->dn_dbufs_mtx); 3651602Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 3661602Smaybee db = list_head(&dn->dn_dbufs); 3671602Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 3681602Smaybee list_remove(&dn->dn_dbufs, db); 3691602Smaybee list_insert_tail(&dn->dn_dbufs, db); 3701596Sahrens 3711544Seschrock mutex_enter(&db->db_mtx); 3721596Sahrens if (db->db_state == DB_EVICTING) { 3731596Sahrens progress = TRUE; 3741596Sahrens evicting = TRUE; 3751596Sahrens mutex_exit(&db->db_mtx); 3761596Sahrens } else if (refcount_is_zero(&db->db_holds)) { 3771596Sahrens progress = TRUE; 3781596Sahrens ASSERT(!arc_released(db->db_buf)); 3791596Sahrens dbuf_clear(db); /* exits db_mtx for us */ 3801596Sahrens } else { 3811596Sahrens mutex_exit(&db->db_mtx); 3821596Sahrens } 3831596Sahrens 3841544Seschrock } 3851602Smaybee list_remove(&dn->dn_dbufs, &marker); 3861596Sahrens /* 3871596Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 3881596Sahrens * that any DB_EVICTING dbufs can make progress. 3891596Sahrens * Ideally, we would have some cv we could wait on, but 3901596Sahrens * since we don't, just wait a bit to give the other 3911596Sahrens * thread a chance to run. 3921596Sahrens */ 3931596Sahrens mutex_exit(&dn->dn_dbufs_mtx); 3941596Sahrens if (evicting) 3951596Sahrens delay(1); 3961596Sahrens pass++; 3971596Sahrens ASSERT(pass < 100); /* sanity check */ 3981596Sahrens } while (progress); 3991596Sahrens 4001596Sahrens /* 4011646Sperrin * This function works fine even if it can't evict everything. 4021646Sperrin * If were only asked to try to evict everything then 4031646Sperrin * return an error if we can't. Otherwise panic as the caller 4041646Sperrin * expects total eviction. 4051596Sahrens */ 4061596Sahrens if (list_head(&dn->dn_dbufs) != NULL) { 4071646Sperrin if (try) { 4081646Sperrin return (1); 4091646Sperrin } else { 4101646Sperrin panic("dangling dbufs (dn=%p, dbuf=%p)\n", 4111646Sperrin dn, list_head(&dn->dn_dbufs)); 4121646Sperrin } 4131544Seschrock } 4141596Sahrens 4151544Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 4161544Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 4171544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 4181544Seschrock dbuf_evict(dn->dn_bonus); 4191544Seschrock dn->dn_bonus = NULL; 4201544Seschrock } 4211544Seschrock rw_exit(&dn->dn_struct_rwlock); 4221646Sperrin return (0); 4231544Seschrock } 4241544Seschrock 425*3547Smaybee static void 426*3547Smaybee dnode_undirty_dbufs(list_t *list) 427*3547Smaybee { 428*3547Smaybee dbuf_dirty_record_t *dr; 429*3547Smaybee 430*3547Smaybee while (dr = list_head(list)) { 431*3547Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 432*3547Smaybee uint64_t txg = dr->dr_txg; 433*3547Smaybee 434*3547Smaybee mutex_enter(&db->db_mtx); 435*3547Smaybee /* XXX - use dbuf_undirty()? */ 436*3547Smaybee list_remove(list, dr); 437*3547Smaybee ASSERT(db->db_last_dirty == dr); 438*3547Smaybee db->db_last_dirty = NULL; 439*3547Smaybee db->db_dirtycnt -= 1; 440*3547Smaybee if (db->db_level == 0) { 441*3547Smaybee ASSERT(db->db_blkid == DB_BONUS_BLKID || 442*3547Smaybee dr->dt.dl.dr_data == db->db_buf); 443*3547Smaybee dbuf_unoverride(dr); 444*3547Smaybee mutex_exit(&db->db_mtx); 445*3547Smaybee } else { 446*3547Smaybee mutex_exit(&db->db_mtx); 447*3547Smaybee dnode_undirty_dbufs(&dr->dt.di.dr_children); 448*3547Smaybee } 449*3547Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 450*3547Smaybee dbuf_rele(db, (void *)(uintptr_t)txg); 451*3547Smaybee } 452*3547Smaybee } 453*3547Smaybee 454*3547Smaybee static void 455789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 456789Sahrens { 457789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 458789Sahrens 459789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 460789Sahrens 461*3547Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 4621646Sperrin (void) dnode_evict_dbufs(dn, 0); 4631544Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 4641544Seschrock 4651544Seschrock /* 4661544Seschrock * XXX - It would be nice to assert this, but we may still 4671544Seschrock * have residual holds from async evictions from the arc... 4681544Seschrock * 4693444Sek110237 * zfs_obj_to_path() also depends on this being 4703444Sek110237 * commented out. 4713444Sek110237 * 4721544Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 4731544Seschrock */ 474789Sahrens 475789Sahrens /* Undirty next bits */ 476789Sahrens dn->dn_next_nlevels[txgoff] = 0; 477789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 4781596Sahrens dn->dn_next_blksz[txgoff] = 0; 479789Sahrens 480789Sahrens /* free up all the blocks in the file. */ 481789Sahrens dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 4822082Seschrock ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0); 483789Sahrens 484789Sahrens /* ASSERT(blkptrs are zero); */ 485789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 486789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 487789Sahrens 488789Sahrens ASSERT(dn->dn_free_txg > 0); 489789Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 490789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 491789Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 492789Sahrens 493789Sahrens mutex_enter(&dn->dn_mtx); 494789Sahrens dn->dn_type = DMU_OT_NONE; 495789Sahrens dn->dn_maxblkid = 0; 496789Sahrens dn->dn_allocated_txg = 0; 497789Sahrens mutex_exit(&dn->dn_mtx); 498789Sahrens 4991544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 500789Sahrens 501789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 502789Sahrens /* 503789Sahrens * Now that we've released our hold, the dnode may 504789Sahrens * be evicted, so we musn't access it. 505789Sahrens */ 506789Sahrens } 507789Sahrens 508789Sahrens /* 509*3547Smaybee * Write out the dnode's dirty buffers. 510789Sahrens * 511789Sahrens * NOTE: The dnode is kept in memory by being dirty. Once the 512789Sahrens * dirty bit is cleared, it may be evicted. Beware of this! 513789Sahrens */ 514*3547Smaybee void 515*3547Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 516789Sahrens { 517789Sahrens free_range_t *rp; 518*3547Smaybee dnode_phys_t *dnp = dn->dn_phys; 519789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 520*3547Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 521789Sahrens 522789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 523789Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 524873Sek110237 DNODE_VERIFY(dn); 5251596Sahrens 526*3547Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 527789Sahrens 528789Sahrens mutex_enter(&dn->dn_mtx); 529789Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 530789Sahrens /* The dnode is newly allocated or reallocated */ 531789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 532789Sahrens /* this is a first alloc, not a realloc */ 533789Sahrens /* XXX shouldn't the phys already be zeroed? */ 534789Sahrens bzero(dnp, DNODE_CORE_SIZE); 535789Sahrens dnp->dn_nlevels = 1; 536789Sahrens } 537789Sahrens 538789Sahrens if (dn->dn_nblkptr > dnp->dn_nblkptr) { 539789Sahrens /* zero the new blkptrs we are gaining */ 540789Sahrens bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 541789Sahrens sizeof (blkptr_t) * 542789Sahrens (dn->dn_nblkptr - dnp->dn_nblkptr)); 543789Sahrens } 544789Sahrens dnp->dn_type = dn->dn_type; 545789Sahrens dnp->dn_bonustype = dn->dn_bonustype; 546789Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 547789Sahrens dnp->dn_nblkptr = dn->dn_nblkptr; 548789Sahrens } 549789Sahrens 550*3547Smaybee ASSERT(dnp->dn_nlevels > 1 || 5511599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5521599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 5531599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5541599Sahrens 5551596Sahrens if (dn->dn_next_blksz[txgoff]) { 5561596Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 557789Sahrens SPA_MINBLOCKSIZE) == 0); 5581599Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 559*3547Smaybee list_head(list) != NULL || 5601600Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 5611600Sahrens dnp->dn_datablkszsec); 562789Sahrens dnp->dn_datablkszsec = 5631596Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 5641596Sahrens dn->dn_next_blksz[txgoff] = 0; 565789Sahrens } 566789Sahrens 567789Sahrens if (dn->dn_next_indblkshift[txgoff]) { 568789Sahrens ASSERT(dnp->dn_nlevels == 1); 569789Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 570789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 571789Sahrens } 572789Sahrens 573789Sahrens /* 574789Sahrens * Just take the live (open-context) values for checksum and compress. 575789Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 576789Sahrens * start using the new checksum or compress algorithm a little early. 577789Sahrens */ 578789Sahrens dnp->dn_checksum = dn->dn_checksum; 579789Sahrens dnp->dn_compress = dn->dn_compress; 580789Sahrens 581789Sahrens mutex_exit(&dn->dn_mtx); 582789Sahrens 583789Sahrens /* process all the "freed" ranges in the file */ 584789Sahrens if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 5851163Smaybee for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 5861163Smaybee rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 587789Sahrens dnode_sync_free_range(dn, 588789Sahrens rp->fr_blkid, rp->fr_nblks, tx); 589789Sahrens } 590789Sahrens mutex_enter(&dn->dn_mtx); 591789Sahrens for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 592789Sahrens free_range_t *last = rp; 593789Sahrens rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 594789Sahrens avl_remove(&dn->dn_ranges[txgoff], last); 595789Sahrens kmem_free(last, sizeof (free_range_t)); 596789Sahrens } 597789Sahrens mutex_exit(&dn->dn_mtx); 598789Sahrens 599789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 600*3547Smaybee dnode_sync_free(dn, tx); 601*3547Smaybee return; 602789Sahrens } 603789Sahrens 604789Sahrens if (dn->dn_next_nlevels[txgoff]) { 605*3547Smaybee dnode_increase_indirection(dn, tx); 606789Sahrens dn->dn_next_nlevels[txgoff] = 0; 607789Sahrens } 608789Sahrens 609*3547Smaybee dbuf_sync_list(list, tx); 610789Sahrens 611*3547Smaybee if (dn->dn_object != DMU_META_DNODE_OBJECT) { 612*3547Smaybee ASSERT3P(list_head(list), ==, NULL); 613*3547Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 614*3547Smaybee } 6151599Sahrens 616*3547Smaybee /* 617*3547Smaybee * Although we have dropped our reference to the dnode, it 618*3547Smaybee * can't be evicted until its written, and we haven't yet 619*3547Smaybee * initiated the IO for the dnode's dbuf. 620*3547Smaybee */ 621789Sahrens } 622