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
;
110 uint64_t obj
= dn
->dn_object
;
111 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
112 uint64_t idx
= hv
& h
->hash_table_mask
;
115 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
116 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
117 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
118 mutex_enter(&db
->db_mtx
);
119 if (db
->db_state
!= DB_EVICTING
) {
120 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
123 mutex_exit(&db
->db_mtx
);
126 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
131 * Insert an entry into the hash table. If there is already an element
132 * equal to elem in the hash table, then the already existing element
133 * will be returned and the new element will not be inserted.
134 * Otherwise returns NULL.
136 static dmu_buf_impl_t
*
137 dbuf_hash_insert(dmu_buf_impl_t
*db
)
139 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
140 objset_t
*os
= db
->db_objset
;
141 uint64_t obj
= db
->db
.db_object
;
142 int level
= db
->db_level
;
143 uint64_t blkid
= db
->db_blkid
;
144 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
145 uint64_t idx
= hv
& h
->hash_table_mask
;
148 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
149 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
150 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
151 mutex_enter(&dbf
->db_mtx
);
152 if (dbf
->db_state
!= DB_EVICTING
) {
153 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
156 mutex_exit(&dbf
->db_mtx
);
160 mutex_enter(&db
->db_mtx
);
161 db
->db_hash_next
= h
->hash_table
[idx
];
162 h
->hash_table
[idx
] = db
;
163 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
164 atomic_add_64(&dbuf_hash_count
, 1);
170 * Remove an entry from the hash table. This operation will
171 * fail if there are any existing holds on the db.
174 dbuf_hash_remove(dmu_buf_impl_t
*db
)
176 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
177 uint64_t hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
178 db
->db_level
, db
->db_blkid
);
179 uint64_t idx
= hv
& h
->hash_table_mask
;
180 dmu_buf_impl_t
*dbf
, **dbp
;
183 * We musn't hold db_mtx to maintin lock ordering:
184 * DBUF_HASH_MUTEX > db_mtx.
186 ASSERT(refcount_is_zero(&db
->db_holds
));
187 ASSERT(db
->db_state
== DB_EVICTING
);
188 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
190 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
191 dbp
= &h
->hash_table
[idx
];
192 while ((dbf
= *dbp
) != db
) {
193 dbp
= &dbf
->db_hash_next
;
196 *dbp
= db
->db_hash_next
;
197 db
->db_hash_next
= NULL
;
198 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
199 atomic_add_64(&dbuf_hash_count
, -1);
202 static arc_evict_func_t dbuf_do_evict
;
205 dbuf_evict_user(dmu_buf_impl_t
*db
)
207 ASSERT(MUTEX_HELD(&db
->db_mtx
));
209 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
212 if (db
->db_user_data_ptr_ptr
)
213 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
214 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
215 db
->db_user_ptr
= NULL
;
216 db
->db_user_data_ptr_ptr
= NULL
;
217 db
->db_evict_func
= NULL
;
221 dbuf_evict(dmu_buf_impl_t
*db
)
223 ASSERT(MUTEX_HELD(&db
->db_mtx
));
224 ASSERT(db
->db_buf
== NULL
);
225 ASSERT(db
->db_data_pending
== NULL
);
234 uint64_t hsize
= 1ULL << 16;
235 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
239 * The hash table is big enough to fill all of physical memory
240 * with an average 4K block size. The table will take up
241 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
243 while (hsize
* 4096 < physmem
* PAGESIZE
)
247 h
->hash_table_mask
= hsize
- 1;
248 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
249 if (h
->hash_table
== NULL
) {
250 /* XXX - we should really return an error instead of assert */
251 ASSERT(hsize
> (1ULL << 10));
256 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
257 sizeof (dmu_buf_impl_t
),
258 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
260 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
261 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
267 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
270 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
271 mutex_destroy(&h
->hash_mutexes
[i
]);
272 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
273 kmem_cache_destroy(dbuf_cache
);
282 dbuf_verify(dmu_buf_impl_t
*db
)
284 dnode_t
*dn
= db
->db_dnode
;
285 dbuf_dirty_record_t
*dr
;
287 ASSERT(MUTEX_HELD(&db
->db_mtx
));
289 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
292 ASSERT(db
->db_objset
!= NULL
);
294 ASSERT(db
->db_parent
== NULL
);
295 ASSERT(db
->db_blkptr
== NULL
);
297 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
298 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
299 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
300 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
|| db
->db_blkid
==
301 DMU_SPILL_BLKID
|| list_head(&dn
->dn_dbufs
));
303 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
305 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
306 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
307 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
309 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
310 ASSERT3U(db
->db
.db_offset
, ==, 0);
312 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
315 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
316 ASSERT(dr
->dr_dbuf
== db
);
318 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
319 ASSERT(dr
->dr_dbuf
== db
);
322 * We can't assert that db_size matches dn_datablksz because it
323 * can be momentarily different when another thread is doing
326 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
327 dr
= db
->db_data_pending
;
329 * It should only be modified in syncing context, so
330 * make sure we only have one copy of the data.
332 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
335 /* verify db->db_blkptr */
337 if (db
->db_parent
== dn
->dn_dbuf
) {
338 /* db is pointed to by the dnode */
339 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
340 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
341 ASSERT(db
->db_parent
== NULL
);
343 ASSERT(db
->db_parent
!= NULL
);
344 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
345 ASSERT3P(db
->db_blkptr
, ==,
346 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
348 /* db is pointed to by an indirect block */
349 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
350 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
351 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
354 * dnode_grow_indblksz() can make this fail if we don't
355 * have the struct_rwlock. XXX indblksz no longer
356 * grows. safe to do this now?
358 if (RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
)) {
359 ASSERT3P(db
->db_blkptr
, ==,
360 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
361 db
->db_blkid
% epb
));
365 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
366 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
367 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
368 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
370 * If the blkptr isn't set but they have nonzero data,
371 * it had better be dirty, otherwise we'll lose that
372 * data when we evict this buffer.
374 if (db
->db_dirtycnt
== 0) {
375 uint64_t *buf
= db
->db
.db_data
;
378 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
387 dbuf_update_data(dmu_buf_impl_t
*db
)
389 ASSERT(MUTEX_HELD(&db
->db_mtx
));
390 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
391 ASSERT(!refcount_is_zero(&db
->db_holds
));
392 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
397 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
399 ASSERT(MUTEX_HELD(&db
->db_mtx
));
400 ASSERT(db
->db_buf
== NULL
|| !arc_has_callback(db
->db_buf
));
403 ASSERT(buf
->b_data
!= NULL
);
404 db
->db
.db_data
= buf
->b_data
;
405 if (!arc_released(buf
))
406 arc_set_callback(buf
, dbuf_do_evict
, db
);
407 dbuf_update_data(db
);
410 db
->db
.db_data
= NULL
;
411 if (db
->db_state
!= DB_NOFILL
)
412 db
->db_state
= DB_UNCACHED
;
417 * Loan out an arc_buf for read. Return the loaned arc_buf.
420 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
424 mutex_enter(&db
->db_mtx
);
425 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
426 int blksz
= db
->db
.db_size
;
427 mutex_exit(&db
->db_mtx
);
428 abuf
= arc_loan_buf(db
->db_dnode
->dn_objset
->os_spa
, blksz
);
429 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
432 arc_loan_inuse_buf(abuf
, db
);
433 dbuf_set_data(db
, NULL
);
434 mutex_exit(&db
->db_mtx
);
440 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
442 if (dn
->dn_datablkshift
) {
443 return (offset
>> dn
->dn_datablkshift
);
445 ASSERT3U(offset
, <, dn
->dn_datablksz
);
451 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
453 dmu_buf_impl_t
*db
= vdb
;
455 mutex_enter(&db
->db_mtx
);
456 ASSERT3U(db
->db_state
, ==, DB_READ
);
458 * All reads are synchronous, so we must have a hold on the dbuf
460 ASSERT(refcount_count(&db
->db_holds
) > 0);
461 ASSERT(db
->db_buf
== NULL
);
462 ASSERT(db
->db
.db_data
== NULL
);
463 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
464 /* we were freed in flight; disregard any error */
465 arc_release(buf
, db
);
466 bzero(buf
->b_data
, db
->db
.db_size
);
468 db
->db_freed_in_flight
= FALSE
;
469 dbuf_set_data(db
, buf
);
470 db
->db_state
= DB_CACHED
;
471 } else if (zio
== NULL
|| zio
->io_error
== 0) {
472 dbuf_set_data(db
, buf
);
473 db
->db_state
= DB_CACHED
;
475 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
476 ASSERT3P(db
->db_buf
, ==, NULL
);
477 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
478 db
->db_state
= DB_UNCACHED
;
480 cv_broadcast(&db
->db_changed
);
481 dbuf_rele_and_unlock(db
, NULL
);
485 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
487 dnode_t
*dn
= db
->db_dnode
;
489 uint32_t aflags
= ARC_NOWAIT
;
492 ASSERT(!refcount_is_zero(&db
->db_holds
));
493 /* We need the struct_rwlock to prevent db_blkptr from changing. */
494 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
495 ASSERT(MUTEX_HELD(&db
->db_mtx
));
496 ASSERT(db
->db_state
== DB_UNCACHED
);
497 ASSERT(db
->db_buf
== NULL
);
499 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
500 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
502 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
503 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
504 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
505 if (bonuslen
< DN_MAX_BONUSLEN
)
506 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
508 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
509 dbuf_update_data(db
);
510 db
->db_state
= DB_CACHED
;
511 mutex_exit(&db
->db_mtx
);
516 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
517 * processes the delete record and clears the bp while we are waiting
518 * for the dn_mtx (resulting in a "no" from block_freed).
520 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
521 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
522 BP_IS_HOLE(db
->db_blkptr
)))) {
523 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
525 dbuf_set_data(db
, arc_buf_alloc(dn
->dn_objset
->os_spa
,
526 db
->db
.db_size
, db
, type
));
527 bzero(db
->db
.db_data
, db
->db
.db_size
);
528 db
->db_state
= DB_CACHED
;
529 *flags
|= DB_RF_CACHED
;
530 mutex_exit(&db
->db_mtx
);
534 db
->db_state
= DB_READ
;
535 mutex_exit(&db
->db_mtx
);
537 if (DBUF_IS_L2CACHEABLE(db
))
538 aflags
|= ARC_L2CACHE
;
540 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
541 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
542 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
544 dbuf_add_ref(db
, NULL
);
545 /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
548 pbuf
= db
->db_parent
->db_buf
;
550 pbuf
= db
->db_objset
->os_phys_buf
;
552 (void) dsl_read(zio
, dn
->dn_objset
->os_spa
, db
->db_blkptr
, pbuf
,
553 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
554 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
556 if (aflags
& ARC_CACHED
)
557 *flags
|= DB_RF_CACHED
;
561 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
564 int havepzio
= (zio
!= NULL
);
568 * We don't have to hold the mutex to check db_state because it
569 * can't be freed while we have a hold on the buffer.
571 ASSERT(!refcount_is_zero(&db
->db_holds
));
573 if (db
->db_state
== DB_NOFILL
)
576 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
577 rw_enter(&db
->db_dnode
->dn_struct_rwlock
, RW_READER
);
579 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
580 (flags
& DB_RF_NOPREFETCH
) == 0 && db
->db_dnode
!= NULL
&&
581 DBUF_IS_CACHEABLE(db
);
583 mutex_enter(&db
->db_mtx
);
584 if (db
->db_state
== DB_CACHED
) {
585 mutex_exit(&db
->db_mtx
);
587 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
588 db
->db
.db_size
, TRUE
);
589 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
590 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
591 } else if (db
->db_state
== DB_UNCACHED
) {
593 zio
= zio_root(db
->db_dnode
->dn_objset
->os_spa
,
594 NULL
, NULL
, ZIO_FLAG_CANFAIL
);
596 dbuf_read_impl(db
, zio
, &flags
);
598 /* dbuf_read_impl has dropped db_mtx for us */
601 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
602 db
->db
.db_size
, flags
& DB_RF_CACHED
);
604 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
605 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
610 mutex_exit(&db
->db_mtx
);
612 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
613 db
->db
.db_size
, TRUE
);
614 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
615 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
617 mutex_enter(&db
->db_mtx
);
618 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
619 while (db
->db_state
== DB_READ
||
620 db
->db_state
== DB_FILL
) {
621 ASSERT(db
->db_state
== DB_READ
||
622 (flags
& DB_RF_HAVESTRUCT
) == 0);
623 cv_wait(&db
->db_changed
, &db
->db_mtx
);
625 if (db
->db_state
== DB_UNCACHED
)
628 mutex_exit(&db
->db_mtx
);
631 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
636 dbuf_noread(dmu_buf_impl_t
*db
)
638 ASSERT(!refcount_is_zero(&db
->db_holds
));
639 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
640 mutex_enter(&db
->db_mtx
);
641 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
642 cv_wait(&db
->db_changed
, &db
->db_mtx
);
643 if (db
->db_state
== DB_UNCACHED
) {
644 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
646 ASSERT(db
->db_buf
== NULL
);
647 ASSERT(db
->db
.db_data
== NULL
);
648 dbuf_set_data(db
, arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
,
649 db
->db
.db_size
, db
, type
));
650 db
->db_state
= DB_FILL
;
651 } else if (db
->db_state
== DB_NOFILL
) {
652 dbuf_set_data(db
, NULL
);
654 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
656 mutex_exit(&db
->db_mtx
);
660 * This is our just-in-time copy function. It makes a copy of
661 * buffers, that have been modified in a previous transaction
662 * group, before we modify them in the current active group.
664 * This function is used in two places: when we are dirtying a
665 * buffer for the first time in a txg, and when we are freeing
666 * a range in a dnode that includes this buffer.
668 * Note that when we are called from dbuf_free_range() we do
669 * not put a hold on the buffer, we just traverse the active
670 * dbuf list for the dnode.
673 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
675 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
677 ASSERT(MUTEX_HELD(&db
->db_mtx
));
678 ASSERT(db
->db
.db_data
!= NULL
);
679 ASSERT(db
->db_level
== 0);
680 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
683 (dr
->dt
.dl
.dr_data
!=
684 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
688 * If the last dirty record for this dbuf has not yet synced
689 * and its referencing the dbuf data, either:
690 * reset the reference to point to a new copy,
691 * or (if there a no active holders)
692 * just null out the current db_data pointer.
694 ASSERT(dr
->dr_txg
>= txg
- 2);
695 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
696 /* Note that the data bufs here are zio_bufs */
697 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
698 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
699 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
700 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
701 int size
= db
->db
.db_size
;
702 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
703 dr
->dt
.dl
.dr_data
= arc_buf_alloc(
704 db
->db_dnode
->dn_objset
->os_spa
, size
, db
, type
);
705 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
707 dbuf_set_data(db
, NULL
);
712 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
714 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
715 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
716 uint64_t txg
= dr
->dr_txg
;
718 ASSERT(MUTEX_HELD(&db
->db_mtx
));
719 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
720 ASSERT(db
->db_level
== 0);
722 if (db
->db_blkid
== DMU_BONUS_BLKID
||
723 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
726 ASSERT(db
->db_data_pending
!= dr
);
728 /* free this block */
730 zio_free(db
->db_dnode
->dn_objset
->os_spa
, txg
, bp
);
732 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
734 * Release the already-written buffer, so we leave it in
735 * a consistent dirty state. Note that all callers are
736 * modifying the buffer, so they will immediately do
737 * another (redundant) arc_release(). Therefore, leave
738 * the buf thawed to save the effort of freezing &
739 * immediately re-thawing it.
741 arc_release(dr
->dt
.dl
.dr_data
, db
);
745 * Evict (if its unreferenced) or clear (if its referenced) any level-0
746 * data blocks in the free range, so that any future readers will find
747 * empty blocks. Also, if we happen accross any level-1 dbufs in the
748 * range that have not already been marked dirty, mark them dirty so
749 * they stay in memory.
752 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
754 dmu_buf_impl_t
*db
, *db_next
;
755 uint64_t txg
= tx
->tx_txg
;
756 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
757 uint64_t first_l1
= start
>> epbs
;
758 uint64_t last_l1
= end
>> epbs
;
760 if (end
> dn
->dn_maxblkid
&& (end
!= DMU_SPILL_BLKID
)) {
761 end
= dn
->dn_maxblkid
;
762 last_l1
= end
>> epbs
;
764 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
765 mutex_enter(&dn
->dn_dbufs_mtx
);
766 for (db
= list_head(&dn
->dn_dbufs
); db
; db
= db_next
) {
767 db_next
= list_next(&dn
->dn_dbufs
, db
);
768 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
770 if (db
->db_level
== 1 &&
771 db
->db_blkid
>= first_l1
&& db
->db_blkid
<= last_l1
) {
772 mutex_enter(&db
->db_mtx
);
773 if (db
->db_last_dirty
&&
774 db
->db_last_dirty
->dr_txg
< txg
) {
775 dbuf_add_ref(db
, FTAG
);
776 mutex_exit(&db
->db_mtx
);
777 dbuf_will_dirty(db
, tx
);
780 mutex_exit(&db
->db_mtx
);
784 if (db
->db_level
!= 0)
786 dprintf_dbuf(db
, "found buf %s\n", "");
787 if (db
->db_blkid
< start
|| db
->db_blkid
> end
)
790 /* found a level 0 buffer in the range */
791 if (dbuf_undirty(db
, tx
))
794 mutex_enter(&db
->db_mtx
);
795 if (db
->db_state
== DB_UNCACHED
||
796 db
->db_state
== DB_NOFILL
||
797 db
->db_state
== DB_EVICTING
) {
798 ASSERT(db
->db
.db_data
== NULL
);
799 mutex_exit(&db
->db_mtx
);
802 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
803 /* will be handled in dbuf_read_done or dbuf_rele */
804 db
->db_freed_in_flight
= TRUE
;
805 mutex_exit(&db
->db_mtx
);
808 if (refcount_count(&db
->db_holds
) == 0) {
813 /* The dbuf is referenced */
815 if (db
->db_last_dirty
!= NULL
) {
816 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
818 if (dr
->dr_txg
== txg
) {
820 * This buffer is "in-use", re-adjust the file
821 * size to reflect that this buffer may
822 * contain new data when we sync.
824 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
825 db
->db_blkid
> dn
->dn_maxblkid
)
826 dn
->dn_maxblkid
= db
->db_blkid
;
830 * This dbuf is not dirty in the open context.
831 * Either uncache it (if its not referenced in
832 * the open context) or reset its contents to
835 dbuf_fix_old_data(db
, txg
);
838 /* clear the contents if its cached */
839 if (db
->db_state
== DB_CACHED
) {
840 ASSERT(db
->db
.db_data
!= NULL
);
841 arc_release(db
->db_buf
, db
);
842 bzero(db
->db
.db_data
, db
->db
.db_size
);
843 arc_buf_freeze(db
->db_buf
);
846 mutex_exit(&db
->db_mtx
);
848 mutex_exit(&dn
->dn_dbufs_mtx
);
852 dbuf_block_freeable(dmu_buf_impl_t
*db
)
854 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
855 uint64_t birth_txg
= 0;
858 * We don't need any locking to protect db_blkptr:
859 * If it's syncing, then db_last_dirty will be set
860 * so we'll ignore db_blkptr.
862 ASSERT(MUTEX_HELD(&db
->db_mtx
));
863 if (db
->db_last_dirty
)
864 birth_txg
= db
->db_last_dirty
->dr_txg
;
865 else if (db
->db_blkptr
)
866 birth_txg
= db
->db_blkptr
->blk_birth
;
868 /* If we don't exist or are in a snapshot, we can't be freed */
870 return (ds
== NULL
||
871 dsl_dataset_block_freeable(ds
, db
->db_blkptr
, birth_txg
));
877 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
879 arc_buf_t
*buf
, *obuf
;
880 int osize
= db
->db
.db_size
;
881 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
883 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
885 /* XXX does *this* func really need the lock? */
886 ASSERT(RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
));
889 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
890 * is OK, because there can be no other references to the db
891 * when we are changing its size, so no concurrent DB_FILL can
895 * XXX we should be doing a dbuf_read, checking the return
896 * value and returning that up to our callers
898 dbuf_will_dirty(db
, tx
);
900 /* create the data buffer for the new block */
901 buf
= arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
, size
, db
, type
);
903 /* copy old block data to the new block */
905 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
906 /* zero the remainder */
908 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
910 mutex_enter(&db
->db_mtx
);
911 dbuf_set_data(db
, buf
);
912 VERIFY(arc_buf_remove_ref(obuf
, db
) == 1);
913 db
->db
.db_size
= size
;
915 if (db
->db_level
== 0) {
916 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
917 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
919 mutex_exit(&db
->db_mtx
);
921 dnode_willuse_space(db
->db_dnode
, size
-osize
, tx
);
925 dbuf_release_bp(dmu_buf_impl_t
*db
)
927 objset_t
*os
= db
->db_dnode
->dn_objset
;
930 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
931 ASSERT(arc_released(os
->os_phys_buf
) ||
932 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
933 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
935 zb
.zb_objset
= os
->os_dsl_dataset
?
936 os
->os_dsl_dataset
->ds_object
: 0;
937 zb
.zb_object
= db
->db
.db_object
;
938 zb
.zb_level
= db
->db_level
;
939 zb
.zb_blkid
= db
->db_blkid
;
940 (void) arc_release_bp(db
->db_buf
, db
,
941 db
->db_blkptr
, os
->os_spa
, &zb
);
944 dbuf_dirty_record_t
*
945 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
947 dnode_t
*dn
= db
->db_dnode
;
948 objset_t
*os
= dn
->dn_objset
;
949 dbuf_dirty_record_t
**drp
, *dr
;
950 int drop_struct_lock
= FALSE
;
951 boolean_t do_free_accounting
= B_FALSE
;
952 int txgoff
= tx
->tx_txg
& TXG_MASK
;
954 ASSERT(tx
->tx_txg
!= 0);
955 ASSERT(!refcount_is_zero(&db
->db_holds
));
956 DMU_TX_DIRTY_BUF(tx
, db
);
959 * Shouldn't dirty a regular buffer in syncing context. Private
960 * objects may be dirtied in syncing context, but only if they
961 * were already pre-dirtied in open context.
963 ASSERT(!dmu_tx_is_syncing(tx
) ||
964 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
965 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
966 dn
->dn_objset
->os_dsl_dataset
== NULL
);
968 * We make this assert for private objects as well, but after we
969 * check if we're already dirty. They are allowed to re-dirty
970 * in syncing context.
972 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
973 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
974 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
976 mutex_enter(&db
->db_mtx
);
978 * XXX make this true for indirects too? The problem is that
979 * transactions created with dmu_tx_create_assigned() from
980 * syncing context don't bother holding ahead.
982 ASSERT(db
->db_level
!= 0 ||
983 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
984 db
->db_state
== DB_NOFILL
);
986 mutex_enter(&dn
->dn_mtx
);
988 * Don't set dirtyctx to SYNC if we're just modifying this as we
989 * initialize the objset.
991 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
992 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
994 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
995 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
996 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
998 mutex_exit(&dn
->dn_mtx
);
1000 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1001 dn
->dn_have_spill
= B_TRUE
;
1004 * If this buffer is already dirty, we're done.
1006 drp
= &db
->db_last_dirty
;
1007 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1008 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1009 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1011 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1012 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1014 * If this buffer has already been written out,
1015 * we now need to reset its state.
1017 dbuf_unoverride(dr
);
1018 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1019 db
->db_state
!= DB_NOFILL
)
1020 arc_buf_thaw(db
->db_buf
);
1022 mutex_exit(&db
->db_mtx
);
1027 * Only valid if not already dirty.
1029 ASSERT(dn
->dn_object
== 0 ||
1030 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1031 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1033 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1034 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1035 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1036 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1037 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1038 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1041 * We should only be dirtying in syncing context if it's the
1042 * mos or we're initializing the os or it's a special object.
1043 * However, we are allowed to dirty in syncing context provided
1044 * we already dirtied it in open context. Hence we must make
1045 * this assertion only if we're not already dirty.
1047 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1048 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1049 ASSERT(db
->db
.db_size
!= 0);
1051 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1053 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1055 * Update the accounting.
1056 * Note: we delay "free accounting" until after we drop
1057 * the db_mtx. This keeps us from grabbing other locks
1058 * (and possibly deadlocking) in bp_get_dsize() while
1059 * also holding the db_mtx.
1061 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1062 do_free_accounting
= dbuf_block_freeable(db
);
1066 * If this buffer is dirty in an old transaction group we need
1067 * to make a copy of it so that the changes we make in this
1068 * transaction group won't leak out when we sync the older txg.
1070 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1071 if (db
->db_level
== 0) {
1072 void *data_old
= db
->db_buf
;
1074 if (db
->db_state
!= DB_NOFILL
) {
1075 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1076 dbuf_fix_old_data(db
, tx
->tx_txg
);
1077 data_old
= db
->db
.db_data
;
1078 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1080 * Release the data buffer from the cache so
1081 * that we can modify it without impacting
1082 * possible other users of this cached data
1083 * block. Note that indirect blocks and
1084 * private objects are not released until the
1085 * syncing state (since they are only modified
1088 arc_release(db
->db_buf
, db
);
1089 dbuf_fix_old_data(db
, tx
->tx_txg
);
1090 data_old
= db
->db_buf
;
1092 ASSERT(data_old
!= NULL
);
1094 dr
->dt
.dl
.dr_data
= data_old
;
1096 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1097 list_create(&dr
->dt
.di
.dr_children
,
1098 sizeof (dbuf_dirty_record_t
),
1099 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1102 dr
->dr_txg
= tx
->tx_txg
;
1107 * We could have been freed_in_flight between the dbuf_noread
1108 * and dbuf_dirty. We win, as though the dbuf_noread() had
1109 * happened after the free.
1111 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1112 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1113 mutex_enter(&dn
->dn_mtx
);
1114 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1115 mutex_exit(&dn
->dn_mtx
);
1116 db
->db_freed_in_flight
= FALSE
;
1120 * This buffer is now part of this txg
1122 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1123 db
->db_dirtycnt
+= 1;
1124 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1126 mutex_exit(&db
->db_mtx
);
1128 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1129 db
->db_blkid
== DMU_SPILL_BLKID
) {
1130 mutex_enter(&dn
->dn_mtx
);
1131 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1132 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1133 mutex_exit(&dn
->dn_mtx
);
1134 dnode_setdirty(dn
, tx
);
1136 } else if (do_free_accounting
) {
1137 blkptr_t
*bp
= db
->db_blkptr
;
1138 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1139 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1141 * This is only a guess -- if the dbuf is dirty
1142 * in a previous txg, we don't know how much
1143 * space it will use on disk yet. We should
1144 * really have the struct_rwlock to access
1145 * db_blkptr, but since this is just a guess,
1146 * it's OK if we get an odd answer.
1148 dnode_willuse_space(dn
, -willfree
, tx
);
1151 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1152 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1153 drop_struct_lock
= TRUE
;
1156 if (db
->db_level
== 0) {
1157 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1158 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1161 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1162 dmu_buf_impl_t
*parent
= db
->db_parent
;
1163 dbuf_dirty_record_t
*di
;
1164 int parent_held
= FALSE
;
1166 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1167 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1169 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1170 db
->db_blkid
>> epbs
, FTAG
);
1171 ASSERT(parent
!= NULL
);
1174 if (drop_struct_lock
)
1175 rw_exit(&dn
->dn_struct_rwlock
);
1176 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1177 di
= dbuf_dirty(parent
, tx
);
1179 dbuf_rele(parent
, FTAG
);
1181 mutex_enter(&db
->db_mtx
);
1182 /* possible race with dbuf_undirty() */
1183 if (db
->db_last_dirty
== dr
||
1184 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1185 mutex_enter(&di
->dt
.di
.dr_mtx
);
1186 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1187 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1188 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1189 mutex_exit(&di
->dt
.di
.dr_mtx
);
1192 mutex_exit(&db
->db_mtx
);
1194 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1195 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1196 ASSERT(db
->db_parent
== NULL
||
1197 db
->db_parent
== db
->db_dnode
->dn_dbuf
);
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 if (drop_struct_lock
)
1203 rw_exit(&dn
->dn_struct_rwlock
);
1206 dnode_setdirty(dn
, tx
);
1211 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1213 dnode_t
*dn
= db
->db_dnode
;
1214 uint64_t txg
= tx
->tx_txg
;
1215 dbuf_dirty_record_t
*dr
, **drp
;
1218 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1220 mutex_enter(&db
->db_mtx
);
1222 * If this buffer is not dirty, we're done.
1224 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1225 if (dr
->dr_txg
<= txg
)
1227 if (dr
== NULL
|| dr
->dr_txg
< txg
) {
1228 mutex_exit(&db
->db_mtx
);
1231 ASSERT(dr
->dr_txg
== txg
);
1232 ASSERT(dr
->dr_dbuf
== db
);
1235 * If this buffer is currently held, we cannot undirty
1236 * it, since one of the current holders may be in the
1237 * middle of an update. Note that users of dbuf_undirty()
1238 * should not place a hold on the dbuf before the call.
1240 if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1241 mutex_exit(&db
->db_mtx
);
1242 /* Make sure we don't toss this buffer at sync phase */
1243 mutex_enter(&dn
->dn_mtx
);
1244 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1245 mutex_exit(&dn
->dn_mtx
);
1249 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1251 ASSERT(db
->db
.db_size
!= 0);
1253 /* XXX would be nice to fix up dn_towrite_space[] */
1257 if (dr
->dr_parent
) {
1258 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1259 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1260 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1261 } else if (db
->db_level
+1 == dn
->dn_nlevels
) {
1262 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1263 mutex_enter(&dn
->dn_mtx
);
1264 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1265 mutex_exit(&dn
->dn_mtx
);
1268 if (db
->db_level
== 0) {
1269 if (db
->db_state
!= DB_NOFILL
) {
1270 dbuf_unoverride(dr
);
1272 ASSERT(db
->db_buf
!= NULL
);
1273 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1274 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1275 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
1279 ASSERT(db
->db_buf
!= NULL
);
1280 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
1281 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
1282 list_destroy(&dr
->dt
.di
.dr_children
);
1284 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1286 ASSERT(db
->db_dirtycnt
> 0);
1287 db
->db_dirtycnt
-= 1;
1289 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1290 arc_buf_t
*buf
= db
->db_buf
;
1292 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(buf
));
1293 dbuf_set_data(db
, NULL
);
1294 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1299 mutex_exit(&db
->db_mtx
);
1303 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1305 dbuf_will_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1307 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1309 ASSERT(tx
->tx_txg
!= 0);
1310 ASSERT(!refcount_is_zero(&db
->db_holds
));
1312 if (RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
))
1313 rf
|= DB_RF_HAVESTRUCT
;
1314 (void) dbuf_read(db
, NULL
, rf
);
1315 (void) dbuf_dirty(db
, tx
);
1319 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1321 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1323 db
->db_state
= DB_NOFILL
;
1325 dmu_buf_will_fill(db_fake
, tx
);
1329 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1331 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1333 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1334 ASSERT(tx
->tx_txg
!= 0);
1335 ASSERT(db
->db_level
== 0);
1336 ASSERT(!refcount_is_zero(&db
->db_holds
));
1338 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1339 dmu_tx_private_ok(tx
));
1342 (void) dbuf_dirty(db
, tx
);
1345 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1348 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1350 mutex_enter(&db
->db_mtx
);
1353 if (db
->db_state
== DB_FILL
) {
1354 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1355 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1356 /* we were freed while filling */
1357 /* XXX dbuf_undirty? */
1358 bzero(db
->db
.db_data
, db
->db
.db_size
);
1359 db
->db_freed_in_flight
= FALSE
;
1361 db
->db_state
= DB_CACHED
;
1362 cv_broadcast(&db
->db_changed
);
1364 mutex_exit(&db
->db_mtx
);
1368 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1369 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1372 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1374 ASSERT(!refcount_is_zero(&db
->db_holds
));
1375 ASSERT(db
->db_dnode
->dn_object
!= DMU_META_DNODE_OBJECT
);
1376 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1377 ASSERT(db
->db_level
== 0);
1378 ASSERT(DBUF_GET_BUFC_TYPE(db
) == ARC_BUFC_DATA
);
1379 ASSERT(buf
!= NULL
);
1380 ASSERT(arc_buf_size(buf
) == db
->db
.db_size
);
1381 ASSERT(tx
->tx_txg
!= 0);
1383 arc_return_buf(buf
, db
);
1384 ASSERT(arc_released(buf
));
1386 mutex_enter(&db
->db_mtx
);
1388 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1389 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1391 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1393 if (db
->db_state
== DB_CACHED
&&
1394 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1395 mutex_exit(&db
->db_mtx
);
1396 (void) dbuf_dirty(db
, tx
);
1397 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1398 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1399 xuio_stat_wbuf_copied();
1403 xuio_stat_wbuf_nocopy();
1404 if (db
->db_state
== DB_CACHED
) {
1405 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1407 ASSERT(db
->db_buf
!= NULL
);
1408 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1409 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1410 if (!arc_released(db
->db_buf
)) {
1411 ASSERT(dr
->dt
.dl
.dr_override_state
==
1413 arc_release(db
->db_buf
, db
);
1415 dr
->dt
.dl
.dr_data
= buf
;
1416 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 1);
1417 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1418 arc_release(db
->db_buf
, db
);
1419 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 1);
1423 ASSERT(db
->db_buf
== NULL
);
1424 dbuf_set_data(db
, buf
);
1425 db
->db_state
= DB_FILL
;
1426 mutex_exit(&db
->db_mtx
);
1427 (void) dbuf_dirty(db
, tx
);
1428 dbuf_fill_done(db
, tx
);
1432 * "Clear" the contents of this dbuf. This will mark the dbuf
1433 * EVICTING and clear *most* of its references. Unfortunetely,
1434 * when we are not holding the dn_dbufs_mtx, we can't clear the
1435 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1436 * in this case. For callers from the DMU we will usually see:
1437 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1438 * For the arc callback, we will usually see:
1439 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1440 * Sometimes, though, we will get a mix of these two:
1441 * DMU: dbuf_clear()->arc_buf_evict()
1442 * ARC: dbuf_do_evict()->dbuf_destroy()
1445 dbuf_clear(dmu_buf_impl_t
*db
)
1447 dnode_t
*dn
= db
->db_dnode
;
1448 dmu_buf_impl_t
*parent
= db
->db_parent
;
1449 dmu_buf_impl_t
*dndb
= dn
->dn_dbuf
;
1450 int dbuf_gone
= FALSE
;
1452 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1453 ASSERT(refcount_is_zero(&db
->db_holds
));
1455 dbuf_evict_user(db
);
1457 if (db
->db_state
== DB_CACHED
) {
1458 ASSERT(db
->db
.db_data
!= NULL
);
1459 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1460 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1461 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1463 db
->db
.db_data
= NULL
;
1464 db
->db_state
= DB_UNCACHED
;
1467 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1468 ASSERT(db
->db_data_pending
== NULL
);
1470 db
->db_state
= DB_EVICTING
;
1471 db
->db_blkptr
= NULL
;
1473 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1474 list_remove(&dn
->dn_dbufs
, db
);
1476 db
->db_dnode
= NULL
;
1480 dbuf_gone
= arc_buf_evict(db
->db_buf
);
1483 mutex_exit(&db
->db_mtx
);
1486 * If this dbuf is referened from an indirect dbuf,
1487 * decrement the ref count on the indirect dbuf.
1489 if (parent
&& parent
!= dndb
)
1490 dbuf_rele(parent
, db
);
1494 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1495 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
1502 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1504 if (blkid
== DMU_SPILL_BLKID
) {
1505 mutex_enter(&dn
->dn_mtx
);
1506 if (dn
->dn_have_spill
&&
1507 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
1508 *bpp
= &dn
->dn_phys
->dn_spill
;
1511 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1512 *parentp
= dn
->dn_dbuf
;
1513 mutex_exit(&dn
->dn_mtx
);
1517 if (dn
->dn_phys
->dn_nlevels
== 0)
1520 nlevels
= dn
->dn_phys
->dn_nlevels
;
1522 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1524 ASSERT3U(level
* epbs
, <, 64);
1525 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1526 if (level
>= nlevels
||
1527 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1528 /* the buffer has no parent yet */
1530 } else if (level
< nlevels
-1) {
1531 /* this block is referenced from an indirect block */
1532 int err
= dbuf_hold_impl(dn
, level
+1,
1533 blkid
>> epbs
, fail_sparse
, NULL
, parentp
);
1536 err
= dbuf_read(*parentp
, NULL
,
1537 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1539 dbuf_rele(*parentp
, NULL
);
1543 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1544 (blkid
& ((1ULL << epbs
) - 1));
1547 /* the block is referenced from the dnode */
1548 ASSERT3U(level
, ==, nlevels
-1);
1549 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1550 blkid
< dn
->dn_phys
->dn_nblkptr
);
1552 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1553 *parentp
= dn
->dn_dbuf
;
1555 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1560 static dmu_buf_impl_t
*
1561 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1562 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1564 objset_t
*os
= dn
->dn_objset
;
1565 dmu_buf_impl_t
*db
, *odb
;
1567 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1568 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1570 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
1573 db
->db
.db_object
= dn
->dn_object
;
1574 db
->db_level
= level
;
1575 db
->db_blkid
= blkid
;
1576 db
->db_last_dirty
= NULL
;
1577 db
->db_dirtycnt
= 0;
1579 db
->db_parent
= parent
;
1580 db
->db_blkptr
= blkptr
;
1582 db
->db_user_ptr
= NULL
;
1583 db
->db_user_data_ptr_ptr
= NULL
;
1584 db
->db_evict_func
= NULL
;
1585 db
->db_immediate_evict
= 0;
1586 db
->db_freed_in_flight
= 0;
1588 if (blkid
== DMU_BONUS_BLKID
) {
1589 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1590 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1591 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1592 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1593 db
->db
.db_offset
= DMU_BONUS_BLKID
;
1594 db
->db_state
= DB_UNCACHED
;
1595 /* the bonus dbuf is not placed in the hash table */
1596 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1598 } else if (blkid
== DMU_SPILL_BLKID
) {
1599 db
->db
.db_size
= (blkptr
!= NULL
) ?
1600 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
1601 db
->db
.db_offset
= 0;
1604 db
->db_level
? 1<<dn
->dn_indblkshift
: dn
->dn_datablksz
;
1605 db
->db
.db_size
= blocksize
;
1606 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1610 * Hold the dn_dbufs_mtx while we get the new dbuf
1611 * in the hash table *and* added to the dbufs list.
1612 * This prevents a possible deadlock with someone
1613 * trying to look up this dbuf before its added to the
1616 mutex_enter(&dn
->dn_dbufs_mtx
);
1617 db
->db_state
= DB_EVICTING
;
1618 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1619 /* someone else inserted it first */
1620 kmem_cache_free(dbuf_cache
, db
);
1621 mutex_exit(&dn
->dn_dbufs_mtx
);
1624 list_insert_head(&dn
->dn_dbufs
, db
);
1625 db
->db_state
= DB_UNCACHED
;
1626 mutex_exit(&dn
->dn_dbufs_mtx
);
1627 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1629 if (parent
&& parent
!= dn
->dn_dbuf
)
1630 dbuf_add_ref(parent
, db
);
1632 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1633 refcount_count(&dn
->dn_holds
) > 0);
1634 (void) refcount_add(&dn
->dn_holds
, db
);
1636 dprintf_dbuf(db
, "db=%p\n", db
);
1642 dbuf_do_evict(void *private)
1644 arc_buf_t
*buf
= private;
1645 dmu_buf_impl_t
*db
= buf
->b_private
;
1647 if (!MUTEX_HELD(&db
->db_mtx
))
1648 mutex_enter(&db
->db_mtx
);
1650 ASSERT(refcount_is_zero(&db
->db_holds
));
1652 if (db
->db_state
!= DB_EVICTING
) {
1653 ASSERT(db
->db_state
== DB_CACHED
);
1658 mutex_exit(&db
->db_mtx
);
1665 dbuf_destroy(dmu_buf_impl_t
*db
)
1667 ASSERT(refcount_is_zero(&db
->db_holds
));
1669 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1671 * If this dbuf is still on the dn_dbufs list,
1672 * remove it from that list.
1675 dnode_t
*dn
= db
->db_dnode
;
1677 mutex_enter(&dn
->dn_dbufs_mtx
);
1678 list_remove(&dn
->dn_dbufs
, db
);
1679 mutex_exit(&dn
->dn_dbufs_mtx
);
1682 db
->db_dnode
= NULL
;
1684 dbuf_hash_remove(db
);
1686 db
->db_parent
= NULL
;
1689 ASSERT(!list_link_active(&db
->db_link
));
1690 ASSERT(db
->db
.db_data
== NULL
);
1691 ASSERT(db
->db_hash_next
== NULL
);
1692 ASSERT(db
->db_blkptr
== NULL
);
1693 ASSERT(db
->db_data_pending
== NULL
);
1695 kmem_cache_free(dbuf_cache
, db
);
1696 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
1700 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
)
1702 dmu_buf_impl_t
*db
= NULL
;
1703 blkptr_t
*bp
= NULL
;
1705 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1706 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1708 if (dnode_block_freed(dn
, blkid
))
1711 /* dbuf_find() returns with db_mtx held */
1712 if (db
= dbuf_find(dn
, 0, blkid
)) {
1713 if (refcount_count(&db
->db_holds
) > 0) {
1715 * This dbuf is active. We assume that it is
1716 * already CACHED, or else about to be either
1719 mutex_exit(&db
->db_mtx
);
1722 mutex_exit(&db
->db_mtx
);
1726 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
) == 0) {
1727 if (bp
&& !BP_IS_HOLE(bp
)) {
1728 int priority
= dn
->dn_type
== DMU_OT_DDT_ZAP
?
1729 ZIO_PRIORITY_DDT_PREFETCH
: ZIO_PRIORITY_ASYNC_READ
;
1731 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
1732 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1735 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
1736 dn
->dn_object
, 0, blkid
);
1741 pbuf
= dn
->dn_objset
->os_phys_buf
;
1743 (void) dsl_read(NULL
, dn
->dn_objset
->os_spa
,
1744 bp
, pbuf
, NULL
, NULL
, priority
,
1745 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1749 dbuf_rele(db
, NULL
);
1754 * Returns with db_holds incremented, and db_mtx not held.
1755 * Note: dn_struct_rwlock must be held.
1758 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
1759 void *tag
, dmu_buf_impl_t
**dbp
)
1761 dmu_buf_impl_t
*db
, *parent
= NULL
;
1763 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1764 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1765 ASSERT3U(dn
->dn_nlevels
, >, level
);
1769 /* dbuf_find() returns with db_mtx held */
1770 db
= dbuf_find(dn
, level
, blkid
);
1773 blkptr_t
*bp
= NULL
;
1776 ASSERT3P(parent
, ==, NULL
);
1777 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
1779 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
1783 dbuf_rele(parent
, NULL
);
1787 if (err
&& err
!= ENOENT
)
1789 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
1792 if (db
->db_buf
&& refcount_is_zero(&db
->db_holds
)) {
1793 arc_buf_add_ref(db
->db_buf
, db
);
1794 if (db
->db_buf
->b_data
== NULL
) {
1797 dbuf_rele(parent
, NULL
);
1802 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
1805 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
1808 * If this buffer is currently syncing out, and we are are
1809 * still referencing it from db_data, we need to make a copy
1810 * of it in case we decide we want to dirty it again in this txg.
1812 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1813 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
1814 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
1815 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
1817 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
1818 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1821 arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
,
1822 db
->db
.db_size
, db
, type
));
1823 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
1828 (void) refcount_add(&db
->db_holds
, tag
);
1829 dbuf_update_data(db
);
1831 mutex_exit(&db
->db_mtx
);
1833 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1835 dbuf_rele(parent
, NULL
);
1837 ASSERT3P(db
->db_dnode
, ==, dn
);
1838 ASSERT3U(db
->db_blkid
, ==, blkid
);
1839 ASSERT3U(db
->db_level
, ==, level
);
1846 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
1849 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
1850 return (err
? NULL
: db
);
1854 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
1857 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
1858 return (err
? NULL
: db
);
1862 dbuf_create_bonus(dnode_t
*dn
)
1864 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1866 ASSERT(dn
->dn_bonus
== NULL
);
1867 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
1871 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
1873 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1874 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1877 blksz
= SPA_MINBLOCKSIZE
;
1878 if (blksz
> SPA_MAXBLOCKSIZE
)
1879 blksz
= SPA_MAXBLOCKSIZE
;
1881 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
1883 rw_enter(&db
->db_dnode
->dn_struct_rwlock
, RW_WRITER
);
1884 dbuf_new_size(db
, blksz
, tx
);
1885 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
1891 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
1893 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
1896 #pragma weak dmu_buf_add_ref = dbuf_add_ref
1898 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
1900 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
1904 #pragma weak dmu_buf_rele = dbuf_rele
1906 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
1908 mutex_enter(&db
->db_mtx
);
1909 dbuf_rele_and_unlock(db
, tag
);
1913 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
1914 * db_dirtycnt and db_holds to be updated atomically.
1917 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
1921 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1924 holds
= refcount_remove(&db
->db_holds
, tag
);
1928 * We can't freeze indirects if there is a possibility that they
1929 * may be modified in the current syncing context.
1931 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
1932 arc_buf_freeze(db
->db_buf
);
1934 if (holds
== db
->db_dirtycnt
&&
1935 db
->db_level
== 0 && db
->db_immediate_evict
)
1936 dbuf_evict_user(db
);
1939 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1940 mutex_exit(&db
->db_mtx
);
1941 dnode_rele(db
->db_dnode
, db
);
1942 } else if (db
->db_buf
== NULL
) {
1944 * This is a special case: we never associated this
1945 * dbuf with any data allocated from the ARC.
1947 ASSERT(db
->db_state
== DB_UNCACHED
||
1948 db
->db_state
== DB_NOFILL
);
1950 } else if (arc_released(db
->db_buf
)) {
1951 arc_buf_t
*buf
= db
->db_buf
;
1953 * This dbuf has anonymous data associated with it.
1955 dbuf_set_data(db
, NULL
);
1956 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1959 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 0);
1960 if (!DBUF_IS_CACHEABLE(db
))
1963 mutex_exit(&db
->db_mtx
);
1966 mutex_exit(&db
->db_mtx
);
1970 #pragma weak dmu_buf_refcount = dbuf_refcount
1972 dbuf_refcount(dmu_buf_impl_t
*db
)
1974 return (refcount_count(&db
->db_holds
));
1978 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
1979 dmu_buf_evict_func_t
*evict_func
)
1981 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
1982 user_data_ptr_ptr
, evict_func
));
1986 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
1987 dmu_buf_evict_func_t
*evict_func
)
1989 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1991 db
->db_immediate_evict
= TRUE
;
1992 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
1993 user_data_ptr_ptr
, evict_func
));
1997 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
1998 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
2000 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2001 ASSERT(db
->db_level
== 0);
2003 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
2005 mutex_enter(&db
->db_mtx
);
2007 if (db
->db_user_ptr
== old_user_ptr
) {
2008 db
->db_user_ptr
= user_ptr
;
2009 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
2010 db
->db_evict_func
= evict_func
;
2012 dbuf_update_data(db
);
2014 old_user_ptr
= db
->db_user_ptr
;
2017 mutex_exit(&db
->db_mtx
);
2018 return (old_user_ptr
);
2022 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2024 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2025 ASSERT(!refcount_is_zero(&db
->db_holds
));
2027 return (db
->db_user_ptr
);
2031 dmu_buf_freeable(dmu_buf_t
*dbuf
)
2033 boolean_t res
= B_FALSE
;
2034 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2037 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
2038 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
2044 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2046 /* ASSERT(dmu_tx_is_syncing(tx) */
2047 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2049 if (db
->db_blkptr
!= NULL
)
2052 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2053 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2054 BP_ZERO(db
->db_blkptr
);
2057 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2059 * This buffer was allocated at a time when there was
2060 * no available blkptrs from the dnode, or it was
2061 * inappropriate to hook it in (i.e., nlevels mis-match).
2063 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2064 ASSERT(db
->db_parent
== NULL
);
2065 db
->db_parent
= dn
->dn_dbuf
;
2066 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2069 dmu_buf_impl_t
*parent
= db
->db_parent
;
2070 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2072 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2073 if (parent
== NULL
) {
2074 mutex_exit(&db
->db_mtx
);
2075 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2076 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
2077 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
2078 rw_exit(&dn
->dn_struct_rwlock
);
2079 mutex_enter(&db
->db_mtx
);
2080 db
->db_parent
= parent
;
2082 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2083 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2089 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2091 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2092 dnode_t
*dn
= db
->db_dnode
;
2095 ASSERT(dmu_tx_is_syncing(tx
));
2097 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2099 mutex_enter(&db
->db_mtx
);
2101 ASSERT(db
->db_level
> 0);
2104 if (db
->db_buf
== NULL
) {
2105 mutex_exit(&db
->db_mtx
);
2106 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2107 mutex_enter(&db
->db_mtx
);
2109 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2110 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2111 ASSERT(db
->db_buf
!= NULL
);
2113 dbuf_check_blkptr(dn
, db
);
2115 db
->db_data_pending
= dr
;
2117 mutex_exit(&db
->db_mtx
);
2118 dbuf_write(dr
, db
->db_buf
, tx
);
2121 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2122 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
2123 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2124 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2129 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2131 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
2132 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2133 dnode_t
*dn
= db
->db_dnode
;
2134 objset_t
*os
= dn
->dn_objset
;
2135 uint64_t txg
= tx
->tx_txg
;
2137 ASSERT(dmu_tx_is_syncing(tx
));
2139 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2141 mutex_enter(&db
->db_mtx
);
2143 * To be synced, we must be dirtied. But we
2144 * might have been freed after the dirty.
2146 if (db
->db_state
== DB_UNCACHED
) {
2147 /* This buffer has been freed since it was dirtied */
2148 ASSERT(db
->db
.db_data
== NULL
);
2149 } else if (db
->db_state
== DB_FILL
) {
2150 /* This buffer was freed and is now being re-filled */
2151 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
2153 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
2157 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2158 mutex_enter(&dn
->dn_mtx
);
2159 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
2160 mutex_exit(&dn
->dn_mtx
);
2164 * If this is a bonus buffer, simply copy the bonus data into the
2165 * dnode. It will be written out when the dnode is synced (and it
2166 * will be synced, since it must have been dirty for dbuf_sync to
2169 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2170 dbuf_dirty_record_t
**drp
;
2172 ASSERT(*datap
!= NULL
);
2173 ASSERT3U(db
->db_level
, ==, 0);
2174 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
2175 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
2176 if (*datap
!= db
->db
.db_data
) {
2177 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
2178 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2180 db
->db_data_pending
= NULL
;
2181 drp
= &db
->db_last_dirty
;
2183 drp
= &(*drp
)->dr_next
;
2184 ASSERT(dr
->dr_next
== NULL
);
2185 ASSERT(dr
->dr_dbuf
== db
);
2187 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2188 ASSERT(db
->db_dirtycnt
> 0);
2189 db
->db_dirtycnt
-= 1;
2190 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2195 * This function may have dropped the db_mtx lock allowing a dmu_sync
2196 * operation to sneak in. As a result, we need to ensure that we
2197 * don't check the dr_override_state until we have returned from
2198 * dbuf_check_blkptr.
2200 dbuf_check_blkptr(dn
, db
);
2203 * If this buffer is in the middle of an immdiate write,
2204 * wait for the synchronous IO to complete.
2206 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2207 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2208 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2209 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2212 if (db
->db_state
!= DB_NOFILL
&&
2213 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2214 refcount_count(&db
->db_holds
) > 1 &&
2215 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
2216 *datap
== db
->db_buf
) {
2218 * If this buffer is currently "in use" (i.e., there
2219 * are active holds and db_data still references it),
2220 * then make a copy before we start the write so that
2221 * any modifications from the open txg will not leak
2224 * NOTE: this copy does not need to be made for
2225 * objects only modified in the syncing context (e.g.
2226 * DNONE_DNODE blocks).
2228 int blksz
= arc_buf_size(*datap
);
2229 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2230 *datap
= arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2231 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2233 db
->db_data_pending
= dr
;
2235 mutex_exit(&db
->db_mtx
);
2237 dbuf_write(dr
, *datap
, tx
);
2239 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2240 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
)
2241 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2243 zio_nowait(dr
->dr_zio
);
2247 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2249 dbuf_dirty_record_t
*dr
;
2251 while (dr
= list_head(list
)) {
2252 if (dr
->dr_zio
!= NULL
) {
2254 * If we find an already initialized zio then we
2255 * are processing the meta-dnode, and we have finished.
2256 * The dbufs for all dnodes are put back on the list
2257 * during processing, so that we can zio_wait()
2258 * these IOs after initiating all child IOs.
2260 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2261 DMU_META_DNODE_OBJECT
);
2264 list_remove(list
, dr
);
2265 if (dr
->dr_dbuf
->db_level
> 0)
2266 dbuf_sync_indirect(dr
, tx
);
2268 dbuf_sync_leaf(dr
, tx
);
2274 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2276 dmu_buf_impl_t
*db
= vdb
;
2277 blkptr_t
*bp
= zio
->io_bp
;
2278 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2279 dnode_t
*dn
= db
->db_dnode
;
2280 spa_t
*spa
= zio
->io_spa
;
2285 ASSERT(db
->db_blkptr
== bp
);
2287 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
2288 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
2289 zio
->io_prev_space_delta
= delta
;
2291 if (BP_IS_HOLE(bp
)) {
2292 ASSERT(bp
->blk_fill
== 0);
2296 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
2297 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
2298 (db
->db_blkid
== DMU_SPILL_BLKID
&&
2299 BP_GET_TYPE(bp
) == dn
->dn_bonustype
));
2300 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2302 mutex_enter(&db
->db_mtx
);
2305 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2306 dnode_t
*dn
= db
->db_dnode
;
2307 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2308 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2309 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2313 if (db
->db_level
== 0) {
2314 mutex_enter(&dn
->dn_mtx
);
2315 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
2316 db
->db_blkid
!= DMU_SPILL_BLKID
)
2317 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2318 mutex_exit(&dn
->dn_mtx
);
2320 if (dn
->dn_type
== DMU_OT_DNODE
) {
2321 dnode_phys_t
*dnp
= db
->db
.db_data
;
2322 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2324 if (dnp
->dn_type
!= DMU_OT_NONE
)
2331 blkptr_t
*ibp
= db
->db
.db_data
;
2332 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2333 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2334 if (BP_IS_HOLE(ibp
))
2336 fill
+= ibp
->blk_fill
;
2340 bp
->blk_fill
= fill
;
2342 mutex_exit(&db
->db_mtx
);
2347 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2349 dmu_buf_impl_t
*db
= vdb
;
2350 blkptr_t
*bp
= zio
->io_bp
;
2351 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2352 dnode_t
*dn
= db
->db_dnode
;
2353 objset_t
*os
= dn
->dn_objset
;
2354 uint64_t txg
= zio
->io_txg
;
2355 dbuf_dirty_record_t
**drp
, *dr
;
2357 ASSERT3U(zio
->io_error
, ==, 0);
2358 ASSERT(db
->db_blkptr
== bp
);
2360 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
2361 ASSERT(BP_EQUAL(bp
, bp_orig
));
2363 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2364 dmu_tx_t
*tx
= os
->os_synctx
;
2366 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
2367 dsl_dataset_block_born(ds
, bp
, tx
);
2370 mutex_enter(&db
->db_mtx
);
2374 drp
= &db
->db_last_dirty
;
2375 while ((dr
= *drp
) != db
->db_data_pending
)
2377 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2378 ASSERT(dr
->dr_txg
== txg
);
2379 ASSERT(dr
->dr_dbuf
== db
);
2380 ASSERT(dr
->dr_next
== NULL
);
2384 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2385 dnode_t
*dn
= db
->db_dnode
;
2386 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
2387 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
2388 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
2392 if (db
->db_level
== 0) {
2393 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2394 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2395 if (db
->db_state
!= DB_NOFILL
) {
2396 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2397 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2399 else if (!arc_released(db
->db_buf
))
2400 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2403 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2404 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2405 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2407 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2408 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2410 ASSERT3U(dn
->dn_phys
->dn_maxblkid
2411 >> (db
->db_level
* epbs
), >=, db
->db_blkid
);
2412 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2414 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2415 list_destroy(&dr
->dt
.di
.dr_children
);
2417 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2419 cv_broadcast(&db
->db_changed
);
2420 ASSERT(db
->db_dirtycnt
> 0);
2421 db
->db_dirtycnt
-= 1;
2422 db
->db_data_pending
= NULL
;
2423 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
2427 dbuf_write_nofill_ready(zio_t
*zio
)
2429 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2433 dbuf_write_nofill_done(zio_t
*zio
)
2435 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2439 dbuf_write_override_ready(zio_t
*zio
)
2441 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2442 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2444 dbuf_write_ready(zio
, NULL
, db
);
2448 dbuf_write_override_done(zio_t
*zio
)
2450 dbuf_dirty_record_t
*dr
= zio
->io_private
;
2451 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2452 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
2454 mutex_enter(&db
->db_mtx
);
2455 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
2456 if (!BP_IS_HOLE(obp
))
2457 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
2458 arc_release(dr
->dt
.dl
.dr_data
, db
);
2460 mutex_exit(&db
->db_mtx
);
2462 dbuf_write_done(zio
, NULL
, db
);
2466 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2468 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2469 dnode_t
*dn
= db
->db_dnode
;
2470 objset_t
*os
= dn
->dn_objset
;
2471 dmu_buf_impl_t
*parent
= db
->db_parent
;
2472 uint64_t txg
= tx
->tx_txg
;
2478 if (db
->db_state
!= DB_NOFILL
) {
2479 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
2481 * Private object buffers are released here rather
2482 * than in dbuf_dirty() since they are only modified
2483 * in the syncing context and we don't want the
2484 * overhead of making multiple copies of the data.
2486 if (BP_IS_HOLE(db
->db_blkptr
)) {
2489 dbuf_release_bp(db
);
2494 if (parent
!= dn
->dn_dbuf
) {
2495 ASSERT(parent
&& parent
->db_data_pending
);
2496 ASSERT(db
->db_level
== parent
->db_level
-1);
2497 ASSERT(arc_released(parent
->db_buf
));
2498 zio
= parent
->db_data_pending
->dr_zio
;
2500 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
2501 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
2502 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
2503 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2504 ASSERT3P(db
->db_blkptr
, ==,
2505 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2509 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2510 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2513 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
2514 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
2515 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2517 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2519 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
2521 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
2523 if (db
->db_level
== 0 && dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2524 ASSERT(db
->db_state
!= DB_NOFILL
);
2525 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2526 db
->db_blkptr
, data
->b_data
, arc_buf_size(data
), &zp
,
2527 dbuf_write_override_ready
, dbuf_write_override_done
, dr
,
2528 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2529 mutex_enter(&db
->db_mtx
);
2530 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2531 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
2532 dr
->dt
.dl
.dr_copies
);
2533 mutex_exit(&db
->db_mtx
);
2534 } else if (db
->db_state
== DB_NOFILL
) {
2535 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
2536 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
2537 db
->db_blkptr
, NULL
, db
->db
.db_size
, &zp
,
2538 dbuf_write_nofill_ready
, dbuf_write_nofill_done
, db
,
2539 ZIO_PRIORITY_ASYNC_WRITE
,
2540 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
2542 ASSERT(arc_released(data
));
2543 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
2544 db
->db_blkptr
, data
, DBUF_IS_L2CACHEABLE(db
), &zp
,
2545 dbuf_write_ready
, dbuf_write_done
, db
,
2546 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);