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>
46 #include <sys/trace_dbuf.h>
48 struct dbuf_hold_impl_data
{
49 /* Function arguments */
55 dmu_buf_impl_t
**dh_dbp
;
57 dmu_buf_impl_t
*dh_db
;
58 dmu_buf_impl_t
*dh_parent
;
61 dbuf_dirty_record_t
*dh_dr
;
62 arc_buf_contents_t dh_type
;
66 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
67 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
68 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
69 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
72 * Number of times that zfs_free_range() took the slow path while doing
73 * a zfs receive. A nonzero value indicates a potential performance problem.
75 uint64_t zfs_free_range_recv_miss
;
77 static void dbuf_destroy(dmu_buf_impl_t
*db
);
78 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
79 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
82 * Global data structures and functions for the dbuf cache.
84 static kmem_cache_t
*dbuf_cache
;
88 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
90 dmu_buf_impl_t
*db
= vdb
;
91 bzero(db
, sizeof (dmu_buf_impl_t
));
93 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
94 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
95 refcount_create(&db
->db_holds
);
96 list_link_init(&db
->db_link
);
102 dbuf_dest(void *vdb
, void *unused
)
104 dmu_buf_impl_t
*db
= vdb
;
105 mutex_destroy(&db
->db_mtx
);
106 cv_destroy(&db
->db_changed
);
107 refcount_destroy(&db
->db_holds
);
111 * dbuf hash table routines
113 static dbuf_hash_table_t dbuf_hash_table
;
115 static uint64_t dbuf_hash_count
;
118 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
120 uintptr_t osv
= (uintptr_t)os
;
121 uint64_t crc
= -1ULL;
123 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
124 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
125 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
126 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
127 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
128 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
129 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
131 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
136 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
138 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
139 ((dbuf)->db.db_object == (obj) && \
140 (dbuf)->db_objset == (os) && \
141 (dbuf)->db_level == (level) && \
142 (dbuf)->db_blkid == (blkid))
145 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
147 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
148 objset_t
*os
= dn
->dn_objset
;
155 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
156 idx
= hv
& h
->hash_table_mask
;
158 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
159 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
160 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
161 mutex_enter(&db
->db_mtx
);
162 if (db
->db_state
!= DB_EVICTING
) {
163 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
166 mutex_exit(&db
->db_mtx
);
169 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
174 * Insert an entry into the hash table. If there is already an element
175 * equal to elem in the hash table, then the already existing element
176 * will be returned and the new element will not be inserted.
177 * Otherwise returns NULL.
179 static dmu_buf_impl_t
*
180 dbuf_hash_insert(dmu_buf_impl_t
*db
)
182 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
183 objset_t
*os
= db
->db_objset
;
184 uint64_t obj
= db
->db
.db_object
;
185 int level
= db
->db_level
;
186 uint64_t blkid
, hv
, idx
;
189 blkid
= db
->db_blkid
;
190 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
191 idx
= hv
& h
->hash_table_mask
;
193 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
194 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
195 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
196 mutex_enter(&dbf
->db_mtx
);
197 if (dbf
->db_state
!= DB_EVICTING
) {
198 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
201 mutex_exit(&dbf
->db_mtx
);
205 mutex_enter(&db
->db_mtx
);
206 db
->db_hash_next
= h
->hash_table
[idx
];
207 h
->hash_table
[idx
] = db
;
208 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
209 atomic_add_64(&dbuf_hash_count
, 1);
215 * Remove an entry from the hash table. It must be in the EVICTING state.
218 dbuf_hash_remove(dmu_buf_impl_t
*db
)
220 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
222 dmu_buf_impl_t
*dbf
, **dbp
;
224 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
225 db
->db_level
, db
->db_blkid
);
226 idx
= hv
& h
->hash_table_mask
;
229 * We musn't hold db_mtx to maintain lock ordering:
230 * DBUF_HASH_MUTEX > db_mtx.
232 ASSERT(refcount_is_zero(&db
->db_holds
));
233 ASSERT(db
->db_state
== DB_EVICTING
);
234 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
236 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
237 dbp
= &h
->hash_table
[idx
];
238 while ((dbf
= *dbp
) != db
) {
239 dbp
= &dbf
->db_hash_next
;
242 *dbp
= db
->db_hash_next
;
243 db
->db_hash_next
= NULL
;
244 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
245 atomic_add_64(&dbuf_hash_count
, -1);
248 static arc_evict_func_t dbuf_do_evict
;
251 dbuf_evict_user(dmu_buf_impl_t
*db
)
253 ASSERT(MUTEX_HELD(&db
->db_mtx
));
255 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
258 if (db
->db_user_data_ptr_ptr
)
259 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
260 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
261 db
->db_user_ptr
= NULL
;
262 db
->db_user_data_ptr_ptr
= NULL
;
263 db
->db_evict_func
= NULL
;
267 dbuf_is_metadata(dmu_buf_impl_t
*db
)
270 * Consider indirect blocks and spill blocks to be meta data.
272 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
275 boolean_t is_metadata
;
278 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
281 return (is_metadata
);
286 dbuf_evict(dmu_buf_impl_t
*db
)
288 ASSERT(MUTEX_HELD(&db
->db_mtx
));
289 ASSERT(db
->db_buf
== NULL
);
290 ASSERT(db
->db_data_pending
== NULL
);
299 uint64_t hsize
= 1ULL << 16;
300 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
304 * The hash table is big enough to fill all of physical memory
305 * with an average 4K block size. The table will take up
306 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
308 while (hsize
* 4096 < physmem
* PAGESIZE
)
312 h
->hash_table_mask
= hsize
- 1;
313 #if defined(_KERNEL) && defined(HAVE_SPL)
315 * Large allocations which do not require contiguous pages
316 * should be using vmem_alloc() in the linux kernel
318 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
320 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
322 if (h
->hash_table
== NULL
) {
323 /* XXX - we should really return an error instead of assert */
324 ASSERT(hsize
> (1ULL << 10));
329 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
330 sizeof (dmu_buf_impl_t
),
331 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
333 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
334 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
342 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
345 dbuf_stats_destroy();
347 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
348 mutex_destroy(&h
->hash_mutexes
[i
]);
349 #if defined(_KERNEL) && defined(HAVE_SPL)
351 * Large allocations which do not require contiguous pages
352 * should be using vmem_free() in the linux kernel
354 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
356 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
358 kmem_cache_destroy(dbuf_cache
);
367 dbuf_verify(dmu_buf_impl_t
*db
)
370 dbuf_dirty_record_t
*dr
;
372 ASSERT(MUTEX_HELD(&db
->db_mtx
));
374 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
377 ASSERT(db
->db_objset
!= NULL
);
381 ASSERT(db
->db_parent
== NULL
);
382 ASSERT(db
->db_blkptr
== NULL
);
384 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
385 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
386 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
387 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
388 db
->db_blkid
== DMU_SPILL_BLKID
||
389 !list_is_empty(&dn
->dn_dbufs
));
391 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
393 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
394 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
395 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
397 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
398 ASSERT0(db
->db
.db_offset
);
400 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
403 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
404 ASSERT(dr
->dr_dbuf
== db
);
406 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
407 ASSERT(dr
->dr_dbuf
== db
);
410 * We can't assert that db_size matches dn_datablksz because it
411 * can be momentarily different when another thread is doing
414 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
415 dr
= db
->db_data_pending
;
417 * It should only be modified in syncing context, so
418 * make sure we only have one copy of the data.
420 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
423 /* verify db->db_blkptr */
425 if (db
->db_parent
== dn
->dn_dbuf
) {
426 /* db is pointed to by the dnode */
427 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
428 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
429 ASSERT(db
->db_parent
== NULL
);
431 ASSERT(db
->db_parent
!= NULL
);
432 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
433 ASSERT3P(db
->db_blkptr
, ==,
434 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
436 /* db is pointed to by an indirect block */
437 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
439 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
440 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
443 * dnode_grow_indblksz() can make this fail if we don't
444 * have the struct_rwlock. XXX indblksz no longer
445 * grows. safe to do this now?
447 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
448 ASSERT3P(db
->db_blkptr
, ==,
449 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
450 db
->db_blkid
% epb
));
454 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
455 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
456 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
457 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
459 * If the blkptr isn't set but they have nonzero data,
460 * it had better be dirty, otherwise we'll lose that
461 * data when we evict this buffer.
463 if (db
->db_dirtycnt
== 0) {
464 ASSERTV(uint64_t *buf
= db
->db
.db_data
);
467 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
477 dbuf_update_data(dmu_buf_impl_t
*db
)
479 ASSERT(MUTEX_HELD(&db
->db_mtx
));
480 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
481 ASSERT(!refcount_is_zero(&db
->db_holds
));
482 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
487 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
489 ASSERT(MUTEX_HELD(&db
->db_mtx
));
492 ASSERT(buf
->b_data
!= NULL
);
493 db
->db
.db_data
= buf
->b_data
;
494 if (!arc_released(buf
))
495 arc_set_callback(buf
, dbuf_do_evict
, db
);
496 dbuf_update_data(db
);
499 db
->db
.db_data
= NULL
;
500 if (db
->db_state
!= DB_NOFILL
)
501 db
->db_state
= DB_UNCACHED
;
506 * Loan out an arc_buf for read. Return the loaned arc_buf.
509 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
513 mutex_enter(&db
->db_mtx
);
514 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
515 int blksz
= db
->db
.db_size
;
516 spa_t
*spa
= db
->db_objset
->os_spa
;
518 mutex_exit(&db
->db_mtx
);
519 abuf
= arc_loan_buf(spa
, blksz
);
520 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
523 arc_loan_inuse_buf(abuf
, db
);
524 dbuf_set_data(db
, NULL
);
525 mutex_exit(&db
->db_mtx
);
531 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
533 if (dn
->dn_datablkshift
) {
534 return (offset
>> dn
->dn_datablkshift
);
536 ASSERT3U(offset
, <, dn
->dn_datablksz
);
542 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
544 dmu_buf_impl_t
*db
= vdb
;
546 mutex_enter(&db
->db_mtx
);
547 ASSERT3U(db
->db_state
, ==, DB_READ
);
549 * All reads are synchronous, so we must have a hold on the dbuf
551 ASSERT(refcount_count(&db
->db_holds
) > 0);
552 ASSERT(db
->db_buf
== NULL
);
553 ASSERT(db
->db
.db_data
== NULL
);
554 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
555 /* we were freed in flight; disregard any error */
556 arc_release(buf
, db
);
557 bzero(buf
->b_data
, db
->db
.db_size
);
559 db
->db_freed_in_flight
= FALSE
;
560 dbuf_set_data(db
, buf
);
561 db
->db_state
= DB_CACHED
;
562 } else if (zio
== NULL
|| zio
->io_error
== 0) {
563 dbuf_set_data(db
, buf
);
564 db
->db_state
= DB_CACHED
;
566 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
567 ASSERT3P(db
->db_buf
, ==, NULL
);
568 VERIFY(arc_buf_remove_ref(buf
, db
));
569 db
->db_state
= DB_UNCACHED
;
571 cv_broadcast(&db
->db_changed
);
572 dbuf_rele_and_unlock(db
, NULL
);
576 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
580 uint32_t aflags
= ARC_NOWAIT
;
585 ASSERT(!refcount_is_zero(&db
->db_holds
));
586 /* We need the struct_rwlock to prevent db_blkptr from changing. */
587 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
588 ASSERT(MUTEX_HELD(&db
->db_mtx
));
589 ASSERT(db
->db_state
== DB_UNCACHED
);
590 ASSERT(db
->db_buf
== NULL
);
592 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
593 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
595 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
596 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
597 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
598 if (bonuslen
< DN_MAX_BONUSLEN
)
599 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
601 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
603 dbuf_update_data(db
);
604 db
->db_state
= DB_CACHED
;
605 mutex_exit(&db
->db_mtx
);
610 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
611 * processes the delete record and clears the bp while we are waiting
612 * for the dn_mtx (resulting in a "no" from block_freed).
614 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
615 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
616 BP_IS_HOLE(db
->db_blkptr
)))) {
617 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
620 dbuf_set_data(db
, arc_buf_alloc(db
->db_objset
->os_spa
,
621 db
->db
.db_size
, db
, type
));
622 bzero(db
->db
.db_data
, db
->db
.db_size
);
623 db
->db_state
= DB_CACHED
;
624 *flags
|= DB_RF_CACHED
;
625 mutex_exit(&db
->db_mtx
);
631 db
->db_state
= DB_READ
;
632 mutex_exit(&db
->db_mtx
);
634 if (DBUF_IS_L2CACHEABLE(db
))
635 aflags
|= ARC_L2CACHE
;
636 if (DBUF_IS_L2COMPRESSIBLE(db
))
637 aflags
|= ARC_L2COMPRESS
;
639 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
640 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
641 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
643 dbuf_add_ref(db
, NULL
);
645 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
646 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
647 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
649 if (aflags
& ARC_CACHED
)
650 *flags
|= DB_RF_CACHED
;
652 return (SET_ERROR(err
));
656 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
659 boolean_t havepzio
= (zio
!= NULL
);
664 * We don't have to hold the mutex to check db_state because it
665 * can't be freed while we have a hold on the buffer.
667 ASSERT(!refcount_is_zero(&db
->db_holds
));
669 if (db
->db_state
== DB_NOFILL
)
670 return (SET_ERROR(EIO
));
674 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
675 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
677 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
678 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
679 DBUF_IS_CACHEABLE(db
);
681 mutex_enter(&db
->db_mtx
);
682 if (db
->db_state
== DB_CACHED
) {
683 mutex_exit(&db
->db_mtx
);
685 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
686 db
->db
.db_size
, TRUE
);
687 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
688 rw_exit(&dn
->dn_struct_rwlock
);
690 } else if (db
->db_state
== DB_UNCACHED
) {
691 spa_t
*spa
= dn
->dn_objset
->os_spa
;
694 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
696 err
= dbuf_read_impl(db
, zio
, &flags
);
698 /* dbuf_read_impl has dropped db_mtx for us */
700 if (!err
&& prefetch
)
701 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
702 db
->db
.db_size
, flags
& DB_RF_CACHED
);
704 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
705 rw_exit(&dn
->dn_struct_rwlock
);
708 if (!err
&& !havepzio
)
712 * Another reader came in while the dbuf was in flight
713 * between UNCACHED and CACHED. Either a writer will finish
714 * writing the buffer (sending the dbuf to CACHED) or the
715 * first reader's request will reach the read_done callback
716 * and send the dbuf to CACHED. Otherwise, a failure
717 * occurred and the dbuf went to UNCACHED.
719 mutex_exit(&db
->db_mtx
);
721 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
722 db
->db
.db_size
, TRUE
);
723 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
724 rw_exit(&dn
->dn_struct_rwlock
);
727 /* Skip the wait per the caller's request. */
728 mutex_enter(&db
->db_mtx
);
729 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
730 while (db
->db_state
== DB_READ
||
731 db
->db_state
== DB_FILL
) {
732 ASSERT(db
->db_state
== DB_READ
||
733 (flags
& DB_RF_HAVESTRUCT
) == 0);
734 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
736 cv_wait(&db
->db_changed
, &db
->db_mtx
);
738 if (db
->db_state
== DB_UNCACHED
)
739 err
= SET_ERROR(EIO
);
741 mutex_exit(&db
->db_mtx
);
744 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
749 dbuf_noread(dmu_buf_impl_t
*db
)
751 ASSERT(!refcount_is_zero(&db
->db_holds
));
752 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
753 mutex_enter(&db
->db_mtx
);
754 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
755 cv_wait(&db
->db_changed
, &db
->db_mtx
);
756 if (db
->db_state
== DB_UNCACHED
) {
757 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
758 spa_t
*spa
= db
->db_objset
->os_spa
;
760 ASSERT(db
->db_buf
== NULL
);
761 ASSERT(db
->db
.db_data
== NULL
);
762 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
763 db
->db_state
= DB_FILL
;
764 } else if (db
->db_state
== DB_NOFILL
) {
765 dbuf_set_data(db
, NULL
);
767 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
769 mutex_exit(&db
->db_mtx
);
773 * This is our just-in-time copy function. It makes a copy of
774 * buffers, that have been modified in a previous transaction
775 * group, before we modify them in the current active group.
777 * This function is used in two places: when we are dirtying a
778 * buffer for the first time in a txg, and when we are freeing
779 * a range in a dnode that includes this buffer.
781 * Note that when we are called from dbuf_free_range() we do
782 * not put a hold on the buffer, we just traverse the active
783 * dbuf list for the dnode.
786 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
788 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
790 ASSERT(MUTEX_HELD(&db
->db_mtx
));
791 ASSERT(db
->db
.db_data
!= NULL
);
792 ASSERT(db
->db_level
== 0);
793 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
796 (dr
->dt
.dl
.dr_data
!=
797 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
801 * If the last dirty record for this dbuf has not yet synced
802 * and its referencing the dbuf data, either:
803 * reset the reference to point to a new copy,
804 * or (if there a no active holders)
805 * just null out the current db_data pointer.
807 ASSERT(dr
->dr_txg
>= txg
- 2);
808 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
809 /* Note that the data bufs here are zio_bufs */
810 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
811 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
812 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
813 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
814 int size
= db
->db
.db_size
;
815 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
816 spa_t
*spa
= db
->db_objset
->os_spa
;
818 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
819 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
821 dbuf_set_data(db
, NULL
);
826 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
828 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
829 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
830 uint64_t txg
= dr
->dr_txg
;
832 ASSERT(MUTEX_HELD(&db
->db_mtx
));
833 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
834 ASSERT(db
->db_level
== 0);
836 if (db
->db_blkid
== DMU_BONUS_BLKID
||
837 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
840 ASSERT(db
->db_data_pending
!= dr
);
842 /* free this block */
843 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
844 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
846 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
847 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
850 * Release the already-written buffer, so we leave it in
851 * a consistent dirty state. Note that all callers are
852 * modifying the buffer, so they will immediately do
853 * another (redundant) arc_release(). Therefore, leave
854 * the buf thawed to save the effort of freezing &
855 * immediately re-thawing it.
857 arc_release(dr
->dt
.dl
.dr_data
, db
);
861 * Evict (if its unreferenced) or clear (if its referenced) any level-0
862 * data blocks in the free range, so that any future readers will find
865 * This is a no-op if the dataset is in the middle of an incremental
866 * receive; see comment below for details.
869 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
871 dmu_buf_impl_t
*db
, *db_next
;
872 uint64_t txg
= tx
->tx_txg
;
873 boolean_t freespill
=
874 (start
== DMU_SPILL_BLKID
|| end
== DMU_SPILL_BLKID
);
876 if (end
> dn
->dn_maxblkid
&& !freespill
)
877 end
= dn
->dn_maxblkid
;
878 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
880 mutex_enter(&dn
->dn_dbufs_mtx
);
881 if (start
>= dn
->dn_unlisted_l0_blkid
* dn
->dn_datablksz
&&
883 /* There can't be any dbufs in this range; no need to search. */
884 mutex_exit(&dn
->dn_dbufs_mtx
);
886 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
888 * If we are receiving, we expect there to be no dbufs in
889 * the range to be freed, because receive modifies each
890 * block at most once, and in offset order. If this is
891 * not the case, it can lead to performance problems,
892 * so note that we unexpectedly took the slow path.
894 atomic_inc_64(&zfs_free_range_recv_miss
);
897 for (db
= list_head(&dn
->dn_dbufs
); db
!= NULL
; db
= db_next
) {
898 db_next
= list_next(&dn
->dn_dbufs
, db
);
899 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
901 /* Skip indirect blocks. */
902 if (db
->db_level
!= 0)
904 /* Skip direct blocks outside the range. */
905 if (!freespill
&& (db
->db_blkid
< start
|| db
->db_blkid
> end
))
907 /* Skip all direct blocks, only free spill blocks. */
908 if (freespill
&& (db
->db_blkid
!= DMU_SPILL_BLKID
))
911 /* found a level 0 buffer in the range */
912 mutex_enter(&db
->db_mtx
);
913 if (dbuf_undirty(db
, tx
)) {
914 /* mutex has been dropped and dbuf destroyed */
918 if (db
->db_state
== DB_UNCACHED
||
919 db
->db_state
== DB_NOFILL
||
920 db
->db_state
== DB_EVICTING
) {
921 ASSERT(db
->db
.db_data
== NULL
);
922 mutex_exit(&db
->db_mtx
);
925 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
926 /* will be handled in dbuf_read_done or dbuf_rele */
927 db
->db_freed_in_flight
= TRUE
;
928 mutex_exit(&db
->db_mtx
);
931 if (refcount_count(&db
->db_holds
) == 0) {
936 /* The dbuf is referenced */
938 if (db
->db_last_dirty
!= NULL
) {
939 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
941 if (dr
->dr_txg
== txg
) {
943 * This buffer is "in-use", re-adjust the file
944 * size to reflect that this buffer may
945 * contain new data when we sync.
947 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
948 db
->db_blkid
> dn
->dn_maxblkid
)
949 dn
->dn_maxblkid
= db
->db_blkid
;
953 * This dbuf is not dirty in the open context.
954 * Either uncache it (if its not referenced in
955 * the open context) or reset its contents to
958 dbuf_fix_old_data(db
, txg
);
961 /* clear the contents if its cached */
962 if (db
->db_state
== DB_CACHED
) {
963 ASSERT(db
->db
.db_data
!= NULL
);
964 arc_release(db
->db_buf
, db
);
965 bzero(db
->db
.db_data
, db
->db
.db_size
);
966 arc_buf_freeze(db
->db_buf
);
969 mutex_exit(&db
->db_mtx
);
971 mutex_exit(&dn
->dn_dbufs_mtx
);
975 dbuf_block_freeable(dmu_buf_impl_t
*db
)
977 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
978 uint64_t birth_txg
= 0;
981 * We don't need any locking to protect db_blkptr:
982 * If it's syncing, then db_last_dirty will be set
983 * so we'll ignore db_blkptr.
985 * This logic ensures that only block births for
986 * filled blocks are considered.
988 ASSERT(MUTEX_HELD(&db
->db_mtx
));
989 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
990 !BP_IS_HOLE(db
->db_blkptr
))) {
991 birth_txg
= db
->db_last_dirty
->dr_txg
;
992 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
993 birth_txg
= db
->db_blkptr
->blk_birth
;
997 * If this block don't exist or is in a snapshot, it can't be freed.
998 * Don't pass the bp to dsl_dataset_block_freeable() since we
999 * are holding the db_mtx lock and might deadlock if we are
1000 * prefetching a dedup-ed block.
1003 return (ds
== NULL
||
1004 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1010 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1012 arc_buf_t
*buf
, *obuf
;
1013 int osize
= db
->db
.db_size
;
1014 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1017 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1022 /* XXX does *this* func really need the lock? */
1023 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1026 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1027 * is OK, because there can be no other references to the db
1028 * when we are changing its size, so no concurrent DB_FILL can
1032 * XXX we should be doing a dbuf_read, checking the return
1033 * value and returning that up to our callers
1035 dmu_buf_will_dirty(&db
->db
, tx
);
1037 /* create the data buffer for the new block */
1038 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1040 /* copy old block data to the new block */
1042 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1043 /* zero the remainder */
1045 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1047 mutex_enter(&db
->db_mtx
);
1048 dbuf_set_data(db
, buf
);
1049 VERIFY(arc_buf_remove_ref(obuf
, db
));
1050 db
->db
.db_size
= size
;
1052 if (db
->db_level
== 0) {
1053 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1054 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1056 mutex_exit(&db
->db_mtx
);
1058 dnode_willuse_space(dn
, size
-osize
, tx
);
1063 dbuf_release_bp(dmu_buf_impl_t
*db
)
1065 ASSERTV(objset_t
*os
= db
->db_objset
);
1067 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1068 ASSERT(arc_released(os
->os_phys_buf
) ||
1069 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1070 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1072 (void) arc_release(db
->db_buf
, db
);
1075 dbuf_dirty_record_t
*
1076 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1080 dbuf_dirty_record_t
**drp
, *dr
;
1081 int drop_struct_lock
= FALSE
;
1082 boolean_t do_free_accounting
= B_FALSE
;
1083 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1085 ASSERT(tx
->tx_txg
!= 0);
1086 ASSERT(!refcount_is_zero(&db
->db_holds
));
1087 DMU_TX_DIRTY_BUF(tx
, db
);
1092 * Shouldn't dirty a regular buffer in syncing context. Private
1093 * objects may be dirtied in syncing context, but only if they
1094 * were already pre-dirtied in open context.
1096 ASSERT(!dmu_tx_is_syncing(tx
) ||
1097 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1098 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1099 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1101 * We make this assert for private objects as well, but after we
1102 * check if we're already dirty. They are allowed to re-dirty
1103 * in syncing context.
1105 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1106 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1107 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1109 mutex_enter(&db
->db_mtx
);
1111 * XXX make this true for indirects too? The problem is that
1112 * transactions created with dmu_tx_create_assigned() from
1113 * syncing context don't bother holding ahead.
1115 ASSERT(db
->db_level
!= 0 ||
1116 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1117 db
->db_state
== DB_NOFILL
);
1119 mutex_enter(&dn
->dn_mtx
);
1121 * Don't set dirtyctx to SYNC if we're just modifying this as we
1122 * initialize the objset.
1124 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1125 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1127 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1128 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1129 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1131 mutex_exit(&dn
->dn_mtx
);
1133 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1134 dn
->dn_have_spill
= B_TRUE
;
1137 * If this buffer is already dirty, we're done.
1139 drp
= &db
->db_last_dirty
;
1140 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1141 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1142 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1144 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1147 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1149 * If this buffer has already been written out,
1150 * we now need to reset its state.
1152 dbuf_unoverride(dr
);
1153 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1154 db
->db_state
!= DB_NOFILL
)
1155 arc_buf_thaw(db
->db_buf
);
1157 mutex_exit(&db
->db_mtx
);
1162 * Only valid if not already dirty.
1164 ASSERT(dn
->dn_object
== 0 ||
1165 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1166 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1168 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1169 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1170 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1171 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1172 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1173 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1176 * We should only be dirtying in syncing context if it's the
1177 * mos or we're initializing the os or it's a special object.
1178 * However, we are allowed to dirty in syncing context provided
1179 * we already dirtied it in open context. Hence we must make
1180 * this assertion only if we're not already dirty.
1183 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1184 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1185 ASSERT(db
->db
.db_size
!= 0);
1187 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1189 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1191 * Update the accounting.
1192 * Note: we delay "free accounting" until after we drop
1193 * the db_mtx. This keeps us from grabbing other locks
1194 * (and possibly deadlocking) in bp_get_dsize() while
1195 * also holding the db_mtx.
1197 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1198 do_free_accounting
= dbuf_block_freeable(db
);
1202 * If this buffer is dirty in an old transaction group we need
1203 * to make a copy of it so that the changes we make in this
1204 * transaction group won't leak out when we sync the older txg.
1206 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1207 list_link_init(&dr
->dr_dirty_node
);
1208 if (db
->db_level
== 0) {
1209 void *data_old
= db
->db_buf
;
1211 if (db
->db_state
!= DB_NOFILL
) {
1212 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1213 dbuf_fix_old_data(db
, tx
->tx_txg
);
1214 data_old
= db
->db
.db_data
;
1215 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1217 * Release the data buffer from the cache so
1218 * that we can modify it without impacting
1219 * possible other users of this cached data
1220 * block. Note that indirect blocks and
1221 * private objects are not released until the
1222 * syncing state (since they are only modified
1225 arc_release(db
->db_buf
, db
);
1226 dbuf_fix_old_data(db
, tx
->tx_txg
);
1227 data_old
= db
->db_buf
;
1229 ASSERT(data_old
!= NULL
);
1231 dr
->dt
.dl
.dr_data
= data_old
;
1233 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1234 list_create(&dr
->dt
.di
.dr_children
,
1235 sizeof (dbuf_dirty_record_t
),
1236 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1238 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1239 dr
->dr_accounted
= db
->db
.db_size
;
1241 dr
->dr_txg
= tx
->tx_txg
;
1246 * We could have been freed_in_flight between the dbuf_noread
1247 * and dbuf_dirty. We win, as though the dbuf_noread() had
1248 * happened after the free.
1250 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1251 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1252 mutex_enter(&dn
->dn_mtx
);
1253 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1254 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1257 mutex_exit(&dn
->dn_mtx
);
1258 db
->db_freed_in_flight
= FALSE
;
1262 * This buffer is now part of this txg
1264 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1265 db
->db_dirtycnt
+= 1;
1266 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1268 mutex_exit(&db
->db_mtx
);
1270 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1271 db
->db_blkid
== DMU_SPILL_BLKID
) {
1272 mutex_enter(&dn
->dn_mtx
);
1273 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1274 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1275 mutex_exit(&dn
->dn_mtx
);
1276 dnode_setdirty(dn
, tx
);
1279 } else if (do_free_accounting
) {
1280 blkptr_t
*bp
= db
->db_blkptr
;
1281 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1282 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1284 * This is only a guess -- if the dbuf is dirty
1285 * in a previous txg, we don't know how much
1286 * space it will use on disk yet. We should
1287 * really have the struct_rwlock to access
1288 * db_blkptr, but since this is just a guess,
1289 * it's OK if we get an odd answer.
1291 ddt_prefetch(os
->os_spa
, bp
);
1292 dnode_willuse_space(dn
, -willfree
, tx
);
1295 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1296 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1297 drop_struct_lock
= TRUE
;
1300 if (db
->db_level
== 0) {
1301 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1302 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1305 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1306 dmu_buf_impl_t
*parent
= db
->db_parent
;
1307 dbuf_dirty_record_t
*di
;
1308 int parent_held
= FALSE
;
1310 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1311 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1313 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1314 db
->db_blkid
>> epbs
, FTAG
);
1315 ASSERT(parent
!= NULL
);
1318 if (drop_struct_lock
)
1319 rw_exit(&dn
->dn_struct_rwlock
);
1320 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1321 di
= dbuf_dirty(parent
, tx
);
1323 dbuf_rele(parent
, FTAG
);
1325 mutex_enter(&db
->db_mtx
);
1327 * Since we've dropped the mutex, it's possible that
1328 * dbuf_undirty() might have changed this out from under us.
1330 if (db
->db_last_dirty
== dr
||
1331 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1332 mutex_enter(&di
->dt
.di
.dr_mtx
);
1333 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1334 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1335 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1336 mutex_exit(&di
->dt
.di
.dr_mtx
);
1339 mutex_exit(&db
->db_mtx
);
1341 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1342 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1343 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1344 mutex_enter(&dn
->dn_mtx
);
1345 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1346 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1347 mutex_exit(&dn
->dn_mtx
);
1348 if (drop_struct_lock
)
1349 rw_exit(&dn
->dn_struct_rwlock
);
1352 dnode_setdirty(dn
, tx
);
1358 * Undirty a buffer in the transaction group referenced by the given
1359 * transaction. Return whether this evicted the dbuf.
1362 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1365 uint64_t txg
= tx
->tx_txg
;
1366 dbuf_dirty_record_t
*dr
, **drp
;
1369 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1370 ASSERT0(db
->db_level
);
1371 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1374 * If this buffer is not dirty, we're done.
1376 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1377 if (dr
->dr_txg
<= txg
)
1379 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1381 ASSERT(dr
->dr_txg
== txg
);
1382 ASSERT(dr
->dr_dbuf
== db
);
1387 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1389 ASSERT(db
->db
.db_size
!= 0);
1392 * Any space we accounted for in dp_dirty_* will be cleaned up by
1393 * dsl_pool_sync(). This is relatively rare so the discrepancy
1394 * is not a big deal.
1400 * Note that there are three places in dbuf_dirty()
1401 * where this dirty record may be put on a list.
1402 * Make sure to do a list_remove corresponding to
1403 * every one of those list_insert calls.
1405 if (dr
->dr_parent
) {
1406 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1407 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1408 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1409 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1410 db
->db_level
+1 == dn
->dn_nlevels
) {
1411 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1412 mutex_enter(&dn
->dn_mtx
);
1413 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1414 mutex_exit(&dn
->dn_mtx
);
1418 if (db
->db_state
!= DB_NOFILL
) {
1419 dbuf_unoverride(dr
);
1421 ASSERT(db
->db_buf
!= NULL
);
1422 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1423 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1424 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1426 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1428 ASSERT(db
->db_dirtycnt
> 0);
1429 db
->db_dirtycnt
-= 1;
1431 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1432 arc_buf_t
*buf
= db
->db_buf
;
1434 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1435 dbuf_set_data(db
, NULL
);
1436 VERIFY(arc_buf_remove_ref(buf
, db
));
1445 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1447 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1448 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1450 ASSERT(tx
->tx_txg
!= 0);
1451 ASSERT(!refcount_is_zero(&db
->db_holds
));
1454 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1455 rf
|= DB_RF_HAVESTRUCT
;
1457 (void) dbuf_read(db
, NULL
, rf
);
1458 (void) dbuf_dirty(db
, tx
);
1462 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1464 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1466 db
->db_state
= DB_NOFILL
;
1468 dmu_buf_will_fill(db_fake
, tx
);
1472 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1474 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1476 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1477 ASSERT(tx
->tx_txg
!= 0);
1478 ASSERT(db
->db_level
== 0);
1479 ASSERT(!refcount_is_zero(&db
->db_holds
));
1481 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1482 dmu_tx_private_ok(tx
));
1485 (void) dbuf_dirty(db
, tx
);
1488 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1491 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1493 mutex_enter(&db
->db_mtx
);
1496 if (db
->db_state
== DB_FILL
) {
1497 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1498 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1499 /* we were freed while filling */
1500 /* XXX dbuf_undirty? */
1501 bzero(db
->db
.db_data
, db
->db
.db_size
);
1502 db
->db_freed_in_flight
= FALSE
;
1504 db
->db_state
= DB_CACHED
;
1505 cv_broadcast(&db
->db_changed
);
1507 mutex_exit(&db
->db_mtx
);
1511 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1512 bp_embedded_type_t etype
, enum zio_compress comp
,
1513 int uncompressed_size
, int compressed_size
, int byteorder
,
1516 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1517 struct dirty_leaf
*dl
;
1518 dmu_object_type_t type
;
1521 type
= DB_DNODE(db
)->dn_type
;
1524 ASSERT0(db
->db_level
);
1525 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1527 dmu_buf_will_not_fill(dbuf
, tx
);
1529 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1530 dl
= &db
->db_last_dirty
->dt
.dl
;
1531 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1532 data
, comp
, uncompressed_size
, compressed_size
);
1533 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1534 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1535 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1536 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1538 dl
->dr_override_state
= DR_OVERRIDDEN
;
1539 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1543 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1544 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1547 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1549 ASSERT(!refcount_is_zero(&db
->db_holds
));
1550 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1551 ASSERT(db
->db_level
== 0);
1552 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1553 ASSERT(buf
!= NULL
);
1554 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1555 ASSERT(tx
->tx_txg
!= 0);
1557 arc_return_buf(buf
, db
);
1558 ASSERT(arc_released(buf
));
1560 mutex_enter(&db
->db_mtx
);
1562 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1563 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1565 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1567 if (db
->db_state
== DB_CACHED
&&
1568 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1569 mutex_exit(&db
->db_mtx
);
1570 (void) dbuf_dirty(db
, tx
);
1571 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1572 VERIFY(arc_buf_remove_ref(buf
, db
));
1573 xuio_stat_wbuf_copied();
1577 xuio_stat_wbuf_nocopy();
1578 if (db
->db_state
== DB_CACHED
) {
1579 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1581 ASSERT(db
->db_buf
!= NULL
);
1582 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1583 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1584 if (!arc_released(db
->db_buf
)) {
1585 ASSERT(dr
->dt
.dl
.dr_override_state
==
1587 arc_release(db
->db_buf
, db
);
1589 dr
->dt
.dl
.dr_data
= buf
;
1590 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1591 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1592 arc_release(db
->db_buf
, db
);
1593 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1597 ASSERT(db
->db_buf
== NULL
);
1598 dbuf_set_data(db
, buf
);
1599 db
->db_state
= DB_FILL
;
1600 mutex_exit(&db
->db_mtx
);
1601 (void) dbuf_dirty(db
, tx
);
1602 dmu_buf_fill_done(&db
->db
, tx
);
1606 * "Clear" the contents of this dbuf. This will mark the dbuf
1607 * EVICTING and clear *most* of its references. Unfortunately,
1608 * when we are not holding the dn_dbufs_mtx, we can't clear the
1609 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1610 * in this case. For callers from the DMU we will usually see:
1611 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1612 * For the arc callback, we will usually see:
1613 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1614 * Sometimes, though, we will get a mix of these two:
1615 * DMU: dbuf_clear()->arc_clear_callback()
1616 * ARC: dbuf_do_evict()->dbuf_destroy()
1618 * This routine will dissociate the dbuf from the arc, by calling
1619 * arc_clear_callback(), but will not evict the data from the ARC.
1622 dbuf_clear(dmu_buf_impl_t
*db
)
1625 dmu_buf_impl_t
*parent
= db
->db_parent
;
1626 dmu_buf_impl_t
*dndb
;
1627 boolean_t dbuf_gone
= B_FALSE
;
1629 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1630 ASSERT(refcount_is_zero(&db
->db_holds
));
1632 dbuf_evict_user(db
);
1634 if (db
->db_state
== DB_CACHED
) {
1635 ASSERT(db
->db
.db_data
!= NULL
);
1636 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1637 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1638 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1640 db
->db
.db_data
= NULL
;
1641 db
->db_state
= DB_UNCACHED
;
1644 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1645 ASSERT(db
->db_data_pending
== NULL
);
1647 db
->db_state
= DB_EVICTING
;
1648 db
->db_blkptr
= NULL
;
1653 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1654 list_remove(&dn
->dn_dbufs
, db
);
1655 atomic_dec_32(&dn
->dn_dbufs_count
);
1659 * Decrementing the dbuf count means that the hold corresponding
1660 * to the removed dbuf is no longer discounted in dnode_move(),
1661 * so the dnode cannot be moved until after we release the hold.
1662 * The membar_producer() ensures visibility of the decremented
1663 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1667 db
->db_dnode_handle
= NULL
;
1673 dbuf_gone
= arc_clear_callback(db
->db_buf
);
1676 mutex_exit(&db
->db_mtx
);
1679 * If this dbuf is referenced from an indirect dbuf,
1680 * decrement the ref count on the indirect dbuf.
1682 if (parent
&& parent
!= dndb
)
1683 dbuf_rele(parent
, db
);
1686 __attribute__((always_inline
))
1688 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1689 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
1696 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1698 if (blkid
== DMU_SPILL_BLKID
) {
1699 mutex_enter(&dn
->dn_mtx
);
1700 if (dn
->dn_have_spill
&&
1701 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1702 *bpp
= &dn
->dn_phys
->dn_spill
;
1705 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1706 *parentp
= dn
->dn_dbuf
;
1707 mutex_exit(&dn
->dn_mtx
);
1711 if (dn
->dn_phys
->dn_nlevels
== 0)
1714 nlevels
= dn
->dn_phys
->dn_nlevels
;
1716 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1718 ASSERT3U(level
* epbs
, <, 64);
1719 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1720 if (level
>= nlevels
||
1721 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1722 /* the buffer has no parent yet */
1723 return (SET_ERROR(ENOENT
));
1724 } else if (level
< nlevels
-1) {
1725 /* this block is referenced from an indirect block */
1728 err
= dbuf_hold_impl(dn
, level
+1, blkid
>> epbs
,
1729 fail_sparse
, NULL
, parentp
);
1731 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
1732 blkid
>> epbs
, fail_sparse
, NULL
,
1733 parentp
, dh
->dh_depth
+ 1);
1734 err
= __dbuf_hold_impl(dh
+ 1);
1738 err
= dbuf_read(*parentp
, NULL
,
1739 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1741 dbuf_rele(*parentp
, NULL
);
1745 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1746 (blkid
& ((1ULL << epbs
) - 1));
1749 /* the block is referenced from the dnode */
1750 ASSERT3U(level
, ==, nlevels
-1);
1751 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1752 blkid
< dn
->dn_phys
->dn_nblkptr
);
1754 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1755 *parentp
= dn
->dn_dbuf
;
1757 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1762 static dmu_buf_impl_t
*
1763 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1764 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1766 objset_t
*os
= dn
->dn_objset
;
1767 dmu_buf_impl_t
*db
, *odb
;
1769 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1770 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1772 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
1775 db
->db
.db_object
= dn
->dn_object
;
1776 db
->db_level
= level
;
1777 db
->db_blkid
= blkid
;
1778 db
->db_last_dirty
= NULL
;
1779 db
->db_dirtycnt
= 0;
1780 db
->db_dnode_handle
= dn
->dn_handle
;
1781 db
->db_parent
= parent
;
1782 db
->db_blkptr
= blkptr
;
1784 db
->db_user_ptr
= NULL
;
1785 db
->db_user_data_ptr_ptr
= NULL
;
1786 db
->db_evict_func
= NULL
;
1787 db
->db_immediate_evict
= 0;
1788 db
->db_freed_in_flight
= 0;
1790 if (blkid
== DMU_BONUS_BLKID
) {
1791 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1792 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1793 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1794 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1795 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1796 db
->db_state
= DB_UNCACHED
;
1797 /* the bonus dbuf is not placed in the hash table */
1798 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1800 } else if (blkid
== DMU_SPILL_BLKID
) {
1801 db
->db
.db_size
= (blkptr
!= NULL
) ?
1802 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1803 db
->db
.db_offset
= 0;
1806 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
1807 db
->db
.db_size
= blocksize
;
1808 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1812 * Hold the dn_dbufs_mtx while we get the new dbuf
1813 * in the hash table *and* added to the dbufs list.
1814 * This prevents a possible deadlock with someone
1815 * trying to look up this dbuf before its added to the
1818 mutex_enter(&dn
->dn_dbufs_mtx
);
1819 db
->db_state
= DB_EVICTING
;
1820 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1821 /* someone else inserted it first */
1822 kmem_cache_free(dbuf_cache
, db
);
1823 mutex_exit(&dn
->dn_dbufs_mtx
);
1826 list_insert_head(&dn
->dn_dbufs
, db
);
1827 if (db
->db_level
== 0 && db
->db_blkid
>=
1828 dn
->dn_unlisted_l0_blkid
)
1829 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
1830 db
->db_state
= DB_UNCACHED
;
1831 mutex_exit(&dn
->dn_dbufs_mtx
);
1832 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1834 if (parent
&& parent
!= dn
->dn_dbuf
)
1835 dbuf_add_ref(parent
, db
);
1837 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1838 refcount_count(&dn
->dn_holds
) > 0);
1839 (void) refcount_add(&dn
->dn_holds
, db
);
1840 atomic_inc_32(&dn
->dn_dbufs_count
);
1842 dprintf_dbuf(db
, "db=%p\n", db
);
1848 dbuf_do_evict(void *private)
1850 dmu_buf_impl_t
*db
= private;
1852 if (!MUTEX_HELD(&db
->db_mtx
))
1853 mutex_enter(&db
->db_mtx
);
1855 ASSERT(refcount_is_zero(&db
->db_holds
));
1857 if (db
->db_state
!= DB_EVICTING
) {
1858 ASSERT(db
->db_state
== DB_CACHED
);
1863 mutex_exit(&db
->db_mtx
);
1870 dbuf_destroy(dmu_buf_impl_t
*db
)
1872 ASSERT(refcount_is_zero(&db
->db_holds
));
1874 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1876 * If this dbuf is still on the dn_dbufs list,
1877 * remove it from that list.
1879 if (db
->db_dnode_handle
!= NULL
) {
1884 mutex_enter(&dn
->dn_dbufs_mtx
);
1885 list_remove(&dn
->dn_dbufs
, db
);
1886 atomic_dec_32(&dn
->dn_dbufs_count
);
1887 mutex_exit(&dn
->dn_dbufs_mtx
);
1890 * Decrementing the dbuf count means that the hold
1891 * corresponding to the removed dbuf is no longer
1892 * discounted in dnode_move(), so the dnode cannot be
1893 * moved until after we release the hold.
1896 db
->db_dnode_handle
= NULL
;
1898 dbuf_hash_remove(db
);
1900 db
->db_parent
= NULL
;
1903 ASSERT(!list_link_active(&db
->db_link
));
1904 ASSERT(db
->db
.db_data
== NULL
);
1905 ASSERT(db
->db_hash_next
== NULL
);
1906 ASSERT(db
->db_blkptr
== NULL
);
1907 ASSERT(db
->db_data_pending
== NULL
);
1909 kmem_cache_free(dbuf_cache
, db
);
1910 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1914 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
, zio_priority_t prio
)
1916 dmu_buf_impl_t
*db
= NULL
;
1917 blkptr_t
*bp
= NULL
;
1919 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1920 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1922 if (dnode_block_freed(dn
, blkid
))
1925 /* dbuf_find() returns with db_mtx held */
1926 if ((db
= dbuf_find(dn
, 0, blkid
))) {
1928 * This dbuf is already in the cache. We assume that
1929 * it is already CACHED, or else about to be either
1932 mutex_exit(&db
->db_mtx
);
1936 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
, NULL
) == 0) {
1937 if (bp
&& !BP_IS_HOLE(bp
) && !BP_IS_EMBEDDED(bp
)) {
1938 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1939 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1940 zbookmark_phys_t zb
;
1942 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1943 dn
->dn_object
, 0, blkid
);
1945 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1946 bp
, NULL
, NULL
, prio
,
1947 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1951 dbuf_rele(db
, NULL
);
1955 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1958 * Returns with db_holds incremented, and db_mtx not held.
1959 * Note: dn_struct_rwlock must be held.
1962 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
1964 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
1965 dh
->dh_parent
= NULL
;
1967 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
1968 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
1969 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
1971 *(dh
->dh_dbp
) = NULL
;
1973 /* dbuf_find() returns with db_mtx held */
1974 dh
->dh_db
= dbuf_find(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
);
1976 if (dh
->dh_db
== NULL
) {
1979 ASSERT3P(dh
->dh_parent
, ==, NULL
);
1980 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1981 dh
->dh_fail_sparse
, &dh
->dh_parent
,
1983 if (dh
->dh_fail_sparse
) {
1984 if (dh
->dh_err
== 0 &&
1985 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
1986 dh
->dh_err
= SET_ERROR(ENOENT
);
1989 dbuf_rele(dh
->dh_parent
, NULL
);
1990 return (dh
->dh_err
);
1993 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
1994 return (dh
->dh_err
);
1995 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1996 dh
->dh_parent
, dh
->dh_bp
);
1999 if (dh
->dh_db
->db_buf
&& refcount_is_zero(&dh
->dh_db
->db_holds
)) {
2000 arc_buf_add_ref(dh
->dh_db
->db_buf
, dh
->dh_db
);
2001 if (dh
->dh_db
->db_buf
->b_data
== NULL
) {
2002 dbuf_clear(dh
->dh_db
);
2003 if (dh
->dh_parent
) {
2004 dbuf_rele(dh
->dh_parent
, NULL
);
2005 dh
->dh_parent
= NULL
;
2009 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2012 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2015 * If this buffer is currently syncing out, and we are are
2016 * still referencing it from db_data, we need to make a copy
2017 * of it in case we decide we want to dirty it again in this txg.
2019 if (dh
->dh_db
->db_level
== 0 &&
2020 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2021 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2022 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2023 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2025 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2026 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2028 dbuf_set_data(dh
->dh_db
,
2029 arc_buf_alloc(dh
->dh_dn
->dn_objset
->os_spa
,
2030 dh
->dh_db
->db
.db_size
, dh
->dh_db
, dh
->dh_type
));
2031 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2032 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2036 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2037 dbuf_update_data(dh
->dh_db
);
2038 DBUF_VERIFY(dh
->dh_db
);
2039 mutex_exit(&dh
->dh_db
->db_mtx
);
2041 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2043 dbuf_rele(dh
->dh_parent
, NULL
);
2045 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2046 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2047 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2048 *(dh
->dh_dbp
) = dh
->dh_db
;
2054 * The following code preserves the recursive function dbuf_hold_impl()
2055 * but moves the local variables AND function arguments to the heap to
2056 * minimize the stack frame size. Enough space is initially allocated
2057 * on the stack for 20 levels of recursion.
2060 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2061 void *tag
, dmu_buf_impl_t
**dbp
)
2063 struct dbuf_hold_impl_data
*dh
;
2066 dh
= kmem_zalloc(sizeof (struct dbuf_hold_impl_data
) *
2067 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2068 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
, tag
, dbp
, 0);
2070 error
= __dbuf_hold_impl(dh
);
2072 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2073 DBUF_HOLD_IMPL_MAX_DEPTH
);
2079 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2080 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2081 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2084 dh
->dh_level
= level
;
2085 dh
->dh_blkid
= blkid
;
2086 dh
->dh_fail_sparse
= fail_sparse
;
2089 dh
->dh_depth
= depth
;
2093 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2096 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
2097 return (err
? NULL
: db
);
2101 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2104 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
2105 return (err
? NULL
: db
);
2109 dbuf_create_bonus(dnode_t
*dn
)
2111 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2113 ASSERT(dn
->dn_bonus
== NULL
);
2114 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2118 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2120 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2123 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2124 return (SET_ERROR(ENOTSUP
));
2126 blksz
= SPA_MINBLOCKSIZE
;
2127 if (blksz
> SPA_MAXBLOCKSIZE
)
2128 blksz
= SPA_MAXBLOCKSIZE
;
2130 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2134 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2135 dbuf_new_size(db
, blksz
, tx
);
2136 rw_exit(&dn
->dn_struct_rwlock
);
2143 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2145 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2148 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2150 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2152 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2156 * If you call dbuf_rele() you had better not be referencing the dnode handle
2157 * unless you have some other direct or indirect hold on the dnode. (An indirect
2158 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2159 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2160 * dnode's parent dbuf evicting its dnode handles.
2163 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2165 mutex_enter(&db
->db_mtx
);
2166 dbuf_rele_and_unlock(db
, tag
);
2170 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2172 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2176 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2177 * db_dirtycnt and db_holds to be updated atomically.
2180 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2184 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2188 * Remove the reference to the dbuf before removing its hold on the
2189 * dnode so we can guarantee in dnode_move() that a referenced bonus
2190 * buffer has a corresponding dnode hold.
2192 holds
= refcount_remove(&db
->db_holds
, tag
);
2196 * We can't freeze indirects if there is a possibility that they
2197 * may be modified in the current syncing context.
2199 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2200 arc_buf_freeze(db
->db_buf
);
2202 if (holds
== db
->db_dirtycnt
&&
2203 db
->db_level
== 0 && db
->db_immediate_evict
)
2204 dbuf_evict_user(db
);
2207 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2211 * If the dnode moves here, we cannot cross this
2212 * barrier until the move completes.
2217 atomic_dec_32(&dn
->dn_dbufs_count
);
2220 * Decrementing the dbuf count means that the bonus
2221 * buffer's dnode hold is no longer discounted in
2222 * dnode_move(). The dnode cannot move until after
2223 * the dnode_rele_and_unlock() below.
2228 * Do not reference db after its lock is dropped.
2229 * Another thread may evict it.
2231 mutex_exit(&db
->db_mtx
);
2234 * If the dnode has been freed, evict the bonus
2235 * buffer immediately. The data in the bonus
2236 * buffer is no longer relevant and this prevents
2237 * a stale bonus buffer from being associated
2238 * with this dnode_t should the dnode_t be reused
2239 * prior to being destroyed.
2241 mutex_enter(&dn
->dn_mtx
);
2242 if (dn
->dn_type
== DMU_OT_NONE
||
2243 dn
->dn_free_txg
!= 0) {
2245 * Drop dn_mtx. It is a leaf lock and
2246 * cannot be held when dnode_evict_bonus()
2247 * acquires other locks in order to
2248 * perform the eviction.
2250 * Freed dnodes cannot be reused until the
2251 * last hold is released. Since this bonus
2252 * buffer has a hold, the dnode will remain
2253 * in the free state, even without dn_mtx
2254 * held, until the dnode_rele_and_unlock()
2257 mutex_exit(&dn
->dn_mtx
);
2258 dnode_evict_bonus(dn
);
2259 mutex_enter(&dn
->dn_mtx
);
2261 dnode_rele_and_unlock(dn
, db
);
2262 } else if (db
->db_buf
== NULL
) {
2264 * This is a special case: we never associated this
2265 * dbuf with any data allocated from the ARC.
2267 ASSERT(db
->db_state
== DB_UNCACHED
||
2268 db
->db_state
== DB_NOFILL
);
2270 } else if (arc_released(db
->db_buf
)) {
2271 arc_buf_t
*buf
= db
->db_buf
;
2273 * This dbuf has anonymous data associated with it.
2275 dbuf_set_data(db
, NULL
);
2276 VERIFY(arc_buf_remove_ref(buf
, db
));
2279 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2282 * A dbuf will be eligible for eviction if either the
2283 * 'primarycache' property is set or a duplicate
2284 * copy of this buffer is already cached in the arc.
2286 * In the case of the 'primarycache' a buffer
2287 * is considered for eviction if it matches the
2288 * criteria set in the property.
2290 * To decide if our buffer is considered a
2291 * duplicate, we must call into the arc to determine
2292 * if multiple buffers are referencing the same
2293 * block on-disk. If so, then we simply evict
2296 if (!DBUF_IS_CACHEABLE(db
)) {
2297 if (db
->db_blkptr
!= NULL
&&
2298 !BP_IS_HOLE(db
->db_blkptr
) &&
2299 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2301 dmu_objset_spa(db
->db_objset
);
2302 blkptr_t bp
= *db
->db_blkptr
;
2304 arc_freed(spa
, &bp
);
2308 } else if (arc_buf_eviction_needed(db
->db_buf
)) {
2311 mutex_exit(&db
->db_mtx
);
2315 mutex_exit(&db
->db_mtx
);
2319 #pragma weak dmu_buf_refcount = dbuf_refcount
2321 dbuf_refcount(dmu_buf_impl_t
*db
)
2323 return (refcount_count(&db
->db_holds
));
2327 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2328 dmu_buf_evict_func_t
*evict_func
)
2330 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2331 user_data_ptr_ptr
, evict_func
));
2335 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2336 dmu_buf_evict_func_t
*evict_func
)
2338 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2340 db
->db_immediate_evict
= TRUE
;
2341 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2342 user_data_ptr_ptr
, evict_func
));
2346 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
2347 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2349 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2350 ASSERT(db
->db_level
== 0);
2352 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2354 mutex_enter(&db
->db_mtx
);
2356 if (db
->db_user_ptr
== old_user_ptr
) {
2357 db
->db_user_ptr
= user_ptr
;
2358 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2359 db
->db_evict_func
= evict_func
;
2361 dbuf_update_data(db
);
2363 old_user_ptr
= db
->db_user_ptr
;
2366 mutex_exit(&db
->db_mtx
);
2367 return (old_user_ptr
);
2371 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2373 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2374 ASSERT(!refcount_is_zero(&db
->db_holds
));
2376 return (db
->db_user_ptr
);
2380 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2382 boolean_t res
= B_FALSE
;
2383 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2386 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2387 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2393 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2395 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2396 return (dbi
->db_blkptr
);
2400 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2402 /* ASSERT(dmu_tx_is_syncing(tx) */
2403 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2405 if (db
->db_blkptr
!= NULL
)
2408 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2409 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2410 BP_ZERO(db
->db_blkptr
);
2413 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2415 * This buffer was allocated at a time when there was
2416 * no available blkptrs from the dnode, or it was
2417 * inappropriate to hook it in (i.e., nlevels mis-match).
2419 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2420 ASSERT(db
->db_parent
== NULL
);
2421 db
->db_parent
= dn
->dn_dbuf
;
2422 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2425 dmu_buf_impl_t
*parent
= db
->db_parent
;
2426 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2428 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2429 if (parent
== NULL
) {
2430 mutex_exit(&db
->db_mtx
);
2431 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2432 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2433 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2434 rw_exit(&dn
->dn_struct_rwlock
);
2435 mutex_enter(&db
->db_mtx
);
2436 db
->db_parent
= parent
;
2438 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2439 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2445 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2446 * is critical the we not allow the compiler to inline this function in to
2447 * dbuf_sync_list() thereby drastically bloating the stack usage.
2449 noinline
static void
2450 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2452 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2456 ASSERT(dmu_tx_is_syncing(tx
));
2458 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2460 mutex_enter(&db
->db_mtx
);
2462 ASSERT(db
->db_level
> 0);
2465 /* Read the block if it hasn't been read yet. */
2466 if (db
->db_buf
== NULL
) {
2467 mutex_exit(&db
->db_mtx
);
2468 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2469 mutex_enter(&db
->db_mtx
);
2471 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2472 ASSERT(db
->db_buf
!= NULL
);
2476 /* Indirect block size must match what the dnode thinks it is. */
2477 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2478 dbuf_check_blkptr(dn
, db
);
2481 /* Provide the pending dirty record to child dbufs */
2482 db
->db_data_pending
= dr
;
2484 mutex_exit(&db
->db_mtx
);
2485 dbuf_write(dr
, db
->db_buf
, tx
);
2488 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2489 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2490 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2491 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2496 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2497 * critical the we not allow the compiler to inline this function in to
2498 * dbuf_sync_list() thereby drastically bloating the stack usage.
2500 noinline
static void
2501 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2503 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2504 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2507 uint64_t txg
= tx
->tx_txg
;
2509 ASSERT(dmu_tx_is_syncing(tx
));
2511 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2513 mutex_enter(&db
->db_mtx
);
2515 * To be synced, we must be dirtied. But we
2516 * might have been freed after the dirty.
2518 if (db
->db_state
== DB_UNCACHED
) {
2519 /* This buffer has been freed since it was dirtied */
2520 ASSERT(db
->db
.db_data
== NULL
);
2521 } else if (db
->db_state
== DB_FILL
) {
2522 /* This buffer was freed and is now being re-filled */
2523 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2525 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2532 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2533 mutex_enter(&dn
->dn_mtx
);
2534 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2535 mutex_exit(&dn
->dn_mtx
);
2539 * If this is a bonus buffer, simply copy the bonus data into the
2540 * dnode. It will be written out when the dnode is synced (and it
2541 * will be synced, since it must have been dirty for dbuf_sync to
2544 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2545 dbuf_dirty_record_t
**drp
;
2547 ASSERT(*datap
!= NULL
);
2548 ASSERT0(db
->db_level
);
2549 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2550 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2553 if (*datap
!= db
->db
.db_data
) {
2554 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2555 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2557 db
->db_data_pending
= NULL
;
2558 drp
= &db
->db_last_dirty
;
2560 drp
= &(*drp
)->dr_next
;
2561 ASSERT(dr
->dr_next
== NULL
);
2562 ASSERT(dr
->dr_dbuf
== db
);
2564 if (dr
->dr_dbuf
->db_level
!= 0) {
2565 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2566 list_destroy(&dr
->dt
.di
.dr_children
);
2568 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2569 ASSERT(db
->db_dirtycnt
> 0);
2570 db
->db_dirtycnt
-= 1;
2571 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2578 * This function may have dropped the db_mtx lock allowing a dmu_sync
2579 * operation to sneak in. As a result, we need to ensure that we
2580 * don't check the dr_override_state until we have returned from
2581 * dbuf_check_blkptr.
2583 dbuf_check_blkptr(dn
, db
);
2586 * If this buffer is in the middle of an immediate write,
2587 * wait for the synchronous IO to complete.
2589 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2590 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2591 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2592 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2595 if (db
->db_state
!= DB_NOFILL
&&
2596 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2597 refcount_count(&db
->db_holds
) > 1 &&
2598 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2599 *datap
== db
->db_buf
) {
2601 * If this buffer is currently "in use" (i.e., there
2602 * are active holds and db_data still references it),
2603 * then make a copy before we start the write so that
2604 * any modifications from the open txg will not leak
2607 * NOTE: this copy does not need to be made for
2608 * objects only modified in the syncing context (e.g.
2609 * DNONE_DNODE blocks).
2611 int blksz
= arc_buf_size(*datap
);
2612 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2613 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2614 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2616 db
->db_data_pending
= dr
;
2618 mutex_exit(&db
->db_mtx
);
2620 dbuf_write(dr
, *datap
, tx
);
2622 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2623 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2624 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2628 * Although zio_nowait() does not "wait for an IO", it does
2629 * initiate the IO. If this is an empty write it seems plausible
2630 * that the IO could actually be completed before the nowait
2631 * returns. We need to DB_DNODE_EXIT() first in case
2632 * zio_nowait() invalidates the dbuf.
2635 zio_nowait(dr
->dr_zio
);
2640 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2642 dbuf_dirty_record_t
*dr
;
2644 while ((dr
= list_head(list
))) {
2645 if (dr
->dr_zio
!= NULL
) {
2647 * If we find an already initialized zio then we
2648 * are processing the meta-dnode, and we have finished.
2649 * The dbufs for all dnodes are put back on the list
2650 * during processing, so that we can zio_wait()
2651 * these IOs after initiating all child IOs.
2653 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2654 DMU_META_DNODE_OBJECT
);
2657 list_remove(list
, dr
);
2658 if (dr
->dr_dbuf
->db_level
> 0)
2659 dbuf_sync_indirect(dr
, tx
);
2661 dbuf_sync_leaf(dr
, tx
);
2667 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2669 dmu_buf_impl_t
*db
= vdb
;
2671 blkptr_t
*bp
= zio
->io_bp
;
2672 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2673 spa_t
*spa
= zio
->io_spa
;
2678 ASSERT3P(db
->db_blkptr
, ==, bp
);
2682 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2683 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2684 zio
->io_prev_space_delta
= delta
;
2686 if (bp
->blk_birth
!= 0) {
2687 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2688 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2689 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2690 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
2691 BP_IS_EMBEDDED(bp
));
2692 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2695 mutex_enter(&db
->db_mtx
);
2698 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2699 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2700 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2701 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2705 if (db
->db_level
== 0) {
2706 mutex_enter(&dn
->dn_mtx
);
2707 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2708 db
->db_blkid
!= DMU_SPILL_BLKID
)
2709 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2710 mutex_exit(&dn
->dn_mtx
);
2712 if (dn
->dn_type
== DMU_OT_DNODE
) {
2713 dnode_phys_t
*dnp
= db
->db
.db_data
;
2714 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2716 if (dnp
->dn_type
!= DMU_OT_NONE
)
2720 if (BP_IS_HOLE(bp
)) {
2727 blkptr_t
*ibp
= db
->db
.db_data
;
2728 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2729 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2730 if (BP_IS_HOLE(ibp
))
2732 fill
+= BP_GET_FILL(ibp
);
2737 if (!BP_IS_EMBEDDED(bp
))
2738 bp
->blk_fill
= fill
;
2740 mutex_exit(&db
->db_mtx
);
2744 * The SPA will call this callback several times for each zio - once
2745 * for every physical child i/o (zio->io_phys_children times). This
2746 * allows the DMU to monitor the progress of each logical i/o. For example,
2747 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2748 * block. There may be a long delay before all copies/fragments are completed,
2749 * so this callback allows us to retire dirty space gradually, as the physical
2754 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2756 dmu_buf_impl_t
*db
= arg
;
2757 objset_t
*os
= db
->db_objset
;
2758 dsl_pool_t
*dp
= dmu_objset_pool(os
);
2759 dbuf_dirty_record_t
*dr
;
2762 dr
= db
->db_data_pending
;
2763 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
2766 * The callback will be called io_phys_children times. Retire one
2767 * portion of our dirty space each time we are called. Any rounding
2768 * error will be cleaned up by dsl_pool_sync()'s call to
2769 * dsl_pool_undirty_space().
2771 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
2772 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
2777 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2779 dmu_buf_impl_t
*db
= vdb
;
2780 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2781 blkptr_t
*bp
= db
->db_blkptr
;
2782 objset_t
*os
= db
->db_objset
;
2783 dmu_tx_t
*tx
= os
->os_synctx
;
2784 dbuf_dirty_record_t
**drp
, *dr
;
2786 ASSERT0(zio
->io_error
);
2787 ASSERT(db
->db_blkptr
== bp
);
2790 * For nopwrites and rewrites we ensure that the bp matches our
2791 * original and bypass all the accounting.
2793 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
2794 ASSERT(BP_EQUAL(bp
, bp_orig
));
2796 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2797 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2798 dsl_dataset_block_born(ds
, bp
, tx
);
2801 mutex_enter(&db
->db_mtx
);
2805 drp
= &db
->db_last_dirty
;
2806 while ((dr
= *drp
) != db
->db_data_pending
)
2808 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2809 ASSERT(dr
->dr_dbuf
== db
);
2810 ASSERT(dr
->dr_next
== NULL
);
2814 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2819 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2820 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2821 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2826 if (db
->db_level
== 0) {
2827 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2828 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2829 if (db
->db_state
!= DB_NOFILL
) {
2830 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2831 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2833 else if (!arc_released(db
->db_buf
))
2834 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2841 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2842 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
2843 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2844 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
2846 ASSERT3U(db
->db_blkid
, <=,
2847 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
2848 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2850 if (!arc_released(db
->db_buf
))
2851 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2854 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2855 list_destroy(&dr
->dt
.di
.dr_children
);
2857 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2859 cv_broadcast(&db
->db_changed
);
2860 ASSERT(db
->db_dirtycnt
> 0);
2861 db
->db_dirtycnt
-= 1;
2862 db
->db_data_pending
= NULL
;
2863 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
2867 dbuf_write_nofill_ready(zio_t
*zio
)
2869 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2873 dbuf_write_nofill_done(zio_t
*zio
)
2875 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2879 dbuf_write_override_ready(zio_t
*zio
)
2881 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2882 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2884 dbuf_write_ready(zio
, NULL
, db
);
2888 dbuf_write_override_done(zio_t
*zio
)
2890 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2891 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2892 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2894 mutex_enter(&db
->db_mtx
);
2895 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2896 if (!BP_IS_HOLE(obp
))
2897 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2898 arc_release(dr
->dt
.dl
.dr_data
, db
);
2900 mutex_exit(&db
->db_mtx
);
2902 dbuf_write_done(zio
, NULL
, db
);
2905 /* Issue I/O to commit a dirty buffer to disk. */
2907 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2909 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2912 dmu_buf_impl_t
*parent
= db
->db_parent
;
2913 uint64_t txg
= tx
->tx_txg
;
2914 zbookmark_phys_t zb
;
2923 if (db
->db_state
!= DB_NOFILL
) {
2924 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2926 * Private object buffers are released here rather
2927 * than in dbuf_dirty() since they are only modified
2928 * in the syncing context and we don't want the
2929 * overhead of making multiple copies of the data.
2931 if (BP_IS_HOLE(db
->db_blkptr
)) {
2934 dbuf_release_bp(db
);
2939 if (parent
!= dn
->dn_dbuf
) {
2940 /* Our parent is an indirect block. */
2941 /* We have a dirty parent that has been scheduled for write. */
2942 ASSERT(parent
&& parent
->db_data_pending
);
2943 /* Our parent's buffer is one level closer to the dnode. */
2944 ASSERT(db
->db_level
== parent
->db_level
-1);
2946 * We're about to modify our parent's db_data by modifying
2947 * our block pointer, so the parent must be released.
2949 ASSERT(arc_released(parent
->db_buf
));
2950 zio
= parent
->db_data_pending
->dr_zio
;
2952 /* Our parent is the dnode itself. */
2953 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2954 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2955 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2956 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2957 ASSERT3P(db
->db_blkptr
, ==,
2958 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2962 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2963 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2966 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2967 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2968 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2970 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2972 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2974 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2977 if (db
->db_level
== 0 &&
2978 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2980 * The BP for this block has been provided by open context
2981 * (by dmu_sync() or dmu_buf_write_embedded()).
2983 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
2985 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2986 db
->db_blkptr
, contents
, db
->db
.db_size
, &zp
,
2987 dbuf_write_override_ready
, NULL
, dbuf_write_override_done
,
2988 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2989 mutex_enter(&db
->db_mtx
);
2990 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2991 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2992 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
2993 mutex_exit(&db
->db_mtx
);
2994 } else if (db
->db_state
== DB_NOFILL
) {
2995 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2996 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2997 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2998 dbuf_write_nofill_ready
, NULL
, dbuf_write_nofill_done
, db
,
2999 ZIO_PRIORITY_ASYNC_WRITE
,
3000 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3002 ASSERT(arc_released(data
));
3003 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3004 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
),
3005 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
3006 dbuf_write_physdone
, dbuf_write_done
, db
,
3007 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3011 #if defined(_KERNEL) && defined(HAVE_SPL)
3012 EXPORT_SYMBOL(dbuf_find
);
3013 EXPORT_SYMBOL(dbuf_is_metadata
);
3014 EXPORT_SYMBOL(dbuf_evict
);
3015 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3016 EXPORT_SYMBOL(dbuf_whichblock
);
3017 EXPORT_SYMBOL(dbuf_read
);
3018 EXPORT_SYMBOL(dbuf_unoverride
);
3019 EXPORT_SYMBOL(dbuf_free_range
);
3020 EXPORT_SYMBOL(dbuf_new_size
);
3021 EXPORT_SYMBOL(dbuf_release_bp
);
3022 EXPORT_SYMBOL(dbuf_dirty
);
3023 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3024 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3025 EXPORT_SYMBOL(dmu_buf_will_fill
);
3026 EXPORT_SYMBOL(dmu_buf_fill_done
);
3027 EXPORT_SYMBOL(dmu_buf_rele
);
3028 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3029 EXPORT_SYMBOL(dbuf_clear
);
3030 EXPORT_SYMBOL(dbuf_prefetch
);
3031 EXPORT_SYMBOL(dbuf_hold_impl
);
3032 EXPORT_SYMBOL(dbuf_hold
);
3033 EXPORT_SYMBOL(dbuf_hold_level
);
3034 EXPORT_SYMBOL(dbuf_create_bonus
);
3035 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3036 EXPORT_SYMBOL(dbuf_rm_spill
);
3037 EXPORT_SYMBOL(dbuf_add_ref
);
3038 EXPORT_SYMBOL(dbuf_rele
);
3039 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3040 EXPORT_SYMBOL(dbuf_refcount
);
3041 EXPORT_SYMBOL(dbuf_sync_list
);
3042 EXPORT_SYMBOL(dmu_buf_set_user
);
3043 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3044 EXPORT_SYMBOL(dmu_buf_update_user
);
3045 EXPORT_SYMBOL(dmu_buf_get_user
);
3046 EXPORT_SYMBOL(dmu_buf_freeable
);
3047 EXPORT_SYMBOL(dmu_buf_get_blkptr
);