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