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