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