1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy /* 22*eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*eda14cbcSMatt Macy * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*eda14cbcSMatt Macy * Copyright (c) 2012, 2019 by Delphix. All rights reserved. 25*eda14cbcSMatt Macy * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26*eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27*eda14cbcSMatt Macy * Copyright (c) 2019, Klara Inc. 28*eda14cbcSMatt Macy * Copyright (c) 2019, Allan Jude 29*eda14cbcSMatt Macy */ 30*eda14cbcSMatt Macy 31*eda14cbcSMatt Macy #include <sys/zfs_context.h> 32*eda14cbcSMatt Macy #include <sys/arc.h> 33*eda14cbcSMatt Macy #include <sys/dmu.h> 34*eda14cbcSMatt Macy #include <sys/dmu_send.h> 35*eda14cbcSMatt Macy #include <sys/dmu_impl.h> 36*eda14cbcSMatt Macy #include <sys/dbuf.h> 37*eda14cbcSMatt Macy #include <sys/dmu_objset.h> 38*eda14cbcSMatt Macy #include <sys/dsl_dataset.h> 39*eda14cbcSMatt Macy #include <sys/dsl_dir.h> 40*eda14cbcSMatt Macy #include <sys/dmu_tx.h> 41*eda14cbcSMatt Macy #include <sys/spa.h> 42*eda14cbcSMatt Macy #include <sys/zio.h> 43*eda14cbcSMatt Macy #include <sys/dmu_zfetch.h> 44*eda14cbcSMatt Macy #include <sys/sa.h> 45*eda14cbcSMatt Macy #include <sys/sa_impl.h> 46*eda14cbcSMatt Macy #include <sys/zfeature.h> 47*eda14cbcSMatt Macy #include <sys/blkptr.h> 48*eda14cbcSMatt Macy #include <sys/range_tree.h> 49*eda14cbcSMatt Macy #include <sys/trace_zfs.h> 50*eda14cbcSMatt Macy #include <sys/callb.h> 51*eda14cbcSMatt Macy #include <sys/abd.h> 52*eda14cbcSMatt Macy #include <sys/vdev.h> 53*eda14cbcSMatt Macy #include <cityhash.h> 54*eda14cbcSMatt Macy #include <sys/spa_impl.h> 55*eda14cbcSMatt Macy 56*eda14cbcSMatt Macy kstat_t *dbuf_ksp; 57*eda14cbcSMatt Macy 58*eda14cbcSMatt Macy typedef struct dbuf_stats { 59*eda14cbcSMatt Macy /* 60*eda14cbcSMatt Macy * Various statistics about the size of the dbuf cache. 61*eda14cbcSMatt Macy */ 62*eda14cbcSMatt Macy kstat_named_t cache_count; 63*eda14cbcSMatt Macy kstat_named_t cache_size_bytes; 64*eda14cbcSMatt Macy kstat_named_t cache_size_bytes_max; 65*eda14cbcSMatt Macy /* 66*eda14cbcSMatt Macy * Statistics regarding the bounds on the dbuf cache size. 67*eda14cbcSMatt Macy */ 68*eda14cbcSMatt Macy kstat_named_t cache_target_bytes; 69*eda14cbcSMatt Macy kstat_named_t cache_lowater_bytes; 70*eda14cbcSMatt Macy kstat_named_t cache_hiwater_bytes; 71*eda14cbcSMatt Macy /* 72*eda14cbcSMatt Macy * Total number of dbuf cache evictions that have occurred. 73*eda14cbcSMatt Macy */ 74*eda14cbcSMatt Macy kstat_named_t cache_total_evicts; 75*eda14cbcSMatt Macy /* 76*eda14cbcSMatt Macy * The distribution of dbuf levels in the dbuf cache and 77*eda14cbcSMatt Macy * the total size of all dbufs at each level. 78*eda14cbcSMatt Macy */ 79*eda14cbcSMatt Macy kstat_named_t cache_levels[DN_MAX_LEVELS]; 80*eda14cbcSMatt Macy kstat_named_t cache_levels_bytes[DN_MAX_LEVELS]; 81*eda14cbcSMatt Macy /* 82*eda14cbcSMatt Macy * Statistics about the dbuf hash table. 83*eda14cbcSMatt Macy */ 84*eda14cbcSMatt Macy kstat_named_t hash_hits; 85*eda14cbcSMatt Macy kstat_named_t hash_misses; 86*eda14cbcSMatt Macy kstat_named_t hash_collisions; 87*eda14cbcSMatt Macy kstat_named_t hash_elements; 88*eda14cbcSMatt Macy kstat_named_t hash_elements_max; 89*eda14cbcSMatt Macy /* 90*eda14cbcSMatt Macy * Number of sublists containing more than one dbuf in the dbuf 91*eda14cbcSMatt Macy * hash table. Keep track of the longest hash chain. 92*eda14cbcSMatt Macy */ 93*eda14cbcSMatt Macy kstat_named_t hash_chains; 94*eda14cbcSMatt Macy kstat_named_t hash_chain_max; 95*eda14cbcSMatt Macy /* 96*eda14cbcSMatt Macy * Number of times a dbuf_create() discovers that a dbuf was 97*eda14cbcSMatt Macy * already created and in the dbuf hash table. 98*eda14cbcSMatt Macy */ 99*eda14cbcSMatt Macy kstat_named_t hash_insert_race; 100*eda14cbcSMatt Macy /* 101*eda14cbcSMatt Macy * Statistics about the size of the metadata dbuf cache. 102*eda14cbcSMatt Macy */ 103*eda14cbcSMatt Macy kstat_named_t metadata_cache_count; 104*eda14cbcSMatt Macy kstat_named_t metadata_cache_size_bytes; 105*eda14cbcSMatt Macy kstat_named_t metadata_cache_size_bytes_max; 106*eda14cbcSMatt Macy /* 107*eda14cbcSMatt Macy * For diagnostic purposes, this is incremented whenever we can't add 108*eda14cbcSMatt Macy * something to the metadata cache because it's full, and instead put 109*eda14cbcSMatt Macy * the data in the regular dbuf cache. 110*eda14cbcSMatt Macy */ 111*eda14cbcSMatt Macy kstat_named_t metadata_cache_overflow; 112*eda14cbcSMatt Macy } dbuf_stats_t; 113*eda14cbcSMatt Macy 114*eda14cbcSMatt Macy dbuf_stats_t dbuf_stats = { 115*eda14cbcSMatt Macy { "cache_count", KSTAT_DATA_UINT64 }, 116*eda14cbcSMatt Macy { "cache_size_bytes", KSTAT_DATA_UINT64 }, 117*eda14cbcSMatt Macy { "cache_size_bytes_max", KSTAT_DATA_UINT64 }, 118*eda14cbcSMatt Macy { "cache_target_bytes", KSTAT_DATA_UINT64 }, 119*eda14cbcSMatt Macy { "cache_lowater_bytes", KSTAT_DATA_UINT64 }, 120*eda14cbcSMatt Macy { "cache_hiwater_bytes", KSTAT_DATA_UINT64 }, 121*eda14cbcSMatt Macy { "cache_total_evicts", KSTAT_DATA_UINT64 }, 122*eda14cbcSMatt Macy { { "cache_levels_N", KSTAT_DATA_UINT64 } }, 123*eda14cbcSMatt Macy { { "cache_levels_bytes_N", KSTAT_DATA_UINT64 } }, 124*eda14cbcSMatt Macy { "hash_hits", KSTAT_DATA_UINT64 }, 125*eda14cbcSMatt Macy { "hash_misses", KSTAT_DATA_UINT64 }, 126*eda14cbcSMatt Macy { "hash_collisions", KSTAT_DATA_UINT64 }, 127*eda14cbcSMatt Macy { "hash_elements", KSTAT_DATA_UINT64 }, 128*eda14cbcSMatt Macy { "hash_elements_max", KSTAT_DATA_UINT64 }, 129*eda14cbcSMatt Macy { "hash_chains", KSTAT_DATA_UINT64 }, 130*eda14cbcSMatt Macy { "hash_chain_max", KSTAT_DATA_UINT64 }, 131*eda14cbcSMatt Macy { "hash_insert_race", KSTAT_DATA_UINT64 }, 132*eda14cbcSMatt Macy { "metadata_cache_count", KSTAT_DATA_UINT64 }, 133*eda14cbcSMatt Macy { "metadata_cache_size_bytes", KSTAT_DATA_UINT64 }, 134*eda14cbcSMatt Macy { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64 }, 135*eda14cbcSMatt Macy { "metadata_cache_overflow", KSTAT_DATA_UINT64 } 136*eda14cbcSMatt Macy }; 137*eda14cbcSMatt Macy 138*eda14cbcSMatt Macy #define DBUF_STAT_INCR(stat, val) \ 139*eda14cbcSMatt Macy atomic_add_64(&dbuf_stats.stat.value.ui64, (val)); 140*eda14cbcSMatt Macy #define DBUF_STAT_DECR(stat, val) \ 141*eda14cbcSMatt Macy DBUF_STAT_INCR(stat, -(val)); 142*eda14cbcSMatt Macy #define DBUF_STAT_BUMP(stat) \ 143*eda14cbcSMatt Macy DBUF_STAT_INCR(stat, 1); 144*eda14cbcSMatt Macy #define DBUF_STAT_BUMPDOWN(stat) \ 145*eda14cbcSMatt Macy DBUF_STAT_INCR(stat, -1); 146*eda14cbcSMatt Macy #define DBUF_STAT_MAX(stat, v) { \ 147*eda14cbcSMatt Macy uint64_t _m; \ 148*eda14cbcSMatt Macy while ((v) > (_m = dbuf_stats.stat.value.ui64) && \ 149*eda14cbcSMatt Macy (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\ 150*eda14cbcSMatt Macy continue; \ 151*eda14cbcSMatt Macy } 152*eda14cbcSMatt Macy 153*eda14cbcSMatt Macy static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 154*eda14cbcSMatt Macy static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx); 155*eda14cbcSMatt Macy static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr); 156*eda14cbcSMatt Macy static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags); 157*eda14cbcSMatt Macy 158*eda14cbcSMatt Macy extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu, 159*eda14cbcSMatt Macy dmu_buf_evict_func_t *evict_func_sync, 160*eda14cbcSMatt Macy dmu_buf_evict_func_t *evict_func_async, 161*eda14cbcSMatt Macy dmu_buf_t **clear_on_evict_dbufp); 162*eda14cbcSMatt Macy 163*eda14cbcSMatt Macy /* 164*eda14cbcSMatt Macy * Global data structures and functions for the dbuf cache. 165*eda14cbcSMatt Macy */ 166*eda14cbcSMatt Macy static kmem_cache_t *dbuf_kmem_cache; 167*eda14cbcSMatt Macy static taskq_t *dbu_evict_taskq; 168*eda14cbcSMatt Macy 169*eda14cbcSMatt Macy static kthread_t *dbuf_cache_evict_thread; 170*eda14cbcSMatt Macy static kmutex_t dbuf_evict_lock; 171*eda14cbcSMatt Macy static kcondvar_t dbuf_evict_cv; 172*eda14cbcSMatt Macy static boolean_t dbuf_evict_thread_exit; 173*eda14cbcSMatt Macy 174*eda14cbcSMatt Macy /* 175*eda14cbcSMatt Macy * There are two dbuf caches; each dbuf can only be in one of them at a time. 176*eda14cbcSMatt Macy * 177*eda14cbcSMatt Macy * 1. Cache of metadata dbufs, to help make read-heavy administrative commands 178*eda14cbcSMatt Macy * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs 179*eda14cbcSMatt Macy * that represent the metadata that describes filesystems/snapshots/ 180*eda14cbcSMatt Macy * bookmarks/properties/etc. We only evict from this cache when we export a 181*eda14cbcSMatt Macy * pool, to short-circuit as much I/O as possible for all administrative 182*eda14cbcSMatt Macy * commands that need the metadata. There is no eviction policy for this 183*eda14cbcSMatt Macy * cache, because we try to only include types in it which would occupy a 184*eda14cbcSMatt Macy * very small amount of space per object but create a large impact on the 185*eda14cbcSMatt Macy * performance of these commands. Instead, after it reaches a maximum size 186*eda14cbcSMatt Macy * (which should only happen on very small memory systems with a very large 187*eda14cbcSMatt Macy * number of filesystem objects), we stop taking new dbufs into the 188*eda14cbcSMatt Macy * metadata cache, instead putting them in the normal dbuf cache. 189*eda14cbcSMatt Macy * 190*eda14cbcSMatt Macy * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that 191*eda14cbcSMatt Macy * are not currently held but have been recently released. These dbufs 192*eda14cbcSMatt Macy * are not eligible for arc eviction until they are aged out of the cache. 193*eda14cbcSMatt Macy * Dbufs that are aged out of the cache will be immediately destroyed and 194*eda14cbcSMatt Macy * become eligible for arc eviction. 195*eda14cbcSMatt Macy * 196*eda14cbcSMatt Macy * Dbufs are added to these caches once the last hold is released. If a dbuf is 197*eda14cbcSMatt Macy * later accessed and still exists in the dbuf cache, then it will be removed 198*eda14cbcSMatt Macy * from the cache and later re-added to the head of the cache. 199*eda14cbcSMatt Macy * 200*eda14cbcSMatt Macy * If a given dbuf meets the requirements for the metadata cache, it will go 201*eda14cbcSMatt Macy * there, otherwise it will be considered for the generic LRU dbuf cache. The 202*eda14cbcSMatt Macy * caches and the refcounts tracking their sizes are stored in an array indexed 203*eda14cbcSMatt Macy * by those caches' matching enum values (from dbuf_cached_state_t). 204*eda14cbcSMatt Macy */ 205*eda14cbcSMatt Macy typedef struct dbuf_cache { 206*eda14cbcSMatt Macy multilist_t *cache; 207*eda14cbcSMatt Macy zfs_refcount_t size; 208*eda14cbcSMatt Macy } dbuf_cache_t; 209*eda14cbcSMatt Macy dbuf_cache_t dbuf_caches[DB_CACHE_MAX]; 210*eda14cbcSMatt Macy 211*eda14cbcSMatt Macy /* Size limits for the caches */ 212*eda14cbcSMatt Macy unsigned long dbuf_cache_max_bytes = ULONG_MAX; 213*eda14cbcSMatt Macy unsigned long dbuf_metadata_cache_max_bytes = ULONG_MAX; 214*eda14cbcSMatt Macy 215*eda14cbcSMatt Macy /* Set the default sizes of the caches to log2 fraction of arc size */ 216*eda14cbcSMatt Macy int dbuf_cache_shift = 5; 217*eda14cbcSMatt Macy int dbuf_metadata_cache_shift = 6; 218*eda14cbcSMatt Macy 219*eda14cbcSMatt Macy static unsigned long dbuf_cache_target_bytes(void); 220*eda14cbcSMatt Macy static unsigned long dbuf_metadata_cache_target_bytes(void); 221*eda14cbcSMatt Macy 222*eda14cbcSMatt Macy /* 223*eda14cbcSMatt Macy * The LRU dbuf cache uses a three-stage eviction policy: 224*eda14cbcSMatt Macy * - A low water marker designates when the dbuf eviction thread 225*eda14cbcSMatt Macy * should stop evicting from the dbuf cache. 226*eda14cbcSMatt Macy * - When we reach the maximum size (aka mid water mark), we 227*eda14cbcSMatt Macy * signal the eviction thread to run. 228*eda14cbcSMatt Macy * - The high water mark indicates when the eviction thread 229*eda14cbcSMatt Macy * is unable to keep up with the incoming load and eviction must 230*eda14cbcSMatt Macy * happen in the context of the calling thread. 231*eda14cbcSMatt Macy * 232*eda14cbcSMatt Macy * The dbuf cache: 233*eda14cbcSMatt Macy * (max size) 234*eda14cbcSMatt Macy * low water mid water hi water 235*eda14cbcSMatt Macy * +----------------------------------------+----------+----------+ 236*eda14cbcSMatt Macy * | | | | 237*eda14cbcSMatt Macy * | | | | 238*eda14cbcSMatt Macy * | | | | 239*eda14cbcSMatt Macy * | | | | 240*eda14cbcSMatt Macy * +----------------------------------------+----------+----------+ 241*eda14cbcSMatt Macy * stop signal evict 242*eda14cbcSMatt Macy * evicting eviction directly 243*eda14cbcSMatt Macy * thread 244*eda14cbcSMatt Macy * 245*eda14cbcSMatt Macy * The high and low water marks indicate the operating range for the eviction 246*eda14cbcSMatt Macy * thread. The low water mark is, by default, 90% of the total size of the 247*eda14cbcSMatt Macy * cache and the high water mark is at 110% (both of these percentages can be 248*eda14cbcSMatt Macy * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct, 249*eda14cbcSMatt Macy * respectively). The eviction thread will try to ensure that the cache remains 250*eda14cbcSMatt Macy * within this range by waking up every second and checking if the cache is 251*eda14cbcSMatt Macy * above the low water mark. The thread can also be woken up by callers adding 252*eda14cbcSMatt Macy * elements into the cache if the cache is larger than the mid water (i.e max 253*eda14cbcSMatt Macy * cache size). Once the eviction thread is woken up and eviction is required, 254*eda14cbcSMatt Macy * it will continue evicting buffers until it's able to reduce the cache size 255*eda14cbcSMatt Macy * to the low water mark. If the cache size continues to grow and hits the high 256*eda14cbcSMatt Macy * water mark, then callers adding elements to the cache will begin to evict 257*eda14cbcSMatt Macy * directly from the cache until the cache is no longer above the high water 258*eda14cbcSMatt Macy * mark. 259*eda14cbcSMatt Macy */ 260*eda14cbcSMatt Macy 261*eda14cbcSMatt Macy /* 262*eda14cbcSMatt Macy * The percentage above and below the maximum cache size. 263*eda14cbcSMatt Macy */ 264*eda14cbcSMatt Macy uint_t dbuf_cache_hiwater_pct = 10; 265*eda14cbcSMatt Macy uint_t dbuf_cache_lowater_pct = 10; 266*eda14cbcSMatt Macy 267*eda14cbcSMatt Macy /* ARGSUSED */ 268*eda14cbcSMatt Macy static int 269*eda14cbcSMatt Macy dbuf_cons(void *vdb, void *unused, int kmflag) 270*eda14cbcSMatt Macy { 271*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 272*eda14cbcSMatt Macy bzero(db, sizeof (dmu_buf_impl_t)); 273*eda14cbcSMatt Macy 274*eda14cbcSMatt Macy mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL); 275*eda14cbcSMatt Macy rw_init(&db->db_rwlock, NULL, RW_DEFAULT, NULL); 276*eda14cbcSMatt Macy cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL); 277*eda14cbcSMatt Macy multilist_link_init(&db->db_cache_link); 278*eda14cbcSMatt Macy zfs_refcount_create(&db->db_holds); 279*eda14cbcSMatt Macy 280*eda14cbcSMatt Macy return (0); 281*eda14cbcSMatt Macy } 282*eda14cbcSMatt Macy 283*eda14cbcSMatt Macy /* ARGSUSED */ 284*eda14cbcSMatt Macy static void 285*eda14cbcSMatt Macy dbuf_dest(void *vdb, void *unused) 286*eda14cbcSMatt Macy { 287*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 288*eda14cbcSMatt Macy mutex_destroy(&db->db_mtx); 289*eda14cbcSMatt Macy rw_destroy(&db->db_rwlock); 290*eda14cbcSMatt Macy cv_destroy(&db->db_changed); 291*eda14cbcSMatt Macy ASSERT(!multilist_link_active(&db->db_cache_link)); 292*eda14cbcSMatt Macy zfs_refcount_destroy(&db->db_holds); 293*eda14cbcSMatt Macy } 294*eda14cbcSMatt Macy 295*eda14cbcSMatt Macy /* 296*eda14cbcSMatt Macy * dbuf hash table routines 297*eda14cbcSMatt Macy */ 298*eda14cbcSMatt Macy static dbuf_hash_table_t dbuf_hash_table; 299*eda14cbcSMatt Macy 300*eda14cbcSMatt Macy static uint64_t dbuf_hash_count; 301*eda14cbcSMatt Macy 302*eda14cbcSMatt Macy /* 303*eda14cbcSMatt Macy * We use Cityhash for this. It's fast, and has good hash properties without 304*eda14cbcSMatt Macy * requiring any large static buffers. 305*eda14cbcSMatt Macy */ 306*eda14cbcSMatt Macy static uint64_t 307*eda14cbcSMatt Macy dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid) 308*eda14cbcSMatt Macy { 309*eda14cbcSMatt Macy return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid)); 310*eda14cbcSMatt Macy } 311*eda14cbcSMatt Macy 312*eda14cbcSMatt Macy #define DTRACE_SET_STATE(db, why) \ 313*eda14cbcSMatt Macy DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \ 314*eda14cbcSMatt Macy const char *, why) 315*eda14cbcSMatt Macy 316*eda14cbcSMatt Macy #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \ 317*eda14cbcSMatt Macy ((dbuf)->db.db_object == (obj) && \ 318*eda14cbcSMatt Macy (dbuf)->db_objset == (os) && \ 319*eda14cbcSMatt Macy (dbuf)->db_level == (level) && \ 320*eda14cbcSMatt Macy (dbuf)->db_blkid == (blkid)) 321*eda14cbcSMatt Macy 322*eda14cbcSMatt Macy dmu_buf_impl_t * 323*eda14cbcSMatt Macy dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid) 324*eda14cbcSMatt Macy { 325*eda14cbcSMatt Macy dbuf_hash_table_t *h = &dbuf_hash_table; 326*eda14cbcSMatt Macy uint64_t hv; 327*eda14cbcSMatt Macy uint64_t idx; 328*eda14cbcSMatt Macy dmu_buf_impl_t *db; 329*eda14cbcSMatt Macy 330*eda14cbcSMatt Macy hv = dbuf_hash(os, obj, level, blkid); 331*eda14cbcSMatt Macy idx = hv & h->hash_table_mask; 332*eda14cbcSMatt Macy 333*eda14cbcSMatt Macy mutex_enter(DBUF_HASH_MUTEX(h, idx)); 334*eda14cbcSMatt Macy for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) { 335*eda14cbcSMatt Macy if (DBUF_EQUAL(db, os, obj, level, blkid)) { 336*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 337*eda14cbcSMatt Macy if (db->db_state != DB_EVICTING) { 338*eda14cbcSMatt Macy mutex_exit(DBUF_HASH_MUTEX(h, idx)); 339*eda14cbcSMatt Macy return (db); 340*eda14cbcSMatt Macy } 341*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 342*eda14cbcSMatt Macy } 343*eda14cbcSMatt Macy } 344*eda14cbcSMatt Macy mutex_exit(DBUF_HASH_MUTEX(h, idx)); 345*eda14cbcSMatt Macy return (NULL); 346*eda14cbcSMatt Macy } 347*eda14cbcSMatt Macy 348*eda14cbcSMatt Macy static dmu_buf_impl_t * 349*eda14cbcSMatt Macy dbuf_find_bonus(objset_t *os, uint64_t object) 350*eda14cbcSMatt Macy { 351*eda14cbcSMatt Macy dnode_t *dn; 352*eda14cbcSMatt Macy dmu_buf_impl_t *db = NULL; 353*eda14cbcSMatt Macy 354*eda14cbcSMatt Macy if (dnode_hold(os, object, FTAG, &dn) == 0) { 355*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 356*eda14cbcSMatt Macy if (dn->dn_bonus != NULL) { 357*eda14cbcSMatt Macy db = dn->dn_bonus; 358*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 359*eda14cbcSMatt Macy } 360*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 361*eda14cbcSMatt Macy dnode_rele(dn, FTAG); 362*eda14cbcSMatt Macy } 363*eda14cbcSMatt Macy return (db); 364*eda14cbcSMatt Macy } 365*eda14cbcSMatt Macy 366*eda14cbcSMatt Macy /* 367*eda14cbcSMatt Macy * Insert an entry into the hash table. If there is already an element 368*eda14cbcSMatt Macy * equal to elem in the hash table, then the already existing element 369*eda14cbcSMatt Macy * will be returned and the new element will not be inserted. 370*eda14cbcSMatt Macy * Otherwise returns NULL. 371*eda14cbcSMatt Macy */ 372*eda14cbcSMatt Macy static dmu_buf_impl_t * 373*eda14cbcSMatt Macy dbuf_hash_insert(dmu_buf_impl_t *db) 374*eda14cbcSMatt Macy { 375*eda14cbcSMatt Macy dbuf_hash_table_t *h = &dbuf_hash_table; 376*eda14cbcSMatt Macy objset_t *os = db->db_objset; 377*eda14cbcSMatt Macy uint64_t obj = db->db.db_object; 378*eda14cbcSMatt Macy int level = db->db_level; 379*eda14cbcSMatt Macy uint64_t blkid, hv, idx; 380*eda14cbcSMatt Macy dmu_buf_impl_t *dbf; 381*eda14cbcSMatt Macy uint32_t i; 382*eda14cbcSMatt Macy 383*eda14cbcSMatt Macy blkid = db->db_blkid; 384*eda14cbcSMatt Macy hv = dbuf_hash(os, obj, level, blkid); 385*eda14cbcSMatt Macy idx = hv & h->hash_table_mask; 386*eda14cbcSMatt Macy 387*eda14cbcSMatt Macy mutex_enter(DBUF_HASH_MUTEX(h, idx)); 388*eda14cbcSMatt Macy for (dbf = h->hash_table[idx], i = 0; dbf != NULL; 389*eda14cbcSMatt Macy dbf = dbf->db_hash_next, i++) { 390*eda14cbcSMatt Macy if (DBUF_EQUAL(dbf, os, obj, level, blkid)) { 391*eda14cbcSMatt Macy mutex_enter(&dbf->db_mtx); 392*eda14cbcSMatt Macy if (dbf->db_state != DB_EVICTING) { 393*eda14cbcSMatt Macy mutex_exit(DBUF_HASH_MUTEX(h, idx)); 394*eda14cbcSMatt Macy return (dbf); 395*eda14cbcSMatt Macy } 396*eda14cbcSMatt Macy mutex_exit(&dbf->db_mtx); 397*eda14cbcSMatt Macy } 398*eda14cbcSMatt Macy } 399*eda14cbcSMatt Macy 400*eda14cbcSMatt Macy if (i > 0) { 401*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_collisions); 402*eda14cbcSMatt Macy if (i == 1) 403*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_chains); 404*eda14cbcSMatt Macy 405*eda14cbcSMatt Macy DBUF_STAT_MAX(hash_chain_max, i); 406*eda14cbcSMatt Macy } 407*eda14cbcSMatt Macy 408*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 409*eda14cbcSMatt Macy db->db_hash_next = h->hash_table[idx]; 410*eda14cbcSMatt Macy h->hash_table[idx] = db; 411*eda14cbcSMatt Macy mutex_exit(DBUF_HASH_MUTEX(h, idx)); 412*eda14cbcSMatt Macy atomic_inc_64(&dbuf_hash_count); 413*eda14cbcSMatt Macy DBUF_STAT_MAX(hash_elements_max, dbuf_hash_count); 414*eda14cbcSMatt Macy 415*eda14cbcSMatt Macy return (NULL); 416*eda14cbcSMatt Macy } 417*eda14cbcSMatt Macy 418*eda14cbcSMatt Macy /* 419*eda14cbcSMatt Macy * This returns whether this dbuf should be stored in the metadata cache, which 420*eda14cbcSMatt Macy * is based on whether it's from one of the dnode types that store data related 421*eda14cbcSMatt Macy * to traversing dataset hierarchies. 422*eda14cbcSMatt Macy */ 423*eda14cbcSMatt Macy static boolean_t 424*eda14cbcSMatt Macy dbuf_include_in_metadata_cache(dmu_buf_impl_t *db) 425*eda14cbcSMatt Macy { 426*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 427*eda14cbcSMatt Macy dmu_object_type_t type = DB_DNODE(db)->dn_type; 428*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 429*eda14cbcSMatt Macy 430*eda14cbcSMatt Macy /* Check if this dbuf is one of the types we care about */ 431*eda14cbcSMatt Macy if (DMU_OT_IS_METADATA_CACHED(type)) { 432*eda14cbcSMatt Macy /* If we hit this, then we set something up wrong in dmu_ot */ 433*eda14cbcSMatt Macy ASSERT(DMU_OT_IS_METADATA(type)); 434*eda14cbcSMatt Macy 435*eda14cbcSMatt Macy /* 436*eda14cbcSMatt Macy * Sanity check for small-memory systems: don't allocate too 437*eda14cbcSMatt Macy * much memory for this purpose. 438*eda14cbcSMatt Macy */ 439*eda14cbcSMatt Macy if (zfs_refcount_count( 440*eda14cbcSMatt Macy &dbuf_caches[DB_DBUF_METADATA_CACHE].size) > 441*eda14cbcSMatt Macy dbuf_metadata_cache_target_bytes()) { 442*eda14cbcSMatt Macy DBUF_STAT_BUMP(metadata_cache_overflow); 443*eda14cbcSMatt Macy return (B_FALSE); 444*eda14cbcSMatt Macy } 445*eda14cbcSMatt Macy 446*eda14cbcSMatt Macy return (B_TRUE); 447*eda14cbcSMatt Macy } 448*eda14cbcSMatt Macy 449*eda14cbcSMatt Macy return (B_FALSE); 450*eda14cbcSMatt Macy } 451*eda14cbcSMatt Macy 452*eda14cbcSMatt Macy /* 453*eda14cbcSMatt Macy * Remove an entry from the hash table. It must be in the EVICTING state. 454*eda14cbcSMatt Macy */ 455*eda14cbcSMatt Macy static void 456*eda14cbcSMatt Macy dbuf_hash_remove(dmu_buf_impl_t *db) 457*eda14cbcSMatt Macy { 458*eda14cbcSMatt Macy dbuf_hash_table_t *h = &dbuf_hash_table; 459*eda14cbcSMatt Macy uint64_t hv, idx; 460*eda14cbcSMatt Macy dmu_buf_impl_t *dbf, **dbp; 461*eda14cbcSMatt Macy 462*eda14cbcSMatt Macy hv = dbuf_hash(db->db_objset, db->db.db_object, 463*eda14cbcSMatt Macy db->db_level, db->db_blkid); 464*eda14cbcSMatt Macy idx = hv & h->hash_table_mask; 465*eda14cbcSMatt Macy 466*eda14cbcSMatt Macy /* 467*eda14cbcSMatt Macy * We mustn't hold db_mtx to maintain lock ordering: 468*eda14cbcSMatt Macy * DBUF_HASH_MUTEX > db_mtx. 469*eda14cbcSMatt Macy */ 470*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&db->db_holds)); 471*eda14cbcSMatt Macy ASSERT(db->db_state == DB_EVICTING); 472*eda14cbcSMatt Macy ASSERT(!MUTEX_HELD(&db->db_mtx)); 473*eda14cbcSMatt Macy 474*eda14cbcSMatt Macy mutex_enter(DBUF_HASH_MUTEX(h, idx)); 475*eda14cbcSMatt Macy dbp = &h->hash_table[idx]; 476*eda14cbcSMatt Macy while ((dbf = *dbp) != db) { 477*eda14cbcSMatt Macy dbp = &dbf->db_hash_next; 478*eda14cbcSMatt Macy ASSERT(dbf != NULL); 479*eda14cbcSMatt Macy } 480*eda14cbcSMatt Macy *dbp = db->db_hash_next; 481*eda14cbcSMatt Macy db->db_hash_next = NULL; 482*eda14cbcSMatt Macy if (h->hash_table[idx] && 483*eda14cbcSMatt Macy h->hash_table[idx]->db_hash_next == NULL) 484*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(hash_chains); 485*eda14cbcSMatt Macy mutex_exit(DBUF_HASH_MUTEX(h, idx)); 486*eda14cbcSMatt Macy atomic_dec_64(&dbuf_hash_count); 487*eda14cbcSMatt Macy } 488*eda14cbcSMatt Macy 489*eda14cbcSMatt Macy typedef enum { 490*eda14cbcSMatt Macy DBVU_EVICTING, 491*eda14cbcSMatt Macy DBVU_NOT_EVICTING 492*eda14cbcSMatt Macy } dbvu_verify_type_t; 493*eda14cbcSMatt Macy 494*eda14cbcSMatt Macy static void 495*eda14cbcSMatt Macy dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type) 496*eda14cbcSMatt Macy { 497*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 498*eda14cbcSMatt Macy int64_t holds; 499*eda14cbcSMatt Macy 500*eda14cbcSMatt Macy if (db->db_user == NULL) 501*eda14cbcSMatt Macy return; 502*eda14cbcSMatt Macy 503*eda14cbcSMatt Macy /* Only data blocks support the attachment of user data. */ 504*eda14cbcSMatt Macy ASSERT(db->db_level == 0); 505*eda14cbcSMatt Macy 506*eda14cbcSMatt Macy /* Clients must resolve a dbuf before attaching user data. */ 507*eda14cbcSMatt Macy ASSERT(db->db.db_data != NULL); 508*eda14cbcSMatt Macy ASSERT3U(db->db_state, ==, DB_CACHED); 509*eda14cbcSMatt Macy 510*eda14cbcSMatt Macy holds = zfs_refcount_count(&db->db_holds); 511*eda14cbcSMatt Macy if (verify_type == DBVU_EVICTING) { 512*eda14cbcSMatt Macy /* 513*eda14cbcSMatt Macy * Immediate eviction occurs when holds == dirtycnt. 514*eda14cbcSMatt Macy * For normal eviction buffers, holds is zero on 515*eda14cbcSMatt Macy * eviction, except when dbuf_fix_old_data() calls 516*eda14cbcSMatt Macy * dbuf_clear_data(). However, the hold count can grow 517*eda14cbcSMatt Macy * during eviction even though db_mtx is held (see 518*eda14cbcSMatt Macy * dmu_bonus_hold() for an example), so we can only 519*eda14cbcSMatt Macy * test the generic invariant that holds >= dirtycnt. 520*eda14cbcSMatt Macy */ 521*eda14cbcSMatt Macy ASSERT3U(holds, >=, db->db_dirtycnt); 522*eda14cbcSMatt Macy } else { 523*eda14cbcSMatt Macy if (db->db_user_immediate_evict == TRUE) 524*eda14cbcSMatt Macy ASSERT3U(holds, >=, db->db_dirtycnt); 525*eda14cbcSMatt Macy else 526*eda14cbcSMatt Macy ASSERT3U(holds, >, 0); 527*eda14cbcSMatt Macy } 528*eda14cbcSMatt Macy #endif 529*eda14cbcSMatt Macy } 530*eda14cbcSMatt Macy 531*eda14cbcSMatt Macy static void 532*eda14cbcSMatt Macy dbuf_evict_user(dmu_buf_impl_t *db) 533*eda14cbcSMatt Macy { 534*eda14cbcSMatt Macy dmu_buf_user_t *dbu = db->db_user; 535*eda14cbcSMatt Macy 536*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 537*eda14cbcSMatt Macy 538*eda14cbcSMatt Macy if (dbu == NULL) 539*eda14cbcSMatt Macy return; 540*eda14cbcSMatt Macy 541*eda14cbcSMatt Macy dbuf_verify_user(db, DBVU_EVICTING); 542*eda14cbcSMatt Macy db->db_user = NULL; 543*eda14cbcSMatt Macy 544*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 545*eda14cbcSMatt Macy if (dbu->dbu_clear_on_evict_dbufp != NULL) 546*eda14cbcSMatt Macy *dbu->dbu_clear_on_evict_dbufp = NULL; 547*eda14cbcSMatt Macy #endif 548*eda14cbcSMatt Macy 549*eda14cbcSMatt Macy /* 550*eda14cbcSMatt Macy * There are two eviction callbacks - one that we call synchronously 551*eda14cbcSMatt Macy * and one that we invoke via a taskq. The async one is useful for 552*eda14cbcSMatt Macy * avoiding lock order reversals and limiting stack depth. 553*eda14cbcSMatt Macy * 554*eda14cbcSMatt Macy * Note that if we have a sync callback but no async callback, 555*eda14cbcSMatt Macy * it's likely that the sync callback will free the structure 556*eda14cbcSMatt Macy * containing the dbu. In that case we need to take care to not 557*eda14cbcSMatt Macy * dereference dbu after calling the sync evict func. 558*eda14cbcSMatt Macy */ 559*eda14cbcSMatt Macy boolean_t has_async = (dbu->dbu_evict_func_async != NULL); 560*eda14cbcSMatt Macy 561*eda14cbcSMatt Macy if (dbu->dbu_evict_func_sync != NULL) 562*eda14cbcSMatt Macy dbu->dbu_evict_func_sync(dbu); 563*eda14cbcSMatt Macy 564*eda14cbcSMatt Macy if (has_async) { 565*eda14cbcSMatt Macy taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async, 566*eda14cbcSMatt Macy dbu, 0, &dbu->dbu_tqent); 567*eda14cbcSMatt Macy } 568*eda14cbcSMatt Macy } 569*eda14cbcSMatt Macy 570*eda14cbcSMatt Macy boolean_t 571*eda14cbcSMatt Macy dbuf_is_metadata(dmu_buf_impl_t *db) 572*eda14cbcSMatt Macy { 573*eda14cbcSMatt Macy /* 574*eda14cbcSMatt Macy * Consider indirect blocks and spill blocks to be meta data. 575*eda14cbcSMatt Macy */ 576*eda14cbcSMatt Macy if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) { 577*eda14cbcSMatt Macy return (B_TRUE); 578*eda14cbcSMatt Macy } else { 579*eda14cbcSMatt Macy boolean_t is_metadata; 580*eda14cbcSMatt Macy 581*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 582*eda14cbcSMatt Macy is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type); 583*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 584*eda14cbcSMatt Macy 585*eda14cbcSMatt Macy return (is_metadata); 586*eda14cbcSMatt Macy } 587*eda14cbcSMatt Macy } 588*eda14cbcSMatt Macy 589*eda14cbcSMatt Macy 590*eda14cbcSMatt Macy /* 591*eda14cbcSMatt Macy * This function *must* return indices evenly distributed between all 592*eda14cbcSMatt Macy * sublists of the multilist. This is needed due to how the dbuf eviction 593*eda14cbcSMatt Macy * code is laid out; dbuf_evict_thread() assumes dbufs are evenly 594*eda14cbcSMatt Macy * distributed between all sublists and uses this assumption when 595*eda14cbcSMatt Macy * deciding which sublist to evict from and how much to evict from it. 596*eda14cbcSMatt Macy */ 597*eda14cbcSMatt Macy static unsigned int 598*eda14cbcSMatt Macy dbuf_cache_multilist_index_func(multilist_t *ml, void *obj) 599*eda14cbcSMatt Macy { 600*eda14cbcSMatt Macy dmu_buf_impl_t *db = obj; 601*eda14cbcSMatt Macy 602*eda14cbcSMatt Macy /* 603*eda14cbcSMatt Macy * The assumption here, is the hash value for a given 604*eda14cbcSMatt Macy * dmu_buf_impl_t will remain constant throughout it's lifetime 605*eda14cbcSMatt Macy * (i.e. it's objset, object, level and blkid fields don't change). 606*eda14cbcSMatt Macy * Thus, we don't need to store the dbuf's sublist index 607*eda14cbcSMatt Macy * on insertion, as this index can be recalculated on removal. 608*eda14cbcSMatt Macy * 609*eda14cbcSMatt Macy * Also, the low order bits of the hash value are thought to be 610*eda14cbcSMatt Macy * distributed evenly. Otherwise, in the case that the multilist 611*eda14cbcSMatt Macy * has a power of two number of sublists, each sublists' usage 612*eda14cbcSMatt Macy * would not be evenly distributed. 613*eda14cbcSMatt Macy */ 614*eda14cbcSMatt Macy return (dbuf_hash(db->db_objset, db->db.db_object, 615*eda14cbcSMatt Macy db->db_level, db->db_blkid) % 616*eda14cbcSMatt Macy multilist_get_num_sublists(ml)); 617*eda14cbcSMatt Macy } 618*eda14cbcSMatt Macy 619*eda14cbcSMatt Macy /* 620*eda14cbcSMatt Macy * The target size of the dbuf cache can grow with the ARC target, 621*eda14cbcSMatt Macy * unless limited by the tunable dbuf_cache_max_bytes. 622*eda14cbcSMatt Macy */ 623*eda14cbcSMatt Macy static inline unsigned long 624*eda14cbcSMatt Macy dbuf_cache_target_bytes(void) 625*eda14cbcSMatt Macy { 626*eda14cbcSMatt Macy return (MIN(dbuf_cache_max_bytes, 627*eda14cbcSMatt Macy arc_target_bytes() >> dbuf_cache_shift)); 628*eda14cbcSMatt Macy } 629*eda14cbcSMatt Macy 630*eda14cbcSMatt Macy /* 631*eda14cbcSMatt Macy * The target size of the dbuf metadata cache can grow with the ARC target, 632*eda14cbcSMatt Macy * unless limited by the tunable dbuf_metadata_cache_max_bytes. 633*eda14cbcSMatt Macy */ 634*eda14cbcSMatt Macy static inline unsigned long 635*eda14cbcSMatt Macy dbuf_metadata_cache_target_bytes(void) 636*eda14cbcSMatt Macy { 637*eda14cbcSMatt Macy return (MIN(dbuf_metadata_cache_max_bytes, 638*eda14cbcSMatt Macy arc_target_bytes() >> dbuf_metadata_cache_shift)); 639*eda14cbcSMatt Macy } 640*eda14cbcSMatt Macy 641*eda14cbcSMatt Macy static inline uint64_t 642*eda14cbcSMatt Macy dbuf_cache_hiwater_bytes(void) 643*eda14cbcSMatt Macy { 644*eda14cbcSMatt Macy uint64_t dbuf_cache_target = dbuf_cache_target_bytes(); 645*eda14cbcSMatt Macy return (dbuf_cache_target + 646*eda14cbcSMatt Macy (dbuf_cache_target * dbuf_cache_hiwater_pct) / 100); 647*eda14cbcSMatt Macy } 648*eda14cbcSMatt Macy 649*eda14cbcSMatt Macy static inline uint64_t 650*eda14cbcSMatt Macy dbuf_cache_lowater_bytes(void) 651*eda14cbcSMatt Macy { 652*eda14cbcSMatt Macy uint64_t dbuf_cache_target = dbuf_cache_target_bytes(); 653*eda14cbcSMatt Macy return (dbuf_cache_target - 654*eda14cbcSMatt Macy (dbuf_cache_target * dbuf_cache_lowater_pct) / 100); 655*eda14cbcSMatt Macy } 656*eda14cbcSMatt Macy 657*eda14cbcSMatt Macy static inline boolean_t 658*eda14cbcSMatt Macy dbuf_cache_above_lowater(void) 659*eda14cbcSMatt Macy { 660*eda14cbcSMatt Macy return (zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) > 661*eda14cbcSMatt Macy dbuf_cache_lowater_bytes()); 662*eda14cbcSMatt Macy } 663*eda14cbcSMatt Macy 664*eda14cbcSMatt Macy /* 665*eda14cbcSMatt Macy * Evict the oldest eligible dbuf from the dbuf cache. 666*eda14cbcSMatt Macy */ 667*eda14cbcSMatt Macy static void 668*eda14cbcSMatt Macy dbuf_evict_one(void) 669*eda14cbcSMatt Macy { 670*eda14cbcSMatt Macy int idx = multilist_get_random_index(dbuf_caches[DB_DBUF_CACHE].cache); 671*eda14cbcSMatt Macy multilist_sublist_t *mls = multilist_sublist_lock( 672*eda14cbcSMatt Macy dbuf_caches[DB_DBUF_CACHE].cache, idx); 673*eda14cbcSMatt Macy 674*eda14cbcSMatt Macy ASSERT(!MUTEX_HELD(&dbuf_evict_lock)); 675*eda14cbcSMatt Macy 676*eda14cbcSMatt Macy dmu_buf_impl_t *db = multilist_sublist_tail(mls); 677*eda14cbcSMatt Macy while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) { 678*eda14cbcSMatt Macy db = multilist_sublist_prev(mls, db); 679*eda14cbcSMatt Macy } 680*eda14cbcSMatt Macy 681*eda14cbcSMatt Macy DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db, 682*eda14cbcSMatt Macy multilist_sublist_t *, mls); 683*eda14cbcSMatt Macy 684*eda14cbcSMatt Macy if (db != NULL) { 685*eda14cbcSMatt Macy multilist_sublist_remove(mls, db); 686*eda14cbcSMatt Macy multilist_sublist_unlock(mls); 687*eda14cbcSMatt Macy (void) zfs_refcount_remove_many( 688*eda14cbcSMatt Macy &dbuf_caches[DB_DBUF_CACHE].size, db->db.db_size, db); 689*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 690*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_count); 691*eda14cbcSMatt Macy DBUF_STAT_DECR(cache_levels_bytes[db->db_level], 692*eda14cbcSMatt Macy db->db.db_size); 693*eda14cbcSMatt Macy ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE); 694*eda14cbcSMatt Macy db->db_caching_status = DB_NO_CACHE; 695*eda14cbcSMatt Macy dbuf_destroy(db); 696*eda14cbcSMatt Macy DBUF_STAT_BUMP(cache_total_evicts); 697*eda14cbcSMatt Macy } else { 698*eda14cbcSMatt Macy multilist_sublist_unlock(mls); 699*eda14cbcSMatt Macy } 700*eda14cbcSMatt Macy } 701*eda14cbcSMatt Macy 702*eda14cbcSMatt Macy /* 703*eda14cbcSMatt Macy * The dbuf evict thread is responsible for aging out dbufs from the 704*eda14cbcSMatt Macy * cache. Once the cache has reached it's maximum size, dbufs are removed 705*eda14cbcSMatt Macy * and destroyed. The eviction thread will continue running until the size 706*eda14cbcSMatt Macy * of the dbuf cache is at or below the maximum size. Once the dbuf is aged 707*eda14cbcSMatt Macy * out of the cache it is destroyed and becomes eligible for arc eviction. 708*eda14cbcSMatt Macy */ 709*eda14cbcSMatt Macy /* ARGSUSED */ 710*eda14cbcSMatt Macy static void 711*eda14cbcSMatt Macy dbuf_evict_thread(void *unused) 712*eda14cbcSMatt Macy { 713*eda14cbcSMatt Macy callb_cpr_t cpr; 714*eda14cbcSMatt Macy 715*eda14cbcSMatt Macy CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG); 716*eda14cbcSMatt Macy 717*eda14cbcSMatt Macy mutex_enter(&dbuf_evict_lock); 718*eda14cbcSMatt Macy while (!dbuf_evict_thread_exit) { 719*eda14cbcSMatt Macy while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 720*eda14cbcSMatt Macy CALLB_CPR_SAFE_BEGIN(&cpr); 721*eda14cbcSMatt Macy (void) cv_timedwait_sig_hires(&dbuf_evict_cv, 722*eda14cbcSMatt Macy &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 723*eda14cbcSMatt Macy CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock); 724*eda14cbcSMatt Macy } 725*eda14cbcSMatt Macy mutex_exit(&dbuf_evict_lock); 726*eda14cbcSMatt Macy 727*eda14cbcSMatt Macy /* 728*eda14cbcSMatt Macy * Keep evicting as long as we're above the low water mark 729*eda14cbcSMatt Macy * for the cache. We do this without holding the locks to 730*eda14cbcSMatt Macy * minimize lock contention. 731*eda14cbcSMatt Macy */ 732*eda14cbcSMatt Macy while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 733*eda14cbcSMatt Macy dbuf_evict_one(); 734*eda14cbcSMatt Macy } 735*eda14cbcSMatt Macy 736*eda14cbcSMatt Macy mutex_enter(&dbuf_evict_lock); 737*eda14cbcSMatt Macy } 738*eda14cbcSMatt Macy 739*eda14cbcSMatt Macy dbuf_evict_thread_exit = B_FALSE; 740*eda14cbcSMatt Macy cv_broadcast(&dbuf_evict_cv); 741*eda14cbcSMatt Macy CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */ 742*eda14cbcSMatt Macy thread_exit(); 743*eda14cbcSMatt Macy } 744*eda14cbcSMatt Macy 745*eda14cbcSMatt Macy /* 746*eda14cbcSMatt Macy * Wake up the dbuf eviction thread if the dbuf cache is at its max size. 747*eda14cbcSMatt Macy * If the dbuf cache is at its high water mark, then evict a dbuf from the 748*eda14cbcSMatt Macy * dbuf cache using the callers context. 749*eda14cbcSMatt Macy */ 750*eda14cbcSMatt Macy static void 751*eda14cbcSMatt Macy dbuf_evict_notify(uint64_t size) 752*eda14cbcSMatt Macy { 753*eda14cbcSMatt Macy /* 754*eda14cbcSMatt Macy * We check if we should evict without holding the dbuf_evict_lock, 755*eda14cbcSMatt Macy * because it's OK to occasionally make the wrong decision here, 756*eda14cbcSMatt Macy * and grabbing the lock results in massive lock contention. 757*eda14cbcSMatt Macy */ 758*eda14cbcSMatt Macy if (size > dbuf_cache_target_bytes()) { 759*eda14cbcSMatt Macy if (size > dbuf_cache_hiwater_bytes()) 760*eda14cbcSMatt Macy dbuf_evict_one(); 761*eda14cbcSMatt Macy cv_signal(&dbuf_evict_cv); 762*eda14cbcSMatt Macy } 763*eda14cbcSMatt Macy } 764*eda14cbcSMatt Macy 765*eda14cbcSMatt Macy static int 766*eda14cbcSMatt Macy dbuf_kstat_update(kstat_t *ksp, int rw) 767*eda14cbcSMatt Macy { 768*eda14cbcSMatt Macy dbuf_stats_t *ds = ksp->ks_data; 769*eda14cbcSMatt Macy 770*eda14cbcSMatt Macy if (rw == KSTAT_WRITE) { 771*eda14cbcSMatt Macy return (SET_ERROR(EACCES)); 772*eda14cbcSMatt Macy } else { 773*eda14cbcSMatt Macy ds->metadata_cache_size_bytes.value.ui64 = zfs_refcount_count( 774*eda14cbcSMatt Macy &dbuf_caches[DB_DBUF_METADATA_CACHE].size); 775*eda14cbcSMatt Macy ds->cache_size_bytes.value.ui64 = 776*eda14cbcSMatt Macy zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size); 777*eda14cbcSMatt Macy ds->cache_target_bytes.value.ui64 = dbuf_cache_target_bytes(); 778*eda14cbcSMatt Macy ds->cache_hiwater_bytes.value.ui64 = dbuf_cache_hiwater_bytes(); 779*eda14cbcSMatt Macy ds->cache_lowater_bytes.value.ui64 = dbuf_cache_lowater_bytes(); 780*eda14cbcSMatt Macy ds->hash_elements.value.ui64 = dbuf_hash_count; 781*eda14cbcSMatt Macy } 782*eda14cbcSMatt Macy 783*eda14cbcSMatt Macy return (0); 784*eda14cbcSMatt Macy } 785*eda14cbcSMatt Macy 786*eda14cbcSMatt Macy void 787*eda14cbcSMatt Macy dbuf_init(void) 788*eda14cbcSMatt Macy { 789*eda14cbcSMatt Macy uint64_t hsize = 1ULL << 16; 790*eda14cbcSMatt Macy dbuf_hash_table_t *h = &dbuf_hash_table; 791*eda14cbcSMatt Macy int i; 792*eda14cbcSMatt Macy 793*eda14cbcSMatt Macy /* 794*eda14cbcSMatt Macy * The hash table is big enough to fill all of physical memory 795*eda14cbcSMatt Macy * with an average block size of zfs_arc_average_blocksize (default 8K). 796*eda14cbcSMatt Macy * By default, the table will take up 797*eda14cbcSMatt Macy * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 798*eda14cbcSMatt Macy */ 799*eda14cbcSMatt Macy while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE) 800*eda14cbcSMatt Macy hsize <<= 1; 801*eda14cbcSMatt Macy 802*eda14cbcSMatt Macy retry: 803*eda14cbcSMatt Macy h->hash_table_mask = hsize - 1; 804*eda14cbcSMatt Macy #if defined(_KERNEL) 805*eda14cbcSMatt Macy /* 806*eda14cbcSMatt Macy * Large allocations which do not require contiguous pages 807*eda14cbcSMatt Macy * should be using vmem_alloc() in the linux kernel 808*eda14cbcSMatt Macy */ 809*eda14cbcSMatt Macy h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP); 810*eda14cbcSMatt Macy #else 811*eda14cbcSMatt Macy h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP); 812*eda14cbcSMatt Macy #endif 813*eda14cbcSMatt Macy if (h->hash_table == NULL) { 814*eda14cbcSMatt Macy /* XXX - we should really return an error instead of assert */ 815*eda14cbcSMatt Macy ASSERT(hsize > (1ULL << 10)); 816*eda14cbcSMatt Macy hsize >>= 1; 817*eda14cbcSMatt Macy goto retry; 818*eda14cbcSMatt Macy } 819*eda14cbcSMatt Macy 820*eda14cbcSMatt Macy dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t", 821*eda14cbcSMatt Macy sizeof (dmu_buf_impl_t), 822*eda14cbcSMatt Macy 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0); 823*eda14cbcSMatt Macy 824*eda14cbcSMatt Macy for (i = 0; i < DBUF_MUTEXES; i++) 825*eda14cbcSMatt Macy mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL); 826*eda14cbcSMatt Macy 827*eda14cbcSMatt Macy dbuf_stats_init(h); 828*eda14cbcSMatt Macy 829*eda14cbcSMatt Macy /* 830*eda14cbcSMatt Macy * All entries are queued via taskq_dispatch_ent(), so min/maxalloc 831*eda14cbcSMatt Macy * configuration is not required. 832*eda14cbcSMatt Macy */ 833*eda14cbcSMatt Macy dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0); 834*eda14cbcSMatt Macy 835*eda14cbcSMatt Macy for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 836*eda14cbcSMatt Macy dbuf_caches[dcs].cache = 837*eda14cbcSMatt Macy multilist_create(sizeof (dmu_buf_impl_t), 838*eda14cbcSMatt Macy offsetof(dmu_buf_impl_t, db_cache_link), 839*eda14cbcSMatt Macy dbuf_cache_multilist_index_func); 840*eda14cbcSMatt Macy zfs_refcount_create(&dbuf_caches[dcs].size); 841*eda14cbcSMatt Macy } 842*eda14cbcSMatt Macy 843*eda14cbcSMatt Macy dbuf_evict_thread_exit = B_FALSE; 844*eda14cbcSMatt Macy mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL); 845*eda14cbcSMatt Macy cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL); 846*eda14cbcSMatt Macy dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread, 847*eda14cbcSMatt Macy NULL, 0, &p0, TS_RUN, minclsyspri); 848*eda14cbcSMatt Macy 849*eda14cbcSMatt Macy dbuf_ksp = kstat_create("zfs", 0, "dbufstats", "misc", 850*eda14cbcSMatt Macy KSTAT_TYPE_NAMED, sizeof (dbuf_stats) / sizeof (kstat_named_t), 851*eda14cbcSMatt Macy KSTAT_FLAG_VIRTUAL); 852*eda14cbcSMatt Macy if (dbuf_ksp != NULL) { 853*eda14cbcSMatt Macy for (i = 0; i < DN_MAX_LEVELS; i++) { 854*eda14cbcSMatt Macy snprintf(dbuf_stats.cache_levels[i].name, 855*eda14cbcSMatt Macy KSTAT_STRLEN, "cache_level_%d", i); 856*eda14cbcSMatt Macy dbuf_stats.cache_levels[i].data_type = 857*eda14cbcSMatt Macy KSTAT_DATA_UINT64; 858*eda14cbcSMatt Macy snprintf(dbuf_stats.cache_levels_bytes[i].name, 859*eda14cbcSMatt Macy KSTAT_STRLEN, "cache_level_%d_bytes", i); 860*eda14cbcSMatt Macy dbuf_stats.cache_levels_bytes[i].data_type = 861*eda14cbcSMatt Macy KSTAT_DATA_UINT64; 862*eda14cbcSMatt Macy } 863*eda14cbcSMatt Macy dbuf_ksp->ks_data = &dbuf_stats; 864*eda14cbcSMatt Macy dbuf_ksp->ks_update = dbuf_kstat_update; 865*eda14cbcSMatt Macy kstat_install(dbuf_ksp); 866*eda14cbcSMatt Macy } 867*eda14cbcSMatt Macy } 868*eda14cbcSMatt Macy 869*eda14cbcSMatt Macy void 870*eda14cbcSMatt Macy dbuf_fini(void) 871*eda14cbcSMatt Macy { 872*eda14cbcSMatt Macy dbuf_hash_table_t *h = &dbuf_hash_table; 873*eda14cbcSMatt Macy int i; 874*eda14cbcSMatt Macy 875*eda14cbcSMatt Macy dbuf_stats_destroy(); 876*eda14cbcSMatt Macy 877*eda14cbcSMatt Macy for (i = 0; i < DBUF_MUTEXES; i++) 878*eda14cbcSMatt Macy mutex_destroy(&h->hash_mutexes[i]); 879*eda14cbcSMatt Macy #if defined(_KERNEL) 880*eda14cbcSMatt Macy /* 881*eda14cbcSMatt Macy * Large allocations which do not require contiguous pages 882*eda14cbcSMatt Macy * should be using vmem_free() in the linux kernel 883*eda14cbcSMatt Macy */ 884*eda14cbcSMatt Macy vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 885*eda14cbcSMatt Macy #else 886*eda14cbcSMatt Macy kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 887*eda14cbcSMatt Macy #endif 888*eda14cbcSMatt Macy kmem_cache_destroy(dbuf_kmem_cache); 889*eda14cbcSMatt Macy taskq_destroy(dbu_evict_taskq); 890*eda14cbcSMatt Macy 891*eda14cbcSMatt Macy mutex_enter(&dbuf_evict_lock); 892*eda14cbcSMatt Macy dbuf_evict_thread_exit = B_TRUE; 893*eda14cbcSMatt Macy while (dbuf_evict_thread_exit) { 894*eda14cbcSMatt Macy cv_signal(&dbuf_evict_cv); 895*eda14cbcSMatt Macy cv_wait(&dbuf_evict_cv, &dbuf_evict_lock); 896*eda14cbcSMatt Macy } 897*eda14cbcSMatt Macy mutex_exit(&dbuf_evict_lock); 898*eda14cbcSMatt Macy 899*eda14cbcSMatt Macy mutex_destroy(&dbuf_evict_lock); 900*eda14cbcSMatt Macy cv_destroy(&dbuf_evict_cv); 901*eda14cbcSMatt Macy 902*eda14cbcSMatt Macy for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 903*eda14cbcSMatt Macy zfs_refcount_destroy(&dbuf_caches[dcs].size); 904*eda14cbcSMatt Macy multilist_destroy(dbuf_caches[dcs].cache); 905*eda14cbcSMatt Macy } 906*eda14cbcSMatt Macy 907*eda14cbcSMatt Macy if (dbuf_ksp != NULL) { 908*eda14cbcSMatt Macy kstat_delete(dbuf_ksp); 909*eda14cbcSMatt Macy dbuf_ksp = NULL; 910*eda14cbcSMatt Macy } 911*eda14cbcSMatt Macy } 912*eda14cbcSMatt Macy 913*eda14cbcSMatt Macy /* 914*eda14cbcSMatt Macy * Other stuff. 915*eda14cbcSMatt Macy */ 916*eda14cbcSMatt Macy 917*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 918*eda14cbcSMatt Macy static void 919*eda14cbcSMatt Macy dbuf_verify(dmu_buf_impl_t *db) 920*eda14cbcSMatt Macy { 921*eda14cbcSMatt Macy dnode_t *dn; 922*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 923*eda14cbcSMatt Macy uint32_t txg_prev; 924*eda14cbcSMatt Macy 925*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 926*eda14cbcSMatt Macy 927*eda14cbcSMatt Macy if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY)) 928*eda14cbcSMatt Macy return; 929*eda14cbcSMatt Macy 930*eda14cbcSMatt Macy ASSERT(db->db_objset != NULL); 931*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 932*eda14cbcSMatt Macy dn = DB_DNODE(db); 933*eda14cbcSMatt Macy if (dn == NULL) { 934*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL); 935*eda14cbcSMatt Macy ASSERT(db->db_blkptr == NULL); 936*eda14cbcSMatt Macy } else { 937*eda14cbcSMatt Macy ASSERT3U(db->db.db_object, ==, dn->dn_object); 938*eda14cbcSMatt Macy ASSERT3P(db->db_objset, ==, dn->dn_objset); 939*eda14cbcSMatt Macy ASSERT3U(db->db_level, <, dn->dn_nlevels); 940*eda14cbcSMatt Macy ASSERT(db->db_blkid == DMU_BONUS_BLKID || 941*eda14cbcSMatt Macy db->db_blkid == DMU_SPILL_BLKID || 942*eda14cbcSMatt Macy !avl_is_empty(&dn->dn_dbufs)); 943*eda14cbcSMatt Macy } 944*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 945*eda14cbcSMatt Macy ASSERT(dn != NULL); 946*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 947*eda14cbcSMatt Macy ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID); 948*eda14cbcSMatt Macy } else if (db->db_blkid == DMU_SPILL_BLKID) { 949*eda14cbcSMatt Macy ASSERT(dn != NULL); 950*eda14cbcSMatt Macy ASSERT0(db->db.db_offset); 951*eda14cbcSMatt Macy } else { 952*eda14cbcSMatt Macy ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size); 953*eda14cbcSMatt Macy } 954*eda14cbcSMatt Macy 955*eda14cbcSMatt Macy if ((dr = list_head(&db->db_dirty_records)) != NULL) { 956*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf == db); 957*eda14cbcSMatt Macy txg_prev = dr->dr_txg; 958*eda14cbcSMatt Macy for (dr = list_next(&db->db_dirty_records, dr); dr != NULL; 959*eda14cbcSMatt Macy dr = list_next(&db->db_dirty_records, dr)) { 960*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf == db); 961*eda14cbcSMatt Macy ASSERT(txg_prev > dr->dr_txg); 962*eda14cbcSMatt Macy txg_prev = dr->dr_txg; 963*eda14cbcSMatt Macy } 964*eda14cbcSMatt Macy } 965*eda14cbcSMatt Macy 966*eda14cbcSMatt Macy /* 967*eda14cbcSMatt Macy * We can't assert that db_size matches dn_datablksz because it 968*eda14cbcSMatt Macy * can be momentarily different when another thread is doing 969*eda14cbcSMatt Macy * dnode_set_blksz(). 970*eda14cbcSMatt Macy */ 971*eda14cbcSMatt Macy if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) { 972*eda14cbcSMatt Macy dr = db->db_data_pending; 973*eda14cbcSMatt Macy /* 974*eda14cbcSMatt Macy * It should only be modified in syncing context, so 975*eda14cbcSMatt Macy * make sure we only have one copy of the data. 976*eda14cbcSMatt Macy */ 977*eda14cbcSMatt Macy ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf); 978*eda14cbcSMatt Macy } 979*eda14cbcSMatt Macy 980*eda14cbcSMatt Macy /* verify db->db_blkptr */ 981*eda14cbcSMatt Macy if (db->db_blkptr) { 982*eda14cbcSMatt Macy if (db->db_parent == dn->dn_dbuf) { 983*eda14cbcSMatt Macy /* db is pointed to by the dnode */ 984*eda14cbcSMatt Macy /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */ 985*eda14cbcSMatt Macy if (DMU_OBJECT_IS_SPECIAL(db->db.db_object)) 986*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL); 987*eda14cbcSMatt Macy else 988*eda14cbcSMatt Macy ASSERT(db->db_parent != NULL); 989*eda14cbcSMatt Macy if (db->db_blkid != DMU_SPILL_BLKID) 990*eda14cbcSMatt Macy ASSERT3P(db->db_blkptr, ==, 991*eda14cbcSMatt Macy &dn->dn_phys->dn_blkptr[db->db_blkid]); 992*eda14cbcSMatt Macy } else { 993*eda14cbcSMatt Macy /* db is pointed to by an indirect block */ 994*eda14cbcSMatt Macy int epb __maybe_unused = db->db_parent->db.db_size >> 995*eda14cbcSMatt Macy SPA_BLKPTRSHIFT; 996*eda14cbcSMatt Macy ASSERT3U(db->db_parent->db_level, ==, db->db_level+1); 997*eda14cbcSMatt Macy ASSERT3U(db->db_parent->db.db_object, ==, 998*eda14cbcSMatt Macy db->db.db_object); 999*eda14cbcSMatt Macy /* 1000*eda14cbcSMatt Macy * dnode_grow_indblksz() can make this fail if we don't 1001*eda14cbcSMatt Macy * have the parent's rwlock. XXX indblksz no longer 1002*eda14cbcSMatt Macy * grows. safe to do this now? 1003*eda14cbcSMatt Macy */ 1004*eda14cbcSMatt Macy if (RW_LOCK_HELD(&db->db_parent->db_rwlock)) { 1005*eda14cbcSMatt Macy ASSERT3P(db->db_blkptr, ==, 1006*eda14cbcSMatt Macy ((blkptr_t *)db->db_parent->db.db_data + 1007*eda14cbcSMatt Macy db->db_blkid % epb)); 1008*eda14cbcSMatt Macy } 1009*eda14cbcSMatt Macy } 1010*eda14cbcSMatt Macy } 1011*eda14cbcSMatt Macy if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) && 1012*eda14cbcSMatt Macy (db->db_buf == NULL || db->db_buf->b_data) && 1013*eda14cbcSMatt Macy db->db.db_data && db->db_blkid != DMU_BONUS_BLKID && 1014*eda14cbcSMatt Macy db->db_state != DB_FILL && !dn->dn_free_txg) { 1015*eda14cbcSMatt Macy /* 1016*eda14cbcSMatt Macy * If the blkptr isn't set but they have nonzero data, 1017*eda14cbcSMatt Macy * it had better be dirty, otherwise we'll lose that 1018*eda14cbcSMatt Macy * data when we evict this buffer. 1019*eda14cbcSMatt Macy * 1020*eda14cbcSMatt Macy * There is an exception to this rule for indirect blocks; in 1021*eda14cbcSMatt Macy * this case, if the indirect block is a hole, we fill in a few 1022*eda14cbcSMatt Macy * fields on each of the child blocks (importantly, birth time) 1023*eda14cbcSMatt Macy * to prevent hole birth times from being lost when you 1024*eda14cbcSMatt Macy * partially fill in a hole. 1025*eda14cbcSMatt Macy */ 1026*eda14cbcSMatt Macy if (db->db_dirtycnt == 0) { 1027*eda14cbcSMatt Macy if (db->db_level == 0) { 1028*eda14cbcSMatt Macy uint64_t *buf = db->db.db_data; 1029*eda14cbcSMatt Macy int i; 1030*eda14cbcSMatt Macy 1031*eda14cbcSMatt Macy for (i = 0; i < db->db.db_size >> 3; i++) { 1032*eda14cbcSMatt Macy ASSERT(buf[i] == 0); 1033*eda14cbcSMatt Macy } 1034*eda14cbcSMatt Macy } else { 1035*eda14cbcSMatt Macy blkptr_t *bps = db->db.db_data; 1036*eda14cbcSMatt Macy ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==, 1037*eda14cbcSMatt Macy db->db.db_size); 1038*eda14cbcSMatt Macy /* 1039*eda14cbcSMatt Macy * We want to verify that all the blkptrs in the 1040*eda14cbcSMatt Macy * indirect block are holes, but we may have 1041*eda14cbcSMatt Macy * automatically set up a few fields for them. 1042*eda14cbcSMatt Macy * We iterate through each blkptr and verify 1043*eda14cbcSMatt Macy * they only have those fields set. 1044*eda14cbcSMatt Macy */ 1045*eda14cbcSMatt Macy for (int i = 0; 1046*eda14cbcSMatt Macy i < db->db.db_size / sizeof (blkptr_t); 1047*eda14cbcSMatt Macy i++) { 1048*eda14cbcSMatt Macy blkptr_t *bp = &bps[i]; 1049*eda14cbcSMatt Macy ASSERT(ZIO_CHECKSUM_IS_ZERO( 1050*eda14cbcSMatt Macy &bp->blk_cksum)); 1051*eda14cbcSMatt Macy ASSERT( 1052*eda14cbcSMatt Macy DVA_IS_EMPTY(&bp->blk_dva[0]) && 1053*eda14cbcSMatt Macy DVA_IS_EMPTY(&bp->blk_dva[1]) && 1054*eda14cbcSMatt Macy DVA_IS_EMPTY(&bp->blk_dva[2])); 1055*eda14cbcSMatt Macy ASSERT0(bp->blk_fill); 1056*eda14cbcSMatt Macy ASSERT0(bp->blk_pad[0]); 1057*eda14cbcSMatt Macy ASSERT0(bp->blk_pad[1]); 1058*eda14cbcSMatt Macy ASSERT(!BP_IS_EMBEDDED(bp)); 1059*eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(bp)); 1060*eda14cbcSMatt Macy ASSERT0(bp->blk_phys_birth); 1061*eda14cbcSMatt Macy } 1062*eda14cbcSMatt Macy } 1063*eda14cbcSMatt Macy } 1064*eda14cbcSMatt Macy } 1065*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1066*eda14cbcSMatt Macy } 1067*eda14cbcSMatt Macy #endif 1068*eda14cbcSMatt Macy 1069*eda14cbcSMatt Macy static void 1070*eda14cbcSMatt Macy dbuf_clear_data(dmu_buf_impl_t *db) 1071*eda14cbcSMatt Macy { 1072*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1073*eda14cbcSMatt Macy dbuf_evict_user(db); 1074*eda14cbcSMatt Macy ASSERT3P(db->db_buf, ==, NULL); 1075*eda14cbcSMatt Macy db->db.db_data = NULL; 1076*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL) { 1077*eda14cbcSMatt Macy db->db_state = DB_UNCACHED; 1078*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "clear data"); 1079*eda14cbcSMatt Macy } 1080*eda14cbcSMatt Macy } 1081*eda14cbcSMatt Macy 1082*eda14cbcSMatt Macy static void 1083*eda14cbcSMatt Macy dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf) 1084*eda14cbcSMatt Macy { 1085*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1086*eda14cbcSMatt Macy ASSERT(buf != NULL); 1087*eda14cbcSMatt Macy 1088*eda14cbcSMatt Macy db->db_buf = buf; 1089*eda14cbcSMatt Macy ASSERT(buf->b_data != NULL); 1090*eda14cbcSMatt Macy db->db.db_data = buf->b_data; 1091*eda14cbcSMatt Macy } 1092*eda14cbcSMatt Macy 1093*eda14cbcSMatt Macy static arc_buf_t * 1094*eda14cbcSMatt Macy dbuf_alloc_arcbuf_from_arcbuf(dmu_buf_impl_t *db, arc_buf_t *data) 1095*eda14cbcSMatt Macy { 1096*eda14cbcSMatt Macy objset_t *os = db->db_objset; 1097*eda14cbcSMatt Macy spa_t *spa = os->os_spa; 1098*eda14cbcSMatt Macy arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1099*eda14cbcSMatt Macy enum zio_compress compress_type; 1100*eda14cbcSMatt Macy uint8_t complevel; 1101*eda14cbcSMatt Macy int psize, lsize; 1102*eda14cbcSMatt Macy 1103*eda14cbcSMatt Macy psize = arc_buf_size(data); 1104*eda14cbcSMatt Macy lsize = arc_buf_lsize(data); 1105*eda14cbcSMatt Macy compress_type = arc_get_compression(data); 1106*eda14cbcSMatt Macy complevel = arc_get_complevel(data); 1107*eda14cbcSMatt Macy 1108*eda14cbcSMatt Macy if (arc_is_encrypted(data)) { 1109*eda14cbcSMatt Macy boolean_t byteorder; 1110*eda14cbcSMatt Macy uint8_t salt[ZIO_DATA_SALT_LEN]; 1111*eda14cbcSMatt Macy uint8_t iv[ZIO_DATA_IV_LEN]; 1112*eda14cbcSMatt Macy uint8_t mac[ZIO_DATA_MAC_LEN]; 1113*eda14cbcSMatt Macy dnode_t *dn = DB_DNODE(db); 1114*eda14cbcSMatt Macy 1115*eda14cbcSMatt Macy arc_get_raw_params(data, &byteorder, salt, iv, mac); 1116*eda14cbcSMatt Macy data = arc_alloc_raw_buf(spa, db, dmu_objset_id(os), 1117*eda14cbcSMatt Macy byteorder, salt, iv, mac, dn->dn_type, psize, lsize, 1118*eda14cbcSMatt Macy compress_type, complevel); 1119*eda14cbcSMatt Macy } else if (compress_type != ZIO_COMPRESS_OFF) { 1120*eda14cbcSMatt Macy ASSERT3U(type, ==, ARC_BUFC_DATA); 1121*eda14cbcSMatt Macy data = arc_alloc_compressed_buf(spa, db, 1122*eda14cbcSMatt Macy psize, lsize, compress_type, complevel); 1123*eda14cbcSMatt Macy } else { 1124*eda14cbcSMatt Macy data = arc_alloc_buf(spa, db, type, psize); 1125*eda14cbcSMatt Macy } 1126*eda14cbcSMatt Macy return (data); 1127*eda14cbcSMatt Macy } 1128*eda14cbcSMatt Macy 1129*eda14cbcSMatt Macy static arc_buf_t * 1130*eda14cbcSMatt Macy dbuf_alloc_arcbuf(dmu_buf_impl_t *db) 1131*eda14cbcSMatt Macy { 1132*eda14cbcSMatt Macy spa_t *spa = db->db_objset->os_spa; 1133*eda14cbcSMatt Macy 1134*eda14cbcSMatt Macy return (arc_alloc_buf(spa, db, DBUF_GET_BUFC_TYPE(db), db->db.db_size)); 1135*eda14cbcSMatt Macy } 1136*eda14cbcSMatt Macy 1137*eda14cbcSMatt Macy /* 1138*eda14cbcSMatt Macy * Loan out an arc_buf for read. Return the loaned arc_buf. 1139*eda14cbcSMatt Macy */ 1140*eda14cbcSMatt Macy arc_buf_t * 1141*eda14cbcSMatt Macy dbuf_loan_arcbuf(dmu_buf_impl_t *db) 1142*eda14cbcSMatt Macy { 1143*eda14cbcSMatt Macy arc_buf_t *abuf; 1144*eda14cbcSMatt Macy 1145*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1146*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1147*eda14cbcSMatt Macy if (arc_released(db->db_buf) || zfs_refcount_count(&db->db_holds) > 1) { 1148*eda14cbcSMatt Macy int blksz = db->db.db_size; 1149*eda14cbcSMatt Macy spa_t *spa = db->db_objset->os_spa; 1150*eda14cbcSMatt Macy 1151*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1152*eda14cbcSMatt Macy abuf = arc_loan_buf(spa, B_FALSE, blksz); 1153*eda14cbcSMatt Macy bcopy(db->db.db_data, abuf->b_data, blksz); 1154*eda14cbcSMatt Macy } else { 1155*eda14cbcSMatt Macy abuf = db->db_buf; 1156*eda14cbcSMatt Macy arc_loan_inuse_buf(abuf, db); 1157*eda14cbcSMatt Macy db->db_buf = NULL; 1158*eda14cbcSMatt Macy dbuf_clear_data(db); 1159*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1160*eda14cbcSMatt Macy } 1161*eda14cbcSMatt Macy return (abuf); 1162*eda14cbcSMatt Macy } 1163*eda14cbcSMatt Macy 1164*eda14cbcSMatt Macy /* 1165*eda14cbcSMatt Macy * Calculate which level n block references the data at the level 0 offset 1166*eda14cbcSMatt Macy * provided. 1167*eda14cbcSMatt Macy */ 1168*eda14cbcSMatt Macy uint64_t 1169*eda14cbcSMatt Macy dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset) 1170*eda14cbcSMatt Macy { 1171*eda14cbcSMatt Macy if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) { 1172*eda14cbcSMatt Macy /* 1173*eda14cbcSMatt Macy * The level n blkid is equal to the level 0 blkid divided by 1174*eda14cbcSMatt Macy * the number of level 0s in a level n block. 1175*eda14cbcSMatt Macy * 1176*eda14cbcSMatt Macy * The level 0 blkid is offset >> datablkshift = 1177*eda14cbcSMatt Macy * offset / 2^datablkshift. 1178*eda14cbcSMatt Macy * 1179*eda14cbcSMatt Macy * The number of level 0s in a level n is the number of block 1180*eda14cbcSMatt Macy * pointers in an indirect block, raised to the power of level. 1181*eda14cbcSMatt Macy * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level = 1182*eda14cbcSMatt Macy * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)). 1183*eda14cbcSMatt Macy * 1184*eda14cbcSMatt Macy * Thus, the level n blkid is: offset / 1185*eda14cbcSMatt Macy * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT)))) 1186*eda14cbcSMatt Macy * = offset / 2^(datablkshift + level * 1187*eda14cbcSMatt Macy * (indblkshift - SPA_BLKPTRSHIFT)) 1188*eda14cbcSMatt Macy * = offset >> (datablkshift + level * 1189*eda14cbcSMatt Macy * (indblkshift - SPA_BLKPTRSHIFT)) 1190*eda14cbcSMatt Macy */ 1191*eda14cbcSMatt Macy 1192*eda14cbcSMatt Macy const unsigned exp = dn->dn_datablkshift + 1193*eda14cbcSMatt Macy level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT); 1194*eda14cbcSMatt Macy 1195*eda14cbcSMatt Macy if (exp >= 8 * sizeof (offset)) { 1196*eda14cbcSMatt Macy /* This only happens on the highest indirection level */ 1197*eda14cbcSMatt Macy ASSERT3U(level, ==, dn->dn_nlevels - 1); 1198*eda14cbcSMatt Macy return (0); 1199*eda14cbcSMatt Macy } 1200*eda14cbcSMatt Macy 1201*eda14cbcSMatt Macy ASSERT3U(exp, <, 8 * sizeof (offset)); 1202*eda14cbcSMatt Macy 1203*eda14cbcSMatt Macy return (offset >> exp); 1204*eda14cbcSMatt Macy } else { 1205*eda14cbcSMatt Macy ASSERT3U(offset, <, dn->dn_datablksz); 1206*eda14cbcSMatt Macy return (0); 1207*eda14cbcSMatt Macy } 1208*eda14cbcSMatt Macy } 1209*eda14cbcSMatt Macy 1210*eda14cbcSMatt Macy /* 1211*eda14cbcSMatt Macy * This function is used to lock the parent of the provided dbuf. This should be 1212*eda14cbcSMatt Macy * used when modifying or reading db_blkptr. 1213*eda14cbcSMatt Macy */ 1214*eda14cbcSMatt Macy db_lock_type_t 1215*eda14cbcSMatt Macy dmu_buf_lock_parent(dmu_buf_impl_t *db, krw_t rw, void *tag) 1216*eda14cbcSMatt Macy { 1217*eda14cbcSMatt Macy enum db_lock_type ret = DLT_NONE; 1218*eda14cbcSMatt Macy if (db->db_parent != NULL) { 1219*eda14cbcSMatt Macy rw_enter(&db->db_parent->db_rwlock, rw); 1220*eda14cbcSMatt Macy ret = DLT_PARENT; 1221*eda14cbcSMatt Macy } else if (dmu_objset_ds(db->db_objset) != NULL) { 1222*eda14cbcSMatt Macy rrw_enter(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, rw, 1223*eda14cbcSMatt Macy tag); 1224*eda14cbcSMatt Macy ret = DLT_OBJSET; 1225*eda14cbcSMatt Macy } 1226*eda14cbcSMatt Macy /* 1227*eda14cbcSMatt Macy * We only return a DLT_NONE lock when it's the top-most indirect block 1228*eda14cbcSMatt Macy * of the meta-dnode of the MOS. 1229*eda14cbcSMatt Macy */ 1230*eda14cbcSMatt Macy return (ret); 1231*eda14cbcSMatt Macy } 1232*eda14cbcSMatt Macy 1233*eda14cbcSMatt Macy /* 1234*eda14cbcSMatt Macy * We need to pass the lock type in because it's possible that the block will 1235*eda14cbcSMatt Macy * move from being the topmost indirect block in a dnode (and thus, have no 1236*eda14cbcSMatt Macy * parent) to not the top-most via an indirection increase. This would cause a 1237*eda14cbcSMatt Macy * panic if we didn't pass the lock type in. 1238*eda14cbcSMatt Macy */ 1239*eda14cbcSMatt Macy void 1240*eda14cbcSMatt Macy dmu_buf_unlock_parent(dmu_buf_impl_t *db, db_lock_type_t type, void *tag) 1241*eda14cbcSMatt Macy { 1242*eda14cbcSMatt Macy if (type == DLT_PARENT) 1243*eda14cbcSMatt Macy rw_exit(&db->db_parent->db_rwlock); 1244*eda14cbcSMatt Macy else if (type == DLT_OBJSET) 1245*eda14cbcSMatt Macy rrw_exit(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, tag); 1246*eda14cbcSMatt Macy } 1247*eda14cbcSMatt Macy 1248*eda14cbcSMatt Macy static void 1249*eda14cbcSMatt Macy dbuf_read_done(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 1250*eda14cbcSMatt Macy arc_buf_t *buf, void *vdb) 1251*eda14cbcSMatt Macy { 1252*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 1253*eda14cbcSMatt Macy 1254*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1255*eda14cbcSMatt Macy ASSERT3U(db->db_state, ==, DB_READ); 1256*eda14cbcSMatt Macy /* 1257*eda14cbcSMatt Macy * All reads are synchronous, so we must have a hold on the dbuf 1258*eda14cbcSMatt Macy */ 1259*eda14cbcSMatt Macy ASSERT(zfs_refcount_count(&db->db_holds) > 0); 1260*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL); 1261*eda14cbcSMatt Macy ASSERT(db->db.db_data == NULL); 1262*eda14cbcSMatt Macy if (buf == NULL) { 1263*eda14cbcSMatt Macy /* i/o error */ 1264*eda14cbcSMatt Macy ASSERT(zio == NULL || zio->io_error != 0); 1265*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1266*eda14cbcSMatt Macy ASSERT3P(db->db_buf, ==, NULL); 1267*eda14cbcSMatt Macy db->db_state = DB_UNCACHED; 1268*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "i/o error"); 1269*eda14cbcSMatt Macy } else if (db->db_level == 0 && db->db_freed_in_flight) { 1270*eda14cbcSMatt Macy /* freed in flight */ 1271*eda14cbcSMatt Macy ASSERT(zio == NULL || zio->io_error == 0); 1272*eda14cbcSMatt Macy arc_release(buf, db); 1273*eda14cbcSMatt Macy bzero(buf->b_data, db->db.db_size); 1274*eda14cbcSMatt Macy arc_buf_freeze(buf); 1275*eda14cbcSMatt Macy db->db_freed_in_flight = FALSE; 1276*eda14cbcSMatt Macy dbuf_set_data(db, buf); 1277*eda14cbcSMatt Macy db->db_state = DB_CACHED; 1278*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "freed in flight"); 1279*eda14cbcSMatt Macy } else { 1280*eda14cbcSMatt Macy /* success */ 1281*eda14cbcSMatt Macy ASSERT(zio == NULL || zio->io_error == 0); 1282*eda14cbcSMatt Macy dbuf_set_data(db, buf); 1283*eda14cbcSMatt Macy db->db_state = DB_CACHED; 1284*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "successful read"); 1285*eda14cbcSMatt Macy } 1286*eda14cbcSMatt Macy cv_broadcast(&db->db_changed); 1287*eda14cbcSMatt Macy dbuf_rele_and_unlock(db, NULL, B_FALSE); 1288*eda14cbcSMatt Macy } 1289*eda14cbcSMatt Macy 1290*eda14cbcSMatt Macy /* 1291*eda14cbcSMatt Macy * Shortcut for performing reads on bonus dbufs. Returns 1292*eda14cbcSMatt Macy * an error if we fail to verify the dnode associated with 1293*eda14cbcSMatt Macy * a decrypted block. Otherwise success. 1294*eda14cbcSMatt Macy */ 1295*eda14cbcSMatt Macy static int 1296*eda14cbcSMatt Macy dbuf_read_bonus(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags) 1297*eda14cbcSMatt Macy { 1298*eda14cbcSMatt Macy int bonuslen, max_bonuslen, err; 1299*eda14cbcSMatt Macy 1300*eda14cbcSMatt Macy err = dbuf_read_verify_dnode_crypt(db, flags); 1301*eda14cbcSMatt Macy if (err) 1302*eda14cbcSMatt Macy return (err); 1303*eda14cbcSMatt Macy 1304*eda14cbcSMatt Macy bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen); 1305*eda14cbcSMatt Macy max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 1306*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1307*eda14cbcSMatt Macy ASSERT(DB_DNODE_HELD(db)); 1308*eda14cbcSMatt Macy ASSERT3U(bonuslen, <=, db->db.db_size); 1309*eda14cbcSMatt Macy db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP); 1310*eda14cbcSMatt Macy arc_space_consume(max_bonuslen, ARC_SPACE_BONUS); 1311*eda14cbcSMatt Macy if (bonuslen < max_bonuslen) 1312*eda14cbcSMatt Macy bzero(db->db.db_data, max_bonuslen); 1313*eda14cbcSMatt Macy if (bonuslen) 1314*eda14cbcSMatt Macy bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen); 1315*eda14cbcSMatt Macy db->db_state = DB_CACHED; 1316*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "bonus buffer filled"); 1317*eda14cbcSMatt Macy return (0); 1318*eda14cbcSMatt Macy } 1319*eda14cbcSMatt Macy 1320*eda14cbcSMatt Macy static void 1321*eda14cbcSMatt Macy dbuf_handle_indirect_hole(dmu_buf_impl_t *db, dnode_t *dn) 1322*eda14cbcSMatt Macy { 1323*eda14cbcSMatt Macy blkptr_t *bps = db->db.db_data; 1324*eda14cbcSMatt Macy uint32_t indbs = 1ULL << dn->dn_indblkshift; 1325*eda14cbcSMatt Macy int n_bps = indbs >> SPA_BLKPTRSHIFT; 1326*eda14cbcSMatt Macy 1327*eda14cbcSMatt Macy for (int i = 0; i < n_bps; i++) { 1328*eda14cbcSMatt Macy blkptr_t *bp = &bps[i]; 1329*eda14cbcSMatt Macy 1330*eda14cbcSMatt Macy ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, indbs); 1331*eda14cbcSMatt Macy BP_SET_LSIZE(bp, BP_GET_LEVEL(db->db_blkptr) == 1 ? 1332*eda14cbcSMatt Macy dn->dn_datablksz : BP_GET_LSIZE(db->db_blkptr)); 1333*eda14cbcSMatt Macy BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr)); 1334*eda14cbcSMatt Macy BP_SET_LEVEL(bp, BP_GET_LEVEL(db->db_blkptr) - 1); 1335*eda14cbcSMatt Macy BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0); 1336*eda14cbcSMatt Macy } 1337*eda14cbcSMatt Macy } 1338*eda14cbcSMatt Macy 1339*eda14cbcSMatt Macy /* 1340*eda14cbcSMatt Macy * Handle reads on dbufs that are holes, if necessary. This function 1341*eda14cbcSMatt Macy * requires that the dbuf's mutex is held. Returns success (0) if action 1342*eda14cbcSMatt Macy * was taken, ENOENT if no action was taken. 1343*eda14cbcSMatt Macy */ 1344*eda14cbcSMatt Macy static int 1345*eda14cbcSMatt Macy dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags) 1346*eda14cbcSMatt Macy { 1347*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1348*eda14cbcSMatt Macy 1349*eda14cbcSMatt Macy int is_hole = db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr); 1350*eda14cbcSMatt Macy /* 1351*eda14cbcSMatt Macy * For level 0 blocks only, if the above check fails: 1352*eda14cbcSMatt Macy * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync() 1353*eda14cbcSMatt Macy * processes the delete record and clears the bp while we are waiting 1354*eda14cbcSMatt Macy * for the dn_mtx (resulting in a "no" from block_freed). 1355*eda14cbcSMatt Macy */ 1356*eda14cbcSMatt Macy if (!is_hole && db->db_level == 0) { 1357*eda14cbcSMatt Macy is_hole = dnode_block_freed(dn, db->db_blkid) || 1358*eda14cbcSMatt Macy BP_IS_HOLE(db->db_blkptr); 1359*eda14cbcSMatt Macy } 1360*eda14cbcSMatt Macy 1361*eda14cbcSMatt Macy if (is_hole) { 1362*eda14cbcSMatt Macy dbuf_set_data(db, dbuf_alloc_arcbuf(db)); 1363*eda14cbcSMatt Macy bzero(db->db.db_data, db->db.db_size); 1364*eda14cbcSMatt Macy 1365*eda14cbcSMatt Macy if (db->db_blkptr != NULL && db->db_level > 0 && 1366*eda14cbcSMatt Macy BP_IS_HOLE(db->db_blkptr) && 1367*eda14cbcSMatt Macy db->db_blkptr->blk_birth != 0) { 1368*eda14cbcSMatt Macy dbuf_handle_indirect_hole(db, dn); 1369*eda14cbcSMatt Macy } 1370*eda14cbcSMatt Macy db->db_state = DB_CACHED; 1371*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "hole read satisfied"); 1372*eda14cbcSMatt Macy return (0); 1373*eda14cbcSMatt Macy } 1374*eda14cbcSMatt Macy return (ENOENT); 1375*eda14cbcSMatt Macy } 1376*eda14cbcSMatt Macy 1377*eda14cbcSMatt Macy /* 1378*eda14cbcSMatt Macy * This function ensures that, when doing a decrypting read of a block, 1379*eda14cbcSMatt Macy * we make sure we have decrypted the dnode associated with it. We must do 1380*eda14cbcSMatt Macy * this so that we ensure we are fully authenticating the checksum-of-MACs 1381*eda14cbcSMatt Macy * tree from the root of the objset down to this block. Indirect blocks are 1382*eda14cbcSMatt Macy * always verified against their secure checksum-of-MACs assuming that the 1383*eda14cbcSMatt Macy * dnode containing them is correct. Now that we are doing a decrypting read, 1384*eda14cbcSMatt Macy * we can be sure that the key is loaded and verify that assumption. This is 1385*eda14cbcSMatt Macy * especially important considering that we always read encrypted dnode 1386*eda14cbcSMatt Macy * blocks as raw data (without verifying their MACs) to start, and 1387*eda14cbcSMatt Macy * decrypt / authenticate them when we need to read an encrypted bonus buffer. 1388*eda14cbcSMatt Macy */ 1389*eda14cbcSMatt Macy static int 1390*eda14cbcSMatt Macy dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags) 1391*eda14cbcSMatt Macy { 1392*eda14cbcSMatt Macy int err = 0; 1393*eda14cbcSMatt Macy objset_t *os = db->db_objset; 1394*eda14cbcSMatt Macy arc_buf_t *dnode_abuf; 1395*eda14cbcSMatt Macy dnode_t *dn; 1396*eda14cbcSMatt Macy zbookmark_phys_t zb; 1397*eda14cbcSMatt Macy 1398*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1399*eda14cbcSMatt Macy 1400*eda14cbcSMatt Macy if (!os->os_encrypted || os->os_raw_receive || 1401*eda14cbcSMatt Macy (flags & DB_RF_NO_DECRYPT) != 0) 1402*eda14cbcSMatt Macy return (0); 1403*eda14cbcSMatt Macy 1404*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 1405*eda14cbcSMatt Macy dn = DB_DNODE(db); 1406*eda14cbcSMatt Macy dnode_abuf = (dn->dn_dbuf != NULL) ? dn->dn_dbuf->db_buf : NULL; 1407*eda14cbcSMatt Macy 1408*eda14cbcSMatt Macy if (dnode_abuf == NULL || !arc_is_encrypted(dnode_abuf)) { 1409*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1410*eda14cbcSMatt Macy return (0); 1411*eda14cbcSMatt Macy } 1412*eda14cbcSMatt Macy 1413*eda14cbcSMatt Macy SET_BOOKMARK(&zb, dmu_objset_id(os), 1414*eda14cbcSMatt Macy DMU_META_DNODE_OBJECT, 0, dn->dn_dbuf->db_blkid); 1415*eda14cbcSMatt Macy err = arc_untransform(dnode_abuf, os->os_spa, &zb, B_TRUE); 1416*eda14cbcSMatt Macy 1417*eda14cbcSMatt Macy /* 1418*eda14cbcSMatt Macy * An error code of EACCES tells us that the key is still not 1419*eda14cbcSMatt Macy * available. This is ok if we are only reading authenticated 1420*eda14cbcSMatt Macy * (and therefore non-encrypted) blocks. 1421*eda14cbcSMatt Macy */ 1422*eda14cbcSMatt Macy if (err == EACCES && ((db->db_blkid != DMU_BONUS_BLKID && 1423*eda14cbcSMatt Macy !DMU_OT_IS_ENCRYPTED(dn->dn_type)) || 1424*eda14cbcSMatt Macy (db->db_blkid == DMU_BONUS_BLKID && 1425*eda14cbcSMatt Macy !DMU_OT_IS_ENCRYPTED(dn->dn_bonustype)))) 1426*eda14cbcSMatt Macy err = 0; 1427*eda14cbcSMatt Macy 1428*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1429*eda14cbcSMatt Macy 1430*eda14cbcSMatt Macy return (err); 1431*eda14cbcSMatt Macy } 1432*eda14cbcSMatt Macy 1433*eda14cbcSMatt Macy /* 1434*eda14cbcSMatt Macy * Drops db_mtx and the parent lock specified by dblt and tag before 1435*eda14cbcSMatt Macy * returning. 1436*eda14cbcSMatt Macy */ 1437*eda14cbcSMatt Macy static int 1438*eda14cbcSMatt Macy dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags, 1439*eda14cbcSMatt Macy db_lock_type_t dblt, void *tag) 1440*eda14cbcSMatt Macy { 1441*eda14cbcSMatt Macy dnode_t *dn; 1442*eda14cbcSMatt Macy zbookmark_phys_t zb; 1443*eda14cbcSMatt Macy uint32_t aflags = ARC_FLAG_NOWAIT; 1444*eda14cbcSMatt Macy int err, zio_flags; 1445*eda14cbcSMatt Macy boolean_t bonus_read; 1446*eda14cbcSMatt Macy 1447*eda14cbcSMatt Macy err = zio_flags = 0; 1448*eda14cbcSMatt Macy bonus_read = B_FALSE; 1449*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 1450*eda14cbcSMatt Macy dn = DB_DNODE(db); 1451*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1452*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1453*eda14cbcSMatt Macy ASSERT(db->db_state == DB_UNCACHED); 1454*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL); 1455*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL || 1456*eda14cbcSMatt Macy RW_LOCK_HELD(&db->db_parent->db_rwlock)); 1457*eda14cbcSMatt Macy 1458*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 1459*eda14cbcSMatt Macy err = dbuf_read_bonus(db, dn, flags); 1460*eda14cbcSMatt Macy goto early_unlock; 1461*eda14cbcSMatt Macy } 1462*eda14cbcSMatt Macy 1463*eda14cbcSMatt Macy err = dbuf_read_hole(db, dn, flags); 1464*eda14cbcSMatt Macy if (err == 0) 1465*eda14cbcSMatt Macy goto early_unlock; 1466*eda14cbcSMatt Macy 1467*eda14cbcSMatt Macy /* 1468*eda14cbcSMatt Macy * Any attempt to read a redacted block should result in an error. This 1469*eda14cbcSMatt Macy * will never happen under normal conditions, but can be useful for 1470*eda14cbcSMatt Macy * debugging purposes. 1471*eda14cbcSMatt Macy */ 1472*eda14cbcSMatt Macy if (BP_IS_REDACTED(db->db_blkptr)) { 1473*eda14cbcSMatt Macy ASSERT(dsl_dataset_feature_is_active( 1474*eda14cbcSMatt Macy db->db_objset->os_dsl_dataset, 1475*eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)); 1476*eda14cbcSMatt Macy err = SET_ERROR(EIO); 1477*eda14cbcSMatt Macy goto early_unlock; 1478*eda14cbcSMatt Macy } 1479*eda14cbcSMatt Macy 1480*eda14cbcSMatt Macy SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 1481*eda14cbcSMatt Macy db->db.db_object, db->db_level, db->db_blkid); 1482*eda14cbcSMatt Macy 1483*eda14cbcSMatt Macy /* 1484*eda14cbcSMatt Macy * All bps of an encrypted os should have the encryption bit set. 1485*eda14cbcSMatt Macy * If this is not true it indicates tampering and we report an error. 1486*eda14cbcSMatt Macy */ 1487*eda14cbcSMatt Macy if (db->db_objset->os_encrypted && !BP_USES_CRYPT(db->db_blkptr)) { 1488*eda14cbcSMatt Macy spa_log_error(db->db_objset->os_spa, &zb); 1489*eda14cbcSMatt Macy zfs_panic_recover("unencrypted block in encrypted " 1490*eda14cbcSMatt Macy "object set %llu", dmu_objset_id(db->db_objset)); 1491*eda14cbcSMatt Macy err = SET_ERROR(EIO); 1492*eda14cbcSMatt Macy goto early_unlock; 1493*eda14cbcSMatt Macy } 1494*eda14cbcSMatt Macy 1495*eda14cbcSMatt Macy err = dbuf_read_verify_dnode_crypt(db, flags); 1496*eda14cbcSMatt Macy if (err != 0) 1497*eda14cbcSMatt Macy goto early_unlock; 1498*eda14cbcSMatt Macy 1499*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1500*eda14cbcSMatt Macy 1501*eda14cbcSMatt Macy db->db_state = DB_READ; 1502*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "read issued"); 1503*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1504*eda14cbcSMatt Macy 1505*eda14cbcSMatt Macy if (DBUF_IS_L2CACHEABLE(db)) 1506*eda14cbcSMatt Macy aflags |= ARC_FLAG_L2CACHE; 1507*eda14cbcSMatt Macy 1508*eda14cbcSMatt Macy dbuf_add_ref(db, NULL); 1509*eda14cbcSMatt Macy 1510*eda14cbcSMatt Macy zio_flags = (flags & DB_RF_CANFAIL) ? 1511*eda14cbcSMatt Macy ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED; 1512*eda14cbcSMatt Macy 1513*eda14cbcSMatt Macy if ((flags & DB_RF_NO_DECRYPT) && BP_IS_PROTECTED(db->db_blkptr)) 1514*eda14cbcSMatt Macy zio_flags |= ZIO_FLAG_RAW; 1515*eda14cbcSMatt Macy /* 1516*eda14cbcSMatt Macy * The zio layer will copy the provided blkptr later, but we need to 1517*eda14cbcSMatt Macy * do this now so that we can release the parent's rwlock. We have to 1518*eda14cbcSMatt Macy * do that now so that if dbuf_read_done is called synchronously (on 1519*eda14cbcSMatt Macy * an l1 cache hit) we don't acquire the db_mtx while holding the 1520*eda14cbcSMatt Macy * parent's rwlock, which would be a lock ordering violation. 1521*eda14cbcSMatt Macy */ 1522*eda14cbcSMatt Macy blkptr_t bp = *db->db_blkptr; 1523*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, tag); 1524*eda14cbcSMatt Macy (void) arc_read(zio, db->db_objset->os_spa, &bp, 1525*eda14cbcSMatt Macy dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, zio_flags, 1526*eda14cbcSMatt Macy &aflags, &zb); 1527*eda14cbcSMatt Macy return (err); 1528*eda14cbcSMatt Macy early_unlock: 1529*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1530*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1531*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, tag); 1532*eda14cbcSMatt Macy return (err); 1533*eda14cbcSMatt Macy } 1534*eda14cbcSMatt Macy 1535*eda14cbcSMatt Macy /* 1536*eda14cbcSMatt Macy * This is our just-in-time copy function. It makes a copy of buffers that 1537*eda14cbcSMatt Macy * have been modified in a previous transaction group before we access them in 1538*eda14cbcSMatt Macy * the current active group. 1539*eda14cbcSMatt Macy * 1540*eda14cbcSMatt Macy * This function is used in three places: when we are dirtying a buffer for the 1541*eda14cbcSMatt Macy * first time in a txg, when we are freeing a range in a dnode that includes 1542*eda14cbcSMatt Macy * this buffer, and when we are accessing a buffer which was received compressed 1543*eda14cbcSMatt Macy * and later referenced in a WRITE_BYREF record. 1544*eda14cbcSMatt Macy * 1545*eda14cbcSMatt Macy * Note that when we are called from dbuf_free_range() we do not put a hold on 1546*eda14cbcSMatt Macy * the buffer, we just traverse the active dbuf list for the dnode. 1547*eda14cbcSMatt Macy */ 1548*eda14cbcSMatt Macy static void 1549*eda14cbcSMatt Macy dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg) 1550*eda14cbcSMatt Macy { 1551*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records); 1552*eda14cbcSMatt Macy 1553*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1554*eda14cbcSMatt Macy ASSERT(db->db.db_data != NULL); 1555*eda14cbcSMatt Macy ASSERT(db->db_level == 0); 1556*eda14cbcSMatt Macy ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT); 1557*eda14cbcSMatt Macy 1558*eda14cbcSMatt Macy if (dr == NULL || 1559*eda14cbcSMatt Macy (dr->dt.dl.dr_data != 1560*eda14cbcSMatt Macy ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf))) 1561*eda14cbcSMatt Macy return; 1562*eda14cbcSMatt Macy 1563*eda14cbcSMatt Macy /* 1564*eda14cbcSMatt Macy * If the last dirty record for this dbuf has not yet synced 1565*eda14cbcSMatt Macy * and its referencing the dbuf data, either: 1566*eda14cbcSMatt Macy * reset the reference to point to a new copy, 1567*eda14cbcSMatt Macy * or (if there a no active holders) 1568*eda14cbcSMatt Macy * just null out the current db_data pointer. 1569*eda14cbcSMatt Macy */ 1570*eda14cbcSMatt Macy ASSERT3U(dr->dr_txg, >=, txg - 2); 1571*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 1572*eda14cbcSMatt Macy dnode_t *dn = DB_DNODE(db); 1573*eda14cbcSMatt Macy int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 1574*eda14cbcSMatt Macy dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP); 1575*eda14cbcSMatt Macy arc_space_consume(bonuslen, ARC_SPACE_BONUS); 1576*eda14cbcSMatt Macy bcopy(db->db.db_data, dr->dt.dl.dr_data, bonuslen); 1577*eda14cbcSMatt Macy } else if (zfs_refcount_count(&db->db_holds) > db->db_dirtycnt) { 1578*eda14cbcSMatt Macy arc_buf_t *buf = dbuf_alloc_arcbuf_from_arcbuf(db, db->db_buf); 1579*eda14cbcSMatt Macy dr->dt.dl.dr_data = buf; 1580*eda14cbcSMatt Macy bcopy(db->db.db_data, buf->b_data, arc_buf_size(buf)); 1581*eda14cbcSMatt Macy } else { 1582*eda14cbcSMatt Macy db->db_buf = NULL; 1583*eda14cbcSMatt Macy dbuf_clear_data(db); 1584*eda14cbcSMatt Macy } 1585*eda14cbcSMatt Macy } 1586*eda14cbcSMatt Macy 1587*eda14cbcSMatt Macy int 1588*eda14cbcSMatt Macy dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 1589*eda14cbcSMatt Macy { 1590*eda14cbcSMatt Macy int err = 0; 1591*eda14cbcSMatt Macy boolean_t prefetch; 1592*eda14cbcSMatt Macy dnode_t *dn; 1593*eda14cbcSMatt Macy 1594*eda14cbcSMatt Macy /* 1595*eda14cbcSMatt Macy * We don't have to hold the mutex to check db_state because it 1596*eda14cbcSMatt Macy * can't be freed while we have a hold on the buffer. 1597*eda14cbcSMatt Macy */ 1598*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1599*eda14cbcSMatt Macy 1600*eda14cbcSMatt Macy if (db->db_state == DB_NOFILL) 1601*eda14cbcSMatt Macy return (SET_ERROR(EIO)); 1602*eda14cbcSMatt Macy 1603*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 1604*eda14cbcSMatt Macy dn = DB_DNODE(db); 1605*eda14cbcSMatt Macy 1606*eda14cbcSMatt Macy prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1607*eda14cbcSMatt Macy (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL && 1608*eda14cbcSMatt Macy DBUF_IS_CACHEABLE(db); 1609*eda14cbcSMatt Macy 1610*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1611*eda14cbcSMatt Macy if (db->db_state == DB_CACHED) { 1612*eda14cbcSMatt Macy spa_t *spa = dn->dn_objset->os_spa; 1613*eda14cbcSMatt Macy 1614*eda14cbcSMatt Macy /* 1615*eda14cbcSMatt Macy * Ensure that this block's dnode has been decrypted if 1616*eda14cbcSMatt Macy * the caller has requested decrypted data. 1617*eda14cbcSMatt Macy */ 1618*eda14cbcSMatt Macy err = dbuf_read_verify_dnode_crypt(db, flags); 1619*eda14cbcSMatt Macy 1620*eda14cbcSMatt Macy /* 1621*eda14cbcSMatt Macy * If the arc buf is compressed or encrypted and the caller 1622*eda14cbcSMatt Macy * requested uncompressed data, we need to untransform it 1623*eda14cbcSMatt Macy * before returning. We also call arc_untransform() on any 1624*eda14cbcSMatt Macy * unauthenticated blocks, which will verify their MAC if 1625*eda14cbcSMatt Macy * the key is now available. 1626*eda14cbcSMatt Macy */ 1627*eda14cbcSMatt Macy if (err == 0 && db->db_buf != NULL && 1628*eda14cbcSMatt Macy (flags & DB_RF_NO_DECRYPT) == 0 && 1629*eda14cbcSMatt Macy (arc_is_encrypted(db->db_buf) || 1630*eda14cbcSMatt Macy arc_is_unauthenticated(db->db_buf) || 1631*eda14cbcSMatt Macy arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF)) { 1632*eda14cbcSMatt Macy zbookmark_phys_t zb; 1633*eda14cbcSMatt Macy 1634*eda14cbcSMatt Macy SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 1635*eda14cbcSMatt Macy db->db.db_object, db->db_level, db->db_blkid); 1636*eda14cbcSMatt Macy dbuf_fix_old_data(db, spa_syncing_txg(spa)); 1637*eda14cbcSMatt Macy err = arc_untransform(db->db_buf, spa, &zb, B_FALSE); 1638*eda14cbcSMatt Macy dbuf_set_data(db, db->db_buf); 1639*eda14cbcSMatt Macy } 1640*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1641*eda14cbcSMatt Macy if (err == 0 && prefetch) { 1642*eda14cbcSMatt Macy dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1643*eda14cbcSMatt Macy flags & DB_RF_HAVESTRUCT); 1644*eda14cbcSMatt Macy } 1645*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1646*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_hits); 1647*eda14cbcSMatt Macy } else if (db->db_state == DB_UNCACHED) { 1648*eda14cbcSMatt Macy spa_t *spa = dn->dn_objset->os_spa; 1649*eda14cbcSMatt Macy boolean_t need_wait = B_FALSE; 1650*eda14cbcSMatt Macy 1651*eda14cbcSMatt Macy db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG); 1652*eda14cbcSMatt Macy 1653*eda14cbcSMatt Macy if (zio == NULL && 1654*eda14cbcSMatt Macy db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) { 1655*eda14cbcSMatt Macy zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 1656*eda14cbcSMatt Macy need_wait = B_TRUE; 1657*eda14cbcSMatt Macy } 1658*eda14cbcSMatt Macy err = dbuf_read_impl(db, zio, flags, dblt, FTAG); 1659*eda14cbcSMatt Macy /* 1660*eda14cbcSMatt Macy * dbuf_read_impl has dropped db_mtx and our parent's rwlock 1661*eda14cbcSMatt Macy * for us 1662*eda14cbcSMatt Macy */ 1663*eda14cbcSMatt Macy if (!err && prefetch) { 1664*eda14cbcSMatt Macy dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1665*eda14cbcSMatt Macy flags & DB_RF_HAVESTRUCT); 1666*eda14cbcSMatt Macy } 1667*eda14cbcSMatt Macy 1668*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1669*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_misses); 1670*eda14cbcSMatt Macy 1671*eda14cbcSMatt Macy /* 1672*eda14cbcSMatt Macy * If we created a zio_root we must execute it to avoid 1673*eda14cbcSMatt Macy * leaking it, even if it isn't attached to any work due 1674*eda14cbcSMatt Macy * to an error in dbuf_read_impl(). 1675*eda14cbcSMatt Macy */ 1676*eda14cbcSMatt Macy if (need_wait) { 1677*eda14cbcSMatt Macy if (err == 0) 1678*eda14cbcSMatt Macy err = zio_wait(zio); 1679*eda14cbcSMatt Macy else 1680*eda14cbcSMatt Macy VERIFY0(zio_wait(zio)); 1681*eda14cbcSMatt Macy } 1682*eda14cbcSMatt Macy } else { 1683*eda14cbcSMatt Macy /* 1684*eda14cbcSMatt Macy * Another reader came in while the dbuf was in flight 1685*eda14cbcSMatt Macy * between UNCACHED and CACHED. Either a writer will finish 1686*eda14cbcSMatt Macy * writing the buffer (sending the dbuf to CACHED) or the 1687*eda14cbcSMatt Macy * first reader's request will reach the read_done callback 1688*eda14cbcSMatt Macy * and send the dbuf to CACHED. Otherwise, a failure 1689*eda14cbcSMatt Macy * occurred and the dbuf went to UNCACHED. 1690*eda14cbcSMatt Macy */ 1691*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1692*eda14cbcSMatt Macy if (prefetch) { 1693*eda14cbcSMatt Macy dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1694*eda14cbcSMatt Macy flags & DB_RF_HAVESTRUCT); 1695*eda14cbcSMatt Macy } 1696*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1697*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_misses); 1698*eda14cbcSMatt Macy 1699*eda14cbcSMatt Macy /* Skip the wait per the caller's request. */ 1700*eda14cbcSMatt Macy if ((flags & DB_RF_NEVERWAIT) == 0) { 1701*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1702*eda14cbcSMatt Macy while (db->db_state == DB_READ || 1703*eda14cbcSMatt Macy db->db_state == DB_FILL) { 1704*eda14cbcSMatt Macy ASSERT(db->db_state == DB_READ || 1705*eda14cbcSMatt Macy (flags & DB_RF_HAVESTRUCT) == 0); 1706*eda14cbcSMatt Macy DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *, 1707*eda14cbcSMatt Macy db, zio_t *, zio); 1708*eda14cbcSMatt Macy cv_wait(&db->db_changed, &db->db_mtx); 1709*eda14cbcSMatt Macy } 1710*eda14cbcSMatt Macy if (db->db_state == DB_UNCACHED) 1711*eda14cbcSMatt Macy err = SET_ERROR(EIO); 1712*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1713*eda14cbcSMatt Macy } 1714*eda14cbcSMatt Macy } 1715*eda14cbcSMatt Macy 1716*eda14cbcSMatt Macy return (err); 1717*eda14cbcSMatt Macy } 1718*eda14cbcSMatt Macy 1719*eda14cbcSMatt Macy static void 1720*eda14cbcSMatt Macy dbuf_noread(dmu_buf_impl_t *db) 1721*eda14cbcSMatt Macy { 1722*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1723*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1724*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1725*eda14cbcSMatt Macy while (db->db_state == DB_READ || db->db_state == DB_FILL) 1726*eda14cbcSMatt Macy cv_wait(&db->db_changed, &db->db_mtx); 1727*eda14cbcSMatt Macy if (db->db_state == DB_UNCACHED) { 1728*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL); 1729*eda14cbcSMatt Macy ASSERT(db->db.db_data == NULL); 1730*eda14cbcSMatt Macy dbuf_set_data(db, dbuf_alloc_arcbuf(db)); 1731*eda14cbcSMatt Macy db->db_state = DB_FILL; 1732*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "assigning filled buffer"); 1733*eda14cbcSMatt Macy } else if (db->db_state == DB_NOFILL) { 1734*eda14cbcSMatt Macy dbuf_clear_data(db); 1735*eda14cbcSMatt Macy } else { 1736*eda14cbcSMatt Macy ASSERT3U(db->db_state, ==, DB_CACHED); 1737*eda14cbcSMatt Macy } 1738*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1739*eda14cbcSMatt Macy } 1740*eda14cbcSMatt Macy 1741*eda14cbcSMatt Macy void 1742*eda14cbcSMatt Macy dbuf_unoverride(dbuf_dirty_record_t *dr) 1743*eda14cbcSMatt Macy { 1744*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 1745*eda14cbcSMatt Macy blkptr_t *bp = &dr->dt.dl.dr_overridden_by; 1746*eda14cbcSMatt Macy uint64_t txg = dr->dr_txg; 1747*eda14cbcSMatt Macy 1748*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1749*eda14cbcSMatt Macy /* 1750*eda14cbcSMatt Macy * This assert is valid because dmu_sync() expects to be called by 1751*eda14cbcSMatt Macy * a zilog's get_data while holding a range lock. This call only 1752*eda14cbcSMatt Macy * comes from dbuf_dirty() callers who must also hold a range lock. 1753*eda14cbcSMatt Macy */ 1754*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC); 1755*eda14cbcSMatt Macy ASSERT(db->db_level == 0); 1756*eda14cbcSMatt Macy 1757*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID || 1758*eda14cbcSMatt Macy dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN) 1759*eda14cbcSMatt Macy return; 1760*eda14cbcSMatt Macy 1761*eda14cbcSMatt Macy ASSERT(db->db_data_pending != dr); 1762*eda14cbcSMatt Macy 1763*eda14cbcSMatt Macy /* free this block */ 1764*eda14cbcSMatt Macy if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) 1765*eda14cbcSMatt Macy zio_free(db->db_objset->os_spa, txg, bp); 1766*eda14cbcSMatt Macy 1767*eda14cbcSMatt Macy dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1768*eda14cbcSMatt Macy dr->dt.dl.dr_nopwrite = B_FALSE; 1769*eda14cbcSMatt Macy dr->dt.dl.dr_has_raw_params = B_FALSE; 1770*eda14cbcSMatt Macy 1771*eda14cbcSMatt Macy /* 1772*eda14cbcSMatt Macy * Release the already-written buffer, so we leave it in 1773*eda14cbcSMatt Macy * a consistent dirty state. Note that all callers are 1774*eda14cbcSMatt Macy * modifying the buffer, so they will immediately do 1775*eda14cbcSMatt Macy * another (redundant) arc_release(). Therefore, leave 1776*eda14cbcSMatt Macy * the buf thawed to save the effort of freezing & 1777*eda14cbcSMatt Macy * immediately re-thawing it. 1778*eda14cbcSMatt Macy */ 1779*eda14cbcSMatt Macy arc_release(dr->dt.dl.dr_data, db); 1780*eda14cbcSMatt Macy } 1781*eda14cbcSMatt Macy 1782*eda14cbcSMatt Macy /* 1783*eda14cbcSMatt Macy * Evict (if its unreferenced) or clear (if its referenced) any level-0 1784*eda14cbcSMatt Macy * data blocks in the free range, so that any future readers will find 1785*eda14cbcSMatt Macy * empty blocks. 1786*eda14cbcSMatt Macy */ 1787*eda14cbcSMatt Macy void 1788*eda14cbcSMatt Macy dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid, 1789*eda14cbcSMatt Macy dmu_tx_t *tx) 1790*eda14cbcSMatt Macy { 1791*eda14cbcSMatt Macy dmu_buf_impl_t *db_search; 1792*eda14cbcSMatt Macy dmu_buf_impl_t *db, *db_next; 1793*eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 1794*eda14cbcSMatt Macy avl_index_t where; 1795*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 1796*eda14cbcSMatt Macy 1797*eda14cbcSMatt Macy if (end_blkid > dn->dn_maxblkid && 1798*eda14cbcSMatt Macy !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID)) 1799*eda14cbcSMatt Macy end_blkid = dn->dn_maxblkid; 1800*eda14cbcSMatt Macy dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid); 1801*eda14cbcSMatt Macy 1802*eda14cbcSMatt Macy db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP); 1803*eda14cbcSMatt Macy db_search->db_level = 0; 1804*eda14cbcSMatt Macy db_search->db_blkid = start_blkid; 1805*eda14cbcSMatt Macy db_search->db_state = DB_SEARCH; 1806*eda14cbcSMatt Macy 1807*eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 1808*eda14cbcSMatt Macy db = avl_find(&dn->dn_dbufs, db_search, &where); 1809*eda14cbcSMatt Macy ASSERT3P(db, ==, NULL); 1810*eda14cbcSMatt Macy 1811*eda14cbcSMatt Macy db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 1812*eda14cbcSMatt Macy 1813*eda14cbcSMatt Macy for (; db != NULL; db = db_next) { 1814*eda14cbcSMatt Macy db_next = AVL_NEXT(&dn->dn_dbufs, db); 1815*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1816*eda14cbcSMatt Macy 1817*eda14cbcSMatt Macy if (db->db_level != 0 || db->db_blkid > end_blkid) { 1818*eda14cbcSMatt Macy break; 1819*eda14cbcSMatt Macy } 1820*eda14cbcSMatt Macy ASSERT3U(db->db_blkid, >=, start_blkid); 1821*eda14cbcSMatt Macy 1822*eda14cbcSMatt Macy /* found a level 0 buffer in the range */ 1823*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1824*eda14cbcSMatt Macy if (dbuf_undirty(db, tx)) { 1825*eda14cbcSMatt Macy /* mutex has been dropped and dbuf destroyed */ 1826*eda14cbcSMatt Macy continue; 1827*eda14cbcSMatt Macy } 1828*eda14cbcSMatt Macy 1829*eda14cbcSMatt Macy if (db->db_state == DB_UNCACHED || 1830*eda14cbcSMatt Macy db->db_state == DB_NOFILL || 1831*eda14cbcSMatt Macy db->db_state == DB_EVICTING) { 1832*eda14cbcSMatt Macy ASSERT(db->db.db_data == NULL); 1833*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1834*eda14cbcSMatt Macy continue; 1835*eda14cbcSMatt Macy } 1836*eda14cbcSMatt Macy if (db->db_state == DB_READ || db->db_state == DB_FILL) { 1837*eda14cbcSMatt Macy /* will be handled in dbuf_read_done or dbuf_rele */ 1838*eda14cbcSMatt Macy db->db_freed_in_flight = TRUE; 1839*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1840*eda14cbcSMatt Macy continue; 1841*eda14cbcSMatt Macy } 1842*eda14cbcSMatt Macy if (zfs_refcount_count(&db->db_holds) == 0) { 1843*eda14cbcSMatt Macy ASSERT(db->db_buf); 1844*eda14cbcSMatt Macy dbuf_destroy(db); 1845*eda14cbcSMatt Macy continue; 1846*eda14cbcSMatt Macy } 1847*eda14cbcSMatt Macy /* The dbuf is referenced */ 1848*eda14cbcSMatt Macy 1849*eda14cbcSMatt Macy dr = list_head(&db->db_dirty_records); 1850*eda14cbcSMatt Macy if (dr != NULL) { 1851*eda14cbcSMatt Macy if (dr->dr_txg == txg) { 1852*eda14cbcSMatt Macy /* 1853*eda14cbcSMatt Macy * This buffer is "in-use", re-adjust the file 1854*eda14cbcSMatt Macy * size to reflect that this buffer may 1855*eda14cbcSMatt Macy * contain new data when we sync. 1856*eda14cbcSMatt Macy */ 1857*eda14cbcSMatt Macy if (db->db_blkid != DMU_SPILL_BLKID && 1858*eda14cbcSMatt Macy db->db_blkid > dn->dn_maxblkid) 1859*eda14cbcSMatt Macy dn->dn_maxblkid = db->db_blkid; 1860*eda14cbcSMatt Macy dbuf_unoverride(dr); 1861*eda14cbcSMatt Macy } else { 1862*eda14cbcSMatt Macy /* 1863*eda14cbcSMatt Macy * This dbuf is not dirty in the open context. 1864*eda14cbcSMatt Macy * Either uncache it (if its not referenced in 1865*eda14cbcSMatt Macy * the open context) or reset its contents to 1866*eda14cbcSMatt Macy * empty. 1867*eda14cbcSMatt Macy */ 1868*eda14cbcSMatt Macy dbuf_fix_old_data(db, txg); 1869*eda14cbcSMatt Macy } 1870*eda14cbcSMatt Macy } 1871*eda14cbcSMatt Macy /* clear the contents if its cached */ 1872*eda14cbcSMatt Macy if (db->db_state == DB_CACHED) { 1873*eda14cbcSMatt Macy ASSERT(db->db.db_data != NULL); 1874*eda14cbcSMatt Macy arc_release(db->db_buf, db); 1875*eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 1876*eda14cbcSMatt Macy bzero(db->db.db_data, db->db.db_size); 1877*eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 1878*eda14cbcSMatt Macy arc_buf_freeze(db->db_buf); 1879*eda14cbcSMatt Macy } 1880*eda14cbcSMatt Macy 1881*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1882*eda14cbcSMatt Macy } 1883*eda14cbcSMatt Macy 1884*eda14cbcSMatt Macy kmem_free(db_search, sizeof (dmu_buf_impl_t)); 1885*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 1886*eda14cbcSMatt Macy } 1887*eda14cbcSMatt Macy 1888*eda14cbcSMatt Macy void 1889*eda14cbcSMatt Macy dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx) 1890*eda14cbcSMatt Macy { 1891*eda14cbcSMatt Macy arc_buf_t *buf, *old_buf; 1892*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 1893*eda14cbcSMatt Macy int osize = db->db.db_size; 1894*eda14cbcSMatt Macy arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1895*eda14cbcSMatt Macy dnode_t *dn; 1896*eda14cbcSMatt Macy 1897*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1898*eda14cbcSMatt Macy 1899*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 1900*eda14cbcSMatt Macy dn = DB_DNODE(db); 1901*eda14cbcSMatt Macy 1902*eda14cbcSMatt Macy /* 1903*eda14cbcSMatt Macy * XXX we should be doing a dbuf_read, checking the return 1904*eda14cbcSMatt Macy * value and returning that up to our callers 1905*eda14cbcSMatt Macy */ 1906*eda14cbcSMatt Macy dmu_buf_will_dirty(&db->db, tx); 1907*eda14cbcSMatt Macy 1908*eda14cbcSMatt Macy /* create the data buffer for the new block */ 1909*eda14cbcSMatt Macy buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size); 1910*eda14cbcSMatt Macy 1911*eda14cbcSMatt Macy /* copy old block data to the new block */ 1912*eda14cbcSMatt Macy old_buf = db->db_buf; 1913*eda14cbcSMatt Macy bcopy(old_buf->b_data, buf->b_data, MIN(osize, size)); 1914*eda14cbcSMatt Macy /* zero the remainder */ 1915*eda14cbcSMatt Macy if (size > osize) 1916*eda14cbcSMatt Macy bzero((uint8_t *)buf->b_data + osize, size - osize); 1917*eda14cbcSMatt Macy 1918*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 1919*eda14cbcSMatt Macy dbuf_set_data(db, buf); 1920*eda14cbcSMatt Macy arc_buf_destroy(old_buf, db); 1921*eda14cbcSMatt Macy db->db.db_size = size; 1922*eda14cbcSMatt Macy 1923*eda14cbcSMatt Macy dr = list_head(&db->db_dirty_records); 1924*eda14cbcSMatt Macy /* dirty record added by dmu_buf_will_dirty() */ 1925*eda14cbcSMatt Macy VERIFY(dr != NULL); 1926*eda14cbcSMatt Macy if (db->db_level == 0) 1927*eda14cbcSMatt Macy dr->dt.dl.dr_data = buf; 1928*eda14cbcSMatt Macy ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 1929*eda14cbcSMatt Macy ASSERT3U(dr->dr_accounted, ==, osize); 1930*eda14cbcSMatt Macy dr->dr_accounted = size; 1931*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 1932*eda14cbcSMatt Macy 1933*eda14cbcSMatt Macy dmu_objset_willuse_space(dn->dn_objset, size - osize, tx); 1934*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 1935*eda14cbcSMatt Macy } 1936*eda14cbcSMatt Macy 1937*eda14cbcSMatt Macy void 1938*eda14cbcSMatt Macy dbuf_release_bp(dmu_buf_impl_t *db) 1939*eda14cbcSMatt Macy { 1940*eda14cbcSMatt Macy objset_t *os __maybe_unused = db->db_objset; 1941*eda14cbcSMatt Macy 1942*eda14cbcSMatt Macy ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 1943*eda14cbcSMatt Macy ASSERT(arc_released(os->os_phys_buf) || 1944*eda14cbcSMatt Macy list_link_active(&os->os_dsl_dataset->ds_synced_link)); 1945*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf)); 1946*eda14cbcSMatt Macy 1947*eda14cbcSMatt Macy (void) arc_release(db->db_buf, db); 1948*eda14cbcSMatt Macy } 1949*eda14cbcSMatt Macy 1950*eda14cbcSMatt Macy /* 1951*eda14cbcSMatt Macy * We already have a dirty record for this TXG, and we are being 1952*eda14cbcSMatt Macy * dirtied again. 1953*eda14cbcSMatt Macy */ 1954*eda14cbcSMatt Macy static void 1955*eda14cbcSMatt Macy dbuf_redirty(dbuf_dirty_record_t *dr) 1956*eda14cbcSMatt Macy { 1957*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 1958*eda14cbcSMatt Macy 1959*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 1960*eda14cbcSMatt Macy 1961*eda14cbcSMatt Macy if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) { 1962*eda14cbcSMatt Macy /* 1963*eda14cbcSMatt Macy * If this buffer has already been written out, 1964*eda14cbcSMatt Macy * we now need to reset its state. 1965*eda14cbcSMatt Macy */ 1966*eda14cbcSMatt Macy dbuf_unoverride(dr); 1967*eda14cbcSMatt Macy if (db->db.db_object != DMU_META_DNODE_OBJECT && 1968*eda14cbcSMatt Macy db->db_state != DB_NOFILL) { 1969*eda14cbcSMatt Macy /* Already released on initial dirty, so just thaw. */ 1970*eda14cbcSMatt Macy ASSERT(arc_released(db->db_buf)); 1971*eda14cbcSMatt Macy arc_buf_thaw(db->db_buf); 1972*eda14cbcSMatt Macy } 1973*eda14cbcSMatt Macy } 1974*eda14cbcSMatt Macy } 1975*eda14cbcSMatt Macy 1976*eda14cbcSMatt Macy dbuf_dirty_record_t * 1977*eda14cbcSMatt Macy dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1978*eda14cbcSMatt Macy { 1979*eda14cbcSMatt Macy dnode_t *dn; 1980*eda14cbcSMatt Macy objset_t *os; 1981*eda14cbcSMatt Macy dbuf_dirty_record_t *dr, *dr_next, *dr_head; 1982*eda14cbcSMatt Macy int txgoff = tx->tx_txg & TXG_MASK; 1983*eda14cbcSMatt Macy boolean_t drop_struct_rwlock = B_FALSE; 1984*eda14cbcSMatt Macy 1985*eda14cbcSMatt Macy ASSERT(tx->tx_txg != 0); 1986*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1987*eda14cbcSMatt Macy DMU_TX_DIRTY_BUF(tx, db); 1988*eda14cbcSMatt Macy 1989*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 1990*eda14cbcSMatt Macy dn = DB_DNODE(db); 1991*eda14cbcSMatt Macy /* 1992*eda14cbcSMatt Macy * Shouldn't dirty a regular buffer in syncing context. Private 1993*eda14cbcSMatt Macy * objects may be dirtied in syncing context, but only if they 1994*eda14cbcSMatt Macy * were already pre-dirtied in open context. 1995*eda14cbcSMatt Macy */ 1996*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 1997*eda14cbcSMatt Macy if (dn->dn_objset->os_dsl_dataset != NULL) { 1998*eda14cbcSMatt Macy rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, 1999*eda14cbcSMatt Macy RW_READER, FTAG); 2000*eda14cbcSMatt Macy } 2001*eda14cbcSMatt Macy ASSERT(!dmu_tx_is_syncing(tx) || 2002*eda14cbcSMatt Macy BP_IS_HOLE(dn->dn_objset->os_rootbp) || 2003*eda14cbcSMatt Macy DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 2004*eda14cbcSMatt Macy dn->dn_objset->os_dsl_dataset == NULL); 2005*eda14cbcSMatt Macy if (dn->dn_objset->os_dsl_dataset != NULL) 2006*eda14cbcSMatt Macy rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG); 2007*eda14cbcSMatt Macy #endif 2008*eda14cbcSMatt Macy /* 2009*eda14cbcSMatt Macy * We make this assert for private objects as well, but after we 2010*eda14cbcSMatt Macy * check if we're already dirty. They are allowed to re-dirty 2011*eda14cbcSMatt Macy * in syncing context. 2012*eda14cbcSMatt Macy */ 2013*eda14cbcSMatt Macy ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 2014*eda14cbcSMatt Macy dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 2015*eda14cbcSMatt Macy (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 2016*eda14cbcSMatt Macy 2017*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2018*eda14cbcSMatt Macy /* 2019*eda14cbcSMatt Macy * XXX make this true for indirects too? The problem is that 2020*eda14cbcSMatt Macy * transactions created with dmu_tx_create_assigned() from 2021*eda14cbcSMatt Macy * syncing context don't bother holding ahead. 2022*eda14cbcSMatt Macy */ 2023*eda14cbcSMatt Macy ASSERT(db->db_level != 0 || 2024*eda14cbcSMatt Macy db->db_state == DB_CACHED || db->db_state == DB_FILL || 2025*eda14cbcSMatt Macy db->db_state == DB_NOFILL); 2026*eda14cbcSMatt Macy 2027*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2028*eda14cbcSMatt Macy dnode_set_dirtyctx(dn, tx, db); 2029*eda14cbcSMatt Macy if (tx->tx_txg > dn->dn_dirty_txg) 2030*eda14cbcSMatt Macy dn->dn_dirty_txg = tx->tx_txg; 2031*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2032*eda14cbcSMatt Macy 2033*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) 2034*eda14cbcSMatt Macy dn->dn_have_spill = B_TRUE; 2035*eda14cbcSMatt Macy 2036*eda14cbcSMatt Macy /* 2037*eda14cbcSMatt Macy * If this buffer is already dirty, we're done. 2038*eda14cbcSMatt Macy */ 2039*eda14cbcSMatt Macy dr_head = list_head(&db->db_dirty_records); 2040*eda14cbcSMatt Macy ASSERT(dr_head == NULL || dr_head->dr_txg <= tx->tx_txg || 2041*eda14cbcSMatt Macy db->db.db_object == DMU_META_DNODE_OBJECT); 2042*eda14cbcSMatt Macy dr_next = dbuf_find_dirty_lte(db, tx->tx_txg); 2043*eda14cbcSMatt Macy if (dr_next && dr_next->dr_txg == tx->tx_txg) { 2044*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2045*eda14cbcSMatt Macy 2046*eda14cbcSMatt Macy dbuf_redirty(dr_next); 2047*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2048*eda14cbcSMatt Macy return (dr_next); 2049*eda14cbcSMatt Macy } 2050*eda14cbcSMatt Macy 2051*eda14cbcSMatt Macy /* 2052*eda14cbcSMatt Macy * Only valid if not already dirty. 2053*eda14cbcSMatt Macy */ 2054*eda14cbcSMatt Macy ASSERT(dn->dn_object == 0 || 2055*eda14cbcSMatt Macy dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 2056*eda14cbcSMatt Macy (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 2057*eda14cbcSMatt Macy 2058*eda14cbcSMatt Macy ASSERT3U(dn->dn_nlevels, >, db->db_level); 2059*eda14cbcSMatt Macy 2060*eda14cbcSMatt Macy /* 2061*eda14cbcSMatt Macy * We should only be dirtying in syncing context if it's the 2062*eda14cbcSMatt Macy * mos or we're initializing the os or it's a special object. 2063*eda14cbcSMatt Macy * However, we are allowed to dirty in syncing context provided 2064*eda14cbcSMatt Macy * we already dirtied it in open context. Hence we must make 2065*eda14cbcSMatt Macy * this assertion only if we're not already dirty. 2066*eda14cbcSMatt Macy */ 2067*eda14cbcSMatt Macy os = dn->dn_objset; 2068*eda14cbcSMatt Macy VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa)); 2069*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 2070*eda14cbcSMatt Macy if (dn->dn_objset->os_dsl_dataset != NULL) 2071*eda14cbcSMatt Macy rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG); 2072*eda14cbcSMatt Macy ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 2073*eda14cbcSMatt Macy os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp)); 2074*eda14cbcSMatt Macy if (dn->dn_objset->os_dsl_dataset != NULL) 2075*eda14cbcSMatt Macy rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 2076*eda14cbcSMatt Macy #endif 2077*eda14cbcSMatt Macy ASSERT(db->db.db_size != 0); 2078*eda14cbcSMatt Macy 2079*eda14cbcSMatt Macy dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 2080*eda14cbcSMatt Macy 2081*eda14cbcSMatt Macy if (db->db_blkid != DMU_BONUS_BLKID) { 2082*eda14cbcSMatt Macy dmu_objset_willuse_space(os, db->db.db_size, tx); 2083*eda14cbcSMatt Macy } 2084*eda14cbcSMatt Macy 2085*eda14cbcSMatt Macy /* 2086*eda14cbcSMatt Macy * If this buffer is dirty in an old transaction group we need 2087*eda14cbcSMatt Macy * to make a copy of it so that the changes we make in this 2088*eda14cbcSMatt Macy * transaction group won't leak out when we sync the older txg. 2089*eda14cbcSMatt Macy */ 2090*eda14cbcSMatt Macy dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP); 2091*eda14cbcSMatt Macy list_link_init(&dr->dr_dirty_node); 2092*eda14cbcSMatt Macy list_link_init(&dr->dr_dbuf_node); 2093*eda14cbcSMatt Macy if (db->db_level == 0) { 2094*eda14cbcSMatt Macy void *data_old = db->db_buf; 2095*eda14cbcSMatt Macy 2096*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL) { 2097*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 2098*eda14cbcSMatt Macy dbuf_fix_old_data(db, tx->tx_txg); 2099*eda14cbcSMatt Macy data_old = db->db.db_data; 2100*eda14cbcSMatt Macy } else if (db->db.db_object != DMU_META_DNODE_OBJECT) { 2101*eda14cbcSMatt Macy /* 2102*eda14cbcSMatt Macy * Release the data buffer from the cache so 2103*eda14cbcSMatt Macy * that we can modify it without impacting 2104*eda14cbcSMatt Macy * possible other users of this cached data 2105*eda14cbcSMatt Macy * block. Note that indirect blocks and 2106*eda14cbcSMatt Macy * private objects are not released until the 2107*eda14cbcSMatt Macy * syncing state (since they are only modified 2108*eda14cbcSMatt Macy * then). 2109*eda14cbcSMatt Macy */ 2110*eda14cbcSMatt Macy arc_release(db->db_buf, db); 2111*eda14cbcSMatt Macy dbuf_fix_old_data(db, tx->tx_txg); 2112*eda14cbcSMatt Macy data_old = db->db_buf; 2113*eda14cbcSMatt Macy } 2114*eda14cbcSMatt Macy ASSERT(data_old != NULL); 2115*eda14cbcSMatt Macy } 2116*eda14cbcSMatt Macy dr->dt.dl.dr_data = data_old; 2117*eda14cbcSMatt Macy } else { 2118*eda14cbcSMatt Macy mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL); 2119*eda14cbcSMatt Macy list_create(&dr->dt.di.dr_children, 2120*eda14cbcSMatt Macy sizeof (dbuf_dirty_record_t), 2121*eda14cbcSMatt Macy offsetof(dbuf_dirty_record_t, dr_dirty_node)); 2122*eda14cbcSMatt Macy } 2123*eda14cbcSMatt Macy if (db->db_blkid != DMU_BONUS_BLKID) 2124*eda14cbcSMatt Macy dr->dr_accounted = db->db.db_size; 2125*eda14cbcSMatt Macy dr->dr_dbuf = db; 2126*eda14cbcSMatt Macy dr->dr_txg = tx->tx_txg; 2127*eda14cbcSMatt Macy list_insert_before(&db->db_dirty_records, dr_next, dr); 2128*eda14cbcSMatt Macy 2129*eda14cbcSMatt Macy /* 2130*eda14cbcSMatt Macy * We could have been freed_in_flight between the dbuf_noread 2131*eda14cbcSMatt Macy * and dbuf_dirty. We win, as though the dbuf_noread() had 2132*eda14cbcSMatt Macy * happened after the free. 2133*eda14cbcSMatt Macy */ 2134*eda14cbcSMatt Macy if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 2135*eda14cbcSMatt Macy db->db_blkid != DMU_SPILL_BLKID) { 2136*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2137*eda14cbcSMatt Macy if (dn->dn_free_ranges[txgoff] != NULL) { 2138*eda14cbcSMatt Macy range_tree_clear(dn->dn_free_ranges[txgoff], 2139*eda14cbcSMatt Macy db->db_blkid, 1); 2140*eda14cbcSMatt Macy } 2141*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2142*eda14cbcSMatt Macy db->db_freed_in_flight = FALSE; 2143*eda14cbcSMatt Macy } 2144*eda14cbcSMatt Macy 2145*eda14cbcSMatt Macy /* 2146*eda14cbcSMatt Macy * This buffer is now part of this txg 2147*eda14cbcSMatt Macy */ 2148*eda14cbcSMatt Macy dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg); 2149*eda14cbcSMatt Macy db->db_dirtycnt += 1; 2150*eda14cbcSMatt Macy ASSERT3U(db->db_dirtycnt, <=, 3); 2151*eda14cbcSMatt Macy 2152*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2153*eda14cbcSMatt Macy 2154*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID || 2155*eda14cbcSMatt Macy db->db_blkid == DMU_SPILL_BLKID) { 2156*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2157*eda14cbcSMatt Macy ASSERT(!list_link_active(&dr->dr_dirty_node)); 2158*eda14cbcSMatt Macy list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 2159*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2160*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 2161*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2162*eda14cbcSMatt Macy return (dr); 2163*eda14cbcSMatt Macy } 2164*eda14cbcSMatt Macy 2165*eda14cbcSMatt Macy if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 2166*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 2167*eda14cbcSMatt Macy drop_struct_rwlock = B_TRUE; 2168*eda14cbcSMatt Macy } 2169*eda14cbcSMatt Macy 2170*eda14cbcSMatt Macy /* 2171*eda14cbcSMatt Macy * If we are overwriting a dedup BP, then unless it is snapshotted, 2172*eda14cbcSMatt Macy * when we get to syncing context we will need to decrement its 2173*eda14cbcSMatt Macy * refcount in the DDT. Prefetch the relevant DDT block so that 2174*eda14cbcSMatt Macy * syncing context won't have to wait for the i/o. 2175*eda14cbcSMatt Macy */ 2176*eda14cbcSMatt Macy if (db->db_blkptr != NULL) { 2177*eda14cbcSMatt Macy db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG); 2178*eda14cbcSMatt Macy ddt_prefetch(os->os_spa, db->db_blkptr); 2179*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, FTAG); 2180*eda14cbcSMatt Macy } 2181*eda14cbcSMatt Macy 2182*eda14cbcSMatt Macy /* 2183*eda14cbcSMatt Macy * We need to hold the dn_struct_rwlock to make this assertion, 2184*eda14cbcSMatt Macy * because it protects dn_phys / dn_next_nlevels from changing. 2185*eda14cbcSMatt Macy */ 2186*eda14cbcSMatt Macy ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) || 2187*eda14cbcSMatt Macy dn->dn_phys->dn_nlevels > db->db_level || 2188*eda14cbcSMatt Macy dn->dn_next_nlevels[txgoff] > db->db_level || 2189*eda14cbcSMatt Macy dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level || 2190*eda14cbcSMatt Macy dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level); 2191*eda14cbcSMatt Macy 2192*eda14cbcSMatt Macy 2193*eda14cbcSMatt Macy if (db->db_level == 0) { 2194*eda14cbcSMatt Macy ASSERT(!db->db_objset->os_raw_receive || 2195*eda14cbcSMatt Macy dn->dn_maxblkid >= db->db_blkid); 2196*eda14cbcSMatt Macy dnode_new_blkid(dn, db->db_blkid, tx, 2197*eda14cbcSMatt Macy drop_struct_rwlock, B_FALSE); 2198*eda14cbcSMatt Macy ASSERT(dn->dn_maxblkid >= db->db_blkid); 2199*eda14cbcSMatt Macy } 2200*eda14cbcSMatt Macy 2201*eda14cbcSMatt Macy if (db->db_level+1 < dn->dn_nlevels) { 2202*eda14cbcSMatt Macy dmu_buf_impl_t *parent = db->db_parent; 2203*eda14cbcSMatt Macy dbuf_dirty_record_t *di; 2204*eda14cbcSMatt Macy int parent_held = FALSE; 2205*eda14cbcSMatt Macy 2206*eda14cbcSMatt Macy if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) { 2207*eda14cbcSMatt Macy int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2208*eda14cbcSMatt Macy parent = dbuf_hold_level(dn, db->db_level + 1, 2209*eda14cbcSMatt Macy db->db_blkid >> epbs, FTAG); 2210*eda14cbcSMatt Macy ASSERT(parent != NULL); 2211*eda14cbcSMatt Macy parent_held = TRUE; 2212*eda14cbcSMatt Macy } 2213*eda14cbcSMatt Macy if (drop_struct_rwlock) 2214*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2215*eda14cbcSMatt Macy ASSERT3U(db->db_level + 1, ==, parent->db_level); 2216*eda14cbcSMatt Macy di = dbuf_dirty(parent, tx); 2217*eda14cbcSMatt Macy if (parent_held) 2218*eda14cbcSMatt Macy dbuf_rele(parent, FTAG); 2219*eda14cbcSMatt Macy 2220*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2221*eda14cbcSMatt Macy /* 2222*eda14cbcSMatt Macy * Since we've dropped the mutex, it's possible that 2223*eda14cbcSMatt Macy * dbuf_undirty() might have changed this out from under us. 2224*eda14cbcSMatt Macy */ 2225*eda14cbcSMatt Macy if (list_head(&db->db_dirty_records) == dr || 2226*eda14cbcSMatt Macy dn->dn_object == DMU_META_DNODE_OBJECT) { 2227*eda14cbcSMatt Macy mutex_enter(&di->dt.di.dr_mtx); 2228*eda14cbcSMatt Macy ASSERT3U(di->dr_txg, ==, tx->tx_txg); 2229*eda14cbcSMatt Macy ASSERT(!list_link_active(&dr->dr_dirty_node)); 2230*eda14cbcSMatt Macy list_insert_tail(&di->dt.di.dr_children, dr); 2231*eda14cbcSMatt Macy mutex_exit(&di->dt.di.dr_mtx); 2232*eda14cbcSMatt Macy dr->dr_parent = di; 2233*eda14cbcSMatt Macy } 2234*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2235*eda14cbcSMatt Macy } else { 2236*eda14cbcSMatt Macy ASSERT(db->db_level + 1 == dn->dn_nlevels); 2237*eda14cbcSMatt Macy ASSERT(db->db_blkid < dn->dn_nblkptr); 2238*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf); 2239*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2240*eda14cbcSMatt Macy ASSERT(!list_link_active(&dr->dr_dirty_node)); 2241*eda14cbcSMatt Macy list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 2242*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2243*eda14cbcSMatt Macy if (drop_struct_rwlock) 2244*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 2245*eda14cbcSMatt Macy } 2246*eda14cbcSMatt Macy 2247*eda14cbcSMatt Macy dnode_setdirty(dn, tx); 2248*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2249*eda14cbcSMatt Macy return (dr); 2250*eda14cbcSMatt Macy } 2251*eda14cbcSMatt Macy 2252*eda14cbcSMatt Macy static void 2253*eda14cbcSMatt Macy dbuf_undirty_bonus(dbuf_dirty_record_t *dr) 2254*eda14cbcSMatt Macy { 2255*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 2256*eda14cbcSMatt Macy 2257*eda14cbcSMatt Macy if (dr->dt.dl.dr_data != db->db.db_data) { 2258*eda14cbcSMatt Macy struct dnode *dn = DB_DNODE(db); 2259*eda14cbcSMatt Macy int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 2260*eda14cbcSMatt Macy 2261*eda14cbcSMatt Macy kmem_free(dr->dt.dl.dr_data, max_bonuslen); 2262*eda14cbcSMatt Macy arc_space_return(max_bonuslen, ARC_SPACE_BONUS); 2263*eda14cbcSMatt Macy } 2264*eda14cbcSMatt Macy db->db_data_pending = NULL; 2265*eda14cbcSMatt Macy ASSERT(list_next(&db->db_dirty_records, dr) == NULL); 2266*eda14cbcSMatt Macy list_remove(&db->db_dirty_records, dr); 2267*eda14cbcSMatt Macy if (dr->dr_dbuf->db_level != 0) { 2268*eda14cbcSMatt Macy mutex_destroy(&dr->dt.di.dr_mtx); 2269*eda14cbcSMatt Macy list_destroy(&dr->dt.di.dr_children); 2270*eda14cbcSMatt Macy } 2271*eda14cbcSMatt Macy kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2272*eda14cbcSMatt Macy ASSERT3U(db->db_dirtycnt, >, 0); 2273*eda14cbcSMatt Macy db->db_dirtycnt -= 1; 2274*eda14cbcSMatt Macy } 2275*eda14cbcSMatt Macy 2276*eda14cbcSMatt Macy /* 2277*eda14cbcSMatt Macy * Undirty a buffer in the transaction group referenced by the given 2278*eda14cbcSMatt Macy * transaction. Return whether this evicted the dbuf. 2279*eda14cbcSMatt Macy */ 2280*eda14cbcSMatt Macy static boolean_t 2281*eda14cbcSMatt Macy dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 2282*eda14cbcSMatt Macy { 2283*eda14cbcSMatt Macy dnode_t *dn; 2284*eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 2285*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 2286*eda14cbcSMatt Macy 2287*eda14cbcSMatt Macy ASSERT(txg != 0); 2288*eda14cbcSMatt Macy 2289*eda14cbcSMatt Macy /* 2290*eda14cbcSMatt Macy * Due to our use of dn_nlevels below, this can only be called 2291*eda14cbcSMatt Macy * in open context, unless we are operating on the MOS. 2292*eda14cbcSMatt Macy * From syncing context, dn_nlevels may be different from the 2293*eda14cbcSMatt Macy * dn_nlevels used when dbuf was dirtied. 2294*eda14cbcSMatt Macy */ 2295*eda14cbcSMatt Macy ASSERT(db->db_objset == 2296*eda14cbcSMatt Macy dmu_objset_pool(db->db_objset)->dp_meta_objset || 2297*eda14cbcSMatt Macy txg != spa_syncing_txg(dmu_objset_spa(db->db_objset))); 2298*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2299*eda14cbcSMatt Macy ASSERT0(db->db_level); 2300*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 2301*eda14cbcSMatt Macy 2302*eda14cbcSMatt Macy /* 2303*eda14cbcSMatt Macy * If this buffer is not dirty, we're done. 2304*eda14cbcSMatt Macy */ 2305*eda14cbcSMatt Macy dr = dbuf_find_dirty_eq(db, txg); 2306*eda14cbcSMatt Macy if (dr == NULL) 2307*eda14cbcSMatt Macy return (B_FALSE); 2308*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf == db); 2309*eda14cbcSMatt Macy 2310*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 2311*eda14cbcSMatt Macy dn = DB_DNODE(db); 2312*eda14cbcSMatt Macy 2313*eda14cbcSMatt Macy dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 2314*eda14cbcSMatt Macy 2315*eda14cbcSMatt Macy ASSERT(db->db.db_size != 0); 2316*eda14cbcSMatt Macy 2317*eda14cbcSMatt Macy dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset), 2318*eda14cbcSMatt Macy dr->dr_accounted, txg); 2319*eda14cbcSMatt Macy 2320*eda14cbcSMatt Macy list_remove(&db->db_dirty_records, dr); 2321*eda14cbcSMatt Macy 2322*eda14cbcSMatt Macy /* 2323*eda14cbcSMatt Macy * Note that there are three places in dbuf_dirty() 2324*eda14cbcSMatt Macy * where this dirty record may be put on a list. 2325*eda14cbcSMatt Macy * Make sure to do a list_remove corresponding to 2326*eda14cbcSMatt Macy * every one of those list_insert calls. 2327*eda14cbcSMatt Macy */ 2328*eda14cbcSMatt Macy if (dr->dr_parent) { 2329*eda14cbcSMatt Macy mutex_enter(&dr->dr_parent->dt.di.dr_mtx); 2330*eda14cbcSMatt Macy list_remove(&dr->dr_parent->dt.di.dr_children, dr); 2331*eda14cbcSMatt Macy mutex_exit(&dr->dr_parent->dt.di.dr_mtx); 2332*eda14cbcSMatt Macy } else if (db->db_blkid == DMU_SPILL_BLKID || 2333*eda14cbcSMatt Macy db->db_level + 1 == dn->dn_nlevels) { 2334*eda14cbcSMatt Macy ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf); 2335*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2336*eda14cbcSMatt Macy list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr); 2337*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2338*eda14cbcSMatt Macy } 2339*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2340*eda14cbcSMatt Macy 2341*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL) { 2342*eda14cbcSMatt Macy dbuf_unoverride(dr); 2343*eda14cbcSMatt Macy 2344*eda14cbcSMatt Macy ASSERT(db->db_buf != NULL); 2345*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_data != NULL); 2346*eda14cbcSMatt Macy if (dr->dt.dl.dr_data != db->db_buf) 2347*eda14cbcSMatt Macy arc_buf_destroy(dr->dt.dl.dr_data, db); 2348*eda14cbcSMatt Macy } 2349*eda14cbcSMatt Macy 2350*eda14cbcSMatt Macy kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2351*eda14cbcSMatt Macy 2352*eda14cbcSMatt Macy ASSERT(db->db_dirtycnt > 0); 2353*eda14cbcSMatt Macy db->db_dirtycnt -= 1; 2354*eda14cbcSMatt Macy 2355*eda14cbcSMatt Macy if (zfs_refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) { 2356*eda14cbcSMatt Macy ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf)); 2357*eda14cbcSMatt Macy dbuf_destroy(db); 2358*eda14cbcSMatt Macy return (B_TRUE); 2359*eda14cbcSMatt Macy } 2360*eda14cbcSMatt Macy 2361*eda14cbcSMatt Macy return (B_FALSE); 2362*eda14cbcSMatt Macy } 2363*eda14cbcSMatt Macy 2364*eda14cbcSMatt Macy static void 2365*eda14cbcSMatt Macy dmu_buf_will_dirty_impl(dmu_buf_t *db_fake, int flags, dmu_tx_t *tx) 2366*eda14cbcSMatt Macy { 2367*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2368*eda14cbcSMatt Macy 2369*eda14cbcSMatt Macy ASSERT(tx->tx_txg != 0); 2370*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2371*eda14cbcSMatt Macy 2372*eda14cbcSMatt Macy /* 2373*eda14cbcSMatt Macy * Quick check for dirtiness. For already dirty blocks, this 2374*eda14cbcSMatt Macy * reduces runtime of this function by >90%, and overall performance 2375*eda14cbcSMatt Macy * by 50% for some workloads (e.g. file deletion with indirect blocks 2376*eda14cbcSMatt Macy * cached). 2377*eda14cbcSMatt Macy */ 2378*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2379*eda14cbcSMatt Macy 2380*eda14cbcSMatt Macy if (db->db_state == DB_CACHED) { 2381*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2382*eda14cbcSMatt Macy /* 2383*eda14cbcSMatt Macy * It's possible that it is already dirty but not cached, 2384*eda14cbcSMatt Macy * because there are some calls to dbuf_dirty() that don't 2385*eda14cbcSMatt Macy * go through dmu_buf_will_dirty(). 2386*eda14cbcSMatt Macy */ 2387*eda14cbcSMatt Macy if (dr != NULL) { 2388*eda14cbcSMatt Macy /* This dbuf is already dirty and cached. */ 2389*eda14cbcSMatt Macy dbuf_redirty(dr); 2390*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2391*eda14cbcSMatt Macy return; 2392*eda14cbcSMatt Macy } 2393*eda14cbcSMatt Macy } 2394*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2395*eda14cbcSMatt Macy 2396*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 2397*eda14cbcSMatt Macy if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock)) 2398*eda14cbcSMatt Macy flags |= DB_RF_HAVESTRUCT; 2399*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2400*eda14cbcSMatt Macy (void) dbuf_read(db, NULL, flags); 2401*eda14cbcSMatt Macy (void) dbuf_dirty(db, tx); 2402*eda14cbcSMatt Macy } 2403*eda14cbcSMatt Macy 2404*eda14cbcSMatt Macy void 2405*eda14cbcSMatt Macy dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 2406*eda14cbcSMatt Macy { 2407*eda14cbcSMatt Macy dmu_buf_will_dirty_impl(db_fake, 2408*eda14cbcSMatt Macy DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH, tx); 2409*eda14cbcSMatt Macy } 2410*eda14cbcSMatt Macy 2411*eda14cbcSMatt Macy boolean_t 2412*eda14cbcSMatt Macy dmu_buf_is_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 2413*eda14cbcSMatt Macy { 2414*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2415*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 2416*eda14cbcSMatt Macy 2417*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2418*eda14cbcSMatt Macy dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2419*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2420*eda14cbcSMatt Macy return (dr != NULL); 2421*eda14cbcSMatt Macy } 2422*eda14cbcSMatt Macy 2423*eda14cbcSMatt Macy void 2424*eda14cbcSMatt Macy dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 2425*eda14cbcSMatt Macy { 2426*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2427*eda14cbcSMatt Macy 2428*eda14cbcSMatt Macy db->db_state = DB_NOFILL; 2429*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "allocating NOFILL buffer"); 2430*eda14cbcSMatt Macy dmu_buf_will_fill(db_fake, tx); 2431*eda14cbcSMatt Macy } 2432*eda14cbcSMatt Macy 2433*eda14cbcSMatt Macy void 2434*eda14cbcSMatt Macy dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 2435*eda14cbcSMatt Macy { 2436*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2437*eda14cbcSMatt Macy 2438*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2439*eda14cbcSMatt Macy ASSERT(tx->tx_txg != 0); 2440*eda14cbcSMatt Macy ASSERT(db->db_level == 0); 2441*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2442*eda14cbcSMatt Macy 2443*eda14cbcSMatt Macy ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT || 2444*eda14cbcSMatt Macy dmu_tx_private_ok(tx)); 2445*eda14cbcSMatt Macy 2446*eda14cbcSMatt Macy dbuf_noread(db); 2447*eda14cbcSMatt Macy (void) dbuf_dirty(db, tx); 2448*eda14cbcSMatt Macy } 2449*eda14cbcSMatt Macy 2450*eda14cbcSMatt Macy /* 2451*eda14cbcSMatt Macy * This function is effectively the same as dmu_buf_will_dirty(), but 2452*eda14cbcSMatt Macy * indicates the caller expects raw encrypted data in the db, and provides 2453*eda14cbcSMatt Macy * the crypt params (byteorder, salt, iv, mac) which should be stored in the 2454*eda14cbcSMatt Macy * blkptr_t when this dbuf is written. This is only used for blocks of 2455*eda14cbcSMatt Macy * dnodes, during raw receive. 2456*eda14cbcSMatt Macy */ 2457*eda14cbcSMatt Macy void 2458*eda14cbcSMatt Macy dmu_buf_set_crypt_params(dmu_buf_t *db_fake, boolean_t byteorder, 2459*eda14cbcSMatt Macy const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, dmu_tx_t *tx) 2460*eda14cbcSMatt Macy { 2461*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2462*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 2463*eda14cbcSMatt Macy 2464*eda14cbcSMatt Macy /* 2465*eda14cbcSMatt Macy * dr_has_raw_params is only processed for blocks of dnodes 2466*eda14cbcSMatt Macy * (see dbuf_sync_dnode_leaf_crypt()). 2467*eda14cbcSMatt Macy */ 2468*eda14cbcSMatt Macy ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT); 2469*eda14cbcSMatt Macy ASSERT3U(db->db_level, ==, 0); 2470*eda14cbcSMatt Macy ASSERT(db->db_objset->os_raw_receive); 2471*eda14cbcSMatt Macy 2472*eda14cbcSMatt Macy dmu_buf_will_dirty_impl(db_fake, 2473*eda14cbcSMatt Macy DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_NO_DECRYPT, tx); 2474*eda14cbcSMatt Macy 2475*eda14cbcSMatt Macy dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2476*eda14cbcSMatt Macy 2477*eda14cbcSMatt Macy ASSERT3P(dr, !=, NULL); 2478*eda14cbcSMatt Macy 2479*eda14cbcSMatt Macy dr->dt.dl.dr_has_raw_params = B_TRUE; 2480*eda14cbcSMatt Macy dr->dt.dl.dr_byteorder = byteorder; 2481*eda14cbcSMatt Macy bcopy(salt, dr->dt.dl.dr_salt, ZIO_DATA_SALT_LEN); 2482*eda14cbcSMatt Macy bcopy(iv, dr->dt.dl.dr_iv, ZIO_DATA_IV_LEN); 2483*eda14cbcSMatt Macy bcopy(mac, dr->dt.dl.dr_mac, ZIO_DATA_MAC_LEN); 2484*eda14cbcSMatt Macy } 2485*eda14cbcSMatt Macy 2486*eda14cbcSMatt Macy static void 2487*eda14cbcSMatt Macy dbuf_override_impl(dmu_buf_impl_t *db, const blkptr_t *bp, dmu_tx_t *tx) 2488*eda14cbcSMatt Macy { 2489*eda14cbcSMatt Macy struct dirty_leaf *dl; 2490*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 2491*eda14cbcSMatt Macy 2492*eda14cbcSMatt Macy dr = list_head(&db->db_dirty_records); 2493*eda14cbcSMatt Macy ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2494*eda14cbcSMatt Macy dl = &dr->dt.dl; 2495*eda14cbcSMatt Macy dl->dr_overridden_by = *bp; 2496*eda14cbcSMatt Macy dl->dr_override_state = DR_OVERRIDDEN; 2497*eda14cbcSMatt Macy dl->dr_overridden_by.blk_birth = dr->dr_txg; 2498*eda14cbcSMatt Macy } 2499*eda14cbcSMatt Macy 2500*eda14cbcSMatt Macy /* ARGSUSED */ 2501*eda14cbcSMatt Macy void 2502*eda14cbcSMatt Macy dmu_buf_fill_done(dmu_buf_t *dbuf, dmu_tx_t *tx) 2503*eda14cbcSMatt Macy { 2504*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2505*eda14cbcSMatt Macy dbuf_states_t old_state; 2506*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2507*eda14cbcSMatt Macy DBUF_VERIFY(db); 2508*eda14cbcSMatt Macy 2509*eda14cbcSMatt Macy old_state = db->db_state; 2510*eda14cbcSMatt Macy db->db_state = DB_CACHED; 2511*eda14cbcSMatt Macy if (old_state == DB_FILL) { 2512*eda14cbcSMatt Macy if (db->db_level == 0 && db->db_freed_in_flight) { 2513*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2514*eda14cbcSMatt Macy /* we were freed while filling */ 2515*eda14cbcSMatt Macy /* XXX dbuf_undirty? */ 2516*eda14cbcSMatt Macy bzero(db->db.db_data, db->db.db_size); 2517*eda14cbcSMatt Macy db->db_freed_in_flight = FALSE; 2518*eda14cbcSMatt Macy DTRACE_SET_STATE(db, 2519*eda14cbcSMatt Macy "fill done handling freed in flight"); 2520*eda14cbcSMatt Macy } else { 2521*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "fill done"); 2522*eda14cbcSMatt Macy } 2523*eda14cbcSMatt Macy cv_broadcast(&db->db_changed); 2524*eda14cbcSMatt Macy } 2525*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2526*eda14cbcSMatt Macy } 2527*eda14cbcSMatt Macy 2528*eda14cbcSMatt Macy void 2529*eda14cbcSMatt Macy dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data, 2530*eda14cbcSMatt Macy bp_embedded_type_t etype, enum zio_compress comp, 2531*eda14cbcSMatt Macy int uncompressed_size, int compressed_size, int byteorder, 2532*eda14cbcSMatt Macy dmu_tx_t *tx) 2533*eda14cbcSMatt Macy { 2534*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2535*eda14cbcSMatt Macy struct dirty_leaf *dl; 2536*eda14cbcSMatt Macy dmu_object_type_t type; 2537*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 2538*eda14cbcSMatt Macy 2539*eda14cbcSMatt Macy if (etype == BP_EMBEDDED_TYPE_DATA) { 2540*eda14cbcSMatt Macy ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset), 2541*eda14cbcSMatt Macy SPA_FEATURE_EMBEDDED_DATA)); 2542*eda14cbcSMatt Macy } 2543*eda14cbcSMatt Macy 2544*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 2545*eda14cbcSMatt Macy type = DB_DNODE(db)->dn_type; 2546*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2547*eda14cbcSMatt Macy 2548*eda14cbcSMatt Macy ASSERT0(db->db_level); 2549*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2550*eda14cbcSMatt Macy 2551*eda14cbcSMatt Macy dmu_buf_will_not_fill(dbuf, tx); 2552*eda14cbcSMatt Macy 2553*eda14cbcSMatt Macy dr = list_head(&db->db_dirty_records); 2554*eda14cbcSMatt Macy ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2555*eda14cbcSMatt Macy dl = &dr->dt.dl; 2556*eda14cbcSMatt Macy encode_embedded_bp_compressed(&dl->dr_overridden_by, 2557*eda14cbcSMatt Macy data, comp, uncompressed_size, compressed_size); 2558*eda14cbcSMatt Macy BPE_SET_ETYPE(&dl->dr_overridden_by, etype); 2559*eda14cbcSMatt Macy BP_SET_TYPE(&dl->dr_overridden_by, type); 2560*eda14cbcSMatt Macy BP_SET_LEVEL(&dl->dr_overridden_by, 0); 2561*eda14cbcSMatt Macy BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder); 2562*eda14cbcSMatt Macy 2563*eda14cbcSMatt Macy dl->dr_override_state = DR_OVERRIDDEN; 2564*eda14cbcSMatt Macy dl->dr_overridden_by.blk_birth = dr->dr_txg; 2565*eda14cbcSMatt Macy } 2566*eda14cbcSMatt Macy 2567*eda14cbcSMatt Macy void 2568*eda14cbcSMatt Macy dmu_buf_redact(dmu_buf_t *dbuf, dmu_tx_t *tx) 2569*eda14cbcSMatt Macy { 2570*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2571*eda14cbcSMatt Macy dmu_object_type_t type; 2572*eda14cbcSMatt Macy ASSERT(dsl_dataset_feature_is_active(db->db_objset->os_dsl_dataset, 2573*eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)); 2574*eda14cbcSMatt Macy 2575*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 2576*eda14cbcSMatt Macy type = DB_DNODE(db)->dn_type; 2577*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2578*eda14cbcSMatt Macy 2579*eda14cbcSMatt Macy ASSERT0(db->db_level); 2580*eda14cbcSMatt Macy dmu_buf_will_not_fill(dbuf, tx); 2581*eda14cbcSMatt Macy 2582*eda14cbcSMatt Macy blkptr_t bp = { { { {0} } } }; 2583*eda14cbcSMatt Macy BP_SET_TYPE(&bp, type); 2584*eda14cbcSMatt Macy BP_SET_LEVEL(&bp, 0); 2585*eda14cbcSMatt Macy BP_SET_BIRTH(&bp, tx->tx_txg, 0); 2586*eda14cbcSMatt Macy BP_SET_REDACTED(&bp); 2587*eda14cbcSMatt Macy BPE_SET_LSIZE(&bp, dbuf->db_size); 2588*eda14cbcSMatt Macy 2589*eda14cbcSMatt Macy dbuf_override_impl(db, &bp, tx); 2590*eda14cbcSMatt Macy } 2591*eda14cbcSMatt Macy 2592*eda14cbcSMatt Macy /* 2593*eda14cbcSMatt Macy * Directly assign a provided arc buf to a given dbuf if it's not referenced 2594*eda14cbcSMatt Macy * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf. 2595*eda14cbcSMatt Macy */ 2596*eda14cbcSMatt Macy void 2597*eda14cbcSMatt Macy dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx) 2598*eda14cbcSMatt Macy { 2599*eda14cbcSMatt Macy ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2600*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2601*eda14cbcSMatt Macy ASSERT(db->db_level == 0); 2602*eda14cbcSMatt Macy ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf)); 2603*eda14cbcSMatt Macy ASSERT(buf != NULL); 2604*eda14cbcSMatt Macy ASSERT3U(arc_buf_lsize(buf), ==, db->db.db_size); 2605*eda14cbcSMatt Macy ASSERT(tx->tx_txg != 0); 2606*eda14cbcSMatt Macy 2607*eda14cbcSMatt Macy arc_return_buf(buf, db); 2608*eda14cbcSMatt Macy ASSERT(arc_released(buf)); 2609*eda14cbcSMatt Macy 2610*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 2611*eda14cbcSMatt Macy 2612*eda14cbcSMatt Macy while (db->db_state == DB_READ || db->db_state == DB_FILL) 2613*eda14cbcSMatt Macy cv_wait(&db->db_changed, &db->db_mtx); 2614*eda14cbcSMatt Macy 2615*eda14cbcSMatt Macy ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED); 2616*eda14cbcSMatt Macy 2617*eda14cbcSMatt Macy if (db->db_state == DB_CACHED && 2618*eda14cbcSMatt Macy zfs_refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) { 2619*eda14cbcSMatt Macy /* 2620*eda14cbcSMatt Macy * In practice, we will never have a case where we have an 2621*eda14cbcSMatt Macy * encrypted arc buffer while additional holds exist on the 2622*eda14cbcSMatt Macy * dbuf. We don't handle this here so we simply assert that 2623*eda14cbcSMatt Macy * fact instead. 2624*eda14cbcSMatt Macy */ 2625*eda14cbcSMatt Macy ASSERT(!arc_is_encrypted(buf)); 2626*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2627*eda14cbcSMatt Macy (void) dbuf_dirty(db, tx); 2628*eda14cbcSMatt Macy bcopy(buf->b_data, db->db.db_data, db->db.db_size); 2629*eda14cbcSMatt Macy arc_buf_destroy(buf, db); 2630*eda14cbcSMatt Macy xuio_stat_wbuf_copied(); 2631*eda14cbcSMatt Macy return; 2632*eda14cbcSMatt Macy } 2633*eda14cbcSMatt Macy 2634*eda14cbcSMatt Macy xuio_stat_wbuf_nocopy(); 2635*eda14cbcSMatt Macy if (db->db_state == DB_CACHED) { 2636*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records); 2637*eda14cbcSMatt Macy 2638*eda14cbcSMatt Macy ASSERT(db->db_buf != NULL); 2639*eda14cbcSMatt Macy if (dr != NULL && dr->dr_txg == tx->tx_txg) { 2640*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_data == db->db_buf); 2641*eda14cbcSMatt Macy 2642*eda14cbcSMatt Macy if (!arc_released(db->db_buf)) { 2643*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_override_state == 2644*eda14cbcSMatt Macy DR_OVERRIDDEN); 2645*eda14cbcSMatt Macy arc_release(db->db_buf, db); 2646*eda14cbcSMatt Macy } 2647*eda14cbcSMatt Macy dr->dt.dl.dr_data = buf; 2648*eda14cbcSMatt Macy arc_buf_destroy(db->db_buf, db); 2649*eda14cbcSMatt Macy } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) { 2650*eda14cbcSMatt Macy arc_release(db->db_buf, db); 2651*eda14cbcSMatt Macy arc_buf_destroy(db->db_buf, db); 2652*eda14cbcSMatt Macy } 2653*eda14cbcSMatt Macy db->db_buf = NULL; 2654*eda14cbcSMatt Macy } 2655*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL); 2656*eda14cbcSMatt Macy dbuf_set_data(db, buf); 2657*eda14cbcSMatt Macy db->db_state = DB_FILL; 2658*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "filling assigned arcbuf"); 2659*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2660*eda14cbcSMatt Macy (void) dbuf_dirty(db, tx); 2661*eda14cbcSMatt Macy dmu_buf_fill_done(&db->db, tx); 2662*eda14cbcSMatt Macy } 2663*eda14cbcSMatt Macy 2664*eda14cbcSMatt Macy void 2665*eda14cbcSMatt Macy dbuf_destroy(dmu_buf_impl_t *db) 2666*eda14cbcSMatt Macy { 2667*eda14cbcSMatt Macy dnode_t *dn; 2668*eda14cbcSMatt Macy dmu_buf_impl_t *parent = db->db_parent; 2669*eda14cbcSMatt Macy dmu_buf_impl_t *dndb; 2670*eda14cbcSMatt Macy 2671*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 2672*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&db->db_holds)); 2673*eda14cbcSMatt Macy 2674*eda14cbcSMatt Macy if (db->db_buf != NULL) { 2675*eda14cbcSMatt Macy arc_buf_destroy(db->db_buf, db); 2676*eda14cbcSMatt Macy db->db_buf = NULL; 2677*eda14cbcSMatt Macy } 2678*eda14cbcSMatt Macy 2679*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 2680*eda14cbcSMatt Macy int slots = DB_DNODE(db)->dn_num_slots; 2681*eda14cbcSMatt Macy int bonuslen = DN_SLOTS_TO_BONUSLEN(slots); 2682*eda14cbcSMatt Macy if (db->db.db_data != NULL) { 2683*eda14cbcSMatt Macy kmem_free(db->db.db_data, bonuslen); 2684*eda14cbcSMatt Macy arc_space_return(bonuslen, ARC_SPACE_BONUS); 2685*eda14cbcSMatt Macy db->db_state = DB_UNCACHED; 2686*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "buffer cleared"); 2687*eda14cbcSMatt Macy } 2688*eda14cbcSMatt Macy } 2689*eda14cbcSMatt Macy 2690*eda14cbcSMatt Macy dbuf_clear_data(db); 2691*eda14cbcSMatt Macy 2692*eda14cbcSMatt Macy if (multilist_link_active(&db->db_cache_link)) { 2693*eda14cbcSMatt Macy ASSERT(db->db_caching_status == DB_DBUF_CACHE || 2694*eda14cbcSMatt Macy db->db_caching_status == DB_DBUF_METADATA_CACHE); 2695*eda14cbcSMatt Macy 2696*eda14cbcSMatt Macy multilist_remove(dbuf_caches[db->db_caching_status].cache, db); 2697*eda14cbcSMatt Macy (void) zfs_refcount_remove_many( 2698*eda14cbcSMatt Macy &dbuf_caches[db->db_caching_status].size, 2699*eda14cbcSMatt Macy db->db.db_size, db); 2700*eda14cbcSMatt Macy 2701*eda14cbcSMatt Macy if (db->db_caching_status == DB_DBUF_METADATA_CACHE) { 2702*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(metadata_cache_count); 2703*eda14cbcSMatt Macy } else { 2704*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 2705*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_count); 2706*eda14cbcSMatt Macy DBUF_STAT_DECR(cache_levels_bytes[db->db_level], 2707*eda14cbcSMatt Macy db->db.db_size); 2708*eda14cbcSMatt Macy } 2709*eda14cbcSMatt Macy db->db_caching_status = DB_NO_CACHE; 2710*eda14cbcSMatt Macy } 2711*eda14cbcSMatt Macy 2712*eda14cbcSMatt Macy ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL); 2713*eda14cbcSMatt Macy ASSERT(db->db_data_pending == NULL); 2714*eda14cbcSMatt Macy ASSERT(list_is_empty(&db->db_dirty_records)); 2715*eda14cbcSMatt Macy 2716*eda14cbcSMatt Macy db->db_state = DB_EVICTING; 2717*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "buffer eviction started"); 2718*eda14cbcSMatt Macy db->db_blkptr = NULL; 2719*eda14cbcSMatt Macy 2720*eda14cbcSMatt Macy /* 2721*eda14cbcSMatt Macy * Now that db_state is DB_EVICTING, nobody else can find this via 2722*eda14cbcSMatt Macy * the hash table. We can now drop db_mtx, which allows us to 2723*eda14cbcSMatt Macy * acquire the dn_dbufs_mtx. 2724*eda14cbcSMatt Macy */ 2725*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 2726*eda14cbcSMatt Macy 2727*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 2728*eda14cbcSMatt Macy dn = DB_DNODE(db); 2729*eda14cbcSMatt Macy dndb = dn->dn_dbuf; 2730*eda14cbcSMatt Macy if (db->db_blkid != DMU_BONUS_BLKID) { 2731*eda14cbcSMatt Macy boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx); 2732*eda14cbcSMatt Macy if (needlock) 2733*eda14cbcSMatt Macy mutex_enter_nested(&dn->dn_dbufs_mtx, 2734*eda14cbcSMatt Macy NESTED_SINGLE); 2735*eda14cbcSMatt Macy avl_remove(&dn->dn_dbufs, db); 2736*eda14cbcSMatt Macy membar_producer(); 2737*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2738*eda14cbcSMatt Macy if (needlock) 2739*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 2740*eda14cbcSMatt Macy /* 2741*eda14cbcSMatt Macy * Decrementing the dbuf count means that the hold corresponding 2742*eda14cbcSMatt Macy * to the removed dbuf is no longer discounted in dnode_move(), 2743*eda14cbcSMatt Macy * so the dnode cannot be moved until after we release the hold. 2744*eda14cbcSMatt Macy * The membar_producer() ensures visibility of the decremented 2745*eda14cbcSMatt Macy * value in dnode_move(), since DB_DNODE_EXIT doesn't actually 2746*eda14cbcSMatt Macy * release any lock. 2747*eda14cbcSMatt Macy */ 2748*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2749*eda14cbcSMatt Macy dnode_rele_and_unlock(dn, db, B_TRUE); 2750*eda14cbcSMatt Macy db->db_dnode_handle = NULL; 2751*eda14cbcSMatt Macy 2752*eda14cbcSMatt Macy dbuf_hash_remove(db); 2753*eda14cbcSMatt Macy } else { 2754*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 2755*eda14cbcSMatt Macy } 2756*eda14cbcSMatt Macy 2757*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&db->db_holds)); 2758*eda14cbcSMatt Macy 2759*eda14cbcSMatt Macy db->db_parent = NULL; 2760*eda14cbcSMatt Macy 2761*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL); 2762*eda14cbcSMatt Macy ASSERT(db->db.db_data == NULL); 2763*eda14cbcSMatt Macy ASSERT(db->db_hash_next == NULL); 2764*eda14cbcSMatt Macy ASSERT(db->db_blkptr == NULL); 2765*eda14cbcSMatt Macy ASSERT(db->db_data_pending == NULL); 2766*eda14cbcSMatt Macy ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 2767*eda14cbcSMatt Macy ASSERT(!multilist_link_active(&db->db_cache_link)); 2768*eda14cbcSMatt Macy 2769*eda14cbcSMatt Macy kmem_cache_free(dbuf_kmem_cache, db); 2770*eda14cbcSMatt Macy arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 2771*eda14cbcSMatt Macy 2772*eda14cbcSMatt Macy /* 2773*eda14cbcSMatt Macy * If this dbuf is referenced from an indirect dbuf, 2774*eda14cbcSMatt Macy * decrement the ref count on the indirect dbuf. 2775*eda14cbcSMatt Macy */ 2776*eda14cbcSMatt Macy if (parent && parent != dndb) { 2777*eda14cbcSMatt Macy mutex_enter(&parent->db_mtx); 2778*eda14cbcSMatt Macy dbuf_rele_and_unlock(parent, db, B_TRUE); 2779*eda14cbcSMatt Macy } 2780*eda14cbcSMatt Macy } 2781*eda14cbcSMatt Macy 2782*eda14cbcSMatt Macy /* 2783*eda14cbcSMatt Macy * Note: While bpp will always be updated if the function returns success, 2784*eda14cbcSMatt Macy * parentp will not be updated if the dnode does not have dn_dbuf filled in; 2785*eda14cbcSMatt Macy * this happens when the dnode is the meta-dnode, or {user|group|project}used 2786*eda14cbcSMatt Macy * object. 2787*eda14cbcSMatt Macy */ 2788*eda14cbcSMatt Macy __attribute__((always_inline)) 2789*eda14cbcSMatt Macy static inline int 2790*eda14cbcSMatt Macy dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse, 2791*eda14cbcSMatt Macy dmu_buf_impl_t **parentp, blkptr_t **bpp) 2792*eda14cbcSMatt Macy { 2793*eda14cbcSMatt Macy *parentp = NULL; 2794*eda14cbcSMatt Macy *bpp = NULL; 2795*eda14cbcSMatt Macy 2796*eda14cbcSMatt Macy ASSERT(blkid != DMU_BONUS_BLKID); 2797*eda14cbcSMatt Macy 2798*eda14cbcSMatt Macy if (blkid == DMU_SPILL_BLKID) { 2799*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 2800*eda14cbcSMatt Macy if (dn->dn_have_spill && 2801*eda14cbcSMatt Macy (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 2802*eda14cbcSMatt Macy *bpp = DN_SPILL_BLKPTR(dn->dn_phys); 2803*eda14cbcSMatt Macy else 2804*eda14cbcSMatt Macy *bpp = NULL; 2805*eda14cbcSMatt Macy dbuf_add_ref(dn->dn_dbuf, NULL); 2806*eda14cbcSMatt Macy *parentp = dn->dn_dbuf; 2807*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 2808*eda14cbcSMatt Macy return (0); 2809*eda14cbcSMatt Macy } 2810*eda14cbcSMatt Macy 2811*eda14cbcSMatt Macy int nlevels = 2812*eda14cbcSMatt Macy (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels; 2813*eda14cbcSMatt Macy int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2814*eda14cbcSMatt Macy 2815*eda14cbcSMatt Macy ASSERT3U(level * epbs, <, 64); 2816*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2817*eda14cbcSMatt Macy /* 2818*eda14cbcSMatt Macy * This assertion shouldn't trip as long as the max indirect block size 2819*eda14cbcSMatt Macy * is less than 1M. The reason for this is that up to that point, 2820*eda14cbcSMatt Macy * the number of levels required to address an entire object with blocks 2821*eda14cbcSMatt Macy * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In 2822*eda14cbcSMatt Macy * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55 2823*eda14cbcSMatt Macy * (i.e. we can address the entire object), objects will all use at most 2824*eda14cbcSMatt Macy * N-1 levels and the assertion won't overflow. However, once epbs is 2825*eda14cbcSMatt Macy * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be 2826*eda14cbcSMatt Macy * enough to address an entire object, so objects will have 5 levels, 2827*eda14cbcSMatt Macy * but then this assertion will overflow. 2828*eda14cbcSMatt Macy * 2829*eda14cbcSMatt Macy * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we 2830*eda14cbcSMatt Macy * need to redo this logic to handle overflows. 2831*eda14cbcSMatt Macy */ 2832*eda14cbcSMatt Macy ASSERT(level >= nlevels || 2833*eda14cbcSMatt Macy ((nlevels - level - 1) * epbs) + 2834*eda14cbcSMatt Macy highbit64(dn->dn_phys->dn_nblkptr) <= 64); 2835*eda14cbcSMatt Macy if (level >= nlevels || 2836*eda14cbcSMatt Macy blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr << 2837*eda14cbcSMatt Macy ((nlevels - level - 1) * epbs)) || 2838*eda14cbcSMatt Macy (fail_sparse && 2839*eda14cbcSMatt Macy blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) { 2840*eda14cbcSMatt Macy /* the buffer has no parent yet */ 2841*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 2842*eda14cbcSMatt Macy } else if (level < nlevels-1) { 2843*eda14cbcSMatt Macy /* this block is referenced from an indirect block */ 2844*eda14cbcSMatt Macy int err; 2845*eda14cbcSMatt Macy 2846*eda14cbcSMatt Macy err = dbuf_hold_impl(dn, level + 1, 2847*eda14cbcSMatt Macy blkid >> epbs, fail_sparse, FALSE, NULL, parentp); 2848*eda14cbcSMatt Macy 2849*eda14cbcSMatt Macy if (err) 2850*eda14cbcSMatt Macy return (err); 2851*eda14cbcSMatt Macy err = dbuf_read(*parentp, NULL, 2852*eda14cbcSMatt Macy (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 2853*eda14cbcSMatt Macy if (err) { 2854*eda14cbcSMatt Macy dbuf_rele(*parentp, NULL); 2855*eda14cbcSMatt Macy *parentp = NULL; 2856*eda14cbcSMatt Macy return (err); 2857*eda14cbcSMatt Macy } 2858*eda14cbcSMatt Macy rw_enter(&(*parentp)->db_rwlock, RW_READER); 2859*eda14cbcSMatt Macy *bpp = ((blkptr_t *)(*parentp)->db.db_data) + 2860*eda14cbcSMatt Macy (blkid & ((1ULL << epbs) - 1)); 2861*eda14cbcSMatt Macy if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs))) 2862*eda14cbcSMatt Macy ASSERT(BP_IS_HOLE(*bpp)); 2863*eda14cbcSMatt Macy rw_exit(&(*parentp)->db_rwlock); 2864*eda14cbcSMatt Macy return (0); 2865*eda14cbcSMatt Macy } else { 2866*eda14cbcSMatt Macy /* the block is referenced from the dnode */ 2867*eda14cbcSMatt Macy ASSERT3U(level, ==, nlevels-1); 2868*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_nblkptr == 0 || 2869*eda14cbcSMatt Macy blkid < dn->dn_phys->dn_nblkptr); 2870*eda14cbcSMatt Macy if (dn->dn_dbuf) { 2871*eda14cbcSMatt Macy dbuf_add_ref(dn->dn_dbuf, NULL); 2872*eda14cbcSMatt Macy *parentp = dn->dn_dbuf; 2873*eda14cbcSMatt Macy } 2874*eda14cbcSMatt Macy *bpp = &dn->dn_phys->dn_blkptr[blkid]; 2875*eda14cbcSMatt Macy return (0); 2876*eda14cbcSMatt Macy } 2877*eda14cbcSMatt Macy } 2878*eda14cbcSMatt Macy 2879*eda14cbcSMatt Macy static dmu_buf_impl_t * 2880*eda14cbcSMatt Macy dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid, 2881*eda14cbcSMatt Macy dmu_buf_impl_t *parent, blkptr_t *blkptr) 2882*eda14cbcSMatt Macy { 2883*eda14cbcSMatt Macy objset_t *os = dn->dn_objset; 2884*eda14cbcSMatt Macy dmu_buf_impl_t *db, *odb; 2885*eda14cbcSMatt Macy 2886*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2887*eda14cbcSMatt Macy ASSERT(dn->dn_type != DMU_OT_NONE); 2888*eda14cbcSMatt Macy 2889*eda14cbcSMatt Macy db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP); 2890*eda14cbcSMatt Macy 2891*eda14cbcSMatt Macy list_create(&db->db_dirty_records, sizeof (dbuf_dirty_record_t), 2892*eda14cbcSMatt Macy offsetof(dbuf_dirty_record_t, dr_dbuf_node)); 2893*eda14cbcSMatt Macy 2894*eda14cbcSMatt Macy db->db_objset = os; 2895*eda14cbcSMatt Macy db->db.db_object = dn->dn_object; 2896*eda14cbcSMatt Macy db->db_level = level; 2897*eda14cbcSMatt Macy db->db_blkid = blkid; 2898*eda14cbcSMatt Macy db->db_dirtycnt = 0; 2899*eda14cbcSMatt Macy db->db_dnode_handle = dn->dn_handle; 2900*eda14cbcSMatt Macy db->db_parent = parent; 2901*eda14cbcSMatt Macy db->db_blkptr = blkptr; 2902*eda14cbcSMatt Macy 2903*eda14cbcSMatt Macy db->db_user = NULL; 2904*eda14cbcSMatt Macy db->db_user_immediate_evict = FALSE; 2905*eda14cbcSMatt Macy db->db_freed_in_flight = FALSE; 2906*eda14cbcSMatt Macy db->db_pending_evict = FALSE; 2907*eda14cbcSMatt Macy 2908*eda14cbcSMatt Macy if (blkid == DMU_BONUS_BLKID) { 2909*eda14cbcSMatt Macy ASSERT3P(parent, ==, dn->dn_dbuf); 2910*eda14cbcSMatt Macy db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) - 2911*eda14cbcSMatt Macy (dn->dn_nblkptr-1) * sizeof (blkptr_t); 2912*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 2913*eda14cbcSMatt Macy db->db.db_offset = DMU_BONUS_BLKID; 2914*eda14cbcSMatt Macy db->db_state = DB_UNCACHED; 2915*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "bonus buffer created"); 2916*eda14cbcSMatt Macy db->db_caching_status = DB_NO_CACHE; 2917*eda14cbcSMatt Macy /* the bonus dbuf is not placed in the hash table */ 2918*eda14cbcSMatt Macy arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 2919*eda14cbcSMatt Macy return (db); 2920*eda14cbcSMatt Macy } else if (blkid == DMU_SPILL_BLKID) { 2921*eda14cbcSMatt Macy db->db.db_size = (blkptr != NULL) ? 2922*eda14cbcSMatt Macy BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE; 2923*eda14cbcSMatt Macy db->db.db_offset = 0; 2924*eda14cbcSMatt Macy } else { 2925*eda14cbcSMatt Macy int blocksize = 2926*eda14cbcSMatt Macy db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz; 2927*eda14cbcSMatt Macy db->db.db_size = blocksize; 2928*eda14cbcSMatt Macy db->db.db_offset = db->db_blkid * blocksize; 2929*eda14cbcSMatt Macy } 2930*eda14cbcSMatt Macy 2931*eda14cbcSMatt Macy /* 2932*eda14cbcSMatt Macy * Hold the dn_dbufs_mtx while we get the new dbuf 2933*eda14cbcSMatt Macy * in the hash table *and* added to the dbufs list. 2934*eda14cbcSMatt Macy * This prevents a possible deadlock with someone 2935*eda14cbcSMatt Macy * trying to look up this dbuf before it's added to the 2936*eda14cbcSMatt Macy * dn_dbufs list. 2937*eda14cbcSMatt Macy */ 2938*eda14cbcSMatt Macy mutex_enter(&dn->dn_dbufs_mtx); 2939*eda14cbcSMatt Macy db->db_state = DB_EVICTING; /* not worth logging this state change */ 2940*eda14cbcSMatt Macy if ((odb = dbuf_hash_insert(db)) != NULL) { 2941*eda14cbcSMatt Macy /* someone else inserted it first */ 2942*eda14cbcSMatt Macy kmem_cache_free(dbuf_kmem_cache, db); 2943*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 2944*eda14cbcSMatt Macy DBUF_STAT_BUMP(hash_insert_race); 2945*eda14cbcSMatt Macy return (odb); 2946*eda14cbcSMatt Macy } 2947*eda14cbcSMatt Macy avl_add(&dn->dn_dbufs, db); 2948*eda14cbcSMatt Macy 2949*eda14cbcSMatt Macy db->db_state = DB_UNCACHED; 2950*eda14cbcSMatt Macy DTRACE_SET_STATE(db, "regular buffer created"); 2951*eda14cbcSMatt Macy db->db_caching_status = DB_NO_CACHE; 2952*eda14cbcSMatt Macy mutex_exit(&dn->dn_dbufs_mtx); 2953*eda14cbcSMatt Macy arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 2954*eda14cbcSMatt Macy 2955*eda14cbcSMatt Macy if (parent && parent != dn->dn_dbuf) 2956*eda14cbcSMatt Macy dbuf_add_ref(parent, db); 2957*eda14cbcSMatt Macy 2958*eda14cbcSMatt Macy ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 2959*eda14cbcSMatt Macy zfs_refcount_count(&dn->dn_holds) > 0); 2960*eda14cbcSMatt Macy (void) zfs_refcount_add(&dn->dn_holds, db); 2961*eda14cbcSMatt Macy 2962*eda14cbcSMatt Macy dprintf_dbuf(db, "db=%p\n", db); 2963*eda14cbcSMatt Macy 2964*eda14cbcSMatt Macy return (db); 2965*eda14cbcSMatt Macy } 2966*eda14cbcSMatt Macy 2967*eda14cbcSMatt Macy /* 2968*eda14cbcSMatt Macy * This function returns a block pointer and information about the object, 2969*eda14cbcSMatt Macy * given a dnode and a block. This is a publicly accessible version of 2970*eda14cbcSMatt Macy * dbuf_findbp that only returns some information, rather than the 2971*eda14cbcSMatt Macy * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock 2972*eda14cbcSMatt Macy * should be locked as (at least) a reader. 2973*eda14cbcSMatt Macy */ 2974*eda14cbcSMatt Macy int 2975*eda14cbcSMatt Macy dbuf_dnode_findbp(dnode_t *dn, uint64_t level, uint64_t blkid, 2976*eda14cbcSMatt Macy blkptr_t *bp, uint16_t *datablkszsec, uint8_t *indblkshift) 2977*eda14cbcSMatt Macy { 2978*eda14cbcSMatt Macy dmu_buf_impl_t *dbp = NULL; 2979*eda14cbcSMatt Macy blkptr_t *bp2; 2980*eda14cbcSMatt Macy int err = 0; 2981*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2982*eda14cbcSMatt Macy 2983*eda14cbcSMatt Macy err = dbuf_findbp(dn, level, blkid, B_FALSE, &dbp, &bp2); 2984*eda14cbcSMatt Macy if (err == 0) { 2985*eda14cbcSMatt Macy *bp = *bp2; 2986*eda14cbcSMatt Macy if (dbp != NULL) 2987*eda14cbcSMatt Macy dbuf_rele(dbp, NULL); 2988*eda14cbcSMatt Macy if (datablkszsec != NULL) 2989*eda14cbcSMatt Macy *datablkszsec = dn->dn_phys->dn_datablkszsec; 2990*eda14cbcSMatt Macy if (indblkshift != NULL) 2991*eda14cbcSMatt Macy *indblkshift = dn->dn_phys->dn_indblkshift; 2992*eda14cbcSMatt Macy } 2993*eda14cbcSMatt Macy 2994*eda14cbcSMatt Macy return (err); 2995*eda14cbcSMatt Macy } 2996*eda14cbcSMatt Macy 2997*eda14cbcSMatt Macy typedef struct dbuf_prefetch_arg { 2998*eda14cbcSMatt Macy spa_t *dpa_spa; /* The spa to issue the prefetch in. */ 2999*eda14cbcSMatt Macy zbookmark_phys_t dpa_zb; /* The target block to prefetch. */ 3000*eda14cbcSMatt Macy int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */ 3001*eda14cbcSMatt Macy int dpa_curlevel; /* The current level that we're reading */ 3002*eda14cbcSMatt Macy dnode_t *dpa_dnode; /* The dnode associated with the prefetch */ 3003*eda14cbcSMatt Macy zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */ 3004*eda14cbcSMatt Macy zio_t *dpa_zio; /* The parent zio_t for all prefetches. */ 3005*eda14cbcSMatt Macy arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */ 3006*eda14cbcSMatt Macy } dbuf_prefetch_arg_t; 3007*eda14cbcSMatt Macy 3008*eda14cbcSMatt Macy /* 3009*eda14cbcSMatt Macy * Actually issue the prefetch read for the block given. 3010*eda14cbcSMatt Macy */ 3011*eda14cbcSMatt Macy static void 3012*eda14cbcSMatt Macy dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp) 3013*eda14cbcSMatt Macy { 3014*eda14cbcSMatt Macy ASSERT(!BP_IS_REDACTED(bp) || 3015*eda14cbcSMatt Macy dsl_dataset_feature_is_active( 3016*eda14cbcSMatt Macy dpa->dpa_dnode->dn_objset->os_dsl_dataset, 3017*eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)); 3018*eda14cbcSMatt Macy 3019*eda14cbcSMatt Macy if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) 3020*eda14cbcSMatt Macy return; 3021*eda14cbcSMatt Macy 3022*eda14cbcSMatt Macy int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE; 3023*eda14cbcSMatt Macy arc_flags_t aflags = 3024*eda14cbcSMatt Macy dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH; 3025*eda14cbcSMatt Macy 3026*eda14cbcSMatt Macy /* dnodes are always read as raw and then converted later */ 3027*eda14cbcSMatt Macy if (BP_GET_TYPE(bp) == DMU_OT_DNODE && BP_IS_PROTECTED(bp) && 3028*eda14cbcSMatt Macy dpa->dpa_curlevel == 0) 3029*eda14cbcSMatt Macy zio_flags |= ZIO_FLAG_RAW; 3030*eda14cbcSMatt Macy 3031*eda14cbcSMatt Macy ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 3032*eda14cbcSMatt Macy ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level); 3033*eda14cbcSMatt Macy ASSERT(dpa->dpa_zio != NULL); 3034*eda14cbcSMatt Macy (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL, 3035*eda14cbcSMatt Macy dpa->dpa_prio, zio_flags, &aflags, &dpa->dpa_zb); 3036*eda14cbcSMatt Macy } 3037*eda14cbcSMatt Macy 3038*eda14cbcSMatt Macy /* 3039*eda14cbcSMatt Macy * Called when an indirect block above our prefetch target is read in. This 3040*eda14cbcSMatt Macy * will either read in the next indirect block down the tree or issue the actual 3041*eda14cbcSMatt Macy * prefetch if the next block down is our target. 3042*eda14cbcSMatt Macy */ 3043*eda14cbcSMatt Macy static void 3044*eda14cbcSMatt Macy dbuf_prefetch_indirect_done(zio_t *zio, const zbookmark_phys_t *zb, 3045*eda14cbcSMatt Macy const blkptr_t *iobp, arc_buf_t *abuf, void *private) 3046*eda14cbcSMatt Macy { 3047*eda14cbcSMatt Macy dbuf_prefetch_arg_t *dpa = private; 3048*eda14cbcSMatt Macy 3049*eda14cbcSMatt Macy ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel); 3050*eda14cbcSMatt Macy ASSERT3S(dpa->dpa_curlevel, >, 0); 3051*eda14cbcSMatt Macy 3052*eda14cbcSMatt Macy if (abuf == NULL) { 3053*eda14cbcSMatt Macy ASSERT(zio == NULL || zio->io_error != 0); 3054*eda14cbcSMatt Macy kmem_free(dpa, sizeof (*dpa)); 3055*eda14cbcSMatt Macy return; 3056*eda14cbcSMatt Macy } 3057*eda14cbcSMatt Macy ASSERT(zio == NULL || zio->io_error == 0); 3058*eda14cbcSMatt Macy 3059*eda14cbcSMatt Macy /* 3060*eda14cbcSMatt Macy * The dpa_dnode is only valid if we are called with a NULL 3061*eda14cbcSMatt Macy * zio. This indicates that the arc_read() returned without 3062*eda14cbcSMatt Macy * first calling zio_read() to issue a physical read. Once 3063*eda14cbcSMatt Macy * a physical read is made the dpa_dnode must be invalidated 3064*eda14cbcSMatt Macy * as the locks guarding it may have been dropped. If the 3065*eda14cbcSMatt Macy * dpa_dnode is still valid, then we want to add it to the dbuf 3066*eda14cbcSMatt Macy * cache. To do so, we must hold the dbuf associated with the block 3067*eda14cbcSMatt Macy * we just prefetched, read its contents so that we associate it 3068*eda14cbcSMatt Macy * with an arc_buf_t, and then release it. 3069*eda14cbcSMatt Macy */ 3070*eda14cbcSMatt Macy if (zio != NULL) { 3071*eda14cbcSMatt Macy ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel); 3072*eda14cbcSMatt Macy if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) { 3073*eda14cbcSMatt Macy ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size); 3074*eda14cbcSMatt Macy } else { 3075*eda14cbcSMatt Macy ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size); 3076*eda14cbcSMatt Macy } 3077*eda14cbcSMatt Macy ASSERT3P(zio->io_spa, ==, dpa->dpa_spa); 3078*eda14cbcSMatt Macy 3079*eda14cbcSMatt Macy dpa->dpa_dnode = NULL; 3080*eda14cbcSMatt Macy } else if (dpa->dpa_dnode != NULL) { 3081*eda14cbcSMatt Macy uint64_t curblkid = dpa->dpa_zb.zb_blkid >> 3082*eda14cbcSMatt Macy (dpa->dpa_epbs * (dpa->dpa_curlevel - 3083*eda14cbcSMatt Macy dpa->dpa_zb.zb_level)); 3084*eda14cbcSMatt Macy dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode, 3085*eda14cbcSMatt Macy dpa->dpa_curlevel, curblkid, FTAG); 3086*eda14cbcSMatt Macy if (db == NULL) { 3087*eda14cbcSMatt Macy kmem_free(dpa, sizeof (*dpa)); 3088*eda14cbcSMatt Macy arc_buf_destroy(abuf, private); 3089*eda14cbcSMatt Macy return; 3090*eda14cbcSMatt Macy } 3091*eda14cbcSMatt Macy 3092*eda14cbcSMatt Macy (void) dbuf_read(db, NULL, 3093*eda14cbcSMatt Macy DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT); 3094*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 3095*eda14cbcSMatt Macy } 3096*eda14cbcSMatt Macy 3097*eda14cbcSMatt Macy dpa->dpa_curlevel--; 3098*eda14cbcSMatt Macy uint64_t nextblkid = dpa->dpa_zb.zb_blkid >> 3099*eda14cbcSMatt Macy (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level)); 3100*eda14cbcSMatt Macy blkptr_t *bp = ((blkptr_t *)abuf->b_data) + 3101*eda14cbcSMatt Macy P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs); 3102*eda14cbcSMatt Macy 3103*eda14cbcSMatt Macy ASSERT(!BP_IS_REDACTED(bp) || 3104*eda14cbcSMatt Macy dsl_dataset_feature_is_active( 3105*eda14cbcSMatt Macy dpa->dpa_dnode->dn_objset->os_dsl_dataset, 3106*eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)); 3107*eda14cbcSMatt Macy if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) { 3108*eda14cbcSMatt Macy kmem_free(dpa, sizeof (*dpa)); 3109*eda14cbcSMatt Macy } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) { 3110*eda14cbcSMatt Macy ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid); 3111*eda14cbcSMatt Macy dbuf_issue_final_prefetch(dpa, bp); 3112*eda14cbcSMatt Macy kmem_free(dpa, sizeof (*dpa)); 3113*eda14cbcSMatt Macy } else { 3114*eda14cbcSMatt Macy arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 3115*eda14cbcSMatt Macy zbookmark_phys_t zb; 3116*eda14cbcSMatt Macy 3117*eda14cbcSMatt Macy /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 3118*eda14cbcSMatt Macy if (dpa->dpa_aflags & ARC_FLAG_L2CACHE) 3119*eda14cbcSMatt Macy iter_aflags |= ARC_FLAG_L2CACHE; 3120*eda14cbcSMatt Macy 3121*eda14cbcSMatt Macy ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 3122*eda14cbcSMatt Macy 3123*eda14cbcSMatt Macy SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset, 3124*eda14cbcSMatt Macy dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid); 3125*eda14cbcSMatt Macy 3126*eda14cbcSMatt Macy (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 3127*eda14cbcSMatt Macy bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio, 3128*eda14cbcSMatt Macy ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 3129*eda14cbcSMatt Macy &iter_aflags, &zb); 3130*eda14cbcSMatt Macy } 3131*eda14cbcSMatt Macy 3132*eda14cbcSMatt Macy arc_buf_destroy(abuf, private); 3133*eda14cbcSMatt Macy } 3134*eda14cbcSMatt Macy 3135*eda14cbcSMatt Macy /* 3136*eda14cbcSMatt Macy * Issue prefetch reads for the given block on the given level. If the indirect 3137*eda14cbcSMatt Macy * blocks above that block are not in memory, we will read them in 3138*eda14cbcSMatt Macy * asynchronously. As a result, this call never blocks waiting for a read to 3139*eda14cbcSMatt Macy * complete. Note that the prefetch might fail if the dataset is encrypted and 3140*eda14cbcSMatt Macy * the encryption key is unmapped before the IO completes. 3141*eda14cbcSMatt Macy */ 3142*eda14cbcSMatt Macy void 3143*eda14cbcSMatt Macy dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio, 3144*eda14cbcSMatt Macy arc_flags_t aflags) 3145*eda14cbcSMatt Macy { 3146*eda14cbcSMatt Macy blkptr_t bp; 3147*eda14cbcSMatt Macy int epbs, nlevels, curlevel; 3148*eda14cbcSMatt Macy uint64_t curblkid; 3149*eda14cbcSMatt Macy 3150*eda14cbcSMatt Macy ASSERT(blkid != DMU_BONUS_BLKID); 3151*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3152*eda14cbcSMatt Macy 3153*eda14cbcSMatt Macy if (blkid > dn->dn_maxblkid) 3154*eda14cbcSMatt Macy return; 3155*eda14cbcSMatt Macy 3156*eda14cbcSMatt Macy if (level == 0 && dnode_block_freed(dn, blkid)) 3157*eda14cbcSMatt Macy return; 3158*eda14cbcSMatt Macy 3159*eda14cbcSMatt Macy /* 3160*eda14cbcSMatt Macy * This dnode hasn't been written to disk yet, so there's nothing to 3161*eda14cbcSMatt Macy * prefetch. 3162*eda14cbcSMatt Macy */ 3163*eda14cbcSMatt Macy nlevels = dn->dn_phys->dn_nlevels; 3164*eda14cbcSMatt Macy if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0) 3165*eda14cbcSMatt Macy return; 3166*eda14cbcSMatt Macy 3167*eda14cbcSMatt Macy epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3168*eda14cbcSMatt Macy if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level)) 3169*eda14cbcSMatt Macy return; 3170*eda14cbcSMatt Macy 3171*eda14cbcSMatt Macy dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object, 3172*eda14cbcSMatt Macy level, blkid); 3173*eda14cbcSMatt Macy if (db != NULL) { 3174*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3175*eda14cbcSMatt Macy /* 3176*eda14cbcSMatt Macy * This dbuf already exists. It is either CACHED, or 3177*eda14cbcSMatt Macy * (we assume) about to be read or filled. 3178*eda14cbcSMatt Macy */ 3179*eda14cbcSMatt Macy return; 3180*eda14cbcSMatt Macy } 3181*eda14cbcSMatt Macy 3182*eda14cbcSMatt Macy /* 3183*eda14cbcSMatt Macy * Find the closest ancestor (indirect block) of the target block 3184*eda14cbcSMatt Macy * that is present in the cache. In this indirect block, we will 3185*eda14cbcSMatt Macy * find the bp that is at curlevel, curblkid. 3186*eda14cbcSMatt Macy */ 3187*eda14cbcSMatt Macy curlevel = level; 3188*eda14cbcSMatt Macy curblkid = blkid; 3189*eda14cbcSMatt Macy while (curlevel < nlevels - 1) { 3190*eda14cbcSMatt Macy int parent_level = curlevel + 1; 3191*eda14cbcSMatt Macy uint64_t parent_blkid = curblkid >> epbs; 3192*eda14cbcSMatt Macy dmu_buf_impl_t *db; 3193*eda14cbcSMatt Macy 3194*eda14cbcSMatt Macy if (dbuf_hold_impl(dn, parent_level, parent_blkid, 3195*eda14cbcSMatt Macy FALSE, TRUE, FTAG, &db) == 0) { 3196*eda14cbcSMatt Macy blkptr_t *bpp = db->db_buf->b_data; 3197*eda14cbcSMatt Macy bp = bpp[P2PHASE(curblkid, 1 << epbs)]; 3198*eda14cbcSMatt Macy dbuf_rele(db, FTAG); 3199*eda14cbcSMatt Macy break; 3200*eda14cbcSMatt Macy } 3201*eda14cbcSMatt Macy 3202*eda14cbcSMatt Macy curlevel = parent_level; 3203*eda14cbcSMatt Macy curblkid = parent_blkid; 3204*eda14cbcSMatt Macy } 3205*eda14cbcSMatt Macy 3206*eda14cbcSMatt Macy if (curlevel == nlevels - 1) { 3207*eda14cbcSMatt Macy /* No cached indirect blocks found. */ 3208*eda14cbcSMatt Macy ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr); 3209*eda14cbcSMatt Macy bp = dn->dn_phys->dn_blkptr[curblkid]; 3210*eda14cbcSMatt Macy } 3211*eda14cbcSMatt Macy ASSERT(!BP_IS_REDACTED(&bp) || 3212*eda14cbcSMatt Macy dsl_dataset_feature_is_active(dn->dn_objset->os_dsl_dataset, 3213*eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)); 3214*eda14cbcSMatt Macy if (BP_IS_HOLE(&bp) || BP_IS_REDACTED(&bp)) 3215*eda14cbcSMatt Macy return; 3216*eda14cbcSMatt Macy 3217*eda14cbcSMatt Macy ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp)); 3218*eda14cbcSMatt Macy 3219*eda14cbcSMatt Macy zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL, 3220*eda14cbcSMatt Macy ZIO_FLAG_CANFAIL); 3221*eda14cbcSMatt Macy 3222*eda14cbcSMatt Macy dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP); 3223*eda14cbcSMatt Macy dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 3224*eda14cbcSMatt Macy SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 3225*eda14cbcSMatt Macy dn->dn_object, level, blkid); 3226*eda14cbcSMatt Macy dpa->dpa_curlevel = curlevel; 3227*eda14cbcSMatt Macy dpa->dpa_prio = prio; 3228*eda14cbcSMatt Macy dpa->dpa_aflags = aflags; 3229*eda14cbcSMatt Macy dpa->dpa_spa = dn->dn_objset->os_spa; 3230*eda14cbcSMatt Macy dpa->dpa_dnode = dn; 3231*eda14cbcSMatt Macy dpa->dpa_epbs = epbs; 3232*eda14cbcSMatt Macy dpa->dpa_zio = pio; 3233*eda14cbcSMatt Macy 3234*eda14cbcSMatt Macy /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 3235*eda14cbcSMatt Macy if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level)) 3236*eda14cbcSMatt Macy dpa->dpa_aflags |= ARC_FLAG_L2CACHE; 3237*eda14cbcSMatt Macy 3238*eda14cbcSMatt Macy /* 3239*eda14cbcSMatt Macy * If we have the indirect just above us, no need to do the asynchronous 3240*eda14cbcSMatt Macy * prefetch chain; we'll just run the last step ourselves. If we're at 3241*eda14cbcSMatt Macy * a higher level, though, we want to issue the prefetches for all the 3242*eda14cbcSMatt Macy * indirect blocks asynchronously, so we can go on with whatever we were 3243*eda14cbcSMatt Macy * doing. 3244*eda14cbcSMatt Macy */ 3245*eda14cbcSMatt Macy if (curlevel == level) { 3246*eda14cbcSMatt Macy ASSERT3U(curblkid, ==, blkid); 3247*eda14cbcSMatt Macy dbuf_issue_final_prefetch(dpa, &bp); 3248*eda14cbcSMatt Macy kmem_free(dpa, sizeof (*dpa)); 3249*eda14cbcSMatt Macy } else { 3250*eda14cbcSMatt Macy arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 3251*eda14cbcSMatt Macy zbookmark_phys_t zb; 3252*eda14cbcSMatt Macy 3253*eda14cbcSMatt Macy /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 3254*eda14cbcSMatt Macy if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level)) 3255*eda14cbcSMatt Macy iter_aflags |= ARC_FLAG_L2CACHE; 3256*eda14cbcSMatt Macy 3257*eda14cbcSMatt Macy SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 3258*eda14cbcSMatt Macy dn->dn_object, curlevel, curblkid); 3259*eda14cbcSMatt Macy (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 3260*eda14cbcSMatt Macy &bp, dbuf_prefetch_indirect_done, dpa, prio, 3261*eda14cbcSMatt Macy ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 3262*eda14cbcSMatt Macy &iter_aflags, &zb); 3263*eda14cbcSMatt Macy } 3264*eda14cbcSMatt Macy /* 3265*eda14cbcSMatt Macy * We use pio here instead of dpa_zio since it's possible that 3266*eda14cbcSMatt Macy * dpa may have already been freed. 3267*eda14cbcSMatt Macy */ 3268*eda14cbcSMatt Macy zio_nowait(pio); 3269*eda14cbcSMatt Macy } 3270*eda14cbcSMatt Macy 3271*eda14cbcSMatt Macy /* 3272*eda14cbcSMatt Macy * Helper function for dbuf_hold_impl() to copy a buffer. Handles 3273*eda14cbcSMatt Macy * the case of encrypted, compressed and uncompressed buffers by 3274*eda14cbcSMatt Macy * allocating the new buffer, respectively, with arc_alloc_raw_buf(), 3275*eda14cbcSMatt Macy * arc_alloc_compressed_buf() or arc_alloc_buf().* 3276*eda14cbcSMatt Macy * 3277*eda14cbcSMatt Macy * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl(). 3278*eda14cbcSMatt Macy */ 3279*eda14cbcSMatt Macy noinline static void 3280*eda14cbcSMatt Macy dbuf_hold_copy(dnode_t *dn, dmu_buf_impl_t *db) 3281*eda14cbcSMatt Macy { 3282*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = db->db_data_pending; 3283*eda14cbcSMatt Macy arc_buf_t *newdata, *data = dr->dt.dl.dr_data; 3284*eda14cbcSMatt Macy 3285*eda14cbcSMatt Macy newdata = dbuf_alloc_arcbuf_from_arcbuf(db, data); 3286*eda14cbcSMatt Macy dbuf_set_data(db, newdata); 3287*eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 3288*eda14cbcSMatt Macy bcopy(data->b_data, db->db.db_data, arc_buf_size(data)); 3289*eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 3290*eda14cbcSMatt Macy } 3291*eda14cbcSMatt Macy 3292*eda14cbcSMatt Macy /* 3293*eda14cbcSMatt Macy * Returns with db_holds incremented, and db_mtx not held. 3294*eda14cbcSMatt Macy * Note: dn_struct_rwlock must be held. 3295*eda14cbcSMatt Macy */ 3296*eda14cbcSMatt Macy int 3297*eda14cbcSMatt Macy dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, 3298*eda14cbcSMatt Macy boolean_t fail_sparse, boolean_t fail_uncached, 3299*eda14cbcSMatt Macy void *tag, dmu_buf_impl_t **dbp) 3300*eda14cbcSMatt Macy { 3301*eda14cbcSMatt Macy dmu_buf_impl_t *db, *parent = NULL; 3302*eda14cbcSMatt Macy 3303*eda14cbcSMatt Macy /* If the pool has been created, verify the tx_sync_lock is not held */ 3304*eda14cbcSMatt Macy spa_t *spa = dn->dn_objset->os_spa; 3305*eda14cbcSMatt Macy dsl_pool_t *dp = spa->spa_dsl_pool; 3306*eda14cbcSMatt Macy if (dp != NULL) { 3307*eda14cbcSMatt Macy ASSERT(!MUTEX_HELD(&dp->dp_tx.tx_sync_lock)); 3308*eda14cbcSMatt Macy } 3309*eda14cbcSMatt Macy 3310*eda14cbcSMatt Macy ASSERT(blkid != DMU_BONUS_BLKID); 3311*eda14cbcSMatt Macy ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3312*eda14cbcSMatt Macy ASSERT3U(dn->dn_nlevels, >, level); 3313*eda14cbcSMatt Macy 3314*eda14cbcSMatt Macy *dbp = NULL; 3315*eda14cbcSMatt Macy 3316*eda14cbcSMatt Macy /* dbuf_find() returns with db_mtx held */ 3317*eda14cbcSMatt Macy db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid); 3318*eda14cbcSMatt Macy 3319*eda14cbcSMatt Macy if (db == NULL) { 3320*eda14cbcSMatt Macy blkptr_t *bp = NULL; 3321*eda14cbcSMatt Macy int err; 3322*eda14cbcSMatt Macy 3323*eda14cbcSMatt Macy if (fail_uncached) 3324*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 3325*eda14cbcSMatt Macy 3326*eda14cbcSMatt Macy ASSERT3P(parent, ==, NULL); 3327*eda14cbcSMatt Macy err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp); 3328*eda14cbcSMatt Macy if (fail_sparse) { 3329*eda14cbcSMatt Macy if (err == 0 && bp && BP_IS_HOLE(bp)) 3330*eda14cbcSMatt Macy err = SET_ERROR(ENOENT); 3331*eda14cbcSMatt Macy if (err) { 3332*eda14cbcSMatt Macy if (parent) 3333*eda14cbcSMatt Macy dbuf_rele(parent, NULL); 3334*eda14cbcSMatt Macy return (err); 3335*eda14cbcSMatt Macy } 3336*eda14cbcSMatt Macy } 3337*eda14cbcSMatt Macy if (err && err != ENOENT) 3338*eda14cbcSMatt Macy return (err); 3339*eda14cbcSMatt Macy db = dbuf_create(dn, level, blkid, parent, bp); 3340*eda14cbcSMatt Macy } 3341*eda14cbcSMatt Macy 3342*eda14cbcSMatt Macy if (fail_uncached && db->db_state != DB_CACHED) { 3343*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3344*eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 3345*eda14cbcSMatt Macy } 3346*eda14cbcSMatt Macy 3347*eda14cbcSMatt Macy if (db->db_buf != NULL) { 3348*eda14cbcSMatt Macy arc_buf_access(db->db_buf); 3349*eda14cbcSMatt Macy ASSERT3P(db->db.db_data, ==, db->db_buf->b_data); 3350*eda14cbcSMatt Macy } 3351*eda14cbcSMatt Macy 3352*eda14cbcSMatt Macy ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf)); 3353*eda14cbcSMatt Macy 3354*eda14cbcSMatt Macy /* 3355*eda14cbcSMatt Macy * If this buffer is currently syncing out, and we are 3356*eda14cbcSMatt Macy * still referencing it from db_data, we need to make a copy 3357*eda14cbcSMatt Macy * of it in case we decide we want to dirty it again in this txg. 3358*eda14cbcSMatt Macy */ 3359*eda14cbcSMatt Macy if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 3360*eda14cbcSMatt Macy dn->dn_object != DMU_META_DNODE_OBJECT && 3361*eda14cbcSMatt Macy db->db_state == DB_CACHED && db->db_data_pending) { 3362*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = db->db_data_pending; 3363*eda14cbcSMatt Macy if (dr->dt.dl.dr_data == db->db_buf) 3364*eda14cbcSMatt Macy dbuf_hold_copy(dn, db); 3365*eda14cbcSMatt Macy } 3366*eda14cbcSMatt Macy 3367*eda14cbcSMatt Macy if (multilist_link_active(&db->db_cache_link)) { 3368*eda14cbcSMatt Macy ASSERT(zfs_refcount_is_zero(&db->db_holds)); 3369*eda14cbcSMatt Macy ASSERT(db->db_caching_status == DB_DBUF_CACHE || 3370*eda14cbcSMatt Macy db->db_caching_status == DB_DBUF_METADATA_CACHE); 3371*eda14cbcSMatt Macy 3372*eda14cbcSMatt Macy multilist_remove(dbuf_caches[db->db_caching_status].cache, db); 3373*eda14cbcSMatt Macy (void) zfs_refcount_remove_many( 3374*eda14cbcSMatt Macy &dbuf_caches[db->db_caching_status].size, 3375*eda14cbcSMatt Macy db->db.db_size, db); 3376*eda14cbcSMatt Macy 3377*eda14cbcSMatt Macy if (db->db_caching_status == DB_DBUF_METADATA_CACHE) { 3378*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(metadata_cache_count); 3379*eda14cbcSMatt Macy } else { 3380*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 3381*eda14cbcSMatt Macy DBUF_STAT_BUMPDOWN(cache_count); 3382*eda14cbcSMatt Macy DBUF_STAT_DECR(cache_levels_bytes[db->db_level], 3383*eda14cbcSMatt Macy db->db.db_size); 3384*eda14cbcSMatt Macy } 3385*eda14cbcSMatt Macy db->db_caching_status = DB_NO_CACHE; 3386*eda14cbcSMatt Macy } 3387*eda14cbcSMatt Macy (void) zfs_refcount_add(&db->db_holds, tag); 3388*eda14cbcSMatt Macy DBUF_VERIFY(db); 3389*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3390*eda14cbcSMatt Macy 3391*eda14cbcSMatt Macy /* NOTE: we can't rele the parent until after we drop the db_mtx */ 3392*eda14cbcSMatt Macy if (parent) 3393*eda14cbcSMatt Macy dbuf_rele(parent, NULL); 3394*eda14cbcSMatt Macy 3395*eda14cbcSMatt Macy ASSERT3P(DB_DNODE(db), ==, dn); 3396*eda14cbcSMatt Macy ASSERT3U(db->db_blkid, ==, blkid); 3397*eda14cbcSMatt Macy ASSERT3U(db->db_level, ==, level); 3398*eda14cbcSMatt Macy *dbp = db; 3399*eda14cbcSMatt Macy 3400*eda14cbcSMatt Macy return (0); 3401*eda14cbcSMatt Macy } 3402*eda14cbcSMatt Macy 3403*eda14cbcSMatt Macy dmu_buf_impl_t * 3404*eda14cbcSMatt Macy dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag) 3405*eda14cbcSMatt Macy { 3406*eda14cbcSMatt Macy return (dbuf_hold_level(dn, 0, blkid, tag)); 3407*eda14cbcSMatt Macy } 3408*eda14cbcSMatt Macy 3409*eda14cbcSMatt Macy dmu_buf_impl_t * 3410*eda14cbcSMatt Macy dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag) 3411*eda14cbcSMatt Macy { 3412*eda14cbcSMatt Macy dmu_buf_impl_t *db; 3413*eda14cbcSMatt Macy int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db); 3414*eda14cbcSMatt Macy return (err ? NULL : db); 3415*eda14cbcSMatt Macy } 3416*eda14cbcSMatt Macy 3417*eda14cbcSMatt Macy void 3418*eda14cbcSMatt Macy dbuf_create_bonus(dnode_t *dn) 3419*eda14cbcSMatt Macy { 3420*eda14cbcSMatt Macy ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 3421*eda14cbcSMatt Macy 3422*eda14cbcSMatt Macy ASSERT(dn->dn_bonus == NULL); 3423*eda14cbcSMatt Macy dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL); 3424*eda14cbcSMatt Macy } 3425*eda14cbcSMatt Macy 3426*eda14cbcSMatt Macy int 3427*eda14cbcSMatt Macy dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx) 3428*eda14cbcSMatt Macy { 3429*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3430*eda14cbcSMatt Macy 3431*eda14cbcSMatt Macy if (db->db_blkid != DMU_SPILL_BLKID) 3432*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 3433*eda14cbcSMatt Macy if (blksz == 0) 3434*eda14cbcSMatt Macy blksz = SPA_MINBLOCKSIZE; 3435*eda14cbcSMatt Macy ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset))); 3436*eda14cbcSMatt Macy blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE); 3437*eda14cbcSMatt Macy 3438*eda14cbcSMatt Macy dbuf_new_size(db, blksz, tx); 3439*eda14cbcSMatt Macy 3440*eda14cbcSMatt Macy return (0); 3441*eda14cbcSMatt Macy } 3442*eda14cbcSMatt Macy 3443*eda14cbcSMatt Macy void 3444*eda14cbcSMatt Macy dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx) 3445*eda14cbcSMatt Macy { 3446*eda14cbcSMatt Macy dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx); 3447*eda14cbcSMatt Macy } 3448*eda14cbcSMatt Macy 3449*eda14cbcSMatt Macy #pragma weak dmu_buf_add_ref = dbuf_add_ref 3450*eda14cbcSMatt Macy void 3451*eda14cbcSMatt Macy dbuf_add_ref(dmu_buf_impl_t *db, void *tag) 3452*eda14cbcSMatt Macy { 3453*eda14cbcSMatt Macy int64_t holds = zfs_refcount_add(&db->db_holds, tag); 3454*eda14cbcSMatt Macy VERIFY3S(holds, >, 1); 3455*eda14cbcSMatt Macy } 3456*eda14cbcSMatt Macy 3457*eda14cbcSMatt Macy #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref 3458*eda14cbcSMatt Macy boolean_t 3459*eda14cbcSMatt Macy dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid, 3460*eda14cbcSMatt Macy void *tag) 3461*eda14cbcSMatt Macy { 3462*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3463*eda14cbcSMatt Macy dmu_buf_impl_t *found_db; 3464*eda14cbcSMatt Macy boolean_t result = B_FALSE; 3465*eda14cbcSMatt Macy 3466*eda14cbcSMatt Macy if (blkid == DMU_BONUS_BLKID) 3467*eda14cbcSMatt Macy found_db = dbuf_find_bonus(os, obj); 3468*eda14cbcSMatt Macy else 3469*eda14cbcSMatt Macy found_db = dbuf_find(os, obj, 0, blkid); 3470*eda14cbcSMatt Macy 3471*eda14cbcSMatt Macy if (found_db != NULL) { 3472*eda14cbcSMatt Macy if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) { 3473*eda14cbcSMatt Macy (void) zfs_refcount_add(&db->db_holds, tag); 3474*eda14cbcSMatt Macy result = B_TRUE; 3475*eda14cbcSMatt Macy } 3476*eda14cbcSMatt Macy mutex_exit(&found_db->db_mtx); 3477*eda14cbcSMatt Macy } 3478*eda14cbcSMatt Macy return (result); 3479*eda14cbcSMatt Macy } 3480*eda14cbcSMatt Macy 3481*eda14cbcSMatt Macy /* 3482*eda14cbcSMatt Macy * If you call dbuf_rele() you had better not be referencing the dnode handle 3483*eda14cbcSMatt Macy * unless you have some other direct or indirect hold on the dnode. (An indirect 3484*eda14cbcSMatt Macy * hold is a hold on one of the dnode's dbufs, including the bonus buffer.) 3485*eda14cbcSMatt Macy * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the 3486*eda14cbcSMatt Macy * dnode's parent dbuf evicting its dnode handles. 3487*eda14cbcSMatt Macy */ 3488*eda14cbcSMatt Macy void 3489*eda14cbcSMatt Macy dbuf_rele(dmu_buf_impl_t *db, void *tag) 3490*eda14cbcSMatt Macy { 3491*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3492*eda14cbcSMatt Macy dbuf_rele_and_unlock(db, tag, B_FALSE); 3493*eda14cbcSMatt Macy } 3494*eda14cbcSMatt Macy 3495*eda14cbcSMatt Macy void 3496*eda14cbcSMatt Macy dmu_buf_rele(dmu_buf_t *db, void *tag) 3497*eda14cbcSMatt Macy { 3498*eda14cbcSMatt Macy dbuf_rele((dmu_buf_impl_t *)db, tag); 3499*eda14cbcSMatt Macy } 3500*eda14cbcSMatt Macy 3501*eda14cbcSMatt Macy /* 3502*eda14cbcSMatt Macy * dbuf_rele() for an already-locked dbuf. This is necessary to allow 3503*eda14cbcSMatt Macy * db_dirtycnt and db_holds to be updated atomically. The 'evicting' 3504*eda14cbcSMatt Macy * argument should be set if we are already in the dbuf-evicting code 3505*eda14cbcSMatt Macy * path, in which case we don't want to recursively evict. This allows us to 3506*eda14cbcSMatt Macy * avoid deeply nested stacks that would have a call flow similar to this: 3507*eda14cbcSMatt Macy * 3508*eda14cbcSMatt Macy * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify() 3509*eda14cbcSMatt Macy * ^ | 3510*eda14cbcSMatt Macy * | | 3511*eda14cbcSMatt Macy * +-----dbuf_destroy()<--dbuf_evict_one()<--------+ 3512*eda14cbcSMatt Macy * 3513*eda14cbcSMatt Macy */ 3514*eda14cbcSMatt Macy void 3515*eda14cbcSMatt Macy dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag, boolean_t evicting) 3516*eda14cbcSMatt Macy { 3517*eda14cbcSMatt Macy int64_t holds; 3518*eda14cbcSMatt Macy uint64_t size; 3519*eda14cbcSMatt Macy 3520*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 3521*eda14cbcSMatt Macy DBUF_VERIFY(db); 3522*eda14cbcSMatt Macy 3523*eda14cbcSMatt Macy /* 3524*eda14cbcSMatt Macy * Remove the reference to the dbuf before removing its hold on the 3525*eda14cbcSMatt Macy * dnode so we can guarantee in dnode_move() that a referenced bonus 3526*eda14cbcSMatt Macy * buffer has a corresponding dnode hold. 3527*eda14cbcSMatt Macy */ 3528*eda14cbcSMatt Macy holds = zfs_refcount_remove(&db->db_holds, tag); 3529*eda14cbcSMatt Macy ASSERT(holds >= 0); 3530*eda14cbcSMatt Macy 3531*eda14cbcSMatt Macy /* 3532*eda14cbcSMatt Macy * We can't freeze indirects if there is a possibility that they 3533*eda14cbcSMatt Macy * may be modified in the current syncing context. 3534*eda14cbcSMatt Macy */ 3535*eda14cbcSMatt Macy if (db->db_buf != NULL && 3536*eda14cbcSMatt Macy holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) { 3537*eda14cbcSMatt Macy arc_buf_freeze(db->db_buf); 3538*eda14cbcSMatt Macy } 3539*eda14cbcSMatt Macy 3540*eda14cbcSMatt Macy if (holds == db->db_dirtycnt && 3541*eda14cbcSMatt Macy db->db_level == 0 && db->db_user_immediate_evict) 3542*eda14cbcSMatt Macy dbuf_evict_user(db); 3543*eda14cbcSMatt Macy 3544*eda14cbcSMatt Macy if (holds == 0) { 3545*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 3546*eda14cbcSMatt Macy dnode_t *dn; 3547*eda14cbcSMatt Macy boolean_t evict_dbuf = db->db_pending_evict; 3548*eda14cbcSMatt Macy 3549*eda14cbcSMatt Macy /* 3550*eda14cbcSMatt Macy * If the dnode moves here, we cannot cross this 3551*eda14cbcSMatt Macy * barrier until the move completes. 3552*eda14cbcSMatt Macy */ 3553*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 3554*eda14cbcSMatt Macy 3555*eda14cbcSMatt Macy dn = DB_DNODE(db); 3556*eda14cbcSMatt Macy atomic_dec_32(&dn->dn_dbufs_count); 3557*eda14cbcSMatt Macy 3558*eda14cbcSMatt Macy /* 3559*eda14cbcSMatt Macy * Decrementing the dbuf count means that the bonus 3560*eda14cbcSMatt Macy * buffer's dnode hold is no longer discounted in 3561*eda14cbcSMatt Macy * dnode_move(). The dnode cannot move until after 3562*eda14cbcSMatt Macy * the dnode_rele() below. 3563*eda14cbcSMatt Macy */ 3564*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 3565*eda14cbcSMatt Macy 3566*eda14cbcSMatt Macy /* 3567*eda14cbcSMatt Macy * Do not reference db after its lock is dropped. 3568*eda14cbcSMatt Macy * Another thread may evict it. 3569*eda14cbcSMatt Macy */ 3570*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3571*eda14cbcSMatt Macy 3572*eda14cbcSMatt Macy if (evict_dbuf) 3573*eda14cbcSMatt Macy dnode_evict_bonus(dn); 3574*eda14cbcSMatt Macy 3575*eda14cbcSMatt Macy dnode_rele(dn, db); 3576*eda14cbcSMatt Macy } else if (db->db_buf == NULL) { 3577*eda14cbcSMatt Macy /* 3578*eda14cbcSMatt Macy * This is a special case: we never associated this 3579*eda14cbcSMatt Macy * dbuf with any data allocated from the ARC. 3580*eda14cbcSMatt Macy */ 3581*eda14cbcSMatt Macy ASSERT(db->db_state == DB_UNCACHED || 3582*eda14cbcSMatt Macy db->db_state == DB_NOFILL); 3583*eda14cbcSMatt Macy dbuf_destroy(db); 3584*eda14cbcSMatt Macy } else if (arc_released(db->db_buf)) { 3585*eda14cbcSMatt Macy /* 3586*eda14cbcSMatt Macy * This dbuf has anonymous data associated with it. 3587*eda14cbcSMatt Macy */ 3588*eda14cbcSMatt Macy dbuf_destroy(db); 3589*eda14cbcSMatt Macy } else { 3590*eda14cbcSMatt Macy boolean_t do_arc_evict = B_FALSE; 3591*eda14cbcSMatt Macy blkptr_t bp; 3592*eda14cbcSMatt Macy spa_t *spa = dmu_objset_spa(db->db_objset); 3593*eda14cbcSMatt Macy 3594*eda14cbcSMatt Macy if (!DBUF_IS_CACHEABLE(db) && 3595*eda14cbcSMatt Macy db->db_blkptr != NULL && 3596*eda14cbcSMatt Macy !BP_IS_HOLE(db->db_blkptr) && 3597*eda14cbcSMatt Macy !BP_IS_EMBEDDED(db->db_blkptr)) { 3598*eda14cbcSMatt Macy do_arc_evict = B_TRUE; 3599*eda14cbcSMatt Macy bp = *db->db_blkptr; 3600*eda14cbcSMatt Macy } 3601*eda14cbcSMatt Macy 3602*eda14cbcSMatt Macy if (!DBUF_IS_CACHEABLE(db) || 3603*eda14cbcSMatt Macy db->db_pending_evict) { 3604*eda14cbcSMatt Macy dbuf_destroy(db); 3605*eda14cbcSMatt Macy } else if (!multilist_link_active(&db->db_cache_link)) { 3606*eda14cbcSMatt Macy ASSERT3U(db->db_caching_status, ==, 3607*eda14cbcSMatt Macy DB_NO_CACHE); 3608*eda14cbcSMatt Macy 3609*eda14cbcSMatt Macy dbuf_cached_state_t dcs = 3610*eda14cbcSMatt Macy dbuf_include_in_metadata_cache(db) ? 3611*eda14cbcSMatt Macy DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE; 3612*eda14cbcSMatt Macy db->db_caching_status = dcs; 3613*eda14cbcSMatt Macy 3614*eda14cbcSMatt Macy multilist_insert(dbuf_caches[dcs].cache, db); 3615*eda14cbcSMatt Macy size = zfs_refcount_add_many( 3616*eda14cbcSMatt Macy &dbuf_caches[dcs].size, 3617*eda14cbcSMatt Macy db->db.db_size, db); 3618*eda14cbcSMatt Macy 3619*eda14cbcSMatt Macy if (dcs == DB_DBUF_METADATA_CACHE) { 3620*eda14cbcSMatt Macy DBUF_STAT_BUMP(metadata_cache_count); 3621*eda14cbcSMatt Macy DBUF_STAT_MAX( 3622*eda14cbcSMatt Macy metadata_cache_size_bytes_max, 3623*eda14cbcSMatt Macy size); 3624*eda14cbcSMatt Macy } else { 3625*eda14cbcSMatt Macy DBUF_STAT_BUMP( 3626*eda14cbcSMatt Macy cache_levels[db->db_level]); 3627*eda14cbcSMatt Macy DBUF_STAT_BUMP(cache_count); 3628*eda14cbcSMatt Macy DBUF_STAT_INCR( 3629*eda14cbcSMatt Macy cache_levels_bytes[db->db_level], 3630*eda14cbcSMatt Macy db->db.db_size); 3631*eda14cbcSMatt Macy DBUF_STAT_MAX(cache_size_bytes_max, 3632*eda14cbcSMatt Macy size); 3633*eda14cbcSMatt Macy } 3634*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3635*eda14cbcSMatt Macy 3636*eda14cbcSMatt Macy if (dcs == DB_DBUF_CACHE && !evicting) 3637*eda14cbcSMatt Macy dbuf_evict_notify(size); 3638*eda14cbcSMatt Macy } 3639*eda14cbcSMatt Macy 3640*eda14cbcSMatt Macy if (do_arc_evict) 3641*eda14cbcSMatt Macy arc_freed(spa, &bp); 3642*eda14cbcSMatt Macy } 3643*eda14cbcSMatt Macy } else { 3644*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3645*eda14cbcSMatt Macy } 3646*eda14cbcSMatt Macy 3647*eda14cbcSMatt Macy } 3648*eda14cbcSMatt Macy 3649*eda14cbcSMatt Macy #pragma weak dmu_buf_refcount = dbuf_refcount 3650*eda14cbcSMatt Macy uint64_t 3651*eda14cbcSMatt Macy dbuf_refcount(dmu_buf_impl_t *db) 3652*eda14cbcSMatt Macy { 3653*eda14cbcSMatt Macy return (zfs_refcount_count(&db->db_holds)); 3654*eda14cbcSMatt Macy } 3655*eda14cbcSMatt Macy 3656*eda14cbcSMatt Macy uint64_t 3657*eda14cbcSMatt Macy dmu_buf_user_refcount(dmu_buf_t *db_fake) 3658*eda14cbcSMatt Macy { 3659*eda14cbcSMatt Macy uint64_t holds; 3660*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3661*eda14cbcSMatt Macy 3662*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3663*eda14cbcSMatt Macy ASSERT3U(zfs_refcount_count(&db->db_holds), >=, db->db_dirtycnt); 3664*eda14cbcSMatt Macy holds = zfs_refcount_count(&db->db_holds) - db->db_dirtycnt; 3665*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3666*eda14cbcSMatt Macy 3667*eda14cbcSMatt Macy return (holds); 3668*eda14cbcSMatt Macy } 3669*eda14cbcSMatt Macy 3670*eda14cbcSMatt Macy void * 3671*eda14cbcSMatt Macy dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user, 3672*eda14cbcSMatt Macy dmu_buf_user_t *new_user) 3673*eda14cbcSMatt Macy { 3674*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3675*eda14cbcSMatt Macy 3676*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3677*eda14cbcSMatt Macy dbuf_verify_user(db, DBVU_NOT_EVICTING); 3678*eda14cbcSMatt Macy if (db->db_user == old_user) 3679*eda14cbcSMatt Macy db->db_user = new_user; 3680*eda14cbcSMatt Macy else 3681*eda14cbcSMatt Macy old_user = db->db_user; 3682*eda14cbcSMatt Macy dbuf_verify_user(db, DBVU_NOT_EVICTING); 3683*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3684*eda14cbcSMatt Macy 3685*eda14cbcSMatt Macy return (old_user); 3686*eda14cbcSMatt Macy } 3687*eda14cbcSMatt Macy 3688*eda14cbcSMatt Macy void * 3689*eda14cbcSMatt Macy dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 3690*eda14cbcSMatt Macy { 3691*eda14cbcSMatt Macy return (dmu_buf_replace_user(db_fake, NULL, user)); 3692*eda14cbcSMatt Macy } 3693*eda14cbcSMatt Macy 3694*eda14cbcSMatt Macy void * 3695*eda14cbcSMatt Macy dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user) 3696*eda14cbcSMatt Macy { 3697*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3698*eda14cbcSMatt Macy 3699*eda14cbcSMatt Macy db->db_user_immediate_evict = TRUE; 3700*eda14cbcSMatt Macy return (dmu_buf_set_user(db_fake, user)); 3701*eda14cbcSMatt Macy } 3702*eda14cbcSMatt Macy 3703*eda14cbcSMatt Macy void * 3704*eda14cbcSMatt Macy dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 3705*eda14cbcSMatt Macy { 3706*eda14cbcSMatt Macy return (dmu_buf_replace_user(db_fake, user, NULL)); 3707*eda14cbcSMatt Macy } 3708*eda14cbcSMatt Macy 3709*eda14cbcSMatt Macy void * 3710*eda14cbcSMatt Macy dmu_buf_get_user(dmu_buf_t *db_fake) 3711*eda14cbcSMatt Macy { 3712*eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3713*eda14cbcSMatt Macy 3714*eda14cbcSMatt Macy dbuf_verify_user(db, DBVU_NOT_EVICTING); 3715*eda14cbcSMatt Macy return (db->db_user); 3716*eda14cbcSMatt Macy } 3717*eda14cbcSMatt Macy 3718*eda14cbcSMatt Macy void 3719*eda14cbcSMatt Macy dmu_buf_user_evict_wait() 3720*eda14cbcSMatt Macy { 3721*eda14cbcSMatt Macy taskq_wait(dbu_evict_taskq); 3722*eda14cbcSMatt Macy } 3723*eda14cbcSMatt Macy 3724*eda14cbcSMatt Macy blkptr_t * 3725*eda14cbcSMatt Macy dmu_buf_get_blkptr(dmu_buf_t *db) 3726*eda14cbcSMatt Macy { 3727*eda14cbcSMatt Macy dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3728*eda14cbcSMatt Macy return (dbi->db_blkptr); 3729*eda14cbcSMatt Macy } 3730*eda14cbcSMatt Macy 3731*eda14cbcSMatt Macy objset_t * 3732*eda14cbcSMatt Macy dmu_buf_get_objset(dmu_buf_t *db) 3733*eda14cbcSMatt Macy { 3734*eda14cbcSMatt Macy dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3735*eda14cbcSMatt Macy return (dbi->db_objset); 3736*eda14cbcSMatt Macy } 3737*eda14cbcSMatt Macy 3738*eda14cbcSMatt Macy dnode_t * 3739*eda14cbcSMatt Macy dmu_buf_dnode_enter(dmu_buf_t *db) 3740*eda14cbcSMatt Macy { 3741*eda14cbcSMatt Macy dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3742*eda14cbcSMatt Macy DB_DNODE_ENTER(dbi); 3743*eda14cbcSMatt Macy return (DB_DNODE(dbi)); 3744*eda14cbcSMatt Macy } 3745*eda14cbcSMatt Macy 3746*eda14cbcSMatt Macy void 3747*eda14cbcSMatt Macy dmu_buf_dnode_exit(dmu_buf_t *db) 3748*eda14cbcSMatt Macy { 3749*eda14cbcSMatt Macy dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3750*eda14cbcSMatt Macy DB_DNODE_EXIT(dbi); 3751*eda14cbcSMatt Macy } 3752*eda14cbcSMatt Macy 3753*eda14cbcSMatt Macy static void 3754*eda14cbcSMatt Macy dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db) 3755*eda14cbcSMatt Macy { 3756*eda14cbcSMatt Macy /* ASSERT(dmu_tx_is_syncing(tx) */ 3757*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 3758*eda14cbcSMatt Macy 3759*eda14cbcSMatt Macy if (db->db_blkptr != NULL) 3760*eda14cbcSMatt Macy return; 3761*eda14cbcSMatt Macy 3762*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) { 3763*eda14cbcSMatt Macy db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys); 3764*eda14cbcSMatt Macy BP_ZERO(db->db_blkptr); 3765*eda14cbcSMatt Macy return; 3766*eda14cbcSMatt Macy } 3767*eda14cbcSMatt Macy if (db->db_level == dn->dn_phys->dn_nlevels-1) { 3768*eda14cbcSMatt Macy /* 3769*eda14cbcSMatt Macy * This buffer was allocated at a time when there was 3770*eda14cbcSMatt Macy * no available blkptrs from the dnode, or it was 3771*eda14cbcSMatt Macy * inappropriate to hook it in (i.e., nlevels mismatch). 3772*eda14cbcSMatt Macy */ 3773*eda14cbcSMatt Macy ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr); 3774*eda14cbcSMatt Macy ASSERT(db->db_parent == NULL); 3775*eda14cbcSMatt Macy db->db_parent = dn->dn_dbuf; 3776*eda14cbcSMatt Macy db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid]; 3777*eda14cbcSMatt Macy DBUF_VERIFY(db); 3778*eda14cbcSMatt Macy } else { 3779*eda14cbcSMatt Macy dmu_buf_impl_t *parent = db->db_parent; 3780*eda14cbcSMatt Macy int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3781*eda14cbcSMatt Macy 3782*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_nlevels > 1); 3783*eda14cbcSMatt Macy if (parent == NULL) { 3784*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3785*eda14cbcSMatt Macy rw_enter(&dn->dn_struct_rwlock, RW_READER); 3786*eda14cbcSMatt Macy parent = dbuf_hold_level(dn, db->db_level + 1, 3787*eda14cbcSMatt Macy db->db_blkid >> epbs, db); 3788*eda14cbcSMatt Macy rw_exit(&dn->dn_struct_rwlock); 3789*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3790*eda14cbcSMatt Macy db->db_parent = parent; 3791*eda14cbcSMatt Macy } 3792*eda14cbcSMatt Macy db->db_blkptr = (blkptr_t *)parent->db.db_data + 3793*eda14cbcSMatt Macy (db->db_blkid & ((1ULL << epbs) - 1)); 3794*eda14cbcSMatt Macy DBUF_VERIFY(db); 3795*eda14cbcSMatt Macy } 3796*eda14cbcSMatt Macy } 3797*eda14cbcSMatt Macy 3798*eda14cbcSMatt Macy static void 3799*eda14cbcSMatt Macy dbuf_sync_bonus(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 3800*eda14cbcSMatt Macy { 3801*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 3802*eda14cbcSMatt Macy void *data = dr->dt.dl.dr_data; 3803*eda14cbcSMatt Macy 3804*eda14cbcSMatt Macy ASSERT0(db->db_level); 3805*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 3806*eda14cbcSMatt Macy ASSERT(DB_DNODE_HELD(db)); 3807*eda14cbcSMatt Macy ASSERT(db->db_blkid == DMU_BONUS_BLKID); 3808*eda14cbcSMatt Macy ASSERT(data != NULL); 3809*eda14cbcSMatt Macy 3810*eda14cbcSMatt Macy dnode_t *dn = DB_DNODE(db); 3811*eda14cbcSMatt Macy ASSERT3U(DN_MAX_BONUS_LEN(dn->dn_phys), <=, 3812*eda14cbcSMatt Macy DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1)); 3813*eda14cbcSMatt Macy bcopy(data, DN_BONUS(dn->dn_phys), DN_MAX_BONUS_LEN(dn->dn_phys)); 3814*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 3815*eda14cbcSMatt Macy 3816*eda14cbcSMatt Macy dbuf_sync_leaf_verify_bonus_dnode(dr); 3817*eda14cbcSMatt Macy 3818*eda14cbcSMatt Macy dbuf_undirty_bonus(dr); 3819*eda14cbcSMatt Macy dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE); 3820*eda14cbcSMatt Macy } 3821*eda14cbcSMatt Macy 3822*eda14cbcSMatt Macy /* 3823*eda14cbcSMatt Macy * When syncing out a blocks of dnodes, adjust the block to deal with 3824*eda14cbcSMatt Macy * encryption. Normally, we make sure the block is decrypted before writing 3825*eda14cbcSMatt Macy * it. If we have crypt params, then we are writing a raw (encrypted) block, 3826*eda14cbcSMatt Macy * from a raw receive. In this case, set the ARC buf's crypt params so 3827*eda14cbcSMatt Macy * that the BP will be filled with the correct byteorder, salt, iv, and mac. 3828*eda14cbcSMatt Macy */ 3829*eda14cbcSMatt Macy static void 3830*eda14cbcSMatt Macy dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t *dr) 3831*eda14cbcSMatt Macy { 3832*eda14cbcSMatt Macy int err; 3833*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 3834*eda14cbcSMatt Macy 3835*eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&db->db_mtx)); 3836*eda14cbcSMatt Macy ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT); 3837*eda14cbcSMatt Macy ASSERT3U(db->db_level, ==, 0); 3838*eda14cbcSMatt Macy 3839*eda14cbcSMatt Macy if (!db->db_objset->os_raw_receive && arc_is_encrypted(db->db_buf)) { 3840*eda14cbcSMatt Macy zbookmark_phys_t zb; 3841*eda14cbcSMatt Macy 3842*eda14cbcSMatt Macy /* 3843*eda14cbcSMatt Macy * Unfortunately, there is currently no mechanism for 3844*eda14cbcSMatt Macy * syncing context to handle decryption errors. An error 3845*eda14cbcSMatt Macy * here is only possible if an attacker maliciously 3846*eda14cbcSMatt Macy * changed a dnode block and updated the associated 3847*eda14cbcSMatt Macy * checksums going up the block tree. 3848*eda14cbcSMatt Macy */ 3849*eda14cbcSMatt Macy SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 3850*eda14cbcSMatt Macy db->db.db_object, db->db_level, db->db_blkid); 3851*eda14cbcSMatt Macy err = arc_untransform(db->db_buf, db->db_objset->os_spa, 3852*eda14cbcSMatt Macy &zb, B_TRUE); 3853*eda14cbcSMatt Macy if (err) 3854*eda14cbcSMatt Macy panic("Invalid dnode block MAC"); 3855*eda14cbcSMatt Macy } else if (dr->dt.dl.dr_has_raw_params) { 3856*eda14cbcSMatt Macy (void) arc_release(dr->dt.dl.dr_data, db); 3857*eda14cbcSMatt Macy arc_convert_to_raw(dr->dt.dl.dr_data, 3858*eda14cbcSMatt Macy dmu_objset_id(db->db_objset), 3859*eda14cbcSMatt Macy dr->dt.dl.dr_byteorder, DMU_OT_DNODE, 3860*eda14cbcSMatt Macy dr->dt.dl.dr_salt, dr->dt.dl.dr_iv, dr->dt.dl.dr_mac); 3861*eda14cbcSMatt Macy } 3862*eda14cbcSMatt Macy } 3863*eda14cbcSMatt Macy 3864*eda14cbcSMatt Macy /* 3865*eda14cbcSMatt Macy * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it 3866*eda14cbcSMatt Macy * is critical the we not allow the compiler to inline this function in to 3867*eda14cbcSMatt Macy * dbuf_sync_list() thereby drastically bloating the stack usage. 3868*eda14cbcSMatt Macy */ 3869*eda14cbcSMatt Macy noinline static void 3870*eda14cbcSMatt Macy dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 3871*eda14cbcSMatt Macy { 3872*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 3873*eda14cbcSMatt Macy dnode_t *dn; 3874*eda14cbcSMatt Macy zio_t *zio; 3875*eda14cbcSMatt Macy 3876*eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 3877*eda14cbcSMatt Macy 3878*eda14cbcSMatt Macy dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 3879*eda14cbcSMatt Macy 3880*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3881*eda14cbcSMatt Macy 3882*eda14cbcSMatt Macy ASSERT(db->db_level > 0); 3883*eda14cbcSMatt Macy DBUF_VERIFY(db); 3884*eda14cbcSMatt Macy 3885*eda14cbcSMatt Macy /* Read the block if it hasn't been read yet. */ 3886*eda14cbcSMatt Macy if (db->db_buf == NULL) { 3887*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3888*eda14cbcSMatt Macy (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 3889*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3890*eda14cbcSMatt Macy } 3891*eda14cbcSMatt Macy ASSERT3U(db->db_state, ==, DB_CACHED); 3892*eda14cbcSMatt Macy ASSERT(db->db_buf != NULL); 3893*eda14cbcSMatt Macy 3894*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 3895*eda14cbcSMatt Macy dn = DB_DNODE(db); 3896*eda14cbcSMatt Macy /* Indirect block size must match what the dnode thinks it is. */ 3897*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 3898*eda14cbcSMatt Macy dbuf_check_blkptr(dn, db); 3899*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 3900*eda14cbcSMatt Macy 3901*eda14cbcSMatt Macy /* Provide the pending dirty record to child dbufs */ 3902*eda14cbcSMatt Macy db->db_data_pending = dr; 3903*eda14cbcSMatt Macy 3904*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 3905*eda14cbcSMatt Macy 3906*eda14cbcSMatt Macy dbuf_write(dr, db->db_buf, tx); 3907*eda14cbcSMatt Macy 3908*eda14cbcSMatt Macy zio = dr->dr_zio; 3909*eda14cbcSMatt Macy mutex_enter(&dr->dt.di.dr_mtx); 3910*eda14cbcSMatt Macy dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx); 3911*eda14cbcSMatt Macy ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 3912*eda14cbcSMatt Macy mutex_exit(&dr->dt.di.dr_mtx); 3913*eda14cbcSMatt Macy zio_nowait(zio); 3914*eda14cbcSMatt Macy } 3915*eda14cbcSMatt Macy 3916*eda14cbcSMatt Macy /* 3917*eda14cbcSMatt Macy * Verify that the size of the data in our bonus buffer does not exceed 3918*eda14cbcSMatt Macy * its recorded size. 3919*eda14cbcSMatt Macy * 3920*eda14cbcSMatt Macy * The purpose of this verification is to catch any cases in development 3921*eda14cbcSMatt Macy * where the size of a phys structure (i.e space_map_phys_t) grows and, 3922*eda14cbcSMatt Macy * due to incorrect feature management, older pools expect to read more 3923*eda14cbcSMatt Macy * data even though they didn't actually write it to begin with. 3924*eda14cbcSMatt Macy * 3925*eda14cbcSMatt Macy * For a example, this would catch an error in the feature logic where we 3926*eda14cbcSMatt Macy * open an older pool and we expect to write the space map histogram of 3927*eda14cbcSMatt Macy * a space map with size SPACE_MAP_SIZE_V0. 3928*eda14cbcSMatt Macy */ 3929*eda14cbcSMatt Macy static void 3930*eda14cbcSMatt Macy dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr) 3931*eda14cbcSMatt Macy { 3932*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 3933*eda14cbcSMatt Macy dnode_t *dn = DB_DNODE(dr->dr_dbuf); 3934*eda14cbcSMatt Macy 3935*eda14cbcSMatt Macy /* 3936*eda14cbcSMatt Macy * Encrypted bonus buffers can have data past their bonuslen. 3937*eda14cbcSMatt Macy * Skip the verification of these blocks. 3938*eda14cbcSMatt Macy */ 3939*eda14cbcSMatt Macy if (DMU_OT_IS_ENCRYPTED(dn->dn_bonustype)) 3940*eda14cbcSMatt Macy return; 3941*eda14cbcSMatt Macy 3942*eda14cbcSMatt Macy uint16_t bonuslen = dn->dn_phys->dn_bonuslen; 3943*eda14cbcSMatt Macy uint16_t maxbonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 3944*eda14cbcSMatt Macy ASSERT3U(bonuslen, <=, maxbonuslen); 3945*eda14cbcSMatt Macy 3946*eda14cbcSMatt Macy arc_buf_t *datap = dr->dt.dl.dr_data; 3947*eda14cbcSMatt Macy char *datap_end = ((char *)datap) + bonuslen; 3948*eda14cbcSMatt Macy char *datap_max = ((char *)datap) + maxbonuslen; 3949*eda14cbcSMatt Macy 3950*eda14cbcSMatt Macy /* ensure that everything is zero after our data */ 3951*eda14cbcSMatt Macy for (; datap_end < datap_max; datap_end++) 3952*eda14cbcSMatt Macy ASSERT(*datap_end == 0); 3953*eda14cbcSMatt Macy #endif 3954*eda14cbcSMatt Macy } 3955*eda14cbcSMatt Macy 3956*eda14cbcSMatt Macy /* 3957*eda14cbcSMatt Macy * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is 3958*eda14cbcSMatt Macy * critical the we not allow the compiler to inline this function in to 3959*eda14cbcSMatt Macy * dbuf_sync_list() thereby drastically bloating the stack usage. 3960*eda14cbcSMatt Macy */ 3961*eda14cbcSMatt Macy noinline static void 3962*eda14cbcSMatt Macy dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 3963*eda14cbcSMatt Macy { 3964*eda14cbcSMatt Macy arc_buf_t **datap = &dr->dt.dl.dr_data; 3965*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 3966*eda14cbcSMatt Macy dnode_t *dn; 3967*eda14cbcSMatt Macy objset_t *os; 3968*eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 3969*eda14cbcSMatt Macy 3970*eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 3971*eda14cbcSMatt Macy 3972*eda14cbcSMatt Macy dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 3973*eda14cbcSMatt Macy 3974*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 3975*eda14cbcSMatt Macy /* 3976*eda14cbcSMatt Macy * To be synced, we must be dirtied. But we 3977*eda14cbcSMatt Macy * might have been freed after the dirty. 3978*eda14cbcSMatt Macy */ 3979*eda14cbcSMatt Macy if (db->db_state == DB_UNCACHED) { 3980*eda14cbcSMatt Macy /* This buffer has been freed since it was dirtied */ 3981*eda14cbcSMatt Macy ASSERT(db->db.db_data == NULL); 3982*eda14cbcSMatt Macy } else if (db->db_state == DB_FILL) { 3983*eda14cbcSMatt Macy /* This buffer was freed and is now being re-filled */ 3984*eda14cbcSMatt Macy ASSERT(db->db.db_data != dr->dt.dl.dr_data); 3985*eda14cbcSMatt Macy } else { 3986*eda14cbcSMatt Macy ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL); 3987*eda14cbcSMatt Macy } 3988*eda14cbcSMatt Macy DBUF_VERIFY(db); 3989*eda14cbcSMatt Macy 3990*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 3991*eda14cbcSMatt Macy dn = DB_DNODE(db); 3992*eda14cbcSMatt Macy 3993*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) { 3994*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 3995*eda14cbcSMatt Macy if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 3996*eda14cbcSMatt Macy /* 3997*eda14cbcSMatt Macy * In the previous transaction group, the bonus buffer 3998*eda14cbcSMatt Macy * was entirely used to store the attributes for the 3999*eda14cbcSMatt Macy * dnode which overrode the dn_spill field. However, 4000*eda14cbcSMatt Macy * when adding more attributes to the file a spill 4001*eda14cbcSMatt Macy * block was required to hold the extra attributes. 4002*eda14cbcSMatt Macy * 4003*eda14cbcSMatt Macy * Make sure to clear the garbage left in the dn_spill 4004*eda14cbcSMatt Macy * field from the previous attributes in the bonus 4005*eda14cbcSMatt Macy * buffer. Otherwise, after writing out the spill 4006*eda14cbcSMatt Macy * block to the new allocated dva, it will free 4007*eda14cbcSMatt Macy * the old block pointed to by the invalid dn_spill. 4008*eda14cbcSMatt Macy */ 4009*eda14cbcSMatt Macy db->db_blkptr = NULL; 4010*eda14cbcSMatt Macy } 4011*eda14cbcSMatt Macy dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR; 4012*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 4013*eda14cbcSMatt Macy } 4014*eda14cbcSMatt Macy 4015*eda14cbcSMatt Macy /* 4016*eda14cbcSMatt Macy * If this is a bonus buffer, simply copy the bonus data into the 4017*eda14cbcSMatt Macy * dnode. It will be written out when the dnode is synced (and it 4018*eda14cbcSMatt Macy * will be synced, since it must have been dirty for dbuf_sync to 4019*eda14cbcSMatt Macy * be called). 4020*eda14cbcSMatt Macy */ 4021*eda14cbcSMatt Macy if (db->db_blkid == DMU_BONUS_BLKID) { 4022*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf == db); 4023*eda14cbcSMatt Macy dbuf_sync_bonus(dr, tx); 4024*eda14cbcSMatt Macy return; 4025*eda14cbcSMatt Macy } 4026*eda14cbcSMatt Macy 4027*eda14cbcSMatt Macy os = dn->dn_objset; 4028*eda14cbcSMatt Macy 4029*eda14cbcSMatt Macy /* 4030*eda14cbcSMatt Macy * This function may have dropped the db_mtx lock allowing a dmu_sync 4031*eda14cbcSMatt Macy * operation to sneak in. As a result, we need to ensure that we 4032*eda14cbcSMatt Macy * don't check the dr_override_state until we have returned from 4033*eda14cbcSMatt Macy * dbuf_check_blkptr. 4034*eda14cbcSMatt Macy */ 4035*eda14cbcSMatt Macy dbuf_check_blkptr(dn, db); 4036*eda14cbcSMatt Macy 4037*eda14cbcSMatt Macy /* 4038*eda14cbcSMatt Macy * If this buffer is in the middle of an immediate write, 4039*eda14cbcSMatt Macy * wait for the synchronous IO to complete. 4040*eda14cbcSMatt Macy */ 4041*eda14cbcSMatt Macy while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 4042*eda14cbcSMatt Macy ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 4043*eda14cbcSMatt Macy cv_wait(&db->db_changed, &db->db_mtx); 4044*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN); 4045*eda14cbcSMatt Macy } 4046*eda14cbcSMatt Macy 4047*eda14cbcSMatt Macy /* 4048*eda14cbcSMatt Macy * If this is a dnode block, ensure it is appropriately encrypted 4049*eda14cbcSMatt Macy * or decrypted, depending on what we are writing to it this txg. 4050*eda14cbcSMatt Macy */ 4051*eda14cbcSMatt Macy if (os->os_encrypted && dn->dn_object == DMU_META_DNODE_OBJECT) 4052*eda14cbcSMatt Macy dbuf_prepare_encrypted_dnode_leaf(dr); 4053*eda14cbcSMatt Macy 4054*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL && 4055*eda14cbcSMatt Macy dn->dn_object != DMU_META_DNODE_OBJECT && 4056*eda14cbcSMatt Macy zfs_refcount_count(&db->db_holds) > 1 && 4057*eda14cbcSMatt Macy dr->dt.dl.dr_override_state != DR_OVERRIDDEN && 4058*eda14cbcSMatt Macy *datap == db->db_buf) { 4059*eda14cbcSMatt Macy /* 4060*eda14cbcSMatt Macy * If this buffer is currently "in use" (i.e., there 4061*eda14cbcSMatt Macy * are active holds and db_data still references it), 4062*eda14cbcSMatt Macy * then make a copy before we start the write so that 4063*eda14cbcSMatt Macy * any modifications from the open txg will not leak 4064*eda14cbcSMatt Macy * into this write. 4065*eda14cbcSMatt Macy * 4066*eda14cbcSMatt Macy * NOTE: this copy does not need to be made for 4067*eda14cbcSMatt Macy * objects only modified in the syncing context (e.g. 4068*eda14cbcSMatt Macy * DNONE_DNODE blocks). 4069*eda14cbcSMatt Macy */ 4070*eda14cbcSMatt Macy *datap = dbuf_alloc_arcbuf_from_arcbuf(db, db->db_buf); 4071*eda14cbcSMatt Macy bcopy(db->db.db_data, (*datap)->b_data, arc_buf_size(*datap)); 4072*eda14cbcSMatt Macy } 4073*eda14cbcSMatt Macy db->db_data_pending = dr; 4074*eda14cbcSMatt Macy 4075*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 4076*eda14cbcSMatt Macy 4077*eda14cbcSMatt Macy dbuf_write(dr, *datap, tx); 4078*eda14cbcSMatt Macy 4079*eda14cbcSMatt Macy ASSERT(!list_link_active(&dr->dr_dirty_node)); 4080*eda14cbcSMatt Macy if (dn->dn_object == DMU_META_DNODE_OBJECT) { 4081*eda14cbcSMatt Macy list_insert_tail(&dn->dn_dirty_records[txg & TXG_MASK], dr); 4082*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4083*eda14cbcSMatt Macy } else { 4084*eda14cbcSMatt Macy /* 4085*eda14cbcSMatt Macy * Although zio_nowait() does not "wait for an IO", it does 4086*eda14cbcSMatt Macy * initiate the IO. If this is an empty write it seems plausible 4087*eda14cbcSMatt Macy * that the IO could actually be completed before the nowait 4088*eda14cbcSMatt Macy * returns. We need to DB_DNODE_EXIT() first in case 4089*eda14cbcSMatt Macy * zio_nowait() invalidates the dbuf. 4090*eda14cbcSMatt Macy */ 4091*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4092*eda14cbcSMatt Macy zio_nowait(dr->dr_zio); 4093*eda14cbcSMatt Macy } 4094*eda14cbcSMatt Macy } 4095*eda14cbcSMatt Macy 4096*eda14cbcSMatt Macy void 4097*eda14cbcSMatt Macy dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx) 4098*eda14cbcSMatt Macy { 4099*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 4100*eda14cbcSMatt Macy 4101*eda14cbcSMatt Macy while ((dr = list_head(list))) { 4102*eda14cbcSMatt Macy if (dr->dr_zio != NULL) { 4103*eda14cbcSMatt Macy /* 4104*eda14cbcSMatt Macy * If we find an already initialized zio then we 4105*eda14cbcSMatt Macy * are processing the meta-dnode, and we have finished. 4106*eda14cbcSMatt Macy * The dbufs for all dnodes are put back on the list 4107*eda14cbcSMatt Macy * during processing, so that we can zio_wait() 4108*eda14cbcSMatt Macy * these IOs after initiating all child IOs. 4109*eda14cbcSMatt Macy */ 4110*eda14cbcSMatt Macy ASSERT3U(dr->dr_dbuf->db.db_object, ==, 4111*eda14cbcSMatt Macy DMU_META_DNODE_OBJECT); 4112*eda14cbcSMatt Macy break; 4113*eda14cbcSMatt Macy } 4114*eda14cbcSMatt Macy if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID && 4115*eda14cbcSMatt Macy dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) { 4116*eda14cbcSMatt Macy VERIFY3U(dr->dr_dbuf->db_level, ==, level); 4117*eda14cbcSMatt Macy } 4118*eda14cbcSMatt Macy list_remove(list, dr); 4119*eda14cbcSMatt Macy if (dr->dr_dbuf->db_level > 0) 4120*eda14cbcSMatt Macy dbuf_sync_indirect(dr, tx); 4121*eda14cbcSMatt Macy else 4122*eda14cbcSMatt Macy dbuf_sync_leaf(dr, tx); 4123*eda14cbcSMatt Macy } 4124*eda14cbcSMatt Macy } 4125*eda14cbcSMatt Macy 4126*eda14cbcSMatt Macy /* ARGSUSED */ 4127*eda14cbcSMatt Macy static void 4128*eda14cbcSMatt Macy dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 4129*eda14cbcSMatt Macy { 4130*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 4131*eda14cbcSMatt Macy dnode_t *dn; 4132*eda14cbcSMatt Macy blkptr_t *bp = zio->io_bp; 4133*eda14cbcSMatt Macy blkptr_t *bp_orig = &zio->io_bp_orig; 4134*eda14cbcSMatt Macy spa_t *spa = zio->io_spa; 4135*eda14cbcSMatt Macy int64_t delta; 4136*eda14cbcSMatt Macy uint64_t fill = 0; 4137*eda14cbcSMatt Macy int i; 4138*eda14cbcSMatt Macy 4139*eda14cbcSMatt Macy ASSERT3P(db->db_blkptr, !=, NULL); 4140*eda14cbcSMatt Macy ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp); 4141*eda14cbcSMatt Macy 4142*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 4143*eda14cbcSMatt Macy dn = DB_DNODE(db); 4144*eda14cbcSMatt Macy delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig); 4145*eda14cbcSMatt Macy dnode_diduse_space(dn, delta - zio->io_prev_space_delta); 4146*eda14cbcSMatt Macy zio->io_prev_space_delta = delta; 4147*eda14cbcSMatt Macy 4148*eda14cbcSMatt Macy if (bp->blk_birth != 0) { 4149*eda14cbcSMatt Macy ASSERT((db->db_blkid != DMU_SPILL_BLKID && 4150*eda14cbcSMatt Macy BP_GET_TYPE(bp) == dn->dn_type) || 4151*eda14cbcSMatt Macy (db->db_blkid == DMU_SPILL_BLKID && 4152*eda14cbcSMatt Macy BP_GET_TYPE(bp) == dn->dn_bonustype) || 4153*eda14cbcSMatt Macy BP_IS_EMBEDDED(bp)); 4154*eda14cbcSMatt Macy ASSERT(BP_GET_LEVEL(bp) == db->db_level); 4155*eda14cbcSMatt Macy } 4156*eda14cbcSMatt Macy 4157*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 4158*eda14cbcSMatt Macy 4159*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 4160*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) { 4161*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 4162*eda14cbcSMatt Macy ASSERT(!(BP_IS_HOLE(bp)) && 4163*eda14cbcSMatt Macy db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys)); 4164*eda14cbcSMatt Macy } 4165*eda14cbcSMatt Macy #endif 4166*eda14cbcSMatt Macy 4167*eda14cbcSMatt Macy if (db->db_level == 0) { 4168*eda14cbcSMatt Macy mutex_enter(&dn->dn_mtx); 4169*eda14cbcSMatt Macy if (db->db_blkid > dn->dn_phys->dn_maxblkid && 4170*eda14cbcSMatt Macy db->db_blkid != DMU_SPILL_BLKID) { 4171*eda14cbcSMatt Macy ASSERT0(db->db_objset->os_raw_receive); 4172*eda14cbcSMatt Macy dn->dn_phys->dn_maxblkid = db->db_blkid; 4173*eda14cbcSMatt Macy } 4174*eda14cbcSMatt Macy mutex_exit(&dn->dn_mtx); 4175*eda14cbcSMatt Macy 4176*eda14cbcSMatt Macy if (dn->dn_type == DMU_OT_DNODE) { 4177*eda14cbcSMatt Macy i = 0; 4178*eda14cbcSMatt Macy while (i < db->db.db_size) { 4179*eda14cbcSMatt Macy dnode_phys_t *dnp = 4180*eda14cbcSMatt Macy (void *)(((char *)db->db.db_data) + i); 4181*eda14cbcSMatt Macy 4182*eda14cbcSMatt Macy i += DNODE_MIN_SIZE; 4183*eda14cbcSMatt Macy if (dnp->dn_type != DMU_OT_NONE) { 4184*eda14cbcSMatt Macy fill++; 4185*eda14cbcSMatt Macy i += dnp->dn_extra_slots * 4186*eda14cbcSMatt Macy DNODE_MIN_SIZE; 4187*eda14cbcSMatt Macy } 4188*eda14cbcSMatt Macy } 4189*eda14cbcSMatt Macy } else { 4190*eda14cbcSMatt Macy if (BP_IS_HOLE(bp)) { 4191*eda14cbcSMatt Macy fill = 0; 4192*eda14cbcSMatt Macy } else { 4193*eda14cbcSMatt Macy fill = 1; 4194*eda14cbcSMatt Macy } 4195*eda14cbcSMatt Macy } 4196*eda14cbcSMatt Macy } else { 4197*eda14cbcSMatt Macy blkptr_t *ibp = db->db.db_data; 4198*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 4199*eda14cbcSMatt Macy for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) { 4200*eda14cbcSMatt Macy if (BP_IS_HOLE(ibp)) 4201*eda14cbcSMatt Macy continue; 4202*eda14cbcSMatt Macy fill += BP_GET_FILL(ibp); 4203*eda14cbcSMatt Macy } 4204*eda14cbcSMatt Macy } 4205*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4206*eda14cbcSMatt Macy 4207*eda14cbcSMatt Macy if (!BP_IS_EMBEDDED(bp)) 4208*eda14cbcSMatt Macy BP_SET_FILL(bp, fill); 4209*eda14cbcSMatt Macy 4210*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 4211*eda14cbcSMatt Macy 4212*eda14cbcSMatt Macy db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_WRITER, FTAG); 4213*eda14cbcSMatt Macy *db->db_blkptr = *bp; 4214*eda14cbcSMatt Macy dmu_buf_unlock_parent(db, dblt, FTAG); 4215*eda14cbcSMatt Macy } 4216*eda14cbcSMatt Macy 4217*eda14cbcSMatt Macy /* ARGSUSED */ 4218*eda14cbcSMatt Macy /* 4219*eda14cbcSMatt Macy * This function gets called just prior to running through the compression 4220*eda14cbcSMatt Macy * stage of the zio pipeline. If we're an indirect block comprised of only 4221*eda14cbcSMatt Macy * holes, then we want this indirect to be compressed away to a hole. In 4222*eda14cbcSMatt Macy * order to do that we must zero out any information about the holes that 4223*eda14cbcSMatt Macy * this indirect points to prior to before we try to compress it. 4224*eda14cbcSMatt Macy */ 4225*eda14cbcSMatt Macy static void 4226*eda14cbcSMatt Macy dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 4227*eda14cbcSMatt Macy { 4228*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 4229*eda14cbcSMatt Macy dnode_t *dn; 4230*eda14cbcSMatt Macy blkptr_t *bp; 4231*eda14cbcSMatt Macy unsigned int epbs, i; 4232*eda14cbcSMatt Macy 4233*eda14cbcSMatt Macy ASSERT3U(db->db_level, >, 0); 4234*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 4235*eda14cbcSMatt Macy dn = DB_DNODE(db); 4236*eda14cbcSMatt Macy epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 4237*eda14cbcSMatt Macy ASSERT3U(epbs, <, 31); 4238*eda14cbcSMatt Macy 4239*eda14cbcSMatt Macy /* Determine if all our children are holes */ 4240*eda14cbcSMatt Macy for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) { 4241*eda14cbcSMatt Macy if (!BP_IS_HOLE(bp)) 4242*eda14cbcSMatt Macy break; 4243*eda14cbcSMatt Macy } 4244*eda14cbcSMatt Macy 4245*eda14cbcSMatt Macy /* 4246*eda14cbcSMatt Macy * If all the children are holes, then zero them all out so that 4247*eda14cbcSMatt Macy * we may get compressed away. 4248*eda14cbcSMatt Macy */ 4249*eda14cbcSMatt Macy if (i == 1ULL << epbs) { 4250*eda14cbcSMatt Macy /* 4251*eda14cbcSMatt Macy * We only found holes. Grab the rwlock to prevent 4252*eda14cbcSMatt Macy * anybody from reading the blocks we're about to 4253*eda14cbcSMatt Macy * zero out. 4254*eda14cbcSMatt Macy */ 4255*eda14cbcSMatt Macy rw_enter(&db->db_rwlock, RW_WRITER); 4256*eda14cbcSMatt Macy bzero(db->db.db_data, db->db.db_size); 4257*eda14cbcSMatt Macy rw_exit(&db->db_rwlock); 4258*eda14cbcSMatt Macy } 4259*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4260*eda14cbcSMatt Macy } 4261*eda14cbcSMatt Macy 4262*eda14cbcSMatt Macy /* 4263*eda14cbcSMatt Macy * The SPA will call this callback several times for each zio - once 4264*eda14cbcSMatt Macy * for every physical child i/o (zio->io_phys_children times). This 4265*eda14cbcSMatt Macy * allows the DMU to monitor the progress of each logical i/o. For example, 4266*eda14cbcSMatt Macy * there may be 2 copies of an indirect block, or many fragments of a RAID-Z 4267*eda14cbcSMatt Macy * block. There may be a long delay before all copies/fragments are completed, 4268*eda14cbcSMatt Macy * so this callback allows us to retire dirty space gradually, as the physical 4269*eda14cbcSMatt Macy * i/os complete. 4270*eda14cbcSMatt Macy */ 4271*eda14cbcSMatt Macy /* ARGSUSED */ 4272*eda14cbcSMatt Macy static void 4273*eda14cbcSMatt Macy dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg) 4274*eda14cbcSMatt Macy { 4275*eda14cbcSMatt Macy dmu_buf_impl_t *db = arg; 4276*eda14cbcSMatt Macy objset_t *os = db->db_objset; 4277*eda14cbcSMatt Macy dsl_pool_t *dp = dmu_objset_pool(os); 4278*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 4279*eda14cbcSMatt Macy int delta = 0; 4280*eda14cbcSMatt Macy 4281*eda14cbcSMatt Macy dr = db->db_data_pending; 4282*eda14cbcSMatt Macy ASSERT3U(dr->dr_txg, ==, zio->io_txg); 4283*eda14cbcSMatt Macy 4284*eda14cbcSMatt Macy /* 4285*eda14cbcSMatt Macy * The callback will be called io_phys_children times. Retire one 4286*eda14cbcSMatt Macy * portion of our dirty space each time we are called. Any rounding 4287*eda14cbcSMatt Macy * error will be cleaned up by dbuf_write_done(). 4288*eda14cbcSMatt Macy */ 4289*eda14cbcSMatt Macy delta = dr->dr_accounted / zio->io_phys_children; 4290*eda14cbcSMatt Macy dsl_pool_undirty_space(dp, delta, zio->io_txg); 4291*eda14cbcSMatt Macy } 4292*eda14cbcSMatt Macy 4293*eda14cbcSMatt Macy /* ARGSUSED */ 4294*eda14cbcSMatt Macy static void 4295*eda14cbcSMatt Macy dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb) 4296*eda14cbcSMatt Macy { 4297*eda14cbcSMatt Macy dmu_buf_impl_t *db = vdb; 4298*eda14cbcSMatt Macy blkptr_t *bp_orig = &zio->io_bp_orig; 4299*eda14cbcSMatt Macy blkptr_t *bp = db->db_blkptr; 4300*eda14cbcSMatt Macy objset_t *os = db->db_objset; 4301*eda14cbcSMatt Macy dmu_tx_t *tx = os->os_synctx; 4302*eda14cbcSMatt Macy dbuf_dirty_record_t *dr; 4303*eda14cbcSMatt Macy 4304*eda14cbcSMatt Macy ASSERT0(zio->io_error); 4305*eda14cbcSMatt Macy ASSERT(db->db_blkptr == bp); 4306*eda14cbcSMatt Macy 4307*eda14cbcSMatt Macy /* 4308*eda14cbcSMatt Macy * For nopwrites and rewrites we ensure that the bp matches our 4309*eda14cbcSMatt Macy * original and bypass all the accounting. 4310*eda14cbcSMatt Macy */ 4311*eda14cbcSMatt Macy if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) { 4312*eda14cbcSMatt Macy ASSERT(BP_EQUAL(bp, bp_orig)); 4313*eda14cbcSMatt Macy } else { 4314*eda14cbcSMatt Macy dsl_dataset_t *ds = os->os_dsl_dataset; 4315*eda14cbcSMatt Macy (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 4316*eda14cbcSMatt Macy dsl_dataset_block_born(ds, bp, tx); 4317*eda14cbcSMatt Macy } 4318*eda14cbcSMatt Macy 4319*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 4320*eda14cbcSMatt Macy 4321*eda14cbcSMatt Macy DBUF_VERIFY(db); 4322*eda14cbcSMatt Macy 4323*eda14cbcSMatt Macy dr = db->db_data_pending; 4324*eda14cbcSMatt Macy ASSERT(!list_link_active(&dr->dr_dirty_node)); 4325*eda14cbcSMatt Macy ASSERT(dr->dr_dbuf == db); 4326*eda14cbcSMatt Macy ASSERT(list_next(&db->db_dirty_records, dr) == NULL); 4327*eda14cbcSMatt Macy list_remove(&db->db_dirty_records, dr); 4328*eda14cbcSMatt Macy 4329*eda14cbcSMatt Macy #ifdef ZFS_DEBUG 4330*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) { 4331*eda14cbcSMatt Macy dnode_t *dn; 4332*eda14cbcSMatt Macy 4333*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 4334*eda14cbcSMatt Macy dn = DB_DNODE(db); 4335*eda14cbcSMatt Macy ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 4336*eda14cbcSMatt Macy ASSERT(!(BP_IS_HOLE(db->db_blkptr)) && 4337*eda14cbcSMatt Macy db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys)); 4338*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4339*eda14cbcSMatt Macy } 4340*eda14cbcSMatt Macy #endif 4341*eda14cbcSMatt Macy 4342*eda14cbcSMatt Macy if (db->db_level == 0) { 4343*eda14cbcSMatt Macy ASSERT(db->db_blkid != DMU_BONUS_BLKID); 4344*eda14cbcSMatt Macy ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 4345*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL) { 4346*eda14cbcSMatt Macy if (dr->dt.dl.dr_data != db->db_buf) 4347*eda14cbcSMatt Macy arc_buf_destroy(dr->dt.dl.dr_data, db); 4348*eda14cbcSMatt Macy } 4349*eda14cbcSMatt Macy } else { 4350*eda14cbcSMatt Macy dnode_t *dn; 4351*eda14cbcSMatt Macy 4352*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 4353*eda14cbcSMatt Macy dn = DB_DNODE(db); 4354*eda14cbcSMatt Macy ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 4355*eda14cbcSMatt Macy ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 4356*eda14cbcSMatt Macy if (!BP_IS_HOLE(db->db_blkptr)) { 4357*eda14cbcSMatt Macy int epbs __maybe_unused = dn->dn_phys->dn_indblkshift - 4358*eda14cbcSMatt Macy SPA_BLKPTRSHIFT; 4359*eda14cbcSMatt Macy ASSERT3U(db->db_blkid, <=, 4360*eda14cbcSMatt Macy dn->dn_phys->dn_maxblkid >> (db->db_level * epbs)); 4361*eda14cbcSMatt Macy ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 4362*eda14cbcSMatt Macy db->db.db_size); 4363*eda14cbcSMatt Macy } 4364*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4365*eda14cbcSMatt Macy mutex_destroy(&dr->dt.di.dr_mtx); 4366*eda14cbcSMatt Macy list_destroy(&dr->dt.di.dr_children); 4367*eda14cbcSMatt Macy } 4368*eda14cbcSMatt Macy 4369*eda14cbcSMatt Macy cv_broadcast(&db->db_changed); 4370*eda14cbcSMatt Macy ASSERT(db->db_dirtycnt > 0); 4371*eda14cbcSMatt Macy db->db_dirtycnt -= 1; 4372*eda14cbcSMatt Macy db->db_data_pending = NULL; 4373*eda14cbcSMatt Macy dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE); 4374*eda14cbcSMatt Macy 4375*eda14cbcSMatt Macy /* 4376*eda14cbcSMatt Macy * If we didn't do a physical write in this ZIO and we 4377*eda14cbcSMatt Macy * still ended up here, it means that the space of the 4378*eda14cbcSMatt Macy * dbuf that we just released (and undirtied) above hasn't 4379*eda14cbcSMatt Macy * been marked as undirtied in the pool's accounting. 4380*eda14cbcSMatt Macy * 4381*eda14cbcSMatt Macy * Thus, we undirty that space in the pool's view of the 4382*eda14cbcSMatt Macy * world here. For physical writes this type of update 4383*eda14cbcSMatt Macy * happens in dbuf_write_physdone(). 4384*eda14cbcSMatt Macy * 4385*eda14cbcSMatt Macy * If we did a physical write, cleanup any rounding errors 4386*eda14cbcSMatt Macy * that came up due to writing multiple copies of a block 4387*eda14cbcSMatt Macy * on disk [see dbuf_write_physdone()]. 4388*eda14cbcSMatt Macy */ 4389*eda14cbcSMatt Macy if (zio->io_phys_children == 0) { 4390*eda14cbcSMatt Macy dsl_pool_undirty_space(dmu_objset_pool(os), 4391*eda14cbcSMatt Macy dr->dr_accounted, zio->io_txg); 4392*eda14cbcSMatt Macy } else { 4393*eda14cbcSMatt Macy dsl_pool_undirty_space(dmu_objset_pool(os), 4394*eda14cbcSMatt Macy dr->dr_accounted % zio->io_phys_children, zio->io_txg); 4395*eda14cbcSMatt Macy } 4396*eda14cbcSMatt Macy 4397*eda14cbcSMatt Macy kmem_free(dr, sizeof (dbuf_dirty_record_t)); 4398*eda14cbcSMatt Macy } 4399*eda14cbcSMatt Macy 4400*eda14cbcSMatt Macy static void 4401*eda14cbcSMatt Macy dbuf_write_nofill_ready(zio_t *zio) 4402*eda14cbcSMatt Macy { 4403*eda14cbcSMatt Macy dbuf_write_ready(zio, NULL, zio->io_private); 4404*eda14cbcSMatt Macy } 4405*eda14cbcSMatt Macy 4406*eda14cbcSMatt Macy static void 4407*eda14cbcSMatt Macy dbuf_write_nofill_done(zio_t *zio) 4408*eda14cbcSMatt Macy { 4409*eda14cbcSMatt Macy dbuf_write_done(zio, NULL, zio->io_private); 4410*eda14cbcSMatt Macy } 4411*eda14cbcSMatt Macy 4412*eda14cbcSMatt Macy static void 4413*eda14cbcSMatt Macy dbuf_write_override_ready(zio_t *zio) 4414*eda14cbcSMatt Macy { 4415*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = zio->io_private; 4416*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 4417*eda14cbcSMatt Macy 4418*eda14cbcSMatt Macy dbuf_write_ready(zio, NULL, db); 4419*eda14cbcSMatt Macy } 4420*eda14cbcSMatt Macy 4421*eda14cbcSMatt Macy static void 4422*eda14cbcSMatt Macy dbuf_write_override_done(zio_t *zio) 4423*eda14cbcSMatt Macy { 4424*eda14cbcSMatt Macy dbuf_dirty_record_t *dr = zio->io_private; 4425*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 4426*eda14cbcSMatt Macy blkptr_t *obp = &dr->dt.dl.dr_overridden_by; 4427*eda14cbcSMatt Macy 4428*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 4429*eda14cbcSMatt Macy if (!BP_EQUAL(zio->io_bp, obp)) { 4430*eda14cbcSMatt Macy if (!BP_IS_HOLE(obp)) 4431*eda14cbcSMatt Macy dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp); 4432*eda14cbcSMatt Macy arc_release(dr->dt.dl.dr_data, db); 4433*eda14cbcSMatt Macy } 4434*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 4435*eda14cbcSMatt Macy 4436*eda14cbcSMatt Macy dbuf_write_done(zio, NULL, db); 4437*eda14cbcSMatt Macy 4438*eda14cbcSMatt Macy if (zio->io_abd != NULL) 4439*eda14cbcSMatt Macy abd_put(zio->io_abd); 4440*eda14cbcSMatt Macy } 4441*eda14cbcSMatt Macy 4442*eda14cbcSMatt Macy typedef struct dbuf_remap_impl_callback_arg { 4443*eda14cbcSMatt Macy objset_t *drica_os; 4444*eda14cbcSMatt Macy uint64_t drica_blk_birth; 4445*eda14cbcSMatt Macy dmu_tx_t *drica_tx; 4446*eda14cbcSMatt Macy } dbuf_remap_impl_callback_arg_t; 4447*eda14cbcSMatt Macy 4448*eda14cbcSMatt Macy static void 4449*eda14cbcSMatt Macy dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size, 4450*eda14cbcSMatt Macy void *arg) 4451*eda14cbcSMatt Macy { 4452*eda14cbcSMatt Macy dbuf_remap_impl_callback_arg_t *drica = arg; 4453*eda14cbcSMatt Macy objset_t *os = drica->drica_os; 4454*eda14cbcSMatt Macy spa_t *spa = dmu_objset_spa(os); 4455*eda14cbcSMatt Macy dmu_tx_t *tx = drica->drica_tx; 4456*eda14cbcSMatt Macy 4457*eda14cbcSMatt Macy ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 4458*eda14cbcSMatt Macy 4459*eda14cbcSMatt Macy if (os == spa_meta_objset(spa)) { 4460*eda14cbcSMatt Macy spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx); 4461*eda14cbcSMatt Macy } else { 4462*eda14cbcSMatt Macy dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset, 4463*eda14cbcSMatt Macy size, drica->drica_blk_birth, tx); 4464*eda14cbcSMatt Macy } 4465*eda14cbcSMatt Macy } 4466*eda14cbcSMatt Macy 4467*eda14cbcSMatt Macy static void 4468*eda14cbcSMatt Macy dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, krwlock_t *rw, dmu_tx_t *tx) 4469*eda14cbcSMatt Macy { 4470*eda14cbcSMatt Macy blkptr_t bp_copy = *bp; 4471*eda14cbcSMatt Macy spa_t *spa = dmu_objset_spa(dn->dn_objset); 4472*eda14cbcSMatt Macy dbuf_remap_impl_callback_arg_t drica; 4473*eda14cbcSMatt Macy 4474*eda14cbcSMatt Macy ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 4475*eda14cbcSMatt Macy 4476*eda14cbcSMatt Macy drica.drica_os = dn->dn_objset; 4477*eda14cbcSMatt Macy drica.drica_blk_birth = bp->blk_birth; 4478*eda14cbcSMatt Macy drica.drica_tx = tx; 4479*eda14cbcSMatt Macy if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback, 4480*eda14cbcSMatt Macy &drica)) { 4481*eda14cbcSMatt Macy /* 4482*eda14cbcSMatt Macy * If the blkptr being remapped is tracked by a livelist, 4483*eda14cbcSMatt Macy * then we need to make sure the livelist reflects the update. 4484*eda14cbcSMatt Macy * First, cancel out the old blkptr by appending a 'FREE' 4485*eda14cbcSMatt Macy * entry. Next, add an 'ALLOC' to track the new version. This 4486*eda14cbcSMatt Macy * way we avoid trying to free an inaccurate blkptr at delete. 4487*eda14cbcSMatt Macy * Note that embedded blkptrs are not tracked in livelists. 4488*eda14cbcSMatt Macy */ 4489*eda14cbcSMatt Macy if (dn->dn_objset != spa_meta_objset(spa)) { 4490*eda14cbcSMatt Macy dsl_dataset_t *ds = dmu_objset_ds(dn->dn_objset); 4491*eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) && 4492*eda14cbcSMatt Macy bp->blk_birth > ds->ds_dir->dd_origin_txg) { 4493*eda14cbcSMatt Macy ASSERT(!BP_IS_EMBEDDED(bp)); 4494*eda14cbcSMatt Macy ASSERT(dsl_dir_is_clone(ds->ds_dir)); 4495*eda14cbcSMatt Macy ASSERT(spa_feature_is_enabled(spa, 4496*eda14cbcSMatt Macy SPA_FEATURE_LIVELIST)); 4497*eda14cbcSMatt Macy bplist_append(&ds->ds_dir->dd_pending_frees, 4498*eda14cbcSMatt Macy bp); 4499*eda14cbcSMatt Macy bplist_append(&ds->ds_dir->dd_pending_allocs, 4500*eda14cbcSMatt Macy &bp_copy); 4501*eda14cbcSMatt Macy } 4502*eda14cbcSMatt Macy } 4503*eda14cbcSMatt Macy 4504*eda14cbcSMatt Macy /* 4505*eda14cbcSMatt Macy * The db_rwlock prevents dbuf_read_impl() from 4506*eda14cbcSMatt Macy * dereferencing the BP while we are changing it. To 4507*eda14cbcSMatt Macy * avoid lock contention, only grab it when we are actually 4508*eda14cbcSMatt Macy * changing the BP. 4509*eda14cbcSMatt Macy */ 4510*eda14cbcSMatt Macy if (rw != NULL) 4511*eda14cbcSMatt Macy rw_enter(rw, RW_WRITER); 4512*eda14cbcSMatt Macy *bp = bp_copy; 4513*eda14cbcSMatt Macy if (rw != NULL) 4514*eda14cbcSMatt Macy rw_exit(rw); 4515*eda14cbcSMatt Macy } 4516*eda14cbcSMatt Macy } 4517*eda14cbcSMatt Macy 4518*eda14cbcSMatt Macy /* 4519*eda14cbcSMatt Macy * Remap any existing BP's to concrete vdevs, if possible. 4520*eda14cbcSMatt Macy */ 4521*eda14cbcSMatt Macy static void 4522*eda14cbcSMatt Macy dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx) 4523*eda14cbcSMatt Macy { 4524*eda14cbcSMatt Macy spa_t *spa = dmu_objset_spa(db->db_objset); 4525*eda14cbcSMatt Macy ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 4526*eda14cbcSMatt Macy 4527*eda14cbcSMatt Macy if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)) 4528*eda14cbcSMatt Macy return; 4529*eda14cbcSMatt Macy 4530*eda14cbcSMatt Macy if (db->db_level > 0) { 4531*eda14cbcSMatt Macy blkptr_t *bp = db->db.db_data; 4532*eda14cbcSMatt Macy for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) { 4533*eda14cbcSMatt Macy dbuf_remap_impl(dn, &bp[i], &db->db_rwlock, tx); 4534*eda14cbcSMatt Macy } 4535*eda14cbcSMatt Macy } else if (db->db.db_object == DMU_META_DNODE_OBJECT) { 4536*eda14cbcSMatt Macy dnode_phys_t *dnp = db->db.db_data; 4537*eda14cbcSMatt Macy ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==, 4538*eda14cbcSMatt Macy DMU_OT_DNODE); 4539*eda14cbcSMatt Macy for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; 4540*eda14cbcSMatt Macy i += dnp[i].dn_extra_slots + 1) { 4541*eda14cbcSMatt Macy for (int j = 0; j < dnp[i].dn_nblkptr; j++) { 4542*eda14cbcSMatt Macy krwlock_t *lock = (dn->dn_dbuf == NULL ? NULL : 4543*eda14cbcSMatt Macy &dn->dn_dbuf->db_rwlock); 4544*eda14cbcSMatt Macy dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], lock, 4545*eda14cbcSMatt Macy tx); 4546*eda14cbcSMatt Macy } 4547*eda14cbcSMatt Macy } 4548*eda14cbcSMatt Macy } 4549*eda14cbcSMatt Macy } 4550*eda14cbcSMatt Macy 4551*eda14cbcSMatt Macy 4552*eda14cbcSMatt Macy /* Issue I/O to commit a dirty buffer to disk. */ 4553*eda14cbcSMatt Macy static void 4554*eda14cbcSMatt Macy dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx) 4555*eda14cbcSMatt Macy { 4556*eda14cbcSMatt Macy dmu_buf_impl_t *db = dr->dr_dbuf; 4557*eda14cbcSMatt Macy dnode_t *dn; 4558*eda14cbcSMatt Macy objset_t *os; 4559*eda14cbcSMatt Macy dmu_buf_impl_t *parent = db->db_parent; 4560*eda14cbcSMatt Macy uint64_t txg = tx->tx_txg; 4561*eda14cbcSMatt Macy zbookmark_phys_t zb; 4562*eda14cbcSMatt Macy zio_prop_t zp; 4563*eda14cbcSMatt Macy zio_t *pio; /* parent I/O */ 4564*eda14cbcSMatt Macy int wp_flag = 0; 4565*eda14cbcSMatt Macy 4566*eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 4567*eda14cbcSMatt Macy 4568*eda14cbcSMatt Macy DB_DNODE_ENTER(db); 4569*eda14cbcSMatt Macy dn = DB_DNODE(db); 4570*eda14cbcSMatt Macy os = dn->dn_objset; 4571*eda14cbcSMatt Macy 4572*eda14cbcSMatt Macy if (db->db_state != DB_NOFILL) { 4573*eda14cbcSMatt Macy if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) { 4574*eda14cbcSMatt Macy /* 4575*eda14cbcSMatt Macy * Private object buffers are released here rather 4576*eda14cbcSMatt Macy * than in dbuf_dirty() since they are only modified 4577*eda14cbcSMatt Macy * in the syncing context and we don't want the 4578*eda14cbcSMatt Macy * overhead of making multiple copies of the data. 4579*eda14cbcSMatt Macy */ 4580*eda14cbcSMatt Macy if (BP_IS_HOLE(db->db_blkptr)) { 4581*eda14cbcSMatt Macy arc_buf_thaw(data); 4582*eda14cbcSMatt Macy } else { 4583*eda14cbcSMatt Macy dbuf_release_bp(db); 4584*eda14cbcSMatt Macy } 4585*eda14cbcSMatt Macy dbuf_remap(dn, db, tx); 4586*eda14cbcSMatt Macy } 4587*eda14cbcSMatt Macy } 4588*eda14cbcSMatt Macy 4589*eda14cbcSMatt Macy if (parent != dn->dn_dbuf) { 4590*eda14cbcSMatt Macy /* Our parent is an indirect block. */ 4591*eda14cbcSMatt Macy /* We have a dirty parent that has been scheduled for write. */ 4592*eda14cbcSMatt Macy ASSERT(parent && parent->db_data_pending); 4593*eda14cbcSMatt Macy /* Our parent's buffer is one level closer to the dnode. */ 4594*eda14cbcSMatt Macy ASSERT(db->db_level == parent->db_level-1); 4595*eda14cbcSMatt Macy /* 4596*eda14cbcSMatt Macy * We're about to modify our parent's db_data by modifying 4597*eda14cbcSMatt Macy * our block pointer, so the parent must be released. 4598*eda14cbcSMatt Macy */ 4599*eda14cbcSMatt Macy ASSERT(arc_released(parent->db_buf)); 4600*eda14cbcSMatt Macy pio = parent->db_data_pending->dr_zio; 4601*eda14cbcSMatt Macy } else { 4602*eda14cbcSMatt Macy /* Our parent is the dnode itself. */ 4603*eda14cbcSMatt Macy ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 && 4604*eda14cbcSMatt Macy db->db_blkid != DMU_SPILL_BLKID) || 4605*eda14cbcSMatt Macy (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0)); 4606*eda14cbcSMatt Macy if (db->db_blkid != DMU_SPILL_BLKID) 4607*eda14cbcSMatt Macy ASSERT3P(db->db_blkptr, ==, 4608*eda14cbcSMatt Macy &dn->dn_phys->dn_blkptr[db->db_blkid]); 4609*eda14cbcSMatt Macy pio = dn->dn_zio; 4610*eda14cbcSMatt Macy } 4611*eda14cbcSMatt Macy 4612*eda14cbcSMatt Macy ASSERT(db->db_level == 0 || data == db->db_buf); 4613*eda14cbcSMatt Macy ASSERT3U(db->db_blkptr->blk_birth, <=, txg); 4614*eda14cbcSMatt Macy ASSERT(pio); 4615*eda14cbcSMatt Macy 4616*eda14cbcSMatt Macy SET_BOOKMARK(&zb, os->os_dsl_dataset ? 4617*eda14cbcSMatt Macy os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 4618*eda14cbcSMatt Macy db->db.db_object, db->db_level, db->db_blkid); 4619*eda14cbcSMatt Macy 4620*eda14cbcSMatt Macy if (db->db_blkid == DMU_SPILL_BLKID) 4621*eda14cbcSMatt Macy wp_flag = WP_SPILL; 4622*eda14cbcSMatt Macy wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0; 4623*eda14cbcSMatt Macy 4624*eda14cbcSMatt Macy dmu_write_policy(os, dn, db->db_level, wp_flag, &zp); 4625*eda14cbcSMatt Macy DB_DNODE_EXIT(db); 4626*eda14cbcSMatt Macy 4627*eda14cbcSMatt Macy /* 4628*eda14cbcSMatt Macy * We copy the blkptr now (rather than when we instantiate the dirty 4629*eda14cbcSMatt Macy * record), because its value can change between open context and 4630*eda14cbcSMatt Macy * syncing context. We do not need to hold dn_struct_rwlock to read 4631*eda14cbcSMatt Macy * db_blkptr because we are in syncing context. 4632*eda14cbcSMatt Macy */ 4633*eda14cbcSMatt Macy dr->dr_bp_copy = *db->db_blkptr; 4634*eda14cbcSMatt Macy 4635*eda14cbcSMatt Macy if (db->db_level == 0 && 4636*eda14cbcSMatt Macy dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 4637*eda14cbcSMatt Macy /* 4638*eda14cbcSMatt Macy * The BP for this block has been provided by open context 4639*eda14cbcSMatt Macy * (by dmu_sync() or dmu_buf_write_embedded()). 4640*eda14cbcSMatt Macy */ 4641*eda14cbcSMatt Macy abd_t *contents = (data != NULL) ? 4642*eda14cbcSMatt Macy abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL; 4643*eda14cbcSMatt Macy 4644*eda14cbcSMatt Macy dr->dr_zio = zio_write(pio, os->os_spa, txg, &dr->dr_bp_copy, 4645*eda14cbcSMatt Macy contents, db->db.db_size, db->db.db_size, &zp, 4646*eda14cbcSMatt Macy dbuf_write_override_ready, NULL, NULL, 4647*eda14cbcSMatt Macy dbuf_write_override_done, 4648*eda14cbcSMatt Macy dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 4649*eda14cbcSMatt Macy mutex_enter(&db->db_mtx); 4650*eda14cbcSMatt Macy dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 4651*eda14cbcSMatt Macy zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by, 4652*eda14cbcSMatt Macy dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite); 4653*eda14cbcSMatt Macy mutex_exit(&db->db_mtx); 4654*eda14cbcSMatt Macy } else if (db->db_state == DB_NOFILL) { 4655*eda14cbcSMatt Macy ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF || 4656*eda14cbcSMatt Macy zp.zp_checksum == ZIO_CHECKSUM_NOPARITY); 4657*eda14cbcSMatt Macy dr->dr_zio = zio_write(pio, os->os_spa, txg, 4658*eda14cbcSMatt Macy &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp, 4659*eda14cbcSMatt Macy dbuf_write_nofill_ready, NULL, NULL, 4660*eda14cbcSMatt Macy dbuf_write_nofill_done, db, 4661*eda14cbcSMatt Macy ZIO_PRIORITY_ASYNC_WRITE, 4662*eda14cbcSMatt Macy ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb); 4663*eda14cbcSMatt Macy } else { 4664*eda14cbcSMatt Macy ASSERT(arc_released(data)); 4665*eda14cbcSMatt Macy 4666*eda14cbcSMatt Macy /* 4667*eda14cbcSMatt Macy * For indirect blocks, we want to setup the children 4668*eda14cbcSMatt Macy * ready callback so that we can properly handle an indirect 4669*eda14cbcSMatt Macy * block that only contains holes. 4670*eda14cbcSMatt Macy */ 4671*eda14cbcSMatt Macy arc_write_done_func_t *children_ready_cb = NULL; 4672*eda14cbcSMatt Macy if (db->db_level != 0) 4673*eda14cbcSMatt Macy children_ready_cb = dbuf_write_children_ready; 4674*eda14cbcSMatt Macy 4675*eda14cbcSMatt Macy dr->dr_zio = arc_write(pio, os->os_spa, txg, 4676*eda14cbcSMatt Macy &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db), 4677*eda14cbcSMatt Macy &zp, dbuf_write_ready, 4678*eda14cbcSMatt Macy children_ready_cb, dbuf_write_physdone, 4679*eda14cbcSMatt Macy dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE, 4680*eda14cbcSMatt Macy ZIO_FLAG_MUSTSUCCEED, &zb); 4681*eda14cbcSMatt Macy } 4682*eda14cbcSMatt Macy } 4683*eda14cbcSMatt Macy 4684*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_find); 4685*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_is_metadata); 4686*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_destroy); 4687*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_loan_arcbuf); 4688*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_whichblock); 4689*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_read); 4690*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_unoverride); 4691*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_free_range); 4692*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_new_size); 4693*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_release_bp); 4694*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_dirty); 4695*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_crypt_params); 4696*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_dirty); 4697*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_is_dirty); 4698*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_not_fill); 4699*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_fill); 4700*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_fill_done); 4701*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_rele); 4702*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_assign_arcbuf); 4703*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_prefetch); 4704*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold_impl); 4705*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold); 4706*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold_level); 4707*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_create_bonus); 4708*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_spill_set_blksz); 4709*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rm_spill); 4710*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_add_ref); 4711*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rele); 4712*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rele_and_unlock); 4713*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_refcount); 4714*eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_sync_list); 4715*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_user); 4716*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_user_ie); 4717*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_get_user); 4718*eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_get_blkptr); 4719*eda14cbcSMatt Macy 4720*eda14cbcSMatt Macy /* BEGIN CSTYLED */ 4721*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, max_bytes, ULONG, ZMOD_RW, 4722*eda14cbcSMatt Macy "Maximum size in bytes of the dbuf cache."); 4723*eda14cbcSMatt Macy 4724*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, hiwater_pct, UINT, ZMOD_RW, 4725*eda14cbcSMatt Macy "Percentage over dbuf_cache_max_bytes when dbufs must be evicted " 4726*eda14cbcSMatt Macy "directly."); 4727*eda14cbcSMatt Macy 4728*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, lowater_pct, UINT, ZMOD_RW, 4729*eda14cbcSMatt Macy "Percentage below dbuf_cache_max_bytes when the evict thread stops " 4730*eda14cbcSMatt Macy "evicting dbufs."); 4731*eda14cbcSMatt Macy 4732*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_max_bytes, ULONG, ZMOD_RW, 4733*eda14cbcSMatt Macy "Maximum size in bytes of the dbuf metadata cache."); 4734*eda14cbcSMatt Macy 4735*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, cache_shift, INT, ZMOD_RW, 4736*eda14cbcSMatt Macy "Set the size of the dbuf cache to a log2 fraction of arc size."); 4737*eda14cbcSMatt Macy 4738*eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_shift, INT, ZMOD_RW, 4739*eda14cbcSMatt Macy "Set the size of the dbuf metadata cache to a log2 fraction of arc " 4740*eda14cbcSMatt Macy "size."); 4741*eda14cbcSMatt Macy /* END CSTYLED */ 4742