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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
28 #include <sys/dmu_impl.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dmu_tx.h>
36 #include <sys/dmu_zfetch.h>
38 static void dbuf_destroy(dmu_buf_impl_t
*db
);
39 static int dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
40 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
41 static arc_done_func_t dbuf_write_ready
;
42 static arc_done_func_t dbuf_write_done
;
43 static zio_done_func_t dbuf_skip_write_ready
;
44 static zio_done_func_t dbuf_skip_write_done
;
47 * Global data structures and functions for the dbuf cache.
49 static kmem_cache_t
*dbuf_cache
;
53 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
55 dmu_buf_impl_t
*db
= unused
;
56 bzero(db
, sizeof (dmu_buf_impl_t
));
58 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
59 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
60 refcount_create(&db
->db_holds
);
66 dbuf_dest(void *vdb
, void *unused
)
68 dmu_buf_impl_t
*db
= unused
;
69 mutex_destroy(&db
->db_mtx
);
70 cv_destroy(&db
->db_changed
);
71 refcount_destroy(&db
->db_holds
);
75 * dbuf hash table routines
77 static dbuf_hash_table_t dbuf_hash_table
;
79 static uint64_t dbuf_hash_count
;
82 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
84 uintptr_t osv
= (uintptr_t)os
;
87 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
88 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
89 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
90 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
91 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
92 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
93 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
95 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
100 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
102 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
103 ((dbuf)->db.db_object == (obj) && \
104 (dbuf)->db_objset == (os) && \
105 (dbuf)->db_level == (level) && \
106 (dbuf)->db_blkid == (blkid))
109 dbuf_find(dnode_t
*dn
, uint8_t level
, uint64_t blkid
)
111 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
112 objset_impl_t
*os
= dn
->dn_objset
;
113 uint64_t obj
= dn
->dn_object
;
114 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
115 uint64_t idx
= hv
& h
->hash_table_mask
;
118 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
119 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
120 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
121 mutex_enter(&db
->db_mtx
);
122 if (db
->db_state
!= DB_EVICTING
) {
123 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
126 mutex_exit(&db
->db_mtx
);
129 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
134 * Insert an entry into the hash table. If there is already an element
135 * equal to elem in the hash table, then the already existing element
136 * will be returned and the new element will not be inserted.
137 * Otherwise returns NULL.
139 static dmu_buf_impl_t
*
140 dbuf_hash_insert(dmu_buf_impl_t
*db
)
142 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
143 objset_impl_t
*os
= db
->db_objset
;
144 uint64_t obj
= db
->db
.db_object
;
145 int level
= db
->db_level
;
146 uint64_t blkid
= db
->db_blkid
;
147 uint64_t hv
= DBUF_HASH(os
, obj
, level
, blkid
);
148 uint64_t idx
= hv
& h
->hash_table_mask
;
151 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
152 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
153 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
154 mutex_enter(&dbf
->db_mtx
);
155 if (dbf
->db_state
!= DB_EVICTING
) {
156 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
159 mutex_exit(&dbf
->db_mtx
);
163 mutex_enter(&db
->db_mtx
);
164 db
->db_hash_next
= h
->hash_table
[idx
];
165 h
->hash_table
[idx
] = db
;
166 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
167 atomic_add_64(&dbuf_hash_count
, 1);
173 * Remove an entry from the hash table. This operation will
174 * fail if there are any existing holds on the db.
177 dbuf_hash_remove(dmu_buf_impl_t
*db
)
179 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
180 uint64_t hv
= DBUF_HASH(db
->db_objset
, db
->db
.db_object
,
181 db
->db_level
, db
->db_blkid
);
182 uint64_t idx
= hv
& h
->hash_table_mask
;
183 dmu_buf_impl_t
*dbf
, **dbp
;
186 * We musn't hold db_mtx to maintin lock ordering:
187 * DBUF_HASH_MUTEX > db_mtx.
189 ASSERT(refcount_is_zero(&db
->db_holds
));
190 ASSERT(db
->db_state
== DB_EVICTING
);
191 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
193 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
194 dbp
= &h
->hash_table
[idx
];
195 while ((dbf
= *dbp
) != db
) {
196 dbp
= &dbf
->db_hash_next
;
199 *dbp
= db
->db_hash_next
;
200 db
->db_hash_next
= NULL
;
201 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
202 atomic_add_64(&dbuf_hash_count
, -1);
205 static arc_evict_func_t dbuf_do_evict
;
208 dbuf_evict_user(dmu_buf_impl_t
*db
)
210 ASSERT(MUTEX_HELD(&db
->db_mtx
));
212 if (db
->db_level
!= 0 || db
->db_evict_func
== NULL
)
215 if (db
->db_user_data_ptr_ptr
)
216 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
217 db
->db_evict_func(&db
->db
, db
->db_user_ptr
);
218 db
->db_user_ptr
= NULL
;
219 db
->db_user_data_ptr_ptr
= NULL
;
220 db
->db_evict_func
= NULL
;
224 dbuf_evict(dmu_buf_impl_t
*db
)
226 ASSERT(MUTEX_HELD(&db
->db_mtx
));
227 ASSERT(db
->db_buf
== NULL
);
228 ASSERT(db
->db_data_pending
== NULL
);
237 uint64_t hsize
= 1ULL << 16;
238 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
242 * The hash table is big enough to fill all of physical memory
243 * with an average 4K block size. The table will take up
244 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
246 while (hsize
* 4096 < (uint64_t)physmem
* PAGESIZE
)
250 h
->hash_table_mask
= hsize
- 1;
251 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
252 if (h
->hash_table
== NULL
) {
253 /* XXX - we should really return an error instead of assert */
254 ASSERT(hsize
> (1ULL << 10));
259 dbuf_cache
= kmem_cache_create("dmu_buf_impl_t",
260 sizeof (dmu_buf_impl_t
),
261 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
263 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
264 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
270 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
273 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
274 mutex_destroy(&h
->hash_mutexes
[i
]);
275 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
276 kmem_cache_destroy(dbuf_cache
);
285 dbuf_verify(dmu_buf_impl_t
*db
)
287 dnode_t
*dn
= db
->db_dnode
;
289 ASSERT(MUTEX_HELD(&db
->db_mtx
));
291 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
294 ASSERT(db
->db_objset
!= NULL
);
296 ASSERT(db
->db_parent
== NULL
);
297 ASSERT(db
->db_blkptr
== NULL
);
299 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
300 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
301 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
302 ASSERT(db
->db_blkid
== DB_BONUS_BLKID
||
303 list_head(&dn
->dn_dbufs
));
305 if (db
->db_blkid
== DB_BONUS_BLKID
) {
307 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
308 ASSERT3U(db
->db
.db_offset
, ==, DB_BONUS_BLKID
);
310 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
314 * We can't assert that db_size matches dn_datablksz because it
315 * can be momentarily different when another thread is doing
318 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
319 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
321 * It should only be modified in syncing context, so
322 * make sure we only have one copy of the data.
324 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
327 /* verify db->db_blkptr */
329 if (db
->db_parent
== dn
->dn_dbuf
) {
330 /* db is pointed to by the dnode */
331 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
332 if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
)
333 ASSERT(db
->db_parent
== NULL
);
335 ASSERT(db
->db_parent
!= NULL
);
336 ASSERT3P(db
->db_blkptr
, ==,
337 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
339 /* db is pointed to by an indirect block */
340 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
341 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
342 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
345 * dnode_grow_indblksz() can make this fail if we don't
346 * have the struct_rwlock. XXX indblksz no longer
347 * grows. safe to do this now?
349 if (RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
)) {
350 ASSERT3P(db
->db_blkptr
, ==,
351 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
352 db
->db_blkid
% epb
));
356 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
357 db
->db
.db_data
&& db
->db_blkid
!= DB_BONUS_BLKID
&&
358 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
360 * If the blkptr isn't set but they have nonzero data,
361 * it had better be dirty, otherwise we'll lose that
362 * data when we evict this buffer.
364 if (db
->db_dirtycnt
== 0) {
365 uint64_t *buf
= db
->db
.db_data
;
368 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
377 dbuf_update_data(dmu_buf_impl_t
*db
)
379 ASSERT(MUTEX_HELD(&db
->db_mtx
));
380 if (db
->db_level
== 0 && db
->db_user_data_ptr_ptr
) {
381 ASSERT(!refcount_is_zero(&db
->db_holds
));
382 *db
->db_user_data_ptr_ptr
= db
->db
.db_data
;
387 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
389 ASSERT(MUTEX_HELD(&db
->db_mtx
));
390 ASSERT(db
->db_buf
== NULL
|| !arc_has_callback(db
->db_buf
));
393 ASSERT(buf
->b_data
!= NULL
);
394 db
->db
.db_data
= buf
->b_data
;
395 if (!arc_released(buf
))
396 arc_set_callback(buf
, dbuf_do_evict
, db
);
397 dbuf_update_data(db
);
400 db
->db
.db_data
= NULL
;
401 if (db
->db_state
!= DB_NOFILL
)
402 db
->db_state
= DB_UNCACHED
;
407 dbuf_whichblock(dnode_t
*dn
, uint64_t offset
)
409 if (dn
->dn_datablkshift
) {
410 return (offset
>> dn
->dn_datablkshift
);
412 ASSERT3U(offset
, <, dn
->dn_datablksz
);
418 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
420 dmu_buf_impl_t
*db
= vdb
;
422 mutex_enter(&db
->db_mtx
);
423 ASSERT3U(db
->db_state
, ==, DB_READ
);
425 * All reads are synchronous, so we must have a hold on the dbuf
427 ASSERT(refcount_count(&db
->db_holds
) > 0);
428 ASSERT(db
->db_buf
== NULL
);
429 ASSERT(db
->db
.db_data
== NULL
);
430 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
431 /* we were freed in flight; disregard any error */
432 arc_release(buf
, db
);
433 bzero(buf
->b_data
, db
->db
.db_size
);
435 db
->db_freed_in_flight
= FALSE
;
436 dbuf_set_data(db
, buf
);
437 db
->db_state
= DB_CACHED
;
438 } else if (zio
== NULL
|| zio
->io_error
== 0) {
439 dbuf_set_data(db
, buf
);
440 db
->db_state
= DB_CACHED
;
442 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
443 ASSERT3P(db
->db_buf
, ==, NULL
);
444 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
445 db
->db_state
= DB_UNCACHED
;
447 cv_broadcast(&db
->db_changed
);
448 mutex_exit(&db
->db_mtx
);
453 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t *flags
)
455 dnode_t
*dn
= db
->db_dnode
;
457 uint32_t aflags
= ARC_NOWAIT
;
460 ASSERT(!refcount_is_zero(&db
->db_holds
));
461 /* We need the struct_rwlock to prevent db_blkptr from changing. */
462 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
463 ASSERT(MUTEX_HELD(&db
->db_mtx
));
464 ASSERT(db
->db_state
== DB_UNCACHED
);
465 ASSERT(db
->db_buf
== NULL
);
467 if (db
->db_blkid
== DB_BONUS_BLKID
) {
468 int bonuslen
= dn
->dn_bonuslen
;
470 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
471 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
472 arc_space_consume(DN_MAX_BONUSLEN
);
473 if (bonuslen
< DN_MAX_BONUSLEN
)
474 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
475 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
,
477 dbuf_update_data(db
);
478 db
->db_state
= DB_CACHED
;
479 mutex_exit(&db
->db_mtx
);
484 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
485 * processes the delete record and clears the bp while we are waiting
486 * for the dn_mtx (resulting in a "no" from block_freed).
488 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
489 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
490 BP_IS_HOLE(db
->db_blkptr
)))) {
491 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
493 dbuf_set_data(db
, arc_buf_alloc(dn
->dn_objset
->os_spa
,
494 db
->db
.db_size
, db
, type
));
495 bzero(db
->db
.db_data
, db
->db
.db_size
);
496 db
->db_state
= DB_CACHED
;
497 *flags
|= DB_RF_CACHED
;
498 mutex_exit(&db
->db_mtx
);
502 db
->db_state
= DB_READ
;
503 mutex_exit(&db
->db_mtx
);
505 if (DBUF_IS_L2CACHEABLE(db
))
506 aflags
|= ARC_L2CACHE
;
508 zb
.zb_objset
= db
->db_objset
->os_dsl_dataset
?
509 db
->db_objset
->os_dsl_dataset
->ds_object
: 0;
510 zb
.zb_object
= db
->db
.db_object
;
511 zb
.zb_level
= db
->db_level
;
512 zb
.zb_blkid
= db
->db_blkid
;
514 dbuf_add_ref(db
, NULL
);
515 /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
518 pbuf
= db
->db_parent
->db_buf
;
520 pbuf
= db
->db_objset
->os_phys_buf
;
522 (void) arc_read(zio
, dn
->dn_objset
->os_spa
, db
->db_blkptr
, pbuf
,
523 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
524 (*flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
526 if (aflags
& ARC_CACHED
)
527 *flags
|= DB_RF_CACHED
;
531 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
534 int havepzio
= (zio
!= NULL
);
538 * We don't have to hold the mutex to check db_state because it
539 * can't be freed while we have a hold on the buffer.
541 ASSERT(!refcount_is_zero(&db
->db_holds
));
543 if (db
->db_state
== DB_NOFILL
)
546 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
547 rw_enter(&db
->db_dnode
->dn_struct_rwlock
, RW_READER
);
549 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DB_BONUS_BLKID
&&
550 (flags
& DB_RF_NOPREFETCH
) == 0 && db
->db_dnode
!= NULL
&&
551 DBUF_IS_CACHEABLE(db
);
553 mutex_enter(&db
->db_mtx
);
554 if (db
->db_state
== DB_CACHED
) {
555 mutex_exit(&db
->db_mtx
);
557 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
558 db
->db
.db_size
, TRUE
);
559 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
560 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
561 } else if (db
->db_state
== DB_UNCACHED
) {
563 zio
= zio_root(db
->db_dnode
->dn_objset
->os_spa
,
564 NULL
, NULL
, ZIO_FLAG_CANFAIL
);
566 dbuf_read_impl(db
, zio
, &flags
);
568 /* dbuf_read_impl has dropped db_mtx for us */
571 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
572 db
->db
.db_size
, flags
& DB_RF_CACHED
);
574 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
575 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
580 mutex_exit(&db
->db_mtx
);
582 dmu_zfetch(&db
->db_dnode
->dn_zfetch
, db
->db
.db_offset
,
583 db
->db
.db_size
, TRUE
);
584 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
585 rw_exit(&db
->db_dnode
->dn_struct_rwlock
);
587 mutex_enter(&db
->db_mtx
);
588 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
589 while (db
->db_state
== DB_READ
||
590 db
->db_state
== DB_FILL
) {
591 ASSERT(db
->db_state
== DB_READ
||
592 (flags
& DB_RF_HAVESTRUCT
) == 0);
593 cv_wait(&db
->db_changed
, &db
->db_mtx
);
595 if (db
->db_state
== DB_UNCACHED
)
598 mutex_exit(&db
->db_mtx
);
601 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
606 dbuf_noread(dmu_buf_impl_t
*db
)
608 ASSERT(!refcount_is_zero(&db
->db_holds
));
609 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
610 mutex_enter(&db
->db_mtx
);
611 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
612 cv_wait(&db
->db_changed
, &db
->db_mtx
);
613 if (db
->db_state
== DB_UNCACHED
) {
614 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
616 ASSERT(db
->db_buf
== NULL
);
617 ASSERT(db
->db
.db_data
== NULL
);
618 dbuf_set_data(db
, arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
,
619 db
->db
.db_size
, db
, type
));
620 db
->db_state
= DB_FILL
;
621 } else if (db
->db_state
== DB_NOFILL
) {
622 dbuf_set_data(db
, NULL
);
624 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
626 mutex_exit(&db
->db_mtx
);
630 * This is our just-in-time copy function. It makes a copy of
631 * buffers, that have been modified in a previous transaction
632 * group, before we modify them in the current active group.
634 * This function is used in two places: when we are dirtying a
635 * buffer for the first time in a txg, and when we are freeing
636 * a range in a dnode that includes this buffer.
638 * Note that when we are called from dbuf_free_range() we do
639 * not put a hold on the buffer, we just traverse the active
640 * dbuf list for the dnode.
643 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
645 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
647 ASSERT(MUTEX_HELD(&db
->db_mtx
));
648 ASSERT(db
->db
.db_data
!= NULL
);
649 ASSERT(db
->db_level
== 0);
650 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
653 (dr
->dt
.dl
.dr_data
!=
654 ((db
->db_blkid
== DB_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
658 * If the last dirty record for this dbuf has not yet synced
659 * and its referencing the dbuf data, either:
660 * reset the reference to point to a new copy,
661 * or (if there a no active holders)
662 * just null out the current db_data pointer.
664 ASSERT(dr
->dr_txg
>= txg
- 2);
665 if (db
->db_blkid
== DB_BONUS_BLKID
) {
666 /* Note that the data bufs here are zio_bufs */
667 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
668 arc_space_consume(DN_MAX_BONUSLEN
);
669 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
670 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
671 int size
= db
->db
.db_size
;
672 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
673 dr
->dt
.dl
.dr_data
= arc_buf_alloc(
674 db
->db_dnode
->dn_objset
->os_spa
, size
, db
, type
);
675 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
677 dbuf_set_data(db
, NULL
);
682 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
684 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
685 uint64_t txg
= dr
->dr_txg
;
687 ASSERT(MUTEX_HELD(&db
->db_mtx
));
688 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
689 ASSERT(db
->db_level
== 0);
691 if (db
->db_blkid
== DB_BONUS_BLKID
||
692 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
695 /* free this block */
696 if (!BP_IS_HOLE(&dr
->dt
.dl
.dr_overridden_by
)) {
697 /* XXX can get silent EIO here */
698 (void) dsl_free(NULL
,
699 spa_get_dsl(db
->db_dnode
->dn_objset
->os_spa
),
700 txg
, &dr
->dt
.dl
.dr_overridden_by
, NULL
, NULL
, ARC_WAIT
);
702 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
704 * Release the already-written buffer, so we leave it in
705 * a consistent dirty state. Note that all callers are
706 * modifying the buffer, so they will immediately do
707 * another (redundant) arc_release(). Therefore, leave
708 * the buf thawed to save the effort of freezing &
709 * immediately re-thawing it.
711 arc_release(dr
->dt
.dl
.dr_data
, db
);
715 * Evict (if its unreferenced) or clear (if its referenced) any level-0
716 * data blocks in the free range, so that any future readers will find
717 * empty blocks. Also, if we happen accross any level-1 dbufs in the
718 * range that have not already been marked dirty, mark them dirty so
719 * they stay in memory.
722 dbuf_free_range(dnode_t
*dn
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
724 dmu_buf_impl_t
*db
, *db_next
;
725 uint64_t txg
= tx
->tx_txg
;
726 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
727 uint64_t first_l1
= start
>> epbs
;
728 uint64_t last_l1
= end
>> epbs
;
730 if (end
> dn
->dn_maxblkid
) {
731 end
= dn
->dn_maxblkid
;
732 last_l1
= end
>> epbs
;
734 dprintf_dnode(dn
, "start=%llu end=%llu\n", start
, end
);
735 mutex_enter(&dn
->dn_dbufs_mtx
);
736 for (db
= list_head(&dn
->dn_dbufs
); db
; db
= db_next
) {
737 db_next
= list_next(&dn
->dn_dbufs
, db
);
738 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
740 if (db
->db_level
== 1 &&
741 db
->db_blkid
>= first_l1
&& db
->db_blkid
<= last_l1
) {
742 mutex_enter(&db
->db_mtx
);
743 if (db
->db_last_dirty
&&
744 db
->db_last_dirty
->dr_txg
< txg
) {
745 dbuf_add_ref(db
, FTAG
);
746 mutex_exit(&db
->db_mtx
);
747 dbuf_will_dirty(db
, tx
);
750 mutex_exit(&db
->db_mtx
);
754 if (db
->db_level
!= 0)
756 dprintf_dbuf(db
, "found buf %s\n", "");
757 if (db
->db_blkid
< start
|| db
->db_blkid
> end
)
760 /* found a level 0 buffer in the range */
761 if (dbuf_undirty(db
, tx
))
764 mutex_enter(&db
->db_mtx
);
765 if (db
->db_state
== DB_UNCACHED
||
766 db
->db_state
== DB_NOFILL
||
767 db
->db_state
== DB_EVICTING
) {
768 ASSERT(db
->db
.db_data
== NULL
);
769 mutex_exit(&db
->db_mtx
);
772 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
773 /* will be handled in dbuf_read_done or dbuf_rele */
774 db
->db_freed_in_flight
= TRUE
;
775 mutex_exit(&db
->db_mtx
);
778 if (refcount_count(&db
->db_holds
) == 0) {
783 /* The dbuf is referenced */
785 if (db
->db_last_dirty
!= NULL
) {
786 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
788 if (dr
->dr_txg
== txg
) {
790 * This buffer is "in-use", re-adjust the file
791 * size to reflect that this buffer may
792 * contain new data when we sync.
794 if (db
->db_blkid
> dn
->dn_maxblkid
)
795 dn
->dn_maxblkid
= db
->db_blkid
;
799 * This dbuf is not dirty in the open context.
800 * Either uncache it (if its not referenced in
801 * the open context) or reset its contents to
804 dbuf_fix_old_data(db
, txg
);
807 /* clear the contents if its cached */
808 if (db
->db_state
== DB_CACHED
) {
809 ASSERT(db
->db
.db_data
!= NULL
);
810 arc_release(db
->db_buf
, db
);
811 bzero(db
->db
.db_data
, db
->db
.db_size
);
812 arc_buf_freeze(db
->db_buf
);
815 mutex_exit(&db
->db_mtx
);
817 mutex_exit(&dn
->dn_dbufs_mtx
);
821 dbuf_block_freeable(dmu_buf_impl_t
*db
)
823 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
824 uint64_t birth_txg
= 0;
827 * We don't need any locking to protect db_blkptr:
828 * If it's syncing, then db_last_dirty will be set
829 * so we'll ignore db_blkptr.
831 ASSERT(MUTEX_HELD(&db
->db_mtx
));
832 if (db
->db_last_dirty
)
833 birth_txg
= db
->db_last_dirty
->dr_txg
;
834 else if (db
->db_blkptr
)
835 birth_txg
= db
->db_blkptr
->blk_birth
;
837 /* If we don't exist or are in a snapshot, we can't be freed */
839 return (ds
== NULL
||
840 dsl_dataset_block_freeable(ds
, birth_txg
));
846 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
848 arc_buf_t
*buf
, *obuf
;
849 int osize
= db
->db
.db_size
;
850 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
852 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
854 /* XXX does *this* func really need the lock? */
855 ASSERT(RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
));
858 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
859 * is OK, because there can be no other references to the db
860 * when we are changing its size, so no concurrent DB_FILL can
864 * XXX we should be doing a dbuf_read, checking the return
865 * value and returning that up to our callers
867 dbuf_will_dirty(db
, tx
);
869 /* create the data buffer for the new block */
870 buf
= arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
, size
, db
, type
);
872 /* copy old block data to the new block */
874 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
875 /* zero the remainder */
877 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
879 mutex_enter(&db
->db_mtx
);
880 dbuf_set_data(db
, buf
);
881 VERIFY(arc_buf_remove_ref(obuf
, db
) == 1);
882 db
->db
.db_size
= size
;
884 if (db
->db_level
== 0) {
885 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
886 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
888 mutex_exit(&db
->db_mtx
);
890 dnode_willuse_space(db
->db_dnode
, size
-osize
, tx
);
893 dbuf_dirty_record_t
*
894 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
896 dnode_t
*dn
= db
->db_dnode
;
897 objset_impl_t
*os
= dn
->dn_objset
;
898 dbuf_dirty_record_t
**drp
, *dr
;
899 int drop_struct_lock
= FALSE
;
900 boolean_t do_free_accounting
= B_FALSE
;
901 int txgoff
= tx
->tx_txg
& TXG_MASK
;
903 ASSERT(tx
->tx_txg
!= 0);
904 ASSERT(!refcount_is_zero(&db
->db_holds
));
905 DMU_TX_DIRTY_BUF(tx
, db
);
908 * Shouldn't dirty a regular buffer in syncing context. Private
909 * objects may be dirtied in syncing context, but only if they
910 * were already pre-dirtied in open context.
911 * XXX We may want to prohibit dirtying in syncing context even
912 * if they did pre-dirty.
914 ASSERT(!dmu_tx_is_syncing(tx
) ||
915 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
916 dn
->dn_object
== DMU_META_DNODE_OBJECT
||
917 dn
->dn_objset
->os_dsl_dataset
== NULL
||
918 dsl_dir_is_private(dn
->dn_objset
->os_dsl_dataset
->ds_dir
));
921 * We make this assert for private objects as well, but after we
922 * check if we're already dirty. They are allowed to re-dirty
923 * in syncing context.
925 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
926 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
927 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
929 mutex_enter(&db
->db_mtx
);
931 * XXX make this true for indirects too? The problem is that
932 * transactions created with dmu_tx_create_assigned() from
933 * syncing context don't bother holding ahead.
935 ASSERT(db
->db_level
!= 0 ||
936 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
937 db
->db_state
== DB_NOFILL
);
939 mutex_enter(&dn
->dn_mtx
);
941 * Don't set dirtyctx to SYNC if we're just modifying this as we
942 * initialize the objset.
944 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
945 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
947 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
948 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
949 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
951 mutex_exit(&dn
->dn_mtx
);
954 * If this buffer is already dirty, we're done.
956 drp
= &db
->db_last_dirty
;
957 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
958 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
959 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
961 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
962 if (db
->db_level
== 0 && db
->db_blkid
!= DB_BONUS_BLKID
) {
964 * If this buffer has already been written out,
965 * we now need to reset its state.
968 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
)
969 arc_buf_thaw(db
->db_buf
);
971 mutex_exit(&db
->db_mtx
);
976 * Only valid if not already dirty.
978 ASSERT(dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
979 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
981 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
982 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
983 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
984 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
985 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
986 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
989 * We should only be dirtying in syncing context if it's the
990 * mos, a spa os, or we're initializing the os. However, we are
991 * allowed to dirty in syncing context provided we already
992 * dirtied it in open context. Hence we must make this
993 * assertion only if we're not already dirty.
995 ASSERT(!dmu_tx_is_syncing(tx
) ||
996 os
->os_dsl_dataset
== NULL
||
997 !dsl_dir_is_private(os
->os_dsl_dataset
->ds_dir
) ||
998 !BP_IS_HOLE(os
->os_rootbp
));
999 ASSERT(db
->db
.db_size
!= 0);
1001 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1003 if (db
->db_blkid
!= DB_BONUS_BLKID
) {
1005 * Update the accounting.
1006 * Note: we delay "free accounting" until after we drop
1007 * the db_mtx. This keeps us from grabbing other locks
1008 * (and possibly deadlocking) in bp_get_dasize() while
1009 * also holding the db_mtx.
1011 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1012 do_free_accounting
= dbuf_block_freeable(db
);
1016 * If this buffer is dirty in an old transaction group we need
1017 * to make a copy of it so that the changes we make in this
1018 * transaction group won't leak out when we sync the older txg.
1020 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1021 if (db
->db_level
== 0) {
1022 void *data_old
= db
->db_buf
;
1024 if (db
->db_state
!= DB_NOFILL
) {
1025 if (db
->db_blkid
== DB_BONUS_BLKID
) {
1026 dbuf_fix_old_data(db
, tx
->tx_txg
);
1027 data_old
= db
->db
.db_data
;
1028 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1030 * Release the data buffer from the cache so
1031 * that we can modify it without impacting
1032 * possible other users of this cached data
1033 * block. Note that indirect blocks and
1034 * private objects are not released until the
1035 * syncing state (since they are only modified
1038 arc_release(db
->db_buf
, db
);
1039 dbuf_fix_old_data(db
, tx
->tx_txg
);
1040 data_old
= db
->db_buf
;
1042 ASSERT(data_old
!= NULL
);
1044 dr
->dt
.dl
.dr_data
= data_old
;
1046 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1047 list_create(&dr
->dt
.di
.dr_children
,
1048 sizeof (dbuf_dirty_record_t
),
1049 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1052 dr
->dr_txg
= tx
->tx_txg
;
1057 * We could have been freed_in_flight between the dbuf_noread
1058 * and dbuf_dirty. We win, as though the dbuf_noread() had
1059 * happened after the free.
1061 if (db
->db_level
== 0 && db
->db_blkid
!= DB_BONUS_BLKID
) {
1062 mutex_enter(&dn
->dn_mtx
);
1063 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1064 mutex_exit(&dn
->dn_mtx
);
1065 db
->db_freed_in_flight
= FALSE
;
1069 * This buffer is now part of this txg
1071 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1072 db
->db_dirtycnt
+= 1;
1073 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1075 mutex_exit(&db
->db_mtx
);
1077 if (db
->db_blkid
== DB_BONUS_BLKID
) {
1078 mutex_enter(&dn
->dn_mtx
);
1079 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1080 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1081 mutex_exit(&dn
->dn_mtx
);
1082 dnode_setdirty(dn
, tx
);
1084 } else if (do_free_accounting
) {
1085 blkptr_t
*bp
= db
->db_blkptr
;
1086 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1087 bp_get_dasize(os
->os_spa
, bp
) : db
->db
.db_size
;
1089 * This is only a guess -- if the dbuf is dirty
1090 * in a previous txg, we don't know how much
1091 * space it will use on disk yet. We should
1092 * really have the struct_rwlock to access
1093 * db_blkptr, but since this is just a guess,
1094 * it's OK if we get an odd answer.
1096 dnode_willuse_space(dn
, -willfree
, tx
);
1099 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1100 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1101 drop_struct_lock
= TRUE
;
1104 if (db
->db_level
== 0) {
1105 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1106 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1109 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1110 dmu_buf_impl_t
*parent
= db
->db_parent
;
1111 dbuf_dirty_record_t
*di
;
1112 int parent_held
= FALSE
;
1114 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1115 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1117 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1118 db
->db_blkid
>> epbs
, FTAG
);
1121 if (drop_struct_lock
)
1122 rw_exit(&dn
->dn_struct_rwlock
);
1123 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1124 di
= dbuf_dirty(parent
, tx
);
1126 dbuf_rele(parent
, FTAG
);
1128 mutex_enter(&db
->db_mtx
);
1129 /* possible race with dbuf_undirty() */
1130 if (db
->db_last_dirty
== dr
||
1131 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1132 mutex_enter(&di
->dt
.di
.dr_mtx
);
1133 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1134 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1135 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1136 mutex_exit(&di
->dt
.di
.dr_mtx
);
1139 mutex_exit(&db
->db_mtx
);
1141 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1142 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1143 ASSERT(db
->db_parent
== NULL
||
1144 db
->db_parent
== db
->db_dnode
->dn_dbuf
);
1145 mutex_enter(&dn
->dn_mtx
);
1146 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1147 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1148 mutex_exit(&dn
->dn_mtx
);
1149 if (drop_struct_lock
)
1150 rw_exit(&dn
->dn_struct_rwlock
);
1153 dnode_setdirty(dn
, tx
);
1158 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1160 dnode_t
*dn
= db
->db_dnode
;
1161 uint64_t txg
= tx
->tx_txg
;
1162 dbuf_dirty_record_t
*dr
, **drp
;
1165 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
1167 mutex_enter(&db
->db_mtx
);
1170 * If this buffer is not dirty, we're done.
1172 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1173 if (dr
->dr_txg
<= txg
)
1175 if (dr
== NULL
|| dr
->dr_txg
< txg
) {
1176 mutex_exit(&db
->db_mtx
);
1179 ASSERT(dr
->dr_txg
== txg
);
1182 * If this buffer is currently held, we cannot undirty
1183 * it, since one of the current holders may be in the
1184 * middle of an update. Note that users of dbuf_undirty()
1185 * should not place a hold on the dbuf before the call.
1187 if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1188 mutex_exit(&db
->db_mtx
);
1189 /* Make sure we don't toss this buffer at sync phase */
1190 mutex_enter(&dn
->dn_mtx
);
1191 dnode_clear_range(dn
, db
->db_blkid
, 1, tx
);
1192 mutex_exit(&dn
->dn_mtx
);
1196 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1198 ASSERT(db
->db
.db_size
!= 0);
1200 /* XXX would be nice to fix up dn_towrite_space[] */
1204 if (dr
->dr_parent
) {
1205 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1206 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1207 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1208 } else if (db
->db_level
+1 == dn
->dn_nlevels
) {
1209 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1210 mutex_enter(&dn
->dn_mtx
);
1211 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1212 mutex_exit(&dn
->dn_mtx
);
1215 if (db
->db_level
== 0) {
1216 if (db
->db_state
!= DB_NOFILL
) {
1217 dbuf_unoverride(dr
);
1219 ASSERT(db
->db_buf
!= NULL
);
1220 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1221 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1222 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
1226 ASSERT(db
->db_buf
!= NULL
);
1227 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
1228 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
1229 list_destroy(&dr
->dt
.di
.dr_children
);
1231 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1233 ASSERT(db
->db_dirtycnt
> 0);
1234 db
->db_dirtycnt
-= 1;
1236 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1237 arc_buf_t
*buf
= db
->db_buf
;
1239 ASSERT(arc_released(buf
));
1240 dbuf_set_data(db
, NULL
);
1241 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1246 mutex_exit(&db
->db_mtx
);
1250 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1252 dbuf_will_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1254 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1256 ASSERT(tx
->tx_txg
!= 0);
1257 ASSERT(!refcount_is_zero(&db
->db_holds
));
1259 if (RW_WRITE_HELD(&db
->db_dnode
->dn_struct_rwlock
))
1260 rf
|= DB_RF_HAVESTRUCT
;
1261 (void) dbuf_read(db
, NULL
, rf
);
1262 (void) dbuf_dirty(db
, tx
);
1266 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1268 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1270 db
->db_state
= DB_NOFILL
;
1272 dmu_buf_will_fill(db_fake
, tx
);
1276 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1278 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1280 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
1281 ASSERT(tx
->tx_txg
!= 0);
1282 ASSERT(db
->db_level
== 0);
1283 ASSERT(!refcount_is_zero(&db
->db_holds
));
1285 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1286 dmu_tx_private_ok(tx
));
1289 (void) dbuf_dirty(db
, tx
);
1292 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1295 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1297 mutex_enter(&db
->db_mtx
);
1300 if (db
->db_state
== DB_FILL
) {
1301 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1302 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
1303 /* we were freed while filling */
1304 /* XXX dbuf_undirty? */
1305 bzero(db
->db
.db_data
, db
->db
.db_size
);
1306 db
->db_freed_in_flight
= FALSE
;
1308 db
->db_state
= DB_CACHED
;
1309 cv_broadcast(&db
->db_changed
);
1311 mutex_exit(&db
->db_mtx
);
1315 * "Clear" the contents of this dbuf. This will mark the dbuf
1316 * EVICTING and clear *most* of its references. Unfortunetely,
1317 * when we are not holding the dn_dbufs_mtx, we can't clear the
1318 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1319 * in this case. For callers from the DMU we will usually see:
1320 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1321 * For the arc callback, we will usually see:
1322 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1323 * Sometimes, though, we will get a mix of these two:
1324 * DMU: dbuf_clear()->arc_buf_evict()
1325 * ARC: dbuf_do_evict()->dbuf_destroy()
1328 dbuf_clear(dmu_buf_impl_t
*db
)
1330 dnode_t
*dn
= db
->db_dnode
;
1331 dmu_buf_impl_t
*parent
= db
->db_parent
;
1332 dmu_buf_impl_t
*dndb
= dn
->dn_dbuf
;
1333 int dbuf_gone
= FALSE
;
1335 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1336 ASSERT(refcount_is_zero(&db
->db_holds
));
1338 dbuf_evict_user(db
);
1340 if (db
->db_state
== DB_CACHED
) {
1341 ASSERT(db
->db
.db_data
!= NULL
);
1342 if (db
->db_blkid
== DB_BONUS_BLKID
) {
1343 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
1344 arc_space_return(DN_MAX_BONUSLEN
);
1346 db
->db
.db_data
= NULL
;
1347 db
->db_state
= DB_UNCACHED
;
1350 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1351 ASSERT(db
->db_data_pending
== NULL
);
1353 db
->db_state
= DB_EVICTING
;
1354 db
->db_blkptr
= NULL
;
1356 if (db
->db_blkid
!= DB_BONUS_BLKID
&& MUTEX_HELD(&dn
->dn_dbufs_mtx
)) {
1357 list_remove(&dn
->dn_dbufs
, db
);
1359 db
->db_dnode
= NULL
;
1363 dbuf_gone
= arc_buf_evict(db
->db_buf
);
1366 mutex_exit(&db
->db_mtx
);
1369 * If this dbuf is referened from an indirect dbuf,
1370 * decrement the ref count on the indirect dbuf.
1372 if (parent
&& parent
!= dndb
)
1373 dbuf_rele(parent
, db
);
1377 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
1378 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
1385 ASSERT(blkid
!= DB_BONUS_BLKID
);
1387 if (dn
->dn_phys
->dn_nlevels
== 0)
1390 nlevels
= dn
->dn_phys
->dn_nlevels
;
1392 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1394 ASSERT3U(level
* epbs
, <, 64);
1395 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1396 if (level
>= nlevels
||
1397 (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
1398 /* the buffer has no parent yet */
1400 } else if (level
< nlevels
-1) {
1401 /* this block is referenced from an indirect block */
1402 int err
= dbuf_hold_impl(dn
, level
+1,
1403 blkid
>> epbs
, fail_sparse
, NULL
, parentp
);
1406 err
= dbuf_read(*parentp
, NULL
,
1407 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
1409 dbuf_rele(*parentp
, NULL
);
1413 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
1414 (blkid
& ((1ULL << epbs
) - 1));
1417 /* the block is referenced from the dnode */
1418 ASSERT3U(level
, ==, nlevels
-1);
1419 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
1420 blkid
< dn
->dn_phys
->dn_nblkptr
);
1422 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
1423 *parentp
= dn
->dn_dbuf
;
1425 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
1430 static dmu_buf_impl_t
*
1431 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
1432 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
1434 objset_impl_t
*os
= dn
->dn_objset
;
1435 dmu_buf_impl_t
*db
, *odb
;
1437 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1438 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
1440 db
= kmem_cache_alloc(dbuf_cache
, KM_SLEEP
);
1443 db
->db
.db_object
= dn
->dn_object
;
1444 db
->db_level
= level
;
1445 db
->db_blkid
= blkid
;
1446 db
->db_last_dirty
= NULL
;
1447 db
->db_dirtycnt
= 0;
1449 db
->db_parent
= parent
;
1450 db
->db_blkptr
= blkptr
;
1452 db
->db_user_ptr
= NULL
;
1453 db
->db_user_data_ptr_ptr
= NULL
;
1454 db
->db_evict_func
= NULL
;
1455 db
->db_immediate_evict
= 0;
1456 db
->db_freed_in_flight
= 0;
1458 if (blkid
== DB_BONUS_BLKID
) {
1459 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
1460 db
->db
.db_size
= DN_MAX_BONUSLEN
-
1461 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
1462 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1463 db
->db
.db_offset
= DB_BONUS_BLKID
;
1464 db
->db_state
= DB_UNCACHED
;
1465 /* the bonus dbuf is not placed in the hash table */
1466 arc_space_consume(sizeof (dmu_buf_impl_t
));
1470 db
->db_level
? 1<<dn
->dn_indblkshift
: dn
->dn_datablksz
;
1471 db
->db
.db_size
= blocksize
;
1472 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
1476 * Hold the dn_dbufs_mtx while we get the new dbuf
1477 * in the hash table *and* added to the dbufs list.
1478 * This prevents a possible deadlock with someone
1479 * trying to look up this dbuf before its added to the
1482 mutex_enter(&dn
->dn_dbufs_mtx
);
1483 db
->db_state
= DB_EVICTING
;
1484 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
1485 /* someone else inserted it first */
1486 kmem_cache_free(dbuf_cache
, db
);
1487 mutex_exit(&dn
->dn_dbufs_mtx
);
1490 list_insert_head(&dn
->dn_dbufs
, db
);
1491 db
->db_state
= DB_UNCACHED
;
1492 mutex_exit(&dn
->dn_dbufs_mtx
);
1493 arc_space_consume(sizeof (dmu_buf_impl_t
));
1495 if (parent
&& parent
!= dn
->dn_dbuf
)
1496 dbuf_add_ref(parent
, db
);
1498 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1499 refcount_count(&dn
->dn_holds
) > 0);
1500 (void) refcount_add(&dn
->dn_holds
, db
);
1502 dprintf_dbuf(db
, "db=%p\n", db
);
1508 dbuf_do_evict(void *private)
1510 arc_buf_t
*buf
= private;
1511 dmu_buf_impl_t
*db
= buf
->b_private
;
1513 if (!MUTEX_HELD(&db
->db_mtx
))
1514 mutex_enter(&db
->db_mtx
);
1516 ASSERT(refcount_is_zero(&db
->db_holds
));
1518 if (db
->db_state
!= DB_EVICTING
) {
1519 ASSERT(db
->db_state
== DB_CACHED
);
1524 mutex_exit(&db
->db_mtx
);
1531 dbuf_destroy(dmu_buf_impl_t
*db
)
1533 ASSERT(refcount_is_zero(&db
->db_holds
));
1535 if (db
->db_blkid
!= DB_BONUS_BLKID
) {
1537 * If this dbuf is still on the dn_dbufs list,
1538 * remove it from that list.
1541 dnode_t
*dn
= db
->db_dnode
;
1543 mutex_enter(&dn
->dn_dbufs_mtx
);
1544 list_remove(&dn
->dn_dbufs
, db
);
1545 mutex_exit(&dn
->dn_dbufs_mtx
);
1548 db
->db_dnode
= NULL
;
1550 dbuf_hash_remove(db
);
1552 db
->db_parent
= NULL
;
1555 ASSERT(!list_link_active(&db
->db_link
));
1556 ASSERT(db
->db
.db_data
== NULL
);
1557 ASSERT(db
->db_hash_next
== NULL
);
1558 ASSERT(db
->db_blkptr
== NULL
);
1559 ASSERT(db
->db_data_pending
== NULL
);
1561 kmem_cache_free(dbuf_cache
, db
);
1562 arc_space_return(sizeof (dmu_buf_impl_t
));
1566 dbuf_prefetch(dnode_t
*dn
, uint64_t blkid
)
1568 dmu_buf_impl_t
*db
= NULL
;
1569 blkptr_t
*bp
= NULL
;
1571 ASSERT(blkid
!= DB_BONUS_BLKID
);
1572 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1574 if (dnode_block_freed(dn
, blkid
))
1577 /* dbuf_find() returns with db_mtx held */
1578 if (db
= dbuf_find(dn
, 0, blkid
)) {
1579 if (refcount_count(&db
->db_holds
) > 0) {
1581 * This dbuf is active. We assume that it is
1582 * already CACHED, or else about to be either
1585 mutex_exit(&db
->db_mtx
);
1588 mutex_exit(&db
->db_mtx
);
1592 if (dbuf_findbp(dn
, 0, blkid
, TRUE
, &db
, &bp
) == 0) {
1593 if (bp
&& !BP_IS_HOLE(bp
)) {
1595 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1597 zb
.zb_objset
= dn
->dn_objset
->os_dsl_dataset
?
1598 dn
->dn_objset
->os_dsl_dataset
->ds_object
: 0;
1599 zb
.zb_object
= dn
->dn_object
;
1601 zb
.zb_blkid
= blkid
;
1606 pbuf
= dn
->dn_objset
->os_phys_buf
;
1608 (void) arc_read(NULL
, dn
->dn_objset
->os_spa
,
1609 bp
, pbuf
, NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
1610 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1614 dbuf_rele(db
, NULL
);
1619 * Returns with db_holds incremented, and db_mtx not held.
1620 * Note: dn_struct_rwlock must be held.
1623 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
, int fail_sparse
,
1624 void *tag
, dmu_buf_impl_t
**dbp
)
1626 dmu_buf_impl_t
*db
, *parent
= NULL
;
1628 ASSERT(blkid
!= DB_BONUS_BLKID
);
1629 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1630 ASSERT3U(dn
->dn_nlevels
, >, level
);
1634 /* dbuf_find() returns with db_mtx held */
1635 db
= dbuf_find(dn
, level
, blkid
);
1638 blkptr_t
*bp
= NULL
;
1641 ASSERT3P(parent
, ==, NULL
);
1642 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
1644 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
1648 dbuf_rele(parent
, NULL
);
1652 if (err
&& err
!= ENOENT
)
1654 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
1657 if (db
->db_buf
&& refcount_is_zero(&db
->db_holds
)) {
1658 arc_buf_add_ref(db
->db_buf
, db
);
1659 if (db
->db_buf
->b_data
== NULL
) {
1662 dbuf_rele(parent
, NULL
);
1667 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
1670 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
1673 * If this buffer is currently syncing out, and we are are
1674 * still referencing it from db_data, we need to make a copy
1675 * of it in case we decide we want to dirty it again in this txg.
1677 if (db
->db_level
== 0 && db
->db_blkid
!= DB_BONUS_BLKID
&&
1678 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
1679 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
1680 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
1682 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
1683 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1686 arc_buf_alloc(db
->db_dnode
->dn_objset
->os_spa
,
1687 db
->db
.db_size
, db
, type
));
1688 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
1693 (void) refcount_add(&db
->db_holds
, tag
);
1694 dbuf_update_data(db
);
1696 mutex_exit(&db
->db_mtx
);
1698 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1700 dbuf_rele(parent
, NULL
);
1702 ASSERT3P(db
->db_dnode
, ==, dn
);
1703 ASSERT3U(db
->db_blkid
, ==, blkid
);
1704 ASSERT3U(db
->db_level
, ==, level
);
1711 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
1714 int err
= dbuf_hold_impl(dn
, 0, blkid
, FALSE
, tag
, &db
);
1715 return (err
? NULL
: db
);
1719 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
1722 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, tag
, &db
);
1723 return (err
? NULL
: db
);
1727 dbuf_create_bonus(dnode_t
*dn
)
1729 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1731 ASSERT(dn
->dn_bonus
== NULL
);
1732 dn
->dn_bonus
= dbuf_create(dn
, 0, DB_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
1735 #pragma weak dmu_buf_add_ref = dbuf_add_ref
1737 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
1739 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
1743 #pragma weak dmu_buf_rele = dbuf_rele
1745 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
1749 mutex_enter(&db
->db_mtx
);
1752 holds
= refcount_remove(&db
->db_holds
, tag
);
1756 * We can't freeze indirects if there is a possibility that they
1757 * may be modified in the current syncing context.
1759 if (db
->db_buf
&& holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0))
1760 arc_buf_freeze(db
->db_buf
);
1762 if (holds
== db
->db_dirtycnt
&&
1763 db
->db_level
== 0 && db
->db_immediate_evict
)
1764 dbuf_evict_user(db
);
1767 if (db
->db_blkid
== DB_BONUS_BLKID
) {
1768 mutex_exit(&db
->db_mtx
);
1769 dnode_rele(db
->db_dnode
, db
);
1770 } else if (db
->db_buf
== NULL
) {
1772 * This is a special case: we never associated this
1773 * dbuf with any data allocated from the ARC.
1775 ASSERT(db
->db_state
== DB_UNCACHED
||
1776 db
->db_state
== DB_NOFILL
);
1778 } else if (arc_released(db
->db_buf
)) {
1779 arc_buf_t
*buf
= db
->db_buf
;
1781 * This dbuf has anonymous data associated with it.
1783 dbuf_set_data(db
, NULL
);
1784 VERIFY(arc_buf_remove_ref(buf
, db
) == 1);
1787 VERIFY(arc_buf_remove_ref(db
->db_buf
, db
) == 0);
1788 if (!DBUF_IS_CACHEABLE(db
))
1791 mutex_exit(&db
->db_mtx
);
1794 mutex_exit(&db
->db_mtx
);
1798 #pragma weak dmu_buf_refcount = dbuf_refcount
1800 dbuf_refcount(dmu_buf_impl_t
*db
)
1802 return (refcount_count(&db
->db_holds
));
1806 dmu_buf_set_user(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
1807 dmu_buf_evict_func_t
*evict_func
)
1809 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
1810 user_data_ptr_ptr
, evict_func
));
1814 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, void *user_ptr
, void *user_data_ptr_ptr
,
1815 dmu_buf_evict_func_t
*evict_func
)
1817 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1819 db
->db_immediate_evict
= TRUE
;
1820 return (dmu_buf_update_user(db_fake
, NULL
, user_ptr
,
1821 user_data_ptr_ptr
, evict_func
));
1825 dmu_buf_update_user(dmu_buf_t
*db_fake
, void *old_user_ptr
, void *user_ptr
,
1826 void *user_data_ptr_ptr
, dmu_buf_evict_func_t
*evict_func
)
1828 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1829 ASSERT(db
->db_level
== 0);
1831 ASSERT((user_ptr
== NULL
) == (evict_func
== NULL
));
1833 mutex_enter(&db
->db_mtx
);
1835 if (db
->db_user_ptr
== old_user_ptr
) {
1836 db
->db_user_ptr
= user_ptr
;
1837 db
->db_user_data_ptr_ptr
= user_data_ptr_ptr
;
1838 db
->db_evict_func
= evict_func
;
1840 dbuf_update_data(db
);
1842 old_user_ptr
= db
->db_user_ptr
;
1845 mutex_exit(&db
->db_mtx
);
1846 return (old_user_ptr
);
1850 dmu_buf_get_user(dmu_buf_t
*db_fake
)
1852 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1853 ASSERT(!refcount_is_zero(&db
->db_holds
));
1855 return (db
->db_user_ptr
);
1859 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
1861 /* ASSERT(dmu_tx_is_syncing(tx) */
1862 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1864 if (db
->db_blkptr
!= NULL
)
1867 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
1869 * This buffer was allocated at a time when there was
1870 * no available blkptrs from the dnode, or it was
1871 * inappropriate to hook it in (i.e., nlevels mis-match).
1873 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
1874 ASSERT(db
->db_parent
== NULL
);
1875 db
->db_parent
= dn
->dn_dbuf
;
1876 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
1879 dmu_buf_impl_t
*parent
= db
->db_parent
;
1880 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1882 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
1883 if (parent
== NULL
) {
1884 mutex_exit(&db
->db_mtx
);
1885 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1886 (void) dbuf_hold_impl(dn
, db
->db_level
+1,
1887 db
->db_blkid
>> epbs
, FALSE
, db
, &parent
);
1888 rw_exit(&dn
->dn_struct_rwlock
);
1889 mutex_enter(&db
->db_mtx
);
1890 db
->db_parent
= parent
;
1892 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
1893 (db
->db_blkid
& ((1ULL << epbs
) - 1));
1899 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
1901 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1902 dnode_t
*dn
= db
->db_dnode
;
1905 ASSERT(dmu_tx_is_syncing(tx
));
1907 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
1909 mutex_enter(&db
->db_mtx
);
1911 ASSERT(db
->db_level
> 0);
1914 if (db
->db_buf
== NULL
) {
1915 mutex_exit(&db
->db_mtx
);
1916 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
1917 mutex_enter(&db
->db_mtx
);
1919 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1920 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
1921 ASSERT(db
->db_buf
!= NULL
);
1923 dbuf_check_blkptr(dn
, db
);
1925 db
->db_data_pending
= dr
;
1927 mutex_exit(&db
->db_mtx
);
1928 dbuf_write(dr
, db
->db_buf
, tx
);
1931 mutex_enter(&dr
->dt
.di
.dr_mtx
);
1932 dbuf_sync_list(&dr
->dt
.di
.dr_children
, tx
);
1933 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
1934 mutex_exit(&dr
->dt
.di
.dr_mtx
);
1939 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
1941 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
1942 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1943 dnode_t
*dn
= db
->db_dnode
;
1944 objset_impl_t
*os
= dn
->dn_objset
;
1945 uint64_t txg
= tx
->tx_txg
;
1948 ASSERT(dmu_tx_is_syncing(tx
));
1950 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
1952 mutex_enter(&db
->db_mtx
);
1954 * To be synced, we must be dirtied. But we
1955 * might have been freed after the dirty.
1957 if (db
->db_state
== DB_UNCACHED
) {
1958 /* This buffer has been freed since it was dirtied */
1959 ASSERT(db
->db
.db_data
== NULL
);
1960 } else if (db
->db_state
== DB_FILL
) {
1961 /* This buffer was freed and is now being re-filled */
1962 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
1964 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
1969 * If this is a bonus buffer, simply copy the bonus data into the
1970 * dnode. It will be written out when the dnode is synced (and it
1971 * will be synced, since it must have been dirty for dbuf_sync to
1974 if (db
->db_blkid
== DB_BONUS_BLKID
) {
1975 dbuf_dirty_record_t
**drp
;
1977 ASSERT(*datap
!= NULL
);
1978 ASSERT3U(db
->db_level
, ==, 0);
1979 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
1980 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
1981 if (*datap
!= db
->db
.db_data
) {
1982 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
1983 arc_space_return(DN_MAX_BONUSLEN
);
1985 db
->db_data_pending
= NULL
;
1986 drp
= &db
->db_last_dirty
;
1988 drp
= &(*drp
)->dr_next
;
1989 ASSERT(dr
->dr_next
== NULL
);
1991 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1992 ASSERT(db
->db_dirtycnt
> 0);
1993 db
->db_dirtycnt
-= 1;
1994 mutex_exit(&db
->db_mtx
);
1995 dbuf_rele(db
, (void *)(uintptr_t)txg
);
2000 * This function may have dropped the db_mtx lock allowing a dmu_sync
2001 * operation to sneak in. As a result, we need to ensure that we
2002 * don't check the dr_override_state until we have returned from
2003 * dbuf_check_blkptr.
2005 dbuf_check_blkptr(dn
, db
);
2008 * If this buffer is in the middle of an immdiate write,
2009 * wait for the synchronous IO to complete.
2011 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
2012 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
2013 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2014 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
2018 * If this dbuf has already been written out via an immediate write,
2019 * just complete the write by copying over the new block pointer and
2020 * updating the accounting via the write-completion functions.
2022 if (dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2025 zio_fake
.io_private
= &db
;
2026 zio_fake
.io_error
= 0;
2027 zio_fake
.io_bp
= db
->db_blkptr
;
2028 zio_fake
.io_bp_orig
= *db
->db_blkptr
;
2029 zio_fake
.io_txg
= txg
;
2030 zio_fake
.io_flags
= 0;
2032 *db
->db_blkptr
= dr
->dt
.dl
.dr_overridden_by
;
2033 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
2034 db
->db_data_pending
= dr
;
2035 dr
->dr_zio
= &zio_fake
;
2036 mutex_exit(&db
->db_mtx
);
2038 ASSERT(!DVA_EQUAL(BP_IDENTITY(zio_fake
.io_bp
),
2039 BP_IDENTITY(&zio_fake
.io_bp_orig
)) ||
2040 BP_IS_HOLE(zio_fake
.io_bp
));
2042 if (BP_IS_OLDER(&zio_fake
.io_bp_orig
, txg
))
2043 (void) dsl_dataset_block_kill(os
->os_dsl_dataset
,
2044 &zio_fake
.io_bp_orig
, dn
->dn_zio
, tx
);
2046 dbuf_write_ready(&zio_fake
, db
->db_buf
, db
);
2047 dbuf_write_done(&zio_fake
, db
->db_buf
, db
);
2052 if (db
->db_state
!= DB_NOFILL
) {
2053 blksz
= arc_buf_size(*datap
);
2055 if (dn
->dn_object
!= DMU_META_DNODE_OBJECT
) {
2057 * If this buffer is currently "in use" (i.e., there
2058 * are active holds and db_data still references it),
2059 * then make a copy before we start the write so that
2060 * any modifications from the open txg will not leak
2063 * NOTE: this copy does not need to be made for
2064 * objects only modified in the syncing context (e.g.
2065 * DNONE_DNODE blocks).
2067 if (refcount_count(&db
->db_holds
) > 1 &&
2068 *datap
== db
->db_buf
) {
2069 arc_buf_contents_t type
=
2070 DBUF_GET_BUFC_TYPE(db
);
2072 arc_buf_alloc(os
->os_spa
, blksz
, db
, type
);
2073 bcopy(db
->db
.db_data
, (*datap
)->b_data
, blksz
);
2077 ASSERT(*datap
!= NULL
);
2079 db
->db_data_pending
= dr
;
2081 mutex_exit(&db
->db_mtx
);
2083 dbuf_write(dr
, *datap
, tx
);
2085 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2086 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
)
2087 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
2089 zio_nowait(dr
->dr_zio
);
2093 dbuf_sync_list(list_t
*list
, dmu_tx_t
*tx
)
2095 dbuf_dirty_record_t
*dr
;
2097 while (dr
= list_head(list
)) {
2098 if (dr
->dr_zio
!= NULL
) {
2100 * If we find an already initialized zio then we
2101 * are processing the meta-dnode, and we have finished.
2102 * The dbufs for all dnodes are put back on the list
2103 * during processing, so that we can zio_wait()
2104 * these IOs after initiating all child IOs.
2106 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
2107 DMU_META_DNODE_OBJECT
);
2110 list_remove(list
, dr
);
2111 if (dr
->dr_dbuf
->db_level
> 0)
2112 dbuf_sync_indirect(dr
, tx
);
2114 dbuf_sync_leaf(dr
, tx
);
2119 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
2121 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2122 dnode_t
*dn
= db
->db_dnode
;
2123 objset_impl_t
*os
= dn
->dn_objset
;
2124 dmu_buf_impl_t
*parent
= db
->db_parent
;
2125 uint64_t txg
= tx
->tx_txg
;
2127 writeprops_t wp
= { 0 };
2130 if (!BP_IS_HOLE(db
->db_blkptr
) &&
2131 (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
)) {
2133 * Private object buffers are released here rather
2134 * than in dbuf_dirty() since they are only modified
2135 * in the syncing context and we don't want the
2136 * overhead of making multiple copies of the data.
2138 arc_release(data
, db
);
2139 } else if (db
->db_state
!= DB_NOFILL
) {
2140 ASSERT(arc_released(data
));
2141 /* XXX why do we need to thaw here? */
2145 if (parent
!= dn
->dn_dbuf
) {
2146 ASSERT(parent
&& parent
->db_data_pending
);
2147 ASSERT(db
->db_level
== parent
->db_level
-1);
2148 ASSERT(arc_released(parent
->db_buf
));
2149 zio
= parent
->db_data_pending
->dr_zio
;
2151 ASSERT(db
->db_level
== dn
->dn_phys
->dn_nlevels
-1);
2152 ASSERT3P(db
->db_blkptr
, ==,
2153 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
2157 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
2158 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
2161 zb
.zb_objset
= os
->os_dsl_dataset
? os
->os_dsl_dataset
->ds_object
: 0;
2162 zb
.zb_object
= db
->db
.db_object
;
2163 zb
.zb_level
= db
->db_level
;
2164 zb
.zb_blkid
= db
->db_blkid
;
2166 wp
.wp_type
= dn
->dn_type
;
2167 wp
.wp_level
= db
->db_level
;
2168 wp
.wp_copies
= os
->os_copies
;
2169 wp
.wp_dncompress
= dn
->dn_compress
;
2170 wp
.wp_oscompress
= os
->os_compress
;
2171 wp
.wp_dnchecksum
= dn
->dn_checksum
;
2172 wp
.wp_oschecksum
= os
->os_checksum
;
2174 if (BP_IS_OLDER(db
->db_blkptr
, txg
))
2175 (void) dsl_dataset_block_kill(
2176 os
->os_dsl_dataset
, db
->db_blkptr
, zio
, tx
);
2178 if (db
->db_state
== DB_NOFILL
) {
2179 zio_prop_t zp
= { 0 };
2181 write_policy(os
->os_spa
, &wp
, &zp
);
2182 dr
->dr_zio
= zio_write(zio
, os
->os_spa
,
2183 txg
, db
->db_blkptr
, NULL
,
2184 db
->db
.db_size
, &zp
, dbuf_skip_write_ready
,
2185 dbuf_skip_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
2186 ZIO_FLAG_MUSTSUCCEED
, &zb
);
2188 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, &wp
,
2189 DBUF_IS_L2CACHEABLE(db
), txg
, db
->db_blkptr
,
2190 data
, dbuf_write_ready
, dbuf_write_done
, db
,
2191 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
2195 /* wrapper function for dbuf_write_ready bypassing ARC */
2197 dbuf_skip_write_ready(zio_t
*zio
)
2199 blkptr_t
*bp
= zio
->io_bp
;
2201 if (!BP_IS_GANG(bp
))
2202 zio_skip_write(zio
);
2204 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
2207 /* wrapper function for dbuf_write_done bypassing ARC */
2209 dbuf_skip_write_done(zio_t
*zio
)
2211 dbuf_write_done(zio
, NULL
, zio
->io_private
);
2216 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2218 dmu_buf_impl_t
*db
= vdb
;
2219 dnode_t
*dn
= db
->db_dnode
;
2220 objset_impl_t
*os
= dn
->dn_objset
;
2221 blkptr_t
*bp
= zio
->io_bp
;
2222 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2224 int old_size
, new_size
, i
;
2226 ASSERT(db
->db_blkptr
== bp
);
2228 dprintf_dbuf_bp(db
, bp_orig
, "bp_orig: %s", "");
2230 old_size
= bp_get_dasize(os
->os_spa
, bp_orig
);
2231 new_size
= bp_get_dasize(os
->os_spa
, bp
);
2233 dnode_diduse_space(dn
, new_size
- old_size
);
2235 if (BP_IS_HOLE(bp
)) {
2236 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2237 dmu_tx_t
*tx
= os
->os_synctx
;
2239 if (bp_orig
->blk_birth
== tx
->tx_txg
)
2240 (void) dsl_dataset_block_kill(ds
, bp_orig
, zio
, tx
);
2241 ASSERT3U(bp
->blk_fill
, ==, 0);
2245 ASSERT(BP_GET_TYPE(bp
) == dn
->dn_type
);
2246 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
2248 mutex_enter(&db
->db_mtx
);
2250 if (db
->db_level
== 0) {
2251 mutex_enter(&dn
->dn_mtx
);
2252 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
)
2253 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
2254 mutex_exit(&dn
->dn_mtx
);
2256 if (dn
->dn_type
== DMU_OT_DNODE
) {
2257 dnode_phys_t
*dnp
= db
->db
.db_data
;
2258 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
2260 if (dnp
->dn_type
!= DMU_OT_NONE
)
2267 blkptr_t
*ibp
= db
->db
.db_data
;
2268 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2269 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
2270 if (BP_IS_HOLE(ibp
))
2272 ASSERT3U(BP_GET_LSIZE(ibp
), ==,
2273 db
->db_level
== 1 ? dn
->dn_datablksz
:
2274 (1<<dn
->dn_phys
->dn_indblkshift
));
2275 fill
+= ibp
->blk_fill
;
2279 bp
->blk_fill
= fill
;
2281 mutex_exit(&db
->db_mtx
);
2283 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
2284 ASSERT(DVA_EQUAL(BP_IDENTITY(bp
), BP_IDENTITY(bp_orig
)));
2286 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2287 dmu_tx_t
*tx
= os
->os_synctx
;
2289 if (bp_orig
->blk_birth
== tx
->tx_txg
)
2290 (void) dsl_dataset_block_kill(ds
, bp_orig
, zio
, tx
);
2291 dsl_dataset_block_born(ds
, bp
, tx
);
2297 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
2299 dmu_buf_impl_t
*db
= vdb
;
2300 uint64_t txg
= zio
->io_txg
;
2301 dbuf_dirty_record_t
**drp
, *dr
;
2303 ASSERT3U(zio
->io_error
, ==, 0);
2305 mutex_enter(&db
->db_mtx
);
2307 drp
= &db
->db_last_dirty
;
2308 while ((dr
= *drp
) != db
->db_data_pending
)
2310 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2311 ASSERT(dr
->dr_txg
== txg
);
2312 ASSERT(dr
->dr_next
== NULL
);
2315 if (db
->db_level
== 0) {
2316 ASSERT(db
->db_blkid
!= DB_BONUS_BLKID
);
2317 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2319 if (db
->db_state
!= DB_NOFILL
) {
2320 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2321 VERIFY(arc_buf_remove_ref(dr
->dt
.dl
.dr_data
,
2323 else if (!BP_IS_HOLE(db
->db_blkptr
))
2324 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2326 ASSERT(arc_released(db
->db_buf
));
2329 dnode_t
*dn
= db
->db_dnode
;
2331 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2332 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2333 if (!BP_IS_HOLE(db
->db_blkptr
)) {
2335 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2336 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
2338 ASSERT3U(dn
->dn_phys
->dn_maxblkid
2339 >> (db
->db_level
* epbs
), >=, db
->db_blkid
);
2340 arc_set_callback(db
->db_buf
, dbuf_do_evict
, db
);
2342 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2343 list_destroy(&dr
->dt
.di
.dr_children
);
2345 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2347 cv_broadcast(&db
->db_changed
);
2348 ASSERT(db
->db_dirtycnt
> 0);
2349 db
->db_dirtycnt
-= 1;
2350 db
->db_data_pending
= NULL
;
2351 mutex_exit(&db
->db_mtx
);
2353 dprintf_dbuf_bp(db
, zio
->io_bp
, "bp: %s", "");
2355 dbuf_rele(db
, (void *)(uintptr_t)txg
);