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 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_impl.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dmu_tx.h>
39 #include <sys/dmu_zfetch.h>
41 #include <sys/sa_impl.h>
43 struct dbuf_hold_impl_data
{
44 /* Function arguments */
50 dmu_buf_impl_t
**dh_dbp
;
52 dmu_buf_impl_t
*dh_db
;
53 dmu_buf_impl_t
*dh_parent
;
56 dbuf_dirty_record_t
*dh_dr
;
57 arc_buf_contents_t dh_type
;
61 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
62 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
63 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
64 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
66 static void dbuf_destroy(dmu_buf_impl_t
*db
);
67 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
68 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
71 * Global data structures and functions for the dbuf cache.
73 static kmem_cache_t
*dbuf_cache
;
77 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
79 dmu_buf_impl_t
*db
= vdb
;
80 bzero(db
, sizeof (dmu_buf_impl_t
));
82 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
83 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
84 refcount_create(&db
->db_holds
);
85 list_link_init(&db
->db_link
);
91 dbuf_dest(void *vdb
, void *unused
)
93 dmu_buf_impl_t
*db
= vdb
;
94 mutex_destroy(&db
->db_mtx
);
95 cv_destroy(&db
->db_changed
);
96 refcount_destroy(&db
->db_holds
);
100 * dbuf hash table routines
102 static dbuf_hash_table_t dbuf_hash_table
;
104 static uint64_t dbuf_hash_count
;
107 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
109 uintptr_t osv
= (uintptr_t)os
;
110 uint64_t crc
= -1ULL;
112 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
113 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
114 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
115 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
116 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
117 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
118 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
120 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
125 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
127 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
128 ((dbuf)->db.db_object == (obj) && \
129 (dbuf)->db_objset == (os) && \
130 (dbuf)->db_level == (level) && \
131 (dbuf)->db_blkid == (blkid))
134 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
136 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
137 objset_t
*os
= dn
->dn_objset
;
144 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
145 idx
= hv
& h
->hash_table_mask
;
147 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
148 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
149 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
150 mutex_enter(&db
->db_mtx
);
151 if (db
->db_state
!= DB_EVICTING
) {
152 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
155 mutex_exit(&db
->db_mtx
);
158 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
163 * Insert an entry into the hash table. If there is already an element
164 * equal to elem in the hash table, then the already existing element
165 * will be returned and the new element will not be inserted.
166 * Otherwise returns NULL.
168 static dmu_buf_impl_t
*
169 dbuf_hash_insert(dmu_buf_impl_t
*db
)
171 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
172 objset_t
*os
= db
->db_objset
;
173 uint64_t obj
= db
->db
.db_object
;
174 int level
= db
->db_level
;
175 uint64_t blkid
, hv
, idx
;
178 blkid
= db
->db_blkid
;
179 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
180 idx
= hv
& h
->hash_table_mask
;
182 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
183 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
184 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
185 mutex_enter(&dbf
->db_mtx
);
186 if (dbf
->db_state
!= DB_EVICTING
) {
187 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
190 mutex_exit(&dbf
->db_mtx
);
194 mutex_enter(&db
->db_mtx
);
195 db
->db_hash_next
= h
->hash_table
[idx
];
196 h
->hash_table
[idx
] = db
;
197 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
198 atomic_add_64(&dbuf_hash_count
, 1);
204 * Remove an entry from the hash table. This operation will
205 * fail if there are any existing holds on the db.
208 dbuf_hash_remove(dmu_buf_impl_t
*db
)
210 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
212 dmu_buf_impl_t
*dbf
, **dbp
;
214 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
215 db
->db_level
, db
->db_blkid
);
216 idx
= hv
& h
->hash_table_mask
;
219 * We musn't hold db_mtx to maintin lock ordering:
220 * DBUF_HASH_MUTEX > db_mtx.
222 ASSERT(refcount_is_zero(&db
->db_holds
));
223 ASSERT(db
->db_state
== DB_EVICTING
);
224 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
226 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
227 dbp
= &h
->hash_table
[idx
];
228 while ((dbf
= *dbp
) != db
) {
229 dbp
= &dbf
->db_hash_next
;
232 *dbp
= db
->db_hash_next
;
233 db
->db_hash_next
= NULL
;
234 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
235 atomic_add_64(&dbuf_hash_count
, -1);
238 static arc_evict_func_t dbuf_do_evict
;
241 dbuf_evict_user(dmu_buf_impl_t
*db
)
243 ASSERT(MUTEX_HELD(&db
->db_mtx
));
245 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
248 if (db
->db_user_data_ptr_ptr
)
249 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
250 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
251 db
->db_user_ptr
= NULL
;
252 db
->db_user_data_ptr_ptr
= NULL
;
253 db
->db_evict_func
= NULL
;
257 dbuf_is_metadata(dmu_buf_impl_t
*db
)
259 if (db
->db_level
> 0) {
262 boolean_t is_metadata
;
265 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
268 return (is_metadata
);
273 dbuf_evict(dmu_buf_impl_t
*db
)
275 ASSERT(MUTEX_HELD(&db
->db_mtx
));
276 ASSERT(db
->db_buf
== NULL
);
277 ASSERT(db
->db_data_pending
== NULL
);
286 uint64_t hsize
= 1ULL << 16;
287 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
291 * The hash table is big enough to fill all of physical memory
292 * with an average 4K block size. The table will take up
293 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
295 while (hsize
* 4096 < physmem
* PAGESIZE
)
299 h
->hash_table_mask
= hsize
- 1;
300 #if defined(_KERNEL) && defined(HAVE_SPL)
301 /* Large allocations which do not require contiguous pages
302 * should be using vmem_alloc() in the linux kernel */
303 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_PUSHPAGE
);
305 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
307 if (h
->hash_table
== NULL
) {
308 /* XXX - we should really return an error instead of assert */
309 ASSERT(hsize
> (1ULL << 10));
314 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
315 sizeof (dmu_buf_impl_t
),
316 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
318 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
319 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
327 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
330 dbuf_stats_destroy();
332 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
333 mutex_destroy(&h
->hash_mutexes
[i
]);
334 #if defined(_KERNEL) && defined(HAVE_SPL)
335 /* Large allocations which do not require contiguous pages
336 * should be using vmem_free() in the linux kernel */
337 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
339 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
341 kmem_cache_destroy(dbuf_cache
);
350 dbuf_verify(dmu_buf_impl_t
*db
)
353 dbuf_dirty_record_t
*dr
;
355 ASSERT(MUTEX_HELD(&db
->db_mtx
));
357 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
360 ASSERT(db
->db_objset
!= NULL
);
364 ASSERT(db
->db_parent
== NULL
);
365 ASSERT(db
->db_blkptr
== NULL
);
367 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
368 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
369 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
370 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
371 db
->db_blkid
== DMU_SPILL_BLKID
||
372 !list_is_empty(&dn
->dn_dbufs
));
374 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
376 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
377 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
378 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
380 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
381 ASSERT0(db
->db
.db_offset
);
383 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
386 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
387 ASSERT(dr
->dr_dbuf
== db
);
389 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
390 ASSERT(dr
->dr_dbuf
== db
);
393 * We can't assert that db_size matches dn_datablksz because it
394 * can be momentarily different when another thread is doing
397 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
398 dr
= db
->db_data_pending
;
400 * It should only be modified in syncing context, so
401 * make sure we only have one copy of the data.
403 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
406 /* verify db->db_blkptr */
408 if (db
->db_parent
== dn
->dn_dbuf
) {
409 /* db is pointed to by the dnode */
410 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
411 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
412 ASSERT(db
->db_parent
== NULL
);
414 ASSERT(db
->db_parent
!= NULL
);
415 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
416 ASSERT3P(db
->db_blkptr
, ==,
417 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
419 /* db is pointed to by an indirect block */
420 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
422 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
423 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
426 * dnode_grow_indblksz() can make this fail if we don't
427 * have the struct_rwlock. XXX indblksz no longer
428 * grows. safe to do this now?
430 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
431 ASSERT3P(db
->db_blkptr
, ==,
432 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
433 db
->db_blkid
% epb
));
437 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
438 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
439 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
440 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
442 * If the blkptr isn't set but they have nonzero data,
443 * it had better be dirty, otherwise we'll lose that
444 * data when we evict this buffer.
446 if (db
->db_dirtycnt
== 0) {
447 ASSERTV(uint64_t *buf
= db
->db
.db_data
);
450 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
460 dbuf_update_data(dmu_buf_impl_t
*db
)
462 ASSERT(MUTEX_HELD(&db
->db_mtx
));
463 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
464 ASSERT(!refcount_is_zero(&db
->db_holds
));
465 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
470 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
472 ASSERT(MUTEX_HELD(&db
->db_mtx
));
473 ASSERT(db
->db_buf
== NULL
|| !arc_has_callback(db
->db_buf
));
476 ASSERT(buf
->b_data
!= NULL
);
477 db
->db
.db_data
= buf
->b_data
;
478 if (!arc_released(buf
))
479 arc_set_callback(buf
, dbuf_do_evict
, db
);
480 dbuf_update_data(db
);
483 db
->db
.db_data
= NULL
;
484 if (db
->db_state
!= DB_NOFILL
)
485 db
->db_state
= DB_UNCACHED
;
490 * Loan out an arc_buf for read. Return the loaned arc_buf.
493 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
497 mutex_enter(&db
->db_mtx
);
498 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
499 int blksz
= db
->db
.db_size
;
502 mutex_exit(&db
->db_mtx
);
503 DB_GET_SPA(&spa
, db
);
504 abuf
= arc_loan_buf(spa
, blksz
);
505 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
508 arc_loan_inuse_buf(abuf
, db
);
509 dbuf_set_data(db
, NULL
);
510 mutex_exit(&db
->db_mtx
);
516 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
518 if (dn
->dn_datablkshift
) {
519 return (offset
>> dn
->dn_datablkshift
);
521 ASSERT3U(offset
, <, dn
->dn_datablksz
);
527 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
529 dmu_buf_impl_t
*db
= vdb
;
531 mutex_enter(&db
->db_mtx
);
532 ASSERT3U(db
->db_state
, ==, DB_READ
);
534 * All reads are synchronous, so we must have a hold on the dbuf
536 ASSERT(refcount_count(&db
->db_holds
) > 0);
537 ASSERT(db
->db_buf
== NULL
);
538 ASSERT(db
->db
.db_data
== NULL
);
539 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
540 /* we were freed in flight; disregard any error */
541 arc_release(buf
, db
);
542 bzero(buf
->b_data
, db
->db
.db_size
);
544 db
->db_freed_in_flight
= FALSE
;
545 dbuf_set_data(db
, buf
);
546 db
->db_state
= DB_CACHED
;
547 } else if (zio
== NULL
|| zio
->io_error
== 0) {
548 dbuf_set_data(db
, buf
);
549 db
->db_state
= DB_CACHED
;
551 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
552 ASSERT3P(db
->db_buf
, ==, NULL
);
553 VERIFY(arc_buf_remove_ref(buf
, db
));
554 db
->db_state
= DB_UNCACHED
;
556 cv_broadcast(&db
->db_changed
);
557 dbuf_rele_and_unlock(db
, NULL
);
561 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
566 uint32_t aflags
= ARC_NOWAIT
;
570 ASSERT(!refcount_is_zero(&db
->db_holds
));
571 /* We need the struct_rwlock to prevent db_blkptr from changing. */
572 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
573 ASSERT(MUTEX_HELD(&db
->db_mtx
));
574 ASSERT(db
->db_state
== DB_UNCACHED
);
575 ASSERT(db
->db_buf
== NULL
);
577 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
578 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
580 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
581 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
582 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
583 if (bonuslen
< DN_MAX_BONUSLEN
)
584 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
586 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
588 dbuf_update_data(db
);
589 db
->db_state
= DB_CACHED
;
590 mutex_exit(&db
->db_mtx
);
595 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
596 * processes the delete record and clears the bp while we are waiting
597 * for the dn_mtx (resulting in a "no" from block_freed).
599 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
600 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
601 BP_IS_HOLE(db
->db_blkptr
)))) {
602 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
604 dbuf_set_data(db
, arc_buf_alloc(dn
->dn_objset
->os_spa
,
605 db
->db
.db_size
, db
, type
));
607 bzero(db
->db
.db_data
, db
->db
.db_size
);
608 db
->db_state
= DB_CACHED
;
609 *flags
|= DB_RF_CACHED
;
610 mutex_exit(&db
->db_mtx
);
614 spa
= dn
->dn_objset
->os_spa
;
617 db
->db_state
= DB_READ
;
618 mutex_exit(&db
->db_mtx
);
620 if (DBUF_IS_L2CACHEABLE(db
))
621 aflags
|= ARC_L2CACHE
;
622 if (DBUF_IS_L2COMPRESSIBLE(db
))
623 aflags
|= ARC_L2COMPRESS
;
625 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
626 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
627 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
629 dbuf_add_ref(db
, NULL
);
631 (void) arc_read(zio
, spa
, db
->db_blkptr
,
632 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
633 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
635 if (aflags
& ARC_CACHED
)
636 *flags
|= DB_RF_CACHED
;
640 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
643 int havepzio
= (zio
!= NULL
);
648 * We don't have to hold the mutex to check db_state because it
649 * can't be freed while we have a hold on the buffer.
651 ASSERT(!refcount_is_zero(&db
->db_holds
));
653 if (db
->db_state
== DB_NOFILL
)
658 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
659 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
661 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
662 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
663 DBUF_IS_CACHEABLE(db
);
665 mutex_enter(&db
->db_mtx
);
666 if (db
->db_state
== DB_CACHED
) {
667 mutex_exit(&db
->db_mtx
);
669 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
670 db
->db
.db_size
, TRUE
);
671 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
672 rw_exit(&dn
->dn_struct_rwlock
);
674 } else if (db
->db_state
== DB_UNCACHED
) {
675 spa_t
*spa
= dn
->dn_objset
->os_spa
;
678 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
679 dbuf_read_impl(db
, zio
, &flags
);
681 /* dbuf_read_impl has dropped db_mtx for us */
684 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
685 db
->db
.db_size
, flags
& DB_RF_CACHED
);
687 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
688 rw_exit(&dn
->dn_struct_rwlock
);
694 mutex_exit(&db
->db_mtx
);
696 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
697 db
->db
.db_size
, TRUE
);
698 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
699 rw_exit(&dn
->dn_struct_rwlock
);
702 mutex_enter(&db
->db_mtx
);
703 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
704 while (db
->db_state
== DB_READ
||
705 db
->db_state
== DB_FILL
) {
706 ASSERT(db
->db_state
== DB_READ
||
707 (flags
& DB_RF_HAVESTRUCT
) == 0);
708 cv_wait(&db
->db_changed
, &db
->db_mtx
);
710 if (db
->db_state
== DB_UNCACHED
)
713 mutex_exit(&db
->db_mtx
);
716 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
721 dbuf_noread(dmu_buf_impl_t
*db
)
723 ASSERT(!refcount_is_zero(&db
->db_holds
));
724 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
725 mutex_enter(&db
->db_mtx
);
726 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
727 cv_wait(&db
->db_changed
, &db
->db_mtx
);
728 if (db
->db_state
== DB_UNCACHED
) {
729 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
732 ASSERT(db
->db_buf
== NULL
);
733 ASSERT(db
->db
.db_data
== NULL
);
734 DB_GET_SPA(&spa
, db
);
735 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
736 db
->db_state
= DB_FILL
;
737 } else if (db
->db_state
== DB_NOFILL
) {
738 dbuf_set_data(db
, NULL
);
740 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
742 mutex_exit(&db
->db_mtx
);
746 * This is our just-in-time copy function. It makes a copy of
747 * buffers, that have been modified in a previous transaction
748 * group, before we modify them in the current active group.
750 * This function is used in two places: when we are dirtying a
751 * buffer for the first time in a txg, and when we are freeing
752 * a range in a dnode that includes this buffer.
754 * Note that when we are called from dbuf_free_range() we do
755 * not put a hold on the buffer, we just traverse the active
756 * dbuf list for the dnode.
759 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
761 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
763 ASSERT(MUTEX_HELD(&db
->db_mtx
));
764 ASSERT(db
->db
.db_data
!= NULL
);
765 ASSERT(db
->db_level
== 0);
766 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
769 (dr
->dt
.dl
.dr_data
!=
770 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
774 * If the last dirty record for this dbuf has not yet synced
775 * and its referencing the dbuf data, either:
776 * reset the reference to point to a new copy,
777 * or (if there a no active holders)
778 * just null out the current db_data pointer.
780 ASSERT(dr
->dr_txg
>= txg
- 2);
781 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
782 /* Note that the data bufs here are zio_bufs */
783 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
784 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
785 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
786 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
787 int size
= db
->db
.db_size
;
788 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
791 DB_GET_SPA(&spa
, db
);
792 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
793 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
795 dbuf_set_data(db
, NULL
);
800 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
802 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
803 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
804 uint64_t txg
= dr
->dr_txg
;
806 ASSERT(MUTEX_HELD(&db
->db_mtx
));
807 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
808 ASSERT(db
->db_level
== 0);
810 if (db
->db_blkid
== DMU_BONUS_BLKID
||
811 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
814 ASSERT(db
->db_data_pending
!= dr
);
816 /* free this block */
817 if (!BP_IS_HOLE(bp
)) {
820 DB_GET_SPA(&spa
, db
);
821 zio_free(spa
, txg
, bp
);
823 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
825 * Release the already-written buffer, so we leave it in
826 * a consistent dirty state. Note that all callers are
827 * modifying the buffer, so they will immediately do
828 * another (redundant) arc_release(). Therefore, leave
829 * the buf thawed to save the effort of freezing &
830 * immediately re-thawing it.
832 arc_release(dr
->dt
.dl
.dr_data
, db
);
836 * Evict (if its unreferenced) or clear (if its referenced) any level-0
837 * data blocks in the free range, so that any future readers will find
838 * empty blocks. Also, if we happen accross any level-1 dbufs in the
839 * range that have not already been marked dirty, mark them dirty so
840 * they stay in memory.
843 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
845 dmu_buf_impl_t
*db
, *db_next
;
846 uint64_t txg
= tx
->tx_txg
;
847 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
848 uint64_t first_l1
= start
>> epbs
;
849 uint64_t last_l1
= end
>> epbs
;
851 if (end
> dn
->dn_maxblkid
&& (end
!= DMU_SPILL_BLKID
)) {
852 end
= dn
->dn_maxblkid
;
853 last_l1
= end
>> epbs
;
855 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
856 mutex_enter(&dn
->dn_dbufs_mtx
);
857 for (db
= list_head(&dn
->dn_dbufs
); db
; db
= db_next
) {
858 db_next
= list_next(&dn
->dn_dbufs
, db
);
859 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
861 if (db
->db_level
== 1 &&
862 db
->db_blkid
>= first_l1
&& db
->db_blkid
<= last_l1
) {
863 mutex_enter(&db
->db_mtx
);
864 if (db
->db_last_dirty
&&
865 db
->db_last_dirty
->dr_txg
< txg
) {
866 dbuf_add_ref(db
, FTAG
);
867 mutex_exit(&db
->db_mtx
);
868 dbuf_will_dirty(db
, tx
);
871 mutex_exit(&db
->db_mtx
);
875 if (db
->db_level
!= 0)
877 dprintf_dbuf(db
, "found buf %s\n", "");
878 if (db
->db_blkid
< start
|| db
->db_blkid
> end
)
881 /* found a level 0 buffer in the range */
882 mutex_enter(&db
->db_mtx
);
883 if (dbuf_undirty(db
, tx
)) {
884 /* mutex has been dropped and dbuf destroyed */
888 if (db
->db_state
== DB_UNCACHED
||
889 db
->db_state
== DB_NOFILL
||
890 db
->db_state
== DB_EVICTING
) {
891 ASSERT(db
->db
.db_data
== NULL
);
892 mutex_exit(&db
->db_mtx
);
895 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
896 /* will be handled in dbuf_read_done or dbuf_rele */
897 db
->db_freed_in_flight
= TRUE
;
898 mutex_exit(&db
->db_mtx
);
901 if (refcount_count(&db
->db_holds
) == 0) {
906 /* The dbuf is referenced */
908 if (db
->db_last_dirty
!= NULL
) {
909 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
911 if (dr
->dr_txg
== txg
) {
913 * This buffer is "in-use", re-adjust the file
914 * size to reflect that this buffer may
915 * contain new data when we sync.
917 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
918 db
->db_blkid
> dn
->dn_maxblkid
)
919 dn
->dn_maxblkid
= db
->db_blkid
;
923 * This dbuf is not dirty in the open context.
924 * Either uncache it (if its not referenced in
925 * the open context) or reset its contents to
928 dbuf_fix_old_data(db
, txg
);
931 /* clear the contents if its cached */
932 if (db
->db_state
== DB_CACHED
) {
933 ASSERT(db
->db
.db_data
!= NULL
);
934 arc_release(db
->db_buf
, db
);
935 bzero(db
->db
.db_data
, db
->db
.db_size
);
936 arc_buf_freeze(db
->db_buf
);
939 mutex_exit(&db
->db_mtx
);
941 mutex_exit(&dn
->dn_dbufs_mtx
);
945 dbuf_block_freeable(dmu_buf_impl_t
*db
)
947 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
948 uint64_t birth_txg
= 0;
951 * We don't need any locking to protect db_blkptr:
952 * If it's syncing, then db_last_dirty will be set
953 * so we'll ignore db_blkptr.
955 ASSERT(MUTEX_HELD(&db
->db_mtx
));
956 if (db
->db_last_dirty
)
957 birth_txg
= db
->db_last_dirty
->dr_txg
;
958 else if (db
->db_blkptr
)
959 birth_txg
= db
->db_blkptr
->blk_birth
;
962 * If we don't exist or are in a snapshot, we can't be freed.
963 * Don't pass the bp to dsl_dataset_block_freeable() since we
964 * are holding the db_mtx lock and might deadlock if we are
965 * prefetching a dedup-ed block.
968 return (ds
== NULL
||
969 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
975 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
977 arc_buf_t
*buf
, *obuf
;
978 int osize
= db
->db
.db_size
;
979 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
982 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
987 /* XXX does *this* func really need the lock? */
988 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
991 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
992 * is OK, because there can be no other references to the db
993 * when we are changing its size, so no concurrent DB_FILL can
997 * XXX we should be doing a dbuf_read, checking the return
998 * value and returning that up to our callers
1000 dbuf_will_dirty(db
, tx
);
1002 /* create the data buffer for the new block */
1003 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1005 /* copy old block data to the new block */
1007 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1008 /* zero the remainder */
1010 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1012 mutex_enter(&db
->db_mtx
);
1013 dbuf_set_data(db
, buf
);
1014 VERIFY(arc_buf_remove_ref(obuf
, db
));
1015 db
->db
.db_size
= size
;
1017 if (db
->db_level
== 0) {
1018 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1019 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1021 mutex_exit(&db
->db_mtx
);
1023 dnode_willuse_space(dn
, size
-osize
, tx
);
1028 dbuf_release_bp(dmu_buf_impl_t
*db
)
1032 DB_GET_OBJSET(&os
, db
);
1033 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1034 ASSERT(arc_released(os
->os_phys_buf
) ||
1035 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1036 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1038 (void) arc_release(db
->db_buf
, db
);
1041 dbuf_dirty_record_t
*
1042 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1046 dbuf_dirty_record_t
**drp
, *dr
;
1047 int drop_struct_lock
= FALSE
;
1048 boolean_t do_free_accounting
= B_FALSE
;
1049 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1051 ASSERT(tx
->tx_txg
!= 0);
1052 ASSERT(!refcount_is_zero(&db
->db_holds
));
1053 DMU_TX_DIRTY_BUF(tx
, db
);
1058 * Shouldn't dirty a regular buffer in syncing context. Private
1059 * objects may be dirtied in syncing context, but only if they
1060 * were already pre-dirtied in open context.
1062 ASSERT(!dmu_tx_is_syncing(tx
) ||
1063 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1064 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1065 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1067 * We make this assert for private objects as well, but after we
1068 * check if we're already dirty. They are allowed to re-dirty
1069 * in syncing context.
1071 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1072 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1073 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1075 mutex_enter(&db
->db_mtx
);
1077 * XXX make this true for indirects too? The problem is that
1078 * transactions created with dmu_tx_create_assigned() from
1079 * syncing context don't bother holding ahead.
1081 ASSERT(db
->db_level
!= 0 ||
1082 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1083 db
->db_state
== DB_NOFILL
);
1085 mutex_enter(&dn
->dn_mtx
);
1087 * Don't set dirtyctx to SYNC if we're just modifying this as we
1088 * initialize the objset.
1090 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1091 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1093 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1094 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1095 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_PUSHPAGE
);
1097 mutex_exit(&dn
->dn_mtx
);
1099 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1100 dn
->dn_have_spill
= B_TRUE
;
1103 * If this buffer is already dirty, we're done.
1105 drp
= &db
->db_last_dirty
;
1106 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1107 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1108 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1110 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1113 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1115 * If this buffer has already been written out,
1116 * we now need to reset its state.
1118 dbuf_unoverride(dr
);
1119 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1120 db
->db_state
!= DB_NOFILL
)
1121 arc_buf_thaw(db
->db_buf
);
1123 mutex_exit(&db
->db_mtx
);
1128 * Only valid if not already dirty.
1130 ASSERT(dn
->dn_object
== 0 ||
1131 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1132 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1134 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1135 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1136 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1137 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1138 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1139 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1142 * We should only be dirtying in syncing context if it's the
1143 * mos or we're initializing the os or it's a special object.
1144 * However, we are allowed to dirty in syncing context provided
1145 * we already dirtied it in open context. Hence we must make
1146 * this assertion only if we're not already dirty.
1149 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1150 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1151 ASSERT(db
->db
.db_size
!= 0);
1153 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1155 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1157 * Update the accounting.
1158 * Note: we delay "free accounting" until after we drop
1159 * the db_mtx. This keeps us from grabbing other locks
1160 * (and possibly deadlocking) in bp_get_dsize() while
1161 * also holding the db_mtx.
1163 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1164 do_free_accounting
= dbuf_block_freeable(db
);
1168 * If this buffer is dirty in an old transaction group we need
1169 * to make a copy of it so that the changes we make in this
1170 * transaction group won't leak out when we sync the older txg.
1172 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_PUSHPAGE
);
1173 list_link_init(&dr
->dr_dirty_node
);
1174 if (db
->db_level
== 0) {
1175 void *data_old
= db
->db_buf
;
1177 if (db
->db_state
!= DB_NOFILL
) {
1178 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1179 dbuf_fix_old_data(db
, tx
->tx_txg
);
1180 data_old
= db
->db
.db_data
;
1181 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1183 * Release the data buffer from the cache so
1184 * that we can modify it without impacting
1185 * possible other users of this cached data
1186 * block. Note that indirect blocks and
1187 * private objects are not released until the
1188 * syncing state (since they are only modified
1191 arc_release(db
->db_buf
, db
);
1192 dbuf_fix_old_data(db
, tx
->tx_txg
);
1193 data_old
= db
->db_buf
;
1195 ASSERT(data_old
!= NULL
);
1197 dr
->dt
.dl
.dr_data
= data_old
;
1199 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1200 list_create(&dr
->dt
.di
.dr_children
,
1201 sizeof (dbuf_dirty_record_t
),
1202 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1205 dr
->dr_txg
= tx
->tx_txg
;
1210 * We could have been freed_in_flight between the dbuf_noread
1211 * and dbuf_dirty. We win, as though the dbuf_noread() had
1212 * happened after the free.
1214 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1215 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1216 mutex_enter(&dn
->dn_mtx
);
1217 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1218 mutex_exit(&dn
->dn_mtx
);
1219 db
->db_freed_in_flight
= FALSE
;
1223 * This buffer is now part of this txg
1225 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1226 db
->db_dirtycnt
+= 1;
1227 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1229 mutex_exit(&db
->db_mtx
);
1231 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1232 db
->db_blkid
== DMU_SPILL_BLKID
) {
1233 mutex_enter(&dn
->dn_mtx
);
1234 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1235 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1236 mutex_exit(&dn
->dn_mtx
);
1237 dnode_setdirty(dn
, tx
);
1240 } else if (do_free_accounting
) {
1241 blkptr_t
*bp
= db
->db_blkptr
;
1242 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1243 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1245 * This is only a guess -- if the dbuf is dirty
1246 * in a previous txg, we don't know how much
1247 * space it will use on disk yet. We should
1248 * really have the struct_rwlock to access
1249 * db_blkptr, but since this is just a guess,
1250 * it's OK if we get an odd answer.
1252 ddt_prefetch(os
->os_spa
, bp
);
1253 dnode_willuse_space(dn
, -willfree
, tx
);
1256 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1257 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1258 drop_struct_lock
= TRUE
;
1261 if (db
->db_level
== 0) {
1262 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1263 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1266 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1267 dmu_buf_impl_t
*parent
= db
->db_parent
;
1268 dbuf_dirty_record_t
*di
;
1269 int parent_held
= FALSE
;
1271 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1272 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1274 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1275 db
->db_blkid
>> epbs
, FTAG
);
1276 ASSERT(parent
!= NULL
);
1279 if (drop_struct_lock
)
1280 rw_exit(&dn
->dn_struct_rwlock
);
1281 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1282 di
= dbuf_dirty(parent
, tx
);
1284 dbuf_rele(parent
, FTAG
);
1286 mutex_enter(&db
->db_mtx
);
1287 /* possible race with dbuf_undirty() */
1288 if (db
->db_last_dirty
== dr
||
1289 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1290 mutex_enter(&di
->dt
.di
.dr_mtx
);
1291 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1292 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1293 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1294 mutex_exit(&di
->dt
.di
.dr_mtx
);
1297 mutex_exit(&db
->db_mtx
);
1299 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1300 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1301 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1302 mutex_enter(&dn
->dn_mtx
);
1303 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1304 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1305 mutex_exit(&dn
->dn_mtx
);
1306 if (drop_struct_lock
)
1307 rw_exit(&dn
->dn_struct_rwlock
);
1310 dnode_setdirty(dn
, tx
);
1316 * Return TRUE if this evicted the dbuf.
1319 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1322 uint64_t txg
= tx
->tx_txg
;
1323 dbuf_dirty_record_t
*dr
, **drp
;
1326 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1327 ASSERT0(db
->db_level
);
1328 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1331 * If this buffer is not dirty, we're done.
1333 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1334 if (dr
->dr_txg
<= txg
)
1336 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1338 ASSERT(dr
->dr_txg
== txg
);
1339 ASSERT(dr
->dr_dbuf
== db
);
1345 * Note: This code will probably work even if there are concurrent
1346 * holders, but it is untested in that scenerio, as the ZPL and
1347 * ztest have additional locking (the range locks) that prevents
1348 * that type of concurrent access.
1350 ASSERT3U(refcount_count(&db
->db_holds
), ==, db
->db_dirtycnt
);
1352 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1354 ASSERT(db
->db
.db_size
!= 0);
1356 /* XXX would be nice to fix up dn_towrite_space[] */
1361 * Note that there are three places in dbuf_dirty()
1362 * where this dirty record may be put on a list.
1363 * Make sure to do a list_remove corresponding to
1364 * every one of those list_insert calls.
1366 if (dr
->dr_parent
) {
1367 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1368 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1369 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1370 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1371 db
->db_level
+1 == dn
->dn_nlevels
) {
1372 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1373 mutex_enter(&dn
->dn_mtx
);
1374 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1375 mutex_exit(&dn
->dn_mtx
);
1379 if (db
->db_state
!= DB_NOFILL
) {
1380 dbuf_unoverride(dr
);
1382 ASSERT(db
->db_buf
!= NULL
);
1383 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1384 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1385 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1387 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1389 ASSERT(db
->db_dirtycnt
> 0);
1390 db
->db_dirtycnt
-= 1;
1392 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1393 arc_buf_t
*buf
= db
->db_buf
;
1395 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1396 dbuf_set_data(db
, NULL
);
1397 VERIFY(arc_buf_remove_ref(buf
, db
));
1405 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1407 dbuf_will_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1409 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1411 ASSERT(tx
->tx_txg
!= 0);
1412 ASSERT(!refcount_is_zero(&db
->db_holds
));
1415 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1416 rf
|= DB_RF_HAVESTRUCT
;
1418 (void) dbuf_read(db
, NULL
, rf
);
1419 (void) dbuf_dirty(db
, tx
);
1423 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1425 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1427 db
->db_state
= DB_NOFILL
;
1429 dmu_buf_will_fill(db_fake
, tx
);
1433 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1435 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1437 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1438 ASSERT(tx
->tx_txg
!= 0);
1439 ASSERT(db
->db_level
== 0);
1440 ASSERT(!refcount_is_zero(&db
->db_holds
));
1442 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1443 dmu_tx_private_ok(tx
));
1446 (void) dbuf_dirty(db
, tx
);
1449 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1452 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1454 mutex_enter(&db
->db_mtx
);
1457 if (db
->db_state
== DB_FILL
) {
1458 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1459 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1460 /* we were freed while filling */
1461 /* XXX dbuf_undirty? */
1462 bzero(db
->db
.db_data
, db
->db
.db_size
);
1463 db
->db_freed_in_flight
= FALSE
;
1465 db
->db_state
= DB_CACHED
;
1466 cv_broadcast(&db
->db_changed
);
1468 mutex_exit(&db
->db_mtx
);
1472 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1473 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1476 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1478 ASSERT(!refcount_is_zero(&db
->db_holds
));
1479 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1480 ASSERT(db
->db_level
== 0);
1481 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1482 ASSERT(buf
!= NULL
);
1483 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1484 ASSERT(tx
->tx_txg
!= 0);
1486 arc_return_buf(buf
, db
);
1487 ASSERT(arc_released(buf
));
1489 mutex_enter(&db
->db_mtx
);
1491 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1492 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1494 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1496 if (db
->db_state
== DB_CACHED
&&
1497 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1498 mutex_exit(&db
->db_mtx
);
1499 (void) dbuf_dirty(db
, tx
);
1500 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1501 VERIFY(arc_buf_remove_ref(buf
, db
));
1502 xuio_stat_wbuf_copied();
1506 xuio_stat_wbuf_nocopy();
1507 if (db
->db_state
== DB_CACHED
) {
1508 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1510 ASSERT(db
->db_buf
!= NULL
);
1511 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1512 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1513 if (!arc_released(db
->db_buf
)) {
1514 ASSERT(dr
->dt
.dl
.dr_override_state
==
1516 arc_release(db
->db_buf
, db
);
1518 dr
->dt
.dl
.dr_data
= buf
;
1519 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1520 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1521 arc_release(db
->db_buf
, db
);
1522 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1526 ASSERT(db
->db_buf
== NULL
);
1527 dbuf_set_data(db
, buf
);
1528 db
->db_state
= DB_FILL
;
1529 mutex_exit(&db
->db_mtx
);
1530 (void) dbuf_dirty(db
, tx
);
1531 dbuf_fill_done(db
, tx
);
1535 * "Clear" the contents of this dbuf. This will mark the dbuf
1536 * EVICTING and clear *most* of its references. Unfortunetely,
1537 * when we are not holding the dn_dbufs_mtx, we can't clear the
1538 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1539 * in this case. For callers from the DMU we will usually see:
1540 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1541 * For the arc callback, we will usually see:
1542 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1543 * Sometimes, though, we will get a mix of these two:
1544 * DMU: dbuf_clear()->arc_buf_evict()
1545 * ARC: dbuf_do_evict()->dbuf_destroy()
1548 dbuf_clear(dmu_buf_impl_t
*db
)
1551 dmu_buf_impl_t
*parent
= db
->db_parent
;
1552 dmu_buf_impl_t
*dndb
;
1553 int dbuf_gone
= FALSE
;
1555 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1556 ASSERT(refcount_is_zero(&db
->db_holds
));
1558 dbuf_evict_user(db
);
1560 if (db
->db_state
== DB_CACHED
) {
1561 ASSERT(db
->db
.db_data
!= NULL
);
1562 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1563 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1564 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1566 db
->db
.db_data
= NULL
;
1567 db
->db_state
= DB_UNCACHED
;
1570 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1571 ASSERT(db
->db_data_pending
== NULL
);
1573 db
->db_state
= DB_EVICTING
;
1574 db
->db_blkptr
= NULL
;
1579 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1580 list_remove(&dn
->dn_dbufs
, db
);
1581 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1585 * Decrementing the dbuf count means that the hold corresponding
1586 * to the removed dbuf is no longer discounted in dnode_move(),
1587 * so the dnode cannot be moved until after we release the hold.
1588 * The membar_producer() ensures visibility of the decremented
1589 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1593 db
->db_dnode_handle
= NULL
;
1599 dbuf_gone
= arc_buf_evict(db
->db_buf
);
1602 mutex_exit(&db
->db_mtx
);
1605 * If this dbuf is referenced from an indirect dbuf,
1606 * decrement the ref count on the indirect dbuf.
1608 if (parent
&& parent
!= dndb
)
1609 dbuf_rele(parent
, db
);
1612 __attribute__((always_inline
))
1614 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1615 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
1622 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1624 if (blkid
== DMU_SPILL_BLKID
) {
1625 mutex_enter(&dn
->dn_mtx
);
1626 if (dn
->dn_have_spill
&&
1627 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1628 *bpp
= &dn
->dn_phys
->dn_spill
;
1631 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1632 *parentp
= dn
->dn_dbuf
;
1633 mutex_exit(&dn
->dn_mtx
);
1637 if (dn
->dn_phys
->dn_nlevels
== 0)
1640 nlevels
= dn
->dn_phys
->dn_nlevels
;
1642 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1644 ASSERT3U(level
* epbs
, <, 64);
1645 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1646 if (level
>= nlevels
||
1647 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1648 /* the buffer has no parent yet */
1650 } else if (level
< nlevels
-1) {
1651 /* this block is referenced from an indirect block */
1654 err
= dbuf_hold_impl(dn
, level
+1, blkid
>> epbs
,
1655 fail_sparse
, NULL
, parentp
);
1658 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
1659 blkid
>> epbs
, fail_sparse
, NULL
,
1660 parentp
, dh
->dh_depth
+ 1);
1661 err
= __dbuf_hold_impl(dh
+ 1);
1665 err
= dbuf_read(*parentp
, NULL
,
1666 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1668 dbuf_rele(*parentp
, NULL
);
1672 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1673 (blkid
& ((1ULL << epbs
) - 1));
1676 /* the block is referenced from the dnode */
1677 ASSERT3U(level
, ==, nlevels
-1);
1678 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1679 blkid
< dn
->dn_phys
->dn_nblkptr
);
1681 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1682 *parentp
= dn
->dn_dbuf
;
1684 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1689 static dmu_buf_impl_t
*
1690 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1691 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1693 objset_t
*os
= dn
->dn_objset
;
1694 dmu_buf_impl_t
*db
, *odb
;
1696 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1697 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1699 db
= kmem_cache_alloc(dbuf_cache
, KM_PUSHPAGE
);
1702 db
->db
.db_object
= dn
->dn_object
;
1703 db
->db_level
= level
;
1704 db
->db_blkid
= blkid
;
1705 db
->db_last_dirty
= NULL
;
1706 db
->db_dirtycnt
= 0;
1707 db
->db_dnode_handle
= dn
->dn_handle
;
1708 db
->db_parent
= parent
;
1709 db
->db_blkptr
= blkptr
;
1711 db
->db_user_ptr
= NULL
;
1712 db
->db_user_data_ptr_ptr
= NULL
;
1713 db
->db_evict_func
= NULL
;
1714 db
->db_immediate_evict
= 0;
1715 db
->db_freed_in_flight
= 0;
1717 if (blkid
== DMU_BONUS_BLKID
) {
1718 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1719 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1720 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1721 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1722 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1723 db
->db_state
= DB_UNCACHED
;
1724 /* the bonus dbuf is not placed in the hash table */
1725 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1727 } else if (blkid
== DMU_SPILL_BLKID
) {
1728 db
->db
.db_size
= (blkptr
!= NULL
) ?
1729 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1730 db
->db
.db_offset
= 0;
1733 db
->db_level
? 1<<dn
->dn_indblkshift
: dn
->dn_datablksz
;
1734 db
->db
.db_size
= blocksize
;
1735 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1739 * Hold the dn_dbufs_mtx while we get the new dbuf
1740 * in the hash table *and* added to the dbufs list.
1741 * This prevents a possible deadlock with someone
1742 * trying to look up this dbuf before its added to the
1745 mutex_enter(&dn
->dn_dbufs_mtx
);
1746 db
->db_state
= DB_EVICTING
;
1747 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1748 /* someone else inserted it first */
1749 kmem_cache_free(dbuf_cache
, db
);
1750 mutex_exit(&dn
->dn_dbufs_mtx
);
1753 list_insert_head(&dn
->dn_dbufs
, db
);
1754 db
->db_state
= DB_UNCACHED
;
1755 mutex_exit(&dn
->dn_dbufs_mtx
);
1756 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1758 if (parent
&& parent
!= dn
->dn_dbuf
)
1759 dbuf_add_ref(parent
, db
);
1761 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1762 refcount_count(&dn
->dn_holds
) > 0);
1763 (void) refcount_add(&dn
->dn_holds
, db
);
1764 (void) atomic_inc_32_nv(&dn
->dn_dbufs_count
);
1766 dprintf_dbuf(db
, "db=%p\n", db
);
1772 dbuf_do_evict(void *private)
1774 arc_buf_t
*buf
= private;
1775 dmu_buf_impl_t
*db
= buf
->b_private
;
1777 if (!MUTEX_HELD(&db
->db_mtx
))
1778 mutex_enter(&db
->db_mtx
);
1780 ASSERT(refcount_is_zero(&db
->db_holds
));
1782 if (db
->db_state
!= DB_EVICTING
) {
1783 ASSERT(db
->db_state
== DB_CACHED
);
1788 mutex_exit(&db
->db_mtx
);
1795 dbuf_destroy(dmu_buf_impl_t
*db
)
1797 ASSERT(refcount_is_zero(&db
->db_holds
));
1799 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1801 * If this dbuf is still on the dn_dbufs list,
1802 * remove it from that list.
1804 if (db
->db_dnode_handle
!= NULL
) {
1809 mutex_enter(&dn
->dn_dbufs_mtx
);
1810 list_remove(&dn
->dn_dbufs
, db
);
1811 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1812 mutex_exit(&dn
->dn_dbufs_mtx
);
1815 * Decrementing the dbuf count means that the hold
1816 * corresponding to the removed dbuf is no longer
1817 * discounted in dnode_move(), so the dnode cannot be
1818 * moved until after we release the hold.
1821 db
->db_dnode_handle
= NULL
;
1823 dbuf_hash_remove(db
);
1825 db
->db_parent
= NULL
;
1828 ASSERT(!list_link_active(&db
->db_link
));
1829 ASSERT(db
->db
.db_data
== NULL
);
1830 ASSERT(db
->db_hash_next
== NULL
);
1831 ASSERT(db
->db_blkptr
== NULL
);
1832 ASSERT(db
->db_data_pending
== NULL
);
1834 kmem_cache_free(dbuf_cache
, db
);
1835 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1839 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
)
1841 dmu_buf_impl_t
*db
= NULL
;
1842 blkptr_t
*bp
= NULL
;
1844 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1845 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1847 if (dnode_block_freed(dn
, blkid
))
1850 /* dbuf_find() returns with db_mtx held */
1851 if ((db
= dbuf_find(dn
, 0, blkid
))) {
1853 * This dbuf is already in the cache. We assume that
1854 * it is already CACHED, or else about to be either
1857 mutex_exit(&db
->db_mtx
);
1861 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
, NULL
) == 0) {
1862 if (bp
&& !BP_IS_HOLE(bp
)) {
1863 int priority
= dn
->dn_type
== DMU_OT_DDT_ZAP
?
1864 ZIO_PRIORITY_DDT_PREFETCH
: ZIO_PRIORITY_ASYNC_READ
;
1865 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1866 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1869 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1870 dn
->dn_object
, 0, blkid
);
1872 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1873 bp
, NULL
, NULL
, priority
,
1874 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1878 dbuf_rele(db
, NULL
);
1882 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1885 * Returns with db_holds incremented, and db_mtx not held.
1886 * Note: dn_struct_rwlock must be held.
1889 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
1891 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
1892 dh
->dh_parent
= NULL
;
1894 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
1895 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
1896 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
1898 *(dh
->dh_dbp
) = NULL
;
1900 /* dbuf_find() returns with db_mtx held */
1901 dh
->dh_db
= dbuf_find(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
);
1903 if (dh
->dh_db
== NULL
) {
1906 ASSERT3P(dh
->dh_parent
, ==, NULL
);
1907 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1908 dh
->dh_fail_sparse
, &dh
->dh_parent
,
1910 if (dh
->dh_fail_sparse
) {
1911 if (dh
->dh_err
== 0 && dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
1912 dh
->dh_err
= ENOENT
;
1915 dbuf_rele(dh
->dh_parent
, NULL
);
1916 return (dh
->dh_err
);
1919 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
1920 return (dh
->dh_err
);
1921 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1922 dh
->dh_parent
, dh
->dh_bp
);
1925 if (dh
->dh_db
->db_buf
&& refcount_is_zero(&dh
->dh_db
->db_holds
)) {
1926 arc_buf_add_ref(dh
->dh_db
->db_buf
, dh
->dh_db
);
1927 if (dh
->dh_db
->db_buf
->b_data
== NULL
) {
1928 dbuf_clear(dh
->dh_db
);
1929 if (dh
->dh_parent
) {
1930 dbuf_rele(dh
->dh_parent
, NULL
);
1931 dh
->dh_parent
= NULL
;
1935 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
1938 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
1941 * If this buffer is currently syncing out, and we are are
1942 * still referencing it from db_data, we need to make a copy
1943 * of it in case we decide we want to dirty it again in this txg.
1945 if (dh
->dh_db
->db_level
== 0 &&
1946 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
1947 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
1948 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
1949 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
1951 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
1952 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
1954 dbuf_set_data(dh
->dh_db
,
1955 arc_buf_alloc(dh
->dh_dn
->dn_objset
->os_spa
,
1956 dh
->dh_db
->db
.db_size
, dh
->dh_db
, dh
->dh_type
));
1957 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
1958 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
1962 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
1963 dbuf_update_data(dh
->dh_db
);
1964 DBUF_VERIFY(dh
->dh_db
);
1965 mutex_exit(&dh
->dh_db
->db_mtx
);
1967 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1969 dbuf_rele(dh
->dh_parent
, NULL
);
1971 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
1972 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
1973 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
1974 *(dh
->dh_dbp
) = dh
->dh_db
;
1980 * The following code preserves the recursive function dbuf_hold_impl()
1981 * but moves the local variables AND function arguments to the heap to
1982 * minimize the stack frame size. Enough space is initially allocated
1983 * on the stack for 20 levels of recursion.
1986 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
1987 void *tag
, dmu_buf_impl_t
**dbp
)
1989 struct dbuf_hold_impl_data
*dh
;
1992 dh
= kmem_zalloc(sizeof(struct dbuf_hold_impl_data
) *
1993 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_PUSHPAGE
);
1994 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
, tag
, dbp
, 0);
1996 error
= __dbuf_hold_impl(dh
);
1998 kmem_free(dh
, sizeof(struct dbuf_hold_impl_data
) *
1999 DBUF_HOLD_IMPL_MAX_DEPTH
);
2005 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2006 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2007 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2010 dh
->dh_level
= level
;
2011 dh
->dh_blkid
= blkid
;
2012 dh
->dh_fail_sparse
= fail_sparse
;
2015 dh
->dh_depth
= depth
;
2019 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2022 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
2023 return (err
? NULL
: db
);
2027 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2030 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
2031 return (err
? NULL
: db
);
2035 dbuf_create_bonus(dnode_t
*dn
)
2037 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2039 ASSERT(dn
->dn_bonus
== NULL
);
2040 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2044 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2046 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2049 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2052 blksz
= SPA_MINBLOCKSIZE
;
2053 if (blksz
> SPA_MAXBLOCKSIZE
)
2054 blksz
= SPA_MAXBLOCKSIZE
;
2056 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2060 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2061 dbuf_new_size(db
, blksz
, tx
);
2062 rw_exit(&dn
->dn_struct_rwlock
);
2069 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2071 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2074 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2076 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2078 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2082 * If you call dbuf_rele() you had better not be referencing the dnode handle
2083 * unless you have some other direct or indirect hold on the dnode. (An indirect
2084 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2085 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2086 * dnode's parent dbuf evicting its dnode handles.
2088 #pragma weak dmu_buf_rele = dbuf_rele
2090 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2092 mutex_enter(&db
->db_mtx
);
2093 dbuf_rele_and_unlock(db
, tag
);
2097 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2098 * db_dirtycnt and db_holds to be updated atomically.
2101 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2105 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2109 * Remove the reference to the dbuf before removing its hold on the
2110 * dnode so we can guarantee in dnode_move() that a referenced bonus
2111 * buffer has a corresponding dnode hold.
2113 holds
= refcount_remove(&db
->db_holds
, tag
);
2117 * We can't freeze indirects if there is a possibility that they
2118 * may be modified in the current syncing context.
2120 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2121 arc_buf_freeze(db
->db_buf
);
2123 if (holds
== db
->db_dirtycnt
&&
2124 db
->db_level
== 0 && db
->db_immediate_evict
)
2125 dbuf_evict_user(db
);
2128 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2129 mutex_exit(&db
->db_mtx
);
2132 * If the dnode moves here, we cannot cross this barrier
2133 * until the move completes.
2136 (void) atomic_dec_32_nv(&DB_DNODE(db
)->dn_dbufs_count
);
2139 * The bonus buffer's dnode hold is no longer discounted
2140 * in dnode_move(). The dnode cannot move until after
2143 dnode_rele(DB_DNODE(db
), db
);
2144 } else if (db
->db_buf
== NULL
) {
2146 * This is a special case: we never associated this
2147 * dbuf with any data allocated from the ARC.
2149 ASSERT(db
->db_state
== DB_UNCACHED
||
2150 db
->db_state
== DB_NOFILL
);
2152 } else if (arc_released(db
->db_buf
)) {
2153 arc_buf_t
*buf
= db
->db_buf
;
2155 * This dbuf has anonymous data associated with it.
2157 dbuf_set_data(db
, NULL
);
2158 VERIFY(arc_buf_remove_ref(buf
, db
));
2161 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2164 * A dbuf will be eligible for eviction if either the
2165 * 'primarycache' property is set or a duplicate
2166 * copy of this buffer is already cached in the arc.
2168 * In the case of the 'primarycache' a buffer
2169 * is considered for eviction if it matches the
2170 * criteria set in the property.
2172 * To decide if our buffer is considered a
2173 * duplicate, we must call into the arc to determine
2174 * if multiple buffers are referencing the same
2175 * block on-disk. If so, then we simply evict
2178 if (!DBUF_IS_CACHEABLE(db
) ||
2179 arc_buf_eviction_needed(db
->db_buf
))
2182 mutex_exit(&db
->db_mtx
);
2185 mutex_exit(&db
->db_mtx
);
2189 #pragma weak dmu_buf_refcount = dbuf_refcount
2191 dbuf_refcount(dmu_buf_impl_t
*db
)
2193 return (refcount_count(&db
->db_holds
));
2197 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2198 dmu_buf_evict_func_t
*evict_func
)
2200 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2201 user_data_ptr_ptr
, evict_func
));
2205 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2206 dmu_buf_evict_func_t
*evict_func
)
2208 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2210 db
->db_immediate_evict
= TRUE
;
2211 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2212 user_data_ptr_ptr
, evict_func
));
2216 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
2217 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2219 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2220 ASSERT(db
->db_level
== 0);
2222 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2224 mutex_enter(&db
->db_mtx
);
2226 if (db
->db_user_ptr
== old_user_ptr
) {
2227 db
->db_user_ptr
= user_ptr
;
2228 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2229 db
->db_evict_func
= evict_func
;
2231 dbuf_update_data(db
);
2233 old_user_ptr
= db
->db_user_ptr
;
2236 mutex_exit(&db
->db_mtx
);
2237 return (old_user_ptr
);
2241 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2243 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2244 ASSERT(!refcount_is_zero(&db
->db_holds
));
2246 return (db
->db_user_ptr
);
2250 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2252 boolean_t res
= B_FALSE
;
2253 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2256 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2257 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2263 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2265 /* ASSERT(dmu_tx_is_syncing(tx) */
2266 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2268 if (db
->db_blkptr
!= NULL
)
2271 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2272 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2273 BP_ZERO(db
->db_blkptr
);
2276 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2278 * This buffer was allocated at a time when there was
2279 * no available blkptrs from the dnode, or it was
2280 * inappropriate to hook it in (i.e., nlevels mis-match).
2282 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2283 ASSERT(db
->db_parent
== NULL
);
2284 db
->db_parent
= dn
->dn_dbuf
;
2285 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2288 dmu_buf_impl_t
*parent
= db
->db_parent
;
2289 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2291 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2292 if (parent
== NULL
) {
2293 mutex_exit(&db
->db_mtx
);
2294 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2295 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2296 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2297 rw_exit(&dn
->dn_struct_rwlock
);
2298 mutex_enter(&db
->db_mtx
);
2299 db
->db_parent
= parent
;
2301 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2302 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2307 /* dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2308 * is critical the we not allow the compiler to inline this function in to
2309 * dbuf_sync_list() thereby drastically bloating the stack usage.
2311 noinline
static void
2312 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2314 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2318 ASSERT(dmu_tx_is_syncing(tx
));
2320 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2322 mutex_enter(&db
->db_mtx
);
2324 ASSERT(db
->db_level
> 0);
2327 if (db
->db_buf
== NULL
) {
2328 mutex_exit(&db
->db_mtx
);
2329 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2330 mutex_enter(&db
->db_mtx
);
2332 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2333 ASSERT(db
->db_buf
!= NULL
);
2337 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2338 dbuf_check_blkptr(dn
, db
);
2341 db
->db_data_pending
= dr
;
2343 mutex_exit(&db
->db_mtx
);
2344 dbuf_write(dr
, db
->db_buf
, tx
);
2347 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2348 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2349 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2350 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2354 /* dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2355 * critical the we not allow the compiler to inline this function in to
2356 * dbuf_sync_list() thereby drastically bloating the stack usage.
2358 noinline
static void
2359 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2361 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2362 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2365 uint64_t txg
= tx
->tx_txg
;
2367 ASSERT(dmu_tx_is_syncing(tx
));
2369 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2371 mutex_enter(&db
->db_mtx
);
2373 * To be synced, we must be dirtied. But we
2374 * might have been freed after the dirty.
2376 if (db
->db_state
== DB_UNCACHED
) {
2377 /* This buffer has been freed since it was dirtied */
2378 ASSERT(db
->db
.db_data
== NULL
);
2379 } else if (db
->db_state
== DB_FILL
) {
2380 /* This buffer was freed and is now being re-filled */
2381 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2383 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2390 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2391 mutex_enter(&dn
->dn_mtx
);
2392 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2393 mutex_exit(&dn
->dn_mtx
);
2397 * If this is a bonus buffer, simply copy the bonus data into the
2398 * dnode. It will be written out when the dnode is synced (and it
2399 * will be synced, since it must have been dirty for dbuf_sync to
2402 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2403 dbuf_dirty_record_t
**drp
;
2405 ASSERT(*datap
!= NULL
);
2406 ASSERT0(db
->db_level
);
2407 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2408 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2411 if (*datap
!= db
->db
.db_data
) {
2412 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2413 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2415 db
->db_data_pending
= NULL
;
2416 drp
= &db
->db_last_dirty
;
2418 drp
= &(*drp
)->dr_next
;
2419 ASSERT(dr
->dr_next
== NULL
);
2420 ASSERT(dr
->dr_dbuf
== db
);
2422 if (dr
->dr_dbuf
->db_level
!= 0) {
2423 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2424 list_destroy(&dr
->dt
.di
.dr_children
);
2426 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2427 ASSERT(db
->db_dirtycnt
> 0);
2428 db
->db_dirtycnt
-= 1;
2429 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2436 * This function may have dropped the db_mtx lock allowing a dmu_sync
2437 * operation to sneak in. As a result, we need to ensure that we
2438 * don't check the dr_override_state until we have returned from
2439 * dbuf_check_blkptr.
2441 dbuf_check_blkptr(dn
, db
);
2444 * If this buffer is in the middle of an immediate write,
2445 * wait for the synchronous IO to complete.
2447 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2448 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2449 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2450 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2453 if (db
->db_state
!= DB_NOFILL
&&
2454 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2455 refcount_count(&db
->db_holds
) > 1 &&
2456 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2457 *datap
== db
->db_buf
) {
2459 * If this buffer is currently "in use" (i.e., there
2460 * are active holds and db_data still references it),
2461 * then make a copy before we start the write so that
2462 * any modifications from the open txg will not leak
2465 * NOTE: this copy does not need to be made for
2466 * objects only modified in the syncing context (e.g.
2467 * DNONE_DNODE blocks).
2469 int blksz
= arc_buf_size(*datap
);
2470 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2471 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2472 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2474 db
->db_data_pending
= dr
;
2476 mutex_exit(&db
->db_mtx
);
2478 dbuf_write(dr
, *datap
, tx
);
2480 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2481 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2482 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2486 * Although zio_nowait() does not "wait for an IO", it does
2487 * initiate the IO. If this is an empty write it seems plausible
2488 * that the IO could actually be completed before the nowait
2489 * returns. We need to DB_DNODE_EXIT() first in case
2490 * zio_nowait() invalidates the dbuf.
2493 zio_nowait(dr
->dr_zio
);
2498 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2500 dbuf_dirty_record_t
*dr
;
2502 while ((dr
= list_head(list
))) {
2503 if (dr
->dr_zio
!= NULL
) {
2505 * If we find an already initialized zio then we
2506 * are processing the meta-dnode, and we have finished.
2507 * The dbufs for all dnodes are put back on the list
2508 * during processing, so that we can zio_wait()
2509 * these IOs after initiating all child IOs.
2511 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2512 DMU_META_DNODE_OBJECT
);
2515 list_remove(list
, dr
);
2516 if (dr
->dr_dbuf
->db_level
> 0)
2517 dbuf_sync_indirect(dr
, tx
);
2519 dbuf_sync_leaf(dr
, tx
);
2525 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2527 dmu_buf_impl_t
*db
= vdb
;
2529 blkptr_t
*bp
= zio
->io_bp
;
2530 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2531 spa_t
*spa
= zio
->io_spa
;
2536 ASSERT(db
->db_blkptr
== bp
);
2540 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2541 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2542 zio
->io_prev_space_delta
= delta
;
2544 if (BP_IS_HOLE(bp
)) {
2545 ASSERT(bp
->blk_fill
== 0);
2550 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2551 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2552 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2553 BP_GET_TYPE(bp
) == dn
->dn_bonustype
));
2554 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2556 mutex_enter(&db
->db_mtx
);
2559 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2560 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2561 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2562 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2566 if (db
->db_level
== 0) {
2567 mutex_enter(&dn
->dn_mtx
);
2568 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2569 db
->db_blkid
!= DMU_SPILL_BLKID
)
2570 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2571 mutex_exit(&dn
->dn_mtx
);
2573 if (dn
->dn_type
== DMU_OT_DNODE
) {
2574 dnode_phys_t
*dnp
= db
->db
.db_data
;
2575 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2577 if (dnp
->dn_type
!= DMU_OT_NONE
)
2584 blkptr_t
*ibp
= db
->db
.db_data
;
2585 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2586 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2587 if (BP_IS_HOLE(ibp
))
2589 fill
+= ibp
->blk_fill
;
2594 bp
->blk_fill
= fill
;
2596 mutex_exit(&db
->db_mtx
);
2601 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2603 dmu_buf_impl_t
*db
= vdb
;
2604 blkptr_t
*bp
= zio
->io_bp
;
2605 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2606 uint64_t txg
= zio
->io_txg
;
2607 dbuf_dirty_record_t
**drp
, *dr
;
2609 ASSERT0(zio
->io_error
);
2610 ASSERT(db
->db_blkptr
== bp
);
2612 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
2613 ASSERT(BP_EQUAL(bp
, bp_orig
));
2619 DB_GET_OBJSET(&os
, db
);
2620 ds
= os
->os_dsl_dataset
;
2623 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2624 dsl_dataset_block_born(ds
, bp
, tx
);
2627 mutex_enter(&db
->db_mtx
);
2631 drp
= &db
->db_last_dirty
;
2632 while ((dr
= *drp
) != db
->db_data_pending
)
2634 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2635 ASSERT(dr
->dr_txg
== txg
);
2636 ASSERT(dr
->dr_dbuf
== db
);
2637 ASSERT(dr
->dr_next
== NULL
);
2641 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2646 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2647 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2648 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2653 if (db
->db_level
== 0) {
2654 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2655 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2656 if (db
->db_state
!= DB_NOFILL
) {
2657 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2658 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2660 else if (!arc_released(db
->db_buf
))
2661 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2668 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2669 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2670 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2671 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
2673 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2675 ASSERT3U(dn
->dn_phys
->dn_maxblkid
2676 >> (db
->db_level
* epbs
), >=, db
->db_blkid
);
2677 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2680 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2681 list_destroy(&dr
->dt
.di
.dr_children
);
2683 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2685 cv_broadcast(&db
->db_changed
);
2686 ASSERT(db
->db_dirtycnt
> 0);
2687 db
->db_dirtycnt
-= 1;
2688 db
->db_data_pending
= NULL
;
2689 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2693 dbuf_write_nofill_ready(zio_t
*zio
)
2695 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2699 dbuf_write_nofill_done(zio_t
*zio
)
2701 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2705 dbuf_write_override_ready(zio_t
*zio
)
2707 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2708 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2710 dbuf_write_ready(zio
, NULL
, db
);
2714 dbuf_write_override_done(zio_t
*zio
)
2716 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2717 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2718 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2720 mutex_enter(&db
->db_mtx
);
2721 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2722 if (!BP_IS_HOLE(obp
))
2723 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2724 arc_release(dr
->dt
.dl
.dr_data
, db
);
2726 mutex_exit(&db
->db_mtx
);
2728 dbuf_write_done(zio
, NULL
, db
);
2732 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2734 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2737 dmu_buf_impl_t
*parent
= db
->db_parent
;
2738 uint64_t txg
= tx
->tx_txg
;
2748 if (db
->db_state
!= DB_NOFILL
) {
2749 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2751 * Private object buffers are released here rather
2752 * than in dbuf_dirty() since they are only modified
2753 * in the syncing context and we don't want the
2754 * overhead of making multiple copies of the data.
2756 if (BP_IS_HOLE(db
->db_blkptr
)) {
2759 dbuf_release_bp(db
);
2764 if (parent
!= dn
->dn_dbuf
) {
2765 ASSERT(parent
&& parent
->db_data_pending
);
2766 ASSERT(db
->db_level
== parent
->db_level
-1);
2767 ASSERT(arc_released(parent
->db_buf
));
2768 zio
= parent
->db_data_pending
->dr_zio
;
2770 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2771 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2772 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2773 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2774 ASSERT3P(db
->db_blkptr
, ==,
2775 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2779 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2780 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2783 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2784 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2785 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2787 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2789 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2791 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2794 if (db
->db_level
== 0 && dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2795 ASSERT(db
->db_state
!= DB_NOFILL
);
2796 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2797 db
->db_blkptr
, data
->b_data
, arc_buf_size(data
), &zp
,
2798 dbuf_write_override_ready
, dbuf_write_override_done
, dr
,
2799 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2800 mutex_enter(&db
->db_mtx
);
2801 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2802 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2803 dr
->dt
.dl
.dr_copies
);
2804 mutex_exit(&db
->db_mtx
);
2805 } else if (db
->db_state
== DB_NOFILL
) {
2806 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2807 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2808 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2809 dbuf_write_nofill_ready
, dbuf_write_nofill_done
, db
,
2810 ZIO_PRIORITY_ASYNC_WRITE
,
2811 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2813 ASSERT(arc_released(data
));
2814 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2815 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
),
2816 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
2817 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
2818 ZIO_FLAG_MUSTSUCCEED
, &zb
);
2822 #if defined(_KERNEL) && defined(HAVE_SPL)
2823 EXPORT_SYMBOL(dbuf_find
);
2824 EXPORT_SYMBOL(dbuf_is_metadata
);
2825 EXPORT_SYMBOL(dbuf_evict
);
2826 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
2827 EXPORT_SYMBOL(dbuf_whichblock
);
2828 EXPORT_SYMBOL(dbuf_read
);
2829 EXPORT_SYMBOL(dbuf_unoverride
);
2830 EXPORT_SYMBOL(dbuf_free_range
);
2831 EXPORT_SYMBOL(dbuf_new_size
);
2832 EXPORT_SYMBOL(dbuf_release_bp
);
2833 EXPORT_SYMBOL(dbuf_dirty
);
2834 EXPORT_SYMBOL(dmu_buf_will_dirty
);
2835 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
2836 EXPORT_SYMBOL(dmu_buf_will_fill
);
2837 EXPORT_SYMBOL(dmu_buf_fill_done
);
2838 EXPORT_SYMBOL(dmu_buf_rele
);
2839 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
2840 EXPORT_SYMBOL(dbuf_clear
);
2841 EXPORT_SYMBOL(dbuf_prefetch
);
2842 EXPORT_SYMBOL(dbuf_hold_impl
);
2843 EXPORT_SYMBOL(dbuf_hold
);
2844 EXPORT_SYMBOL(dbuf_hold_level
);
2845 EXPORT_SYMBOL(dbuf_create_bonus
);
2846 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
2847 EXPORT_SYMBOL(dbuf_rm_spill
);
2848 EXPORT_SYMBOL(dbuf_add_ref
);
2849 EXPORT_SYMBOL(dbuf_rele
);
2850 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
2851 EXPORT_SYMBOL(dbuf_refcount
);
2852 EXPORT_SYMBOL(dbuf_sync_list
);
2853 EXPORT_SYMBOL(dmu_buf_set_user
);
2854 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
2855 EXPORT_SYMBOL(dmu_buf_update_user
);
2856 EXPORT_SYMBOL(dmu_buf_get_user
);
2857 EXPORT_SYMBOL(dmu_buf_freeable
);