]> granicus.if.org Git - zfs/blob - module/zfs/dbuf.c
Illumos #3464
[zfs] / 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 by Delphix. All rights reserved.
25  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26  */
27
28 #include <sys/zfs_context.h>
29 #include <sys/arc.h>
30 #include <sys/dmu.h>
31 #include <sys/dmu_impl.h>
32 #include <sys/dbuf.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/spa.h>
38 #include <sys/zio.h>
39 #include <sys/dmu_zfetch.h>
40 #include <sys/sa.h>
41 #include <sys/sa_impl.h>
42
43 struct dbuf_hold_impl_data {
44         /* Function arguments */
45         dnode_t *dh_dn;
46         uint8_t dh_level;
47         uint64_t dh_blkid;
48         int dh_fail_sparse;
49         void *dh_tag;
50         dmu_buf_impl_t **dh_dbp;
51         /* Local variables */
52         dmu_buf_impl_t *dh_db;
53         dmu_buf_impl_t *dh_parent;
54         blkptr_t *dh_bp;
55         int dh_err;
56         dbuf_dirty_record_t *dh_dr;
57         arc_buf_contents_t dh_type;
58         int dh_depth;
59 };
60
61 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
62     dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
63     void *tag, dmu_buf_impl_t **dbp, int depth);
64 static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
65
66 static void dbuf_destroy(dmu_buf_impl_t *db);
67 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
68 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
69
70 /*
71  * Global data structures and functions for the dbuf cache.
72  */
73 static kmem_cache_t *dbuf_cache;
74
75 /* ARGSUSED */
76 static int
77 dbuf_cons(void *vdb, void *unused, int kmflag)
78 {
79         dmu_buf_impl_t *db = vdb;
80         bzero(db, sizeof (dmu_buf_impl_t));
81
82         mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
83         cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
84         refcount_create(&db->db_holds);
85         list_link_init(&db->db_link);
86         return (0);
87 }
88
89 /* ARGSUSED */
90 static void
91 dbuf_dest(void *vdb, void *unused)
92 {
93         dmu_buf_impl_t *db = vdb;
94         mutex_destroy(&db->db_mtx);
95         cv_destroy(&db->db_changed);
96         refcount_destroy(&db->db_holds);
97 }
98
99 /*
100  * dbuf hash table routines
101  */
102 static dbuf_hash_table_t dbuf_hash_table;
103
104 static uint64_t dbuf_hash_count;
105
106 static uint64_t
107 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
108 {
109         uintptr_t osv = (uintptr_t)os;
110         uint64_t crc = -1ULL;
111
112         ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
113         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
114         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
115         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
116         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
117         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
118         crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
119
120         crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
121
122         return (crc);
123 }
124
125 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
126
127 #define DBUF_EQUAL(dbuf, os, obj, level, blkid)         \
128         ((dbuf)->db.db_object == (obj) &&               \
129         (dbuf)->db_objset == (os) &&                    \
130         (dbuf)->db_level == (level) &&                  \
131         (dbuf)->db_blkid == (blkid))
132
133 dmu_buf_impl_t *
134 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
135 {
136         dbuf_hash_table_t *h = &dbuf_hash_table;
137         objset_t *os = dn->dn_objset;
138         uint64_t obj;
139         uint64_t hv;
140         uint64_t idx;
141         dmu_buf_impl_t *db;
142
143         obj = dn->dn_object;
144         hv = DBUF_HASH(os, obj, level, blkid);
145         idx = hv & h->hash_table_mask;
146
147         mutex_enter(DBUF_HASH_MUTEX(h, idx));
148         for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
149                 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
150                         mutex_enter(&db->db_mtx);
151                         if (db->db_state != DB_EVICTING) {
152                                 mutex_exit(DBUF_HASH_MUTEX(h, idx));
153                                 return (db);
154                         }
155                         mutex_exit(&db->db_mtx);
156                 }
157         }
158         mutex_exit(DBUF_HASH_MUTEX(h, idx));
159         return (NULL);
160 }
161
162 /*
163  * Insert an entry into the hash table.  If there is already an element
164  * equal to elem in the hash table, then the already existing element
165  * will be returned and the new element will not be inserted.
166  * Otherwise returns NULL.
167  */
168 static dmu_buf_impl_t *
169 dbuf_hash_insert(dmu_buf_impl_t *db)
170 {
171         dbuf_hash_table_t *h = &dbuf_hash_table;
172         objset_t *os = db->db_objset;
173         uint64_t obj = db->db.db_object;
174         int level = db->db_level;
175         uint64_t blkid, hv, idx;
176         dmu_buf_impl_t *dbf;
177
178         blkid = db->db_blkid;
179         hv = DBUF_HASH(os, obj, level, blkid);
180         idx = hv & h->hash_table_mask;
181
182         mutex_enter(DBUF_HASH_MUTEX(h, idx));
183         for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
184                 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
185                         mutex_enter(&dbf->db_mtx);
186                         if (dbf->db_state != DB_EVICTING) {
187                                 mutex_exit(DBUF_HASH_MUTEX(h, idx));
188                                 return (dbf);
189                         }
190                         mutex_exit(&dbf->db_mtx);
191                 }
192         }
193
194         mutex_enter(&db->db_mtx);
195         db->db_hash_next = h->hash_table[idx];
196         h->hash_table[idx] = db;
197         mutex_exit(DBUF_HASH_MUTEX(h, idx));
198         atomic_add_64(&dbuf_hash_count, 1);
199
200         return (NULL);
201 }
202
203 /*
204  * Remove an entry from the hash table.  This operation will
205  * fail if there are any existing holds on the db.
206  */
207 static void
208 dbuf_hash_remove(dmu_buf_impl_t *db)
209 {
210         dbuf_hash_table_t *h = &dbuf_hash_table;
211         uint64_t hv, idx;
212         dmu_buf_impl_t *dbf, **dbp;
213
214         hv = DBUF_HASH(db->db_objset, db->db.db_object,
215             db->db_level, db->db_blkid);
216         idx = hv & h->hash_table_mask;
217
218         /*
219          * We musn't hold db_mtx to maintin lock ordering:
220          * DBUF_HASH_MUTEX > db_mtx.
221          */
222         ASSERT(refcount_is_zero(&db->db_holds));
223         ASSERT(db->db_state == DB_EVICTING);
224         ASSERT(!MUTEX_HELD(&db->db_mtx));
225
226         mutex_enter(DBUF_HASH_MUTEX(h, idx));
227         dbp = &h->hash_table[idx];
228         while ((dbf = *dbp) != db) {
229                 dbp = &dbf->db_hash_next;
230                 ASSERT(dbf != NULL);
231         }
232         *dbp = db->db_hash_next;
233         db->db_hash_next = NULL;
234         mutex_exit(DBUF_HASH_MUTEX(h, idx));
235         atomic_add_64(&dbuf_hash_count, -1);
236 }
237
238 static arc_evict_func_t dbuf_do_evict;
239
240 static void
241 dbuf_evict_user(dmu_buf_impl_t *db)
242 {
243         ASSERT(MUTEX_HELD(&db->db_mtx));
244
245         if (db->db_level != 0 || db->db_evict_func == NULL)
246                 return;
247
248         if (db->db_user_data_ptr_ptr)
249                 *db->db_user_data_ptr_ptr = db->db.db_data;
250         db->db_evict_func(&db->db, db->db_user_ptr);
251         db->db_user_ptr = NULL;
252         db->db_user_data_ptr_ptr = NULL;
253         db->db_evict_func = NULL;
254 }
255
256 boolean_t
257 dbuf_is_metadata(dmu_buf_impl_t *db)
258 {
259         if (db->db_level > 0) {
260                 return (B_TRUE);
261         } else {
262                 boolean_t is_metadata;
263
264                 DB_DNODE_ENTER(db);
265                 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
266                 DB_DNODE_EXIT(db);
267
268                 return (is_metadata);
269         }
270 }
271
272 void
273 dbuf_evict(dmu_buf_impl_t *db)
274 {
275         ASSERT(MUTEX_HELD(&db->db_mtx));
276         ASSERT(db->db_buf == NULL);
277         ASSERT(db->db_data_pending == NULL);
278
279         dbuf_clear(db);
280         dbuf_destroy(db);
281 }
282
283 void
284 dbuf_init(void)
285 {
286         uint64_t hsize = 1ULL << 16;
287         dbuf_hash_table_t *h = &dbuf_hash_table;
288         int i;
289
290         /*
291          * The hash table is big enough to fill all of physical memory
292          * with an average 4K block size.  The table will take up
293          * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
294          */
295         while (hsize * 4096 < physmem * PAGESIZE)
296                 hsize <<= 1;
297
298 retry:
299         h->hash_table_mask = hsize - 1;
300 #if defined(_KERNEL) && defined(HAVE_SPL)
301         /* Large allocations which do not require contiguous pages
302          * should be using vmem_alloc() in the linux kernel */
303         h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_PUSHPAGE);
304 #else
305         h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
306 #endif
307         if (h->hash_table == NULL) {
308                 /* XXX - we should really return an error instead of assert */
309                 ASSERT(hsize > (1ULL << 10));
310                 hsize >>= 1;
311                 goto retry;
312         }
313
314         dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
315             sizeof (dmu_buf_impl_t),
316             0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
317
318         for (i = 0; i < DBUF_MUTEXES; i++)
319                 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
320 }
321
322 void
323 dbuf_fini(void)
324 {
325         dbuf_hash_table_t *h = &dbuf_hash_table;
326         int i;
327
328         for (i = 0; i < DBUF_MUTEXES; i++)
329                 mutex_destroy(&h->hash_mutexes[i]);
330 #if defined(_KERNEL) && defined(HAVE_SPL)
331         /* Large allocations which do not require contiguous pages
332          * should be using vmem_free() in the linux kernel */
333         vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
334 #else
335         kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
336 #endif
337         kmem_cache_destroy(dbuf_cache);
338 }
339
340 /*
341  * Other stuff.
342  */
343
344 #ifdef ZFS_DEBUG
345 static void
346 dbuf_verify(dmu_buf_impl_t *db)
347 {
348         dnode_t *dn;
349         dbuf_dirty_record_t *dr;
350
351         ASSERT(MUTEX_HELD(&db->db_mtx));
352
353         if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
354                 return;
355
356         ASSERT(db->db_objset != NULL);
357         DB_DNODE_ENTER(db);
358         dn = DB_DNODE(db);
359         if (dn == NULL) {
360                 ASSERT(db->db_parent == NULL);
361                 ASSERT(db->db_blkptr == NULL);
362         } else {
363                 ASSERT3U(db->db.db_object, ==, dn->dn_object);
364                 ASSERT3P(db->db_objset, ==, dn->dn_objset);
365                 ASSERT3U(db->db_level, <, dn->dn_nlevels);
366                 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
367                     db->db_blkid == DMU_SPILL_BLKID ||
368                     !list_is_empty(&dn->dn_dbufs));
369         }
370         if (db->db_blkid == DMU_BONUS_BLKID) {
371                 ASSERT(dn != NULL);
372                 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
373                 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
374         } else if (db->db_blkid == DMU_SPILL_BLKID) {
375                 ASSERT(dn != NULL);
376                 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
377                 ASSERT0(db->db.db_offset);
378         } else {
379                 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
380         }
381
382         for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
383                 ASSERT(dr->dr_dbuf == db);
384
385         for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
386                 ASSERT(dr->dr_dbuf == db);
387
388         /*
389          * We can't assert that db_size matches dn_datablksz because it
390          * can be momentarily different when another thread is doing
391          * dnode_set_blksz().
392          */
393         if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
394                 dr = db->db_data_pending;
395                 /*
396                  * It should only be modified in syncing context, so
397                  * make sure we only have one copy of the data.
398                  */
399                 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
400         }
401
402         /* verify db->db_blkptr */
403         if (db->db_blkptr) {
404                 if (db->db_parent == dn->dn_dbuf) {
405                         /* db is pointed to by the dnode */
406                         /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
407                         if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
408                                 ASSERT(db->db_parent == NULL);
409                         else
410                                 ASSERT(db->db_parent != NULL);
411                         if (db->db_blkid != DMU_SPILL_BLKID)
412                                 ASSERT3P(db->db_blkptr, ==,
413                                     &dn->dn_phys->dn_blkptr[db->db_blkid]);
414                 } else {
415                         /* db is pointed to by an indirect block */
416                         ASSERTV(int epb = db->db_parent->db.db_size >>
417                                 SPA_BLKPTRSHIFT);
418                         ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
419                         ASSERT3U(db->db_parent->db.db_object, ==,
420                             db->db.db_object);
421                         /*
422                          * dnode_grow_indblksz() can make this fail if we don't
423                          * have the struct_rwlock.  XXX indblksz no longer
424                          * grows.  safe to do this now?
425                          */
426                         if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
427                                 ASSERT3P(db->db_blkptr, ==,
428                                     ((blkptr_t *)db->db_parent->db.db_data +
429                                     db->db_blkid % epb));
430                         }
431                 }
432         }
433         if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
434             (db->db_buf == NULL || db->db_buf->b_data) &&
435             db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
436             db->db_state != DB_FILL && !dn->dn_free_txg) {
437                 /*
438                  * If the blkptr isn't set but they have nonzero data,
439                  * it had better be dirty, otherwise we'll lose that
440                  * data when we evict this buffer.
441                  */
442                 if (db->db_dirtycnt == 0) {
443                         ASSERTV(uint64_t *buf = db->db.db_data);
444                         int i;
445
446                         for (i = 0; i < db->db.db_size >> 3; i++) {
447                                 ASSERT(buf[i] == 0);
448                         }
449                 }
450         }
451         DB_DNODE_EXIT(db);
452 }
453 #endif
454
455 static void
456 dbuf_update_data(dmu_buf_impl_t *db)
457 {
458         ASSERT(MUTEX_HELD(&db->db_mtx));
459         if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
460                 ASSERT(!refcount_is_zero(&db->db_holds));
461                 *db->db_user_data_ptr_ptr = db->db.db_data;
462         }
463 }
464
465 static void
466 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
467 {
468         ASSERT(MUTEX_HELD(&db->db_mtx));
469         ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
470         db->db_buf = buf;
471         if (buf != NULL) {
472                 ASSERT(buf->b_data != NULL);
473                 db->db.db_data = buf->b_data;
474                 if (!arc_released(buf))
475                         arc_set_callback(buf, dbuf_do_evict, db);
476                 dbuf_update_data(db);
477         } else {
478                 dbuf_evict_user(db);
479                 db->db.db_data = NULL;
480                 if (db->db_state != DB_NOFILL)
481                         db->db_state = DB_UNCACHED;
482         }
483 }
484
485 /*
486  * Loan out an arc_buf for read.  Return the loaned arc_buf.
487  */
488 arc_buf_t *
489 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
490 {
491         arc_buf_t *abuf;
492
493         mutex_enter(&db->db_mtx);
494         if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
495                 int blksz = db->db.db_size;
496                 spa_t *spa;
497
498                 mutex_exit(&db->db_mtx);
499                 DB_GET_SPA(&spa, db);
500                 abuf = arc_loan_buf(spa, blksz);
501                 bcopy(db->db.db_data, abuf->b_data, blksz);
502         } else {
503                 abuf = db->db_buf;
504                 arc_loan_inuse_buf(abuf, db);
505                 dbuf_set_data(db, NULL);
506                 mutex_exit(&db->db_mtx);
507         }
508         return (abuf);
509 }
510
511 uint64_t
512 dbuf_whichblock(dnode_t *dn, uint64_t offset)
513 {
514         if (dn->dn_datablkshift) {
515                 return (offset >> dn->dn_datablkshift);
516         } else {
517                 ASSERT3U(offset, <, dn->dn_datablksz);
518                 return (0);
519         }
520 }
521
522 static void
523 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
524 {
525         dmu_buf_impl_t *db = vdb;
526
527         mutex_enter(&db->db_mtx);
528         ASSERT3U(db->db_state, ==, DB_READ);
529         /*
530          * All reads are synchronous, so we must have a hold on the dbuf
531          */
532         ASSERT(refcount_count(&db->db_holds) > 0);
533         ASSERT(db->db_buf == NULL);
534         ASSERT(db->db.db_data == NULL);
535         if (db->db_level == 0 && db->db_freed_in_flight) {
536                 /* we were freed in flight; disregard any error */
537                 arc_release(buf, db);
538                 bzero(buf->b_data, db->db.db_size);
539                 arc_buf_freeze(buf);
540                 db->db_freed_in_flight = FALSE;
541                 dbuf_set_data(db, buf);
542                 db->db_state = DB_CACHED;
543         } else if (zio == NULL || zio->io_error == 0) {
544                 dbuf_set_data(db, buf);
545                 db->db_state = DB_CACHED;
546         } else {
547                 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
548                 ASSERT3P(db->db_buf, ==, NULL);
549                 VERIFY(arc_buf_remove_ref(buf, db));
550                 db->db_state = DB_UNCACHED;
551         }
552         cv_broadcast(&db->db_changed);
553         dbuf_rele_and_unlock(db, NULL);
554 }
555
556 static void
557 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
558 {
559         dnode_t *dn;
560         spa_t *spa;
561         zbookmark_t zb;
562         uint32_t aflags = ARC_NOWAIT;
563
564         DB_DNODE_ENTER(db);
565         dn = DB_DNODE(db);
566         ASSERT(!refcount_is_zero(&db->db_holds));
567         /* We need the struct_rwlock to prevent db_blkptr from changing. */
568         ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
569         ASSERT(MUTEX_HELD(&db->db_mtx));
570         ASSERT(db->db_state == DB_UNCACHED);
571         ASSERT(db->db_buf == NULL);
572
573         if (db->db_blkid == DMU_BONUS_BLKID) {
574                 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
575
576                 ASSERT3U(bonuslen, <=, db->db.db_size);
577                 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
578                 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
579                 if (bonuslen < DN_MAX_BONUSLEN)
580                         bzero(db->db.db_data, DN_MAX_BONUSLEN);
581                 if (bonuslen)
582                         bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
583                 DB_DNODE_EXIT(db);
584                 dbuf_update_data(db);
585                 db->db_state = DB_CACHED;
586                 mutex_exit(&db->db_mtx);
587                 return;
588         }
589
590         /*
591          * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
592          * processes the delete record and clears the bp while we are waiting
593          * for the dn_mtx (resulting in a "no" from block_freed).
594          */
595         if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
596             (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
597             BP_IS_HOLE(db->db_blkptr)))) {
598                 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
599
600                 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
601                     db->db.db_size, db, type));
602                 DB_DNODE_EXIT(db);
603                 bzero(db->db.db_data, db->db.db_size);
604                 db->db_state = DB_CACHED;
605                 *flags |= DB_RF_CACHED;
606                 mutex_exit(&db->db_mtx);
607                 return;
608         }
609
610         spa = dn->dn_objset->os_spa;
611         DB_DNODE_EXIT(db);
612
613         db->db_state = DB_READ;
614         mutex_exit(&db->db_mtx);
615
616         if (DBUF_IS_L2CACHEABLE(db))
617                 aflags |= ARC_L2CACHE;
618         if (DBUF_IS_L2COMPRESSIBLE(db))
619                 aflags |= ARC_L2COMPRESS;
620
621         SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
622             db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
623             db->db.db_object, db->db_level, db->db_blkid);
624
625         dbuf_add_ref(db, NULL);
626
627         (void) arc_read(zio, spa, db->db_blkptr,
628             dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
629             (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
630             &aflags, &zb);
631         if (aflags & ARC_CACHED)
632                 *flags |= DB_RF_CACHED;
633 }
634
635 int
636 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
637 {
638         int err = 0;
639         int havepzio = (zio != NULL);
640         int prefetch;
641         dnode_t *dn;
642
643         /*
644          * We don't have to hold the mutex to check db_state because it
645          * can't be freed while we have a hold on the buffer.
646          */
647         ASSERT(!refcount_is_zero(&db->db_holds));
648
649         if (db->db_state == DB_NOFILL)
650                 return (EIO);
651
652         DB_DNODE_ENTER(db);
653         dn = DB_DNODE(db);
654         if ((flags & DB_RF_HAVESTRUCT) == 0)
655                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
656
657         prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
658             (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
659             DBUF_IS_CACHEABLE(db);
660
661         mutex_enter(&db->db_mtx);
662         if (db->db_state == DB_CACHED) {
663                 mutex_exit(&db->db_mtx);
664                 if (prefetch)
665                         dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
666                             db->db.db_size, TRUE);
667                 if ((flags & DB_RF_HAVESTRUCT) == 0)
668                         rw_exit(&dn->dn_struct_rwlock);
669                 DB_DNODE_EXIT(db);
670         } else if (db->db_state == DB_UNCACHED) {
671                 spa_t *spa = dn->dn_objset->os_spa;
672
673                 if (zio == NULL)
674                         zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
675                 dbuf_read_impl(db, zio, &flags);
676
677                 /* dbuf_read_impl has dropped db_mtx for us */
678
679                 if (prefetch)
680                         dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
681                             db->db.db_size, flags & DB_RF_CACHED);
682
683                 if ((flags & DB_RF_HAVESTRUCT) == 0)
684                         rw_exit(&dn->dn_struct_rwlock);
685                 DB_DNODE_EXIT(db);
686
687                 if (!havepzio)
688                         err = zio_wait(zio);
689         } else {
690                 mutex_exit(&db->db_mtx);
691                 if (prefetch)
692                         dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
693                             db->db.db_size, TRUE);
694                 if ((flags & DB_RF_HAVESTRUCT) == 0)
695                         rw_exit(&dn->dn_struct_rwlock);
696                 DB_DNODE_EXIT(db);
697
698                 mutex_enter(&db->db_mtx);
699                 if ((flags & DB_RF_NEVERWAIT) == 0) {
700                         while (db->db_state == DB_READ ||
701                             db->db_state == DB_FILL) {
702                                 ASSERT(db->db_state == DB_READ ||
703                                     (flags & DB_RF_HAVESTRUCT) == 0);
704                                 cv_wait(&db->db_changed, &db->db_mtx);
705                         }
706                         if (db->db_state == DB_UNCACHED)
707                                 err = EIO;
708                 }
709                 mutex_exit(&db->db_mtx);
710         }
711
712         ASSERT(err || havepzio || db->db_state == DB_CACHED);
713         return (err);
714 }
715
716 static void
717 dbuf_noread(dmu_buf_impl_t *db)
718 {
719         ASSERT(!refcount_is_zero(&db->db_holds));
720         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
721         mutex_enter(&db->db_mtx);
722         while (db->db_state == DB_READ || db->db_state == DB_FILL)
723                 cv_wait(&db->db_changed, &db->db_mtx);
724         if (db->db_state == DB_UNCACHED) {
725                 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
726                 spa_t *spa;
727
728                 ASSERT(db->db_buf == NULL);
729                 ASSERT(db->db.db_data == NULL);
730                 DB_GET_SPA(&spa, db);
731                 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
732                 db->db_state = DB_FILL;
733         } else if (db->db_state == DB_NOFILL) {
734                 dbuf_set_data(db, NULL);
735         } else {
736                 ASSERT3U(db->db_state, ==, DB_CACHED);
737         }
738         mutex_exit(&db->db_mtx);
739 }
740
741 /*
742  * This is our just-in-time copy function.  It makes a copy of
743  * buffers, that have been modified in a previous transaction
744  * group, before we modify them in the current active group.
745  *
746  * This function is used in two places: when we are dirtying a
747  * buffer for the first time in a txg, and when we are freeing
748  * a range in a dnode that includes this buffer.
749  *
750  * Note that when we are called from dbuf_free_range() we do
751  * not put a hold on the buffer, we just traverse the active
752  * dbuf list for the dnode.
753  */
754 static void
755 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
756 {
757         dbuf_dirty_record_t *dr = db->db_last_dirty;
758
759         ASSERT(MUTEX_HELD(&db->db_mtx));
760         ASSERT(db->db.db_data != NULL);
761         ASSERT(db->db_level == 0);
762         ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
763
764         if (dr == NULL ||
765             (dr->dt.dl.dr_data !=
766             ((db->db_blkid  == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
767                 return;
768
769         /*
770          * If the last dirty record for this dbuf has not yet synced
771          * and its referencing the dbuf data, either:
772          *      reset the reference to point to a new copy,
773          * or (if there a no active holders)
774          *      just null out the current db_data pointer.
775          */
776         ASSERT(dr->dr_txg >= txg - 2);
777         if (db->db_blkid == DMU_BONUS_BLKID) {
778                 /* Note that the data bufs here are zio_bufs */
779                 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
780                 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
781                 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
782         } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
783                 int size = db->db.db_size;
784                 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
785                 spa_t *spa;
786
787                 DB_GET_SPA(&spa, db);
788                 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
789                 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
790         } else {
791                 dbuf_set_data(db, NULL);
792         }
793 }
794
795 void
796 dbuf_unoverride(dbuf_dirty_record_t *dr)
797 {
798         dmu_buf_impl_t *db = dr->dr_dbuf;
799         blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
800         uint64_t txg = dr->dr_txg;
801
802         ASSERT(MUTEX_HELD(&db->db_mtx));
803         ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
804         ASSERT(db->db_level == 0);
805
806         if (db->db_blkid == DMU_BONUS_BLKID ||
807             dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
808                 return;
809
810         ASSERT(db->db_data_pending != dr);
811
812         /* free this block */
813         if (!BP_IS_HOLE(bp)) {
814                 spa_t *spa;
815
816                 DB_GET_SPA(&spa, db);
817                 zio_free(spa, txg, bp);
818         }
819         dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
820         /*
821          * Release the already-written buffer, so we leave it in
822          * a consistent dirty state.  Note that all callers are
823          * modifying the buffer, so they will immediately do
824          * another (redundant) arc_release().  Therefore, leave
825          * the buf thawed to save the effort of freezing &
826          * immediately re-thawing it.
827          */
828         arc_release(dr->dt.dl.dr_data, db);
829 }
830
831 /*
832  * Evict (if its unreferenced) or clear (if its referenced) any level-0
833  * data blocks in the free range, so that any future readers will find
834  * empty blocks.  Also, if we happen accross any level-1 dbufs in the
835  * range that have not already been marked dirty, mark them dirty so
836  * they stay in memory.
837  */
838 void
839 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
840 {
841         dmu_buf_impl_t *db, *db_next;
842         uint64_t txg = tx->tx_txg;
843         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
844         uint64_t first_l1 = start >> epbs;
845         uint64_t last_l1 = end >> epbs;
846
847         if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
848                 end = dn->dn_maxblkid;
849                 last_l1 = end >> epbs;
850         }
851         dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
852         mutex_enter(&dn->dn_dbufs_mtx);
853         for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
854                 db_next = list_next(&dn->dn_dbufs, db);
855                 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
856
857                 if (db->db_level == 1 &&
858                     db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
859                         mutex_enter(&db->db_mtx);
860                         if (db->db_last_dirty &&
861                             db->db_last_dirty->dr_txg < txg) {
862                                 dbuf_add_ref(db, FTAG);
863                                 mutex_exit(&db->db_mtx);
864                                 dbuf_will_dirty(db, tx);
865                                 dbuf_rele(db, FTAG);
866                         } else {
867                                 mutex_exit(&db->db_mtx);
868                         }
869                 }
870
871                 if (db->db_level != 0)
872                         continue;
873                 dprintf_dbuf(db, "found buf %s\n", "");
874                 if (db->db_blkid < start || db->db_blkid > end)
875                         continue;
876
877                 /* found a level 0 buffer in the range */
878                 mutex_enter(&db->db_mtx);
879                 if (dbuf_undirty(db, tx)) {
880                         /* mutex has been dropped and dbuf destroyed */
881                         continue;
882                 }
883
884                 if (db->db_state == DB_UNCACHED ||
885                     db->db_state == DB_NOFILL ||
886                     db->db_state == DB_EVICTING) {
887                         ASSERT(db->db.db_data == NULL);
888                         mutex_exit(&db->db_mtx);
889                         continue;
890                 }
891                 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
892                         /* will be handled in dbuf_read_done or dbuf_rele */
893                         db->db_freed_in_flight = TRUE;
894                         mutex_exit(&db->db_mtx);
895                         continue;
896                 }
897                 if (refcount_count(&db->db_holds) == 0) {
898                         ASSERT(db->db_buf);
899                         dbuf_clear(db);
900                         continue;
901                 }
902                 /* The dbuf is referenced */
903
904                 if (db->db_last_dirty != NULL) {
905                         dbuf_dirty_record_t *dr = db->db_last_dirty;
906
907                         if (dr->dr_txg == txg) {
908                                 /*
909                                  * This buffer is "in-use", re-adjust the file
910                                  * size to reflect that this buffer may
911                                  * contain new data when we sync.
912                                  */
913                                 if (db->db_blkid != DMU_SPILL_BLKID &&
914                                     db->db_blkid > dn->dn_maxblkid)
915                                         dn->dn_maxblkid = db->db_blkid;
916                                 dbuf_unoverride(dr);
917                         } else {
918                                 /*
919                                  * This dbuf is not dirty in the open context.
920                                  * Either uncache it (if its not referenced in
921                                  * the open context) or reset its contents to
922                                  * empty.
923                                  */
924                                 dbuf_fix_old_data(db, txg);
925                         }
926                 }
927                 /* clear the contents if its cached */
928                 if (db->db_state == DB_CACHED) {
929                         ASSERT(db->db.db_data != NULL);
930                         arc_release(db->db_buf, db);
931                         bzero(db->db.db_data, db->db.db_size);
932                         arc_buf_freeze(db->db_buf);
933                 }
934
935                 mutex_exit(&db->db_mtx);
936         }
937         mutex_exit(&dn->dn_dbufs_mtx);
938 }
939
940 static int
941 dbuf_block_freeable(dmu_buf_impl_t *db)
942 {
943         dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
944         uint64_t birth_txg = 0;
945
946         /*
947          * We don't need any locking to protect db_blkptr:
948          * If it's syncing, then db_last_dirty will be set
949          * so we'll ignore db_blkptr.
950          */
951         ASSERT(MUTEX_HELD(&db->db_mtx));
952         if (db->db_last_dirty)
953                 birth_txg = db->db_last_dirty->dr_txg;
954         else if (db->db_blkptr)
955                 birth_txg = db->db_blkptr->blk_birth;
956
957         /*
958          * If we don't exist or are in a snapshot, we can't be freed.
959          * Don't pass the bp to dsl_dataset_block_freeable() since we
960          * are holding the db_mtx lock and might deadlock if we are
961          * prefetching a dedup-ed block.
962          */
963         if (birth_txg)
964                 return (ds == NULL ||
965                     dsl_dataset_block_freeable(ds, NULL, birth_txg));
966         else
967                 return (FALSE);
968 }
969
970 void
971 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
972 {
973         arc_buf_t *buf, *obuf;
974         int osize = db->db.db_size;
975         arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
976         dnode_t *dn;
977
978         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
979
980         DB_DNODE_ENTER(db);
981         dn = DB_DNODE(db);
982
983         /* XXX does *this* func really need the lock? */
984         ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
985
986         /*
987          * This call to dbuf_will_dirty() with the dn_struct_rwlock held
988          * is OK, because there can be no other references to the db
989          * when we are changing its size, so no concurrent DB_FILL can
990          * be happening.
991          */
992         /*
993          * XXX we should be doing a dbuf_read, checking the return
994          * value and returning that up to our callers
995          */
996         dbuf_will_dirty(db, tx);
997
998         /* create the data buffer for the new block */
999         buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
1000
1001         /* copy old block data to the new block */
1002         obuf = db->db_buf;
1003         bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1004         /* zero the remainder */
1005         if (size > osize)
1006                 bzero((uint8_t *)buf->b_data + osize, size - osize);
1007
1008         mutex_enter(&db->db_mtx);
1009         dbuf_set_data(db, buf);
1010         VERIFY(arc_buf_remove_ref(obuf, db));
1011         db->db.db_size = size;
1012
1013         if (db->db_level == 0) {
1014                 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1015                 db->db_last_dirty->dt.dl.dr_data = buf;
1016         }
1017         mutex_exit(&db->db_mtx);
1018
1019         dnode_willuse_space(dn, size-osize, tx);
1020         DB_DNODE_EXIT(db);
1021 }
1022
1023 void
1024 dbuf_release_bp(dmu_buf_impl_t *db)
1025 {
1026         objset_t *os;
1027
1028         DB_GET_OBJSET(&os, db);
1029         ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1030         ASSERT(arc_released(os->os_phys_buf) ||
1031             list_link_active(&os->os_dsl_dataset->ds_synced_link));
1032         ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1033
1034         (void) arc_release(db->db_buf, db);
1035 }
1036
1037 dbuf_dirty_record_t *
1038 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1039 {
1040         dnode_t *dn;
1041         objset_t *os;
1042         dbuf_dirty_record_t **drp, *dr;
1043         int drop_struct_lock = FALSE;
1044         boolean_t do_free_accounting = B_FALSE;
1045         int txgoff = tx->tx_txg & TXG_MASK;
1046
1047         ASSERT(tx->tx_txg != 0);
1048         ASSERT(!refcount_is_zero(&db->db_holds));
1049         DMU_TX_DIRTY_BUF(tx, db);
1050
1051         DB_DNODE_ENTER(db);
1052         dn = DB_DNODE(db);
1053         /*
1054          * Shouldn't dirty a regular buffer in syncing context.  Private
1055          * objects may be dirtied in syncing context, but only if they
1056          * were already pre-dirtied in open context.
1057          */
1058         ASSERT(!dmu_tx_is_syncing(tx) ||
1059             BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1060             DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1061             dn->dn_objset->os_dsl_dataset == NULL);
1062         /*
1063          * We make this assert for private objects as well, but after we
1064          * check if we're already dirty.  They are allowed to re-dirty
1065          * in syncing context.
1066          */
1067         ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1068             dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1069             (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1070
1071         mutex_enter(&db->db_mtx);
1072         /*
1073          * XXX make this true for indirects too?  The problem is that
1074          * transactions created with dmu_tx_create_assigned() from
1075          * syncing context don't bother holding ahead.
1076          */
1077         ASSERT(db->db_level != 0 ||
1078             db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1079             db->db_state == DB_NOFILL);
1080
1081         mutex_enter(&dn->dn_mtx);
1082         /*
1083          * Don't set dirtyctx to SYNC if we're just modifying this as we
1084          * initialize the objset.
1085          */
1086         if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1087             !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1088                 dn->dn_dirtyctx =
1089                     (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1090                 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1091                 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_PUSHPAGE);
1092         }
1093         mutex_exit(&dn->dn_mtx);
1094
1095         if (db->db_blkid == DMU_SPILL_BLKID)
1096                 dn->dn_have_spill = B_TRUE;
1097
1098         /*
1099          * If this buffer is already dirty, we're done.
1100          */
1101         drp = &db->db_last_dirty;
1102         ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1103             db->db.db_object == DMU_META_DNODE_OBJECT);
1104         while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1105                 drp = &dr->dr_next;
1106         if (dr && dr->dr_txg == tx->tx_txg) {
1107                 DB_DNODE_EXIT(db);
1108
1109                 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1110                         /*
1111                          * If this buffer has already been written out,
1112                          * we now need to reset its state.
1113                          */
1114                         dbuf_unoverride(dr);
1115                         if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1116                             db->db_state != DB_NOFILL)
1117                                 arc_buf_thaw(db->db_buf);
1118                 }
1119                 mutex_exit(&db->db_mtx);
1120                 return (dr);
1121         }
1122
1123         /*
1124          * Only valid if not already dirty.
1125          */
1126         ASSERT(dn->dn_object == 0 ||
1127             dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1128             (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1129
1130         ASSERT3U(dn->dn_nlevels, >, db->db_level);
1131         ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1132             dn->dn_phys->dn_nlevels > db->db_level ||
1133             dn->dn_next_nlevels[txgoff] > db->db_level ||
1134             dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1135             dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1136
1137         /*
1138          * We should only be dirtying in syncing context if it's the
1139          * mos or we're initializing the os or it's a special object.
1140          * However, we are allowed to dirty in syncing context provided
1141          * we already dirtied it in open context.  Hence we must make
1142          * this assertion only if we're not already dirty.
1143          */
1144         os = dn->dn_objset;
1145         ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1146             os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1147         ASSERT(db->db.db_size != 0);
1148
1149         dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1150
1151         if (db->db_blkid != DMU_BONUS_BLKID) {
1152                 /*
1153                  * Update the accounting.
1154                  * Note: we delay "free accounting" until after we drop
1155                  * the db_mtx.  This keeps us from grabbing other locks
1156                  * (and possibly deadlocking) in bp_get_dsize() while
1157                  * also holding the db_mtx.
1158                  */
1159                 dnode_willuse_space(dn, db->db.db_size, tx);
1160                 do_free_accounting = dbuf_block_freeable(db);
1161         }
1162
1163         /*
1164          * If this buffer is dirty in an old transaction group we need
1165          * to make a copy of it so that the changes we make in this
1166          * transaction group won't leak out when we sync the older txg.
1167          */
1168         dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_PUSHPAGE);
1169         list_link_init(&dr->dr_dirty_node);
1170         if (db->db_level == 0) {
1171                 void *data_old = db->db_buf;
1172
1173                 if (db->db_state != DB_NOFILL) {
1174                         if (db->db_blkid == DMU_BONUS_BLKID) {
1175                                 dbuf_fix_old_data(db, tx->tx_txg);
1176                                 data_old = db->db.db_data;
1177                         } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1178                                 /*
1179                                  * Release the data buffer from the cache so
1180                                  * that we can modify it without impacting
1181                                  * possible other users of this cached data
1182                                  * block.  Note that indirect blocks and
1183                                  * private objects are not released until the
1184                                  * syncing state (since they are only modified
1185                                  * then).
1186                                  */
1187                                 arc_release(db->db_buf, db);
1188                                 dbuf_fix_old_data(db, tx->tx_txg);
1189                                 data_old = db->db_buf;
1190                         }
1191                         ASSERT(data_old != NULL);
1192                 }
1193                 dr->dt.dl.dr_data = data_old;
1194         } else {
1195                 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1196                 list_create(&dr->dt.di.dr_children,
1197                     sizeof (dbuf_dirty_record_t),
1198                     offsetof(dbuf_dirty_record_t, dr_dirty_node));
1199         }
1200         dr->dr_dbuf = db;
1201         dr->dr_txg = tx->tx_txg;
1202         dr->dr_next = *drp;
1203         *drp = dr;
1204
1205         /*
1206          * We could have been freed_in_flight between the dbuf_noread
1207          * and dbuf_dirty.  We win, as though the dbuf_noread() had
1208          * happened after the free.
1209          */
1210         if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1211             db->db_blkid != DMU_SPILL_BLKID) {
1212                 mutex_enter(&dn->dn_mtx);
1213                 dnode_clear_range(dn, db->db_blkid, 1, tx);
1214                 mutex_exit(&dn->dn_mtx);
1215                 db->db_freed_in_flight = FALSE;
1216         }
1217
1218         /*
1219          * This buffer is now part of this txg
1220          */
1221         dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1222         db->db_dirtycnt += 1;
1223         ASSERT3U(db->db_dirtycnt, <=, 3);
1224
1225         mutex_exit(&db->db_mtx);
1226
1227         if (db->db_blkid == DMU_BONUS_BLKID ||
1228             db->db_blkid == DMU_SPILL_BLKID) {
1229                 mutex_enter(&dn->dn_mtx);
1230                 ASSERT(!list_link_active(&dr->dr_dirty_node));
1231                 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1232                 mutex_exit(&dn->dn_mtx);
1233                 dnode_setdirty(dn, tx);
1234                 DB_DNODE_EXIT(db);
1235                 return (dr);
1236         } else if (do_free_accounting) {
1237                 blkptr_t *bp = db->db_blkptr;
1238                 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1239                     bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1240                 /*
1241                  * This is only a guess -- if the dbuf is dirty
1242                  * in a previous txg, we don't know how much
1243                  * space it will use on disk yet.  We should
1244                  * really have the struct_rwlock to access
1245                  * db_blkptr, but since this is just a guess,
1246                  * it's OK if we get an odd answer.
1247                  */
1248                 ddt_prefetch(os->os_spa, bp);
1249                 dnode_willuse_space(dn, -willfree, tx);
1250         }
1251
1252         if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1253                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1254                 drop_struct_lock = TRUE;
1255         }
1256
1257         if (db->db_level == 0) {
1258                 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1259                 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1260         }
1261
1262         if (db->db_level+1 < dn->dn_nlevels) {
1263                 dmu_buf_impl_t *parent = db->db_parent;
1264                 dbuf_dirty_record_t *di;
1265                 int parent_held = FALSE;
1266
1267                 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1268                         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1269
1270                         parent = dbuf_hold_level(dn, db->db_level+1,
1271                             db->db_blkid >> epbs, FTAG);
1272                         ASSERT(parent != NULL);
1273                         parent_held = TRUE;
1274                 }
1275                 if (drop_struct_lock)
1276                         rw_exit(&dn->dn_struct_rwlock);
1277                 ASSERT3U(db->db_level+1, ==, parent->db_level);
1278                 di = dbuf_dirty(parent, tx);
1279                 if (parent_held)
1280                         dbuf_rele(parent, FTAG);
1281
1282                 mutex_enter(&db->db_mtx);
1283                 /*  possible race with dbuf_undirty() */
1284                 if (db->db_last_dirty == dr ||
1285                     dn->dn_object == DMU_META_DNODE_OBJECT) {
1286                         mutex_enter(&di->dt.di.dr_mtx);
1287                         ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1288                         ASSERT(!list_link_active(&dr->dr_dirty_node));
1289                         list_insert_tail(&di->dt.di.dr_children, dr);
1290                         mutex_exit(&di->dt.di.dr_mtx);
1291                         dr->dr_parent = di;
1292                 }
1293                 mutex_exit(&db->db_mtx);
1294         } else {
1295                 ASSERT(db->db_level+1 == dn->dn_nlevels);
1296                 ASSERT(db->db_blkid < dn->dn_nblkptr);
1297                 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1298                 mutex_enter(&dn->dn_mtx);
1299                 ASSERT(!list_link_active(&dr->dr_dirty_node));
1300                 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1301                 mutex_exit(&dn->dn_mtx);
1302                 if (drop_struct_lock)
1303                         rw_exit(&dn->dn_struct_rwlock);
1304         }
1305
1306         dnode_setdirty(dn, tx);
1307         DB_DNODE_EXIT(db);
1308         return (dr);
1309 }
1310
1311 /*
1312  * Return TRUE if this evicted the dbuf.
1313  */
1314 static boolean_t
1315 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1316 {
1317         dnode_t *dn;
1318         uint64_t txg = tx->tx_txg;
1319         dbuf_dirty_record_t *dr, **drp;
1320
1321         ASSERT(txg != 0);
1322         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1323         ASSERT0(db->db_level);
1324         ASSERT(MUTEX_HELD(&db->db_mtx));
1325
1326         /*
1327          * If this buffer is not dirty, we're done.
1328          */
1329         for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1330                 if (dr->dr_txg <= txg)
1331                         break;
1332         if (dr == NULL || dr->dr_txg < txg)
1333                 return (B_FALSE);
1334         ASSERT(dr->dr_txg == txg);
1335         ASSERT(dr->dr_dbuf == db);
1336
1337         DB_DNODE_ENTER(db);
1338         dn = DB_DNODE(db);
1339
1340         /*
1341          * Note:  This code will probably work even if there are concurrent
1342          * holders, but it is untested in that scenerio, as the ZPL and
1343          * ztest have additional locking (the range locks) that prevents
1344          * that type of concurrent access.
1345          */
1346         ASSERT3U(refcount_count(&db->db_holds), ==, db->db_dirtycnt);
1347
1348         dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1349
1350         ASSERT(db->db.db_size != 0);
1351
1352         /* XXX would be nice to fix up dn_towrite_space[] */
1353
1354         *drp = dr->dr_next;
1355
1356         /*
1357          * Note that there are three places in dbuf_dirty()
1358          * where this dirty record may be put on a list.
1359          * Make sure to do a list_remove corresponding to
1360          * every one of those list_insert calls.
1361          */
1362         if (dr->dr_parent) {
1363                 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1364                 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1365                 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1366         } else if (db->db_blkid == DMU_SPILL_BLKID ||
1367             db->db_level+1 == dn->dn_nlevels) {
1368                 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1369                 mutex_enter(&dn->dn_mtx);
1370                 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1371                 mutex_exit(&dn->dn_mtx);
1372         }
1373         DB_DNODE_EXIT(db);
1374
1375         if (db->db_state != DB_NOFILL) {
1376                 dbuf_unoverride(dr);
1377
1378                 ASSERT(db->db_buf != NULL);
1379                 ASSERT(dr->dt.dl.dr_data != NULL);
1380                 if (dr->dt.dl.dr_data != db->db_buf)
1381                         VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1382         }
1383         kmem_free(dr, sizeof (dbuf_dirty_record_t));
1384
1385         ASSERT(db->db_dirtycnt > 0);
1386         db->db_dirtycnt -= 1;
1387
1388         if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1389                 arc_buf_t *buf = db->db_buf;
1390
1391                 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1392                 dbuf_set_data(db, NULL);
1393                 VERIFY(arc_buf_remove_ref(buf, db));
1394                 dbuf_evict(db);
1395                 return (B_TRUE);
1396         }
1397
1398         return (B_FALSE);
1399 }
1400
1401 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1402 void
1403 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1404 {
1405         int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1406
1407         ASSERT(tx->tx_txg != 0);
1408         ASSERT(!refcount_is_zero(&db->db_holds));
1409
1410         DB_DNODE_ENTER(db);
1411         if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1412                 rf |= DB_RF_HAVESTRUCT;
1413         DB_DNODE_EXIT(db);
1414         (void) dbuf_read(db, NULL, rf);
1415         (void) dbuf_dirty(db, tx);
1416 }
1417
1418 void
1419 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1420 {
1421         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1422
1423         db->db_state = DB_NOFILL;
1424
1425         dmu_buf_will_fill(db_fake, tx);
1426 }
1427
1428 void
1429 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1430 {
1431         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1432
1433         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1434         ASSERT(tx->tx_txg != 0);
1435         ASSERT(db->db_level == 0);
1436         ASSERT(!refcount_is_zero(&db->db_holds));
1437
1438         ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1439             dmu_tx_private_ok(tx));
1440
1441         dbuf_noread(db);
1442         (void) dbuf_dirty(db, tx);
1443 }
1444
1445 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1446 /* ARGSUSED */
1447 void
1448 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1449 {
1450         mutex_enter(&db->db_mtx);
1451         DBUF_VERIFY(db);
1452
1453         if (db->db_state == DB_FILL) {
1454                 if (db->db_level == 0 && db->db_freed_in_flight) {
1455                         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1456                         /* we were freed while filling */
1457                         /* XXX dbuf_undirty? */
1458                         bzero(db->db.db_data, db->db.db_size);
1459                         db->db_freed_in_flight = FALSE;
1460                 }
1461                 db->db_state = DB_CACHED;
1462                 cv_broadcast(&db->db_changed);
1463         }
1464         mutex_exit(&db->db_mtx);
1465 }
1466
1467 /*
1468  * Directly assign a provided arc buf to a given dbuf if it's not referenced
1469  * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1470  */
1471 void
1472 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1473 {
1474         ASSERT(!refcount_is_zero(&db->db_holds));
1475         ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1476         ASSERT(db->db_level == 0);
1477         ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1478         ASSERT(buf != NULL);
1479         ASSERT(arc_buf_size(buf) == db->db.db_size);
1480         ASSERT(tx->tx_txg != 0);
1481
1482         arc_return_buf(buf, db);
1483         ASSERT(arc_released(buf));
1484
1485         mutex_enter(&db->db_mtx);
1486
1487         while (db->db_state == DB_READ || db->db_state == DB_FILL)
1488                 cv_wait(&db->db_changed, &db->db_mtx);
1489
1490         ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1491
1492         if (db->db_state == DB_CACHED &&
1493             refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1494                 mutex_exit(&db->db_mtx);
1495                 (void) dbuf_dirty(db, tx);
1496                 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1497                 VERIFY(arc_buf_remove_ref(buf, db));
1498                 xuio_stat_wbuf_copied();
1499                 return;
1500         }
1501
1502         xuio_stat_wbuf_nocopy();
1503         if (db->db_state == DB_CACHED) {
1504                 dbuf_dirty_record_t *dr = db->db_last_dirty;
1505
1506                 ASSERT(db->db_buf != NULL);
1507                 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1508                         ASSERT(dr->dt.dl.dr_data == db->db_buf);
1509                         if (!arc_released(db->db_buf)) {
1510                                 ASSERT(dr->dt.dl.dr_override_state ==
1511                                     DR_OVERRIDDEN);
1512                                 arc_release(db->db_buf, db);
1513                         }
1514                         dr->dt.dl.dr_data = buf;
1515                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1516                 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1517                         arc_release(db->db_buf, db);
1518                         VERIFY(arc_buf_remove_ref(db->db_buf, db));
1519                 }
1520                 db->db_buf = NULL;
1521         }
1522         ASSERT(db->db_buf == NULL);
1523         dbuf_set_data(db, buf);
1524         db->db_state = DB_FILL;
1525         mutex_exit(&db->db_mtx);
1526         (void) dbuf_dirty(db, tx);
1527         dbuf_fill_done(db, tx);
1528 }
1529
1530 /*
1531  * "Clear" the contents of this dbuf.  This will mark the dbuf
1532  * EVICTING and clear *most* of its references.  Unfortunetely,
1533  * when we are not holding the dn_dbufs_mtx, we can't clear the
1534  * entry in the dn_dbufs list.  We have to wait until dbuf_destroy()
1535  * in this case.  For callers from the DMU we will usually see:
1536  *      dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1537  * For the arc callback, we will usually see:
1538  *      dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1539  * Sometimes, though, we will get a mix of these two:
1540  *      DMU: dbuf_clear()->arc_buf_evict()
1541  *      ARC: dbuf_do_evict()->dbuf_destroy()
1542  */
1543 void
1544 dbuf_clear(dmu_buf_impl_t *db)
1545 {
1546         dnode_t *dn;
1547         dmu_buf_impl_t *parent = db->db_parent;
1548         dmu_buf_impl_t *dndb;
1549         int dbuf_gone = FALSE;
1550
1551         ASSERT(MUTEX_HELD(&db->db_mtx));
1552         ASSERT(refcount_is_zero(&db->db_holds));
1553
1554         dbuf_evict_user(db);
1555
1556         if (db->db_state == DB_CACHED) {
1557                 ASSERT(db->db.db_data != NULL);
1558                 if (db->db_blkid == DMU_BONUS_BLKID) {
1559                         zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1560                         arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1561                 }
1562                 db->db.db_data = NULL;
1563                 db->db_state = DB_UNCACHED;
1564         }
1565
1566         ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1567         ASSERT(db->db_data_pending == NULL);
1568
1569         db->db_state = DB_EVICTING;
1570         db->db_blkptr = NULL;
1571
1572         DB_DNODE_ENTER(db);
1573         dn = DB_DNODE(db);
1574         dndb = dn->dn_dbuf;
1575         if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1576                 list_remove(&dn->dn_dbufs, db);
1577                 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1578                 membar_producer();
1579                 DB_DNODE_EXIT(db);
1580                 /*
1581                  * Decrementing the dbuf count means that the hold corresponding
1582                  * to the removed dbuf is no longer discounted in dnode_move(),
1583                  * so the dnode cannot be moved until after we release the hold.
1584                  * The membar_producer() ensures visibility of the decremented
1585                  * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1586                  * release any lock.
1587                  */
1588                 dnode_rele(dn, db);
1589                 db->db_dnode_handle = NULL;
1590         } else {
1591                 DB_DNODE_EXIT(db);
1592         }
1593
1594         if (db->db_buf)
1595                 dbuf_gone = arc_buf_evict(db->db_buf);
1596
1597         if (!dbuf_gone)
1598                 mutex_exit(&db->db_mtx);
1599
1600         /*
1601          * If this dbuf is referenced from an indirect dbuf,
1602          * decrement the ref count on the indirect dbuf.
1603          */
1604         if (parent && parent != dndb)
1605                 dbuf_rele(parent, db);
1606 }
1607
1608 __attribute__((always_inline))
1609 static inline int
1610 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1611     dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
1612 {
1613         int nlevels, epbs;
1614
1615         *parentp = NULL;
1616         *bpp = NULL;
1617
1618         ASSERT(blkid != DMU_BONUS_BLKID);
1619
1620         if (blkid == DMU_SPILL_BLKID) {
1621                 mutex_enter(&dn->dn_mtx);
1622                 if (dn->dn_have_spill &&
1623                     (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1624                         *bpp = &dn->dn_phys->dn_spill;
1625                 else
1626                         *bpp = NULL;
1627                 dbuf_add_ref(dn->dn_dbuf, NULL);
1628                 *parentp = dn->dn_dbuf;
1629                 mutex_exit(&dn->dn_mtx);
1630                 return (0);
1631         }
1632
1633         if (dn->dn_phys->dn_nlevels == 0)
1634                 nlevels = 1;
1635         else
1636                 nlevels = dn->dn_phys->dn_nlevels;
1637
1638         epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1639
1640         ASSERT3U(level * epbs, <, 64);
1641         ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1642         if (level >= nlevels ||
1643             (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1644                 /* the buffer has no parent yet */
1645                 return (ENOENT);
1646         } else if (level < nlevels-1) {
1647                 /* this block is referenced from an indirect block */
1648                 int err;
1649                 if (dh == NULL) {
1650                         err = dbuf_hold_impl(dn, level+1, blkid >> epbs,
1651                                         fail_sparse, NULL, parentp);
1652                 }
1653                 else {
1654                         __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
1655                                         blkid >> epbs, fail_sparse, NULL,
1656                                         parentp, dh->dh_depth + 1);
1657                         err = __dbuf_hold_impl(dh + 1);
1658                 }
1659                 if (err)
1660                         return (err);
1661                 err = dbuf_read(*parentp, NULL,
1662                     (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1663                 if (err) {
1664                         dbuf_rele(*parentp, NULL);
1665                         *parentp = NULL;
1666                         return (err);
1667                 }
1668                 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1669                     (blkid & ((1ULL << epbs) - 1));
1670                 return (0);
1671         } else {
1672                 /* the block is referenced from the dnode */
1673                 ASSERT3U(level, ==, nlevels-1);
1674                 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1675                     blkid < dn->dn_phys->dn_nblkptr);
1676                 if (dn->dn_dbuf) {
1677                         dbuf_add_ref(dn->dn_dbuf, NULL);
1678                         *parentp = dn->dn_dbuf;
1679                 }
1680                 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1681                 return (0);
1682         }
1683 }
1684
1685 static dmu_buf_impl_t *
1686 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1687     dmu_buf_impl_t *parent, blkptr_t *blkptr)
1688 {
1689         objset_t *os = dn->dn_objset;
1690         dmu_buf_impl_t *db, *odb;
1691
1692         ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1693         ASSERT(dn->dn_type != DMU_OT_NONE);
1694
1695         db = kmem_cache_alloc(dbuf_cache, KM_PUSHPAGE);
1696
1697         db->db_objset = os;
1698         db->db.db_object = dn->dn_object;
1699         db->db_level = level;
1700         db->db_blkid = blkid;
1701         db->db_last_dirty = NULL;
1702         db->db_dirtycnt = 0;
1703         db->db_dnode_handle = dn->dn_handle;
1704         db->db_parent = parent;
1705         db->db_blkptr = blkptr;
1706
1707         db->db_user_ptr = NULL;
1708         db->db_user_data_ptr_ptr = NULL;
1709         db->db_evict_func = NULL;
1710         db->db_immediate_evict = 0;
1711         db->db_freed_in_flight = 0;
1712
1713         if (blkid == DMU_BONUS_BLKID) {
1714                 ASSERT3P(parent, ==, dn->dn_dbuf);
1715                 db->db.db_size = DN_MAX_BONUSLEN -
1716                     (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1717                 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1718                 db->db.db_offset = DMU_BONUS_BLKID;
1719                 db->db_state = DB_UNCACHED;
1720                 /* the bonus dbuf is not placed in the hash table */
1721                 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1722                 return (db);
1723         } else if (blkid == DMU_SPILL_BLKID) {
1724                 db->db.db_size = (blkptr != NULL) ?
1725                     BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1726                 db->db.db_offset = 0;
1727         } else {
1728                 int blocksize =
1729                     db->db_level ? 1<<dn->dn_indblkshift :  dn->dn_datablksz;
1730                 db->db.db_size = blocksize;
1731                 db->db.db_offset = db->db_blkid * blocksize;
1732         }
1733
1734         /*
1735          * Hold the dn_dbufs_mtx while we get the new dbuf
1736          * in the hash table *and* added to the dbufs list.
1737          * This prevents a possible deadlock with someone
1738          * trying to look up this dbuf before its added to the
1739          * dn_dbufs list.
1740          */
1741         mutex_enter(&dn->dn_dbufs_mtx);
1742         db->db_state = DB_EVICTING;
1743         if ((odb = dbuf_hash_insert(db)) != NULL) {
1744                 /* someone else inserted it first */
1745                 kmem_cache_free(dbuf_cache, db);
1746                 mutex_exit(&dn->dn_dbufs_mtx);
1747                 return (odb);
1748         }
1749         list_insert_head(&dn->dn_dbufs, db);
1750         db->db_state = DB_UNCACHED;
1751         mutex_exit(&dn->dn_dbufs_mtx);
1752         arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1753
1754         if (parent && parent != dn->dn_dbuf)
1755                 dbuf_add_ref(parent, db);
1756
1757         ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1758             refcount_count(&dn->dn_holds) > 0);
1759         (void) refcount_add(&dn->dn_holds, db);
1760         (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1761
1762         dprintf_dbuf(db, "db=%p\n", db);
1763
1764         return (db);
1765 }
1766
1767 static int
1768 dbuf_do_evict(void *private)
1769 {
1770         arc_buf_t *buf = private;
1771         dmu_buf_impl_t *db = buf->b_private;
1772
1773         if (!MUTEX_HELD(&db->db_mtx))
1774                 mutex_enter(&db->db_mtx);
1775
1776         ASSERT(refcount_is_zero(&db->db_holds));
1777
1778         if (db->db_state != DB_EVICTING) {
1779                 ASSERT(db->db_state == DB_CACHED);
1780                 DBUF_VERIFY(db);
1781                 db->db_buf = NULL;
1782                 dbuf_evict(db);
1783         } else {
1784                 mutex_exit(&db->db_mtx);
1785                 dbuf_destroy(db);
1786         }
1787         return (0);
1788 }
1789
1790 static void
1791 dbuf_destroy(dmu_buf_impl_t *db)
1792 {
1793         ASSERT(refcount_is_zero(&db->db_holds));
1794
1795         if (db->db_blkid != DMU_BONUS_BLKID) {
1796                 /*
1797                  * If this dbuf is still on the dn_dbufs list,
1798                  * remove it from that list.
1799                  */
1800                 if (db->db_dnode_handle != NULL) {
1801                         dnode_t *dn;
1802
1803                         DB_DNODE_ENTER(db);
1804                         dn = DB_DNODE(db);
1805                         mutex_enter(&dn->dn_dbufs_mtx);
1806                         list_remove(&dn->dn_dbufs, db);
1807                         (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1808                         mutex_exit(&dn->dn_dbufs_mtx);
1809                         DB_DNODE_EXIT(db);
1810                         /*
1811                          * Decrementing the dbuf count means that the hold
1812                          * corresponding to the removed dbuf is no longer
1813                          * discounted in dnode_move(), so the dnode cannot be
1814                          * moved until after we release the hold.
1815                          */
1816                         dnode_rele(dn, db);
1817                         db->db_dnode_handle = NULL;
1818                 }
1819                 dbuf_hash_remove(db);
1820         }
1821         db->db_parent = NULL;
1822         db->db_buf = NULL;
1823
1824         ASSERT(!list_link_active(&db->db_link));
1825         ASSERT(db->db.db_data == NULL);
1826         ASSERT(db->db_hash_next == NULL);
1827         ASSERT(db->db_blkptr == NULL);
1828         ASSERT(db->db_data_pending == NULL);
1829
1830         kmem_cache_free(dbuf_cache, db);
1831         arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1832 }
1833
1834 void
1835 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1836 {
1837         dmu_buf_impl_t *db = NULL;
1838         blkptr_t *bp = NULL;
1839
1840         ASSERT(blkid != DMU_BONUS_BLKID);
1841         ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1842
1843         if (dnode_block_freed(dn, blkid))
1844                 return;
1845
1846         /* dbuf_find() returns with db_mtx held */
1847         if ((db = dbuf_find(dn, 0, blkid))) {
1848                 /*
1849                  * This dbuf is already in the cache.  We assume that
1850                  * it is already CACHED, or else about to be either
1851                  * read or filled.
1852                  */
1853                 mutex_exit(&db->db_mtx);
1854                 return;
1855         }
1856
1857         if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp, NULL) == 0) {
1858                 if (bp && !BP_IS_HOLE(bp)) {
1859                         int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1860                             ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1861                         dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1862                         uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1863                         zbookmark_t zb;
1864
1865                         SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1866                             dn->dn_object, 0, blkid);
1867
1868                         (void) arc_read(NULL, dn->dn_objset->os_spa,
1869                             bp, NULL, NULL, priority,
1870                             ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1871                             &aflags, &zb);
1872                 }
1873                 if (db)
1874                         dbuf_rele(db, NULL);
1875         }
1876 }
1877
1878 #define DBUF_HOLD_IMPL_MAX_DEPTH        20
1879
1880 /*
1881  * Returns with db_holds incremented, and db_mtx not held.
1882  * Note: dn_struct_rwlock must be held.
1883  */
1884 static int
1885 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
1886 {
1887         ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
1888         dh->dh_parent = NULL;
1889
1890         ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
1891         ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
1892         ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
1893
1894         *(dh->dh_dbp) = NULL;
1895 top:
1896         /* dbuf_find() returns with db_mtx held */
1897         dh->dh_db = dbuf_find(dh->dh_dn, dh->dh_level, dh->dh_blkid);
1898
1899         if (dh->dh_db == NULL) {
1900                 dh->dh_bp = NULL;
1901
1902                 ASSERT3P(dh->dh_parent, ==, NULL);
1903                 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1904                                         dh->dh_fail_sparse, &dh->dh_parent,
1905                                         &dh->dh_bp, dh);
1906                 if (dh->dh_fail_sparse) {
1907                         if (dh->dh_err == 0 && dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
1908                                 dh->dh_err = ENOENT;
1909                         if (dh->dh_err) {
1910                                 if (dh->dh_parent)
1911                                         dbuf_rele(dh->dh_parent, NULL);
1912                                 return (dh->dh_err);
1913                         }
1914                 }
1915                 if (dh->dh_err && dh->dh_err != ENOENT)
1916                         return (dh->dh_err);
1917                 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1918                                         dh->dh_parent, dh->dh_bp);
1919         }
1920
1921         if (dh->dh_db->db_buf && refcount_is_zero(&dh->dh_db->db_holds)) {
1922                 arc_buf_add_ref(dh->dh_db->db_buf, dh->dh_db);
1923                 if (dh->dh_db->db_buf->b_data == NULL) {
1924                         dbuf_clear(dh->dh_db);
1925                         if (dh->dh_parent) {
1926                                 dbuf_rele(dh->dh_parent, NULL);
1927                                 dh->dh_parent = NULL;
1928                         }
1929                         goto top;
1930                 }
1931                 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
1932         }
1933
1934         ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
1935
1936         /*
1937          * If this buffer is currently syncing out, and we are are
1938          * still referencing it from db_data, we need to make a copy
1939          * of it in case we decide we want to dirty it again in this txg.
1940          */
1941         if (dh->dh_db->db_level == 0 &&
1942             dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
1943             dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
1944             dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
1945                 dh->dh_dr = dh->dh_db->db_data_pending;
1946
1947                 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
1948                         dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
1949
1950                         dbuf_set_data(dh->dh_db,
1951                             arc_buf_alloc(dh->dh_dn->dn_objset->os_spa,
1952                             dh->dh_db->db.db_size, dh->dh_db, dh->dh_type));
1953                         bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
1954                             dh->dh_db->db.db_data, dh->dh_db->db.db_size);
1955                 }
1956         }
1957
1958         (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
1959         dbuf_update_data(dh->dh_db);
1960         DBUF_VERIFY(dh->dh_db);
1961         mutex_exit(&dh->dh_db->db_mtx);
1962
1963         /* NOTE: we can't rele the parent until after we drop the db_mtx */
1964         if (dh->dh_parent)
1965                 dbuf_rele(dh->dh_parent, NULL);
1966
1967         ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
1968         ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
1969         ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
1970         *(dh->dh_dbp) = dh->dh_db;
1971
1972         return (0);
1973 }
1974
1975 /*
1976  * The following code preserves the recursive function dbuf_hold_impl()
1977  * but moves the local variables AND function arguments to the heap to
1978  * minimize the stack frame size.  Enough space is initially allocated
1979  * on the stack for 20 levels of recursion.
1980  */
1981 int
1982 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1983     void *tag, dmu_buf_impl_t **dbp)
1984 {
1985         struct dbuf_hold_impl_data *dh;
1986         int error;
1987
1988         dh = kmem_zalloc(sizeof(struct dbuf_hold_impl_data) *
1989             DBUF_HOLD_IMPL_MAX_DEPTH, KM_PUSHPAGE);
1990         __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse, tag, dbp, 0);
1991
1992         error = __dbuf_hold_impl(dh);
1993
1994         kmem_free(dh, sizeof(struct dbuf_hold_impl_data) *
1995             DBUF_HOLD_IMPL_MAX_DEPTH);
1996
1997         return (error);
1998 }
1999
2000 static void
2001 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2002     dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2003     void *tag, dmu_buf_impl_t **dbp, int depth)
2004 {
2005         dh->dh_dn = dn;
2006         dh->dh_level = level;
2007         dh->dh_blkid = blkid;
2008         dh->dh_fail_sparse = fail_sparse;
2009         dh->dh_tag = tag;
2010         dh->dh_dbp = dbp;
2011         dh->dh_depth = depth;
2012 }
2013
2014 dmu_buf_impl_t *
2015 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2016 {
2017         dmu_buf_impl_t *db;
2018         int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
2019         return (err ? NULL : db);
2020 }
2021
2022 dmu_buf_impl_t *
2023 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2024 {
2025         dmu_buf_impl_t *db;
2026         int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2027         return (err ? NULL : db);
2028 }
2029
2030 void
2031 dbuf_create_bonus(dnode_t *dn)
2032 {
2033         ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2034
2035         ASSERT(dn->dn_bonus == NULL);
2036         dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2037 }
2038
2039 int
2040 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2041 {
2042         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2043         dnode_t *dn;
2044
2045         if (db->db_blkid != DMU_SPILL_BLKID)
2046                 return (ENOTSUP);
2047         if (blksz == 0)
2048                 blksz = SPA_MINBLOCKSIZE;
2049         if (blksz > SPA_MAXBLOCKSIZE)
2050                 blksz = SPA_MAXBLOCKSIZE;
2051         else
2052                 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2053
2054         DB_DNODE_ENTER(db);
2055         dn = DB_DNODE(db);
2056         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2057         dbuf_new_size(db, blksz, tx);
2058         rw_exit(&dn->dn_struct_rwlock);
2059         DB_DNODE_EXIT(db);
2060
2061         return (0);
2062 }
2063
2064 void
2065 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2066 {
2067         dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2068 }
2069
2070 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2071 void
2072 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2073 {
2074         VERIFY(refcount_add(&db->db_holds, tag) > 1);
2075 }
2076
2077 /*
2078  * If you call dbuf_rele() you had better not be referencing the dnode handle
2079  * unless you have some other direct or indirect hold on the dnode. (An indirect
2080  * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2081  * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2082  * dnode's parent dbuf evicting its dnode handles.
2083  */
2084 #pragma weak dmu_buf_rele = dbuf_rele
2085 void
2086 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2087 {
2088         mutex_enter(&db->db_mtx);
2089         dbuf_rele_and_unlock(db, tag);
2090 }
2091
2092 /*
2093  * dbuf_rele() for an already-locked dbuf.  This is necessary to allow
2094  * db_dirtycnt and db_holds to be updated atomically.
2095  */
2096 void
2097 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2098 {
2099         int64_t holds;
2100
2101         ASSERT(MUTEX_HELD(&db->db_mtx));
2102         DBUF_VERIFY(db);
2103
2104         /*
2105          * Remove the reference to the dbuf before removing its hold on the
2106          * dnode so we can guarantee in dnode_move() that a referenced bonus
2107          * buffer has a corresponding dnode hold.
2108          */
2109         holds = refcount_remove(&db->db_holds, tag);
2110         ASSERT(holds >= 0);
2111
2112         /*
2113          * We can't freeze indirects if there is a possibility that they
2114          * may be modified in the current syncing context.
2115          */
2116         if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2117                 arc_buf_freeze(db->db_buf);
2118
2119         if (holds == db->db_dirtycnt &&
2120             db->db_level == 0 && db->db_immediate_evict)
2121                 dbuf_evict_user(db);
2122
2123         if (holds == 0) {
2124                 if (db->db_blkid == DMU_BONUS_BLKID) {
2125                         mutex_exit(&db->db_mtx);
2126
2127                         /*
2128                          * If the dnode moves here, we cannot cross this barrier
2129                          * until the move completes.
2130                          */
2131                         DB_DNODE_ENTER(db);
2132                         (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2133                         DB_DNODE_EXIT(db);
2134                         /*
2135                          * The bonus buffer's dnode hold is no longer discounted
2136                          * in dnode_move(). The dnode cannot move until after
2137                          * the dnode_rele().
2138                          */
2139                         dnode_rele(DB_DNODE(db), db);
2140                 } else if (db->db_buf == NULL) {
2141                         /*
2142                          * This is a special case: we never associated this
2143                          * dbuf with any data allocated from the ARC.
2144                          */
2145                         ASSERT(db->db_state == DB_UNCACHED ||
2146                             db->db_state == DB_NOFILL);
2147                         dbuf_evict(db);
2148                 } else if (arc_released(db->db_buf)) {
2149                         arc_buf_t *buf = db->db_buf;
2150                         /*
2151                          * This dbuf has anonymous data associated with it.
2152                          */
2153                         dbuf_set_data(db, NULL);
2154                         VERIFY(arc_buf_remove_ref(buf, db));
2155                         dbuf_evict(db);
2156                 } else {
2157                         VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2158
2159                         /*
2160                          * A dbuf will be eligible for eviction if either the
2161                          * 'primarycache' property is set or a duplicate
2162                          * copy of this buffer is already cached in the arc.
2163                          *
2164                          * In the case of the 'primarycache' a buffer
2165                          * is considered for eviction if it matches the
2166                          * criteria set in the property.
2167                          *
2168                          * To decide if our buffer is considered a
2169                          * duplicate, we must call into the arc to determine
2170                          * if multiple buffers are referencing the same
2171                          * block on-disk. If so, then we simply evict
2172                          * ourselves.
2173                          */
2174                         if (!DBUF_IS_CACHEABLE(db) ||
2175                             arc_buf_eviction_needed(db->db_buf))
2176                                 dbuf_clear(db);
2177                         else
2178                                 mutex_exit(&db->db_mtx);
2179                 }
2180         } else {
2181                 mutex_exit(&db->db_mtx);
2182         }
2183 }
2184
2185 #pragma weak dmu_buf_refcount = dbuf_refcount
2186 uint64_t
2187 dbuf_refcount(dmu_buf_impl_t *db)
2188 {
2189         return (refcount_count(&db->db_holds));
2190 }
2191
2192 void *
2193 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2194     dmu_buf_evict_func_t *evict_func)
2195 {
2196         return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2197             user_data_ptr_ptr, evict_func));
2198 }
2199
2200 void *
2201 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2202     dmu_buf_evict_func_t *evict_func)
2203 {
2204         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2205
2206         db->db_immediate_evict = TRUE;
2207         return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2208             user_data_ptr_ptr, evict_func));
2209 }
2210
2211 void *
2212 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2213     void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2214 {
2215         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2216         ASSERT(db->db_level == 0);
2217
2218         ASSERT((user_ptr == NULL) == (evict_func == NULL));
2219
2220         mutex_enter(&db->db_mtx);
2221
2222         if (db->db_user_ptr == old_user_ptr) {
2223                 db->db_user_ptr = user_ptr;
2224                 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2225                 db->db_evict_func = evict_func;
2226
2227                 dbuf_update_data(db);
2228         } else {
2229                 old_user_ptr = db->db_user_ptr;
2230         }
2231
2232         mutex_exit(&db->db_mtx);
2233         return (old_user_ptr);
2234 }
2235
2236 void *
2237 dmu_buf_get_user(dmu_buf_t *db_fake)
2238 {
2239         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2240         ASSERT(!refcount_is_zero(&db->db_holds));
2241
2242         return (db->db_user_ptr);
2243 }
2244
2245 boolean_t
2246 dmu_buf_freeable(dmu_buf_t *dbuf)
2247 {
2248         boolean_t res = B_FALSE;
2249         dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2250
2251         if (db->db_blkptr)
2252                 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2253                     db->db_blkptr, db->db_blkptr->blk_birth);
2254
2255         return (res);
2256 }
2257
2258 static void
2259 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2260 {
2261         /* ASSERT(dmu_tx_is_syncing(tx) */
2262         ASSERT(MUTEX_HELD(&db->db_mtx));
2263
2264         if (db->db_blkptr != NULL)
2265                 return;
2266
2267         if (db->db_blkid == DMU_SPILL_BLKID) {
2268                 db->db_blkptr = &dn->dn_phys->dn_spill;
2269                 BP_ZERO(db->db_blkptr);
2270                 return;
2271         }
2272         if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2273                 /*
2274                  * This buffer was allocated at a time when there was
2275                  * no available blkptrs from the dnode, or it was
2276                  * inappropriate to hook it in (i.e., nlevels mis-match).
2277                  */
2278                 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2279                 ASSERT(db->db_parent == NULL);
2280                 db->db_parent = dn->dn_dbuf;
2281                 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2282                 DBUF_VERIFY(db);
2283         } else {
2284                 dmu_buf_impl_t *parent = db->db_parent;
2285                 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2286
2287                 ASSERT(dn->dn_phys->dn_nlevels > 1);
2288                 if (parent == NULL) {
2289                         mutex_exit(&db->db_mtx);
2290                         rw_enter(&dn->dn_struct_rwlock, RW_READER);
2291                         (void) dbuf_hold_impl(dn, db->db_level+1,
2292                             db->db_blkid >> epbs, FALSE, db, &parent);
2293                         rw_exit(&dn->dn_struct_rwlock);
2294                         mutex_enter(&db->db_mtx);
2295                         db->db_parent = parent;
2296                 }
2297                 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2298                     (db->db_blkid & ((1ULL << epbs) - 1));
2299                 DBUF_VERIFY(db);
2300         }
2301 }
2302
2303 /* dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2304  * is critical the we not allow the compiler to inline this function in to
2305  * dbuf_sync_list() thereby drastically bloating the stack usage.
2306  */
2307 noinline static void
2308 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2309 {
2310         dmu_buf_impl_t *db = dr->dr_dbuf;
2311         dnode_t *dn;
2312         zio_t *zio;
2313
2314         ASSERT(dmu_tx_is_syncing(tx));
2315
2316         dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2317
2318         mutex_enter(&db->db_mtx);
2319
2320         ASSERT(db->db_level > 0);
2321         DBUF_VERIFY(db);
2322
2323         if (db->db_buf == NULL) {
2324                 mutex_exit(&db->db_mtx);
2325                 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2326                 mutex_enter(&db->db_mtx);
2327         }
2328         ASSERT3U(db->db_state, ==, DB_CACHED);
2329         ASSERT(db->db_buf != NULL);
2330
2331         DB_DNODE_ENTER(db);
2332         dn = DB_DNODE(db);
2333         ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2334         dbuf_check_blkptr(dn, db);
2335         DB_DNODE_EXIT(db);
2336
2337         db->db_data_pending = dr;
2338
2339         mutex_exit(&db->db_mtx);
2340         dbuf_write(dr, db->db_buf, tx);
2341
2342         zio = dr->dr_zio;
2343         mutex_enter(&dr->dt.di.dr_mtx);
2344         dbuf_sync_list(&dr->dt.di.dr_children, tx);
2345         ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2346         mutex_exit(&dr->dt.di.dr_mtx);
2347         zio_nowait(zio);
2348 }
2349
2350 /* dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2351  * critical the we not allow the compiler to inline this function in to
2352  * dbuf_sync_list() thereby drastically bloating the stack usage.
2353  */
2354 noinline static void
2355 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2356 {
2357         arc_buf_t **datap = &dr->dt.dl.dr_data;
2358         dmu_buf_impl_t *db = dr->dr_dbuf;
2359         dnode_t *dn;
2360         objset_t *os;
2361         uint64_t txg = tx->tx_txg;
2362
2363         ASSERT(dmu_tx_is_syncing(tx));
2364
2365         dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2366
2367         mutex_enter(&db->db_mtx);
2368         /*
2369          * To be synced, we must be dirtied.  But we
2370          * might have been freed after the dirty.
2371          */
2372         if (db->db_state == DB_UNCACHED) {
2373                 /* This buffer has been freed since it was dirtied */
2374                 ASSERT(db->db.db_data == NULL);
2375         } else if (db->db_state == DB_FILL) {
2376                 /* This buffer was freed and is now being re-filled */
2377                 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2378         } else {
2379                 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2380         }
2381         DBUF_VERIFY(db);
2382
2383         DB_DNODE_ENTER(db);
2384         dn = DB_DNODE(db);
2385
2386         if (db->db_blkid == DMU_SPILL_BLKID) {
2387                 mutex_enter(&dn->dn_mtx);
2388                 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2389                 mutex_exit(&dn->dn_mtx);
2390         }
2391
2392         /*
2393          * If this is a bonus buffer, simply copy the bonus data into the
2394          * dnode.  It will be written out when the dnode is synced (and it
2395          * will be synced, since it must have been dirty for dbuf_sync to
2396          * be called).
2397          */
2398         if (db->db_blkid == DMU_BONUS_BLKID) {
2399                 dbuf_dirty_record_t **drp;
2400
2401                 ASSERT(*datap != NULL);
2402                 ASSERT0(db->db_level);
2403                 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2404                 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2405                 DB_DNODE_EXIT(db);
2406
2407                 if (*datap != db->db.db_data) {
2408                         zio_buf_free(*datap, DN_MAX_BONUSLEN);
2409                         arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2410                 }
2411                 db->db_data_pending = NULL;
2412                 drp = &db->db_last_dirty;
2413                 while (*drp != dr)
2414                         drp = &(*drp)->dr_next;
2415                 ASSERT(dr->dr_next == NULL);
2416                 ASSERT(dr->dr_dbuf == db);
2417                 *drp = dr->dr_next;
2418                 if (dr->dr_dbuf->db_level != 0) {
2419                         mutex_destroy(&dr->dt.di.dr_mtx);
2420                         list_destroy(&dr->dt.di.dr_children);
2421                 }
2422                 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2423                 ASSERT(db->db_dirtycnt > 0);
2424                 db->db_dirtycnt -= 1;
2425                 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2426                 return;
2427         }
2428
2429         os = dn->dn_objset;
2430
2431         /*
2432          * This function may have dropped the db_mtx lock allowing a dmu_sync
2433          * operation to sneak in. As a result, we need to ensure that we
2434          * don't check the dr_override_state until we have returned from
2435          * dbuf_check_blkptr.
2436          */
2437         dbuf_check_blkptr(dn, db);
2438
2439         /*
2440          * If this buffer is in the middle of an immediate write,
2441          * wait for the synchronous IO to complete.
2442          */
2443         while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2444                 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2445                 cv_wait(&db->db_changed, &db->db_mtx);
2446                 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2447         }
2448
2449         if (db->db_state != DB_NOFILL &&
2450             dn->dn_object != DMU_META_DNODE_OBJECT &&
2451             refcount_count(&db->db_holds) > 1 &&
2452             dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2453             *datap == db->db_buf) {
2454                 /*
2455                  * If this buffer is currently "in use" (i.e., there
2456                  * are active holds and db_data still references it),
2457                  * then make a copy before we start the write so that
2458                  * any modifications from the open txg will not leak
2459                  * into this write.
2460                  *
2461                  * NOTE: this copy does not need to be made for
2462                  * objects only modified in the syncing context (e.g.
2463                  * DNONE_DNODE blocks).
2464                  */
2465                 int blksz = arc_buf_size(*datap);
2466                 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2467                 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2468                 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2469         }
2470         db->db_data_pending = dr;
2471
2472         mutex_exit(&db->db_mtx);
2473
2474         dbuf_write(dr, *datap, tx);
2475
2476         ASSERT(!list_link_active(&dr->dr_dirty_node));
2477         if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2478                 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2479                 DB_DNODE_EXIT(db);
2480         } else {
2481                 /*
2482                  * Although zio_nowait() does not "wait for an IO", it does
2483                  * initiate the IO. If this is an empty write it seems plausible
2484                  * that the IO could actually be completed before the nowait
2485                  * returns. We need to DB_DNODE_EXIT() first in case
2486                  * zio_nowait() invalidates the dbuf.
2487                  */
2488                 DB_DNODE_EXIT(db);
2489                 zio_nowait(dr->dr_zio);
2490         }
2491 }
2492
2493 void
2494 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2495 {
2496         dbuf_dirty_record_t *dr;
2497
2498         while ((dr = list_head(list))) {
2499                 if (dr->dr_zio != NULL) {
2500                         /*
2501                          * If we find an already initialized zio then we
2502                          * are processing the meta-dnode, and we have finished.
2503                          * The dbufs for all dnodes are put back on the list
2504                          * during processing, so that we can zio_wait()
2505                          * these IOs after initiating all child IOs.
2506                          */
2507                         ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2508                             DMU_META_DNODE_OBJECT);
2509                         break;
2510                 }
2511                 list_remove(list, dr);
2512                 if (dr->dr_dbuf->db_level > 0)
2513                         dbuf_sync_indirect(dr, tx);
2514                 else
2515                         dbuf_sync_leaf(dr, tx);
2516         }
2517 }
2518
2519 /* ARGSUSED */
2520 static void
2521 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2522 {
2523         dmu_buf_impl_t *db = vdb;
2524         dnode_t *dn;
2525         blkptr_t *bp = zio->io_bp;
2526         blkptr_t *bp_orig = &zio->io_bp_orig;
2527         spa_t *spa = zio->io_spa;
2528         int64_t delta;
2529         uint64_t fill = 0;
2530         int i;
2531
2532         ASSERT(db->db_blkptr == bp);
2533
2534         DB_DNODE_ENTER(db);
2535         dn = DB_DNODE(db);
2536         delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2537         dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2538         zio->io_prev_space_delta = delta;
2539
2540         if (BP_IS_HOLE(bp)) {
2541                 ASSERT(bp->blk_fill == 0);
2542                 DB_DNODE_EXIT(db);
2543                 return;
2544         }
2545
2546         ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2547             BP_GET_TYPE(bp) == dn->dn_type) ||
2548             (db->db_blkid == DMU_SPILL_BLKID &&
2549             BP_GET_TYPE(bp) == dn->dn_bonustype));
2550         ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2551
2552         mutex_enter(&db->db_mtx);
2553
2554 #ifdef ZFS_DEBUG
2555         if (db->db_blkid == DMU_SPILL_BLKID) {
2556                 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2557                 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2558                     db->db_blkptr == &dn->dn_phys->dn_spill);
2559         }
2560 #endif
2561
2562         if (db->db_level == 0) {
2563                 mutex_enter(&dn->dn_mtx);
2564                 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2565                     db->db_blkid != DMU_SPILL_BLKID)
2566                         dn->dn_phys->dn_maxblkid = db->db_blkid;
2567                 mutex_exit(&dn->dn_mtx);
2568
2569                 if (dn->dn_type == DMU_OT_DNODE) {
2570                         dnode_phys_t *dnp = db->db.db_data;
2571                         for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2572                             i--, dnp++) {
2573                                 if (dnp->dn_type != DMU_OT_NONE)
2574                                         fill++;
2575                         }
2576                 } else {
2577                         fill = 1;
2578                 }
2579         } else {
2580                 blkptr_t *ibp = db->db.db_data;
2581                 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2582                 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2583                         if (BP_IS_HOLE(ibp))
2584                                 continue;
2585                         fill += ibp->blk_fill;
2586                 }
2587         }
2588         DB_DNODE_EXIT(db);
2589
2590         bp->blk_fill = fill;
2591
2592         mutex_exit(&db->db_mtx);
2593 }
2594
2595 /* ARGSUSED */
2596 static void
2597 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2598 {
2599         dmu_buf_impl_t *db = vdb;
2600         blkptr_t *bp = zio->io_bp;
2601         blkptr_t *bp_orig = &zio->io_bp_orig;
2602         uint64_t txg = zio->io_txg;
2603         dbuf_dirty_record_t **drp, *dr;
2604
2605         ASSERT0(zio->io_error);
2606         ASSERT(db->db_blkptr == bp);
2607
2608         if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
2609                 ASSERT(BP_EQUAL(bp, bp_orig));
2610         } else {
2611                 objset_t *os;
2612                 dsl_dataset_t *ds;
2613                 dmu_tx_t *tx;
2614
2615                 DB_GET_OBJSET(&os, db);
2616                 ds = os->os_dsl_dataset;
2617                 tx = os->os_synctx;
2618
2619                 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2620                 dsl_dataset_block_born(ds, bp, tx);
2621         }
2622
2623         mutex_enter(&db->db_mtx);
2624
2625         DBUF_VERIFY(db);
2626
2627         drp = &db->db_last_dirty;
2628         while ((dr = *drp) != db->db_data_pending)
2629                 drp = &dr->dr_next;
2630         ASSERT(!list_link_active(&dr->dr_dirty_node));
2631         ASSERT(dr->dr_txg == txg);
2632         ASSERT(dr->dr_dbuf == db);
2633         ASSERT(dr->dr_next == NULL);
2634         *drp = dr->dr_next;
2635
2636 #ifdef ZFS_DEBUG
2637         if (db->db_blkid == DMU_SPILL_BLKID) {
2638                 dnode_t *dn;
2639
2640                 DB_DNODE_ENTER(db);
2641                 dn = DB_DNODE(db);
2642                 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2643                 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2644                     db->db_blkptr == &dn->dn_phys->dn_spill);
2645                 DB_DNODE_EXIT(db);
2646         }
2647 #endif
2648
2649         if (db->db_level == 0) {
2650                 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2651                 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2652                 if (db->db_state != DB_NOFILL) {
2653                         if (dr->dt.dl.dr_data != db->db_buf)
2654                                 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2655                                     db));
2656                         else if (!arc_released(db->db_buf))
2657                                 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2658                 }
2659         } else {
2660                 dnode_t *dn;
2661
2662                 DB_DNODE_ENTER(db);
2663                 dn = DB_DNODE(db);
2664                 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2665                 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2666                 if (!BP_IS_HOLE(db->db_blkptr)) {
2667                         ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
2668                             SPA_BLKPTRSHIFT);
2669                         ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2670                             db->db.db_size);
2671                         ASSERT3U(dn->dn_phys->dn_maxblkid
2672                             >> (db->db_level * epbs), >=, db->db_blkid);
2673                         arc_set_callback(db->db_buf, dbuf_do_evict, db);
2674                 }
2675                 DB_DNODE_EXIT(db);
2676                 mutex_destroy(&dr->dt.di.dr_mtx);
2677                 list_destroy(&dr->dt.di.dr_children);
2678         }
2679         kmem_free(dr, sizeof (dbuf_dirty_record_t));
2680
2681         cv_broadcast(&db->db_changed);
2682         ASSERT(db->db_dirtycnt > 0);
2683         db->db_dirtycnt -= 1;
2684         db->db_data_pending = NULL;
2685         dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2686 }
2687
2688 static void
2689 dbuf_write_nofill_ready(zio_t *zio)
2690 {
2691         dbuf_write_ready(zio, NULL, zio->io_private);
2692 }
2693
2694 static void
2695 dbuf_write_nofill_done(zio_t *zio)
2696 {
2697         dbuf_write_done(zio, NULL, zio->io_private);
2698 }
2699
2700 static void
2701 dbuf_write_override_ready(zio_t *zio)
2702 {
2703         dbuf_dirty_record_t *dr = zio->io_private;
2704         dmu_buf_impl_t *db = dr->dr_dbuf;
2705
2706         dbuf_write_ready(zio, NULL, db);
2707 }
2708
2709 static void
2710 dbuf_write_override_done(zio_t *zio)
2711 {
2712         dbuf_dirty_record_t *dr = zio->io_private;
2713         dmu_buf_impl_t *db = dr->dr_dbuf;
2714         blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2715
2716         mutex_enter(&db->db_mtx);
2717         if (!BP_EQUAL(zio->io_bp, obp)) {
2718                 if (!BP_IS_HOLE(obp))
2719                         dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2720                 arc_release(dr->dt.dl.dr_data, db);
2721         }
2722         mutex_exit(&db->db_mtx);
2723
2724         dbuf_write_done(zio, NULL, db);
2725 }
2726
2727 static void
2728 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2729 {
2730         dmu_buf_impl_t *db = dr->dr_dbuf;
2731         dnode_t *dn;
2732         objset_t *os;
2733         dmu_buf_impl_t *parent = db->db_parent;
2734         uint64_t txg = tx->tx_txg;
2735         zbookmark_t zb;
2736         zio_prop_t zp;
2737         zio_t *zio;
2738         int wp_flag = 0;
2739
2740         DB_DNODE_ENTER(db);
2741         dn = DB_DNODE(db);
2742         os = dn->dn_objset;
2743
2744         if (db->db_state != DB_NOFILL) {
2745                 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2746                         /*
2747                          * Private object buffers are released here rather
2748                          * than in dbuf_dirty() since they are only modified
2749                          * in the syncing context and we don't want the
2750                          * overhead of making multiple copies of the data.
2751                          */
2752                         if (BP_IS_HOLE(db->db_blkptr)) {
2753                                 arc_buf_thaw(data);
2754                         } else {
2755                                 dbuf_release_bp(db);
2756                         }
2757                 }
2758         }
2759
2760         if (parent != dn->dn_dbuf) {
2761                 ASSERT(parent && parent->db_data_pending);
2762                 ASSERT(db->db_level == parent->db_level-1);
2763                 ASSERT(arc_released(parent->db_buf));
2764                 zio = parent->db_data_pending->dr_zio;
2765         } else {
2766                 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2767                     db->db_blkid != DMU_SPILL_BLKID) ||
2768                     (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2769                 if (db->db_blkid != DMU_SPILL_BLKID)
2770                         ASSERT3P(db->db_blkptr, ==,
2771                             &dn->dn_phys->dn_blkptr[db->db_blkid]);
2772                 zio = dn->dn_zio;
2773         }
2774
2775         ASSERT(db->db_level == 0 || data == db->db_buf);
2776         ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2777         ASSERT(zio);
2778
2779         SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2780             os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2781             db->db.db_object, db->db_level, db->db_blkid);
2782
2783         if (db->db_blkid == DMU_SPILL_BLKID)
2784                 wp_flag = WP_SPILL;
2785         wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2786
2787         dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2788         DB_DNODE_EXIT(db);
2789
2790         if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2791                 ASSERT(db->db_state != DB_NOFILL);
2792                 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2793                     db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2794                     dbuf_write_override_ready, dbuf_write_override_done, dr,
2795                     ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2796                 mutex_enter(&db->db_mtx);
2797                 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2798                 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2799                     dr->dt.dl.dr_copies);
2800                 mutex_exit(&db->db_mtx);
2801         } else if (db->db_state == DB_NOFILL) {
2802                 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2803                 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2804                     db->db_blkptr, NULL, db->db.db_size, &zp,
2805                     dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2806                     ZIO_PRIORITY_ASYNC_WRITE,
2807                     ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2808         } else {
2809                 ASSERT(arc_released(data));
2810                 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2811                     db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
2812                     DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
2813                     dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
2814                     ZIO_FLAG_MUSTSUCCEED, &zb);
2815         }
2816 }
2817
2818 #if defined(_KERNEL) && defined(HAVE_SPL)
2819 EXPORT_SYMBOL(dbuf_find);
2820 EXPORT_SYMBOL(dbuf_is_metadata);
2821 EXPORT_SYMBOL(dbuf_evict);
2822 EXPORT_SYMBOL(dbuf_loan_arcbuf);
2823 EXPORT_SYMBOL(dbuf_whichblock);
2824 EXPORT_SYMBOL(dbuf_read);
2825 EXPORT_SYMBOL(dbuf_unoverride);
2826 EXPORT_SYMBOL(dbuf_free_range);
2827 EXPORT_SYMBOL(dbuf_new_size);
2828 EXPORT_SYMBOL(dbuf_release_bp);
2829 EXPORT_SYMBOL(dbuf_dirty);
2830 EXPORT_SYMBOL(dmu_buf_will_dirty);
2831 EXPORT_SYMBOL(dmu_buf_will_not_fill);
2832 EXPORT_SYMBOL(dmu_buf_will_fill);
2833 EXPORT_SYMBOL(dmu_buf_fill_done);
2834 EXPORT_SYMBOL(dmu_buf_rele);
2835 EXPORT_SYMBOL(dbuf_assign_arcbuf);
2836 EXPORT_SYMBOL(dbuf_clear);
2837 EXPORT_SYMBOL(dbuf_prefetch);
2838 EXPORT_SYMBOL(dbuf_hold_impl);
2839 EXPORT_SYMBOL(dbuf_hold);
2840 EXPORT_SYMBOL(dbuf_hold_level);
2841 EXPORT_SYMBOL(dbuf_create_bonus);
2842 EXPORT_SYMBOL(dbuf_spill_set_blksz);
2843 EXPORT_SYMBOL(dbuf_rm_spill);
2844 EXPORT_SYMBOL(dbuf_add_ref);
2845 EXPORT_SYMBOL(dbuf_rele);
2846 EXPORT_SYMBOL(dbuf_rele_and_unlock);
2847 EXPORT_SYMBOL(dbuf_refcount);
2848 EXPORT_SYMBOL(dbuf_sync_list);
2849 EXPORT_SYMBOL(dmu_buf_set_user);
2850 EXPORT_SYMBOL(dmu_buf_set_user_ie);
2851 EXPORT_SYMBOL(dmu_buf_update_user);
2852 EXPORT_SYMBOL(dmu_buf_get_user);
2853 EXPORT_SYMBOL(dmu_buf_freeable);
2854 #endif