1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 2255434c77Sek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/zfs_context.h> 29fa9e4066Sahrens #include <sys/dbuf.h> 30fa9e4066Sahrens #include <sys/dnode.h> 31fa9e4066Sahrens #include <sys/dmu.h> 32fa9e4066Sahrens #include <sys/dmu_tx.h> 33fa9e4066Sahrens #include <sys/dmu_objset.h> 34fa9e4066Sahrens #include <sys/dsl_dataset.h> 35fa9e4066Sahrens #include <sys/spa.h> 36fa9e4066Sahrens 37fa9e4066Sahrens static void 38fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 39fa9e4066Sahrens { 40fa9e4066Sahrens dmu_buf_impl_t *db; 41*c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 42*c717a561Smaybee int nblkptr = dn->dn_phys->dn_nblkptr; 43*c717a561Smaybee int old_toplvl = dn->dn_phys->dn_nlevels - 1; 44*c717a561Smaybee int new_level = dn->dn_next_nlevels[txgoff]; 45fa9e4066Sahrens int i; 46fa9e4066Sahrens 47*c717a561Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 48*c717a561Smaybee 49*c717a561Smaybee /* this dnode can't be paged out because it's dirty */ 50fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 51fa9e4066Sahrens ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 52*c717a561Smaybee ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); 53fa9e4066Sahrens 54fa9e4066Sahrens db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 55ea8dc4b6Seschrock ASSERT(db != NULL); 56*c717a561Smaybee 57*c717a561Smaybee dn->dn_phys->dn_nlevels = new_level; 58*c717a561Smaybee dprintf("os=%p obj=%llu, increase to %d\n", 59*c717a561Smaybee dn->dn_objset, dn->dn_object, 60*c717a561Smaybee dn->dn_phys->dn_nlevels); 61*c717a561Smaybee 62*c717a561Smaybee /* check for existing blkptrs in the dnode */ 63*c717a561Smaybee for (i = 0; i < nblkptr; i++) 64fa9e4066Sahrens if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 65fa9e4066Sahrens break; 66*c717a561Smaybee if (i != nblkptr) { 67*c717a561Smaybee /* transfer dnode's block pointers to new indirect block */ 68*c717a561Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 69*c717a561Smaybee ASSERT(db->db.db_data); 70*c717a561Smaybee ASSERT(arc_released(db->db_buf)); 71*c717a561Smaybee ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 72fa9e4066Sahrens bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 73*c717a561Smaybee sizeof (blkptr_t) * nblkptr); 746b4acc8bSahrens arc_buf_freeze(db->db_buf); 75fa9e4066Sahrens } 76fa9e4066Sahrens 77fa9e4066Sahrens /* set dbuf's parent pointers to new indirect buf */ 78*c717a561Smaybee for (i = 0; i < nblkptr; i++) { 79*c717a561Smaybee dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); 80*c717a561Smaybee 81fa9e4066Sahrens if (child == NULL) 82fa9e4066Sahrens continue; 83*c717a561Smaybee ASSERT3P(child->db_dnode, ==, dn); 84*c717a561Smaybee if (child->db_parent && child->db_parent != dn->dn_dbuf) { 85*c717a561Smaybee ASSERT(child->db_parent->db_level == db->db_level); 86*c717a561Smaybee ASSERT(child->db_blkptr != 87*c717a561Smaybee &dn->dn_phys->dn_blkptr[child->db_blkid]); 88fa9e4066Sahrens mutex_exit(&child->db_mtx); 89fa9e4066Sahrens continue; 90fa9e4066Sahrens } 91*c717a561Smaybee ASSERT(child->db_parent == NULL || 92*c717a561Smaybee child->db_parent == dn->dn_dbuf); 93*c717a561Smaybee 94*c717a561Smaybee child->db_parent = db; 95*c717a561Smaybee dbuf_add_ref(db, child); 96*c717a561Smaybee if (db->db.db_data) 97*c717a561Smaybee child->db_blkptr = (blkptr_t *)db->db.db_data + i; 98*c717a561Smaybee else 99*c717a561Smaybee child->db_blkptr = NULL; 100*c717a561Smaybee dprintf_dbuf_bp(child, child->db_blkptr, 101*c717a561Smaybee "changed db_blkptr to new indirect %s", ""); 102fa9e4066Sahrens 103fa9e4066Sahrens mutex_exit(&child->db_mtx); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106*c717a561Smaybee bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 107fa9e4066Sahrens 108ea8dc4b6Seschrock dbuf_rele(db, FTAG); 109*c717a561Smaybee 110*c717a561Smaybee rw_exit(&dn->dn_struct_rwlock); 111fa9e4066Sahrens } 112fa9e4066Sahrens 113fa9e4066Sahrens static void 114fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 115fa9e4066Sahrens { 116fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 117fa9e4066Sahrens uint64_t bytesfreed = 0; 118fa9e4066Sahrens int i; 119fa9e4066Sahrens 120fa9e4066Sahrens dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num); 121fa9e4066Sahrens 122fa9e4066Sahrens for (i = 0; i < num; i++, bp++) { 123fa9e4066Sahrens if (BP_IS_HOLE(bp)) 124fa9e4066Sahrens continue; 125fa9e4066Sahrens 12699653d4eSeschrock bytesfreed += bp_get_dasize(os->os_spa, bp); 12799653d4eSeschrock ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 128*c717a561Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, bp, dn->dn_zio, tx); 129*c717a561Smaybee bzero(bp, sizeof (blkptr_t)); 130fa9e4066Sahrens } 131fa9e4066Sahrens dnode_diduse_space(dn, -bytesfreed); 132fa9e4066Sahrens } 133fa9e4066Sahrens 1349c9dc39aSek #ifdef ZFS_DEBUG 135fa9e4066Sahrens static void 136fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 137fa9e4066Sahrens { 138fa9e4066Sahrens int off, num; 139fa9e4066Sahrens int i, err, epbs; 140fa9e4066Sahrens uint64_t txg = tx->tx_txg; 141fa9e4066Sahrens 142fa9e4066Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 143fa9e4066Sahrens off = start - (db->db_blkid * 1<<epbs); 144fa9e4066Sahrens num = end - start + 1; 145fa9e4066Sahrens 146fa9e4066Sahrens ASSERT3U(off, >=, 0); 147fa9e4066Sahrens ASSERT3U(num, >=, 0); 148fa9e4066Sahrens ASSERT3U(db->db_level, >, 0); 149fa9e4066Sahrens ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift); 150fa9e4066Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 151fa9e4066Sahrens ASSERT(db->db_blkptr != NULL); 152fa9e4066Sahrens 153fa9e4066Sahrens for (i = off; i < off+num; i++) { 154fa9e4066Sahrens uint64_t *buf; 155fa9e4066Sahrens dmu_buf_impl_t *child; 156*c717a561Smaybee dbuf_dirty_record_t *dr; 157*c717a561Smaybee int j; 158fa9e4066Sahrens 159fa9e4066Sahrens ASSERT(db->db_level == 1); 160fa9e4066Sahrens 161fa9e4066Sahrens rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 162fa9e4066Sahrens err = dbuf_hold_impl(db->db_dnode, db->db_level-1, 163fa9e4066Sahrens (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 164fa9e4066Sahrens rw_exit(&db->db_dnode->dn_struct_rwlock); 165fa9e4066Sahrens if (err == ENOENT) 166fa9e4066Sahrens continue; 167fa9e4066Sahrens ASSERT(err == 0); 168fa9e4066Sahrens ASSERT(child->db_level == 0); 169*c717a561Smaybee dr = child->db_last_dirty; 170*c717a561Smaybee while (dr && dr->dr_txg > txg) 171*c717a561Smaybee dr = dr->dr_next; 172*c717a561Smaybee ASSERT(dr == NULL || dr->dr_txg == txg); 173*c717a561Smaybee 174*c717a561Smaybee /* data_old better be zeroed */ 175*c717a561Smaybee if (dr) { 176*c717a561Smaybee buf = dr->dt.dl.dr_data->b_data; 177fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 178fa9e4066Sahrens if (buf[j] != 0) { 179fa9e4066Sahrens panic("freed data not zero: " 180fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 181fa9e4066Sahrens child, i, off, num); 182fa9e4066Sahrens } 183fa9e4066Sahrens } 184fa9e4066Sahrens } 185fa9e4066Sahrens 186fa9e4066Sahrens /* 187fa9e4066Sahrens * db_data better be zeroed unless it's dirty in a 188fa9e4066Sahrens * future txg. 189fa9e4066Sahrens */ 190fa9e4066Sahrens mutex_enter(&child->db_mtx); 191fa9e4066Sahrens buf = child->db.db_data; 192fa9e4066Sahrens if (buf != NULL && child->db_state != DB_FILL && 193*c717a561Smaybee child->db_last_dirty == NULL) { 194fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 195fa9e4066Sahrens if (buf[j] != 0) { 196fa9e4066Sahrens panic("freed data not zero: " 197fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 198fa9e4066Sahrens child, i, off, num); 199fa9e4066Sahrens } 200fa9e4066Sahrens } 201fa9e4066Sahrens } 202fa9e4066Sahrens mutex_exit(&child->db_mtx); 203fa9e4066Sahrens 204ea8dc4b6Seschrock dbuf_rele(child, FTAG); 205fa9e4066Sahrens } 206fa9e4066Sahrens } 2079c9dc39aSek #endif 208fa9e4066Sahrens 209fa9e4066Sahrens static int 210fa9e4066Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 211fa9e4066Sahrens dmu_tx_t *tx) 212fa9e4066Sahrens { 213fa9e4066Sahrens dnode_t *dn = db->db_dnode; 214fa9e4066Sahrens blkptr_t *bp; 215fa9e4066Sahrens dmu_buf_impl_t *subdb; 216fa9e4066Sahrens uint64_t start, end, dbstart, dbend, i; 217fa9e4066Sahrens int epbs, shift, err; 218fa9e4066Sahrens int all = TRUE; 219fa9e4066Sahrens 220ea8dc4b6Seschrock (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 221fa9e4066Sahrens arc_release(db->db_buf, db); 222fa9e4066Sahrens bp = (blkptr_t *)db->db.db_data; 223fa9e4066Sahrens 224fa9e4066Sahrens epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 225fa9e4066Sahrens shift = (db->db_level - 1) * epbs; 226fa9e4066Sahrens dbstart = db->db_blkid << epbs; 227fa9e4066Sahrens start = blkid >> shift; 228fa9e4066Sahrens if (dbstart < start) { 229fa9e4066Sahrens bp += start - dbstart; 230fa9e4066Sahrens all = FALSE; 231fa9e4066Sahrens } else { 232fa9e4066Sahrens start = dbstart; 233fa9e4066Sahrens } 234fa9e4066Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 235fa9e4066Sahrens end = (blkid + nblks - 1) >> shift; 236fa9e4066Sahrens if (dbend <= end) 237fa9e4066Sahrens end = dbend; 238fa9e4066Sahrens else if (all) 239fa9e4066Sahrens all = trunc; 240fa9e4066Sahrens ASSERT3U(start, <=, end); 241fa9e4066Sahrens 242fa9e4066Sahrens if (db->db_level == 1) { 2439c9dc39aSek FREE_VERIFY(db, start, end, tx); 244fa9e4066Sahrens free_blocks(dn, bp, end-start+1, tx); 2456b4acc8bSahrens arc_buf_freeze(db->db_buf); 246*c717a561Smaybee ASSERT(all || db->db_last_dirty); 247fa9e4066Sahrens return (all); 248fa9e4066Sahrens } 249fa9e4066Sahrens 250fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 251fa9e4066Sahrens if (BP_IS_HOLE(bp)) 252fa9e4066Sahrens continue; 253fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 254fa9e4066Sahrens err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 255fa9e4066Sahrens ASSERT3U(err, ==, 0); 256fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 257fa9e4066Sahrens 258fa9e4066Sahrens if (free_children(subdb, blkid, nblks, trunc, tx)) { 259fa9e4066Sahrens ASSERT3P(subdb->db_blkptr, ==, bp); 260fa9e4066Sahrens free_blocks(dn, bp, 1, tx); 26123b11526Smaybee } else { 26223b11526Smaybee all = FALSE; 263fa9e4066Sahrens } 264ea8dc4b6Seschrock dbuf_rele(subdb, FTAG); 265fa9e4066Sahrens } 2666b4acc8bSahrens arc_buf_freeze(db->db_buf); 267fa9e4066Sahrens #ifdef ZFS_DEBUG 268fa9e4066Sahrens bp -= (end-start)+1; 269fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 270fa9e4066Sahrens if (i == start && blkid != 0) 271fa9e4066Sahrens continue; 272fa9e4066Sahrens else if (i == end && !trunc) 273fa9e4066Sahrens continue; 274fa9e4066Sahrens ASSERT3U(bp->blk_birth, ==, 0); 275fa9e4066Sahrens } 276fa9e4066Sahrens #endif 277*c717a561Smaybee ASSERT(all || db->db_last_dirty); 278fa9e4066Sahrens return (all); 279fa9e4066Sahrens } 280fa9e4066Sahrens 281fa9e4066Sahrens /* 282fa9e4066Sahrens * free_range: Traverse the indicated range of the provided file 283fa9e4066Sahrens * and "free" all the blocks contained there. 284fa9e4066Sahrens */ 285fa9e4066Sahrens static void 286fa9e4066Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 287fa9e4066Sahrens { 288fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 289fa9e4066Sahrens dmu_buf_impl_t *db; 290fa9e4066Sahrens int trunc, start, end, shift, i, err; 291fa9e4066Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 292fa9e4066Sahrens 293fa9e4066Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 294fa9e4066Sahrens return; 295fa9e4066Sahrens 296fa9e4066Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 297fa9e4066Sahrens trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 298fa9e4066Sahrens if (trunc) 299fa9e4066Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 300fa9e4066Sahrens 301fa9e4066Sahrens /* There are no indirect blocks in the object */ 302fa9e4066Sahrens if (dnlevel == 1) { 303fa9e4066Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 304fa9e4066Sahrens /* this range was never made persistent */ 305fa9e4066Sahrens return; 306fa9e4066Sahrens } 307fa9e4066Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 308fa9e4066Sahrens free_blocks(dn, bp + blkid, nblks, tx); 309fa9e4066Sahrens if (trunc) { 310fa9e4066Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 311fa9e4066Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 312fa9e4066Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 313fa9e4066Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 314fa9e4066Sahrens dn->dn_phys->dn_maxblkid == 0 || 3156754306eSahrens dnode_next_offset(dn, FALSE, &off, 3166754306eSahrens 1, 1, 0) != 0); 317fa9e4066Sahrens } 318fa9e4066Sahrens return; 319fa9e4066Sahrens } 320fa9e4066Sahrens 321fa9e4066Sahrens shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 322fa9e4066Sahrens start = blkid >> shift; 323fa9e4066Sahrens ASSERT(start < dn->dn_phys->dn_nblkptr); 324fa9e4066Sahrens end = (blkid + nblks - 1) >> shift; 325fa9e4066Sahrens bp += start; 326fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 327fa9e4066Sahrens if (BP_IS_HOLE(bp)) 328fa9e4066Sahrens continue; 329fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 330fa9e4066Sahrens err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 331fa9e4066Sahrens ASSERT3U(err, ==, 0); 332fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 333fa9e4066Sahrens 334fa9e4066Sahrens if (free_children(db, blkid, nblks, trunc, tx)) { 335fa9e4066Sahrens ASSERT3P(db->db_blkptr, ==, bp); 336fa9e4066Sahrens free_blocks(dn, bp, 1, tx); 337fa9e4066Sahrens } 338ea8dc4b6Seschrock dbuf_rele(db, FTAG); 339fa9e4066Sahrens } 340fa9e4066Sahrens if (trunc) { 341fa9e4066Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 342fa9e4066Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 343fa9e4066Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 344fa9e4066Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 345fa9e4066Sahrens dn->dn_phys->dn_maxblkid == 0 || 3466754306eSahrens dnode_next_offset(dn, FALSE, &off, 1, 1, 0) != 0); 347fa9e4066Sahrens } 348fa9e4066Sahrens } 349fa9e4066Sahrens 350ea8dc4b6Seschrock /* 351ea8dc4b6Seschrock * Try to kick all the dnodes dbufs out of the cache... 352ea8dc4b6Seschrock */ 353436b2950Sperrin int 354436b2950Sperrin dnode_evict_dbufs(dnode_t *dn, int try) 355ea8dc4b6Seschrock { 356c543ec06Sahrens int progress; 357c543ec06Sahrens int pass = 0; 358c543ec06Sahrens 359c543ec06Sahrens do { 360d90a49d6Smaybee dmu_buf_impl_t *db, marker; 361c543ec06Sahrens int evicting = FALSE; 362c543ec06Sahrens 363c543ec06Sahrens progress = FALSE; 364c543ec06Sahrens mutex_enter(&dn->dn_dbufs_mtx); 365d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 366d90a49d6Smaybee db = list_head(&dn->dn_dbufs); 367d90a49d6Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 368d90a49d6Smaybee list_remove(&dn->dn_dbufs, db); 369d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, db); 370ea8dc4b6Seschrock 371ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 372c543ec06Sahrens if (db->db_state == DB_EVICTING) { 373c543ec06Sahrens progress = TRUE; 374c543ec06Sahrens evicting = TRUE; 375c543ec06Sahrens mutex_exit(&db->db_mtx); 376c543ec06Sahrens } else if (refcount_is_zero(&db->db_holds)) { 377c543ec06Sahrens progress = TRUE; 378c543ec06Sahrens ASSERT(!arc_released(db->db_buf)); 379c543ec06Sahrens dbuf_clear(db); /* exits db_mtx for us */ 380c543ec06Sahrens } else { 381c543ec06Sahrens mutex_exit(&db->db_mtx); 382c543ec06Sahrens } 383c543ec06Sahrens 384ea8dc4b6Seschrock } 385d90a49d6Smaybee list_remove(&dn->dn_dbufs, &marker); 386c543ec06Sahrens /* 387c543ec06Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 388c543ec06Sahrens * that any DB_EVICTING dbufs can make progress. 389c543ec06Sahrens * Ideally, we would have some cv we could wait on, but 390c543ec06Sahrens * since we don't, just wait a bit to give the other 391c543ec06Sahrens * thread a chance to run. 392c543ec06Sahrens */ 393c543ec06Sahrens mutex_exit(&dn->dn_dbufs_mtx); 394c543ec06Sahrens if (evicting) 395c543ec06Sahrens delay(1); 396c543ec06Sahrens pass++; 397c543ec06Sahrens ASSERT(pass < 100); /* sanity check */ 398c543ec06Sahrens } while (progress); 399c543ec06Sahrens 400c543ec06Sahrens /* 401436b2950Sperrin * This function works fine even if it can't evict everything. 402436b2950Sperrin * If were only asked to try to evict everything then 403436b2950Sperrin * return an error if we can't. Otherwise panic as the caller 404436b2950Sperrin * expects total eviction. 405c543ec06Sahrens */ 406c543ec06Sahrens if (list_head(&dn->dn_dbufs) != NULL) { 407436b2950Sperrin if (try) { 408436b2950Sperrin return (1); 409436b2950Sperrin } else { 410436b2950Sperrin panic("dangling dbufs (dn=%p, dbuf=%p)\n", 411436b2950Sperrin dn, list_head(&dn->dn_dbufs)); 412436b2950Sperrin } 413ea8dc4b6Seschrock } 414c543ec06Sahrens 415ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 416ea8dc4b6Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 417ea8dc4b6Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 418ea8dc4b6Seschrock dbuf_evict(dn->dn_bonus); 419ea8dc4b6Seschrock dn->dn_bonus = NULL; 420ea8dc4b6Seschrock } 421ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 422436b2950Sperrin return (0); 423ea8dc4b6Seschrock } 424ea8dc4b6Seschrock 425*c717a561Smaybee static void 426*c717a561Smaybee dnode_undirty_dbufs(list_t *list) 427fa9e4066Sahrens { 428*c717a561Smaybee dbuf_dirty_record_t *dr; 429fa9e4066Sahrens 430*c717a561Smaybee while (dr = list_head(list)) { 431*c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 432*c717a561Smaybee uint64_t txg = dr->dr_txg; 433fa9e4066Sahrens 434fa9e4066Sahrens mutex_enter(&db->db_mtx); 435fa9e4066Sahrens /* XXX - use dbuf_undirty()? */ 436*c717a561Smaybee list_remove(list, dr); 437*c717a561Smaybee ASSERT(db->db_last_dirty == dr); 438*c717a561Smaybee db->db_last_dirty = NULL; 439*c717a561Smaybee db->db_dirtycnt -= 1; 440fa9e4066Sahrens if (db->db_level == 0) { 441ea8dc4b6Seschrock ASSERT(db->db_blkid == DB_BONUS_BLKID || 442*c717a561Smaybee dr->dt.dl.dr_data == db->db_buf); 443*c717a561Smaybee dbuf_unoverride(dr); 444*c717a561Smaybee mutex_exit(&db->db_mtx); 445*c717a561Smaybee } else { 446*c717a561Smaybee mutex_exit(&db->db_mtx); 447*c717a561Smaybee dnode_undirty_dbufs(&dr->dt.di.dr_children); 448fa9e4066Sahrens } 449*c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 450*c717a561Smaybee dbuf_rele(db, (void *)(uintptr_t)txg); 451fa9e4066Sahrens } 452*c717a561Smaybee } 453fa9e4066Sahrens 454*c717a561Smaybee static void 455*c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 456*c717a561Smaybee { 457*c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 458*c717a561Smaybee 459*c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx)); 460*c717a561Smaybee 461*c717a561Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 462436b2950Sperrin (void) dnode_evict_dbufs(dn, 0); 463ea8dc4b6Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 464ea8dc4b6Seschrock 465ea8dc4b6Seschrock /* 466ea8dc4b6Seschrock * XXX - It would be nice to assert this, but we may still 467ea8dc4b6Seschrock * have residual holds from async evictions from the arc... 468ea8dc4b6Seschrock * 46955434c77Sek * zfs_obj_to_path() also depends on this being 47055434c77Sek * commented out. 47155434c77Sek * 472ea8dc4b6Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 473ea8dc4b6Seschrock */ 474fa9e4066Sahrens 475fa9e4066Sahrens /* Undirty next bits */ 476fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 477fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 478c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 479fa9e4066Sahrens 480fa9e4066Sahrens /* free up all the blocks in the file. */ 481fa9e4066Sahrens dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 48299653d4eSeschrock ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0); 483fa9e4066Sahrens 484fa9e4066Sahrens /* ASSERT(blkptrs are zero); */ 485fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 486fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 487fa9e4066Sahrens 488fa9e4066Sahrens ASSERT(dn->dn_free_txg > 0); 489fa9e4066Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 490fa9e4066Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 491fa9e4066Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 492fa9e4066Sahrens 493fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 494fa9e4066Sahrens dn->dn_type = DMU_OT_NONE; 495fa9e4066Sahrens dn->dn_maxblkid = 0; 496fa9e4066Sahrens dn->dn_allocated_txg = 0; 497fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 498fa9e4066Sahrens 499ea8dc4b6Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 500fa9e4066Sahrens 501fa9e4066Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 502fa9e4066Sahrens /* 503fa9e4066Sahrens * Now that we've released our hold, the dnode may 504fa9e4066Sahrens * be evicted, so we musn't access it. 505fa9e4066Sahrens */ 506fa9e4066Sahrens } 507fa9e4066Sahrens 508fa9e4066Sahrens /* 509*c717a561Smaybee * Write out the dnode's dirty buffers. 510fa9e4066Sahrens * 511fa9e4066Sahrens * NOTE: The dnode is kept in memory by being dirty. Once the 512fa9e4066Sahrens * dirty bit is cleared, it may be evicted. Beware of this! 513fa9e4066Sahrens */ 514*c717a561Smaybee void 515*c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 516fa9e4066Sahrens { 517fa9e4066Sahrens free_range_t *rp; 518fa9e4066Sahrens dnode_phys_t *dnp = dn->dn_phys; 519*c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 520*c717a561Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 521fa9e4066Sahrens 522fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 523fa9e4066Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 5249c9dc39aSek DNODE_VERIFY(dn); 525c543ec06Sahrens 526*c717a561Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 527fa9e4066Sahrens 528fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 529fa9e4066Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 530fa9e4066Sahrens /* The dnode is newly allocated or reallocated */ 531fa9e4066Sahrens if (dnp->dn_type == DMU_OT_NONE) { 532fa9e4066Sahrens /* this is a first alloc, not a realloc */ 533fa9e4066Sahrens /* XXX shouldn't the phys already be zeroed? */ 534fa9e4066Sahrens bzero(dnp, DNODE_CORE_SIZE); 535fa9e4066Sahrens dnp->dn_nlevels = 1; 536fa9e4066Sahrens } 537fa9e4066Sahrens 538fa9e4066Sahrens if (dn->dn_nblkptr > dnp->dn_nblkptr) { 539fa9e4066Sahrens /* zero the new blkptrs we are gaining */ 540fa9e4066Sahrens bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 541fa9e4066Sahrens sizeof (blkptr_t) * 542fa9e4066Sahrens (dn->dn_nblkptr - dnp->dn_nblkptr)); 543fa9e4066Sahrens } 544fa9e4066Sahrens dnp->dn_type = dn->dn_type; 545fa9e4066Sahrens dnp->dn_bonustype = dn->dn_bonustype; 546fa9e4066Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 547fa9e4066Sahrens dnp->dn_nblkptr = dn->dn_nblkptr; 548fa9e4066Sahrens } 549fa9e4066Sahrens 550*c717a561Smaybee ASSERT(dnp->dn_nlevels > 1 || 551f676ed34Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 552f676ed34Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 553f676ed34Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 554f676ed34Sahrens 555c543ec06Sahrens if (dn->dn_next_blksz[txgoff]) { 556c543ec06Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 557fa9e4066Sahrens SPA_MINBLOCKSIZE) == 0); 558f676ed34Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 559*c717a561Smaybee list_head(list) != NULL || 560347a31bcSahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 561347a31bcSahrens dnp->dn_datablkszsec); 562fa9e4066Sahrens dnp->dn_datablkszsec = 563c543ec06Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 564c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 565fa9e4066Sahrens } 566fa9e4066Sahrens 567fa9e4066Sahrens if (dn->dn_next_indblkshift[txgoff]) { 568fa9e4066Sahrens ASSERT(dnp->dn_nlevels == 1); 569fa9e4066Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 570fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 571fa9e4066Sahrens } 572fa9e4066Sahrens 573fa9e4066Sahrens /* 574fa9e4066Sahrens * Just take the live (open-context) values for checksum and compress. 575fa9e4066Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 576fa9e4066Sahrens * start using the new checksum or compress algorithm a little early. 577fa9e4066Sahrens */ 578fa9e4066Sahrens dnp->dn_checksum = dn->dn_checksum; 579fa9e4066Sahrens dnp->dn_compress = dn->dn_compress; 580fa9e4066Sahrens 581fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 582fa9e4066Sahrens 583fa9e4066Sahrens /* process all the "freed" ranges in the file */ 584fa9e4066Sahrens if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 58523b11526Smaybee for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 58623b11526Smaybee rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 587fa9e4066Sahrens dnode_sync_free_range(dn, 588fa9e4066Sahrens rp->fr_blkid, rp->fr_nblks, tx); 589fa9e4066Sahrens } 590fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 591fa9e4066Sahrens for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 592fa9e4066Sahrens free_range_t *last = rp; 593fa9e4066Sahrens rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 594fa9e4066Sahrens avl_remove(&dn->dn_ranges[txgoff], last); 595fa9e4066Sahrens kmem_free(last, sizeof (free_range_t)); 596fa9e4066Sahrens } 597fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 598fa9e4066Sahrens 599fa9e4066Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 600*c717a561Smaybee dnode_sync_free(dn, tx); 601*c717a561Smaybee return; 602fa9e4066Sahrens } 603fa9e4066Sahrens 604fa9e4066Sahrens if (dn->dn_next_nlevels[txgoff]) { 605*c717a561Smaybee dnode_increase_indirection(dn, tx); 606fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 607fa9e4066Sahrens } 608fa9e4066Sahrens 609*c717a561Smaybee dbuf_sync_list(list, tx); 610fa9e4066Sahrens 611*c717a561Smaybee if (dn->dn_object != DMU_META_DNODE_OBJECT) { 612*c717a561Smaybee ASSERT3P(list_head(list), ==, NULL); 613*c717a561Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 614fa9e4066Sahrens } 615*c717a561Smaybee 616*c717a561Smaybee /* 617*c717a561Smaybee * Although we have dropped our reference to the dnode, it 618*c717a561Smaybee * can't be evicted until its written, and we haven't yet 619*c717a561Smaybee * initiated the IO for the dnode's dbuf. 620*c717a561Smaybee */ 621fa9e4066Sahrens } 622