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