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.
25 #include <sys/zfs_context.h>
27 #include <sys/dmu_impl.h>
29 #include <sys/dmu_objset.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dmu_tx.h>
35 #include <sys/dmu_zfetch.h>
37 #include <sys/sa_impl.h>
39 static void dbuf_destroy(dmu_buf_impl_t
*db
);
40 static int dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
41 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
44 * Global data structures and functions for the dbuf cache.
46 static kmem_cache_t
*dbuf_cache
;
50 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
52 dmu_buf_impl_t
*db
= vdb
;
53 bzero(db
, sizeof (dmu_buf_impl_t
));
55 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
56 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
57 refcount_create(&db
->db_holds
);
63 dbuf_dest(void *vdb
, void *unused
)
65 dmu_buf_impl_t
*db
= vdb
;
66 mutex_destroy(&db
->db_mtx
);
67 cv_destroy(&db
->db_changed
);
68 refcount_destroy(&db
->db_holds
);
72 * dbuf hash table routines
74 static dbuf_hash_table_t dbuf_hash_table
;
76 static uint64_t dbuf_hash_count
;
79 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
81 uintptr_t osv
= (uintptr_t)os
;
84 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
85 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
86 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
87 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
88 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
89 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
90 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
92 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
97 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
99 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
100 ((dbuf)->db.db_object == (obj) && \
101 (dbuf)->db_objset == (os) && \
102 (dbuf)->db_level == (level) && \
103 (dbuf)->db_blkid == (blkid))
106 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
108 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
109 objset_t
*os
= dn
->dn_objset
;
116 hv
= DBUF_HASH(os
, obj
, level
, blkid
);
117 idx
= hv
& h
->hash_table_mask
;
119 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
120 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
121 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
122 mutex_enter(&db
->db_mtx
);
123 if (db
->db_state
!= DB_EVICTING
) {
124 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
127 mutex_exit(&db
->db_mtx
);
130 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
135 * Insert an entry into the hash table. If there is already an element
136 * equal to elem in the hash table, then the already existing element
137 * will be returned and the new element will not be inserted.
138 * Otherwise returns NULL.
140 static dmu_buf_impl_t
*
141 dbuf_hash_insert(dmu_buf_impl_t
*db
)
143 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
144 objset_t
*os
= db
->db_objset
;
145 uint64_t obj
= db
->db
.db_object
;
146 int level
= db
->db_level
;
147 uint64_t blkid
, hv
, idx
;
150 blkid
= db
->db_blkid
;
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 (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
156 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
157 mutex_enter(&dbf
->db_mtx
);
158 if (dbf
->db_state
!= DB_EVICTING
) {
159 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
162 mutex_exit(&dbf
->db_mtx
);
166 mutex_enter(&db
->db_mtx
);
167 db
->db_hash_next
= h
->hash_table
[idx
];
168 h
->hash_table
[idx
] = db
;
169 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
170 atomic_add_64(&dbuf_hash_count
, 1);
176 * Remove an entry from the hash table. This operation will
177 * fail if there are any existing holds on the db.
180 dbuf_hash_remove(dmu_buf_impl_t
*db
)
182 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
184 dmu_buf_impl_t
*dbf
, **dbp
;
186 hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
187 db
->db_level
, db
->db_blkid
);
188 idx
= hv
& h
->hash_table_mask
;
191 * We musn't hold db_mtx to maintin lock ordering:
192 * DBUF_HASH_MUTEX > db_mtx.
194 ASSERT(refcount_is_zero(&db
->db_holds
));
195 ASSERT(db
->db_state
== DB_EVICTING
);
196 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
198 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
199 dbp
= &h
->hash_table
[idx
];
200 while ((dbf
= *dbp
) != db
) {
201 dbp
= &dbf
->db_hash_next
;
204 *dbp
= db
->db_hash_next
;
205 db
->db_hash_next
= NULL
;
206 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
207 atomic_add_64(&dbuf_hash_count
, -1);
210 static arc_evict_func_t dbuf_do_evict
;
213 dbuf_evict_user(dmu_buf_impl_t
*db
)
215 ASSERT(MUTEX_HELD(&db
->db_mtx
));
217 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
220 if (db
->db_user_data_ptr_ptr
)
221 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
222 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
223 db
->db_user_ptr
= NULL
;
224 db
->db_user_data_ptr_ptr
= NULL
;
225 db
->db_evict_func
= NULL
;
229 dbuf_is_metadata(dmu_buf_impl_t
*db
)
231 if (db
->db_level
> 0) {
234 boolean_t is_metadata
;
237 is_metadata
= dmu_ot
[DB_DNODE(db
)->dn_type
].ot_metadata
;
240 return (is_metadata
);
245 dbuf_evict(dmu_buf_impl_t
*db
)
247 ASSERT(MUTEX_HELD(&db
->db_mtx
));
248 ASSERT(db
->db_buf
== NULL
);
249 ASSERT(db
->db_data_pending
== NULL
);
258 uint64_t hsize
= 1ULL << 16;
259 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
263 * The hash table is big enough to fill all of physical memory
264 * with an average 4K block size. The table will take up
265 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
267 while (hsize
* 4096 < physmem
* PAGESIZE
)
271 h
->hash_table_mask
= hsize
- 1;
272 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
273 if (h
->hash_table
== NULL
) {
274 /* XXX - we should really return an error instead of assert */
275 ASSERT(hsize
> (1ULL << 10));
280 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
281 sizeof (dmu_buf_impl_t
),
282 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
284 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
285 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
291 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
294 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
295 mutex_destroy(&h
->hash_mutexes
[i
]);
296 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
297 kmem_cache_destroy(dbuf_cache
);
306 dbuf_verify(dmu_buf_impl_t
*db
)
309 dbuf_dirty_record_t
*dr
;
311 ASSERT(MUTEX_HELD(&db
->db_mtx
));
313 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
316 ASSERT(db
->db_objset
!= NULL
);
320 ASSERT(db
->db_parent
== NULL
);
321 ASSERT(db
->db_blkptr
== NULL
);
323 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
324 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
325 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
326 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
327 db
->db_blkid
== DMU_SPILL_BLKID
||
328 !list_is_empty(&dn
->dn_dbufs
));
330 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
332 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
333 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
334 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
336 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
337 ASSERT3U(db
->db
.db_offset
, ==, 0);
339 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
342 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
343 ASSERT(dr
->dr_dbuf
== db
);
345 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
346 ASSERT(dr
->dr_dbuf
== db
);
349 * We can't assert that db_size matches dn_datablksz because it
350 * can be momentarily different when another thread is doing
353 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
354 dr
= db
->db_data_pending
;
356 * It should only be modified in syncing context, so
357 * make sure we only have one copy of the data.
359 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
362 /* verify db->db_blkptr */
364 if (db
->db_parent
== dn
->dn_dbuf
) {
365 /* db is pointed to by the dnode */
366 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
367 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
368 ASSERT(db
->db_parent
== NULL
);
370 ASSERT(db
->db_parent
!= NULL
);
371 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
372 ASSERT3P(db
->db_blkptr
, ==,
373 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
375 /* db is pointed to by an indirect block */
376 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
378 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
379 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
382 * dnode_grow_indblksz() can make this fail if we don't
383 * have the struct_rwlock. XXX indblksz no longer
384 * grows. safe to do this now?
386 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
387 ASSERT3P(db
->db_blkptr
, ==,
388 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
389 db
->db_blkid
% epb
));
393 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
394 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
395 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
396 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
398 * If the blkptr isn't set but they have nonzero data,
399 * it had better be dirty, otherwise we'll lose that
400 * data when we evict this buffer.
402 if (db
->db_dirtycnt
== 0) {
403 ASSERTV(uint64_t *buf
= db
->db
.db_data
);
406 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
416 dbuf_update_data(dmu_buf_impl_t
*db
)
418 ASSERT(MUTEX_HELD(&db
->db_mtx
));
419 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
420 ASSERT(!refcount_is_zero(&db
->db_holds
));
421 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
426 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
428 ASSERT(MUTEX_HELD(&db
->db_mtx
));
429 ASSERT(db
->db_buf
== NULL
|| !arc_has_callback(db
->db_buf
));
432 ASSERT(buf
->b_data
!= NULL
);
433 db
->db
.db_data
= buf
->b_data
;
434 if (!arc_released(buf
))
435 arc_set_callback(buf
, dbuf_do_evict
, db
);
436 dbuf_update_data(db
);
439 db
->db
.db_data
= NULL
;
440 if (db
->db_state
!= DB_NOFILL
)
441 db
->db_state
= DB_UNCACHED
;
446 * Loan out an arc_buf for read. Return the loaned arc_buf.
449 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
453 mutex_enter(&db
->db_mtx
);
454 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
455 int blksz
= db
->db
.db_size
;
458 mutex_exit(&db
->db_mtx
);
459 DB_GET_SPA(&spa
, db
);
460 abuf
= arc_loan_buf(spa
, blksz
);
461 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
464 arc_loan_inuse_buf(abuf
, db
);
465 dbuf_set_data(db
, NULL
);
466 mutex_exit(&db
->db_mtx
);
472 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
474 if (dn
->dn_datablkshift
) {
475 return (offset
>> dn
->dn_datablkshift
);
477 ASSERT3U(offset
, <, dn
->dn_datablksz
);
483 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
485 dmu_buf_impl_t
*db
= vdb
;
487 mutex_enter(&db
->db_mtx
);
488 ASSERT3U(db
->db_state
, ==, DB_READ
);
490 * All reads are synchronous, so we must have a hold on the dbuf
492 ASSERT(refcount_count(&db
->db_holds
) > 0);
493 ASSERT(db
->db_buf
== NULL
);
494 ASSERT(db
->db
.db_data
== NULL
);
495 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
496 /* we were freed in flight; disregard any error */
497 arc_release(buf
, db
);
498 bzero(buf
->b_data
, db
->db
.db_size
);
500 db
->db_freed_in_flight
= FALSE
;
501 dbuf_set_data(db
, buf
);
502 db
->db_state
= DB_CACHED
;
503 } else if (zio
== NULL
|| zio
->io_error
== 0) {
504 dbuf_set_data(db
, buf
);
505 db
->db_state
= DB_CACHED
;
507 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
508 ASSERT3P(db
->db_buf
, ==, NULL
);
509 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
510 db
->db_state
= DB_UNCACHED
;
512 cv_broadcast(&db
->db_changed
);
513 dbuf_rele_and_unlock(db
, NULL
);
517 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
522 uint32_t aflags
= ARC_NOWAIT
;
527 ASSERT(!refcount_is_zero(&db
->db_holds
));
528 /* We need the struct_rwlock to prevent db_blkptr from changing. */
529 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
530 ASSERT(MUTEX_HELD(&db
->db_mtx
));
531 ASSERT(db
->db_state
== DB_UNCACHED
);
532 ASSERT(db
->db_buf
== NULL
);
534 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
535 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
537 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
538 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
539 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
540 if (bonuslen
< DN_MAX_BONUSLEN
)
541 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
543 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
545 dbuf_update_data(db
);
546 db
->db_state
= DB_CACHED
;
547 mutex_exit(&db
->db_mtx
);
552 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
553 * processes the delete record and clears the bp while we are waiting
554 * for the dn_mtx (resulting in a "no" from block_freed).
556 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
557 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
558 BP_IS_HOLE(db
->db_blkptr
)))) {
559 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
561 dbuf_set_data(db
, arc_buf_alloc(dn
->dn_objset
->os_spa
,
562 db
->db
.db_size
, db
, type
));
564 bzero(db
->db
.db_data
, db
->db
.db_size
);
565 db
->db_state
= DB_CACHED
;
566 *flags
|= DB_RF_CACHED
;
567 mutex_exit(&db
->db_mtx
);
571 spa
= dn
->dn_objset
->os_spa
;
574 db
->db_state
= DB_READ
;
575 mutex_exit(&db
->db_mtx
);
577 if (DBUF_IS_L2CACHEABLE(db
))
578 aflags
|= ARC_L2CACHE
;
580 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
581 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
582 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
584 dbuf_add_ref(db
, NULL
);
585 /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
588 pbuf
= db
->db_parent
->db_buf
;
590 pbuf
= db
->db_objset
->os_phys_buf
;
592 (void) dsl_read(zio
, spa
, db
->db_blkptr
, pbuf
,
593 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
594 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
596 if (aflags
& ARC_CACHED
)
597 *flags
|= DB_RF_CACHED
;
601 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
604 int havepzio
= (zio
!= NULL
);
609 * We don't have to hold the mutex to check db_state because it
610 * can't be freed while we have a hold on the buffer.
612 ASSERT(!refcount_is_zero(&db
->db_holds
));
614 if (db
->db_state
== DB_NOFILL
)
619 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
620 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
622 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
623 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
624 DBUF_IS_CACHEABLE(db
);
626 mutex_enter(&db
->db_mtx
);
627 if (db
->db_state
== DB_CACHED
) {
628 mutex_exit(&db
->db_mtx
);
630 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
631 db
->db
.db_size
, TRUE
);
632 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
633 rw_exit(&dn
->dn_struct_rwlock
);
635 } else if (db
->db_state
== DB_UNCACHED
) {
636 spa_t
*spa
= dn
->dn_objset
->os_spa
;
639 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
640 dbuf_read_impl(db
, zio
, &flags
);
642 /* dbuf_read_impl has dropped db_mtx for us */
645 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
646 db
->db
.db_size
, flags
& DB_RF_CACHED
);
648 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
649 rw_exit(&dn
->dn_struct_rwlock
);
655 mutex_exit(&db
->db_mtx
);
657 dmu_zfetch(&dn
->dn_zfetch
, db
->db
.db_offset
,
658 db
->db
.db_size
, TRUE
);
659 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
660 rw_exit(&dn
->dn_struct_rwlock
);
663 mutex_enter(&db
->db_mtx
);
664 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
665 while (db
->db_state
== DB_READ
||
666 db
->db_state
== DB_FILL
) {
667 ASSERT(db
->db_state
== DB_READ
||
668 (flags
& DB_RF_HAVESTRUCT
) == 0);
669 cv_wait(&db
->db_changed
, &db
->db_mtx
);
671 if (db
->db_state
== DB_UNCACHED
)
674 mutex_exit(&db
->db_mtx
);
677 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
682 dbuf_noread(dmu_buf_impl_t
*db
)
684 ASSERT(!refcount_is_zero(&db
->db_holds
));
685 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
686 mutex_enter(&db
->db_mtx
);
687 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
688 cv_wait(&db
->db_changed
, &db
->db_mtx
);
689 if (db
->db_state
== DB_UNCACHED
) {
690 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
693 ASSERT(db
->db_buf
== NULL
);
694 ASSERT(db
->db
.db_data
== NULL
);
695 DB_GET_SPA(&spa
, db
);
696 dbuf_set_data(db
, arc_buf_alloc(spa
, db
->db
.db_size
, db
, type
));
697 db
->db_state
= DB_FILL
;
698 } else if (db
->db_state
== DB_NOFILL
) {
699 dbuf_set_data(db
, NULL
);
701 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
703 mutex_exit(&db
->db_mtx
);
707 * This is our just-in-time copy function. It makes a copy of
708 * buffers, that have been modified in a previous transaction
709 * group, before we modify them in the current active group.
711 * This function is used in two places: when we are dirtying a
712 * buffer for the first time in a txg, and when we are freeing
713 * a range in a dnode that includes this buffer.
715 * Note that when we are called from dbuf_free_range() we do
716 * not put a hold on the buffer, we just traverse the active
717 * dbuf list for the dnode.
720 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
722 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
724 ASSERT(MUTEX_HELD(&db
->db_mtx
));
725 ASSERT(db
->db
.db_data
!= NULL
);
726 ASSERT(db
->db_level
== 0);
727 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
730 (dr
->dt
.dl
.dr_data
!=
731 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
735 * If the last dirty record for this dbuf has not yet synced
736 * and its referencing the dbuf data, either:
737 * reset the reference to point to a new copy,
738 * or (if there a no active holders)
739 * just null out the current db_data pointer.
741 ASSERT(dr
->dr_txg
>= txg
- 2);
742 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
743 /* Note that the data bufs here are zio_bufs */
744 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
745 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
746 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
747 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
748 int size
= db
->db
.db_size
;
749 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
752 DB_GET_SPA(&spa
, db
);
753 dr
->dt
.dl
.dr_data
= arc_buf_alloc(spa
, size
, db
, type
);
754 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
756 dbuf_set_data(db
, NULL
);
761 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
763 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
764 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
765 uint64_t txg
= dr
->dr_txg
;
767 ASSERT(MUTEX_HELD(&db
->db_mtx
));
768 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
769 ASSERT(db
->db_level
== 0);
771 if (db
->db_blkid
== DMU_BONUS_BLKID
||
772 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
775 ASSERT(db
->db_data_pending
!= dr
);
777 /* free this block */
778 if (!BP_IS_HOLE(bp
)) {
781 DB_GET_SPA(&spa
, db
);
782 zio_free(spa
, txg
, bp
);
784 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
786 * Release the already-written buffer, so we leave it in
787 * a consistent dirty state. Note that all callers are
788 * modifying the buffer, so they will immediately do
789 * another (redundant) arc_release(). Therefore, leave
790 * the buf thawed to save the effort of freezing &
791 * immediately re-thawing it.
793 arc_release(dr
->dt
.dl
.dr_data
, db
);
797 * Evict (if its unreferenced) or clear (if its referenced) any level-0
798 * data blocks in the free range, so that any future readers will find
799 * empty blocks. Also, if we happen accross any level-1 dbufs in the
800 * range that have not already been marked dirty, mark them dirty so
801 * they stay in memory.
804 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
806 dmu_buf_impl_t
*db
, *db_next
;
807 uint64_t txg
= tx
->tx_txg
;
808 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
809 uint64_t first_l1
= start
>> epbs
;
810 uint64_t last_l1
= end
>> epbs
;
812 if (end
> dn
->dn_maxblkid
&& (end
!= DMU_SPILL_BLKID
)) {
813 end
= dn
->dn_maxblkid
;
814 last_l1
= end
>> epbs
;
816 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
817 mutex_enter(&dn
->dn_dbufs_mtx
);
818 for (db
= list_head(&dn
->dn_dbufs
); db
; db
= db_next
) {
819 db_next
= list_next(&dn
->dn_dbufs
, db
);
820 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
822 if (db
->db_level
== 1 &&
823 db
->db_blkid
>= first_l1
&& db
->db_blkid
<= last_l1
) {
824 mutex_enter(&db
->db_mtx
);
825 if (db
->db_last_dirty
&&
826 db
->db_last_dirty
->dr_txg
< txg
) {
827 dbuf_add_ref(db
, FTAG
);
828 mutex_exit(&db
->db_mtx
);
829 dbuf_will_dirty(db
, tx
);
832 mutex_exit(&db
->db_mtx
);
836 if (db
->db_level
!= 0)
838 dprintf_dbuf(db
, "found buf %s\n", "");
839 if (db
->db_blkid
< start
|| db
->db_blkid
> end
)
842 /* found a level 0 buffer in the range */
843 if (dbuf_undirty(db
, tx
))
846 mutex_enter(&db
->db_mtx
);
847 if (db
->db_state
== DB_UNCACHED
||
848 db
->db_state
== DB_NOFILL
||
849 db
->db_state
== DB_EVICTING
) {
850 ASSERT(db
->db
.db_data
== NULL
);
851 mutex_exit(&db
->db_mtx
);
854 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
855 /* will be handled in dbuf_read_done or dbuf_rele */
856 db
->db_freed_in_flight
= TRUE
;
857 mutex_exit(&db
->db_mtx
);
860 if (refcount_count(&db
->db_holds
) == 0) {
865 /* The dbuf is referenced */
867 if (db
->db_last_dirty
!= NULL
) {
868 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
870 if (dr
->dr_txg
== txg
) {
872 * This buffer is "in-use", re-adjust the file
873 * size to reflect that this buffer may
874 * contain new data when we sync.
876 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
877 db
->db_blkid
> dn
->dn_maxblkid
)
878 dn
->dn_maxblkid
= db
->db_blkid
;
882 * This dbuf is not dirty in the open context.
883 * Either uncache it (if its not referenced in
884 * the open context) or reset its contents to
887 dbuf_fix_old_data(db
, txg
);
890 /* clear the contents if its cached */
891 if (db
->db_state
== DB_CACHED
) {
892 ASSERT(db
->db
.db_data
!= NULL
);
893 arc_release(db
->db_buf
, db
);
894 bzero(db
->db
.db_data
, db
->db
.db_size
);
895 arc_buf_freeze(db
->db_buf
);
898 mutex_exit(&db
->db_mtx
);
900 mutex_exit(&dn
->dn_dbufs_mtx
);
904 dbuf_block_freeable(dmu_buf_impl_t
*db
)
906 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
907 uint64_t birth_txg
= 0;
910 * We don't need any locking to protect db_blkptr:
911 * If it's syncing, then db_last_dirty will be set
912 * so we'll ignore db_blkptr.
914 ASSERT(MUTEX_HELD(&db
->db_mtx
));
915 if (db
->db_last_dirty
)
916 birth_txg
= db
->db_last_dirty
->dr_txg
;
917 else if (db
->db_blkptr
)
918 birth_txg
= db
->db_blkptr
->blk_birth
;
921 * If we don't exist or are in a snapshot, we can't be freed.
922 * Don't pass the bp to dsl_dataset_block_freeable() since we
923 * are holding the db_mtx lock and might deadlock if we are
924 * prefetching a dedup-ed block.
927 return (ds
== NULL
||
928 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
934 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
936 arc_buf_t
*buf
, *obuf
;
937 int osize
= db
->db
.db_size
;
938 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
941 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
946 /* XXX does *this* func really need the lock? */
947 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
950 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
951 * is OK, because there can be no other references to the db
952 * when we are changing its size, so no concurrent DB_FILL can
956 * XXX we should be doing a dbuf_read, checking the return
957 * value and returning that up to our callers
959 dbuf_will_dirty(db
, tx
);
961 /* create the data buffer for the new block */
962 buf
= arc_buf_alloc(dn
->dn_objset
->os_spa
, size
, db
, type
);
964 /* copy old block data to the new block */
966 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
967 /* zero the remainder */
969 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
971 mutex_enter(&db
->db_mtx
);
972 dbuf_set_data(db
, buf
);
973 VERIFY(arc_buf_remove_ref(obuf
, db
) == 1);
974 db
->db
.db_size
= size
;
976 if (db
->db_level
== 0) {
977 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
978 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
980 mutex_exit(&db
->db_mtx
);
982 dnode_willuse_space(dn
, size
-osize
, tx
);
987 dbuf_release_bp(dmu_buf_impl_t
*db
)
992 DB_GET_OBJSET(&os
, db
);
993 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
994 ASSERT(arc_released(os
->os_phys_buf
) ||
995 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
996 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
998 zb
.zb_objset
= os
->os_dsl_dataset
?
999 os
->os_dsl_dataset
->ds_object
: 0;
1000 zb
.zb_object
= db
->db
.db_object
;
1001 zb
.zb_level
= db
->db_level
;
1002 zb
.zb_blkid
= db
->db_blkid
;
1003 (void) arc_release_bp(db
->db_buf
, db
,
1004 db
->db_blkptr
, os
->os_spa
, &zb
);
1007 dbuf_dirty_record_t
*
1008 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1012 dbuf_dirty_record_t
**drp
, *dr
;
1013 int drop_struct_lock
= FALSE
;
1014 boolean_t do_free_accounting
= B_FALSE
;
1015 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1017 ASSERT(tx
->tx_txg
!= 0);
1018 ASSERT(!refcount_is_zero(&db
->db_holds
));
1019 DMU_TX_DIRTY_BUF(tx
, db
);
1024 * Shouldn't dirty a regular buffer in syncing context. Private
1025 * objects may be dirtied in syncing context, but only if they
1026 * were already pre-dirtied in open context.
1028 ASSERT(!dmu_tx_is_syncing(tx
) ||
1029 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1030 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1031 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1033 * We make this assert for private objects as well, but after we
1034 * check if we're already dirty. They are allowed to re-dirty
1035 * in syncing context.
1037 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1038 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1039 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1041 mutex_enter(&db
->db_mtx
);
1043 * XXX make this true for indirects too? The problem is that
1044 * transactions created with dmu_tx_create_assigned() from
1045 * syncing context don't bother holding ahead.
1047 ASSERT(db
->db_level
!= 0 ||
1048 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1049 db
->db_state
== DB_NOFILL
);
1051 mutex_enter(&dn
->dn_mtx
);
1053 * Don't set dirtyctx to SYNC if we're just modifying this as we
1054 * initialize the objset.
1056 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1057 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1059 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1060 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1061 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1063 mutex_exit(&dn
->dn_mtx
);
1065 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1066 dn
->dn_have_spill
= B_TRUE
;
1069 * If this buffer is already dirty, we're done.
1071 drp
= &db
->db_last_dirty
;
1072 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1073 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1074 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1076 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1079 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1081 * If this buffer has already been written out,
1082 * we now need to reset its state.
1084 dbuf_unoverride(dr
);
1085 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1086 db
->db_state
!= DB_NOFILL
)
1087 arc_buf_thaw(db
->db_buf
);
1089 mutex_exit(&db
->db_mtx
);
1094 * Only valid if not already dirty.
1096 ASSERT(dn
->dn_object
== 0 ||
1097 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1098 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1100 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1101 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1102 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1103 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1104 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1105 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1108 * We should only be dirtying in syncing context if it's the
1109 * mos or we're initializing the os or it's a special object.
1110 * However, we are allowed to dirty in syncing context provided
1111 * we already dirtied it in open context. Hence we must make
1112 * this assertion only if we're not already dirty.
1115 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1116 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1117 ASSERT(db
->db
.db_size
!= 0);
1119 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1121 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1123 * Update the accounting.
1124 * Note: we delay "free accounting" until after we drop
1125 * the db_mtx. This keeps us from grabbing other locks
1126 * (and possibly deadlocking) in bp_get_dsize() while
1127 * also holding the db_mtx.
1129 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1130 do_free_accounting
= dbuf_block_freeable(db
);
1134 * If this buffer is dirty in an old transaction group we need
1135 * to make a copy of it so that the changes we make in this
1136 * transaction group won't leak out when we sync the older txg.
1138 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1139 if (db
->db_level
== 0) {
1140 void *data_old
= db
->db_buf
;
1142 if (db
->db_state
!= DB_NOFILL
) {
1143 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1144 dbuf_fix_old_data(db
, tx
->tx_txg
);
1145 data_old
= db
->db
.db_data
;
1146 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1148 * Release the data buffer from the cache so
1149 * that we can modify it without impacting
1150 * possible other users of this cached data
1151 * block. Note that indirect blocks and
1152 * private objects are not released until the
1153 * syncing state (since they are only modified
1156 arc_release(db
->db_buf
, db
);
1157 dbuf_fix_old_data(db
, tx
->tx_txg
);
1158 data_old
= db
->db_buf
;
1160 ASSERT(data_old
!= NULL
);
1162 dr
->dt
.dl
.dr_data
= data_old
;
1164 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1165 list_create(&dr
->dt
.di
.dr_children
,
1166 sizeof (dbuf_dirty_record_t
),
1167 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1170 dr
->dr_txg
= tx
->tx_txg
;
1175 * We could have been freed_in_flight between the dbuf_noread
1176 * and dbuf_dirty. We win, as though the dbuf_noread() had
1177 * happened after the free.
1179 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1180 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1181 mutex_enter(&dn
->dn_mtx
);
1182 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1183 mutex_exit(&dn
->dn_mtx
);
1184 db
->db_freed_in_flight
= FALSE
;
1188 * This buffer is now part of this txg
1190 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1191 db
->db_dirtycnt
+= 1;
1192 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1194 mutex_exit(&db
->db_mtx
);
1196 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1197 db
->db_blkid
== DMU_SPILL_BLKID
) {
1198 mutex_enter(&dn
->dn_mtx
);
1199 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1200 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1201 mutex_exit(&dn
->dn_mtx
);
1202 dnode_setdirty(dn
, tx
);
1205 } else if (do_free_accounting
) {
1206 blkptr_t
*bp
= db
->db_blkptr
;
1207 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1208 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1210 * This is only a guess -- if the dbuf is dirty
1211 * in a previous txg, we don't know how much
1212 * space it will use on disk yet. We should
1213 * really have the struct_rwlock to access
1214 * db_blkptr, but since this is just a guess,
1215 * it's OK if we get an odd answer.
1217 ddt_prefetch(os
->os_spa
, bp
);
1218 dnode_willuse_space(dn
, -willfree
, tx
);
1221 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1222 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1223 drop_struct_lock
= TRUE
;
1226 if (db
->db_level
== 0) {
1227 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1228 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1231 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1232 dmu_buf_impl_t
*parent
= db
->db_parent
;
1233 dbuf_dirty_record_t
*di
;
1234 int parent_held
= FALSE
;
1236 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1237 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1239 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1240 db
->db_blkid
>> epbs
, FTAG
);
1241 ASSERT(parent
!= NULL
);
1244 if (drop_struct_lock
)
1245 rw_exit(&dn
->dn_struct_rwlock
);
1246 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1247 di
= dbuf_dirty(parent
, tx
);
1249 dbuf_rele(parent
, FTAG
);
1251 mutex_enter(&db
->db_mtx
);
1252 /* possible race with dbuf_undirty() */
1253 if (db
->db_last_dirty
== dr
||
1254 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1255 mutex_enter(&di
->dt
.di
.dr_mtx
);
1256 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1257 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1258 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1259 mutex_exit(&di
->dt
.di
.dr_mtx
);
1262 mutex_exit(&db
->db_mtx
);
1264 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1265 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1266 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1267 mutex_enter(&dn
->dn_mtx
);
1268 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1269 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1270 mutex_exit(&dn
->dn_mtx
);
1271 if (drop_struct_lock
)
1272 rw_exit(&dn
->dn_struct_rwlock
);
1275 dnode_setdirty(dn
, tx
);
1281 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1284 uint64_t txg
= tx
->tx_txg
;
1285 dbuf_dirty_record_t
*dr
, **drp
;
1288 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1290 mutex_enter(&db
->db_mtx
);
1292 * If this buffer is not dirty, we're done.
1294 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1295 if (dr
->dr_txg
<= txg
)
1297 if (dr
== NULL
|| dr
->dr_txg
< txg
) {
1298 mutex_exit(&db
->db_mtx
);
1301 ASSERT(dr
->dr_txg
== txg
);
1302 ASSERT(dr
->dr_dbuf
== db
);
1308 * If this buffer is currently held, we cannot undirty
1309 * it, since one of the current holders may be in the
1310 * middle of an update. Note that users of dbuf_undirty()
1311 * should not place a hold on the dbuf before the call.
1313 if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1314 mutex_exit(&db
->db_mtx
);
1315 /* Make sure we don't toss this buffer at sync phase */
1316 mutex_enter(&dn
->dn_mtx
);
1317 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1318 mutex_exit(&dn
->dn_mtx
);
1323 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1325 ASSERT(db
->db
.db_size
!= 0);
1327 /* XXX would be nice to fix up dn_towrite_space[] */
1331 if (dr
->dr_parent
) {
1332 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1333 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1334 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1335 } else if (db
->db_level
+1 == dn
->dn_nlevels
) {
1336 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1337 mutex_enter(&dn
->dn_mtx
);
1338 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1339 mutex_exit(&dn
->dn_mtx
);
1343 if (db
->db_level
== 0) {
1344 if (db
->db_state
!= DB_NOFILL
) {
1345 dbuf_unoverride(dr
);
1347 ASSERT(db
->db_buf
!= NULL
);
1348 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1349 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1350 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
1354 ASSERT(db
->db_buf
!= NULL
);
1355 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
1356 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
1357 list_destroy(&dr
->dt
.di
.dr_children
);
1359 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1361 ASSERT(db
->db_dirtycnt
> 0);
1362 db
->db_dirtycnt
-= 1;
1364 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1365 arc_buf_t
*buf
= db
->db_buf
;
1367 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1368 dbuf_set_data(db
, NULL
);
1369 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1374 mutex_exit(&db
->db_mtx
);
1378 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1380 dbuf_will_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1382 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1384 ASSERT(tx
->tx_txg
!= 0);
1385 ASSERT(!refcount_is_zero(&db
->db_holds
));
1388 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1389 rf
|= DB_RF_HAVESTRUCT
;
1391 (void) dbuf_read(db
, NULL
, rf
);
1392 (void) dbuf_dirty(db
, tx
);
1396 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1398 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1400 db
->db_state
= DB_NOFILL
;
1402 dmu_buf_will_fill(db_fake
, tx
);
1406 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1408 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1410 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1411 ASSERT(tx
->tx_txg
!= 0);
1412 ASSERT(db
->db_level
== 0);
1413 ASSERT(!refcount_is_zero(&db
->db_holds
));
1415 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1416 dmu_tx_private_ok(tx
));
1419 (void) dbuf_dirty(db
, tx
);
1422 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1425 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1427 mutex_enter(&db
->db_mtx
);
1430 if (db
->db_state
== DB_FILL
) {
1431 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1432 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1433 /* we were freed while filling */
1434 /* XXX dbuf_undirty? */
1435 bzero(db
->db
.db_data
, db
->db
.db_size
);
1436 db
->db_freed_in_flight
= FALSE
;
1438 db
->db_state
= DB_CACHED
;
1439 cv_broadcast(&db
->db_changed
);
1441 mutex_exit(&db
->db_mtx
);
1445 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1446 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1449 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1451 ASSERT(!refcount_is_zero(&db
->db_holds
));
1452 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1453 ASSERT(db
->db_level
== 0);
1454 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1455 ASSERT(buf
!= NULL
);
1456 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1457 ASSERT(tx
->tx_txg
!= 0);
1459 arc_return_buf(buf
, db
);
1460 ASSERT(arc_released(buf
));
1462 mutex_enter(&db
->db_mtx
);
1464 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1465 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1467 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1469 if (db
->db_state
== DB_CACHED
&&
1470 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1471 mutex_exit(&db
->db_mtx
);
1472 (void) dbuf_dirty(db
, tx
);
1473 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1474 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1475 xuio_stat_wbuf_copied();
1479 xuio_stat_wbuf_nocopy();
1480 if (db
->db_state
== DB_CACHED
) {
1481 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1483 ASSERT(db
->db_buf
!= NULL
);
1484 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1485 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1486 if (!arc_released(db
->db_buf
)) {
1487 ASSERT(dr
->dt
.dl
.dr_override_state
==
1489 arc_release(db
->db_buf
, db
);
1491 dr
->dt
.dl
.dr_data
= buf
;
1492 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 1);
1493 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1494 arc_release(db
->db_buf
, db
);
1495 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 1);
1499 ASSERT(db
->db_buf
== NULL
);
1500 dbuf_set_data(db
, buf
);
1501 db
->db_state
= DB_FILL
;
1502 mutex_exit(&db
->db_mtx
);
1503 (void) dbuf_dirty(db
, tx
);
1504 dbuf_fill_done(db
, tx
);
1508 * "Clear" the contents of this dbuf. This will mark the dbuf
1509 * EVICTING and clear *most* of its references. Unfortunetely,
1510 * when we are not holding the dn_dbufs_mtx, we can't clear the
1511 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1512 * in this case. For callers from the DMU we will usually see:
1513 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1514 * For the arc callback, we will usually see:
1515 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1516 * Sometimes, though, we will get a mix of these two:
1517 * DMU: dbuf_clear()->arc_buf_evict()
1518 * ARC: dbuf_do_evict()->dbuf_destroy()
1521 dbuf_clear(dmu_buf_impl_t
*db
)
1524 dmu_buf_impl_t
*parent
= db
->db_parent
;
1525 dmu_buf_impl_t
*dndb
;
1526 int dbuf_gone
= FALSE
;
1528 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1529 ASSERT(refcount_is_zero(&db
->db_holds
));
1531 dbuf_evict_user(db
);
1533 if (db
->db_state
== DB_CACHED
) {
1534 ASSERT(db
->db
.db_data
!= NULL
);
1535 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1536 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1537 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1539 db
->db
.db_data
= NULL
;
1540 db
->db_state
= DB_UNCACHED
;
1543 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1544 ASSERT(db
->db_data_pending
== NULL
);
1546 db
->db_state
= DB_EVICTING
;
1547 db
->db_blkptr
= NULL
;
1552 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1553 list_remove(&dn
->dn_dbufs
, db
);
1554 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1558 * Decrementing the dbuf count means that the hold corresponding
1559 * to the removed dbuf is no longer discounted in dnode_move(),
1560 * so the dnode cannot be moved until after we release the hold.
1561 * The membar_producer() ensures visibility of the decremented
1562 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1566 db
->db_dnode_handle
= NULL
;
1572 dbuf_gone
= arc_buf_evict(db
->db_buf
);
1575 mutex_exit(&db
->db_mtx
);
1578 * If this dbuf is referenced from an indirect dbuf,
1579 * decrement the ref count on the indirect dbuf.
1581 if (parent
&& parent
!= dndb
)
1582 dbuf_rele(parent
, db
);
1586 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1587 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
1594 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1596 if (blkid
== DMU_SPILL_BLKID
) {
1597 mutex_enter(&dn
->dn_mtx
);
1598 if (dn
->dn_have_spill
&&
1599 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1600 *bpp
= &dn
->dn_phys
->dn_spill
;
1603 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1604 *parentp
= dn
->dn_dbuf
;
1605 mutex_exit(&dn
->dn_mtx
);
1609 if (dn
->dn_phys
->dn_nlevels
== 0)
1612 nlevels
= dn
->dn_phys
->dn_nlevels
;
1614 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1616 ASSERT3U(level
* epbs
, <, 64);
1617 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1618 if (level
>= nlevels
||
1619 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1620 /* the buffer has no parent yet */
1622 } else if (level
< nlevels
-1) {
1623 /* this block is referenced from an indirect block */
1624 int err
= dbuf_hold_impl(dn
, level
+1,
1625 blkid
>> epbs
, fail_sparse
, NULL
, parentp
);
1628 err
= dbuf_read(*parentp
, NULL
,
1629 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1631 dbuf_rele(*parentp
, NULL
);
1635 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1636 (blkid
& ((1ULL << epbs
) - 1));
1639 /* the block is referenced from the dnode */
1640 ASSERT3U(level
, ==, nlevels
-1);
1641 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1642 blkid
< dn
->dn_phys
->dn_nblkptr
);
1644 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1645 *parentp
= dn
->dn_dbuf
;
1647 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1652 static dmu_buf_impl_t
*
1653 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1654 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1656 objset_t
*os
= dn
->dn_objset
;
1657 dmu_buf_impl_t
*db
, *odb
;
1659 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1660 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1662 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
1665 db
->db
.db_object
= dn
->dn_object
;
1666 db
->db_level
= level
;
1667 db
->db_blkid
= blkid
;
1668 db
->db_last_dirty
= NULL
;
1669 db
->db_dirtycnt
= 0;
1670 db
->db_dnode_handle
= dn
->dn_handle
;
1671 db
->db_parent
= parent
;
1672 db
->db_blkptr
= blkptr
;
1674 db
->db_user_ptr
= NULL
;
1675 db
->db_user_data_ptr_ptr
= NULL
;
1676 db
->db_evict_func
= NULL
;
1677 db
->db_immediate_evict
= 0;
1678 db
->db_freed_in_flight
= 0;
1680 if (blkid
== DMU_BONUS_BLKID
) {
1681 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1682 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1683 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1684 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1685 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1686 db
->db_state
= DB_UNCACHED
;
1687 /* the bonus dbuf is not placed in the hash table */
1688 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1690 } else if (blkid
== DMU_SPILL_BLKID
) {
1691 db
->db
.db_size
= (blkptr
!= NULL
) ?
1692 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1693 db
->db
.db_offset
= 0;
1696 db
->db_level
? 1<<dn
->dn_indblkshift
: dn
->dn_datablksz
;
1697 db
->db
.db_size
= blocksize
;
1698 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1702 * Hold the dn_dbufs_mtx while we get the new dbuf
1703 * in the hash table *and* added to the dbufs list.
1704 * This prevents a possible deadlock with someone
1705 * trying to look up this dbuf before its added to the
1708 mutex_enter(&dn
->dn_dbufs_mtx
);
1709 db
->db_state
= DB_EVICTING
;
1710 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1711 /* someone else inserted it first */
1712 kmem_cache_free(dbuf_cache
, db
);
1713 mutex_exit(&dn
->dn_dbufs_mtx
);
1716 list_insert_head(&dn
->dn_dbufs
, db
);
1717 db
->db_state
= DB_UNCACHED
;
1718 mutex_exit(&dn
->dn_dbufs_mtx
);
1719 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1721 if (parent
&& parent
!= dn
->dn_dbuf
)
1722 dbuf_add_ref(parent
, db
);
1724 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1725 refcount_count(&dn
->dn_holds
) > 0);
1726 (void) refcount_add(&dn
->dn_holds
, db
);
1727 (void) atomic_inc_32_nv(&dn
->dn_dbufs_count
);
1729 dprintf_dbuf(db
, "db=%p\n", db
);
1735 dbuf_do_evict(void *private)
1737 arc_buf_t
*buf
= private;
1738 dmu_buf_impl_t
*db
= buf
->b_private
;
1740 if (!MUTEX_HELD(&db
->db_mtx
))
1741 mutex_enter(&db
->db_mtx
);
1743 ASSERT(refcount_is_zero(&db
->db_holds
));
1745 if (db
->db_state
!= DB_EVICTING
) {
1746 ASSERT(db
->db_state
== DB_CACHED
);
1751 mutex_exit(&db
->db_mtx
);
1758 dbuf_destroy(dmu_buf_impl_t
*db
)
1760 ASSERT(refcount_is_zero(&db
->db_holds
));
1762 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1764 * If this dbuf is still on the dn_dbufs list,
1765 * remove it from that list.
1767 if (db
->db_dnode_handle
!= NULL
) {
1772 mutex_enter(&dn
->dn_dbufs_mtx
);
1773 list_remove(&dn
->dn_dbufs
, db
);
1774 (void) atomic_dec_32_nv(&dn
->dn_dbufs_count
);
1775 mutex_exit(&dn
->dn_dbufs_mtx
);
1778 * Decrementing the dbuf count means that the hold
1779 * corresponding to the removed dbuf is no longer
1780 * discounted in dnode_move(), so the dnode cannot be
1781 * moved until after we release the hold.
1784 db
->db_dnode_handle
= NULL
;
1786 dbuf_hash_remove(db
);
1788 db
->db_parent
= NULL
;
1791 ASSERT(!list_link_active(&db
->db_link
));
1792 ASSERT(db
->db
.db_data
== NULL
);
1793 ASSERT(db
->db_hash_next
== NULL
);
1794 ASSERT(db
->db_blkptr
== NULL
);
1795 ASSERT(db
->db_data_pending
== NULL
);
1797 kmem_cache_free(dbuf_cache
, db
);
1798 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1802 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
)
1804 dmu_buf_impl_t
*db
= NULL
;
1805 blkptr_t
*bp
= NULL
;
1807 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1808 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1810 if (dnode_block_freed(dn
, blkid
))
1813 /* dbuf_find() returns with db_mtx held */
1814 if ((db
= dbuf_find(dn
, 0, blkid
))) {
1816 * This dbuf is already in the cache. We assume that
1817 * it is already CACHED, or else about to be either
1820 mutex_exit(&db
->db_mtx
);
1824 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
) == 0) {
1825 if (bp
&& !BP_IS_HOLE(bp
)) {
1826 int priority
= dn
->dn_type
== DMU_OT_DDT_ZAP
?
1827 ZIO_PRIORITY_DDT_PREFETCH
: ZIO_PRIORITY_ASYNC_READ
;
1829 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1830 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1833 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1834 dn
->dn_object
, 0, blkid
);
1839 pbuf
= dn
->dn_objset
->os_phys_buf
;
1841 (void) dsl_read(NULL
, dn
->dn_objset
->os_spa
,
1842 bp
, pbuf
, NULL
, NULL
, priority
,
1843 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1847 dbuf_rele(db
, NULL
);
1852 * Returns with db_holds incremented, and db_mtx not held.
1853 * Note: dn_struct_rwlock must be held.
1856 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
1857 void *tag
, dmu_buf_impl_t
**dbp
)
1859 dmu_buf_impl_t
*db
, *parent
= NULL
;
1861 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1862 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1863 ASSERT3U(dn
->dn_nlevels
, >, level
);
1867 /* dbuf_find() returns with db_mtx held */
1868 db
= dbuf_find(dn
, level
, blkid
);
1871 blkptr_t
*bp
= NULL
;
1874 ASSERT3P(parent
, ==, NULL
);
1875 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
1877 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
1881 dbuf_rele(parent
, NULL
);
1885 if (err
&& err
!= ENOENT
)
1887 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
1890 if (db
->db_buf
&& refcount_is_zero(&db
->db_holds
)) {
1891 arc_buf_add_ref(db
->db_buf
, db
);
1892 if (db
->db_buf
->b_data
== NULL
) {
1895 dbuf_rele(parent
, NULL
);
1900 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
1903 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
1906 * If this buffer is currently syncing out, and we are are
1907 * still referencing it from db_data, we need to make a copy
1908 * of it in case we decide we want to dirty it again in this txg.
1910 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1911 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
1912 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
1913 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
1915 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
1916 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1919 arc_buf_alloc(dn
->dn_objset
->os_spa
,
1920 db
->db
.db_size
, db
, type
));
1921 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
1926 (void) refcount_add(&db
->db_holds
, tag
);
1927 dbuf_update_data(db
);
1929 mutex_exit(&db
->db_mtx
);
1931 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1933 dbuf_rele(parent
, NULL
);
1935 ASSERT3P(DB_DNODE(db
), ==, dn
);
1936 ASSERT3U(db
->db_blkid
, ==, blkid
);
1937 ASSERT3U(db
->db_level
, ==, level
);
1944 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
1947 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
1948 return (err
? NULL
: db
);
1952 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
1955 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
1956 return (err
? NULL
: db
);
1960 dbuf_create_bonus(dnode_t
*dn
)
1962 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1964 ASSERT(dn
->dn_bonus
== NULL
);
1965 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
1969 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
1971 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1974 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1977 blksz
= SPA_MINBLOCKSIZE
;
1978 if (blksz
> SPA_MAXBLOCKSIZE
)
1979 blksz
= SPA_MAXBLOCKSIZE
;
1981 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
1985 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1986 dbuf_new_size(db
, blksz
, tx
);
1987 rw_exit(&dn
->dn_struct_rwlock
);
1994 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
1996 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
1999 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2001 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2003 VERIFY(refcount_add(&db
->db_holds
, tag
) > 1);
2007 * If you call dbuf_rele() you had better not be referencing the dnode handle
2008 * unless you have some other direct or indirect hold on the dnode. (An indirect
2009 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2010 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2011 * dnode's parent dbuf evicting its dnode handles.
2013 #pragma weak dmu_buf_rele = dbuf_rele
2015 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2017 mutex_enter(&db
->db_mtx
);
2018 dbuf_rele_and_unlock(db
, tag
);
2022 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2023 * db_dirtycnt and db_holds to be updated atomically.
2026 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2030 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2034 * Remove the reference to the dbuf before removing its hold on the
2035 * dnode so we can guarantee in dnode_move() that a referenced bonus
2036 * buffer has a corresponding dnode hold.
2038 holds
= refcount_remove(&db
->db_holds
, tag
);
2042 * We can't freeze indirects if there is a possibility that they
2043 * may be modified in the current syncing context.
2045 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
2046 arc_buf_freeze(db
->db_buf
);
2048 if (holds
== db
->db_dirtycnt
&&
2049 db
->db_level
== 0 && db
->db_immediate_evict
)
2050 dbuf_evict_user(db
);
2053 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2054 mutex_exit(&db
->db_mtx
);
2057 * If the dnode moves here, we cannot cross this barrier
2058 * until the move completes.
2061 (void) atomic_dec_32_nv(&DB_DNODE(db
)->dn_dbufs_count
);
2064 * The bonus buffer's dnode hold is no longer discounted
2065 * in dnode_move(). The dnode cannot move until after
2068 dnode_rele(DB_DNODE(db
), db
);
2069 } else if (db
->db_buf
== NULL
) {
2071 * This is a special case: we never associated this
2072 * dbuf with any data allocated from the ARC.
2074 ASSERT(db
->db_state
== DB_UNCACHED
||
2075 db
->db_state
== DB_NOFILL
);
2077 } else if (arc_released(db
->db_buf
)) {
2078 arc_buf_t
*buf
= db
->db_buf
;
2080 * This dbuf has anonymous data associated with it.
2082 dbuf_set_data(db
, NULL
);
2083 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
2086 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 0);
2087 if (!DBUF_IS_CACHEABLE(db
))
2090 mutex_exit(&db
->db_mtx
);
2093 mutex_exit(&db
->db_mtx
);
2097 #pragma weak dmu_buf_refcount = dbuf_refcount
2099 dbuf_refcount(dmu_buf_impl_t
*db
)
2101 return (refcount_count(&db
->db_holds
));
2105 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2106 dmu_buf_evict_func_t
*evict_func
)
2108 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2109 user_data_ptr_ptr
, evict_func
));
2113 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
2114 dmu_buf_evict_func_t
*evict_func
)
2116 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2118 db
->db_immediate_evict
= TRUE
;
2119 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
2120 user_data_ptr_ptr
, evict_func
));
2124 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
2125 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2127 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2128 ASSERT(db
->db_level
== 0);
2130 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2132 mutex_enter(&db
->db_mtx
);
2134 if (db
->db_user_ptr
== old_user_ptr
) {
2135 db
->db_user_ptr
= user_ptr
;
2136 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2137 db
->db_evict_func
= evict_func
;
2139 dbuf_update_data(db
);
2141 old_user_ptr
= db
->db_user_ptr
;
2144 mutex_exit(&db
->db_mtx
);
2145 return (old_user_ptr
);
2149 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2151 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2152 ASSERT(!refcount_is_zero(&db
->db_holds
));
2154 return (db
->db_user_ptr
);
2158 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2160 boolean_t res
= B_FALSE
;
2161 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2164 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2165 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2171 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2173 /* ASSERT(dmu_tx_is_syncing(tx) */
2174 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2176 if (db
->db_blkptr
!= NULL
)
2179 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2180 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2181 BP_ZERO(db
->db_blkptr
);
2184 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2186 * This buffer was allocated at a time when there was
2187 * no available blkptrs from the dnode, or it was
2188 * inappropriate to hook it in (i.e., nlevels mis-match).
2190 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2191 ASSERT(db
->db_parent
== NULL
);
2192 db
->db_parent
= dn
->dn_dbuf
;
2193 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2196 dmu_buf_impl_t
*parent
= db
->db_parent
;
2197 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2199 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2200 if (parent
== NULL
) {
2201 mutex_exit(&db
->db_mtx
);
2202 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2203 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2204 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2205 rw_exit(&dn
->dn_struct_rwlock
);
2206 mutex_enter(&db
->db_mtx
);
2207 db
->db_parent
= parent
;
2209 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2210 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2216 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2218 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2222 ASSERT(dmu_tx_is_syncing(tx
));
2224 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2226 mutex_enter(&db
->db_mtx
);
2228 ASSERT(db
->db_level
> 0);
2231 if (db
->db_buf
== NULL
) {
2232 mutex_exit(&db
->db_mtx
);
2233 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2234 mutex_enter(&db
->db_mtx
);
2236 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2237 ASSERT(db
->db_buf
!= NULL
);
2241 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2242 dbuf_check_blkptr(dn
, db
);
2245 db
->db_data_pending
= dr
;
2247 mutex_exit(&db
->db_mtx
);
2248 dbuf_write(dr
, db
->db_buf
, tx
);
2251 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2252 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2253 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2254 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2259 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2261 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2262 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2265 uint64_t txg
= tx
->tx_txg
;
2267 ASSERT(dmu_tx_is_syncing(tx
));
2269 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2271 mutex_enter(&db
->db_mtx
);
2273 * To be synced, we must be dirtied. But we
2274 * might have been freed after the dirty.
2276 if (db
->db_state
== DB_UNCACHED
) {
2277 /* This buffer has been freed since it was dirtied */
2278 ASSERT(db
->db
.db_data
== NULL
);
2279 } else if (db
->db_state
== DB_FILL
) {
2280 /* This buffer was freed and is now being re-filled */
2281 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2283 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2290 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2291 mutex_enter(&dn
->dn_mtx
);
2292 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2293 mutex_exit(&dn
->dn_mtx
);
2297 * If this is a bonus buffer, simply copy the bonus data into the
2298 * dnode. It will be written out when the dnode is synced (and it
2299 * will be synced, since it must have been dirty for dbuf_sync to
2302 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2303 dbuf_dirty_record_t
**drp
;
2305 ASSERT(*datap
!= NULL
);
2306 ASSERT3U(db
->db_level
, ==, 0);
2307 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2308 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2311 if (*datap
!= db
->db
.db_data
) {
2312 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2313 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2315 db
->db_data_pending
= NULL
;
2316 drp
= &db
->db_last_dirty
;
2318 drp
= &(*drp
)->dr_next
;
2319 ASSERT(dr
->dr_next
== NULL
);
2320 ASSERT(dr
->dr_dbuf
== db
);
2322 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2323 ASSERT(db
->db_dirtycnt
> 0);
2324 db
->db_dirtycnt
-= 1;
2325 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2332 * This function may have dropped the db_mtx lock allowing a dmu_sync
2333 * operation to sneak in. As a result, we need to ensure that we
2334 * don't check the dr_override_state until we have returned from
2335 * dbuf_check_blkptr.
2337 dbuf_check_blkptr(dn
, db
);
2340 * If this buffer is in the middle of an immediate write,
2341 * wait for the synchronous IO to complete.
2343 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2344 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2345 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2346 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2349 if (db
->db_state
!= DB_NOFILL
&&
2350 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2351 refcount_count(&db
->db_holds
) > 1 &&
2352 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2353 *datap
== db
->db_buf
) {
2355 * If this buffer is currently "in use" (i.e., there
2356 * are active holds and db_data still references it),
2357 * then make a copy before we start the write so that
2358 * any modifications from the open txg will not leak
2361 * NOTE: this copy does not need to be made for
2362 * objects only modified in the syncing context (e.g.
2363 * DNONE_DNODE blocks).
2365 int blksz
= arc_buf_size(*datap
);
2366 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2367 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2368 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2370 db
->db_data_pending
= dr
;
2372 mutex_exit(&db
->db_mtx
);
2374 dbuf_write(dr
, *datap
, tx
);
2376 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2377 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2378 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2382 * Although zio_nowait() does not "wait for an IO", it does
2383 * initiate the IO. If this is an empty write it seems plausible
2384 * that the IO could actually be completed before the nowait
2385 * returns. We need to DB_DNODE_EXIT() first in case
2386 * zio_nowait() invalidates the dbuf.
2389 zio_nowait(dr
->dr_zio
);
2394 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2396 dbuf_dirty_record_t
*dr
;
2398 while ((dr
= list_head(list
))) {
2399 if (dr
->dr_zio
!= NULL
) {
2401 * If we find an already initialized zio then we
2402 * are processing the meta-dnode, and we have finished.
2403 * The dbufs for all dnodes are put back on the list
2404 * during processing, so that we can zio_wait()
2405 * these IOs after initiating all child IOs.
2407 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2408 DMU_META_DNODE_OBJECT
);
2411 list_remove(list
, dr
);
2412 if (dr
->dr_dbuf
->db_level
> 0)
2413 dbuf_sync_indirect(dr
, tx
);
2415 dbuf_sync_leaf(dr
, tx
);
2421 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2423 dmu_buf_impl_t
*db
= vdb
;
2425 blkptr_t
*bp
= zio
->io_bp
;
2426 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2427 spa_t
*spa
= zio
->io_spa
;
2432 ASSERT(db
->db_blkptr
== bp
);
2436 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2437 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2438 zio
->io_prev_space_delta
= delta
;
2440 if (BP_IS_HOLE(bp
)) {
2441 ASSERT(bp
->blk_fill
== 0);
2446 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2447 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2448 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2449 BP_GET_TYPE(bp
) == dn
->dn_bonustype
));
2450 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2452 mutex_enter(&db
->db_mtx
);
2455 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2456 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2457 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2458 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2462 if (db
->db_level
== 0) {
2463 mutex_enter(&dn
->dn_mtx
);
2464 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2465 db
->db_blkid
!= DMU_SPILL_BLKID
)
2466 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2467 mutex_exit(&dn
->dn_mtx
);
2469 if (dn
->dn_type
== DMU_OT_DNODE
) {
2470 dnode_phys_t
*dnp
= db
->db
.db_data
;
2471 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2473 if (dnp
->dn_type
!= DMU_OT_NONE
)
2480 blkptr_t
*ibp
= db
->db
.db_data
;
2481 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2482 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2483 if (BP_IS_HOLE(ibp
))
2485 fill
+= ibp
->blk_fill
;
2490 bp
->blk_fill
= fill
;
2492 mutex_exit(&db
->db_mtx
);
2497 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2499 dmu_buf_impl_t
*db
= vdb
;
2500 blkptr_t
*bp
= zio
->io_bp
;
2501 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2502 uint64_t txg
= zio
->io_txg
;
2503 dbuf_dirty_record_t
**drp
, *dr
;
2505 ASSERT3U(zio
->io_error
, ==, 0);
2506 ASSERT(db
->db_blkptr
== bp
);
2508 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
2509 ASSERT(BP_EQUAL(bp
, bp_orig
));
2515 DB_GET_OBJSET(&os
, db
);
2516 ds
= os
->os_dsl_dataset
;
2519 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2520 dsl_dataset_block_born(ds
, bp
, tx
);
2523 mutex_enter(&db
->db_mtx
);
2527 drp
= &db
->db_last_dirty
;
2528 while ((dr
= *drp
) != db
->db_data_pending
)
2530 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2531 ASSERT(dr
->dr_txg
== txg
);
2532 ASSERT(dr
->dr_dbuf
== db
);
2533 ASSERT(dr
->dr_next
== NULL
);
2537 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2542 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2543 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2544 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2549 if (db
->db_level
== 0) {
2550 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2551 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2552 if (db
->db_state
!= DB_NOFILL
) {
2553 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2554 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2556 else if (!arc_released(db
->db_buf
))
2557 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2564 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2565 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2566 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2567 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
2569 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2571 ASSERT3U(dn
->dn_phys
->dn_maxblkid
2572 >> (db
->db_level
* epbs
), >=, db
->db_blkid
);
2573 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2576 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2577 list_destroy(&dr
->dt
.di
.dr_children
);
2579 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2581 cv_broadcast(&db
->db_changed
);
2582 ASSERT(db
->db_dirtycnt
> 0);
2583 db
->db_dirtycnt
-= 1;
2584 db
->db_data_pending
= NULL
;
2585 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2589 dbuf_write_nofill_ready(zio_t
*zio
)
2591 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2595 dbuf_write_nofill_done(zio_t
*zio
)
2597 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2601 dbuf_write_override_ready(zio_t
*zio
)
2603 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2604 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2606 dbuf_write_ready(zio
, NULL
, db
);
2610 dbuf_write_override_done(zio_t
*zio
)
2612 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2613 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2614 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2616 mutex_enter(&db
->db_mtx
);
2617 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2618 if (!BP_IS_HOLE(obp
))
2619 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2620 arc_release(dr
->dt
.dl
.dr_data
, db
);
2622 mutex_exit(&db
->db_mtx
);
2624 dbuf_write_done(zio
, NULL
, db
);
2628 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2630 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2633 dmu_buf_impl_t
*parent
= db
->db_parent
;
2634 uint64_t txg
= tx
->tx_txg
;
2644 if (db
->db_state
!= DB_NOFILL
) {
2645 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2647 * Private object buffers are released here rather
2648 * than in dbuf_dirty() since they are only modified
2649 * in the syncing context and we don't want the
2650 * overhead of making multiple copies of the data.
2652 if (BP_IS_HOLE(db
->db_blkptr
)) {
2655 dbuf_release_bp(db
);
2660 if (parent
!= dn
->dn_dbuf
) {
2661 ASSERT(parent
&& parent
->db_data_pending
);
2662 ASSERT(db
->db_level
== parent
->db_level
-1);
2663 ASSERT(arc_released(parent
->db_buf
));
2664 zio
= parent
->db_data_pending
->dr_zio
;
2666 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2667 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2668 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2669 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2670 ASSERT3P(db
->db_blkptr
, ==,
2671 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2675 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2676 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2679 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2680 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2681 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2683 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2685 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2687 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2690 if (db
->db_level
== 0 && dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2691 ASSERT(db
->db_state
!= DB_NOFILL
);
2692 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2693 db
->db_blkptr
, data
->b_data
, arc_buf_size(data
), &zp
,
2694 dbuf_write_override_ready
, dbuf_write_override_done
, dr
,
2695 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2696 mutex_enter(&db
->db_mtx
);
2697 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2698 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2699 dr
->dt
.dl
.dr_copies
);
2700 mutex_exit(&db
->db_mtx
);
2701 } else if (db
->db_state
== DB_NOFILL
) {
2702 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2703 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2704 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2705 dbuf_write_nofill_ready
, dbuf_write_nofill_done
, db
,
2706 ZIO_PRIORITY_ASYNC_WRITE
,
2707 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2709 ASSERT(arc_released(data
));
2710 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2711 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
), &zp
,
2712 dbuf_write_ready
, dbuf_write_done
, db
,
2713 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);