3236 zio nop-write
[unleashed.git] / usr / src / uts / common / fs / zfs / dbuf.c
blobd56dbc95ac3a6d6cbbdec27ce5b1ffbbcced910a
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/dmu.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dbuf.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/spa.h>
36 #include <sys/zio.h>
37 #include <sys/dmu_zfetch.h>
38 #include <sys/sa.h>
39 #include <sys/sa_impl.h>
41 static void dbuf_destroy(dmu_buf_impl_t *db);
42 static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
43 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
46 * Global data structures and functions for the dbuf cache.
48 static kmem_cache_t *dbuf_cache;
50 /* ARGSUSED */
51 static int
52 dbuf_cons(void *vdb, void *unused, int kmflag)
54 dmu_buf_impl_t *db = vdb;
55 bzero(db, sizeof (dmu_buf_impl_t));
57 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
58 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
59 refcount_create(&db->db_holds);
60 return (0);
63 /* ARGSUSED */
64 static void
65 dbuf_dest(void *vdb, void *unused)
67 dmu_buf_impl_t *db = vdb;
68 mutex_destroy(&db->db_mtx);
69 cv_destroy(&db->db_changed);
70 refcount_destroy(&db->db_holds);
74 * dbuf hash table routines
76 static dbuf_hash_table_t dbuf_hash_table;
78 static uint64_t dbuf_hash_count;
80 static uint64_t
81 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
83 uintptr_t osv = (uintptr_t)os;
84 uint64_t crc = -1ULL;
86 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
87 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
88 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
89 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
90 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
91 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
92 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
94 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
96 return (crc);
99 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
101 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
102 ((dbuf)->db.db_object == (obj) && \
103 (dbuf)->db_objset == (os) && \
104 (dbuf)->db_level == (level) && \
105 (dbuf)->db_blkid == (blkid))
107 dmu_buf_impl_t *
108 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
110 dbuf_hash_table_t *h = &dbuf_hash_table;
111 objset_t *os = dn->dn_objset;
112 uint64_t obj = dn->dn_object;
113 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
114 uint64_t idx = hv & h->hash_table_mask;
115 dmu_buf_impl_t *db;
117 mutex_enter(DBUF_HASH_MUTEX(h, idx));
118 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
119 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
120 mutex_enter(&db->db_mtx);
121 if (db->db_state != DB_EVICTING) {
122 mutex_exit(DBUF_HASH_MUTEX(h, idx));
123 return (db);
125 mutex_exit(&db->db_mtx);
128 mutex_exit(DBUF_HASH_MUTEX(h, idx));
129 return (NULL);
133 * Insert an entry into the hash table. If there is already an element
134 * equal to elem in the hash table, then the already existing element
135 * will be returned and the new element will not be inserted.
136 * Otherwise returns NULL.
138 static dmu_buf_impl_t *
139 dbuf_hash_insert(dmu_buf_impl_t *db)
141 dbuf_hash_table_t *h = &dbuf_hash_table;
142 objset_t *os = db->db_objset;
143 uint64_t obj = db->db.db_object;
144 int level = db->db_level;
145 uint64_t blkid = db->db_blkid;
146 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
147 uint64_t idx = hv & h->hash_table_mask;
148 dmu_buf_impl_t *dbf;
150 mutex_enter(DBUF_HASH_MUTEX(h, idx));
151 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
152 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
153 mutex_enter(&dbf->db_mtx);
154 if (dbf->db_state != DB_EVICTING) {
155 mutex_exit(DBUF_HASH_MUTEX(h, idx));
156 return (dbf);
158 mutex_exit(&dbf->db_mtx);
162 mutex_enter(&db->db_mtx);
163 db->db_hash_next = h->hash_table[idx];
164 h->hash_table[idx] = db;
165 mutex_exit(DBUF_HASH_MUTEX(h, idx));
166 atomic_add_64(&dbuf_hash_count, 1);
168 return (NULL);
172 * Remove an entry from the hash table. This operation will
173 * fail if there are any existing holds on the db.
175 static void
176 dbuf_hash_remove(dmu_buf_impl_t *db)
178 dbuf_hash_table_t *h = &dbuf_hash_table;
179 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
180 db->db_level, db->db_blkid);
181 uint64_t idx = hv & h->hash_table_mask;
182 dmu_buf_impl_t *dbf, **dbp;
185 * We musn't hold db_mtx to maintin lock ordering:
186 * DBUF_HASH_MUTEX > db_mtx.
188 ASSERT(refcount_is_zero(&db->db_holds));
189 ASSERT(db->db_state == DB_EVICTING);
190 ASSERT(!MUTEX_HELD(&db->db_mtx));
192 mutex_enter(DBUF_HASH_MUTEX(h, idx));
193 dbp = &h->hash_table[idx];
194 while ((dbf = *dbp) != db) {
195 dbp = &dbf->db_hash_next;
196 ASSERT(dbf != NULL);
198 *dbp = db->db_hash_next;
199 db->db_hash_next = NULL;
200 mutex_exit(DBUF_HASH_MUTEX(h, idx));
201 atomic_add_64(&dbuf_hash_count, -1);
204 static arc_evict_func_t dbuf_do_evict;
206 static void
207 dbuf_evict_user(dmu_buf_impl_t *db)
209 ASSERT(MUTEX_HELD(&db->db_mtx));
211 if (db->db_level != 0 || db->db_evict_func == NULL)
212 return;
214 if (db->db_user_data_ptr_ptr)
215 *db->db_user_data_ptr_ptr = db->db.db_data;
216 db->db_evict_func(&db->db, db->db_user_ptr);
217 db->db_user_ptr = NULL;
218 db->db_user_data_ptr_ptr = NULL;
219 db->db_evict_func = NULL;
222 boolean_t
223 dbuf_is_metadata(dmu_buf_impl_t *db)
225 if (db->db_level > 0) {
226 return (B_TRUE);
227 } else {
228 boolean_t is_metadata;
230 DB_DNODE_ENTER(db);
231 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
232 DB_DNODE_EXIT(db);
234 return (is_metadata);
238 void
239 dbuf_evict(dmu_buf_impl_t *db)
241 ASSERT(MUTEX_HELD(&db->db_mtx));
242 ASSERT(db->db_buf == NULL);
243 ASSERT(db->db_data_pending == NULL);
245 dbuf_clear(db);
246 dbuf_destroy(db);
249 void
250 dbuf_init(void)
252 uint64_t hsize = 1ULL << 16;
253 dbuf_hash_table_t *h = &dbuf_hash_table;
254 int i;
257 * The hash table is big enough to fill all of physical memory
258 * with an average 4K block size. The table will take up
259 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
261 while (hsize * 4096 < physmem * PAGESIZE)
262 hsize <<= 1;
264 retry:
265 h->hash_table_mask = hsize - 1;
266 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
267 if (h->hash_table == NULL) {
268 /* XXX - we should really return an error instead of assert */
269 ASSERT(hsize > (1ULL << 10));
270 hsize >>= 1;
271 goto retry;
274 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
275 sizeof (dmu_buf_impl_t),
276 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
278 for (i = 0; i < DBUF_MUTEXES; i++)
279 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
282 void
283 dbuf_fini(void)
285 dbuf_hash_table_t *h = &dbuf_hash_table;
286 int i;
288 for (i = 0; i < DBUF_MUTEXES; i++)
289 mutex_destroy(&h->hash_mutexes[i]);
290 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
291 kmem_cache_destroy(dbuf_cache);
295 * Other stuff.
298 #ifdef ZFS_DEBUG
299 static void
300 dbuf_verify(dmu_buf_impl_t *db)
302 dnode_t *dn;
303 dbuf_dirty_record_t *dr;
305 ASSERT(MUTEX_HELD(&db->db_mtx));
307 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
308 return;
310 ASSERT(db->db_objset != NULL);
311 DB_DNODE_ENTER(db);
312 dn = DB_DNODE(db);
313 if (dn == NULL) {
314 ASSERT(db->db_parent == NULL);
315 ASSERT(db->db_blkptr == NULL);
316 } else {
317 ASSERT3U(db->db.db_object, ==, dn->dn_object);
318 ASSERT3P(db->db_objset, ==, dn->dn_objset);
319 ASSERT3U(db->db_level, <, dn->dn_nlevels);
320 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
321 db->db_blkid == DMU_SPILL_BLKID ||
322 !list_is_empty(&dn->dn_dbufs));
324 if (db->db_blkid == DMU_BONUS_BLKID) {
325 ASSERT(dn != NULL);
326 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
327 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
328 } else if (db->db_blkid == DMU_SPILL_BLKID) {
329 ASSERT(dn != NULL);
330 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
331 ASSERT0(db->db.db_offset);
332 } else {
333 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
336 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
337 ASSERT(dr->dr_dbuf == db);
339 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
340 ASSERT(dr->dr_dbuf == db);
343 * We can't assert that db_size matches dn_datablksz because it
344 * can be momentarily different when another thread is doing
345 * dnode_set_blksz().
347 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
348 dr = db->db_data_pending;
350 * It should only be modified in syncing context, so
351 * make sure we only have one copy of the data.
353 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
356 /* verify db->db_blkptr */
357 if (db->db_blkptr) {
358 if (db->db_parent == dn->dn_dbuf) {
359 /* db is pointed to by the dnode */
360 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
361 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
362 ASSERT(db->db_parent == NULL);
363 else
364 ASSERT(db->db_parent != NULL);
365 if (db->db_blkid != DMU_SPILL_BLKID)
366 ASSERT3P(db->db_blkptr, ==,
367 &dn->dn_phys->dn_blkptr[db->db_blkid]);
368 } else {
369 /* db is pointed to by an indirect block */
370 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
371 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
372 ASSERT3U(db->db_parent->db.db_object, ==,
373 db->db.db_object);
375 * dnode_grow_indblksz() can make this fail if we don't
376 * have the struct_rwlock. XXX indblksz no longer
377 * grows. safe to do this now?
379 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
380 ASSERT3P(db->db_blkptr, ==,
381 ((blkptr_t *)db->db_parent->db.db_data +
382 db->db_blkid % epb));
386 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
387 (db->db_buf == NULL || db->db_buf->b_data) &&
388 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
389 db->db_state != DB_FILL && !dn->dn_free_txg) {
391 * If the blkptr isn't set but they have nonzero data,
392 * it had better be dirty, otherwise we'll lose that
393 * data when we evict this buffer.
395 if (db->db_dirtycnt == 0) {
396 uint64_t *buf = db->db.db_data;
397 int i;
399 for (i = 0; i < db->db.db_size >> 3; i++) {
400 ASSERT(buf[i] == 0);
404 DB_DNODE_EXIT(db);
406 #endif
408 static void
409 dbuf_update_data(dmu_buf_impl_t *db)
411 ASSERT(MUTEX_HELD(&db->db_mtx));
412 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
413 ASSERT(!refcount_is_zero(&db->db_holds));
414 *db->db_user_data_ptr_ptr = db->db.db_data;
418 static void
419 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
421 ASSERT(MUTEX_HELD(&db->db_mtx));
422 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
423 db->db_buf = buf;
424 if (buf != NULL) {
425 ASSERT(buf->b_data != NULL);
426 db->db.db_data = buf->b_data;
427 if (!arc_released(buf))
428 arc_set_callback(buf, dbuf_do_evict, db);
429 dbuf_update_data(db);
430 } else {
431 dbuf_evict_user(db);
432 db->db.db_data = NULL;
433 if (db->db_state != DB_NOFILL)
434 db->db_state = DB_UNCACHED;
439 * Loan out an arc_buf for read. Return the loaned arc_buf.
441 arc_buf_t *
442 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
444 arc_buf_t *abuf;
446 mutex_enter(&db->db_mtx);
447 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
448 int blksz = db->db.db_size;
449 spa_t *spa;
451 mutex_exit(&db->db_mtx);
452 DB_GET_SPA(&spa, db);
453 abuf = arc_loan_buf(spa, blksz);
454 bcopy(db->db.db_data, abuf->b_data, blksz);
455 } else {
456 abuf = db->db_buf;
457 arc_loan_inuse_buf(abuf, db);
458 dbuf_set_data(db, NULL);
459 mutex_exit(&db->db_mtx);
461 return (abuf);
464 uint64_t
465 dbuf_whichblock(dnode_t *dn, uint64_t offset)
467 if (dn->dn_datablkshift) {
468 return (offset >> dn->dn_datablkshift);
469 } else {
470 ASSERT3U(offset, <, dn->dn_datablksz);
471 return (0);
475 static void
476 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
478 dmu_buf_impl_t *db = vdb;
480 mutex_enter(&db->db_mtx);
481 ASSERT3U(db->db_state, ==, DB_READ);
483 * All reads are synchronous, so we must have a hold on the dbuf
485 ASSERT(refcount_count(&db->db_holds) > 0);
486 ASSERT(db->db_buf == NULL);
487 ASSERT(db->db.db_data == NULL);
488 if (db->db_level == 0 && db->db_freed_in_flight) {
489 /* we were freed in flight; disregard any error */
490 arc_release(buf, db);
491 bzero(buf->b_data, db->db.db_size);
492 arc_buf_freeze(buf);
493 db->db_freed_in_flight = FALSE;
494 dbuf_set_data(db, buf);
495 db->db_state = DB_CACHED;
496 } else if (zio == NULL || zio->io_error == 0) {
497 dbuf_set_data(db, buf);
498 db->db_state = DB_CACHED;
499 } else {
500 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
501 ASSERT3P(db->db_buf, ==, NULL);
502 VERIFY(arc_buf_remove_ref(buf, db) == 1);
503 db->db_state = DB_UNCACHED;
505 cv_broadcast(&db->db_changed);
506 dbuf_rele_and_unlock(db, NULL);
509 static void
510 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
512 dnode_t *dn;
513 spa_t *spa;
514 zbookmark_t zb;
515 uint32_t aflags = ARC_NOWAIT;
516 arc_buf_t *pbuf;
518 DB_DNODE_ENTER(db);
519 dn = DB_DNODE(db);
520 ASSERT(!refcount_is_zero(&db->db_holds));
521 /* We need the struct_rwlock to prevent db_blkptr from changing. */
522 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
523 ASSERT(MUTEX_HELD(&db->db_mtx));
524 ASSERT(db->db_state == DB_UNCACHED);
525 ASSERT(db->db_buf == NULL);
527 if (db->db_blkid == DMU_BONUS_BLKID) {
528 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
530 ASSERT3U(bonuslen, <=, db->db.db_size);
531 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
532 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
533 if (bonuslen < DN_MAX_BONUSLEN)
534 bzero(db->db.db_data, DN_MAX_BONUSLEN);
535 if (bonuslen)
536 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
537 DB_DNODE_EXIT(db);
538 dbuf_update_data(db);
539 db->db_state = DB_CACHED;
540 mutex_exit(&db->db_mtx);
541 return;
545 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
546 * processes the delete record and clears the bp while we are waiting
547 * for the dn_mtx (resulting in a "no" from block_freed).
549 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
550 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
551 BP_IS_HOLE(db->db_blkptr)))) {
552 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
554 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
555 db->db.db_size, db, type));
556 DB_DNODE_EXIT(db);
557 bzero(db->db.db_data, db->db.db_size);
558 db->db_state = DB_CACHED;
559 *flags |= DB_RF_CACHED;
560 mutex_exit(&db->db_mtx);
561 return;
564 spa = dn->dn_objset->os_spa;
565 DB_DNODE_EXIT(db);
567 db->db_state = DB_READ;
568 mutex_exit(&db->db_mtx);
570 if (DBUF_IS_L2CACHEABLE(db))
571 aflags |= ARC_L2CACHE;
573 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
574 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
575 db->db.db_object, db->db_level, db->db_blkid);
577 dbuf_add_ref(db, NULL);
578 /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
580 if (db->db_parent)
581 pbuf = db->db_parent->db_buf;
582 else
583 pbuf = db->db_objset->os_phys_buf;
585 (void) dsl_read(zio, spa, db->db_blkptr, pbuf,
586 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
587 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
588 &aflags, &zb);
589 if (aflags & ARC_CACHED)
590 *flags |= DB_RF_CACHED;
594 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
596 int err = 0;
597 int havepzio = (zio != NULL);
598 int prefetch;
599 dnode_t *dn;
602 * We don't have to hold the mutex to check db_state because it
603 * can't be freed while we have a hold on the buffer.
605 ASSERT(!refcount_is_zero(&db->db_holds));
607 if (db->db_state == DB_NOFILL)
608 return (EIO);
610 DB_DNODE_ENTER(db);
611 dn = DB_DNODE(db);
612 if ((flags & DB_RF_HAVESTRUCT) == 0)
613 rw_enter(&dn->dn_struct_rwlock, RW_READER);
615 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
616 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
617 DBUF_IS_CACHEABLE(db);
619 mutex_enter(&db->db_mtx);
620 if (db->db_state == DB_CACHED) {
621 mutex_exit(&db->db_mtx);
622 if (prefetch)
623 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
624 db->db.db_size, TRUE);
625 if ((flags & DB_RF_HAVESTRUCT) == 0)
626 rw_exit(&dn->dn_struct_rwlock);
627 DB_DNODE_EXIT(db);
628 } else if (db->db_state == DB_UNCACHED) {
629 spa_t *spa = dn->dn_objset->os_spa;
631 if (zio == NULL)
632 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
633 dbuf_read_impl(db, zio, &flags);
635 /* dbuf_read_impl has dropped db_mtx for us */
637 if (prefetch)
638 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
639 db->db.db_size, flags & DB_RF_CACHED);
641 if ((flags & DB_RF_HAVESTRUCT) == 0)
642 rw_exit(&dn->dn_struct_rwlock);
643 DB_DNODE_EXIT(db);
645 if (!havepzio)
646 err = zio_wait(zio);
647 } else {
648 mutex_exit(&db->db_mtx);
649 if (prefetch)
650 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
651 db->db.db_size, TRUE);
652 if ((flags & DB_RF_HAVESTRUCT) == 0)
653 rw_exit(&dn->dn_struct_rwlock);
654 DB_DNODE_EXIT(db);
656 mutex_enter(&db->db_mtx);
657 if ((flags & DB_RF_NEVERWAIT) == 0) {
658 while (db->db_state == DB_READ ||
659 db->db_state == DB_FILL) {
660 ASSERT(db->db_state == DB_READ ||
661 (flags & DB_RF_HAVESTRUCT) == 0);
662 cv_wait(&db->db_changed, &db->db_mtx);
664 if (db->db_state == DB_UNCACHED)
665 err = EIO;
667 mutex_exit(&db->db_mtx);
670 ASSERT(err || havepzio || db->db_state == DB_CACHED);
671 return (err);
674 static void
675 dbuf_noread(dmu_buf_impl_t *db)
677 ASSERT(!refcount_is_zero(&db->db_holds));
678 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
679 mutex_enter(&db->db_mtx);
680 while (db->db_state == DB_READ || db->db_state == DB_FILL)
681 cv_wait(&db->db_changed, &db->db_mtx);
682 if (db->db_state == DB_UNCACHED) {
683 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
684 spa_t *spa;
686 ASSERT(db->db_buf == NULL);
687 ASSERT(db->db.db_data == NULL);
688 DB_GET_SPA(&spa, db);
689 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
690 db->db_state = DB_FILL;
691 } else if (db->db_state == DB_NOFILL) {
692 dbuf_set_data(db, NULL);
693 } else {
694 ASSERT3U(db->db_state, ==, DB_CACHED);
696 mutex_exit(&db->db_mtx);
700 * This is our just-in-time copy function. It makes a copy of
701 * buffers, that have been modified in a previous transaction
702 * group, before we modify them in the current active group.
704 * This function is used in two places: when we are dirtying a
705 * buffer for the first time in a txg, and when we are freeing
706 * a range in a dnode that includes this buffer.
708 * Note that when we are called from dbuf_free_range() we do
709 * not put a hold on the buffer, we just traverse the active
710 * dbuf list for the dnode.
712 static void
713 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
715 dbuf_dirty_record_t *dr = db->db_last_dirty;
717 ASSERT(MUTEX_HELD(&db->db_mtx));
718 ASSERT(db->db.db_data != NULL);
719 ASSERT(db->db_level == 0);
720 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
722 if (dr == NULL ||
723 (dr->dt.dl.dr_data !=
724 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
725 return;
728 * If the last dirty record for this dbuf has not yet synced
729 * and its referencing the dbuf data, either:
730 * reset the reference to point to a new copy,
731 * or (if there a no active holders)
732 * just null out the current db_data pointer.
734 ASSERT(dr->dr_txg >= txg - 2);
735 if (db->db_blkid == DMU_BONUS_BLKID) {
736 /* Note that the data bufs here are zio_bufs */
737 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
738 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
739 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
740 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
741 int size = db->db.db_size;
742 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
743 spa_t *spa;
745 DB_GET_SPA(&spa, db);
746 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
747 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
748 } else {
749 dbuf_set_data(db, NULL);
753 void
754 dbuf_unoverride(dbuf_dirty_record_t *dr)
756 dmu_buf_impl_t *db = dr->dr_dbuf;
757 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
758 uint64_t txg = dr->dr_txg;
760 ASSERT(MUTEX_HELD(&db->db_mtx));
761 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
762 ASSERT(db->db_level == 0);
764 if (db->db_blkid == DMU_BONUS_BLKID ||
765 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
766 return;
768 ASSERT(db->db_data_pending != dr);
770 /* free this block */
771 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) {
772 spa_t *spa;
774 DB_GET_SPA(&spa, db);
775 zio_free(spa, txg, bp);
777 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
778 dr->dt.dl.dr_nopwrite = B_FALSE;
781 * Release the already-written buffer, so we leave it in
782 * a consistent dirty state. Note that all callers are
783 * modifying the buffer, so they will immediately do
784 * another (redundant) arc_release(). Therefore, leave
785 * the buf thawed to save the effort of freezing &
786 * immediately re-thawing it.
788 arc_release(dr->dt.dl.dr_data, db);
792 * Evict (if its unreferenced) or clear (if its referenced) any level-0
793 * data blocks in the free range, so that any future readers will find
794 * empty blocks. Also, if we happen accross any level-1 dbufs in the
795 * range that have not already been marked dirty, mark them dirty so
796 * they stay in memory.
798 void
799 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
801 dmu_buf_impl_t *db, *db_next;
802 uint64_t txg = tx->tx_txg;
803 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
804 uint64_t first_l1 = start >> epbs;
805 uint64_t last_l1 = end >> epbs;
807 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
808 end = dn->dn_maxblkid;
809 last_l1 = end >> epbs;
811 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
812 mutex_enter(&dn->dn_dbufs_mtx);
813 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
814 db_next = list_next(&dn->dn_dbufs, db);
815 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
817 if (db->db_level == 1 &&
818 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
819 mutex_enter(&db->db_mtx);
820 if (db->db_last_dirty &&
821 db->db_last_dirty->dr_txg < txg) {
822 dbuf_add_ref(db, FTAG);
823 mutex_exit(&db->db_mtx);
824 dbuf_will_dirty(db, tx);
825 dbuf_rele(db, FTAG);
826 } else {
827 mutex_exit(&db->db_mtx);
831 if (db->db_level != 0)
832 continue;
833 dprintf_dbuf(db, "found buf %s\n", "");
834 if (db->db_blkid < start || db->db_blkid > end)
835 continue;
837 /* found a level 0 buffer in the range */
838 if (dbuf_undirty(db, tx))
839 continue;
841 mutex_enter(&db->db_mtx);
842 if (db->db_state == DB_UNCACHED ||
843 db->db_state == DB_NOFILL ||
844 db->db_state == DB_EVICTING) {
845 ASSERT(db->db.db_data == NULL);
846 mutex_exit(&db->db_mtx);
847 continue;
849 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
850 /* will be handled in dbuf_read_done or dbuf_rele */
851 db->db_freed_in_flight = TRUE;
852 mutex_exit(&db->db_mtx);
853 continue;
855 if (refcount_count(&db->db_holds) == 0) {
856 ASSERT(db->db_buf);
857 dbuf_clear(db);
858 continue;
860 /* The dbuf is referenced */
862 if (db->db_last_dirty != NULL) {
863 dbuf_dirty_record_t *dr = db->db_last_dirty;
865 if (dr->dr_txg == txg) {
867 * This buffer is "in-use", re-adjust the file
868 * size to reflect that this buffer may
869 * contain new data when we sync.
871 if (db->db_blkid != DMU_SPILL_BLKID &&
872 db->db_blkid > dn->dn_maxblkid)
873 dn->dn_maxblkid = db->db_blkid;
874 dbuf_unoverride(dr);
875 } else {
877 * This dbuf is not dirty in the open context.
878 * Either uncache it (if its not referenced in
879 * the open context) or reset its contents to
880 * empty.
882 dbuf_fix_old_data(db, txg);
885 /* clear the contents if its cached */
886 if (db->db_state == DB_CACHED) {
887 ASSERT(db->db.db_data != NULL);
888 arc_release(db->db_buf, db);
889 bzero(db->db.db_data, db->db.db_size);
890 arc_buf_freeze(db->db_buf);
893 mutex_exit(&db->db_mtx);
895 mutex_exit(&dn->dn_dbufs_mtx);
898 static int
899 dbuf_block_freeable(dmu_buf_impl_t *db)
901 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
902 uint64_t birth_txg = 0;
905 * We don't need any locking to protect db_blkptr:
906 * If it's syncing, then db_last_dirty will be set
907 * so we'll ignore db_blkptr.
909 ASSERT(MUTEX_HELD(&db->db_mtx));
910 if (db->db_last_dirty)
911 birth_txg = db->db_last_dirty->dr_txg;
912 else if (db->db_blkptr)
913 birth_txg = db->db_blkptr->blk_birth;
916 * If we don't exist or are in a snapshot, we can't be freed.
917 * Don't pass the bp to dsl_dataset_block_freeable() since we
918 * are holding the db_mtx lock and might deadlock if we are
919 * prefetching a dedup-ed block.
921 if (birth_txg)
922 return (ds == NULL ||
923 dsl_dataset_block_freeable(ds, NULL, birth_txg));
924 else
925 return (FALSE);
928 void
929 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
931 arc_buf_t *buf, *obuf;
932 int osize = db->db.db_size;
933 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
934 dnode_t *dn;
936 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
938 DB_DNODE_ENTER(db);
939 dn = DB_DNODE(db);
941 /* XXX does *this* func really need the lock? */
942 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
945 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
946 * is OK, because there can be no other references to the db
947 * when we are changing its size, so no concurrent DB_FILL can
948 * be happening.
951 * XXX we should be doing a dbuf_read, checking the return
952 * value and returning that up to our callers
954 dbuf_will_dirty(db, tx);
956 /* create the data buffer for the new block */
957 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
959 /* copy old block data to the new block */
960 obuf = db->db_buf;
961 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
962 /* zero the remainder */
963 if (size > osize)
964 bzero((uint8_t *)buf->b_data + osize, size - osize);
966 mutex_enter(&db->db_mtx);
967 dbuf_set_data(db, buf);
968 VERIFY(arc_buf_remove_ref(obuf, db) == 1);
969 db->db.db_size = size;
971 if (db->db_level == 0) {
972 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
973 db->db_last_dirty->dt.dl.dr_data = buf;
975 mutex_exit(&db->db_mtx);
977 dnode_willuse_space(dn, size-osize, tx);
978 DB_DNODE_EXIT(db);
981 void
982 dbuf_release_bp(dmu_buf_impl_t *db)
984 objset_t *os;
985 zbookmark_t zb;
987 DB_GET_OBJSET(&os, db);
988 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
989 ASSERT(arc_released(os->os_phys_buf) ||
990 list_link_active(&os->os_dsl_dataset->ds_synced_link));
991 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
993 zb.zb_objset = os->os_dsl_dataset ?
994 os->os_dsl_dataset->ds_object : 0;
995 zb.zb_object = db->db.db_object;
996 zb.zb_level = db->db_level;
997 zb.zb_blkid = db->db_blkid;
998 (void) arc_release_bp(db->db_buf, db,
999 db->db_blkptr, os->os_spa, &zb);
1002 dbuf_dirty_record_t *
1003 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1005 dnode_t *dn;
1006 objset_t *os;
1007 dbuf_dirty_record_t **drp, *dr;
1008 int drop_struct_lock = FALSE;
1009 boolean_t do_free_accounting = B_FALSE;
1010 int txgoff = tx->tx_txg & TXG_MASK;
1012 ASSERT(tx->tx_txg != 0);
1013 ASSERT(!refcount_is_zero(&db->db_holds));
1014 DMU_TX_DIRTY_BUF(tx, db);
1016 DB_DNODE_ENTER(db);
1017 dn = DB_DNODE(db);
1019 * Shouldn't dirty a regular buffer in syncing context. Private
1020 * objects may be dirtied in syncing context, but only if they
1021 * were already pre-dirtied in open context.
1023 ASSERT(!dmu_tx_is_syncing(tx) ||
1024 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1025 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1026 dn->dn_objset->os_dsl_dataset == NULL);
1028 * We make this assert for private objects as well, but after we
1029 * check if we're already dirty. They are allowed to re-dirty
1030 * in syncing context.
1032 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1033 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1034 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1036 mutex_enter(&db->db_mtx);
1038 * XXX make this true for indirects too? The problem is that
1039 * transactions created with dmu_tx_create_assigned() from
1040 * syncing context don't bother holding ahead.
1042 ASSERT(db->db_level != 0 ||
1043 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1044 db->db_state == DB_NOFILL);
1046 mutex_enter(&dn->dn_mtx);
1048 * Don't set dirtyctx to SYNC if we're just modifying this as we
1049 * initialize the objset.
1051 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1052 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1053 dn->dn_dirtyctx =
1054 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1055 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1056 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1058 mutex_exit(&dn->dn_mtx);
1060 if (db->db_blkid == DMU_SPILL_BLKID)
1061 dn->dn_have_spill = B_TRUE;
1064 * If this buffer is already dirty, we're done.
1066 drp = &db->db_last_dirty;
1067 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1068 db->db.db_object == DMU_META_DNODE_OBJECT);
1069 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1070 drp = &dr->dr_next;
1071 if (dr && dr->dr_txg == tx->tx_txg) {
1072 DB_DNODE_EXIT(db);
1074 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1076 * If this buffer has already been written out,
1077 * we now need to reset its state.
1079 dbuf_unoverride(dr);
1080 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1081 db->db_state != DB_NOFILL)
1082 arc_buf_thaw(db->db_buf);
1084 mutex_exit(&db->db_mtx);
1085 return (dr);
1089 * Only valid if not already dirty.
1091 ASSERT(dn->dn_object == 0 ||
1092 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1093 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1095 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1096 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1097 dn->dn_phys->dn_nlevels > db->db_level ||
1098 dn->dn_next_nlevels[txgoff] > db->db_level ||
1099 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1100 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1103 * We should only be dirtying in syncing context if it's the
1104 * mos or we're initializing the os or it's a special object.
1105 * However, we are allowed to dirty in syncing context provided
1106 * we already dirtied it in open context. Hence we must make
1107 * this assertion only if we're not already dirty.
1109 os = dn->dn_objset;
1110 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1111 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1112 ASSERT(db->db.db_size != 0);
1114 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1116 if (db->db_blkid != DMU_BONUS_BLKID) {
1118 * Update the accounting.
1119 * Note: we delay "free accounting" until after we drop
1120 * the db_mtx. This keeps us from grabbing other locks
1121 * (and possibly deadlocking) in bp_get_dsize() while
1122 * also holding the db_mtx.
1124 dnode_willuse_space(dn, db->db.db_size, tx);
1125 do_free_accounting = dbuf_block_freeable(db);
1129 * If this buffer is dirty in an old transaction group we need
1130 * to make a copy of it so that the changes we make in this
1131 * transaction group won't leak out when we sync the older txg.
1133 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1134 if (db->db_level == 0) {
1135 void *data_old = db->db_buf;
1137 if (db->db_state != DB_NOFILL) {
1138 if (db->db_blkid == DMU_BONUS_BLKID) {
1139 dbuf_fix_old_data(db, tx->tx_txg);
1140 data_old = db->db.db_data;
1141 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1143 * Release the data buffer from the cache so
1144 * that we can modify it without impacting
1145 * possible other users of this cached data
1146 * block. Note that indirect blocks and
1147 * private objects are not released until the
1148 * syncing state (since they are only modified
1149 * then).
1151 arc_release(db->db_buf, db);
1152 dbuf_fix_old_data(db, tx->tx_txg);
1153 data_old = db->db_buf;
1155 ASSERT(data_old != NULL);
1157 dr->dt.dl.dr_data = data_old;
1158 } else {
1159 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1160 list_create(&dr->dt.di.dr_children,
1161 sizeof (dbuf_dirty_record_t),
1162 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1164 dr->dr_dbuf = db;
1165 dr->dr_txg = tx->tx_txg;
1166 dr->dr_next = *drp;
1167 *drp = dr;
1170 * We could have been freed_in_flight between the dbuf_noread
1171 * and dbuf_dirty. We win, as though the dbuf_noread() had
1172 * happened after the free.
1174 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1175 db->db_blkid != DMU_SPILL_BLKID) {
1176 mutex_enter(&dn->dn_mtx);
1177 dnode_clear_range(dn, db->db_blkid, 1, tx);
1178 mutex_exit(&dn->dn_mtx);
1179 db->db_freed_in_flight = FALSE;
1183 * This buffer is now part of this txg
1185 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1186 db->db_dirtycnt += 1;
1187 ASSERT3U(db->db_dirtycnt, <=, 3);
1189 mutex_exit(&db->db_mtx);
1191 if (db->db_blkid == DMU_BONUS_BLKID ||
1192 db->db_blkid == DMU_SPILL_BLKID) {
1193 mutex_enter(&dn->dn_mtx);
1194 ASSERT(!list_link_active(&dr->dr_dirty_node));
1195 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1196 mutex_exit(&dn->dn_mtx);
1197 dnode_setdirty(dn, tx);
1198 DB_DNODE_EXIT(db);
1199 return (dr);
1200 } else if (do_free_accounting) {
1201 blkptr_t *bp = db->db_blkptr;
1202 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1203 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1205 * This is only a guess -- if the dbuf is dirty
1206 * in a previous txg, we don't know how much
1207 * space it will use on disk yet. We should
1208 * really have the struct_rwlock to access
1209 * db_blkptr, but since this is just a guess,
1210 * it's OK if we get an odd answer.
1212 ddt_prefetch(os->os_spa, bp);
1213 dnode_willuse_space(dn, -willfree, tx);
1216 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1217 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1218 drop_struct_lock = TRUE;
1221 if (db->db_level == 0) {
1222 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1223 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1226 if (db->db_level+1 < dn->dn_nlevels) {
1227 dmu_buf_impl_t *parent = db->db_parent;
1228 dbuf_dirty_record_t *di;
1229 int parent_held = FALSE;
1231 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1232 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1234 parent = dbuf_hold_level(dn, db->db_level+1,
1235 db->db_blkid >> epbs, FTAG);
1236 ASSERT(parent != NULL);
1237 parent_held = TRUE;
1239 if (drop_struct_lock)
1240 rw_exit(&dn->dn_struct_rwlock);
1241 ASSERT3U(db->db_level+1, ==, parent->db_level);
1242 di = dbuf_dirty(parent, tx);
1243 if (parent_held)
1244 dbuf_rele(parent, FTAG);
1246 mutex_enter(&db->db_mtx);
1247 /* possible race with dbuf_undirty() */
1248 if (db->db_last_dirty == dr ||
1249 dn->dn_object == DMU_META_DNODE_OBJECT) {
1250 mutex_enter(&di->dt.di.dr_mtx);
1251 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1252 ASSERT(!list_link_active(&dr->dr_dirty_node));
1253 list_insert_tail(&di->dt.di.dr_children, dr);
1254 mutex_exit(&di->dt.di.dr_mtx);
1255 dr->dr_parent = di;
1257 mutex_exit(&db->db_mtx);
1258 } else {
1259 ASSERT(db->db_level+1 == dn->dn_nlevels);
1260 ASSERT(db->db_blkid < dn->dn_nblkptr);
1261 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1262 mutex_enter(&dn->dn_mtx);
1263 ASSERT(!list_link_active(&dr->dr_dirty_node));
1264 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1265 mutex_exit(&dn->dn_mtx);
1266 if (drop_struct_lock)
1267 rw_exit(&dn->dn_struct_rwlock);
1270 dnode_setdirty(dn, tx);
1271 DB_DNODE_EXIT(db);
1272 return (dr);
1275 static int
1276 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1278 dnode_t *dn;
1279 uint64_t txg = tx->tx_txg;
1280 dbuf_dirty_record_t *dr, **drp;
1282 ASSERT(txg != 0);
1283 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1285 mutex_enter(&db->db_mtx);
1287 * If this buffer is not dirty, we're done.
1289 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1290 if (dr->dr_txg <= txg)
1291 break;
1292 if (dr == NULL || dr->dr_txg < txg) {
1293 mutex_exit(&db->db_mtx);
1294 return (0);
1296 ASSERT(dr->dr_txg == txg);
1297 ASSERT(dr->dr_dbuf == db);
1299 DB_DNODE_ENTER(db);
1300 dn = DB_DNODE(db);
1303 * If this buffer is currently held, we cannot undirty
1304 * it, since one of the current holders may be in the
1305 * middle of an update. Note that users of dbuf_undirty()
1306 * should not place a hold on the dbuf before the call.
1307 * Also note: we can get here with a spill block, so
1308 * test for that similar to how dbuf_dirty does.
1310 if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1311 mutex_exit(&db->db_mtx);
1312 /* Make sure we don't toss this buffer at sync phase */
1313 if (db->db_blkid != DMU_SPILL_BLKID) {
1314 mutex_enter(&dn->dn_mtx);
1315 dnode_clear_range(dn, db->db_blkid, 1, tx);
1316 mutex_exit(&dn->dn_mtx);
1318 DB_DNODE_EXIT(db);
1319 return (0);
1322 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1324 ASSERT(db->db.db_size != 0);
1326 /* XXX would be nice to fix up dn_towrite_space[] */
1328 *drp = dr->dr_next;
1331 * Note that there are three places in dbuf_dirty()
1332 * where this dirty record may be put on a list.
1333 * Make sure to do a list_remove corresponding to
1334 * every one of those list_insert calls.
1336 if (dr->dr_parent) {
1337 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1338 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1339 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1340 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1341 db->db_level+1 == dn->dn_nlevels) {
1342 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1343 mutex_enter(&dn->dn_mtx);
1344 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1345 mutex_exit(&dn->dn_mtx);
1347 DB_DNODE_EXIT(db);
1349 if (db->db_level == 0) {
1350 if (db->db_state != DB_NOFILL) {
1351 dbuf_unoverride(dr);
1353 ASSERT(db->db_buf != NULL);
1354 ASSERT(dr->dt.dl.dr_data != NULL);
1355 if (dr->dt.dl.dr_data != db->db_buf)
1356 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
1357 db) == 1);
1359 } else {
1360 ASSERT(db->db_buf != NULL);
1361 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1362 mutex_destroy(&dr->dt.di.dr_mtx);
1363 list_destroy(&dr->dt.di.dr_children);
1365 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1367 ASSERT(db->db_dirtycnt > 0);
1368 db->db_dirtycnt -= 1;
1370 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1371 arc_buf_t *buf = db->db_buf;
1373 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1374 dbuf_set_data(db, NULL);
1375 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1376 dbuf_evict(db);
1377 return (1);
1380 mutex_exit(&db->db_mtx);
1381 return (0);
1384 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1385 void
1386 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1388 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1390 ASSERT(tx->tx_txg != 0);
1391 ASSERT(!refcount_is_zero(&db->db_holds));
1393 DB_DNODE_ENTER(db);
1394 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1395 rf |= DB_RF_HAVESTRUCT;
1396 DB_DNODE_EXIT(db);
1397 (void) dbuf_read(db, NULL, rf);
1398 (void) dbuf_dirty(db, tx);
1401 void
1402 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1404 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1406 db->db_state = DB_NOFILL;
1408 dmu_buf_will_fill(db_fake, tx);
1411 void
1412 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1414 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1416 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1417 ASSERT(tx->tx_txg != 0);
1418 ASSERT(db->db_level == 0);
1419 ASSERT(!refcount_is_zero(&db->db_holds));
1421 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1422 dmu_tx_private_ok(tx));
1424 dbuf_noread(db);
1425 (void) dbuf_dirty(db, tx);
1428 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1429 /* ARGSUSED */
1430 void
1431 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1433 mutex_enter(&db->db_mtx);
1434 DBUF_VERIFY(db);
1436 if (db->db_state == DB_FILL) {
1437 if (db->db_level == 0 && db->db_freed_in_flight) {
1438 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1439 /* we were freed while filling */
1440 /* XXX dbuf_undirty? */
1441 bzero(db->db.db_data, db->db.db_size);
1442 db->db_freed_in_flight = FALSE;
1444 db->db_state = DB_CACHED;
1445 cv_broadcast(&db->db_changed);
1447 mutex_exit(&db->db_mtx);
1451 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1452 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1454 void
1455 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1457 ASSERT(!refcount_is_zero(&db->db_holds));
1458 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1459 ASSERT(db->db_level == 0);
1460 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1461 ASSERT(buf != NULL);
1462 ASSERT(arc_buf_size(buf) == db->db.db_size);
1463 ASSERT(tx->tx_txg != 0);
1465 arc_return_buf(buf, db);
1466 ASSERT(arc_released(buf));
1468 mutex_enter(&db->db_mtx);
1470 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1471 cv_wait(&db->db_changed, &db->db_mtx);
1473 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1475 if (db->db_state == DB_CACHED &&
1476 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1477 mutex_exit(&db->db_mtx);
1478 (void) dbuf_dirty(db, tx);
1479 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1480 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1481 xuio_stat_wbuf_copied();
1482 return;
1485 xuio_stat_wbuf_nocopy();
1486 if (db->db_state == DB_CACHED) {
1487 dbuf_dirty_record_t *dr = db->db_last_dirty;
1489 ASSERT(db->db_buf != NULL);
1490 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1491 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1492 if (!arc_released(db->db_buf)) {
1493 ASSERT(dr->dt.dl.dr_override_state ==
1494 DR_OVERRIDDEN);
1495 arc_release(db->db_buf, db);
1497 dr->dt.dl.dr_data = buf;
1498 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1499 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1500 arc_release(db->db_buf, db);
1501 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1503 db->db_buf = NULL;
1505 ASSERT(db->db_buf == NULL);
1506 dbuf_set_data(db, buf);
1507 db->db_state = DB_FILL;
1508 mutex_exit(&db->db_mtx);
1509 (void) dbuf_dirty(db, tx);
1510 dbuf_fill_done(db, tx);
1514 * "Clear" the contents of this dbuf. This will mark the dbuf
1515 * EVICTING and clear *most* of its references. Unfortunetely,
1516 * when we are not holding the dn_dbufs_mtx, we can't clear the
1517 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1518 * in this case. For callers from the DMU we will usually see:
1519 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1520 * For the arc callback, we will usually see:
1521 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1522 * Sometimes, though, we will get a mix of these two:
1523 * DMU: dbuf_clear()->arc_buf_evict()
1524 * ARC: dbuf_do_evict()->dbuf_destroy()
1526 void
1527 dbuf_clear(dmu_buf_impl_t *db)
1529 dnode_t *dn;
1530 dmu_buf_impl_t *parent = db->db_parent;
1531 dmu_buf_impl_t *dndb;
1532 int dbuf_gone = FALSE;
1534 ASSERT(MUTEX_HELD(&db->db_mtx));
1535 ASSERT(refcount_is_zero(&db->db_holds));
1537 dbuf_evict_user(db);
1539 if (db->db_state == DB_CACHED) {
1540 ASSERT(db->db.db_data != NULL);
1541 if (db->db_blkid == DMU_BONUS_BLKID) {
1542 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1543 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1545 db->db.db_data = NULL;
1546 db->db_state = DB_UNCACHED;
1549 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1550 ASSERT(db->db_data_pending == NULL);
1552 db->db_state = DB_EVICTING;
1553 db->db_blkptr = NULL;
1555 DB_DNODE_ENTER(db);
1556 dn = DB_DNODE(db);
1557 dndb = dn->dn_dbuf;
1558 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1559 list_remove(&dn->dn_dbufs, db);
1560 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1561 membar_producer();
1562 DB_DNODE_EXIT(db);
1564 * Decrementing the dbuf count means that the hold corresponding
1565 * to the removed dbuf is no longer discounted in dnode_move(),
1566 * so the dnode cannot be moved until after we release the hold.
1567 * The membar_producer() ensures visibility of the decremented
1568 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1569 * release any lock.
1571 dnode_rele(dn, db);
1572 db->db_dnode_handle = NULL;
1573 } else {
1574 DB_DNODE_EXIT(db);
1577 if (db->db_buf)
1578 dbuf_gone = arc_buf_evict(db->db_buf);
1580 if (!dbuf_gone)
1581 mutex_exit(&db->db_mtx);
1584 * If this dbuf is referenced from an indirect dbuf,
1585 * decrement the ref count on the indirect dbuf.
1587 if (parent && parent != dndb)
1588 dbuf_rele(parent, db);
1591 static int
1592 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1593 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1595 int nlevels, epbs;
1597 *parentp = NULL;
1598 *bpp = NULL;
1600 ASSERT(blkid != DMU_BONUS_BLKID);
1602 if (blkid == DMU_SPILL_BLKID) {
1603 mutex_enter(&dn->dn_mtx);
1604 if (dn->dn_have_spill &&
1605 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1606 *bpp = &dn->dn_phys->dn_spill;
1607 else
1608 *bpp = NULL;
1609 dbuf_add_ref(dn->dn_dbuf, NULL);
1610 *parentp = dn->dn_dbuf;
1611 mutex_exit(&dn->dn_mtx);
1612 return (0);
1615 if (dn->dn_phys->dn_nlevels == 0)
1616 nlevels = 1;
1617 else
1618 nlevels = dn->dn_phys->dn_nlevels;
1620 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1622 ASSERT3U(level * epbs, <, 64);
1623 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1624 if (level >= nlevels ||
1625 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1626 /* the buffer has no parent yet */
1627 return (ENOENT);
1628 } else if (level < nlevels-1) {
1629 /* this block is referenced from an indirect block */
1630 int err = dbuf_hold_impl(dn, level+1,
1631 blkid >> epbs, fail_sparse, NULL, parentp);
1632 if (err)
1633 return (err);
1634 err = dbuf_read(*parentp, NULL,
1635 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1636 if (err) {
1637 dbuf_rele(*parentp, NULL);
1638 *parentp = NULL;
1639 return (err);
1641 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1642 (blkid & ((1ULL << epbs) - 1));
1643 return (0);
1644 } else {
1645 /* the block is referenced from the dnode */
1646 ASSERT3U(level, ==, nlevels-1);
1647 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1648 blkid < dn->dn_phys->dn_nblkptr);
1649 if (dn->dn_dbuf) {
1650 dbuf_add_ref(dn->dn_dbuf, NULL);
1651 *parentp = dn->dn_dbuf;
1653 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1654 return (0);
1658 static dmu_buf_impl_t *
1659 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1660 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1662 objset_t *os = dn->dn_objset;
1663 dmu_buf_impl_t *db, *odb;
1665 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1666 ASSERT(dn->dn_type != DMU_OT_NONE);
1668 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1670 db->db_objset = os;
1671 db->db.db_object = dn->dn_object;
1672 db->db_level = level;
1673 db->db_blkid = blkid;
1674 db->db_last_dirty = NULL;
1675 db->db_dirtycnt = 0;
1676 db->db_dnode_handle = dn->dn_handle;
1677 db->db_parent = parent;
1678 db->db_blkptr = blkptr;
1680 db->db_user_ptr = NULL;
1681 db->db_user_data_ptr_ptr = NULL;
1682 db->db_evict_func = NULL;
1683 db->db_immediate_evict = 0;
1684 db->db_freed_in_flight = 0;
1686 if (blkid == DMU_BONUS_BLKID) {
1687 ASSERT3P(parent, ==, dn->dn_dbuf);
1688 db->db.db_size = DN_MAX_BONUSLEN -
1689 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1690 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1691 db->db.db_offset = DMU_BONUS_BLKID;
1692 db->db_state = DB_UNCACHED;
1693 /* the bonus dbuf is not placed in the hash table */
1694 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1695 return (db);
1696 } else if (blkid == DMU_SPILL_BLKID) {
1697 db->db.db_size = (blkptr != NULL) ?
1698 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1699 db->db.db_offset = 0;
1700 } else {
1701 int blocksize =
1702 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1703 db->db.db_size = blocksize;
1704 db->db.db_offset = db->db_blkid * blocksize;
1708 * Hold the dn_dbufs_mtx while we get the new dbuf
1709 * in the hash table *and* added to the dbufs list.
1710 * This prevents a possible deadlock with someone
1711 * trying to look up this dbuf before its added to the
1712 * dn_dbufs list.
1714 mutex_enter(&dn->dn_dbufs_mtx);
1715 db->db_state = DB_EVICTING;
1716 if ((odb = dbuf_hash_insert(db)) != NULL) {
1717 /* someone else inserted it first */
1718 kmem_cache_free(dbuf_cache, db);
1719 mutex_exit(&dn->dn_dbufs_mtx);
1720 return (odb);
1722 list_insert_head(&dn->dn_dbufs, db);
1723 db->db_state = DB_UNCACHED;
1724 mutex_exit(&dn->dn_dbufs_mtx);
1725 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1727 if (parent && parent != dn->dn_dbuf)
1728 dbuf_add_ref(parent, db);
1730 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1731 refcount_count(&dn->dn_holds) > 0);
1732 (void) refcount_add(&dn->dn_holds, db);
1733 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1735 dprintf_dbuf(db, "db=%p\n", db);
1737 return (db);
1740 static int
1741 dbuf_do_evict(void *private)
1743 arc_buf_t *buf = private;
1744 dmu_buf_impl_t *db = buf->b_private;
1746 if (!MUTEX_HELD(&db->db_mtx))
1747 mutex_enter(&db->db_mtx);
1749 ASSERT(refcount_is_zero(&db->db_holds));
1751 if (db->db_state != DB_EVICTING) {
1752 ASSERT(db->db_state == DB_CACHED);
1753 DBUF_VERIFY(db);
1754 db->db_buf = NULL;
1755 dbuf_evict(db);
1756 } else {
1757 mutex_exit(&db->db_mtx);
1758 dbuf_destroy(db);
1760 return (0);
1763 static void
1764 dbuf_destroy(dmu_buf_impl_t *db)
1766 ASSERT(refcount_is_zero(&db->db_holds));
1768 if (db->db_blkid != DMU_BONUS_BLKID) {
1770 * If this dbuf is still on the dn_dbufs list,
1771 * remove it from that list.
1773 if (db->db_dnode_handle != NULL) {
1774 dnode_t *dn;
1776 DB_DNODE_ENTER(db);
1777 dn = DB_DNODE(db);
1778 mutex_enter(&dn->dn_dbufs_mtx);
1779 list_remove(&dn->dn_dbufs, db);
1780 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1781 mutex_exit(&dn->dn_dbufs_mtx);
1782 DB_DNODE_EXIT(db);
1784 * Decrementing the dbuf count means that the hold
1785 * corresponding to the removed dbuf is no longer
1786 * discounted in dnode_move(), so the dnode cannot be
1787 * moved until after we release the hold.
1789 dnode_rele(dn, db);
1790 db->db_dnode_handle = NULL;
1792 dbuf_hash_remove(db);
1794 db->db_parent = NULL;
1795 db->db_buf = NULL;
1797 ASSERT(!list_link_active(&db->db_link));
1798 ASSERT(db->db.db_data == NULL);
1799 ASSERT(db->db_hash_next == NULL);
1800 ASSERT(db->db_blkptr == NULL);
1801 ASSERT(db->db_data_pending == NULL);
1803 kmem_cache_free(dbuf_cache, db);
1804 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1807 void
1808 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1810 dmu_buf_impl_t *db = NULL;
1811 blkptr_t *bp = NULL;
1813 ASSERT(blkid != DMU_BONUS_BLKID);
1814 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1816 if (dnode_block_freed(dn, blkid))
1817 return;
1819 /* dbuf_find() returns with db_mtx held */
1820 if (db = dbuf_find(dn, 0, blkid)) {
1822 * This dbuf is already in the cache. We assume that
1823 * it is already CACHED, or else about to be either
1824 * read or filled.
1826 mutex_exit(&db->db_mtx);
1827 return;
1830 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1831 if (bp && !BP_IS_HOLE(bp)) {
1832 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1833 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1834 arc_buf_t *pbuf;
1835 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1836 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1837 zbookmark_t zb;
1839 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1840 dn->dn_object, 0, blkid);
1842 if (db)
1843 pbuf = db->db_buf;
1844 else
1845 pbuf = dn->dn_objset->os_phys_buf;
1847 (void) dsl_read(NULL, dn->dn_objset->os_spa,
1848 bp, pbuf, NULL, NULL, priority,
1849 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1850 &aflags, &zb);
1852 if (db)
1853 dbuf_rele(db, NULL);
1858 * Returns with db_holds incremented, and db_mtx not held.
1859 * Note: dn_struct_rwlock must be held.
1862 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1863 void *tag, dmu_buf_impl_t **dbp)
1865 dmu_buf_impl_t *db, *parent = NULL;
1867 ASSERT(blkid != DMU_BONUS_BLKID);
1868 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1869 ASSERT3U(dn->dn_nlevels, >, level);
1871 *dbp = NULL;
1872 top:
1873 /* dbuf_find() returns with db_mtx held */
1874 db = dbuf_find(dn, level, blkid);
1876 if (db == NULL) {
1877 blkptr_t *bp = NULL;
1878 int err;
1880 ASSERT3P(parent, ==, NULL);
1881 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1882 if (fail_sparse) {
1883 if (err == 0 && bp && BP_IS_HOLE(bp))
1884 err = ENOENT;
1885 if (err) {
1886 if (parent)
1887 dbuf_rele(parent, NULL);
1888 return (err);
1891 if (err && err != ENOENT)
1892 return (err);
1893 db = dbuf_create(dn, level, blkid, parent, bp);
1896 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1897 arc_buf_add_ref(db->db_buf, db);
1898 if (db->db_buf->b_data == NULL) {
1899 dbuf_clear(db);
1900 if (parent) {
1901 dbuf_rele(parent, NULL);
1902 parent = NULL;
1904 goto top;
1906 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1909 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1912 * If this buffer is currently syncing out, and we are are
1913 * still referencing it from db_data, we need to make a copy
1914 * of it in case we decide we want to dirty it again in this txg.
1916 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1917 dn->dn_object != DMU_META_DNODE_OBJECT &&
1918 db->db_state == DB_CACHED && db->db_data_pending) {
1919 dbuf_dirty_record_t *dr = db->db_data_pending;
1921 if (dr->dt.dl.dr_data == db->db_buf) {
1922 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1924 dbuf_set_data(db,
1925 arc_buf_alloc(dn->dn_objset->os_spa,
1926 db->db.db_size, db, type));
1927 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1928 db->db.db_size);
1932 (void) refcount_add(&db->db_holds, tag);
1933 dbuf_update_data(db);
1934 DBUF_VERIFY(db);
1935 mutex_exit(&db->db_mtx);
1937 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1938 if (parent)
1939 dbuf_rele(parent, NULL);
1941 ASSERT3P(DB_DNODE(db), ==, dn);
1942 ASSERT3U(db->db_blkid, ==, blkid);
1943 ASSERT3U(db->db_level, ==, level);
1944 *dbp = db;
1946 return (0);
1949 dmu_buf_impl_t *
1950 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1952 dmu_buf_impl_t *db;
1953 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1954 return (err ? NULL : db);
1957 dmu_buf_impl_t *
1958 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1960 dmu_buf_impl_t *db;
1961 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1962 return (err ? NULL : db);
1965 void
1966 dbuf_create_bonus(dnode_t *dn)
1968 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1970 ASSERT(dn->dn_bonus == NULL);
1971 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
1975 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
1977 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1978 dnode_t *dn;
1980 if (db->db_blkid != DMU_SPILL_BLKID)
1981 return (ENOTSUP);
1982 if (blksz == 0)
1983 blksz = SPA_MINBLOCKSIZE;
1984 if (blksz > SPA_MAXBLOCKSIZE)
1985 blksz = SPA_MAXBLOCKSIZE;
1986 else
1987 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
1989 DB_DNODE_ENTER(db);
1990 dn = DB_DNODE(db);
1991 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1992 dbuf_new_size(db, blksz, tx);
1993 rw_exit(&dn->dn_struct_rwlock);
1994 DB_DNODE_EXIT(db);
1996 return (0);
1999 void
2000 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2002 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2005 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2006 void
2007 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2009 int64_t holds = refcount_add(&db->db_holds, tag);
2010 ASSERT(holds > 1);
2014 * If you call dbuf_rele() you had better not be referencing the dnode handle
2015 * unless you have some other direct or indirect hold on the dnode. (An indirect
2016 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2017 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2018 * dnode's parent dbuf evicting its dnode handles.
2020 #pragma weak dmu_buf_rele = dbuf_rele
2021 void
2022 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2024 mutex_enter(&db->db_mtx);
2025 dbuf_rele_and_unlock(db, tag);
2029 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2030 * db_dirtycnt and db_holds to be updated atomically.
2032 void
2033 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2035 int64_t holds;
2037 ASSERT(MUTEX_HELD(&db->db_mtx));
2038 DBUF_VERIFY(db);
2041 * Remove the reference to the dbuf before removing its hold on the
2042 * dnode so we can guarantee in dnode_move() that a referenced bonus
2043 * buffer has a corresponding dnode hold.
2045 holds = refcount_remove(&db->db_holds, tag);
2046 ASSERT(holds >= 0);
2049 * We can't freeze indirects if there is a possibility that they
2050 * may be modified in the current syncing context.
2052 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2053 arc_buf_freeze(db->db_buf);
2055 if (holds == db->db_dirtycnt &&
2056 db->db_level == 0 && db->db_immediate_evict)
2057 dbuf_evict_user(db);
2059 if (holds == 0) {
2060 if (db->db_blkid == DMU_BONUS_BLKID) {
2061 mutex_exit(&db->db_mtx);
2064 * If the dnode moves here, we cannot cross this barrier
2065 * until the move completes.
2067 DB_DNODE_ENTER(db);
2068 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2069 DB_DNODE_EXIT(db);
2071 * The bonus buffer's dnode hold is no longer discounted
2072 * in dnode_move(). The dnode cannot move until after
2073 * the dnode_rele().
2075 dnode_rele(DB_DNODE(db), db);
2076 } else if (db->db_buf == NULL) {
2078 * This is a special case: we never associated this
2079 * dbuf with any data allocated from the ARC.
2081 ASSERT(db->db_state == DB_UNCACHED ||
2082 db->db_state == DB_NOFILL);
2083 dbuf_evict(db);
2084 } else if (arc_released(db->db_buf)) {
2085 arc_buf_t *buf = db->db_buf;
2087 * This dbuf has anonymous data associated with it.
2089 dbuf_set_data(db, NULL);
2090 VERIFY(arc_buf_remove_ref(buf, db) == 1);
2091 dbuf_evict(db);
2092 } else {
2093 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
2096 * A dbuf will be eligible for eviction if either the
2097 * 'primarycache' property is set or a duplicate
2098 * copy of this buffer is already cached in the arc.
2100 * In the case of the 'primarycache' a buffer
2101 * is considered for eviction if it matches the
2102 * criteria set in the property.
2104 * To decide if our buffer is considered a
2105 * duplicate, we must call into the arc to determine
2106 * if multiple buffers are referencing the same
2107 * block on-disk. If so, then we simply evict
2108 * ourselves.
2110 if (!DBUF_IS_CACHEABLE(db) ||
2111 arc_buf_eviction_needed(db->db_buf))
2112 dbuf_clear(db);
2113 else
2114 mutex_exit(&db->db_mtx);
2116 } else {
2117 mutex_exit(&db->db_mtx);
2121 #pragma weak dmu_buf_refcount = dbuf_refcount
2122 uint64_t
2123 dbuf_refcount(dmu_buf_impl_t *db)
2125 return (refcount_count(&db->db_holds));
2128 void *
2129 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2130 dmu_buf_evict_func_t *evict_func)
2132 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2133 user_data_ptr_ptr, evict_func));
2136 void *
2137 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2138 dmu_buf_evict_func_t *evict_func)
2140 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2142 db->db_immediate_evict = TRUE;
2143 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2144 user_data_ptr_ptr, evict_func));
2147 void *
2148 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2149 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2151 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2152 ASSERT(db->db_level == 0);
2154 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2156 mutex_enter(&db->db_mtx);
2158 if (db->db_user_ptr == old_user_ptr) {
2159 db->db_user_ptr = user_ptr;
2160 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2161 db->db_evict_func = evict_func;
2163 dbuf_update_data(db);
2164 } else {
2165 old_user_ptr = db->db_user_ptr;
2168 mutex_exit(&db->db_mtx);
2169 return (old_user_ptr);
2172 void *
2173 dmu_buf_get_user(dmu_buf_t *db_fake)
2175 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2176 ASSERT(!refcount_is_zero(&db->db_holds));
2178 return (db->db_user_ptr);
2181 boolean_t
2182 dmu_buf_freeable(dmu_buf_t *dbuf)
2184 boolean_t res = B_FALSE;
2185 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2187 if (db->db_blkptr)
2188 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2189 db->db_blkptr, db->db_blkptr->blk_birth);
2191 return (res);
2194 blkptr_t *
2195 dmu_buf_get_blkptr(dmu_buf_t *db)
2197 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2198 return (dbi->db_blkptr);
2201 static void
2202 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2204 /* ASSERT(dmu_tx_is_syncing(tx) */
2205 ASSERT(MUTEX_HELD(&db->db_mtx));
2207 if (db->db_blkptr != NULL)
2208 return;
2210 if (db->db_blkid == DMU_SPILL_BLKID) {
2211 db->db_blkptr = &dn->dn_phys->dn_spill;
2212 BP_ZERO(db->db_blkptr);
2213 return;
2215 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2217 * This buffer was allocated at a time when there was
2218 * no available blkptrs from the dnode, or it was
2219 * inappropriate to hook it in (i.e., nlevels mis-match).
2221 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2222 ASSERT(db->db_parent == NULL);
2223 db->db_parent = dn->dn_dbuf;
2224 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2225 DBUF_VERIFY(db);
2226 } else {
2227 dmu_buf_impl_t *parent = db->db_parent;
2228 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2230 ASSERT(dn->dn_phys->dn_nlevels > 1);
2231 if (parent == NULL) {
2232 mutex_exit(&db->db_mtx);
2233 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2234 (void) dbuf_hold_impl(dn, db->db_level+1,
2235 db->db_blkid >> epbs, FALSE, db, &parent);
2236 rw_exit(&dn->dn_struct_rwlock);
2237 mutex_enter(&db->db_mtx);
2238 db->db_parent = parent;
2240 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2241 (db->db_blkid & ((1ULL << epbs) - 1));
2242 DBUF_VERIFY(db);
2246 static void
2247 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2249 dmu_buf_impl_t *db = dr->dr_dbuf;
2250 dnode_t *dn;
2251 zio_t *zio;
2253 ASSERT(dmu_tx_is_syncing(tx));
2255 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2257 mutex_enter(&db->db_mtx);
2259 ASSERT(db->db_level > 0);
2260 DBUF_VERIFY(db);
2262 if (db->db_buf == NULL) {
2263 mutex_exit(&db->db_mtx);
2264 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2265 mutex_enter(&db->db_mtx);
2267 ASSERT3U(db->db_state, ==, DB_CACHED);
2268 ASSERT(db->db_buf != NULL);
2270 DB_DNODE_ENTER(db);
2271 dn = DB_DNODE(db);
2272 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2273 dbuf_check_blkptr(dn, db);
2274 DB_DNODE_EXIT(db);
2276 db->db_data_pending = dr;
2278 mutex_exit(&db->db_mtx);
2279 dbuf_write(dr, db->db_buf, tx);
2281 zio = dr->dr_zio;
2282 mutex_enter(&dr->dt.di.dr_mtx);
2283 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2284 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2285 mutex_exit(&dr->dt.di.dr_mtx);
2286 zio_nowait(zio);
2289 static void
2290 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2292 arc_buf_t **datap = &dr->dt.dl.dr_data;
2293 dmu_buf_impl_t *db = dr->dr_dbuf;
2294 dnode_t *dn;
2295 objset_t *os;
2296 uint64_t txg = tx->tx_txg;
2298 ASSERT(dmu_tx_is_syncing(tx));
2300 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2302 mutex_enter(&db->db_mtx);
2304 * To be synced, we must be dirtied. But we
2305 * might have been freed after the dirty.
2307 if (db->db_state == DB_UNCACHED) {
2308 /* This buffer has been freed since it was dirtied */
2309 ASSERT(db->db.db_data == NULL);
2310 } else if (db->db_state == DB_FILL) {
2311 /* This buffer was freed and is now being re-filled */
2312 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2313 } else {
2314 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2316 DBUF_VERIFY(db);
2318 DB_DNODE_ENTER(db);
2319 dn = DB_DNODE(db);
2321 if (db->db_blkid == DMU_SPILL_BLKID) {
2322 mutex_enter(&dn->dn_mtx);
2323 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2324 mutex_exit(&dn->dn_mtx);
2328 * If this is a bonus buffer, simply copy the bonus data into the
2329 * dnode. It will be written out when the dnode is synced (and it
2330 * will be synced, since it must have been dirty for dbuf_sync to
2331 * be called).
2333 if (db->db_blkid == DMU_BONUS_BLKID) {
2334 dbuf_dirty_record_t **drp;
2336 ASSERT(*datap != NULL);
2337 ASSERT0(db->db_level);
2338 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2339 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2340 DB_DNODE_EXIT(db);
2342 if (*datap != db->db.db_data) {
2343 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2344 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2346 db->db_data_pending = NULL;
2347 drp = &db->db_last_dirty;
2348 while (*drp != dr)
2349 drp = &(*drp)->dr_next;
2350 ASSERT(dr->dr_next == NULL);
2351 ASSERT(dr->dr_dbuf == db);
2352 *drp = dr->dr_next;
2353 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2354 ASSERT(db->db_dirtycnt > 0);
2355 db->db_dirtycnt -= 1;
2356 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2357 return;
2360 os = dn->dn_objset;
2363 * This function may have dropped the db_mtx lock allowing a dmu_sync
2364 * operation to sneak in. As a result, we need to ensure that we
2365 * don't check the dr_override_state until we have returned from
2366 * dbuf_check_blkptr.
2368 dbuf_check_blkptr(dn, db);
2371 * If this buffer is in the middle of an immediate write,
2372 * wait for the synchronous IO to complete.
2374 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2375 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2376 cv_wait(&db->db_changed, &db->db_mtx);
2377 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2380 if (db->db_state != DB_NOFILL &&
2381 dn->dn_object != DMU_META_DNODE_OBJECT &&
2382 refcount_count(&db->db_holds) > 1 &&
2383 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2384 *datap == db->db_buf) {
2386 * If this buffer is currently "in use" (i.e., there
2387 * are active holds and db_data still references it),
2388 * then make a copy before we start the write so that
2389 * any modifications from the open txg will not leak
2390 * into this write.
2392 * NOTE: this copy does not need to be made for
2393 * objects only modified in the syncing context (e.g.
2394 * DNONE_DNODE blocks).
2396 int blksz = arc_buf_size(*datap);
2397 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2398 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2399 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2401 db->db_data_pending = dr;
2403 mutex_exit(&db->db_mtx);
2405 dbuf_write(dr, *datap, tx);
2407 ASSERT(!list_link_active(&dr->dr_dirty_node));
2408 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2409 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2410 DB_DNODE_EXIT(db);
2411 } else {
2413 * Although zio_nowait() does not "wait for an IO", it does
2414 * initiate the IO. If this is an empty write it seems plausible
2415 * that the IO could actually be completed before the nowait
2416 * returns. We need to DB_DNODE_EXIT() first in case
2417 * zio_nowait() invalidates the dbuf.
2419 DB_DNODE_EXIT(db);
2420 zio_nowait(dr->dr_zio);
2424 void
2425 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2427 dbuf_dirty_record_t *dr;
2429 while (dr = list_head(list)) {
2430 if (dr->dr_zio != NULL) {
2432 * If we find an already initialized zio then we
2433 * are processing the meta-dnode, and we have finished.
2434 * The dbufs for all dnodes are put back on the list
2435 * during processing, so that we can zio_wait()
2436 * these IOs after initiating all child IOs.
2438 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2439 DMU_META_DNODE_OBJECT);
2440 break;
2442 list_remove(list, dr);
2443 if (dr->dr_dbuf->db_level > 0)
2444 dbuf_sync_indirect(dr, tx);
2445 else
2446 dbuf_sync_leaf(dr, tx);
2450 /* ARGSUSED */
2451 static void
2452 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2454 dmu_buf_impl_t *db = vdb;
2455 dnode_t *dn;
2456 blkptr_t *bp = zio->io_bp;
2457 blkptr_t *bp_orig = &zio->io_bp_orig;
2458 spa_t *spa = zio->io_spa;
2459 int64_t delta;
2460 uint64_t fill = 0;
2461 int i;
2463 ASSERT(db->db_blkptr == bp);
2465 DB_DNODE_ENTER(db);
2466 dn = DB_DNODE(db);
2467 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2468 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2469 zio->io_prev_space_delta = delta;
2471 if (BP_IS_HOLE(bp)) {
2472 ASSERT(bp->blk_fill == 0);
2473 DB_DNODE_EXIT(db);
2474 return;
2477 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2478 BP_GET_TYPE(bp) == dn->dn_type) ||
2479 (db->db_blkid == DMU_SPILL_BLKID &&
2480 BP_GET_TYPE(bp) == dn->dn_bonustype));
2481 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2483 mutex_enter(&db->db_mtx);
2485 #ifdef ZFS_DEBUG
2486 if (db->db_blkid == DMU_SPILL_BLKID) {
2487 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2488 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2489 db->db_blkptr == &dn->dn_phys->dn_spill);
2491 #endif
2493 if (db->db_level == 0) {
2494 mutex_enter(&dn->dn_mtx);
2495 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2496 db->db_blkid != DMU_SPILL_BLKID)
2497 dn->dn_phys->dn_maxblkid = db->db_blkid;
2498 mutex_exit(&dn->dn_mtx);
2500 if (dn->dn_type == DMU_OT_DNODE) {
2501 dnode_phys_t *dnp = db->db.db_data;
2502 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2503 i--, dnp++) {
2504 if (dnp->dn_type != DMU_OT_NONE)
2505 fill++;
2507 } else {
2508 fill = 1;
2510 } else {
2511 blkptr_t *ibp = db->db.db_data;
2512 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2513 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2514 if (BP_IS_HOLE(ibp))
2515 continue;
2516 fill += ibp->blk_fill;
2519 DB_DNODE_EXIT(db);
2521 bp->blk_fill = fill;
2523 mutex_exit(&db->db_mtx);
2526 /* ARGSUSED */
2527 static void
2528 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2530 dmu_buf_impl_t *db = vdb;
2531 blkptr_t *bp = zio->io_bp;
2532 blkptr_t *bp_orig = &zio->io_bp_orig;
2533 uint64_t txg = zio->io_txg;
2534 dbuf_dirty_record_t **drp, *dr;
2536 ASSERT0(zio->io_error);
2537 ASSERT(db->db_blkptr == bp);
2540 * For nopwrites and rewrites we ensure that the bp matches our
2541 * original and bypass all the accounting.
2543 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2544 ASSERT(BP_EQUAL(bp, bp_orig));
2545 } else {
2546 objset_t *os;
2547 dsl_dataset_t *ds;
2548 dmu_tx_t *tx;
2550 DB_GET_OBJSET(&os, db);
2551 ds = os->os_dsl_dataset;
2552 tx = os->os_synctx;
2554 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2555 dsl_dataset_block_born(ds, bp, tx);
2558 mutex_enter(&db->db_mtx);
2560 DBUF_VERIFY(db);
2562 drp = &db->db_last_dirty;
2563 while ((dr = *drp) != db->db_data_pending)
2564 drp = &dr->dr_next;
2565 ASSERT(!list_link_active(&dr->dr_dirty_node));
2566 ASSERT(dr->dr_txg == txg);
2567 ASSERT(dr->dr_dbuf == db);
2568 ASSERT(dr->dr_next == NULL);
2569 *drp = dr->dr_next;
2571 #ifdef ZFS_DEBUG
2572 if (db->db_blkid == DMU_SPILL_BLKID) {
2573 dnode_t *dn;
2575 DB_DNODE_ENTER(db);
2576 dn = DB_DNODE(db);
2577 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2578 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2579 db->db_blkptr == &dn->dn_phys->dn_spill);
2580 DB_DNODE_EXIT(db);
2582 #endif
2584 if (db->db_level == 0) {
2585 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2586 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2587 if (db->db_state != DB_NOFILL) {
2588 if (dr->dt.dl.dr_data != db->db_buf)
2589 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2590 db) == 1);
2591 else if (!arc_released(db->db_buf))
2592 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2594 } else {
2595 dnode_t *dn;
2597 DB_DNODE_ENTER(db);
2598 dn = DB_DNODE(db);
2599 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2600 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2601 if (!BP_IS_HOLE(db->db_blkptr)) {
2602 int epbs =
2603 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2604 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2605 db->db.db_size);
2606 ASSERT3U(dn->dn_phys->dn_maxblkid
2607 >> (db->db_level * epbs), >=, db->db_blkid);
2608 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2610 DB_DNODE_EXIT(db);
2611 mutex_destroy(&dr->dt.di.dr_mtx);
2612 list_destroy(&dr->dt.di.dr_children);
2614 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2616 cv_broadcast(&db->db_changed);
2617 ASSERT(db->db_dirtycnt > 0);
2618 db->db_dirtycnt -= 1;
2619 db->db_data_pending = NULL;
2620 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2623 static void
2624 dbuf_write_nofill_ready(zio_t *zio)
2626 dbuf_write_ready(zio, NULL, zio->io_private);
2629 static void
2630 dbuf_write_nofill_done(zio_t *zio)
2632 dbuf_write_done(zio, NULL, zio->io_private);
2635 static void
2636 dbuf_write_override_ready(zio_t *zio)
2638 dbuf_dirty_record_t *dr = zio->io_private;
2639 dmu_buf_impl_t *db = dr->dr_dbuf;
2641 dbuf_write_ready(zio, NULL, db);
2644 static void
2645 dbuf_write_override_done(zio_t *zio)
2647 dbuf_dirty_record_t *dr = zio->io_private;
2648 dmu_buf_impl_t *db = dr->dr_dbuf;
2649 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2651 mutex_enter(&db->db_mtx);
2652 if (!BP_EQUAL(zio->io_bp, obp)) {
2653 if (!BP_IS_HOLE(obp))
2654 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2655 arc_release(dr->dt.dl.dr_data, db);
2657 mutex_exit(&db->db_mtx);
2659 dbuf_write_done(zio, NULL, db);
2662 static void
2663 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2665 dmu_buf_impl_t *db = dr->dr_dbuf;
2666 dnode_t *dn;
2667 objset_t *os;
2668 dmu_buf_impl_t *parent = db->db_parent;
2669 uint64_t txg = tx->tx_txg;
2670 zbookmark_t zb;
2671 zio_prop_t zp;
2672 zio_t *zio;
2673 int wp_flag = 0;
2675 DB_DNODE_ENTER(db);
2676 dn = DB_DNODE(db);
2677 os = dn->dn_objset;
2679 if (db->db_state != DB_NOFILL) {
2680 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2682 * Private object buffers are released here rather
2683 * than in dbuf_dirty() since they are only modified
2684 * in the syncing context and we don't want the
2685 * overhead of making multiple copies of the data.
2687 if (BP_IS_HOLE(db->db_blkptr)) {
2688 arc_buf_thaw(data);
2689 } else {
2690 dbuf_release_bp(db);
2695 if (parent != dn->dn_dbuf) {
2696 ASSERT(parent && parent->db_data_pending);
2697 ASSERT(db->db_level == parent->db_level-1);
2698 ASSERT(arc_released(parent->db_buf));
2699 zio = parent->db_data_pending->dr_zio;
2700 } else {
2701 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2702 db->db_blkid != DMU_SPILL_BLKID) ||
2703 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2704 if (db->db_blkid != DMU_SPILL_BLKID)
2705 ASSERT3P(db->db_blkptr, ==,
2706 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2707 zio = dn->dn_zio;
2710 ASSERT(db->db_level == 0 || data == db->db_buf);
2711 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2712 ASSERT(zio);
2714 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2715 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2716 db->db.db_object, db->db_level, db->db_blkid);
2718 if (db->db_blkid == DMU_SPILL_BLKID)
2719 wp_flag = WP_SPILL;
2720 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2722 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2723 DB_DNODE_EXIT(db);
2725 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2726 ASSERT(db->db_state != DB_NOFILL);
2727 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2728 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2729 dbuf_write_override_ready, dbuf_write_override_done, dr,
2730 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2731 mutex_enter(&db->db_mtx);
2732 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2733 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2734 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2735 mutex_exit(&db->db_mtx);
2736 } else if (db->db_state == DB_NOFILL) {
2737 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2738 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2739 db->db_blkptr, NULL, db->db.db_size, &zp,
2740 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2741 ZIO_PRIORITY_ASYNC_WRITE,
2742 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2743 } else {
2744 ASSERT(arc_released(data));
2745 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2746 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db), &zp,
2747 dbuf_write_ready, dbuf_write_done, db,
2748 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);