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