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; 413547Smaybee int txgoff = tx->tx_txg & TXG_MASK; 423547Smaybee int nblkptr = dn->dn_phys->dn_nblkptr; 433547Smaybee int old_toplvl = dn->dn_phys->dn_nlevels - 1; 443547Smaybee int new_level = dn->dn_next_nlevels[txgoff]; 45789Sahrens int i; 46789Sahrens 473547Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 483547Smaybee 493547Smaybee /* 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)); 523547Smaybee 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 573547Smaybee dn->dn_phys->dn_nlevels = new_level; 584312Sgw25295 dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, 594312Sgw25295 dn->dn_object, dn->dn_phys->dn_nlevels); 60789Sahrens 613547Smaybee /* check for existing blkptrs in the dnode */ 623547Smaybee for (i = 0; i < nblkptr; i++) 633547Smaybee if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 643547Smaybee break; 653547Smaybee if (i != nblkptr) { 663547Smaybee /* transfer dnode's block pointers to new indirect block */ 673547Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 683547Smaybee ASSERT(db->db.db_data); 693547Smaybee ASSERT(arc_released(db->db_buf)); 703547Smaybee ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 713547Smaybee bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 723547Smaybee sizeof (blkptr_t) * nblkptr); 733547Smaybee arc_buf_freeze(db->db_buf); 743547Smaybee } 753547Smaybee 76789Sahrens /* set dbuf's parent pointers to new indirect buf */ 773547Smaybee for (i = 0; i < nblkptr; i++) { 783547Smaybee dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); 793547Smaybee 80789Sahrens if (child == NULL) 81789Sahrens continue; 823547Smaybee ASSERT3P(child->db_dnode, ==, dn); 833547Smaybee if (child->db_parent && child->db_parent != dn->dn_dbuf) { 843547Smaybee ASSERT(child->db_parent->db_level == db->db_level); 853547Smaybee ASSERT(child->db_blkptr != 863547Smaybee &dn->dn_phys->dn_blkptr[child->db_blkid]); 87789Sahrens mutex_exit(&child->db_mtx); 88789Sahrens continue; 89789Sahrens } 903547Smaybee ASSERT(child->db_parent == NULL || 913547Smaybee child->db_parent == dn->dn_dbuf); 92789Sahrens 933547Smaybee child->db_parent = db; 943547Smaybee dbuf_add_ref(db, child); 953547Smaybee if (db->db.db_data) 963547Smaybee child->db_blkptr = (blkptr_t *)db->db.db_data + i; 973547Smaybee else 983547Smaybee child->db_blkptr = NULL; 993547Smaybee dprintf_dbuf_bp(child, child->db_blkptr, 1003547Smaybee "changed db_blkptr to new indirect %s", ""); 101789Sahrens 102789Sahrens mutex_exit(&child->db_mtx); 103789Sahrens } 104789Sahrens 1053547Smaybee bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 106789Sahrens 1071544Seschrock dbuf_rele(db, FTAG); 1083547Smaybee 1093547Smaybee rw_exit(&dn->dn_struct_rwlock); 110789Sahrens } 111789Sahrens 112789Sahrens static void 113789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 114789Sahrens { 115789Sahrens objset_impl_t *os = dn->dn_objset; 116789Sahrens uint64_t bytesfreed = 0; 117789Sahrens int i; 118789Sahrens 119789Sahrens dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num); 120789Sahrens 121789Sahrens for (i = 0; i < num; i++, bp++) { 122789Sahrens if (BP_IS_HOLE(bp)) 123789Sahrens continue; 124789Sahrens 1252082Seschrock bytesfreed += bp_get_dasize(os->os_spa, bp); 1262082Seschrock ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 1273547Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, bp, dn->dn_zio, tx); 1283547Smaybee bzero(bp, sizeof (blkptr_t)); 129789Sahrens } 130789Sahrens dnode_diduse_space(dn, -bytesfreed); 131789Sahrens } 132789Sahrens 133873Sek110237 #ifdef ZFS_DEBUG 134789Sahrens static void 135789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 136789Sahrens { 137789Sahrens int off, num; 138789Sahrens int i, err, epbs; 139789Sahrens uint64_t txg = tx->tx_txg; 140789Sahrens 141789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 142789Sahrens off = start - (db->db_blkid * 1<<epbs); 143789Sahrens num = end - start + 1; 144789Sahrens 145789Sahrens ASSERT3U(off, >=, 0); 146789Sahrens ASSERT3U(num, >=, 0); 147789Sahrens ASSERT3U(db->db_level, >, 0); 148789Sahrens ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift); 149789Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 150789Sahrens ASSERT(db->db_blkptr != NULL); 151789Sahrens 152789Sahrens for (i = off; i < off+num; i++) { 153789Sahrens uint64_t *buf; 1543547Smaybee dmu_buf_impl_t *child; 1553547Smaybee dbuf_dirty_record_t *dr; 156789Sahrens int j; 157789Sahrens 158789Sahrens ASSERT(db->db_level == 1); 159789Sahrens 160789Sahrens rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 161789Sahrens err = dbuf_hold_impl(db->db_dnode, db->db_level-1, 1624312Sgw25295 (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 163789Sahrens rw_exit(&db->db_dnode->dn_struct_rwlock); 164789Sahrens if (err == ENOENT) 165789Sahrens continue; 166789Sahrens ASSERT(err == 0); 167789Sahrens ASSERT(child->db_level == 0); 1683547Smaybee dr = child->db_last_dirty; 1693547Smaybee while (dr && dr->dr_txg > txg) 1703547Smaybee dr = dr->dr_next; 1713547Smaybee ASSERT(dr == NULL || dr->dr_txg == txg); 172789Sahrens 1733547Smaybee /* data_old better be zeroed */ 1743547Smaybee if (dr) { 1753547Smaybee buf = dr->dt.dl.dr_data->b_data; 176789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 177789Sahrens if (buf[j] != 0) { 178789Sahrens panic("freed data not zero: " 179789Sahrens "child=%p i=%d off=%d num=%d\n", 180789Sahrens child, i, off, num); 181789Sahrens } 182789Sahrens } 183789Sahrens } 184789Sahrens 185789Sahrens /* 186789Sahrens * db_data better be zeroed unless it's dirty in a 187789Sahrens * future txg. 188789Sahrens */ 189789Sahrens mutex_enter(&child->db_mtx); 190789Sahrens buf = child->db.db_data; 191789Sahrens if (buf != NULL && child->db_state != DB_FILL && 1923547Smaybee child->db_last_dirty == NULL) { 193789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 194789Sahrens if (buf[j] != 0) { 195789Sahrens panic("freed data not zero: " 196789Sahrens "child=%p i=%d off=%d num=%d\n", 197789Sahrens child, i, off, num); 198789Sahrens } 199789Sahrens } 200789Sahrens } 201789Sahrens mutex_exit(&child->db_mtx); 202789Sahrens 2031544Seschrock dbuf_rele(child, FTAG); 204789Sahrens } 205873Sek110237 } 206789Sahrens #endif 207789Sahrens 208789Sahrens static int 209789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 210789Sahrens dmu_tx_t *tx) 211789Sahrens { 212789Sahrens dnode_t *dn = db->db_dnode; 213789Sahrens blkptr_t *bp; 214789Sahrens dmu_buf_impl_t *subdb; 215789Sahrens uint64_t start, end, dbstart, dbend, i; 216789Sahrens int epbs, shift, err; 217789Sahrens int all = TRUE; 218789Sahrens 2191544Seschrock (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 220789Sahrens arc_release(db->db_buf, db); 221789Sahrens bp = (blkptr_t *)db->db.db_data; 222789Sahrens 223789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 224789Sahrens shift = (db->db_level - 1) * epbs; 225789Sahrens dbstart = db->db_blkid << epbs; 226789Sahrens start = blkid >> shift; 227789Sahrens if (dbstart < start) { 228789Sahrens bp += start - dbstart; 229789Sahrens all = FALSE; 230789Sahrens } else { 231789Sahrens start = dbstart; 232789Sahrens } 233789Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 234789Sahrens end = (blkid + nblks - 1) >> shift; 235789Sahrens if (dbend <= end) 236789Sahrens end = dbend; 237789Sahrens else if (all) 238789Sahrens all = trunc; 239789Sahrens ASSERT3U(start, <=, end); 240789Sahrens 241789Sahrens if (db->db_level == 1) { 242873Sek110237 FREE_VERIFY(db, start, end, tx); 243789Sahrens free_blocks(dn, bp, end-start+1, tx); 2443093Sahrens arc_buf_freeze(db->db_buf); 2453547Smaybee ASSERT(all || db->db_last_dirty); 246789Sahrens return (all); 247789Sahrens } 248789Sahrens 249789Sahrens for (i = start; i <= end; i++, bp++) { 250789Sahrens if (BP_IS_HOLE(bp)) 251789Sahrens continue; 252789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 253789Sahrens err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 254789Sahrens ASSERT3U(err, ==, 0); 255789Sahrens rw_exit(&dn->dn_struct_rwlock); 256789Sahrens 257789Sahrens if (free_children(subdb, blkid, nblks, trunc, tx)) { 258789Sahrens ASSERT3P(subdb->db_blkptr, ==, bp); 259789Sahrens free_blocks(dn, bp, 1, tx); 2601163Smaybee } else { 2611163Smaybee all = FALSE; 262789Sahrens } 2631544Seschrock dbuf_rele(subdb, FTAG); 264789Sahrens } 2653093Sahrens arc_buf_freeze(db->db_buf); 266789Sahrens #ifdef ZFS_DEBUG 267789Sahrens bp -= (end-start)+1; 268789Sahrens for (i = start; i <= end; i++, bp++) { 269789Sahrens if (i == start && blkid != 0) 270789Sahrens continue; 271789Sahrens else if (i == end && !trunc) 272789Sahrens continue; 273789Sahrens ASSERT3U(bp->blk_birth, ==, 0); 274789Sahrens } 275789Sahrens #endif 2763547Smaybee ASSERT(all || db->db_last_dirty); 277789Sahrens return (all); 278789Sahrens } 279789Sahrens 280789Sahrens /* 281789Sahrens * free_range: Traverse the indicated range of the provided file 282789Sahrens * and "free" all the blocks contained there. 283789Sahrens */ 284789Sahrens static void 285789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 286789Sahrens { 287789Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 288789Sahrens dmu_buf_impl_t *db; 289789Sahrens int trunc, start, end, shift, i, err; 290789Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 291789Sahrens 292789Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 293789Sahrens return; 294789Sahrens 295789Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 296789Sahrens trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 297789Sahrens if (trunc) 298789Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 299789Sahrens 300789Sahrens /* There are no indirect blocks in the object */ 301789Sahrens if (dnlevel == 1) { 302789Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 303789Sahrens /* this range was never made persistent */ 304789Sahrens return; 305789Sahrens } 306789Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 307789Sahrens free_blocks(dn, bp + blkid, nblks, tx); 308789Sahrens if (trunc) { 309789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 310789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 311789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 312789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 313789Sahrens dn->dn_phys->dn_maxblkid == 0 || 3143025Sahrens dnode_next_offset(dn, FALSE, &off, 3153025Sahrens 1, 1, 0) != 0); 316789Sahrens } 317789Sahrens return; 318789Sahrens } 319789Sahrens 320789Sahrens shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 321789Sahrens start = blkid >> shift; 322789Sahrens ASSERT(start < dn->dn_phys->dn_nblkptr); 323789Sahrens end = (blkid + nblks - 1) >> shift; 324789Sahrens bp += start; 325789Sahrens for (i = start; i <= end; i++, bp++) { 326789Sahrens if (BP_IS_HOLE(bp)) 327789Sahrens continue; 328789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 329789Sahrens err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 330789Sahrens ASSERT3U(err, ==, 0); 331789Sahrens rw_exit(&dn->dn_struct_rwlock); 332789Sahrens 333789Sahrens if (free_children(db, blkid, nblks, trunc, tx)) { 334789Sahrens ASSERT3P(db->db_blkptr, ==, bp); 335789Sahrens free_blocks(dn, bp, 1, tx); 336789Sahrens } 3371544Seschrock dbuf_rele(db, FTAG); 338789Sahrens } 339789Sahrens if (trunc) { 340789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 341789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 342789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 343789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 344789Sahrens dn->dn_phys->dn_maxblkid == 0 || 3453025Sahrens dnode_next_offset(dn, FALSE, &off, 1, 1, 0) != 0); 346789Sahrens } 347789Sahrens } 348789Sahrens 3491544Seschrock /* 3501544Seschrock * Try to kick all the dnodes dbufs out of the cache... 3511544Seschrock */ 352*4944Smaybee void 353*4944Smaybee dnode_evict_dbufs(dnode_t *dn) 3541544Seschrock { 3551596Sahrens int progress; 3561596Sahrens int pass = 0; 3571596Sahrens 3581596Sahrens do { 3591602Smaybee dmu_buf_impl_t *db, marker; 3601596Sahrens int evicting = FALSE; 3611544Seschrock 3621596Sahrens progress = FALSE; 3631596Sahrens mutex_enter(&dn->dn_dbufs_mtx); 3641602Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 3651602Smaybee db = list_head(&dn->dn_dbufs); 3661602Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 3671602Smaybee list_remove(&dn->dn_dbufs, db); 3681602Smaybee list_insert_tail(&dn->dn_dbufs, db); 3694312Sgw25295 ASSERT3P(db->db_dnode, ==, dn); 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 4001544Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 4011544Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 4021544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 4031544Seschrock dbuf_evict(dn->dn_bonus); 4041544Seschrock dn->dn_bonus = NULL; 4051544Seschrock } 4061544Seschrock rw_exit(&dn->dn_struct_rwlock); 4071544Seschrock } 4081544Seschrock 4093547Smaybee static void 4103547Smaybee dnode_undirty_dbufs(list_t *list) 4113547Smaybee { 4123547Smaybee dbuf_dirty_record_t *dr; 4133547Smaybee 4143547Smaybee while (dr = list_head(list)) { 4153547Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 4163547Smaybee uint64_t txg = dr->dr_txg; 4173547Smaybee 4183547Smaybee mutex_enter(&db->db_mtx); 4193547Smaybee /* XXX - use dbuf_undirty()? */ 4203547Smaybee list_remove(list, dr); 4213547Smaybee ASSERT(db->db_last_dirty == dr); 4223547Smaybee db->db_last_dirty = NULL; 4233547Smaybee db->db_dirtycnt -= 1; 4243547Smaybee if (db->db_level == 0) { 4253547Smaybee ASSERT(db->db_blkid == DB_BONUS_BLKID || 4263547Smaybee dr->dt.dl.dr_data == db->db_buf); 4273547Smaybee dbuf_unoverride(dr); 4283547Smaybee mutex_exit(&db->db_mtx); 4293547Smaybee } else { 4303547Smaybee mutex_exit(&db->db_mtx); 4313547Smaybee dnode_undirty_dbufs(&dr->dt.di.dr_children); 4323547Smaybee } 4333547Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 4343547Smaybee dbuf_rele(db, (void *)(uintptr_t)txg); 4353547Smaybee } 4363547Smaybee } 4373547Smaybee 4383547Smaybee static void 439789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 440789Sahrens { 441789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 442789Sahrens 443789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 444789Sahrens 4453547Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 446*4944Smaybee dnode_evict_dbufs(dn); 4471544Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 4481544Seschrock 4491544Seschrock /* 4501544Seschrock * XXX - It would be nice to assert this, but we may still 4511544Seschrock * have residual holds from async evictions from the arc... 4521544Seschrock * 4533444Sek110237 * zfs_obj_to_path() also depends on this being 4543444Sek110237 * commented out. 4553444Sek110237 * 4561544Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 4571544Seschrock */ 458789Sahrens 459789Sahrens /* Undirty next bits */ 460789Sahrens dn->dn_next_nlevels[txgoff] = 0; 461789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 4621596Sahrens dn->dn_next_blksz[txgoff] = 0; 463789Sahrens 464789Sahrens /* free up all the blocks in the file. */ 465789Sahrens dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 4662082Seschrock ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0); 467789Sahrens 468789Sahrens /* ASSERT(blkptrs are zero); */ 469789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 470789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 471789Sahrens 472789Sahrens ASSERT(dn->dn_free_txg > 0); 473789Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 474789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 475789Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 476789Sahrens 477789Sahrens mutex_enter(&dn->dn_mtx); 478789Sahrens dn->dn_type = DMU_OT_NONE; 479789Sahrens dn->dn_maxblkid = 0; 480789Sahrens dn->dn_allocated_txg = 0; 4814480Sgw25295 dn->dn_free_txg = 0; 482789Sahrens mutex_exit(&dn->dn_mtx); 483789Sahrens 4841544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 485789Sahrens 486789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 487789Sahrens /* 488789Sahrens * Now that we've released our hold, the dnode may 489789Sahrens * be evicted, so we musn't access it. 490789Sahrens */ 491789Sahrens } 492789Sahrens 493789Sahrens /* 4943547Smaybee * Write out the dnode's dirty buffers. 495789Sahrens * 496789Sahrens * NOTE: The dnode is kept in memory by being dirty. Once the 497789Sahrens * dirty bit is cleared, it may be evicted. Beware of this! 498789Sahrens */ 4993547Smaybee void 5003547Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 501789Sahrens { 502789Sahrens free_range_t *rp; 5033547Smaybee dnode_phys_t *dnp = dn->dn_phys; 504789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 5053547Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 506789Sahrens 507789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 508789Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 509873Sek110237 DNODE_VERIFY(dn); 5101596Sahrens 5113547Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 512789Sahrens 513789Sahrens mutex_enter(&dn->dn_mtx); 514789Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 515789Sahrens /* The dnode is newly allocated or reallocated */ 516789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 517789Sahrens /* this is a first alloc, not a realloc */ 518789Sahrens /* XXX shouldn't the phys already be zeroed? */ 519789Sahrens bzero(dnp, DNODE_CORE_SIZE); 520789Sahrens dnp->dn_nlevels = 1; 521789Sahrens } 522789Sahrens 523789Sahrens if (dn->dn_nblkptr > dnp->dn_nblkptr) { 524789Sahrens /* zero the new blkptrs we are gaining */ 525789Sahrens bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 526789Sahrens sizeof (blkptr_t) * 527789Sahrens (dn->dn_nblkptr - dnp->dn_nblkptr)); 528789Sahrens } 529789Sahrens dnp->dn_type = dn->dn_type; 530789Sahrens dnp->dn_bonustype = dn->dn_bonustype; 531789Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 532789Sahrens dnp->dn_nblkptr = dn->dn_nblkptr; 533789Sahrens } 534789Sahrens 5353547Smaybee ASSERT(dnp->dn_nlevels > 1 || 5361599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5371599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 5381599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5391599Sahrens 5401596Sahrens if (dn->dn_next_blksz[txgoff]) { 5411596Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 542789Sahrens SPA_MINBLOCKSIZE) == 0); 5431599Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5443547Smaybee list_head(list) != NULL || 5451600Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 5461600Sahrens dnp->dn_datablkszsec); 547789Sahrens dnp->dn_datablkszsec = 5481596Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 5491596Sahrens dn->dn_next_blksz[txgoff] = 0; 550789Sahrens } 551789Sahrens 552*4944Smaybee if (dn->dn_next_bonuslen[txgoff]) { 553*4944Smaybee if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) 554*4944Smaybee dnp->dn_bonuslen = 0; 555*4944Smaybee else 556*4944Smaybee dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; 557*4944Smaybee ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); 558*4944Smaybee dn->dn_next_bonuslen[txgoff] = 0; 559*4944Smaybee } 560*4944Smaybee 561789Sahrens if (dn->dn_next_indblkshift[txgoff]) { 562789Sahrens ASSERT(dnp->dn_nlevels == 1); 563789Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 564789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 565789Sahrens } 566789Sahrens 567789Sahrens /* 568789Sahrens * Just take the live (open-context) values for checksum and compress. 569789Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 570789Sahrens * start using the new checksum or compress algorithm a little early. 571789Sahrens */ 572789Sahrens dnp->dn_checksum = dn->dn_checksum; 573789Sahrens dnp->dn_compress = dn->dn_compress; 574789Sahrens 575789Sahrens mutex_exit(&dn->dn_mtx); 576789Sahrens 577789Sahrens /* process all the "freed" ranges in the file */ 578789Sahrens if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 5791163Smaybee for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 5801163Smaybee rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 581789Sahrens dnode_sync_free_range(dn, 582789Sahrens rp->fr_blkid, rp->fr_nblks, tx); 583789Sahrens } 584*4944Smaybee /* grab the mutex so we don't race with dnode_block_freed() */ 585789Sahrens mutex_enter(&dn->dn_mtx); 586789Sahrens for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 587*4944Smaybee 588789Sahrens free_range_t *last = rp; 589789Sahrens rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 590789Sahrens avl_remove(&dn->dn_ranges[txgoff], last); 591789Sahrens kmem_free(last, sizeof (free_range_t)); 592789Sahrens } 593789Sahrens mutex_exit(&dn->dn_mtx); 594789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 5953547Smaybee dnode_sync_free(dn, tx); 5963547Smaybee return; 597789Sahrens } 598789Sahrens 599789Sahrens if (dn->dn_next_nlevels[txgoff]) { 6003547Smaybee dnode_increase_indirection(dn, tx); 601789Sahrens dn->dn_next_nlevels[txgoff] = 0; 602789Sahrens } 603789Sahrens 6043547Smaybee dbuf_sync_list(list, tx); 605789Sahrens 6063547Smaybee if (dn->dn_object != DMU_META_DNODE_OBJECT) { 6073547Smaybee ASSERT3P(list_head(list), ==, NULL); 6083547Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 6093547Smaybee } 6101599Sahrens 6113547Smaybee /* 6123547Smaybee * Although we have dropped our reference to the dnode, it 6133547Smaybee * can't be evicted until its written, and we haven't yet 6143547Smaybee * initiated the IO for the dnode's dbuf. 6153547Smaybee */ 616789Sahrens } 617