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 { 3531596Sahrens dmu_buf_impl_t *db, *db_next; 3541596Sahrens int evicting = FALSE; 3551544Seschrock 3561596Sahrens progress = FALSE; 3571596Sahrens mutex_enter(&dn->dn_dbufs_mtx); 3581596Sahrens for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 3591596Sahrens /* dbuf_clear() may remove db from this list */ 3601596Sahrens db_next = list_next(&dn->dn_dbufs, db); 3611596Sahrens 3621544Seschrock mutex_enter(&db->db_mtx); 3631596Sahrens if (db->db_state == DB_EVICTING) { 3641596Sahrens progress = TRUE; 3651596Sahrens evicting = TRUE; 3661596Sahrens mutex_exit(&db->db_mtx); 3671596Sahrens } else if (refcount_is_zero(&db->db_holds)) { 3681596Sahrens progress = TRUE; 3691596Sahrens ASSERT(!arc_released(db->db_buf)); 3701596Sahrens dbuf_clear(db); /* exits db_mtx for us */ 3711596Sahrens } else { 3721596Sahrens mutex_exit(&db->db_mtx); 3731596Sahrens } 3741596Sahrens 3751544Seschrock } 3761596Sahrens /* 3771596Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 3781596Sahrens * that any DB_EVICTING dbufs can make progress. 3791596Sahrens * Ideally, we would have some cv we could wait on, but 3801596Sahrens * since we don't, just wait a bit to give the other 3811596Sahrens * thread a chance to run. 3821596Sahrens */ 3831596Sahrens mutex_exit(&dn->dn_dbufs_mtx); 3841596Sahrens if (evicting) 3851596Sahrens delay(1); 3861596Sahrens pass++; 3871596Sahrens ASSERT(pass < 100); /* sanity check */ 3881596Sahrens } while (progress); 3891596Sahrens 3901596Sahrens /* 3911596Sahrens * This function works fine even if it can't evict everything, 3921596Sahrens * but all of our callers need this assertion, so let's put it 3931596Sahrens * here (for now). Perhaps in the future there will be a try vs 3941596Sahrens * doall flag. 3951596Sahrens */ 3961596Sahrens if (list_head(&dn->dn_dbufs) != NULL) { 3971596Sahrens panic("dangling dbufs (dn=%p, dbuf=%p)\n", 3981596Sahrens dn, list_head(&dn->dn_dbufs)); 3991544Seschrock } 4001596Sahrens 4011544Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 4021544Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 4031544Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 4041544Seschrock dbuf_evict(dn->dn_bonus); 4051544Seschrock dn->dn_bonus = NULL; 4061544Seschrock } 4071544Seschrock rw_exit(&dn->dn_struct_rwlock); 4081596Sahrens 4091544Seschrock } 4101544Seschrock 411789Sahrens static int 412789Sahrens dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 413789Sahrens { 414789Sahrens dmu_buf_impl_t *db; 415789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 416789Sahrens 417789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 418789Sahrens 419789Sahrens /* Undirty all buffers */ 420789Sahrens while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) { 421789Sahrens mutex_enter(&db->db_mtx); 422789Sahrens /* XXX - use dbuf_undirty()? */ 423789Sahrens list_remove(&dn->dn_dirty_dbufs[txgoff], db); 424789Sahrens if (db->db_level == 0) { 4251544Seschrock ASSERT(db->db_blkid == DB_BONUS_BLKID || 4261544Seschrock db->db_d.db_data_old[txgoff] == db->db_buf); 427789Sahrens if (db->db_d.db_overridden_by[txgoff]) 428789Sahrens dbuf_unoverride(db, tx->tx_txg); 429789Sahrens db->db_d.db_data_old[txgoff] = NULL; 430789Sahrens } 431789Sahrens db->db_dirtycnt -= 1; 432789Sahrens mutex_exit(&db->db_mtx); 4331544Seschrock dbuf_rele(db, (void *)(uintptr_t)tx->tx_txg); 434789Sahrens } 435789Sahrens 4361544Seschrock dnode_evict_dbufs(dn); 4371544Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 4381544Seschrock 4391544Seschrock /* 4401544Seschrock * XXX - It would be nice to assert this, but we may still 4411544Seschrock * have residual holds from async evictions from the arc... 4421544Seschrock * 4431544Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 4441544Seschrock */ 445789Sahrens 446789Sahrens /* Undirty next bits */ 447789Sahrens dn->dn_next_nlevels[txgoff] = 0; 448789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 4491596Sahrens dn->dn_next_blksz[txgoff] = 0; 450789Sahrens 451789Sahrens /* free up all the blocks in the file. */ 452789Sahrens dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 453789Sahrens ASSERT3U(dn->dn_phys->dn_secphys, ==, 0); 454789Sahrens 455789Sahrens /* ASSERT(blkptrs are zero); */ 456789Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 457789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 458789Sahrens 459789Sahrens ASSERT(dn->dn_free_txg > 0); 460789Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 461789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 462789Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 463789Sahrens 464789Sahrens mutex_enter(&dn->dn_mtx); 465789Sahrens dn->dn_type = DMU_OT_NONE; 466789Sahrens dn->dn_maxblkid = 0; 467789Sahrens dn->dn_allocated_txg = 0; 468789Sahrens mutex_exit(&dn->dn_mtx); 469789Sahrens 4701544Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 471789Sahrens 472789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 473789Sahrens /* 474789Sahrens * Now that we've released our hold, the dnode may 475789Sahrens * be evicted, so we musn't access it. 476789Sahrens */ 477789Sahrens return (1); 478789Sahrens } 479789Sahrens 480789Sahrens /* 481789Sahrens * Write out the dnode's dirty buffers at the specified level. 482789Sahrens * This may create more dirty buffers at the next level up. 483789Sahrens * 484789Sahrens * NOTE: The dnode is kept in memory by being dirty. Once the 485789Sahrens * dirty bit is cleared, it may be evicted. Beware of this! 486789Sahrens */ 487789Sahrens int 488789Sahrens dnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx) 489789Sahrens { 490789Sahrens free_range_t *rp; 491789Sahrens int txgoff = tx->tx_txg & TXG_MASK; 492789Sahrens dnode_phys_t *dnp = dn->dn_phys; 493789Sahrens 494789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 495789Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 496873Sek110237 DNODE_VERIFY(dn); 4971596Sahrens 498789Sahrens /* 499789Sahrens * Make sure the dbuf for the dn_phys is released before we modify it. 500789Sahrens */ 501789Sahrens if (dn->dn_dbuf) 502789Sahrens arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf); 503789Sahrens 504789Sahrens mutex_enter(&dn->dn_mtx); 505789Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 506789Sahrens /* The dnode is newly allocated or reallocated */ 507789Sahrens if (dnp->dn_type == DMU_OT_NONE) { 508789Sahrens /* this is a first alloc, not a realloc */ 509789Sahrens /* XXX shouldn't the phys already be zeroed? */ 510789Sahrens bzero(dnp, DNODE_CORE_SIZE); 511789Sahrens dnp->dn_nlevels = 1; 512789Sahrens } 513789Sahrens 514789Sahrens if (dn->dn_nblkptr > dnp->dn_nblkptr) { 515789Sahrens /* zero the new blkptrs we are gaining */ 516789Sahrens bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 517789Sahrens sizeof (blkptr_t) * 518789Sahrens (dn->dn_nblkptr - dnp->dn_nblkptr)); 519789Sahrens } 520789Sahrens dnp->dn_type = dn->dn_type; 521789Sahrens dnp->dn_bonustype = dn->dn_bonustype; 522789Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 523789Sahrens dnp->dn_nblkptr = dn->dn_nblkptr; 524789Sahrens } 525789Sahrens 5261599Sahrens ASSERT(level != 0 || dnp->dn_nlevels > 1 || 5271599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5281599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 5291599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5301599Sahrens 5311596Sahrens if (dn->dn_next_blksz[txgoff]) { 5321596Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 533789Sahrens SPA_MINBLOCKSIZE) == 0); 5341599Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 535*1600Sahrens list_head(&dn->dn_dirty_dbufs[txgoff]) != NULL || 536*1600Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 537*1600Sahrens dnp->dn_datablkszsec); 538789Sahrens dnp->dn_datablkszsec = 5391596Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 5401596Sahrens dn->dn_next_blksz[txgoff] = 0; 541789Sahrens } 542789Sahrens 543789Sahrens if (dn->dn_next_indblkshift[txgoff]) { 544789Sahrens ASSERT(dnp->dn_nlevels == 1); 545789Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 546789Sahrens dn->dn_next_indblkshift[txgoff] = 0; 547789Sahrens } 548789Sahrens 549789Sahrens /* 550789Sahrens * Just take the live (open-context) values for checksum and compress. 551789Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 552789Sahrens * start using the new checksum or compress algorithm a little early. 553789Sahrens */ 554789Sahrens dnp->dn_checksum = dn->dn_checksum; 555789Sahrens dnp->dn_compress = dn->dn_compress; 556789Sahrens 557789Sahrens mutex_exit(&dn->dn_mtx); 558789Sahrens 559789Sahrens /* process all the "freed" ranges in the file */ 560789Sahrens if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 5611163Smaybee for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 5621163Smaybee rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 563789Sahrens dnode_sync_free_range(dn, 564789Sahrens rp->fr_blkid, rp->fr_nblks, tx); 565789Sahrens } 566789Sahrens mutex_enter(&dn->dn_mtx); 567789Sahrens for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 568789Sahrens free_range_t *last = rp; 569789Sahrens rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 570789Sahrens avl_remove(&dn->dn_ranges[txgoff], last); 571789Sahrens kmem_free(last, sizeof (free_range_t)); 572789Sahrens } 573789Sahrens mutex_exit(&dn->dn_mtx); 574789Sahrens 575789Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 576789Sahrens ASSERT3U(level, ==, 0); 577789Sahrens return (dnode_sync_free(dn, tx)); 578789Sahrens } 579789Sahrens 580789Sahrens if (dn->dn_next_nlevels[txgoff]) { 581789Sahrens int new_lvl = dn->dn_next_nlevels[txgoff]; 582789Sahrens 583789Sahrens rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 584789Sahrens while (new_lvl > dnp->dn_nlevels) 585789Sahrens dnode_increase_indirection(dn, tx); 586789Sahrens rw_exit(&dn->dn_struct_rwlock); 587789Sahrens dn->dn_next_nlevels[txgoff] = 0; 588789Sahrens } 589789Sahrens 590789Sahrens if (level == dnp->dn_nlevels) { 591789Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 592789Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 593789Sahrens 594789Sahrens /* we've already synced out all data and indirect blocks */ 595789Sahrens /* there are no more dirty dbufs under this dnode */ 596789Sahrens ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL); 597789Sahrens ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg); 598789Sahrens 599789Sahrens /* XXX this is expensive. remove once 6343073 is closed. */ 600789Sahrens /* NB: the "off < maxblkid" is to catch overflow */ 601789Sahrens /* 602789Sahrens * NB: if blocksize is changing, we could get confused, 603789Sahrens * so only bother if there are multiple blocks and thus 604789Sahrens * it can't be changing. 605789Sahrens */ 6061163Smaybee if (!(off < dn->dn_phys->dn_maxblkid || 607789Sahrens dn->dn_phys->dn_maxblkid == 0 || 6081163Smaybee dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH)) 6091199Seschrock panic("data after EOF: off=%llu\n", (u_longlong_t)off); 610789Sahrens 6111599Sahrens ASSERT(dnp->dn_nlevels > 1 || 6121599Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 6131599Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 6141599Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 6151599Sahrens 6161544Seschrock if (dn->dn_object != DMU_META_DNODE_OBJECT) { 617789Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 618789Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 619789Sahrens } 620789Sahrens 621789Sahrens /* 622789Sahrens * Now that we've dropped the reference, the dnode may 623789Sahrens * be evicted, so we musn't access it. 624789Sahrens */ 625789Sahrens return (1); 626789Sahrens } else { 627789Sahrens dmu_buf_impl_t *db, *db_next; 628789Sahrens list_t *list = &dn->dn_dirty_dbufs[txgoff]; 629789Sahrens /* 630789Sahrens * Iterate over the list, removing and sync'ing dbufs 631789Sahrens * which are on the level we want, and leaving others. 632789Sahrens */ 633789Sahrens for (db = list_head(list); db; db = db_next) { 634789Sahrens db_next = list_next(list, db); 635789Sahrens if (db->db_level == level) { 636789Sahrens list_remove(list, db); 637789Sahrens dbuf_sync(db, zio, tx); 638789Sahrens } 639789Sahrens } 640789Sahrens return (0); 641789Sahrens } 642789Sahrens } 643