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