1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5f65e61c0Sahrens * Common Development and Distribution License (the "License"). 6f65e61c0Sahrens * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 221ab7f2deSmaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/zfs_context.h> 29fa9e4066Sahrens #include <sys/dmu.h> 30fa9e4066Sahrens #include <sys/dmu_impl.h> 31fa9e4066Sahrens #include <sys/dbuf.h> 32fa9e4066Sahrens #include <sys/dmu_objset.h> 33fa9e4066Sahrens #include <sys/dsl_dataset.h> 34fa9e4066Sahrens #include <sys/dsl_dir.h> 35fa9e4066Sahrens #include <sys/dmu_tx.h> 36fa9e4066Sahrens #include <sys/spa.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/dmu_zfetch.h> 39fa9e4066Sahrens 40fa9e4066Sahrens static void dbuf_destroy(dmu_buf_impl_t *db); 41fa9e4066Sahrens static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 42088f3894Sahrens static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx); 43c717a561Smaybee static arc_done_func_t dbuf_write_ready; 44fa9e4066Sahrens static arc_done_func_t dbuf_write_done; 45fa9e4066Sahrens 46fa9e4066Sahrens /* 47fa9e4066Sahrens * Global data structures and functions for the dbuf cache. 48fa9e4066Sahrens */ 49fa9e4066Sahrens static kmem_cache_t *dbuf_cache; 50fa9e4066Sahrens 51fa9e4066Sahrens /* ARGSUSED */ 52fa9e4066Sahrens static int 53fa9e4066Sahrens dbuf_cons(void *vdb, void *unused, int kmflag) 54fa9e4066Sahrens { 55fa9e4066Sahrens dmu_buf_impl_t *db = vdb; 56fa9e4066Sahrens bzero(db, sizeof (dmu_buf_impl_t)); 57fa9e4066Sahrens 58fa9e4066Sahrens mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL); 59fa9e4066Sahrens cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL); 60fa9e4066Sahrens refcount_create(&db->db_holds); 61fa9e4066Sahrens return (0); 62fa9e4066Sahrens } 63fa9e4066Sahrens 64fa9e4066Sahrens /* ARGSUSED */ 65fa9e4066Sahrens static void 66fa9e4066Sahrens dbuf_dest(void *vdb, void *unused) 67fa9e4066Sahrens { 68fa9e4066Sahrens dmu_buf_impl_t *db = vdb; 69fa9e4066Sahrens mutex_destroy(&db->db_mtx); 70fa9e4066Sahrens cv_destroy(&db->db_changed); 71fa9e4066Sahrens refcount_destroy(&db->db_holds); 72fa9e4066Sahrens } 73fa9e4066Sahrens 74fa9e4066Sahrens /* 75fa9e4066Sahrens * dbuf hash table routines 76fa9e4066Sahrens */ 77fa9e4066Sahrens static dbuf_hash_table_t dbuf_hash_table; 78fa9e4066Sahrens 79fa9e4066Sahrens static uint64_t dbuf_hash_count; 80fa9e4066Sahrens 81fa9e4066Sahrens static uint64_t 82fa9e4066Sahrens dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid) 83fa9e4066Sahrens { 84fa9e4066Sahrens uintptr_t osv = (uintptr_t)os; 85fa9e4066Sahrens uint64_t crc = -1ULL; 86fa9e4066Sahrens 87fa9e4066Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 88fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF]; 89fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; 90fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; 91fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; 92fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF]; 93fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF]; 94fa9e4066Sahrens 95fa9e4066Sahrens crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16); 96fa9e4066Sahrens 97fa9e4066Sahrens return (crc); 98fa9e4066Sahrens } 99fa9e4066Sahrens 100fa9e4066Sahrens #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid); 101fa9e4066Sahrens 102fa9e4066Sahrens #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \ 103fa9e4066Sahrens ((dbuf)->db.db_object == (obj) && \ 104fa9e4066Sahrens (dbuf)->db_objset == (os) && \ 105fa9e4066Sahrens (dbuf)->db_level == (level) && \ 106fa9e4066Sahrens (dbuf)->db_blkid == (blkid)) 107fa9e4066Sahrens 108fa9e4066Sahrens dmu_buf_impl_t * 109fa9e4066Sahrens dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid) 110fa9e4066Sahrens { 111fa9e4066Sahrens dbuf_hash_table_t *h = &dbuf_hash_table; 112fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 113fa9e4066Sahrens uint64_t obj = dn->dn_object; 114fa9e4066Sahrens uint64_t hv = DBUF_HASH(os, obj, level, blkid); 115fa9e4066Sahrens uint64_t idx = hv & h->hash_table_mask; 116fa9e4066Sahrens dmu_buf_impl_t *db; 117fa9e4066Sahrens 118fa9e4066Sahrens mutex_enter(DBUF_HASH_MUTEX(h, idx)); 119fa9e4066Sahrens for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) { 120fa9e4066Sahrens if (DBUF_EQUAL(db, os, obj, level, blkid)) { 121fa9e4066Sahrens mutex_enter(&db->db_mtx); 122ea8dc4b6Seschrock if (db->db_state != DB_EVICTING) { 123fa9e4066Sahrens mutex_exit(DBUF_HASH_MUTEX(h, idx)); 124fa9e4066Sahrens return (db); 125fa9e4066Sahrens } 126fa9e4066Sahrens mutex_exit(&db->db_mtx); 127fa9e4066Sahrens } 128fa9e4066Sahrens } 129fa9e4066Sahrens mutex_exit(DBUF_HASH_MUTEX(h, idx)); 130fa9e4066Sahrens return (NULL); 131fa9e4066Sahrens } 132fa9e4066Sahrens 133fa9e4066Sahrens /* 134fa9e4066Sahrens * Insert an entry into the hash table. If there is already an element 135fa9e4066Sahrens * equal to elem in the hash table, then the already existing element 136fa9e4066Sahrens * will be returned and the new element will not be inserted. 137fa9e4066Sahrens * Otherwise returns NULL. 138fa9e4066Sahrens */ 139fa9e4066Sahrens static dmu_buf_impl_t * 140fa9e4066Sahrens dbuf_hash_insert(dmu_buf_impl_t *db) 141fa9e4066Sahrens { 142fa9e4066Sahrens dbuf_hash_table_t *h = &dbuf_hash_table; 143fa9e4066Sahrens objset_impl_t *os = db->db_objset; 144fa9e4066Sahrens uint64_t obj = db->db.db_object; 145fa9e4066Sahrens int level = db->db_level; 146fa9e4066Sahrens uint64_t blkid = db->db_blkid; 147fa9e4066Sahrens uint64_t hv = DBUF_HASH(os, obj, level, blkid); 148fa9e4066Sahrens uint64_t idx = hv & h->hash_table_mask; 149fa9e4066Sahrens dmu_buf_impl_t *dbf; 150fa9e4066Sahrens 151fa9e4066Sahrens mutex_enter(DBUF_HASH_MUTEX(h, idx)); 152fa9e4066Sahrens for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) { 153fa9e4066Sahrens if (DBUF_EQUAL(dbf, os, obj, level, blkid)) { 154fa9e4066Sahrens mutex_enter(&dbf->db_mtx); 155ea8dc4b6Seschrock if (dbf->db_state != DB_EVICTING) { 156fa9e4066Sahrens mutex_exit(DBUF_HASH_MUTEX(h, idx)); 157fa9e4066Sahrens return (dbf); 158fa9e4066Sahrens } 159fa9e4066Sahrens mutex_exit(&dbf->db_mtx); 160fa9e4066Sahrens } 161fa9e4066Sahrens } 162fa9e4066Sahrens 163fa9e4066Sahrens mutex_enter(&db->db_mtx); 164fa9e4066Sahrens db->db_hash_next = h->hash_table[idx]; 165fa9e4066Sahrens h->hash_table[idx] = db; 166fa9e4066Sahrens mutex_exit(DBUF_HASH_MUTEX(h, idx)); 167fa9e4066Sahrens atomic_add_64(&dbuf_hash_count, 1); 168fa9e4066Sahrens 169fa9e4066Sahrens return (NULL); 170fa9e4066Sahrens } 171fa9e4066Sahrens 172fa9e4066Sahrens /* 173fa9e4066Sahrens * Remove an entry from the hash table. This operation will 174fa9e4066Sahrens * fail if there are any existing holds on the db. 175fa9e4066Sahrens */ 176fa9e4066Sahrens static void 177fa9e4066Sahrens dbuf_hash_remove(dmu_buf_impl_t *db) 178fa9e4066Sahrens { 179fa9e4066Sahrens dbuf_hash_table_t *h = &dbuf_hash_table; 180fa9e4066Sahrens uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object, 181fa9e4066Sahrens db->db_level, db->db_blkid); 182fa9e4066Sahrens uint64_t idx = hv & h->hash_table_mask; 183fa9e4066Sahrens dmu_buf_impl_t *dbf, **dbp; 184fa9e4066Sahrens 185fa9e4066Sahrens /* 186fa9e4066Sahrens * We musn't hold db_mtx to maintin lock ordering: 187fa9e4066Sahrens * DBUF_HASH_MUTEX > db_mtx. 188fa9e4066Sahrens */ 189fa9e4066Sahrens ASSERT(refcount_is_zero(&db->db_holds)); 190ea8dc4b6Seschrock ASSERT(db->db_state == DB_EVICTING); 191fa9e4066Sahrens ASSERT(!MUTEX_HELD(&db->db_mtx)); 192fa9e4066Sahrens 193fa9e4066Sahrens mutex_enter(DBUF_HASH_MUTEX(h, idx)); 194fa9e4066Sahrens dbp = &h->hash_table[idx]; 195fa9e4066Sahrens while ((dbf = *dbp) != db) { 196fa9e4066Sahrens dbp = &dbf->db_hash_next; 197fa9e4066Sahrens ASSERT(dbf != NULL); 198fa9e4066Sahrens } 199fa9e4066Sahrens *dbp = db->db_hash_next; 200fa9e4066Sahrens db->db_hash_next = NULL; 201fa9e4066Sahrens mutex_exit(DBUF_HASH_MUTEX(h, idx)); 202fa9e4066Sahrens atomic_add_64(&dbuf_hash_count, -1); 203fa9e4066Sahrens } 204fa9e4066Sahrens 205ea8dc4b6Seschrock static arc_evict_func_t dbuf_do_evict; 206fa9e4066Sahrens 207fa9e4066Sahrens static void 208fa9e4066Sahrens dbuf_evict_user(dmu_buf_impl_t *db) 209fa9e4066Sahrens { 210fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 211fa9e4066Sahrens 212c717a561Smaybee if (db->db_level != 0 || db->db_evict_func == NULL) 213fa9e4066Sahrens return; 214fa9e4066Sahrens 215c717a561Smaybee if (db->db_user_data_ptr_ptr) 216c717a561Smaybee *db->db_user_data_ptr_ptr = db->db.db_data; 217c717a561Smaybee db->db_evict_func(&db->db, db->db_user_ptr); 218c717a561Smaybee db->db_user_ptr = NULL; 219c717a561Smaybee db->db_user_data_ptr_ptr = NULL; 220c717a561Smaybee db->db_evict_func = NULL; 221fa9e4066Sahrens } 222fa9e4066Sahrens 223fa9e4066Sahrens void 224ea8dc4b6Seschrock dbuf_evict(dmu_buf_impl_t *db) 225ea8dc4b6Seschrock { 226ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&db->db_mtx)); 227ea8dc4b6Seschrock ASSERT(db->db_buf == NULL); 228c717a561Smaybee ASSERT(db->db_data_pending == NULL); 229ea8dc4b6Seschrock 230ea8dc4b6Seschrock dbuf_clear(db); 231ea8dc4b6Seschrock dbuf_destroy(db); 232ea8dc4b6Seschrock } 233ea8dc4b6Seschrock 234ea8dc4b6Seschrock void 235fa9e4066Sahrens dbuf_init(void) 236fa9e4066Sahrens { 237ea8dc4b6Seschrock uint64_t hsize = 1ULL << 16; 238fa9e4066Sahrens dbuf_hash_table_t *h = &dbuf_hash_table; 239fa9e4066Sahrens int i; 240fa9e4066Sahrens 241fa9e4066Sahrens /* 242fa9e4066Sahrens * The hash table is big enough to fill all of physical memory 243ea8dc4b6Seschrock * with an average 4K block size. The table will take up 244ea8dc4b6Seschrock * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers). 245fa9e4066Sahrens */ 246ea8dc4b6Seschrock while (hsize * 4096 < physmem * PAGESIZE) 247fa9e4066Sahrens hsize <<= 1; 248fa9e4066Sahrens 249ea8dc4b6Seschrock retry: 250fa9e4066Sahrens h->hash_table_mask = hsize - 1; 251ea8dc4b6Seschrock h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP); 252ea8dc4b6Seschrock if (h->hash_table == NULL) { 253ea8dc4b6Seschrock /* XXX - we should really return an error instead of assert */ 254ea8dc4b6Seschrock ASSERT(hsize > (1ULL << 10)); 255ea8dc4b6Seschrock hsize >>= 1; 256ea8dc4b6Seschrock goto retry; 257ea8dc4b6Seschrock } 258fa9e4066Sahrens 259fa9e4066Sahrens dbuf_cache = kmem_cache_create("dmu_buf_impl_t", 260fa9e4066Sahrens sizeof (dmu_buf_impl_t), 261fa9e4066Sahrens 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0); 262fa9e4066Sahrens 263fa9e4066Sahrens for (i = 0; i < DBUF_MUTEXES; i++) 264fa9e4066Sahrens mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL); 265fa9e4066Sahrens } 266fa9e4066Sahrens 267fa9e4066Sahrens void 268fa9e4066Sahrens dbuf_fini(void) 269fa9e4066Sahrens { 270fa9e4066Sahrens dbuf_hash_table_t *h = &dbuf_hash_table; 271fa9e4066Sahrens int i; 272fa9e4066Sahrens 273fa9e4066Sahrens for (i = 0; i < DBUF_MUTEXES; i++) 274fa9e4066Sahrens mutex_destroy(&h->hash_mutexes[i]); 275fa9e4066Sahrens kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 276fa9e4066Sahrens kmem_cache_destroy(dbuf_cache); 277fa9e4066Sahrens } 278fa9e4066Sahrens 279fa9e4066Sahrens /* 280fa9e4066Sahrens * Other stuff. 281fa9e4066Sahrens */ 282fa9e4066Sahrens 2839c9dc39aSek110237 #ifdef ZFS_DEBUG 284fa9e4066Sahrens static void 285fa9e4066Sahrens dbuf_verify(dmu_buf_impl_t *db) 286fa9e4066Sahrens { 287fa9e4066Sahrens dnode_t *dn = db->db_dnode; 288fa9e4066Sahrens 289fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 290fa9e4066Sahrens 291fa9e4066Sahrens if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY)) 292fa9e4066Sahrens return; 293fa9e4066Sahrens 294fa9e4066Sahrens ASSERT(db->db_objset != NULL); 295fa9e4066Sahrens if (dn == NULL) { 296fa9e4066Sahrens ASSERT(db->db_parent == NULL); 297fa9e4066Sahrens ASSERT(db->db_blkptr == NULL); 298fa9e4066Sahrens } else { 299fa9e4066Sahrens ASSERT3U(db->db.db_object, ==, dn->dn_object); 300fa9e4066Sahrens ASSERT3P(db->db_objset, ==, dn->dn_objset); 301fa9e4066Sahrens ASSERT3U(db->db_level, <, dn->dn_nlevels); 302ea8dc4b6Seschrock ASSERT(db->db_blkid == DB_BONUS_BLKID || 303ea8dc4b6Seschrock list_head(&dn->dn_dbufs)); 304fa9e4066Sahrens } 305fa9e4066Sahrens if (db->db_blkid == DB_BONUS_BLKID) { 306fa9e4066Sahrens ASSERT(dn != NULL); 3071934e92fSmaybee ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 308fa9e4066Sahrens ASSERT3U(db->db.db_offset, ==, DB_BONUS_BLKID); 309fa9e4066Sahrens } else { 310fa9e4066Sahrens ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size); 311fa9e4066Sahrens } 312fa9e4066Sahrens 313fa9e4066Sahrens if (db->db_level == 0) { 314fa9e4066Sahrens /* we can be momentarily larger in dnode_set_blksz() */ 315fa9e4066Sahrens if (db->db_blkid != DB_BONUS_BLKID && dn) { 316fa9e4066Sahrens ASSERT3U(db->db.db_size, >=, dn->dn_datablksz); 317fa9e4066Sahrens } 318ea8dc4b6Seschrock if (db->db.db_object == DMU_META_DNODE_OBJECT) { 319c717a561Smaybee dbuf_dirty_record_t *dr = db->db_data_pending; 320fa9e4066Sahrens /* 321fa9e4066Sahrens * it should only be modified in syncing 322fa9e4066Sahrens * context, so make sure we only have 323fa9e4066Sahrens * one copy of the data. 324fa9e4066Sahrens */ 325c717a561Smaybee ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf); 326fa9e4066Sahrens } 327fa9e4066Sahrens } 328fa9e4066Sahrens 329fa9e4066Sahrens /* verify db->db_blkptr */ 330fa9e4066Sahrens if (db->db_blkptr) { 331fa9e4066Sahrens if (db->db_parent == dn->dn_dbuf) { 332fa9e4066Sahrens /* db is pointed to by the dnode */ 333fa9e4066Sahrens /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */ 334ea8dc4b6Seschrock if (db->db.db_object == DMU_META_DNODE_OBJECT) 335fa9e4066Sahrens ASSERT(db->db_parent == NULL); 336fa9e4066Sahrens else 337fa9e4066Sahrens ASSERT(db->db_parent != NULL); 338fa9e4066Sahrens ASSERT3P(db->db_blkptr, ==, 339fa9e4066Sahrens &dn->dn_phys->dn_blkptr[db->db_blkid]); 340fa9e4066Sahrens } else { 341fa9e4066Sahrens /* db is pointed to by an indirect block */ 342fa9e4066Sahrens int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT; 343fa9e4066Sahrens ASSERT3U(db->db_parent->db_level, ==, db->db_level+1); 344fa9e4066Sahrens ASSERT3U(db->db_parent->db.db_object, ==, 345fa9e4066Sahrens db->db.db_object); 346fa9e4066Sahrens /* 347fa9e4066Sahrens * dnode_grow_indblksz() can make this fail if we don't 348fa9e4066Sahrens * have the struct_rwlock. XXX indblksz no longer 349fa9e4066Sahrens * grows. safe to do this now? 350fa9e4066Sahrens */ 351fa9e4066Sahrens if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock)) { 352fa9e4066Sahrens ASSERT3P(db->db_blkptr, ==, 353fa9e4066Sahrens ((blkptr_t *)db->db_parent->db.db_data + 354fa9e4066Sahrens db->db_blkid % epb)); 355fa9e4066Sahrens } 356fa9e4066Sahrens } 357fa9e4066Sahrens } 358fa9e4066Sahrens if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) && 359fa9e4066Sahrens db->db.db_data && db->db_blkid != DB_BONUS_BLKID && 360fa9e4066Sahrens db->db_state != DB_FILL && !dn->dn_free_txg) { 361fa9e4066Sahrens /* 362fa9e4066Sahrens * If the blkptr isn't set but they have nonzero data, 363fa9e4066Sahrens * it had better be dirty, otherwise we'll lose that 364fa9e4066Sahrens * data when we evict this buffer. 365fa9e4066Sahrens */ 366fa9e4066Sahrens if (db->db_dirtycnt == 0) { 367fa9e4066Sahrens uint64_t *buf = db->db.db_data; 368fa9e4066Sahrens int i; 369fa9e4066Sahrens 370fa9e4066Sahrens for (i = 0; i < db->db.db_size >> 3; i++) { 371fa9e4066Sahrens ASSERT(buf[i] == 0); 372fa9e4066Sahrens } 373fa9e4066Sahrens } 374fa9e4066Sahrens } 375fa9e4066Sahrens } 3769c9dc39aSek110237 #endif 377fa9e4066Sahrens 378fa9e4066Sahrens static void 379fa9e4066Sahrens dbuf_update_data(dmu_buf_impl_t *db) 380fa9e4066Sahrens { 381fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 382c717a561Smaybee if (db->db_level == 0 && db->db_user_data_ptr_ptr) { 383fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 384c717a561Smaybee *db->db_user_data_ptr_ptr = db->db.db_data; 385fa9e4066Sahrens } 386fa9e4066Sahrens } 387fa9e4066Sahrens 388fa9e4066Sahrens static void 389fa9e4066Sahrens dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf) 390fa9e4066Sahrens { 391fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 392ea8dc4b6Seschrock ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf)); 393fa9e4066Sahrens db->db_buf = buf; 394ea8dc4b6Seschrock if (buf != NULL) { 395ea8dc4b6Seschrock ASSERT(buf->b_data != NULL); 396fa9e4066Sahrens db->db.db_data = buf->b_data; 397ea8dc4b6Seschrock if (!arc_released(buf)) 398ea8dc4b6Seschrock arc_set_callback(buf, dbuf_do_evict, db); 399fa9e4066Sahrens dbuf_update_data(db); 400ea8dc4b6Seschrock } else { 401ea8dc4b6Seschrock dbuf_evict_user(db); 402ea8dc4b6Seschrock db->db.db_data = NULL; 403ea8dc4b6Seschrock db->db_state = DB_UNCACHED; 404ea8dc4b6Seschrock } 405fa9e4066Sahrens } 406fa9e4066Sahrens 407fa9e4066Sahrens uint64_t 408fa9e4066Sahrens dbuf_whichblock(dnode_t *dn, uint64_t offset) 409fa9e4066Sahrens { 410fa9e4066Sahrens if (dn->dn_datablkshift) { 411fa9e4066Sahrens return (offset >> dn->dn_datablkshift); 412fa9e4066Sahrens } else { 413fa9e4066Sahrens ASSERT3U(offset, <, dn->dn_datablksz); 414fa9e4066Sahrens return (0); 415fa9e4066Sahrens } 416fa9e4066Sahrens } 417fa9e4066Sahrens 418fa9e4066Sahrens static void 419fa9e4066Sahrens dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb) 420fa9e4066Sahrens { 421fa9e4066Sahrens dmu_buf_impl_t *db = vdb; 422fa9e4066Sahrens 423fa9e4066Sahrens mutex_enter(&db->db_mtx); 424fa9e4066Sahrens ASSERT3U(db->db_state, ==, DB_READ); 425fa9e4066Sahrens /* 426fa9e4066Sahrens * All reads are synchronous, so we must have a hold on the dbuf 427fa9e4066Sahrens */ 428fa9e4066Sahrens ASSERT(refcount_count(&db->db_holds) > 0); 429ea8dc4b6Seschrock ASSERT(db->db_buf == NULL); 430fa9e4066Sahrens ASSERT(db->db.db_data == NULL); 431c717a561Smaybee if (db->db_level == 0 && db->db_freed_in_flight) { 432fa9e4066Sahrens /* we were freed in flight; disregard any error */ 433fa9e4066Sahrens arc_release(buf, db); 434fa9e4066Sahrens bzero(buf->b_data, db->db.db_size); 4356b4acc8bSahrens arc_buf_freeze(buf); 436c717a561Smaybee db->db_freed_in_flight = FALSE; 437fa9e4066Sahrens dbuf_set_data(db, buf); 438fa9e4066Sahrens db->db_state = DB_CACHED; 439fa9e4066Sahrens } else if (zio == NULL || zio->io_error == 0) { 440fa9e4066Sahrens dbuf_set_data(db, buf); 441fa9e4066Sahrens db->db_state = DB_CACHED; 442fa9e4066Sahrens } else { 443fa9e4066Sahrens ASSERT(db->db_blkid != DB_BONUS_BLKID); 444fa9e4066Sahrens ASSERT3P(db->db_buf, ==, NULL); 445ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(buf, db) == 1); 446ea8dc4b6Seschrock db->db_state = DB_UNCACHED; 447fa9e4066Sahrens } 448fa9e4066Sahrens cv_broadcast(&db->db_changed); 449fa9e4066Sahrens mutex_exit(&db->db_mtx); 450ea8dc4b6Seschrock dbuf_rele(db, NULL); 451fa9e4066Sahrens } 452fa9e4066Sahrens 453ea8dc4b6Seschrock static void 45413506d1eSmaybee dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags) 455fa9e4066Sahrens { 456088f3894Sahrens dnode_t *dn = db->db_dnode; 457ea8dc4b6Seschrock zbookmark_t zb; 45813506d1eSmaybee uint32_t aflags = ARC_NOWAIT; 459088f3894Sahrens arc_buf_t *pbuf; 460fa9e4066Sahrens 461fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 462fa9e4066Sahrens /* We need the struct_rwlock to prevent db_blkptr from changing. */ 463088f3894Sahrens ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 464ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&db->db_mtx)); 465ea8dc4b6Seschrock ASSERT(db->db_state == DB_UNCACHED); 466ea8dc4b6Seschrock ASSERT(db->db_buf == NULL); 467fa9e4066Sahrens 468fa9e4066Sahrens if (db->db_blkid == DB_BONUS_BLKID) { 469088f3894Sahrens int bonuslen = dn->dn_bonuslen; 4701934e92fSmaybee 4711934e92fSmaybee ASSERT3U(bonuslen, <=, db->db.db_size); 472ea8dc4b6Seschrock db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN); 47325c2057bSmaybee arc_space_consume(DN_MAX_BONUSLEN); 4741934e92fSmaybee if (bonuslen < DN_MAX_BONUSLEN) 475ea8dc4b6Seschrock bzero(db->db.db_data, DN_MAX_BONUSLEN); 476088f3894Sahrens bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, 4771934e92fSmaybee bonuslen); 478ea8dc4b6Seschrock dbuf_update_data(db); 479fa9e4066Sahrens db->db_state = DB_CACHED; 480fa9e4066Sahrens mutex_exit(&db->db_mtx); 481fa9e4066Sahrens return; 482fa9e4066Sahrens } 483fa9e4066Sahrens 484088f3894Sahrens if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) || 485088f3894Sahrens (db->db_level == 0 && dnode_block_freed(dn, db->db_blkid))) { 486ad23a2dbSjohansen arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 487ad23a2dbSjohansen 488088f3894Sahrens dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa, 489ad23a2dbSjohansen db->db.db_size, db, type)); 490fa9e4066Sahrens bzero(db->db.db_data, db->db.db_size); 491fa9e4066Sahrens db->db_state = DB_CACHED; 49213506d1eSmaybee *flags |= DB_RF_CACHED; 493fa9e4066Sahrens mutex_exit(&db->db_mtx); 494fa9e4066Sahrens return; 495fa9e4066Sahrens } 496fa9e4066Sahrens 497fa9e4066Sahrens db->db_state = DB_READ; 498fa9e4066Sahrens mutex_exit(&db->db_mtx); 499fa9e4066Sahrens 500*3baa08fcSek110237 if (DBUF_IS_L2CACHEABLE(db)) 501*3baa08fcSek110237 aflags |= ARC_L2CACHE; 502*3baa08fcSek110237 503ea8dc4b6Seschrock zb.zb_objset = db->db_objset->os_dsl_dataset ? 504ea8dc4b6Seschrock db->db_objset->os_dsl_dataset->ds_object : 0; 505ea8dc4b6Seschrock zb.zb_object = db->db.db_object; 506ea8dc4b6Seschrock zb.zb_level = db->db_level; 507ea8dc4b6Seschrock zb.zb_blkid = db->db_blkid; 508ea8dc4b6Seschrock 509ea8dc4b6Seschrock dbuf_add_ref(db, NULL); 510fa9e4066Sahrens /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */ 511088f3894Sahrens 512088f3894Sahrens if (db->db_parent) 513088f3894Sahrens pbuf = db->db_parent->db_buf; 514088f3894Sahrens else 515088f3894Sahrens pbuf = db->db_objset->os_phys_buf; 516088f3894Sahrens 517088f3894Sahrens (void) arc_read(zio, dn->dn_objset->os_spa, db->db_blkptr, pbuf, 518fa9e4066Sahrens dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, 51913506d1eSmaybee (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED, 52013506d1eSmaybee &aflags, &zb); 52113506d1eSmaybee if (aflags & ARC_CACHED) 52213506d1eSmaybee *flags |= DB_RF_CACHED; 523fa9e4066Sahrens } 524fa9e4066Sahrens 525ea8dc4b6Seschrock int 526ea8dc4b6Seschrock dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 527fa9e4066Sahrens { 528ea8dc4b6Seschrock int err = 0; 529ea8dc4b6Seschrock int havepzio = (zio != NULL); 53013506d1eSmaybee int prefetch; 531fa9e4066Sahrens 532fa9e4066Sahrens /* 533fa9e4066Sahrens * We don't have to hold the mutex to check db_state because it 534fa9e4066Sahrens * can't be freed while we have a hold on the buffer. 535fa9e4066Sahrens */ 536fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 537fa9e4066Sahrens 538fa9e4066Sahrens if ((flags & DB_RF_HAVESTRUCT) == 0) 539fa9e4066Sahrens rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 540fa9e4066Sahrens 54113506d1eSmaybee prefetch = db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID && 542*3baa08fcSek110237 (flags & DB_RF_NOPREFETCH) == 0 && db->db_dnode != NULL && 543*3baa08fcSek110237 DBUF_IS_CACHEABLE(db); 54413506d1eSmaybee 545fa9e4066Sahrens mutex_enter(&db->db_mtx); 546ea8dc4b6Seschrock if (db->db_state == DB_CACHED) { 547ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 54813506d1eSmaybee if (prefetch) 54913506d1eSmaybee dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset, 55013506d1eSmaybee db->db.db_size, TRUE); 551ea8dc4b6Seschrock if ((flags & DB_RF_HAVESTRUCT) == 0) 552ea8dc4b6Seschrock rw_exit(&db->db_dnode->dn_struct_rwlock); 553ea8dc4b6Seschrock } else if (db->db_state == DB_UNCACHED) { 554ea8dc4b6Seschrock if (zio == NULL) { 555ea8dc4b6Seschrock zio = zio_root(db->db_dnode->dn_objset->os_spa, 556ea8dc4b6Seschrock NULL, NULL, ZIO_FLAG_CANFAIL); 557ea8dc4b6Seschrock } 55813506d1eSmaybee dbuf_read_impl(db, zio, &flags); 55913506d1eSmaybee 560ea8dc4b6Seschrock /* dbuf_read_impl has dropped db_mtx for us */ 561ea8dc4b6Seschrock 56213506d1eSmaybee if (prefetch) 563ea8dc4b6Seschrock dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset, 56413506d1eSmaybee db->db.db_size, flags & DB_RF_CACHED); 565ea8dc4b6Seschrock 566ea8dc4b6Seschrock if ((flags & DB_RF_HAVESTRUCT) == 0) 567ea8dc4b6Seschrock rw_exit(&db->db_dnode->dn_struct_rwlock); 568ea8dc4b6Seschrock 569ea8dc4b6Seschrock if (!havepzio) 570ea8dc4b6Seschrock err = zio_wait(zio); 571ea8dc4b6Seschrock } else { 57213506d1eSmaybee mutex_exit(&db->db_mtx); 57313506d1eSmaybee if (prefetch) 57413506d1eSmaybee dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset, 57513506d1eSmaybee db->db.db_size, TRUE); 576ea8dc4b6Seschrock if ((flags & DB_RF_HAVESTRUCT) == 0) 577ea8dc4b6Seschrock rw_exit(&db->db_dnode->dn_struct_rwlock); 57813506d1eSmaybee 57913506d1eSmaybee mutex_enter(&db->db_mtx); 580ea8dc4b6Seschrock if ((flags & DB_RF_NEVERWAIT) == 0) { 581ea8dc4b6Seschrock while (db->db_state == DB_READ || 582ea8dc4b6Seschrock db->db_state == DB_FILL) { 583fa9e4066Sahrens ASSERT(db->db_state == DB_READ || 584fa9e4066Sahrens (flags & DB_RF_HAVESTRUCT) == 0); 585fa9e4066Sahrens cv_wait(&db->db_changed, &db->db_mtx); 586fa9e4066Sahrens } 587ea8dc4b6Seschrock if (db->db_state == DB_UNCACHED) 588ea8dc4b6Seschrock err = EIO; 589ea8dc4b6Seschrock } 590fa9e4066Sahrens mutex_exit(&db->db_mtx); 591fa9e4066Sahrens } 592fa9e4066Sahrens 593ea8dc4b6Seschrock ASSERT(err || havepzio || db->db_state == DB_CACHED); 594ea8dc4b6Seschrock return (err); 595fa9e4066Sahrens } 596fa9e4066Sahrens 597fa9e4066Sahrens static void 598fa9e4066Sahrens dbuf_noread(dmu_buf_impl_t *db) 599fa9e4066Sahrens { 600fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 601ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 602fa9e4066Sahrens mutex_enter(&db->db_mtx); 603fa9e4066Sahrens while (db->db_state == DB_READ || db->db_state == DB_FILL) 604fa9e4066Sahrens cv_wait(&db->db_changed, &db->db_mtx); 605fa9e4066Sahrens if (db->db_state == DB_UNCACHED) { 606ad23a2dbSjohansen arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 607ad23a2dbSjohansen 608ea8dc4b6Seschrock ASSERT(db->db_buf == NULL); 609fa9e4066Sahrens ASSERT(db->db.db_data == NULL); 610fa9e4066Sahrens dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa, 611ad23a2dbSjohansen db->db.db_size, db, type)); 612fa9e4066Sahrens db->db_state = DB_FILL; 613fa9e4066Sahrens } else { 614fa9e4066Sahrens ASSERT3U(db->db_state, ==, DB_CACHED); 615fa9e4066Sahrens } 616fa9e4066Sahrens mutex_exit(&db->db_mtx); 617fa9e4066Sahrens } 618fa9e4066Sahrens 619fa9e4066Sahrens /* 620fa9e4066Sahrens * This is our just-in-time copy function. It makes a copy of 621fa9e4066Sahrens * buffers, that have been modified in a previous transaction 622fa9e4066Sahrens * group, before we modify them in the current active group. 623fa9e4066Sahrens * 624fa9e4066Sahrens * This function is used in two places: when we are dirtying a 625fa9e4066Sahrens * buffer for the first time in a txg, and when we are freeing 626fa9e4066Sahrens * a range in a dnode that includes this buffer. 627fa9e4066Sahrens * 628fa9e4066Sahrens * Note that when we are called from dbuf_free_range() we do 629fa9e4066Sahrens * not put a hold on the buffer, we just traverse the active 630fa9e4066Sahrens * dbuf list for the dnode. 631fa9e4066Sahrens */ 632fa9e4066Sahrens static void 633fa9e4066Sahrens dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg) 634fa9e4066Sahrens { 635c717a561Smaybee dbuf_dirty_record_t *dr = db->db_last_dirty; 636fa9e4066Sahrens 637fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 638fa9e4066Sahrens ASSERT(db->db.db_data != NULL); 639c717a561Smaybee ASSERT(db->db_level == 0); 640c717a561Smaybee ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT); 641fa9e4066Sahrens 6424d31c452Smaybee if (dr == NULL || 6434d31c452Smaybee (dr->dt.dl.dr_data != 6444d31c452Smaybee ((db->db_blkid == DB_BONUS_BLKID) ? db->db.db_data : db->db_buf))) 645fa9e4066Sahrens return; 646fa9e4066Sahrens 647fa9e4066Sahrens /* 648c717a561Smaybee * If the last dirty record for this dbuf has not yet synced 649c717a561Smaybee * and its referencing the dbuf data, either: 650c717a561Smaybee * reset the reference to point to a new copy, 651c717a561Smaybee * or (if there a no active holders) 652c717a561Smaybee * just null out the current db_data pointer. 653fa9e4066Sahrens */ 654c717a561Smaybee ASSERT(dr->dr_txg >= txg - 2); 655c717a561Smaybee if (db->db_blkid == DB_BONUS_BLKID) { 656c717a561Smaybee /* Note that the data bufs here are zio_bufs */ 657c717a561Smaybee dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN); 6580e8c6158Smaybee arc_space_consume(DN_MAX_BONUSLEN); 659c717a561Smaybee bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN); 660c717a561Smaybee } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) { 661ea8dc4b6Seschrock int size = db->db.db_size; 662c717a561Smaybee arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 663c717a561Smaybee dr->dt.dl.dr_data = arc_buf_alloc( 664ad23a2dbSjohansen db->db_dnode->dn_objset->os_spa, size, db, type); 665c717a561Smaybee bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size); 666fa9e4066Sahrens } else { 667ea8dc4b6Seschrock dbuf_set_data(db, NULL); 668fa9e4066Sahrens } 669fa9e4066Sahrens } 670ea8dc4b6Seschrock 671fa9e4066Sahrens void 672c717a561Smaybee dbuf_unoverride(dbuf_dirty_record_t *dr) 673fa9e4066Sahrens { 674c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 675c717a561Smaybee uint64_t txg = dr->dr_txg; 676c5c6ffa0Smaybee 677c717a561Smaybee ASSERT(MUTEX_HELD(&db->db_mtx)); 678c717a561Smaybee ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC); 679c717a561Smaybee ASSERT(db->db_level == 0); 680c717a561Smaybee 681c717a561Smaybee if (db->db_blkid == DB_BONUS_BLKID || 682c717a561Smaybee dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN) 683c717a561Smaybee return; 684c717a561Smaybee 685fa9e4066Sahrens /* free this block */ 686c717a561Smaybee if (!BP_IS_HOLE(&dr->dt.dl.dr_overridden_by)) { 687fa9e4066Sahrens /* XXX can get silent EIO here */ 688088f3894Sahrens (void) dsl_free(NULL, 689088f3894Sahrens spa_get_dsl(db->db_dnode->dn_objset->os_spa), 690c717a561Smaybee txg, &dr->dt.dl.dr_overridden_by, NULL, NULL, ARC_WAIT); 691fa9e4066Sahrens } 692c717a561Smaybee dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 6936b4acc8bSahrens /* 6946b4acc8bSahrens * Release the already-written buffer, so we leave it in 6956b4acc8bSahrens * a consistent dirty state. Note that all callers are 6966b4acc8bSahrens * modifying the buffer, so they will immediately do 6976b4acc8bSahrens * another (redundant) arc_release(). Therefore, leave 6986b4acc8bSahrens * the buf thawed to save the effort of freezing & 6996b4acc8bSahrens * immediately re-thawing it. 7006b4acc8bSahrens */ 701c717a561Smaybee arc_release(dr->dt.dl.dr_data, db); 702fa9e4066Sahrens } 703fa9e4066Sahrens 704cdb0ab79Smaybee /* 705cdb0ab79Smaybee * Evict (if its unreferenced) or clear (if its referenced) any level-0 706cdb0ab79Smaybee * data blocks in the free range, so that any future readers will find 707cdb0ab79Smaybee * empty blocks. Also, if we happen accross any level-1 dbufs in the 708cdb0ab79Smaybee * range that have not already been marked dirty, mark them dirty so 709cdb0ab79Smaybee * they stay in memory. 710cdb0ab79Smaybee */ 711fa9e4066Sahrens void 712cdb0ab79Smaybee dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx) 713fa9e4066Sahrens { 714fa9e4066Sahrens dmu_buf_impl_t *db, *db_next; 715fa9e4066Sahrens uint64_t txg = tx->tx_txg; 716cdb0ab79Smaybee int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 717cdb0ab79Smaybee uint64_t first_l1 = start >> epbs; 718cdb0ab79Smaybee uint64_t last_l1 = end >> epbs; 719fa9e4066Sahrens 720cdb0ab79Smaybee if (end > dn->dn_maxblkid) { 721cdb0ab79Smaybee end = dn->dn_maxblkid; 722cdb0ab79Smaybee last_l1 = end >> epbs; 723cdb0ab79Smaybee } 724cdb0ab79Smaybee dprintf_dnode(dn, "start=%llu end=%llu\n", start, end); 725fa9e4066Sahrens mutex_enter(&dn->dn_dbufs_mtx); 726fa9e4066Sahrens for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 727fa9e4066Sahrens db_next = list_next(&dn->dn_dbufs, db); 728ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 729cdb0ab79Smaybee 730cdb0ab79Smaybee if (db->db_level == 1 && 731cdb0ab79Smaybee db->db_blkid >= first_l1 && db->db_blkid <= last_l1) { 732cdb0ab79Smaybee mutex_enter(&db->db_mtx); 733cdb0ab79Smaybee if (db->db_last_dirty && 734cdb0ab79Smaybee db->db_last_dirty->dr_txg < txg) { 735cdb0ab79Smaybee dbuf_add_ref(db, FTAG); 736cdb0ab79Smaybee mutex_exit(&db->db_mtx); 737cdb0ab79Smaybee dbuf_will_dirty(db, tx); 738cdb0ab79Smaybee dbuf_rele(db, FTAG); 739cdb0ab79Smaybee } else { 740cdb0ab79Smaybee mutex_exit(&db->db_mtx); 741cdb0ab79Smaybee } 742cdb0ab79Smaybee } 743cdb0ab79Smaybee 744ea8dc4b6Seschrock if (db->db_level != 0) 745fa9e4066Sahrens continue; 746fa9e4066Sahrens dprintf_dbuf(db, "found buf %s\n", ""); 747cdb0ab79Smaybee if (db->db_blkid < start || db->db_blkid > end) 748fa9e4066Sahrens continue; 749fa9e4066Sahrens 750fa9e4066Sahrens /* found a level 0 buffer in the range */ 751fa9e4066Sahrens if (dbuf_undirty(db, tx)) 752fa9e4066Sahrens continue; 753fa9e4066Sahrens 754fa9e4066Sahrens mutex_enter(&db->db_mtx); 755ea8dc4b6Seschrock if (db->db_state == DB_UNCACHED || 756ea8dc4b6Seschrock db->db_state == DB_EVICTING) { 757fa9e4066Sahrens ASSERT(db->db.db_data == NULL); 758fa9e4066Sahrens mutex_exit(&db->db_mtx); 759fa9e4066Sahrens continue; 760fa9e4066Sahrens } 761c543ec06Sahrens if (db->db_state == DB_READ || db->db_state == DB_FILL) { 762c543ec06Sahrens /* will be handled in dbuf_read_done or dbuf_rele */ 763c717a561Smaybee db->db_freed_in_flight = TRUE; 764fa9e4066Sahrens mutex_exit(&db->db_mtx); 765fa9e4066Sahrens continue; 766fa9e4066Sahrens } 767ea8dc4b6Seschrock if (refcount_count(&db->db_holds) == 0) { 768ea8dc4b6Seschrock ASSERT(db->db_buf); 769ea8dc4b6Seschrock dbuf_clear(db); 770ea8dc4b6Seschrock continue; 771ea8dc4b6Seschrock } 772c717a561Smaybee /* The dbuf is referenced */ 773fa9e4066Sahrens 774c717a561Smaybee if (db->db_last_dirty != NULL) { 775c717a561Smaybee dbuf_dirty_record_t *dr = db->db_last_dirty; 776c717a561Smaybee 777c717a561Smaybee if (dr->dr_txg == txg) { 778ea8dc4b6Seschrock /* 779c717a561Smaybee * This buffer is "in-use", re-adjust the file 780c717a561Smaybee * size to reflect that this buffer may 781c717a561Smaybee * contain new data when we sync. 782ea8dc4b6Seschrock */ 78344eda4d7Smaybee if (db->db_blkid > dn->dn_maxblkid) 78444eda4d7Smaybee dn->dn_maxblkid = db->db_blkid; 785c717a561Smaybee dbuf_unoverride(dr); 786c717a561Smaybee } else { 787c717a561Smaybee /* 788c717a561Smaybee * This dbuf is not dirty in the open context. 789c717a561Smaybee * Either uncache it (if its not referenced in 790c717a561Smaybee * the open context) or reset its contents to 791c717a561Smaybee * empty. 792c717a561Smaybee */ 793c717a561Smaybee dbuf_fix_old_data(db, txg); 79444eda4d7Smaybee } 795c717a561Smaybee } 796c717a561Smaybee /* clear the contents if its cached */ 797ea8dc4b6Seschrock if (db->db_state == DB_CACHED) { 798ea8dc4b6Seschrock ASSERT(db->db.db_data != NULL); 799fa9e4066Sahrens arc_release(db->db_buf, db); 800fa9e4066Sahrens bzero(db->db.db_data, db->db.db_size); 8016b4acc8bSahrens arc_buf_freeze(db->db_buf); 802fa9e4066Sahrens } 803ea8dc4b6Seschrock 804fa9e4066Sahrens mutex_exit(&db->db_mtx); 805fa9e4066Sahrens } 806fa9e4066Sahrens mutex_exit(&dn->dn_dbufs_mtx); 807fa9e4066Sahrens } 808fa9e4066Sahrens 809fa9e4066Sahrens static int 8101934e92fSmaybee dbuf_block_freeable(dmu_buf_impl_t *db) 811fa9e4066Sahrens { 812fa9e4066Sahrens dsl_dataset_t *ds = db->db_objset->os_dsl_dataset; 813fa9e4066Sahrens uint64_t birth_txg = 0; 814fa9e4066Sahrens 815fa9e4066Sahrens /* 816fa9e4066Sahrens * We don't need any locking to protect db_blkptr: 817c717a561Smaybee * If it's syncing, then db_last_dirty will be set 818c717a561Smaybee * so we'll ignore db_blkptr. 819fa9e4066Sahrens */ 820c717a561Smaybee ASSERT(MUTEX_HELD(&db->db_mtx)); 821c717a561Smaybee if (db->db_last_dirty) 822c717a561Smaybee birth_txg = db->db_last_dirty->dr_txg; 823fa9e4066Sahrens else if (db->db_blkptr) 824fa9e4066Sahrens birth_txg = db->db_blkptr->blk_birth; 825fa9e4066Sahrens 8261934e92fSmaybee /* If we don't exist or are in a snapshot, we can't be freed */ 827fa9e4066Sahrens if (birth_txg) 8281934e92fSmaybee return (ds == NULL || 8291934e92fSmaybee dsl_dataset_block_freeable(ds, birth_txg)); 830fa9e4066Sahrens else 8311934e92fSmaybee return (FALSE); 832fa9e4066Sahrens } 833fa9e4066Sahrens 834fa9e4066Sahrens void 835fa9e4066Sahrens dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx) 836fa9e4066Sahrens { 837fa9e4066Sahrens arc_buf_t *buf, *obuf; 838fa9e4066Sahrens int osize = db->db.db_size; 839ad23a2dbSjohansen arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 840fa9e4066Sahrens 841ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 842ea8dc4b6Seschrock 843fa9e4066Sahrens /* XXX does *this* func really need the lock? */ 844fa9e4066Sahrens ASSERT(RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock)); 845fa9e4066Sahrens 846fa9e4066Sahrens /* 847fa9e4066Sahrens * This call to dbuf_will_dirty() with the dn_struct_rwlock held 848fa9e4066Sahrens * is OK, because there can be no other references to the db 849fa9e4066Sahrens * when we are changing its size, so no concurrent DB_FILL can 850fa9e4066Sahrens * be happening. 851fa9e4066Sahrens */ 852ea8dc4b6Seschrock /* 853ea8dc4b6Seschrock * XXX we should be doing a dbuf_read, checking the return 854ea8dc4b6Seschrock * value and returning that up to our callers 855ea8dc4b6Seschrock */ 856fa9e4066Sahrens dbuf_will_dirty(db, tx); 857fa9e4066Sahrens 858fa9e4066Sahrens /* create the data buffer for the new block */ 859ad23a2dbSjohansen buf = arc_buf_alloc(db->db_dnode->dn_objset->os_spa, size, db, type); 860fa9e4066Sahrens 861fa9e4066Sahrens /* copy old block data to the new block */ 862fa9e4066Sahrens obuf = db->db_buf; 863f65e61c0Sahrens bcopy(obuf->b_data, buf->b_data, MIN(osize, size)); 864fa9e4066Sahrens /* zero the remainder */ 865f65e61c0Sahrens if (size > osize) 866fa9e4066Sahrens bzero((uint8_t *)buf->b_data + osize, size - osize); 867fa9e4066Sahrens 868fa9e4066Sahrens mutex_enter(&db->db_mtx); 869fa9e4066Sahrens dbuf_set_data(db, buf); 870ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(obuf, db) == 1); 871fa9e4066Sahrens db->db.db_size = size; 872fa9e4066Sahrens 873c717a561Smaybee if (db->db_level == 0) { 874c717a561Smaybee ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg); 875c717a561Smaybee db->db_last_dirty->dt.dl.dr_data = buf; 876c717a561Smaybee } 877fa9e4066Sahrens mutex_exit(&db->db_mtx); 878fa9e4066Sahrens 879fa9e4066Sahrens dnode_willuse_space(db->db_dnode, size-osize, tx); 880fa9e4066Sahrens } 881fa9e4066Sahrens 882c717a561Smaybee dbuf_dirty_record_t * 883fa9e4066Sahrens dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 884fa9e4066Sahrens { 885fa9e4066Sahrens dnode_t *dn = db->db_dnode; 886fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 887c717a561Smaybee dbuf_dirty_record_t **drp, *dr; 888fa9e4066Sahrens int drop_struct_lock = FALSE; 889fa9e4066Sahrens int txgoff = tx->tx_txg & TXG_MASK; 890fa9e4066Sahrens 891fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 892fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 8939c9dc39aSek110237 DMU_TX_DIRTY_BUF(tx, db); 894fa9e4066Sahrens 895fa9e4066Sahrens /* 896fa9e4066Sahrens * Shouldn't dirty a regular buffer in syncing context. Private 897fa9e4066Sahrens * objects may be dirtied in syncing context, but only if they 898fa9e4066Sahrens * were already pre-dirtied in open context. 899fa9e4066Sahrens * XXX We may want to prohibit dirtying in syncing context even 900fa9e4066Sahrens * if they did pre-dirty. 901fa9e4066Sahrens */ 902c717a561Smaybee ASSERT(!dmu_tx_is_syncing(tx) || 903c717a561Smaybee BP_IS_HOLE(dn->dn_objset->os_rootbp) || 904c717a561Smaybee dn->dn_object == DMU_META_DNODE_OBJECT || 905c717a561Smaybee dn->dn_objset->os_dsl_dataset == NULL || 906c717a561Smaybee dsl_dir_is_private(dn->dn_objset->os_dsl_dataset->ds_dir)); 907fa9e4066Sahrens 908fa9e4066Sahrens /* 909fa9e4066Sahrens * We make this assert for private objects as well, but after we 910fa9e4066Sahrens * check if we're already dirty. They are allowed to re-dirty 911fa9e4066Sahrens * in syncing context. 912fa9e4066Sahrens */ 913ea8dc4b6Seschrock ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 914c717a561Smaybee dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 915fa9e4066Sahrens (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 916fa9e4066Sahrens 917fa9e4066Sahrens mutex_enter(&db->db_mtx); 918fa9e4066Sahrens /* 919c717a561Smaybee * XXX make this true for indirects too? The problem is that 920c717a561Smaybee * transactions created with dmu_tx_create_assigned() from 921c717a561Smaybee * syncing context don't bother holding ahead. 922fa9e4066Sahrens */ 923c717a561Smaybee ASSERT(db->db_level != 0 || 924c717a561Smaybee db->db_state == DB_CACHED || db->db_state == DB_FILL); 925fa9e4066Sahrens 926fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 927fa9e4066Sahrens /* 928fa9e4066Sahrens * Don't set dirtyctx to SYNC if we're just modifying this as we 929fa9e4066Sahrens * initialize the objset. 930fa9e4066Sahrens */ 931fa9e4066Sahrens if (dn->dn_dirtyctx == DN_UNDIRTIED && 932c717a561Smaybee !BP_IS_HOLE(dn->dn_objset->os_rootbp)) { 933fa9e4066Sahrens dn->dn_dirtyctx = 934fa9e4066Sahrens (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN); 935fa9e4066Sahrens ASSERT(dn->dn_dirtyctx_firstset == NULL); 936fa9e4066Sahrens dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP); 937fa9e4066Sahrens } 938fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 939fa9e4066Sahrens 940fa9e4066Sahrens /* 941fa9e4066Sahrens * If this buffer is already dirty, we're done. 942fa9e4066Sahrens */ 943c717a561Smaybee drp = &db->db_last_dirty; 944c717a561Smaybee ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg || 945c717a561Smaybee db->db.db_object == DMU_META_DNODE_OBJECT); 9467e2186e3Sbonwick while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg) 9477e2186e3Sbonwick drp = &dr->dr_next; 9487e2186e3Sbonwick if (dr && dr->dr_txg == tx->tx_txg) { 949c717a561Smaybee if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) { 950c717a561Smaybee /* 951c717a561Smaybee * If this buffer has already been written out, 952c717a561Smaybee * we now need to reset its state. 953c717a561Smaybee */ 9547e2186e3Sbonwick dbuf_unoverride(dr); 955c717a561Smaybee if (db->db.db_object != DMU_META_DNODE_OBJECT) 9566b4acc8bSahrens arc_buf_thaw(db->db_buf); 957c717a561Smaybee } 958fa9e4066Sahrens mutex_exit(&db->db_mtx); 9597e2186e3Sbonwick return (dr); 960fa9e4066Sahrens } 961fa9e4066Sahrens 962fa9e4066Sahrens /* 963fa9e4066Sahrens * Only valid if not already dirty. 964fa9e4066Sahrens */ 965fa9e4066Sahrens ASSERT(dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 966fa9e4066Sahrens (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 967fa9e4066Sahrens 968fa9e4066Sahrens ASSERT3U(dn->dn_nlevels, >, db->db_level); 969fa9e4066Sahrens ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) || 970fa9e4066Sahrens dn->dn_phys->dn_nlevels > db->db_level || 971fa9e4066Sahrens dn->dn_next_nlevels[txgoff] > db->db_level || 972fa9e4066Sahrens dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level || 973fa9e4066Sahrens dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level); 974fa9e4066Sahrens 975fa9e4066Sahrens /* 976fa9e4066Sahrens * We should only be dirtying in syncing context if it's the 977fa9e4066Sahrens * mos, a spa os, or we're initializing the os. However, we are 978fa9e4066Sahrens * allowed to dirty in syncing context provided we already 979fa9e4066Sahrens * dirtied it in open context. Hence we must make this 980fa9e4066Sahrens * assertion only if we're not already dirty. 981fa9e4066Sahrens */ 982fa9e4066Sahrens ASSERT(!dmu_tx_is_syncing(tx) || 983fa9e4066Sahrens os->os_dsl_dataset == NULL || 984fa9e4066Sahrens !dsl_dir_is_private(os->os_dsl_dataset->ds_dir) || 985c717a561Smaybee !BP_IS_HOLE(os->os_rootbp)); 986fa9e4066Sahrens ASSERT(db->db.db_size != 0); 987fa9e4066Sahrens 988fa9e4066Sahrens dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 989fa9e4066Sahrens 9901934e92fSmaybee if (db->db_blkid != DB_BONUS_BLKID) { 9911934e92fSmaybee /* 9921934e92fSmaybee * Update the accounting. 9931934e92fSmaybee */ 9941934e92fSmaybee if (dbuf_block_freeable(db)) { 9951934e92fSmaybee blkptr_t *bp = db->db_blkptr; 9961934e92fSmaybee int64_t willfree = (bp && !BP_IS_HOLE(bp)) ? 9971934e92fSmaybee bp_get_dasize(os->os_spa, bp) : db->db.db_size; 9981934e92fSmaybee /* 9991934e92fSmaybee * This is only a guess -- if the dbuf is dirty 10001934e92fSmaybee * in a previous txg, we don't know how much 10011934e92fSmaybee * space it will use on disk yet. We should 10021934e92fSmaybee * really have the struct_rwlock to access 10031934e92fSmaybee * db_blkptr, but since this is just a guess, 10041934e92fSmaybee * it's OK if we get an odd answer. 10051934e92fSmaybee */ 10061934e92fSmaybee dnode_willuse_space(dn, -willfree, tx); 10071934e92fSmaybee } 10081934e92fSmaybee dnode_willuse_space(dn, db->db.db_size, tx); 10091934e92fSmaybee } 10101934e92fSmaybee 1011ea8dc4b6Seschrock /* 1012ea8dc4b6Seschrock * If this buffer is dirty in an old transaction group we need 1013ea8dc4b6Seschrock * to make a copy of it so that the changes we make in this 1014ea8dc4b6Seschrock * transaction group won't leak out when we sync the older txg. 1015ea8dc4b6Seschrock */ 1016c717a561Smaybee dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP); 1017c717a561Smaybee if (db->db_level == 0) { 1018c717a561Smaybee void *data_old = db->db_buf; 1019c717a561Smaybee 1020ea8dc4b6Seschrock if (db->db_blkid == DB_BONUS_BLKID) { 1021c717a561Smaybee dbuf_fix_old_data(db, tx->tx_txg); 1022c717a561Smaybee data_old = db->db.db_data; 1023c717a561Smaybee } else if (db->db.db_object != DMU_META_DNODE_OBJECT) { 1024fa9e4066Sahrens /* 1025fa9e4066Sahrens * Release the data buffer from the cache so that we 1026fa9e4066Sahrens * can modify it without impacting possible other users 1027c717a561Smaybee * of this cached data block. Note that indirect 1028c717a561Smaybee * blocks and private objects are not released until the 1029c717a561Smaybee * syncing state (since they are only modified then). 1030fa9e4066Sahrens */ 1031fa9e4066Sahrens arc_release(db->db_buf, db); 1032fa9e4066Sahrens dbuf_fix_old_data(db, tx->tx_txg); 1033c717a561Smaybee data_old = db->db_buf; 1034fa9e4066Sahrens } 1035c717a561Smaybee ASSERT(data_old != NULL); 1036c717a561Smaybee dr->dt.dl.dr_data = data_old; 1037c717a561Smaybee } else { 1038c717a561Smaybee mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL); 1039c717a561Smaybee list_create(&dr->dt.di.dr_children, 1040c717a561Smaybee sizeof (dbuf_dirty_record_t), 1041c717a561Smaybee offsetof(dbuf_dirty_record_t, dr_dirty_node)); 1042fa9e4066Sahrens } 1043c717a561Smaybee dr->dr_dbuf = db; 1044c717a561Smaybee dr->dr_txg = tx->tx_txg; 1045c717a561Smaybee dr->dr_next = *drp; 1046c717a561Smaybee *drp = dr; 1047fa9e4066Sahrens 1048fa9e4066Sahrens /* 1049fa9e4066Sahrens * We could have been freed_in_flight between the dbuf_noread 1050fa9e4066Sahrens * and dbuf_dirty. We win, as though the dbuf_noread() had 1051fa9e4066Sahrens * happened after the free. 1052fa9e4066Sahrens */ 1053fa9e4066Sahrens if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) { 1054c717a561Smaybee mutex_enter(&dn->dn_mtx); 1055fa9e4066Sahrens dnode_clear_range(dn, db->db_blkid, 1, tx); 1056fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1057c717a561Smaybee db->db_freed_in_flight = FALSE; 1058c717a561Smaybee } 1059fa9e4066Sahrens 1060fa9e4066Sahrens /* 1061fa9e4066Sahrens * This buffer is now part of this txg 1062fa9e4066Sahrens */ 1063fa9e4066Sahrens dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg); 1064fa9e4066Sahrens db->db_dirtycnt += 1; 1065fa9e4066Sahrens ASSERT3U(db->db_dirtycnt, <=, 3); 1066fa9e4066Sahrens 1067fa9e4066Sahrens mutex_exit(&db->db_mtx); 1068fa9e4066Sahrens 1069fa9e4066Sahrens if (db->db_blkid == DB_BONUS_BLKID) { 1070c717a561Smaybee mutex_enter(&dn->dn_mtx); 1071c717a561Smaybee ASSERT(!list_link_active(&dr->dr_dirty_node)); 1072c717a561Smaybee list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1073c717a561Smaybee mutex_exit(&dn->dn_mtx); 1074fa9e4066Sahrens dnode_setdirty(dn, tx); 1075c717a561Smaybee return (dr); 1076fa9e4066Sahrens } 1077fa9e4066Sahrens 107844eda4d7Smaybee if (db->db_level == 0) { 1079fa9e4066Sahrens dnode_new_blkid(dn, db->db_blkid, tx); 108044eda4d7Smaybee ASSERT(dn->dn_maxblkid >= db->db_blkid); 108144eda4d7Smaybee } 1082fa9e4066Sahrens 1083fa9e4066Sahrens if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 1084fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 1085fa9e4066Sahrens drop_struct_lock = TRUE; 1086fa9e4066Sahrens } 1087fa9e4066Sahrens 108844eda4d7Smaybee if (db->db_level+1 < dn->dn_nlevels) { 1089c717a561Smaybee dmu_buf_impl_t *parent = db->db_parent; 1090c717a561Smaybee dbuf_dirty_record_t *di; 1091c717a561Smaybee int parent_held = FALSE; 1092c717a561Smaybee 1093c717a561Smaybee if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) { 1094fa9e4066Sahrens int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1095c717a561Smaybee 1096fa9e4066Sahrens parent = dbuf_hold_level(dn, db->db_level+1, 1097fa9e4066Sahrens db->db_blkid >> epbs, FTAG); 1098c717a561Smaybee parent_held = TRUE; 1099c717a561Smaybee } 1100fa9e4066Sahrens if (drop_struct_lock) 1101fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 1102c717a561Smaybee ASSERT3U(db->db_level+1, ==, parent->db_level); 1103c717a561Smaybee di = dbuf_dirty(parent, tx); 1104c717a561Smaybee if (parent_held) 1105ea8dc4b6Seschrock dbuf_rele(parent, FTAG); 1106c717a561Smaybee 1107c717a561Smaybee mutex_enter(&db->db_mtx); 1108c717a561Smaybee /* possible race with dbuf_undirty() */ 1109c717a561Smaybee if (db->db_last_dirty == dr || 1110c717a561Smaybee dn->dn_object == DMU_META_DNODE_OBJECT) { 1111c717a561Smaybee mutex_enter(&di->dt.di.dr_mtx); 1112c717a561Smaybee ASSERT3U(di->dr_txg, ==, tx->tx_txg); 1113c717a561Smaybee ASSERT(!list_link_active(&dr->dr_dirty_node)); 1114c717a561Smaybee list_insert_tail(&di->dt.di.dr_children, dr); 1115c717a561Smaybee mutex_exit(&di->dt.di.dr_mtx); 1116c717a561Smaybee dr->dr_parent = di; 1117c717a561Smaybee } 1118c717a561Smaybee mutex_exit(&db->db_mtx); 1119fa9e4066Sahrens } else { 1120c717a561Smaybee ASSERT(db->db_level+1 == dn->dn_nlevels); 1121c717a561Smaybee ASSERT(db->db_blkid < dn->dn_nblkptr); 1122c717a561Smaybee ASSERT(db->db_parent == NULL || 1123c717a561Smaybee db->db_parent == db->db_dnode->dn_dbuf); 1124c717a561Smaybee mutex_enter(&dn->dn_mtx); 1125c717a561Smaybee ASSERT(!list_link_active(&dr->dr_dirty_node)); 1126c717a561Smaybee list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1127c717a561Smaybee mutex_exit(&dn->dn_mtx); 1128fa9e4066Sahrens if (drop_struct_lock) 1129fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 1130fa9e4066Sahrens } 1131fa9e4066Sahrens 1132fa9e4066Sahrens dnode_setdirty(dn, tx); 1133c717a561Smaybee return (dr); 1134fa9e4066Sahrens } 1135fa9e4066Sahrens 1136fa9e4066Sahrens static int 1137fa9e4066Sahrens dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1138fa9e4066Sahrens { 1139fa9e4066Sahrens dnode_t *dn = db->db_dnode; 1140c717a561Smaybee uint64_t txg = tx->tx_txg; 114117f17c2dSbonwick dbuf_dirty_record_t *dr, **drp; 1142fa9e4066Sahrens 1143c717a561Smaybee ASSERT(txg != 0); 1144ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 1145fa9e4066Sahrens 1146fa9e4066Sahrens mutex_enter(&db->db_mtx); 1147fa9e4066Sahrens 1148fa9e4066Sahrens /* 1149fa9e4066Sahrens * If this buffer is not dirty, we're done. 1150fa9e4066Sahrens */ 115117f17c2dSbonwick for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1152c717a561Smaybee if (dr->dr_txg <= txg) 1153c717a561Smaybee break; 1154c717a561Smaybee if (dr == NULL || dr->dr_txg < txg) { 1155fa9e4066Sahrens mutex_exit(&db->db_mtx); 1156fa9e4066Sahrens return (0); 1157fa9e4066Sahrens } 1158c717a561Smaybee ASSERT(dr->dr_txg == txg); 1159fa9e4066Sahrens 1160fa9e4066Sahrens /* 1161fa9e4066Sahrens * If this buffer is currently held, we cannot undirty 1162fa9e4066Sahrens * it, since one of the current holders may be in the 1163fa9e4066Sahrens * middle of an update. Note that users of dbuf_undirty() 1164fa9e4066Sahrens * should not place a hold on the dbuf before the call. 1165fa9e4066Sahrens */ 1166fa9e4066Sahrens if (refcount_count(&db->db_holds) > db->db_dirtycnt) { 1167fa9e4066Sahrens mutex_exit(&db->db_mtx); 116844eda4d7Smaybee /* Make sure we don't toss this buffer at sync phase */ 1169fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 1170fa9e4066Sahrens dnode_clear_range(dn, db->db_blkid, 1, tx); 1171fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1172fa9e4066Sahrens return (0); 1173fa9e4066Sahrens } 1174fa9e4066Sahrens 1175fa9e4066Sahrens dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 1176fa9e4066Sahrens 1177fa9e4066Sahrens ASSERT(db->db.db_size != 0); 1178fa9e4066Sahrens 1179fa9e4066Sahrens /* XXX would be nice to fix up dn_towrite_space[] */ 1180fa9e4066Sahrens 118117f17c2dSbonwick *drp = dr->dr_next; 1182c717a561Smaybee 1183c717a561Smaybee if (dr->dr_parent) { 1184c717a561Smaybee mutex_enter(&dr->dr_parent->dt.di.dr_mtx); 1185c717a561Smaybee list_remove(&dr->dr_parent->dt.di.dr_children, dr); 1186c717a561Smaybee mutex_exit(&dr->dr_parent->dt.di.dr_mtx); 1187c717a561Smaybee } else if (db->db_level+1 == dn->dn_nlevels) { 1188cdb0ab79Smaybee ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf); 1189fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 1190c717a561Smaybee list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr); 1191fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1192c717a561Smaybee } 1193c717a561Smaybee 1194c717a561Smaybee if (db->db_level == 0) { 1195c717a561Smaybee dbuf_unoverride(dr); 1196c717a561Smaybee 1197c717a561Smaybee ASSERT(db->db_buf != NULL); 1198c717a561Smaybee ASSERT(dr->dt.dl.dr_data != NULL); 1199c717a561Smaybee if (dr->dt.dl.dr_data != db->db_buf) 1200c717a561Smaybee VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1); 1201c717a561Smaybee } else { 1202c717a561Smaybee ASSERT(db->db_buf != NULL); 1203c717a561Smaybee ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 1204c25056deSgw25295 mutex_destroy(&dr->dt.di.dr_mtx); 1205c25056deSgw25295 list_destroy(&dr->dt.di.dr_children); 1206c717a561Smaybee } 1207c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 1208fa9e4066Sahrens 1209fa9e4066Sahrens ASSERT(db->db_dirtycnt > 0); 1210fa9e4066Sahrens db->db_dirtycnt -= 1; 1211fa9e4066Sahrens 1212c717a561Smaybee if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) { 1213ea8dc4b6Seschrock arc_buf_t *buf = db->db_buf; 1214fa9e4066Sahrens 1215ea8dc4b6Seschrock ASSERT(arc_released(buf)); 1216ea8dc4b6Seschrock dbuf_set_data(db, NULL); 1217ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(buf, db) == 1); 1218fa9e4066Sahrens dbuf_evict(db); 1219fa9e4066Sahrens return (1); 1220fa9e4066Sahrens } 1221fa9e4066Sahrens 1222fa9e4066Sahrens mutex_exit(&db->db_mtx); 1223fa9e4066Sahrens return (0); 1224fa9e4066Sahrens } 1225fa9e4066Sahrens 1226fa9e4066Sahrens #pragma weak dmu_buf_will_dirty = dbuf_will_dirty 1227fa9e4066Sahrens void 1228fa9e4066Sahrens dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1229fa9e4066Sahrens { 12301ab7f2deSmaybee int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH; 1231fa9e4066Sahrens 1232fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1233fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 1234fa9e4066Sahrens 1235fa9e4066Sahrens if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock)) 1236fa9e4066Sahrens rf |= DB_RF_HAVESTRUCT; 1237ea8dc4b6Seschrock (void) dbuf_read(db, NULL, rf); 1238c717a561Smaybee (void) dbuf_dirty(db, tx); 1239fa9e4066Sahrens } 1240fa9e4066Sahrens 1241fa9e4066Sahrens void 1242ea8dc4b6Seschrock dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 1243fa9e4066Sahrens { 1244ea8dc4b6Seschrock dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1245ea8dc4b6Seschrock 1246ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 1247fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1248fa9e4066Sahrens ASSERT(db->db_level == 0); 1249fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 1250fa9e4066Sahrens 1251ea8dc4b6Seschrock ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT || 1252fa9e4066Sahrens dmu_tx_private_ok(tx)); 1253fa9e4066Sahrens 1254fa9e4066Sahrens dbuf_noread(db); 1255c717a561Smaybee (void) dbuf_dirty(db, tx); 1256fa9e4066Sahrens } 1257fa9e4066Sahrens 1258fa9e4066Sahrens #pragma weak dmu_buf_fill_done = dbuf_fill_done 1259fa9e4066Sahrens /* ARGSUSED */ 1260fa9e4066Sahrens void 1261fa9e4066Sahrens dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx) 1262fa9e4066Sahrens { 1263fa9e4066Sahrens mutex_enter(&db->db_mtx); 12649c9dc39aSek110237 DBUF_VERIFY(db); 1265fa9e4066Sahrens 1266fa9e4066Sahrens if (db->db_state == DB_FILL) { 1267c717a561Smaybee if (db->db_level == 0 && db->db_freed_in_flight) { 1268ea8dc4b6Seschrock ASSERT(db->db_blkid != DB_BONUS_BLKID); 1269fa9e4066Sahrens /* we were freed while filling */ 1270fa9e4066Sahrens /* XXX dbuf_undirty? */ 1271fa9e4066Sahrens bzero(db->db.db_data, db->db.db_size); 1272c717a561Smaybee db->db_freed_in_flight = FALSE; 1273fa9e4066Sahrens } 1274fa9e4066Sahrens db->db_state = DB_CACHED; 1275fa9e4066Sahrens cv_broadcast(&db->db_changed); 1276fa9e4066Sahrens } 1277fa9e4066Sahrens mutex_exit(&db->db_mtx); 1278fa9e4066Sahrens } 1279fa9e4066Sahrens 1280ea8dc4b6Seschrock /* 1281ea8dc4b6Seschrock * "Clear" the contents of this dbuf. This will mark the dbuf 1282ea8dc4b6Seschrock * EVICTING and clear *most* of its references. Unfortunetely, 1283ea8dc4b6Seschrock * when we are not holding the dn_dbufs_mtx, we can't clear the 1284ea8dc4b6Seschrock * entry in the dn_dbufs list. We have to wait until dbuf_destroy() 1285ea8dc4b6Seschrock * in this case. For callers from the DMU we will usually see: 1286ea8dc4b6Seschrock * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy() 1287ea8dc4b6Seschrock * For the arc callback, we will usually see: 1288ea8dc4b6Seschrock * dbuf_do_evict()->dbuf_clear();dbuf_destroy() 1289ea8dc4b6Seschrock * Sometimes, though, we will get a mix of these two: 1290ea8dc4b6Seschrock * DMU: dbuf_clear()->arc_buf_evict() 1291ea8dc4b6Seschrock * ARC: dbuf_do_evict()->dbuf_destroy() 1292ea8dc4b6Seschrock */ 1293ea8dc4b6Seschrock void 1294fa9e4066Sahrens dbuf_clear(dmu_buf_impl_t *db) 1295fa9e4066Sahrens { 1296fa9e4066Sahrens dnode_t *dn = db->db_dnode; 1297ea8dc4b6Seschrock dmu_buf_impl_t *parent = db->db_parent; 1298c543ec06Sahrens dmu_buf_impl_t *dndb = dn->dn_dbuf; 1299ea8dc4b6Seschrock int dbuf_gone = FALSE; 1300fa9e4066Sahrens 1301fa9e4066Sahrens ASSERT(MUTEX_HELD(&db->db_mtx)); 1302fa9e4066Sahrens ASSERT(refcount_is_zero(&db->db_holds)); 1303fa9e4066Sahrens 1304ea8dc4b6Seschrock dbuf_evict_user(db); 1305ea8dc4b6Seschrock 1306fa9e4066Sahrens if (db->db_state == DB_CACHED) { 1307ea8dc4b6Seschrock ASSERT(db->db.db_data != NULL); 13080e8c6158Smaybee if (db->db_blkid == DB_BONUS_BLKID) { 1309ea8dc4b6Seschrock zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN); 13100e8c6158Smaybee arc_space_return(DN_MAX_BONUSLEN); 13110e8c6158Smaybee } 1312fa9e4066Sahrens db->db.db_data = NULL; 1313fa9e4066Sahrens db->db_state = DB_UNCACHED; 1314fa9e4066Sahrens } 1315fa9e4066Sahrens 1316fa9e4066Sahrens ASSERT3U(db->db_state, ==, DB_UNCACHED); 1317fa9e4066Sahrens ASSERT(db->db_data_pending == NULL); 1318fa9e4066Sahrens 1319ea8dc4b6Seschrock db->db_state = DB_EVICTING; 1320ea8dc4b6Seschrock db->db_blkptr = NULL; 1321ea8dc4b6Seschrock 1322ea8dc4b6Seschrock if (db->db_blkid != DB_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) { 1323ea8dc4b6Seschrock list_remove(&dn->dn_dbufs, db); 1324ea8dc4b6Seschrock dnode_rele(dn, db); 13251934e92fSmaybee db->db_dnode = NULL; 1326ea8dc4b6Seschrock } 1327ea8dc4b6Seschrock 1328ea8dc4b6Seschrock if (db->db_buf) 1329ea8dc4b6Seschrock dbuf_gone = arc_buf_evict(db->db_buf); 1330ea8dc4b6Seschrock 1331ea8dc4b6Seschrock if (!dbuf_gone) 1332fa9e4066Sahrens mutex_exit(&db->db_mtx); 1333fa9e4066Sahrens 1334fa9e4066Sahrens /* 1335fa9e4066Sahrens * If this dbuf is referened from an indirect dbuf, 1336fa9e4066Sahrens * decrement the ref count on the indirect dbuf. 1337fa9e4066Sahrens */ 1338c543ec06Sahrens if (parent && parent != dndb) 1339ea8dc4b6Seschrock dbuf_rele(parent, db); 1340fa9e4066Sahrens } 1341fa9e4066Sahrens 1342fa9e4066Sahrens static int 1343fa9e4066Sahrens dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse, 1344fa9e4066Sahrens dmu_buf_impl_t **parentp, blkptr_t **bpp) 1345fa9e4066Sahrens { 1346fa9e4066Sahrens int nlevels, epbs; 1347fa9e4066Sahrens 13480b69c2f0Sahrens *parentp = NULL; 13490b69c2f0Sahrens *bpp = NULL; 13500b69c2f0Sahrens 1351ea8dc4b6Seschrock ASSERT(blkid != DB_BONUS_BLKID); 1352ea8dc4b6Seschrock 1353fa9e4066Sahrens if (dn->dn_phys->dn_nlevels == 0) 1354fa9e4066Sahrens nlevels = 1; 1355fa9e4066Sahrens else 1356fa9e4066Sahrens nlevels = dn->dn_phys->dn_nlevels; 1357fa9e4066Sahrens 1358fa9e4066Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1359fa9e4066Sahrens 1360fa9e4066Sahrens ASSERT3U(level * epbs, <, 64); 1361fa9e4066Sahrens ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1362ea8dc4b6Seschrock if (level >= nlevels || 1363fa9e4066Sahrens (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) { 1364fa9e4066Sahrens /* the buffer has no parent yet */ 1365fa9e4066Sahrens return (ENOENT); 1366fa9e4066Sahrens } else if (level < nlevels-1) { 1367fa9e4066Sahrens /* this block is referenced from an indirect block */ 1368fa9e4066Sahrens int err = dbuf_hold_impl(dn, level+1, 1369fa9e4066Sahrens blkid >> epbs, fail_sparse, NULL, parentp); 1370fa9e4066Sahrens if (err) 1371fa9e4066Sahrens return (err); 1372ea8dc4b6Seschrock err = dbuf_read(*parentp, NULL, 1373ea8dc4b6Seschrock (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 1374c543ec06Sahrens if (err) { 1375c543ec06Sahrens dbuf_rele(*parentp, NULL); 1376c543ec06Sahrens *parentp = NULL; 1377c543ec06Sahrens return (err); 1378c543ec06Sahrens } 1379fa9e4066Sahrens *bpp = ((blkptr_t *)(*parentp)->db.db_data) + 1380fa9e4066Sahrens (blkid & ((1ULL << epbs) - 1)); 1381c543ec06Sahrens return (0); 1382fa9e4066Sahrens } else { 1383fa9e4066Sahrens /* the block is referenced from the dnode */ 1384fa9e4066Sahrens ASSERT3U(level, ==, nlevels-1); 1385fa9e4066Sahrens ASSERT(dn->dn_phys->dn_nblkptr == 0 || 1386fa9e4066Sahrens blkid < dn->dn_phys->dn_nblkptr); 1387c543ec06Sahrens if (dn->dn_dbuf) { 1388c543ec06Sahrens dbuf_add_ref(dn->dn_dbuf, NULL); 1389fa9e4066Sahrens *parentp = dn->dn_dbuf; 1390c543ec06Sahrens } 1391fa9e4066Sahrens *bpp = &dn->dn_phys->dn_blkptr[blkid]; 1392fa9e4066Sahrens return (0); 1393fa9e4066Sahrens } 1394fa9e4066Sahrens } 1395fa9e4066Sahrens 1396fa9e4066Sahrens static dmu_buf_impl_t * 1397fa9e4066Sahrens dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid, 1398fa9e4066Sahrens dmu_buf_impl_t *parent, blkptr_t *blkptr) 1399fa9e4066Sahrens { 1400fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 1401fa9e4066Sahrens dmu_buf_impl_t *db, *odb; 1402fa9e4066Sahrens 1403fa9e4066Sahrens ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1404fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 1405fa9e4066Sahrens 1406fa9e4066Sahrens db = kmem_cache_alloc(dbuf_cache, KM_SLEEP); 1407fa9e4066Sahrens 1408fa9e4066Sahrens db->db_objset = os; 1409fa9e4066Sahrens db->db.db_object = dn->dn_object; 1410fa9e4066Sahrens db->db_level = level; 1411fa9e4066Sahrens db->db_blkid = blkid; 1412c717a561Smaybee db->db_last_dirty = NULL; 1413ea8dc4b6Seschrock db->db_dirtycnt = 0; 1414ea8dc4b6Seschrock db->db_dnode = dn; 1415ea8dc4b6Seschrock db->db_parent = parent; 1416ea8dc4b6Seschrock db->db_blkptr = blkptr; 1417fa9e4066Sahrens 1418c717a561Smaybee db->db_user_ptr = NULL; 1419c717a561Smaybee db->db_user_data_ptr_ptr = NULL; 1420c717a561Smaybee db->db_evict_func = NULL; 1421c717a561Smaybee db->db_immediate_evict = 0; 1422c717a561Smaybee db->db_freed_in_flight = 0; 1423ea8dc4b6Seschrock 1424ea8dc4b6Seschrock if (blkid == DB_BONUS_BLKID) { 1425ea8dc4b6Seschrock ASSERT3P(parent, ==, dn->dn_dbuf); 14261934e92fSmaybee db->db.db_size = DN_MAX_BONUSLEN - 14271934e92fSmaybee (dn->dn_nblkptr-1) * sizeof (blkptr_t); 14281934e92fSmaybee ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 1429fa9e4066Sahrens db->db.db_offset = DB_BONUS_BLKID; 1430ea8dc4b6Seschrock db->db_state = DB_UNCACHED; 1431ea8dc4b6Seschrock /* the bonus dbuf is not placed in the hash table */ 14320e8c6158Smaybee arc_space_consume(sizeof (dmu_buf_impl_t)); 1433ea8dc4b6Seschrock return (db); 1434fa9e4066Sahrens } else { 1435fa9e4066Sahrens int blocksize = 1436fa9e4066Sahrens db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz; 1437fa9e4066Sahrens db->db.db_size = blocksize; 1438fa9e4066Sahrens db->db.db_offset = db->db_blkid * blocksize; 1439fa9e4066Sahrens } 1440fa9e4066Sahrens 1441fa9e4066Sahrens /* 1442fa9e4066Sahrens * Hold the dn_dbufs_mtx while we get the new dbuf 1443fa9e4066Sahrens * in the hash table *and* added to the dbufs list. 1444fa9e4066Sahrens * This prevents a possible deadlock with someone 1445fa9e4066Sahrens * trying to look up this dbuf before its added to the 1446fa9e4066Sahrens * dn_dbufs list. 1447fa9e4066Sahrens */ 1448fa9e4066Sahrens mutex_enter(&dn->dn_dbufs_mtx); 1449ea8dc4b6Seschrock db->db_state = DB_EVICTING; 1450fa9e4066Sahrens if ((odb = dbuf_hash_insert(db)) != NULL) { 1451fa9e4066Sahrens /* someone else inserted it first */ 1452fa9e4066Sahrens kmem_cache_free(dbuf_cache, db); 1453fa9e4066Sahrens mutex_exit(&dn->dn_dbufs_mtx); 1454fa9e4066Sahrens return (odb); 1455fa9e4066Sahrens } 1456fa9e4066Sahrens list_insert_head(&dn->dn_dbufs, db); 1457ea8dc4b6Seschrock db->db_state = DB_UNCACHED; 1458fa9e4066Sahrens mutex_exit(&dn->dn_dbufs_mtx); 14590e8c6158Smaybee arc_space_consume(sizeof (dmu_buf_impl_t)); 1460fa9e4066Sahrens 1461fa9e4066Sahrens if (parent && parent != dn->dn_dbuf) 1462fa9e4066Sahrens dbuf_add_ref(parent, db); 1463fa9e4066Sahrens 1464ea8dc4b6Seschrock ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 1465ea8dc4b6Seschrock refcount_count(&dn->dn_holds) > 0); 1466fa9e4066Sahrens (void) refcount_add(&dn->dn_holds, db); 1467fa9e4066Sahrens 1468fa9e4066Sahrens dprintf_dbuf(db, "db=%p\n", db); 1469fa9e4066Sahrens 1470fa9e4066Sahrens return (db); 1471fa9e4066Sahrens } 1472fa9e4066Sahrens 1473fa9e4066Sahrens static int 1474ea8dc4b6Seschrock dbuf_do_evict(void *private) 1475fa9e4066Sahrens { 1476ea8dc4b6Seschrock arc_buf_t *buf = private; 1477ea8dc4b6Seschrock dmu_buf_impl_t *db = buf->b_private; 1478fa9e4066Sahrens 1479ea8dc4b6Seschrock if (!MUTEX_HELD(&db->db_mtx)) 1480ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 1481ea8dc4b6Seschrock 1482ea8dc4b6Seschrock ASSERT(refcount_is_zero(&db->db_holds)); 1483ea8dc4b6Seschrock 1484ea8dc4b6Seschrock if (db->db_state != DB_EVICTING) { 1485ea8dc4b6Seschrock ASSERT(db->db_state == DB_CACHED); 14869c9dc39aSek110237 DBUF_VERIFY(db); 1487ea8dc4b6Seschrock db->db_buf = NULL; 1488ea8dc4b6Seschrock dbuf_evict(db); 1489ea8dc4b6Seschrock } else { 1490ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 1491ea8dc4b6Seschrock dbuf_destroy(db); 1492fa9e4066Sahrens } 1493ea8dc4b6Seschrock return (0); 1494fa9e4066Sahrens } 1495fa9e4066Sahrens 1496fa9e4066Sahrens static void 1497fa9e4066Sahrens dbuf_destroy(dmu_buf_impl_t *db) 1498fa9e4066Sahrens { 1499fa9e4066Sahrens ASSERT(refcount_is_zero(&db->db_holds)); 1500fa9e4066Sahrens 1501ea8dc4b6Seschrock if (db->db_blkid != DB_BONUS_BLKID) { 1502ea8dc4b6Seschrock /* 1503ea8dc4b6Seschrock * If this dbuf is still on the dn_dbufs list, 1504ea8dc4b6Seschrock * remove it from that list. 1505ea8dc4b6Seschrock */ 15061934e92fSmaybee if (db->db_dnode) { 15071934e92fSmaybee dnode_t *dn = db->db_dnode; 15081934e92fSmaybee 15091934e92fSmaybee mutex_enter(&dn->dn_dbufs_mtx); 1510ea8dc4b6Seschrock list_remove(&dn->dn_dbufs, db); 1511ea8dc4b6Seschrock mutex_exit(&dn->dn_dbufs_mtx); 1512ea8dc4b6Seschrock 1513ea8dc4b6Seschrock dnode_rele(dn, db); 15141934e92fSmaybee db->db_dnode = NULL; 1515ea8dc4b6Seschrock } 1516ea8dc4b6Seschrock dbuf_hash_remove(db); 1517ea8dc4b6Seschrock } 1518ea8dc4b6Seschrock db->db_parent = NULL; 1519ea8dc4b6Seschrock db->db_buf = NULL; 1520ea8dc4b6Seschrock 1521f82bfe17Sgw25295 ASSERT(!list_link_active(&db->db_link)); 1522fa9e4066Sahrens ASSERT(db->db.db_data == NULL); 1523fa9e4066Sahrens ASSERT(db->db_hash_next == NULL); 1524fa9e4066Sahrens ASSERT(db->db_blkptr == NULL); 1525fa9e4066Sahrens ASSERT(db->db_data_pending == NULL); 1526fa9e4066Sahrens 1527fa9e4066Sahrens kmem_cache_free(dbuf_cache, db); 15280e8c6158Smaybee arc_space_return(sizeof (dmu_buf_impl_t)); 1529fa9e4066Sahrens } 1530fa9e4066Sahrens 1531fa9e4066Sahrens void 1532fa9e4066Sahrens dbuf_prefetch(dnode_t *dn, uint64_t blkid) 1533fa9e4066Sahrens { 153413506d1eSmaybee dmu_buf_impl_t *db = NULL; 1535fa9e4066Sahrens blkptr_t *bp = NULL; 1536fa9e4066Sahrens 1537fa9e4066Sahrens ASSERT(blkid != DB_BONUS_BLKID); 1538fa9e4066Sahrens ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1539fa9e4066Sahrens 1540fa9e4066Sahrens if (dnode_block_freed(dn, blkid)) 1541fa9e4066Sahrens return; 1542fa9e4066Sahrens 1543fa9e4066Sahrens /* dbuf_find() returns with db_mtx held */ 1544fa9e4066Sahrens if (db = dbuf_find(dn, 0, blkid)) { 154513506d1eSmaybee if (refcount_count(&db->db_holds) > 0) { 1546fa9e4066Sahrens /* 154713506d1eSmaybee * This dbuf is active. We assume that it is 154813506d1eSmaybee * already CACHED, or else about to be either 1549fa9e4066Sahrens * read or filled. 1550fa9e4066Sahrens */ 1551fa9e4066Sahrens mutex_exit(&db->db_mtx); 1552fa9e4066Sahrens return; 1553fa9e4066Sahrens } 155413506d1eSmaybee mutex_exit(&db->db_mtx); 15550b69c2f0Sahrens db = NULL; 155613506d1eSmaybee } 1557fa9e4066Sahrens 155813506d1eSmaybee if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) { 1559fa9e4066Sahrens if (bp && !BP_IS_HOLE(bp)) { 1560088f3894Sahrens arc_buf_t *pbuf; 156113506d1eSmaybee uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 1562ea8dc4b6Seschrock zbookmark_t zb; 1563ea8dc4b6Seschrock zb.zb_objset = dn->dn_objset->os_dsl_dataset ? 1564ea8dc4b6Seschrock dn->dn_objset->os_dsl_dataset->ds_object : 0; 1565ea8dc4b6Seschrock zb.zb_object = dn->dn_object; 1566ea8dc4b6Seschrock zb.zb_level = 0; 1567ea8dc4b6Seschrock zb.zb_blkid = blkid; 1568ea8dc4b6Seschrock 1569088f3894Sahrens if (db) 1570088f3894Sahrens pbuf = db->db_buf; 1571088f3894Sahrens else 1572088f3894Sahrens pbuf = dn->dn_objset->os_phys_buf; 1573088f3894Sahrens 1574088f3894Sahrens (void) arc_read(NULL, dn->dn_objset->os_spa, 1575088f3894Sahrens bp, pbuf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 1576fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 157713506d1eSmaybee &aflags, &zb); 1578fa9e4066Sahrens } 157913506d1eSmaybee if (db) 158013506d1eSmaybee dbuf_rele(db, NULL); 1581fa9e4066Sahrens } 1582fa9e4066Sahrens } 1583fa9e4066Sahrens 1584fa9e4066Sahrens /* 1585fa9e4066Sahrens * Returns with db_holds incremented, and db_mtx not held. 1586fa9e4066Sahrens * Note: dn_struct_rwlock must be held. 1587fa9e4066Sahrens */ 1588fa9e4066Sahrens int 1589fa9e4066Sahrens dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse, 1590fa9e4066Sahrens void *tag, dmu_buf_impl_t **dbp) 1591fa9e4066Sahrens { 1592fa9e4066Sahrens dmu_buf_impl_t *db, *parent = NULL; 1593fa9e4066Sahrens 1594ea8dc4b6Seschrock ASSERT(blkid != DB_BONUS_BLKID); 1595fa9e4066Sahrens ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 1596fa9e4066Sahrens ASSERT3U(dn->dn_nlevels, >, level); 1597fa9e4066Sahrens 1598fa9e4066Sahrens *dbp = NULL; 1599ea8dc4b6Seschrock top: 1600fa9e4066Sahrens /* dbuf_find() returns with db_mtx held */ 1601fa9e4066Sahrens db = dbuf_find(dn, level, blkid); 1602fa9e4066Sahrens 1603fa9e4066Sahrens if (db == NULL) { 1604fa9e4066Sahrens blkptr_t *bp = NULL; 1605fa9e4066Sahrens int err; 1606fa9e4066Sahrens 1607c543ec06Sahrens ASSERT3P(parent, ==, NULL); 1608fa9e4066Sahrens err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp); 1609fa9e4066Sahrens if (fail_sparse) { 1610fa9e4066Sahrens if (err == 0 && bp && BP_IS_HOLE(bp)) 1611fa9e4066Sahrens err = ENOENT; 1612fa9e4066Sahrens if (err) { 1613c543ec06Sahrens if (parent) 1614ea8dc4b6Seschrock dbuf_rele(parent, NULL); 1615fa9e4066Sahrens return (err); 1616fa9e4066Sahrens } 1617fa9e4066Sahrens } 1618ea8dc4b6Seschrock if (err && err != ENOENT) 1619ea8dc4b6Seschrock return (err); 1620fa9e4066Sahrens db = dbuf_create(dn, level, blkid, parent, bp); 1621fa9e4066Sahrens } 1622fa9e4066Sahrens 1623ea8dc4b6Seschrock if (db->db_buf && refcount_is_zero(&db->db_holds)) { 1624ea8dc4b6Seschrock arc_buf_add_ref(db->db_buf, db); 1625ea8dc4b6Seschrock if (db->db_buf->b_data == NULL) { 1626ea8dc4b6Seschrock dbuf_clear(db); 1627c543ec06Sahrens if (parent) { 1628c543ec06Sahrens dbuf_rele(parent, NULL); 1629c543ec06Sahrens parent = NULL; 1630c543ec06Sahrens } 1631ea8dc4b6Seschrock goto top; 1632ea8dc4b6Seschrock } 1633ea8dc4b6Seschrock ASSERT3P(db->db.db_data, ==, db->db_buf->b_data); 1634ea8dc4b6Seschrock } 1635ea8dc4b6Seschrock 1636ea8dc4b6Seschrock ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf)); 1637ea8dc4b6Seschrock 1638fa9e4066Sahrens /* 1639c717a561Smaybee * If this buffer is currently syncing out, and we are are 1640c717a561Smaybee * still referencing it from db_data, we need to make a copy 1641c717a561Smaybee * of it in case we decide we want to dirty it again in this txg. 1642fa9e4066Sahrens */ 1643c717a561Smaybee if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID && 1644ea8dc4b6Seschrock dn->dn_object != DMU_META_DNODE_OBJECT && 1645c717a561Smaybee db->db_state == DB_CACHED && db->db_data_pending) { 1646c717a561Smaybee dbuf_dirty_record_t *dr = db->db_data_pending; 1647c717a561Smaybee 1648c717a561Smaybee if (dr->dt.dl.dr_data == db->db_buf) { 1649ad23a2dbSjohansen arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1650fa9e4066Sahrens 1651c717a561Smaybee dbuf_set_data(db, 1652c717a561Smaybee arc_buf_alloc(db->db_dnode->dn_objset->os_spa, 1653c717a561Smaybee db->db.db_size, db, type)); 1654c717a561Smaybee bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data, 1655fa9e4066Sahrens db->db.db_size); 1656fa9e4066Sahrens } 1657c717a561Smaybee } 1658fa9e4066Sahrens 1659ea8dc4b6Seschrock (void) refcount_add(&db->db_holds, tag); 1660fa9e4066Sahrens dbuf_update_data(db); 16619c9dc39aSek110237 DBUF_VERIFY(db); 1662fa9e4066Sahrens mutex_exit(&db->db_mtx); 1663fa9e4066Sahrens 1664fa9e4066Sahrens /* NOTE: we can't rele the parent until after we drop the db_mtx */ 1665c543ec06Sahrens if (parent) 1666ea8dc4b6Seschrock dbuf_rele(parent, NULL); 1667fa9e4066Sahrens 1668fa9e4066Sahrens ASSERT3P(db->db_dnode, ==, dn); 1669fa9e4066Sahrens ASSERT3U(db->db_blkid, ==, blkid); 1670fa9e4066Sahrens ASSERT3U(db->db_level, ==, level); 1671fa9e4066Sahrens *dbp = db; 1672fa9e4066Sahrens 1673fa9e4066Sahrens return (0); 1674fa9e4066Sahrens } 1675fa9e4066Sahrens 1676fa9e4066Sahrens dmu_buf_impl_t * 1677ea8dc4b6Seschrock dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag) 1678fa9e4066Sahrens { 1679fa9e4066Sahrens dmu_buf_impl_t *db; 1680ea8dc4b6Seschrock int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db); 1681ea8dc4b6Seschrock return (err ? NULL : db); 1682fa9e4066Sahrens } 1683fa9e4066Sahrens 1684fa9e4066Sahrens dmu_buf_impl_t * 1685fa9e4066Sahrens dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag) 1686fa9e4066Sahrens { 1687fa9e4066Sahrens dmu_buf_impl_t *db; 1688ea8dc4b6Seschrock int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db); 1689ea8dc4b6Seschrock return (err ? NULL : db); 1690fa9e4066Sahrens } 1691fa9e4066Sahrens 16921934e92fSmaybee void 1693ea8dc4b6Seschrock dbuf_create_bonus(dnode_t *dn) 1694fa9e4066Sahrens { 1695ea8dc4b6Seschrock ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 1696ea8dc4b6Seschrock 1697ea8dc4b6Seschrock ASSERT(dn->dn_bonus == NULL); 16981934e92fSmaybee dn->dn_bonus = dbuf_create(dn, 0, DB_BONUS_BLKID, dn->dn_dbuf, NULL); 1699fa9e4066Sahrens } 1700fa9e4066Sahrens 1701ea8dc4b6Seschrock #pragma weak dmu_buf_add_ref = dbuf_add_ref 1702fa9e4066Sahrens void 1703fa9e4066Sahrens dbuf_add_ref(dmu_buf_impl_t *db, void *tag) 1704fa9e4066Sahrens { 1705ea8dc4b6Seschrock int64_t holds = refcount_add(&db->db_holds, tag); 1706ea8dc4b6Seschrock ASSERT(holds > 1); 1707fa9e4066Sahrens } 1708fa9e4066Sahrens 1709ea8dc4b6Seschrock #pragma weak dmu_buf_rele = dbuf_rele 1710fa9e4066Sahrens void 1711ea8dc4b6Seschrock dbuf_rele(dmu_buf_impl_t *db, void *tag) 1712fa9e4066Sahrens { 1713fa9e4066Sahrens int64_t holds; 1714fa9e4066Sahrens 1715fa9e4066Sahrens mutex_enter(&db->db_mtx); 17169c9dc39aSek110237 DBUF_VERIFY(db); 1717fa9e4066Sahrens 1718fa9e4066Sahrens holds = refcount_remove(&db->db_holds, tag); 1719ea8dc4b6Seschrock ASSERT(holds >= 0); 1720fa9e4066Sahrens 1721c717a561Smaybee /* 1722c717a561Smaybee * We can't freeze indirects if there is a possibility that they 1723c717a561Smaybee * may be modified in the current syncing context. 1724c717a561Smaybee */ 1725c717a561Smaybee if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) 17266b4acc8bSahrens arc_buf_freeze(db->db_buf); 17276b4acc8bSahrens 1728fa9e4066Sahrens if (holds == db->db_dirtycnt && 1729c717a561Smaybee db->db_level == 0 && db->db_immediate_evict) 1730fa9e4066Sahrens dbuf_evict_user(db); 1731ea8dc4b6Seschrock 1732ea8dc4b6Seschrock if (holds == 0) { 1733ea8dc4b6Seschrock if (db->db_blkid == DB_BONUS_BLKID) { 1734ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 1735ea8dc4b6Seschrock dnode_rele(db->db_dnode, db); 1736ea8dc4b6Seschrock } else if (db->db_buf == NULL) { 1737ea8dc4b6Seschrock /* 1738ea8dc4b6Seschrock * This is a special case: we never associated this 1739ea8dc4b6Seschrock * dbuf with any data allocated from the ARC. 1740ea8dc4b6Seschrock */ 1741ea8dc4b6Seschrock ASSERT3U(db->db_state, ==, DB_UNCACHED); 1742ea8dc4b6Seschrock dbuf_evict(db); 1743ea8dc4b6Seschrock } else if (arc_released(db->db_buf)) { 1744ea8dc4b6Seschrock arc_buf_t *buf = db->db_buf; 1745ea8dc4b6Seschrock /* 1746ea8dc4b6Seschrock * This dbuf has anonymous data associated with it. 1747ea8dc4b6Seschrock */ 1748ea8dc4b6Seschrock dbuf_set_data(db, NULL); 1749ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(buf, db) == 1); 1750ea8dc4b6Seschrock dbuf_evict(db); 1751ea8dc4b6Seschrock } else { 1752ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0); 1753*3baa08fcSek110237 if (!DBUF_IS_CACHEABLE(db)) 1754*3baa08fcSek110237 dbuf_clear(db); 1755*3baa08fcSek110237 else 1756fa9e4066Sahrens mutex_exit(&db->db_mtx); 1757fa9e4066Sahrens } 1758ea8dc4b6Seschrock } else { 1759ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 1760fa9e4066Sahrens } 1761fa9e4066Sahrens } 1762fa9e4066Sahrens 1763fa9e4066Sahrens #pragma weak dmu_buf_refcount = dbuf_refcount 1764fa9e4066Sahrens uint64_t 1765fa9e4066Sahrens dbuf_refcount(dmu_buf_impl_t *db) 1766fa9e4066Sahrens { 1767fa9e4066Sahrens return (refcount_count(&db->db_holds)); 1768fa9e4066Sahrens } 1769fa9e4066Sahrens 1770fa9e4066Sahrens void * 1771fa9e4066Sahrens dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr, 1772fa9e4066Sahrens dmu_buf_evict_func_t *evict_func) 1773fa9e4066Sahrens { 1774fa9e4066Sahrens return (dmu_buf_update_user(db_fake, NULL, user_ptr, 1775fa9e4066Sahrens user_data_ptr_ptr, evict_func)); 1776fa9e4066Sahrens } 1777fa9e4066Sahrens 1778fa9e4066Sahrens void * 1779fa9e4066Sahrens dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr, 1780fa9e4066Sahrens dmu_buf_evict_func_t *evict_func) 1781fa9e4066Sahrens { 1782fa9e4066Sahrens dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1783fa9e4066Sahrens 1784c717a561Smaybee db->db_immediate_evict = TRUE; 1785fa9e4066Sahrens return (dmu_buf_update_user(db_fake, NULL, user_ptr, 1786fa9e4066Sahrens user_data_ptr_ptr, evict_func)); 1787fa9e4066Sahrens } 1788fa9e4066Sahrens 1789fa9e4066Sahrens void * 1790fa9e4066Sahrens dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr, 1791fa9e4066Sahrens void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func) 1792fa9e4066Sahrens { 1793fa9e4066Sahrens dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1794fa9e4066Sahrens ASSERT(db->db_level == 0); 1795fa9e4066Sahrens 1796fa9e4066Sahrens ASSERT((user_ptr == NULL) == (evict_func == NULL)); 1797fa9e4066Sahrens 1798fa9e4066Sahrens mutex_enter(&db->db_mtx); 1799fa9e4066Sahrens 1800c717a561Smaybee if (db->db_user_ptr == old_user_ptr) { 1801c717a561Smaybee db->db_user_ptr = user_ptr; 1802c717a561Smaybee db->db_user_data_ptr_ptr = user_data_ptr_ptr; 1803c717a561Smaybee db->db_evict_func = evict_func; 1804fa9e4066Sahrens 1805fa9e4066Sahrens dbuf_update_data(db); 1806fa9e4066Sahrens } else { 1807c717a561Smaybee old_user_ptr = db->db_user_ptr; 1808fa9e4066Sahrens } 1809fa9e4066Sahrens 1810fa9e4066Sahrens mutex_exit(&db->db_mtx); 1811fa9e4066Sahrens return (old_user_ptr); 1812fa9e4066Sahrens } 1813fa9e4066Sahrens 1814fa9e4066Sahrens void * 1815fa9e4066Sahrens dmu_buf_get_user(dmu_buf_t *db_fake) 1816fa9e4066Sahrens { 1817fa9e4066Sahrens dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1818fa9e4066Sahrens ASSERT(!refcount_is_zero(&db->db_holds)); 1819fa9e4066Sahrens 1820c717a561Smaybee return (db->db_user_ptr); 1821fa9e4066Sahrens } 1822fa9e4066Sahrens 1823c717a561Smaybee static void 1824c717a561Smaybee dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db) 1825fa9e4066Sahrens { 1826c717a561Smaybee /* ASSERT(dmu_tx_is_syncing(tx) */ 1827c717a561Smaybee ASSERT(MUTEX_HELD(&db->db_mtx)); 1828c717a561Smaybee 1829c717a561Smaybee if (db->db_blkptr != NULL) 1830c717a561Smaybee return; 1831c717a561Smaybee 1832c717a561Smaybee if (db->db_level == dn->dn_phys->dn_nlevels-1) { 1833c717a561Smaybee /* 1834c717a561Smaybee * This buffer was allocated at a time when there was 1835c717a561Smaybee * no available blkptrs from the dnode, or it was 1836c717a561Smaybee * inappropriate to hook it in (i.e., nlevels mis-match). 1837c717a561Smaybee */ 1838c717a561Smaybee ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr); 1839c717a561Smaybee ASSERT(db->db_parent == NULL); 1840c717a561Smaybee db->db_parent = dn->dn_dbuf; 1841c717a561Smaybee db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid]; 1842c717a561Smaybee DBUF_VERIFY(db); 1843c717a561Smaybee } else { 1844c717a561Smaybee dmu_buf_impl_t *parent = db->db_parent; 1845c717a561Smaybee int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1846c717a561Smaybee 1847c717a561Smaybee ASSERT(dn->dn_phys->dn_nlevels > 1); 1848c717a561Smaybee if (parent == NULL) { 1849c717a561Smaybee mutex_exit(&db->db_mtx); 1850c717a561Smaybee rw_enter(&dn->dn_struct_rwlock, RW_READER); 1851c717a561Smaybee (void) dbuf_hold_impl(dn, db->db_level+1, 1852c717a561Smaybee db->db_blkid >> epbs, FALSE, db, &parent); 1853c717a561Smaybee rw_exit(&dn->dn_struct_rwlock); 1854c717a561Smaybee mutex_enter(&db->db_mtx); 1855c717a561Smaybee db->db_parent = parent; 1856c717a561Smaybee } 1857c717a561Smaybee db->db_blkptr = (blkptr_t *)parent->db.db_data + 1858c717a561Smaybee (db->db_blkid & ((1ULL << epbs) - 1)); 1859c717a561Smaybee DBUF_VERIFY(db); 1860c717a561Smaybee } 1861c717a561Smaybee } 1862c717a561Smaybee 1863c717a561Smaybee static void 1864c717a561Smaybee dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 1865c717a561Smaybee { 1866c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 1867c717a561Smaybee dnode_t *dn = db->db_dnode; 1868c717a561Smaybee zio_t *zio; 1869c717a561Smaybee 1870c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx)); 1871c717a561Smaybee 1872c717a561Smaybee dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 1873c717a561Smaybee 1874c717a561Smaybee mutex_enter(&db->db_mtx); 1875c717a561Smaybee 1876c717a561Smaybee ASSERT(db->db_level > 0); 1877c717a561Smaybee DBUF_VERIFY(db); 1878c717a561Smaybee 1879c717a561Smaybee if (db->db_buf == NULL) { 1880c717a561Smaybee mutex_exit(&db->db_mtx); 1881c717a561Smaybee (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 1882c717a561Smaybee mutex_enter(&db->db_mtx); 1883c717a561Smaybee } 1884c717a561Smaybee ASSERT3U(db->db_state, ==, DB_CACHED); 1885c717a561Smaybee ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 1886c717a561Smaybee ASSERT(db->db_buf != NULL); 1887c717a561Smaybee 1888c717a561Smaybee dbuf_check_blkptr(dn, db); 1889c717a561Smaybee 1890c717a561Smaybee db->db_data_pending = dr; 1891c717a561Smaybee 1892af2c4821Smaybee mutex_exit(&db->db_mtx); 1893088f3894Sahrens dbuf_write(dr, db->db_buf, tx); 1894c717a561Smaybee 1895c717a561Smaybee zio = dr->dr_zio; 1896c717a561Smaybee mutex_enter(&dr->dt.di.dr_mtx); 1897c717a561Smaybee dbuf_sync_list(&dr->dt.di.dr_children, tx); 1898c717a561Smaybee ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 1899c717a561Smaybee mutex_exit(&dr->dt.di.dr_mtx); 1900c717a561Smaybee zio_nowait(zio); 1901c717a561Smaybee } 1902c717a561Smaybee 1903c717a561Smaybee static void 1904c717a561Smaybee dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 1905c717a561Smaybee { 1906c717a561Smaybee arc_buf_t **datap = &dr->dt.dl.dr_data; 1907c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 1908fa9e4066Sahrens dnode_t *dn = db->db_dnode; 1909fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 1910c717a561Smaybee uint64_t txg = tx->tx_txg; 1911fa9e4066Sahrens int blksz; 1912fa9e4066Sahrens 1913fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1914fa9e4066Sahrens 1915fa9e4066Sahrens dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 1916fa9e4066Sahrens 1917fa9e4066Sahrens mutex_enter(&db->db_mtx); 1918fa9e4066Sahrens /* 1919fa9e4066Sahrens * To be synced, we must be dirtied. But we 1920fa9e4066Sahrens * might have been freed after the dirty. 1921fa9e4066Sahrens */ 1922fa9e4066Sahrens if (db->db_state == DB_UNCACHED) { 1923fa9e4066Sahrens /* This buffer has been freed since it was dirtied */ 1924fa9e4066Sahrens ASSERT(db->db.db_data == NULL); 1925fa9e4066Sahrens } else if (db->db_state == DB_FILL) { 1926fa9e4066Sahrens /* This buffer was freed and is now being re-filled */ 1927c717a561Smaybee ASSERT(db->db.db_data != dr->dt.dl.dr_data); 1928fa9e4066Sahrens } else { 1929fa9e4066Sahrens ASSERT3U(db->db_state, ==, DB_CACHED); 1930fa9e4066Sahrens } 19319c9dc39aSek110237 DBUF_VERIFY(db); 1932fa9e4066Sahrens 1933fa9e4066Sahrens /* 1934c717a561Smaybee * If this is a bonus buffer, simply copy the bonus data into the 1935c717a561Smaybee * dnode. It will be written out when the dnode is synced (and it 1936c717a561Smaybee * will be synced, since it must have been dirty for dbuf_sync to 1937c717a561Smaybee * be called). 1938fa9e4066Sahrens */ 1939ea8dc4b6Seschrock if (db->db_blkid == DB_BONUS_BLKID) { 1940c717a561Smaybee dbuf_dirty_record_t **drp; 19411934e92fSmaybee 1942ea8dc4b6Seschrock ASSERT(*datap != NULL); 1943ea8dc4b6Seschrock ASSERT3U(db->db_level, ==, 0); 1944ea8dc4b6Seschrock ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN); 1945ea8dc4b6Seschrock bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen); 19460e8c6158Smaybee if (*datap != db->db.db_data) { 1947ea8dc4b6Seschrock zio_buf_free(*datap, DN_MAX_BONUSLEN); 19480e8c6158Smaybee arc_space_return(DN_MAX_BONUSLEN); 19490e8c6158Smaybee } 1950ea8dc4b6Seschrock db->db_data_pending = NULL; 1951c717a561Smaybee drp = &db->db_last_dirty; 1952c717a561Smaybee while (*drp != dr) 1953c717a561Smaybee drp = &(*drp)->dr_next; 195417f17c2dSbonwick ASSERT(dr->dr_next == NULL); 195517f17c2dSbonwick *drp = dr->dr_next; 1956c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 1957ea8dc4b6Seschrock ASSERT(db->db_dirtycnt > 0); 1958ea8dc4b6Seschrock db->db_dirtycnt -= 1; 1959ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 1960ea8dc4b6Seschrock dbuf_rele(db, (void *)(uintptr_t)txg); 1961ea8dc4b6Seschrock return; 1962ea8dc4b6Seschrock } 1963ea8dc4b6Seschrock 1964c5c6ffa0Smaybee /* 1965f82bfe17Sgw25295 * This function may have dropped the db_mtx lock allowing a dmu_sync 1966f82bfe17Sgw25295 * operation to sneak in. As a result, we need to ensure that we 1967f82bfe17Sgw25295 * don't check the dr_override_state until we have returned from 1968f82bfe17Sgw25295 * dbuf_check_blkptr. 1969f82bfe17Sgw25295 */ 1970f82bfe17Sgw25295 dbuf_check_blkptr(dn, db); 1971f82bfe17Sgw25295 1972f82bfe17Sgw25295 /* 1973c717a561Smaybee * If this buffer is in the middle of an immdiate write, 1974c717a561Smaybee * wait for the synchronous IO to complete. 1975c5c6ffa0Smaybee */ 1976c717a561Smaybee while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 1977c5c6ffa0Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1978c5c6ffa0Smaybee cv_wait(&db->db_changed, &db->db_mtx); 1979c717a561Smaybee ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN); 1980c5c6ffa0Smaybee } 1981c717a561Smaybee 1982c717a561Smaybee /* 1983c717a561Smaybee * If this dbuf has already been written out via an immediate write, 1984c717a561Smaybee * just complete the write by copying over the new block pointer and 1985c717a561Smaybee * updating the accounting via the write-completion functions. 1986c717a561Smaybee */ 1987c717a561Smaybee if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 1988c717a561Smaybee zio_t zio_fake; 1989c717a561Smaybee 1990c717a561Smaybee zio_fake.io_private = &db; 1991c717a561Smaybee zio_fake.io_error = 0; 1992c717a561Smaybee zio_fake.io_bp = db->db_blkptr; 1993c717a561Smaybee zio_fake.io_bp_orig = *db->db_blkptr; 1994c717a561Smaybee zio_fake.io_txg = txg; 1995c717a561Smaybee 1996c717a561Smaybee *db->db_blkptr = dr->dt.dl.dr_overridden_by; 1997c717a561Smaybee dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1998c717a561Smaybee db->db_data_pending = dr; 1999c717a561Smaybee dr->dr_zio = &zio_fake; 2000c717a561Smaybee mutex_exit(&db->db_mtx); 2001c717a561Smaybee 2002c717a561Smaybee if (BP_IS_OLDER(&zio_fake.io_bp_orig, txg)) 2003cdb0ab79Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 2004c717a561Smaybee &zio_fake.io_bp_orig, dn->dn_zio, tx); 2005c717a561Smaybee 2006c717a561Smaybee dbuf_write_ready(&zio_fake, db->db_buf, db); 2007c717a561Smaybee dbuf_write_done(&zio_fake, db->db_buf, db); 2008c717a561Smaybee 2009c717a561Smaybee return; 2010c717a561Smaybee } 2011c717a561Smaybee 2012c717a561Smaybee blksz = arc_buf_size(*datap); 2013c717a561Smaybee 2014c717a561Smaybee if (dn->dn_object != DMU_META_DNODE_OBJECT) { 2015fa9e4066Sahrens /* 2016fa9e4066Sahrens * If this buffer is currently "in use" (i.e., there are 2017fa9e4066Sahrens * active holds and db_data still references it), then make 2018fa9e4066Sahrens * a copy before we start the write so that any modifications 2019fa9e4066Sahrens * from the open txg will not leak into this write. 2020fa9e4066Sahrens * 2021fa9e4066Sahrens * NOTE: this copy does not need to be made for objects only 2022c717a561Smaybee * modified in the syncing context (e.g. DNONE_DNODE blocks). 2023fa9e4066Sahrens */ 2024c717a561Smaybee if (refcount_count(&db->db_holds) > 1 && *datap == db->db_buf) { 2025c717a561Smaybee arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 2026c717a561Smaybee *datap = arc_buf_alloc(os->os_spa, blksz, db, type); 2027c717a561Smaybee bcopy(db->db.db_data, (*datap)->b_data, blksz); 2028fa9e4066Sahrens } 2029fa9e4066Sahrens } 2030fa9e4066Sahrens 2031c717a561Smaybee ASSERT(*datap != NULL); 2032c717a561Smaybee db->db_data_pending = dr; 2033fa9e4066Sahrens 2034fa9e4066Sahrens mutex_exit(&db->db_mtx); 2035fa9e4066Sahrens 2036088f3894Sahrens dbuf_write(dr, *datap, tx); 2037c717a561Smaybee 2038c717a561Smaybee ASSERT(!list_link_active(&dr->dr_dirty_node)); 2039c717a561Smaybee if (dn->dn_object == DMU_META_DNODE_OBJECT) 2040c717a561Smaybee list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr); 2041c717a561Smaybee else 2042c717a561Smaybee zio_nowait(dr->dr_zio); 2043fa9e4066Sahrens } 2044c717a561Smaybee 2045c717a561Smaybee void 2046c717a561Smaybee dbuf_sync_list(list_t *list, dmu_tx_t *tx) 2047c717a561Smaybee { 2048c717a561Smaybee dbuf_dirty_record_t *dr; 2049c717a561Smaybee 2050c717a561Smaybee while (dr = list_head(list)) { 2051c717a561Smaybee if (dr->dr_zio != NULL) { 2052c717a561Smaybee /* 2053c717a561Smaybee * If we find an already initialized zio then we 2054c717a561Smaybee * are processing the meta-dnode, and we have finished. 2055c717a561Smaybee * The dbufs for all dnodes are put back on the list 2056c717a561Smaybee * during processing, so that we can zio_wait() 2057c717a561Smaybee * these IOs after initiating all child IOs. 2058c717a561Smaybee */ 2059c717a561Smaybee ASSERT3U(dr->dr_dbuf->db.db_object, ==, 2060c717a561Smaybee DMU_META_DNODE_OBJECT); 2061c717a561Smaybee break; 2062fa9e4066Sahrens } 2063c717a561Smaybee list_remove(list, dr); 2064c717a561Smaybee if (dr->dr_dbuf->db_level > 0) 2065c717a561Smaybee dbuf_sync_indirect(dr, tx); 2066c717a561Smaybee else 2067c717a561Smaybee dbuf_sync_leaf(dr, tx); 2068c717a561Smaybee } 2069c717a561Smaybee } 2070c717a561Smaybee 2071c717a561Smaybee static void 2072088f3894Sahrens dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx) 2073c717a561Smaybee { 2074c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 2075c717a561Smaybee dnode_t *dn = db->db_dnode; 2076c717a561Smaybee objset_impl_t *os = dn->dn_objset; 2077c717a561Smaybee dmu_buf_impl_t *parent = db->db_parent; 2078c717a561Smaybee uint64_t txg = tx->tx_txg; 2079c717a561Smaybee zbookmark_t zb; 2080088f3894Sahrens writeprops_t wp = { 0 }; 2081c717a561Smaybee zio_t *zio; 20829bc11082Sek110237 int zio_flags; 2083c717a561Smaybee 2084088f3894Sahrens if (!BP_IS_HOLE(db->db_blkptr) && 2085088f3894Sahrens (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE)) { 2086088f3894Sahrens /* 2087088f3894Sahrens * Private object buffers are released here rather 2088088f3894Sahrens * than in dbuf_dirty() since they are only modified 2089088f3894Sahrens * in the syncing context and we don't want the 2090088f3894Sahrens * overhead of making multiple copies of the data. 2091088f3894Sahrens */ 2092088f3894Sahrens arc_release(data, db); 2093088f3894Sahrens } else { 2094088f3894Sahrens ASSERT(arc_released(data)); 2095088f3894Sahrens /* XXX why do we need to thaw here? */ 2096088f3894Sahrens arc_buf_thaw(data); 2097088f3894Sahrens } 2098088f3894Sahrens 2099c717a561Smaybee if (parent != dn->dn_dbuf) { 2100c717a561Smaybee ASSERT(parent && parent->db_data_pending); 2101c717a561Smaybee ASSERT(db->db_level == parent->db_level-1); 2102c717a561Smaybee ASSERT(arc_released(parent->db_buf)); 2103c717a561Smaybee zio = parent->db_data_pending->dr_zio; 2104c717a561Smaybee } else { 2105c717a561Smaybee ASSERT(db->db_level == dn->dn_phys->dn_nlevels-1); 2106c717a561Smaybee ASSERT3P(db->db_blkptr, ==, 2107c717a561Smaybee &dn->dn_phys->dn_blkptr[db->db_blkid]); 2108c717a561Smaybee zio = dn->dn_zio; 2109c717a561Smaybee } 2110c717a561Smaybee 2111c717a561Smaybee ASSERT(db->db_level == 0 || data == db->db_buf); 2112c717a561Smaybee ASSERT3U(db->db_blkptr->blk_birth, <=, txg); 2113c717a561Smaybee ASSERT(zio); 2114c717a561Smaybee 2115ea8dc4b6Seschrock zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 2116ea8dc4b6Seschrock zb.zb_object = db->db.db_object; 2117ea8dc4b6Seschrock zb.zb_level = db->db_level; 2118ea8dc4b6Seschrock zb.zb_blkid = db->db_blkid; 211944cd46caSbillm 21209bc11082Sek110237 zio_flags = ZIO_FLAG_MUSTSUCCEED; 21219bc11082Sek110237 if (dmu_ot[dn->dn_type].ot_metadata || zb.zb_level != 0) 21229bc11082Sek110237 zio_flags |= ZIO_FLAG_METADATA; 2123088f3894Sahrens wp.wp_type = dn->dn_type; 2124088f3894Sahrens wp.wp_level = db->db_level; 2125088f3894Sahrens wp.wp_copies = os->os_copies; 2126088f3894Sahrens wp.wp_dncompress = dn->dn_compress; 2127088f3894Sahrens wp.wp_oscompress = os->os_compress; 2128088f3894Sahrens wp.wp_dnchecksum = dn->dn_checksum; 2129088f3894Sahrens wp.wp_oschecksum = os->os_checksum; 2130088f3894Sahrens 2131c717a561Smaybee if (BP_IS_OLDER(db->db_blkptr, txg)) 2132cdb0ab79Smaybee (void) dsl_dataset_block_kill( 2133c717a561Smaybee os->os_dsl_dataset, db->db_blkptr, zio, tx); 2134c717a561Smaybee 2135088f3894Sahrens dr->dr_zio = arc_write(zio, os->os_spa, &wp, 2136*3baa08fcSek110237 DBUF_IS_L2CACHEABLE(db), txg, db->db_blkptr, 2137*3baa08fcSek110237 data, dbuf_write_ready, dbuf_write_done, db, 21389bc11082Sek110237 ZIO_PRIORITY_ASYNC_WRITE, zio_flags, &zb); 2139fa9e4066Sahrens } 2140fa9e4066Sahrens 2141fa9e4066Sahrens /* ARGSUSED */ 2142fa9e4066Sahrens static void 2143c717a561Smaybee dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 2144fa9e4066Sahrens { 2145fa9e4066Sahrens dmu_buf_impl_t *db = vdb; 2146fa9e4066Sahrens dnode_t *dn = db->db_dnode; 2147fa9e4066Sahrens objset_impl_t *os = dn->dn_objset; 2148c717a561Smaybee blkptr_t *bp_orig = &zio->io_bp_orig; 2149fa9e4066Sahrens uint64_t fill = 0; 2150c717a561Smaybee int old_size, new_size, i; 2151fa9e4066Sahrens 2152c717a561Smaybee dprintf_dbuf_bp(db, bp_orig, "bp_orig: %s", ""); 2153fa9e4066Sahrens 2154c717a561Smaybee old_size = bp_get_dasize(os->os_spa, bp_orig); 215599653d4eSeschrock new_size = bp_get_dasize(os->os_spa, zio->io_bp); 2156fa9e4066Sahrens 2157fa9e4066Sahrens dnode_diduse_space(dn, new_size-old_size); 2158fa9e4066Sahrens 2159c717a561Smaybee if (BP_IS_HOLE(zio->io_bp)) { 2160c717a561Smaybee dsl_dataset_t *ds = os->os_dsl_dataset; 2161c717a561Smaybee dmu_tx_t *tx = os->os_synctx; 2162c717a561Smaybee 2163c717a561Smaybee if (bp_orig->blk_birth == tx->tx_txg) 2164cdb0ab79Smaybee (void) dsl_dataset_block_kill(ds, bp_orig, NULL, tx); 2165c717a561Smaybee ASSERT3U(db->db_blkptr->blk_fill, ==, 0); 2166c717a561Smaybee return; 2167c717a561Smaybee } 2168c717a561Smaybee 2169fa9e4066Sahrens mutex_enter(&db->db_mtx); 2170fa9e4066Sahrens 2171fa9e4066Sahrens if (db->db_level == 0) { 2172fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 2173c717a561Smaybee if (db->db_blkid > dn->dn_phys->dn_maxblkid) 2174fa9e4066Sahrens dn->dn_phys->dn_maxblkid = db->db_blkid; 2175fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 2176fa9e4066Sahrens 2177fa9e4066Sahrens if (dn->dn_type == DMU_OT_DNODE) { 2178fa9e4066Sahrens dnode_phys_t *dnp = db->db.db_data; 2179fa9e4066Sahrens for (i = db->db.db_size >> DNODE_SHIFT; i > 0; 2180fa9e4066Sahrens i--, dnp++) { 2181fa9e4066Sahrens if (dnp->dn_type != DMU_OT_NONE) 2182fa9e4066Sahrens fill++; 2183fa9e4066Sahrens } 2184fa9e4066Sahrens } else { 2185fa9e4066Sahrens fill = 1; 2186fa9e4066Sahrens } 2187fa9e4066Sahrens } else { 2188fa9e4066Sahrens blkptr_t *bp = db->db.db_data; 2189fa9e4066Sahrens ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 2190fa9e4066Sahrens for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, bp++) { 2191fa9e4066Sahrens if (BP_IS_HOLE(bp)) 2192fa9e4066Sahrens continue; 2193fa9e4066Sahrens ASSERT3U(BP_GET_LSIZE(bp), ==, 2194fa9e4066Sahrens db->db_level == 1 ? dn->dn_datablksz : 2195fa9e4066Sahrens (1<<dn->dn_phys->dn_indblkshift)); 2196fa9e4066Sahrens fill += bp->blk_fill; 2197fa9e4066Sahrens } 2198fa9e4066Sahrens } 2199fa9e4066Sahrens 2200fa9e4066Sahrens db->db_blkptr->blk_fill = fill; 2201fa9e4066Sahrens BP_SET_TYPE(db->db_blkptr, dn->dn_type); 2202fa9e4066Sahrens BP_SET_LEVEL(db->db_blkptr, db->db_level); 2203fa9e4066Sahrens 2204fa9e4066Sahrens mutex_exit(&db->db_mtx); 2205fa9e4066Sahrens 2206fa9e4066Sahrens /* We must do this after we've set the bp's type and level */ 2207c717a561Smaybee if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp), BP_IDENTITY(bp_orig))) { 2208c717a561Smaybee dsl_dataset_t *ds = os->os_dsl_dataset; 2209c717a561Smaybee dmu_tx_t *tx = os->os_synctx; 2210c717a561Smaybee 2211c717a561Smaybee if (bp_orig->blk_birth == tx->tx_txg) 2212cdb0ab79Smaybee (void) dsl_dataset_block_kill(ds, bp_orig, NULL, tx); 2213c717a561Smaybee dsl_dataset_block_born(ds, zio->io_bp, tx); 2214fa9e4066Sahrens } 2215fa9e4066Sahrens } 2216fa9e4066Sahrens 2217c717a561Smaybee /* ARGSUSED */ 2218c717a561Smaybee static void 2219c717a561Smaybee dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb) 2220c717a561Smaybee { 2221c717a561Smaybee dmu_buf_impl_t *db = vdb; 2222c717a561Smaybee uint64_t txg = zio->io_txg; 2223c717a561Smaybee dbuf_dirty_record_t **drp, *dr; 2224c717a561Smaybee 2225c717a561Smaybee ASSERT3U(zio->io_error, ==, 0); 2226c717a561Smaybee 2227c717a561Smaybee mutex_enter(&db->db_mtx); 2228c717a561Smaybee 2229c717a561Smaybee drp = &db->db_last_dirty; 223017f17c2dSbonwick while ((dr = *drp) != db->db_data_pending) 223117f17c2dSbonwick drp = &dr->dr_next; 223217f17c2dSbonwick ASSERT(!list_link_active(&dr->dr_dirty_node)); 223317f17c2dSbonwick ASSERT(dr->dr_txg == txg); 223417f17c2dSbonwick ASSERT(dr->dr_next == NULL); 223517f17c2dSbonwick *drp = dr->dr_next; 2236c717a561Smaybee 2237c717a561Smaybee if (db->db_level == 0) { 2238c717a561Smaybee ASSERT(db->db_blkid != DB_BONUS_BLKID); 2239c717a561Smaybee ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 2240c717a561Smaybee 2241c717a561Smaybee if (dr->dt.dl.dr_data != db->db_buf) 2242c717a561Smaybee VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1); 2243c717a561Smaybee else if (!BP_IS_HOLE(db->db_blkptr)) 2244c717a561Smaybee arc_set_callback(db->db_buf, dbuf_do_evict, db); 2245c717a561Smaybee else 2246c717a561Smaybee ASSERT(arc_released(db->db_buf)); 2247c717a561Smaybee } else { 2248c717a561Smaybee dnode_t *dn = db->db_dnode; 2249c717a561Smaybee 2250c717a561Smaybee ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 2251c717a561Smaybee ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 2252c717a561Smaybee if (!BP_IS_HOLE(db->db_blkptr)) { 2253c717a561Smaybee int epbs = 2254c717a561Smaybee dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 2255c717a561Smaybee ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 2256c717a561Smaybee db->db.db_size); 2257c717a561Smaybee ASSERT3U(dn->dn_phys->dn_maxblkid 2258c717a561Smaybee >> (db->db_level * epbs), >=, db->db_blkid); 2259c717a561Smaybee arc_set_callback(db->db_buf, dbuf_do_evict, db); 2260c717a561Smaybee } 2261c25056deSgw25295 mutex_destroy(&dr->dt.di.dr_mtx); 2262c25056deSgw25295 list_destroy(&dr->dt.di.dr_children); 2263c717a561Smaybee } 2264c717a561Smaybee kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2265c717a561Smaybee 2266c717a561Smaybee cv_broadcast(&db->db_changed); 2267c717a561Smaybee ASSERT(db->db_dirtycnt > 0); 2268c717a561Smaybee db->db_dirtycnt -= 1; 2269c717a561Smaybee db->db_data_pending = NULL; 2270c717a561Smaybee mutex_exit(&db->db_mtx); 2271c717a561Smaybee 2272c717a561Smaybee dprintf_dbuf_bp(db, zio->io_bp, "bp: %s", ""); 2273c717a561Smaybee 2274ea8dc4b6Seschrock dbuf_rele(db, (void *)(uintptr_t)txg); 2275fa9e4066Sahrens } 2276