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) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 #include <sys/zfs_context.h>
31 #include <sys/dmu_send.h>
32 #include <sys/dmu_impl.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dmu_tx.h>
40 #include <sys/dmu_zfetch.h>
42 #include <sys/sa_impl.h>
44 struct dbuf_hold_impl_data
{
45 /* Function arguments */
51 dmu_buf_impl_t
**dh_dbp
;
53 dmu_buf_impl_t
*dh_db
;
54 dmu_buf_impl_t
*dh_parent
;
57 dbuf_dirty_record_t
*dh_dr
;
58 arc_buf_contents_t dh_type
;
62 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
63 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
64 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
65 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
68 * Number of times that zfs_free_range() took the slow path while doing
69 * a zfs receive. A nonzero value indicates a potential performance problem.
71 uint64_t zfs_free_range_recv_miss
;
73 static void dbuf_destroy(dmu_buf_impl_t
*db
);
74 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
75 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
78 * Global data structures and functions for the dbuf cache.
80 static kmem_cache_t
*dbuf_cache
;
84 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
86 dmu_buf_impl_t
*db
= vdb
;
87 bzero(db
, sizeof (dmu_buf_impl_t
));
89 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
90 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
91 refcount_create(&db
->db_holds
);
92 list_link_init(&db
->db_link
);
98 dbuf_dest(void *vdb
, void *unused
)
100 dmu_buf_impl_t
*db
= vdb
;
101 mutex_destroy(&db
->db_mtx
);
102 cv_destroy(&db
->db_changed
);
103 refcount_destroy(&db
->db_holds
);
107 * dbuf hash table routines
109 static dbuf_hash_table_t dbuf_hash_table
;
111 static uint64_t dbuf_hash_count
;
114 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
116 uintptr_t osv
= (uintptr_t)os
;
117 uint64_t crc
= -1ULL;
119 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
120 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
121 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
122 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
123 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
124 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
125 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
127 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
132 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
134 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
135 ((dbuf)->db.db_object == (obj) && \
136 (dbuf)->db_objset == (os) && \
137 (dbuf)->db_level == (level) && \
138 (dbuf)->db_blkid == (blkid))
141 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
143 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
144 objset_t
*os
= dn
->dn_objset
;
151 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
152 idx
= hv
& h
->hash_table_mask
;
154 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
155 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
156 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
157 mutex_enter(&db
->db_mtx
);
158 if (db
->db_state
!= DB_EVICTING
) {
159 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
162 mutex_exit(&db
->db_mtx
);
165 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
170 * Insert an entry into the hash table. If there is already an element
171 * equal to elem in the hash table, then the already existing element
172 * will be returned and the new element will not be inserted.
173 * Otherwise returns NULL.
175 static dmu_buf_impl_t
*
176 dbuf_hash_insert(dmu_buf_impl_t
*db
)
178 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
179 objset_t
*os
= db
->db_objset
;
180 uint64_t obj
= db
->db
.db_object
;
181 int level
= db
->db_level
;
182 uint64_t blkid
, hv
, idx
;
185 blkid
= db
->db_blkid
;
186 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
187 idx
= hv
& h
->hash_table_mask
;
189 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
190 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
191 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
192 mutex_enter(&dbf
->db_mtx
);
193 if (dbf
->db_state
!= DB_EVICTING
) {
194 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
197 mutex_exit(&dbf
->db_mtx
);
201 mutex_enter(&db
->db_mtx
);
202 db
->db_hash_next
= h
->hash_table
[idx
];
203 h
->hash_table
[idx
] = db
;
204 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
205 atomic_add_64(&dbuf_hash_count
, 1);
211 * Remove an entry from the hash table. This operation will
212 * fail if there are any existing holds on the db.
215 dbuf_hash_remove(dmu_buf_impl_t
*db
)
217 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
219 dmu_buf_impl_t
*dbf
, **dbp
;
221 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
222 db
->db_level
, db
->db_blkid
);
223 idx
= hv
& h
->hash_table_mask
;
226 * We musn't hold db_mtx to maintin lock ordering:
227 * DBUF_HASH_MUTEX > db_mtx.
229 ASSERT(refcount_is_zero(&db
->db_holds
));
230 ASSERT(db
->db_state
== DB_EVICTING
);
231 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
233 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
234 dbp
= &h
->hash_table
[idx
];
235 while ((dbf
= *dbp
) != db
) {
236 dbp
= &dbf
->db_hash_next
;
239 *dbp
= db
->db_hash_next
;
240 db
->db_hash_next
= NULL
;
241 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
242 atomic_add_64(&dbuf_hash_count
, -1);
245 static arc_evict_func_t dbuf_do_evict
;
248 dbuf_evict_user(dmu_buf_impl_t
*db
)
250 ASSERT(MUTEX_HELD(&db
->db_mtx
));
252 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
255 if (db
->db_user_data_ptr_ptr
)
256 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
257 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
258 db
->db_user_ptr
= NULL
;
259 db
->db_user_data_ptr_ptr
= NULL
;
260 db
->db_evict_func
= NULL
;
264 dbuf_is_metadata(dmu_buf_impl_t
*db
)
266 if (db
->db_level
> 0) {
269 boolean_t is_metadata
;
272 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
275 return (is_metadata
);
280 dbuf_evict(dmu_buf_impl_t
*db
)
282 ASSERT(MUTEX_HELD(&db
->db_mtx
));
283 ASSERT(db
->db_buf
== NULL
);
284 ASSERT(db
->db_data_pending
== NULL
);
293 uint64_t hsize
= 1ULL << 16;
294 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
298 * The hash table is big enough to fill all of physical memory
299 * with an average 4K block size. The table will take up
300 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
302 while (hsize
* 4096 < physmem
* PAGESIZE
)
306 h
->hash_table_mask
= hsize
- 1;
307 #if defined(_KERNEL) && defined(HAVE_SPL)
309 * Large allocations which do not require contiguous pages
310 * should be using vmem_alloc() in the linux kernel
312 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_PUSHPAGE
);
314 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
316 if (h
->hash_table
== NULL
) {
317 /* XXX - we should really return an error instead of assert */
318 ASSERT(hsize
> (1ULL << 10));
323 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
324 sizeof (dmu_buf_impl_t
),
325 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
327 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
328 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
336 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
339 dbuf_stats_destroy();
341 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
342 mutex_destroy(&h
->hash_mutexes
[i
]);
343 #if defined(_KERNEL) && defined(HAVE_SPL)
345 * Large allocations which do not require contiguous pages
346 * should be using vmem_free() in the linux kernel
348 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
350 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
352 kmem_cache_destroy(dbuf_cache
);
361 dbuf_verify(dmu_buf_impl_t
*db
)
364 dbuf_dirty_record_t
*dr
;
366 ASSERT(MUTEX_HELD(&db
->db_mtx
));
368 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
371 ASSERT(db
->db_objset
!= NULL
);
375 ASSERT(db
->db_parent
== NULL
);
376 ASSERT(db
->db_blkptr
== NULL
);
378 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
379 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
380 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
381 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
382 db
->db_blkid
== DMU_SPILL_BLKID
||
383 !list_is_empty(&dn
->dn_dbufs
));
385 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
387 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
388 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
389 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
391 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
392 ASSERT0(db
->db
.db_offset
);
394 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
397 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
398 ASSERT(dr
->dr_dbuf
== db
);
400 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
401 ASSERT(dr
->dr_dbuf
== db
);
404 * We can't assert that db_size matches dn_datablksz because it
405 * can be momentarily different when another thread is doing
408 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
409 dr
= db
->db_data_pending
;
411 * It should only be modified in syncing context, so
412 * make sure we only have one copy of the data.
414 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
417 /* verify db->db_blkptr */
419 if (db
->db_parent
== dn
->dn_dbuf
) {
420 /* db is pointed to by the dnode */
421 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
422 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
423 ASSERT(db
->db_parent
== NULL
);
425 ASSERT(db
->db_parent
!= NULL
);
426 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
427 ASSERT3P(db
->db_blkptr
, ==,
428 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
430 /* db is pointed to by an indirect block */
431 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
433 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
434 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
437 * dnode_grow_indblksz() can make this fail if we don't
438 * have the struct_rwlock. XXX indblksz no longer
439 * grows. safe to do this now?
441 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
442 ASSERT3P(db
->db_blkptr
, ==,
443 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
444 db
->db_blkid
% epb
));
448 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
449 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
450 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
451 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
453 * If the blkptr isn't set but they have nonzero data,
454 * it had better be dirty, otherwise we'll lose that
455 * data when we evict this buffer.
457 if (db
->db_dirtycnt
== 0) {
458 ASSERTV(uint64_t *buf
= db
->db
.db_data
);
461 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
471 dbuf_update_data(dmu_buf_impl_t
*db
)
473 ASSERT(MUTEX_HELD(&db
->db_mtx
));
474 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
475 ASSERT(!refcount_is_zero(&db
->db_holds
));
476 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
481 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
483 ASSERT(MUTEX_HELD(&db
->db_mtx
));
484 ASSERT(db
->db_buf
== NULL
|| !arc_has_callback(db
->db_buf
));
487 ASSERT(buf
->b_data
!= NULL
);
488 db
->db
.db_data
= buf
->b_data
;
489 if (!arc_released(buf
))
490 arc_set_callback(buf
, dbuf_do_evict
, db
);
491 dbuf_update_data(db
);
494 db
->db
.db_data
= NULL
;
495 if (db
->db_state
!= DB_NOFILL
)
496 db
->db_state
= DB_UNCACHED
;
501 * Loan out an arc_buf for read. Return the loaned arc_buf.
504 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
508 mutex_enter(&db
->db_mtx
);
509 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
510 int blksz
= db
->db
.db_size
;
513 mutex_exit(&db
->db_mtx
);
514 DB_GET_SPA(&spa
, db
);
515 abuf
= arc_loan_buf(spa
, blksz
);
516 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
519 arc_loan_inuse_buf(abuf
, db
);
520 dbuf_set_data(db
, NULL
);
521 mutex_exit(&db
->db_mtx
);
527 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
529 if (dn
->dn_datablkshift
) {
530 return (offset
>> dn
->dn_datablkshift
);
532 ASSERT3U(offset
, <, dn
->dn_datablksz
);
538 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
540 dmu_buf_impl_t
*db
= vdb
;
542 mutex_enter(&db
->db_mtx
);
543 ASSERT3U(db
->db_state
, ==, DB_READ
);
545 * All reads are synchronous, so we must have a hold on the dbuf
547 ASSERT(refcount_count(&db
->db_holds
) > 0);
548 ASSERT(db
->db_buf
== NULL
);
549 ASSERT(db
->db
.db_data
== NULL
);
550 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
551 /* we were freed in flight; disregard any error */
552 arc_release(buf
, db
);
553 bzero(buf
->b_data
, db
->db
.db_size
);
555 db
->db_freed_in_flight
= FALSE
;
556 dbuf_set_data(db
, buf
);
557 db
->db_state
= DB_CACHED
;
558 } else if (zio
== NULL
|| zio
->io_error
== 0) {
559 dbuf_set_data(db
, buf
);
560 db
->db_state
= DB_CACHED
;
562 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
563 ASSERT3P(db
->db_buf
, ==, NULL
);
564 VERIFY(arc_buf_remove_ref(buf
, db
));
565 db
->db_state
= DB_UNCACHED
;
567 cv_broadcast(&db
->db_changed
);
568 dbuf_rele_and_unlock(db
, NULL
);
572 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
577 uint32_t aflags
= ARC_NOWAIT
;
581 ASSERT(!refcount_is_zero(&db
->db_holds
));
582 /* We need the struct_rwlock to prevent db_blkptr from changing. */
583 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
584 ASSERT(MUTEX_HELD(&db
->db_mtx
));
585 ASSERT(db
->db_state
== DB_UNCACHED
);
586 ASSERT(db
->db_buf
== NULL
);
588 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
589 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
591 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
592 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
593 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
594 if (bonuslen
< DN_MAX_BONUSLEN
)
595 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
597 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
599 dbuf_update_data(db
);
600 db
->db_state
= DB_CACHED
;
601 mutex_exit(&db
->db_mtx
);
606 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
607 * processes the delete record and clears the bp while we are waiting
608 * for the dn_mtx (resulting in a "no" from block_freed).
610 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
611 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
612 BP_IS_HOLE(db
->db_blkptr
)))) {
613 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
615 dbuf_set_data(db
, arc_buf_alloc(dn
->dn_objset
->os_spa
,
616 db
->db
.db_size
, db
, type
));
618 bzero(db
->db
.db_data
, db
->db
.db_size
);
619 db
->db_state
= DB_CACHED
;
620 *flags
|= DB_RF_CACHED
;
621 mutex_exit(&db
->db_mtx
);
625 spa
= dn
->dn_objset
->os_spa
;
628 db
->db_state
= DB_READ
;
629 mutex_exit(&db
->db_mtx
);
631 if (DBUF_IS_L2CACHEABLE(db
))
632 aflags
|= ARC_L2CACHE
;
633 if (DBUF_IS_L2COMPRESSIBLE(db
))
634 aflags
|= ARC_L2COMPRESS
;
636 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
637 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
638 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
640 dbuf_add_ref(db
, NULL
);
642 (void) arc_read(zio
, spa
, db
->db_blkptr
,
643 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
644 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
646 if (aflags
& ARC_CACHED
)
647 *flags
|= DB_RF_CACHED
;
651 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
654 int havepzio
= (zio
!= NULL
);
659 * We don't have to hold the mutex to check db_state because it
660 * can't be freed while we have a hold on the buffer.
662 ASSERT(!refcount_is_zero(&db
->db_holds
));
664 if (db
->db_state
== DB_NOFILL
)
665 return (SET_ERROR(EIO
));
669 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
670 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
672 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
673 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
674 DBUF_IS_CACHEABLE(db
);
676 mutex_enter(&db
->db_mtx
);
677 if (db
->db_state
== DB_CACHED
) {
678 mutex_exit(&db
->db_mtx
);
680 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
681 db
->db
.db_size
, TRUE
);
682 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
683 rw_exit(&dn
->dn_struct_rwlock
);
685 } else if (db
->db_state
== DB_UNCACHED
) {
686 spa_t
*spa
= dn
->dn_objset
->os_spa
;
689 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
690 dbuf_read_impl(db
, zio
, &flags
);
692 /* dbuf_read_impl has dropped db_mtx for us */
695 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
696 db
->db
.db_size
, flags
& DB_RF_CACHED
);
698 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
699 rw_exit(&dn
->dn_struct_rwlock
);
706 * Another reader came in while the dbuf was in flight
707 * between UNCACHED and CACHED. Either a writer will finish
708 * writing the buffer (sending the dbuf to CACHED) or the
709 * first reader's request will reach the read_done callback
710 * and send the dbuf to CACHED. Otherwise, a failure
711 * occurred and the dbuf went to UNCACHED.
713 mutex_exit(&db
->db_mtx
);
715 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
716 db
->db
.db_size
, TRUE
);
717 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
718 rw_exit(&dn
->dn_struct_rwlock
);
721 /* Skip the wait per the caller's request. */
722 mutex_enter(&db
->db_mtx
);
723 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
724 while (db
->db_state
== DB_READ
||
725 db
->db_state
== DB_FILL
) {
726 ASSERT(db
->db_state
== DB_READ
||
727 (flags
& DB_RF_HAVESTRUCT
) == 0);
728 cv_wait(&db
->db_changed
, &db
->db_mtx
);
730 if (db
->db_state
== DB_UNCACHED
)
731 err
= SET_ERROR(EIO
);
733 mutex_exit(&db
->db_mtx
);
736 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
741 dbuf_noread(dmu_buf_impl_t
*db
)
743 ASSERT(!refcount_is_zero(&db
->db_holds
));
744 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
745 mutex_enter(&db
->db_mtx
);
746 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
747 cv_wait(&db
->db_changed
, &db
->db_mtx
);
748 if (db
->db_state
== DB_UNCACHED
) {
749 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
752 ASSERT(db
->db_buf
== NULL
);
753 ASSERT(db
->db
.db_data
== NULL
);
754 DB_GET_SPA(&spa
, db
);
755 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
756 db
->db_state
= DB_FILL
;
757 } else if (db
->db_state
== DB_NOFILL
) {
758 dbuf_set_data(db
, NULL
);
760 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
762 mutex_exit(&db
->db_mtx
);
766 * This is our just-in-time copy function. It makes a copy of
767 * buffers, that have been modified in a previous transaction
768 * group, before we modify them in the current active group.
770 * This function is used in two places: when we are dirtying a
771 * buffer for the first time in a txg, and when we are freeing
772 * a range in a dnode that includes this buffer.
774 * Note that when we are called from dbuf_free_range() we do
775 * not put a hold on the buffer, we just traverse the active
776 * dbuf list for the dnode.
779 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
781 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
783 ASSERT(MUTEX_HELD(&db
->db_mtx
));
784 ASSERT(db
->db
.db_data
!= NULL
);
785 ASSERT(db
->db_level
== 0);
786 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
789 (dr
->dt
.dl
.dr_data
!=
790 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
794 * If the last dirty record for this dbuf has not yet synced
795 * and its referencing the dbuf data, either:
796 * reset the reference to point to a new copy,
797 * or (if there a no active holders)
798 * just null out the current db_data pointer.
800 ASSERT(dr
->dr_txg
>= txg
- 2);
801 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
802 /* Note that the data bufs here are zio_bufs */
803 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
804 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
805 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
806 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
807 int size
= db
->db
.db_size
;
808 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
811 DB_GET_SPA(&spa
, db
);
812 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
813 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
815 dbuf_set_data(db
, NULL
);
820 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
822 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
823 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
824 uint64_t txg
= dr
->dr_txg
;
826 ASSERT(MUTEX_HELD(&db
->db_mtx
));
827 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
828 ASSERT(db
->db_level
== 0);
830 if (db
->db_blkid
== DMU_BONUS_BLKID
||
831 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
834 ASSERT(db
->db_data_pending
!= dr
);
836 /* free this block */
837 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
) {
840 DB_GET_SPA(&spa
, db
);
841 zio_free(spa
, txg
, bp
);
843 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
844 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
847 * Release the already-written buffer, so we leave it in
848 * a consistent dirty state. Note that all callers are
849 * modifying the buffer, so they will immediately do
850 * another (redundant) arc_release(). Therefore, leave
851 * the buf thawed to save the effort of freezing &
852 * immediately re-thawing it.
854 arc_release(dr
->dt
.dl
.dr_data
, db
);
858 * Evict (if its unreferenced) or clear (if its referenced) any level-0
859 * data blocks in the free range, so that any future readers will find
860 * empty blocks. Also, if we happen across any level-1 dbufs in the
861 * range that have not already been marked dirty, mark them dirty so
862 * they stay in memory.
864 * This is a no-op if the dataset is in the middle of an incremental
865 * receive; see comment below for details.
868 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
870 dmu_buf_impl_t
*db
, *db_next
;
871 uint64_t txg
= tx
->tx_txg
;
872 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
873 uint64_t first_l1
= start
>> epbs
;
874 uint64_t last_l1
= end
>> epbs
;
876 if (end
> dn
->dn_maxblkid
&& (end
!= DMU_SPILL_BLKID
)) {
877 end
= dn
->dn_maxblkid
;
878 last_l1
= end
>> epbs
;
880 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
882 mutex_enter(&dn
->dn_dbufs_mtx
);
883 if (start
>= dn
->dn_unlisted_l0_blkid
* dn
->dn_datablksz
) {
884 /* There can't be any dbufs in this range; no need to search. */
885 mutex_exit(&dn
->dn_dbufs_mtx
);
887 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
889 * If we are receiving, we expect there to be no dbufs in
890 * the range to be freed, because receive modifies each
891 * block at most once, and in offset order. If this is
892 * not the case, it can lead to performance problems,
893 * so note that we unexpectedly took the slow path.
895 atomic_inc_64(&zfs_free_range_recv_miss
);
898 for (db
= list_head(&dn
->dn_dbufs
); db
!= NULL
; db
= db_next
) {
899 db_next
= list_next(&dn
->dn_dbufs
, db
);
900 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
902 if (db
->db_level
== 1 &&
903 db
->db_blkid
>= first_l1
&& db
->db_blkid
<= last_l1
) {
904 mutex_enter(&db
->db_mtx
);
905 if (db
->db_last_dirty
&&
906 db
->db_last_dirty
->dr_txg
< txg
) {
907 dbuf_add_ref(db
, FTAG
);
908 mutex_exit(&db
->db_mtx
);
909 dbuf_will_dirty(db
, tx
);
912 mutex_exit(&db
->db_mtx
);
916 if (db
->db_level
!= 0)
918 dprintf_dbuf(db
, "found buf %s\n", "");
919 if (db
->db_blkid
< start
|| db
->db_blkid
> end
)
922 /* found a level 0 buffer in the range */
923 mutex_enter(&db
->db_mtx
);
924 if (dbuf_undirty(db
, tx
)) {
925 /* mutex has been dropped and dbuf destroyed */
929 if (db
->db_state
== DB_UNCACHED
||
930 db
->db_state
== DB_NOFILL
||
931 db
->db_state
== DB_EVICTING
) {
932 ASSERT(db
->db
.db_data
== NULL
);
933 mutex_exit(&db
->db_mtx
);
936 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
937 /* will be handled in dbuf_read_done or dbuf_rele */
938 db
->db_freed_in_flight
= TRUE
;
939 mutex_exit(&db
->db_mtx
);
942 if (refcount_count(&db
->db_holds
) == 0) {
947 /* The dbuf is referenced */
949 if (db
->db_last_dirty
!= NULL
) {
950 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
952 if (dr
->dr_txg
== txg
) {
954 * This buffer is "in-use", re-adjust the file
955 * size to reflect that this buffer may
956 * contain new data when we sync.
958 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
959 db
->db_blkid
> dn
->dn_maxblkid
)
960 dn
->dn_maxblkid
= db
->db_blkid
;
964 * This dbuf is not dirty in the open context.
965 * Either uncache it (if its not referenced in
966 * the open context) or reset its contents to
969 dbuf_fix_old_data(db
, txg
);
972 /* clear the contents if its cached */
973 if (db
->db_state
== DB_CACHED
) {
974 ASSERT(db
->db
.db_data
!= NULL
);
975 arc_release(db
->db_buf
, db
);
976 bzero(db
->db
.db_data
, db
->db
.db_size
);
977 arc_buf_freeze(db
->db_buf
);
980 mutex_exit(&db
->db_mtx
);
982 mutex_exit(&dn
->dn_dbufs_mtx
);
986 dbuf_block_freeable(dmu_buf_impl_t
*db
)
988 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
989 uint64_t birth_txg
= 0;
992 * We don't need any locking to protect db_blkptr:
993 * If it's syncing, then db_last_dirty will be set
994 * so we'll ignore db_blkptr.
996 ASSERT(MUTEX_HELD(&db
->db_mtx
));
997 if (db
->db_last_dirty
)
998 birth_txg
= db
->db_last_dirty
->dr_txg
;
999 else if (db
->db_blkptr
)
1000 birth_txg
= db
->db_blkptr
->blk_birth
;
1003 * If we don't exist or are in a snapshot, we can't be freed.
1004 * Don't pass the bp to dsl_dataset_block_freeable() since we
1005 * are holding the db_mtx lock and might deadlock if we are
1006 * prefetching a dedup-ed block.
1009 return (ds
== NULL
||
1010 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1016 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1018 arc_buf_t
*buf
, *obuf
;
1019 int osize
= db
->db
.db_size
;
1020 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1023 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1028 /* XXX does *this* func really need the lock? */
1029 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1032 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
1033 * is OK, because there can be no other references to the db
1034 * when we are changing its size, so no concurrent DB_FILL can
1038 * XXX we should be doing a dbuf_read, checking the return
1039 * value and returning that up to our callers
1041 dbuf_will_dirty(db
, tx
);
1043 /* create the data buffer for the new block */
1044 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1046 /* copy old block data to the new block */
1048 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1049 /* zero the remainder */
1051 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1053 mutex_enter(&db
->db_mtx
);
1054 dbuf_set_data(db
, buf
);
1055 VERIFY(arc_buf_remove_ref(obuf
, db
));
1056 db
->db
.db_size
= size
;
1058 if (db
->db_level
== 0) {
1059 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1060 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1062 mutex_exit(&db
->db_mtx
);
1064 dnode_willuse_space(dn
, size
-osize
, tx
);
1069 dbuf_release_bp(dmu_buf_impl_t
*db
)
1073 DB_GET_OBJSET(&os
, db
);
1074 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1075 ASSERT(arc_released(os
->os_phys_buf
) ||
1076 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1077 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1079 (void) arc_release(db
->db_buf
, db
);
1082 dbuf_dirty_record_t
*
1083 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1087 dbuf_dirty_record_t
**drp
, *dr
;
1088 int drop_struct_lock
= FALSE
;
1089 boolean_t do_free_accounting
= B_FALSE
;
1090 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1092 ASSERT(tx
->tx_txg
!= 0);
1093 ASSERT(!refcount_is_zero(&db
->db_holds
));
1094 DMU_TX_DIRTY_BUF(tx
, db
);
1099 * Shouldn't dirty a regular buffer in syncing context. Private
1100 * objects may be dirtied in syncing context, but only if they
1101 * were already pre-dirtied in open context.
1103 ASSERT(!dmu_tx_is_syncing(tx
) ||
1104 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1105 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1106 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1108 * We make this assert for private objects as well, but after we
1109 * check if we're already dirty. They are allowed to re-dirty
1110 * in syncing context.
1112 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1113 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1114 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1116 mutex_enter(&db
->db_mtx
);
1118 * XXX make this true for indirects too? The problem is that
1119 * transactions created with dmu_tx_create_assigned() from
1120 * syncing context don't bother holding ahead.
1122 ASSERT(db
->db_level
!= 0 ||
1123 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1124 db
->db_state
== DB_NOFILL
);
1126 mutex_enter(&dn
->dn_mtx
);
1128 * Don't set dirtyctx to SYNC if we're just modifying this as we
1129 * initialize the objset.
1131 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1132 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1134 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1135 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1136 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_PUSHPAGE
);
1138 mutex_exit(&dn
->dn_mtx
);
1140 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1141 dn
->dn_have_spill
= B_TRUE
;
1144 * If this buffer is already dirty, we're done.
1146 drp
= &db
->db_last_dirty
;
1147 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1148 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1149 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1151 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1154 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1156 * If this buffer has already been written out,
1157 * we now need to reset its state.
1159 dbuf_unoverride(dr
);
1160 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1161 db
->db_state
!= DB_NOFILL
)
1162 arc_buf_thaw(db
->db_buf
);
1164 mutex_exit(&db
->db_mtx
);
1169 * Only valid if not already dirty.
1171 ASSERT(dn
->dn_object
== 0 ||
1172 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1173 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1175 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1176 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1177 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1178 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1179 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1180 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1183 * We should only be dirtying in syncing context if it's the
1184 * mos or we're initializing the os or it's a special object.
1185 * However, we are allowed to dirty in syncing context provided
1186 * we already dirtied it in open context. Hence we must make
1187 * this assertion only if we're not already dirty.
1190 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1191 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1192 ASSERT(db
->db
.db_size
!= 0);
1194 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1196 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1198 * Update the accounting.
1199 * Note: we delay "free accounting" until after we drop
1200 * the db_mtx. This keeps us from grabbing other locks
1201 * (and possibly deadlocking) in bp_get_dsize() while
1202 * also holding the db_mtx.
1204 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1205 do_free_accounting
= dbuf_block_freeable(db
);
1209 * If this buffer is dirty in an old transaction group we need
1210 * to make a copy of it so that the changes we make in this
1211 * transaction group won't leak out when we sync the older txg.
1213 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_PUSHPAGE
);
1214 list_link_init(&dr
->dr_dirty_node
);
1215 if (db
->db_level
== 0) {
1216 void *data_old
= db
->db_buf
;
1218 if (db
->db_state
!= DB_NOFILL
) {
1219 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1220 dbuf_fix_old_data(db
, tx
->tx_txg
);
1221 data_old
= db
->db
.db_data
;
1222 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1224 * Release the data buffer from the cache so
1225 * that we can modify it without impacting
1226 * possible other users of this cached data
1227 * block. Note that indirect blocks and
1228 * private objects are not released until the
1229 * syncing state (since they are only modified
1232 arc_release(db
->db_buf
, db
);
1233 dbuf_fix_old_data(db
, tx
->tx_txg
);
1234 data_old
= db
->db_buf
;
1236 ASSERT(data_old
!= NULL
);
1238 dr
->dt
.dl
.dr_data
= data_old
;
1240 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1241 list_create(&dr
->dt
.di
.dr_children
,
1242 sizeof (dbuf_dirty_record_t
),
1243 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1245 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1246 dr
->dr_accounted
= db
->db
.db_size
;
1248 dr
->dr_txg
= tx
->tx_txg
;
1253 * We could have been freed_in_flight between the dbuf_noread
1254 * and dbuf_dirty. We win, as though the dbuf_noread() had
1255 * happened after the free.
1257 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1258 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1259 mutex_enter(&dn
->dn_mtx
);
1260 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1261 mutex_exit(&dn
->dn_mtx
);
1262 db
->db_freed_in_flight
= FALSE
;
1266 * This buffer is now part of this txg
1268 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1269 db
->db_dirtycnt
+= 1;
1270 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1272 mutex_exit(&db
->db_mtx
);
1274 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1275 db
->db_blkid
== DMU_SPILL_BLKID
) {
1276 mutex_enter(&dn
->dn_mtx
);
1277 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1278 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1279 mutex_exit(&dn
->dn_mtx
);
1280 dnode_setdirty(dn
, tx
);
1283 } else if (do_free_accounting
) {
1284 blkptr_t
*bp
= db
->db_blkptr
;
1285 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1286 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1288 * This is only a guess -- if the dbuf is dirty
1289 * in a previous txg, we don't know how much
1290 * space it will use on disk yet. We should
1291 * really have the struct_rwlock to access
1292 * db_blkptr, but since this is just a guess,
1293 * it's OK if we get an odd answer.
1295 ddt_prefetch(os
->os_spa
, bp
);
1296 dnode_willuse_space(dn
, -willfree
, tx
);
1299 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1300 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1301 drop_struct_lock
= TRUE
;
1304 if (db
->db_level
== 0) {
1305 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1306 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1309 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1310 dmu_buf_impl_t
*parent
= db
->db_parent
;
1311 dbuf_dirty_record_t
*di
;
1312 int parent_held
= FALSE
;
1314 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1315 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1317 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1318 db
->db_blkid
>> epbs
, FTAG
);
1319 ASSERT(parent
!= NULL
);
1322 if (drop_struct_lock
)
1323 rw_exit(&dn
->dn_struct_rwlock
);
1324 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1325 di
= dbuf_dirty(parent
, tx
);
1327 dbuf_rele(parent
, FTAG
);
1329 mutex_enter(&db
->db_mtx
);
1331 * Since we've dropped the mutex, it's possible that
1332 * dbuf_undirty() might have changed this out from under us.
1334 if (db
->db_last_dirty
== dr
||
1335 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1336 mutex_enter(&di
->dt
.di
.dr_mtx
);
1337 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1338 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1339 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1340 mutex_exit(&di
->dt
.di
.dr_mtx
);
1343 mutex_exit(&db
->db_mtx
);
1345 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1346 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1347 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1348 mutex_enter(&dn
->dn_mtx
);
1349 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1350 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1351 mutex_exit(&dn
->dn_mtx
);
1352 if (drop_struct_lock
)
1353 rw_exit(&dn
->dn_struct_rwlock
);
1356 dnode_setdirty(dn
, tx
);
1362 * Undirty a buffer in the transaction group referenced by the given
1363 * transaction. Return whether this evicted the dbuf.
1366 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1369 uint64_t txg
= tx
->tx_txg
;
1370 dbuf_dirty_record_t
*dr
, **drp
;
1373 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1374 ASSERT0(db
->db_level
);
1375 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1378 * If this buffer is not dirty, we're done.
1380 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1381 if (dr
->dr_txg
<= txg
)
1383 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1385 ASSERT(dr
->dr_txg
== txg
);
1386 ASSERT(dr
->dr_dbuf
== db
);
1392 * Note: This code will probably work even if there are concurrent
1393 * holders, but it is untested in that scenerio, as the ZPL and
1394 * ztest have additional locking (the range locks) that prevents
1395 * that type of concurrent access.
1397 ASSERT3U(refcount_count(&db
->db_holds
), ==, db
->db_dirtycnt
);
1399 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1401 ASSERT(db
->db
.db_size
!= 0);
1404 * Any space we accounted for in dp_dirty_* will be cleaned up by
1405 * dsl_pool_sync(). This is relatively rare so the discrepancy
1406 * is not a big deal.
1412 * Note that there are three places in dbuf_dirty()
1413 * where this dirty record may be put on a list.
1414 * Make sure to do a list_remove corresponding to
1415 * every one of those list_insert calls.
1417 if (dr
->dr_parent
) {
1418 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1419 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1420 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1421 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1422 db
->db_level
+1 == dn
->dn_nlevels
) {
1423 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1424 mutex_enter(&dn
->dn_mtx
);
1425 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1426 mutex_exit(&dn
->dn_mtx
);
1430 if (db
->db_state
!= DB_NOFILL
) {
1431 dbuf_unoverride(dr
);
1433 ASSERT(db
->db_buf
!= NULL
);
1434 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1435 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1436 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1438 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1440 ASSERT(db
->db_dirtycnt
> 0);
1441 db
->db_dirtycnt
-= 1;
1443 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1444 arc_buf_t
*buf
= db
->db_buf
;
1446 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1447 dbuf_set_data(db
, NULL
);
1448 VERIFY(arc_buf_remove_ref(buf
, db
));
1456 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1458 dbuf_will_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1460 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1462 ASSERT(tx
->tx_txg
!= 0);
1463 ASSERT(!refcount_is_zero(&db
->db_holds
));
1466 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1467 rf
|= DB_RF_HAVESTRUCT
;
1469 (void) dbuf_read(db
, NULL
, rf
);
1470 (void) dbuf_dirty(db
, tx
);
1474 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1476 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1478 db
->db_state
= DB_NOFILL
;
1480 dmu_buf_will_fill(db_fake
, tx
);
1484 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1486 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1488 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1489 ASSERT(tx
->tx_txg
!= 0);
1490 ASSERT(db
->db_level
== 0);
1491 ASSERT(!refcount_is_zero(&db
->db_holds
));
1493 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1494 dmu_tx_private_ok(tx
));
1497 (void) dbuf_dirty(db
, tx
);
1500 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1503 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1505 mutex_enter(&db
->db_mtx
);
1508 if (db
->db_state
== DB_FILL
) {
1509 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1510 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1511 /* we were freed while filling */
1512 /* XXX dbuf_undirty? */
1513 bzero(db
->db
.db_data
, db
->db
.db_size
);
1514 db
->db_freed_in_flight
= FALSE
;
1516 db
->db_state
= DB_CACHED
;
1517 cv_broadcast(&db
->db_changed
);
1519 mutex_exit(&db
->db_mtx
);
1523 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1524 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1527 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1529 ASSERT(!refcount_is_zero(&db
->db_holds
));
1530 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1531 ASSERT(db
->db_level
== 0);
1532 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1533 ASSERT(buf
!= NULL
);
1534 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1535 ASSERT(tx
->tx_txg
!= 0);
1537 arc_return_buf(buf
, db
);
1538 ASSERT(arc_released(buf
));
1540 mutex_enter(&db
->db_mtx
);
1542 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1543 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1545 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1547 if (db
->db_state
== DB_CACHED
&&
1548 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1549 mutex_exit(&db
->db_mtx
);
1550 (void) dbuf_dirty(db
, tx
);
1551 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1552 VERIFY(arc_buf_remove_ref(buf
, db
));
1553 xuio_stat_wbuf_copied();
1557 xuio_stat_wbuf_nocopy();
1558 if (db
->db_state
== DB_CACHED
) {
1559 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1561 ASSERT(db
->db_buf
!= NULL
);
1562 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1563 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1564 if (!arc_released(db
->db_buf
)) {
1565 ASSERT(dr
->dt
.dl
.dr_override_state
==
1567 arc_release(db
->db_buf
, db
);
1569 dr
->dt
.dl
.dr_data
= buf
;
1570 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1571 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1572 arc_release(db
->db_buf
, db
);
1573 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1577 ASSERT(db
->db_buf
== NULL
);
1578 dbuf_set_data(db
, buf
);
1579 db
->db_state
= DB_FILL
;
1580 mutex_exit(&db
->db_mtx
);
1581 (void) dbuf_dirty(db
, tx
);
1582 dbuf_fill_done(db
, tx
);
1586 * "Clear" the contents of this dbuf. This will mark the dbuf
1587 * EVICTING and clear *most* of its references. Unfortunately,
1588 * when we are not holding the dn_dbufs_mtx, we can't clear the
1589 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1590 * in this case. For callers from the DMU we will usually see:
1591 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1592 * For the arc callback, we will usually see:
1593 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1594 * Sometimes, though, we will get a mix of these two:
1595 * DMU: dbuf_clear()->arc_buf_evict()
1596 * ARC: dbuf_do_evict()->dbuf_destroy()
1599 dbuf_clear(dmu_buf_impl_t
*db
)
1602 dmu_buf_impl_t
*parent
= db
->db_parent
;
1603 dmu_buf_impl_t
*dndb
;
1604 int dbuf_gone
= FALSE
;
1606 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1607 ASSERT(refcount_is_zero(&db
->db_holds
));
1609 dbuf_evict_user(db
);
1611 if (db
->db_state
== DB_CACHED
) {
1612 ASSERT(db
->db
.db_data
!= NULL
);
1613 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1614 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1615 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1617 db
->db
.db_data
= NULL
;
1618 db
->db_state
= DB_UNCACHED
;
1621 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1622 ASSERT(db
->db_data_pending
== NULL
);
1624 db
->db_state
= DB_EVICTING
;
1625 db
->db_blkptr
= NULL
;
1630 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1631 list_remove(&dn
->dn_dbufs
, db
);
1632 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1636 * Decrementing the dbuf count means that the hold corresponding
1637 * to the removed dbuf is no longer discounted in dnode_move(),
1638 * so the dnode cannot be moved until after we release the hold.
1639 * The membar_producer() ensures visibility of the decremented
1640 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1644 db
->db_dnode_handle
= NULL
;
1650 dbuf_gone
= arc_buf_evict(db
->db_buf
);
1653 mutex_exit(&db
->db_mtx
);
1656 * If this dbuf is referenced from an indirect dbuf,
1657 * decrement the ref count on the indirect dbuf.
1659 if (parent
&& parent
!= dndb
)
1660 dbuf_rele(parent
, db
);
1663 __attribute__((always_inline
))
1665 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1666 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
1673 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1675 if (blkid
== DMU_SPILL_BLKID
) {
1676 mutex_enter(&dn
->dn_mtx
);
1677 if (dn
->dn_have_spill
&&
1678 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1679 *bpp
= &dn
->dn_phys
->dn_spill
;
1682 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1683 *parentp
= dn
->dn_dbuf
;
1684 mutex_exit(&dn
->dn_mtx
);
1688 if (dn
->dn_phys
->dn_nlevels
== 0)
1691 nlevels
= dn
->dn_phys
->dn_nlevels
;
1693 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1695 ASSERT3U(level
* epbs
, <, 64);
1696 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1697 if (level
>= nlevels
||
1698 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1699 /* the buffer has no parent yet */
1700 return (SET_ERROR(ENOENT
));
1701 } else if (level
< nlevels
-1) {
1702 /* this block is referenced from an indirect block */
1705 err
= dbuf_hold_impl(dn
, level
+1, blkid
>> epbs
,
1706 fail_sparse
, NULL
, parentp
);
1708 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
1709 blkid
>> epbs
, fail_sparse
, NULL
,
1710 parentp
, dh
->dh_depth
+ 1);
1711 err
= __dbuf_hold_impl(dh
+ 1);
1715 err
= dbuf_read(*parentp
, NULL
,
1716 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1718 dbuf_rele(*parentp
, NULL
);
1722 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1723 (blkid
& ((1ULL << epbs
) - 1));
1726 /* the block is referenced from the dnode */
1727 ASSERT3U(level
, ==, nlevels
-1);
1728 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1729 blkid
< dn
->dn_phys
->dn_nblkptr
);
1731 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1732 *parentp
= dn
->dn_dbuf
;
1734 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1739 static dmu_buf_impl_t
*
1740 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1741 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1743 objset_t
*os
= dn
->dn_objset
;
1744 dmu_buf_impl_t
*db
, *odb
;
1746 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1747 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1749 db
= kmem_cache_alloc(dbuf_cache
, KM_PUSHPAGE
);
1752 db
->db
.db_object
= dn
->dn_object
;
1753 db
->db_level
= level
;
1754 db
->db_blkid
= blkid
;
1755 db
->db_last_dirty
= NULL
;
1756 db
->db_dirtycnt
= 0;
1757 db
->db_dnode_handle
= dn
->dn_handle
;
1758 db
->db_parent
= parent
;
1759 db
->db_blkptr
= blkptr
;
1761 db
->db_user_ptr
= NULL
;
1762 db
->db_user_data_ptr_ptr
= NULL
;
1763 db
->db_evict_func
= NULL
;
1764 db
->db_immediate_evict
= 0;
1765 db
->db_freed_in_flight
= 0;
1767 if (blkid
== DMU_BONUS_BLKID
) {
1768 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1769 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1770 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1771 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1772 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1773 db
->db_state
= DB_UNCACHED
;
1774 /* the bonus dbuf is not placed in the hash table */
1775 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1777 } else if (blkid
== DMU_SPILL_BLKID
) {
1778 db
->db
.db_size
= (blkptr
!= NULL
) ?
1779 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1780 db
->db
.db_offset
= 0;
1783 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
1784 db
->db
.db_size
= blocksize
;
1785 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1789 * Hold the dn_dbufs_mtx while we get the new dbuf
1790 * in the hash table *and* added to the dbufs list.
1791 * This prevents a possible deadlock with someone
1792 * trying to look up this dbuf before its added to the
1795 mutex_enter(&dn
->dn_dbufs_mtx
);
1796 db
->db_state
= DB_EVICTING
;
1797 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1798 /* someone else inserted it first */
1799 kmem_cache_free(dbuf_cache
, db
);
1800 mutex_exit(&dn
->dn_dbufs_mtx
);
1803 list_insert_head(&dn
->dn_dbufs
, db
);
1804 if (db
->db_level
== 0 && db
->db_blkid
>=
1805 dn
->dn_unlisted_l0_blkid
)
1806 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
1807 db
->db_state
= DB_UNCACHED
;
1808 mutex_exit(&dn
->dn_dbufs_mtx
);
1809 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1811 if (parent
&& parent
!= dn
->dn_dbuf
)
1812 dbuf_add_ref(parent
, db
);
1814 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1815 refcount_count(&dn
->dn_holds
) > 0);
1816 (void) refcount_add(&dn
->dn_holds
, db
);
1817 (void) atomic_inc_32_nv(&dn
->dn_dbufs_count
);
1819 dprintf_dbuf(db
, "db=%p\n", db
);
1825 dbuf_do_evict(void *private)
1827 arc_buf_t
*buf
= private;
1828 dmu_buf_impl_t
*db
= buf
->b_private
;
1830 if (!MUTEX_HELD(&db
->db_mtx
))
1831 mutex_enter(&db
->db_mtx
);
1833 ASSERT(refcount_is_zero(&db
->db_holds
));
1835 if (db
->db_state
!= DB_EVICTING
) {
1836 ASSERT(db
->db_state
== DB_CACHED
);
1841 mutex_exit(&db
->db_mtx
);
1848 dbuf_destroy(dmu_buf_impl_t
*db
)
1850 ASSERT(refcount_is_zero(&db
->db_holds
));
1852 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1854 * If this dbuf is still on the dn_dbufs list,
1855 * remove it from that list.
1857 if (db
->db_dnode_handle
!= NULL
) {
1862 mutex_enter(&dn
->dn_dbufs_mtx
);
1863 list_remove(&dn
->dn_dbufs
, db
);
1864 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1865 mutex_exit(&dn
->dn_dbufs_mtx
);
1868 * Decrementing the dbuf count means that the hold
1869 * corresponding to the removed dbuf is no longer
1870 * discounted in dnode_move(), so the dnode cannot be
1871 * moved until after we release the hold.
1874 db
->db_dnode_handle
= NULL
;
1876 dbuf_hash_remove(db
);
1878 db
->db_parent
= NULL
;
1881 ASSERT(!list_link_active(&db
->db_link
));
1882 ASSERT(db
->db
.db_data
== NULL
);
1883 ASSERT(db
->db_hash_next
== NULL
);
1884 ASSERT(db
->db_blkptr
== NULL
);
1885 ASSERT(db
->db_data_pending
== NULL
);
1887 kmem_cache_free(dbuf_cache
, db
);
1888 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1892 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
, zio_priority_t prio
)
1894 dmu_buf_impl_t
*db
= NULL
;
1895 blkptr_t
*bp
= NULL
;
1897 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1898 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1900 if (dnode_block_freed(dn
, blkid
))
1903 /* dbuf_find() returns with db_mtx held */
1904 if ((db
= dbuf_find(dn
, 0, blkid
))) {
1906 * This dbuf is already in the cache. We assume that
1907 * it is already CACHED, or else about to be either
1910 mutex_exit(&db
->db_mtx
);
1914 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
, NULL
) == 0) {
1915 if (bp
&& !BP_IS_HOLE(bp
)) {
1916 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1917 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1920 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1921 dn
->dn_object
, 0, blkid
);
1923 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1924 bp
, NULL
, NULL
, prio
,
1925 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1929 dbuf_rele(db
, NULL
);
1933 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1936 * Returns with db_holds incremented, and db_mtx not held.
1937 * Note: dn_struct_rwlock must be held.
1940 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
1942 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
1943 dh
->dh_parent
= NULL
;
1945 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
1946 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
1947 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
1949 *(dh
->dh_dbp
) = NULL
;
1951 /* dbuf_find() returns with db_mtx held */
1952 dh
->dh_db
= dbuf_find(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
);
1954 if (dh
->dh_db
== NULL
) {
1957 ASSERT3P(dh
->dh_parent
, ==, NULL
);
1958 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1959 dh
->dh_fail_sparse
, &dh
->dh_parent
,
1961 if (dh
->dh_fail_sparse
) {
1962 if (dh
->dh_err
== 0 &&
1963 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
1964 dh
->dh_err
= SET_ERROR(ENOENT
);
1967 dbuf_rele(dh
->dh_parent
, NULL
);
1968 return (dh
->dh_err
);
1971 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
1972 return (dh
->dh_err
);
1973 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
1974 dh
->dh_parent
, dh
->dh_bp
);
1977 if (dh
->dh_db
->db_buf
&& refcount_is_zero(&dh
->dh_db
->db_holds
)) {
1978 arc_buf_add_ref(dh
->dh_db
->db_buf
, dh
->dh_db
);
1979 if (dh
->dh_db
->db_buf
->b_data
== NULL
) {
1980 dbuf_clear(dh
->dh_db
);
1981 if (dh
->dh_parent
) {
1982 dbuf_rele(dh
->dh_parent
, NULL
);
1983 dh
->dh_parent
= NULL
;
1987 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
1990 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
1993 * If this buffer is currently syncing out, and we are are
1994 * still referencing it from db_data, we need to make a copy
1995 * of it in case we decide we want to dirty it again in this txg.
1997 if (dh
->dh_db
->db_level
== 0 &&
1998 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
1999 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2000 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2001 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2003 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2004 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2006 dbuf_set_data(dh
->dh_db
,
2007 arc_buf_alloc(dh
->dh_dn
->dn_objset
->os_spa
,
2008 dh
->dh_db
->db
.db_size
, dh
->dh_db
, dh
->dh_type
));
2009 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2010 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2014 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2015 dbuf_update_data(dh
->dh_db
);
2016 DBUF_VERIFY(dh
->dh_db
);
2017 mutex_exit(&dh
->dh_db
->db_mtx
);
2019 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2021 dbuf_rele(dh
->dh_parent
, NULL
);
2023 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2024 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2025 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2026 *(dh
->dh_dbp
) = dh
->dh_db
;
2032 * The following code preserves the recursive function dbuf_hold_impl()
2033 * but moves the local variables AND function arguments to the heap to
2034 * minimize the stack frame size. Enough space is initially allocated
2035 * on the stack for 20 levels of recursion.
2038 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2039 void *tag
, dmu_buf_impl_t
**dbp
)
2041 struct dbuf_hold_impl_data
*dh
;
2044 dh
= kmem_zalloc(sizeof (struct dbuf_hold_impl_data
) *
2045 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_PUSHPAGE
);
2046 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
, tag
, dbp
, 0);
2048 error
= __dbuf_hold_impl(dh
);
2050 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2051 DBUF_HOLD_IMPL_MAX_DEPTH
);
2057 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2058 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
2059 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2062 dh
->dh_level
= level
;
2063 dh
->dh_blkid
= blkid
;
2064 dh
->dh_fail_sparse
= fail_sparse
;
2067 dh
->dh_depth
= depth
;
2071 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2074 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
2075 return (err
? NULL
: db
);
2079 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2082 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
2083 return (err
? NULL
: db
);
2087 dbuf_create_bonus(dnode_t
*dn
)
2089 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2091 ASSERT(dn
->dn_bonus
== NULL
);
2092 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2096 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2098 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2101 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2102 return (SET_ERROR(ENOTSUP
));
2104 blksz
= SPA_MINBLOCKSIZE
;
2105 if (blksz
> SPA_MAXBLOCKSIZE
)
2106 blksz
= SPA_MAXBLOCKSIZE
;
2108 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2112 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2113 dbuf_new_size(db
, blksz
, tx
);
2114 rw_exit(&dn
->dn_struct_rwlock
);
2121 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2123 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2126 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2128 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2130 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2134 * If you call dbuf_rele() you had better not be referencing the dnode handle
2135 * unless you have some other direct or indirect hold on the dnode. (An indirect
2136 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2137 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2138 * dnode's parent dbuf evicting its dnode handles.
2140 #pragma weak dmu_buf_rele = dbuf_rele
2142 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2144 mutex_enter(&db
->db_mtx
);
2145 dbuf_rele_and_unlock(db
, tag
);
2149 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2150 * db_dirtycnt and db_holds to be updated atomically.
2153 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2157 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2161 * Remove the reference to the dbuf before removing its hold on the
2162 * dnode so we can guarantee in dnode_move() that a referenced bonus
2163 * buffer has a corresponding dnode hold.
2165 holds
= refcount_remove(&db
->db_holds
, tag
);
2169 * We can't freeze indirects if there is a possibility that they
2170 * may be modified in the current syncing context.
2172 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2173 arc_buf_freeze(db
->db_buf
);
2175 if (holds
== db
->db_dirtycnt
&&
2176 db
->db_level
== 0 && db
->db_immediate_evict
)
2177 dbuf_evict_user(db
);
2180 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2181 mutex_exit(&db
->db_mtx
);
2184 * If the dnode moves here, we cannot cross this barrier
2185 * until the move completes.
2188 (void) atomic_dec_32_nv(&DB_DNODE(db
)->dn_dbufs_count
);
2191 * The bonus buffer's dnode hold is no longer discounted
2192 * in dnode_move(). The dnode cannot move until after
2195 dnode_rele(DB_DNODE(db
), db
);
2196 } else if (db
->db_buf
== NULL
) {
2198 * This is a special case: we never associated this
2199 * dbuf with any data allocated from the ARC.
2201 ASSERT(db
->db_state
== DB_UNCACHED
||
2202 db
->db_state
== DB_NOFILL
);
2204 } else if (arc_released(db
->db_buf
)) {
2205 arc_buf_t
*buf
= db
->db_buf
;
2207 * This dbuf has anonymous data associated with it.
2209 dbuf_set_data(db
, NULL
);
2210 VERIFY(arc_buf_remove_ref(buf
, db
));
2213 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2216 * A dbuf will be eligible for eviction if either the
2217 * 'primarycache' property is set or a duplicate
2218 * copy of this buffer is already cached in the arc.
2220 * In the case of the 'primarycache' a buffer
2221 * is considered for eviction if it matches the
2222 * criteria set in the property.
2224 * To decide if our buffer is considered a
2225 * duplicate, we must call into the arc to determine
2226 * if multiple buffers are referencing the same
2227 * block on-disk. If so, then we simply evict
2230 if (!DBUF_IS_CACHEABLE(db
) ||
2231 arc_buf_eviction_needed(db
->db_buf
))
2234 mutex_exit(&db
->db_mtx
);
2237 mutex_exit(&db
->db_mtx
);
2241 #pragma weak dmu_buf_refcount = dbuf_refcount
2243 dbuf_refcount(dmu_buf_impl_t
*db
)
2245 return (refcount_count(&db
->db_holds
));
2249 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2250 dmu_buf_evict_func_t
*evict_func
)
2252 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2253 user_data_ptr_ptr
, evict_func
));
2257 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2258 dmu_buf_evict_func_t
*evict_func
)
2260 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2262 db
->db_immediate_evict
= TRUE
;
2263 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2264 user_data_ptr_ptr
, evict_func
));
2268 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
2269 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2271 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2272 ASSERT(db
->db_level
== 0);
2274 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2276 mutex_enter(&db
->db_mtx
);
2278 if (db
->db_user_ptr
== old_user_ptr
) {
2279 db
->db_user_ptr
= user_ptr
;
2280 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2281 db
->db_evict_func
= evict_func
;
2283 dbuf_update_data(db
);
2285 old_user_ptr
= db
->db_user_ptr
;
2288 mutex_exit(&db
->db_mtx
);
2289 return (old_user_ptr
);
2293 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2295 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2296 ASSERT(!refcount_is_zero(&db
->db_holds
));
2298 return (db
->db_user_ptr
);
2302 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2304 boolean_t res
= B_FALSE
;
2305 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2308 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2309 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2315 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2317 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2318 return (dbi
->db_blkptr
);
2322 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2324 /* ASSERT(dmu_tx_is_syncing(tx) */
2325 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2327 if (db
->db_blkptr
!= NULL
)
2330 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2331 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2332 BP_ZERO(db
->db_blkptr
);
2335 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2337 * This buffer was allocated at a time when there was
2338 * no available blkptrs from the dnode, or it was
2339 * inappropriate to hook it in (i.e., nlevels mis-match).
2341 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2342 ASSERT(db
->db_parent
== NULL
);
2343 db
->db_parent
= dn
->dn_dbuf
;
2344 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2347 dmu_buf_impl_t
*parent
= db
->db_parent
;
2348 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2350 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2351 if (parent
== NULL
) {
2352 mutex_exit(&db
->db_mtx
);
2353 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2354 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2355 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2356 rw_exit(&dn
->dn_struct_rwlock
);
2357 mutex_enter(&db
->db_mtx
);
2358 db
->db_parent
= parent
;
2360 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2361 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2367 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2368 * is critical the we not allow the compiler to inline this function in to
2369 * dbuf_sync_list() thereby drastically bloating the stack usage.
2371 noinline
static void
2372 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2374 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2378 ASSERT(dmu_tx_is_syncing(tx
));
2380 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2382 mutex_enter(&db
->db_mtx
);
2384 ASSERT(db
->db_level
> 0);
2387 /* Read the block if it hasn't been read yet. */
2388 if (db
->db_buf
== NULL
) {
2389 mutex_exit(&db
->db_mtx
);
2390 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2391 mutex_enter(&db
->db_mtx
);
2393 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2394 ASSERT(db
->db_buf
!= NULL
);
2398 /* Indirect block size must match what the dnode thinks it is. */
2399 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2400 dbuf_check_blkptr(dn
, db
);
2403 /* Provide the pending dirty record to child dbufs */
2404 db
->db_data_pending
= dr
;
2406 mutex_exit(&db
->db_mtx
);
2407 dbuf_write(dr
, db
->db_buf
, tx
);
2410 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2411 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2412 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2413 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2418 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2419 * critical the we not allow the compiler to inline this function in to
2420 * dbuf_sync_list() thereby drastically bloating the stack usage.
2422 noinline
static void
2423 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2425 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2426 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2429 uint64_t txg
= tx
->tx_txg
;
2431 ASSERT(dmu_tx_is_syncing(tx
));
2433 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2435 mutex_enter(&db
->db_mtx
);
2437 * To be synced, we must be dirtied. But we
2438 * might have been freed after the dirty.
2440 if (db
->db_state
== DB_UNCACHED
) {
2441 /* This buffer has been freed since it was dirtied */
2442 ASSERT(db
->db
.db_data
== NULL
);
2443 } else if (db
->db_state
== DB_FILL
) {
2444 /* This buffer was freed and is now being re-filled */
2445 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2447 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2454 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2455 mutex_enter(&dn
->dn_mtx
);
2456 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2457 mutex_exit(&dn
->dn_mtx
);
2461 * If this is a bonus buffer, simply copy the bonus data into the
2462 * dnode. It will be written out when the dnode is synced (and it
2463 * will be synced, since it must have been dirty for dbuf_sync to
2466 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2467 dbuf_dirty_record_t
**drp
;
2469 ASSERT(*datap
!= NULL
);
2470 ASSERT0(db
->db_level
);
2471 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2472 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2475 if (*datap
!= db
->db
.db_data
) {
2476 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2477 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2479 db
->db_data_pending
= NULL
;
2480 drp
= &db
->db_last_dirty
;
2482 drp
= &(*drp
)->dr_next
;
2483 ASSERT(dr
->dr_next
== NULL
);
2484 ASSERT(dr
->dr_dbuf
== db
);
2486 if (dr
->dr_dbuf
->db_level
!= 0) {
2487 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2488 list_destroy(&dr
->dt
.di
.dr_children
);
2490 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2491 ASSERT(db
->db_dirtycnt
> 0);
2492 db
->db_dirtycnt
-= 1;
2493 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2500 * This function may have dropped the db_mtx lock allowing a dmu_sync
2501 * operation to sneak in. As a result, we need to ensure that we
2502 * don't check the dr_override_state until we have returned from
2503 * dbuf_check_blkptr.
2505 dbuf_check_blkptr(dn
, db
);
2508 * If this buffer is in the middle of an immediate write,
2509 * wait for the synchronous IO to complete.
2511 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2512 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2513 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2514 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2517 if (db
->db_state
!= DB_NOFILL
&&
2518 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2519 refcount_count(&db
->db_holds
) > 1 &&
2520 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2521 *datap
== db
->db_buf
) {
2523 * If this buffer is currently "in use" (i.e., there
2524 * are active holds and db_data still references it),
2525 * then make a copy before we start the write so that
2526 * any modifications from the open txg will not leak
2529 * NOTE: this copy does not need to be made for
2530 * objects only modified in the syncing context (e.g.
2531 * DNONE_DNODE blocks).
2533 int blksz
= arc_buf_size(*datap
);
2534 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2535 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2536 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2538 db
->db_data_pending
= dr
;
2540 mutex_exit(&db
->db_mtx
);
2542 dbuf_write(dr
, *datap
, tx
);
2544 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2545 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2546 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2550 * Although zio_nowait() does not "wait for an IO", it does
2551 * initiate the IO. If this is an empty write it seems plausible
2552 * that the IO could actually be completed before the nowait
2553 * returns. We need to DB_DNODE_EXIT() first in case
2554 * zio_nowait() invalidates the dbuf.
2557 zio_nowait(dr
->dr_zio
);
2562 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2564 dbuf_dirty_record_t
*dr
;
2566 while ((dr
= list_head(list
))) {
2567 if (dr
->dr_zio
!= NULL
) {
2569 * If we find an already initialized zio then we
2570 * are processing the meta-dnode, and we have finished.
2571 * The dbufs for all dnodes are put back on the list
2572 * during processing, so that we can zio_wait()
2573 * these IOs after initiating all child IOs.
2575 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2576 DMU_META_DNODE_OBJECT
);
2579 list_remove(list
, dr
);
2580 if (dr
->dr_dbuf
->db_level
> 0)
2581 dbuf_sync_indirect(dr
, tx
);
2583 dbuf_sync_leaf(dr
, tx
);
2589 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2591 dmu_buf_impl_t
*db
= vdb
;
2593 blkptr_t
*bp
= zio
->io_bp
;
2594 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2595 spa_t
*spa
= zio
->io_spa
;
2600 ASSERT(db
->db_blkptr
== bp
);
2604 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2605 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2606 zio
->io_prev_space_delta
= delta
;
2608 if (BP_IS_HOLE(bp
)) {
2609 ASSERT(bp
->blk_fill
== 0);
2614 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2615 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2616 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2617 BP_GET_TYPE(bp
) == dn
->dn_bonustype
));
2618 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2620 mutex_enter(&db
->db_mtx
);
2623 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2624 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2625 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2626 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2630 if (db
->db_level
== 0) {
2631 mutex_enter(&dn
->dn_mtx
);
2632 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2633 db
->db_blkid
!= DMU_SPILL_BLKID
)
2634 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2635 mutex_exit(&dn
->dn_mtx
);
2637 if (dn
->dn_type
== DMU_OT_DNODE
) {
2638 dnode_phys_t
*dnp
= db
->db
.db_data
;
2639 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2641 if (dnp
->dn_type
!= DMU_OT_NONE
)
2648 blkptr_t
*ibp
= db
->db
.db_data
;
2649 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2650 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2651 if (BP_IS_HOLE(ibp
))
2653 fill
+= ibp
->blk_fill
;
2658 bp
->blk_fill
= fill
;
2660 mutex_exit(&db
->db_mtx
);
2664 * The SPA will call this callback several times for each zio - once
2665 * for every physical child i/o (zio->io_phys_children times). This
2666 * allows the DMU to monitor the progress of each logical i/o. For example,
2667 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2668 * block. There may be a long delay before all copies/fragments are completed,
2669 * so this callback allows us to retire dirty space gradually, as the physical
2674 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2676 dmu_buf_impl_t
*db
= arg
;
2677 objset_t
*os
= db
->db_objset
;
2678 dsl_pool_t
*dp
= dmu_objset_pool(os
);
2679 dbuf_dirty_record_t
*dr
;
2682 dr
= db
->db_data_pending
;
2683 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
2686 * The callback will be called io_phys_children times. Retire one
2687 * portion of our dirty space each time we are called. Any rounding
2688 * error will be cleaned up by dsl_pool_sync()'s call to
2689 * dsl_pool_undirty_space().
2691 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
2692 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
2697 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2699 dmu_buf_impl_t
*db
= vdb
;
2700 blkptr_t
*bp
= zio
->io_bp
;
2701 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2702 uint64_t txg
= zio
->io_txg
;
2703 dbuf_dirty_record_t
**drp
, *dr
;
2705 ASSERT0(zio
->io_error
);
2706 ASSERT(db
->db_blkptr
== bp
);
2709 * For nopwrites and rewrites we ensure that the bp matches our
2710 * original and bypass all the accounting.
2712 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
2713 ASSERT(BP_EQUAL(bp
, bp_orig
));
2719 DB_GET_OBJSET(&os
, db
);
2720 ds
= os
->os_dsl_dataset
;
2723 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2724 dsl_dataset_block_born(ds
, bp
, tx
);
2727 mutex_enter(&db
->db_mtx
);
2731 drp
= &db
->db_last_dirty
;
2732 while ((dr
= *drp
) != db
->db_data_pending
)
2734 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2735 ASSERT(dr
->dr_txg
== txg
);
2736 ASSERT(dr
->dr_dbuf
== db
);
2737 ASSERT(dr
->dr_next
== NULL
);
2741 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2746 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2747 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2748 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2753 if (db
->db_level
== 0) {
2754 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2755 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2756 if (db
->db_state
!= DB_NOFILL
) {
2757 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2758 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2760 else if (!arc_released(db
->db_buf
))
2761 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2768 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2769 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2770 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2771 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
2773 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2775 ASSERT3U(dn
->dn_phys
->dn_maxblkid
2776 >> (db
->db_level
* epbs
), >=, db
->db_blkid
);
2777 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2780 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2781 list_destroy(&dr
->dt
.di
.dr_children
);
2783 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2785 cv_broadcast(&db
->db_changed
);
2786 ASSERT(db
->db_dirtycnt
> 0);
2787 db
->db_dirtycnt
-= 1;
2788 db
->db_data_pending
= NULL
;
2790 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2794 dbuf_write_nofill_ready(zio_t
*zio
)
2796 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2800 dbuf_write_nofill_done(zio_t
*zio
)
2802 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2806 dbuf_write_override_ready(zio_t
*zio
)
2808 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2809 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2811 dbuf_write_ready(zio
, NULL
, db
);
2815 dbuf_write_override_done(zio_t
*zio
)
2817 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2818 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2819 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2821 mutex_enter(&db
->db_mtx
);
2822 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2823 if (!BP_IS_HOLE(obp
))
2824 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2825 arc_release(dr
->dt
.dl
.dr_data
, db
);
2827 mutex_exit(&db
->db_mtx
);
2829 dbuf_write_done(zio
, NULL
, db
);
2832 /* Issue I/O to commit a dirty buffer to disk. */
2834 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2836 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2839 dmu_buf_impl_t
*parent
= db
->db_parent
;
2840 uint64_t txg
= tx
->tx_txg
;
2850 if (db
->db_state
!= DB_NOFILL
) {
2851 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2853 * Private object buffers are released here rather
2854 * than in dbuf_dirty() since they are only modified
2855 * in the syncing context and we don't want the
2856 * overhead of making multiple copies of the data.
2858 if (BP_IS_HOLE(db
->db_blkptr
)) {
2861 dbuf_release_bp(db
);
2866 if (parent
!= dn
->dn_dbuf
) {
2867 /* Our parent is an indirect block. */
2868 /* We have a dirty parent that has been scheduled for write. */
2869 ASSERT(parent
&& parent
->db_data_pending
);
2870 /* Our parent's buffer is one level closer to the dnode. */
2871 ASSERT(db
->db_level
== parent
->db_level
-1);
2873 * We're about to modify our parent's db_data by modifying
2874 * our block pointer, so the parent must be released.
2876 ASSERT(arc_released(parent
->db_buf
));
2877 zio
= parent
->db_data_pending
->dr_zio
;
2879 /* Our parent is the dnode itself. */
2880 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2881 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2882 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2883 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2884 ASSERT3P(db
->db_blkptr
, ==,
2885 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2889 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2890 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2893 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2894 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2895 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2897 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2899 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2901 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2904 if (db
->db_level
== 0 && dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2905 ASSERT(db
->db_state
!= DB_NOFILL
);
2906 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2907 db
->db_blkptr
, data
->b_data
, arc_buf_size(data
), &zp
,
2908 dbuf_write_override_ready
, NULL
, dbuf_write_override_done
,
2909 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2910 mutex_enter(&db
->db_mtx
);
2911 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2912 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2913 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
2914 mutex_exit(&db
->db_mtx
);
2915 } else if (db
->db_state
== DB_NOFILL
) {
2916 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2917 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2918 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2919 dbuf_write_nofill_ready
, NULL
, dbuf_write_nofill_done
, db
,
2920 ZIO_PRIORITY_ASYNC_WRITE
,
2921 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2923 ASSERT(arc_released(data
));
2924 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2925 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
),
2926 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
2927 dbuf_write_physdone
, dbuf_write_done
, db
,
2928 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2932 #if defined(_KERNEL) && defined(HAVE_SPL)
2933 EXPORT_SYMBOL(dbuf_find
);
2934 EXPORT_SYMBOL(dbuf_is_metadata
);
2935 EXPORT_SYMBOL(dbuf_evict
);
2936 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
2937 EXPORT_SYMBOL(dbuf_whichblock
);
2938 EXPORT_SYMBOL(dbuf_read
);
2939 EXPORT_SYMBOL(dbuf_unoverride
);
2940 EXPORT_SYMBOL(dbuf_free_range
);
2941 EXPORT_SYMBOL(dbuf_new_size
);
2942 EXPORT_SYMBOL(dbuf_release_bp
);
2943 EXPORT_SYMBOL(dbuf_dirty
);
2944 EXPORT_SYMBOL(dmu_buf_will_dirty
);
2945 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
2946 EXPORT_SYMBOL(dmu_buf_will_fill
);
2947 EXPORT_SYMBOL(dmu_buf_fill_done
);
2948 EXPORT_SYMBOL(dmu_buf_rele
);
2949 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
2950 EXPORT_SYMBOL(dbuf_clear
);
2951 EXPORT_SYMBOL(dbuf_prefetch
);
2952 EXPORT_SYMBOL(dbuf_hold_impl
);
2953 EXPORT_SYMBOL(dbuf_hold
);
2954 EXPORT_SYMBOL(dbuf_hold_level
);
2955 EXPORT_SYMBOL(dbuf_create_bonus
);
2956 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
2957 EXPORT_SYMBOL(dbuf_rm_spill
);
2958 EXPORT_SYMBOL(dbuf_add_ref
);
2959 EXPORT_SYMBOL(dbuf_rele
);
2960 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
2961 EXPORT_SYMBOL(dbuf_refcount
);
2962 EXPORT_SYMBOL(dbuf_sync_list
);
2963 EXPORT_SYMBOL(dmu_buf_set_user
);
2964 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
2965 EXPORT_SYMBOL(dmu_buf_update_user
);
2966 EXPORT_SYMBOL(dmu_buf_get_user
);
2967 EXPORT_SYMBOL(dmu_buf_freeable
);