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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 #include <sys/zfs_context.h>
31 #include <sys/dmu_send.h>
32 #include <sys/dmu_impl.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dmu_tx.h>
40 #include <sys/dmu_zfetch.h>
42 #include <sys/sa_impl.h>
43 #include <sys/zfeature.h>
44 #include <sys/blkptr.h>
45 #include <sys/range_tree.h>
47 struct dbuf_hold_impl_data
{
48 /* Function arguments */
54 dmu_buf_impl_t
**dh_dbp
;
56 dmu_buf_impl_t
*dh_db
;
57 dmu_buf_impl_t
*dh_parent
;
60 dbuf_dirty_record_t
*dh_dr
;
61 arc_buf_contents_t dh_type
;
65 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
66 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
67 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
68 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
71 * Number of times that zfs_free_range() took the slow path while doing
72 * a zfs receive. A nonzero value indicates a potential performance problem.
74 uint64_t zfs_free_range_recv_miss
;
76 static void dbuf_destroy(dmu_buf_impl_t
*db
);
77 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
78 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
81 * Global data structures and functions for the dbuf cache.
83 static kmem_cache_t
*dbuf_cache
;
87 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
89 dmu_buf_impl_t
*db
= vdb
;
90 bzero(db
, sizeof (dmu_buf_impl_t
));
92 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
93 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
94 refcount_create(&db
->db_holds
);
95 list_link_init(&db
->db_link
);
101 dbuf_dest(void *vdb
, void *unused
)
103 dmu_buf_impl_t
*db
= vdb
;
104 mutex_destroy(&db
->db_mtx
);
105 cv_destroy(&db
->db_changed
);
106 refcount_destroy(&db
->db_holds
);
110 * dbuf hash table routines
112 static dbuf_hash_table_t dbuf_hash_table
;
114 static uint64_t dbuf_hash_count
;
117 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
119 uintptr_t osv
= (uintptr_t)os
;
120 uint64_t crc
= -1ULL;
122 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
123 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
124 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
125 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
126 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
127 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
128 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
130 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
135 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
137 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
138 ((dbuf)->db.db_object == (obj) && \
139 (dbuf)->db_objset == (os) && \
140 (dbuf)->db_level == (level) && \
141 (dbuf)->db_blkid == (blkid))
144 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
146 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
147 objset_t
*os
= dn
->dn_objset
;
154 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
155 idx
= hv
& h
->hash_table_mask
;
157 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
158 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
159 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
160 mutex_enter(&db
->db_mtx
);
161 if (db
->db_state
!= DB_EVICTING
) {
162 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
165 mutex_exit(&db
->db_mtx
);
168 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
173 * Insert an entry into the hash table. If there is already an element
174 * equal to elem in the hash table, then the already existing element
175 * will be returned and the new element will not be inserted.
176 * Otherwise returns NULL.
178 static dmu_buf_impl_t
*
179 dbuf_hash_insert(dmu_buf_impl_t
*db
)
181 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
182 objset_t
*os
= db
->db_objset
;
183 uint64_t obj
= db
->db
.db_object
;
184 int level
= db
->db_level
;
185 uint64_t blkid
, hv
, idx
;
188 blkid
= db
->db_blkid
;
189 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
190 idx
= hv
& h
->hash_table_mask
;
192 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
193 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
194 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
195 mutex_enter(&dbf
->db_mtx
);
196 if (dbf
->db_state
!= DB_EVICTING
) {
197 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
200 mutex_exit(&dbf
->db_mtx
);
204 mutex_enter(&db
->db_mtx
);
205 db
->db_hash_next
= h
->hash_table
[idx
];
206 h
->hash_table
[idx
] = db
;
207 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
208 atomic_add_64(&dbuf_hash_count
, 1);
214 * Remove an entry from the hash table. It must be in the EVICTING state.
217 dbuf_hash_remove(dmu_buf_impl_t
*db
)
219 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
221 dmu_buf_impl_t
*dbf
, **dbp
;
223 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
224 db
->db_level
, db
->db_blkid
);
225 idx
= hv
& h
->hash_table_mask
;
228 * We musn't hold db_mtx to maintain lock ordering:
229 * DBUF_HASH_MUTEX > db_mtx.
231 ASSERT(refcount_is_zero(&db
->db_holds
));
232 ASSERT(db
->db_state
== DB_EVICTING
);
233 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
235 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
236 dbp
= &h
->hash_table
[idx
];
237 while ((dbf
= *dbp
) != db
) {
238 dbp
= &dbf
->db_hash_next
;
241 *dbp
= db
->db_hash_next
;
242 db
->db_hash_next
= NULL
;
243 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
244 atomic_add_64(&dbuf_hash_count
, -1);
247 static arc_evict_func_t dbuf_do_evict
;
250 dbuf_evict_user(dmu_buf_impl_t
*db
)
252 ASSERT(MUTEX_HELD(&db
->db_mtx
));
254 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
257 if (db
->db_user_data_ptr_ptr
)
258 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
259 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
260 db
->db_user_ptr
= NULL
;
261 db
->db_user_data_ptr_ptr
= NULL
;
262 db
->db_evict_func
= NULL
;
266 dbuf_is_metadata(dmu_buf_impl_t
*db
)
269 * Consider indirect blocks and spill blocks to be meta data.
271 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
274 boolean_t is_metadata
;
277 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
280 return (is_metadata
);
285 dbuf_evict(dmu_buf_impl_t
*db
)
287 ASSERT(MUTEX_HELD(&db
->db_mtx
));
288 ASSERT(db
->db_buf
== NULL
);
289 ASSERT(db
->db_data_pending
== NULL
);
298 uint64_t hsize
= 1ULL << 16;
299 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
303 * The hash table is big enough to fill all of physical memory
304 * with an average 4K block size. The table will take up
305 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
307 while (hsize
* 4096 < physmem
* PAGESIZE
)
311 h
->hash_table_mask
= hsize
- 1;
312 #if defined(_KERNEL) && defined(HAVE_SPL)
314 * Large allocations which do not require contiguous pages
315 * should be using vmem_alloc() in the linux kernel
317 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_PUSHPAGE
);
319 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
321 if (h
->hash_table
== NULL
) {
322 /* XXX - we should really return an error instead of assert */
323 ASSERT(hsize
> (1ULL << 10));
328 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
329 sizeof (dmu_buf_impl_t
),
330 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
332 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
333 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
341 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
344 dbuf_stats_destroy();
346 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
347 mutex_destroy(&h
->hash_mutexes
[i
]);
348 #if defined(_KERNEL) && defined(HAVE_SPL)
350 * Large allocations which do not require contiguous pages
351 * should be using vmem_free() in the linux kernel
353 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
355 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
357 kmem_cache_destroy(dbuf_cache
);
366 dbuf_verify(dmu_buf_impl_t
*db
)
369 dbuf_dirty_record_t
*dr
;
371 ASSERT(MUTEX_HELD(&db
->db_mtx
));
373 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
376 ASSERT(db
->db_objset
!= NULL
);
380 ASSERT(db
->db_parent
== NULL
);
381 ASSERT(db
->db_blkptr
== NULL
);
383 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
384 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
385 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
386 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
387 db
->db_blkid
== DMU_SPILL_BLKID
||
388 !list_is_empty(&dn
->dn_dbufs
));
390 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
392 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
393 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
394 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
396 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
397 ASSERT0(db
->db
.db_offset
);
399 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
402 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
403 ASSERT(dr
->dr_dbuf
== db
);
405 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
406 ASSERT(dr
->dr_dbuf
== db
);
409 * We can't assert that db_size matches dn_datablksz because it
410 * can be momentarily different when another thread is doing
413 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
414 dr
= db
->db_data_pending
;
416 * It should only be modified in syncing context, so
417 * make sure we only have one copy of the data.
419 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
422 /* verify db->db_blkptr */
424 if (db
->db_parent
== dn
->dn_dbuf
) {
425 /* db is pointed to by the dnode */
426 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
427 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
428 ASSERT(db
->db_parent
== NULL
);
430 ASSERT(db
->db_parent
!= NULL
);
431 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
432 ASSERT3P(db
->db_blkptr
, ==,
433 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
435 /* db is pointed to by an indirect block */
436 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
438 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
439 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
442 * dnode_grow_indblksz() can make this fail if we don't
443 * have the struct_rwlock. XXX indblksz no longer
444 * grows. safe to do this now?
446 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
447 ASSERT3P(db
->db_blkptr
, ==,
448 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
449 db
->db_blkid
% epb
));
453 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
454 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
455 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
456 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
458 * If the blkptr isn't set but they have nonzero data,
459 * it had better be dirty, otherwise we'll lose that
460 * data when we evict this buffer.
462 if (db
->db_dirtycnt
== 0) {
463 ASSERTV(uint64_t *buf
= db
->db
.db_data
);
466 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
476 dbuf_update_data(dmu_buf_impl_t
*db
)
478 ASSERT(MUTEX_HELD(&db
->db_mtx
));
479 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
480 ASSERT(!refcount_is_zero(&db
->db_holds
));
481 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
486 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
488 ASSERT(MUTEX_HELD(&db
->db_mtx
));
491 ASSERT(buf
->b_data
!= NULL
);
492 db
->db
.db_data
= buf
->b_data
;
493 if (!arc_released(buf
))
494 arc_set_callback(buf
, dbuf_do_evict
, db
);
495 dbuf_update_data(db
);
498 db
->db
.db_data
= NULL
;
499 if (db
->db_state
!= DB_NOFILL
)
500 db
->db_state
= DB_UNCACHED
;
505 * Loan out an arc_buf for read. Return the loaned arc_buf.
508 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
512 mutex_enter(&db
->db_mtx
);
513 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
514 int blksz
= db
->db
.db_size
;
515 spa_t
*spa
= db
->db_objset
->os_spa
;
517 mutex_exit(&db
->db_mtx
);
518 abuf
= arc_loan_buf(spa
, blksz
);
519 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
522 arc_loan_inuse_buf(abuf
, db
);
523 dbuf_set_data(db
, NULL
);
524 mutex_exit(&db
->db_mtx
);
530 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
532 if (dn
->dn_datablkshift
) {
533 return (offset
>> dn
->dn_datablkshift
);
535 ASSERT3U(offset
, <, dn
->dn_datablksz
);
541 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
543 dmu_buf_impl_t
*db
= vdb
;
545 mutex_enter(&db
->db_mtx
);
546 ASSERT3U(db
->db_state
, ==, DB_READ
);
548 * All reads are synchronous, so we must have a hold on the dbuf
550 ASSERT(refcount_count(&db
->db_holds
) > 0);
551 ASSERT(db
->db_buf
== NULL
);
552 ASSERT(db
->db
.db_data
== NULL
);
553 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
554 /* we were freed in flight; disregard any error */
555 arc_release(buf
, db
);
556 bzero(buf
->b_data
, db
->db
.db_size
);
558 db
->db_freed_in_flight
= FALSE
;
559 dbuf_set_data(db
, buf
);
560 db
->db_state
= DB_CACHED
;
561 } else if (zio
== NULL
|| zio
->io_error
== 0) {
562 dbuf_set_data(db
, buf
);
563 db
->db_state
= DB_CACHED
;
565 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
566 ASSERT3P(db
->db_buf
, ==, NULL
);
567 VERIFY(arc_buf_remove_ref(buf
, db
));
568 db
->db_state
= DB_UNCACHED
;
570 cv_broadcast(&db
->db_changed
);
571 dbuf_rele_and_unlock(db
, NULL
);
575 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
579 uint32_t aflags
= ARC_NOWAIT
;
584 ASSERT(!refcount_is_zero(&db
->db_holds
));
585 /* We need the struct_rwlock to prevent db_blkptr from changing. */
586 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
587 ASSERT(MUTEX_HELD(&db
->db_mtx
));
588 ASSERT(db
->db_state
== DB_UNCACHED
);
589 ASSERT(db
->db_buf
== NULL
);
591 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
592 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
594 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
595 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
596 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
597 if (bonuslen
< DN_MAX_BONUSLEN
)
598 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
600 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
602 dbuf_update_data(db
);
603 db
->db_state
= DB_CACHED
;
604 mutex_exit(&db
->db_mtx
);
609 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
610 * processes the delete record and clears the bp while we are waiting
611 * for the dn_mtx (resulting in a "no" from block_freed).
613 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
614 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
615 BP_IS_HOLE(db
->db_blkptr
)))) {
616 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
619 dbuf_set_data(db
, arc_buf_alloc(db
->db_objset
->os_spa
,
620 db
->db
.db_size
, db
, type
));
621 bzero(db
->db
.db_data
, db
->db
.db_size
);
622 db
->db_state
= DB_CACHED
;
623 *flags
|= DB_RF_CACHED
;
624 mutex_exit(&db
->db_mtx
);
630 db
->db_state
= DB_READ
;
631 mutex_exit(&db
->db_mtx
);
633 if (DBUF_IS_L2CACHEABLE(db
))
634 aflags
|= ARC_L2CACHE
;
635 if (DBUF_IS_L2COMPRESSIBLE(db
))
636 aflags
|= ARC_L2COMPRESS
;
638 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
639 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
640 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
642 dbuf_add_ref(db
, NULL
);
644 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
645 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
646 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
648 if (aflags
& ARC_CACHED
)
649 *flags
|= DB_RF_CACHED
;
651 return (SET_ERROR(err
));
655 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
658 boolean_t havepzio
= (zio
!= NULL
);
663 * We don't have to hold the mutex to check db_state because it
664 * can't be freed while we have a hold on the buffer.
666 ASSERT(!refcount_is_zero(&db
->db_holds
));
668 if (db
->db_state
== DB_NOFILL
)
669 return (SET_ERROR(EIO
));
673 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
674 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
676 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
677 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
678 DBUF_IS_CACHEABLE(db
);
680 mutex_enter(&db
->db_mtx
);
681 if (db
->db_state
== DB_CACHED
) {
682 mutex_exit(&db
->db_mtx
);
684 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
685 db
->db
.db_size
, TRUE
);
686 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
687 rw_exit(&dn
->dn_struct_rwlock
);
689 } else if (db
->db_state
== DB_UNCACHED
) {
690 spa_t
*spa
= dn
->dn_objset
->os_spa
;
693 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
695 err
= dbuf_read_impl(db
, zio
, &flags
);
697 /* dbuf_read_impl has dropped db_mtx for us */
699 if (!err
&& prefetch
)
700 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
701 db
->db
.db_size
, flags
& DB_RF_CACHED
);
703 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
704 rw_exit(&dn
->dn_struct_rwlock
);
707 if (!err
&& !havepzio
)
711 * Another reader came in while the dbuf was in flight
712 * between UNCACHED and CACHED. Either a writer will finish
713 * writing the buffer (sending the dbuf to CACHED) or the
714 * first reader's request will reach the read_done callback
715 * and send the dbuf to CACHED. Otherwise, a failure
716 * occurred and the dbuf went to UNCACHED.
718 mutex_exit(&db
->db_mtx
);
720 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
721 db
->db
.db_size
, TRUE
);
722 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
723 rw_exit(&dn
->dn_struct_rwlock
);
726 /* Skip the wait per the caller's request. */
727 mutex_enter(&db
->db_mtx
);
728 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
729 while (db
->db_state
== DB_READ
||
730 db
->db_state
== DB_FILL
) {
731 ASSERT(db
->db_state
== DB_READ
||
732 (flags
& DB_RF_HAVESTRUCT
) == 0);
733 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
735 cv_wait(&db
->db_changed
, &db
->db_mtx
);
737 if (db
->db_state
== DB_UNCACHED
)
738 err
= SET_ERROR(EIO
);
740 mutex_exit(&db
->db_mtx
);
743 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
748 dbuf_noread(dmu_buf_impl_t
*db
)
750 ASSERT(!refcount_is_zero(&db
->db_holds
));
751 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
752 mutex_enter(&db
->db_mtx
);
753 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
754 cv_wait(&db
->db_changed
, &db
->db_mtx
);
755 if (db
->db_state
== DB_UNCACHED
) {
756 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
757 spa_t
*spa
= db
->db_objset
->os_spa
;
759 ASSERT(db
->db_buf
== NULL
);
760 ASSERT(db
->db
.db_data
== NULL
);
761 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
762 db
->db_state
= DB_FILL
;
763 } else if (db
->db_state
== DB_NOFILL
) {
764 dbuf_set_data(db
, NULL
);
766 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
768 mutex_exit(&db
->db_mtx
);
772 * This is our just-in-time copy function. It makes a copy of
773 * buffers, that have been modified in a previous transaction
774 * group, before we modify them in the current active group.
776 * This function is used in two places: when we are dirtying a
777 * buffer for the first time in a txg, and when we are freeing
778 * a range in a dnode that includes this buffer.
780 * Note that when we are called from dbuf_free_range() we do
781 * not put a hold on the buffer, we just traverse the active
782 * dbuf list for the dnode.
785 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
787 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
789 ASSERT(MUTEX_HELD(&db
->db_mtx
));
790 ASSERT(db
->db
.db_data
!= NULL
);
791 ASSERT(db
->db_level
== 0);
792 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
795 (dr
->dt
.dl
.dr_data
!=
796 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
800 * If the last dirty record for this dbuf has not yet synced
801 * and its referencing the dbuf data, either:
802 * reset the reference to point to a new copy,
803 * or (if there a no active holders)
804 * just null out the current db_data pointer.
806 ASSERT(dr
->dr_txg
>= txg
- 2);
807 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
808 /* Note that the data bufs here are zio_bufs */
809 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
810 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
811 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
812 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
813 int size
= db
->db
.db_size
;
814 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
815 spa_t
*spa
= db
->db_objset
->os_spa
;
817 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
818 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
820 dbuf_set_data(db
, NULL
);
825 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
827 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
828 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
829 uint64_t txg
= dr
->dr_txg
;
831 ASSERT(MUTEX_HELD(&db
->db_mtx
));
832 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
833 ASSERT(db
->db_level
== 0);
835 if (db
->db_blkid
== DMU_BONUS_BLKID
||
836 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
839 ASSERT(db
->db_data_pending
!= dr
);
841 /* free this block */
842 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
843 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
845 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
846 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
849 * Release the already-written buffer, so we leave it in
850 * a consistent dirty state. Note that all callers are
851 * modifying the buffer, so they will immediately do
852 * another (redundant) arc_release(). Therefore, leave
853 * the buf thawed to save the effort of freezing &
854 * immediately re-thawing it.
856 arc_release(dr
->dt
.dl
.dr_data
, db
);
860 * Evict (if its unreferenced) or clear (if its referenced) any level-0
861 * data blocks in the free range, so that any future readers will find
864 * This is a no-op if the dataset is in the middle of an incremental
865 * receive; see comment below for details.
868 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
870 dmu_buf_impl_t
*db
, *db_next
;
871 uint64_t txg
= tx
->tx_txg
;
872 boolean_t freespill
=
873 (start
== DMU_SPILL_BLKID
|| end
== DMU_SPILL_BLKID
);
875 if (end
> dn
->dn_maxblkid
&& !freespill
)
876 end
= dn
->dn_maxblkid
;
877 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
879 mutex_enter(&dn
->dn_dbufs_mtx
);
880 if (start
>= dn
->dn_unlisted_l0_blkid
* dn
->dn_datablksz
&&
882 /* There can't be any dbufs in this range; no need to search. */
883 mutex_exit(&dn
->dn_dbufs_mtx
);
885 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
887 * If we are receiving, we expect there to be no dbufs in
888 * the range to be freed, because receive modifies each
889 * block at most once, and in offset order. If this is
890 * not the case, it can lead to performance problems,
891 * so note that we unexpectedly took the slow path.
893 atomic_inc_64(&zfs_free_range_recv_miss
);
896 for (db
= list_head(&dn
->dn_dbufs
); db
!= NULL
; db
= db_next
) {
897 db_next
= list_next(&dn
->dn_dbufs
, db
);
898 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
900 if (db
->db_level
!= 0)
902 if ((db
->db_blkid
< start
|| db
->db_blkid
> end
) && !freespill
)
905 /* found a level 0 buffer in the range */
906 mutex_enter(&db
->db_mtx
);
907 if (dbuf_undirty(db
, tx
)) {
908 /* mutex has been dropped and dbuf destroyed */
912 if (db
->db_state
== DB_UNCACHED
||
913 db
->db_state
== DB_NOFILL
||
914 db
->db_state
== DB_EVICTING
) {
915 ASSERT(db
->db
.db_data
== NULL
);
916 mutex_exit(&db
->db_mtx
);
919 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
920 /* will be handled in dbuf_read_done or dbuf_rele */
921 db
->db_freed_in_flight
= TRUE
;
922 mutex_exit(&db
->db_mtx
);
925 if (refcount_count(&db
->db_holds
) == 0) {
930 /* The dbuf is referenced */
932 if (db
->db_last_dirty
!= NULL
) {
933 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
935 if (dr
->dr_txg
== txg
) {
937 * This buffer is "in-use", re-adjust the file
938 * size to reflect that this buffer may
939 * contain new data when we sync.
941 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
942 db
->db_blkid
> dn
->dn_maxblkid
)
943 dn
->dn_maxblkid
= db
->db_blkid
;
947 * This dbuf is not dirty in the open context.
948 * Either uncache it (if its not referenced in
949 * the open context) or reset its contents to
952 dbuf_fix_old_data(db
, txg
);
955 /* clear the contents if its cached */
956 if (db
->db_state
== DB_CACHED
) {
957 ASSERT(db
->db
.db_data
!= NULL
);
958 arc_release(db
->db_buf
, db
);
959 bzero(db
->db
.db_data
, db
->db
.db_size
);
960 arc_buf_freeze(db
->db_buf
);
963 mutex_exit(&db
->db_mtx
);
965 mutex_exit(&dn
->dn_dbufs_mtx
);
969 dbuf_block_freeable(dmu_buf_impl_t
*db
)
971 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
972 uint64_t birth_txg
= 0;
975 * We don't need any locking to protect db_blkptr:
976 * If it's syncing, then db_last_dirty will be set
977 * so we'll ignore db_blkptr.
979 * This logic ensures that only block births for
980 * filled blocks are considered.
982 ASSERT(MUTEX_HELD(&db
->db_mtx
));
983 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
984 !BP_IS_HOLE(db
->db_blkptr
))) {
985 birth_txg
= db
->db_last_dirty
->dr_txg
;
986 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
987 birth_txg
= db
->db_blkptr
->blk_birth
;
991 * If this block don't exist or is in a snapshot, it can't be freed.
992 * Don't pass the bp to dsl_dataset_block_freeable() since we
993 * are holding the db_mtx lock and might deadlock if we are
994 * prefetching a dedup-ed block.
997 return (ds
== NULL
||
998 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1004 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1006 arc_buf_t
*buf
, *obuf
;
1007 int osize
= db
->db
.db_size
;
1008 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1011 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1016 /* XXX does *this* func really need the lock? */
1017 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1020 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1021 * is OK, because there can be no other references to the db
1022 * when we are changing its size, so no concurrent DB_FILL can
1026 * XXX we should be doing a dbuf_read, checking the return
1027 * value and returning that up to our callers
1029 dmu_buf_will_dirty(&db
->db
, tx
);
1031 /* create the data buffer for the new block */
1032 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1034 /* copy old block data to the new block */
1036 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1037 /* zero the remainder */
1039 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1041 mutex_enter(&db
->db_mtx
);
1042 dbuf_set_data(db
, buf
);
1043 VERIFY(arc_buf_remove_ref(obuf
, db
));
1044 db
->db
.db_size
= size
;
1046 if (db
->db_level
== 0) {
1047 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1048 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1050 mutex_exit(&db
->db_mtx
);
1052 dnode_willuse_space(dn
, size
-osize
, tx
);
1057 dbuf_release_bp(dmu_buf_impl_t
*db
)
1059 ASSERTV(objset_t
*os
= db
->db_objset
);
1061 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1062 ASSERT(arc_released(os
->os_phys_buf
) ||
1063 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1064 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1066 (void) arc_release(db
->db_buf
, db
);
1069 dbuf_dirty_record_t
*
1070 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1074 dbuf_dirty_record_t
**drp
, *dr
;
1075 int drop_struct_lock
= FALSE
;
1076 boolean_t do_free_accounting
= B_FALSE
;
1077 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1079 ASSERT(tx
->tx_txg
!= 0);
1080 ASSERT(!refcount_is_zero(&db
->db_holds
));
1081 DMU_TX_DIRTY_BUF(tx
, db
);
1086 * Shouldn't dirty a regular buffer in syncing context. Private
1087 * objects may be dirtied in syncing context, but only if they
1088 * were already pre-dirtied in open context.
1090 ASSERT(!dmu_tx_is_syncing(tx
) ||
1091 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1092 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1093 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1095 * We make this assert for private objects as well, but after we
1096 * check if we're already dirty. They are allowed to re-dirty
1097 * in syncing context.
1099 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1100 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1101 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1103 mutex_enter(&db
->db_mtx
);
1105 * XXX make this true for indirects too? The problem is that
1106 * transactions created with dmu_tx_create_assigned() from
1107 * syncing context don't bother holding ahead.
1109 ASSERT(db
->db_level
!= 0 ||
1110 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1111 db
->db_state
== DB_NOFILL
);
1113 mutex_enter(&dn
->dn_mtx
);
1115 * Don't set dirtyctx to SYNC if we're just modifying this as we
1116 * initialize the objset.
1118 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1119 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1121 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1122 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1123 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_PUSHPAGE
);
1125 mutex_exit(&dn
->dn_mtx
);
1127 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1128 dn
->dn_have_spill
= B_TRUE
;
1131 * If this buffer is already dirty, we're done.
1133 drp
= &db
->db_last_dirty
;
1134 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1135 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1136 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1138 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1141 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1143 * If this buffer has already been written out,
1144 * we now need to reset its state.
1146 dbuf_unoverride(dr
);
1147 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1148 db
->db_state
!= DB_NOFILL
)
1149 arc_buf_thaw(db
->db_buf
);
1151 mutex_exit(&db
->db_mtx
);
1156 * Only valid if not already dirty.
1158 ASSERT(dn
->dn_object
== 0 ||
1159 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1160 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1162 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1163 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1164 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1165 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1166 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1167 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1170 * We should only be dirtying in syncing context if it's the
1171 * mos or we're initializing the os or it's a special object.
1172 * However, we are allowed to dirty in syncing context provided
1173 * we already dirtied it in open context. Hence we must make
1174 * this assertion only if we're not already dirty.
1177 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1178 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1179 ASSERT(db
->db
.db_size
!= 0);
1181 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1183 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1185 * Update the accounting.
1186 * Note: we delay "free accounting" until after we drop
1187 * the db_mtx. This keeps us from grabbing other locks
1188 * (and possibly deadlocking) in bp_get_dsize() while
1189 * also holding the db_mtx.
1191 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1192 do_free_accounting
= dbuf_block_freeable(db
);
1196 * If this buffer is dirty in an old transaction group we need
1197 * to make a copy of it so that the changes we make in this
1198 * transaction group won't leak out when we sync the older txg.
1200 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_PUSHPAGE
);
1201 list_link_init(&dr
->dr_dirty_node
);
1202 if (db
->db_level
== 0) {
1203 void *data_old
= db
->db_buf
;
1205 if (db
->db_state
!= DB_NOFILL
) {
1206 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1207 dbuf_fix_old_data(db
, tx
->tx_txg
);
1208 data_old
= db
->db
.db_data
;
1209 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1211 * Release the data buffer from the cache so
1212 * that we can modify it without impacting
1213 * possible other users of this cached data
1214 * block. Note that indirect blocks and
1215 * private objects are not released until the
1216 * syncing state (since they are only modified
1219 arc_release(db
->db_buf
, db
);
1220 dbuf_fix_old_data(db
, tx
->tx_txg
);
1221 data_old
= db
->db_buf
;
1223 ASSERT(data_old
!= NULL
);
1225 dr
->dt
.dl
.dr_data
= data_old
;
1227 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1228 list_create(&dr
->dt
.di
.dr_children
,
1229 sizeof (dbuf_dirty_record_t
),
1230 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1232 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1233 dr
->dr_accounted
= db
->db
.db_size
;
1235 dr
->dr_txg
= tx
->tx_txg
;
1240 * We could have been freed_in_flight between the dbuf_noread
1241 * and dbuf_dirty. We win, as though the dbuf_noread() had
1242 * happened after the free.
1244 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1245 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1246 mutex_enter(&dn
->dn_mtx
);
1247 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1248 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1251 mutex_exit(&dn
->dn_mtx
);
1252 db
->db_freed_in_flight
= FALSE
;
1256 * This buffer is now part of this txg
1258 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1259 db
->db_dirtycnt
+= 1;
1260 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1262 mutex_exit(&db
->db_mtx
);
1264 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1265 db
->db_blkid
== DMU_SPILL_BLKID
) {
1266 mutex_enter(&dn
->dn_mtx
);
1267 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1268 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1269 mutex_exit(&dn
->dn_mtx
);
1270 dnode_setdirty(dn
, tx
);
1273 } else if (do_free_accounting
) {
1274 blkptr_t
*bp
= db
->db_blkptr
;
1275 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1276 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1278 * This is only a guess -- if the dbuf is dirty
1279 * in a previous txg, we don't know how much
1280 * space it will use on disk yet. We should
1281 * really have the struct_rwlock to access
1282 * db_blkptr, but since this is just a guess,
1283 * it's OK if we get an odd answer.
1285 ddt_prefetch(os
->os_spa
, bp
);
1286 dnode_willuse_space(dn
, -willfree
, tx
);
1289 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1290 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1291 drop_struct_lock
= TRUE
;
1294 if (db
->db_level
== 0) {
1295 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1296 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1299 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1300 dmu_buf_impl_t
*parent
= db
->db_parent
;
1301 dbuf_dirty_record_t
*di
;
1302 int parent_held
= FALSE
;
1304 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1305 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1307 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1308 db
->db_blkid
>> epbs
, FTAG
);
1309 ASSERT(parent
!= NULL
);
1312 if (drop_struct_lock
)
1313 rw_exit(&dn
->dn_struct_rwlock
);
1314 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1315 di
= dbuf_dirty(parent
, tx
);
1317 dbuf_rele(parent
, FTAG
);
1319 mutex_enter(&db
->db_mtx
);
1321 * Since we've dropped the mutex, it's possible that
1322 * dbuf_undirty() might have changed this out from under us.
1324 if (db
->db_last_dirty
== dr
||
1325 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1326 mutex_enter(&di
->dt
.di
.dr_mtx
);
1327 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1328 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1329 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1330 mutex_exit(&di
->dt
.di
.dr_mtx
);
1333 mutex_exit(&db
->db_mtx
);
1335 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1336 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1337 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1338 mutex_enter(&dn
->dn_mtx
);
1339 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1340 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1341 mutex_exit(&dn
->dn_mtx
);
1342 if (drop_struct_lock
)
1343 rw_exit(&dn
->dn_struct_rwlock
);
1346 dnode_setdirty(dn
, tx
);
1352 * Undirty a buffer in the transaction group referenced by the given
1353 * transaction. Return whether this evicted the dbuf.
1356 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1359 uint64_t txg
= tx
->tx_txg
;
1360 dbuf_dirty_record_t
*dr
, **drp
;
1363 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1364 ASSERT0(db
->db_level
);
1365 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1368 * If this buffer is not dirty, we're done.
1370 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1371 if (dr
->dr_txg
<= txg
)
1373 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1375 ASSERT(dr
->dr_txg
== txg
);
1376 ASSERT(dr
->dr_dbuf
== db
);
1381 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1383 ASSERT(db
->db
.db_size
!= 0);
1386 * Any space we accounted for in dp_dirty_* will be cleaned up by
1387 * dsl_pool_sync(). This is relatively rare so the discrepancy
1388 * is not a big deal.
1394 * Note that there are three places in dbuf_dirty()
1395 * where this dirty record may be put on a list.
1396 * Make sure to do a list_remove corresponding to
1397 * every one of those list_insert calls.
1399 if (dr
->dr_parent
) {
1400 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1401 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1402 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1403 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1404 db
->db_level
+1 == dn
->dn_nlevels
) {
1405 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1406 mutex_enter(&dn
->dn_mtx
);
1407 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1408 mutex_exit(&dn
->dn_mtx
);
1412 if (db
->db_state
!= DB_NOFILL
) {
1413 dbuf_unoverride(dr
);
1415 ASSERT(db
->db_buf
!= NULL
);
1416 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1417 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1418 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1420 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1422 ASSERT(db
->db_dirtycnt
> 0);
1423 db
->db_dirtycnt
-= 1;
1425 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1426 arc_buf_t
*buf
= db
->db_buf
;
1428 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1429 dbuf_set_data(db
, NULL
);
1430 VERIFY(arc_buf_remove_ref(buf
, db
));
1439 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1441 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1442 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1444 ASSERT(tx
->tx_txg
!= 0);
1445 ASSERT(!refcount_is_zero(&db
->db_holds
));
1448 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1449 rf
|= DB_RF_HAVESTRUCT
;
1451 (void) dbuf_read(db
, NULL
, rf
);
1452 (void) dbuf_dirty(db
, tx
);
1456 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1458 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1460 db
->db_state
= DB_NOFILL
;
1462 dmu_buf_will_fill(db_fake
, tx
);
1466 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1468 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1470 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1471 ASSERT(tx
->tx_txg
!= 0);
1472 ASSERT(db
->db_level
== 0);
1473 ASSERT(!refcount_is_zero(&db
->db_holds
));
1475 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1476 dmu_tx_private_ok(tx
));
1479 (void) dbuf_dirty(db
, tx
);
1482 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1485 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1487 mutex_enter(&db
->db_mtx
);
1490 if (db
->db_state
== DB_FILL
) {
1491 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1492 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1493 /* we were freed while filling */
1494 /* XXX dbuf_undirty? */
1495 bzero(db
->db
.db_data
, db
->db
.db_size
);
1496 db
->db_freed_in_flight
= FALSE
;
1498 db
->db_state
= DB_CACHED
;
1499 cv_broadcast(&db
->db_changed
);
1501 mutex_exit(&db
->db_mtx
);
1505 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1506 bp_embedded_type_t etype
, enum zio_compress comp
,
1507 int uncompressed_size
, int compressed_size
, int byteorder
,
1510 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1511 struct dirty_leaf
*dl
;
1512 dmu_object_type_t type
;
1515 type
= DB_DNODE(db
)->dn_type
;
1518 ASSERT0(db
->db_level
);
1519 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1521 dmu_buf_will_not_fill(dbuf
, tx
);
1523 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1524 dl
= &db
->db_last_dirty
->dt
.dl
;
1525 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1526 data
, comp
, uncompressed_size
, compressed_size
);
1527 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1528 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1529 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1530 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1532 dl
->dr_override_state
= DR_OVERRIDDEN
;
1533 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1537 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1538 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1541 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1543 ASSERT(!refcount_is_zero(&db
->db_holds
));
1544 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1545 ASSERT(db
->db_level
== 0);
1546 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1547 ASSERT(buf
!= NULL
);
1548 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1549 ASSERT(tx
->tx_txg
!= 0);
1551 arc_return_buf(buf
, db
);
1552 ASSERT(arc_released(buf
));
1554 mutex_enter(&db
->db_mtx
);
1556 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1557 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1559 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1561 if (db
->db_state
== DB_CACHED
&&
1562 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1563 mutex_exit(&db
->db_mtx
);
1564 (void) dbuf_dirty(db
, tx
);
1565 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1566 VERIFY(arc_buf_remove_ref(buf
, db
));
1567 xuio_stat_wbuf_copied();
1571 xuio_stat_wbuf_nocopy();
1572 if (db
->db_state
== DB_CACHED
) {
1573 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1575 ASSERT(db
->db_buf
!= NULL
);
1576 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1577 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1578 if (!arc_released(db
->db_buf
)) {
1579 ASSERT(dr
->dt
.dl
.dr_override_state
==
1581 arc_release(db
->db_buf
, db
);
1583 dr
->dt
.dl
.dr_data
= buf
;
1584 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1585 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1586 arc_release(db
->db_buf
, db
);
1587 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1591 ASSERT(db
->db_buf
== NULL
);
1592 dbuf_set_data(db
, buf
);
1593 db
->db_state
= DB_FILL
;
1594 mutex_exit(&db
->db_mtx
);
1595 (void) dbuf_dirty(db
, tx
);
1596 dmu_buf_fill_done(&db
->db
, tx
);
1600 * "Clear" the contents of this dbuf. This will mark the dbuf
1601 * EVICTING and clear *most* of its references. Unfortunately,
1602 * when we are not holding the dn_dbufs_mtx, we can't clear the
1603 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1604 * in this case. For callers from the DMU we will usually see:
1605 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1606 * For the arc callback, we will usually see:
1607 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1608 * Sometimes, though, we will get a mix of these two:
1609 * DMU: dbuf_clear()->arc_clear_callback()
1610 * ARC: dbuf_do_evict()->dbuf_destroy()
1612 * This routine will dissociate the dbuf from the arc, by calling
1613 * arc_clear_callback(), but will not evict the data from the ARC.
1616 dbuf_clear(dmu_buf_impl_t
*db
)
1619 dmu_buf_impl_t
*parent
= db
->db_parent
;
1620 dmu_buf_impl_t
*dndb
;
1621 boolean_t dbuf_gone
= B_FALSE
;
1623 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1624 ASSERT(refcount_is_zero(&db
->db_holds
));
1626 dbuf_evict_user(db
);
1628 if (db
->db_state
== DB_CACHED
) {
1629 ASSERT(db
->db
.db_data
!= NULL
);
1630 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1631 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1632 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1634 db
->db
.db_data
= NULL
;
1635 db
->db_state
= DB_UNCACHED
;
1638 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1639 ASSERT(db
->db_data_pending
== NULL
);
1641 db
->db_state
= DB_EVICTING
;
1642 db
->db_blkptr
= NULL
;
1647 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1648 list_remove(&dn
->dn_dbufs
, db
);
1649 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1653 * Decrementing the dbuf count means that the hold corresponding
1654 * to the removed dbuf is no longer discounted in dnode_move(),
1655 * so the dnode cannot be moved until after we release the hold.
1656 * The membar_producer() ensures visibility of the decremented
1657 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1661 db
->db_dnode_handle
= NULL
;
1667 dbuf_gone
= arc_clear_callback(db
->db_buf
);
1670 mutex_exit(&db
->db_mtx
);
1673 * If this dbuf is referenced from an indirect dbuf,
1674 * decrement the ref count on the indirect dbuf.
1676 if (parent
&& parent
!= dndb
)
1677 dbuf_rele(parent
, db
);
1680 __attribute__((always_inline
))
1682 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1683 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
1690 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1692 if (blkid
== DMU_SPILL_BLKID
) {
1693 mutex_enter(&dn
->dn_mtx
);
1694 if (dn
->dn_have_spill
&&
1695 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1696 *bpp
= &dn
->dn_phys
->dn_spill
;
1699 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1700 *parentp
= dn
->dn_dbuf
;
1701 mutex_exit(&dn
->dn_mtx
);
1705 if (dn
->dn_phys
->dn_nlevels
== 0)
1708 nlevels
= dn
->dn_phys
->dn_nlevels
;
1710 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1712 ASSERT3U(level
* epbs
, <, 64);
1713 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1714 if (level
>= nlevels
||
1715 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1716 /* the buffer has no parent yet */
1717 return (SET_ERROR(ENOENT
));
1718 } else if (level
< nlevels
-1) {
1719 /* this block is referenced from an indirect block */
1722 err
= dbuf_hold_impl(dn
, level
+1, blkid
>> epbs
,
1723 fail_sparse
, NULL
, parentp
);
1725 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
1726 blkid
>> epbs
, fail_sparse
, NULL
,
1727 parentp
, dh
->dh_depth
+ 1);
1728 err
= __dbuf_hold_impl(dh
+ 1);
1732 err
= dbuf_read(*parentp
, NULL
,
1733 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1735 dbuf_rele(*parentp
, NULL
);
1739 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1740 (blkid
& ((1ULL << epbs
) - 1));
1743 /* the block is referenced from the dnode */
1744 ASSERT3U(level
, ==, nlevels
-1);
1745 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1746 blkid
< dn
->dn_phys
->dn_nblkptr
);
1748 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1749 *parentp
= dn
->dn_dbuf
;
1751 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1756 static dmu_buf_impl_t
*
1757 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1758 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1760 objset_t
*os
= dn
->dn_objset
;
1761 dmu_buf_impl_t
*db
, *odb
;
1763 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1764 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1766 db
= kmem_cache_alloc(dbuf_cache
, KM_PUSHPAGE
);
1769 db
->db
.db_object
= dn
->dn_object
;
1770 db
->db_level
= level
;
1771 db
->db_blkid
= blkid
;
1772 db
->db_last_dirty
= NULL
;
1773 db
->db_dirtycnt
= 0;
1774 db
->db_dnode_handle
= dn
->dn_handle
;
1775 db
->db_parent
= parent
;
1776 db
->db_blkptr
= blkptr
;
1778 db
->db_user_ptr
= NULL
;
1779 db
->db_user_data_ptr_ptr
= NULL
;
1780 db
->db_evict_func
= NULL
;
1781 db
->db_immediate_evict
= 0;
1782 db
->db_freed_in_flight
= 0;
1784 if (blkid
== DMU_BONUS_BLKID
) {
1785 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1786 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1787 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1788 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1789 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1790 db
->db_state
= DB_UNCACHED
;
1791 /* the bonus dbuf is not placed in the hash table */
1792 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1794 } else if (blkid
== DMU_SPILL_BLKID
) {
1795 db
->db
.db_size
= (blkptr
!= NULL
) ?
1796 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1797 db
->db
.db_offset
= 0;
1800 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
1801 db
->db
.db_size
= blocksize
;
1802 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1806 * Hold the dn_dbufs_mtx while we get the new dbuf
1807 * in the hash table *and* added to the dbufs list.
1808 * This prevents a possible deadlock with someone
1809 * trying to look up this dbuf before its added to the
1812 mutex_enter(&dn
->dn_dbufs_mtx
);
1813 db
->db_state
= DB_EVICTING
;
1814 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1815 /* someone else inserted it first */
1816 kmem_cache_free(dbuf_cache
, db
);
1817 mutex_exit(&dn
->dn_dbufs_mtx
);
1820 list_insert_head(&dn
->dn_dbufs
, db
);
1821 if (db
->db_level
== 0 && db
->db_blkid
>=
1822 dn
->dn_unlisted_l0_blkid
)
1823 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
1824 db
->db_state
= DB_UNCACHED
;
1825 mutex_exit(&dn
->dn_dbufs_mtx
);
1826 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1828 if (parent
&& parent
!= dn
->dn_dbuf
)
1829 dbuf_add_ref(parent
, db
);
1831 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1832 refcount_count(&dn
->dn_holds
) > 0);
1833 (void) refcount_add(&dn
->dn_holds
, db
);
1834 (void) atomic_inc_32_nv(&dn
->dn_dbufs_count
);
1836 dprintf_dbuf(db
, "db=%p\n", db
);
1842 dbuf_do_evict(void *private)
1844 dmu_buf_impl_t
*db
= private;
1846 if (!MUTEX_HELD(&db
->db_mtx
))
1847 mutex_enter(&db
->db_mtx
);
1849 ASSERT(refcount_is_zero(&db
->db_holds
));
1851 if (db
->db_state
!= DB_EVICTING
) {
1852 ASSERT(db
->db_state
== DB_CACHED
);
1857 mutex_exit(&db
->db_mtx
);
1864 dbuf_destroy(dmu_buf_impl_t
*db
)
1866 ASSERT(refcount_is_zero(&db
->db_holds
));
1868 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1870 * If this dbuf is still on the dn_dbufs list,
1871 * remove it from that list.
1873 if (db
->db_dnode_handle
!= NULL
) {
1878 mutex_enter(&dn
->dn_dbufs_mtx
);
1879 list_remove(&dn
->dn_dbufs
, db
);
1880 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1881 mutex_exit(&dn
->dn_dbufs_mtx
);
1884 * Decrementing the dbuf count means that the hold
1885 * corresponding to the removed dbuf is no longer
1886 * discounted in dnode_move(), so the dnode cannot be
1887 * moved until after we release the hold.
1890 db
->db_dnode_handle
= NULL
;
1892 dbuf_hash_remove(db
);
1894 db
->db_parent
= NULL
;
1897 ASSERT(!list_link_active(&db
->db_link
));
1898 ASSERT(db
->db
.db_data
== NULL
);
1899 ASSERT(db
->db_hash_next
== NULL
);
1900 ASSERT(db
->db_blkptr
== NULL
);
1901 ASSERT(db
->db_data_pending
== NULL
);
1903 kmem_cache_free(dbuf_cache
, db
);
1904 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1908 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
, zio_priority_t prio
)
1910 dmu_buf_impl_t
*db
= NULL
;
1911 blkptr_t
*bp
= NULL
;
1913 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1914 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1916 if (dnode_block_freed(dn
, blkid
))
1919 /* dbuf_find() returns with db_mtx held */
1920 if ((db
= dbuf_find(dn
, 0, blkid
))) {
1922 * This dbuf is already in the cache. We assume that
1923 * it is already CACHED, or else about to be either
1926 mutex_exit(&db
->db_mtx
);
1930 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
, NULL
) == 0) {
1931 if (bp
&& !BP_IS_HOLE(bp
) && !BP_IS_EMBEDDED(bp
)) {
1932 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1933 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1934 zbookmark_phys_t zb
;
1936 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1937 dn
->dn_object
, 0, blkid
);
1939 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1940 bp
, NULL
, NULL
, prio
,
1941 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1945 dbuf_rele(db
, NULL
);
1949 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1952 * Returns with db_holds incremented, and db_mtx not held.
1953 * Note: dn_struct_rwlock must be held.
1956 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
1958 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
1959 dh
->dh_parent
= NULL
;
1961 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
1962 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
1963 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
1965 *(dh
->dh_dbp
) = NULL
;
1967 /* dbuf_find() returns with db_mtx held */
1968 dh
->dh_db
= dbuf_find(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
);
1970 if (dh
->dh_db
== NULL
) {
1973 ASSERT3P(dh
->dh_parent
, ==, NULL
);
1974 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1975 dh
->dh_fail_sparse
, &dh
->dh_parent
,
1977 if (dh
->dh_fail_sparse
) {
1978 if (dh
->dh_err
== 0 &&
1979 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
1980 dh
->dh_err
= SET_ERROR(ENOENT
);
1983 dbuf_rele(dh
->dh_parent
, NULL
);
1984 return (dh
->dh_err
);
1987 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
1988 return (dh
->dh_err
);
1989 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1990 dh
->dh_parent
, dh
->dh_bp
);
1993 if (dh
->dh_db
->db_buf
&& refcount_is_zero(&dh
->dh_db
->db_holds
)) {
1994 arc_buf_add_ref(dh
->dh_db
->db_buf
, dh
->dh_db
);
1995 if (dh
->dh_db
->db_buf
->b_data
== NULL
) {
1996 dbuf_clear(dh
->dh_db
);
1997 if (dh
->dh_parent
) {
1998 dbuf_rele(dh
->dh_parent
, NULL
);
1999 dh
->dh_parent
= NULL
;
2003 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2006 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2009 * If this buffer is currently syncing out, and we are are
2010 * still referencing it from db_data, we need to make a copy
2011 * of it in case we decide we want to dirty it again in this txg.
2013 if (dh
->dh_db
->db_level
== 0 &&
2014 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2015 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2016 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2017 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2019 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2020 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2022 dbuf_set_data(dh
->dh_db
,
2023 arc_buf_alloc(dh
->dh_dn
->dn_objset
->os_spa
,
2024 dh
->dh_db
->db
.db_size
, dh
->dh_db
, dh
->dh_type
));
2025 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2026 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2030 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2031 dbuf_update_data(dh
->dh_db
);
2032 DBUF_VERIFY(dh
->dh_db
);
2033 mutex_exit(&dh
->dh_db
->db_mtx
);
2035 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2037 dbuf_rele(dh
->dh_parent
, NULL
);
2039 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2040 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2041 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2042 *(dh
->dh_dbp
) = dh
->dh_db
;
2048 * The following code preserves the recursive function dbuf_hold_impl()
2049 * but moves the local variables AND function arguments to the heap to
2050 * minimize the stack frame size. Enough space is initially allocated
2051 * on the stack for 20 levels of recursion.
2054 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2055 void *tag
, dmu_buf_impl_t
**dbp
)
2057 struct dbuf_hold_impl_data
*dh
;
2060 dh
= kmem_zalloc(sizeof (struct dbuf_hold_impl_data
) *
2061 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_PUSHPAGE
);
2062 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
, tag
, dbp
, 0);
2064 error
= __dbuf_hold_impl(dh
);
2066 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2067 DBUF_HOLD_IMPL_MAX_DEPTH
);
2073 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2074 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2075 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2078 dh
->dh_level
= level
;
2079 dh
->dh_blkid
= blkid
;
2080 dh
->dh_fail_sparse
= fail_sparse
;
2083 dh
->dh_depth
= depth
;
2087 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2090 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
2091 return (err
? NULL
: db
);
2095 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2098 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
2099 return (err
? NULL
: db
);
2103 dbuf_create_bonus(dnode_t
*dn
)
2105 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2107 ASSERT(dn
->dn_bonus
== NULL
);
2108 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2112 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2114 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2117 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2118 return (SET_ERROR(ENOTSUP
));
2120 blksz
= SPA_MINBLOCKSIZE
;
2121 if (blksz
> SPA_MAXBLOCKSIZE
)
2122 blksz
= SPA_MAXBLOCKSIZE
;
2124 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2128 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2129 dbuf_new_size(db
, blksz
, tx
);
2130 rw_exit(&dn
->dn_struct_rwlock
);
2137 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2139 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2142 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2144 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2146 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2150 * If you call dbuf_rele() you had better not be referencing the dnode handle
2151 * unless you have some other direct or indirect hold on the dnode. (An indirect
2152 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2153 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2154 * dnode's parent dbuf evicting its dnode handles.
2157 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2159 mutex_enter(&db
->db_mtx
);
2160 dbuf_rele_and_unlock(db
, tag
);
2164 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2166 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2170 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2171 * db_dirtycnt and db_holds to be updated atomically.
2174 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2178 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2182 * Remove the reference to the dbuf before removing its hold on the
2183 * dnode so we can guarantee in dnode_move() that a referenced bonus
2184 * buffer has a corresponding dnode hold.
2186 holds
= refcount_remove(&db
->db_holds
, tag
);
2190 * We can't freeze indirects if there is a possibility that they
2191 * may be modified in the current syncing context.
2193 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2194 arc_buf_freeze(db
->db_buf
);
2196 if (holds
== db
->db_dirtycnt
&&
2197 db
->db_level
== 0 && db
->db_immediate_evict
)
2198 dbuf_evict_user(db
);
2201 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2202 mutex_exit(&db
->db_mtx
);
2205 * If the dnode moves here, we cannot cross this barrier
2206 * until the move completes.
2209 (void) atomic_dec_32_nv(&DB_DNODE(db
)->dn_dbufs_count
);
2212 * The bonus buffer's dnode hold is no longer discounted
2213 * in dnode_move(). The dnode cannot move until after
2216 dnode_rele(DB_DNODE(db
), db
);
2217 } else if (db
->db_buf
== NULL
) {
2219 * This is a special case: we never associated this
2220 * dbuf with any data allocated from the ARC.
2222 ASSERT(db
->db_state
== DB_UNCACHED
||
2223 db
->db_state
== DB_NOFILL
);
2225 } else if (arc_released(db
->db_buf
)) {
2226 arc_buf_t
*buf
= db
->db_buf
;
2228 * This dbuf has anonymous data associated with it.
2230 dbuf_set_data(db
, NULL
);
2231 VERIFY(arc_buf_remove_ref(buf
, db
));
2234 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2237 * A dbuf will be eligible for eviction if either the
2238 * 'primarycache' property is set or a duplicate
2239 * copy of this buffer is already cached in the arc.
2241 * In the case of the 'primarycache' a buffer
2242 * is considered for eviction if it matches the
2243 * criteria set in the property.
2245 * To decide if our buffer is considered a
2246 * duplicate, we must call into the arc to determine
2247 * if multiple buffers are referencing the same
2248 * block on-disk. If so, then we simply evict
2251 if (!DBUF_IS_CACHEABLE(db
)) {
2252 if (db
->db_blkptr
!= NULL
&&
2253 !BP_IS_HOLE(db
->db_blkptr
) &&
2254 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2256 dmu_objset_spa(db
->db_objset
);
2257 blkptr_t bp
= *db
->db_blkptr
;
2259 arc_freed(spa
, &bp
);
2263 } else if (arc_buf_eviction_needed(db
->db_buf
)) {
2266 mutex_exit(&db
->db_mtx
);
2270 mutex_exit(&db
->db_mtx
);
2274 #pragma weak dmu_buf_refcount = dbuf_refcount
2276 dbuf_refcount(dmu_buf_impl_t
*db
)
2278 return (refcount_count(&db
->db_holds
));
2282 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2283 dmu_buf_evict_func_t
*evict_func
)
2285 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2286 user_data_ptr_ptr
, evict_func
));
2290 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2291 dmu_buf_evict_func_t
*evict_func
)
2293 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2295 db
->db_immediate_evict
= TRUE
;
2296 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2297 user_data_ptr_ptr
, evict_func
));
2301 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
2302 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2304 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2305 ASSERT(db
->db_level
== 0);
2307 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2309 mutex_enter(&db
->db_mtx
);
2311 if (db
->db_user_ptr
== old_user_ptr
) {
2312 db
->db_user_ptr
= user_ptr
;
2313 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2314 db
->db_evict_func
= evict_func
;
2316 dbuf_update_data(db
);
2318 old_user_ptr
= db
->db_user_ptr
;
2321 mutex_exit(&db
->db_mtx
);
2322 return (old_user_ptr
);
2326 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2328 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2329 ASSERT(!refcount_is_zero(&db
->db_holds
));
2331 return (db
->db_user_ptr
);
2335 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2337 boolean_t res
= B_FALSE
;
2338 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2341 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2342 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2348 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2350 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2351 return (dbi
->db_blkptr
);
2355 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2357 /* ASSERT(dmu_tx_is_syncing(tx) */
2358 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2360 if (db
->db_blkptr
!= NULL
)
2363 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2364 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2365 BP_ZERO(db
->db_blkptr
);
2368 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2370 * This buffer was allocated at a time when there was
2371 * no available blkptrs from the dnode, or it was
2372 * inappropriate to hook it in (i.e., nlevels mis-match).
2374 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2375 ASSERT(db
->db_parent
== NULL
);
2376 db
->db_parent
= dn
->dn_dbuf
;
2377 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2380 dmu_buf_impl_t
*parent
= db
->db_parent
;
2381 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2383 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2384 if (parent
== NULL
) {
2385 mutex_exit(&db
->db_mtx
);
2386 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2387 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2388 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2389 rw_exit(&dn
->dn_struct_rwlock
);
2390 mutex_enter(&db
->db_mtx
);
2391 db
->db_parent
= parent
;
2393 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2394 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2400 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2401 * is critical the we not allow the compiler to inline this function in to
2402 * dbuf_sync_list() thereby drastically bloating the stack usage.
2404 noinline
static void
2405 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2407 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2411 ASSERT(dmu_tx_is_syncing(tx
));
2413 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2415 mutex_enter(&db
->db_mtx
);
2417 ASSERT(db
->db_level
> 0);
2420 /* Read the block if it hasn't been read yet. */
2421 if (db
->db_buf
== NULL
) {
2422 mutex_exit(&db
->db_mtx
);
2423 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2424 mutex_enter(&db
->db_mtx
);
2426 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2427 ASSERT(db
->db_buf
!= NULL
);
2431 /* Indirect block size must match what the dnode thinks it is. */
2432 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2433 dbuf_check_blkptr(dn
, db
);
2436 /* Provide the pending dirty record to child dbufs */
2437 db
->db_data_pending
= dr
;
2439 mutex_exit(&db
->db_mtx
);
2440 dbuf_write(dr
, db
->db_buf
, tx
);
2443 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2444 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2445 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2446 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2451 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2452 * critical the we not allow the compiler to inline this function in to
2453 * dbuf_sync_list() thereby drastically bloating the stack usage.
2455 noinline
static void
2456 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2458 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2459 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2462 uint64_t txg
= tx
->tx_txg
;
2464 ASSERT(dmu_tx_is_syncing(tx
));
2466 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2468 mutex_enter(&db
->db_mtx
);
2470 * To be synced, we must be dirtied. But we
2471 * might have been freed after the dirty.
2473 if (db
->db_state
== DB_UNCACHED
) {
2474 /* This buffer has been freed since it was dirtied */
2475 ASSERT(db
->db
.db_data
== NULL
);
2476 } else if (db
->db_state
== DB_FILL
) {
2477 /* This buffer was freed and is now being re-filled */
2478 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2480 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2487 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2488 mutex_enter(&dn
->dn_mtx
);
2489 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2490 mutex_exit(&dn
->dn_mtx
);
2494 * If this is a bonus buffer, simply copy the bonus data into the
2495 * dnode. It will be written out when the dnode is synced (and it
2496 * will be synced, since it must have been dirty for dbuf_sync to
2499 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2500 dbuf_dirty_record_t
**drp
;
2502 ASSERT(*datap
!= NULL
);
2503 ASSERT0(db
->db_level
);
2504 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2505 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2508 if (*datap
!= db
->db
.db_data
) {
2509 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2510 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2512 db
->db_data_pending
= NULL
;
2513 drp
= &db
->db_last_dirty
;
2515 drp
= &(*drp
)->dr_next
;
2516 ASSERT(dr
->dr_next
== NULL
);
2517 ASSERT(dr
->dr_dbuf
== db
);
2519 if (dr
->dr_dbuf
->db_level
!= 0) {
2520 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2521 list_destroy(&dr
->dt
.di
.dr_children
);
2523 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2524 ASSERT(db
->db_dirtycnt
> 0);
2525 db
->db_dirtycnt
-= 1;
2526 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2533 * This function may have dropped the db_mtx lock allowing a dmu_sync
2534 * operation to sneak in. As a result, we need to ensure that we
2535 * don't check the dr_override_state until we have returned from
2536 * dbuf_check_blkptr.
2538 dbuf_check_blkptr(dn
, db
);
2541 * If this buffer is in the middle of an immediate write,
2542 * wait for the synchronous IO to complete.
2544 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2545 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2546 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2547 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2550 if (db
->db_state
!= DB_NOFILL
&&
2551 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2552 refcount_count(&db
->db_holds
) > 1 &&
2553 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2554 *datap
== db
->db_buf
) {
2556 * If this buffer is currently "in use" (i.e., there
2557 * are active holds and db_data still references it),
2558 * then make a copy before we start the write so that
2559 * any modifications from the open txg will not leak
2562 * NOTE: this copy does not need to be made for
2563 * objects only modified in the syncing context (e.g.
2564 * DNONE_DNODE blocks).
2566 int blksz
= arc_buf_size(*datap
);
2567 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2568 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2569 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2571 db
->db_data_pending
= dr
;
2573 mutex_exit(&db
->db_mtx
);
2575 dbuf_write(dr
, *datap
, tx
);
2577 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2578 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2579 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2583 * Although zio_nowait() does not "wait for an IO", it does
2584 * initiate the IO. If this is an empty write it seems plausible
2585 * that the IO could actually be completed before the nowait
2586 * returns. We need to DB_DNODE_EXIT() first in case
2587 * zio_nowait() invalidates the dbuf.
2590 zio_nowait(dr
->dr_zio
);
2595 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2597 dbuf_dirty_record_t
*dr
;
2599 while ((dr
= list_head(list
))) {
2600 if (dr
->dr_zio
!= NULL
) {
2602 * If we find an already initialized zio then we
2603 * are processing the meta-dnode, and we have finished.
2604 * The dbufs for all dnodes are put back on the list
2605 * during processing, so that we can zio_wait()
2606 * these IOs after initiating all child IOs.
2608 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2609 DMU_META_DNODE_OBJECT
);
2612 list_remove(list
, dr
);
2613 if (dr
->dr_dbuf
->db_level
> 0)
2614 dbuf_sync_indirect(dr
, tx
);
2616 dbuf_sync_leaf(dr
, tx
);
2622 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2624 dmu_buf_impl_t
*db
= vdb
;
2626 blkptr_t
*bp
= zio
->io_bp
;
2627 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2628 spa_t
*spa
= zio
->io_spa
;
2633 ASSERT3P(db
->db_blkptr
, ==, bp
);
2637 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2638 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2639 zio
->io_prev_space_delta
= delta
;
2641 if (bp
->blk_birth
!= 0) {
2642 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2643 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2644 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2645 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
2646 BP_IS_EMBEDDED(bp
));
2647 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2650 mutex_enter(&db
->db_mtx
);
2653 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2654 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2655 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2656 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2660 if (db
->db_level
== 0) {
2661 mutex_enter(&dn
->dn_mtx
);
2662 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2663 db
->db_blkid
!= DMU_SPILL_BLKID
)
2664 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2665 mutex_exit(&dn
->dn_mtx
);
2667 if (dn
->dn_type
== DMU_OT_DNODE
) {
2668 dnode_phys_t
*dnp
= db
->db
.db_data
;
2669 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2671 if (dnp
->dn_type
!= DMU_OT_NONE
)
2675 if (BP_IS_HOLE(bp
)) {
2682 blkptr_t
*ibp
= db
->db
.db_data
;
2683 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2684 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2685 if (BP_IS_HOLE(ibp
))
2687 fill
+= BP_GET_FILL(ibp
);
2692 if (!BP_IS_EMBEDDED(bp
))
2693 bp
->blk_fill
= fill
;
2695 mutex_exit(&db
->db_mtx
);
2699 * The SPA will call this callback several times for each zio - once
2700 * for every physical child i/o (zio->io_phys_children times). This
2701 * allows the DMU to monitor the progress of each logical i/o. For example,
2702 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2703 * block. There may be a long delay before all copies/fragments are completed,
2704 * so this callback allows us to retire dirty space gradually, as the physical
2709 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2711 dmu_buf_impl_t
*db
= arg
;
2712 objset_t
*os
= db
->db_objset
;
2713 dsl_pool_t
*dp
= dmu_objset_pool(os
);
2714 dbuf_dirty_record_t
*dr
;
2717 dr
= db
->db_data_pending
;
2718 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
2721 * The callback will be called io_phys_children times. Retire one
2722 * portion of our dirty space each time we are called. Any rounding
2723 * error will be cleaned up by dsl_pool_sync()'s call to
2724 * dsl_pool_undirty_space().
2726 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
2727 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
2732 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2734 dmu_buf_impl_t
*db
= vdb
;
2735 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2736 blkptr_t
*bp
= db
->db_blkptr
;
2737 objset_t
*os
= db
->db_objset
;
2738 dmu_tx_t
*tx
= os
->os_synctx
;
2739 dbuf_dirty_record_t
**drp
, *dr
;
2741 ASSERT0(zio
->io_error
);
2742 ASSERT(db
->db_blkptr
== bp
);
2745 * For nopwrites and rewrites we ensure that the bp matches our
2746 * original and bypass all the accounting.
2748 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
2749 ASSERT(BP_EQUAL(bp
, bp_orig
));
2751 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2752 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2753 dsl_dataset_block_born(ds
, bp
, tx
);
2756 mutex_enter(&db
->db_mtx
);
2760 drp
= &db
->db_last_dirty
;
2761 while ((dr
= *drp
) != db
->db_data_pending
)
2763 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2764 ASSERT(dr
->dr_dbuf
== db
);
2765 ASSERT(dr
->dr_next
== NULL
);
2769 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2774 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2775 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2776 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2781 if (db
->db_level
== 0) {
2782 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2783 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2784 if (db
->db_state
!= DB_NOFILL
) {
2785 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2786 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2788 else if (!arc_released(db
->db_buf
))
2789 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2796 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2797 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
2798 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2799 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
2801 ASSERT3U(db
->db_blkid
, <=,
2802 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
2803 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2805 if (!arc_released(db
->db_buf
))
2806 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2809 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2810 list_destroy(&dr
->dt
.di
.dr_children
);
2812 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2814 cv_broadcast(&db
->db_changed
);
2815 ASSERT(db
->db_dirtycnt
> 0);
2816 db
->db_dirtycnt
-= 1;
2817 db
->db_data_pending
= NULL
;
2818 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
2822 dbuf_write_nofill_ready(zio_t
*zio
)
2824 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2828 dbuf_write_nofill_done(zio_t
*zio
)
2830 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2834 dbuf_write_override_ready(zio_t
*zio
)
2836 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2837 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2839 dbuf_write_ready(zio
, NULL
, db
);
2843 dbuf_write_override_done(zio_t
*zio
)
2845 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2846 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2847 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2849 mutex_enter(&db
->db_mtx
);
2850 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2851 if (!BP_IS_HOLE(obp
))
2852 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2853 arc_release(dr
->dt
.dl
.dr_data
, db
);
2855 mutex_exit(&db
->db_mtx
);
2857 dbuf_write_done(zio
, NULL
, db
);
2860 /* Issue I/O to commit a dirty buffer to disk. */
2862 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2864 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2867 dmu_buf_impl_t
*parent
= db
->db_parent
;
2868 uint64_t txg
= tx
->tx_txg
;
2869 zbookmark_phys_t zb
;
2878 if (db
->db_state
!= DB_NOFILL
) {
2879 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2881 * Private object buffers are released here rather
2882 * than in dbuf_dirty() since they are only modified
2883 * in the syncing context and we don't want the
2884 * overhead of making multiple copies of the data.
2886 if (BP_IS_HOLE(db
->db_blkptr
)) {
2889 dbuf_release_bp(db
);
2894 if (parent
!= dn
->dn_dbuf
) {
2895 /* Our parent is an indirect block. */
2896 /* We have a dirty parent that has been scheduled for write. */
2897 ASSERT(parent
&& parent
->db_data_pending
);
2898 /* Our parent's buffer is one level closer to the dnode. */
2899 ASSERT(db
->db_level
== parent
->db_level
-1);
2901 * We're about to modify our parent's db_data by modifying
2902 * our block pointer, so the parent must be released.
2904 ASSERT(arc_released(parent
->db_buf
));
2905 zio
= parent
->db_data_pending
->dr_zio
;
2907 /* Our parent is the dnode itself. */
2908 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2909 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2910 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2911 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2912 ASSERT3P(db
->db_blkptr
, ==,
2913 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2917 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2918 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2921 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2922 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2923 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2925 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2927 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2929 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2932 if (db
->db_level
== 0 &&
2933 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2935 * The BP for this block has been provided by open context
2936 * (by dmu_sync() or dmu_buf_write_embedded()).
2938 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
2940 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2941 db
->db_blkptr
, contents
, db
->db
.db_size
, &zp
,
2942 dbuf_write_override_ready
, NULL
, dbuf_write_override_done
,
2943 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2944 mutex_enter(&db
->db_mtx
);
2945 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2946 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2947 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
2948 mutex_exit(&db
->db_mtx
);
2949 } else if (db
->db_state
== DB_NOFILL
) {
2950 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2951 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2952 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2953 dbuf_write_nofill_ready
, NULL
, dbuf_write_nofill_done
, db
,
2954 ZIO_PRIORITY_ASYNC_WRITE
,
2955 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2957 ASSERT(arc_released(data
));
2958 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2959 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
),
2960 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
2961 dbuf_write_physdone
, dbuf_write_done
, db
,
2962 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2966 #if defined(_KERNEL) && defined(HAVE_SPL)
2967 EXPORT_SYMBOL(dbuf_find
);
2968 EXPORT_SYMBOL(dbuf_is_metadata
);
2969 EXPORT_SYMBOL(dbuf_evict
);
2970 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
2971 EXPORT_SYMBOL(dbuf_whichblock
);
2972 EXPORT_SYMBOL(dbuf_read
);
2973 EXPORT_SYMBOL(dbuf_unoverride
);
2974 EXPORT_SYMBOL(dbuf_free_range
);
2975 EXPORT_SYMBOL(dbuf_new_size
);
2976 EXPORT_SYMBOL(dbuf_release_bp
);
2977 EXPORT_SYMBOL(dbuf_dirty
);
2978 EXPORT_SYMBOL(dmu_buf_will_dirty
);
2979 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
2980 EXPORT_SYMBOL(dmu_buf_will_fill
);
2981 EXPORT_SYMBOL(dmu_buf_fill_done
);
2982 EXPORT_SYMBOL(dmu_buf_rele
);
2983 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
2984 EXPORT_SYMBOL(dbuf_clear
);
2985 EXPORT_SYMBOL(dbuf_prefetch
);
2986 EXPORT_SYMBOL(dbuf_hold_impl
);
2987 EXPORT_SYMBOL(dbuf_hold
);
2988 EXPORT_SYMBOL(dbuf_hold_level
);
2989 EXPORT_SYMBOL(dbuf_create_bonus
);
2990 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
2991 EXPORT_SYMBOL(dbuf_rm_spill
);
2992 EXPORT_SYMBOL(dbuf_add_ref
);
2993 EXPORT_SYMBOL(dbuf_rele
);
2994 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
2995 EXPORT_SYMBOL(dbuf_refcount
);
2996 EXPORT_SYMBOL(dbuf_sync_list
);
2997 EXPORT_SYMBOL(dmu_buf_set_user
);
2998 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
2999 EXPORT_SYMBOL(dmu_buf_update_user
);
3000 EXPORT_SYMBOL(dmu_buf_get_user
);
3001 EXPORT_SYMBOL(dmu_buf_freeable
);
3002 EXPORT_SYMBOL(dmu_buf_get_blkptr
);