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 http://www.opensolaris.org/os/licensing. 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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 */ 28 29 #include <sys/zfs_context.h> 30 #include <sys/dmu.h> 31 #include <sys/dmu_send.h> 32 #include <sys/dmu_impl.h> 33 #include <sys/dbuf.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dsl_dataset.h> 36 #include <sys/dsl_dir.h> 37 #include <sys/dmu_tx.h> 38 #include <sys/spa.h> 39 #include <sys/zio.h> 40 #include <sys/dmu_zfetch.h> 41 #include <sys/sa.h> 42 #include <sys/sa_impl.h> 43 #include <sys/zfeature.h> 44 #include <sys/blkptr.h> 45 #include <sys/range_tree.h> 46 47 /* 48 * Number of times that zfs_free_range() took the slow path while doing 49 * a zfs receive. A nonzero value indicates a potential performance problem. 50 */ 51 uint64_t zfs_free_range_recv_miss; 52 53 static void dbuf_destroy(dmu_buf_impl_t *db); 54 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 55 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx); 56 57 /* 58 * Global data structures and functions for the dbuf cache. 59 */ 60 static kmem_cache_t *dbuf_cache; 61 62 /* ARGSUSED */ 63 static int 64 dbuf_cons(void *vdb, void *unused, int kmflag) 65 { 66 dmu_buf_impl_t *db = vdb; 67 bzero(db, sizeof (dmu_buf_impl_t)); 68 69 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL); 70 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL); 71 refcount_create(&db->db_holds); 72 73 db->db_creation = gethrtime(); 74 75 return (0); 76 } 77 78 /* ARGSUSED */ 79 static void 80 dbuf_dest(void *vdb, void *unused) 81 { 82 dmu_buf_impl_t *db = vdb; 83 mutex_destroy(&db->db_mtx); 84 cv_destroy(&db->db_changed); 85 refcount_destroy(&db->db_holds); 86 } 87 88 /* 89 * dbuf hash table routines 90 */ 91 static dbuf_hash_table_t dbuf_hash_table; 92 93 static uint64_t dbuf_hash_count; 94 95 static uint64_t 96 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid) 97 { 98 uintptr_t osv = (uintptr_t)os; 99 uint64_t crc = -1ULL; 100 101 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 102 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF]; 103 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; 104 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; 105 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; 106 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF]; 107 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF]; 108 109 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16); 110 111 return (crc); 112 } 113 114 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid); 115 116 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \ 117 ((dbuf)->db.db_object == (obj) && \ 118 (dbuf)->db_objset == (os) && \ 119 (dbuf)->db_level == (level) && \ 120 (dbuf)->db_blkid == (blkid)) 121 122 dmu_buf_impl_t * 123 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid) 124 { 125 dbuf_hash_table_t *h = &dbuf_hash_table; 126 objset_t *os = dn->dn_objset; 127 uint64_t obj = dn->dn_object; 128 uint64_t hv = DBUF_HASH(os, obj, level, blkid); 129 uint64_t idx = hv & h->hash_table_mask; 130 dmu_buf_impl_t *db; 131 132 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 133 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) { 134 if (DBUF_EQUAL(db, os, obj, level, blkid)) { 135 mutex_enter(&db->db_mtx); 136 if (db->db_state != DB_EVICTING) { 137 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 138 return (db); 139 } 140 mutex_exit(&db->db_mtx); 141 } 142 } 143 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 144 return (NULL); 145 } 146 147 /* 148 * Insert an entry into the hash table. If there is already an element 149 * equal to elem in the hash table, then the already existing element 150 * will be returned and the new element will not be inserted. 151 * Otherwise returns NULL. 152 */ 153 static dmu_buf_impl_t * 154 dbuf_hash_insert(dmu_buf_impl_t *db) 155 { 156 dbuf_hash_table_t *h = &dbuf_hash_table; 157 objset_t *os = db->db_objset; 158 uint64_t obj = db->db.db_object; 159 int level = db->db_level; 160 uint64_t blkid = db->db_blkid; 161 uint64_t hv = DBUF_HASH(os, obj, level, blkid); 162 uint64_t idx = hv & h->hash_table_mask; 163 dmu_buf_impl_t *dbf; 164 165 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 166 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) { 167 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) { 168 mutex_enter(&dbf->db_mtx); 169 if (dbf->db_state != DB_EVICTING) { 170 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 171 return (dbf); 172 } 173 mutex_exit(&dbf->db_mtx); 174 } 175 } 176 177 mutex_enter(&db->db_mtx); 178 db->db_hash_next = h->hash_table[idx]; 179 h->hash_table[idx] = db; 180 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 181 atomic_inc_64(&dbuf_hash_count); 182 183 return (NULL); 184 } 185 186 /* 187 * Remove an entry from the hash table. It must be in the EVICTING state. 188 */ 189 static void 190 dbuf_hash_remove(dmu_buf_impl_t *db) 191 { 192 dbuf_hash_table_t *h = &dbuf_hash_table; 193 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object, 194 db->db_level, db->db_blkid); 195 uint64_t idx = hv & h->hash_table_mask; 196 dmu_buf_impl_t *dbf, **dbp; 197 198 /* 199 * We musn't hold db_mtx to maintain lock ordering: 200 * DBUF_HASH_MUTEX > db_mtx. 201 */ 202 ASSERT(refcount_is_zero(&db->db_holds)); 203 ASSERT(db->db_state == DB_EVICTING); 204 ASSERT(!MUTEX_HELD(&db->db_mtx)); 205 206 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 207 dbp = &h->hash_table[idx]; 208 while ((dbf = *dbp) != db) { 209 dbp = &dbf->db_hash_next; 210 ASSERT(dbf != NULL); 211 } 212 *dbp = db->db_hash_next; 213 db->db_hash_next = NULL; 214 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 215 atomic_dec_64(&dbuf_hash_count); 216 } 217 218 static arc_evict_func_t dbuf_do_evict; 219 220 static void 221 dbuf_evict_user(dmu_buf_impl_t *db) 222 { 223 ASSERT(MUTEX_HELD(&db->db_mtx)); 224 225 if (db->db_level != 0 || db->db_evict_func == NULL) 226 return; 227 228 if (db->db_user_data_ptr_ptr) 229 *db->db_user_data_ptr_ptr = db->db.db_data; 230 db->db_evict_func(&db->db, db->db_user_ptr); 231 db->db_user_ptr = NULL; 232 db->db_user_data_ptr_ptr = NULL; 233 db->db_evict_func = NULL; 234 } 235 236 boolean_t 237 dbuf_is_metadata(dmu_buf_impl_t *db) 238 { 239 if (db->db_level > 0) { 240 return (B_TRUE); 241 } else { 242 boolean_t is_metadata; 243 244 DB_DNODE_ENTER(db); 245 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type); 246 DB_DNODE_EXIT(db); 247 248 return (is_metadata); 249 } 250 } 251 252 void 253 dbuf_evict(dmu_buf_impl_t *db) 254 { 255 ASSERT(MUTEX_HELD(&db->db_mtx)); 256 ASSERT(db->db_buf == NULL); 257 ASSERT(db->db_data_pending == NULL); 258 259 dbuf_clear(db); 260 dbuf_destroy(db); 261 } 262 263 void 264 dbuf_init(void) 265 { 266 uint64_t hsize = 1ULL << 16; 267 dbuf_hash_table_t *h = &dbuf_hash_table; 268 int i; 269 270 /* 271 * The hash table is big enough to fill all of physical memory 272 * with an average 4K block size. The table will take up 273 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers). 274 */ 275 while (hsize * 4096 < physmem * PAGESIZE) 276 hsize <<= 1; 277 278 retry: 279 h->hash_table_mask = hsize - 1; 280 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP); 281 if (h->hash_table == NULL) { 282 /* XXX - we should really return an error instead of assert */ 283 ASSERT(hsize > (1ULL << 10)); 284 hsize >>= 1; 285 goto retry; 286 } 287 288 dbuf_cache = kmem_cache_create("dmu_buf_impl_t", 289 sizeof (dmu_buf_impl_t), 290 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0); 291 292 for (i = 0; i < DBUF_MUTEXES; i++) 293 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL); 294 } 295 296 void 297 dbuf_fini(void) 298 { 299 dbuf_hash_table_t *h = &dbuf_hash_table; 300 int i; 301 302 for (i = 0; i < DBUF_MUTEXES; i++) 303 mutex_destroy(&h->hash_mutexes[i]); 304 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 305 kmem_cache_destroy(dbuf_cache); 306 } 307 308 /* 309 * Other stuff. 310 */ 311 312 #ifdef ZFS_DEBUG 313 static void 314 dbuf_verify(dmu_buf_impl_t *db) 315 { 316 dnode_t *dn; 317 dbuf_dirty_record_t *dr; 318 319 ASSERT(MUTEX_HELD(&db->db_mtx)); 320 321 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY)) 322 return; 323 324 ASSERT(db->db_objset != NULL); 325 DB_DNODE_ENTER(db); 326 dn = DB_DNODE(db); 327 if (dn == NULL) { 328 ASSERT(db->db_parent == NULL); 329 ASSERT(db->db_blkptr == NULL); 330 } else { 331 ASSERT3U(db->db.db_object, ==, dn->dn_object); 332 ASSERT3P(db->db_objset, ==, dn->dn_objset); 333 ASSERT3U(db->db_level, <, dn->dn_nlevels); 334 ASSERT(db->db_blkid == DMU_BONUS_BLKID || 335 db->db_blkid == DMU_SPILL_BLKID || 336 !avl_is_empty(&dn->dn_dbufs)); 337 } 338 if (db->db_blkid == DMU_BONUS_BLKID) { 339 ASSERT(dn != NULL); 340 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 341 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID); 342 } else if (db->db_blkid == DMU_SPILL_BLKID) { 343 ASSERT(dn != NULL); 344 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 345 ASSERT0(db->db.db_offset); 346 } else { 347 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size); 348 } 349 350 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next) 351 ASSERT(dr->dr_dbuf == db); 352 353 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next) 354 ASSERT(dr->dr_dbuf == db); 355 356 /* 357 * We can't assert that db_size matches dn_datablksz because it 358 * can be momentarily different when another thread is doing 359 * dnode_set_blksz(). 360 */ 361 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) { 362 dr = db->db_data_pending; 363 /* 364 * It should only be modified in syncing context, so 365 * make sure we only have one copy of the data. 366 */ 367 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf); 368 } 369 370 /* verify db->db_blkptr */ 371 if (db->db_blkptr) { 372 if (db->db_parent == dn->dn_dbuf) { 373 /* db is pointed to by the dnode */ 374 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */ 375 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object)) 376 ASSERT(db->db_parent == NULL); 377 else 378 ASSERT(db->db_parent != NULL); 379 if (db->db_blkid != DMU_SPILL_BLKID) 380 ASSERT3P(db->db_blkptr, ==, 381 &dn->dn_phys->dn_blkptr[db->db_blkid]); 382 } else { 383 /* db is pointed to by an indirect block */ 384 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT; 385 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1); 386 ASSERT3U(db->db_parent->db.db_object, ==, 387 db->db.db_object); 388 /* 389 * dnode_grow_indblksz() can make this fail if we don't 390 * have the struct_rwlock. XXX indblksz no longer 391 * grows. safe to do this now? 392 */ 393 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 394 ASSERT3P(db->db_blkptr, ==, 395 ((blkptr_t *)db->db_parent->db.db_data + 396 db->db_blkid % epb)); 397 } 398 } 399 } 400 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) && 401 (db->db_buf == NULL || db->db_buf->b_data) && 402 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID && 403 db->db_state != DB_FILL && !dn->dn_free_txg) { 404 /* 405 * If the blkptr isn't set but they have nonzero data, 406 * it had better be dirty, otherwise we'll lose that 407 * data when we evict this buffer. 408 */ 409 if (db->db_dirtycnt == 0) { 410 uint64_t *buf = db->db.db_data; 411 int i; 412 413 for (i = 0; i < db->db.db_size >> 3; i++) { 414 ASSERT(buf[i] == 0); 415 } 416 } 417 } 418 DB_DNODE_EXIT(db); 419 } 420 #endif 421 422 static void 423 dbuf_update_data(dmu_buf_impl_t *db) 424 { 425 ASSERT(MUTEX_HELD(&db->db_mtx)); 426 if (db->db_level == 0 && db->db_user_data_ptr_ptr) { 427 ASSERT(!refcount_is_zero(&db->db_holds)); 428 *db->db_user_data_ptr_ptr = db->db.db_data; 429 } 430 } 431 432 static void 433 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf) 434 { 435 ASSERT(MUTEX_HELD(&db->db_mtx)); 436 db->db_buf = buf; 437 if (buf != NULL) { 438 ASSERT(buf->b_data != NULL); 439 db->db.db_data = buf->b_data; 440 if (!arc_released(buf)) 441 arc_set_callback(buf, dbuf_do_evict, db); 442 dbuf_update_data(db); 443 } else { 444 dbuf_evict_user(db); 445 db->db.db_data = NULL; 446 if (db->db_state != DB_NOFILL) 447 db->db_state = DB_UNCACHED; 448 } 449 } 450 451 /* 452 * Loan out an arc_buf for read. Return the loaned arc_buf. 453 */ 454 arc_buf_t * 455 dbuf_loan_arcbuf(dmu_buf_impl_t *db) 456 { 457 arc_buf_t *abuf; 458 459 mutex_enter(&db->db_mtx); 460 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) { 461 int blksz = db->db.db_size; 462 spa_t *spa = db->db_objset->os_spa; 463 464 mutex_exit(&db->db_mtx); 465 abuf = arc_loan_buf(spa, blksz); 466 bcopy(db->db.db_data, abuf->b_data, blksz); 467 } else { 468 abuf = db->db_buf; 469 arc_loan_inuse_buf(abuf, db); 470 dbuf_set_data(db, NULL); 471 mutex_exit(&db->db_mtx); 472 } 473 return (abuf); 474 } 475 476 uint64_t 477 dbuf_whichblock(dnode_t *dn, uint64_t offset) 478 { 479 if (dn->dn_datablkshift) { 480 return (offset >> dn->dn_datablkshift); 481 } else { 482 ASSERT3U(offset, <, dn->dn_datablksz); 483 return (0); 484 } 485 } 486 487 static void 488 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb) 489 { 490 dmu_buf_impl_t *db = vdb; 491 492 mutex_enter(&db->db_mtx); 493 ASSERT3U(db->db_state, ==, DB_READ); 494 /* 495 * All reads are synchronous, so we must have a hold on the dbuf 496 */ 497 ASSERT(refcount_count(&db->db_holds) > 0); 498 ASSERT(db->db_buf == NULL); 499 ASSERT(db->db.db_data == NULL); 500 if (db->db_level == 0 && db->db_freed_in_flight) { 501 /* we were freed in flight; disregard any error */ 502 arc_release(buf, db); 503 bzero(buf->b_data, db->db.db_size); 504 arc_buf_freeze(buf); 505 db->db_freed_in_flight = FALSE; 506 dbuf_set_data(db, buf); 507 db->db_state = DB_CACHED; 508 } else if (zio == NULL || zio->io_error == 0) { 509 dbuf_set_data(db, buf); 510 db->db_state = DB_CACHED; 511 } else { 512 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 513 ASSERT3P(db->db_buf, ==, NULL); 514 VERIFY(arc_buf_remove_ref(buf, db)); 515 db->db_state = DB_UNCACHED; 516 } 517 cv_broadcast(&db->db_changed); 518 dbuf_rele_and_unlock(db, NULL); 519 } 520 521 static void 522 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags) 523 { 524 dnode_t *dn; 525 zbookmark_phys_t zb; 526 uint32_t aflags = ARC_NOWAIT; 527 528 DB_DNODE_ENTER(db); 529 dn = DB_DNODE(db); 530 ASSERT(!refcount_is_zero(&db->db_holds)); 531 /* We need the struct_rwlock to prevent db_blkptr from changing. */ 532 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 533 ASSERT(MUTEX_HELD(&db->db_mtx)); 534 ASSERT(db->db_state == DB_UNCACHED); 535 ASSERT(db->db_buf == NULL); 536 537 if (db->db_blkid == DMU_BONUS_BLKID) { 538 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen); 539 540 ASSERT3U(bonuslen, <=, db->db.db_size); 541 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN); 542 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 543 if (bonuslen < DN_MAX_BONUSLEN) 544 bzero(db->db.db_data, DN_MAX_BONUSLEN); 545 if (bonuslen) 546 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen); 547 DB_DNODE_EXIT(db); 548 dbuf_update_data(db); 549 db->db_state = DB_CACHED; 550 mutex_exit(&db->db_mtx); 551 return; 552 } 553 554 /* 555 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync() 556 * processes the delete record and clears the bp while we are waiting 557 * for the dn_mtx (resulting in a "no" from block_freed). 558 */ 559 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) || 560 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) || 561 BP_IS_HOLE(db->db_blkptr)))) { 562 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 563 564 DB_DNODE_EXIT(db); 565 dbuf_set_data(db, arc_buf_alloc(db->db_objset->os_spa, 566 db->db.db_size, db, type)); 567 bzero(db->db.db_data, db->db.db_size); 568 db->db_state = DB_CACHED; 569 *flags |= DB_RF_CACHED; 570 mutex_exit(&db->db_mtx); 571 return; 572 } 573 574 DB_DNODE_EXIT(db); 575 576 db->db_state = DB_READ; 577 mutex_exit(&db->db_mtx); 578 579 if (DBUF_IS_L2CACHEABLE(db)) 580 aflags |= ARC_L2CACHE; 581 if (DBUF_IS_L2COMPRESSIBLE(db)) 582 aflags |= ARC_L2COMPRESS; 583 584 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ? 585 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET, 586 db->db.db_object, db->db_level, db->db_blkid); 587 588 dbuf_add_ref(db, NULL); 589 590 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr, 591 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, 592 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED, 593 &aflags, &zb); 594 if (aflags & ARC_CACHED) 595 *flags |= DB_RF_CACHED; 596 } 597 598 int 599 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 600 { 601 int err = 0; 602 boolean_t havepzio = (zio != NULL); 603 boolean_t prefetch; 604 dnode_t *dn; 605 606 /* 607 * We don't have to hold the mutex to check db_state because it 608 * can't be freed while we have a hold on the buffer. 609 */ 610 ASSERT(!refcount_is_zero(&db->db_holds)); 611 612 if (db->db_state == DB_NOFILL) 613 return (SET_ERROR(EIO)); 614 615 DB_DNODE_ENTER(db); 616 dn = DB_DNODE(db); 617 if ((flags & DB_RF_HAVESTRUCT) == 0) 618 rw_enter(&dn->dn_struct_rwlock, RW_READER); 619 620 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 621 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL && 622 DBUF_IS_CACHEABLE(db); 623 624 mutex_enter(&db->db_mtx); 625 if (db->db_state == DB_CACHED) { 626 mutex_exit(&db->db_mtx); 627 if (prefetch) 628 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset, 629 db->db.db_size, TRUE); 630 if ((flags & DB_RF_HAVESTRUCT) == 0) 631 rw_exit(&dn->dn_struct_rwlock); 632 DB_DNODE_EXIT(db); 633 } else if (db->db_state == DB_UNCACHED) { 634 spa_t *spa = dn->dn_objset->os_spa; 635 636 if (zio == NULL) 637 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 638 dbuf_read_impl(db, zio, &flags); 639 640 /* dbuf_read_impl has dropped db_mtx for us */ 641 642 if (prefetch) 643 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset, 644 db->db.db_size, flags & DB_RF_CACHED); 645 646 if ((flags & DB_RF_HAVESTRUCT) == 0) 647 rw_exit(&dn->dn_struct_rwlock); 648 DB_DNODE_EXIT(db); 649 650 if (!havepzio) 651 err = zio_wait(zio); 652 } else { 653 /* 654 * Another reader came in while the dbuf was in flight 655 * between UNCACHED and CACHED. Either a writer will finish 656 * writing the buffer (sending the dbuf to CACHED) or the 657 * first reader's request will reach the read_done callback 658 * and send the dbuf to CACHED. Otherwise, a failure 659 * occurred and the dbuf went to UNCACHED. 660 */ 661 mutex_exit(&db->db_mtx); 662 if (prefetch) 663 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset, 664 db->db.db_size, TRUE); 665 if ((flags & DB_RF_HAVESTRUCT) == 0) 666 rw_exit(&dn->dn_struct_rwlock); 667 DB_DNODE_EXIT(db); 668 669 /* Skip the wait per the caller's request. */ 670 mutex_enter(&db->db_mtx); 671 if ((flags & DB_RF_NEVERWAIT) == 0) { 672 while (db->db_state == DB_READ || 673 db->db_state == DB_FILL) { 674 ASSERT(db->db_state == DB_READ || 675 (flags & DB_RF_HAVESTRUCT) == 0); 676 cv_wait(&db->db_changed, &db->db_mtx); 677 } 678 if (db->db_state == DB_UNCACHED) 679 err = SET_ERROR(EIO); 680 } 681 mutex_exit(&db->db_mtx); 682 } 683 684 ASSERT(err || havepzio || db->db_state == DB_CACHED); 685 return (err); 686 } 687 688 static void 689 dbuf_noread(dmu_buf_impl_t *db) 690 { 691 ASSERT(!refcount_is_zero(&db->db_holds)); 692 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 693 mutex_enter(&db->db_mtx); 694 while (db->db_state == DB_READ || db->db_state == DB_FILL) 695 cv_wait(&db->db_changed, &db->db_mtx); 696 if (db->db_state == DB_UNCACHED) { 697 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 698 spa_t *spa = db->db_objset->os_spa; 699 700 ASSERT(db->db_buf == NULL); 701 ASSERT(db->db.db_data == NULL); 702 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type)); 703 db->db_state = DB_FILL; 704 } else if (db->db_state == DB_NOFILL) { 705 dbuf_set_data(db, NULL); 706 } else { 707 ASSERT3U(db->db_state, ==, DB_CACHED); 708 } 709 mutex_exit(&db->db_mtx); 710 } 711 712 /* 713 * This is our just-in-time copy function. It makes a copy of 714 * buffers, that have been modified in a previous transaction 715 * group, before we modify them in the current active group. 716 * 717 * This function is used in two places: when we are dirtying a 718 * buffer for the first time in a txg, and when we are freeing 719 * a range in a dnode that includes this buffer. 720 * 721 * Note that when we are called from dbuf_free_range() we do 722 * not put a hold on the buffer, we just traverse the active 723 * dbuf list for the dnode. 724 */ 725 static void 726 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg) 727 { 728 dbuf_dirty_record_t *dr = db->db_last_dirty; 729 730 ASSERT(MUTEX_HELD(&db->db_mtx)); 731 ASSERT(db->db.db_data != NULL); 732 ASSERT(db->db_level == 0); 733 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT); 734 735 if (dr == NULL || 736 (dr->dt.dl.dr_data != 737 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf))) 738 return; 739 740 /* 741 * If the last dirty record for this dbuf has not yet synced 742 * and its referencing the dbuf data, either: 743 * reset the reference to point to a new copy, 744 * or (if there a no active holders) 745 * just null out the current db_data pointer. 746 */ 747 ASSERT(dr->dr_txg >= txg - 2); 748 if (db->db_blkid == DMU_BONUS_BLKID) { 749 /* Note that the data bufs here are zio_bufs */ 750 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN); 751 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 752 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN); 753 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) { 754 int size = db->db.db_size; 755 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 756 spa_t *spa = db->db_objset->os_spa; 757 758 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type); 759 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size); 760 } else { 761 dbuf_set_data(db, NULL); 762 } 763 } 764 765 void 766 dbuf_unoverride(dbuf_dirty_record_t *dr) 767 { 768 dmu_buf_impl_t *db = dr->dr_dbuf; 769 blkptr_t *bp = &dr->dt.dl.dr_overridden_by; 770 uint64_t txg = dr->dr_txg; 771 772 ASSERT(MUTEX_HELD(&db->db_mtx)); 773 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC); 774 ASSERT(db->db_level == 0); 775 776 if (db->db_blkid == DMU_BONUS_BLKID || 777 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN) 778 return; 779 780 ASSERT(db->db_data_pending != dr); 781 782 /* free this block */ 783 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) 784 zio_free(db->db_objset->os_spa, txg, bp); 785 786 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 787 dr->dt.dl.dr_nopwrite = B_FALSE; 788 789 /* 790 * Release the already-written buffer, so we leave it in 791 * a consistent dirty state. Note that all callers are 792 * modifying the buffer, so they will immediately do 793 * another (redundant) arc_release(). Therefore, leave 794 * the buf thawed to save the effort of freezing & 795 * immediately re-thawing it. 796 */ 797 arc_release(dr->dt.dl.dr_data, db); 798 } 799 800 /* 801 * Evict (if its unreferenced) or clear (if its referenced) any level-0 802 * data blocks in the free range, so that any future readers will find 803 * empty blocks. 804 * 805 * This is a no-op if the dataset is in the middle of an incremental 806 * receive; see comment below for details. 807 */ 808 void 809 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid, 810 dmu_tx_t *tx) 811 { 812 dmu_buf_impl_t *db, *db_next, db_search; 813 uint64_t txg = tx->tx_txg; 814 avl_index_t where; 815 816 if (end_blkid > dn->dn_maxblkid && (end_blkid != DMU_SPILL_BLKID)) 817 end_blkid = dn->dn_maxblkid; 818 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid); 819 820 db_search.db_level = 0; 821 db_search.db_blkid = start_blkid; 822 db_search.db_creation = 0; 823 824 mutex_enter(&dn->dn_dbufs_mtx); 825 if (start_blkid >= dn->dn_unlisted_l0_blkid) { 826 /* There can't be any dbufs in this range; no need to search. */ 827 #ifdef DEBUG 828 db = avl_find(&dn->dn_dbufs, &db_search, &where); 829 ASSERT3P(db, ==, NULL); 830 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 831 ASSERT(db == NULL || db->db_level > 0); 832 #endif 833 mutex_exit(&dn->dn_dbufs_mtx); 834 return; 835 } else if (dmu_objset_is_receiving(dn->dn_objset)) { 836 /* 837 * If we are receiving, we expect there to be no dbufs in 838 * the range to be freed, because receive modifies each 839 * block at most once, and in offset order. If this is 840 * not the case, it can lead to performance problems, 841 * so note that we unexpectedly took the slow path. 842 */ 843 atomic_inc_64(&zfs_free_range_recv_miss); 844 } 845 846 db = avl_find(&dn->dn_dbufs, &db_search, &where); 847 ASSERT3P(db, ==, NULL); 848 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 849 850 for (; db != NULL; db = db_next) { 851 db_next = AVL_NEXT(&dn->dn_dbufs, db); 852 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 853 854 if (db->db_level != 0 || db->db_blkid > end_blkid) { 855 break; 856 } 857 ASSERT3U(db->db_blkid, >=, start_blkid); 858 859 /* found a level 0 buffer in the range */ 860 mutex_enter(&db->db_mtx); 861 if (dbuf_undirty(db, tx)) { 862 /* mutex has been dropped and dbuf destroyed */ 863 continue; 864 } 865 866 if (db->db_state == DB_UNCACHED || 867 db->db_state == DB_NOFILL || 868 db->db_state == DB_EVICTING) { 869 ASSERT(db->db.db_data == NULL); 870 mutex_exit(&db->db_mtx); 871 continue; 872 } 873 if (db->db_state == DB_READ || db->db_state == DB_FILL) { 874 /* will be handled in dbuf_read_done or dbuf_rele */ 875 db->db_freed_in_flight = TRUE; 876 mutex_exit(&db->db_mtx); 877 continue; 878 } 879 if (refcount_count(&db->db_holds) == 0) { 880 ASSERT(db->db_buf); 881 dbuf_clear(db); 882 continue; 883 } 884 /* The dbuf is referenced */ 885 886 if (db->db_last_dirty != NULL) { 887 dbuf_dirty_record_t *dr = db->db_last_dirty; 888 889 if (dr->dr_txg == txg) { 890 /* 891 * This buffer is "in-use", re-adjust the file 892 * size to reflect that this buffer may 893 * contain new data when we sync. 894 */ 895 if (db->db_blkid != DMU_SPILL_BLKID && 896 db->db_blkid > dn->dn_maxblkid) 897 dn->dn_maxblkid = db->db_blkid; 898 dbuf_unoverride(dr); 899 } else { 900 /* 901 * This dbuf is not dirty in the open context. 902 * Either uncache it (if its not referenced in 903 * the open context) or reset its contents to 904 * empty. 905 */ 906 dbuf_fix_old_data(db, txg); 907 } 908 } 909 /* clear the contents if its cached */ 910 if (db->db_state == DB_CACHED) { 911 ASSERT(db->db.db_data != NULL); 912 arc_release(db->db_buf, db); 913 bzero(db->db.db_data, db->db.db_size); 914 arc_buf_freeze(db->db_buf); 915 } 916 917 mutex_exit(&db->db_mtx); 918 } 919 mutex_exit(&dn->dn_dbufs_mtx); 920 } 921 922 static int 923 dbuf_block_freeable(dmu_buf_impl_t *db) 924 { 925 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset; 926 uint64_t birth_txg = 0; 927 928 /* 929 * We don't need any locking to protect db_blkptr: 930 * If it's syncing, then db_last_dirty will be set 931 * so we'll ignore db_blkptr. 932 * 933 * This logic ensures that only block births for 934 * filled blocks are considered. 935 */ 936 ASSERT(MUTEX_HELD(&db->db_mtx)); 937 if (db->db_last_dirty && (db->db_blkptr == NULL || 938 !BP_IS_HOLE(db->db_blkptr))) { 939 birth_txg = db->db_last_dirty->dr_txg; 940 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) { 941 birth_txg = db->db_blkptr->blk_birth; 942 } 943 944 /* 945 * If this block don't exist or is in a snapshot, it can't be freed. 946 * Don't pass the bp to dsl_dataset_block_freeable() since we 947 * are holding the db_mtx lock and might deadlock if we are 948 * prefetching a dedup-ed block. 949 */ 950 if (birth_txg != 0) 951 return (ds == NULL || 952 dsl_dataset_block_freeable(ds, NULL, birth_txg)); 953 else 954 return (B_FALSE); 955 } 956 957 void 958 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx) 959 { 960 arc_buf_t *buf, *obuf; 961 int osize = db->db.db_size; 962 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 963 dnode_t *dn; 964 965 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 966 967 DB_DNODE_ENTER(db); 968 dn = DB_DNODE(db); 969 970 /* XXX does *this* func really need the lock? */ 971 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 972 973 /* 974 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held 975 * is OK, because there can be no other references to the db 976 * when we are changing its size, so no concurrent DB_FILL can 977 * be happening. 978 */ 979 /* 980 * XXX we should be doing a dbuf_read, checking the return 981 * value and returning that up to our callers 982 */ 983 dmu_buf_will_dirty(&db->db, tx); 984 985 /* create the data buffer for the new block */ 986 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type); 987 988 /* copy old block data to the new block */ 989 obuf = db->db_buf; 990 bcopy(obuf->b_data, buf->b_data, MIN(osize, size)); 991 /* zero the remainder */ 992 if (size > osize) 993 bzero((uint8_t *)buf->b_data + osize, size - osize); 994 995 mutex_enter(&db->db_mtx); 996 dbuf_set_data(db, buf); 997 VERIFY(arc_buf_remove_ref(obuf, db)); 998 db->db.db_size = size; 999 1000 if (db->db_level == 0) { 1001 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg); 1002 db->db_last_dirty->dt.dl.dr_data = buf; 1003 } 1004 mutex_exit(&db->db_mtx); 1005 1006 dnode_willuse_space(dn, size-osize, tx); 1007 DB_DNODE_EXIT(db); 1008 } 1009 1010 void 1011 dbuf_release_bp(dmu_buf_impl_t *db) 1012 { 1013 objset_t *os = db->db_objset; 1014 1015 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 1016 ASSERT(arc_released(os->os_phys_buf) || 1017 list_link_active(&os->os_dsl_dataset->ds_synced_link)); 1018 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf)); 1019 1020 (void) arc_release(db->db_buf, db); 1021 } 1022 1023 dbuf_dirty_record_t * 1024 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1025 { 1026 dnode_t *dn; 1027 objset_t *os; 1028 dbuf_dirty_record_t **drp, *dr; 1029 int drop_struct_lock = FALSE; 1030 boolean_t do_free_accounting = B_FALSE; 1031 int txgoff = tx->tx_txg & TXG_MASK; 1032 1033 ASSERT(tx->tx_txg != 0); 1034 ASSERT(!refcount_is_zero(&db->db_holds)); 1035 DMU_TX_DIRTY_BUF(tx, db); 1036 1037 DB_DNODE_ENTER(db); 1038 dn = DB_DNODE(db); 1039 /* 1040 * Shouldn't dirty a regular buffer in syncing context. Private 1041 * objects may be dirtied in syncing context, but only if they 1042 * were already pre-dirtied in open context. 1043 */ 1044 ASSERT(!dmu_tx_is_syncing(tx) || 1045 BP_IS_HOLE(dn->dn_objset->os_rootbp) || 1046 DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 1047 dn->dn_objset->os_dsl_dataset == NULL); 1048 /* 1049 * We make this assert for private objects as well, but after we 1050 * check if we're already dirty. They are allowed to re-dirty 1051 * in syncing context. 1052 */ 1053 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 1054 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 1055 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 1056 1057 mutex_enter(&db->db_mtx); 1058 /* 1059 * XXX make this true for indirects too? The problem is that 1060 * transactions created with dmu_tx_create_assigned() from 1061 * syncing context don't bother holding ahead. 1062 */ 1063 ASSERT(db->db_level != 0 || 1064 db->db_state == DB_CACHED || db->db_state == DB_FILL || 1065 db->db_state == DB_NOFILL); 1066 1067 mutex_enter(&dn->dn_mtx); 1068 /* 1069 * Don't set dirtyctx to SYNC if we're just modifying this as we 1070 * initialize the objset. 1071 */ 1072 if (dn->dn_dirtyctx == DN_UNDIRTIED && 1073 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) { 1074 dn->dn_dirtyctx = 1075 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN); 1076 ASSERT(dn->dn_dirtyctx_firstset == NULL); 1077 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP); 1078 } 1079 mutex_exit(&dn->dn_mtx); 1080 1081 if (db->db_blkid == DMU_SPILL_BLKID) 1082 dn->dn_have_spill = B_TRUE; 1083 1084 /* 1085 * If this buffer is already dirty, we're done. 1086 */ 1087 drp = &db->db_last_dirty; 1088 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg || 1089 db->db.db_object == DMU_META_DNODE_OBJECT); 1090 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg) 1091 drp = &dr->dr_next; 1092 if (dr && dr->dr_txg == tx->tx_txg) { 1093 DB_DNODE_EXIT(db); 1094 1095 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) { 1096 /* 1097 * If this buffer has already been written out, 1098 * we now need to reset its state. 1099 */ 1100 dbuf_unoverride(dr); 1101 if (db->db.db_object != DMU_META_DNODE_OBJECT && 1102 db->db_state != DB_NOFILL) 1103 arc_buf_thaw(db->db_buf); 1104 } 1105 mutex_exit(&db->db_mtx); 1106 return (dr); 1107 } 1108 1109 /* 1110 * Only valid if not already dirty. 1111 */ 1112 ASSERT(dn->dn_object == 0 || 1113 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 1114 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 1115 1116 ASSERT3U(dn->dn_nlevels, >, db->db_level); 1117 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) || 1118 dn->dn_phys->dn_nlevels > db->db_level || 1119 dn->dn_next_nlevels[txgoff] > db->db_level || 1120 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level || 1121 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level); 1122 1123 /* 1124 * We should only be dirtying in syncing context if it's the 1125 * mos or we're initializing the os or it's a special object. 1126 * However, we are allowed to dirty in syncing context provided 1127 * we already dirtied it in open context. Hence we must make 1128 * this assertion only if we're not already dirty. 1129 */ 1130 os = dn->dn_objset; 1131 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 1132 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp)); 1133 ASSERT(db->db.db_size != 0); 1134 1135 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 1136 1137 if (db->db_blkid != DMU_BONUS_BLKID) { 1138 /* 1139 * Update the accounting. 1140 * Note: we delay "free accounting" until after we drop 1141 * the db_mtx. This keeps us from grabbing other locks 1142 * (and possibly deadlocking) in bp_get_dsize() while 1143 * also holding the db_mtx. 1144 */ 1145 dnode_willuse_space(dn, db->db.db_size, tx); 1146 do_free_accounting = dbuf_block_freeable(db); 1147 } 1148 1149 /* 1150 * If this buffer is dirty in an old transaction group we need 1151 * to make a copy of it so that the changes we make in this 1152 * transaction group won't leak out when we sync the older txg. 1153 */ 1154 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP); 1155 if (db->db_level == 0) { 1156 void *data_old = db->db_buf; 1157 1158 if (db->db_state != DB_NOFILL) { 1159 if (db->db_blkid == DMU_BONUS_BLKID) { 1160 dbuf_fix_old_data(db, tx->tx_txg); 1161 data_old = db->db.db_data; 1162 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) { 1163 /* 1164 * Release the data buffer from the cache so 1165 * that we can modify it without impacting 1166 * possible other users of this cached data 1167 * block. Note that indirect blocks and 1168 * private objects are not released until the 1169 * syncing state (since they are only modified 1170 * then). 1171 */ 1172 arc_release(db->db_buf, db); 1173 dbuf_fix_old_data(db, tx->tx_txg); 1174 data_old = db->db_buf; 1175 } 1176 ASSERT(data_old != NULL); 1177 } 1178 dr->dt.dl.dr_data = data_old; 1179 } else { 1180 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL); 1181 list_create(&dr->dt.di.dr_children, 1182 sizeof (dbuf_dirty_record_t), 1183 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 1184 } 1185 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL) 1186 dr->dr_accounted = db->db.db_size; 1187 dr->dr_dbuf = db; 1188 dr->dr_txg = tx->tx_txg; 1189 dr->dr_next = *drp; 1190 *drp = dr; 1191 1192 /* 1193 * We could have been freed_in_flight between the dbuf_noread 1194 * and dbuf_dirty. We win, as though the dbuf_noread() had 1195 * happened after the free. 1196 */ 1197 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1198 db->db_blkid != DMU_SPILL_BLKID) { 1199 mutex_enter(&dn->dn_mtx); 1200 if (dn->dn_free_ranges[txgoff] != NULL) { 1201 range_tree_clear(dn->dn_free_ranges[txgoff], 1202 db->db_blkid, 1); 1203 } 1204 mutex_exit(&dn->dn_mtx); 1205 db->db_freed_in_flight = FALSE; 1206 } 1207 1208 /* 1209 * This buffer is now part of this txg 1210 */ 1211 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg); 1212 db->db_dirtycnt += 1; 1213 ASSERT3U(db->db_dirtycnt, <=, 3); 1214 1215 mutex_exit(&db->db_mtx); 1216 1217 if (db->db_blkid == DMU_BONUS_BLKID || 1218 db->db_blkid == DMU_SPILL_BLKID) { 1219 mutex_enter(&dn->dn_mtx); 1220 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1221 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1222 mutex_exit(&dn->dn_mtx); 1223 dnode_setdirty(dn, tx); 1224 DB_DNODE_EXIT(db); 1225 return (dr); 1226 } else if (do_free_accounting) { 1227 blkptr_t *bp = db->db_blkptr; 1228 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ? 1229 bp_get_dsize(os->os_spa, bp) : db->db.db_size; 1230 /* 1231 * This is only a guess -- if the dbuf is dirty 1232 * in a previous txg, we don't know how much 1233 * space it will use on disk yet. We should 1234 * really have the struct_rwlock to access 1235 * db_blkptr, but since this is just a guess, 1236 * it's OK if we get an odd answer. 1237 */ 1238 ddt_prefetch(os->os_spa, bp); 1239 dnode_willuse_space(dn, -willfree, tx); 1240 } 1241 1242 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 1243 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1244 drop_struct_lock = TRUE; 1245 } 1246 1247 if (db->db_level == 0) { 1248 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock); 1249 ASSERT(dn->dn_maxblkid >= db->db_blkid); 1250 } 1251 1252 if (db->db_level+1 < dn->dn_nlevels) { 1253 dmu_buf_impl_t *parent = db->db_parent; 1254 dbuf_dirty_record_t *di; 1255 int parent_held = FALSE; 1256 1257 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) { 1258 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1259 1260 parent = dbuf_hold_level(dn, db->db_level+1, 1261 db->db_blkid >> epbs, FTAG); 1262 ASSERT(parent != NULL); 1263 parent_held = TRUE; 1264 } 1265 if (drop_struct_lock) 1266 rw_exit(&dn->dn_struct_rwlock); 1267 ASSERT3U(db->db_level+1, ==, parent->db_level); 1268 di = dbuf_dirty(parent, tx); 1269 if (parent_held) 1270 dbuf_rele(parent, FTAG); 1271 1272 mutex_enter(&db->db_mtx); 1273 /* 1274 * Since we've dropped the mutex, it's possible that 1275 * dbuf_undirty() might have changed this out from under us. 1276 */ 1277 if (db->db_last_dirty == dr || 1278 dn->dn_object == DMU_META_DNODE_OBJECT) { 1279 mutex_enter(&di->dt.di.dr_mtx); 1280 ASSERT3U(di->dr_txg, ==, tx->tx_txg); 1281 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1282 list_insert_tail(&di->dt.di.dr_children, dr); 1283 mutex_exit(&di->dt.di.dr_mtx); 1284 dr->dr_parent = di; 1285 } 1286 mutex_exit(&db->db_mtx); 1287 } else { 1288 ASSERT(db->db_level+1 == dn->dn_nlevels); 1289 ASSERT(db->db_blkid < dn->dn_nblkptr); 1290 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf); 1291 mutex_enter(&dn->dn_mtx); 1292 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1293 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1294 mutex_exit(&dn->dn_mtx); 1295 if (drop_struct_lock) 1296 rw_exit(&dn->dn_struct_rwlock); 1297 } 1298 1299 dnode_setdirty(dn, tx); 1300 DB_DNODE_EXIT(db); 1301 return (dr); 1302 } 1303 1304 /* 1305 * Undirty a buffer in the transaction group referenced by the given 1306 * transaction. Return whether this evicted the dbuf. 1307 */ 1308 static boolean_t 1309 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1310 { 1311 dnode_t *dn; 1312 uint64_t txg = tx->tx_txg; 1313 dbuf_dirty_record_t *dr, **drp; 1314 1315 ASSERT(txg != 0); 1316 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1317 ASSERT0(db->db_level); 1318 ASSERT(MUTEX_HELD(&db->db_mtx)); 1319 1320 /* 1321 * If this buffer is not dirty, we're done. 1322 */ 1323 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1324 if (dr->dr_txg <= txg) 1325 break; 1326 if (dr == NULL || dr->dr_txg < txg) 1327 return (B_FALSE); 1328 ASSERT(dr->dr_txg == txg); 1329 ASSERT(dr->dr_dbuf == db); 1330 1331 DB_DNODE_ENTER(db); 1332 dn = DB_DNODE(db); 1333 1334 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 1335 1336 ASSERT(db->db.db_size != 0); 1337 1338 /* 1339 * Any space we accounted for in dp_dirty_* will be cleaned up by 1340 * dsl_pool_sync(). This is relatively rare so the discrepancy 1341 * is not a big deal. 1342 */ 1343 1344 *drp = dr->dr_next; 1345 1346 /* 1347 * Note that there are three places in dbuf_dirty() 1348 * where this dirty record may be put on a list. 1349 * Make sure to do a list_remove corresponding to 1350 * every one of those list_insert calls. 1351 */ 1352 if (dr->dr_parent) { 1353 mutex_enter(&dr->dr_parent->dt.di.dr_mtx); 1354 list_remove(&dr->dr_parent->dt.di.dr_children, dr); 1355 mutex_exit(&dr->dr_parent->dt.di.dr_mtx); 1356 } else if (db->db_blkid == DMU_SPILL_BLKID || 1357 db->db_level+1 == dn->dn_nlevels) { 1358 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf); 1359 mutex_enter(&dn->dn_mtx); 1360 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr); 1361 mutex_exit(&dn->dn_mtx); 1362 } 1363 DB_DNODE_EXIT(db); 1364 1365 if (db->db_state != DB_NOFILL) { 1366 dbuf_unoverride(dr); 1367 1368 ASSERT(db->db_buf != NULL); 1369 ASSERT(dr->dt.dl.dr_data != NULL); 1370 if (dr->dt.dl.dr_data != db->db_buf) 1371 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db)); 1372 } 1373 1374 if (db->db_level != 0) { 1375 mutex_destroy(&dr->dt.di.dr_mtx); 1376 list_destroy(&dr->dt.di.dr_children); 1377 } 1378 1379 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 1380 1381 ASSERT(db->db_dirtycnt > 0); 1382 db->db_dirtycnt -= 1; 1383 1384 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) { 1385 arc_buf_t *buf = db->db_buf; 1386 1387 ASSERT(db->db_state == DB_NOFILL || arc_released(buf)); 1388 dbuf_set_data(db, NULL); 1389 VERIFY(arc_buf_remove_ref(buf, db)); 1390 dbuf_evict(db); 1391 return (B_TRUE); 1392 } 1393 1394 return (B_FALSE); 1395 } 1396 1397 void 1398 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 1399 { 1400 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1401 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH; 1402 1403 ASSERT(tx->tx_txg != 0); 1404 ASSERT(!refcount_is_zero(&db->db_holds)); 1405 1406 DB_DNODE_ENTER(db); 1407 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock)) 1408 rf |= DB_RF_HAVESTRUCT; 1409 DB_DNODE_EXIT(db); 1410 (void) dbuf_read(db, NULL, rf); 1411 (void) dbuf_dirty(db, tx); 1412 } 1413 1414 void 1415 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 1416 { 1417 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1418 1419 db->db_state = DB_NOFILL; 1420 1421 dmu_buf_will_fill(db_fake, tx); 1422 } 1423 1424 void 1425 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 1426 { 1427 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1428 1429 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1430 ASSERT(tx->tx_txg != 0); 1431 ASSERT(db->db_level == 0); 1432 ASSERT(!refcount_is_zero(&db->db_holds)); 1433 1434 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT || 1435 dmu_tx_private_ok(tx)); 1436 1437 dbuf_noread(db); 1438 (void) dbuf_dirty(db, tx); 1439 } 1440 1441 #pragma weak dmu_buf_fill_done = dbuf_fill_done 1442 /* ARGSUSED */ 1443 void 1444 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx) 1445 { 1446 mutex_enter(&db->db_mtx); 1447 DBUF_VERIFY(db); 1448 1449 if (db->db_state == DB_FILL) { 1450 if (db->db_level == 0 && db->db_freed_in_flight) { 1451 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1452 /* we were freed while filling */ 1453 /* XXX dbuf_undirty? */ 1454 bzero(db->db.db_data, db->db.db_size); 1455 db->db_freed_in_flight = FALSE; 1456 } 1457 db->db_state = DB_CACHED; 1458 cv_broadcast(&db->db_changed); 1459 } 1460 mutex_exit(&db->db_mtx); 1461 } 1462 1463 void 1464 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data, 1465 bp_embedded_type_t etype, enum zio_compress comp, 1466 int uncompressed_size, int compressed_size, int byteorder, 1467 dmu_tx_t *tx) 1468 { 1469 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 1470 struct dirty_leaf *dl; 1471 dmu_object_type_t type; 1472 1473 DB_DNODE_ENTER(db); 1474 type = DB_DNODE(db)->dn_type; 1475 DB_DNODE_EXIT(db); 1476 1477 ASSERT0(db->db_level); 1478 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1479 1480 dmu_buf_will_not_fill(dbuf, tx); 1481 1482 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg); 1483 dl = &db->db_last_dirty->dt.dl; 1484 encode_embedded_bp_compressed(&dl->dr_overridden_by, 1485 data, comp, uncompressed_size, compressed_size); 1486 BPE_SET_ETYPE(&dl->dr_overridden_by, etype); 1487 BP_SET_TYPE(&dl->dr_overridden_by, type); 1488 BP_SET_LEVEL(&dl->dr_overridden_by, 0); 1489 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder); 1490 1491 dl->dr_override_state = DR_OVERRIDDEN; 1492 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg; 1493 } 1494 1495 /* 1496 * Directly assign a provided arc buf to a given dbuf if it's not referenced 1497 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf. 1498 */ 1499 void 1500 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx) 1501 { 1502 ASSERT(!refcount_is_zero(&db->db_holds)); 1503 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1504 ASSERT(db->db_level == 0); 1505 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA); 1506 ASSERT(buf != NULL); 1507 ASSERT(arc_buf_size(buf) == db->db.db_size); 1508 ASSERT(tx->tx_txg != 0); 1509 1510 arc_return_buf(buf, db); 1511 ASSERT(arc_released(buf)); 1512 1513 mutex_enter(&db->db_mtx); 1514 1515 while (db->db_state == DB_READ || db->db_state == DB_FILL) 1516 cv_wait(&db->db_changed, &db->db_mtx); 1517 1518 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED); 1519 1520 if (db->db_state == DB_CACHED && 1521 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) { 1522 mutex_exit(&db->db_mtx); 1523 (void) dbuf_dirty(db, tx); 1524 bcopy(buf->b_data, db->db.db_data, db->db.db_size); 1525 VERIFY(arc_buf_remove_ref(buf, db)); 1526 xuio_stat_wbuf_copied(); 1527 return; 1528 } 1529 1530 xuio_stat_wbuf_nocopy(); 1531 if (db->db_state == DB_CACHED) { 1532 dbuf_dirty_record_t *dr = db->db_last_dirty; 1533 1534 ASSERT(db->db_buf != NULL); 1535 if (dr != NULL && dr->dr_txg == tx->tx_txg) { 1536 ASSERT(dr->dt.dl.dr_data == db->db_buf); 1537 if (!arc_released(db->db_buf)) { 1538 ASSERT(dr->dt.dl.dr_override_state == 1539 DR_OVERRIDDEN); 1540 arc_release(db->db_buf, db); 1541 } 1542 dr->dt.dl.dr_data = buf; 1543 VERIFY(arc_buf_remove_ref(db->db_buf, db)); 1544 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) { 1545 arc_release(db->db_buf, db); 1546 VERIFY(arc_buf_remove_ref(db->db_buf, db)); 1547 } 1548 db->db_buf = NULL; 1549 } 1550 ASSERT(db->db_buf == NULL); 1551 dbuf_set_data(db, buf); 1552 db->db_state = DB_FILL; 1553 mutex_exit(&db->db_mtx); 1554 (void) dbuf_dirty(db, tx); 1555 dmu_buf_fill_done(&db->db, tx); 1556 } 1557 1558 /* 1559 * "Clear" the contents of this dbuf. This will mark the dbuf 1560 * EVICTING and clear *most* of its references. Unfortunately, 1561 * when we are not holding the dn_dbufs_mtx, we can't clear the 1562 * entry in the dn_dbufs list. We have to wait until dbuf_destroy() 1563 * in this case. For callers from the DMU we will usually see: 1564 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy() 1565 * For the arc callback, we will usually see: 1566 * dbuf_do_evict()->dbuf_clear();dbuf_destroy() 1567 * Sometimes, though, we will get a mix of these two: 1568 * DMU: dbuf_clear()->arc_clear_callback() 1569 * ARC: dbuf_do_evict()->dbuf_destroy() 1570 * 1571 * This routine will dissociate the dbuf from the arc, by calling 1572 * arc_clear_callback(), but will not evict the data from the ARC. 1573 */ 1574 void 1575 dbuf_clear(dmu_buf_impl_t *db) 1576 { 1577 dnode_t *dn; 1578 dmu_buf_impl_t *parent = db->db_parent; 1579 dmu_buf_impl_t *dndb; 1580 boolean_t dbuf_gone = B_FALSE; 1581 1582 ASSERT(MUTEX_HELD(&db->db_mtx)); 1583 ASSERT(refcount_is_zero(&db->db_holds)); 1584 1585 dbuf_evict_user(db); 1586 1587 if (db->db_state == DB_CACHED) { 1588 ASSERT(db->db.db_data != NULL); 1589 if (db->db_blkid == DMU_BONUS_BLKID) { 1590 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN); 1591 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 1592 } 1593 db->db.db_data = NULL; 1594 db->db_state = DB_UNCACHED; 1595 } 1596 1597 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL); 1598 ASSERT(db->db_data_pending == NULL); 1599 1600 db->db_state = DB_EVICTING; 1601 db->db_blkptr = NULL; 1602 1603 DB_DNODE_ENTER(db); 1604 dn = DB_DNODE(db); 1605 dndb = dn->dn_dbuf; 1606 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) { 1607 avl_remove(&dn->dn_dbufs, db); 1608 atomic_dec_32(&dn->dn_dbufs_count); 1609 membar_producer(); 1610 DB_DNODE_EXIT(db); 1611 /* 1612 * Decrementing the dbuf count means that the hold corresponding 1613 * to the removed dbuf is no longer discounted in dnode_move(), 1614 * so the dnode cannot be moved until after we release the hold. 1615 * The membar_producer() ensures visibility of the decremented 1616 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually 1617 * release any lock. 1618 */ 1619 dnode_rele(dn, db); 1620 db->db_dnode_handle = NULL; 1621 } else { 1622 DB_DNODE_EXIT(db); 1623 } 1624 1625 if (db->db_buf) 1626 dbuf_gone = arc_clear_callback(db->db_buf); 1627 1628 if (!dbuf_gone) 1629 mutex_exit(&db->db_mtx); 1630 1631 /* 1632 * If this dbuf is referenced from an indirect dbuf, 1633 * decrement the ref count on the indirect dbuf. 1634 */ 1635 if (parent && parent != dndb) 1636 dbuf_rele(parent, db); 1637 } 1638 1639 static int 1640 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse, 1641 dmu_buf_impl_t **parentp, blkptr_t **bpp) 1642 { 1643 int nlevels, epbs; 1644 1645 *parentp = NULL; 1646 *bpp = NULL; 1647 1648 ASSERT(blkid != DMU_BONUS_BLKID); 1649 1650 if (blkid == DMU_SPILL_BLKID) { 1651 mutex_enter(&dn->dn_mtx); 1652 if (dn->dn_have_spill && 1653 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 1654 *bpp = &dn->dn_phys->dn_spill; 1655 else 1656 *bpp = NULL; 1657 dbuf_add_ref(dn->dn_dbuf, NULL); 1658 *parentp = dn->dn_dbuf; 1659 mutex_exit(&dn->dn_mtx); 1660 return (0); 1661 } 1662 1663 if (dn->dn_phys->dn_nlevels == 0) 1664 nlevels = 1; 1665 else 1666 nlevels = dn->dn_phys->dn_nlevels; 1667 1668 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1669 1670 ASSERT3U(level * epbs, <, 64); 1671 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1672 if (level >= nlevels || 1673 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) { 1674 /* the buffer has no parent yet */ 1675 return (SET_ERROR(ENOENT)); 1676 } else if (level < nlevels-1) { 1677 /* this block is referenced from an indirect block */ 1678 int err = dbuf_hold_impl(dn, level+1, 1679 blkid >> epbs, fail_sparse, NULL, parentp); 1680 if (err) 1681 return (err); 1682 err = dbuf_read(*parentp, NULL, 1683 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 1684 if (err) { 1685 dbuf_rele(*parentp, NULL); 1686 *parentp = NULL; 1687 return (err); 1688 } 1689 *bpp = ((blkptr_t *)(*parentp)->db.db_data) + 1690 (blkid & ((1ULL << epbs) - 1)); 1691 return (0); 1692 } else { 1693 /* the block is referenced from the dnode */ 1694 ASSERT3U(level, ==, nlevels-1); 1695 ASSERT(dn->dn_phys->dn_nblkptr == 0 || 1696 blkid < dn->dn_phys->dn_nblkptr); 1697 if (dn->dn_dbuf) { 1698 dbuf_add_ref(dn->dn_dbuf, NULL); 1699 *parentp = dn->dn_dbuf; 1700 } 1701 *bpp = &dn->dn_phys->dn_blkptr[blkid]; 1702 return (0); 1703 } 1704 } 1705 1706 static dmu_buf_impl_t * 1707 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid, 1708 dmu_buf_impl_t *parent, blkptr_t *blkptr) 1709 { 1710 objset_t *os = dn->dn_objset; 1711 dmu_buf_impl_t *db, *odb; 1712 1713 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1714 ASSERT(dn->dn_type != DMU_OT_NONE); 1715 1716 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP); 1717 1718 db->db_objset = os; 1719 db->db.db_object = dn->dn_object; 1720 db->db_level = level; 1721 db->db_blkid = blkid; 1722 db->db_last_dirty = NULL; 1723 db->db_dirtycnt = 0; 1724 db->db_dnode_handle = dn->dn_handle; 1725 db->db_parent = parent; 1726 db->db_blkptr = blkptr; 1727 1728 db->db_user_ptr = NULL; 1729 db->db_user_data_ptr_ptr = NULL; 1730 db->db_evict_func = NULL; 1731 db->db_immediate_evict = 0; 1732 db->db_freed_in_flight = 0; 1733 1734 if (blkid == DMU_BONUS_BLKID) { 1735 ASSERT3P(parent, ==, dn->dn_dbuf); 1736 db->db.db_size = DN_MAX_BONUSLEN - 1737 (dn->dn_nblkptr-1) * sizeof (blkptr_t); 1738 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 1739 db->db.db_offset = DMU_BONUS_BLKID; 1740 db->db_state = DB_UNCACHED; 1741 /* the bonus dbuf is not placed in the hash table */ 1742 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 1743 return (db); 1744 } else if (blkid == DMU_SPILL_BLKID) { 1745 db->db.db_size = (blkptr != NULL) ? 1746 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE; 1747 db->db.db_offset = 0; 1748 } else { 1749 int blocksize = 1750 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz; 1751 db->db.db_size = blocksize; 1752 db->db.db_offset = db->db_blkid * blocksize; 1753 } 1754 1755 /* 1756 * Hold the dn_dbufs_mtx while we get the new dbuf 1757 * in the hash table *and* added to the dbufs list. 1758 * This prevents a possible deadlock with someone 1759 * trying to look up this dbuf before its added to the 1760 * dn_dbufs list. 1761 */ 1762 mutex_enter(&dn->dn_dbufs_mtx); 1763 db->db_state = DB_EVICTING; 1764 if ((odb = dbuf_hash_insert(db)) != NULL) { 1765 /* someone else inserted it first */ 1766 kmem_cache_free(dbuf_cache, db); 1767 mutex_exit(&dn->dn_dbufs_mtx); 1768 return (odb); 1769 } 1770 avl_add(&dn->dn_dbufs, db); 1771 if (db->db_level == 0 && db->db_blkid >= 1772 dn->dn_unlisted_l0_blkid) 1773 dn->dn_unlisted_l0_blkid = db->db_blkid + 1; 1774 db->db_state = DB_UNCACHED; 1775 mutex_exit(&dn->dn_dbufs_mtx); 1776 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 1777 1778 if (parent && parent != dn->dn_dbuf) 1779 dbuf_add_ref(parent, db); 1780 1781 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 1782 refcount_count(&dn->dn_holds) > 0); 1783 (void) refcount_add(&dn->dn_holds, db); 1784 atomic_inc_32(&dn->dn_dbufs_count); 1785 1786 dprintf_dbuf(db, "db=%p\n", db); 1787 1788 return (db); 1789 } 1790 1791 static int 1792 dbuf_do_evict(void *private) 1793 { 1794 dmu_buf_impl_t *db = private; 1795 1796 if (!MUTEX_HELD(&db->db_mtx)) 1797 mutex_enter(&db->db_mtx); 1798 1799 ASSERT(refcount_is_zero(&db->db_holds)); 1800 1801 if (db->db_state != DB_EVICTING) { 1802 ASSERT(db->db_state == DB_CACHED); 1803 DBUF_VERIFY(db); 1804 db->db_buf = NULL; 1805 dbuf_evict(db); 1806 } else { 1807 mutex_exit(&db->db_mtx); 1808 dbuf_destroy(db); 1809 } 1810 return (0); 1811 } 1812 1813 static void 1814 dbuf_destroy(dmu_buf_impl_t *db) 1815 { 1816 ASSERT(refcount_is_zero(&db->db_holds)); 1817 1818 if (db->db_blkid != DMU_BONUS_BLKID) { 1819 /* 1820 * If this dbuf is still on the dn_dbufs list, 1821 * remove it from that list. 1822 */ 1823 if (db->db_dnode_handle != NULL) { 1824 dnode_t *dn; 1825 1826 DB_DNODE_ENTER(db); 1827 dn = DB_DNODE(db); 1828 mutex_enter(&dn->dn_dbufs_mtx); 1829 avl_remove(&dn->dn_dbufs, db); 1830 atomic_dec_32(&dn->dn_dbufs_count); 1831 mutex_exit(&dn->dn_dbufs_mtx); 1832 DB_DNODE_EXIT(db); 1833 /* 1834 * Decrementing the dbuf count means that the hold 1835 * corresponding to the removed dbuf is no longer 1836 * discounted in dnode_move(), so the dnode cannot be 1837 * moved until after we release the hold. 1838 */ 1839 dnode_rele(dn, db); 1840 db->db_dnode_handle = NULL; 1841 } 1842 dbuf_hash_remove(db); 1843 } 1844 db->db_parent = NULL; 1845 db->db_buf = NULL; 1846 1847 ASSERT(db->db.db_data == NULL); 1848 ASSERT(db->db_hash_next == NULL); 1849 ASSERT(db->db_blkptr == NULL); 1850 ASSERT(db->db_data_pending == NULL); 1851 1852 kmem_cache_free(dbuf_cache, db); 1853 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 1854 } 1855 1856 void 1857 dbuf_prefetch(dnode_t *dn, uint64_t blkid, zio_priority_t prio) 1858 { 1859 dmu_buf_impl_t *db = NULL; 1860 blkptr_t *bp = NULL; 1861 1862 ASSERT(blkid != DMU_BONUS_BLKID); 1863 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1864 1865 if (dnode_block_freed(dn, blkid)) 1866 return; 1867 1868 /* dbuf_find() returns with db_mtx held */ 1869 if (db = dbuf_find(dn, 0, blkid)) { 1870 /* 1871 * This dbuf is already in the cache. We assume that 1872 * it is already CACHED, or else about to be either 1873 * read or filled. 1874 */ 1875 mutex_exit(&db->db_mtx); 1876 return; 1877 } 1878 1879 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) { 1880 if (bp && !BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) { 1881 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 1882 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 1883 zbookmark_phys_t zb; 1884 1885 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 1886 dn->dn_object, 0, blkid); 1887 1888 (void) arc_read(NULL, dn->dn_objset->os_spa, 1889 bp, NULL, NULL, prio, 1890 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 1891 &aflags, &zb); 1892 } 1893 if (db) 1894 dbuf_rele(db, NULL); 1895 } 1896 } 1897 1898 /* 1899 * Returns with db_holds incremented, and db_mtx not held. 1900 * Note: dn_struct_rwlock must be held. 1901 */ 1902 int 1903 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse, 1904 void *tag, dmu_buf_impl_t **dbp) 1905 { 1906 dmu_buf_impl_t *db, *parent = NULL; 1907 1908 ASSERT(blkid != DMU_BONUS_BLKID); 1909 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1910 ASSERT3U(dn->dn_nlevels, >, level); 1911 1912 *dbp = NULL; 1913 top: 1914 /* dbuf_find() returns with db_mtx held */ 1915 db = dbuf_find(dn, level, blkid); 1916 1917 if (db == NULL) { 1918 blkptr_t *bp = NULL; 1919 int err; 1920 1921 ASSERT3P(parent, ==, NULL); 1922 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp); 1923 if (fail_sparse) { 1924 if (err == 0 && bp && BP_IS_HOLE(bp)) 1925 err = SET_ERROR(ENOENT); 1926 if (err) { 1927 if (parent) 1928 dbuf_rele(parent, NULL); 1929 return (err); 1930 } 1931 } 1932 if (err && err != ENOENT) 1933 return (err); 1934 db = dbuf_create(dn, level, blkid, parent, bp); 1935 } 1936 1937 if (db->db_buf && refcount_is_zero(&db->db_holds)) { 1938 arc_buf_add_ref(db->db_buf, db); 1939 if (db->db_buf->b_data == NULL) { 1940 dbuf_clear(db); 1941 if (parent) { 1942 dbuf_rele(parent, NULL); 1943 parent = NULL; 1944 } 1945 goto top; 1946 } 1947 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data); 1948 } 1949 1950 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf)); 1951 1952 /* 1953 * If this buffer is currently syncing out, and we are are 1954 * still referencing it from db_data, we need to make a copy 1955 * of it in case we decide we want to dirty it again in this txg. 1956 */ 1957 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1958 dn->dn_object != DMU_META_DNODE_OBJECT && 1959 db->db_state == DB_CACHED && db->db_data_pending) { 1960 dbuf_dirty_record_t *dr = db->db_data_pending; 1961 1962 if (dr->dt.dl.dr_data == db->db_buf) { 1963 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1964 1965 dbuf_set_data(db, 1966 arc_buf_alloc(dn->dn_objset->os_spa, 1967 db->db.db_size, db, type)); 1968 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data, 1969 db->db.db_size); 1970 } 1971 } 1972 1973 (void) refcount_add(&db->db_holds, tag); 1974 dbuf_update_data(db); 1975 DBUF_VERIFY(db); 1976 mutex_exit(&db->db_mtx); 1977 1978 /* NOTE: we can't rele the parent until after we drop the db_mtx */ 1979 if (parent) 1980 dbuf_rele(parent, NULL); 1981 1982 ASSERT3P(DB_DNODE(db), ==, dn); 1983 ASSERT3U(db->db_blkid, ==, blkid); 1984 ASSERT3U(db->db_level, ==, level); 1985 *dbp = db; 1986 1987 return (0); 1988 } 1989 1990 dmu_buf_impl_t * 1991 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag) 1992 { 1993 dmu_buf_impl_t *db; 1994 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db); 1995 return (err ? NULL : db); 1996 } 1997 1998 dmu_buf_impl_t * 1999 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag) 2000 { 2001 dmu_buf_impl_t *db; 2002 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db); 2003 return (err ? NULL : db); 2004 } 2005 2006 void 2007 dbuf_create_bonus(dnode_t *dn) 2008 { 2009 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 2010 2011 ASSERT(dn->dn_bonus == NULL); 2012 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL); 2013 } 2014 2015 int 2016 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx) 2017 { 2018 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2019 dnode_t *dn; 2020 2021 if (db->db_blkid != DMU_SPILL_BLKID) 2022 return (SET_ERROR(ENOTSUP)); 2023 if (blksz == 0) 2024 blksz = SPA_MINBLOCKSIZE; 2025 if (blksz > SPA_MAXBLOCKSIZE) 2026 blksz = SPA_MAXBLOCKSIZE; 2027 else 2028 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE); 2029 2030 DB_DNODE_ENTER(db); 2031 dn = DB_DNODE(db); 2032 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2033 dbuf_new_size(db, blksz, tx); 2034 rw_exit(&dn->dn_struct_rwlock); 2035 DB_DNODE_EXIT(db); 2036 2037 return (0); 2038 } 2039 2040 void 2041 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx) 2042 { 2043 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx); 2044 } 2045 2046 #pragma weak dmu_buf_add_ref = dbuf_add_ref 2047 void 2048 dbuf_add_ref(dmu_buf_impl_t *db, void *tag) 2049 { 2050 int64_t holds = refcount_add(&db->db_holds, tag); 2051 ASSERT(holds > 1); 2052 } 2053 2054 /* 2055 * If you call dbuf_rele() you had better not be referencing the dnode handle 2056 * unless you have some other direct or indirect hold on the dnode. (An indirect 2057 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.) 2058 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the 2059 * dnode's parent dbuf evicting its dnode handles. 2060 */ 2061 void 2062 dbuf_rele(dmu_buf_impl_t *db, void *tag) 2063 { 2064 mutex_enter(&db->db_mtx); 2065 dbuf_rele_and_unlock(db, tag); 2066 } 2067 2068 void 2069 dmu_buf_rele(dmu_buf_t *db, void *tag) 2070 { 2071 dbuf_rele((dmu_buf_impl_t *)db, tag); 2072 } 2073 2074 /* 2075 * dbuf_rele() for an already-locked dbuf. This is necessary to allow 2076 * db_dirtycnt and db_holds to be updated atomically. 2077 */ 2078 void 2079 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag) 2080 { 2081 int64_t holds; 2082 2083 ASSERT(MUTEX_HELD(&db->db_mtx)); 2084 DBUF_VERIFY(db); 2085 2086 /* 2087 * Remove the reference to the dbuf before removing its hold on the 2088 * dnode so we can guarantee in dnode_move() that a referenced bonus 2089 * buffer has a corresponding dnode hold. 2090 */ 2091 holds = refcount_remove(&db->db_holds, tag); 2092 ASSERT(holds >= 0); 2093 2094 /* 2095 * We can't freeze indirects if there is a possibility that they 2096 * may be modified in the current syncing context. 2097 */ 2098 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) 2099 arc_buf_freeze(db->db_buf); 2100 2101 if (holds == db->db_dirtycnt && 2102 db->db_level == 0 && db->db_immediate_evict) 2103 dbuf_evict_user(db); 2104 2105 if (holds == 0) { 2106 if (db->db_blkid == DMU_BONUS_BLKID) { 2107 mutex_exit(&db->db_mtx); 2108 2109 /* 2110 * If the dnode moves here, we cannot cross this barrier 2111 * until the move completes. 2112 */ 2113 DB_DNODE_ENTER(db); 2114 atomic_dec_32(&DB_DNODE(db)->dn_dbufs_count); 2115 DB_DNODE_EXIT(db); 2116 /* 2117 * The bonus buffer's dnode hold is no longer discounted 2118 * in dnode_move(). The dnode cannot move until after 2119 * the dnode_rele(). 2120 */ 2121 dnode_rele(DB_DNODE(db), db); 2122 } else if (db->db_buf == NULL) { 2123 /* 2124 * This is a special case: we never associated this 2125 * dbuf with any data allocated from the ARC. 2126 */ 2127 ASSERT(db->db_state == DB_UNCACHED || 2128 db->db_state == DB_NOFILL); 2129 dbuf_evict(db); 2130 } else if (arc_released(db->db_buf)) { 2131 arc_buf_t *buf = db->db_buf; 2132 /* 2133 * This dbuf has anonymous data associated with it. 2134 */ 2135 dbuf_set_data(db, NULL); 2136 VERIFY(arc_buf_remove_ref(buf, db)); 2137 dbuf_evict(db); 2138 } else { 2139 VERIFY(!arc_buf_remove_ref(db->db_buf, db)); 2140 2141 /* 2142 * A dbuf will be eligible for eviction if either the 2143 * 'primarycache' property is set or a duplicate 2144 * copy of this buffer is already cached in the arc. 2145 * 2146 * In the case of the 'primarycache' a buffer 2147 * is considered for eviction if it matches the 2148 * criteria set in the property. 2149 * 2150 * To decide if our buffer is considered a 2151 * duplicate, we must call into the arc to determine 2152 * if multiple buffers are referencing the same 2153 * block on-disk. If so, then we simply evict 2154 * ourselves. 2155 */ 2156 if (!DBUF_IS_CACHEABLE(db)) { 2157 if (db->db_blkptr != NULL && 2158 !BP_IS_HOLE(db->db_blkptr) && 2159 !BP_IS_EMBEDDED(db->db_blkptr)) { 2160 spa_t *spa = 2161 dmu_objset_spa(db->db_objset); 2162 blkptr_t bp = *db->db_blkptr; 2163 dbuf_clear(db); 2164 arc_freed(spa, &bp); 2165 } else { 2166 dbuf_clear(db); 2167 } 2168 } else if (arc_buf_eviction_needed(db->db_buf)) { 2169 dbuf_clear(db); 2170 } else { 2171 mutex_exit(&db->db_mtx); 2172 } 2173 } 2174 } else { 2175 mutex_exit(&db->db_mtx); 2176 } 2177 } 2178 2179 #pragma weak dmu_buf_refcount = dbuf_refcount 2180 uint64_t 2181 dbuf_refcount(dmu_buf_impl_t *db) 2182 { 2183 return (refcount_count(&db->db_holds)); 2184 } 2185 2186 void * 2187 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr, 2188 dmu_buf_evict_func_t *evict_func) 2189 { 2190 return (dmu_buf_update_user(db_fake, NULL, user_ptr, 2191 user_data_ptr_ptr, evict_func)); 2192 } 2193 2194 void * 2195 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr, 2196 dmu_buf_evict_func_t *evict_func) 2197 { 2198 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2199 2200 db->db_immediate_evict = TRUE; 2201 return (dmu_buf_update_user(db_fake, NULL, user_ptr, 2202 user_data_ptr_ptr, evict_func)); 2203 } 2204 2205 void * 2206 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr, 2207 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func) 2208 { 2209 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2210 ASSERT(db->db_level == 0); 2211 2212 ASSERT((user_ptr == NULL) == (evict_func == NULL)); 2213 2214 mutex_enter(&db->db_mtx); 2215 2216 if (db->db_user_ptr == old_user_ptr) { 2217 db->db_user_ptr = user_ptr; 2218 db->db_user_data_ptr_ptr = user_data_ptr_ptr; 2219 db->db_evict_func = evict_func; 2220 2221 dbuf_update_data(db); 2222 } else { 2223 old_user_ptr = db->db_user_ptr; 2224 } 2225 2226 mutex_exit(&db->db_mtx); 2227 return (old_user_ptr); 2228 } 2229 2230 void * 2231 dmu_buf_get_user(dmu_buf_t *db_fake) 2232 { 2233 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2234 ASSERT(!refcount_is_zero(&db->db_holds)); 2235 2236 return (db->db_user_ptr); 2237 } 2238 2239 boolean_t 2240 dmu_buf_freeable(dmu_buf_t *dbuf) 2241 { 2242 boolean_t res = B_FALSE; 2243 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2244 2245 if (db->db_blkptr) 2246 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset, 2247 db->db_blkptr, db->db_blkptr->blk_birth); 2248 2249 return (res); 2250 } 2251 2252 blkptr_t * 2253 dmu_buf_get_blkptr(dmu_buf_t *db) 2254 { 2255 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 2256 return (dbi->db_blkptr); 2257 } 2258 2259 static void 2260 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db) 2261 { 2262 /* ASSERT(dmu_tx_is_syncing(tx) */ 2263 ASSERT(MUTEX_HELD(&db->db_mtx)); 2264 2265 if (db->db_blkptr != NULL) 2266 return; 2267 2268 if (db->db_blkid == DMU_SPILL_BLKID) { 2269 db->db_blkptr = &dn->dn_phys->dn_spill; 2270 BP_ZERO(db->db_blkptr); 2271 return; 2272 } 2273 if (db->db_level == dn->dn_phys->dn_nlevels-1) { 2274 /* 2275 * This buffer was allocated at a time when there was 2276 * no available blkptrs from the dnode, or it was 2277 * inappropriate to hook it in (i.e., nlevels mis-match). 2278 */ 2279 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr); 2280 ASSERT(db->db_parent == NULL); 2281 db->db_parent = dn->dn_dbuf; 2282 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid]; 2283 DBUF_VERIFY(db); 2284 } else { 2285 dmu_buf_impl_t *parent = db->db_parent; 2286 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 2287 2288 ASSERT(dn->dn_phys->dn_nlevels > 1); 2289 if (parent == NULL) { 2290 mutex_exit(&db->db_mtx); 2291 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2292 (void) dbuf_hold_impl(dn, db->db_level+1, 2293 db->db_blkid >> epbs, FALSE, db, &parent); 2294 rw_exit(&dn->dn_struct_rwlock); 2295 mutex_enter(&db->db_mtx); 2296 db->db_parent = parent; 2297 } 2298 db->db_blkptr = (blkptr_t *)parent->db.db_data + 2299 (db->db_blkid & ((1ULL << epbs) - 1)); 2300 DBUF_VERIFY(db); 2301 } 2302 } 2303 2304 static void 2305 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 2306 { 2307 dmu_buf_impl_t *db = dr->dr_dbuf; 2308 dnode_t *dn; 2309 zio_t *zio; 2310 2311 ASSERT(dmu_tx_is_syncing(tx)); 2312 2313 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 2314 2315 mutex_enter(&db->db_mtx); 2316 2317 ASSERT(db->db_level > 0); 2318 DBUF_VERIFY(db); 2319 2320 /* Read the block if it hasn't been read yet. */ 2321 if (db->db_buf == NULL) { 2322 mutex_exit(&db->db_mtx); 2323 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 2324 mutex_enter(&db->db_mtx); 2325 } 2326 ASSERT3U(db->db_state, ==, DB_CACHED); 2327 ASSERT(db->db_buf != NULL); 2328 2329 DB_DNODE_ENTER(db); 2330 dn = DB_DNODE(db); 2331 /* Indirect block size must match what the dnode thinks it is. */ 2332 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 2333 dbuf_check_blkptr(dn, db); 2334 DB_DNODE_EXIT(db); 2335 2336 /* Provide the pending dirty record to child dbufs */ 2337 db->db_data_pending = dr; 2338 2339 mutex_exit(&db->db_mtx); 2340 dbuf_write(dr, db->db_buf, tx); 2341 2342 zio = dr->dr_zio; 2343 mutex_enter(&dr->dt.di.dr_mtx); 2344 dbuf_sync_list(&dr->dt.di.dr_children, tx); 2345 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 2346 mutex_exit(&dr->dt.di.dr_mtx); 2347 zio_nowait(zio); 2348 } 2349 2350 static void 2351 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 2352 { 2353 arc_buf_t **datap = &dr->dt.dl.dr_data; 2354 dmu_buf_impl_t *db = dr->dr_dbuf; 2355 dnode_t *dn; 2356 objset_t *os; 2357 uint64_t txg = tx->tx_txg; 2358 2359 ASSERT(dmu_tx_is_syncing(tx)); 2360 2361 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 2362 2363 mutex_enter(&db->db_mtx); 2364 /* 2365 * To be synced, we must be dirtied. But we 2366 * might have been freed after the dirty. 2367 */ 2368 if (db->db_state == DB_UNCACHED) { 2369 /* This buffer has been freed since it was dirtied */ 2370 ASSERT(db->db.db_data == NULL); 2371 } else if (db->db_state == DB_FILL) { 2372 /* This buffer was freed and is now being re-filled */ 2373 ASSERT(db->db.db_data != dr->dt.dl.dr_data); 2374 } else { 2375 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL); 2376 } 2377 DBUF_VERIFY(db); 2378 2379 DB_DNODE_ENTER(db); 2380 dn = DB_DNODE(db); 2381 2382 if (db->db_blkid == DMU_SPILL_BLKID) { 2383 mutex_enter(&dn->dn_mtx); 2384 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR; 2385 mutex_exit(&dn->dn_mtx); 2386 } 2387 2388 /* 2389 * If this is a bonus buffer, simply copy the bonus data into the 2390 * dnode. It will be written out when the dnode is synced (and it 2391 * will be synced, since it must have been dirty for dbuf_sync to 2392 * be called). 2393 */ 2394 if (db->db_blkid == DMU_BONUS_BLKID) { 2395 dbuf_dirty_record_t **drp; 2396 2397 ASSERT(*datap != NULL); 2398 ASSERT0(db->db_level); 2399 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN); 2400 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen); 2401 DB_DNODE_EXIT(db); 2402 2403 if (*datap != db->db.db_data) { 2404 zio_buf_free(*datap, DN_MAX_BONUSLEN); 2405 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 2406 } 2407 db->db_data_pending = NULL; 2408 drp = &db->db_last_dirty; 2409 while (*drp != dr) 2410 drp = &(*drp)->dr_next; 2411 ASSERT(dr->dr_next == NULL); 2412 ASSERT(dr->dr_dbuf == db); 2413 *drp = dr->dr_next; 2414 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2415 ASSERT(db->db_dirtycnt > 0); 2416 db->db_dirtycnt -= 1; 2417 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); 2418 return; 2419 } 2420 2421 os = dn->dn_objset; 2422 2423 /* 2424 * This function may have dropped the db_mtx lock allowing a dmu_sync 2425 * operation to sneak in. As a result, we need to ensure that we 2426 * don't check the dr_override_state until we have returned from 2427 * dbuf_check_blkptr. 2428 */ 2429 dbuf_check_blkptr(dn, db); 2430 2431 /* 2432 * If this buffer is in the middle of an immediate write, 2433 * wait for the synchronous IO to complete. 2434 */ 2435 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 2436 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 2437 cv_wait(&db->db_changed, &db->db_mtx); 2438 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN); 2439 } 2440 2441 if (db->db_state != DB_NOFILL && 2442 dn->dn_object != DMU_META_DNODE_OBJECT && 2443 refcount_count(&db->db_holds) > 1 && 2444 dr->dt.dl.dr_override_state != DR_OVERRIDDEN && 2445 *datap == db->db_buf) { 2446 /* 2447 * If this buffer is currently "in use" (i.e., there 2448 * are active holds and db_data still references it), 2449 * then make a copy before we start the write so that 2450 * any modifications from the open txg will not leak 2451 * into this write. 2452 * 2453 * NOTE: this copy does not need to be made for 2454 * objects only modified in the syncing context (e.g. 2455 * DNONE_DNODE blocks). 2456 */ 2457 int blksz = arc_buf_size(*datap); 2458 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 2459 *datap = arc_buf_alloc(os->os_spa, blksz, db, type); 2460 bcopy(db->db.db_data, (*datap)->b_data, blksz); 2461 } 2462 db->db_data_pending = dr; 2463 2464 mutex_exit(&db->db_mtx); 2465 2466 dbuf_write(dr, *datap, tx); 2467 2468 ASSERT(!list_link_active(&dr->dr_dirty_node)); 2469 if (dn->dn_object == DMU_META_DNODE_OBJECT) { 2470 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr); 2471 DB_DNODE_EXIT(db); 2472 } else { 2473 /* 2474 * Although zio_nowait() does not "wait for an IO", it does 2475 * initiate the IO. If this is an empty write it seems plausible 2476 * that the IO could actually be completed before the nowait 2477 * returns. We need to DB_DNODE_EXIT() first in case 2478 * zio_nowait() invalidates the dbuf. 2479 */ 2480 DB_DNODE_EXIT(db); 2481 zio_nowait(dr->dr_zio); 2482 } 2483 } 2484 2485 void 2486 dbuf_sync_list(list_t *list, dmu_tx_t *tx) 2487 { 2488 dbuf_dirty_record_t *dr; 2489 2490 while (dr = list_head(list)) { 2491 if (dr->dr_zio != NULL) { 2492 /* 2493 * If we find an already initialized zio then we 2494 * are processing the meta-dnode, and we have finished. 2495 * The dbufs for all dnodes are put back on the list 2496 * during processing, so that we can zio_wait() 2497 * these IOs after initiating all child IOs. 2498 */ 2499 ASSERT3U(dr->dr_dbuf->db.db_object, ==, 2500 DMU_META_DNODE_OBJECT); 2501 break; 2502 } 2503 list_remove(list, dr); 2504 if (dr->dr_dbuf->db_level > 0) 2505 dbuf_sync_indirect(dr, tx); 2506 else 2507 dbuf_sync_leaf(dr, tx); 2508 } 2509 } 2510 2511 /* ARGSUSED */ 2512 static void 2513 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 2514 { 2515 dmu_buf_impl_t *db = vdb; 2516 dnode_t *dn; 2517 blkptr_t *bp = zio->io_bp; 2518 blkptr_t *bp_orig = &zio->io_bp_orig; 2519 spa_t *spa = zio->io_spa; 2520 int64_t delta; 2521 uint64_t fill = 0; 2522 int i; 2523 2524 ASSERT3P(db->db_blkptr, ==, bp); 2525 2526 DB_DNODE_ENTER(db); 2527 dn = DB_DNODE(db); 2528 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig); 2529 dnode_diduse_space(dn, delta - zio->io_prev_space_delta); 2530 zio->io_prev_space_delta = delta; 2531 2532 if (bp->blk_birth != 0) { 2533 ASSERT((db->db_blkid != DMU_SPILL_BLKID && 2534 BP_GET_TYPE(bp) == dn->dn_type) || 2535 (db->db_blkid == DMU_SPILL_BLKID && 2536 BP_GET_TYPE(bp) == dn->dn_bonustype) || 2537 BP_IS_EMBEDDED(bp)); 2538 ASSERT(BP_GET_LEVEL(bp) == db->db_level); 2539 } 2540 2541 mutex_enter(&db->db_mtx); 2542 2543 #ifdef ZFS_DEBUG 2544 if (db->db_blkid == DMU_SPILL_BLKID) { 2545 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 2546 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) && 2547 db->db_blkptr == &dn->dn_phys->dn_spill); 2548 } 2549 #endif 2550 2551 if (db->db_level == 0) { 2552 mutex_enter(&dn->dn_mtx); 2553 if (db->db_blkid > dn->dn_phys->dn_maxblkid && 2554 db->db_blkid != DMU_SPILL_BLKID) 2555 dn->dn_phys->dn_maxblkid = db->db_blkid; 2556 mutex_exit(&dn->dn_mtx); 2557 2558 if (dn->dn_type == DMU_OT_DNODE) { 2559 dnode_phys_t *dnp = db->db.db_data; 2560 for (i = db->db.db_size >> DNODE_SHIFT; i > 0; 2561 i--, dnp++) { 2562 if (dnp->dn_type != DMU_OT_NONE) 2563 fill++; 2564 } 2565 } else { 2566 if (BP_IS_HOLE(bp)) { 2567 fill = 0; 2568 } else { 2569 fill = 1; 2570 } 2571 } 2572 } else { 2573 blkptr_t *ibp = db->db.db_data; 2574 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 2575 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) { 2576 if (BP_IS_HOLE(ibp)) 2577 continue; 2578 fill += BP_GET_FILL(ibp); 2579 } 2580 } 2581 DB_DNODE_EXIT(db); 2582 2583 if (!BP_IS_EMBEDDED(bp)) 2584 bp->blk_fill = fill; 2585 2586 mutex_exit(&db->db_mtx); 2587 } 2588 2589 /* 2590 * The SPA will call this callback several times for each zio - once 2591 * for every physical child i/o (zio->io_phys_children times). This 2592 * allows the DMU to monitor the progress of each logical i/o. For example, 2593 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z 2594 * block. There may be a long delay before all copies/fragments are completed, 2595 * so this callback allows us to retire dirty space gradually, as the physical 2596 * i/os complete. 2597 */ 2598 /* ARGSUSED */ 2599 static void 2600 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg) 2601 { 2602 dmu_buf_impl_t *db = arg; 2603 objset_t *os = db->db_objset; 2604 dsl_pool_t *dp = dmu_objset_pool(os); 2605 dbuf_dirty_record_t *dr; 2606 int delta = 0; 2607 2608 dr = db->db_data_pending; 2609 ASSERT3U(dr->dr_txg, ==, zio->io_txg); 2610 2611 /* 2612 * The callback will be called io_phys_children times. Retire one 2613 * portion of our dirty space each time we are called. Any rounding 2614 * error will be cleaned up by dsl_pool_sync()'s call to 2615 * dsl_pool_undirty_space(). 2616 */ 2617 delta = dr->dr_accounted / zio->io_phys_children; 2618 dsl_pool_undirty_space(dp, delta, zio->io_txg); 2619 } 2620 2621 /* ARGSUSED */ 2622 static void 2623 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb) 2624 { 2625 dmu_buf_impl_t *db = vdb; 2626 blkptr_t *bp_orig = &zio->io_bp_orig; 2627 blkptr_t *bp = db->db_blkptr; 2628 objset_t *os = db->db_objset; 2629 dmu_tx_t *tx = os->os_synctx; 2630 dbuf_dirty_record_t **drp, *dr; 2631 2632 ASSERT0(zio->io_error); 2633 ASSERT(db->db_blkptr == bp); 2634 2635 /* 2636 * For nopwrites and rewrites we ensure that the bp matches our 2637 * original and bypass all the accounting. 2638 */ 2639 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) { 2640 ASSERT(BP_EQUAL(bp, bp_orig)); 2641 } else { 2642 dsl_dataset_t *ds = os->os_dsl_dataset; 2643 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 2644 dsl_dataset_block_born(ds, bp, tx); 2645 } 2646 2647 mutex_enter(&db->db_mtx); 2648 2649 DBUF_VERIFY(db); 2650 2651 drp = &db->db_last_dirty; 2652 while ((dr = *drp) != db->db_data_pending) 2653 drp = &dr->dr_next; 2654 ASSERT(!list_link_active(&dr->dr_dirty_node)); 2655 ASSERT(dr->dr_dbuf == db); 2656 ASSERT(dr->dr_next == NULL); 2657 *drp = dr->dr_next; 2658 2659 #ifdef ZFS_DEBUG 2660 if (db->db_blkid == DMU_SPILL_BLKID) { 2661 dnode_t *dn; 2662 2663 DB_DNODE_ENTER(db); 2664 dn = DB_DNODE(db); 2665 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 2666 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) && 2667 db->db_blkptr == &dn->dn_phys->dn_spill); 2668 DB_DNODE_EXIT(db); 2669 } 2670 #endif 2671 2672 if (db->db_level == 0) { 2673 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2674 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 2675 if (db->db_state != DB_NOFILL) { 2676 if (dr->dt.dl.dr_data != db->db_buf) 2677 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, 2678 db)); 2679 else if (!arc_released(db->db_buf)) 2680 arc_set_callback(db->db_buf, dbuf_do_evict, db); 2681 } 2682 } else { 2683 dnode_t *dn; 2684 2685 DB_DNODE_ENTER(db); 2686 dn = DB_DNODE(db); 2687 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 2688 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 2689 if (!BP_IS_HOLE(db->db_blkptr)) { 2690 int epbs = 2691 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 2692 ASSERT3U(db->db_blkid, <=, 2693 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs)); 2694 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 2695 db->db.db_size); 2696 if (!arc_released(db->db_buf)) 2697 arc_set_callback(db->db_buf, dbuf_do_evict, db); 2698 } 2699 DB_DNODE_EXIT(db); 2700 mutex_destroy(&dr->dt.di.dr_mtx); 2701 list_destroy(&dr->dt.di.dr_children); 2702 } 2703 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2704 2705 cv_broadcast(&db->db_changed); 2706 ASSERT(db->db_dirtycnt > 0); 2707 db->db_dirtycnt -= 1; 2708 db->db_data_pending = NULL; 2709 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg); 2710 } 2711 2712 static void 2713 dbuf_write_nofill_ready(zio_t *zio) 2714 { 2715 dbuf_write_ready(zio, NULL, zio->io_private); 2716 } 2717 2718 static void 2719 dbuf_write_nofill_done(zio_t *zio) 2720 { 2721 dbuf_write_done(zio, NULL, zio->io_private); 2722 } 2723 2724 static void 2725 dbuf_write_override_ready(zio_t *zio) 2726 { 2727 dbuf_dirty_record_t *dr = zio->io_private; 2728 dmu_buf_impl_t *db = dr->dr_dbuf; 2729 2730 dbuf_write_ready(zio, NULL, db); 2731 } 2732 2733 static void 2734 dbuf_write_override_done(zio_t *zio) 2735 { 2736 dbuf_dirty_record_t *dr = zio->io_private; 2737 dmu_buf_impl_t *db = dr->dr_dbuf; 2738 blkptr_t *obp = &dr->dt.dl.dr_overridden_by; 2739 2740 mutex_enter(&db->db_mtx); 2741 if (!BP_EQUAL(zio->io_bp, obp)) { 2742 if (!BP_IS_HOLE(obp)) 2743 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp); 2744 arc_release(dr->dt.dl.dr_data, db); 2745 } 2746 mutex_exit(&db->db_mtx); 2747 2748 dbuf_write_done(zio, NULL, db); 2749 } 2750 2751 /* Issue I/O to commit a dirty buffer to disk. */ 2752 static void 2753 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx) 2754 { 2755 dmu_buf_impl_t *db = dr->dr_dbuf; 2756 dnode_t *dn; 2757 objset_t *os; 2758 dmu_buf_impl_t *parent = db->db_parent; 2759 uint64_t txg = tx->tx_txg; 2760 zbookmark_phys_t zb; 2761 zio_prop_t zp; 2762 zio_t *zio; 2763 int wp_flag = 0; 2764 2765 DB_DNODE_ENTER(db); 2766 dn = DB_DNODE(db); 2767 os = dn->dn_objset; 2768 2769 if (db->db_state != DB_NOFILL) { 2770 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) { 2771 /* 2772 * Private object buffers are released here rather 2773 * than in dbuf_dirty() since they are only modified 2774 * in the syncing context and we don't want the 2775 * overhead of making multiple copies of the data. 2776 */ 2777 if (BP_IS_HOLE(db->db_blkptr)) { 2778 arc_buf_thaw(data); 2779 } else { 2780 dbuf_release_bp(db); 2781 } 2782 } 2783 } 2784 2785 if (parent != dn->dn_dbuf) { 2786 /* Our parent is an indirect block. */ 2787 /* We have a dirty parent that has been scheduled for write. */ 2788 ASSERT(parent && parent->db_data_pending); 2789 /* Our parent's buffer is one level closer to the dnode. */ 2790 ASSERT(db->db_level == parent->db_level-1); 2791 /* 2792 * We're about to modify our parent's db_data by modifying 2793 * our block pointer, so the parent must be released. 2794 */ 2795 ASSERT(arc_released(parent->db_buf)); 2796 zio = parent->db_data_pending->dr_zio; 2797 } else { 2798 /* Our parent is the dnode itself. */ 2799 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 && 2800 db->db_blkid != DMU_SPILL_BLKID) || 2801 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0)); 2802 if (db->db_blkid != DMU_SPILL_BLKID) 2803 ASSERT3P(db->db_blkptr, ==, 2804 &dn->dn_phys->dn_blkptr[db->db_blkid]); 2805 zio = dn->dn_zio; 2806 } 2807 2808 ASSERT(db->db_level == 0 || data == db->db_buf); 2809 ASSERT3U(db->db_blkptr->blk_birth, <=, txg); 2810 ASSERT(zio); 2811 2812 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 2813 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 2814 db->db.db_object, db->db_level, db->db_blkid); 2815 2816 if (db->db_blkid == DMU_SPILL_BLKID) 2817 wp_flag = WP_SPILL; 2818 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0; 2819 2820 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp); 2821 DB_DNODE_EXIT(db); 2822 2823 if (db->db_level == 0 && 2824 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 2825 /* 2826 * The BP for this block has been provided by open context 2827 * (by dmu_sync() or dmu_buf_write_embedded()). 2828 */ 2829 void *contents = (data != NULL) ? data->b_data : NULL; 2830 2831 dr->dr_zio = zio_write(zio, os->os_spa, txg, 2832 db->db_blkptr, contents, db->db.db_size, &zp, 2833 dbuf_write_override_ready, NULL, dbuf_write_override_done, 2834 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 2835 mutex_enter(&db->db_mtx); 2836 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 2837 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by, 2838 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite); 2839 mutex_exit(&db->db_mtx); 2840 } else if (db->db_state == DB_NOFILL) { 2841 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF || 2842 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY); 2843 dr->dr_zio = zio_write(zio, os->os_spa, txg, 2844 db->db_blkptr, NULL, db->db.db_size, &zp, 2845 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db, 2846 ZIO_PRIORITY_ASYNC_WRITE, 2847 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb); 2848 } else { 2849 ASSERT(arc_released(data)); 2850 dr->dr_zio = arc_write(zio, os->os_spa, txg, 2851 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db), 2852 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready, 2853 dbuf_write_physdone, dbuf_write_done, db, 2854 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 2855 } 2856 } 2857