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 { 4030f6d88adSAlex Reece dmu_buf_impl_t *db, *db_next; 404c543ec06Sahrens int evicting = FALSE; 405c543ec06Sahrens 406c543ec06Sahrens progress = FALSE; 407c543ec06Sahrens mutex_enter(&dn->dn_dbufs_mtx); 4080f6d88adSAlex Reece for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) { 4090f6d88adSAlex Reece db_next = AVL_NEXT(&dn->dn_dbufs, db); 410744947dcSTom Erickson #ifdef DEBUG 411744947dcSTom Erickson DB_DNODE_ENTER(db); 412744947dcSTom Erickson ASSERT3P(DB_DNODE(db), ==, dn); 413744947dcSTom Erickson DB_DNODE_EXIT(db); 414744947dcSTom Erickson #endif /* DEBUG */ 415ea8dc4b6Seschrock 416ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 417c543ec06Sahrens if (db->db_state == DB_EVICTING) { 418c543ec06Sahrens progress = TRUE; 419c543ec06Sahrens evicting = TRUE; 420c543ec06Sahrens mutex_exit(&db->db_mtx); 421c543ec06Sahrens } else if (refcount_is_zero(&db->db_holds)) { 422c543ec06Sahrens progress = TRUE; 423c543ec06Sahrens dbuf_clear(db); /* exits db_mtx for us */ 424c543ec06Sahrens } else { 425c543ec06Sahrens mutex_exit(&db->db_mtx); 426c543ec06Sahrens } 427c543ec06Sahrens 428ea8dc4b6Seschrock } 429c543ec06Sahrens /* 430c543ec06Sahrens * NB: we need to drop dn_dbufs_mtx between passes so 431c543ec06Sahrens * that any DB_EVICTING dbufs can make progress. 432c543ec06Sahrens * Ideally, we would have some cv we could wait on, but 433c543ec06Sahrens * since we don't, just wait a bit to give the other 434c543ec06Sahrens * thread a chance to run. 435c543ec06Sahrens */ 436c543ec06Sahrens mutex_exit(&dn->dn_dbufs_mtx); 437c543ec06Sahrens if (evicting) 438c543ec06Sahrens delay(1); 439c543ec06Sahrens pass++; 440c543ec06Sahrens ASSERT(pass < 100); /* sanity check */ 441c543ec06Sahrens } while (progress); 442c543ec06Sahrens 443ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 444ea8dc4b6Seschrock if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { 445ea8dc4b6Seschrock mutex_enter(&dn->dn_bonus->db_mtx); 446ea8dc4b6Seschrock dbuf_evict(dn->dn_bonus); 447ea8dc4b6Seschrock dn->dn_bonus = NULL; 448ea8dc4b6Seschrock } 449ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 450ea8dc4b6Seschrock } 451ea8dc4b6Seschrock 452c717a561Smaybee static void 453c717a561Smaybee dnode_undirty_dbufs(list_t *list) 454fa9e4066Sahrens { 455c717a561Smaybee dbuf_dirty_record_t *dr; 456fa9e4066Sahrens 457c717a561Smaybee while (dr = list_head(list)) { 458c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 459c717a561Smaybee uint64_t txg = dr->dr_txg; 460fa9e4066Sahrens 461b24ab676SJeff Bonwick if (db->db_level != 0) 462b24ab676SJeff Bonwick dnode_undirty_dbufs(&dr->dt.di.dr_children); 463b24ab676SJeff Bonwick 464fa9e4066Sahrens mutex_enter(&db->db_mtx); 465fa9e4066Sahrens /* XXX - use dbuf_undirty()? */ 466c717a561Smaybee list_remove(list, dr); 467c717a561Smaybee ASSERT(db->db_last_dirty == dr); 468c717a561Smaybee db->db_last_dirty = NULL; 469c717a561Smaybee db->db_dirtycnt -= 1; 470fa9e4066Sahrens if (db->db_level == 0) { 4710a586ceaSMark Shellenbaum ASSERT(db->db_blkid == DMU_BONUS_BLKID || 472c717a561Smaybee dr->dt.dl.dr_data == db->db_buf); 473c717a561Smaybee dbuf_unoverride(dr); 474d2b3cbbdSJorgen Lundman } else { 475d2b3cbbdSJorgen Lundman mutex_destroy(&dr->dt.di.dr_mtx); 476d2b3cbbdSJorgen Lundman list_destroy(&dr->dt.di.dr_children); 477fa9e4066Sahrens } 478c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 479b24ab676SJeff Bonwick dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); 480fa9e4066Sahrens } 481c717a561Smaybee } 482fa9e4066Sahrens 483c717a561Smaybee static void 484c717a561Smaybee dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 485c717a561Smaybee { 486c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 487c717a561Smaybee 488c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx)); 489c717a561Smaybee 490cdb0ab79Smaybee /* 491cdb0ab79Smaybee * Our contents should have been freed in dnode_sync() by the 492cdb0ab79Smaybee * free range record inserted by the caller of dnode_free(). 493cdb0ab79Smaybee */ 494fb09f5aaSMadhav Suresh ASSERT0(DN_USED_BYTES(dn->dn_phys)); 495cdb0ab79Smaybee ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); 496cdb0ab79Smaybee 497c717a561Smaybee dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 4981934e92fSmaybee dnode_evict_dbufs(dn); 4990f6d88adSAlex Reece ASSERT(avl_is_empty(&dn->dn_dbufs)); 5003b2aab18SMatthew Ahrens ASSERT3P(dn->dn_bonus, ==, NULL); 501ea8dc4b6Seschrock 502ea8dc4b6Seschrock /* 503ea8dc4b6Seschrock * XXX - It would be nice to assert this, but we may still 504ea8dc4b6Seschrock * have residual holds from async evictions from the arc... 505ea8dc4b6Seschrock * 50655434c77Sek * zfs_obj_to_path() also depends on this being 50755434c77Sek * commented out. 50855434c77Sek * 509ea8dc4b6Seschrock * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 510ea8dc4b6Seschrock */ 511fa9e4066Sahrens 512fa9e4066Sahrens /* Undirty next bits */ 513fa9e4066Sahrens dn->dn_next_nlevels[txgoff] = 0; 514fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 515c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 516fa9e4066Sahrens 517fa9e4066Sahrens /* ASSERT(blkptrs are zero); */ 518fa9e4066Sahrens ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 519fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 520fa9e4066Sahrens 521fa9e4066Sahrens ASSERT(dn->dn_free_txg > 0); 522fa9e4066Sahrens if (dn->dn_allocated_txg != dn->dn_free_txg) 52343466aaeSMax Grossman dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); 524fa9e4066Sahrens bzero(dn->dn_phys, sizeof (dnode_phys_t)); 525fa9e4066Sahrens 526fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 527fa9e4066Sahrens dn->dn_type = DMU_OT_NONE; 528fa9e4066Sahrens dn->dn_maxblkid = 0; 529fa9e4066Sahrens dn->dn_allocated_txg = 0; 530758f6e0bSgw dn->dn_free_txg = 0; 5310a586ceaSMark Shellenbaum dn->dn_have_spill = B_FALSE; 532fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 533fa9e4066Sahrens 534ea8dc4b6Seschrock ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 535fa9e4066Sahrens 536fa9e4066Sahrens dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 537fa9e4066Sahrens /* 538fa9e4066Sahrens * Now that we've released our hold, the dnode may 539fa9e4066Sahrens * be evicted, so we musn't access it. 540fa9e4066Sahrens */ 541fa9e4066Sahrens } 542fa9e4066Sahrens 543fa9e4066Sahrens /* 544c717a561Smaybee * Write out the dnode's dirty buffers. 545fa9e4066Sahrens */ 546c717a561Smaybee void 547c717a561Smaybee dnode_sync(dnode_t *dn, dmu_tx_t *tx) 548fa9e4066Sahrens { 549fa9e4066Sahrens dnode_phys_t *dnp = dn->dn_phys; 550c717a561Smaybee int txgoff = tx->tx_txg & TXG_MASK; 551c717a561Smaybee list_t *list = &dn->dn_dirty_records[txgoff]; 55214843421SMatthew Ahrens static const dnode_phys_t zerodn = { 0 }; 5530a586ceaSMark Shellenbaum boolean_t kill_spill = B_FALSE; 554fa9e4066Sahrens 555fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 556fa9e4066Sahrens ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 55714843421SMatthew Ahrens ASSERT(dnp->dn_type != DMU_OT_NONE || 55814843421SMatthew Ahrens bcmp(dnp, &zerodn, DNODE_SIZE) == 0); 5599c9dc39aSek DNODE_VERIFY(dn); 560c543ec06Sahrens 561c717a561Smaybee ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 562fa9e4066Sahrens 56314843421SMatthew Ahrens if (dmu_objset_userused_enabled(dn->dn_objset) && 56414843421SMatthew Ahrens !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 5650a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 5660a586ceaSMark Shellenbaum dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); 5670a586ceaSMark Shellenbaum dn->dn_oldflags = dn->dn_phys->dn_flags; 56814843421SMatthew Ahrens dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; 5690a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 57006e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dn, B_FALSE, tx); 57114843421SMatthew Ahrens } else { 57214843421SMatthew Ahrens /* Once we account for it, we should always account for it. */ 57314843421SMatthew Ahrens ASSERT(!(dn->dn_phys->dn_flags & 57414843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED)); 57514843421SMatthew Ahrens } 57614843421SMatthew Ahrens 577fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 578fa9e4066Sahrens if (dn->dn_allocated_txg == tx->tx_txg) { 579fa9e4066Sahrens /* The dnode is newly allocated or reallocated */ 580fa9e4066Sahrens if (dnp->dn_type == DMU_OT_NONE) { 581fa9e4066Sahrens /* this is a first alloc, not a realloc */ 582fa9e4066Sahrens dnp->dn_nlevels = 1; 583da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_nblkptr; 584fa9e4066Sahrens } 585fa9e4066Sahrens 586fa9e4066Sahrens dnp->dn_type = dn->dn_type; 587fa9e4066Sahrens dnp->dn_bonustype = dn->dn_bonustype; 588fa9e4066Sahrens dnp->dn_bonuslen = dn->dn_bonuslen; 589fa9e4066Sahrens } 590c717a561Smaybee ASSERT(dnp->dn_nlevels > 1 || 591f676ed34Sahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5925d7b4d43SMatthew Ahrens BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || 593f676ed34Sahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 594f676ed34Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 5955d7b4d43SMatthew Ahrens ASSERT(dnp->dn_nlevels < 2 || 5965d7b4d43SMatthew Ahrens BP_IS_HOLE(&dnp->dn_blkptr[0]) || 5975d7b4d43SMatthew Ahrens BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); 598f676ed34Sahrens 5992acef22dSMatthew Ahrens if (dn->dn_next_type[txgoff] != 0) { 6002acef22dSMatthew Ahrens dnp->dn_type = dn->dn_type; 6012acef22dSMatthew Ahrens dn->dn_next_type[txgoff] = 0; 6022acef22dSMatthew Ahrens } 6032acef22dSMatthew Ahrens 6042acef22dSMatthew Ahrens if (dn->dn_next_blksz[txgoff] != 0) { 605c543ec06Sahrens ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 606fa9e4066Sahrens SPA_MINBLOCKSIZE) == 0); 607f676ed34Sahrens ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 608cdb0ab79Smaybee dn->dn_maxblkid == 0 || list_head(list) != NULL || 609347a31bcSahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 610bf16b11eSMatthew Ahrens dnp->dn_datablkszsec || 611bf16b11eSMatthew Ahrens range_tree_space(dn->dn_free_ranges[txgoff]) != 0); 612fa9e4066Sahrens dnp->dn_datablkszsec = 613c543ec06Sahrens dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 614c543ec06Sahrens dn->dn_next_blksz[txgoff] = 0; 615fa9e4066Sahrens } 616fa9e4066Sahrens 6172acef22dSMatthew Ahrens if (dn->dn_next_bonuslen[txgoff] != 0) { 6181934e92fSmaybee if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) 6191934e92fSmaybee dnp->dn_bonuslen = 0; 6201934e92fSmaybee else 6211934e92fSmaybee dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; 6221934e92fSmaybee ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); 6231934e92fSmaybee dn->dn_next_bonuslen[txgoff] = 0; 6241934e92fSmaybee } 6251934e92fSmaybee 6262acef22dSMatthew Ahrens if (dn->dn_next_bonustype[txgoff] != 0) { 627ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); 6280a586ceaSMark Shellenbaum dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; 6290a586ceaSMark Shellenbaum dn->dn_next_bonustype[txgoff] = 0; 6300a586ceaSMark Shellenbaum } 6310a586ceaSMark Shellenbaum 63243466aaeSMax Grossman boolean_t freeing_dnode = dn->dn_free_txg > 0 && 63343466aaeSMax Grossman dn->dn_free_txg <= tx->tx_txg; 63443466aaeSMax Grossman 6350a586ceaSMark Shellenbaum /* 6360a586ceaSMark Shellenbaum * We will either remove a spill block when a file is being removed 6370a586ceaSMark Shellenbaum * or we have been asked to remove it. 6380a586ceaSMark Shellenbaum */ 6390a586ceaSMark Shellenbaum if (dn->dn_rm_spillblk[txgoff] || 64043466aaeSMax Grossman ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) { 6410a586ceaSMark Shellenbaum if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 6420a586ceaSMark Shellenbaum kill_spill = B_TRUE; 6430a586ceaSMark Shellenbaum dn->dn_rm_spillblk[txgoff] = 0; 6440a586ceaSMark Shellenbaum } 6450a586ceaSMark Shellenbaum 6462acef22dSMatthew Ahrens if (dn->dn_next_indblkshift[txgoff] != 0) { 647fa9e4066Sahrens ASSERT(dnp->dn_nlevels == 1); 648fa9e4066Sahrens dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 649fa9e4066Sahrens dn->dn_next_indblkshift[txgoff] = 0; 650fa9e4066Sahrens } 651fa9e4066Sahrens 652fa9e4066Sahrens /* 653fa9e4066Sahrens * Just take the live (open-context) values for checksum and compress. 654fa9e4066Sahrens * Strictly speaking it's a future leak, but nothing bad happens if we 655fa9e4066Sahrens * start using the new checksum or compress algorithm a little early. 656fa9e4066Sahrens */ 657fa9e4066Sahrens dnp->dn_checksum = dn->dn_checksum; 658fa9e4066Sahrens dnp->dn_compress = dn->dn_compress; 659fa9e4066Sahrens 660fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 661fa9e4066Sahrens 6620a586ceaSMark Shellenbaum if (kill_spill) { 66343466aaeSMax Grossman free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx); 6640a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 6650a586ceaSMark Shellenbaum dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; 6660a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 6670a586ceaSMark Shellenbaum } 6680a586ceaSMark Shellenbaum 669fa9e4066Sahrens /* process all the "freed" ranges in the file */ 670bf16b11eSMatthew Ahrens if (dn->dn_free_ranges[txgoff] != NULL) { 671bf16b11eSMatthew Ahrens dnode_sync_free_range_arg_t dsfra; 672bf16b11eSMatthew Ahrens dsfra.dsfra_dnode = dn; 673bf16b11eSMatthew Ahrens dsfra.dsfra_tx = tx; 674cdb0ab79Smaybee mutex_enter(&dn->dn_mtx); 675bf16b11eSMatthew Ahrens range_tree_vacate(dn->dn_free_ranges[txgoff], 676bf16b11eSMatthew Ahrens dnode_sync_free_range, &dsfra); 677bf16b11eSMatthew Ahrens range_tree_destroy(dn->dn_free_ranges[txgoff]); 678bf16b11eSMatthew Ahrens dn->dn_free_ranges[txgoff] = NULL; 679cdb0ab79Smaybee mutex_exit(&dn->dn_mtx); 680fa9e4066Sahrens } 6811934e92fSmaybee 68243466aaeSMax Grossman if (freeing_dnode) { 683c717a561Smaybee dnode_sync_free(dn, tx); 684c717a561Smaybee return; 685fa9e4066Sahrens } 686fa9e4066Sahrens 687*e503a685SGeorge Wilson if (dn->dn_next_nlevels[txgoff]) { 688*e503a685SGeorge Wilson dnode_increase_indirection(dn, tx); 689*e503a685SGeorge Wilson dn->dn_next_nlevels[txgoff] = 0; 690*e503a685SGeorge Wilson } 691*e503a685SGeorge Wilson 692da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff]) { 693da03de99SMark Maybee /* this should only happen on a realloc */ 694da03de99SMark Maybee ASSERT(dn->dn_allocated_txg == tx->tx_txg); 695da03de99SMark Maybee if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { 696da03de99SMark Maybee /* zero the new blkptrs we are gaining */ 697da03de99SMark Maybee bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 698da03de99SMark Maybee sizeof (blkptr_t) * 699da03de99SMark Maybee (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); 700da03de99SMark Maybee #ifdef ZFS_DEBUG 701da03de99SMark Maybee } else { 702da03de99SMark Maybee int i; 703da03de99SMark Maybee ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); 704da03de99SMark Maybee /* the blkptrs we are losing better be unallocated */ 705da03de99SMark Maybee for (i = dn->dn_next_nblkptr[txgoff]; 706da03de99SMark Maybee i < dnp->dn_nblkptr; i++) 707da03de99SMark Maybee ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); 708da03de99SMark Maybee #endif 709da03de99SMark Maybee } 710da03de99SMark Maybee mutex_enter(&dn->dn_mtx); 711da03de99SMark Maybee dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; 712da03de99SMark Maybee dn->dn_next_nblkptr[txgoff] = 0; 713da03de99SMark Maybee mutex_exit(&dn->dn_mtx); 714da03de99SMark Maybee } 715da03de99SMark Maybee 716c717a561Smaybee dbuf_sync_list(list, tx); 717fa9e4066Sahrens 71814843421SMatthew Ahrens if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 719c717a561Smaybee ASSERT3P(list_head(list), ==, NULL); 720c717a561Smaybee dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 721fa9e4066Sahrens } 722c717a561Smaybee 723c717a561Smaybee /* 724c717a561Smaybee * Although we have dropped our reference to the dnode, it 725c717a561Smaybee * can't be evicted until its written, and we haven't yet 726c717a561Smaybee * initiated the IO for the dnode's dbuf. 727c717a561Smaybee */ 728fa9e4066Sahrens } 729