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