]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dbuf.c
OpenZFS 7968 - multi-threaded spa_sync()
[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 #ifdef DEBUG
1643 if (dn->dn_objset->os_dsl_dataset != NULL)
1644 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1645 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1646 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1647 if (dn->dn_objset->os_dsl_dataset != NULL)
1648 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1649 #endif
1650 ASSERT(db->db.db_size != 0);
1651
1652 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1653
1654 if (db->db_blkid != DMU_BONUS_BLKID) {
1655 dmu_objset_willuse_space(os, db->db.db_size, tx);
1656 }
1657
1658 /*
1659 * If this buffer is dirty in an old transaction group we need
1660 * to make a copy of it so that the changes we make in this
1661 * transaction group won't leak out when we sync the older txg.
1662 */
1663 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1664 list_link_init(&dr->dr_dirty_node);
1665 if (db->db_level == 0) {
1666 void *data_old = db->db_buf;
1667
1668 if (db->db_state != DB_NOFILL) {
1669 if (db->db_blkid == DMU_BONUS_BLKID) {
1670 dbuf_fix_old_data(db, tx->tx_txg);
1671 data_old = db->db.db_data;
1672 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1673 /*
1674 * Release the data buffer from the cache so
1675 * that we can modify it without impacting
1676 * possible other users of this cached data
1677 * block. Note that indirect blocks and
1678 * private objects are not released until the
1679 * syncing state (since they are only modified
1680 * then).
1681 */
1682 arc_release(db->db_buf, db);
1683 dbuf_fix_old_data(db, tx->tx_txg);
1684 data_old = db->db_buf;
1685 }
1686 ASSERT(data_old != NULL);
1687 }
1688 dr->dt.dl.dr_data = data_old;
1689 } else {
1690 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL);
1691 list_create(&dr->dt.di.dr_children,
1692 sizeof (dbuf_dirty_record_t),
1693 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1694 }
1695 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1696 dr->dr_accounted = db->db.db_size;
1697 dr->dr_dbuf = db;
1698 dr->dr_txg = tx->tx_txg;
1699 dr->dr_next = *drp;
1700 *drp = dr;
1701
1702 /*
1703 * We could have been freed_in_flight between the dbuf_noread
1704 * and dbuf_dirty. We win, as though the dbuf_noread() had
1705 * happened after the free.
1706 */
1707 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1708 db->db_blkid != DMU_SPILL_BLKID) {
1709 mutex_enter(&dn->dn_mtx);
1710 if (dn->dn_free_ranges[txgoff] != NULL) {
1711 range_tree_clear(dn->dn_free_ranges[txgoff],
1712 db->db_blkid, 1);
1713 }
1714 mutex_exit(&dn->dn_mtx);
1715 db->db_freed_in_flight = FALSE;
1716 }
1717
1718 /*
1719 * This buffer is now part of this txg
1720 */
1721 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1722 db->db_dirtycnt += 1;
1723 ASSERT3U(db->db_dirtycnt, <=, 3);
1724
1725 mutex_exit(&db->db_mtx);
1726
1727 if (db->db_blkid == DMU_BONUS_BLKID ||
1728 db->db_blkid == DMU_SPILL_BLKID) {
1729 mutex_enter(&dn->dn_mtx);
1730 ASSERT(!list_link_active(&dr->dr_dirty_node));
1731 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1732 mutex_exit(&dn->dn_mtx);
1733 dnode_setdirty(dn, tx);
1734 DB_DNODE_EXIT(db);
1735 return (dr);
1736 }
1737
1738 /*
1739 * The dn_struct_rwlock prevents db_blkptr from changing
1740 * due to a write from syncing context completing
1741 * while we are running, so we want to acquire it before
1742 * looking at db_blkptr.
1743 */
1744 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1745 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1746 drop_struct_lock = TRUE;
1747 }
1748
1749 /*
1750 * If we are overwriting a dedup BP, then unless it is snapshotted,
1751 * when we get to syncing context we will need to decrement its
1752 * refcount in the DDT. Prefetch the relevant DDT block so that
1753 * syncing context won't have to wait for the i/o.
1754 */
1755 ddt_prefetch(os->os_spa, db->db_blkptr);
1756
1757 if (db->db_level == 0) {
1758 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1759 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1760 }
1761
1762 if (db->db_level+1 < dn->dn_nlevels) {
1763 dmu_buf_impl_t *parent = db->db_parent;
1764 dbuf_dirty_record_t *di;
1765 int parent_held = FALSE;
1766
1767 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1768 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1769
1770 parent = dbuf_hold_level(dn, db->db_level+1,
1771 db->db_blkid >> epbs, FTAG);
1772 ASSERT(parent != NULL);
1773 parent_held = TRUE;
1774 }
1775 if (drop_struct_lock)
1776 rw_exit(&dn->dn_struct_rwlock);
1777 ASSERT3U(db->db_level+1, ==, parent->db_level);
1778 di = dbuf_dirty(parent, tx);
1779 if (parent_held)
1780 dbuf_rele(parent, FTAG);
1781
1782 mutex_enter(&db->db_mtx);
1783 /*
1784 * Since we've dropped the mutex, it's possible that
1785 * dbuf_undirty() might have changed this out from under us.
1786 */
1787 if (db->db_last_dirty == dr ||
1788 dn->dn_object == DMU_META_DNODE_OBJECT) {
1789 mutex_enter(&di->dt.di.dr_mtx);
1790 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1791 ASSERT(!list_link_active(&dr->dr_dirty_node));
1792 list_insert_tail(&di->dt.di.dr_children, dr);
1793 mutex_exit(&di->dt.di.dr_mtx);
1794 dr->dr_parent = di;
1795 }
1796 mutex_exit(&db->db_mtx);
1797 } else {
1798 ASSERT(db->db_level+1 == dn->dn_nlevels);
1799 ASSERT(db->db_blkid < dn->dn_nblkptr);
1800 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1801 mutex_enter(&dn->dn_mtx);
1802 ASSERT(!list_link_active(&dr->dr_dirty_node));
1803 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1804 mutex_exit(&dn->dn_mtx);
1805 if (drop_struct_lock)
1806 rw_exit(&dn->dn_struct_rwlock);
1807 }
1808
1809 dnode_setdirty(dn, tx);
1810 DB_DNODE_EXIT(db);
1811 return (dr);
1812 }
1813
1814 /*
1815 * Undirty a buffer in the transaction group referenced by the given
1816 * transaction. Return whether this evicted the dbuf.
1817 */
1818 static boolean_t
1819 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1820 {
1821 dnode_t *dn;
1822 uint64_t txg = tx->tx_txg;
1823 dbuf_dirty_record_t *dr, **drp;
1824
1825 ASSERT(txg != 0);
1826
1827 /*
1828 * Due to our use of dn_nlevels below, this can only be called
1829 * in open context, unless we are operating on the MOS.
1830 * From syncing context, dn_nlevels may be different from the
1831 * dn_nlevels used when dbuf was dirtied.
1832 */
1833 ASSERT(db->db_objset ==
1834 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1835 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1836 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1837 ASSERT0(db->db_level);
1838 ASSERT(MUTEX_HELD(&db->db_mtx));
1839
1840 /*
1841 * If this buffer is not dirty, we're done.
1842 */
1843 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1844 if (dr->dr_txg <= txg)
1845 break;
1846 if (dr == NULL || dr->dr_txg < txg)
1847 return (B_FALSE);
1848 ASSERT(dr->dr_txg == txg);
1849 ASSERT(dr->dr_dbuf == db);
1850
1851 DB_DNODE_ENTER(db);
1852 dn = DB_DNODE(db);
1853
1854 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1855
1856 ASSERT(db->db.db_size != 0);
1857
1858 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1859 dr->dr_accounted, txg);
1860
1861 *drp = dr->dr_next;
1862
1863 /*
1864 * Note that there are three places in dbuf_dirty()
1865 * where this dirty record may be put on a list.
1866 * Make sure to do a list_remove corresponding to
1867 * every one of those list_insert calls.
1868 */
1869 if (dr->dr_parent) {
1870 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1871 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1872 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1873 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1874 db->db_level + 1 == dn->dn_nlevels) {
1875 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1876 mutex_enter(&dn->dn_mtx);
1877 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1878 mutex_exit(&dn->dn_mtx);
1879 }
1880 DB_DNODE_EXIT(db);
1881
1882 if (db->db_state != DB_NOFILL) {
1883 dbuf_unoverride(dr);
1884
1885 ASSERT(db->db_buf != NULL);
1886 ASSERT(dr->dt.dl.dr_data != NULL);
1887 if (dr->dt.dl.dr_data != db->db_buf)
1888 arc_buf_destroy(dr->dt.dl.dr_data, db);
1889 }
1890
1891 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1892
1893 ASSERT(db->db_dirtycnt > 0);
1894 db->db_dirtycnt -= 1;
1895
1896 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1897 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1898 dbuf_destroy(db);
1899 return (B_TRUE);
1900 }
1901
1902 return (B_FALSE);
1903 }
1904
1905 void
1906 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1907 {
1908 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1909 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1910 dbuf_dirty_record_t *dr;
1911
1912 ASSERT(tx->tx_txg != 0);
1913 ASSERT(!refcount_is_zero(&db->db_holds));
1914
1915 /*
1916 * Quick check for dirtyness. For already dirty blocks, this
1917 * reduces runtime of this function by >90%, and overall performance
1918 * by 50% for some workloads (e.g. file deletion with indirect blocks
1919 * cached).
1920 */
1921 mutex_enter(&db->db_mtx);
1922
1923 for (dr = db->db_last_dirty;
1924 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1925 /*
1926 * It's possible that it is already dirty but not cached,
1927 * because there are some calls to dbuf_dirty() that don't
1928 * go through dmu_buf_will_dirty().
1929 */
1930 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1931 /* This dbuf is already dirty and cached. */
1932 dbuf_redirty(dr);
1933 mutex_exit(&db->db_mtx);
1934 return;
1935 }
1936 }
1937 mutex_exit(&db->db_mtx);
1938
1939 DB_DNODE_ENTER(db);
1940 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1941 rf |= DB_RF_HAVESTRUCT;
1942 DB_DNODE_EXIT(db);
1943 (void) dbuf_read(db, NULL, rf);
1944 (void) dbuf_dirty(db, tx);
1945 }
1946
1947 void
1948 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1949 {
1950 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1951
1952 db->db_state = DB_NOFILL;
1953
1954 dmu_buf_will_fill(db_fake, tx);
1955 }
1956
1957 void
1958 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1959 {
1960 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1961
1962 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1963 ASSERT(tx->tx_txg != 0);
1964 ASSERT(db->db_level == 0);
1965 ASSERT(!refcount_is_zero(&db->db_holds));
1966
1967 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1968 dmu_tx_private_ok(tx));
1969
1970 dbuf_noread(db);
1971 (void) dbuf_dirty(db, tx);
1972 }
1973
1974 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1975 /* ARGSUSED */
1976 void
1977 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1978 {
1979 mutex_enter(&db->db_mtx);
1980 DBUF_VERIFY(db);
1981
1982 if (db->db_state == DB_FILL) {
1983 if (db->db_level == 0 && db->db_freed_in_flight) {
1984 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1985 /* we were freed while filling */
1986 /* XXX dbuf_undirty? */
1987 bzero(db->db.db_data, db->db.db_size);
1988 db->db_freed_in_flight = FALSE;
1989 }
1990 db->db_state = DB_CACHED;
1991 cv_broadcast(&db->db_changed);
1992 }
1993 mutex_exit(&db->db_mtx);
1994 }
1995
1996 void
1997 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1998 bp_embedded_type_t etype, enum zio_compress comp,
1999 int uncompressed_size, int compressed_size, int byteorder,
2000 dmu_tx_t *tx)
2001 {
2002 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2003 struct dirty_leaf *dl;
2004 dmu_object_type_t type;
2005
2006 if (etype == BP_EMBEDDED_TYPE_DATA) {
2007 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
2008 SPA_FEATURE_EMBEDDED_DATA));
2009 }
2010
2011 DB_DNODE_ENTER(db);
2012 type = DB_DNODE(db)->dn_type;
2013 DB_DNODE_EXIT(db);
2014
2015 ASSERT0(db->db_level);
2016 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2017
2018 dmu_buf_will_not_fill(dbuf, tx);
2019
2020 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
2021 dl = &db->db_last_dirty->dt.dl;
2022 encode_embedded_bp_compressed(&dl->dr_overridden_by,
2023 data, comp, uncompressed_size, compressed_size);
2024 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
2025 BP_SET_TYPE(&dl->dr_overridden_by, type);
2026 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
2027 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
2028
2029 dl->dr_override_state = DR_OVERRIDDEN;
2030 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
2031 }
2032
2033 /*
2034 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2035 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2036 */
2037 void
2038 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2039 {
2040 ASSERT(!refcount_is_zero(&db->db_holds));
2041 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2042 ASSERT(db->db_level == 0);
2043 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
2044 ASSERT(buf != NULL);
2045 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
2046 ASSERT(tx->tx_txg != 0);
2047
2048 arc_return_buf(buf, db);
2049 ASSERT(arc_released(buf));
2050
2051 mutex_enter(&db->db_mtx);
2052
2053 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2054 cv_wait(&db->db_changed, &db->db_mtx);
2055
2056 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2057
2058 if (db->db_state == DB_CACHED &&
2059 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2060 mutex_exit(&db->db_mtx);
2061 (void) dbuf_dirty(db, tx);
2062 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
2063 arc_buf_destroy(buf, db);
2064 xuio_stat_wbuf_copied();
2065 return;
2066 }
2067
2068 xuio_stat_wbuf_nocopy();
2069 if (db->db_state == DB_CACHED) {
2070 dbuf_dirty_record_t *dr = db->db_last_dirty;
2071
2072 ASSERT(db->db_buf != NULL);
2073 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2074 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2075 if (!arc_released(db->db_buf)) {
2076 ASSERT(dr->dt.dl.dr_override_state ==
2077 DR_OVERRIDDEN);
2078 arc_release(db->db_buf, db);
2079 }
2080 dr->dt.dl.dr_data = buf;
2081 arc_buf_destroy(db->db_buf, db);
2082 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2083 arc_release(db->db_buf, db);
2084 arc_buf_destroy(db->db_buf, db);
2085 }
2086 db->db_buf = NULL;
2087 }
2088 ASSERT(db->db_buf == NULL);
2089 dbuf_set_data(db, buf);
2090 db->db_state = DB_FILL;
2091 mutex_exit(&db->db_mtx);
2092 (void) dbuf_dirty(db, tx);
2093 dmu_buf_fill_done(&db->db, tx);
2094 }
2095
2096 void
2097 dbuf_destroy(dmu_buf_impl_t *db)
2098 {
2099 dnode_t *dn;
2100 dmu_buf_impl_t *parent = db->db_parent;
2101 dmu_buf_impl_t *dndb;
2102
2103 ASSERT(MUTEX_HELD(&db->db_mtx));
2104 ASSERT(refcount_is_zero(&db->db_holds));
2105
2106 if (db->db_buf != NULL) {
2107 arc_buf_destroy(db->db_buf, db);
2108 db->db_buf = NULL;
2109 }
2110
2111 if (db->db_blkid == DMU_BONUS_BLKID) {
2112 int slots = DB_DNODE(db)->dn_num_slots;
2113 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
2114 ASSERT(db->db.db_data != NULL);
2115 kmem_free(db->db.db_data, bonuslen);
2116 arc_space_return(bonuslen, ARC_SPACE_BONUS);
2117 db->db_state = DB_UNCACHED;
2118 }
2119
2120 dbuf_clear_data(db);
2121
2122 if (multilist_link_active(&db->db_cache_link)) {
2123 multilist_remove(dbuf_cache, db);
2124 (void) refcount_remove_many(&dbuf_cache_size,
2125 db->db.db_size, db);
2126 }
2127
2128 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2129 ASSERT(db->db_data_pending == NULL);
2130
2131 db->db_state = DB_EVICTING;
2132 db->db_blkptr = NULL;
2133
2134 /*
2135 * Now that db_state is DB_EVICTING, nobody else can find this via
2136 * the hash table. We can now drop db_mtx, which allows us to
2137 * acquire the dn_dbufs_mtx.
2138 */
2139 mutex_exit(&db->db_mtx);
2140
2141 DB_DNODE_ENTER(db);
2142 dn = DB_DNODE(db);
2143 dndb = dn->dn_dbuf;
2144 if (db->db_blkid != DMU_BONUS_BLKID) {
2145 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2146 if (needlock)
2147 mutex_enter(&dn->dn_dbufs_mtx);
2148 avl_remove(&dn->dn_dbufs, db);
2149 atomic_dec_32(&dn->dn_dbufs_count);
2150 membar_producer();
2151 DB_DNODE_EXIT(db);
2152 if (needlock)
2153 mutex_exit(&dn->dn_dbufs_mtx);
2154 /*
2155 * Decrementing the dbuf count means that the hold corresponding
2156 * to the removed dbuf is no longer discounted in dnode_move(),
2157 * so the dnode cannot be moved until after we release the hold.
2158 * The membar_producer() ensures visibility of the decremented
2159 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2160 * release any lock.
2161 */
2162 dnode_rele(dn, db);
2163 db->db_dnode_handle = NULL;
2164
2165 dbuf_hash_remove(db);
2166 } else {
2167 DB_DNODE_EXIT(db);
2168 }
2169
2170 ASSERT(refcount_is_zero(&db->db_holds));
2171
2172 db->db_parent = NULL;
2173
2174 ASSERT(db->db_buf == NULL);
2175 ASSERT(db->db.db_data == NULL);
2176 ASSERT(db->db_hash_next == NULL);
2177 ASSERT(db->db_blkptr == NULL);
2178 ASSERT(db->db_data_pending == NULL);
2179 ASSERT(!multilist_link_active(&db->db_cache_link));
2180
2181 kmem_cache_free(dbuf_kmem_cache, db);
2182 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2183
2184 /*
2185 * If this dbuf is referenced from an indirect dbuf,
2186 * decrement the ref count on the indirect dbuf.
2187 */
2188 if (parent && parent != dndb)
2189 dbuf_rele(parent, db);
2190 }
2191
2192 /*
2193 * Note: While bpp will always be updated if the function returns success,
2194 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2195 * this happens when the dnode is the meta-dnode, or a userused or groupused
2196 * object.
2197 */
2198 __attribute__((always_inline))
2199 static inline int
2200 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2201 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
2202 {
2203 int nlevels, epbs;
2204
2205 *parentp = NULL;
2206 *bpp = NULL;
2207
2208 ASSERT(blkid != DMU_BONUS_BLKID);
2209
2210 if (blkid == DMU_SPILL_BLKID) {
2211 mutex_enter(&dn->dn_mtx);
2212 if (dn->dn_have_spill &&
2213 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2214 *bpp = DN_SPILL_BLKPTR(dn->dn_phys);
2215 else
2216 *bpp = NULL;
2217 dbuf_add_ref(dn->dn_dbuf, NULL);
2218 *parentp = dn->dn_dbuf;
2219 mutex_exit(&dn->dn_mtx);
2220 return (0);
2221 }
2222
2223 nlevels =
2224 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2225 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2226
2227 ASSERT3U(level * epbs, <, 64);
2228 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2229 /*
2230 * This assertion shouldn't trip as long as the max indirect block size
2231 * is less than 1M. The reason for this is that up to that point,
2232 * the number of levels required to address an entire object with blocks
2233 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2234 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2235 * (i.e. we can address the entire object), objects will all use at most
2236 * N-1 levels and the assertion won't overflow. However, once epbs is
2237 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2238 * enough to address an entire object, so objects will have 5 levels,
2239 * but then this assertion will overflow.
2240 *
2241 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2242 * need to redo this logic to handle overflows.
2243 */
2244 ASSERT(level >= nlevels ||
2245 ((nlevels - level - 1) * epbs) +
2246 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2247 if (level >= nlevels ||
2248 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2249 ((nlevels - level - 1) * epbs)) ||
2250 (fail_sparse &&
2251 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2252 /* the buffer has no parent yet */
2253 return (SET_ERROR(ENOENT));
2254 } else if (level < nlevels-1) {
2255 /* this block is referenced from an indirect block */
2256 int err;
2257 if (dh == NULL) {
2258 err = dbuf_hold_impl(dn, level+1,
2259 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2260 } else {
2261 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
2262 blkid >> epbs, fail_sparse, FALSE, NULL,
2263 parentp, dh->dh_depth + 1);
2264 err = __dbuf_hold_impl(dh + 1);
2265 }
2266 if (err)
2267 return (err);
2268 err = dbuf_read(*parentp, NULL,
2269 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2270 if (err) {
2271 dbuf_rele(*parentp, NULL);
2272 *parentp = NULL;
2273 return (err);
2274 }
2275 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2276 (blkid & ((1ULL << epbs) - 1));
2277 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2278 ASSERT(BP_IS_HOLE(*bpp));
2279 return (0);
2280 } else {
2281 /* the block is referenced from the dnode */
2282 ASSERT3U(level, ==, nlevels-1);
2283 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2284 blkid < dn->dn_phys->dn_nblkptr);
2285 if (dn->dn_dbuf) {
2286 dbuf_add_ref(dn->dn_dbuf, NULL);
2287 *parentp = dn->dn_dbuf;
2288 }
2289 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2290 return (0);
2291 }
2292 }
2293
2294 static dmu_buf_impl_t *
2295 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2296 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2297 {
2298 objset_t *os = dn->dn_objset;
2299 dmu_buf_impl_t *db, *odb;
2300
2301 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2302 ASSERT(dn->dn_type != DMU_OT_NONE);
2303
2304 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2305
2306 db->db_objset = os;
2307 db->db.db_object = dn->dn_object;
2308 db->db_level = level;
2309 db->db_blkid = blkid;
2310 db->db_last_dirty = NULL;
2311 db->db_dirtycnt = 0;
2312 db->db_dnode_handle = dn->dn_handle;
2313 db->db_parent = parent;
2314 db->db_blkptr = blkptr;
2315
2316 db->db_user = NULL;
2317 db->db_user_immediate_evict = FALSE;
2318 db->db_freed_in_flight = FALSE;
2319 db->db_pending_evict = FALSE;
2320
2321 if (blkid == DMU_BONUS_BLKID) {
2322 ASSERT3P(parent, ==, dn->dn_dbuf);
2323 db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
2324 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2325 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2326 db->db.db_offset = DMU_BONUS_BLKID;
2327 db->db_state = DB_UNCACHED;
2328 /* the bonus dbuf is not placed in the hash table */
2329 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2330 return (db);
2331 } else if (blkid == DMU_SPILL_BLKID) {
2332 db->db.db_size = (blkptr != NULL) ?
2333 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2334 db->db.db_offset = 0;
2335 } else {
2336 int blocksize =
2337 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2338 db->db.db_size = blocksize;
2339 db->db.db_offset = db->db_blkid * blocksize;
2340 }
2341
2342 /*
2343 * Hold the dn_dbufs_mtx while we get the new dbuf
2344 * in the hash table *and* added to the dbufs list.
2345 * This prevents a possible deadlock with someone
2346 * trying to look up this dbuf before its added to the
2347 * dn_dbufs list.
2348 */
2349 mutex_enter(&dn->dn_dbufs_mtx);
2350 db->db_state = DB_EVICTING;
2351 if ((odb = dbuf_hash_insert(db)) != NULL) {
2352 /* someone else inserted it first */
2353 kmem_cache_free(dbuf_kmem_cache, db);
2354 mutex_exit(&dn->dn_dbufs_mtx);
2355 return (odb);
2356 }
2357 avl_add(&dn->dn_dbufs, db);
2358
2359 db->db_state = DB_UNCACHED;
2360 mutex_exit(&dn->dn_dbufs_mtx);
2361 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2362
2363 if (parent && parent != dn->dn_dbuf)
2364 dbuf_add_ref(parent, db);
2365
2366 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2367 refcount_count(&dn->dn_holds) > 0);
2368 (void) refcount_add(&dn->dn_holds, db);
2369 atomic_inc_32(&dn->dn_dbufs_count);
2370
2371 dprintf_dbuf(db, "db=%p\n", db);
2372
2373 return (db);
2374 }
2375
2376 typedef struct dbuf_prefetch_arg {
2377 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2378 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2379 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2380 int dpa_curlevel; /* The current level that we're reading */
2381 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2382 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2383 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2384 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2385 } dbuf_prefetch_arg_t;
2386
2387 /*
2388 * Actually issue the prefetch read for the block given.
2389 */
2390 static void
2391 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2392 {
2393 arc_flags_t aflags;
2394 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2395 return;
2396
2397 aflags = dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2398
2399 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2400 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2401 ASSERT(dpa->dpa_zio != NULL);
2402 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2403 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2404 &aflags, &dpa->dpa_zb);
2405 }
2406
2407 /*
2408 * Called when an indirect block above our prefetch target is read in. This
2409 * will either read in the next indirect block down the tree or issue the actual
2410 * prefetch if the next block down is our target.
2411 */
2412 static void
2413 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2414 {
2415 dbuf_prefetch_arg_t *dpa = private;
2416 uint64_t nextblkid;
2417 blkptr_t *bp;
2418
2419 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2420 ASSERT3S(dpa->dpa_curlevel, >, 0);
2421
2422 /*
2423 * The dpa_dnode is only valid if we are called with a NULL
2424 * zio. This indicates that the arc_read() returned without
2425 * first calling zio_read() to issue a physical read. Once
2426 * a physical read is made the dpa_dnode must be invalidated
2427 * as the locks guarding it may have been dropped. If the
2428 * dpa_dnode is still valid, then we want to add it to the dbuf
2429 * cache. To do so, we must hold the dbuf associated with the block
2430 * we just prefetched, read its contents so that we associate it
2431 * with an arc_buf_t, and then release it.
2432 */
2433 if (zio != NULL) {
2434 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2435 if (zio->io_flags & ZIO_FLAG_RAW) {
2436 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2437 } else {
2438 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2439 }
2440 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2441
2442 dpa->dpa_dnode = NULL;
2443 } else if (dpa->dpa_dnode != NULL) {
2444 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2445 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2446 dpa->dpa_zb.zb_level));
2447 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2448 dpa->dpa_curlevel, curblkid, FTAG);
2449 (void) dbuf_read(db, NULL,
2450 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2451 dbuf_rele(db, FTAG);
2452 }
2453
2454 dpa->dpa_curlevel--;
2455
2456 nextblkid = dpa->dpa_zb.zb_blkid >>
2457 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2458 bp = ((blkptr_t *)abuf->b_data) +
2459 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2460 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2461 kmem_free(dpa, sizeof (*dpa));
2462 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2463 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2464 dbuf_issue_final_prefetch(dpa, bp);
2465 kmem_free(dpa, sizeof (*dpa));
2466 } else {
2467 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2468 zbookmark_phys_t zb;
2469
2470 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2471
2472 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2473 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2474
2475 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2476 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2477 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2478 &iter_aflags, &zb);
2479 }
2480
2481 arc_buf_destroy(abuf, private);
2482 }
2483
2484 /*
2485 * Issue prefetch reads for the given block on the given level. If the indirect
2486 * blocks above that block are not in memory, we will read them in
2487 * asynchronously. As a result, this call never blocks waiting for a read to
2488 * complete.
2489 */
2490 void
2491 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2492 arc_flags_t aflags)
2493 {
2494 blkptr_t bp;
2495 int epbs, nlevels, curlevel;
2496 uint64_t curblkid;
2497 dmu_buf_impl_t *db;
2498 zio_t *pio;
2499 dbuf_prefetch_arg_t *dpa;
2500 dsl_dataset_t *ds;
2501
2502 ASSERT(blkid != DMU_BONUS_BLKID);
2503 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2504
2505 if (blkid > dn->dn_maxblkid)
2506 return;
2507
2508 if (dnode_block_freed(dn, blkid))
2509 return;
2510
2511 /*
2512 * This dnode hasn't been written to disk yet, so there's nothing to
2513 * prefetch.
2514 */
2515 nlevels = dn->dn_phys->dn_nlevels;
2516 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2517 return;
2518
2519 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2520 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2521 return;
2522
2523 db = dbuf_find(dn->dn_objset, dn->dn_object,
2524 level, blkid);
2525 if (db != NULL) {
2526 mutex_exit(&db->db_mtx);
2527 /*
2528 * This dbuf already exists. It is either CACHED, or
2529 * (we assume) about to be read or filled.
2530 */
2531 return;
2532 }
2533
2534 /*
2535 * Find the closest ancestor (indirect block) of the target block
2536 * that is present in the cache. In this indirect block, we will
2537 * find the bp that is at curlevel, curblkid.
2538 */
2539 curlevel = level;
2540 curblkid = blkid;
2541 while (curlevel < nlevels - 1) {
2542 int parent_level = curlevel + 1;
2543 uint64_t parent_blkid = curblkid >> epbs;
2544 dmu_buf_impl_t *db;
2545
2546 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2547 FALSE, TRUE, FTAG, &db) == 0) {
2548 blkptr_t *bpp = db->db_buf->b_data;
2549 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2550 dbuf_rele(db, FTAG);
2551 break;
2552 }
2553
2554 curlevel = parent_level;
2555 curblkid = parent_blkid;
2556 }
2557
2558 if (curlevel == nlevels - 1) {
2559 /* No cached indirect blocks found. */
2560 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2561 bp = dn->dn_phys->dn_blkptr[curblkid];
2562 }
2563 if (BP_IS_HOLE(&bp))
2564 return;
2565
2566 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2567
2568 pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2569 ZIO_FLAG_CANFAIL);
2570
2571 dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2572 ds = dn->dn_objset->os_dsl_dataset;
2573 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2574 dn->dn_object, level, blkid);
2575 dpa->dpa_curlevel = curlevel;
2576 dpa->dpa_prio = prio;
2577 dpa->dpa_aflags = aflags;
2578 dpa->dpa_spa = dn->dn_objset->os_spa;
2579 dpa->dpa_dnode = dn;
2580 dpa->dpa_epbs = epbs;
2581 dpa->dpa_zio = pio;
2582
2583 /*
2584 * If we have the indirect just above us, no need to do the asynchronous
2585 * prefetch chain; we'll just run the last step ourselves. If we're at
2586 * a higher level, though, we want to issue the prefetches for all the
2587 * indirect blocks asynchronously, so we can go on with whatever we were
2588 * doing.
2589 */
2590 if (curlevel == level) {
2591 ASSERT3U(curblkid, ==, blkid);
2592 dbuf_issue_final_prefetch(dpa, &bp);
2593 kmem_free(dpa, sizeof (*dpa));
2594 } else {
2595 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2596 zbookmark_phys_t zb;
2597
2598 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2599 dn->dn_object, curlevel, curblkid);
2600 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2601 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2602 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2603 &iter_aflags, &zb);
2604 }
2605 /*
2606 * We use pio here instead of dpa_zio since it's possible that
2607 * dpa may have already been freed.
2608 */
2609 zio_nowait(pio);
2610 }
2611
2612 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2613
2614 /*
2615 * Returns with db_holds incremented, and db_mtx not held.
2616 * Note: dn_struct_rwlock must be held.
2617 */
2618 static int
2619 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
2620 {
2621 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
2622 dh->dh_parent = NULL;
2623
2624 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
2625 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
2626 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
2627
2628 *(dh->dh_dbp) = NULL;
2629
2630 /* dbuf_find() returns with db_mtx held */
2631 dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2632 dh->dh_level, dh->dh_blkid);
2633
2634 if (dh->dh_db == NULL) {
2635 dh->dh_bp = NULL;
2636
2637 if (dh->dh_fail_uncached)
2638 return (SET_ERROR(ENOENT));
2639
2640 ASSERT3P(dh->dh_parent, ==, NULL);
2641 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2642 dh->dh_fail_sparse, &dh->dh_parent, &dh->dh_bp, dh);
2643 if (dh->dh_fail_sparse) {
2644 if (dh->dh_err == 0 &&
2645 dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
2646 dh->dh_err = SET_ERROR(ENOENT);
2647 if (dh->dh_err) {
2648 if (dh->dh_parent)
2649 dbuf_rele(dh->dh_parent, NULL);
2650 return (dh->dh_err);
2651 }
2652 }
2653 if (dh->dh_err && dh->dh_err != ENOENT)
2654 return (dh->dh_err);
2655 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2656 dh->dh_parent, dh->dh_bp);
2657 }
2658
2659 if (dh->dh_fail_uncached && dh->dh_db->db_state != DB_CACHED) {
2660 mutex_exit(&dh->dh_db->db_mtx);
2661 return (SET_ERROR(ENOENT));
2662 }
2663
2664 if (dh->dh_db->db_buf != NULL)
2665 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
2666
2667 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
2668
2669 /*
2670 * If this buffer is currently syncing out, and we are are
2671 * still referencing it from db_data, we need to make a copy
2672 * of it in case we decide we want to dirty it again in this txg.
2673 */
2674 if (dh->dh_db->db_level == 0 &&
2675 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
2676 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
2677 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
2678 dh->dh_dr = dh->dh_db->db_data_pending;
2679
2680 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
2681 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
2682
2683 dbuf_set_data(dh->dh_db,
2684 arc_alloc_buf(dh->dh_dn->dn_objset->os_spa,
2685 dh->dh_db, dh->dh_type, dh->dh_db->db.db_size));
2686 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
2687 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
2688 }
2689 }
2690
2691 if (multilist_link_active(&dh->dh_db->db_cache_link)) {
2692 ASSERT(refcount_is_zero(&dh->dh_db->db_holds));
2693 multilist_remove(dbuf_cache, dh->dh_db);
2694 (void) refcount_remove_many(&dbuf_cache_size,
2695 dh->dh_db->db.db_size, dh->dh_db);
2696 }
2697 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
2698 DBUF_VERIFY(dh->dh_db);
2699 mutex_exit(&dh->dh_db->db_mtx);
2700
2701 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2702 if (dh->dh_parent)
2703 dbuf_rele(dh->dh_parent, NULL);
2704
2705 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
2706 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
2707 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
2708 *(dh->dh_dbp) = dh->dh_db;
2709
2710 return (0);
2711 }
2712
2713 /*
2714 * The following code preserves the recursive function dbuf_hold_impl()
2715 * but moves the local variables AND function arguments to the heap to
2716 * minimize the stack frame size. Enough space is initially allocated
2717 * on the stack for 20 levels of recursion.
2718 */
2719 int
2720 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2721 boolean_t fail_sparse, boolean_t fail_uncached,
2722 void *tag, dmu_buf_impl_t **dbp)
2723 {
2724 struct dbuf_hold_impl_data *dh;
2725 int error;
2726
2727 dh = kmem_alloc(sizeof (struct dbuf_hold_impl_data) *
2728 DBUF_HOLD_IMPL_MAX_DEPTH, KM_SLEEP);
2729 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse,
2730 fail_uncached, tag, dbp, 0);
2731
2732 error = __dbuf_hold_impl(dh);
2733
2734 kmem_free(dh, sizeof (struct dbuf_hold_impl_data) *
2735 DBUF_HOLD_IMPL_MAX_DEPTH);
2736
2737 return (error);
2738 }
2739
2740 static void
2741 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2742 dnode_t *dn, uint8_t level, uint64_t blkid,
2743 boolean_t fail_sparse, boolean_t fail_uncached,
2744 void *tag, dmu_buf_impl_t **dbp, int depth)
2745 {
2746 dh->dh_dn = dn;
2747 dh->dh_level = level;
2748 dh->dh_blkid = blkid;
2749
2750 dh->dh_fail_sparse = fail_sparse;
2751 dh->dh_fail_uncached = fail_uncached;
2752
2753 dh->dh_tag = tag;
2754 dh->dh_dbp = dbp;
2755
2756 dh->dh_db = NULL;
2757 dh->dh_parent = NULL;
2758 dh->dh_bp = NULL;
2759 dh->dh_err = 0;
2760 dh->dh_dr = NULL;
2761 dh->dh_type = 0;
2762
2763 dh->dh_depth = depth;
2764 }
2765
2766 dmu_buf_impl_t *
2767 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2768 {
2769 return (dbuf_hold_level(dn, 0, blkid, tag));
2770 }
2771
2772 dmu_buf_impl_t *
2773 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2774 {
2775 dmu_buf_impl_t *db;
2776 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2777 return (err ? NULL : db);
2778 }
2779
2780 void
2781 dbuf_create_bonus(dnode_t *dn)
2782 {
2783 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2784
2785 ASSERT(dn->dn_bonus == NULL);
2786 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2787 }
2788
2789 int
2790 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2791 {
2792 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2793 dnode_t *dn;
2794
2795 if (db->db_blkid != DMU_SPILL_BLKID)
2796 return (SET_ERROR(ENOTSUP));
2797 if (blksz == 0)
2798 blksz = SPA_MINBLOCKSIZE;
2799 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2800 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2801
2802 DB_DNODE_ENTER(db);
2803 dn = DB_DNODE(db);
2804 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2805 dbuf_new_size(db, blksz, tx);
2806 rw_exit(&dn->dn_struct_rwlock);
2807 DB_DNODE_EXIT(db);
2808
2809 return (0);
2810 }
2811
2812 void
2813 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2814 {
2815 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2816 }
2817
2818 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2819 void
2820 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2821 {
2822 int64_t holds = refcount_add(&db->db_holds, tag);
2823 VERIFY3S(holds, >, 1);
2824 }
2825
2826 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2827 boolean_t
2828 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2829 void *tag)
2830 {
2831 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2832 dmu_buf_impl_t *found_db;
2833 boolean_t result = B_FALSE;
2834
2835 if (blkid == DMU_BONUS_BLKID)
2836 found_db = dbuf_find_bonus(os, obj);
2837 else
2838 found_db = dbuf_find(os, obj, 0, blkid);
2839
2840 if (found_db != NULL) {
2841 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2842 (void) refcount_add(&db->db_holds, tag);
2843 result = B_TRUE;
2844 }
2845 mutex_exit(&found_db->db_mtx);
2846 }
2847 return (result);
2848 }
2849
2850 /*
2851 * If you call dbuf_rele() you had better not be referencing the dnode handle
2852 * unless you have some other direct or indirect hold on the dnode. (An indirect
2853 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2854 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2855 * dnode's parent dbuf evicting its dnode handles.
2856 */
2857 void
2858 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2859 {
2860 mutex_enter(&db->db_mtx);
2861 dbuf_rele_and_unlock(db, tag);
2862 }
2863
2864 void
2865 dmu_buf_rele(dmu_buf_t *db, void *tag)
2866 {
2867 dbuf_rele((dmu_buf_impl_t *)db, tag);
2868 }
2869
2870 /*
2871 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2872 * db_dirtycnt and db_holds to be updated atomically.
2873 */
2874 void
2875 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2876 {
2877 int64_t holds;
2878
2879 ASSERT(MUTEX_HELD(&db->db_mtx));
2880 DBUF_VERIFY(db);
2881
2882 /*
2883 * Remove the reference to the dbuf before removing its hold on the
2884 * dnode so we can guarantee in dnode_move() that a referenced bonus
2885 * buffer has a corresponding dnode hold.
2886 */
2887 holds = refcount_remove(&db->db_holds, tag);
2888 ASSERT(holds >= 0);
2889
2890 /*
2891 * We can't freeze indirects if there is a possibility that they
2892 * may be modified in the current syncing context.
2893 */
2894 if (db->db_buf != NULL &&
2895 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2896 arc_buf_freeze(db->db_buf);
2897 }
2898
2899 if (holds == db->db_dirtycnt &&
2900 db->db_level == 0 && db->db_user_immediate_evict)
2901 dbuf_evict_user(db);
2902
2903 if (holds == 0) {
2904 if (db->db_blkid == DMU_BONUS_BLKID) {
2905 dnode_t *dn;
2906 boolean_t evict_dbuf = db->db_pending_evict;
2907
2908 /*
2909 * If the dnode moves here, we cannot cross this
2910 * barrier until the move completes.
2911 */
2912 DB_DNODE_ENTER(db);
2913
2914 dn = DB_DNODE(db);
2915 atomic_dec_32(&dn->dn_dbufs_count);
2916
2917 /*
2918 * Decrementing the dbuf count means that the bonus
2919 * buffer's dnode hold is no longer discounted in
2920 * dnode_move(). The dnode cannot move until after
2921 * the dnode_rele() below.
2922 */
2923 DB_DNODE_EXIT(db);
2924
2925 /*
2926 * Do not reference db after its lock is dropped.
2927 * Another thread may evict it.
2928 */
2929 mutex_exit(&db->db_mtx);
2930
2931 if (evict_dbuf)
2932 dnode_evict_bonus(dn);
2933
2934 dnode_rele(dn, db);
2935 } else if (db->db_buf == NULL) {
2936 /*
2937 * This is a special case: we never associated this
2938 * dbuf with any data allocated from the ARC.
2939 */
2940 ASSERT(db->db_state == DB_UNCACHED ||
2941 db->db_state == DB_NOFILL);
2942 dbuf_destroy(db);
2943 } else if (arc_released(db->db_buf)) {
2944 /*
2945 * This dbuf has anonymous data associated with it.
2946 */
2947 dbuf_destroy(db);
2948 } else {
2949 boolean_t do_arc_evict = B_FALSE;
2950 blkptr_t bp;
2951 spa_t *spa = dmu_objset_spa(db->db_objset);
2952
2953 if (!DBUF_IS_CACHEABLE(db) &&
2954 db->db_blkptr != NULL &&
2955 !BP_IS_HOLE(db->db_blkptr) &&
2956 !BP_IS_EMBEDDED(db->db_blkptr)) {
2957 do_arc_evict = B_TRUE;
2958 bp = *db->db_blkptr;
2959 }
2960
2961 if (!DBUF_IS_CACHEABLE(db) ||
2962 db->db_pending_evict) {
2963 dbuf_destroy(db);
2964 } else if (!multilist_link_active(&db->db_cache_link)) {
2965 multilist_insert(dbuf_cache, db);
2966 (void) refcount_add_many(&dbuf_cache_size,
2967 db->db.db_size, db);
2968 mutex_exit(&db->db_mtx);
2969
2970 dbuf_evict_notify();
2971 }
2972
2973 if (do_arc_evict)
2974 arc_freed(spa, &bp);
2975 }
2976 } else {
2977 mutex_exit(&db->db_mtx);
2978 }
2979
2980 }
2981
2982 #pragma weak dmu_buf_refcount = dbuf_refcount
2983 uint64_t
2984 dbuf_refcount(dmu_buf_impl_t *db)
2985 {
2986 return (refcount_count(&db->db_holds));
2987 }
2988
2989 void *
2990 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2991 dmu_buf_user_t *new_user)
2992 {
2993 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2994
2995 mutex_enter(&db->db_mtx);
2996 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2997 if (db->db_user == old_user)
2998 db->db_user = new_user;
2999 else
3000 old_user = db->db_user;
3001 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3002 mutex_exit(&db->db_mtx);
3003
3004 return (old_user);
3005 }
3006
3007 void *
3008 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3009 {
3010 return (dmu_buf_replace_user(db_fake, NULL, user));
3011 }
3012
3013 void *
3014 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3015 {
3016 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3017
3018 db->db_user_immediate_evict = TRUE;
3019 return (dmu_buf_set_user(db_fake, user));
3020 }
3021
3022 void *
3023 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3024 {
3025 return (dmu_buf_replace_user(db_fake, user, NULL));
3026 }
3027
3028 void *
3029 dmu_buf_get_user(dmu_buf_t *db_fake)
3030 {
3031 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3032
3033 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3034 return (db->db_user);
3035 }
3036
3037 void
3038 dmu_buf_user_evict_wait()
3039 {
3040 taskq_wait(dbu_evict_taskq);
3041 }
3042
3043 blkptr_t *
3044 dmu_buf_get_blkptr(dmu_buf_t *db)
3045 {
3046 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3047 return (dbi->db_blkptr);
3048 }
3049
3050 objset_t *
3051 dmu_buf_get_objset(dmu_buf_t *db)
3052 {
3053 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3054 return (dbi->db_objset);
3055 }
3056
3057 dnode_t *
3058 dmu_buf_dnode_enter(dmu_buf_t *db)
3059 {
3060 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3061 DB_DNODE_ENTER(dbi);
3062 return (DB_DNODE(dbi));
3063 }
3064
3065 void
3066 dmu_buf_dnode_exit(dmu_buf_t *db)
3067 {
3068 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3069 DB_DNODE_EXIT(dbi);
3070 }
3071
3072 static void
3073 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
3074 {
3075 /* ASSERT(dmu_tx_is_syncing(tx) */
3076 ASSERT(MUTEX_HELD(&db->db_mtx));
3077
3078 if (db->db_blkptr != NULL)
3079 return;
3080
3081 if (db->db_blkid == DMU_SPILL_BLKID) {
3082 db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys);
3083 BP_ZERO(db->db_blkptr);
3084 return;
3085 }
3086 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
3087 /*
3088 * This buffer was allocated at a time when there was
3089 * no available blkptrs from the dnode, or it was
3090 * inappropriate to hook it in (i.e., nlevels mis-match).
3091 */
3092 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
3093 ASSERT(db->db_parent == NULL);
3094 db->db_parent = dn->dn_dbuf;
3095 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
3096 DBUF_VERIFY(db);
3097 } else {
3098 dmu_buf_impl_t *parent = db->db_parent;
3099 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3100
3101 ASSERT(dn->dn_phys->dn_nlevels > 1);
3102 if (parent == NULL) {
3103 mutex_exit(&db->db_mtx);
3104 rw_enter(&dn->dn_struct_rwlock, RW_READER);
3105 parent = dbuf_hold_level(dn, db->db_level + 1,
3106 db->db_blkid >> epbs, db);
3107 rw_exit(&dn->dn_struct_rwlock);
3108 mutex_enter(&db->db_mtx);
3109 db->db_parent = parent;
3110 }
3111 db->db_blkptr = (blkptr_t *)parent->db.db_data +
3112 (db->db_blkid & ((1ULL << epbs) - 1));
3113 DBUF_VERIFY(db);
3114 }
3115 }
3116
3117 /*
3118 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3119 * is critical the we not allow the compiler to inline this function in to
3120 * dbuf_sync_list() thereby drastically bloating the stack usage.
3121 */
3122 noinline static void
3123 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3124 {
3125 dmu_buf_impl_t *db = dr->dr_dbuf;
3126 dnode_t *dn;
3127 zio_t *zio;
3128
3129 ASSERT(dmu_tx_is_syncing(tx));
3130
3131 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3132
3133 mutex_enter(&db->db_mtx);
3134
3135 ASSERT(db->db_level > 0);
3136 DBUF_VERIFY(db);
3137
3138 /* Read the block if it hasn't been read yet. */
3139 if (db->db_buf == NULL) {
3140 mutex_exit(&db->db_mtx);
3141 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3142 mutex_enter(&db->db_mtx);
3143 }
3144 ASSERT3U(db->db_state, ==, DB_CACHED);
3145 ASSERT(db->db_buf != NULL);
3146
3147 DB_DNODE_ENTER(db);
3148 dn = DB_DNODE(db);
3149 /* Indirect block size must match what the dnode thinks it is. */
3150 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3151 dbuf_check_blkptr(dn, db);
3152 DB_DNODE_EXIT(db);
3153
3154 /* Provide the pending dirty record to child dbufs */
3155 db->db_data_pending = dr;
3156
3157 mutex_exit(&db->db_mtx);
3158 dbuf_write(dr, db->db_buf, tx);
3159
3160 zio = dr->dr_zio;
3161 mutex_enter(&dr->dt.di.dr_mtx);
3162 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3163 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3164 mutex_exit(&dr->dt.di.dr_mtx);
3165 zio_nowait(zio);
3166 }
3167
3168 /*
3169 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3170 * critical the we not allow the compiler to inline this function in to
3171 * dbuf_sync_list() thereby drastically bloating the stack usage.
3172 */
3173 noinline static void
3174 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3175 {
3176 arc_buf_t **datap = &dr->dt.dl.dr_data;
3177 dmu_buf_impl_t *db = dr->dr_dbuf;
3178 dnode_t *dn;
3179 objset_t *os;
3180 uint64_t txg = tx->tx_txg;
3181
3182 ASSERT(dmu_tx_is_syncing(tx));
3183
3184 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3185
3186 mutex_enter(&db->db_mtx);
3187 /*
3188 * To be synced, we must be dirtied. But we
3189 * might have been freed after the dirty.
3190 */
3191 if (db->db_state == DB_UNCACHED) {
3192 /* This buffer has been freed since it was dirtied */
3193 ASSERT(db->db.db_data == NULL);
3194 } else if (db->db_state == DB_FILL) {
3195 /* This buffer was freed and is now being re-filled */
3196 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3197 } else {
3198 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3199 }
3200 DBUF_VERIFY(db);
3201
3202 DB_DNODE_ENTER(db);
3203 dn = DB_DNODE(db);
3204
3205 if (db->db_blkid == DMU_SPILL_BLKID) {
3206 mutex_enter(&dn->dn_mtx);
3207 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
3208 /*
3209 * In the previous transaction group, the bonus buffer
3210 * was entirely used to store the attributes for the
3211 * dnode which overrode the dn_spill field. However,
3212 * when adding more attributes to the file a spill
3213 * block was required to hold the extra attributes.
3214 *
3215 * Make sure to clear the garbage left in the dn_spill
3216 * field from the previous attributes in the bonus
3217 * buffer. Otherwise, after writing out the spill
3218 * block to the new allocated dva, it will free
3219 * the old block pointed to by the invalid dn_spill.
3220 */
3221 db->db_blkptr = NULL;
3222 }
3223 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3224 mutex_exit(&dn->dn_mtx);
3225 }
3226
3227 /*
3228 * If this is a bonus buffer, simply copy the bonus data into the
3229 * dnode. It will be written out when the dnode is synced (and it
3230 * will be synced, since it must have been dirty for dbuf_sync to
3231 * be called).
3232 */
3233 if (db->db_blkid == DMU_BONUS_BLKID) {
3234 dbuf_dirty_record_t **drp;
3235
3236 ASSERT(*datap != NULL);
3237 ASSERT0(db->db_level);
3238 ASSERT3U(dn->dn_phys->dn_bonuslen, <=,
3239 DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1));
3240 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3241 DB_DNODE_EXIT(db);
3242
3243 if (*datap != db->db.db_data) {
3244 int slots = DB_DNODE(db)->dn_num_slots;
3245 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
3246 kmem_free(*datap, bonuslen);
3247 arc_space_return(bonuslen, ARC_SPACE_BONUS);
3248 }
3249 db->db_data_pending = NULL;
3250 drp = &db->db_last_dirty;
3251 while (*drp != dr)
3252 drp = &(*drp)->dr_next;
3253 ASSERT(dr->dr_next == NULL);
3254 ASSERT(dr->dr_dbuf == db);
3255 *drp = dr->dr_next;
3256 if (dr->dr_dbuf->db_level != 0) {
3257 mutex_destroy(&dr->dt.di.dr_mtx);
3258 list_destroy(&dr->dt.di.dr_children);
3259 }
3260 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3261 ASSERT(db->db_dirtycnt > 0);
3262 db->db_dirtycnt -= 1;
3263 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3264 return;
3265 }
3266
3267 os = dn->dn_objset;
3268
3269 /*
3270 * This function may have dropped the db_mtx lock allowing a dmu_sync
3271 * operation to sneak in. As a result, we need to ensure that we
3272 * don't check the dr_override_state until we have returned from
3273 * dbuf_check_blkptr.
3274 */
3275 dbuf_check_blkptr(dn, db);
3276
3277 /*
3278 * If this buffer is in the middle of an immediate write,
3279 * wait for the synchronous IO to complete.
3280 */
3281 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3282 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3283 cv_wait(&db->db_changed, &db->db_mtx);
3284 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3285 }
3286
3287 if (db->db_state != DB_NOFILL &&
3288 dn->dn_object != DMU_META_DNODE_OBJECT &&
3289 refcount_count(&db->db_holds) > 1 &&
3290 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3291 *datap == db->db_buf) {
3292 /*
3293 * If this buffer is currently "in use" (i.e., there
3294 * are active holds and db_data still references it),
3295 * then make a copy before we start the write so that
3296 * any modifications from the open txg will not leak
3297 * into this write.
3298 *
3299 * NOTE: this copy does not need to be made for
3300 * objects only modified in the syncing context (e.g.
3301 * DNONE_DNODE blocks).
3302 */
3303 int psize = arc_buf_size(*datap);
3304 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3305 enum zio_compress compress_type = arc_get_compression(*datap);
3306
3307 if (compress_type == ZIO_COMPRESS_OFF) {
3308 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3309 } else {
3310 int lsize = arc_buf_lsize(*datap);
3311 ASSERT3U(type, ==, ARC_BUFC_DATA);
3312 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3313 psize, lsize, compress_type);
3314 }
3315 bcopy(db->db.db_data, (*datap)->b_data, psize);
3316 }
3317 db->db_data_pending = dr;
3318
3319 mutex_exit(&db->db_mtx);
3320
3321 dbuf_write(dr, *datap, tx);
3322
3323 ASSERT(!list_link_active(&dr->dr_dirty_node));
3324 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3325 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3326 DB_DNODE_EXIT(db);
3327 } else {
3328 /*
3329 * Although zio_nowait() does not "wait for an IO", it does
3330 * initiate the IO. If this is an empty write it seems plausible
3331 * that the IO could actually be completed before the nowait
3332 * returns. We need to DB_DNODE_EXIT() first in case
3333 * zio_nowait() invalidates the dbuf.
3334 */
3335 DB_DNODE_EXIT(db);
3336 zio_nowait(dr->dr_zio);
3337 }
3338 }
3339
3340 void
3341 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3342 {
3343 dbuf_dirty_record_t *dr;
3344
3345 while ((dr = list_head(list))) {
3346 if (dr->dr_zio != NULL) {
3347 /*
3348 * If we find an already initialized zio then we
3349 * are processing the meta-dnode, and we have finished.
3350 * The dbufs for all dnodes are put back on the list
3351 * during processing, so that we can zio_wait()
3352 * these IOs after initiating all child IOs.
3353 */
3354 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3355 DMU_META_DNODE_OBJECT);
3356 break;
3357 }
3358 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3359 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3360 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3361 }
3362 list_remove(list, dr);
3363 if (dr->dr_dbuf->db_level > 0)
3364 dbuf_sync_indirect(dr, tx);
3365 else
3366 dbuf_sync_leaf(dr, tx);
3367 }
3368 }
3369
3370 /* ARGSUSED */
3371 static void
3372 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3373 {
3374 dmu_buf_impl_t *db = vdb;
3375 dnode_t *dn;
3376 blkptr_t *bp = zio->io_bp;
3377 blkptr_t *bp_orig = &zio->io_bp_orig;
3378 spa_t *spa = zio->io_spa;
3379 int64_t delta;
3380 uint64_t fill = 0;
3381 int i;
3382
3383 ASSERT3P(db->db_blkptr, !=, NULL);
3384 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3385
3386 DB_DNODE_ENTER(db);
3387 dn = DB_DNODE(db);
3388 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3389 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3390 zio->io_prev_space_delta = delta;
3391
3392 if (bp->blk_birth != 0) {
3393 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3394 BP_GET_TYPE(bp) == dn->dn_type) ||
3395 (db->db_blkid == DMU_SPILL_BLKID &&
3396 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3397 BP_IS_EMBEDDED(bp));
3398 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3399 }
3400
3401 mutex_enter(&db->db_mtx);
3402
3403 #ifdef ZFS_DEBUG
3404 if (db->db_blkid == DMU_SPILL_BLKID) {
3405 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3406 ASSERT(!(BP_IS_HOLE(bp)) &&
3407 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
3408 }
3409 #endif
3410
3411 if (db->db_level == 0) {
3412 mutex_enter(&dn->dn_mtx);
3413 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3414 db->db_blkid != DMU_SPILL_BLKID)
3415 dn->dn_phys->dn_maxblkid = db->db_blkid;
3416 mutex_exit(&dn->dn_mtx);
3417
3418 if (dn->dn_type == DMU_OT_DNODE) {
3419 i = 0;
3420 while (i < db->db.db_size) {
3421 dnode_phys_t *dnp = db->db.db_data + i;
3422
3423 i += DNODE_MIN_SIZE;
3424 if (dnp->dn_type != DMU_OT_NONE) {
3425 fill++;
3426 i += dnp->dn_extra_slots *
3427 DNODE_MIN_SIZE;
3428 }
3429 }
3430 } else {
3431 if (BP_IS_HOLE(bp)) {
3432 fill = 0;
3433 } else {
3434 fill = 1;
3435 }
3436 }
3437 } else {
3438 blkptr_t *ibp = db->db.db_data;
3439 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3440 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3441 if (BP_IS_HOLE(ibp))
3442 continue;
3443 fill += BP_GET_FILL(ibp);
3444 }
3445 }
3446 DB_DNODE_EXIT(db);
3447
3448 if (!BP_IS_EMBEDDED(bp))
3449 bp->blk_fill = fill;
3450
3451 mutex_exit(&db->db_mtx);
3452
3453 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3454 *db->db_blkptr = *bp;
3455 rw_exit(&dn->dn_struct_rwlock);
3456 }
3457
3458 /* ARGSUSED */
3459 /*
3460 * This function gets called just prior to running through the compression
3461 * stage of the zio pipeline. If we're an indirect block comprised of only
3462 * holes, then we want this indirect to be compressed away to a hole. In
3463 * order to do that we must zero out any information about the holes that
3464 * this indirect points to prior to before we try to compress it.
3465 */
3466 static void
3467 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3468 {
3469 dmu_buf_impl_t *db = vdb;
3470 dnode_t *dn;
3471 blkptr_t *bp;
3472 unsigned int epbs, i;
3473
3474 ASSERT3U(db->db_level, >, 0);
3475 DB_DNODE_ENTER(db);
3476 dn = DB_DNODE(db);
3477 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3478 ASSERT3U(epbs, <, 31);
3479
3480 /* Determine if all our children are holes */
3481 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
3482 if (!BP_IS_HOLE(bp))
3483 break;
3484 }
3485
3486 /*
3487 * If all the children are holes, then zero them all out so that
3488 * we may get compressed away.
3489 */
3490 if (i == 1ULL << epbs) {
3491 /*
3492 * We only found holes. Grab the rwlock to prevent
3493 * anybody from reading the blocks we're about to
3494 * zero out.
3495 */
3496 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3497 bzero(db->db.db_data, db->db.db_size);
3498 rw_exit(&dn->dn_struct_rwlock);
3499 }
3500 DB_DNODE_EXIT(db);
3501 }
3502
3503 /*
3504 * The SPA will call this callback several times for each zio - once
3505 * for every physical child i/o (zio->io_phys_children times). This
3506 * allows the DMU to monitor the progress of each logical i/o. For example,
3507 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3508 * block. There may be a long delay before all copies/fragments are completed,
3509 * so this callback allows us to retire dirty space gradually, as the physical
3510 * i/os complete.
3511 */
3512 /* ARGSUSED */
3513 static void
3514 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3515 {
3516 dmu_buf_impl_t *db = arg;
3517 objset_t *os = db->db_objset;
3518 dsl_pool_t *dp = dmu_objset_pool(os);
3519 dbuf_dirty_record_t *dr;
3520 int delta = 0;
3521
3522 dr = db->db_data_pending;
3523 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3524
3525 /*
3526 * The callback will be called io_phys_children times. Retire one
3527 * portion of our dirty space each time we are called. Any rounding
3528 * error will be cleaned up by dsl_pool_sync()'s call to
3529 * dsl_pool_undirty_space().
3530 */
3531 delta = dr->dr_accounted / zio->io_phys_children;
3532 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3533 }
3534
3535 /* ARGSUSED */
3536 static void
3537 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3538 {
3539 dmu_buf_impl_t *db = vdb;
3540 blkptr_t *bp_orig = &zio->io_bp_orig;
3541 blkptr_t *bp = db->db_blkptr;
3542 objset_t *os = db->db_objset;
3543 dmu_tx_t *tx = os->os_synctx;
3544 dbuf_dirty_record_t **drp, *dr;
3545
3546 ASSERT0(zio->io_error);
3547 ASSERT(db->db_blkptr == bp);
3548
3549 /*
3550 * For nopwrites and rewrites we ensure that the bp matches our
3551 * original and bypass all the accounting.
3552 */
3553 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3554 ASSERT(BP_EQUAL(bp, bp_orig));
3555 } else {
3556 dsl_dataset_t *ds = os->os_dsl_dataset;
3557 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3558 dsl_dataset_block_born(ds, bp, tx);
3559 }
3560
3561 mutex_enter(&db->db_mtx);
3562
3563 DBUF_VERIFY(db);
3564
3565 drp = &db->db_last_dirty;
3566 while ((dr = *drp) != db->db_data_pending)
3567 drp = &dr->dr_next;
3568 ASSERT(!list_link_active(&dr->dr_dirty_node));
3569 ASSERT(dr->dr_dbuf == db);
3570 ASSERT(dr->dr_next == NULL);
3571 *drp = dr->dr_next;
3572
3573 #ifdef ZFS_DEBUG
3574 if (db->db_blkid == DMU_SPILL_BLKID) {
3575 dnode_t *dn;
3576
3577 DB_DNODE_ENTER(db);
3578 dn = DB_DNODE(db);
3579 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3580 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3581 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
3582 DB_DNODE_EXIT(db);
3583 }
3584 #endif
3585
3586 if (db->db_level == 0) {
3587 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3588 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3589 if (db->db_state != DB_NOFILL) {
3590 if (dr->dt.dl.dr_data != db->db_buf)
3591 arc_buf_destroy(dr->dt.dl.dr_data, db);
3592 }
3593 } else {
3594 dnode_t *dn;
3595
3596 DB_DNODE_ENTER(db);
3597 dn = DB_DNODE(db);
3598 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3599 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3600 if (!BP_IS_HOLE(db->db_blkptr)) {
3601 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
3602 SPA_BLKPTRSHIFT);
3603 ASSERT3U(db->db_blkid, <=,
3604 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3605 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3606 db->db.db_size);
3607 }
3608 DB_DNODE_EXIT(db);
3609 mutex_destroy(&dr->dt.di.dr_mtx);
3610 list_destroy(&dr->dt.di.dr_children);
3611 }
3612 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3613
3614 cv_broadcast(&db->db_changed);
3615 ASSERT(db->db_dirtycnt > 0);
3616 db->db_dirtycnt -= 1;
3617 db->db_data_pending = NULL;
3618 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3619 }
3620
3621 static void
3622 dbuf_write_nofill_ready(zio_t *zio)
3623 {
3624 dbuf_write_ready(zio, NULL, zio->io_private);
3625 }
3626
3627 static void
3628 dbuf_write_nofill_done(zio_t *zio)
3629 {
3630 dbuf_write_done(zio, NULL, zio->io_private);
3631 }
3632
3633 static void
3634 dbuf_write_override_ready(zio_t *zio)
3635 {
3636 dbuf_dirty_record_t *dr = zio->io_private;
3637 dmu_buf_impl_t *db = dr->dr_dbuf;
3638
3639 dbuf_write_ready(zio, NULL, db);
3640 }
3641
3642 static void
3643 dbuf_write_override_done(zio_t *zio)
3644 {
3645 dbuf_dirty_record_t *dr = zio->io_private;
3646 dmu_buf_impl_t *db = dr->dr_dbuf;
3647 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3648
3649 mutex_enter(&db->db_mtx);
3650 if (!BP_EQUAL(zio->io_bp, obp)) {
3651 if (!BP_IS_HOLE(obp))
3652 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3653 arc_release(dr->dt.dl.dr_data, db);
3654 }
3655 mutex_exit(&db->db_mtx);
3656
3657 dbuf_write_done(zio, NULL, db);
3658
3659 if (zio->io_abd != NULL)
3660 abd_put(zio->io_abd);
3661 }
3662
3663 /* Issue I/O to commit a dirty buffer to disk. */
3664 static void
3665 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3666 {
3667 dmu_buf_impl_t *db = dr->dr_dbuf;
3668 dnode_t *dn;
3669 objset_t *os;
3670 dmu_buf_impl_t *parent = db->db_parent;
3671 uint64_t txg = tx->tx_txg;
3672 zbookmark_phys_t zb;
3673 zio_prop_t zp;
3674 zio_t *zio;
3675 int wp_flag = 0;
3676
3677 ASSERT(dmu_tx_is_syncing(tx));
3678
3679 DB_DNODE_ENTER(db);
3680 dn = DB_DNODE(db);
3681 os = dn->dn_objset;
3682
3683 if (db->db_state != DB_NOFILL) {
3684 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3685 /*
3686 * Private object buffers are released here rather
3687 * than in dbuf_dirty() since they are only modified
3688 * in the syncing context and we don't want the
3689 * overhead of making multiple copies of the data.
3690 */
3691 if (BP_IS_HOLE(db->db_blkptr)) {
3692 arc_buf_thaw(data);
3693 } else {
3694 dbuf_release_bp(db);
3695 }
3696 }
3697 }
3698
3699 if (parent != dn->dn_dbuf) {
3700 /* Our parent is an indirect block. */
3701 /* We have a dirty parent that has been scheduled for write. */
3702 ASSERT(parent && parent->db_data_pending);
3703 /* Our parent's buffer is one level closer to the dnode. */
3704 ASSERT(db->db_level == parent->db_level-1);
3705 /*
3706 * We're about to modify our parent's db_data by modifying
3707 * our block pointer, so the parent must be released.
3708 */
3709 ASSERT(arc_released(parent->db_buf));
3710 zio = parent->db_data_pending->dr_zio;
3711 } else {
3712 /* Our parent is the dnode itself. */
3713 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3714 db->db_blkid != DMU_SPILL_BLKID) ||
3715 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3716 if (db->db_blkid != DMU_SPILL_BLKID)
3717 ASSERT3P(db->db_blkptr, ==,
3718 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3719 zio = dn->dn_zio;
3720 }
3721
3722 ASSERT(db->db_level == 0 || data == db->db_buf);
3723 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3724 ASSERT(zio);
3725
3726 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3727 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3728 db->db.db_object, db->db_level, db->db_blkid);
3729
3730 if (db->db_blkid == DMU_SPILL_BLKID)
3731 wp_flag = WP_SPILL;
3732 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3733
3734 dmu_write_policy(os, dn, db->db_level, wp_flag,
3735 (data != NULL && arc_get_compression(data) != ZIO_COMPRESS_OFF) ?
3736 arc_get_compression(data) : ZIO_COMPRESS_INHERIT, &zp);
3737 DB_DNODE_EXIT(db);
3738
3739 /*
3740 * We copy the blkptr now (rather than when we instantiate the dirty
3741 * record), because its value can change between open context and
3742 * syncing context. We do not need to hold dn_struct_rwlock to read
3743 * db_blkptr because we are in syncing context.
3744 */
3745 dr->dr_bp_copy = *db->db_blkptr;
3746
3747 if (db->db_level == 0 &&
3748 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3749 /*
3750 * The BP for this block has been provided by open context
3751 * (by dmu_sync() or dmu_buf_write_embedded()).
3752 */
3753 abd_t *contents = (data != NULL) ?
3754 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3755
3756 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3757 &dr->dr_bp_copy, contents, db->db.db_size, db->db.db_size,
3758 &zp, dbuf_write_override_ready, NULL, NULL,
3759 dbuf_write_override_done,
3760 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3761 mutex_enter(&db->db_mtx);
3762 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3763 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3764 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3765 mutex_exit(&db->db_mtx);
3766 } else if (db->db_state == DB_NOFILL) {
3767 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3768 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3769 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3770 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3771 dbuf_write_nofill_ready, NULL, NULL,
3772 dbuf_write_nofill_done, db,
3773 ZIO_PRIORITY_ASYNC_WRITE,
3774 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3775 } else {
3776 arc_done_func_t *children_ready_cb = NULL;
3777 ASSERT(arc_released(data));
3778
3779 /*
3780 * For indirect blocks, we want to setup the children
3781 * ready callback so that we can properly handle an indirect
3782 * block that only contains holes.
3783 */
3784 if (db->db_level != 0)
3785 children_ready_cb = dbuf_write_children_ready;
3786
3787 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3788 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3789 &zp, dbuf_write_ready,
3790 children_ready_cb, dbuf_write_physdone,
3791 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
3792 ZIO_FLAG_MUSTSUCCEED, &zb);
3793 }
3794 }
3795
3796 #if defined(_KERNEL) && defined(HAVE_SPL)
3797 EXPORT_SYMBOL(dbuf_find);
3798 EXPORT_SYMBOL(dbuf_is_metadata);
3799 EXPORT_SYMBOL(dbuf_destroy);
3800 EXPORT_SYMBOL(dbuf_loan_arcbuf);
3801 EXPORT_SYMBOL(dbuf_whichblock);
3802 EXPORT_SYMBOL(dbuf_read);
3803 EXPORT_SYMBOL(dbuf_unoverride);
3804 EXPORT_SYMBOL(dbuf_free_range);
3805 EXPORT_SYMBOL(dbuf_new_size);
3806 EXPORT_SYMBOL(dbuf_release_bp);
3807 EXPORT_SYMBOL(dbuf_dirty);
3808 EXPORT_SYMBOL(dmu_buf_will_dirty);
3809 EXPORT_SYMBOL(dmu_buf_will_not_fill);
3810 EXPORT_SYMBOL(dmu_buf_will_fill);
3811 EXPORT_SYMBOL(dmu_buf_fill_done);
3812 EXPORT_SYMBOL(dmu_buf_rele);
3813 EXPORT_SYMBOL(dbuf_assign_arcbuf);
3814 EXPORT_SYMBOL(dbuf_prefetch);
3815 EXPORT_SYMBOL(dbuf_hold_impl);
3816 EXPORT_SYMBOL(dbuf_hold);
3817 EXPORT_SYMBOL(dbuf_hold_level);
3818 EXPORT_SYMBOL(dbuf_create_bonus);
3819 EXPORT_SYMBOL(dbuf_spill_set_blksz);
3820 EXPORT_SYMBOL(dbuf_rm_spill);
3821 EXPORT_SYMBOL(dbuf_add_ref);
3822 EXPORT_SYMBOL(dbuf_rele);
3823 EXPORT_SYMBOL(dbuf_rele_and_unlock);
3824 EXPORT_SYMBOL(dbuf_refcount);
3825 EXPORT_SYMBOL(dbuf_sync_list);
3826 EXPORT_SYMBOL(dmu_buf_set_user);
3827 EXPORT_SYMBOL(dmu_buf_set_user_ie);
3828 EXPORT_SYMBOL(dmu_buf_get_user);
3829 EXPORT_SYMBOL(dmu_buf_get_blkptr);
3830
3831 /* BEGIN CSTYLED */
3832 module_param(dbuf_cache_max_bytes, ulong, 0644);
3833 MODULE_PARM_DESC(dbuf_cache_max_bytes,
3834 "Maximum size in bytes of the dbuf cache.");
3835
3836 module_param(dbuf_cache_hiwater_pct, uint, 0644);
3837 MODULE_PARM_DESC(dbuf_cache_hiwater_pct,
3838 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
3839 "directly.");
3840
3841 module_param(dbuf_cache_lowater_pct, uint, 0644);
3842 MODULE_PARM_DESC(dbuf_cache_lowater_pct,
3843 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
3844 "evicting dbufs.");
3845
3846 module_param(dbuf_cache_max_shift, int, 0644);
3847 MODULE_PARM_DESC(dbuf_cache_max_shift,
3848 "Cap the size of the dbuf cache to a log2 fraction of arc size.");
3849 /* END CSTYLED */
3850 #endif