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