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