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 */ 21ad135b5dSChristopher Siden 22fa9e4066Sahrens /* 2306e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24bf16b11eSMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <sys/zfs_context.h> 28fa9e4066Sahrens #include <sys/dbuf.h> 29fa9e4066Sahrens #include <sys/dnode.h> 30fa9e4066Sahrens #include <sys/dmu.h> 31fa9e4066Sahrens #include <sys/dmu_tx.h> 32fa9e4066Sahrens #include <sys/dmu_objset.h> 33fa9e4066Sahrens #include <sys/dsl_dataset.h> 34fa9e4066Sahrens #include <sys/spa.h> 35bf16b11eSMatthew Ahrens #include <sys/range_tree.h> 3643466aaeSMax Grossman #include <sys/zfeature.h> 37fa9e4066Sahrens 38fa9e4066Sahrens static void 39fa9e4066Sahrens dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 40fa9e4066Sahrens { 41fa9e4066Sahrens dmu_buf_impl_t *db; 42c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 43c717a561Smaybee int nblkptr = dn->dn_phys->dn_nblkptr; 44c717a561Smaybee int old_toplvl = dn->dn_phys->dn_nlevels - 1; 45c717a561Smaybee int new_level = dn->dn_next_nlevels[txgoff]; 46fa9e4066Sahrens int i; 47fa9e4066Sahrens 48c717a561Smaybee rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 49c717a561Smaybee 50c717a561Smaybee /* this dnode can't be paged out because it's dirty */ 51fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 52fa9e4066Sahrens ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 53c717a561Smaybee ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); 54fa9e4066Sahrens 55fa9e4066Sahrens db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 56ea8dc4b6Seschrock ASSERT(db != NULL); 57c717a561Smaybee 58c717a561Smaybee dn->dn_phys->dn_nlevels = new_level; 59f82bfe17Sgw dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, 60f82bfe17Sgw dn->dn_object, dn->dn_phys->dn_nlevels); 61c717a561Smaybee 62c717a561Smaybee /* check for existing blkptrs in the dnode */ 63c717a561Smaybee for (i = 0; i < nblkptr; i++) 64fa9e4066Sahrens if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 65fa9e4066Sahrens break; 66c717a561Smaybee if (i != nblkptr) { 67c717a561Smaybee /* transfer dnode's block pointers to new indirect block */ 68c717a561Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 69c717a561Smaybee ASSERT(db->db.db_data); 70c717a561Smaybee ASSERT(arc_released(db->db_buf)); 71c717a561Smaybee ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 72fa9e4066Sahrens bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 73c717a561Smaybee sizeof (blkptr_t) * nblkptr); 746b4acc8bSahrens arc_buf_freeze(db->db_buf); 75fa9e4066Sahrens } 76fa9e4066Sahrens 77fa9e4066Sahrens /* set dbuf's parent pointers to new indirect buf */ 78c717a561Smaybee for (i = 0; i < nblkptr; i++) { 79c717a561Smaybee dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); 80c717a561Smaybee 81fa9e4066Sahrens if (child == NULL) 82fa9e4066Sahrens continue; 83744947dcSTom Erickson #ifdef DEBUG 84744947dcSTom Erickson DB_DNODE_ENTER(child); 85744947dcSTom Erickson ASSERT3P(DB_DNODE(child), ==, dn); 86744947dcSTom Erickson DB_DNODE_EXIT(child); 87744947dcSTom Erickson #endif /* DEBUG */ 88c717a561Smaybee if (child->db_parent && child->db_parent != dn->dn_dbuf) { 89c717a561Smaybee ASSERT(child->db_parent->db_level == db->db_level); 90c717a561Smaybee ASSERT(child->db_blkptr != 91c717a561Smaybee &dn->dn_phys->dn_blkptr[child->db_blkid]); 92fa9e4066Sahrens mutex_exit(&child->db_mtx); 93fa9e4066Sahrens continue; 94fa9e4066Sahrens } 95c717a561Smaybee ASSERT(child->db_parent == NULL || 96c717a561Smaybee child->db_parent == dn->dn_dbuf); 97c717a561Smaybee 98c717a561Smaybee child->db_parent = db; 99c717a561Smaybee dbuf_add_ref(db, child); 100c717a561Smaybee if (db->db.db_data) 101c717a561Smaybee child->db_blkptr = (blkptr_t *)db->db.db_data + i; 102c717a561Smaybee else 103c717a561Smaybee child->db_blkptr = NULL; 104c717a561Smaybee dprintf_dbuf_bp(child, child->db_blkptr, 105c717a561Smaybee "changed db_blkptr to new indirect %s", ""); 106fa9e4066Sahrens 107fa9e4066Sahrens mutex_exit(&child->db_mtx); 108fa9e4066Sahrens } 109fa9e4066Sahrens 110c717a561Smaybee bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 111fa9e4066Sahrens 112ea8dc4b6Seschrock dbuf_rele(db, FTAG); 113c717a561Smaybee 114c717a561Smaybee rw_exit(&dn->dn_struct_rwlock); 115fa9e4066Sahrens } 116fa9e4066Sahrens 11743466aaeSMax Grossman static void 118fa9e4066Sahrens free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 119fa9e4066Sahrens { 120cdb0ab79Smaybee dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 121fa9e4066Sahrens uint64_t bytesfreed = 0; 122fa9e4066Sahrens 123cdb0ab79Smaybee dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); 124fa9e4066Sahrens 12543466aaeSMax Grossman for (int i = 0; i < num; i++, bp++) { 126fa9e4066Sahrens if (BP_IS_HOLE(bp)) 127fa9e4066Sahrens continue; 128fa9e4066Sahrens 129b24ab676SJeff Bonwick bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); 13099653d4eSeschrock ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 13143466aaeSMax Grossman 13243466aaeSMax Grossman /* 13343466aaeSMax Grossman * Save some useful information on the holes being 13443466aaeSMax Grossman * punched, including logical size, type, and indirection 13543466aaeSMax Grossman * level. Retaining birth time enables detection of when 13643466aaeSMax Grossman * holes are punched for reducing the number of free 13743466aaeSMax Grossman * records transmitted during a zfs send. 13843466aaeSMax Grossman */ 13943466aaeSMax Grossman 14043466aaeSMax Grossman uint64_t lsize = BP_GET_LSIZE(bp); 14143466aaeSMax Grossman dmu_object_type_t type = BP_GET_TYPE(bp); 14243466aaeSMax Grossman uint64_t lvl = BP_GET_LEVEL(bp); 14343466aaeSMax Grossman 144c717a561Smaybee bzero(bp, sizeof (blkptr_t)); 14543466aaeSMax Grossman 14643466aaeSMax Grossman if (spa_feature_is_active(dn->dn_objset->os_spa, 14743466aaeSMax Grossman SPA_FEATURE_HOLE_BIRTH)) { 14843466aaeSMax Grossman BP_SET_LSIZE(bp, lsize); 14943466aaeSMax Grossman BP_SET_TYPE(bp, type); 15043466aaeSMax Grossman BP_SET_LEVEL(bp, lvl); 15143466aaeSMax Grossman BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0); 15243466aaeSMax Grossman } 153fa9e4066Sahrens } 154fa9e4066Sahrens dnode_diduse_space(dn, -bytesfreed); 155fa9e4066Sahrens } 156fa9e4066Sahrens 1579c9dc39aSek #ifdef ZFS_DEBUG 158fa9e4066Sahrens static void 159fa9e4066Sahrens free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 160fa9e4066Sahrens { 161fa9e4066Sahrens int off, num; 162fa9e4066Sahrens int i, err, epbs; 163fa9e4066Sahrens uint64_t txg = tx->tx_txg; 164744947dcSTom Erickson dnode_t *dn; 165fa9e4066Sahrens 166744947dcSTom Erickson DB_DNODE_ENTER(db); 167744947dcSTom Erickson dn = DB_DNODE(db); 168744947dcSTom Erickson epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 169fa9e4066Sahrens off = start - (db->db_blkid * 1<<epbs); 170fa9e4066Sahrens num = end - start + 1; 171fa9e4066Sahrens 172fa9e4066Sahrens ASSERT3U(off, >=, 0); 173fa9e4066Sahrens ASSERT3U(num, >=, 0); 174fa9e4066Sahrens ASSERT3U(db->db_level, >, 0); 175744947dcSTom Erickson ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 176fa9e4066Sahrens ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 177fa9e4066Sahrens ASSERT(db->db_blkptr != NULL); 178fa9e4066Sahrens 179fa9e4066Sahrens for (i = off; i < off+num; i++) { 180fa9e4066Sahrens uint64_t *buf; 181fa9e4066Sahrens dmu_buf_impl_t *child; 182c717a561Smaybee dbuf_dirty_record_t *dr; 183c717a561Smaybee int j; 184fa9e4066Sahrens 185fa9e4066Sahrens ASSERT(db->db_level == 1); 186fa9e4066Sahrens 187744947dcSTom Erickson rw_enter(&dn->dn_struct_rwlock, RW_READER); 188744947dcSTom Erickson err = dbuf_hold_impl(dn, db->db_level-1, 189f82bfe17Sgw (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 190744947dcSTom Erickson rw_exit(&dn->dn_struct_rwlock); 191fa9e4066Sahrens if (err == ENOENT) 192fa9e4066Sahrens continue; 193fa9e4066Sahrens ASSERT(err == 0); 194fa9e4066Sahrens ASSERT(child->db_level == 0); 195c717a561Smaybee dr = child->db_last_dirty; 196c717a561Smaybee while (dr && dr->dr_txg > txg) 197c717a561Smaybee dr = dr->dr_next; 198c717a561Smaybee ASSERT(dr == NULL || dr->dr_txg == txg); 199c717a561Smaybee 200c717a561Smaybee /* data_old better be zeroed */ 201c717a561Smaybee if (dr) { 202c717a561Smaybee buf = dr->dt.dl.dr_data->b_data; 203fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 204fa9e4066Sahrens if (buf[j] != 0) { 205fa9e4066Sahrens panic("freed data not zero: " 206fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 207903a11ebSrh (void *)child, i, off, num); 208fa9e4066Sahrens } 209fa9e4066Sahrens } 210fa9e4066Sahrens } 211fa9e4066Sahrens 212fa9e4066Sahrens /* 213fa9e4066Sahrens * db_data better be zeroed unless it's dirty in a 214fa9e4066Sahrens * future txg. 215fa9e4066Sahrens */ 216fa9e4066Sahrens mutex_enter(&child->db_mtx); 217fa9e4066Sahrens buf = child->db.db_data; 218fa9e4066Sahrens if (buf != NULL && child->db_state != DB_FILL && 219c717a561Smaybee child->db_last_dirty == NULL) { 220fa9e4066Sahrens for (j = 0; j < child->db.db_size >> 3; j++) { 221fa9e4066Sahrens if (buf[j] != 0) { 222fa9e4066Sahrens panic("freed data not zero: " 223fa9e4066Sahrens "child=%p i=%d off=%d num=%d\n", 224903a11ebSrh (void *)child, i, off, num); 225fa9e4066Sahrens } 226fa9e4066Sahrens } 227fa9e4066Sahrens } 228fa9e4066Sahrens mutex_exit(&child->db_mtx); 229fa9e4066Sahrens 230ea8dc4b6Seschrock dbuf_rele(child, FTAG); 231fa9e4066Sahrens } 232744947dcSTom Erickson DB_DNODE_EXIT(db); 233fa9e4066Sahrens } 2349c9dc39aSek #endif 235fa9e4066Sahrens 23643466aaeSMax Grossman static void 23743466aaeSMax Grossman free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, 238fa9e4066Sahrens dmu_tx_t *tx) 239fa9e4066Sahrens { 240744947dcSTom Erickson dnode_t *dn; 241fa9e4066Sahrens blkptr_t *bp; 242fa9e4066Sahrens dmu_buf_impl_t *subdb; 243fa9e4066Sahrens uint64_t start, end, dbstart, dbend, i; 24443466aaeSMax Grossman int epbs, shift; 245cdb0ab79Smaybee 246cdb0ab79Smaybee /* 247cdb0ab79Smaybee * There is a small possibility that this block will not be cached: 248cdb0ab79Smaybee * 1 - if level > 1 and there are no children with level <= 1 24943466aaeSMax Grossman * 2 - if this block was evicted since we read it from 25043466aaeSMax Grossman * dmu_tx_hold_free(). 251cdb0ab79Smaybee */ 252cdb0ab79Smaybee if (db->db_state != DB_CACHED) 253cdb0ab79Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 254fa9e4066Sahrens 2553f9d6ad7SLin Ling dbuf_release_bp(db); 25643466aaeSMax Grossman bp = db->db.db_data; 257fa9e4066Sahrens 258744947dcSTom Erickson DB_DNODE_ENTER(db); 259744947dcSTom Erickson dn = DB_DNODE(db); 260744947dcSTom Erickson epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 261fa9e4066Sahrens shift = (db->db_level - 1) * epbs; 262fa9e4066Sahrens dbstart = db->db_blkid << epbs; 263fa9e4066Sahrens start = blkid >> shift; 264fa9e4066Sahrens if (dbstart < start) { 265fa9e4066Sahrens bp += start - dbstart; 266fa9e4066Sahrens } else { 267fa9e4066Sahrens start = dbstart; 268fa9e4066Sahrens } 269fa9e4066Sahrens dbend = ((db->db_blkid + 1) << epbs) - 1; 270fa9e4066Sahrens end = (blkid + nblks - 1) >> shift; 271fa9e4066Sahrens if (dbend <= end) 272fa9e4066Sahrens end = dbend; 27343466aaeSMax Grossman 274fa9e4066Sahrens ASSERT3U(start, <=, end); 275fa9e4066Sahrens 276fa9e4066Sahrens if (db->db_level == 1) { 2779c9dc39aSek FREE_VERIFY(db, start, end, tx); 27843466aaeSMax Grossman free_blocks(dn, bp, end-start+1, tx); 27943466aaeSMax Grossman } else { 28043466aaeSMax Grossman for (i = start; i <= end; i++, bp++) { 28143466aaeSMax Grossman if (BP_IS_HOLE(bp)) 28243466aaeSMax Grossman continue; 28343466aaeSMax Grossman rw_enter(&dn->dn_struct_rwlock, RW_READER); 28443466aaeSMax Grossman VERIFY0(dbuf_hold_impl(dn, db->db_level - 1, 28543466aaeSMax Grossman i, B_TRUE, FTAG, &subdb)); 28643466aaeSMax Grossman rw_exit(&dn->dn_struct_rwlock); 28743466aaeSMax Grossman ASSERT3P(bp, ==, subdb->db_blkptr); 28843466aaeSMax Grossman 28943466aaeSMax Grossman free_children(subdb, blkid, nblks, tx); 29043466aaeSMax Grossman dbuf_rele(subdb, FTAG); 29143466aaeSMax Grossman } 292fa9e4066Sahrens } 293fa9e4066Sahrens 29443466aaeSMax Grossman /* If this whole block is free, free ourself too. */ 29543466aaeSMax Grossman for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) { 29643466aaeSMax Grossman if (!BP_IS_HOLE(bp)) 29743466aaeSMax Grossman break; 29843466aaeSMax Grossman } 29943466aaeSMax Grossman if (i == 1 << epbs) { 30043466aaeSMax Grossman /* didn't find any non-holes */ 30143466aaeSMax Grossman bzero(db->db.db_data, db->db.db_size); 30243466aaeSMax Grossman free_blocks(dn, db->db_blkptr, 1, tx); 30343466aaeSMax Grossman } else { 30443466aaeSMax Grossman /* 30543466aaeSMax Grossman * Partial block free; must be marked dirty so that it 30643466aaeSMax Grossman * will be written out. 30743466aaeSMax Grossman */ 30843466aaeSMax Grossman ASSERT(db->db_dirtycnt > 0); 309fa9e4066Sahrens } 31043466aaeSMax Grossman 311744947dcSTom Erickson DB_DNODE_EXIT(db); 3126b4acc8bSahrens arc_buf_freeze(db->db_buf); 313fa9e4066Sahrens } 314fa9e4066Sahrens 315fa9e4066Sahrens /* 316f7170741SWill Andrews * Traverse the indicated range of the provided file 317fa9e4066Sahrens * and "free" all the blocks contained there. 318fa9e4066Sahrens */ 319fa9e4066Sahrens static void 320bf16b11eSMatthew Ahrens dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks, 32143466aaeSMax Grossman dmu_tx_t *tx) 322fa9e4066Sahrens { 323fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 324fa9e4066Sahrens int dnlevel = dn->dn_phys->dn_nlevels; 32543466aaeSMax Grossman boolean_t trunc = B_FALSE; 326fa9e4066Sahrens 327fa9e4066Sahrens if (blkid > dn->dn_phys->dn_maxblkid) 328fa9e4066Sahrens return; 329fa9e4066Sahrens 330fa9e4066Sahrens ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 33143466aaeSMax Grossman if (blkid + nblks > dn->dn_phys->dn_maxblkid) { 332fa9e4066Sahrens nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 33343466aaeSMax Grossman trunc = B_TRUE; 33443466aaeSMax Grossman } 335fa9e4066Sahrens 336fa9e4066Sahrens /* There are no indirect blocks in the object */ 337fa9e4066Sahrens if (dnlevel == 1) { 338fa9e4066Sahrens if (blkid >= dn->dn_phys->dn_nblkptr) { 339fa9e4066Sahrens /* this range was never made persistent */ 340fa9e4066Sahrens return; 341fa9e4066Sahrens } 342fa9e4066Sahrens ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 34343466aaeSMax Grossman free_blocks(dn, bp + blkid, nblks, tx); 34443466aaeSMax Grossman } else { 34543466aaeSMax Grossman int shift = (dnlevel - 1) * 34643466aaeSMax Grossman (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 34743466aaeSMax Grossman int start = blkid >> shift; 34843466aaeSMax Grossman int end = (blkid + nblks - 1) >> shift; 34943466aaeSMax Grossman dmu_buf_impl_t *db; 35043466aaeSMax Grossman 35143466aaeSMax Grossman ASSERT(start < dn->dn_phys->dn_nblkptr); 35243466aaeSMax Grossman bp += start; 35343466aaeSMax Grossman for (int i = start; i <= end; i++, bp++) { 35443466aaeSMax Grossman if (BP_IS_HOLE(bp)) 35543466aaeSMax Grossman continue; 35643466aaeSMax Grossman rw_enter(&dn->dn_struct_rwlock, RW_READER); 35743466aaeSMax Grossman VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i, 35843466aaeSMax Grossman TRUE, FTAG, &db)); 35943466aaeSMax Grossman rw_exit(&dn->dn_struct_rwlock); 36043466aaeSMax Grossman 36143466aaeSMax Grossman free_children(db, blkid, nblks, tx); 36243466aaeSMax Grossman dbuf_rele(db, FTAG); 363fa9e4066Sahrens } 364fa9e4066Sahrens } 36543466aaeSMax Grossman 366fa9e4066Sahrens if (trunc) { 36743466aaeSMax Grossman dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1; 36843466aaeSMax Grossman 369fa9e4066Sahrens uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 370fa9e4066Sahrens (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 371fa9e4066Sahrens ASSERT(off < dn->dn_phys->dn_maxblkid || 372fa9e4066Sahrens dn->dn_phys->dn_maxblkid == 0 || 373cdb0ab79Smaybee dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); 374fa9e4066Sahrens } 375fa9e4066Sahrens } 376fa9e4066Sahrens 377bf16b11eSMatthew Ahrens typedef struct dnode_sync_free_range_arg { 378bf16b11eSMatthew Ahrens dnode_t *dsfra_dnode; 379bf16b11eSMatthew Ahrens dmu_tx_t *dsfra_tx; 380bf16b11eSMatthew Ahrens } dnode_sync_free_range_arg_t; 381bf16b11eSMatthew Ahrens 382bf16b11eSMatthew Ahrens static void 383bf16b11eSMatthew Ahrens dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks) 384bf16b11eSMatthew Ahrens { 385bf16b11eSMatthew Ahrens dnode_sync_free_range_arg_t *dsfra = arg; 386bf16b11eSMatthew Ahrens dnode_t *dn = dsfra->dsfra_dnode; 387bf16b11eSMatthew Ahrens 388bf16b11eSMatthew Ahrens mutex_exit(&dn->dn_mtx); 389bf16b11eSMatthew Ahrens dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx); 390bf16b11eSMatthew Ahrens mutex_enter(&dn->dn_mtx); 391bf16b11eSMatthew Ahrens } 392bf16b11eSMatthew Ahrens 393ea8dc4b6Seschrock /* 394f7170741SWill Andrews * Try to kick all the dnode's dbufs out of the cache... 395ea8dc4b6Seschrock */ 3961934e92fSmaybee void 3971934e92fSmaybee dnode_evict_dbufs(dnode_t *dn) 398ea8dc4b6Seschrock { 399c543ec06Sahrens int progress; 400c543ec06Sahrens int pass = 0; 401c543ec06Sahrens 402c543ec06Sahrens do { 403d90a49d6Smaybee dmu_buf_impl_t *db, marker; 404c543ec06Sahrens int evicting = FALSE; 405c543ec06Sahrens 406c543ec06Sahrens progress = FALSE; 407c543ec06Sahrens mutex_enter(&dn->dn_dbufs_mtx); 408d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, &marker); 409d90a49d6Smaybee db = list_head(&dn->dn_dbufs); 410d90a49d6Smaybee for (; db != ▮ db = list_head(&dn->dn_dbufs)) { 411d90a49d6Smaybee list_remove(&dn->dn_dbufs, db); 412d90a49d6Smaybee list_insert_tail(&dn->dn_dbufs, db); 413744947dcSTom Erickson #ifdef DEBUG 414744947dcSTom Erickson DB_DNODE_ENTER(db); 415744947dcSTom Erickson ASSERT3P(DB_DNODE(db), ==, dn); 416744947dcSTom Erickson DB_DNODE_EXIT(db); 417744947dcSTom Erickson #endif /* DEBUG */ 418ea8dc4b6Seschrock 419ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 420c543ec06Sahrens if (db->db_state == DB_EVICTING) { 421c543ec06Sahrens progress = TRUE; 422c543ec06Sahrens evicting = TRUE; 423c543ec06Sahrens mutex_exit(&db->db_mtx); 424c543ec06Sahrens } else if (refcount_is_zero(&db->db_holds)) { 425c543ec06Sahrens progress = TRUE; 426c543ec06Sahrens dbuf_clear(db); /* exits db_mtx for us */ 427c543ec06Sahrens } else { 428c543ec06Sahrens mutex_exit(&db->db_mtx); 429c543ec06Sahrens } 430c543ec06Sahrens 431ea8dc4b6Seschrock } 432d90a49d6Smaybee list_remove(&dn->dn_dbufs, &marker); 433c543ec06Sahrens /* 434c543ec06Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 435c543ec06Sahrens * that any DB_EVICTING dbufs can make progress. 436c543ec06Sahrens * Ideally, we would have some cv we could wait on, but 437c543ec06Sahrens * since we don't, just wait a bit to give the other 438c543ec06Sahrens * thread a chance to run. 439c543ec06Sahrens */ 440c543ec06Sahrens mutex_exit(&dn->dn_dbufs_mtx); 441c543ec06Sahrens if (evicting) 442c543ec06Sahrens delay(1); 443c543ec06Sahrens pass++; 444c543ec06Sahrens ASSERT(pass < 100); /* sanity check */ 445c543ec06Sahrens } while (progress); 446c543ec06Sahrens 447ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 448ea8dc4b6Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 449ea8dc4b6Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 450ea8dc4b6Seschrock dbuf_evict(dn->dn_bonus); 451ea8dc4b6Seschrock dn->dn_bonus = NULL; 452ea8dc4b6Seschrock } 453ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 454ea8dc4b6Seschrock } 455ea8dc4b6Seschrock 456c717a561Smaybee static void 457c717a561Smaybee dnode_undirty_dbufs(list_t *list) 458fa9e4066Sahrens { 459c717a561Smaybee dbuf_dirty_record_t *dr; 460fa9e4066Sahrens 461c717a561Smaybee while (dr = list_head(list)) { 462c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 463c717a561Smaybee uint64_t txg = dr->dr_txg; 464fa9e4066Sahrens 465b24ab676SJeff Bonwick if (db->db_level != 0) 466b24ab676SJeff Bonwick dnode_undirty_dbufs(&dr->dt.di.dr_children); 467b24ab676SJeff Bonwick 468fa9e4066Sahrens mutex_enter(&db->db_mtx); 469fa9e4066Sahrens /* XXX - use dbuf_undirty()? */ 470c717a561Smaybee list_remove(list, dr); 471c717a561Smaybee ASSERT(db->db_last_dirty == dr); 472c717a561Smaybee db->db_last_dirty = NULL; 473c717a561Smaybee db->db_dirtycnt -= 1; 474fa9e4066Sahrens if (db->db_level == 0) { 4750a586ceaSMark Shellenbaum ASSERT(db->db_blkid == DMU_BONUS_BLKID || 476c717a561Smaybee dr->dt.dl.dr_data == db->db_buf); 477c717a561Smaybee dbuf_unoverride(dr); 478*d2b3cbbdSJorgen Lundman } else { 479*d2b3cbbdSJorgen Lundman mutex_destroy(&dr->dt.di.dr_mtx); 480*d2b3cbbdSJorgen Lundman list_destroy(&dr->dt.di.dr_children); 481fa9e4066Sahrens } 482c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 483b24ab676SJeff Bonwick dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); 484fa9e4066Sahrens } 485c717a561Smaybee } 486fa9e4066Sahrens 487c717a561Smaybee static void 488c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 489c717a561Smaybee { 490c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 491c717a561Smaybee 492c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx)); 493c717a561Smaybee 494cdb0ab79Smaybee /* 495cdb0ab79Smaybee * Our contents should have been freed in dnode_sync() by the 496cdb0ab79Smaybee * free range record inserted by the caller of dnode_free(). 497cdb0ab79Smaybee */ 498fb09f5aaSMadhav Suresh ASSERT0(DN_USED_BYTES(dn->dn_phys)); 499cdb0ab79Smaybee ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); 500cdb0ab79Smaybee 501c717a561Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 5021934e92fSmaybee dnode_evict_dbufs(dn); 503ea8dc4b6Seschrock ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 5043b2aab18SMatthew Ahrens ASSERT3P(dn->dn_bonus, ==, NULL); 505ea8dc4b6Seschrock 506ea8dc4b6Seschrock /* 507ea8dc4b6Seschrock * XXX - It would be nice to assert this, but we may still 508ea8dc4b6Seschrock * have residual holds from async evictions from the arc... 509ea8dc4b6Seschrock * 51055434c77Sek * zfs_obj_to_path() also depends on this being 51155434c77Sek * commented out. 51255434c77Sek * 513ea8dc4b6Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 514ea8dc4b6Seschrock */ 515fa9e4066Sahrens 516fa9e4066Sahrens /* Undirty next bits */ 517fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 518fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 519c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 520fa9e4066Sahrens 521fa9e4066Sahrens /* ASSERT(blkptrs are zero); */ 522fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 523fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 524fa9e4066Sahrens 525fa9e4066Sahrens ASSERT(dn->dn_free_txg > 0); 526fa9e4066Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 52743466aaeSMax Grossman dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); 528fa9e4066Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 529fa9e4066Sahrens 530fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 531fa9e4066Sahrens dn->dn_type = DMU_OT_NONE; 532fa9e4066Sahrens dn->dn_maxblkid = 0; 533fa9e4066Sahrens dn->dn_allocated_txg = 0; 534758f6e0bSgw dn->dn_free_txg = 0; 5350a586ceaSMark Shellenbaum dn->dn_have_spill = B_FALSE; 536fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 537fa9e4066Sahrens 538ea8dc4b6Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 539fa9e4066Sahrens 540fa9e4066Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 541fa9e4066Sahrens /* 542fa9e4066Sahrens * Now that we've released our hold, the dnode may 543fa9e4066Sahrens * be evicted, so we musn't access it. 544fa9e4066Sahrens */ 545fa9e4066Sahrens } 546fa9e4066Sahrens 547fa9e4066Sahrens /* 548c717a561Smaybee * Write out the dnode's dirty buffers. 549fa9e4066Sahrens */ 550c717a561Smaybee void 551c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 552fa9e4066Sahrens { 553fa9e4066Sahrens dnode_phys_t *dnp = dn->dn_phys; 554c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 555c717a561Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 55614843421SMatthew Ahrens static const dnode_phys_t zerodn = { 0 }; 5570a586ceaSMark Shellenbaum boolean_t kill_spill = B_FALSE; 558fa9e4066Sahrens 559fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 560fa9e4066Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 56114843421SMatthew Ahrens ASSERT(dnp->dn_type != DMU_OT_NONE || 56214843421SMatthew Ahrens bcmp(dnp, &zerodn, DNODE_SIZE) == 0); 5639c9dc39aSek DNODE_VERIFY(dn); 564c543ec06Sahrens 565c717a561Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 566fa9e4066Sahrens 56714843421SMatthew Ahrens if (dmu_objset_userused_enabled(dn->dn_objset) && 56814843421SMatthew Ahrens !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 5690a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 5700a586ceaSMark Shellenbaum dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); 5710a586ceaSMark Shellenbaum dn->dn_oldflags = dn->dn_phys->dn_flags; 57214843421SMatthew Ahrens dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; 5730a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 57406e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dn, B_FALSE, tx); 57514843421SMatthew Ahrens } else { 57614843421SMatthew Ahrens /* Once we account for it, we should always account for it. */ 57714843421SMatthew Ahrens ASSERT(!(dn->dn_phys->dn_flags & 57814843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED)); 57914843421SMatthew Ahrens } 58014843421SMatthew Ahrens 581fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 582fa9e4066Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 583fa9e4066Sahrens /* The dnode is newly allocated or reallocated */ 584fa9e4066Sahrens if (dnp->dn_type == DMU_OT_NONE) { 585fa9e4066Sahrens /* this is a first alloc, not a realloc */ 586fa9e4066Sahrens dnp->dn_nlevels = 1; 587da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_nblkptr; 588fa9e4066Sahrens } 589fa9e4066Sahrens 590fa9e4066Sahrens dnp->dn_type = dn->dn_type; 591fa9e4066Sahrens dnp->dn_bonustype = dn->dn_bonustype; 592fa9e4066Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 593fa9e4066Sahrens } 594c717a561Smaybee ASSERT(dnp->dn_nlevels > 1 || 595f676ed34Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5965d7b4d43SMatthew Ahrens BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || 597f676ed34Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 598f676ed34Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5995d7b4d43SMatthew Ahrens ASSERT(dnp->dn_nlevels < 2 || 6005d7b4d43SMatthew Ahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 6015d7b4d43SMatthew Ahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); 602f676ed34Sahrens 6032acef22dSMatthew Ahrens if (dn->dn_next_type[txgoff] != 0) { 6042acef22dSMatthew Ahrens dnp->dn_type = dn->dn_type; 6052acef22dSMatthew Ahrens dn->dn_next_type[txgoff] = 0; 6062acef22dSMatthew Ahrens } 6072acef22dSMatthew Ahrens 6082acef22dSMatthew Ahrens if (dn->dn_next_blksz[txgoff] != 0) { 609c543ec06Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 610fa9e4066Sahrens SPA_MINBLOCKSIZE) == 0); 611f676ed34Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 612cdb0ab79Smaybee dn->dn_maxblkid == 0 || list_head(list) != NULL || 613347a31bcSahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 614bf16b11eSMatthew Ahrens dnp->dn_datablkszsec || 615bf16b11eSMatthew Ahrens range_tree_space(dn->dn_free_ranges[txgoff]) != 0); 616fa9e4066Sahrens dnp->dn_datablkszsec = 617c543ec06Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 618c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 619fa9e4066Sahrens } 620fa9e4066Sahrens 6212acef22dSMatthew Ahrens if (dn->dn_next_bonuslen[txgoff] != 0) { 6221934e92fSmaybee if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) 6231934e92fSmaybee dnp->dn_bonuslen = 0; 6241934e92fSmaybee else 6251934e92fSmaybee dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; 6261934e92fSmaybee ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); 6271934e92fSmaybee dn->dn_next_bonuslen[txgoff] = 0; 6281934e92fSmaybee } 6291934e92fSmaybee 6302acef22dSMatthew Ahrens if (dn->dn_next_bonustype[txgoff] != 0) { 631ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); 6320a586ceaSMark Shellenbaum dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; 6330a586ceaSMark Shellenbaum dn->dn_next_bonustype[txgoff] = 0; 6340a586ceaSMark Shellenbaum } 6350a586ceaSMark Shellenbaum 63643466aaeSMax Grossman boolean_t freeing_dnode = dn->dn_free_txg > 0 && 63743466aaeSMax Grossman dn->dn_free_txg <= tx->tx_txg; 63843466aaeSMax Grossman 6390a586ceaSMark Shellenbaum /* 6400a586ceaSMark Shellenbaum * We will either remove a spill block when a file is being removed 6410a586ceaSMark Shellenbaum * or we have been asked to remove it. 6420a586ceaSMark Shellenbaum */ 6430a586ceaSMark Shellenbaum if (dn->dn_rm_spillblk[txgoff] || 64443466aaeSMax Grossman ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) { 6450a586ceaSMark Shellenbaum if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 6460a586ceaSMark Shellenbaum kill_spill = B_TRUE; 6470a586ceaSMark Shellenbaum dn->dn_rm_spillblk[txgoff] = 0; 6480a586ceaSMark Shellenbaum } 6490a586ceaSMark Shellenbaum 6502acef22dSMatthew Ahrens if (dn->dn_next_indblkshift[txgoff] != 0) { 651fa9e4066Sahrens ASSERT(dnp->dn_nlevels == 1); 652fa9e4066Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 653fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 654fa9e4066Sahrens } 655fa9e4066Sahrens 656fa9e4066Sahrens /* 657fa9e4066Sahrens * Just take the live (open-context) values for checksum and compress. 658fa9e4066Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 659fa9e4066Sahrens * start using the new checksum or compress algorithm a little early. 660fa9e4066Sahrens */ 661fa9e4066Sahrens dnp->dn_checksum = dn->dn_checksum; 662fa9e4066Sahrens dnp->dn_compress = dn->dn_compress; 663fa9e4066Sahrens 664fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 665fa9e4066Sahrens 6660a586ceaSMark Shellenbaum if (kill_spill) { 66743466aaeSMax Grossman free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx); 6680a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 6690a586ceaSMark Shellenbaum dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; 6700a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 6710a586ceaSMark Shellenbaum } 6720a586ceaSMark Shellenbaum 673fa9e4066Sahrens /* process all the "freed" ranges in the file */ 674bf16b11eSMatthew Ahrens if (dn->dn_free_ranges[txgoff] != NULL) { 675bf16b11eSMatthew Ahrens dnode_sync_free_range_arg_t dsfra; 676bf16b11eSMatthew Ahrens dsfra.dsfra_dnode = dn; 677bf16b11eSMatthew Ahrens dsfra.dsfra_tx = tx; 678cdb0ab79Smaybee mutex_enter(&dn->dn_mtx); 679bf16b11eSMatthew Ahrens range_tree_vacate(dn->dn_free_ranges[txgoff], 680bf16b11eSMatthew Ahrens dnode_sync_free_range, &dsfra); 681bf16b11eSMatthew Ahrens range_tree_destroy(dn->dn_free_ranges[txgoff]); 682bf16b11eSMatthew Ahrens dn->dn_free_ranges[txgoff] = NULL; 683cdb0ab79Smaybee mutex_exit(&dn->dn_mtx); 684fa9e4066Sahrens } 6851934e92fSmaybee 68643466aaeSMax Grossman if (freeing_dnode) { 687c717a561Smaybee dnode_sync_free(dn, tx); 688c717a561Smaybee return; 689fa9e4066Sahrens } 690fa9e4066Sahrens 691da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff]) { 692da03de99SMark Maybee /* this should only happen on a realloc */ 693da03de99SMark Maybee ASSERT(dn->dn_allocated_txg == tx->tx_txg); 694da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { 695da03de99SMark Maybee /* zero the new blkptrs we are gaining */ 696da03de99SMark Maybee bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 697da03de99SMark Maybee sizeof (blkptr_t) * 698da03de99SMark Maybee (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); 699da03de99SMark Maybee #ifdef ZFS_DEBUG 700da03de99SMark Maybee } else { 701da03de99SMark Maybee int i; 702da03de99SMark Maybee ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); 703da03de99SMark Maybee /* the blkptrs we are losing better be unallocated */ 704da03de99SMark Maybee for (i = dn->dn_next_nblkptr[txgoff]; 705da03de99SMark Maybee i < dnp->dn_nblkptr; i++) 706da03de99SMark Maybee ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); 707da03de99SMark Maybee #endif 708da03de99SMark Maybee } 709da03de99SMark Maybee mutex_enter(&dn->dn_mtx); 710da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; 711da03de99SMark Maybee dn->dn_next_nblkptr[txgoff] = 0; 712da03de99SMark Maybee mutex_exit(&dn->dn_mtx); 713da03de99SMark Maybee } 714da03de99SMark Maybee 715fa9e4066Sahrens if (dn->dn_next_nlevels[txgoff]) { 716c717a561Smaybee dnode_increase_indirection(dn, tx); 717fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 718fa9e4066Sahrens } 719fa9e4066Sahrens 720c717a561Smaybee dbuf_sync_list(list, tx); 721fa9e4066Sahrens 72214843421SMatthew Ahrens if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 723c717a561Smaybee ASSERT3P(list_head(list), ==, NULL); 724c717a561Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 725fa9e4066Sahrens } 726c717a561Smaybee 727c717a561Smaybee /* 728c717a561Smaybee * Although we have dropped our reference to the dnode, it 729c717a561Smaybee * can't be evicted until its written, and we haven't yet 730c717a561Smaybee * initiated the IO for the dnode's dbuf. 731c717a561Smaybee */ 732fa9e4066Sahrens } 733