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, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
49 struct dbuf_hold_impl_data
{
50 /* Function arguments */
54 boolean_t dh_fail_sparse
;
55 boolean_t dh_fail_uncached
;
57 dmu_buf_impl_t
**dh_dbp
;
59 dmu_buf_impl_t
*dh_db
;
60 dmu_buf_impl_t
*dh_parent
;
63 dbuf_dirty_record_t
*dh_dr
;
64 arc_buf_contents_t dh_type
;
68 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
69 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
70 boolean_t fail_uncached
,
71 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
72 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
75 * Number of times that zfs_free_range() took the slow path while doing
76 * a zfs receive. A nonzero value indicates a potential performance problem.
78 uint64_t zfs_free_range_recv_miss
;
80 static void dbuf_destroy(dmu_buf_impl_t
*db
);
81 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
82 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
85 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
86 dmu_buf_evict_func_t
*evict_func
, dmu_buf_t
**clear_on_evict_dbufp
);
90 * Global data structures and functions for the dbuf cache.
92 static kmem_cache_t
*dbuf_cache
;
93 static taskq_t
*dbu_evict_taskq
;
97 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
99 dmu_buf_impl_t
*db
= vdb
;
100 bzero(db
, sizeof (dmu_buf_impl_t
));
102 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
103 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
104 refcount_create(&db
->db_holds
);
111 dbuf_dest(void *vdb
, void *unused
)
113 dmu_buf_impl_t
*db
= vdb
;
114 mutex_destroy(&db
->db_mtx
);
115 cv_destroy(&db
->db_changed
);
116 refcount_destroy(&db
->db_holds
);
120 * dbuf hash table routines
122 static dbuf_hash_table_t dbuf_hash_table
;
124 static uint64_t dbuf_hash_count
;
127 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
129 uintptr_t osv
= (uintptr_t)os
;
130 uint64_t crc
= -1ULL;
132 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
133 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
134 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
135 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
136 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
137 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
138 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
140 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
145 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
147 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
148 ((dbuf)->db.db_object == (obj) && \
149 (dbuf)->db_objset == (os) && \
150 (dbuf)->db_level == (level) && \
151 (dbuf)->db_blkid == (blkid))
154 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
156 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
161 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
162 idx
= hv
& h
->hash_table_mask
;
164 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
165 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
166 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
167 mutex_enter(&db
->db_mtx
);
168 if (db
->db_state
!= DB_EVICTING
) {
169 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
172 mutex_exit(&db
->db_mtx
);
175 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
179 static dmu_buf_impl_t
*
180 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
183 dmu_buf_impl_t
*db
= NULL
;
185 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
186 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
187 if (dn
->dn_bonus
!= NULL
) {
189 mutex_enter(&db
->db_mtx
);
191 rw_exit(&dn
->dn_struct_rwlock
);
192 dnode_rele(dn
, FTAG
);
198 * Insert an entry into the hash table. If there is already an element
199 * equal to elem in the hash table, then the already existing element
200 * will be returned and the new element will not be inserted.
201 * Otherwise returns NULL.
203 static dmu_buf_impl_t
*
204 dbuf_hash_insert(dmu_buf_impl_t
*db
)
206 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
207 objset_t
*os
= db
->db_objset
;
208 uint64_t obj
= db
->db
.db_object
;
209 int level
= db
->db_level
;
210 uint64_t blkid
, hv
, idx
;
213 blkid
= db
->db_blkid
;
214 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
215 idx
= hv
& h
->hash_table_mask
;
217 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
218 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
219 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
220 mutex_enter(&dbf
->db_mtx
);
221 if (dbf
->db_state
!= DB_EVICTING
) {
222 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
225 mutex_exit(&dbf
->db_mtx
);
229 mutex_enter(&db
->db_mtx
);
230 db
->db_hash_next
= h
->hash_table
[idx
];
231 h
->hash_table
[idx
] = db
;
232 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
233 atomic_inc_64(&dbuf_hash_count
);
239 * Remove an entry from the hash table. It must be in the EVICTING state.
242 dbuf_hash_remove(dmu_buf_impl_t
*db
)
244 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
246 dmu_buf_impl_t
*dbf
, **dbp
;
248 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
249 db
->db_level
, db
->db_blkid
);
250 idx
= hv
& h
->hash_table_mask
;
253 * We musn't hold db_mtx to maintain lock ordering:
254 * DBUF_HASH_MUTEX > db_mtx.
256 ASSERT(refcount_is_zero(&db
->db_holds
));
257 ASSERT(db
->db_state
== DB_EVICTING
);
258 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
260 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
261 dbp
= &h
->hash_table
[idx
];
262 while ((dbf
= *dbp
) != db
) {
263 dbp
= &dbf
->db_hash_next
;
266 *dbp
= db
->db_hash_next
;
267 db
->db_hash_next
= NULL
;
268 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
269 atomic_dec_64(&dbuf_hash_count
);
272 static arc_evict_func_t dbuf_do_evict
;
277 } dbvu_verify_type_t
;
280 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
285 if (db
->db_user
== NULL
)
288 /* Only data blocks support the attachment of user data. */
289 ASSERT(db
->db_level
== 0);
291 /* Clients must resolve a dbuf before attaching user data. */
292 ASSERT(db
->db
.db_data
!= NULL
);
293 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
295 holds
= refcount_count(&db
->db_holds
);
296 if (verify_type
== DBVU_EVICTING
) {
298 * Immediate eviction occurs when holds == dirtycnt.
299 * For normal eviction buffers, holds is zero on
300 * eviction, except when dbuf_fix_old_data() calls
301 * dbuf_clear_data(). However, the hold count can grow
302 * during eviction even though db_mtx is held (see
303 * dmu_bonus_hold() for an example), so we can only
304 * test the generic invariant that holds >= dirtycnt.
306 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
308 if (db
->db_user_immediate_evict
== TRUE
)
309 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
311 ASSERT3U(holds
, >, 0);
317 dbuf_evict_user(dmu_buf_impl_t
*db
)
319 dmu_buf_user_t
*dbu
= db
->db_user
;
321 ASSERT(MUTEX_HELD(&db
->db_mtx
));
326 dbuf_verify_user(db
, DBVU_EVICTING
);
330 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
331 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
335 * Invoke the callback from a taskq to avoid lock order reversals
336 * and limit stack depth.
338 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func
, dbu
, 0,
343 dbuf_is_metadata(dmu_buf_impl_t
*db
)
346 * Consider indirect blocks and spill blocks to be meta data.
348 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
351 boolean_t is_metadata
;
354 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
357 return (is_metadata
);
362 dbuf_evict(dmu_buf_impl_t
*db
)
364 ASSERT(MUTEX_HELD(&db
->db_mtx
));
365 ASSERT(db
->db_buf
== NULL
);
366 ASSERT(db
->db_data_pending
== NULL
);
375 uint64_t hsize
= 1ULL << 16;
376 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
380 * The hash table is big enough to fill all of physical memory
381 * with an average block size of zfs_arc_average_blocksize (default 8K).
382 * By default, the table will take up
383 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
385 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
389 h
->hash_table_mask
= hsize
- 1;
390 #if defined(_KERNEL) && defined(HAVE_SPL)
392 * Large allocations which do not require contiguous pages
393 * should be using vmem_alloc() in the linux kernel
395 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
397 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
399 if (h
->hash_table
== NULL
) {
400 /* XXX - we should really return an error instead of assert */
401 ASSERT(hsize
> (1ULL << 10));
406 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
407 sizeof (dmu_buf_impl_t
),
408 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
410 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
411 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
416 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
417 * configuration is not required.
419 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
425 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
428 dbuf_stats_destroy();
430 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
431 mutex_destroy(&h
->hash_mutexes
[i
]);
432 #if defined(_KERNEL) && defined(HAVE_SPL)
434 * Large allocations which do not require contiguous pages
435 * should be using vmem_free() in the linux kernel
437 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
439 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
441 kmem_cache_destroy(dbuf_cache
);
442 taskq_destroy(dbu_evict_taskq
);
451 dbuf_verify(dmu_buf_impl_t
*db
)
454 dbuf_dirty_record_t
*dr
;
456 ASSERT(MUTEX_HELD(&db
->db_mtx
));
458 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
461 ASSERT(db
->db_objset
!= NULL
);
465 ASSERT(db
->db_parent
== NULL
);
466 ASSERT(db
->db_blkptr
== NULL
);
468 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
469 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
470 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
471 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
472 db
->db_blkid
== DMU_SPILL_BLKID
||
473 !avl_is_empty(&dn
->dn_dbufs
));
475 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
477 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
478 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
479 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
481 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
482 ASSERT0(db
->db
.db_offset
);
484 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
487 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
488 ASSERT(dr
->dr_dbuf
== db
);
490 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
491 ASSERT(dr
->dr_dbuf
== db
);
494 * We can't assert that db_size matches dn_datablksz because it
495 * can be momentarily different when another thread is doing
498 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
499 dr
= db
->db_data_pending
;
501 * It should only be modified in syncing context, so
502 * make sure we only have one copy of the data.
504 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
507 /* verify db->db_blkptr */
509 if (db
->db_parent
== dn
->dn_dbuf
) {
510 /* db is pointed to by the dnode */
511 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
512 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
513 ASSERT(db
->db_parent
== NULL
);
515 ASSERT(db
->db_parent
!= NULL
);
516 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
517 ASSERT3P(db
->db_blkptr
, ==,
518 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
520 /* db is pointed to by an indirect block */
521 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
523 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
524 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
527 * dnode_grow_indblksz() can make this fail if we don't
528 * have the struct_rwlock. XXX indblksz no longer
529 * grows. safe to do this now?
531 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
532 ASSERT3P(db
->db_blkptr
, ==,
533 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
534 db
->db_blkid
% epb
));
538 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
539 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
540 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
541 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
543 * If the blkptr isn't set but they have nonzero data,
544 * it had better be dirty, otherwise we'll lose that
545 * data when we evict this buffer.
547 * There is an exception to this rule for indirect blocks; in
548 * this case, if the indirect block is a hole, we fill in a few
549 * fields on each of the child blocks (importantly, birth time)
550 * to prevent hole birth times from being lost when you
551 * partially fill in a hole.
553 if (db
->db_dirtycnt
== 0) {
554 if (db
->db_level
== 0) {
555 uint64_t *buf
= db
->db
.db_data
;
558 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
563 blkptr_t
*bps
= db
->db
.db_data
;
564 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
567 * We want to verify that all the blkptrs in the
568 * indirect block are holes, but we may have
569 * automatically set up a few fields for them.
570 * We iterate through each blkptr and verify
571 * they only have those fields set.
574 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
576 blkptr_t
*bp
= &bps
[i
];
577 ASSERT(ZIO_CHECKSUM_IS_ZERO(
580 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
581 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
582 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
583 ASSERT0(bp
->blk_fill
);
584 ASSERT0(bp
->blk_pad
[0]);
585 ASSERT0(bp
->blk_pad
[1]);
586 ASSERT(!BP_IS_EMBEDDED(bp
));
587 ASSERT(BP_IS_HOLE(bp
));
588 ASSERT0(bp
->blk_phys_birth
);
598 dbuf_clear_data(dmu_buf_impl_t
*db
)
600 ASSERT(MUTEX_HELD(&db
->db_mtx
));
603 db
->db
.db_data
= NULL
;
604 if (db
->db_state
!= DB_NOFILL
)
605 db
->db_state
= DB_UNCACHED
;
609 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
611 ASSERT(MUTEX_HELD(&db
->db_mtx
));
615 ASSERT(buf
->b_data
!= NULL
);
616 db
->db
.db_data
= buf
->b_data
;
617 if (!arc_released(buf
))
618 arc_set_callback(buf
, dbuf_do_evict
, db
);
622 * Loan out an arc_buf for read. Return the loaned arc_buf.
625 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
629 mutex_enter(&db
->db_mtx
);
630 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
631 int blksz
= db
->db
.db_size
;
632 spa_t
*spa
= db
->db_objset
->os_spa
;
634 mutex_exit(&db
->db_mtx
);
635 abuf
= arc_loan_buf(spa
, blksz
);
636 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
639 arc_loan_inuse_buf(abuf
, db
);
641 mutex_exit(&db
->db_mtx
);
647 * Calculate which level n block references the data at the level 0 offset
651 dbuf_whichblock(dnode_t
*dn
, int64_t level
, uint64_t offset
)
653 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
655 * The level n blkid is equal to the level 0 blkid divided by
656 * the number of level 0s in a level n block.
658 * The level 0 blkid is offset >> datablkshift =
659 * offset / 2^datablkshift.
661 * The number of level 0s in a level n is the number of block
662 * pointers in an indirect block, raised to the power of level.
663 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
664 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
666 * Thus, the level n blkid is: offset /
667 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
668 * = offset / 2^(datablkshift + level *
669 * (indblkshift - SPA_BLKPTRSHIFT))
670 * = offset >> (datablkshift + level *
671 * (indblkshift - SPA_BLKPTRSHIFT))
673 return (offset
>> (dn
->dn_datablkshift
+ level
*
674 (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)));
676 ASSERT3U(offset
, <, dn
->dn_datablksz
);
682 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
684 dmu_buf_impl_t
*db
= vdb
;
686 mutex_enter(&db
->db_mtx
);
687 ASSERT3U(db
->db_state
, ==, DB_READ
);
689 * All reads are synchronous, so we must have a hold on the dbuf
691 ASSERT(refcount_count(&db
->db_holds
) > 0);
692 ASSERT(db
->db_buf
== NULL
);
693 ASSERT(db
->db
.db_data
== NULL
);
694 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
695 /* we were freed in flight; disregard any error */
696 arc_release(buf
, db
);
697 bzero(buf
->b_data
, db
->db
.db_size
);
699 db
->db_freed_in_flight
= FALSE
;
700 dbuf_set_data(db
, buf
);
701 db
->db_state
= DB_CACHED
;
702 } else if (zio
== NULL
|| zio
->io_error
== 0) {
703 dbuf_set_data(db
, buf
);
704 db
->db_state
= DB_CACHED
;
706 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
707 ASSERT3P(db
->db_buf
, ==, NULL
);
708 VERIFY(arc_buf_remove_ref(buf
, db
));
709 db
->db_state
= DB_UNCACHED
;
711 cv_broadcast(&db
->db_changed
);
712 dbuf_rele_and_unlock(db
, NULL
);
716 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
720 uint32_t aflags
= ARC_FLAG_NOWAIT
;
725 ASSERT(!refcount_is_zero(&db
->db_holds
));
726 /* We need the struct_rwlock to prevent db_blkptr from changing. */
727 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
728 ASSERT(MUTEX_HELD(&db
->db_mtx
));
729 ASSERT(db
->db_state
== DB_UNCACHED
);
730 ASSERT(db
->db_buf
== NULL
);
732 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
733 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
735 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
736 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
737 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
738 if (bonuslen
< DN_MAX_BONUSLEN
)
739 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
741 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
743 db
->db_state
= DB_CACHED
;
744 mutex_exit(&db
->db_mtx
);
749 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
750 * processes the delete record and clears the bp while we are waiting
751 * for the dn_mtx (resulting in a "no" from block_freed).
753 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
754 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
755 BP_IS_HOLE(db
->db_blkptr
)))) {
756 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
758 dbuf_set_data(db
, arc_buf_alloc(db
->db_objset
->os_spa
,
759 db
->db
.db_size
, db
, type
));
760 bzero(db
->db
.db_data
, db
->db
.db_size
);
762 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
763 BP_IS_HOLE(db
->db_blkptr
) &&
764 db
->db_blkptr
->blk_birth
!= 0) {
765 blkptr_t
*bps
= db
->db
.db_data
;
767 for (i
= 0; i
< ((1 <<
768 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
770 blkptr_t
*bp
= &bps
[i
];
771 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
772 1 << dn
->dn_indblkshift
);
774 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
776 BP_GET_LSIZE(db
->db_blkptr
));
777 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
779 BP_GET_LEVEL(db
->db_blkptr
) - 1);
780 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
784 db
->db_state
= DB_CACHED
;
785 mutex_exit(&db
->db_mtx
);
791 db
->db_state
= DB_READ
;
792 mutex_exit(&db
->db_mtx
);
794 if (DBUF_IS_L2CACHEABLE(db
))
795 aflags
|= ARC_FLAG_L2CACHE
;
796 if (DBUF_IS_L2COMPRESSIBLE(db
))
797 aflags
|= ARC_FLAG_L2COMPRESS
;
799 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
800 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
801 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
803 dbuf_add_ref(db
, NULL
);
805 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
806 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
807 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
810 return (SET_ERROR(err
));
814 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
817 boolean_t havepzio
= (zio
!= NULL
);
822 * We don't have to hold the mutex to check db_state because it
823 * can't be freed while we have a hold on the buffer.
825 ASSERT(!refcount_is_zero(&db
->db_holds
));
827 if (db
->db_state
== DB_NOFILL
)
828 return (SET_ERROR(EIO
));
832 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
833 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
835 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
836 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
837 DBUF_IS_CACHEABLE(db
);
839 mutex_enter(&db
->db_mtx
);
840 if (db
->db_state
== DB_CACHED
) {
841 mutex_exit(&db
->db_mtx
);
843 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1);
844 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
845 rw_exit(&dn
->dn_struct_rwlock
);
847 } else if (db
->db_state
== DB_UNCACHED
) {
848 spa_t
*spa
= dn
->dn_objset
->os_spa
;
851 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
853 err
= dbuf_read_impl(db
, zio
, flags
);
855 /* dbuf_read_impl has dropped db_mtx for us */
857 if (!err
&& prefetch
)
858 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1);
860 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
861 rw_exit(&dn
->dn_struct_rwlock
);
864 if (!err
&& !havepzio
)
868 * Another reader came in while the dbuf was in flight
869 * between UNCACHED and CACHED. Either a writer will finish
870 * writing the buffer (sending the dbuf to CACHED) or the
871 * first reader's request will reach the read_done callback
872 * and send the dbuf to CACHED. Otherwise, a failure
873 * occurred and the dbuf went to UNCACHED.
875 mutex_exit(&db
->db_mtx
);
877 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1);
878 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
879 rw_exit(&dn
->dn_struct_rwlock
);
882 /* Skip the wait per the caller's request. */
883 mutex_enter(&db
->db_mtx
);
884 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
885 while (db
->db_state
== DB_READ
||
886 db
->db_state
== DB_FILL
) {
887 ASSERT(db
->db_state
== DB_READ
||
888 (flags
& DB_RF_HAVESTRUCT
) == 0);
889 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
891 cv_wait(&db
->db_changed
, &db
->db_mtx
);
893 if (db
->db_state
== DB_UNCACHED
)
894 err
= SET_ERROR(EIO
);
896 mutex_exit(&db
->db_mtx
);
899 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
904 dbuf_noread(dmu_buf_impl_t
*db
)
906 ASSERT(!refcount_is_zero(&db
->db_holds
));
907 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
908 mutex_enter(&db
->db_mtx
);
909 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
910 cv_wait(&db
->db_changed
, &db
->db_mtx
);
911 if (db
->db_state
== DB_UNCACHED
) {
912 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
913 spa_t
*spa
= db
->db_objset
->os_spa
;
915 ASSERT(db
->db_buf
== NULL
);
916 ASSERT(db
->db
.db_data
== NULL
);
917 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
918 db
->db_state
= DB_FILL
;
919 } else if (db
->db_state
== DB_NOFILL
) {
922 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
924 mutex_exit(&db
->db_mtx
);
928 * This is our just-in-time copy function. It makes a copy of
929 * buffers, that have been modified in a previous transaction
930 * group, before we modify them in the current active group.
932 * This function is used in two places: when we are dirtying a
933 * buffer for the first time in a txg, and when we are freeing
934 * a range in a dnode that includes this buffer.
936 * Note that when we are called from dbuf_free_range() we do
937 * not put a hold on the buffer, we just traverse the active
938 * dbuf list for the dnode.
941 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
943 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
945 ASSERT(MUTEX_HELD(&db
->db_mtx
));
946 ASSERT(db
->db
.db_data
!= NULL
);
947 ASSERT(db
->db_level
== 0);
948 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
951 (dr
->dt
.dl
.dr_data
!=
952 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
956 * If the last dirty record for this dbuf has not yet synced
957 * and its referencing the dbuf data, either:
958 * reset the reference to point to a new copy,
959 * or (if there a no active holders)
960 * just null out the current db_data pointer.
962 ASSERT(dr
->dr_txg
>= txg
- 2);
963 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
964 /* Note that the data bufs here are zio_bufs */
965 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
966 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
967 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
968 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
969 int size
= db
->db
.db_size
;
970 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
971 spa_t
*spa
= db
->db_objset
->os_spa
;
973 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
974 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
981 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
983 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
984 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
985 uint64_t txg
= dr
->dr_txg
;
987 ASSERT(MUTEX_HELD(&db
->db_mtx
));
988 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
989 ASSERT(db
->db_level
== 0);
991 if (db
->db_blkid
== DMU_BONUS_BLKID
||
992 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
995 ASSERT(db
->db_data_pending
!= dr
);
997 /* free this block */
998 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
999 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1001 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1002 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1005 * Release the already-written buffer, so we leave it in
1006 * a consistent dirty state. Note that all callers are
1007 * modifying the buffer, so they will immediately do
1008 * another (redundant) arc_release(). Therefore, leave
1009 * the buf thawed to save the effort of freezing &
1010 * immediately re-thawing it.
1012 arc_release(dr
->dt
.dl
.dr_data
, db
);
1016 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1017 * data blocks in the free range, so that any future readers will find
1020 * This is a no-op if the dataset is in the middle of an incremental
1021 * receive; see comment below for details.
1024 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1027 dmu_buf_impl_t
*db_search
;
1028 dmu_buf_impl_t
*db
, *db_next
;
1029 uint64_t txg
= tx
->tx_txg
;
1031 boolean_t freespill
=
1032 (start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
);
1034 if (end_blkid
> dn
->dn_maxblkid
&& !freespill
)
1035 end_blkid
= dn
->dn_maxblkid
;
1036 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1038 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1039 db_search
->db_level
= 0;
1040 db_search
->db_blkid
= start_blkid
;
1041 db_search
->db_state
= DB_SEARCH
;
1043 mutex_enter(&dn
->dn_dbufs_mtx
);
1044 if (start_blkid
>= dn
->dn_unlisted_l0_blkid
&& !freespill
) {
1045 /* There can't be any dbufs in this range; no need to search. */
1047 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1048 ASSERT3P(db
, ==, NULL
);
1049 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1050 ASSERT(db
== NULL
|| db
->db_level
> 0);
1053 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
1055 * If we are receiving, we expect there to be no dbufs in
1056 * the range to be freed, because receive modifies each
1057 * block at most once, and in offset order. If this is
1058 * not the case, it can lead to performance problems,
1059 * so note that we unexpectedly took the slow path.
1061 atomic_inc_64(&zfs_free_range_recv_miss
);
1064 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1065 ASSERT3P(db
, ==, NULL
);
1066 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1068 for (; db
!= NULL
; db
= db_next
) {
1069 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1070 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1072 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1075 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1077 /* found a level 0 buffer in the range */
1078 mutex_enter(&db
->db_mtx
);
1079 if (dbuf_undirty(db
, tx
)) {
1080 /* mutex has been dropped and dbuf destroyed */
1084 if (db
->db_state
== DB_UNCACHED
||
1085 db
->db_state
== DB_NOFILL
||
1086 db
->db_state
== DB_EVICTING
) {
1087 ASSERT(db
->db
.db_data
== NULL
);
1088 mutex_exit(&db
->db_mtx
);
1091 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1092 /* will be handled in dbuf_read_done or dbuf_rele */
1093 db
->db_freed_in_flight
= TRUE
;
1094 mutex_exit(&db
->db_mtx
);
1097 if (refcount_count(&db
->db_holds
) == 0) {
1102 /* The dbuf is referenced */
1104 if (db
->db_last_dirty
!= NULL
) {
1105 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1107 if (dr
->dr_txg
== txg
) {
1109 * This buffer is "in-use", re-adjust the file
1110 * size to reflect that this buffer may
1111 * contain new data when we sync.
1113 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1114 db
->db_blkid
> dn
->dn_maxblkid
)
1115 dn
->dn_maxblkid
= db
->db_blkid
;
1116 dbuf_unoverride(dr
);
1119 * This dbuf is not dirty in the open context.
1120 * Either uncache it (if its not referenced in
1121 * the open context) or reset its contents to
1124 dbuf_fix_old_data(db
, txg
);
1127 /* clear the contents if its cached */
1128 if (db
->db_state
== DB_CACHED
) {
1129 ASSERT(db
->db
.db_data
!= NULL
);
1130 arc_release(db
->db_buf
, db
);
1131 bzero(db
->db
.db_data
, db
->db
.db_size
);
1132 arc_buf_freeze(db
->db_buf
);
1135 mutex_exit(&db
->db_mtx
);
1139 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1140 mutex_exit(&dn
->dn_dbufs_mtx
);
1144 dbuf_block_freeable(dmu_buf_impl_t
*db
)
1146 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
1147 uint64_t birth_txg
= 0;
1150 * We don't need any locking to protect db_blkptr:
1151 * If it's syncing, then db_last_dirty will be set
1152 * so we'll ignore db_blkptr.
1154 * This logic ensures that only block births for
1155 * filled blocks are considered.
1157 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1158 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
1159 !BP_IS_HOLE(db
->db_blkptr
))) {
1160 birth_txg
= db
->db_last_dirty
->dr_txg
;
1161 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1162 birth_txg
= db
->db_blkptr
->blk_birth
;
1166 * If this block don't exist or is in a snapshot, it can't be freed.
1167 * Don't pass the bp to dsl_dataset_block_freeable() since we
1168 * are holding the db_mtx lock and might deadlock if we are
1169 * prefetching a dedup-ed block.
1172 return (ds
== NULL
||
1173 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1179 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1181 arc_buf_t
*buf
, *obuf
;
1182 int osize
= db
->db
.db_size
;
1183 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1186 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1191 /* XXX does *this* func really need the lock? */
1192 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1195 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1196 * is OK, because there can be no other references to the db
1197 * when we are changing its size, so no concurrent DB_FILL can
1201 * XXX we should be doing a dbuf_read, checking the return
1202 * value and returning that up to our callers
1204 dmu_buf_will_dirty(&db
->db
, tx
);
1206 /* create the data buffer for the new block */
1207 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1209 /* copy old block data to the new block */
1211 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1212 /* zero the remainder */
1214 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1216 mutex_enter(&db
->db_mtx
);
1217 dbuf_set_data(db
, buf
);
1218 VERIFY(arc_buf_remove_ref(obuf
, db
));
1219 db
->db
.db_size
= size
;
1221 if (db
->db_level
== 0) {
1222 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1223 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1225 mutex_exit(&db
->db_mtx
);
1227 dnode_willuse_space(dn
, size
-osize
, tx
);
1232 dbuf_release_bp(dmu_buf_impl_t
*db
)
1234 ASSERTV(objset_t
*os
= db
->db_objset
);
1236 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1237 ASSERT(arc_released(os
->os_phys_buf
) ||
1238 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1239 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1241 (void) arc_release(db
->db_buf
, db
);
1245 * We already have a dirty record for this TXG, and we are being
1249 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1251 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1253 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1255 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1257 * If this buffer has already been written out,
1258 * we now need to reset its state.
1260 dbuf_unoverride(dr
);
1261 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1262 db
->db_state
!= DB_NOFILL
) {
1263 /* Already released on initial dirty, so just thaw. */
1264 ASSERT(arc_released(db
->db_buf
));
1265 arc_buf_thaw(db
->db_buf
);
1270 dbuf_dirty_record_t
*
1271 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1275 dbuf_dirty_record_t
**drp
, *dr
;
1276 int drop_struct_lock
= FALSE
;
1277 boolean_t do_free_accounting
= B_FALSE
;
1278 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1280 ASSERT(tx
->tx_txg
!= 0);
1281 ASSERT(!refcount_is_zero(&db
->db_holds
));
1282 DMU_TX_DIRTY_BUF(tx
, db
);
1287 * Shouldn't dirty a regular buffer in syncing context. Private
1288 * objects may be dirtied in syncing context, but only if they
1289 * were already pre-dirtied in open context.
1291 ASSERT(!dmu_tx_is_syncing(tx
) ||
1292 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1293 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1294 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1296 * We make this assert for private objects as well, but after we
1297 * check if we're already dirty. They are allowed to re-dirty
1298 * in syncing context.
1300 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1301 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1302 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1304 mutex_enter(&db
->db_mtx
);
1306 * XXX make this true for indirects too? The problem is that
1307 * transactions created with dmu_tx_create_assigned() from
1308 * syncing context don't bother holding ahead.
1310 ASSERT(db
->db_level
!= 0 ||
1311 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1312 db
->db_state
== DB_NOFILL
);
1314 mutex_enter(&dn
->dn_mtx
);
1316 * Don't set dirtyctx to SYNC if we're just modifying this as we
1317 * initialize the objset.
1319 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1320 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1322 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1323 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1324 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1326 mutex_exit(&dn
->dn_mtx
);
1328 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1329 dn
->dn_have_spill
= B_TRUE
;
1332 * If this buffer is already dirty, we're done.
1334 drp
= &db
->db_last_dirty
;
1335 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1336 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1337 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1339 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1343 mutex_exit(&db
->db_mtx
);
1348 * Only valid if not already dirty.
1350 ASSERT(dn
->dn_object
== 0 ||
1351 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1352 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1354 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1355 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1356 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1357 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1358 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1359 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1362 * We should only be dirtying in syncing context if it's the
1363 * mos or we're initializing the os or it's a special object.
1364 * However, we are allowed to dirty in syncing context provided
1365 * we already dirtied it in open context. Hence we must make
1366 * this assertion only if we're not already dirty.
1369 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1370 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1371 ASSERT(db
->db
.db_size
!= 0);
1373 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1375 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1377 * Update the accounting.
1378 * Note: we delay "free accounting" until after we drop
1379 * the db_mtx. This keeps us from grabbing other locks
1380 * (and possibly deadlocking) in bp_get_dsize() while
1381 * also holding the db_mtx.
1383 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1384 do_free_accounting
= dbuf_block_freeable(db
);
1388 * If this buffer is dirty in an old transaction group we need
1389 * to make a copy of it so that the changes we make in this
1390 * transaction group won't leak out when we sync the older txg.
1392 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1393 list_link_init(&dr
->dr_dirty_node
);
1394 if (db
->db_level
== 0) {
1395 void *data_old
= db
->db_buf
;
1397 if (db
->db_state
!= DB_NOFILL
) {
1398 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1399 dbuf_fix_old_data(db
, tx
->tx_txg
);
1400 data_old
= db
->db
.db_data
;
1401 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1403 * Release the data buffer from the cache so
1404 * that we can modify it without impacting
1405 * possible other users of this cached data
1406 * block. Note that indirect blocks and
1407 * private objects are not released until the
1408 * syncing state (since they are only modified
1411 arc_release(db
->db_buf
, db
);
1412 dbuf_fix_old_data(db
, tx
->tx_txg
);
1413 data_old
= db
->db_buf
;
1415 ASSERT(data_old
!= NULL
);
1417 dr
->dt
.dl
.dr_data
= data_old
;
1419 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1420 list_create(&dr
->dt
.di
.dr_children
,
1421 sizeof (dbuf_dirty_record_t
),
1422 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1424 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1425 dr
->dr_accounted
= db
->db
.db_size
;
1427 dr
->dr_txg
= tx
->tx_txg
;
1432 * We could have been freed_in_flight between the dbuf_noread
1433 * and dbuf_dirty. We win, as though the dbuf_noread() had
1434 * happened after the free.
1436 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1437 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1438 mutex_enter(&dn
->dn_mtx
);
1439 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1440 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1443 mutex_exit(&dn
->dn_mtx
);
1444 db
->db_freed_in_flight
= FALSE
;
1448 * This buffer is now part of this txg
1450 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1451 db
->db_dirtycnt
+= 1;
1452 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1454 mutex_exit(&db
->db_mtx
);
1456 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1457 db
->db_blkid
== DMU_SPILL_BLKID
) {
1458 mutex_enter(&dn
->dn_mtx
);
1459 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1460 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1461 mutex_exit(&dn
->dn_mtx
);
1462 dnode_setdirty(dn
, tx
);
1465 } else if (do_free_accounting
) {
1466 blkptr_t
*bp
= db
->db_blkptr
;
1467 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1468 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1470 * This is only a guess -- if the dbuf is dirty
1471 * in a previous txg, we don't know how much
1472 * space it will use on disk yet. We should
1473 * really have the struct_rwlock to access
1474 * db_blkptr, but since this is just a guess,
1475 * it's OK if we get an odd answer.
1477 ddt_prefetch(os
->os_spa
, bp
);
1478 dnode_willuse_space(dn
, -willfree
, tx
);
1481 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1482 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1483 drop_struct_lock
= TRUE
;
1486 if (db
->db_level
== 0) {
1487 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1488 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1491 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1492 dmu_buf_impl_t
*parent
= db
->db_parent
;
1493 dbuf_dirty_record_t
*di
;
1494 int parent_held
= FALSE
;
1496 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1497 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1499 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1500 db
->db_blkid
>> epbs
, FTAG
);
1501 ASSERT(parent
!= NULL
);
1504 if (drop_struct_lock
)
1505 rw_exit(&dn
->dn_struct_rwlock
);
1506 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1507 di
= dbuf_dirty(parent
, tx
);
1509 dbuf_rele(parent
, FTAG
);
1511 mutex_enter(&db
->db_mtx
);
1513 * Since we've dropped the mutex, it's possible that
1514 * dbuf_undirty() might have changed this out from under us.
1516 if (db
->db_last_dirty
== dr
||
1517 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1518 mutex_enter(&di
->dt
.di
.dr_mtx
);
1519 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1520 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1521 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1522 mutex_exit(&di
->dt
.di
.dr_mtx
);
1525 mutex_exit(&db
->db_mtx
);
1527 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1528 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1529 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1530 mutex_enter(&dn
->dn_mtx
);
1531 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1532 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1533 mutex_exit(&dn
->dn_mtx
);
1534 if (drop_struct_lock
)
1535 rw_exit(&dn
->dn_struct_rwlock
);
1538 dnode_setdirty(dn
, tx
);
1544 * Undirty a buffer in the transaction group referenced by the given
1545 * transaction. Return whether this evicted the dbuf.
1548 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1551 uint64_t txg
= tx
->tx_txg
;
1552 dbuf_dirty_record_t
*dr
, **drp
;
1557 * Due to our use of dn_nlevels below, this can only be called
1558 * in open context, unless we are operating on the MOS.
1559 * From syncing context, dn_nlevels may be different from the
1560 * dn_nlevels used when dbuf was dirtied.
1562 ASSERT(db
->db_objset
==
1563 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1564 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1565 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1566 ASSERT0(db
->db_level
);
1567 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1570 * If this buffer is not dirty, we're done.
1572 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1573 if (dr
->dr_txg
<= txg
)
1575 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1577 ASSERT(dr
->dr_txg
== txg
);
1578 ASSERT(dr
->dr_dbuf
== db
);
1583 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1585 ASSERT(db
->db
.db_size
!= 0);
1587 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1588 dr
->dr_accounted
, txg
);
1593 * Note that there are three places in dbuf_dirty()
1594 * where this dirty record may be put on a list.
1595 * Make sure to do a list_remove corresponding to
1596 * every one of those list_insert calls.
1598 if (dr
->dr_parent
) {
1599 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1600 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1601 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1602 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1603 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1604 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1605 mutex_enter(&dn
->dn_mtx
);
1606 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1607 mutex_exit(&dn
->dn_mtx
);
1611 if (db
->db_state
!= DB_NOFILL
) {
1612 dbuf_unoverride(dr
);
1614 ASSERT(db
->db_buf
!= NULL
);
1615 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1616 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1617 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1620 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1622 ASSERT(db
->db_dirtycnt
> 0);
1623 db
->db_dirtycnt
-= 1;
1625 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1626 arc_buf_t
*buf
= db
->db_buf
;
1628 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1629 dbuf_clear_data(db
);
1630 VERIFY(arc_buf_remove_ref(buf
, db
));
1639 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1641 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1642 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1643 dbuf_dirty_record_t
*dr
;
1645 ASSERT(tx
->tx_txg
!= 0);
1646 ASSERT(!refcount_is_zero(&db
->db_holds
));
1649 * Quick check for dirtyness. For already dirty blocks, this
1650 * reduces runtime of this function by >90%, and overall performance
1651 * by 50% for some workloads (e.g. file deletion with indirect blocks
1654 mutex_enter(&db
->db_mtx
);
1656 for (dr
= db
->db_last_dirty
;
1657 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1659 * It's possible that it is already dirty but not cached,
1660 * because there are some calls to dbuf_dirty() that don't
1661 * go through dmu_buf_will_dirty().
1663 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1664 /* This dbuf is already dirty and cached. */
1666 mutex_exit(&db
->db_mtx
);
1670 mutex_exit(&db
->db_mtx
);
1673 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1674 rf
|= DB_RF_HAVESTRUCT
;
1676 (void) dbuf_read(db
, NULL
, rf
);
1677 (void) dbuf_dirty(db
, tx
);
1681 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1683 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1685 db
->db_state
= DB_NOFILL
;
1687 dmu_buf_will_fill(db_fake
, tx
);
1691 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1693 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1695 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1696 ASSERT(tx
->tx_txg
!= 0);
1697 ASSERT(db
->db_level
== 0);
1698 ASSERT(!refcount_is_zero(&db
->db_holds
));
1700 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1701 dmu_tx_private_ok(tx
));
1704 (void) dbuf_dirty(db
, tx
);
1707 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1710 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1712 mutex_enter(&db
->db_mtx
);
1715 if (db
->db_state
== DB_FILL
) {
1716 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1717 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1718 /* we were freed while filling */
1719 /* XXX dbuf_undirty? */
1720 bzero(db
->db
.db_data
, db
->db
.db_size
);
1721 db
->db_freed_in_flight
= FALSE
;
1723 db
->db_state
= DB_CACHED
;
1724 cv_broadcast(&db
->db_changed
);
1726 mutex_exit(&db
->db_mtx
);
1730 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1731 bp_embedded_type_t etype
, enum zio_compress comp
,
1732 int uncompressed_size
, int compressed_size
, int byteorder
,
1735 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1736 struct dirty_leaf
*dl
;
1737 dmu_object_type_t type
;
1739 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
1740 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
1741 SPA_FEATURE_EMBEDDED_DATA
));
1745 type
= DB_DNODE(db
)->dn_type
;
1748 ASSERT0(db
->db_level
);
1749 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1751 dmu_buf_will_not_fill(dbuf
, tx
);
1753 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1754 dl
= &db
->db_last_dirty
->dt
.dl
;
1755 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1756 data
, comp
, uncompressed_size
, compressed_size
);
1757 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1758 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1759 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1760 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1762 dl
->dr_override_state
= DR_OVERRIDDEN
;
1763 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1767 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1768 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1771 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1773 ASSERT(!refcount_is_zero(&db
->db_holds
));
1774 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1775 ASSERT(db
->db_level
== 0);
1776 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1777 ASSERT(buf
!= NULL
);
1778 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1779 ASSERT(tx
->tx_txg
!= 0);
1781 arc_return_buf(buf
, db
);
1782 ASSERT(arc_released(buf
));
1784 mutex_enter(&db
->db_mtx
);
1786 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1787 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1789 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1791 if (db
->db_state
== DB_CACHED
&&
1792 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1793 mutex_exit(&db
->db_mtx
);
1794 (void) dbuf_dirty(db
, tx
);
1795 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1796 VERIFY(arc_buf_remove_ref(buf
, db
));
1797 xuio_stat_wbuf_copied();
1801 xuio_stat_wbuf_nocopy();
1802 if (db
->db_state
== DB_CACHED
) {
1803 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1805 ASSERT(db
->db_buf
!= NULL
);
1806 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1807 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1808 if (!arc_released(db
->db_buf
)) {
1809 ASSERT(dr
->dt
.dl
.dr_override_state
==
1811 arc_release(db
->db_buf
, db
);
1813 dr
->dt
.dl
.dr_data
= buf
;
1814 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1815 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1816 arc_release(db
->db_buf
, db
);
1817 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1821 ASSERT(db
->db_buf
== NULL
);
1822 dbuf_set_data(db
, buf
);
1823 db
->db_state
= DB_FILL
;
1824 mutex_exit(&db
->db_mtx
);
1825 (void) dbuf_dirty(db
, tx
);
1826 dmu_buf_fill_done(&db
->db
, tx
);
1830 * "Clear" the contents of this dbuf. This will mark the dbuf
1831 * EVICTING and clear *most* of its references. Unfortunately,
1832 * when we are not holding the dn_dbufs_mtx, we can't clear the
1833 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1834 * in this case. For callers from the DMU we will usually see:
1835 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1836 * For the arc callback, we will usually see:
1837 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1838 * Sometimes, though, we will get a mix of these two:
1839 * DMU: dbuf_clear()->arc_clear_callback()
1840 * ARC: dbuf_do_evict()->dbuf_destroy()
1842 * This routine will dissociate the dbuf from the arc, by calling
1843 * arc_clear_callback(), but will not evict the data from the ARC.
1846 dbuf_clear(dmu_buf_impl_t
*db
)
1849 dmu_buf_impl_t
*parent
= db
->db_parent
;
1850 dmu_buf_impl_t
*dndb
;
1851 boolean_t dbuf_gone
= B_FALSE
;
1853 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1854 ASSERT(refcount_is_zero(&db
->db_holds
));
1856 dbuf_evict_user(db
);
1858 if (db
->db_state
== DB_CACHED
) {
1859 ASSERT(db
->db
.db_data
!= NULL
);
1860 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1861 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1862 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1864 db
->db
.db_data
= NULL
;
1865 db
->db_state
= DB_UNCACHED
;
1868 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1869 ASSERT(db
->db_data_pending
== NULL
);
1871 db
->db_state
= DB_EVICTING
;
1872 db
->db_blkptr
= NULL
;
1877 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1878 avl_remove(&dn
->dn_dbufs
, db
);
1879 atomic_dec_32(&dn
->dn_dbufs_count
);
1883 * Decrementing the dbuf count means that the hold corresponding
1884 * to the removed dbuf is no longer discounted in dnode_move(),
1885 * so the dnode cannot be moved until after we release the hold.
1886 * The membar_producer() ensures visibility of the decremented
1887 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1891 db
->db_dnode_handle
= NULL
;
1897 dbuf_gone
= arc_clear_callback(db
->db_buf
);
1900 mutex_exit(&db
->db_mtx
);
1903 * If this dbuf is referenced from an indirect dbuf,
1904 * decrement the ref count on the indirect dbuf.
1906 if (parent
&& parent
!= dndb
)
1907 dbuf_rele(parent
, db
);
1911 * Note: While bpp will always be updated if the function returns success,
1912 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
1913 * this happens when the dnode is the meta-dnode, or a userused or groupused
1916 __attribute__((always_inline
))
1918 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1919 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
1926 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1928 if (blkid
== DMU_SPILL_BLKID
) {
1929 mutex_enter(&dn
->dn_mtx
);
1930 if (dn
->dn_have_spill
&&
1931 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1932 *bpp
= &dn
->dn_phys
->dn_spill
;
1935 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1936 *parentp
= dn
->dn_dbuf
;
1937 mutex_exit(&dn
->dn_mtx
);
1941 if (dn
->dn_phys
->dn_nlevels
== 0)
1944 nlevels
= dn
->dn_phys
->dn_nlevels
;
1946 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1948 ASSERT3U(level
* epbs
, <, 64);
1949 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1950 if (level
>= nlevels
||
1951 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1952 /* the buffer has no parent yet */
1953 return (SET_ERROR(ENOENT
));
1954 } else if (level
< nlevels
-1) {
1955 /* this block is referenced from an indirect block */
1958 err
= dbuf_hold_impl(dn
, level
+1,
1959 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
1961 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
1962 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
1963 parentp
, dh
->dh_depth
+ 1);
1964 err
= __dbuf_hold_impl(dh
+ 1);
1968 err
= dbuf_read(*parentp
, NULL
,
1969 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1971 dbuf_rele(*parentp
, NULL
);
1975 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1976 (blkid
& ((1ULL << epbs
) - 1));
1979 /* the block is referenced from the dnode */
1980 ASSERT3U(level
, ==, nlevels
-1);
1981 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1982 blkid
< dn
->dn_phys
->dn_nblkptr
);
1984 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1985 *parentp
= dn
->dn_dbuf
;
1987 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1992 static dmu_buf_impl_t
*
1993 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1994 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1996 objset_t
*os
= dn
->dn_objset
;
1997 dmu_buf_impl_t
*db
, *odb
;
1999 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2000 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2002 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
2005 db
->db
.db_object
= dn
->dn_object
;
2006 db
->db_level
= level
;
2007 db
->db_blkid
= blkid
;
2008 db
->db_last_dirty
= NULL
;
2009 db
->db_dirtycnt
= 0;
2010 db
->db_dnode_handle
= dn
->dn_handle
;
2011 db
->db_parent
= parent
;
2012 db
->db_blkptr
= blkptr
;
2015 db
->db_user_immediate_evict
= FALSE
;
2016 db
->db_freed_in_flight
= FALSE
;
2017 db
->db_pending_evict
= FALSE
;
2019 if (blkid
== DMU_BONUS_BLKID
) {
2020 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2021 db
->db
.db_size
= DN_MAX_BONUSLEN
-
2022 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2023 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2024 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2025 db
->db_state
= DB_UNCACHED
;
2026 /* the bonus dbuf is not placed in the hash table */
2027 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2029 } else if (blkid
== DMU_SPILL_BLKID
) {
2030 db
->db
.db_size
= (blkptr
!= NULL
) ?
2031 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2032 db
->db
.db_offset
= 0;
2035 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2036 db
->db
.db_size
= blocksize
;
2037 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2041 * Hold the dn_dbufs_mtx while we get the new dbuf
2042 * in the hash table *and* added to the dbufs list.
2043 * This prevents a possible deadlock with someone
2044 * trying to look up this dbuf before its added to the
2047 mutex_enter(&dn
->dn_dbufs_mtx
);
2048 db
->db_state
= DB_EVICTING
;
2049 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2050 /* someone else inserted it first */
2051 kmem_cache_free(dbuf_cache
, db
);
2052 mutex_exit(&dn
->dn_dbufs_mtx
);
2055 avl_add(&dn
->dn_dbufs
, db
);
2056 if (db
->db_level
== 0 && db
->db_blkid
>=
2057 dn
->dn_unlisted_l0_blkid
)
2058 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
2059 db
->db_state
= DB_UNCACHED
;
2060 mutex_exit(&dn
->dn_dbufs_mtx
);
2061 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2063 if (parent
&& parent
!= dn
->dn_dbuf
)
2064 dbuf_add_ref(parent
, db
);
2066 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2067 refcount_count(&dn
->dn_holds
) > 0);
2068 (void) refcount_add(&dn
->dn_holds
, db
);
2069 atomic_inc_32(&dn
->dn_dbufs_count
);
2071 dprintf_dbuf(db
, "db=%p\n", db
);
2077 dbuf_do_evict(void *private)
2079 dmu_buf_impl_t
*db
= private;
2081 if (!MUTEX_HELD(&db
->db_mtx
))
2082 mutex_enter(&db
->db_mtx
);
2084 ASSERT(refcount_is_zero(&db
->db_holds
));
2086 if (db
->db_state
!= DB_EVICTING
) {
2087 ASSERT(db
->db_state
== DB_CACHED
);
2092 mutex_exit(&db
->db_mtx
);
2099 dbuf_destroy(dmu_buf_impl_t
*db
)
2101 ASSERT(refcount_is_zero(&db
->db_holds
));
2103 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2105 * If this dbuf is still on the dn_dbufs list,
2106 * remove it from that list.
2108 if (db
->db_dnode_handle
!= NULL
) {
2113 mutex_enter(&dn
->dn_dbufs_mtx
);
2114 avl_remove(&dn
->dn_dbufs
, db
);
2115 atomic_dec_32(&dn
->dn_dbufs_count
);
2116 mutex_exit(&dn
->dn_dbufs_mtx
);
2119 * Decrementing the dbuf count means that the hold
2120 * corresponding to the removed dbuf is no longer
2121 * discounted in dnode_move(), so the dnode cannot be
2122 * moved until after we release the hold.
2125 db
->db_dnode_handle
= NULL
;
2127 dbuf_hash_remove(db
);
2129 db
->db_parent
= NULL
;
2132 ASSERT(db
->db
.db_data
== NULL
);
2133 ASSERT(db
->db_hash_next
== NULL
);
2134 ASSERT(db
->db_blkptr
== NULL
);
2135 ASSERT(db
->db_data_pending
== NULL
);
2137 kmem_cache_free(dbuf_cache
, db
);
2138 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2141 typedef struct dbuf_prefetch_arg
{
2142 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2143 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2144 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2145 int dpa_curlevel
; /* The current level that we're reading */
2146 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2147 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2148 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2149 } dbuf_prefetch_arg_t
;
2152 * Actually issue the prefetch read for the block given.
2155 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2158 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2161 aflags
= dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2163 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2164 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2165 ASSERT(dpa
->dpa_zio
!= NULL
);
2166 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2167 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2168 &aflags
, &dpa
->dpa_zb
);
2172 * Called when an indirect block above our prefetch target is read in. This
2173 * will either read in the next indirect block down the tree or issue the actual
2174 * prefetch if the next block down is our target.
2177 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2179 dbuf_prefetch_arg_t
*dpa
= private;
2183 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2184 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2186 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2187 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2188 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2191 dpa
->dpa_curlevel
--;
2193 nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2194 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2195 bp
= ((blkptr_t
*)abuf
->b_data
) +
2196 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2197 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2198 kmem_free(dpa
, sizeof (*dpa
));
2199 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2200 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2201 dbuf_issue_final_prefetch(dpa
, bp
);
2202 kmem_free(dpa
, sizeof (*dpa
));
2204 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2205 zbookmark_phys_t zb
;
2207 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2209 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2210 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2212 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2213 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2214 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2217 (void) arc_buf_remove_ref(abuf
, private);
2221 * Issue prefetch reads for the given block on the given level. If the indirect
2222 * blocks above that block are not in memory, we will read them in
2223 * asynchronously. As a result, this call never blocks waiting for a read to
2227 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2231 int epbs
, nlevels
, curlevel
;
2235 dbuf_prefetch_arg_t
*dpa
;
2238 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2239 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2241 if (blkid
> dn
->dn_maxblkid
)
2244 if (dnode_block_freed(dn
, blkid
))
2248 * This dnode hasn't been written to disk yet, so there's nothing to
2251 nlevels
= dn
->dn_phys
->dn_nlevels
;
2252 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2255 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2256 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2259 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2262 mutex_exit(&db
->db_mtx
);
2264 * This dbuf already exists. It is either CACHED, or
2265 * (we assume) about to be read or filled.
2271 * Find the closest ancestor (indirect block) of the target block
2272 * that is present in the cache. In this indirect block, we will
2273 * find the bp that is at curlevel, curblkid.
2277 while (curlevel
< nlevels
- 1) {
2278 int parent_level
= curlevel
+ 1;
2279 uint64_t parent_blkid
= curblkid
>> epbs
;
2282 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2283 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2284 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2285 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2286 dbuf_rele(db
, FTAG
);
2290 curlevel
= parent_level
;
2291 curblkid
= parent_blkid
;
2294 if (curlevel
== nlevels
- 1) {
2295 /* No cached indirect blocks found. */
2296 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2297 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2299 if (BP_IS_HOLE(&bp
))
2302 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2304 pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2307 dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2308 ds
= dn
->dn_objset
->os_dsl_dataset
;
2309 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2310 dn
->dn_object
, level
, blkid
);
2311 dpa
->dpa_curlevel
= curlevel
;
2312 dpa
->dpa_prio
= prio
;
2313 dpa
->dpa_aflags
= aflags
;
2314 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2315 dpa
->dpa_epbs
= epbs
;
2319 * If we have the indirect just above us, no need to do the asynchronous
2320 * prefetch chain; we'll just run the last step ourselves. If we're at
2321 * a higher level, though, we want to issue the prefetches for all the
2322 * indirect blocks asynchronously, so we can go on with whatever we were
2325 if (curlevel
== level
) {
2326 ASSERT3U(curblkid
, ==, blkid
);
2327 dbuf_issue_final_prefetch(dpa
, &bp
);
2328 kmem_free(dpa
, sizeof (*dpa
));
2330 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2331 zbookmark_phys_t zb
;
2333 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2334 dn
->dn_object
, curlevel
, curblkid
);
2335 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2336 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2337 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2341 * We use pio here instead of dpa_zio since it's possible that
2342 * dpa may have already been freed.
2347 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2350 * Returns with db_holds incremented, and db_mtx not held.
2351 * Note: dn_struct_rwlock must be held.
2354 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2356 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2357 dh
->dh_parent
= NULL
;
2359 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2360 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2361 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2363 *(dh
->dh_dbp
) = NULL
;
2365 /* dbuf_find() returns with db_mtx held */
2366 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2367 dh
->dh_level
, dh
->dh_blkid
);
2369 if (dh
->dh_db
== NULL
) {
2372 if (dh
->dh_fail_uncached
)
2373 return (SET_ERROR(ENOENT
));
2375 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2376 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2377 dh
->dh_fail_sparse
, &dh
->dh_parent
,
2379 if (dh
->dh_fail_sparse
) {
2380 if (dh
->dh_err
== 0 &&
2381 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2382 dh
->dh_err
= SET_ERROR(ENOENT
);
2385 dbuf_rele(dh
->dh_parent
, NULL
);
2386 return (dh
->dh_err
);
2389 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2390 return (dh
->dh_err
);
2391 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2392 dh
->dh_parent
, dh
->dh_bp
);
2395 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2396 mutex_exit(&dh
->dh_db
->db_mtx
);
2397 return (SET_ERROR(ENOENT
));
2400 if (dh
->dh_db
->db_buf
&& refcount_is_zero(&dh
->dh_db
->db_holds
)) {
2401 arc_buf_add_ref(dh
->dh_db
->db_buf
, dh
->dh_db
);
2402 if (dh
->dh_db
->db_buf
->b_data
== NULL
) {
2403 dbuf_clear(dh
->dh_db
);
2404 if (dh
->dh_parent
) {
2405 dbuf_rele(dh
->dh_parent
, NULL
);
2406 dh
->dh_parent
= NULL
;
2410 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2413 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2416 * If this buffer is currently syncing out, and we are are
2417 * still referencing it from db_data, we need to make a copy
2418 * of it in case we decide we want to dirty it again in this txg.
2420 if (dh
->dh_db
->db_level
== 0 &&
2421 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2422 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2423 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2424 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2426 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2427 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2429 dbuf_set_data(dh
->dh_db
,
2430 arc_buf_alloc(dh
->dh_dn
->dn_objset
->os_spa
,
2431 dh
->dh_db
->db
.db_size
, dh
->dh_db
, dh
->dh_type
));
2432 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2433 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2437 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2438 DBUF_VERIFY(dh
->dh_db
);
2439 mutex_exit(&dh
->dh_db
->db_mtx
);
2441 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2443 dbuf_rele(dh
->dh_parent
, NULL
);
2445 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2446 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2447 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2448 *(dh
->dh_dbp
) = dh
->dh_db
;
2454 * The following code preserves the recursive function dbuf_hold_impl()
2455 * but moves the local variables AND function arguments to the heap to
2456 * minimize the stack frame size. Enough space is initially allocated
2457 * on the stack for 20 levels of recursion.
2460 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2461 boolean_t fail_sparse
, boolean_t fail_uncached
,
2462 void *tag
, dmu_buf_impl_t
**dbp
)
2464 struct dbuf_hold_impl_data
*dh
;
2467 dh
= kmem_zalloc(sizeof (struct dbuf_hold_impl_data
) *
2468 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2469 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2470 fail_uncached
, tag
, dbp
, 0);
2472 error
= __dbuf_hold_impl(dh
);
2474 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2475 DBUF_HOLD_IMPL_MAX_DEPTH
);
2481 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2482 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2483 boolean_t fail_sparse
, boolean_t fail_uncached
,
2484 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2487 dh
->dh_level
= level
;
2488 dh
->dh_blkid
= blkid
;
2490 dh
->dh_fail_sparse
= fail_sparse
;
2491 dh
->dh_fail_uncached
= fail_uncached
;
2495 dh
->dh_depth
= depth
;
2499 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2501 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2505 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2508 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2509 return (err
? NULL
: db
);
2513 dbuf_create_bonus(dnode_t
*dn
)
2515 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2517 ASSERT(dn
->dn_bonus
== NULL
);
2518 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2522 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2524 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2527 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2528 return (SET_ERROR(ENOTSUP
));
2530 blksz
= SPA_MINBLOCKSIZE
;
2531 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2532 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2536 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2537 dbuf_new_size(db
, blksz
, tx
);
2538 rw_exit(&dn
->dn_struct_rwlock
);
2545 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2547 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2550 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2552 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2554 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2557 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2559 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2562 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2563 dmu_buf_impl_t
*found_db
;
2564 boolean_t result
= B_FALSE
;
2566 if (blkid
== DMU_BONUS_BLKID
)
2567 found_db
= dbuf_find_bonus(os
, obj
);
2569 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2571 if (found_db
!= NULL
) {
2572 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2573 (void) refcount_add(&db
->db_holds
, tag
);
2576 mutex_exit(&found_db
->db_mtx
);
2582 * If you call dbuf_rele() you had better not be referencing the dnode handle
2583 * unless you have some other direct or indirect hold on the dnode. (An indirect
2584 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2585 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2586 * dnode's parent dbuf evicting its dnode handles.
2589 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2591 mutex_enter(&db
->db_mtx
);
2592 dbuf_rele_and_unlock(db
, tag
);
2596 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2598 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2602 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2603 * db_dirtycnt and db_holds to be updated atomically.
2606 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2610 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2614 * Remove the reference to the dbuf before removing its hold on the
2615 * dnode so we can guarantee in dnode_move() that a referenced bonus
2616 * buffer has a corresponding dnode hold.
2618 holds
= refcount_remove(&db
->db_holds
, tag
);
2622 * We can't freeze indirects if there is a possibility that they
2623 * may be modified in the current syncing context.
2625 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2626 arc_buf_freeze(db
->db_buf
);
2628 if (holds
== db
->db_dirtycnt
&&
2629 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2630 dbuf_evict_user(db
);
2633 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2635 boolean_t evict_dbuf
= db
->db_pending_evict
;
2638 * If the dnode moves here, we cannot cross this
2639 * barrier until the move completes.
2644 atomic_dec_32(&dn
->dn_dbufs_count
);
2647 * Decrementing the dbuf count means that the bonus
2648 * buffer's dnode hold is no longer discounted in
2649 * dnode_move(). The dnode cannot move until after
2650 * the dnode_rele() below.
2655 * Do not reference db after its lock is dropped.
2656 * Another thread may evict it.
2658 mutex_exit(&db
->db_mtx
);
2661 dnode_evict_bonus(dn
);
2664 } else if (db
->db_buf
== NULL
) {
2666 * This is a special case: we never associated this
2667 * dbuf with any data allocated from the ARC.
2669 ASSERT(db
->db_state
== DB_UNCACHED
||
2670 db
->db_state
== DB_NOFILL
);
2672 } else if (arc_released(db
->db_buf
)) {
2673 arc_buf_t
*buf
= db
->db_buf
;
2675 * This dbuf has anonymous data associated with it.
2677 dbuf_clear_data(db
);
2678 VERIFY(arc_buf_remove_ref(buf
, db
));
2681 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2684 * A dbuf will be eligible for eviction if either the
2685 * 'primarycache' property is set or a duplicate
2686 * copy of this buffer is already cached in the arc.
2688 * In the case of the 'primarycache' a buffer
2689 * is considered for eviction if it matches the
2690 * criteria set in the property.
2692 * To decide if our buffer is considered a
2693 * duplicate, we must call into the arc to determine
2694 * if multiple buffers are referencing the same
2695 * block on-disk. If so, then we simply evict
2698 if (!DBUF_IS_CACHEABLE(db
)) {
2699 if (db
->db_blkptr
!= NULL
&&
2700 !BP_IS_HOLE(db
->db_blkptr
) &&
2701 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2703 dmu_objset_spa(db
->db_objset
);
2704 blkptr_t bp
= *db
->db_blkptr
;
2706 arc_freed(spa
, &bp
);
2710 } else if (db
->db_pending_evict
||
2711 arc_buf_eviction_needed(db
->db_buf
)) {
2714 mutex_exit(&db
->db_mtx
);
2718 mutex_exit(&db
->db_mtx
);
2722 #pragma weak dmu_buf_refcount = dbuf_refcount
2724 dbuf_refcount(dmu_buf_impl_t
*db
)
2726 return (refcount_count(&db
->db_holds
));
2730 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
2731 dmu_buf_user_t
*new_user
)
2733 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2735 mutex_enter(&db
->db_mtx
);
2736 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2737 if (db
->db_user
== old_user
)
2738 db
->db_user
= new_user
;
2740 old_user
= db
->db_user
;
2741 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2742 mutex_exit(&db
->db_mtx
);
2748 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2750 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
2754 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2756 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2758 db
->db_user_immediate_evict
= TRUE
;
2759 return (dmu_buf_set_user(db_fake
, user
));
2763 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2765 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
2769 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2771 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2773 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2774 return (db
->db_user
);
2778 dmu_buf_user_evict_wait()
2780 taskq_wait(dbu_evict_taskq
);
2784 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2786 boolean_t res
= B_FALSE
;
2787 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2790 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2791 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2797 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2799 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2800 return (dbi
->db_blkptr
);
2804 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2806 /* ASSERT(dmu_tx_is_syncing(tx) */
2807 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2809 if (db
->db_blkptr
!= NULL
)
2812 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2813 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2814 BP_ZERO(db
->db_blkptr
);
2817 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2819 * This buffer was allocated at a time when there was
2820 * no available blkptrs from the dnode, or it was
2821 * inappropriate to hook it in (i.e., nlevels mis-match).
2823 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2824 ASSERT(db
->db_parent
== NULL
);
2825 db
->db_parent
= dn
->dn_dbuf
;
2826 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2829 dmu_buf_impl_t
*parent
= db
->db_parent
;
2830 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2832 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2833 if (parent
== NULL
) {
2834 mutex_exit(&db
->db_mtx
);
2835 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2836 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2837 db
->db_blkid
>> epbs
, db
);
2838 rw_exit(&dn
->dn_struct_rwlock
);
2839 mutex_enter(&db
->db_mtx
);
2840 db
->db_parent
= parent
;
2842 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2843 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2849 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2850 * is critical the we not allow the compiler to inline this function in to
2851 * dbuf_sync_list() thereby drastically bloating the stack usage.
2853 noinline
static void
2854 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2856 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2860 ASSERT(dmu_tx_is_syncing(tx
));
2862 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2864 mutex_enter(&db
->db_mtx
);
2866 ASSERT(db
->db_level
> 0);
2869 /* Read the block if it hasn't been read yet. */
2870 if (db
->db_buf
== NULL
) {
2871 mutex_exit(&db
->db_mtx
);
2872 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2873 mutex_enter(&db
->db_mtx
);
2875 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2876 ASSERT(db
->db_buf
!= NULL
);
2880 /* Indirect block size must match what the dnode thinks it is. */
2881 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2882 dbuf_check_blkptr(dn
, db
);
2885 /* Provide the pending dirty record to child dbufs */
2886 db
->db_data_pending
= dr
;
2888 mutex_exit(&db
->db_mtx
);
2889 dbuf_write(dr
, db
->db_buf
, tx
);
2892 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2893 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
2894 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2895 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2900 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2901 * critical the we not allow the compiler to inline this function in to
2902 * dbuf_sync_list() thereby drastically bloating the stack usage.
2904 noinline
static void
2905 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2907 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2908 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2911 uint64_t txg
= tx
->tx_txg
;
2913 ASSERT(dmu_tx_is_syncing(tx
));
2915 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2917 mutex_enter(&db
->db_mtx
);
2919 * To be synced, we must be dirtied. But we
2920 * might have been freed after the dirty.
2922 if (db
->db_state
== DB_UNCACHED
) {
2923 /* This buffer has been freed since it was dirtied */
2924 ASSERT(db
->db
.db_data
== NULL
);
2925 } else if (db
->db_state
== DB_FILL
) {
2926 /* This buffer was freed and is now being re-filled */
2927 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2929 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2936 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2937 mutex_enter(&dn
->dn_mtx
);
2938 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2939 mutex_exit(&dn
->dn_mtx
);
2943 * If this is a bonus buffer, simply copy the bonus data into the
2944 * dnode. It will be written out when the dnode is synced (and it
2945 * will be synced, since it must have been dirty for dbuf_sync to
2948 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2949 dbuf_dirty_record_t
**drp
;
2951 ASSERT(*datap
!= NULL
);
2952 ASSERT0(db
->db_level
);
2953 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2954 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2957 if (*datap
!= db
->db
.db_data
) {
2958 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2959 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2961 db
->db_data_pending
= NULL
;
2962 drp
= &db
->db_last_dirty
;
2964 drp
= &(*drp
)->dr_next
;
2965 ASSERT(dr
->dr_next
== NULL
);
2966 ASSERT(dr
->dr_dbuf
== db
);
2968 if (dr
->dr_dbuf
->db_level
!= 0) {
2969 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2970 list_destroy(&dr
->dt
.di
.dr_children
);
2972 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2973 ASSERT(db
->db_dirtycnt
> 0);
2974 db
->db_dirtycnt
-= 1;
2975 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2982 * This function may have dropped the db_mtx lock allowing a dmu_sync
2983 * operation to sneak in. As a result, we need to ensure that we
2984 * don't check the dr_override_state until we have returned from
2985 * dbuf_check_blkptr.
2987 dbuf_check_blkptr(dn
, db
);
2990 * If this buffer is in the middle of an immediate write,
2991 * wait for the synchronous IO to complete.
2993 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2994 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2995 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2996 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2999 if (db
->db_state
!= DB_NOFILL
&&
3000 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3001 refcount_count(&db
->db_holds
) > 1 &&
3002 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3003 *datap
== db
->db_buf
) {
3005 * If this buffer is currently "in use" (i.e., there
3006 * are active holds and db_data still references it),
3007 * then make a copy before we start the write so that
3008 * any modifications from the open txg will not leak
3011 * NOTE: this copy does not need to be made for
3012 * objects only modified in the syncing context (e.g.
3013 * DNONE_DNODE blocks).
3015 int blksz
= arc_buf_size(*datap
);
3016 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3017 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
3018 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
3020 db
->db_data_pending
= dr
;
3022 mutex_exit(&db
->db_mtx
);
3024 dbuf_write(dr
, *datap
, tx
);
3026 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3027 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3028 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3032 * Although zio_nowait() does not "wait for an IO", it does
3033 * initiate the IO. If this is an empty write it seems plausible
3034 * that the IO could actually be completed before the nowait
3035 * returns. We need to DB_DNODE_EXIT() first in case
3036 * zio_nowait() invalidates the dbuf.
3039 zio_nowait(dr
->dr_zio
);
3044 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3046 dbuf_dirty_record_t
*dr
;
3048 while ((dr
= list_head(list
))) {
3049 if (dr
->dr_zio
!= NULL
) {
3051 * If we find an already initialized zio then we
3052 * are processing the meta-dnode, and we have finished.
3053 * The dbufs for all dnodes are put back on the list
3054 * during processing, so that we can zio_wait()
3055 * these IOs after initiating all child IOs.
3057 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3058 DMU_META_DNODE_OBJECT
);
3061 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3062 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3063 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3065 list_remove(list
, dr
);
3066 if (dr
->dr_dbuf
->db_level
> 0)
3067 dbuf_sync_indirect(dr
, tx
);
3069 dbuf_sync_leaf(dr
, tx
);
3075 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3077 dmu_buf_impl_t
*db
= vdb
;
3079 blkptr_t
*bp
= zio
->io_bp
;
3080 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3081 spa_t
*spa
= zio
->io_spa
;
3086 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3087 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3091 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3092 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3093 zio
->io_prev_space_delta
= delta
;
3095 if (bp
->blk_birth
!= 0) {
3096 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3097 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3098 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3099 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3100 BP_IS_EMBEDDED(bp
));
3101 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3104 mutex_enter(&db
->db_mtx
);
3107 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3108 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3109 ASSERT(!(BP_IS_HOLE(bp
)) &&
3110 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3114 if (db
->db_level
== 0) {
3115 mutex_enter(&dn
->dn_mtx
);
3116 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3117 db
->db_blkid
!= DMU_SPILL_BLKID
)
3118 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3119 mutex_exit(&dn
->dn_mtx
);
3121 if (dn
->dn_type
== DMU_OT_DNODE
) {
3122 dnode_phys_t
*dnp
= db
->db
.db_data
;
3123 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
3125 if (dnp
->dn_type
!= DMU_OT_NONE
)
3129 if (BP_IS_HOLE(bp
)) {
3136 blkptr_t
*ibp
= db
->db
.db_data
;
3137 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3138 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3139 if (BP_IS_HOLE(ibp
))
3141 fill
+= BP_GET_FILL(ibp
);
3146 if (!BP_IS_EMBEDDED(bp
))
3147 bp
->blk_fill
= fill
;
3149 mutex_exit(&db
->db_mtx
);
3151 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3152 *db
->db_blkptr
= *bp
;
3153 rw_exit(&dn
->dn_struct_rwlock
);
3158 * This function gets called just prior to running through the compression
3159 * stage of the zio pipeline. If we're an indirect block comprised of only
3160 * holes, then we want this indirect to be compressed away to a hole. In
3161 * order to do that we must zero out any information about the holes that
3162 * this indirect points to prior to before we try to compress it.
3165 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3167 dmu_buf_impl_t
*db
= vdb
;
3173 ASSERT3U(db
->db_level
, >, 0);
3176 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3178 /* Determine if all our children are holes */
3179 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++) {
3180 if (!BP_IS_HOLE(bp
))
3185 * If all the children are holes, then zero them all out so that
3186 * we may get compressed away.
3188 if (i
== 1 << epbs
) {
3189 /* didn't find any non-holes */
3190 bzero(db
->db
.db_data
, db
->db
.db_size
);
3196 * The SPA will call this callback several times for each zio - once
3197 * for every physical child i/o (zio->io_phys_children times). This
3198 * allows the DMU to monitor the progress of each logical i/o. For example,
3199 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3200 * block. There may be a long delay before all copies/fragments are completed,
3201 * so this callback allows us to retire dirty space gradually, as the physical
3206 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3208 dmu_buf_impl_t
*db
= arg
;
3209 objset_t
*os
= db
->db_objset
;
3210 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3211 dbuf_dirty_record_t
*dr
;
3214 dr
= db
->db_data_pending
;
3215 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3218 * The callback will be called io_phys_children times. Retire one
3219 * portion of our dirty space each time we are called. Any rounding
3220 * error will be cleaned up by dsl_pool_sync()'s call to
3221 * dsl_pool_undirty_space().
3223 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3224 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3229 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3231 dmu_buf_impl_t
*db
= vdb
;
3232 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3233 blkptr_t
*bp
= db
->db_blkptr
;
3234 objset_t
*os
= db
->db_objset
;
3235 dmu_tx_t
*tx
= os
->os_synctx
;
3236 dbuf_dirty_record_t
**drp
, *dr
;
3238 ASSERT0(zio
->io_error
);
3239 ASSERT(db
->db_blkptr
== bp
);
3242 * For nopwrites and rewrites we ensure that the bp matches our
3243 * original and bypass all the accounting.
3245 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3246 ASSERT(BP_EQUAL(bp
, bp_orig
));
3248 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3249 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3250 dsl_dataset_block_born(ds
, bp
, tx
);
3253 mutex_enter(&db
->db_mtx
);
3257 drp
= &db
->db_last_dirty
;
3258 while ((dr
= *drp
) != db
->db_data_pending
)
3260 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3261 ASSERT(dr
->dr_dbuf
== db
);
3262 ASSERT(dr
->dr_next
== NULL
);
3266 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3271 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3272 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3273 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3278 if (db
->db_level
== 0) {
3279 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3280 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3281 if (db
->db_state
!= DB_NOFILL
) {
3282 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3283 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
3285 else if (!arc_released(db
->db_buf
))
3286 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
3293 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3294 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3295 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3296 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3298 ASSERT3U(db
->db_blkid
, <=,
3299 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3300 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3302 if (!arc_released(db
->db_buf
))
3303 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
3306 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3307 list_destroy(&dr
->dt
.di
.dr_children
);
3309 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3311 cv_broadcast(&db
->db_changed
);
3312 ASSERT(db
->db_dirtycnt
> 0);
3313 db
->db_dirtycnt
-= 1;
3314 db
->db_data_pending
= NULL
;
3315 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3319 dbuf_write_nofill_ready(zio_t
*zio
)
3321 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3325 dbuf_write_nofill_done(zio_t
*zio
)
3327 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3331 dbuf_write_override_ready(zio_t
*zio
)
3333 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3334 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3336 dbuf_write_ready(zio
, NULL
, db
);
3340 dbuf_write_override_done(zio_t
*zio
)
3342 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3343 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3344 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3346 mutex_enter(&db
->db_mtx
);
3347 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3348 if (!BP_IS_HOLE(obp
))
3349 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3350 arc_release(dr
->dt
.dl
.dr_data
, db
);
3352 mutex_exit(&db
->db_mtx
);
3354 dbuf_write_done(zio
, NULL
, db
);
3357 /* Issue I/O to commit a dirty buffer to disk. */
3359 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3361 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3364 dmu_buf_impl_t
*parent
= db
->db_parent
;
3365 uint64_t txg
= tx
->tx_txg
;
3366 zbookmark_phys_t zb
;
3371 ASSERT(dmu_tx_is_syncing(tx
));
3377 if (db
->db_state
!= DB_NOFILL
) {
3378 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3380 * Private object buffers are released here rather
3381 * than in dbuf_dirty() since they are only modified
3382 * in the syncing context and we don't want the
3383 * overhead of making multiple copies of the data.
3385 if (BP_IS_HOLE(db
->db_blkptr
)) {
3388 dbuf_release_bp(db
);
3393 if (parent
!= dn
->dn_dbuf
) {
3394 /* Our parent is an indirect block. */
3395 /* We have a dirty parent that has been scheduled for write. */
3396 ASSERT(parent
&& parent
->db_data_pending
);
3397 /* Our parent's buffer is one level closer to the dnode. */
3398 ASSERT(db
->db_level
== parent
->db_level
-1);
3400 * We're about to modify our parent's db_data by modifying
3401 * our block pointer, so the parent must be released.
3403 ASSERT(arc_released(parent
->db_buf
));
3404 zio
= parent
->db_data_pending
->dr_zio
;
3406 /* Our parent is the dnode itself. */
3407 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3408 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3409 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3410 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3411 ASSERT3P(db
->db_blkptr
, ==,
3412 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3416 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3417 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3420 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3421 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3422 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3424 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3426 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3428 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3432 * We copy the blkptr now (rather than when we instantiate the dirty
3433 * record), because its value can change between open context and
3434 * syncing context. We do not need to hold dn_struct_rwlock to read
3435 * db_blkptr because we are in syncing context.
3437 dr
->dr_bp_copy
= *db
->db_blkptr
;
3439 if (db
->db_level
== 0 &&
3440 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3442 * The BP for this block has been provided by open context
3443 * (by dmu_sync() or dmu_buf_write_embedded()).
3445 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
3447 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3448 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, &zp
,
3449 dbuf_write_override_ready
, NULL
, NULL
,
3450 dbuf_write_override_done
,
3451 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3452 mutex_enter(&db
->db_mtx
);
3453 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3454 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3455 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3456 mutex_exit(&db
->db_mtx
);
3457 } else if (db
->db_state
== DB_NOFILL
) {
3458 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
3459 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3460 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, &zp
,
3461 dbuf_write_nofill_ready
, NULL
, NULL
,
3462 dbuf_write_nofill_done
, db
,
3463 ZIO_PRIORITY_ASYNC_WRITE
,
3464 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3466 arc_done_func_t
*children_ready_cb
= NULL
;
3467 ASSERT(arc_released(data
));
3470 * For indirect blocks, we want to setup the children
3471 * ready callback so that we can properly handle an indirect
3472 * block that only contains holes.
3474 if (db
->db_level
!= 0)
3475 children_ready_cb
= dbuf_write_children_ready
;
3477 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3478 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3479 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
3481 dbuf_write_physdone
, dbuf_write_done
, db
,
3482 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3486 #if defined(_KERNEL) && defined(HAVE_SPL)
3487 EXPORT_SYMBOL(dbuf_find
);
3488 EXPORT_SYMBOL(dbuf_is_metadata
);
3489 EXPORT_SYMBOL(dbuf_evict
);
3490 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3491 EXPORT_SYMBOL(dbuf_whichblock
);
3492 EXPORT_SYMBOL(dbuf_read
);
3493 EXPORT_SYMBOL(dbuf_unoverride
);
3494 EXPORT_SYMBOL(dbuf_free_range
);
3495 EXPORT_SYMBOL(dbuf_new_size
);
3496 EXPORT_SYMBOL(dbuf_release_bp
);
3497 EXPORT_SYMBOL(dbuf_dirty
);
3498 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3499 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3500 EXPORT_SYMBOL(dmu_buf_will_fill
);
3501 EXPORT_SYMBOL(dmu_buf_fill_done
);
3502 EXPORT_SYMBOL(dmu_buf_rele
);
3503 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3504 EXPORT_SYMBOL(dbuf_clear
);
3505 EXPORT_SYMBOL(dbuf_prefetch
);
3506 EXPORT_SYMBOL(dbuf_hold_impl
);
3507 EXPORT_SYMBOL(dbuf_hold
);
3508 EXPORT_SYMBOL(dbuf_hold_level
);
3509 EXPORT_SYMBOL(dbuf_create_bonus
);
3510 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3511 EXPORT_SYMBOL(dbuf_rm_spill
);
3512 EXPORT_SYMBOL(dbuf_add_ref
);
3513 EXPORT_SYMBOL(dbuf_rele
);
3514 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3515 EXPORT_SYMBOL(dbuf_refcount
);
3516 EXPORT_SYMBOL(dbuf_sync_list
);
3517 EXPORT_SYMBOL(dmu_buf_set_user
);
3518 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3519 EXPORT_SYMBOL(dmu_buf_get_user
);
3520 EXPORT_SYMBOL(dmu_buf_freeable
);
3521 EXPORT_SYMBOL(dmu_buf_get_blkptr
);