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