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