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