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 /* 221199Seschrock * Copyright 2006 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 #include <sys/zio.h> 37789Sahrens 38789Sahrens static void 39789Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 40789Sahrens { 41789Sahrens dmu_buf_impl_t *db; 42789Sahrens int i; 43789Sahrens uint64_t txg = tx->tx_txg; 44789Sahrens 45789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 46789Sahrens ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 47789Sahrens /* this dnode can't be paged out because it's dirty */ 48789Sahrens 49789Sahrens db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 501544Seschrock ASSERT(db != NULL); 51789Sahrens for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) 52789Sahrens if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 53789Sahrens break; 54789Sahrens if (i != dn->dn_phys->dn_nblkptr) { 55789Sahrens ASSERT(list_link_active(&db->db_dirty_node[txg&TXG_MASK])); 56789Sahrens 571544Seschrock (void) dbuf_read(db, NULL, 581544Seschrock DB_RF_HAVESTRUCT | DB_RF_MUST_SUCCEED); 59789Sahrens arc_release(db->db_buf, db); 60789Sahrens /* copy dnode's block pointers to new indirect block */ 61789Sahrens ASSERT3U(sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr, <=, 62789Sahrens db->db.db_size); 63789Sahrens bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 64789Sahrens sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr); 65789Sahrens } 66789Sahrens 67789Sahrens dn->dn_phys->dn_nlevels += 1; 68789Sahrens dprintf("os=%p obj=%llu, increase to %d\n", 69789Sahrens dn->dn_objset, dn->dn_object, 70789Sahrens dn->dn_phys->dn_nlevels); 71789Sahrens 72789Sahrens /* set dbuf's parent pointers to new indirect buf */ 73789Sahrens for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) { 74789Sahrens dmu_buf_impl_t *child = 75789Sahrens dbuf_find(dn, dn->dn_phys->dn_nlevels-2, i); 76789Sahrens if (child == NULL) 77789Sahrens continue; 78789Sahrens if (child->db_dnode == NULL) { 79789Sahrens mutex_exit(&child->db_mtx); 80789Sahrens continue; 81789Sahrens } 82789Sahrens 83789Sahrens if (child->db_parent == NULL || 84789Sahrens child->db_parent == dn->dn_dbuf) { 85789Sahrens dprintf_dbuf_bp(child, child->db_blkptr, 86789Sahrens "changing db_blkptr to new indirect %s", ""); 87789Sahrens child->db_parent = db; 88789Sahrens dbuf_add_ref(db, child); 89789Sahrens if (db->db.db_data) { 90789Sahrens child->db_blkptr = 91789Sahrens (blkptr_t *)db->db.db_data + i; 92789Sahrens } else { 93789Sahrens child->db_blkptr = NULL; 94789Sahrens } 95789Sahrens dprintf_dbuf_bp(child, child->db_blkptr, 96789Sahrens "changed db_blkptr to new indirect %s", ""); 97789Sahrens } 98789Sahrens ASSERT3P(child->db_parent, ==, db); 99789Sahrens 100789Sahrens mutex_exit(&child->db_mtx); 101789Sahrens } 102789Sahrens 103789Sahrens bzero(dn->dn_phys->dn_blkptr, 104789Sahrens sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr); 105789Sahrens 1061544Seschrock dbuf_rele(db, FTAG); 107789Sahrens } 108789Sahrens 109789Sahrens static void 110789Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 111789Sahrens { 112789Sahrens objset_impl_t *os = dn->dn_objset; 113789Sahrens uint64_t bytesfreed = 0; 114789Sahrens int i; 115789Sahrens 116789Sahrens dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num); 117789Sahrens 118789Sahrens for (i = 0; i < num; i++, bp++) { 119789Sahrens if (BP_IS_HOLE(bp)) 120789Sahrens continue; 121789Sahrens 122789Sahrens bytesfreed += BP_GET_ASIZE(bp); 123789Sahrens ASSERT3U(bytesfreed >> DEV_BSHIFT, <=, dn->dn_phys->dn_secphys); 124789Sahrens dsl_dataset_block_kill(os->os_dsl_dataset, bp, tx); 125789Sahrens } 126789Sahrens dnode_diduse_space(dn, -bytesfreed); 127789Sahrens } 128789Sahrens 129873Sek110237 #ifdef ZFS_DEBUG 130789Sahrens static void 131789Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 132789Sahrens { 133789Sahrens int off, num; 134789Sahrens int i, err, epbs; 135789Sahrens uint64_t txg = tx->tx_txg; 136789Sahrens 137789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 138789Sahrens off = start - (db->db_blkid * 1<<epbs); 139789Sahrens num = end - start + 1; 140789Sahrens 141789Sahrens ASSERT3U(off, >=, 0); 142789Sahrens ASSERT3U(num, >=, 0); 143789Sahrens ASSERT3U(db->db_level, >, 0); 144789Sahrens ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift); 145789Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 146789Sahrens ASSERT(db->db_blkptr != NULL); 147789Sahrens 148789Sahrens for (i = off; i < off+num; i++) { 149789Sahrens uint64_t *buf; 150789Sahrens int j; 151789Sahrens dmu_buf_impl_t *child; 152789Sahrens 153789Sahrens ASSERT(db->db_level == 1); 154789Sahrens 155789Sahrens rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 156789Sahrens err = dbuf_hold_impl(db->db_dnode, db->db_level-1, 157789Sahrens (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 158789Sahrens rw_exit(&db->db_dnode->dn_struct_rwlock); 159789Sahrens if (err == ENOENT) 160789Sahrens continue; 161789Sahrens ASSERT(err == 0); 162789Sahrens ASSERT(child->db_level == 0); 163789Sahrens ASSERT(!list_link_active(&child->db_dirty_node[txg&TXG_MASK])); 164789Sahrens 165789Sahrens /* db_data_old better be zeroed */ 166789Sahrens if (child->db_d.db_data_old[txg & TXG_MASK]) { 1671544Seschrock buf = ((arc_buf_t *)child->db_d.db_data_old 1681544Seschrock [txg & TXG_MASK])->b_data; 169789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 170789Sahrens if (buf[j] != 0) { 171789Sahrens panic("freed data not zero: " 172789Sahrens "child=%p i=%d off=%d num=%d\n", 173789Sahrens child, i, off, num); 174789Sahrens } 175789Sahrens } 176789Sahrens } 177789Sahrens 178789Sahrens /* 179789Sahrens * db_data better be zeroed unless it's dirty in a 180789Sahrens * future txg. 181789Sahrens */ 182789Sahrens mutex_enter(&child->db_mtx); 183789Sahrens buf = child->db.db_data; 184789Sahrens if (buf != NULL && child->db_state != DB_FILL && 185789Sahrens !list_link_active(&child->db_dirty_node 186789Sahrens [(txg+1) & TXG_MASK]) && 187789Sahrens !list_link_active(&child->db_dirty_node 188789Sahrens [(txg+2) & TXG_MASK])) { 189789Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 190789Sahrens if (buf[j] != 0) { 191789Sahrens panic("freed data not zero: " 192789Sahrens "child=%p i=%d off=%d num=%d\n", 193789Sahrens child, i, off, num); 194789Sahrens } 195789Sahrens } 196789Sahrens } 197789Sahrens mutex_exit(&child->db_mtx); 198789Sahrens 1991544Seschrock dbuf_rele(child, FTAG); 200789Sahrens } 201873Sek110237 } 202789Sahrens #endif 203789Sahrens 204789Sahrens static int 205789Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 206789Sahrens dmu_tx_t *tx) 207789Sahrens { 208789Sahrens dnode_t *dn = db->db_dnode; 209789Sahrens blkptr_t *bp; 210789Sahrens dmu_buf_impl_t *subdb; 211789Sahrens uint64_t start, end, dbstart, dbend, i; 212789Sahrens int epbs, shift, err; 2131163Smaybee int txgoff = tx->tx_txg & TXG_MASK; 214789Sahrens int all = TRUE; 215789Sahrens 2161544Seschrock (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 217789Sahrens arc_release(db->db_buf, db); 218789Sahrens bp = (blkptr_t *)db->db.db_data; 219789Sahrens 220789Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 221789Sahrens shift = (db->db_level - 1) * epbs; 222789Sahrens dbstart = db->db_blkid << epbs; 223789Sahrens start = blkid >> shift; 224789Sahrens if (dbstart < start) { 225789Sahrens bp += start - dbstart; 226789Sahrens all = FALSE; 227789Sahrens } else { 228789Sahrens start = dbstart; 229789Sahrens } 230789Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 231789Sahrens end = (blkid + nblks - 1) >> shift; 232789Sahrens if (dbend <= end) 233789Sahrens end = dbend; 234789Sahrens else if (all) 235789Sahrens all = trunc; 236789Sahrens ASSERT3U(start, <=, end); 237789Sahrens 238789Sahrens if (db->db_level == 1) { 239873Sek110237 FREE_VERIFY(db, start, end, tx); 240789Sahrens free_blocks(dn, bp, end-start+1, tx); 2411163Smaybee ASSERT(all || list_link_active(&db->db_dirty_node[txgoff])); 242789Sahrens return (all); 243789Sahrens } 244789Sahrens 245789Sahrens for (i = start; i <= end; i++, bp++) { 246789Sahrens if (BP_IS_HOLE(bp)) 247789Sahrens continue; 248789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 249789Sahrens err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 250789Sahrens ASSERT3U(err, ==, 0); 251789Sahrens rw_exit(&dn->dn_struct_rwlock); 252789Sahrens 253789Sahrens if (free_children(subdb, blkid, nblks, trunc, tx)) { 254789Sahrens ASSERT3P(subdb->db_blkptr, ==, bp); 255789Sahrens free_blocks(dn, bp, 1, tx); 2561163Smaybee } else { 2571163Smaybee all = FALSE; 258789Sahrens } 2591544Seschrock dbuf_rele(subdb, FTAG); 260789Sahrens } 261789Sahrens #ifdef ZFS_DEBUG 262789Sahrens bp -= (end-start)+1; 263789Sahrens for (i = start; i <= end; i++, bp++) { 264789Sahrens if (i == start && blkid != 0) 265789Sahrens continue; 266789Sahrens else if (i == end && !trunc) 267789Sahrens continue; 268789Sahrens ASSERT3U(bp->blk_birth, ==, 0); 269789Sahrens } 270789Sahrens #endif 2711163Smaybee ASSERT(all || list_link_active(&db->db_dirty_node[txgoff])); 272789Sahrens return (all); 273789Sahrens } 274789Sahrens 275789Sahrens /* 276789Sahrens * free_range: Traverse the indicated range of the provided file 277789Sahrens * and "free" all the blocks contained there. 278789Sahrens */ 279789Sahrens static void 280789Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 281789Sahrens { 282789Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 283789Sahrens dmu_buf_impl_t *db; 284789Sahrens int trunc, start, end, shift, i, err; 285789Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 286789Sahrens 287789Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 288789Sahrens return; 289789Sahrens 290789Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 291789Sahrens trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 292789Sahrens if (trunc) 293789Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 294789Sahrens 295789Sahrens /* There are no indirect blocks in the object */ 296789Sahrens if (dnlevel == 1) { 297789Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 298789Sahrens /* this range was never made persistent */ 299789Sahrens return; 300789Sahrens } 301789Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 302789Sahrens free_blocks(dn, bp + blkid, nblks, tx); 303789Sahrens if (trunc) { 304789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 305789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 306789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 307789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 308789Sahrens dn->dn_phys->dn_maxblkid == 0 || 309789Sahrens dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 310789Sahrens } 311789Sahrens return; 312789Sahrens } 313789Sahrens 314789Sahrens shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 315789Sahrens start = blkid >> shift; 316789Sahrens ASSERT(start < dn->dn_phys->dn_nblkptr); 317789Sahrens end = (blkid + nblks - 1) >> shift; 318789Sahrens bp += start; 319789Sahrens for (i = start; i <= end; i++, bp++) { 320789Sahrens if (BP_IS_HOLE(bp)) 321789Sahrens continue; 322789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 323789Sahrens err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 324789Sahrens ASSERT3U(err, ==, 0); 325789Sahrens rw_exit(&dn->dn_struct_rwlock); 326789Sahrens 327789Sahrens if (free_children(db, blkid, nblks, trunc, tx)) { 328789Sahrens ASSERT3P(db->db_blkptr, ==, bp); 329789Sahrens free_blocks(dn, bp, 1, tx); 330789Sahrens } 3311544Seschrock dbuf_rele(db, FTAG); 332789Sahrens } 333789Sahrens if (trunc) { 334789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 335789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 336789Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 337789Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 338789Sahrens dn->dn_phys->dn_maxblkid == 0 || 339789Sahrens dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 340789Sahrens } 341789Sahrens } 342789Sahrens 3431544Seschrock /* 3441544Seschrock * Try to kick all the dnodes dbufs out of the cache... 3451544Seschrock */ 3461544Seschrock void 3471544Seschrock dnode_evict_dbufs(dnode_t *dn) 3481544Seschrock { 3491596Sahrens int progress; 3501596Sahrens int pass = 0; 3511596Sahrens 3521596Sahrens do { 353*1602Smaybee dmu_buf_impl_t *db, marker; 3541596Sahrens int evicting = FALSE; 3551544Seschrock 3561596Sahrens progress = FALSE; 3571596Sahrens mutex_enter(&dn->dn_dbufs_mtx); 358*1602Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 359*1602Smaybee db = list_head(&dn->dn_dbufs); 360*1602Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 361*1602Smaybee list_remove(&dn->dn_dbufs, db); 362*1602Smaybee list_insert_tail(&dn->dn_dbufs, db); 3631596Sahrens 3641544Seschrock mutex_enter(&db->db_mtx); 3651596Sahrens if (db->db_state == DB_EVICTING) { 3661596Sahrens progress = TRUE; 3671596Sahrens evicting = TRUE; 3681596Sahrens mutex_exit(&db->db_mtx); 3691596Sahrens } else if (refcount_is_zero(&db->db_holds)) { 3701596Sahrens progress = TRUE; 3711596Sahrens ASSERT(!arc_released(db->db_buf)); 3721596Sahrens dbuf_clear(db); /* exits db_mtx for us */ 3731596Sahrens } else { 3741596Sahrens mutex_exit(&db->db_mtx); 3751596Sahrens } 3761596Sahrens 3771544Seschrock } 378*1602Smaybee list_remove(&dn->dn_dbufs, &marker); 3791596Sahrens /* 3801596Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 3811596Sahrens * that any DB_EVICTING dbufs can make progress. 3821596Sahrens * Ideally, we would have some cv we could wait on, but 3831596Sahrens * since we don't, just wait a bit to give the other 3841596Sahrens * thread a chance to run. 3851596Sahrens */ 3861596Sahrens mutex_exit(&dn->dn_dbufs_mtx); 3871596Sahrens if (evicting) 3881596Sahrens delay(1); 3891596Sahrens pass++; 3901596Sahrens ASSERT(pass < 100); /* sanity check */ 3911596Sahrens } while (progress); 3921596Sahrens 3931596Sahrens /* 3941596Sahrens * This function works fine even if it can't evict everything, 3951596Sahrens * but all of our callers need this assertion, so let's put it 3961596Sahrens * here (for now). Perhaps in the future there will be a try vs 3971596Sahrens * doall flag. 3981596Sahrens */ 3991596Sahrens if (list_head(&dn->dn_dbufs) != NULL) { 4001596Sahrens panic("dangling dbufs (dn=%p, dbuf=%p)\n", 4011596Sahrens dn, list_head(&dn->dn_dbufs)); 4021544Seschrock } 4031596Sahrens 4041544Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 4051544Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 4061544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 4071544Seschrock dbuf_evict(dn->dn_bonus); 4081544Seschrock dn->dn_bonus = NULL; 4091544Seschrock } 4101544Seschrock rw_exit(&dn->dn_struct_rwlock); 4111596Sahrens 4121544Seschrock } 4131544Seschrock 414789Sahrens static int 415789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 416789Sahrens { 417789Sahrens dmu_buf_impl_t *db; 418789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 419789Sahrens 420789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 421789Sahrens 422789Sahrens /* Undirty all buffers */ 423789Sahrens while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) { 424789Sahrens mutex_enter(&db->db_mtx); 425789Sahrens /* XXX - use dbuf_undirty()? */ 426789Sahrens list_remove(&dn->dn_dirty_dbufs[txgoff], db); 427789Sahrens if (db->db_level == 0) { 4281544Seschrock ASSERT(db->db_blkid == DB_BONUS_BLKID || 4291544Seschrock db->db_d.db_data_old[txgoff] == db->db_buf); 430789Sahrens if (db->db_d.db_overridden_by[txgoff]) 431789Sahrens dbuf_unoverride(db, tx->tx_txg); 432789Sahrens db->db_d.db_data_old[txgoff] = NULL; 433789Sahrens } 434789Sahrens db->db_dirtycnt -= 1; 435789Sahrens mutex_exit(&db->db_mtx); 4361544Seschrock dbuf_rele(db, (void *)(uintptr_t)tx->tx_txg); 437789Sahrens } 438789Sahrens 4391544Seschrock dnode_evict_dbufs(dn); 4401544Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 4411544Seschrock 4421544Seschrock /* 4431544Seschrock * XXX - It would be nice to assert this, but we may still 4441544Seschrock * have residual holds from async evictions from the arc... 4451544Seschrock * 4461544Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 4471544Seschrock */ 448789Sahrens 449789Sahrens /* Undirty next bits */ 450789Sahrens dn->dn_next_nlevels[txgoff] = 0; 451789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 4521596Sahrens dn->dn_next_blksz[txgoff] = 0; 453789Sahrens 454789Sahrens /* free up all the blocks in the file. */ 455789Sahrens dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 456789Sahrens ASSERT3U(dn->dn_phys->dn_secphys, ==, 0); 457789Sahrens 458789Sahrens /* ASSERT(blkptrs are zero); */ 459789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 460789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 461789Sahrens 462789Sahrens ASSERT(dn->dn_free_txg > 0); 463789Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 464789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 465789Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 466789Sahrens 467789Sahrens mutex_enter(&dn->dn_mtx); 468789Sahrens dn->dn_type = DMU_OT_NONE; 469789Sahrens dn->dn_maxblkid = 0; 470789Sahrens dn->dn_allocated_txg = 0; 471789Sahrens mutex_exit(&dn->dn_mtx); 472789Sahrens 4731544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 474789Sahrens 475789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 476789Sahrens /* 477789Sahrens * Now that we've released our hold, the dnode may 478789Sahrens * be evicted, so we musn't access it. 479789Sahrens */ 480789Sahrens return (1); 481789Sahrens } 482789Sahrens 483789Sahrens /* 484789Sahrens * Write out the dnode's dirty buffers at the specified level. 485789Sahrens * This may create more dirty buffers at the next level up. 486789Sahrens * 487789Sahrens * NOTE: The dnode is kept in memory by being dirty. Once the 488789Sahrens * dirty bit is cleared, it may be evicted. Beware of this! 489789Sahrens */ 490789Sahrens int 491789Sahrens dnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx) 492789Sahrens { 493789Sahrens free_range_t *rp; 494789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 495789Sahrens dnode_phys_t *dnp = dn->dn_phys; 496789Sahrens 497789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 498789Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 499873Sek110237 DNODE_VERIFY(dn); 5001596Sahrens 501789Sahrens /* 502789Sahrens * Make sure the dbuf for the dn_phys is released before we modify it. 503789Sahrens */ 504789Sahrens if (dn->dn_dbuf) 505789Sahrens arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf); 506789Sahrens 507789Sahrens mutex_enter(&dn->dn_mtx); 508789Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 509789Sahrens /* The dnode is newly allocated or reallocated */ 510789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 511789Sahrens /* this is a first alloc, not a realloc */ 512789Sahrens /* XXX shouldn't the phys already be zeroed? */ 513789Sahrens bzero(dnp, DNODE_CORE_SIZE); 514789Sahrens dnp->dn_nlevels = 1; 515789Sahrens } 516789Sahrens 517789Sahrens if (dn->dn_nblkptr > dnp->dn_nblkptr) { 518789Sahrens /* zero the new blkptrs we are gaining */ 519789Sahrens bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 520789Sahrens sizeof (blkptr_t) * 521789Sahrens (dn->dn_nblkptr - dnp->dn_nblkptr)); 522789Sahrens } 523789Sahrens dnp->dn_type = dn->dn_type; 524789Sahrens dnp->dn_bonustype = dn->dn_bonustype; 525789Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 526789Sahrens dnp->dn_nblkptr = dn->dn_nblkptr; 527789Sahrens } 528789Sahrens 5291599Sahrens ASSERT(level != 0 || dnp->dn_nlevels > 1 || 5301599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5311599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 5321599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5331599Sahrens 5341596Sahrens if (dn->dn_next_blksz[txgoff]) { 5351596Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 536789Sahrens SPA_MINBLOCKSIZE) == 0); 5371599Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5381600Sahrens list_head(&dn->dn_dirty_dbufs[txgoff]) != NULL || 5391600Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 5401600Sahrens dnp->dn_datablkszsec); 541789Sahrens dnp->dn_datablkszsec = 5421596Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 5431596Sahrens dn->dn_next_blksz[txgoff] = 0; 544789Sahrens } 545789Sahrens 546789Sahrens if (dn->dn_next_indblkshift[txgoff]) { 547789Sahrens ASSERT(dnp->dn_nlevels == 1); 548789Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 549789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 550789Sahrens } 551789Sahrens 552789Sahrens /* 553789Sahrens * Just take the live (open-context) values for checksum and compress. 554789Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 555789Sahrens * start using the new checksum or compress algorithm a little early. 556789Sahrens */ 557789Sahrens dnp->dn_checksum = dn->dn_checksum; 558789Sahrens dnp->dn_compress = dn->dn_compress; 559789Sahrens 560789Sahrens mutex_exit(&dn->dn_mtx); 561789Sahrens 562789Sahrens /* process all the "freed" ranges in the file */ 563789Sahrens if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 5641163Smaybee for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 5651163Smaybee rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 566789Sahrens dnode_sync_free_range(dn, 567789Sahrens rp->fr_blkid, rp->fr_nblks, tx); 568789Sahrens } 569789Sahrens mutex_enter(&dn->dn_mtx); 570789Sahrens for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 571789Sahrens free_range_t *last = rp; 572789Sahrens rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 573789Sahrens avl_remove(&dn->dn_ranges[txgoff], last); 574789Sahrens kmem_free(last, sizeof (free_range_t)); 575789Sahrens } 576789Sahrens mutex_exit(&dn->dn_mtx); 577789Sahrens 578789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 579789Sahrens ASSERT3U(level, ==, 0); 580789Sahrens return (dnode_sync_free(dn, tx)); 581789Sahrens } 582789Sahrens 583789Sahrens if (dn->dn_next_nlevels[txgoff]) { 584789Sahrens int new_lvl = dn->dn_next_nlevels[txgoff]; 585789Sahrens 586789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 587789Sahrens while (new_lvl > dnp->dn_nlevels) 588789Sahrens dnode_increase_indirection(dn, tx); 589789Sahrens rw_exit(&dn->dn_struct_rwlock); 590789Sahrens dn->dn_next_nlevels[txgoff] = 0; 591789Sahrens } 592789Sahrens 593789Sahrens if (level == dnp->dn_nlevels) { 594789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 595789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 596789Sahrens 597789Sahrens /* we've already synced out all data and indirect blocks */ 598789Sahrens /* there are no more dirty dbufs under this dnode */ 599789Sahrens ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL); 600789Sahrens ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg); 601789Sahrens 602789Sahrens /* XXX this is expensive. remove once 6343073 is closed. */ 603789Sahrens /* NB: the "off < maxblkid" is to catch overflow */ 604789Sahrens /* 605789Sahrens * NB: if blocksize is changing, we could get confused, 606789Sahrens * so only bother if there are multiple blocks and thus 607789Sahrens * it can't be changing. 608789Sahrens */ 6091163Smaybee if (!(off < dn->dn_phys->dn_maxblkid || 610789Sahrens dn->dn_phys->dn_maxblkid == 0 || 6111163Smaybee dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH)) 6121199Seschrock panic("data after EOF: off=%llu\n", (u_longlong_t)off); 613789Sahrens 6141599Sahrens ASSERT(dnp->dn_nlevels > 1 || 6151599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 6161599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 6171599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 6181599Sahrens 6191544Seschrock if (dn->dn_object != DMU_META_DNODE_OBJECT) { 620789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 621789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 622789Sahrens } 623789Sahrens 624789Sahrens /* 625789Sahrens * Now that we've dropped the reference, the dnode may 626789Sahrens * be evicted, so we musn't access it. 627789Sahrens */ 628789Sahrens return (1); 629789Sahrens } else { 630789Sahrens dmu_buf_impl_t *db, *db_next; 631789Sahrens list_t *list = &dn->dn_dirty_dbufs[txgoff]; 632789Sahrens /* 633789Sahrens * Iterate over the list, removing and sync'ing dbufs 634789Sahrens * which are on the level we want, and leaving others. 635789Sahrens */ 636789Sahrens for (db = list_head(list); db; db = db_next) { 637789Sahrens db_next = list_next(list, db); 638789Sahrens if (db->db_level == level) { 639789Sahrens list_remove(list, db); 640789Sahrens dbuf_sync(db, zio, tx); 641789Sahrens } 642789Sahrens } 643789Sahrens return (0); 644789Sahrens } 645789Sahrens } 646