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 /* 2206e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25fa9e4066Sahrens #include <sys/zfs_context.h> 26fa9e4066Sahrens #include <sys/dbuf.h> 27fa9e4066Sahrens #include <sys/dnode.h> 28fa9e4066Sahrens #include <sys/dmu.h> 29fa9e4066Sahrens #include <sys/dmu_tx.h> 30fa9e4066Sahrens #include <sys/dmu_objset.h> 31fa9e4066Sahrens #include <sys/dsl_dataset.h> 32fa9e4066Sahrens #include <sys/spa.h> 33fa9e4066Sahrens 34fa9e4066Sahrens static void 35fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 36fa9e4066Sahrens { 37fa9e4066Sahrens dmu_buf_impl_t *db; 38c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 39c717a561Smaybee int nblkptr = dn->dn_phys->dn_nblkptr; 40c717a561Smaybee int old_toplvl = dn->dn_phys->dn_nlevels - 1; 41c717a561Smaybee int new_level = dn->dn_next_nlevels[txgoff]; 42fa9e4066Sahrens int i; 43fa9e4066Sahrens 44c717a561Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 45c717a561Smaybee 46c717a561Smaybee /* this dnode can't be paged out because it's dirty */ 47fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 48fa9e4066Sahrens ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 49c717a561Smaybee ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); 50fa9e4066Sahrens 51fa9e4066Sahrens db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 52ea8dc4b6Seschrock ASSERT(db != NULL); 53c717a561Smaybee 54c717a561Smaybee dn->dn_phys->dn_nlevels = new_level; 55f82bfe17Sgw dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, 56f82bfe17Sgw dn->dn_object, dn->dn_phys->dn_nlevels); 57c717a561Smaybee 58c717a561Smaybee /* check for existing blkptrs in the dnode */ 59c717a561Smaybee for (i = 0; i < nblkptr; i++) 60fa9e4066Sahrens if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 61fa9e4066Sahrens break; 62c717a561Smaybee if (i != nblkptr) { 63c717a561Smaybee /* transfer dnode's block pointers to new indirect block */ 64c717a561Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 65c717a561Smaybee ASSERT(db->db.db_data); 66c717a561Smaybee ASSERT(arc_released(db->db_buf)); 67c717a561Smaybee ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 68fa9e4066Sahrens bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 69c717a561Smaybee sizeof (blkptr_t) * nblkptr); 706b4acc8bSahrens arc_buf_freeze(db->db_buf); 71fa9e4066Sahrens } 72fa9e4066Sahrens 73fa9e4066Sahrens /* set dbuf's parent pointers to new indirect buf */ 74c717a561Smaybee for (i = 0; i < nblkptr; i++) { 75c717a561Smaybee dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); 76c717a561Smaybee 77fa9e4066Sahrens if (child == NULL) 78fa9e4066Sahrens continue; 79*744947dcSTom Erickson #ifdef DEBUG 80*744947dcSTom Erickson DB_DNODE_ENTER(child); 81*744947dcSTom Erickson ASSERT3P(DB_DNODE(child), ==, dn); 82*744947dcSTom Erickson DB_DNODE_EXIT(child); 83*744947dcSTom Erickson #endif /* DEBUG */ 84c717a561Smaybee if (child->db_parent && child->db_parent != dn->dn_dbuf) { 85c717a561Smaybee ASSERT(child->db_parent->db_level == db->db_level); 86c717a561Smaybee ASSERT(child->db_blkptr != 87c717a561Smaybee &dn->dn_phys->dn_blkptr[child->db_blkid]); 88fa9e4066Sahrens mutex_exit(&child->db_mtx); 89fa9e4066Sahrens continue; 90fa9e4066Sahrens } 91c717a561Smaybee ASSERT(child->db_parent == NULL || 92c717a561Smaybee child->db_parent == dn->dn_dbuf); 93c717a561Smaybee 94c717a561Smaybee child->db_parent = db; 95c717a561Smaybee dbuf_add_ref(db, child); 96c717a561Smaybee if (db->db.db_data) 97c717a561Smaybee child->db_blkptr = (blkptr_t *)db->db.db_data + i; 98c717a561Smaybee else 99c717a561Smaybee child->db_blkptr = NULL; 100c717a561Smaybee dprintf_dbuf_bp(child, child->db_blkptr, 101c717a561Smaybee "changed db_blkptr to new indirect %s", ""); 102fa9e4066Sahrens 103fa9e4066Sahrens mutex_exit(&child->db_mtx); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106c717a561Smaybee bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 107fa9e4066Sahrens 108ea8dc4b6Seschrock dbuf_rele(db, FTAG); 109c717a561Smaybee 110c717a561Smaybee rw_exit(&dn->dn_struct_rwlock); 111fa9e4066Sahrens } 112fa9e4066Sahrens 113cdb0ab79Smaybee static int 114fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 115fa9e4066Sahrens { 116cdb0ab79Smaybee dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 117fa9e4066Sahrens uint64_t bytesfreed = 0; 118cdb0ab79Smaybee int i, blocks_freed = 0; 119fa9e4066Sahrens 120cdb0ab79Smaybee dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); 121fa9e4066Sahrens 122fa9e4066Sahrens for (i = 0; i < num; i++, bp++) { 123fa9e4066Sahrens if (BP_IS_HOLE(bp)) 124fa9e4066Sahrens continue; 125fa9e4066Sahrens 126b24ab676SJeff Bonwick bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); 12799653d4eSeschrock ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 128c717a561Smaybee bzero(bp, sizeof (blkptr_t)); 129cdb0ab79Smaybee blocks_freed += 1; 130fa9e4066Sahrens } 131fa9e4066Sahrens dnode_diduse_space(dn, -bytesfreed); 132cdb0ab79Smaybee return (blocks_freed); 133fa9e4066Sahrens } 134fa9e4066Sahrens 1359c9dc39aSek #ifdef ZFS_DEBUG 136fa9e4066Sahrens static void 137fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 138fa9e4066Sahrens { 139fa9e4066Sahrens int off, num; 140fa9e4066Sahrens int i, err, epbs; 141fa9e4066Sahrens uint64_t txg = tx->tx_txg; 142*744947dcSTom Erickson dnode_t *dn; 143fa9e4066Sahrens 144*744947dcSTom Erickson DB_DNODE_ENTER(db); 145*744947dcSTom Erickson dn = DB_DNODE(db); 146*744947dcSTom Erickson epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 147fa9e4066Sahrens off = start - (db->db_blkid * 1<<epbs); 148fa9e4066Sahrens num = end - start + 1; 149fa9e4066Sahrens 150fa9e4066Sahrens ASSERT3U(off, >=, 0); 151fa9e4066Sahrens ASSERT3U(num, >=, 0); 152fa9e4066Sahrens ASSERT3U(db->db_level, >, 0); 153*744947dcSTom Erickson ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 154fa9e4066Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 155fa9e4066Sahrens ASSERT(db->db_blkptr != NULL); 156fa9e4066Sahrens 157fa9e4066Sahrens for (i = off; i < off+num; i++) { 158fa9e4066Sahrens uint64_t *buf; 159fa9e4066Sahrens dmu_buf_impl_t *child; 160c717a561Smaybee dbuf_dirty_record_t *dr; 161c717a561Smaybee int j; 162fa9e4066Sahrens 163fa9e4066Sahrens ASSERT(db->db_level == 1); 164fa9e4066Sahrens 165*744947dcSTom Erickson rw_enter(&dn->dn_struct_rwlock, RW_READER); 166*744947dcSTom Erickson err = dbuf_hold_impl(dn, db->db_level-1, 167f82bfe17Sgw (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 168*744947dcSTom Erickson rw_exit(&dn->dn_struct_rwlock); 169fa9e4066Sahrens if (err == ENOENT) 170fa9e4066Sahrens continue; 171fa9e4066Sahrens ASSERT(err == 0); 172fa9e4066Sahrens ASSERT(child->db_level == 0); 173c717a561Smaybee dr = child->db_last_dirty; 174c717a561Smaybee while (dr && dr->dr_txg > txg) 175c717a561Smaybee dr = dr->dr_next; 176c717a561Smaybee ASSERT(dr == NULL || dr->dr_txg == txg); 177c717a561Smaybee 178c717a561Smaybee /* data_old better be zeroed */ 179c717a561Smaybee if (dr) { 180c717a561Smaybee buf = dr->dt.dl.dr_data->b_data; 181fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 182fa9e4066Sahrens if (buf[j] != 0) { 183fa9e4066Sahrens panic("freed data not zero: " 184fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 185903a11ebSrh (void *)child, i, off, num); 186fa9e4066Sahrens } 187fa9e4066Sahrens } 188fa9e4066Sahrens } 189fa9e4066Sahrens 190fa9e4066Sahrens /* 191fa9e4066Sahrens * db_data better be zeroed unless it's dirty in a 192fa9e4066Sahrens * future txg. 193fa9e4066Sahrens */ 194fa9e4066Sahrens mutex_enter(&child->db_mtx); 195fa9e4066Sahrens buf = child->db.db_data; 196fa9e4066Sahrens if (buf != NULL && child->db_state != DB_FILL && 197c717a561Smaybee child->db_last_dirty == NULL) { 198fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 199fa9e4066Sahrens if (buf[j] != 0) { 200fa9e4066Sahrens panic("freed data not zero: " 201fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 202903a11ebSrh (void *)child, i, off, num); 203fa9e4066Sahrens } 204fa9e4066Sahrens } 205fa9e4066Sahrens } 206fa9e4066Sahrens mutex_exit(&child->db_mtx); 207fa9e4066Sahrens 208ea8dc4b6Seschrock dbuf_rele(child, FTAG); 209fa9e4066Sahrens } 210*744947dcSTom Erickson DB_DNODE_EXIT(db); 211fa9e4066Sahrens } 2129c9dc39aSek #endif 213fa9e4066Sahrens 214cdb0ab79Smaybee #define ALL -1 215cdb0ab79Smaybee 216fa9e4066Sahrens static int 217fa9e4066Sahrens free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 218fa9e4066Sahrens dmu_tx_t *tx) 219fa9e4066Sahrens { 220*744947dcSTom Erickson dnode_t *dn; 221fa9e4066Sahrens blkptr_t *bp; 222fa9e4066Sahrens dmu_buf_impl_t *subdb; 223fa9e4066Sahrens uint64_t start, end, dbstart, dbend, i; 224fa9e4066Sahrens int epbs, shift, err; 225fa9e4066Sahrens int all = TRUE; 226cdb0ab79Smaybee int blocks_freed = 0; 227cdb0ab79Smaybee 228cdb0ab79Smaybee /* 229cdb0ab79Smaybee * There is a small possibility that this block will not be cached: 230cdb0ab79Smaybee * 1 - if level > 1 and there are no children with level <= 1 231cdb0ab79Smaybee * 2 - if we didn't get a dirty hold (because this block had just 232cdb0ab79Smaybee * finished being written -- and so had no holds), and then this 233cdb0ab79Smaybee * block got evicted before we got here. 234cdb0ab79Smaybee */ 235cdb0ab79Smaybee if (db->db_state != DB_CACHED) 236cdb0ab79Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 237fa9e4066Sahrens 2383f9d6ad7SLin Ling dbuf_release_bp(db); 239fa9e4066Sahrens bp = (blkptr_t *)db->db.db_data; 240fa9e4066Sahrens 241*744947dcSTom Erickson DB_DNODE_ENTER(db); 242*744947dcSTom Erickson dn = DB_DNODE(db); 243*744947dcSTom Erickson epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 244fa9e4066Sahrens shift = (db->db_level - 1) * epbs; 245fa9e4066Sahrens dbstart = db->db_blkid << epbs; 246fa9e4066Sahrens start = blkid >> shift; 247fa9e4066Sahrens if (dbstart < start) { 248fa9e4066Sahrens bp += start - dbstart; 249fa9e4066Sahrens all = FALSE; 250fa9e4066Sahrens } else { 251fa9e4066Sahrens start = dbstart; 252fa9e4066Sahrens } 253fa9e4066Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 254fa9e4066Sahrens end = (blkid + nblks - 1) >> shift; 255fa9e4066Sahrens if (dbend <= end) 256fa9e4066Sahrens end = dbend; 257fa9e4066Sahrens else if (all) 258fa9e4066Sahrens all = trunc; 259fa9e4066Sahrens ASSERT3U(start, <=, end); 260fa9e4066Sahrens 261fa9e4066Sahrens if (db->db_level == 1) { 2629c9dc39aSek FREE_VERIFY(db, start, end, tx); 263cdb0ab79Smaybee blocks_freed = free_blocks(dn, bp, end-start+1, tx); 2646b4acc8bSahrens arc_buf_freeze(db->db_buf); 265cdb0ab79Smaybee ASSERT(all || blocks_freed == 0 || db->db_last_dirty); 266*744947dcSTom Erickson DB_DNODE_EXIT(db); 267cdb0ab79Smaybee return (all ? ALL : blocks_freed); 268fa9e4066Sahrens } 269fa9e4066Sahrens 270fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 271fa9e4066Sahrens if (BP_IS_HOLE(bp)) 272fa9e4066Sahrens continue; 273fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 274fa9e4066Sahrens err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 275fa9e4066Sahrens ASSERT3U(err, ==, 0); 276fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 277fa9e4066Sahrens 278cdb0ab79Smaybee if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) { 279fa9e4066Sahrens ASSERT3P(subdb->db_blkptr, ==, bp); 280cdb0ab79Smaybee blocks_freed += free_blocks(dn, bp, 1, tx); 28123b11526Smaybee } else { 28223b11526Smaybee all = FALSE; 283fa9e4066Sahrens } 284ea8dc4b6Seschrock dbuf_rele(subdb, FTAG); 285fa9e4066Sahrens } 286*744947dcSTom Erickson DB_DNODE_EXIT(db); 2876b4acc8bSahrens arc_buf_freeze(db->db_buf); 288fa9e4066Sahrens #ifdef ZFS_DEBUG 289fa9e4066Sahrens bp -= (end-start)+1; 290fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 291fa9e4066Sahrens if (i == start && blkid != 0) 292fa9e4066Sahrens continue; 293fa9e4066Sahrens else if (i == end && !trunc) 294fa9e4066Sahrens continue; 295fa9e4066Sahrens ASSERT3U(bp->blk_birth, ==, 0); 296fa9e4066Sahrens } 297fa9e4066Sahrens #endif 298cdb0ab79Smaybee ASSERT(all || blocks_freed == 0 || db->db_last_dirty); 299cdb0ab79Smaybee return (all ? ALL : blocks_freed); 300fa9e4066Sahrens } 301fa9e4066Sahrens 302fa9e4066Sahrens /* 303fa9e4066Sahrens * free_range: Traverse the indicated range of the provided file 304fa9e4066Sahrens * and "free" all the blocks contained there. 305fa9e4066Sahrens */ 306fa9e4066Sahrens static void 307fa9e4066Sahrens dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 308fa9e4066Sahrens { 309fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 310fa9e4066Sahrens dmu_buf_impl_t *db; 311fa9e4066Sahrens int trunc, start, end, shift, i, err; 312fa9e4066Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 313fa9e4066Sahrens 314fa9e4066Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 315fa9e4066Sahrens return; 316fa9e4066Sahrens 317fa9e4066Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 318fa9e4066Sahrens trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 319fa9e4066Sahrens if (trunc) 320fa9e4066Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 321fa9e4066Sahrens 322fa9e4066Sahrens /* There are no indirect blocks in the object */ 323fa9e4066Sahrens if (dnlevel == 1) { 324fa9e4066Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 325fa9e4066Sahrens /* this range was never made persistent */ 326fa9e4066Sahrens return; 327fa9e4066Sahrens } 328fa9e4066Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 329cdb0ab79Smaybee (void) free_blocks(dn, bp + blkid, nblks, tx); 330fa9e4066Sahrens if (trunc) { 331fa9e4066Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 332fa9e4066Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 333fa9e4066Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 334fa9e4066Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 335fa9e4066Sahrens dn->dn_phys->dn_maxblkid == 0 || 336cdb0ab79Smaybee dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); 337fa9e4066Sahrens } 338fa9e4066Sahrens return; 339fa9e4066Sahrens } 340fa9e4066Sahrens 341fa9e4066Sahrens shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 342fa9e4066Sahrens start = blkid >> shift; 343fa9e4066Sahrens ASSERT(start < dn->dn_phys->dn_nblkptr); 344fa9e4066Sahrens end = (blkid + nblks - 1) >> shift; 345fa9e4066Sahrens bp += start; 346fa9e4066Sahrens for (i = start; i <= end; i++, bp++) { 347fa9e4066Sahrens if (BP_IS_HOLE(bp)) 348fa9e4066Sahrens continue; 349fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 350fa9e4066Sahrens err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 351fa9e4066Sahrens ASSERT3U(err, ==, 0); 352fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 353fa9e4066Sahrens 354cdb0ab79Smaybee if (free_children(db, blkid, nblks, trunc, tx) == ALL) { 355fa9e4066Sahrens ASSERT3P(db->db_blkptr, ==, bp); 356cdb0ab79Smaybee (void) free_blocks(dn, bp, 1, tx); 357fa9e4066Sahrens } 358ea8dc4b6Seschrock dbuf_rele(db, FTAG); 359fa9e4066Sahrens } 360fa9e4066Sahrens if (trunc) { 361fa9e4066Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 362fa9e4066Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 363fa9e4066Sahrens dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 364fa9e4066Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 365fa9e4066Sahrens dn->dn_phys->dn_maxblkid == 0 || 366cdb0ab79Smaybee dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); 367fa9e4066Sahrens } 368fa9e4066Sahrens } 369fa9e4066Sahrens 370ea8dc4b6Seschrock /* 371ea8dc4b6Seschrock * Try to kick all the dnodes dbufs out of the cache... 372ea8dc4b6Seschrock */ 3731934e92fSmaybee void 3741934e92fSmaybee dnode_evict_dbufs(dnode_t *dn) 375ea8dc4b6Seschrock { 376c543ec06Sahrens int progress; 377c543ec06Sahrens int pass = 0; 378c543ec06Sahrens 379c543ec06Sahrens do { 380d90a49d6Smaybee dmu_buf_impl_t *db, marker; 381c543ec06Sahrens int evicting = FALSE; 382c543ec06Sahrens 383c543ec06Sahrens progress = FALSE; 384c543ec06Sahrens mutex_enter(&dn->dn_dbufs_mtx); 385d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 386d90a49d6Smaybee db = list_head(&dn->dn_dbufs); 387d90a49d6Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 388d90a49d6Smaybee list_remove(&dn->dn_dbufs, db); 389d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, db); 390*744947dcSTom Erickson #ifdef DEBUG 391*744947dcSTom Erickson DB_DNODE_ENTER(db); 392*744947dcSTom Erickson ASSERT3P(DB_DNODE(db), ==, dn); 393*744947dcSTom Erickson DB_DNODE_EXIT(db); 394*744947dcSTom Erickson #endif /* DEBUG */ 395ea8dc4b6Seschrock 396ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 397c543ec06Sahrens if (db->db_state == DB_EVICTING) { 398c543ec06Sahrens progress = TRUE; 399c543ec06Sahrens evicting = TRUE; 400c543ec06Sahrens mutex_exit(&db->db_mtx); 401c543ec06Sahrens } else if (refcount_is_zero(&db->db_holds)) { 402c543ec06Sahrens progress = TRUE; 403c543ec06Sahrens dbuf_clear(db); /* exits db_mtx for us */ 404c543ec06Sahrens } else { 405c543ec06Sahrens mutex_exit(&db->db_mtx); 406c543ec06Sahrens } 407c543ec06Sahrens 408ea8dc4b6Seschrock } 409d90a49d6Smaybee list_remove(&dn->dn_dbufs, &marker); 410c543ec06Sahrens /* 411c543ec06Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 412c543ec06Sahrens * that any DB_EVICTING dbufs can make progress. 413c543ec06Sahrens * Ideally, we would have some cv we could wait on, but 414c543ec06Sahrens * since we don't, just wait a bit to give the other 415c543ec06Sahrens * thread a chance to run. 416c543ec06Sahrens */ 417c543ec06Sahrens mutex_exit(&dn->dn_dbufs_mtx); 418c543ec06Sahrens if (evicting) 419c543ec06Sahrens delay(1); 420c543ec06Sahrens pass++; 421c543ec06Sahrens ASSERT(pass < 100); /* sanity check */ 422c543ec06Sahrens } while (progress); 423c543ec06Sahrens 424ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 425ea8dc4b6Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 426ea8dc4b6Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 427ea8dc4b6Seschrock dbuf_evict(dn->dn_bonus); 428ea8dc4b6Seschrock dn->dn_bonus = NULL; 429ea8dc4b6Seschrock } 430ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 431ea8dc4b6Seschrock } 432ea8dc4b6Seschrock 433c717a561Smaybee static void 434c717a561Smaybee dnode_undirty_dbufs(list_t *list) 435fa9e4066Sahrens { 436c717a561Smaybee dbuf_dirty_record_t *dr; 437fa9e4066Sahrens 438c717a561Smaybee while (dr = list_head(list)) { 439c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 440c717a561Smaybee uint64_t txg = dr->dr_txg; 441fa9e4066Sahrens 442b24ab676SJeff Bonwick if (db->db_level != 0) 443b24ab676SJeff Bonwick dnode_undirty_dbufs(&dr->dt.di.dr_children); 444b24ab676SJeff Bonwick 445fa9e4066Sahrens mutex_enter(&db->db_mtx); 446fa9e4066Sahrens /* XXX - use dbuf_undirty()? */ 447c717a561Smaybee list_remove(list, dr); 448c717a561Smaybee ASSERT(db->db_last_dirty == dr); 449c717a561Smaybee db->db_last_dirty = NULL; 450c717a561Smaybee db->db_dirtycnt -= 1; 451fa9e4066Sahrens if (db->db_level == 0) { 4520a586ceaSMark Shellenbaum ASSERT(db->db_blkid == DMU_BONUS_BLKID || 453c717a561Smaybee dr->dt.dl.dr_data == db->db_buf); 454c717a561Smaybee dbuf_unoverride(dr); 455fa9e4066Sahrens } 456c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 457b24ab676SJeff Bonwick dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); 458fa9e4066Sahrens } 459c717a561Smaybee } 460fa9e4066Sahrens 461c717a561Smaybee static void 462c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 463c717a561Smaybee { 464c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 465c717a561Smaybee 466c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx)); 467c717a561Smaybee 468cdb0ab79Smaybee /* 469cdb0ab79Smaybee * Our contents should have been freed in dnode_sync() by the 470cdb0ab79Smaybee * free range record inserted by the caller of dnode_free(). 471cdb0ab79Smaybee */ 472cdb0ab79Smaybee ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0); 473cdb0ab79Smaybee ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); 474cdb0ab79Smaybee 475c717a561Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 4761934e92fSmaybee dnode_evict_dbufs(dn); 477ea8dc4b6Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 478ea8dc4b6Seschrock 479ea8dc4b6Seschrock /* 480ea8dc4b6Seschrock * XXX - It would be nice to assert this, but we may still 481ea8dc4b6Seschrock * have residual holds from async evictions from the arc... 482ea8dc4b6Seschrock * 48355434c77Sek * zfs_obj_to_path() also depends on this being 48455434c77Sek * commented out. 48555434c77Sek * 486ea8dc4b6Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 487ea8dc4b6Seschrock */ 488fa9e4066Sahrens 489fa9e4066Sahrens /* Undirty next bits */ 490fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 491fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 492c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 493fa9e4066Sahrens 494fa9e4066Sahrens /* ASSERT(blkptrs are zero); */ 495fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 496fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 497fa9e4066Sahrens 498fa9e4066Sahrens ASSERT(dn->dn_free_txg > 0); 499fa9e4066Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 500fa9e4066Sahrens dbuf_will_dirty(dn->dn_dbuf, tx); 501fa9e4066Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 502fa9e4066Sahrens 503fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 504fa9e4066Sahrens dn->dn_type = DMU_OT_NONE; 505fa9e4066Sahrens dn->dn_maxblkid = 0; 506fa9e4066Sahrens dn->dn_allocated_txg = 0; 507758f6e0bSgw dn->dn_free_txg = 0; 5080a586ceaSMark Shellenbaum dn->dn_have_spill = B_FALSE; 509fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 510fa9e4066Sahrens 511ea8dc4b6Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 512fa9e4066Sahrens 513fa9e4066Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 514fa9e4066Sahrens /* 515fa9e4066Sahrens * Now that we've released our hold, the dnode may 516fa9e4066Sahrens * be evicted, so we musn't access it. 517fa9e4066Sahrens */ 518fa9e4066Sahrens } 519fa9e4066Sahrens 520fa9e4066Sahrens /* 521c717a561Smaybee * Write out the dnode's dirty buffers. 522fa9e4066Sahrens */ 523c717a561Smaybee void 524c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 525fa9e4066Sahrens { 526fa9e4066Sahrens free_range_t *rp; 527fa9e4066Sahrens dnode_phys_t *dnp = dn->dn_phys; 528c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 529c717a561Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 53014843421SMatthew Ahrens static const dnode_phys_t zerodn = { 0 }; 5310a586ceaSMark Shellenbaum boolean_t kill_spill = B_FALSE; 532fa9e4066Sahrens 533fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 534fa9e4066Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 53514843421SMatthew Ahrens ASSERT(dnp->dn_type != DMU_OT_NONE || 53614843421SMatthew Ahrens bcmp(dnp, &zerodn, DNODE_SIZE) == 0); 5379c9dc39aSek DNODE_VERIFY(dn); 538c543ec06Sahrens 539c717a561Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 540fa9e4066Sahrens 54114843421SMatthew Ahrens if (dmu_objset_userused_enabled(dn->dn_objset) && 54214843421SMatthew Ahrens !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 5430a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 5440a586ceaSMark Shellenbaum dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); 5450a586ceaSMark Shellenbaum dn->dn_oldflags = dn->dn_phys->dn_flags; 54614843421SMatthew Ahrens dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; 5470a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 54806e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dn, B_FALSE, tx); 54914843421SMatthew Ahrens } else { 55014843421SMatthew Ahrens /* Once we account for it, we should always account for it. */ 55114843421SMatthew Ahrens ASSERT(!(dn->dn_phys->dn_flags & 55214843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED)); 55314843421SMatthew Ahrens } 55414843421SMatthew Ahrens 555fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 556fa9e4066Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 557fa9e4066Sahrens /* The dnode is newly allocated or reallocated */ 558fa9e4066Sahrens if (dnp->dn_type == DMU_OT_NONE) { 559fa9e4066Sahrens /* this is a first alloc, not a realloc */ 560fa9e4066Sahrens dnp->dn_nlevels = 1; 561da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_nblkptr; 562fa9e4066Sahrens } 563fa9e4066Sahrens 564fa9e4066Sahrens dnp->dn_type = dn->dn_type; 565fa9e4066Sahrens dnp->dn_bonustype = dn->dn_bonustype; 566fa9e4066Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 567fa9e4066Sahrens } 568fa9e4066Sahrens 569c717a561Smaybee ASSERT(dnp->dn_nlevels > 1 || 570f676ed34Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 571f676ed34Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 572f676ed34Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 573f676ed34Sahrens 574c543ec06Sahrens if (dn->dn_next_blksz[txgoff]) { 575c543ec06Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 576fa9e4066Sahrens SPA_MINBLOCKSIZE) == 0); 577f676ed34Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 578cdb0ab79Smaybee dn->dn_maxblkid == 0 || list_head(list) != NULL || 57906e0070dSMark Shellenbaum avl_last(&dn->dn_ranges[txgoff]) || 580347a31bcSahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 581347a31bcSahrens dnp->dn_datablkszsec); 582fa9e4066Sahrens dnp->dn_datablkszsec = 583c543ec06Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 584c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 585fa9e4066Sahrens } 586fa9e4066Sahrens 5871934e92fSmaybee if (dn->dn_next_bonuslen[txgoff]) { 5881934e92fSmaybee if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) 5891934e92fSmaybee dnp->dn_bonuslen = 0; 5901934e92fSmaybee else 5911934e92fSmaybee dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; 5921934e92fSmaybee ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); 5931934e92fSmaybee dn->dn_next_bonuslen[txgoff] = 0; 5941934e92fSmaybee } 5951934e92fSmaybee 5960a586ceaSMark Shellenbaum if (dn->dn_next_bonustype[txgoff]) { 5970a586ceaSMark Shellenbaum ASSERT(dn->dn_next_bonustype[txgoff] < DMU_OT_NUMTYPES); 5980a586ceaSMark Shellenbaum dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; 5990a586ceaSMark Shellenbaum dn->dn_next_bonustype[txgoff] = 0; 6000a586ceaSMark Shellenbaum } 6010a586ceaSMark Shellenbaum 6020a586ceaSMark Shellenbaum /* 6030a586ceaSMark Shellenbaum * We will either remove a spill block when a file is being removed 6040a586ceaSMark Shellenbaum * or we have been asked to remove it. 6050a586ceaSMark Shellenbaum */ 6060a586ceaSMark Shellenbaum if (dn->dn_rm_spillblk[txgoff] || 6070a586ceaSMark Shellenbaum ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && 6080a586ceaSMark Shellenbaum dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg)) { 6090a586ceaSMark Shellenbaum if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 6100a586ceaSMark Shellenbaum kill_spill = B_TRUE; 6110a586ceaSMark Shellenbaum dn->dn_rm_spillblk[txgoff] = 0; 6120a586ceaSMark Shellenbaum } 6130a586ceaSMark Shellenbaum 614fa9e4066Sahrens if (dn->dn_next_indblkshift[txgoff]) { 615fa9e4066Sahrens ASSERT(dnp->dn_nlevels == 1); 616fa9e4066Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 617fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 618fa9e4066Sahrens } 619fa9e4066Sahrens 620fa9e4066Sahrens /* 621fa9e4066Sahrens * Just take the live (open-context) values for checksum and compress. 622fa9e4066Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 623fa9e4066Sahrens * start using the new checksum or compress algorithm a little early. 624fa9e4066Sahrens */ 625fa9e4066Sahrens dnp->dn_checksum = dn->dn_checksum; 626fa9e4066Sahrens dnp->dn_compress = dn->dn_compress; 627fa9e4066Sahrens 628fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 629fa9e4066Sahrens 6300a586ceaSMark Shellenbaum if (kill_spill) { 6310a586ceaSMark Shellenbaum (void) free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx); 6320a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 6330a586ceaSMark Shellenbaum dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; 6340a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 6350a586ceaSMark Shellenbaum } 6360a586ceaSMark Shellenbaum 637fa9e4066Sahrens /* process all the "freed" ranges in the file */ 638cdb0ab79Smaybee while (rp = avl_last(&dn->dn_ranges[txgoff])) { 639cdb0ab79Smaybee dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx); 640cdb0ab79Smaybee /* grab the mutex so we don't race with dnode_block_freed() */ 641cdb0ab79Smaybee mutex_enter(&dn->dn_mtx); 642cdb0ab79Smaybee avl_remove(&dn->dn_ranges[txgoff], rp); 643cdb0ab79Smaybee mutex_exit(&dn->dn_mtx); 644cdb0ab79Smaybee kmem_free(rp, sizeof (free_range_t)); 645fa9e4066Sahrens } 6461934e92fSmaybee 647fa9e4066Sahrens if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 648c717a561Smaybee dnode_sync_free(dn, tx); 649c717a561Smaybee return; 650fa9e4066Sahrens } 651fa9e4066Sahrens 652da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff]) { 653da03de99SMark Maybee /* this should only happen on a realloc */ 654da03de99SMark Maybee ASSERT(dn->dn_allocated_txg == tx->tx_txg); 655da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { 656da03de99SMark Maybee /* zero the new blkptrs we are gaining */ 657da03de99SMark Maybee bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 658da03de99SMark Maybee sizeof (blkptr_t) * 659da03de99SMark Maybee (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); 660da03de99SMark Maybee #ifdef ZFS_DEBUG 661da03de99SMark Maybee } else { 662da03de99SMark Maybee int i; 663da03de99SMark Maybee ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); 664da03de99SMark Maybee /* the blkptrs we are losing better be unallocated */ 665da03de99SMark Maybee for (i = dn->dn_next_nblkptr[txgoff]; 666da03de99SMark Maybee i < dnp->dn_nblkptr; i++) 667da03de99SMark Maybee ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); 668da03de99SMark Maybee #endif 669da03de99SMark Maybee } 670da03de99SMark Maybee mutex_enter(&dn->dn_mtx); 671da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; 672da03de99SMark Maybee dn->dn_next_nblkptr[txgoff] = 0; 673da03de99SMark Maybee mutex_exit(&dn->dn_mtx); 674da03de99SMark Maybee } 675da03de99SMark Maybee 676fa9e4066Sahrens if (dn->dn_next_nlevels[txgoff]) { 677c717a561Smaybee dnode_increase_indirection(dn, tx); 678fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 679fa9e4066Sahrens } 680fa9e4066Sahrens 681c717a561Smaybee dbuf_sync_list(list, tx); 682fa9e4066Sahrens 68314843421SMatthew Ahrens if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 684c717a561Smaybee ASSERT3P(list_head(list), ==, NULL); 685c717a561Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 686fa9e4066Sahrens } 687c717a561Smaybee 688c717a561Smaybee /* 689c717a561Smaybee * Although we have dropped our reference to the dnode, it 690c717a561Smaybee * can't be evicted until its written, and we haven't yet 691c717a561Smaybee * initiated the IO for the dnode's dbuf. 692c717a561Smaybee */ 693fa9e4066Sahrens } 694