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