4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 #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>
49 * Number of times that zfs_free_range() took the slow path while doing
50 * a zfs receive. A nonzero value indicates a potential performance problem.
52 uint64_t zfs_free_range_recv_miss
;
54 static void dbuf_destroy(dmu_buf_impl_t
*db
);
55 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
56 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
59 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
60 dmu_buf_evict_func_t
*evict_func
, dmu_buf_t
**clear_on_evict_dbufp
);
64 * Global data structures and functions for the dbuf cache.
66 static kmem_cache_t
*dbuf_cache
;
67 static taskq_t
*dbu_evict_taskq
;
71 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
73 dmu_buf_impl_t
*db
= vdb
;
74 bzero(db
, sizeof (dmu_buf_impl_t
));
76 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
77 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
78 refcount_create(&db
->db_holds
);
85 dbuf_dest(void *vdb
, void *unused
)
87 dmu_buf_impl_t
*db
= vdb
;
88 mutex_destroy(&db
->db_mtx
);
89 cv_destroy(&db
->db_changed
);
90 refcount_destroy(&db
->db_holds
);
94 * dbuf hash table routines
96 static dbuf_hash_table_t dbuf_hash_table
;
98 static uint64_t dbuf_hash_count
;
101 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
103 uintptr_t osv
= (uintptr_t)os
;
104 uint64_t crc
= -1ULL;
106 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
107 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
108 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
109 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
110 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
111 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
112 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
114 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
119 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
121 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
122 ((dbuf)->db.db_object == (obj) && \
123 (dbuf)->db_objset == (os) && \
124 (dbuf)->db_level == (level) && \
125 (dbuf)->db_blkid == (blkid))
128 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
130 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
131 objset_t
*os
= dn
->dn_objset
;
132 uint64_t obj
= dn
->dn_object
;
133 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
134 uint64_t idx
= hv
& h
->hash_table_mask
;
137 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
138 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
139 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
140 mutex_enter(&db
->db_mtx
);
141 if (db
->db_state
!= DB_EVICTING
) {
142 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
145 mutex_exit(&db
->db_mtx
);
148 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
153 * Insert an entry into the hash table. If there is already an element
154 * equal to elem in the hash table, then the already existing element
155 * will be returned and the new element will not be inserted.
156 * Otherwise returns NULL.
158 static dmu_buf_impl_t
*
159 dbuf_hash_insert(dmu_buf_impl_t
*db
)
161 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
162 objset_t
*os
= db
->db_objset
;
163 uint64_t obj
= db
->db
.db_object
;
164 int level
= db
->db_level
;
165 uint64_t blkid
= db
->db_blkid
;
166 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
167 uint64_t idx
= hv
& h
->hash_table_mask
;
170 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
171 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
172 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
173 mutex_enter(&dbf
->db_mtx
);
174 if (dbf
->db_state
!= DB_EVICTING
) {
175 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
178 mutex_exit(&dbf
->db_mtx
);
182 mutex_enter(&db
->db_mtx
);
183 db
->db_hash_next
= h
->hash_table
[idx
];
184 h
->hash_table
[idx
] = db
;
185 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
186 atomic_inc_64(&dbuf_hash_count
);
192 * Remove an entry from the hash table. It must be in the EVICTING state.
195 dbuf_hash_remove(dmu_buf_impl_t
*db
)
197 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
198 uint64_t hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
199 db
->db_level
, db
->db_blkid
);
200 uint64_t idx
= hv
& h
->hash_table_mask
;
201 dmu_buf_impl_t
*dbf
, **dbp
;
204 * We musn't hold db_mtx to maintain lock ordering:
205 * DBUF_HASH_MUTEX > db_mtx.
207 ASSERT(refcount_is_zero(&db
->db_holds
));
208 ASSERT(db
->db_state
== DB_EVICTING
);
209 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
211 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
212 dbp
= &h
->hash_table
[idx
];
213 while ((dbf
= *dbp
) != db
) {
214 dbp
= &dbf
->db_hash_next
;
217 *dbp
= db
->db_hash_next
;
218 db
->db_hash_next
= NULL
;
219 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
220 atomic_dec_64(&dbuf_hash_count
);
223 static arc_evict_func_t dbuf_do_evict
;
228 } dbvu_verify_type_t
;
231 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
236 if (db
->db_user
== NULL
)
239 /* Only data blocks support the attachment of user data. */
240 ASSERT(db
->db_level
== 0);
242 /* Clients must resolve a dbuf before attaching user data. */
243 ASSERT(db
->db
.db_data
!= NULL
);
244 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
246 holds
= refcount_count(&db
->db_holds
);
247 if (verify_type
== DBVU_EVICTING
) {
249 * Immediate eviction occurs when holds == dirtycnt.
250 * For normal eviction buffers, holds is zero on
251 * eviction, except when dbuf_fix_old_data() calls
252 * dbuf_clear_data(). However, the hold count can grow
253 * during eviction even though db_mtx is held (see
254 * dmu_bonus_hold() for an example), so we can only
255 * test the generic invariant that holds >= dirtycnt.
257 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
259 if (db
->db_immediate_evict
== TRUE
)
260 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
262 ASSERT3U(holds
, >, 0);
268 dbuf_evict_user(dmu_buf_impl_t
*db
)
270 dmu_buf_user_t
*dbu
= db
->db_user
;
272 ASSERT(MUTEX_HELD(&db
->db_mtx
));
277 dbuf_verify_user(db
, DBVU_EVICTING
);
281 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
282 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
286 * Invoke the callback from a taskq to avoid lock order reversals
287 * and limit stack depth.
289 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func
, dbu
, 0,
294 dbuf_is_metadata(dmu_buf_impl_t
*db
)
296 if (db
->db_level
> 0) {
299 boolean_t is_metadata
;
302 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
305 return (is_metadata
);
310 dbuf_evict(dmu_buf_impl_t
*db
)
312 ASSERT(MUTEX_HELD(&db
->db_mtx
));
313 ASSERT(db
->db_buf
== NULL
);
314 ASSERT(db
->db_data_pending
== NULL
);
323 uint64_t hsize
= 1ULL << 16;
324 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
328 * The hash table is big enough to fill all of physical memory
329 * with an average 4K block size. The table will take up
330 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
332 while (hsize
* 4096 < physmem
* PAGESIZE
)
336 h
->hash_table_mask
= hsize
- 1;
337 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
338 if (h
->hash_table
== NULL
) {
339 /* XXX - we should really return an error instead of assert */
340 ASSERT(hsize
> (1ULL << 10));
345 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
346 sizeof (dmu_buf_impl_t
),
347 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
349 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
350 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
353 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
354 * configuration is not required.
356 dbu_evict_taskq
= taskq_create("dbu_evict", 1, minclsyspri
, 0, 0, 0);
362 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
365 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
366 mutex_destroy(&h
->hash_mutexes
[i
]);
367 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
368 kmem_cache_destroy(dbuf_cache
);
369 taskq_destroy(dbu_evict_taskq
);
378 dbuf_verify(dmu_buf_impl_t
*db
)
381 dbuf_dirty_record_t
*dr
;
383 ASSERT(MUTEX_HELD(&db
->db_mtx
));
385 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
388 ASSERT(db
->db_objset
!= NULL
);
392 ASSERT(db
->db_parent
== NULL
);
393 ASSERT(db
->db_blkptr
== NULL
);
395 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
396 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
397 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
398 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
399 db
->db_blkid
== DMU_SPILL_BLKID
||
400 !avl_is_empty(&dn
->dn_dbufs
));
402 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
404 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
405 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
406 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
408 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
409 ASSERT0(db
->db
.db_offset
);
411 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
414 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
415 ASSERT(dr
->dr_dbuf
== db
);
417 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
418 ASSERT(dr
->dr_dbuf
== db
);
421 * We can't assert that db_size matches dn_datablksz because it
422 * can be momentarily different when another thread is doing
425 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
426 dr
= db
->db_data_pending
;
428 * It should only be modified in syncing context, so
429 * make sure we only have one copy of the data.
431 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
434 /* verify db->db_blkptr */
436 if (db
->db_parent
== dn
->dn_dbuf
) {
437 /* db is pointed to by the dnode */
438 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
439 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
440 ASSERT(db
->db_parent
== NULL
);
442 ASSERT(db
->db_parent
!= NULL
);
443 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
444 ASSERT3P(db
->db_blkptr
, ==,
445 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
447 /* db is pointed to by an indirect block */
448 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
449 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
450 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
453 * dnode_grow_indblksz() can make this fail if we don't
454 * have the struct_rwlock. XXX indblksz no longer
455 * grows. safe to do this now?
457 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
458 ASSERT3P(db
->db_blkptr
, ==,
459 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
460 db
->db_blkid
% epb
));
464 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
465 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
466 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
467 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
469 * If the blkptr isn't set but they have nonzero data,
470 * it had better be dirty, otherwise we'll lose that
471 * data when we evict this buffer.
473 if (db
->db_dirtycnt
== 0) {
474 uint64_t *buf
= db
->db
.db_data
;
477 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
487 dbuf_clear_data(dmu_buf_impl_t
*db
)
489 ASSERT(MUTEX_HELD(&db
->db_mtx
));
492 db
->db
.db_data
= NULL
;
493 if (db
->db_state
!= DB_NOFILL
)
494 db
->db_state
= DB_UNCACHED
;
498 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
500 ASSERT(MUTEX_HELD(&db
->db_mtx
));
504 ASSERT(buf
->b_data
!= NULL
);
505 db
->db
.db_data
= buf
->b_data
;
506 if (!arc_released(buf
))
507 arc_set_callback(buf
, dbuf_do_evict
, db
);
511 * Loan out an arc_buf for read. Return the loaned arc_buf.
514 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
518 mutex_enter(&db
->db_mtx
);
519 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
520 int blksz
= db
->db
.db_size
;
521 spa_t
*spa
= db
->db_objset
->os_spa
;
523 mutex_exit(&db
->db_mtx
);
524 abuf
= arc_loan_buf(spa
, blksz
);
525 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
528 arc_loan_inuse_buf(abuf
, db
);
530 mutex_exit(&db
->db_mtx
);
536 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
538 if (dn
->dn_datablkshift
) {
539 return (offset
>> dn
->dn_datablkshift
);
541 ASSERT3U(offset
, <, dn
->dn_datablksz
);
547 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
549 dmu_buf_impl_t
*db
= vdb
;
551 mutex_enter(&db
->db_mtx
);
552 ASSERT3U(db
->db_state
, ==, DB_READ
);
554 * All reads are synchronous, so we must have a hold on the dbuf
556 ASSERT(refcount_count(&db
->db_holds
) > 0);
557 ASSERT(db
->db_buf
== NULL
);
558 ASSERT(db
->db
.db_data
== NULL
);
559 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
560 /* we were freed in flight; disregard any error */
561 arc_release(buf
, db
);
562 bzero(buf
->b_data
, db
->db
.db_size
);
564 db
->db_freed_in_flight
= FALSE
;
565 dbuf_set_data(db
, buf
);
566 db
->db_state
= DB_CACHED
;
567 } else if (zio
== NULL
|| zio
->io_error
== 0) {
568 dbuf_set_data(db
, buf
);
569 db
->db_state
= DB_CACHED
;
571 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
572 ASSERT3P(db
->db_buf
, ==, NULL
);
573 VERIFY(arc_buf_remove_ref(buf
, db
));
574 db
->db_state
= DB_UNCACHED
;
576 cv_broadcast(&db
->db_changed
);
577 dbuf_rele_and_unlock(db
, NULL
);
581 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
585 arc_flags_t aflags
= ARC_FLAG_NOWAIT
;
589 ASSERT(!refcount_is_zero(&db
->db_holds
));
590 /* We need the struct_rwlock to prevent db_blkptr from changing. */
591 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
592 ASSERT(MUTEX_HELD(&db
->db_mtx
));
593 ASSERT(db
->db_state
== DB_UNCACHED
);
594 ASSERT(db
->db_buf
== NULL
);
596 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
597 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
599 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
600 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
601 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
602 if (bonuslen
< DN_MAX_BONUSLEN
)
603 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
605 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
607 db
->db_state
= DB_CACHED
;
608 mutex_exit(&db
->db_mtx
);
613 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
614 * processes the delete record and clears the bp while we are waiting
615 * for the dn_mtx (resulting in a "no" from block_freed).
617 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
618 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
619 BP_IS_HOLE(db
->db_blkptr
)))) {
620 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
623 dbuf_set_data(db
, arc_buf_alloc(db
->db_objset
->os_spa
,
624 db
->db
.db_size
, db
, type
));
625 bzero(db
->db
.db_data
, db
->db
.db_size
);
626 db
->db_state
= DB_CACHED
;
627 *flags
|= DB_RF_CACHED
;
628 mutex_exit(&db
->db_mtx
);
634 db
->db_state
= DB_READ
;
635 mutex_exit(&db
->db_mtx
);
637 if (DBUF_IS_L2CACHEABLE(db
))
638 aflags
|= ARC_FLAG_L2CACHE
;
639 if (DBUF_IS_L2COMPRESSIBLE(db
))
640 aflags
|= ARC_FLAG_L2COMPRESS
;
642 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
643 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
644 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
646 dbuf_add_ref(db
, NULL
);
648 (void) arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
649 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
650 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
652 if (aflags
& ARC_FLAG_CACHED
)
653 *flags
|= DB_RF_CACHED
;
657 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
660 boolean_t havepzio
= (zio
!= NULL
);
665 * We don't have to hold the mutex to check db_state because it
666 * can't be freed while we have a hold on the buffer.
668 ASSERT(!refcount_is_zero(&db
->db_holds
));
670 if (db
->db_state
== DB_NOFILL
)
671 return (SET_ERROR(EIO
));
675 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
676 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
678 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
679 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
680 DBUF_IS_CACHEABLE(db
);
682 mutex_enter(&db
->db_mtx
);
683 if (db
->db_state
== DB_CACHED
) {
684 mutex_exit(&db
->db_mtx
);
686 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
687 db
->db
.db_size
, TRUE
);
688 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
689 rw_exit(&dn
->dn_struct_rwlock
);
691 } else if (db
->db_state
== DB_UNCACHED
) {
692 spa_t
*spa
= dn
->dn_objset
->os_spa
;
695 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
696 dbuf_read_impl(db
, zio
, &flags
);
698 /* dbuf_read_impl has dropped db_mtx for us */
701 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
702 db
->db
.db_size
, flags
& DB_RF_CACHED
);
704 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
705 rw_exit(&dn
->dn_struct_rwlock
);
712 * Another reader came in while the dbuf was in flight
713 * between UNCACHED and CACHED. Either a writer will finish
714 * writing the buffer (sending the dbuf to CACHED) or the
715 * first reader's request will reach the read_done callback
716 * and send the dbuf to CACHED. Otherwise, a failure
717 * occurred and the dbuf went to UNCACHED.
719 mutex_exit(&db
->db_mtx
);
721 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
722 db
->db
.db_size
, TRUE
);
723 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
724 rw_exit(&dn
->dn_struct_rwlock
);
727 /* Skip the wait per the caller's request. */
728 mutex_enter(&db
->db_mtx
);
729 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
730 while (db
->db_state
== DB_READ
||
731 db
->db_state
== DB_FILL
) {
732 ASSERT(db
->db_state
== DB_READ
||
733 (flags
& DB_RF_HAVESTRUCT
) == 0);
734 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
736 cv_wait(&db
->db_changed
, &db
->db_mtx
);
738 if (db
->db_state
== DB_UNCACHED
)
739 err
= SET_ERROR(EIO
);
741 mutex_exit(&db
->db_mtx
);
744 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
749 dbuf_noread(dmu_buf_impl_t
*db
)
751 ASSERT(!refcount_is_zero(&db
->db_holds
));
752 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
753 mutex_enter(&db
->db_mtx
);
754 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
755 cv_wait(&db
->db_changed
, &db
->db_mtx
);
756 if (db
->db_state
== DB_UNCACHED
) {
757 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
758 spa_t
*spa
= db
->db_objset
->os_spa
;
760 ASSERT(db
->db_buf
== NULL
);
761 ASSERT(db
->db
.db_data
== NULL
);
762 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
763 db
->db_state
= DB_FILL
;
764 } else if (db
->db_state
== DB_NOFILL
) {
767 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
769 mutex_exit(&db
->db_mtx
);
773 * This is our just-in-time copy function. It makes a copy of
774 * buffers, that have been modified in a previous transaction
775 * group, before we modify them in the current active group.
777 * This function is used in two places: when we are dirtying a
778 * buffer for the first time in a txg, and when we are freeing
779 * a range in a dnode that includes this buffer.
781 * Note that when we are called from dbuf_free_range() we do
782 * not put a hold on the buffer, we just traverse the active
783 * dbuf list for the dnode.
786 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
788 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
790 ASSERT(MUTEX_HELD(&db
->db_mtx
));
791 ASSERT(db
->db
.db_data
!= NULL
);
792 ASSERT(db
->db_level
== 0);
793 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
796 (dr
->dt
.dl
.dr_data
!=
797 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
801 * If the last dirty record for this dbuf has not yet synced
802 * and its referencing the dbuf data, either:
803 * reset the reference to point to a new copy,
804 * or (if there a no active holders)
805 * just null out the current db_data pointer.
807 ASSERT(dr
->dr_txg
>= txg
- 2);
808 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
809 /* Note that the data bufs here are zio_bufs */
810 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
811 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
812 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
813 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
814 int size
= db
->db
.db_size
;
815 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
816 spa_t
*spa
= db
->db_objset
->os_spa
;
818 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
819 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
826 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
828 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
829 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
830 uint64_t txg
= dr
->dr_txg
;
832 ASSERT(MUTEX_HELD(&db
->db_mtx
));
833 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
834 ASSERT(db
->db_level
== 0);
836 if (db
->db_blkid
== DMU_BONUS_BLKID
||
837 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
840 ASSERT(db
->db_data_pending
!= dr
);
842 /* free this block */
843 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
844 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
846 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
847 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
850 * Release the already-written buffer, so we leave it in
851 * a consistent dirty state. Note that all callers are
852 * modifying the buffer, so they will immediately do
853 * another (redundant) arc_release(). Therefore, leave
854 * the buf thawed to save the effort of freezing &
855 * immediately re-thawing it.
857 arc_release(dr
->dt
.dl
.dr_data
, db
);
861 * Evict (if its unreferenced) or clear (if its referenced) any level-0
862 * data blocks in the free range, so that any future readers will find
865 * This is a no-op if the dataset is in the middle of an incremental
866 * receive; see comment below for details.
869 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
872 dmu_buf_impl_t db_search
;
873 dmu_buf_impl_t
*db
, *db_next
;
874 uint64_t txg
= tx
->tx_txg
;
877 if (end_blkid
> dn
->dn_maxblkid
&& (end_blkid
!= DMU_SPILL_BLKID
))
878 end_blkid
= dn
->dn_maxblkid
;
879 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
881 db_search
.db_level
= 0;
882 db_search
.db_blkid
= start_blkid
;
883 db_search
.db_state
= DB_SEARCH
;
885 mutex_enter(&dn
->dn_dbufs_mtx
);
886 if (start_blkid
>= dn
->dn_unlisted_l0_blkid
) {
887 /* There can't be any dbufs in this range; no need to search. */
889 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
890 ASSERT3P(db
, ==, NULL
);
891 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
892 ASSERT(db
== NULL
|| db
->db_level
> 0);
894 mutex_exit(&dn
->dn_dbufs_mtx
);
896 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
898 * If we are receiving, we expect there to be no dbufs in
899 * the range to be freed, because receive modifies each
900 * block at most once, and in offset order. If this is
901 * not the case, it can lead to performance problems,
902 * so note that we unexpectedly took the slow path.
904 atomic_inc_64(&zfs_free_range_recv_miss
);
907 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
908 ASSERT3P(db
, ==, NULL
);
909 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
911 for (; db
!= NULL
; db
= db_next
) {
912 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
913 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
915 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
918 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
920 /* found a level 0 buffer in the range */
921 mutex_enter(&db
->db_mtx
);
922 if (dbuf_undirty(db
, tx
)) {
923 /* mutex has been dropped and dbuf destroyed */
927 if (db
->db_state
== DB_UNCACHED
||
928 db
->db_state
== DB_NOFILL
||
929 db
->db_state
== DB_EVICTING
) {
930 ASSERT(db
->db
.db_data
== NULL
);
931 mutex_exit(&db
->db_mtx
);
934 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
935 /* will be handled in dbuf_read_done or dbuf_rele */
936 db
->db_freed_in_flight
= TRUE
;
937 mutex_exit(&db
->db_mtx
);
940 if (refcount_count(&db
->db_holds
) == 0) {
945 /* The dbuf is referenced */
947 if (db
->db_last_dirty
!= NULL
) {
948 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
950 if (dr
->dr_txg
== txg
) {
952 * This buffer is "in-use", re-adjust the file
953 * size to reflect that this buffer may
954 * contain new data when we sync.
956 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
957 db
->db_blkid
> dn
->dn_maxblkid
)
958 dn
->dn_maxblkid
= db
->db_blkid
;
962 * This dbuf is not dirty in the open context.
963 * Either uncache it (if its not referenced in
964 * the open context) or reset its contents to
967 dbuf_fix_old_data(db
, txg
);
970 /* clear the contents if its cached */
971 if (db
->db_state
== DB_CACHED
) {
972 ASSERT(db
->db
.db_data
!= NULL
);
973 arc_release(db
->db_buf
, db
);
974 bzero(db
->db
.db_data
, db
->db
.db_size
);
975 arc_buf_freeze(db
->db_buf
);
978 mutex_exit(&db
->db_mtx
);
980 mutex_exit(&dn
->dn_dbufs_mtx
);
984 dbuf_block_freeable(dmu_buf_impl_t
*db
)
986 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
987 uint64_t birth_txg
= 0;
990 * We don't need any locking to protect db_blkptr:
991 * If it's syncing, then db_last_dirty will be set
992 * so we'll ignore db_blkptr.
994 * This logic ensures that only block births for
995 * filled blocks are considered.
997 ASSERT(MUTEX_HELD(&db
->db_mtx
));
998 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
999 !BP_IS_HOLE(db
->db_blkptr
))) {
1000 birth_txg
= db
->db_last_dirty
->dr_txg
;
1001 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1002 birth_txg
= db
->db_blkptr
->blk_birth
;
1006 * If this block don't exist or is in a snapshot, it can't be freed.
1007 * Don't pass the bp to dsl_dataset_block_freeable() since we
1008 * are holding the db_mtx lock and might deadlock if we are
1009 * prefetching a dedup-ed block.
1012 return (ds
== NULL
||
1013 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1019 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1021 arc_buf_t
*buf
, *obuf
;
1022 int osize
= db
->db
.db_size
;
1023 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1026 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1031 /* XXX does *this* func really need the lock? */
1032 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1035 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1036 * is OK, because there can be no other references to the db
1037 * when we are changing its size, so no concurrent DB_FILL can
1041 * XXX we should be doing a dbuf_read, checking the return
1042 * value and returning that up to our callers
1044 dmu_buf_will_dirty(&db
->db
, tx
);
1046 /* create the data buffer for the new block */
1047 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
1049 /* copy old block data to the new block */
1051 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1052 /* zero the remainder */
1054 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1056 mutex_enter(&db
->db_mtx
);
1057 dbuf_set_data(db
, buf
);
1058 VERIFY(arc_buf_remove_ref(obuf
, db
));
1059 db
->db
.db_size
= size
;
1061 if (db
->db_level
== 0) {
1062 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1063 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1065 mutex_exit(&db
->db_mtx
);
1067 dnode_willuse_space(dn
, size
-osize
, tx
);
1072 dbuf_release_bp(dmu_buf_impl_t
*db
)
1074 objset_t
*os
= db
->db_objset
;
1076 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1077 ASSERT(arc_released(os
->os_phys_buf
) ||
1078 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1079 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1081 (void) arc_release(db
->db_buf
, db
);
1084 dbuf_dirty_record_t
*
1085 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1089 dbuf_dirty_record_t
**drp
, *dr
;
1090 int drop_struct_lock
= FALSE
;
1091 boolean_t do_free_accounting
= B_FALSE
;
1092 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1094 ASSERT(tx
->tx_txg
!= 0);
1095 ASSERT(!refcount_is_zero(&db
->db_holds
));
1096 DMU_TX_DIRTY_BUF(tx
, db
);
1101 * Shouldn't dirty a regular buffer in syncing context. Private
1102 * objects may be dirtied in syncing context, but only if they
1103 * were already pre-dirtied in open context.
1105 ASSERT(!dmu_tx_is_syncing(tx
) ||
1106 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1107 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1108 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1110 * We make this assert for private objects as well, but after we
1111 * check if we're already dirty. They are allowed to re-dirty
1112 * in syncing context.
1114 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1115 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1116 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1118 mutex_enter(&db
->db_mtx
);
1120 * XXX make this true for indirects too? The problem is that
1121 * transactions created with dmu_tx_create_assigned() from
1122 * syncing context don't bother holding ahead.
1124 ASSERT(db
->db_level
!= 0 ||
1125 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1126 db
->db_state
== DB_NOFILL
);
1128 mutex_enter(&dn
->dn_mtx
);
1130 * Don't set dirtyctx to SYNC if we're just modifying this as we
1131 * initialize the objset.
1133 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1134 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1136 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1137 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1138 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1140 mutex_exit(&dn
->dn_mtx
);
1142 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1143 dn
->dn_have_spill
= B_TRUE
;
1146 * If this buffer is already dirty, we're done.
1148 drp
= &db
->db_last_dirty
;
1149 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1150 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1151 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1153 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1156 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1158 * If this buffer has already been written out,
1159 * we now need to reset its state.
1161 dbuf_unoverride(dr
);
1162 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1163 db
->db_state
!= DB_NOFILL
)
1164 arc_buf_thaw(db
->db_buf
);
1166 mutex_exit(&db
->db_mtx
);
1171 * Only valid if not already dirty.
1173 ASSERT(dn
->dn_object
== 0 ||
1174 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1175 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1177 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1178 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1179 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1180 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1181 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1182 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1185 * We should only be dirtying in syncing context if it's the
1186 * mos or we're initializing the os or it's a special object.
1187 * However, we are allowed to dirty in syncing context provided
1188 * we already dirtied it in open context. Hence we must make
1189 * this assertion only if we're not already dirty.
1192 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1193 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1194 ASSERT(db
->db
.db_size
!= 0);
1196 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1198 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1200 * Update the accounting.
1201 * Note: we delay "free accounting" until after we drop
1202 * the db_mtx. This keeps us from grabbing other locks
1203 * (and possibly deadlocking) in bp_get_dsize() while
1204 * also holding the db_mtx.
1206 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1207 do_free_accounting
= dbuf_block_freeable(db
);
1211 * If this buffer is dirty in an old transaction group we need
1212 * to make a copy of it so that the changes we make in this
1213 * transaction group won't leak out when we sync the older txg.
1215 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1216 if (db
->db_level
== 0) {
1217 void *data_old
= db
->db_buf
;
1219 if (db
->db_state
!= DB_NOFILL
) {
1220 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1221 dbuf_fix_old_data(db
, tx
->tx_txg
);
1222 data_old
= db
->db
.db_data
;
1223 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1225 * Release the data buffer from the cache so
1226 * that we can modify it without impacting
1227 * possible other users of this cached data
1228 * block. Note that indirect blocks and
1229 * private objects are not released until the
1230 * syncing state (since they are only modified
1233 arc_release(db
->db_buf
, db
);
1234 dbuf_fix_old_data(db
, tx
->tx_txg
);
1235 data_old
= db
->db_buf
;
1237 ASSERT(data_old
!= NULL
);
1239 dr
->dt
.dl
.dr_data
= data_old
;
1241 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1242 list_create(&dr
->dt
.di
.dr_children
,
1243 sizeof (dbuf_dirty_record_t
),
1244 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1246 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1247 dr
->dr_accounted
= db
->db
.db_size
;
1249 dr
->dr_txg
= tx
->tx_txg
;
1254 * We could have been freed_in_flight between the dbuf_noread
1255 * and dbuf_dirty. We win, as though the dbuf_noread() had
1256 * happened after the free.
1258 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1259 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1260 mutex_enter(&dn
->dn_mtx
);
1261 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1262 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1265 mutex_exit(&dn
->dn_mtx
);
1266 db
->db_freed_in_flight
= FALSE
;
1270 * This buffer is now part of this txg
1272 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1273 db
->db_dirtycnt
+= 1;
1274 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1276 mutex_exit(&db
->db_mtx
);
1278 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1279 db
->db_blkid
== DMU_SPILL_BLKID
) {
1280 mutex_enter(&dn
->dn_mtx
);
1281 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1282 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1283 mutex_exit(&dn
->dn_mtx
);
1284 dnode_setdirty(dn
, tx
);
1287 } else if (do_free_accounting
) {
1288 blkptr_t
*bp
= db
->db_blkptr
;
1289 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1290 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1292 * This is only a guess -- if the dbuf is dirty
1293 * in a previous txg, we don't know how much
1294 * space it will use on disk yet. We should
1295 * really have the struct_rwlock to access
1296 * db_blkptr, but since this is just a guess,
1297 * it's OK if we get an odd answer.
1299 ddt_prefetch(os
->os_spa
, bp
);
1300 dnode_willuse_space(dn
, -willfree
, tx
);
1303 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1304 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1305 drop_struct_lock
= TRUE
;
1308 if (db
->db_level
== 0) {
1309 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1310 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1313 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1314 dmu_buf_impl_t
*parent
= db
->db_parent
;
1315 dbuf_dirty_record_t
*di
;
1316 int parent_held
= FALSE
;
1318 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1319 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1321 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1322 db
->db_blkid
>> epbs
, FTAG
);
1323 ASSERT(parent
!= NULL
);
1326 if (drop_struct_lock
)
1327 rw_exit(&dn
->dn_struct_rwlock
);
1328 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1329 di
= dbuf_dirty(parent
, tx
);
1331 dbuf_rele(parent
, FTAG
);
1333 mutex_enter(&db
->db_mtx
);
1335 * Since we've dropped the mutex, it's possible that
1336 * dbuf_undirty() might have changed this out from under us.
1338 if (db
->db_last_dirty
== dr
||
1339 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1340 mutex_enter(&di
->dt
.di
.dr_mtx
);
1341 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1342 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1343 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1344 mutex_exit(&di
->dt
.di
.dr_mtx
);
1347 mutex_exit(&db
->db_mtx
);
1349 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1350 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1351 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1352 mutex_enter(&dn
->dn_mtx
);
1353 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1354 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1355 mutex_exit(&dn
->dn_mtx
);
1356 if (drop_struct_lock
)
1357 rw_exit(&dn
->dn_struct_rwlock
);
1360 dnode_setdirty(dn
, tx
);
1366 * Undirty a buffer in the transaction group referenced by the given
1367 * transaction. Return whether this evicted the dbuf.
1370 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1373 uint64_t txg
= tx
->tx_txg
;
1374 dbuf_dirty_record_t
*dr
, **drp
;
1377 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1378 ASSERT0(db
->db_level
);
1379 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1382 * If this buffer is not dirty, we're done.
1384 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1385 if (dr
->dr_txg
<= txg
)
1387 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1389 ASSERT(dr
->dr_txg
== txg
);
1390 ASSERT(dr
->dr_dbuf
== db
);
1395 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1397 ASSERT(db
->db
.db_size
!= 0);
1400 * Any space we accounted for in dp_dirty_* will be cleaned up by
1401 * dsl_pool_sync(). This is relatively rare so the discrepancy
1402 * is not a big deal.
1408 * Note that there are three places in dbuf_dirty()
1409 * where this dirty record may be put on a list.
1410 * Make sure to do a list_remove corresponding to
1411 * every one of those list_insert calls.
1413 if (dr
->dr_parent
) {
1414 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1415 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1416 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1417 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1418 db
->db_level
+1 == dn
->dn_nlevels
) {
1419 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1420 mutex_enter(&dn
->dn_mtx
);
1421 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1422 mutex_exit(&dn
->dn_mtx
);
1426 if (db
->db_state
!= DB_NOFILL
) {
1427 dbuf_unoverride(dr
);
1429 ASSERT(db
->db_buf
!= NULL
);
1430 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1431 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1432 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
, db
));
1435 if (db
->db_level
!= 0) {
1436 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
1437 list_destroy(&dr
->dt
.di
.dr_children
);
1440 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1442 ASSERT(db
->db_dirtycnt
> 0);
1443 db
->db_dirtycnt
-= 1;
1445 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1446 arc_buf_t
*buf
= db
->db_buf
;
1448 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1449 dbuf_clear_data(db
);
1450 VERIFY(arc_buf_remove_ref(buf
, db
));
1459 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1461 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1462 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1464 ASSERT(tx
->tx_txg
!= 0);
1465 ASSERT(!refcount_is_zero(&db
->db_holds
));
1468 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1469 rf
|= DB_RF_HAVESTRUCT
;
1471 (void) dbuf_read(db
, NULL
, rf
);
1472 (void) dbuf_dirty(db
, tx
);
1476 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1478 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1480 db
->db_state
= DB_NOFILL
;
1482 dmu_buf_will_fill(db_fake
, tx
);
1486 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1488 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1490 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1491 ASSERT(tx
->tx_txg
!= 0);
1492 ASSERT(db
->db_level
== 0);
1493 ASSERT(!refcount_is_zero(&db
->db_holds
));
1495 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1496 dmu_tx_private_ok(tx
));
1499 (void) dbuf_dirty(db
, tx
);
1502 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1505 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1507 mutex_enter(&db
->db_mtx
);
1510 if (db
->db_state
== DB_FILL
) {
1511 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1512 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1513 /* we were freed while filling */
1514 /* XXX dbuf_undirty? */
1515 bzero(db
->db
.db_data
, db
->db
.db_size
);
1516 db
->db_freed_in_flight
= FALSE
;
1518 db
->db_state
= DB_CACHED
;
1519 cv_broadcast(&db
->db_changed
);
1521 mutex_exit(&db
->db_mtx
);
1525 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1526 bp_embedded_type_t etype
, enum zio_compress comp
,
1527 int uncompressed_size
, int compressed_size
, int byteorder
,
1530 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1531 struct dirty_leaf
*dl
;
1532 dmu_object_type_t type
;
1535 type
= DB_DNODE(db
)->dn_type
;
1538 ASSERT0(db
->db_level
);
1539 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1541 dmu_buf_will_not_fill(dbuf
, tx
);
1543 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1544 dl
= &db
->db_last_dirty
->dt
.dl
;
1545 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1546 data
, comp
, uncompressed_size
, compressed_size
);
1547 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1548 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1549 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1550 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1552 dl
->dr_override_state
= DR_OVERRIDDEN
;
1553 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1557 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1558 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1561 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1563 ASSERT(!refcount_is_zero(&db
->db_holds
));
1564 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1565 ASSERT(db
->db_level
== 0);
1566 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1567 ASSERT(buf
!= NULL
);
1568 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1569 ASSERT(tx
->tx_txg
!= 0);
1571 arc_return_buf(buf
, db
);
1572 ASSERT(arc_released(buf
));
1574 mutex_enter(&db
->db_mtx
);
1576 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1577 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1579 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1581 if (db
->db_state
== DB_CACHED
&&
1582 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1583 mutex_exit(&db
->db_mtx
);
1584 (void) dbuf_dirty(db
, tx
);
1585 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1586 VERIFY(arc_buf_remove_ref(buf
, db
));
1587 xuio_stat_wbuf_copied();
1591 xuio_stat_wbuf_nocopy();
1592 if (db
->db_state
== DB_CACHED
) {
1593 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1595 ASSERT(db
->db_buf
!= NULL
);
1596 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1597 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1598 if (!arc_released(db
->db_buf
)) {
1599 ASSERT(dr
->dt
.dl
.dr_override_state
==
1601 arc_release(db
->db_buf
, db
);
1603 dr
->dt
.dl
.dr_data
= buf
;
1604 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1605 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1606 arc_release(db
->db_buf
, db
);
1607 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
));
1611 ASSERT(db
->db_buf
== NULL
);
1612 dbuf_set_data(db
, buf
);
1613 db
->db_state
= DB_FILL
;
1614 mutex_exit(&db
->db_mtx
);
1615 (void) dbuf_dirty(db
, tx
);
1616 dmu_buf_fill_done(&db
->db
, tx
);
1620 * "Clear" the contents of this dbuf. This will mark the dbuf
1621 * EVICTING and clear *most* of its references. Unfortunately,
1622 * when we are not holding the dn_dbufs_mtx, we can't clear the
1623 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1624 * in this case. For callers from the DMU we will usually see:
1625 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1626 * For the arc callback, we will usually see:
1627 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1628 * Sometimes, though, we will get a mix of these two:
1629 * DMU: dbuf_clear()->arc_clear_callback()
1630 * ARC: dbuf_do_evict()->dbuf_destroy()
1632 * This routine will dissociate the dbuf from the arc, by calling
1633 * arc_clear_callback(), but will not evict the data from the ARC.
1636 dbuf_clear(dmu_buf_impl_t
*db
)
1639 dmu_buf_impl_t
*parent
= db
->db_parent
;
1640 dmu_buf_impl_t
*dndb
;
1641 boolean_t dbuf_gone
= B_FALSE
;
1643 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1644 ASSERT(refcount_is_zero(&db
->db_holds
));
1646 dbuf_evict_user(db
);
1648 if (db
->db_state
== DB_CACHED
) {
1649 ASSERT(db
->db
.db_data
!= NULL
);
1650 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1651 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1652 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1654 db
->db
.db_data
= NULL
;
1655 db
->db_state
= DB_UNCACHED
;
1658 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1659 ASSERT(db
->db_data_pending
== NULL
);
1661 db
->db_state
= DB_EVICTING
;
1662 db
->db_blkptr
= NULL
;
1667 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1668 avl_remove(&dn
->dn_dbufs
, db
);
1669 atomic_dec_32(&dn
->dn_dbufs_count
);
1673 * Decrementing the dbuf count means that the hold corresponding
1674 * to the removed dbuf is no longer discounted in dnode_move(),
1675 * so the dnode cannot be moved until after we release the hold.
1676 * The membar_producer() ensures visibility of the decremented
1677 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1681 db
->db_dnode_handle
= NULL
;
1687 dbuf_gone
= arc_clear_callback(db
->db_buf
);
1690 mutex_exit(&db
->db_mtx
);
1693 * If this dbuf is referenced from an indirect dbuf,
1694 * decrement the ref count on the indirect dbuf.
1696 if (parent
&& parent
!= dndb
)
1697 dbuf_rele(parent
, db
);
1701 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1702 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
1709 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1711 if (blkid
== DMU_SPILL_BLKID
) {
1712 mutex_enter(&dn
->dn_mtx
);
1713 if (dn
->dn_have_spill
&&
1714 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1715 *bpp
= &dn
->dn_phys
->dn_spill
;
1718 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1719 *parentp
= dn
->dn_dbuf
;
1720 mutex_exit(&dn
->dn_mtx
);
1724 if (dn
->dn_phys
->dn_nlevels
== 0)
1727 nlevels
= dn
->dn_phys
->dn_nlevels
;
1729 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1731 ASSERT3U(level
* epbs
, <, 64);
1732 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1733 if (level
>= nlevels
||
1734 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1735 /* the buffer has no parent yet */
1736 return (SET_ERROR(ENOENT
));
1737 } else if (level
< nlevels
-1) {
1738 /* this block is referenced from an indirect block */
1739 int err
= dbuf_hold_impl(dn
, level
+1,
1740 blkid
>> epbs
, fail_sparse
, NULL
, parentp
);
1743 err
= dbuf_read(*parentp
, NULL
,
1744 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1746 dbuf_rele(*parentp
, NULL
);
1750 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1751 (blkid
& ((1ULL << epbs
) - 1));
1754 /* the block is referenced from the dnode */
1755 ASSERT3U(level
, ==, nlevels
-1);
1756 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1757 blkid
< dn
->dn_phys
->dn_nblkptr
);
1759 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1760 *parentp
= dn
->dn_dbuf
;
1762 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1767 static dmu_buf_impl_t
*
1768 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1769 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1771 objset_t
*os
= dn
->dn_objset
;
1772 dmu_buf_impl_t
*db
, *odb
;
1774 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1775 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1777 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
1780 db
->db
.db_object
= dn
->dn_object
;
1781 db
->db_level
= level
;
1782 db
->db_blkid
= blkid
;
1783 db
->db_last_dirty
= NULL
;
1784 db
->db_dirtycnt
= 0;
1785 db
->db_dnode_handle
= dn
->dn_handle
;
1786 db
->db_parent
= parent
;
1787 db
->db_blkptr
= blkptr
;
1790 db
->db_immediate_evict
= 0;
1791 db
->db_freed_in_flight
= 0;
1793 if (blkid
== DMU_BONUS_BLKID
) {
1794 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1795 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1796 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1797 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1798 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1799 db
->db_state
= DB_UNCACHED
;
1800 /* the bonus dbuf is not placed in the hash table */
1801 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1803 } else if (blkid
== DMU_SPILL_BLKID
) {
1804 db
->db
.db_size
= (blkptr
!= NULL
) ?
1805 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1806 db
->db
.db_offset
= 0;
1809 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
1810 db
->db
.db_size
= blocksize
;
1811 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1815 * Hold the dn_dbufs_mtx while we get the new dbuf
1816 * in the hash table *and* added to the dbufs list.
1817 * This prevents a possible deadlock with someone
1818 * trying to look up this dbuf before its added to the
1821 mutex_enter(&dn
->dn_dbufs_mtx
);
1822 db
->db_state
= DB_EVICTING
;
1823 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1824 /* someone else inserted it first */
1825 kmem_cache_free(dbuf_cache
, db
);
1826 mutex_exit(&dn
->dn_dbufs_mtx
);
1829 avl_add(&dn
->dn_dbufs
, db
);
1830 if (db
->db_level
== 0 && db
->db_blkid
>=
1831 dn
->dn_unlisted_l0_blkid
)
1832 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
1833 db
->db_state
= DB_UNCACHED
;
1834 mutex_exit(&dn
->dn_dbufs_mtx
);
1835 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1837 if (parent
&& parent
!= dn
->dn_dbuf
)
1838 dbuf_add_ref(parent
, db
);
1840 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1841 refcount_count(&dn
->dn_holds
) > 0);
1842 (void) refcount_add(&dn
->dn_holds
, db
);
1843 atomic_inc_32(&dn
->dn_dbufs_count
);
1845 dprintf_dbuf(db
, "db=%p\n", db
);
1851 dbuf_do_evict(void *private)
1853 dmu_buf_impl_t
*db
= private;
1855 if (!MUTEX_HELD(&db
->db_mtx
))
1856 mutex_enter(&db
->db_mtx
);
1858 ASSERT(refcount_is_zero(&db
->db_holds
));
1860 if (db
->db_state
!= DB_EVICTING
) {
1861 ASSERT(db
->db_state
== DB_CACHED
);
1866 mutex_exit(&db
->db_mtx
);
1873 dbuf_destroy(dmu_buf_impl_t
*db
)
1875 ASSERT(refcount_is_zero(&db
->db_holds
));
1877 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1879 * If this dbuf is still on the dn_dbufs list,
1880 * remove it from that list.
1882 if (db
->db_dnode_handle
!= NULL
) {
1887 mutex_enter(&dn
->dn_dbufs_mtx
);
1888 avl_remove(&dn
->dn_dbufs
, db
);
1889 atomic_dec_32(&dn
->dn_dbufs_count
);
1890 mutex_exit(&dn
->dn_dbufs_mtx
);
1893 * Decrementing the dbuf count means that the hold
1894 * corresponding to the removed dbuf is no longer
1895 * discounted in dnode_move(), so the dnode cannot be
1896 * moved until after we release the hold.
1899 db
->db_dnode_handle
= NULL
;
1901 dbuf_hash_remove(db
);
1903 db
->db_parent
= NULL
;
1906 ASSERT(db
->db
.db_data
== NULL
);
1907 ASSERT(db
->db_hash_next
== NULL
);
1908 ASSERT(db
->db_blkptr
== NULL
);
1909 ASSERT(db
->db_data_pending
== NULL
);
1911 kmem_cache_free(dbuf_cache
, db
);
1912 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1916 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
, zio_priority_t prio
)
1918 dmu_buf_impl_t
*db
= NULL
;
1919 blkptr_t
*bp
= NULL
;
1921 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1922 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1924 if (dnode_block_freed(dn
, blkid
))
1927 /* dbuf_find() returns with db_mtx held */
1928 if (db
= dbuf_find(dn
, 0, blkid
)) {
1930 * This dbuf is already in the cache. We assume that
1931 * it is already CACHED, or else about to be either
1934 mutex_exit(&db
->db_mtx
);
1938 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
) == 0) {
1939 if (bp
&& !BP_IS_HOLE(bp
) && !BP_IS_EMBEDDED(bp
)) {
1940 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1941 arc_flags_t aflags
=
1942 ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
1943 zbookmark_phys_t zb
;
1945 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1946 dn
->dn_object
, 0, blkid
);
1948 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1949 bp
, NULL
, NULL
, prio
,
1950 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1954 dbuf_rele(db
, NULL
);
1959 * Returns with db_holds incremented, and db_mtx not held.
1960 * Note: dn_struct_rwlock must be held.
1963 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
1964 void *tag
, dmu_buf_impl_t
**dbp
)
1966 dmu_buf_impl_t
*db
, *parent
= NULL
;
1968 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1969 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1970 ASSERT3U(dn
->dn_nlevels
, >, level
);
1974 /* dbuf_find() returns with db_mtx held */
1975 db
= dbuf_find(dn
, level
, blkid
);
1978 blkptr_t
*bp
= NULL
;
1981 ASSERT3P(parent
, ==, NULL
);
1982 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
1984 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
1985 err
= SET_ERROR(ENOENT
);
1988 dbuf_rele(parent
, NULL
);
1992 if (err
&& err
!= ENOENT
)
1994 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
1997 if (db
->db_buf
&& refcount_is_zero(&db
->db_holds
)) {
1998 arc_buf_add_ref(db
->db_buf
, db
);
1999 if (db
->db_buf
->b_data
== NULL
) {
2002 dbuf_rele(parent
, NULL
);
2007 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
2010 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
2013 * If this buffer is currently syncing out, and we are are
2014 * still referencing it from db_data, we need to make a copy
2015 * of it in case we decide we want to dirty it again in this txg.
2017 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2018 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2019 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
2020 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
2022 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
2023 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2026 arc_buf_alloc(dn
->dn_objset
->os_spa
,
2027 db
->db
.db_size
, db
, type
));
2028 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
2033 (void) refcount_add(&db
->db_holds
, tag
);
2035 mutex_exit(&db
->db_mtx
);
2037 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2039 dbuf_rele(parent
, NULL
);
2041 ASSERT3P(DB_DNODE(db
), ==, dn
);
2042 ASSERT3U(db
->db_blkid
, ==, blkid
);
2043 ASSERT3U(db
->db_level
, ==, level
);
2050 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2053 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
2054 return (err
? NULL
: db
);
2058 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2061 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
2062 return (err
? NULL
: db
);
2066 dbuf_create_bonus(dnode_t
*dn
)
2068 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2070 ASSERT(dn
->dn_bonus
== NULL
);
2071 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2075 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2077 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2080 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2081 return (SET_ERROR(ENOTSUP
));
2083 blksz
= SPA_MINBLOCKSIZE
;
2084 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2085 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2089 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2090 dbuf_new_size(db
, blksz
, tx
);
2091 rw_exit(&dn
->dn_struct_rwlock
);
2098 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2100 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2103 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2105 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2107 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2112 * If you call dbuf_rele() you had better not be referencing the dnode handle
2113 * unless you have some other direct or indirect hold on the dnode. (An indirect
2114 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2115 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2116 * dnode's parent dbuf evicting its dnode handles.
2119 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2121 mutex_enter(&db
->db_mtx
);
2122 dbuf_rele_and_unlock(db
, tag
);
2126 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2128 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2132 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2133 * db_dirtycnt and db_holds to be updated atomically.
2136 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2140 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2144 * Remove the reference to the dbuf before removing its hold on the
2145 * dnode so we can guarantee in dnode_move() that a referenced bonus
2146 * buffer has a corresponding dnode hold.
2148 holds
= refcount_remove(&db
->db_holds
, tag
);
2152 * We can't freeze indirects if there is a possibility that they
2153 * may be modified in the current syncing context.
2155 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2156 arc_buf_freeze(db
->db_buf
);
2158 if (holds
== db
->db_dirtycnt
&&
2159 db
->db_level
== 0 && db
->db_immediate_evict
)
2160 dbuf_evict_user(db
);
2163 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2164 mutex_exit(&db
->db_mtx
);
2167 * If the dnode moves here, we cannot cross this barrier
2168 * until the move completes.
2171 atomic_dec_32(&DB_DNODE(db
)->dn_dbufs_count
);
2174 * The bonus buffer's dnode hold is no longer discounted
2175 * in dnode_move(). The dnode cannot move until after
2178 dnode_rele(DB_DNODE(db
), db
);
2179 } else if (db
->db_buf
== NULL
) {
2181 * This is a special case: we never associated this
2182 * dbuf with any data allocated from the ARC.
2184 ASSERT(db
->db_state
== DB_UNCACHED
||
2185 db
->db_state
== DB_NOFILL
);
2187 } else if (arc_released(db
->db_buf
)) {
2188 arc_buf_t
*buf
= db
->db_buf
;
2190 * This dbuf has anonymous data associated with it.
2192 dbuf_clear_data(db
);
2193 VERIFY(arc_buf_remove_ref(buf
, db
));
2196 VERIFY(!arc_buf_remove_ref(db
->db_buf
, db
));
2199 * A dbuf will be eligible for eviction if either the
2200 * 'primarycache' property is set or a duplicate
2201 * copy of this buffer is already cached in the arc.
2203 * In the case of the 'primarycache' a buffer
2204 * is considered for eviction if it matches the
2205 * criteria set in the property.
2207 * To decide if our buffer is considered a
2208 * duplicate, we must call into the arc to determine
2209 * if multiple buffers are referencing the same
2210 * block on-disk. If so, then we simply evict
2213 if (!DBUF_IS_CACHEABLE(db
)) {
2214 if (db
->db_blkptr
!= NULL
&&
2215 !BP_IS_HOLE(db
->db_blkptr
) &&
2216 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2218 dmu_objset_spa(db
->db_objset
);
2219 blkptr_t bp
= *db
->db_blkptr
;
2221 arc_freed(spa
, &bp
);
2225 } else if (db
->db_objset
->os_evicting
||
2226 arc_buf_eviction_needed(db
->db_buf
)) {
2229 mutex_exit(&db
->db_mtx
);
2233 mutex_exit(&db
->db_mtx
);
2237 #pragma weak dmu_buf_refcount = dbuf_refcount
2239 dbuf_refcount(dmu_buf_impl_t
*db
)
2241 return (refcount_count(&db
->db_holds
));
2245 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
2246 dmu_buf_user_t
*new_user
)
2248 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2250 mutex_enter(&db
->db_mtx
);
2251 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2252 if (db
->db_user
== old_user
)
2253 db
->db_user
= new_user
;
2255 old_user
= db
->db_user
;
2256 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2257 mutex_exit(&db
->db_mtx
);
2263 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2265 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
2269 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2271 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2273 db
->db_immediate_evict
= TRUE
;
2274 return (dmu_buf_set_user(db_fake
, user
));
2278 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2280 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
2284 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2286 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2288 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2289 return (db
->db_user
);
2293 dmu_buf_user_evict_wait()
2295 taskq_wait(dbu_evict_taskq
);
2299 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2301 boolean_t res
= B_FALSE
;
2302 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2305 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2306 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2312 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2314 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2315 return (dbi
->db_blkptr
);
2319 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2321 /* ASSERT(dmu_tx_is_syncing(tx) */
2322 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2324 if (db
->db_blkptr
!= NULL
)
2327 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2328 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2329 BP_ZERO(db
->db_blkptr
);
2332 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2334 * This buffer was allocated at a time when there was
2335 * no available blkptrs from the dnode, or it was
2336 * inappropriate to hook it in (i.e., nlevels mis-match).
2338 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2339 ASSERT(db
->db_parent
== NULL
);
2340 db
->db_parent
= dn
->dn_dbuf
;
2341 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2344 dmu_buf_impl_t
*parent
= db
->db_parent
;
2345 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2347 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2348 if (parent
== NULL
) {
2349 mutex_exit(&db
->db_mtx
);
2350 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2351 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2352 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2353 rw_exit(&dn
->dn_struct_rwlock
);
2354 mutex_enter(&db
->db_mtx
);
2355 db
->db_parent
= parent
;
2357 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2358 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2364 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2366 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2370 ASSERT(dmu_tx_is_syncing(tx
));
2372 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2374 mutex_enter(&db
->db_mtx
);
2376 ASSERT(db
->db_level
> 0);
2379 /* Read the block if it hasn't been read yet. */
2380 if (db
->db_buf
== NULL
) {
2381 mutex_exit(&db
->db_mtx
);
2382 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2383 mutex_enter(&db
->db_mtx
);
2385 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2386 ASSERT(db
->db_buf
!= NULL
);
2390 /* Indirect block size must match what the dnode thinks it is. */
2391 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2392 dbuf_check_blkptr(dn
, db
);
2395 /* Provide the pending dirty record to child dbufs */
2396 db
->db_data_pending
= dr
;
2398 mutex_exit(&db
->db_mtx
);
2399 dbuf_write(dr
, db
->db_buf
, tx
);
2402 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2403 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2404 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2405 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2410 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2412 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2413 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2416 uint64_t txg
= tx
->tx_txg
;
2418 ASSERT(dmu_tx_is_syncing(tx
));
2420 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2422 mutex_enter(&db
->db_mtx
);
2424 * To be synced, we must be dirtied. But we
2425 * might have been freed after the dirty.
2427 if (db
->db_state
== DB_UNCACHED
) {
2428 /* This buffer has been freed since it was dirtied */
2429 ASSERT(db
->db
.db_data
== NULL
);
2430 } else if (db
->db_state
== DB_FILL
) {
2431 /* This buffer was freed and is now being re-filled */
2432 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2434 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2441 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2442 mutex_enter(&dn
->dn_mtx
);
2443 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2444 mutex_exit(&dn
->dn_mtx
);
2448 * If this is a bonus buffer, simply copy the bonus data into the
2449 * dnode. It will be written out when the dnode is synced (and it
2450 * will be synced, since it must have been dirty for dbuf_sync to
2453 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2454 dbuf_dirty_record_t
**drp
;
2456 ASSERT(*datap
!= NULL
);
2457 ASSERT0(db
->db_level
);
2458 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2459 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2462 if (*datap
!= db
->db
.db_data
) {
2463 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2464 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2466 db
->db_data_pending
= NULL
;
2467 drp
= &db
->db_last_dirty
;
2469 drp
= &(*drp
)->dr_next
;
2470 ASSERT(dr
->dr_next
== NULL
);
2471 ASSERT(dr
->dr_dbuf
== db
);
2473 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2474 ASSERT(db
->db_dirtycnt
> 0);
2475 db
->db_dirtycnt
-= 1;
2476 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2483 * This function may have dropped the db_mtx lock allowing a dmu_sync
2484 * operation to sneak in. As a result, we need to ensure that we
2485 * don't check the dr_override_state until we have returned from
2486 * dbuf_check_blkptr.
2488 dbuf_check_blkptr(dn
, db
);
2491 * If this buffer is in the middle of an immediate write,
2492 * wait for the synchronous IO to complete.
2494 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2495 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2496 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2497 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2500 if (db
->db_state
!= DB_NOFILL
&&
2501 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2502 refcount_count(&db
->db_holds
) > 1 &&
2503 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2504 *datap
== db
->db_buf
) {
2506 * If this buffer is currently "in use" (i.e., there
2507 * are active holds and db_data still references it),
2508 * then make a copy before we start the write so that
2509 * any modifications from the open txg will not leak
2512 * NOTE: this copy does not need to be made for
2513 * objects only modified in the syncing context (e.g.
2514 * DNONE_DNODE blocks).
2516 int blksz
= arc_buf_size(*datap
);
2517 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2518 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2519 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2521 db
->db_data_pending
= dr
;
2523 mutex_exit(&db
->db_mtx
);
2525 dbuf_write(dr
, *datap
, tx
);
2527 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2528 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2529 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2533 * Although zio_nowait() does not "wait for an IO", it does
2534 * initiate the IO. If this is an empty write it seems plausible
2535 * that the IO could actually be completed before the nowait
2536 * returns. We need to DB_DNODE_EXIT() first in case
2537 * zio_nowait() invalidates the dbuf.
2540 zio_nowait(dr
->dr_zio
);
2545 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2547 dbuf_dirty_record_t
*dr
;
2549 while (dr
= list_head(list
)) {
2550 if (dr
->dr_zio
!= NULL
) {
2552 * If we find an already initialized zio then we
2553 * are processing the meta-dnode, and we have finished.
2554 * The dbufs for all dnodes are put back on the list
2555 * during processing, so that we can zio_wait()
2556 * these IOs after initiating all child IOs.
2558 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2559 DMU_META_DNODE_OBJECT
);
2562 list_remove(list
, dr
);
2563 if (dr
->dr_dbuf
->db_level
> 0)
2564 dbuf_sync_indirect(dr
, tx
);
2566 dbuf_sync_leaf(dr
, tx
);
2572 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2574 dmu_buf_impl_t
*db
= vdb
;
2576 blkptr_t
*bp
= zio
->io_bp
;
2577 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2578 spa_t
*spa
= zio
->io_spa
;
2583 ASSERT3P(db
->db_blkptr
, ==, bp
);
2587 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2588 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2589 zio
->io_prev_space_delta
= delta
;
2591 if (bp
->blk_birth
!= 0) {
2592 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2593 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2594 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2595 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
2596 BP_IS_EMBEDDED(bp
));
2597 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2600 mutex_enter(&db
->db_mtx
);
2603 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2604 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2605 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2606 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2610 if (db
->db_level
== 0) {
2611 mutex_enter(&dn
->dn_mtx
);
2612 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2613 db
->db_blkid
!= DMU_SPILL_BLKID
)
2614 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2615 mutex_exit(&dn
->dn_mtx
);
2617 if (dn
->dn_type
== DMU_OT_DNODE
) {
2618 dnode_phys_t
*dnp
= db
->db
.db_data
;
2619 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2621 if (dnp
->dn_type
!= DMU_OT_NONE
)
2625 if (BP_IS_HOLE(bp
)) {
2632 blkptr_t
*ibp
= db
->db
.db_data
;
2633 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2634 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2635 if (BP_IS_HOLE(ibp
))
2637 fill
+= BP_GET_FILL(ibp
);
2642 if (!BP_IS_EMBEDDED(bp
))
2643 bp
->blk_fill
= fill
;
2645 mutex_exit(&db
->db_mtx
);
2649 * The SPA will call this callback several times for each zio - once
2650 * for every physical child i/o (zio->io_phys_children times). This
2651 * allows the DMU to monitor the progress of each logical i/o. For example,
2652 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2653 * block. There may be a long delay before all copies/fragments are completed,
2654 * so this callback allows us to retire dirty space gradually, as the physical
2659 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2661 dmu_buf_impl_t
*db
= arg
;
2662 objset_t
*os
= db
->db_objset
;
2663 dsl_pool_t
*dp
= dmu_objset_pool(os
);
2664 dbuf_dirty_record_t
*dr
;
2667 dr
= db
->db_data_pending
;
2668 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
2671 * The callback will be called io_phys_children times. Retire one
2672 * portion of our dirty space each time we are called. Any rounding
2673 * error will be cleaned up by dsl_pool_sync()'s call to
2674 * dsl_pool_undirty_space().
2676 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
2677 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
2682 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2684 dmu_buf_impl_t
*db
= vdb
;
2685 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2686 blkptr_t
*bp
= db
->db_blkptr
;
2687 objset_t
*os
= db
->db_objset
;
2688 dmu_tx_t
*tx
= os
->os_synctx
;
2689 dbuf_dirty_record_t
**drp
, *dr
;
2691 ASSERT0(zio
->io_error
);
2692 ASSERT(db
->db_blkptr
== bp
);
2695 * For nopwrites and rewrites we ensure that the bp matches our
2696 * original and bypass all the accounting.
2698 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
2699 ASSERT(BP_EQUAL(bp
, bp_orig
));
2701 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2702 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2703 dsl_dataset_block_born(ds
, bp
, tx
);
2706 mutex_enter(&db
->db_mtx
);
2710 drp
= &db
->db_last_dirty
;
2711 while ((dr
= *drp
) != db
->db_data_pending
)
2713 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2714 ASSERT(dr
->dr_dbuf
== db
);
2715 ASSERT(dr
->dr_next
== NULL
);
2719 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2724 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2725 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2726 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2731 if (db
->db_level
== 0) {
2732 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2733 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2734 if (db
->db_state
!= DB_NOFILL
) {
2735 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2736 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2738 else if (!arc_released(db
->db_buf
))
2739 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2746 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2747 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
2748 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2750 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2751 ASSERT3U(db
->db_blkid
, <=,
2752 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
2753 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2755 if (!arc_released(db
->db_buf
))
2756 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2759 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2760 list_destroy(&dr
->dt
.di
.dr_children
);
2762 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2764 cv_broadcast(&db
->db_changed
);
2765 ASSERT(db
->db_dirtycnt
> 0);
2766 db
->db_dirtycnt
-= 1;
2767 db
->db_data_pending
= NULL
;
2768 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
2772 dbuf_write_nofill_ready(zio_t
*zio
)
2774 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2778 dbuf_write_nofill_done(zio_t
*zio
)
2780 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2784 dbuf_write_override_ready(zio_t
*zio
)
2786 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2787 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2789 dbuf_write_ready(zio
, NULL
, db
);
2793 dbuf_write_override_done(zio_t
*zio
)
2795 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2796 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2797 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2799 mutex_enter(&db
->db_mtx
);
2800 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2801 if (!BP_IS_HOLE(obp
))
2802 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2803 arc_release(dr
->dt
.dl
.dr_data
, db
);
2805 mutex_exit(&db
->db_mtx
);
2807 dbuf_write_done(zio
, NULL
, db
);
2810 /* Issue I/O to commit a dirty buffer to disk. */
2812 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2814 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2817 dmu_buf_impl_t
*parent
= db
->db_parent
;
2818 uint64_t txg
= tx
->tx_txg
;
2819 zbookmark_phys_t zb
;
2828 if (db
->db_state
!= DB_NOFILL
) {
2829 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2831 * Private object buffers are released here rather
2832 * than in dbuf_dirty() since they are only modified
2833 * in the syncing context and we don't want the
2834 * overhead of making multiple copies of the data.
2836 if (BP_IS_HOLE(db
->db_blkptr
)) {
2839 dbuf_release_bp(db
);
2844 if (parent
!= dn
->dn_dbuf
) {
2845 /* Our parent is an indirect block. */
2846 /* We have a dirty parent that has been scheduled for write. */
2847 ASSERT(parent
&& parent
->db_data_pending
);
2848 /* Our parent's buffer is one level closer to the dnode. */
2849 ASSERT(db
->db_level
== parent
->db_level
-1);
2851 * We're about to modify our parent's db_data by modifying
2852 * our block pointer, so the parent must be released.
2854 ASSERT(arc_released(parent
->db_buf
));
2855 zio
= parent
->db_data_pending
->dr_zio
;
2857 /* Our parent is the dnode itself. */
2858 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2859 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2860 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2861 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2862 ASSERT3P(db
->db_blkptr
, ==,
2863 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2867 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2868 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2871 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2872 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2873 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2875 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2877 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2879 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2882 if (db
->db_level
== 0 &&
2883 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2885 * The BP for this block has been provided by open context
2886 * (by dmu_sync() or dmu_buf_write_embedded()).
2888 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
2890 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2891 db
->db_blkptr
, contents
, db
->db
.db_size
, &zp
,
2892 dbuf_write_override_ready
, NULL
, dbuf_write_override_done
,
2893 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2894 mutex_enter(&db
->db_mtx
);
2895 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2896 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2897 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
2898 mutex_exit(&db
->db_mtx
);
2899 } else if (db
->db_state
== DB_NOFILL
) {
2900 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
2901 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
2902 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2903 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2904 dbuf_write_nofill_ready
, NULL
, dbuf_write_nofill_done
, db
,
2905 ZIO_PRIORITY_ASYNC_WRITE
,
2906 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2908 ASSERT(arc_released(data
));
2909 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2910 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
),
2911 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dbuf_write_ready
,
2912 dbuf_write_physdone
, dbuf_write_done
, db
,
2913 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);