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