1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy /* 23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 247877fdebSMatt Macy * Copyright (c) 2012, 2020 by Delphix. All rights reserved. 25eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26ac0bf12eSMatt Macy * Copyright 2020 Oxide Computer Company 27eda14cbcSMatt Macy */ 28eda14cbcSMatt Macy 29eda14cbcSMatt Macy #include <sys/zfs_context.h> 30eda14cbcSMatt Macy #include <sys/dbuf.h> 31eda14cbcSMatt Macy #include <sys/dnode.h> 32eda14cbcSMatt Macy #include <sys/dmu.h> 33eda14cbcSMatt Macy #include <sys/dmu_tx.h> 34eda14cbcSMatt Macy #include <sys/dmu_objset.h> 35eda14cbcSMatt Macy #include <sys/dmu_recv.h> 36eda14cbcSMatt Macy #include <sys/dsl_dataset.h> 37eda14cbcSMatt Macy #include <sys/spa.h> 38eda14cbcSMatt Macy #include <sys/range_tree.h> 39eda14cbcSMatt Macy #include <sys/zfeature.h> 40eda14cbcSMatt Macy 41eda14cbcSMatt Macy static void 42eda14cbcSMatt Macy dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 43eda14cbcSMatt Macy { 44eda14cbcSMatt Macy dmu_buf_impl_t *db; 45eda14cbcSMatt Macy int txgoff = tx->tx_txg & TXG_MASK; 46eda14cbcSMatt Macy int nblkptr = dn->dn_phys->dn_nblkptr; 47eda14cbcSMatt Macy int old_toplvl = dn->dn_phys->dn_nlevels - 1; 48eda14cbcSMatt Macy int new_level = dn->dn_next_nlevels[txgoff]; 49eda14cbcSMatt Macy int i; 50eda14cbcSMatt Macy 51eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 52eda14cbcSMatt Macy 53eda14cbcSMatt Macy /* this dnode can't be paged out because it's dirty */ 54eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 55eda14cbcSMatt Macy ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); 56eda14cbcSMatt Macy 57eda14cbcSMatt Macy db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 58eda14cbcSMatt Macy ASSERT(db != NULL); 59eda14cbcSMatt Macy 60eda14cbcSMatt Macy dn->dn_phys->dn_nlevels = new_level; 61eda14cbcSMatt Macy dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, 6233b8c039SMartin Matuska (u_longlong_t)dn->dn_object, dn->dn_phys->dn_nlevels); 63eda14cbcSMatt Macy 64eda14cbcSMatt Macy /* 65eda14cbcSMatt Macy * Lock ordering requires that we hold the children's db_mutexes (by 66eda14cbcSMatt Macy * calling dbuf_find()) before holding the parent's db_rwlock. The lock 67eda14cbcSMatt Macy * order is imposed by dbuf_read's steps of "grab the lock to protect 68eda14cbcSMatt Macy * db_parent, get db_parent, hold db_parent's db_rwlock". 69eda14cbcSMatt Macy */ 70eda14cbcSMatt Macy dmu_buf_impl_t *children[DN_MAX_NBLKPTR]; 71eda14cbcSMatt Macy ASSERT3U(nblkptr, <=, DN_MAX_NBLKPTR); 72eda14cbcSMatt Macy for (i = 0; i < nblkptr; i++) { 73eda14cbcSMatt Macy children[i] = 74eda14cbcSMatt Macy dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i); 75eda14cbcSMatt Macy } 76eda14cbcSMatt Macy 77eda14cbcSMatt Macy /* transfer dnode's block pointers to new indirect block */ 78eda14cbcSMatt Macy (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); 79eda14cbcSMatt Macy if (dn->dn_dbuf != NULL) 80eda14cbcSMatt Macy rw_enter(&dn->dn_dbuf->db_rwlock, RW_WRITER); 81eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 82eda14cbcSMatt Macy ASSERT(db->db.db_data); 83eda14cbcSMatt Macy ASSERT(arc_released(db->db_buf)); 84eda14cbcSMatt Macy ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); 85eda14cbcSMatt Macy bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 86eda14cbcSMatt Macy sizeof (blkptr_t) * nblkptr); 87eda14cbcSMatt Macy arc_buf_freeze(db->db_buf); 88eda14cbcSMatt Macy 89eda14cbcSMatt Macy /* set dbuf's parent pointers to new indirect buf */ 90eda14cbcSMatt Macy for (i = 0; i < nblkptr; i++) { 91eda14cbcSMatt Macy dmu_buf_impl_t *child = children[i]; 92eda14cbcSMatt Macy 93eda14cbcSMatt Macy if (child == NULL) 94eda14cbcSMatt Macy continue; 95eda14cbcSMatt Macy #ifdef ZFS_DEBUG 96eda14cbcSMatt Macy DB_DNODE_ENTER(child); 97eda14cbcSMatt Macy ASSERT3P(DB_DNODE(child), ==, dn); 98eda14cbcSMatt Macy DB_DNODE_EXIT(child); 99eda14cbcSMatt Macy #endif /* DEBUG */ 100eda14cbcSMatt Macy if (child->db_parent && child->db_parent != dn->dn_dbuf) { 101eda14cbcSMatt Macy ASSERT(child->db_parent->db_level == db->db_level); 102eda14cbcSMatt Macy ASSERT(child->db_blkptr != 103eda14cbcSMatt Macy &dn->dn_phys->dn_blkptr[child->db_blkid]); 104eda14cbcSMatt Macy mutex_exit(&child->db_mtx); 105eda14cbcSMatt Macy continue; 106eda14cbcSMatt Macy } 107eda14cbcSMatt Macy ASSERT(child->db_parent == NULL || 108eda14cbcSMatt Macy child->db_parent == dn->dn_dbuf); 109eda14cbcSMatt Macy 110eda14cbcSMatt Macy child->db_parent = db; 111eda14cbcSMatt Macy dbuf_add_ref(db, child); 112eda14cbcSMatt Macy if (db->db.db_data) 113eda14cbcSMatt Macy child->db_blkptr = (blkptr_t *)db->db.db_data + i; 114eda14cbcSMatt Macy else 115eda14cbcSMatt Macy child->db_blkptr = NULL; 116eda14cbcSMatt Macy dprintf_dbuf_bp(child, child->db_blkptr, 117eda14cbcSMatt Macy "changed db_blkptr to new indirect %s", ""); 118eda14cbcSMatt Macy 119eda14cbcSMatt Macy mutex_exit(&child->db_mtx); 120eda14cbcSMatt Macy } 121eda14cbcSMatt Macy 122eda14cbcSMatt Macy bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); 123eda14cbcSMatt Macy 124eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 125eda14cbcSMatt Macy if (dn->dn_dbuf != NULL) 126eda14cbcSMatt Macy rw_exit(&dn->dn_dbuf->db_rwlock); 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy dbuf_rele(db, FTAG); 129eda14cbcSMatt Macy 130eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 131eda14cbcSMatt Macy } 132eda14cbcSMatt Macy 133eda14cbcSMatt Macy static void 134eda14cbcSMatt Macy free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 135eda14cbcSMatt Macy { 136eda14cbcSMatt Macy dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 137eda14cbcSMatt Macy uint64_t bytesfreed = 0; 138eda14cbcSMatt Macy 13933b8c039SMartin Matuska dprintf("ds=%p obj=%llx num=%d\n", ds, (u_longlong_t)dn->dn_object, 14033b8c039SMartin Matuska num); 141eda14cbcSMatt Macy 142eda14cbcSMatt Macy for (int i = 0; i < num; i++, bp++) { 143eda14cbcSMatt Macy if (BP_IS_HOLE(bp)) 144eda14cbcSMatt Macy continue; 145eda14cbcSMatt Macy 146eda14cbcSMatt Macy bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); 147eda14cbcSMatt Macy ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); 148eda14cbcSMatt Macy 149eda14cbcSMatt Macy /* 150eda14cbcSMatt Macy * Save some useful information on the holes being 151eda14cbcSMatt Macy * punched, including logical size, type, and indirection 152eda14cbcSMatt Macy * level. Retaining birth time enables detection of when 153eda14cbcSMatt Macy * holes are punched for reducing the number of free 154eda14cbcSMatt Macy * records transmitted during a zfs send. 155eda14cbcSMatt Macy */ 156eda14cbcSMatt Macy 157eda14cbcSMatt Macy uint64_t lsize = BP_GET_LSIZE(bp); 158eda14cbcSMatt Macy dmu_object_type_t type = BP_GET_TYPE(bp); 159eda14cbcSMatt Macy uint64_t lvl = BP_GET_LEVEL(bp); 160eda14cbcSMatt Macy 161eda14cbcSMatt Macy bzero(bp, sizeof (blkptr_t)); 162eda14cbcSMatt Macy 163eda14cbcSMatt Macy if (spa_feature_is_active(dn->dn_objset->os_spa, 164eda14cbcSMatt Macy SPA_FEATURE_HOLE_BIRTH)) { 165eda14cbcSMatt Macy BP_SET_LSIZE(bp, lsize); 166eda14cbcSMatt Macy BP_SET_TYPE(bp, type); 167eda14cbcSMatt Macy BP_SET_LEVEL(bp, lvl); 168eda14cbcSMatt Macy BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0); 169eda14cbcSMatt Macy } 170eda14cbcSMatt Macy } 171eda14cbcSMatt Macy dnode_diduse_space(dn, -bytesfreed); 172eda14cbcSMatt Macy } 173eda14cbcSMatt Macy 174eda14cbcSMatt Macy #ifdef ZFS_DEBUG 175eda14cbcSMatt Macy static void 176eda14cbcSMatt Macy free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 177eda14cbcSMatt Macy { 178eda14cbcSMatt Macy int off, num; 179eda14cbcSMatt Macy int i, err, epbs; 180eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 181eda14cbcSMatt Macy dnode_t *dn; 182eda14cbcSMatt Macy 183eda14cbcSMatt Macy DB_DNODE_ENTER(db); 184eda14cbcSMatt Macy dn = DB_DNODE(db); 185eda14cbcSMatt Macy epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 186eda14cbcSMatt Macy off = start - (db->db_blkid * 1<<epbs); 187eda14cbcSMatt Macy num = end - start + 1; 188eda14cbcSMatt Macy 189eda14cbcSMatt Macy ASSERT3U(off, >=, 0); 190eda14cbcSMatt Macy ASSERT3U(num, >=, 0); 191eda14cbcSMatt Macy ASSERT3U(db->db_level, >, 0); 192eda14cbcSMatt Macy ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 193eda14cbcSMatt Macy ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 194eda14cbcSMatt Macy ASSERT(db->db_blkptr != NULL); 195eda14cbcSMatt Macy 196eda14cbcSMatt Macy for (i = off; i < off+num; i++) { 197eda14cbcSMatt Macy uint64_t *buf; 198eda14cbcSMatt Macy dmu_buf_impl_t *child; 199eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 200eda14cbcSMatt Macy int j; 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy ASSERT(db->db_level == 1); 203eda14cbcSMatt Macy 204eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 205eda14cbcSMatt Macy err = dbuf_hold_impl(dn, db->db_level - 1, 206eda14cbcSMatt Macy (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child); 207eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 208eda14cbcSMatt Macy if (err == ENOENT) 209eda14cbcSMatt Macy continue; 210eda14cbcSMatt Macy ASSERT(err == 0); 211eda14cbcSMatt Macy ASSERT(child->db_level == 0); 212eda14cbcSMatt Macy dr = dbuf_find_dirty_eq(child, txg); 213eda14cbcSMatt Macy 214eda14cbcSMatt Macy /* data_old better be zeroed */ 215eda14cbcSMatt Macy if (dr) { 216eda14cbcSMatt Macy buf = dr->dt.dl.dr_data->b_data; 217eda14cbcSMatt Macy for (j = 0; j < child->db.db_size >> 3; j++) { 218eda14cbcSMatt Macy if (buf[j] != 0) { 219eda14cbcSMatt Macy panic("freed data not zero: " 220eda14cbcSMatt Macy "child=%p i=%d off=%d num=%d\n", 221eda14cbcSMatt Macy (void *)child, i, off, num); 222eda14cbcSMatt Macy } 223eda14cbcSMatt Macy } 224eda14cbcSMatt Macy } 225eda14cbcSMatt Macy 226eda14cbcSMatt Macy /* 227eda14cbcSMatt Macy * db_data better be zeroed unless it's dirty in a 228eda14cbcSMatt Macy * future txg. 229eda14cbcSMatt Macy */ 230eda14cbcSMatt Macy mutex_enter(&child->db_mtx); 231eda14cbcSMatt Macy buf = child->db.db_data; 232eda14cbcSMatt Macy if (buf != NULL && child->db_state != DB_FILL && 233eda14cbcSMatt Macy list_is_empty(&child->db_dirty_records)) { 234eda14cbcSMatt Macy for (j = 0; j < child->db.db_size >> 3; j++) { 235eda14cbcSMatt Macy if (buf[j] != 0) { 236eda14cbcSMatt Macy panic("freed data not zero: " 237eda14cbcSMatt Macy "child=%p i=%d off=%d num=%d\n", 238eda14cbcSMatt Macy (void *)child, i, off, num); 239eda14cbcSMatt Macy } 240eda14cbcSMatt Macy } 241eda14cbcSMatt Macy } 242eda14cbcSMatt Macy mutex_exit(&child->db_mtx); 243eda14cbcSMatt Macy 244eda14cbcSMatt Macy dbuf_rele(child, FTAG); 245eda14cbcSMatt Macy } 246eda14cbcSMatt Macy DB_DNODE_EXIT(db); 247eda14cbcSMatt Macy } 248eda14cbcSMatt Macy #endif 249eda14cbcSMatt Macy 250eda14cbcSMatt Macy /* 251eda14cbcSMatt Macy * We don't usually free the indirect blocks here. If in one txg we have a 252eda14cbcSMatt Macy * free_range and a write to the same indirect block, it's important that we 253eda14cbcSMatt Macy * preserve the hole's birth times. Therefore, we don't free any any indirect 254eda14cbcSMatt Macy * blocks in free_children(). If an indirect block happens to turn into all 255eda14cbcSMatt Macy * holes, it will be freed by dbuf_write_children_ready, which happens at a 256eda14cbcSMatt Macy * point in the syncing process where we know for certain the contents of the 257eda14cbcSMatt Macy * indirect block. 258eda14cbcSMatt Macy * 259eda14cbcSMatt Macy * However, if we're freeing a dnode, its space accounting must go to zero 260eda14cbcSMatt Macy * before we actually try to free the dnode, or we will trip an assertion. In 261eda14cbcSMatt Macy * addition, we know the case described above cannot occur, because the dnode is 262eda14cbcSMatt Macy * being freed. Therefore, we free the indirect blocks immediately in that 263eda14cbcSMatt Macy * case. 264eda14cbcSMatt Macy */ 265eda14cbcSMatt Macy static void 266eda14cbcSMatt Macy free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, 267eda14cbcSMatt Macy boolean_t free_indirects, dmu_tx_t *tx) 268eda14cbcSMatt Macy { 269eda14cbcSMatt Macy dnode_t *dn; 270eda14cbcSMatt Macy blkptr_t *bp; 271eda14cbcSMatt Macy dmu_buf_impl_t *subdb; 272eda14cbcSMatt Macy uint64_t start, end, dbstart, dbend; 273eda14cbcSMatt Macy unsigned int epbs, shift, i; 274eda14cbcSMatt Macy 275eda14cbcSMatt Macy /* 276eda14cbcSMatt Macy * There is a small possibility that this block will not be cached: 277eda14cbcSMatt Macy * 1 - if level > 1 and there are no children with level <= 1 278eda14cbcSMatt Macy * 2 - if this block was evicted since we read it from 279eda14cbcSMatt Macy * dmu_tx_hold_free(). 280eda14cbcSMatt Macy */ 281eda14cbcSMatt Macy if (db->db_state != DB_CACHED) 282eda14cbcSMatt Macy (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 283eda14cbcSMatt Macy 284eda14cbcSMatt Macy /* 285eda14cbcSMatt Macy * If we modify this indirect block, and we are not freeing the 286eda14cbcSMatt Macy * dnode (!free_indirects), then this indirect block needs to get 287eda14cbcSMatt Macy * written to disk by dbuf_write(). If it is dirty, we know it will 288eda14cbcSMatt Macy * be written (otherwise, we would have incorrect on-disk state 289eda14cbcSMatt Macy * because the space would be freed but still referenced by the BP 290eda14cbcSMatt Macy * in this indirect block). Therefore we VERIFY that it is 291eda14cbcSMatt Macy * dirty. 292eda14cbcSMatt Macy * 293eda14cbcSMatt Macy * Our VERIFY covers some cases that do not actually have to be 294eda14cbcSMatt Macy * dirty, but the open-context code happens to dirty. E.g. if the 295eda14cbcSMatt Macy * blocks we are freeing are all holes, because in that case, we 296eda14cbcSMatt Macy * are only freeing part of this indirect block, so it is an 297eda14cbcSMatt Macy * ancestor of the first or last block to be freed. The first and 298eda14cbcSMatt Macy * last L1 indirect blocks are always dirtied by dnode_free_range(). 299eda14cbcSMatt Macy */ 300eda14cbcSMatt Macy db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG); 301eda14cbcSMatt Macy VERIFY(BP_GET_FILL(db->db_blkptr) == 0 || db->db_dirtycnt > 0); 302eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, FTAG); 303eda14cbcSMatt Macy 304eda14cbcSMatt Macy dbuf_release_bp(db); 305eda14cbcSMatt Macy bp = db->db.db_data; 306eda14cbcSMatt Macy 307eda14cbcSMatt Macy DB_DNODE_ENTER(db); 308eda14cbcSMatt Macy dn = DB_DNODE(db); 309eda14cbcSMatt Macy epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 310eda14cbcSMatt Macy ASSERT3U(epbs, <, 31); 311eda14cbcSMatt Macy shift = (db->db_level - 1) * epbs; 312eda14cbcSMatt Macy dbstart = db->db_blkid << epbs; 313eda14cbcSMatt Macy start = blkid >> shift; 314eda14cbcSMatt Macy if (dbstart < start) { 315eda14cbcSMatt Macy bp += start - dbstart; 316eda14cbcSMatt Macy } else { 317eda14cbcSMatt Macy start = dbstart; 318eda14cbcSMatt Macy } 319eda14cbcSMatt Macy dbend = ((db->db_blkid + 1) << epbs) - 1; 320eda14cbcSMatt Macy end = (blkid + nblks - 1) >> shift; 321eda14cbcSMatt Macy if (dbend <= end) 322eda14cbcSMatt Macy end = dbend; 323eda14cbcSMatt Macy 324eda14cbcSMatt Macy ASSERT3U(start, <=, end); 325eda14cbcSMatt Macy 326eda14cbcSMatt Macy if (db->db_level == 1) { 327eda14cbcSMatt Macy FREE_VERIFY(db, start, end, tx); 328eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 329eda14cbcSMatt Macy free_blocks(dn, bp, end - start + 1, tx); 330eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 331eda14cbcSMatt Macy } else { 332eda14cbcSMatt Macy for (uint64_t id = start; id <= end; id++, bp++) { 333eda14cbcSMatt Macy if (BP_IS_HOLE(bp)) 334eda14cbcSMatt Macy continue; 335eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 336eda14cbcSMatt Macy VERIFY0(dbuf_hold_impl(dn, db->db_level - 1, 337eda14cbcSMatt Macy id, TRUE, FALSE, FTAG, &subdb)); 338eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 339eda14cbcSMatt Macy ASSERT3P(bp, ==, subdb->db_blkptr); 340eda14cbcSMatt Macy 341eda14cbcSMatt Macy free_children(subdb, blkid, nblks, free_indirects, tx); 342eda14cbcSMatt Macy dbuf_rele(subdb, FTAG); 343eda14cbcSMatt Macy } 344eda14cbcSMatt Macy } 345eda14cbcSMatt Macy 346eda14cbcSMatt Macy if (free_indirects) { 347eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 348eda14cbcSMatt Macy for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) 349eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(bp)); 350eda14cbcSMatt Macy bzero(db->db.db_data, db->db.db_size); 351eda14cbcSMatt Macy free_blocks(dn, db->db_blkptr, 1, tx); 352eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 353eda14cbcSMatt Macy } 354eda14cbcSMatt Macy 355eda14cbcSMatt Macy DB_DNODE_EXIT(db); 356eda14cbcSMatt Macy arc_buf_freeze(db->db_buf); 357eda14cbcSMatt Macy } 358eda14cbcSMatt Macy 359eda14cbcSMatt Macy /* 360eda14cbcSMatt Macy * Traverse the indicated range of the provided file 361eda14cbcSMatt Macy * and "free" all the blocks contained there. 362eda14cbcSMatt Macy */ 363eda14cbcSMatt Macy static void 364eda14cbcSMatt Macy dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks, 365eda14cbcSMatt Macy boolean_t free_indirects, dmu_tx_t *tx) 366eda14cbcSMatt Macy { 367eda14cbcSMatt Macy blkptr_t *bp = dn->dn_phys->dn_blkptr; 368eda14cbcSMatt Macy int dnlevel = dn->dn_phys->dn_nlevels; 369eda14cbcSMatt Macy boolean_t trunc = B_FALSE; 370eda14cbcSMatt Macy 371eda14cbcSMatt Macy if (blkid > dn->dn_phys->dn_maxblkid) 372eda14cbcSMatt Macy return; 373eda14cbcSMatt Macy 374eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 375eda14cbcSMatt Macy if (blkid + nblks > dn->dn_phys->dn_maxblkid) { 376eda14cbcSMatt Macy nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 377eda14cbcSMatt Macy trunc = B_TRUE; 378eda14cbcSMatt Macy } 379eda14cbcSMatt Macy 380eda14cbcSMatt Macy /* There are no indirect blocks in the object */ 381eda14cbcSMatt Macy if (dnlevel == 1) { 382eda14cbcSMatt Macy if (blkid >= dn->dn_phys->dn_nblkptr) { 383eda14cbcSMatt Macy /* this range was never made persistent */ 384eda14cbcSMatt Macy return; 385eda14cbcSMatt Macy } 386eda14cbcSMatt Macy ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 387eda14cbcSMatt Macy free_blocks(dn, bp + blkid, nblks, tx); 388eda14cbcSMatt Macy } else { 389eda14cbcSMatt Macy int shift = (dnlevel - 1) * 390eda14cbcSMatt Macy (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 391eda14cbcSMatt Macy int start = blkid >> shift; 392eda14cbcSMatt Macy int end = (blkid + nblks - 1) >> shift; 393eda14cbcSMatt Macy dmu_buf_impl_t *db; 394eda14cbcSMatt Macy 395eda14cbcSMatt Macy ASSERT(start < dn->dn_phys->dn_nblkptr); 396eda14cbcSMatt Macy bp += start; 397eda14cbcSMatt Macy for (int i = start; i <= end; i++, bp++) { 398eda14cbcSMatt Macy if (BP_IS_HOLE(bp)) 399eda14cbcSMatt Macy continue; 400eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 401eda14cbcSMatt Macy VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i, 402eda14cbcSMatt Macy TRUE, FALSE, FTAG, &db)); 403eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 404eda14cbcSMatt Macy free_children(db, blkid, nblks, free_indirects, tx); 405eda14cbcSMatt Macy dbuf_rele(db, FTAG); 406eda14cbcSMatt Macy } 407eda14cbcSMatt Macy } 408eda14cbcSMatt Macy 409eda14cbcSMatt Macy /* 410eda14cbcSMatt Macy * Do not truncate the maxblkid if we are performing a raw 411eda14cbcSMatt Macy * receive. The raw receive sets the maxblkid manually and 412eda14cbcSMatt Macy * must not be overridden. Usually, the last DRR_FREE record 413eda14cbcSMatt Macy * will be at the maxblkid, because the source system sets 414eda14cbcSMatt Macy * the maxblkid when truncating. However, if the last block 415eda14cbcSMatt Macy * was freed by overwriting with zeros and being compressed 416eda14cbcSMatt Macy * away to a hole, the source system will generate a DRR_FREE 417eda14cbcSMatt Macy * record while leaving the maxblkid after the end of that 418eda14cbcSMatt Macy * record. In this case we need to leave the maxblkid as 419eda14cbcSMatt Macy * indicated in the DRR_OBJECT record, so that it matches the 420eda14cbcSMatt Macy * source system, ensuring that the cryptographic hashes will 421eda14cbcSMatt Macy * match. 422eda14cbcSMatt Macy */ 423eda14cbcSMatt Macy if (trunc && !dn->dn_objset->os_raw_receive) { 424eda14cbcSMatt Macy uint64_t off __maybe_unused; 425eda14cbcSMatt Macy dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1; 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy off = (dn->dn_phys->dn_maxblkid + 1) * 428eda14cbcSMatt Macy (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 429eda14cbcSMatt Macy ASSERT(off < dn->dn_phys->dn_maxblkid || 430eda14cbcSMatt Macy dn->dn_phys->dn_maxblkid == 0 || 431eda14cbcSMatt Macy dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); 432eda14cbcSMatt Macy } 433eda14cbcSMatt Macy } 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy typedef struct dnode_sync_free_range_arg { 436eda14cbcSMatt Macy dnode_t *dsfra_dnode; 437eda14cbcSMatt Macy dmu_tx_t *dsfra_tx; 438eda14cbcSMatt Macy boolean_t dsfra_free_indirects; 439eda14cbcSMatt Macy } dnode_sync_free_range_arg_t; 440eda14cbcSMatt Macy 441eda14cbcSMatt Macy static void 442eda14cbcSMatt Macy dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks) 443eda14cbcSMatt Macy { 444eda14cbcSMatt Macy dnode_sync_free_range_arg_t *dsfra = arg; 445eda14cbcSMatt Macy dnode_t *dn = dsfra->dsfra_dnode; 446eda14cbcSMatt Macy 447eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 448eda14cbcSMatt Macy dnode_sync_free_range_impl(dn, blkid, nblks, 449eda14cbcSMatt Macy dsfra->dsfra_free_indirects, dsfra->dsfra_tx); 450eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 451eda14cbcSMatt Macy } 452eda14cbcSMatt Macy 453eda14cbcSMatt Macy /* 454eda14cbcSMatt Macy * Try to kick all the dnode's dbufs out of the cache... 455eda14cbcSMatt Macy */ 456eda14cbcSMatt Macy void 457eda14cbcSMatt Macy dnode_evict_dbufs(dnode_t *dn) 458eda14cbcSMatt Macy { 459eda14cbcSMatt Macy dmu_buf_impl_t *db_marker; 460eda14cbcSMatt Macy dmu_buf_impl_t *db, *db_next; 461eda14cbcSMatt Macy 462eda14cbcSMatt Macy db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP); 463eda14cbcSMatt Macy 464eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 465eda14cbcSMatt Macy for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) { 466eda14cbcSMatt Macy 467eda14cbcSMatt Macy #ifdef ZFS_DEBUG 468eda14cbcSMatt Macy DB_DNODE_ENTER(db); 469eda14cbcSMatt Macy ASSERT3P(DB_DNODE(db), ==, dn); 470eda14cbcSMatt Macy DB_DNODE_EXIT(db); 471eda14cbcSMatt Macy #endif /* DEBUG */ 472eda14cbcSMatt Macy 473eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 474eda14cbcSMatt Macy if (db->db_state != DB_EVICTING && 475eda14cbcSMatt Macy zfs_refcount_is_zero(&db->db_holds)) { 476eda14cbcSMatt Macy db_marker->db_level = db->db_level; 477eda14cbcSMatt Macy db_marker->db_blkid = db->db_blkid; 478eda14cbcSMatt Macy db_marker->db_state = DB_SEARCH; 479eda14cbcSMatt Macy avl_insert_here(&dn->dn_dbufs, db_marker, db, 480eda14cbcSMatt Macy AVL_BEFORE); 481eda14cbcSMatt Macy 482eda14cbcSMatt Macy /* 483eda14cbcSMatt Macy * We need to use the "marker" dbuf rather than 484eda14cbcSMatt Macy * simply getting the next dbuf, because 485eda14cbcSMatt Macy * dbuf_destroy() may actually remove multiple dbufs. 486eda14cbcSMatt Macy * It can call itself recursively on the parent dbuf, 487eda14cbcSMatt Macy * which may also be removed from dn_dbufs. The code 488eda14cbcSMatt Macy * flow would look like: 489eda14cbcSMatt Macy * 490eda14cbcSMatt Macy * dbuf_destroy(): 491eda14cbcSMatt Macy * dnode_rele_and_unlock(parent_dbuf, evicting=TRUE): 492eda14cbcSMatt Macy * if (!cacheable || pending_evict) 493eda14cbcSMatt Macy * dbuf_destroy() 494eda14cbcSMatt Macy */ 495eda14cbcSMatt Macy dbuf_destroy(db); 496eda14cbcSMatt Macy 497eda14cbcSMatt Macy db_next = AVL_NEXT(&dn->dn_dbufs, db_marker); 498eda14cbcSMatt Macy avl_remove(&dn->dn_dbufs, db_marker); 499eda14cbcSMatt Macy } else { 500eda14cbcSMatt Macy db->db_pending_evict = TRUE; 501eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 502eda14cbcSMatt Macy db_next = AVL_NEXT(&dn->dn_dbufs, db); 503eda14cbcSMatt Macy } 504eda14cbcSMatt Macy } 505eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 506eda14cbcSMatt Macy 507eda14cbcSMatt Macy kmem_free(db_marker, sizeof (dmu_buf_impl_t)); 508eda14cbcSMatt Macy 509eda14cbcSMatt Macy dnode_evict_bonus(dn); 510eda14cbcSMatt Macy } 511eda14cbcSMatt Macy 512eda14cbcSMatt Macy void 513eda14cbcSMatt Macy dnode_evict_bonus(dnode_t *dn) 514eda14cbcSMatt Macy { 515eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 516eda14cbcSMatt Macy if (dn->dn_bonus != NULL) { 517eda14cbcSMatt Macy if (zfs_refcount_is_zero(&dn->dn_bonus->db_holds)) { 518eda14cbcSMatt Macy mutex_enter(&dn->dn_bonus->db_mtx); 519eda14cbcSMatt Macy dbuf_destroy(dn->dn_bonus); 520eda14cbcSMatt Macy dn->dn_bonus = NULL; 521eda14cbcSMatt Macy } else { 522eda14cbcSMatt Macy dn->dn_bonus->db_pending_evict = TRUE; 523eda14cbcSMatt Macy } 524eda14cbcSMatt Macy } 525eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 526eda14cbcSMatt Macy } 527eda14cbcSMatt Macy 528eda14cbcSMatt Macy static void 529eda14cbcSMatt Macy dnode_undirty_dbufs(list_t *list) 530eda14cbcSMatt Macy { 531eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 532eda14cbcSMatt Macy 533eda14cbcSMatt Macy while ((dr = list_head(list))) { 534eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 535eda14cbcSMatt Macy uint64_t txg = dr->dr_txg; 536eda14cbcSMatt Macy 537eda14cbcSMatt Macy if (db->db_level != 0) 538eda14cbcSMatt Macy dnode_undirty_dbufs(&dr->dt.di.dr_children); 539eda14cbcSMatt Macy 540eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 541eda14cbcSMatt Macy /* XXX - use dbuf_undirty()? */ 542eda14cbcSMatt Macy list_remove(list, dr); 543eda14cbcSMatt Macy ASSERT(list_head(&db->db_dirty_records) == dr); 544eda14cbcSMatt Macy list_remove_head(&db->db_dirty_records); 545eda14cbcSMatt Macy ASSERT(list_is_empty(&db->db_dirty_records)); 546eda14cbcSMatt Macy db->db_dirtycnt -= 1; 547eda14cbcSMatt Macy if (db->db_level == 0) { 548eda14cbcSMatt Macy ASSERT(db->db_blkid == DMU_BONUS_BLKID || 549eda14cbcSMatt Macy dr->dt.dl.dr_data == db->db_buf); 550eda14cbcSMatt Macy dbuf_unoverride(dr); 551eda14cbcSMatt Macy } else { 552eda14cbcSMatt Macy mutex_destroy(&dr->dt.di.dr_mtx); 553eda14cbcSMatt Macy list_destroy(&dr->dt.di.dr_children); 554eda14cbcSMatt Macy } 555eda14cbcSMatt Macy kmem_free(dr, sizeof (dbuf_dirty_record_t)); 556eda14cbcSMatt Macy dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE); 557eda14cbcSMatt Macy } 558eda14cbcSMatt Macy } 559eda14cbcSMatt Macy 560eda14cbcSMatt Macy static void 561eda14cbcSMatt Macy dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 562eda14cbcSMatt Macy { 563eda14cbcSMatt Macy int txgoff = tx->tx_txg & TXG_MASK; 564eda14cbcSMatt Macy 565eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 566eda14cbcSMatt Macy 567eda14cbcSMatt Macy /* 568eda14cbcSMatt Macy * Our contents should have been freed in dnode_sync() by the 569eda14cbcSMatt Macy * free range record inserted by the caller of dnode_free(). 570eda14cbcSMatt Macy */ 571eda14cbcSMatt Macy ASSERT0(DN_USED_BYTES(dn->dn_phys)); 572eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); 573eda14cbcSMatt Macy 574eda14cbcSMatt Macy dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); 575eda14cbcSMatt Macy dnode_evict_dbufs(dn); 576eda14cbcSMatt Macy 577eda14cbcSMatt Macy /* 578eda14cbcSMatt Macy * XXX - It would be nice to assert this, but we may still 579eda14cbcSMatt Macy * have residual holds from async evictions from the arc... 580eda14cbcSMatt Macy * 581eda14cbcSMatt Macy * zfs_obj_to_path() also depends on this being 582eda14cbcSMatt Macy * commented out. 583eda14cbcSMatt Macy * 584eda14cbcSMatt Macy * ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 1); 585eda14cbcSMatt Macy */ 586eda14cbcSMatt Macy 587eda14cbcSMatt Macy /* Undirty next bits */ 588eda14cbcSMatt Macy dn->dn_next_nlevels[txgoff] = 0; 589eda14cbcSMatt Macy dn->dn_next_indblkshift[txgoff] = 0; 590eda14cbcSMatt Macy dn->dn_next_blksz[txgoff] = 0; 591eda14cbcSMatt Macy dn->dn_next_maxblkid[txgoff] = 0; 592eda14cbcSMatt Macy 593eda14cbcSMatt Macy /* ASSERT(blkptrs are zero); */ 594eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 595eda14cbcSMatt Macy ASSERT(dn->dn_type != DMU_OT_NONE); 596eda14cbcSMatt Macy 597eda14cbcSMatt Macy ASSERT(dn->dn_free_txg > 0); 598eda14cbcSMatt Macy if (dn->dn_allocated_txg != dn->dn_free_txg) 599eda14cbcSMatt Macy dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); 600eda14cbcSMatt Macy bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots); 601eda14cbcSMatt Macy dnode_free_interior_slots(dn); 602eda14cbcSMatt Macy 603eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 604eda14cbcSMatt Macy dn->dn_type = DMU_OT_NONE; 605eda14cbcSMatt Macy dn->dn_maxblkid = 0; 606eda14cbcSMatt Macy dn->dn_allocated_txg = 0; 607eda14cbcSMatt Macy dn->dn_free_txg = 0; 608eda14cbcSMatt Macy dn->dn_have_spill = B_FALSE; 609eda14cbcSMatt Macy dn->dn_num_slots = 1; 610eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 611eda14cbcSMatt Macy 612eda14cbcSMatt Macy ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 613eda14cbcSMatt Macy 614eda14cbcSMatt Macy dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 615eda14cbcSMatt Macy /* 616eda14cbcSMatt Macy * Now that we've released our hold, the dnode may 617eda14cbcSMatt Macy * be evicted, so we mustn't access it. 618eda14cbcSMatt Macy */ 619eda14cbcSMatt Macy } 620eda14cbcSMatt Macy 621eda14cbcSMatt Macy /* 622eda14cbcSMatt Macy * Write out the dnode's dirty buffers. 623eda14cbcSMatt Macy */ 624eda14cbcSMatt Macy void 625eda14cbcSMatt Macy dnode_sync(dnode_t *dn, dmu_tx_t *tx) 626eda14cbcSMatt Macy { 627eda14cbcSMatt Macy objset_t *os = dn->dn_objset; 628eda14cbcSMatt Macy dnode_phys_t *dnp = dn->dn_phys; 629eda14cbcSMatt Macy int txgoff = tx->tx_txg & TXG_MASK; 630eda14cbcSMatt Macy list_t *list = &dn->dn_dirty_records[txgoff]; 631eda14cbcSMatt Macy static const dnode_phys_t zerodn __maybe_unused = { 0 }; 632eda14cbcSMatt Macy boolean_t kill_spill = B_FALSE; 633eda14cbcSMatt Macy 634eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 635eda14cbcSMatt Macy ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 636eda14cbcSMatt Macy ASSERT(dnp->dn_type != DMU_OT_NONE || 637eda14cbcSMatt Macy bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0); 638eda14cbcSMatt Macy DNODE_VERIFY(dn); 639eda14cbcSMatt Macy 640eda14cbcSMatt Macy ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); 641eda14cbcSMatt Macy 642eda14cbcSMatt Macy /* 643eda14cbcSMatt Macy * Do user accounting if it is enabled and this is not 644eda14cbcSMatt Macy * an encrypted receive. 645eda14cbcSMatt Macy */ 646eda14cbcSMatt Macy if (dmu_objset_userused_enabled(os) && 647eda14cbcSMatt Macy !DMU_OBJECT_IS_SPECIAL(dn->dn_object) && 648eda14cbcSMatt Macy (!os->os_encrypted || !dmu_objset_is_receiving(os))) { 649eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 650eda14cbcSMatt Macy dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); 651eda14cbcSMatt Macy dn->dn_oldflags = dn->dn_phys->dn_flags; 652eda14cbcSMatt Macy dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; 653eda14cbcSMatt Macy if (dmu_objset_userobjused_enabled(dn->dn_objset)) 654eda14cbcSMatt Macy dn->dn_phys->dn_flags |= 655eda14cbcSMatt Macy DNODE_FLAG_USEROBJUSED_ACCOUNTED; 656eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 657eda14cbcSMatt Macy dmu_objset_userquota_get_ids(dn, B_FALSE, tx); 658*e92ffd9bSMartin Matuska } else if (!(os->os_encrypted && dmu_objset_is_receiving(os))) { 659*e92ffd9bSMartin Matuska /* 660*e92ffd9bSMartin Matuska * Once we account for it, we should always account for it, 661*e92ffd9bSMartin Matuska * except for the case of a raw receive. We will not be able 662*e92ffd9bSMartin Matuska * to account for it until the receiving dataset has been 663*e92ffd9bSMartin Matuska * mounted. 664*e92ffd9bSMartin Matuska */ 665eda14cbcSMatt Macy ASSERT(!(dn->dn_phys->dn_flags & 666eda14cbcSMatt Macy DNODE_FLAG_USERUSED_ACCOUNTED)); 667eda14cbcSMatt Macy ASSERT(!(dn->dn_phys->dn_flags & 668eda14cbcSMatt Macy DNODE_FLAG_USEROBJUSED_ACCOUNTED)); 669eda14cbcSMatt Macy } 670eda14cbcSMatt Macy 671eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 672eda14cbcSMatt Macy if (dn->dn_allocated_txg == tx->tx_txg) { 673eda14cbcSMatt Macy /* The dnode is newly allocated or reallocated */ 674eda14cbcSMatt Macy if (dnp->dn_type == DMU_OT_NONE) { 675eda14cbcSMatt Macy /* this is a first alloc, not a realloc */ 676eda14cbcSMatt Macy dnp->dn_nlevels = 1; 677eda14cbcSMatt Macy dnp->dn_nblkptr = dn->dn_nblkptr; 678eda14cbcSMatt Macy } 679eda14cbcSMatt Macy 680eda14cbcSMatt Macy dnp->dn_type = dn->dn_type; 681eda14cbcSMatt Macy dnp->dn_bonustype = dn->dn_bonustype; 682eda14cbcSMatt Macy dnp->dn_bonuslen = dn->dn_bonuslen; 683eda14cbcSMatt Macy } 684eda14cbcSMatt Macy 685eda14cbcSMatt Macy dnp->dn_extra_slots = dn->dn_num_slots - 1; 686eda14cbcSMatt Macy 687eda14cbcSMatt Macy ASSERT(dnp->dn_nlevels > 1 || 688eda14cbcSMatt Macy BP_IS_HOLE(&dnp->dn_blkptr[0]) || 689eda14cbcSMatt Macy BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || 690eda14cbcSMatt Macy BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 691eda14cbcSMatt Macy dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 692eda14cbcSMatt Macy ASSERT(dnp->dn_nlevels < 2 || 693eda14cbcSMatt Macy BP_IS_HOLE(&dnp->dn_blkptr[0]) || 694eda14cbcSMatt Macy BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); 695eda14cbcSMatt Macy 696eda14cbcSMatt Macy if (dn->dn_next_type[txgoff] != 0) { 697eda14cbcSMatt Macy dnp->dn_type = dn->dn_type; 698eda14cbcSMatt Macy dn->dn_next_type[txgoff] = 0; 699eda14cbcSMatt Macy } 700eda14cbcSMatt Macy 701eda14cbcSMatt Macy if (dn->dn_next_blksz[txgoff] != 0) { 702eda14cbcSMatt Macy ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], 703eda14cbcSMatt Macy SPA_MINBLOCKSIZE) == 0); 704eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || 705eda14cbcSMatt Macy dn->dn_maxblkid == 0 || list_head(list) != NULL || 706eda14cbcSMatt Macy dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == 707eda14cbcSMatt Macy dnp->dn_datablkszsec || 708eda14cbcSMatt Macy !range_tree_is_empty(dn->dn_free_ranges[txgoff])); 709eda14cbcSMatt Macy dnp->dn_datablkszsec = 710eda14cbcSMatt Macy dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; 711eda14cbcSMatt Macy dn->dn_next_blksz[txgoff] = 0; 712eda14cbcSMatt Macy } 713eda14cbcSMatt Macy 714eda14cbcSMatt Macy if (dn->dn_next_bonuslen[txgoff] != 0) { 715eda14cbcSMatt Macy if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) 716eda14cbcSMatt Macy dnp->dn_bonuslen = 0; 717eda14cbcSMatt Macy else 718eda14cbcSMatt Macy dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; 719eda14cbcSMatt Macy ASSERT(dnp->dn_bonuslen <= 720eda14cbcSMatt Macy DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1)); 721eda14cbcSMatt Macy dn->dn_next_bonuslen[txgoff] = 0; 722eda14cbcSMatt Macy } 723eda14cbcSMatt Macy 724eda14cbcSMatt Macy if (dn->dn_next_bonustype[txgoff] != 0) { 725eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); 726eda14cbcSMatt Macy dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; 727eda14cbcSMatt Macy dn->dn_next_bonustype[txgoff] = 0; 728eda14cbcSMatt Macy } 729eda14cbcSMatt Macy 730eda14cbcSMatt Macy boolean_t freeing_dnode = dn->dn_free_txg > 0 && 731eda14cbcSMatt Macy dn->dn_free_txg <= tx->tx_txg; 732eda14cbcSMatt Macy 733eda14cbcSMatt Macy /* 734eda14cbcSMatt Macy * Remove the spill block if we have been explicitly asked to 735eda14cbcSMatt Macy * remove it, or if the object is being removed. 736eda14cbcSMatt Macy */ 737eda14cbcSMatt Macy if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) { 738eda14cbcSMatt Macy if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) 739eda14cbcSMatt Macy kill_spill = B_TRUE; 740eda14cbcSMatt Macy dn->dn_rm_spillblk[txgoff] = 0; 741eda14cbcSMatt Macy } 742eda14cbcSMatt Macy 743eda14cbcSMatt Macy if (dn->dn_next_indblkshift[txgoff] != 0) { 744eda14cbcSMatt Macy ASSERT(dnp->dn_nlevels == 1); 745eda14cbcSMatt Macy dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 746eda14cbcSMatt Macy dn->dn_next_indblkshift[txgoff] = 0; 747eda14cbcSMatt Macy } 748eda14cbcSMatt Macy 749eda14cbcSMatt Macy /* 750eda14cbcSMatt Macy * Just take the live (open-context) values for checksum and compress. 751eda14cbcSMatt Macy * Strictly speaking it's a future leak, but nothing bad happens if we 752eda14cbcSMatt Macy * start using the new checksum or compress algorithm a little early. 753eda14cbcSMatt Macy */ 754eda14cbcSMatt Macy dnp->dn_checksum = dn->dn_checksum; 755eda14cbcSMatt Macy dnp->dn_compress = dn->dn_compress; 756eda14cbcSMatt Macy 757eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy if (kill_spill) { 760eda14cbcSMatt Macy free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx); 761eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 762eda14cbcSMatt Macy dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; 763eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 764eda14cbcSMatt Macy } 765eda14cbcSMatt Macy 766eda14cbcSMatt Macy /* process all the "freed" ranges in the file */ 767eda14cbcSMatt Macy if (dn->dn_free_ranges[txgoff] != NULL) { 768eda14cbcSMatt Macy dnode_sync_free_range_arg_t dsfra; 769eda14cbcSMatt Macy dsfra.dsfra_dnode = dn; 770eda14cbcSMatt Macy dsfra.dsfra_tx = tx; 771eda14cbcSMatt Macy dsfra.dsfra_free_indirects = freeing_dnode; 772ac0bf12eSMatt Macy mutex_enter(&dn->dn_mtx); 773eda14cbcSMatt Macy if (freeing_dnode) { 774eda14cbcSMatt Macy ASSERT(range_tree_contains(dn->dn_free_ranges[txgoff], 775eda14cbcSMatt Macy 0, dn->dn_maxblkid + 1)); 776eda14cbcSMatt Macy } 777ac0bf12eSMatt Macy /* 778ac0bf12eSMatt Macy * Because dnode_sync_free_range() must drop dn_mtx during its 779ac0bf12eSMatt Macy * processing, using it as a callback to range_tree_vacate() is 780ac0bf12eSMatt Macy * not safe. No other operations (besides destroy) are allowed 781ac0bf12eSMatt Macy * once range_tree_vacate() has begun, and dropping dn_mtx 782ac0bf12eSMatt Macy * would leave a window open for another thread to observe that 783ac0bf12eSMatt Macy * invalid (and unsafe) state. 784ac0bf12eSMatt Macy */ 785ac0bf12eSMatt Macy range_tree_walk(dn->dn_free_ranges[txgoff], 786eda14cbcSMatt Macy dnode_sync_free_range, &dsfra); 787ac0bf12eSMatt Macy range_tree_vacate(dn->dn_free_ranges[txgoff], NULL, NULL); 788eda14cbcSMatt Macy range_tree_destroy(dn->dn_free_ranges[txgoff]); 789eda14cbcSMatt Macy dn->dn_free_ranges[txgoff] = NULL; 790eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 791eda14cbcSMatt Macy } 792eda14cbcSMatt Macy 793eda14cbcSMatt Macy if (freeing_dnode) { 794eda14cbcSMatt Macy dn->dn_objset->os_freed_dnodes++; 795eda14cbcSMatt Macy dnode_sync_free(dn, tx); 796eda14cbcSMatt Macy return; 797eda14cbcSMatt Macy } 798eda14cbcSMatt Macy 799eda14cbcSMatt Macy if (dn->dn_num_slots > DNODE_MIN_SLOTS) { 800eda14cbcSMatt Macy dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 801eda14cbcSMatt Macy mutex_enter(&ds->ds_lock); 802eda14cbcSMatt Macy ds->ds_feature_activation[SPA_FEATURE_LARGE_DNODE] = 803eda14cbcSMatt Macy (void *)B_TRUE; 804eda14cbcSMatt Macy mutex_exit(&ds->ds_lock); 805eda14cbcSMatt Macy } 806eda14cbcSMatt Macy 807eda14cbcSMatt Macy if (dn->dn_next_nlevels[txgoff]) { 808eda14cbcSMatt Macy dnode_increase_indirection(dn, tx); 809eda14cbcSMatt Macy dn->dn_next_nlevels[txgoff] = 0; 810eda14cbcSMatt Macy } 811eda14cbcSMatt Macy 812eda14cbcSMatt Macy /* 813eda14cbcSMatt Macy * This must be done after dnode_sync_free_range() 814eda14cbcSMatt Macy * and dnode_increase_indirection(). See dnode_new_blkid() 815eda14cbcSMatt Macy * for an explanation of the high bit being set. 816eda14cbcSMatt Macy */ 817eda14cbcSMatt Macy if (dn->dn_next_maxblkid[txgoff]) { 818eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 819eda14cbcSMatt Macy dnp->dn_maxblkid = 820eda14cbcSMatt Macy dn->dn_next_maxblkid[txgoff] & ~DMU_NEXT_MAXBLKID_SET; 821eda14cbcSMatt Macy dn->dn_next_maxblkid[txgoff] = 0; 822eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 823eda14cbcSMatt Macy } 824eda14cbcSMatt Macy 825eda14cbcSMatt Macy if (dn->dn_next_nblkptr[txgoff]) { 826eda14cbcSMatt Macy /* this should only happen on a realloc */ 827eda14cbcSMatt Macy ASSERT(dn->dn_allocated_txg == tx->tx_txg); 828eda14cbcSMatt Macy if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { 829eda14cbcSMatt Macy /* zero the new blkptrs we are gaining */ 830eda14cbcSMatt Macy bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 831eda14cbcSMatt Macy sizeof (blkptr_t) * 832eda14cbcSMatt Macy (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); 833eda14cbcSMatt Macy #ifdef ZFS_DEBUG 834eda14cbcSMatt Macy } else { 835eda14cbcSMatt Macy int i; 836eda14cbcSMatt Macy ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); 837eda14cbcSMatt Macy /* the blkptrs we are losing better be unallocated */ 838eda14cbcSMatt Macy for (i = 0; i < dnp->dn_nblkptr; i++) { 839eda14cbcSMatt Macy if (i >= dn->dn_next_nblkptr[txgoff]) 840eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); 841eda14cbcSMatt Macy } 842eda14cbcSMatt Macy #endif 843eda14cbcSMatt Macy } 844eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 845eda14cbcSMatt Macy dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; 846eda14cbcSMatt Macy dn->dn_next_nblkptr[txgoff] = 0; 847eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 848eda14cbcSMatt Macy } 849eda14cbcSMatt Macy 850eda14cbcSMatt Macy dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx); 851eda14cbcSMatt Macy 852eda14cbcSMatt Macy if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 853eda14cbcSMatt Macy ASSERT3P(list_head(list), ==, NULL); 854eda14cbcSMatt Macy dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 855eda14cbcSMatt Macy } 856eda14cbcSMatt Macy 857eda14cbcSMatt Macy /* 858eda14cbcSMatt Macy * Although we have dropped our reference to the dnode, it 859eda14cbcSMatt Macy * can't be evicted until its written, and we haven't yet 8607877fdebSMatt Macy * initiated the IO for the dnode's dbuf. Additionally, the caller 8617877fdebSMatt Macy * has already added a reference to the dnode because it's on the 8627877fdebSMatt Macy * os_synced_dnodes list. 863eda14cbcSMatt Macy */ 864eda14cbcSMatt Macy } 865