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