]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dbuf.c
Partially revert "Add ddt, ddt_entry, and l2arc_hdr caches"
[mirror_zfs.git] / module / zfs / dbuf.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ef3c1dea 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
9bd274dd 24 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
3a17a7a9 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
0c66c32d 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
34dc7c2f
BB
27 */
28
34dc7c2f 29#include <sys/zfs_context.h>
c28b2279 30#include <sys/arc.h>
34dc7c2f 31#include <sys/dmu.h>
ea97f8ce 32#include <sys/dmu_send.h>
34dc7c2f
BB
33#include <sys/dmu_impl.h>
34#include <sys/dbuf.h>
35#include <sys/dmu_objset.h>
36#include <sys/dsl_dataset.h>
37#include <sys/dsl_dir.h>
38#include <sys/dmu_tx.h>
39#include <sys/spa.h>
40#include <sys/zio.h>
41#include <sys/dmu_zfetch.h>
428870ff
BB
42#include <sys/sa.h>
43#include <sys/sa_impl.h>
9b67f605
MA
44#include <sys/zfeature.h>
45#include <sys/blkptr.h>
9bd274dd 46#include <sys/range_tree.h>
49ee64e5 47#include <sys/trace_dbuf.h>
34dc7c2f 48
fc5bb51f
BB
49struct dbuf_hold_impl_data {
50 /* Function arguments */
51 dnode_t *dh_dn;
52 uint8_t dh_level;
53 uint64_t dh_blkid;
54 int dh_fail_sparse;
55 void *dh_tag;
56 dmu_buf_impl_t **dh_dbp;
57 /* Local variables */
58 dmu_buf_impl_t *dh_db;
59 dmu_buf_impl_t *dh_parent;
60 blkptr_t *dh_bp;
61 int dh_err;
62 dbuf_dirty_record_t *dh_dr;
63 arc_buf_contents_t dh_type;
64 int dh_depth;
65};
66
67static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
68 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
69 void *tag, dmu_buf_impl_t **dbp, int depth);
70static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
71
b663a23d
MA
72/*
73 * Number of times that zfs_free_range() took the slow path while doing
74 * a zfs receive. A nonzero value indicates a potential performance problem.
75 */
76uint64_t zfs_free_range_recv_miss;
77
34dc7c2f 78static void dbuf_destroy(dmu_buf_impl_t *db);
13fe0198 79static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
b128c09f 80static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
34dc7c2f 81
0c66c32d
JG
82#ifndef __lint
83extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
84 dmu_buf_evict_func_t *evict_func, dmu_buf_t **clear_on_evict_dbufp);
85#endif /* ! __lint */
86
34dc7c2f
BB
87/*
88 * Global data structures and functions for the dbuf cache.
89 */
90static kmem_cache_t *dbuf_cache;
0c66c32d 91static taskq_t *dbu_evict_taskq;
34dc7c2f
BB
92
93/* ARGSUSED */
94static int
95dbuf_cons(void *vdb, void *unused, int kmflag)
96{
97 dmu_buf_impl_t *db = vdb;
98 bzero(db, sizeof (dmu_buf_impl_t));
99
100 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
101 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
102 refcount_create(&db->db_holds);
8951cb8d 103
34dc7c2f
BB
104 return (0);
105}
106
107/* ARGSUSED */
108static void
109dbuf_dest(void *vdb, void *unused)
110{
111 dmu_buf_impl_t *db = vdb;
112 mutex_destroy(&db->db_mtx);
113 cv_destroy(&db->db_changed);
114 refcount_destroy(&db->db_holds);
115}
116
117/*
118 * dbuf hash table routines
119 */
120static dbuf_hash_table_t dbuf_hash_table;
121
122static uint64_t dbuf_hash_count;
123
124static uint64_t
125dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
126{
127 uintptr_t osv = (uintptr_t)os;
128 uint64_t crc = -1ULL;
129
130 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
131 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
132 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
133 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
134 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
135 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
136 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
137
138 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
139
140 return (crc);
141}
142
143#define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
144
145#define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
146 ((dbuf)->db.db_object == (obj) && \
147 (dbuf)->db_objset == (os) && \
148 (dbuf)->db_level == (level) && \
149 (dbuf)->db_blkid == (blkid))
150
151dmu_buf_impl_t *
6ebebace 152dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
34dc7c2f
BB
153{
154 dbuf_hash_table_t *h = &dbuf_hash_table;
d6320ddb
BB
155 uint64_t hv;
156 uint64_t idx;
34dc7c2f
BB
157 dmu_buf_impl_t *db;
158
d6320ddb
BB
159 hv = DBUF_HASH(os, obj, level, blkid);
160 idx = hv & h->hash_table_mask;
161
34dc7c2f
BB
162 mutex_enter(DBUF_HASH_MUTEX(h, idx));
163 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
164 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
165 mutex_enter(&db->db_mtx);
166 if (db->db_state != DB_EVICTING) {
167 mutex_exit(DBUF_HASH_MUTEX(h, idx));
168 return (db);
169 }
170 mutex_exit(&db->db_mtx);
171 }
172 }
173 mutex_exit(DBUF_HASH_MUTEX(h, idx));
174 return (NULL);
175}
176
6ebebace
JG
177static dmu_buf_impl_t *
178dbuf_find_bonus(objset_t *os, uint64_t object)
179{
180 dnode_t *dn;
181 dmu_buf_impl_t *db = NULL;
182
183 if (dnode_hold(os, object, FTAG, &dn) == 0) {
184 rw_enter(&dn->dn_struct_rwlock, RW_READER);
185 if (dn->dn_bonus != NULL) {
186 db = dn->dn_bonus;
187 mutex_enter(&db->db_mtx);
188 }
189 rw_exit(&dn->dn_struct_rwlock);
190 dnode_rele(dn, FTAG);
191 }
192 return (db);
193}
194
34dc7c2f
BB
195/*
196 * Insert an entry into the hash table. If there is already an element
197 * equal to elem in the hash table, then the already existing element
198 * will be returned and the new element will not be inserted.
199 * Otherwise returns NULL.
200 */
201static dmu_buf_impl_t *
202dbuf_hash_insert(dmu_buf_impl_t *db)
203{
204 dbuf_hash_table_t *h = &dbuf_hash_table;
428870ff 205 objset_t *os = db->db_objset;
34dc7c2f
BB
206 uint64_t obj = db->db.db_object;
207 int level = db->db_level;
d6320ddb 208 uint64_t blkid, hv, idx;
34dc7c2f
BB
209 dmu_buf_impl_t *dbf;
210
d6320ddb
BB
211 blkid = db->db_blkid;
212 hv = DBUF_HASH(os, obj, level, blkid);
213 idx = hv & h->hash_table_mask;
214
34dc7c2f
BB
215 mutex_enter(DBUF_HASH_MUTEX(h, idx));
216 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
217 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
218 mutex_enter(&dbf->db_mtx);
219 if (dbf->db_state != DB_EVICTING) {
220 mutex_exit(DBUF_HASH_MUTEX(h, idx));
221 return (dbf);
222 }
223 mutex_exit(&dbf->db_mtx);
224 }
225 }
226
227 mutex_enter(&db->db_mtx);
228 db->db_hash_next = h->hash_table[idx];
229 h->hash_table[idx] = db;
230 mutex_exit(DBUF_HASH_MUTEX(h, idx));
231 atomic_add_64(&dbuf_hash_count, 1);
232
233 return (NULL);
234}
235
236/*
bd089c54 237 * Remove an entry from the hash table. It must be in the EVICTING state.
34dc7c2f
BB
238 */
239static void
240dbuf_hash_remove(dmu_buf_impl_t *db)
241{
242 dbuf_hash_table_t *h = &dbuf_hash_table;
d6320ddb 243 uint64_t hv, idx;
34dc7c2f
BB
244 dmu_buf_impl_t *dbf, **dbp;
245
d6320ddb
BB
246 hv = DBUF_HASH(db->db_objset, db->db.db_object,
247 db->db_level, db->db_blkid);
248 idx = hv & h->hash_table_mask;
249
34dc7c2f 250 /*
bd089c54 251 * We musn't hold db_mtx to maintain lock ordering:
34dc7c2f
BB
252 * DBUF_HASH_MUTEX > db_mtx.
253 */
254 ASSERT(refcount_is_zero(&db->db_holds));
255 ASSERT(db->db_state == DB_EVICTING);
256 ASSERT(!MUTEX_HELD(&db->db_mtx));
257
258 mutex_enter(DBUF_HASH_MUTEX(h, idx));
259 dbp = &h->hash_table[idx];
260 while ((dbf = *dbp) != db) {
261 dbp = &dbf->db_hash_next;
262 ASSERT(dbf != NULL);
263 }
264 *dbp = db->db_hash_next;
265 db->db_hash_next = NULL;
266 mutex_exit(DBUF_HASH_MUTEX(h, idx));
267 atomic_add_64(&dbuf_hash_count, -1);
268}
269
270static arc_evict_func_t dbuf_do_evict;
271
0c66c32d
JG
272typedef enum {
273 DBVU_EVICTING,
274 DBVU_NOT_EVICTING
275} dbvu_verify_type_t;
276
277static void
278dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
279{
280#ifdef ZFS_DEBUG
281 int64_t holds;
282
283 if (db->db_user == NULL)
284 return;
285
286 /* Only data blocks support the attachment of user data. */
287 ASSERT(db->db_level == 0);
288
289 /* Clients must resolve a dbuf before attaching user data. */
290 ASSERT(db->db.db_data != NULL);
291 ASSERT3U(db->db_state, ==, DB_CACHED);
292
293 holds = refcount_count(&db->db_holds);
294 if (verify_type == DBVU_EVICTING) {
295 /*
296 * Immediate eviction occurs when holds == dirtycnt.
297 * For normal eviction buffers, holds is zero on
298 * eviction, except when dbuf_fix_old_data() calls
299 * dbuf_clear_data(). However, the hold count can grow
300 * during eviction even though db_mtx is held (see
301 * dmu_bonus_hold() for an example), so we can only
302 * test the generic invariant that holds >= dirtycnt.
303 */
304 ASSERT3U(holds, >=, db->db_dirtycnt);
305 } else {
306 if (db->db_immediate_evict == TRUE)
307 ASSERT3U(holds, >=, db->db_dirtycnt);
308 else
309 ASSERT3U(holds, >, 0);
310 }
311#endif
312}
313
34dc7c2f
BB
314static void
315dbuf_evict_user(dmu_buf_impl_t *db)
316{
0c66c32d
JG
317 dmu_buf_user_t *dbu = db->db_user;
318
34dc7c2f
BB
319 ASSERT(MUTEX_HELD(&db->db_mtx));
320
0c66c32d 321 if (dbu == NULL)
34dc7c2f
BB
322 return;
323
0c66c32d
JG
324 dbuf_verify_user(db, DBVU_EVICTING);
325 db->db_user = NULL;
326
327#ifdef ZFS_DEBUG
328 if (dbu->dbu_clear_on_evict_dbufp != NULL)
329 *dbu->dbu_clear_on_evict_dbufp = NULL;
330#endif
331
332 /*
333 * Invoke the callback from a taskq to avoid lock order reversals
334 * and limit stack depth.
335 */
336 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func, dbu, 0,
337 &dbu->dbu_tqent);
34dc7c2f
BB
338}
339
572e2857
BB
340boolean_t
341dbuf_is_metadata(dmu_buf_impl_t *db)
342{
cc79a5c2
BB
343 /*
344 * Consider indirect blocks and spill blocks to be meta data.
345 */
346 if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) {
572e2857
BB
347 return (B_TRUE);
348 } else {
349 boolean_t is_metadata;
350
351 DB_DNODE_ENTER(db);
9ae529ec 352 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
572e2857
BB
353 DB_DNODE_EXIT(db);
354
355 return (is_metadata);
356 }
357}
358
34dc7c2f
BB
359void
360dbuf_evict(dmu_buf_impl_t *db)
361{
362 ASSERT(MUTEX_HELD(&db->db_mtx));
363 ASSERT(db->db_buf == NULL);
364 ASSERT(db->db_data_pending == NULL);
365
366 dbuf_clear(db);
367 dbuf_destroy(db);
368}
369
370void
371dbuf_init(void)
372{
373 uint64_t hsize = 1ULL << 16;
374 dbuf_hash_table_t *h = &dbuf_hash_table;
375 int i;
376
377 /*
378 * The hash table is big enough to fill all of physical memory
379 * with an average 4K block size. The table will take up
380 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
381 */
382 while (hsize * 4096 < physmem * PAGESIZE)
383 hsize <<= 1;
384
385retry:
386 h->hash_table_mask = hsize - 1;
00b46022 387#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
388 /*
389 * Large allocations which do not require contiguous pages
390 * should be using vmem_alloc() in the linux kernel
391 */
79c76d5b 392 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
00b46022 393#else
34dc7c2f 394 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
00b46022 395#endif
34dc7c2f
BB
396 if (h->hash_table == NULL) {
397 /* XXX - we should really return an error instead of assert */
398 ASSERT(hsize > (1ULL << 10));
399 hsize >>= 1;
400 goto retry;
401 }
402
403 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
404 sizeof (dmu_buf_impl_t),
405 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
406
407 for (i = 0; i < DBUF_MUTEXES; i++)
40d06e3c 408 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
e0b0ca98
BB
409
410 dbuf_stats_init(h);
0c66c32d
JG
411
412 /*
413 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
414 * configuration is not required.
415 */
416 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
34dc7c2f
BB
417}
418
419void
420dbuf_fini(void)
421{
422 dbuf_hash_table_t *h = &dbuf_hash_table;
423 int i;
424
e0b0ca98
BB
425 dbuf_stats_destroy();
426
34dc7c2f
BB
427 for (i = 0; i < DBUF_MUTEXES; i++)
428 mutex_destroy(&h->hash_mutexes[i]);
00b46022 429#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
430 /*
431 * Large allocations which do not require contiguous pages
432 * should be using vmem_free() in the linux kernel
433 */
00b46022
BB
434 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
435#else
34dc7c2f 436 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
00b46022 437#endif
34dc7c2f 438 kmem_cache_destroy(dbuf_cache);
0c66c32d 439 taskq_destroy(dbu_evict_taskq);
34dc7c2f
BB
440}
441
442/*
443 * Other stuff.
444 */
445
446#ifdef ZFS_DEBUG
447static void
448dbuf_verify(dmu_buf_impl_t *db)
449{
572e2857 450 dnode_t *dn;
428870ff 451 dbuf_dirty_record_t *dr;
34dc7c2f
BB
452
453 ASSERT(MUTEX_HELD(&db->db_mtx));
454
455 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
456 return;
457
458 ASSERT(db->db_objset != NULL);
572e2857
BB
459 DB_DNODE_ENTER(db);
460 dn = DB_DNODE(db);
34dc7c2f
BB
461 if (dn == NULL) {
462 ASSERT(db->db_parent == NULL);
463 ASSERT(db->db_blkptr == NULL);
464 } else {
465 ASSERT3U(db->db.db_object, ==, dn->dn_object);
466 ASSERT3P(db->db_objset, ==, dn->dn_objset);
467 ASSERT3U(db->db_level, <, dn->dn_nlevels);
572e2857
BB
468 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
469 db->db_blkid == DMU_SPILL_BLKID ||
8951cb8d 470 !avl_is_empty(&dn->dn_dbufs));
34dc7c2f 471 }
428870ff
BB
472 if (db->db_blkid == DMU_BONUS_BLKID) {
473 ASSERT(dn != NULL);
474 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
475 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
476 } else if (db->db_blkid == DMU_SPILL_BLKID) {
34dc7c2f
BB
477 ASSERT(dn != NULL);
478 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
c99c9001 479 ASSERT0(db->db.db_offset);
34dc7c2f
BB
480 } else {
481 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
482 }
483
428870ff
BB
484 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
485 ASSERT(dr->dr_dbuf == db);
486
487 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
488 ASSERT(dr->dr_dbuf == db);
489
b128c09f
BB
490 /*
491 * We can't assert that db_size matches dn_datablksz because it
492 * can be momentarily different when another thread is doing
493 * dnode_set_blksz().
494 */
495 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
428870ff 496 dr = db->db_data_pending;
b128c09f
BB
497 /*
498 * It should only be modified in syncing context, so
499 * make sure we only have one copy of the data.
500 */
501 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
34dc7c2f
BB
502 }
503
504 /* verify db->db_blkptr */
505 if (db->db_blkptr) {
506 if (db->db_parent == dn->dn_dbuf) {
507 /* db is pointed to by the dnode */
508 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
9babb374 509 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
34dc7c2f
BB
510 ASSERT(db->db_parent == NULL);
511 else
512 ASSERT(db->db_parent != NULL);
428870ff
BB
513 if (db->db_blkid != DMU_SPILL_BLKID)
514 ASSERT3P(db->db_blkptr, ==,
515 &dn->dn_phys->dn_blkptr[db->db_blkid]);
34dc7c2f
BB
516 } else {
517 /* db is pointed to by an indirect block */
1fde1e37
BB
518 ASSERTV(int epb = db->db_parent->db.db_size >>
519 SPA_BLKPTRSHIFT);
34dc7c2f
BB
520 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
521 ASSERT3U(db->db_parent->db.db_object, ==,
522 db->db.db_object);
523 /*
524 * dnode_grow_indblksz() can make this fail if we don't
525 * have the struct_rwlock. XXX indblksz no longer
526 * grows. safe to do this now?
527 */
572e2857 528 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
34dc7c2f
BB
529 ASSERT3P(db->db_blkptr, ==,
530 ((blkptr_t *)db->db_parent->db.db_data +
531 db->db_blkid % epb));
532 }
533 }
534 }
535 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
428870ff
BB
536 (db->db_buf == NULL || db->db_buf->b_data) &&
537 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
34dc7c2f
BB
538 db->db_state != DB_FILL && !dn->dn_free_txg) {
539 /*
540 * If the blkptr isn't set but they have nonzero data,
541 * it had better be dirty, otherwise we'll lose that
542 * data when we evict this buffer.
543 */
544 if (db->db_dirtycnt == 0) {
1fde1e37 545 ASSERTV(uint64_t *buf = db->db.db_data);
34dc7c2f
BB
546 int i;
547
548 for (i = 0; i < db->db.db_size >> 3; i++) {
549 ASSERT(buf[i] == 0);
550 }
551 }
552 }
572e2857 553 DB_DNODE_EXIT(db);
34dc7c2f
BB
554}
555#endif
556
0c66c32d
JG
557static void
558dbuf_clear_data(dmu_buf_impl_t *db)
559{
560 ASSERT(MUTEX_HELD(&db->db_mtx));
561 dbuf_evict_user(db);
562 db->db_buf = NULL;
563 db->db.db_data = NULL;
564 if (db->db_state != DB_NOFILL)
565 db->db_state = DB_UNCACHED;
566}
567
34dc7c2f
BB
568static void
569dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
570{
571 ASSERT(MUTEX_HELD(&db->db_mtx));
0c66c32d
JG
572 ASSERT(buf != NULL);
573
34dc7c2f 574 db->db_buf = buf;
0c66c32d
JG
575 ASSERT(buf->b_data != NULL);
576 db->db.db_data = buf->b_data;
577 if (!arc_released(buf))
578 arc_set_callback(buf, dbuf_do_evict, db);
34dc7c2f
BB
579}
580
428870ff
BB
581/*
582 * Loan out an arc_buf for read. Return the loaned arc_buf.
583 */
584arc_buf_t *
585dbuf_loan_arcbuf(dmu_buf_impl_t *db)
586{
587 arc_buf_t *abuf;
588
589 mutex_enter(&db->db_mtx);
590 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
591 int blksz = db->db.db_size;
b0bc7a84 592 spa_t *spa = db->db_objset->os_spa;
572e2857 593
428870ff 594 mutex_exit(&db->db_mtx);
572e2857 595 abuf = arc_loan_buf(spa, blksz);
428870ff
BB
596 bcopy(db->db.db_data, abuf->b_data, blksz);
597 } else {
598 abuf = db->db_buf;
599 arc_loan_inuse_buf(abuf, db);
0c66c32d 600 dbuf_clear_data(db);
428870ff
BB
601 mutex_exit(&db->db_mtx);
602 }
603 return (abuf);
604}
605
34dc7c2f
BB
606uint64_t
607dbuf_whichblock(dnode_t *dn, uint64_t offset)
608{
609 if (dn->dn_datablkshift) {
610 return (offset >> dn->dn_datablkshift);
611 } else {
612 ASSERT3U(offset, <, dn->dn_datablksz);
613 return (0);
614 }
615}
616
617static void
618dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
619{
620 dmu_buf_impl_t *db = vdb;
621
622 mutex_enter(&db->db_mtx);
623 ASSERT3U(db->db_state, ==, DB_READ);
624 /*
625 * All reads are synchronous, so we must have a hold on the dbuf
626 */
627 ASSERT(refcount_count(&db->db_holds) > 0);
628 ASSERT(db->db_buf == NULL);
629 ASSERT(db->db.db_data == NULL);
630 if (db->db_level == 0 && db->db_freed_in_flight) {
631 /* we were freed in flight; disregard any error */
632 arc_release(buf, db);
633 bzero(buf->b_data, db->db.db_size);
634 arc_buf_freeze(buf);
635 db->db_freed_in_flight = FALSE;
636 dbuf_set_data(db, buf);
637 db->db_state = DB_CACHED;
638 } else if (zio == NULL || zio->io_error == 0) {
639 dbuf_set_data(db, buf);
640 db->db_state = DB_CACHED;
641 } else {
428870ff 642 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 643 ASSERT3P(db->db_buf, ==, NULL);
13fe0198 644 VERIFY(arc_buf_remove_ref(buf, db));
34dc7c2f
BB
645 db->db_state = DB_UNCACHED;
646 }
647 cv_broadcast(&db->db_changed);
428870ff 648 dbuf_rele_and_unlock(db, NULL);
34dc7c2f
BB
649}
650
5f6d0b6f 651static int
34dc7c2f
BB
652dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
653{
572e2857 654 dnode_t *dn;
5dbd68a3 655 zbookmark_phys_t zb;
34dc7c2f 656 uint32_t aflags = ARC_NOWAIT;
5f6d0b6f 657 int err;
34dc7c2f 658
572e2857
BB
659 DB_DNODE_ENTER(db);
660 dn = DB_DNODE(db);
34dc7c2f
BB
661 ASSERT(!refcount_is_zero(&db->db_holds));
662 /* We need the struct_rwlock to prevent db_blkptr from changing. */
b128c09f 663 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
34dc7c2f
BB
664 ASSERT(MUTEX_HELD(&db->db_mtx));
665 ASSERT(db->db_state == DB_UNCACHED);
666 ASSERT(db->db_buf == NULL);
667
428870ff 668 if (db->db_blkid == DMU_BONUS_BLKID) {
9babb374 669 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
34dc7c2f
BB
670
671 ASSERT3U(bonuslen, <=, db->db.db_size);
672 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
d164b209 673 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
34dc7c2f
BB
674 if (bonuslen < DN_MAX_BONUSLEN)
675 bzero(db->db.db_data, DN_MAX_BONUSLEN);
9babb374
BB
676 if (bonuslen)
677 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
572e2857 678 DB_DNODE_EXIT(db);
34dc7c2f
BB
679 db->db_state = DB_CACHED;
680 mutex_exit(&db->db_mtx);
5f6d0b6f 681 return (0);
34dc7c2f
BB
682 }
683
b128c09f
BB
684 /*
685 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
686 * processes the delete record and clears the bp while we are waiting
687 * for the dn_mtx (resulting in a "no" from block_freed).
688 */
689 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
690 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
691 BP_IS_HOLE(db->db_blkptr)))) {
34dc7c2f
BB
692 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
693
572e2857 694 DB_DNODE_EXIT(db);
b0bc7a84
MG
695 dbuf_set_data(db, arc_buf_alloc(db->db_objset->os_spa,
696 db->db.db_size, db, type));
34dc7c2f
BB
697 bzero(db->db.db_data, db->db.db_size);
698 db->db_state = DB_CACHED;
699 *flags |= DB_RF_CACHED;
700 mutex_exit(&db->db_mtx);
5f6d0b6f 701 return (0);
34dc7c2f
BB
702 }
703
572e2857
BB
704 DB_DNODE_EXIT(db);
705
34dc7c2f
BB
706 db->db_state = DB_READ;
707 mutex_exit(&db->db_mtx);
708
b128c09f
BB
709 if (DBUF_IS_L2CACHEABLE(db))
710 aflags |= ARC_L2CACHE;
3a17a7a9
SK
711 if (DBUF_IS_L2COMPRESSIBLE(db))
712 aflags |= ARC_L2COMPRESS;
b128c09f 713
428870ff
BB
714 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
715 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
716 db->db.db_object, db->db_level, db->db_blkid);
34dc7c2f
BB
717
718 dbuf_add_ref(db, NULL);
b128c09f 719
5f6d0b6f 720 err = arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
34dc7c2f
BB
721 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
722 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
723 &aflags, &zb);
724 if (aflags & ARC_CACHED)
725 *flags |= DB_RF_CACHED;
5f6d0b6f
BB
726
727 return (SET_ERROR(err));
34dc7c2f
BB
728}
729
730int
731dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
732{
733 int err = 0;
b0bc7a84
MG
734 boolean_t havepzio = (zio != NULL);
735 boolean_t prefetch;
572e2857 736 dnode_t *dn;
34dc7c2f
BB
737
738 /*
739 * We don't have to hold the mutex to check db_state because it
740 * can't be freed while we have a hold on the buffer.
741 */
742 ASSERT(!refcount_is_zero(&db->db_holds));
743
b128c09f 744 if (db->db_state == DB_NOFILL)
2e528b49 745 return (SET_ERROR(EIO));
b128c09f 746
572e2857
BB
747 DB_DNODE_ENTER(db);
748 dn = DB_DNODE(db);
34dc7c2f 749 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857 750 rw_enter(&dn->dn_struct_rwlock, RW_READER);
34dc7c2f 751
428870ff 752 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
572e2857 753 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
b128c09f 754 DBUF_IS_CACHEABLE(db);
34dc7c2f
BB
755
756 mutex_enter(&db->db_mtx);
757 if (db->db_state == DB_CACHED) {
758 mutex_exit(&db->db_mtx);
759 if (prefetch)
572e2857 760 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
34dc7c2f
BB
761 db->db.db_size, TRUE);
762 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
763 rw_exit(&dn->dn_struct_rwlock);
764 DB_DNODE_EXIT(db);
34dc7c2f 765 } else if (db->db_state == DB_UNCACHED) {
572e2857
BB
766 spa_t *spa = dn->dn_objset->os_spa;
767
768 if (zio == NULL)
769 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
5f6d0b6f
BB
770
771 err = dbuf_read_impl(db, zio, &flags);
34dc7c2f
BB
772
773 /* dbuf_read_impl has dropped db_mtx for us */
774
5f6d0b6f 775 if (!err && prefetch)
572e2857 776 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
34dc7c2f
BB
777 db->db.db_size, flags & DB_RF_CACHED);
778
779 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
780 rw_exit(&dn->dn_struct_rwlock);
781 DB_DNODE_EXIT(db);
34dc7c2f 782
5f6d0b6f 783 if (!err && !havepzio)
34dc7c2f
BB
784 err = zio_wait(zio);
785 } else {
e49f1e20
WA
786 /*
787 * Another reader came in while the dbuf was in flight
788 * between UNCACHED and CACHED. Either a writer will finish
789 * writing the buffer (sending the dbuf to CACHED) or the
790 * first reader's request will reach the read_done callback
791 * and send the dbuf to CACHED. Otherwise, a failure
792 * occurred and the dbuf went to UNCACHED.
793 */
34dc7c2f
BB
794 mutex_exit(&db->db_mtx);
795 if (prefetch)
572e2857 796 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
34dc7c2f
BB
797 db->db.db_size, TRUE);
798 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
799 rw_exit(&dn->dn_struct_rwlock);
800 DB_DNODE_EXIT(db);
34dc7c2f 801
e49f1e20 802 /* Skip the wait per the caller's request. */
34dc7c2f
BB
803 mutex_enter(&db->db_mtx);
804 if ((flags & DB_RF_NEVERWAIT) == 0) {
805 while (db->db_state == DB_READ ||
806 db->db_state == DB_FILL) {
807 ASSERT(db->db_state == DB_READ ||
808 (flags & DB_RF_HAVESTRUCT) == 0);
64dbba36
AL
809 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
810 db, zio_t *, zio);
34dc7c2f
BB
811 cv_wait(&db->db_changed, &db->db_mtx);
812 }
813 if (db->db_state == DB_UNCACHED)
2e528b49 814 err = SET_ERROR(EIO);
34dc7c2f
BB
815 }
816 mutex_exit(&db->db_mtx);
817 }
818
819 ASSERT(err || havepzio || db->db_state == DB_CACHED);
820 return (err);
821}
822
823static void
824dbuf_noread(dmu_buf_impl_t *db)
825{
826 ASSERT(!refcount_is_zero(&db->db_holds));
428870ff 827 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
828 mutex_enter(&db->db_mtx);
829 while (db->db_state == DB_READ || db->db_state == DB_FILL)
830 cv_wait(&db->db_changed, &db->db_mtx);
831 if (db->db_state == DB_UNCACHED) {
832 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
b0bc7a84 833 spa_t *spa = db->db_objset->os_spa;
34dc7c2f
BB
834
835 ASSERT(db->db_buf == NULL);
836 ASSERT(db->db.db_data == NULL);
572e2857 837 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
34dc7c2f 838 db->db_state = DB_FILL;
b128c09f 839 } else if (db->db_state == DB_NOFILL) {
0c66c32d 840 dbuf_clear_data(db);
34dc7c2f
BB
841 } else {
842 ASSERT3U(db->db_state, ==, DB_CACHED);
843 }
844 mutex_exit(&db->db_mtx);
845}
846
847/*
848 * This is our just-in-time copy function. It makes a copy of
849 * buffers, that have been modified in a previous transaction
850 * group, before we modify them in the current active group.
851 *
852 * This function is used in two places: when we are dirtying a
853 * buffer for the first time in a txg, and when we are freeing
854 * a range in a dnode that includes this buffer.
855 *
856 * Note that when we are called from dbuf_free_range() we do
857 * not put a hold on the buffer, we just traverse the active
858 * dbuf list for the dnode.
859 */
860static void
861dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
862{
863 dbuf_dirty_record_t *dr = db->db_last_dirty;
864
865 ASSERT(MUTEX_HELD(&db->db_mtx));
866 ASSERT(db->db.db_data != NULL);
867 ASSERT(db->db_level == 0);
868 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
869
870 if (dr == NULL ||
871 (dr->dt.dl.dr_data !=
428870ff 872 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
34dc7c2f
BB
873 return;
874
875 /*
876 * If the last dirty record for this dbuf has not yet synced
877 * and its referencing the dbuf data, either:
572e2857 878 * reset the reference to point to a new copy,
34dc7c2f
BB
879 * or (if there a no active holders)
880 * just null out the current db_data pointer.
881 */
882 ASSERT(dr->dr_txg >= txg - 2);
428870ff 883 if (db->db_blkid == DMU_BONUS_BLKID) {
34dc7c2f
BB
884 /* Note that the data bufs here are zio_bufs */
885 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
d164b209 886 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
34dc7c2f
BB
887 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
888 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
889 int size = db->db.db_size;
890 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
b0bc7a84 891 spa_t *spa = db->db_objset->os_spa;
572e2857 892
572e2857 893 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
34dc7c2f
BB
894 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
895 } else {
0c66c32d 896 dbuf_clear_data(db);
34dc7c2f
BB
897 }
898}
899
900void
901dbuf_unoverride(dbuf_dirty_record_t *dr)
902{
903 dmu_buf_impl_t *db = dr->dr_dbuf;
428870ff 904 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
34dc7c2f
BB
905 uint64_t txg = dr->dr_txg;
906
907 ASSERT(MUTEX_HELD(&db->db_mtx));
908 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
909 ASSERT(db->db_level == 0);
910
428870ff 911 if (db->db_blkid == DMU_BONUS_BLKID ||
34dc7c2f
BB
912 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
913 return;
914
428870ff
BB
915 ASSERT(db->db_data_pending != dr);
916
34dc7c2f 917 /* free this block */
b0bc7a84
MG
918 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
919 zio_free(db->db_objset->os_spa, txg, bp);
428870ff 920
34dc7c2f 921 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
03c6040b
GW
922 dr->dt.dl.dr_nopwrite = B_FALSE;
923
34dc7c2f
BB
924 /*
925 * Release the already-written buffer, so we leave it in
926 * a consistent dirty state. Note that all callers are
927 * modifying the buffer, so they will immediately do
928 * another (redundant) arc_release(). Therefore, leave
929 * the buf thawed to save the effort of freezing &
930 * immediately re-thawing it.
931 */
932 arc_release(dr->dt.dl.dr_data, db);
933}
934
b128c09f
BB
935/*
936 * Evict (if its unreferenced) or clear (if its referenced) any level-0
937 * data blocks in the free range, so that any future readers will find
b0bc7a84 938 * empty blocks.
ea97f8ce
MA
939 *
940 * This is a no-op if the dataset is in the middle of an incremental
941 * receive; see comment below for details.
b128c09f 942 */
34dc7c2f 943void
8951cb8d
AR
944dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
945 dmu_tx_t *tx)
34dc7c2f 946{
0c66c32d
JG
947 dmu_buf_impl_t *db_search;
948 dmu_buf_impl_t *db, *db_next;
34dc7c2f 949 uint64_t txg = tx->tx_txg;
8951cb8d 950 avl_index_t where;
4254acb0 951 boolean_t freespill =
8951cb8d
AR
952 (start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID);
953
954 if (end_blkid > dn->dn_maxblkid && !freespill)
955 end_blkid = dn->dn_maxblkid;
956 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
34dc7c2f 957
0c66c32d 958 db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
8951cb8d
AR
959 db_search->db_level = 0;
960 db_search->db_blkid = start_blkid;
9925c28c 961 db_search->db_state = DB_SEARCH;
ea97f8ce 962
b663a23d 963 mutex_enter(&dn->dn_dbufs_mtx);
8951cb8d 964 if (start_blkid >= dn->dn_unlisted_l0_blkid && !freespill) {
b663a23d 965 /* There can't be any dbufs in this range; no need to search. */
8951cb8d
AR
966#ifdef DEBUG
967 db = avl_find(&dn->dn_dbufs, db_search, &where);
968 ASSERT3P(db, ==, NULL);
969 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
970 ASSERT(db == NULL || db->db_level > 0);
971#endif
972 goto out;
b663a23d 973 } else if (dmu_objset_is_receiving(dn->dn_objset)) {
ea97f8ce 974 /*
b663a23d
MA
975 * If we are receiving, we expect there to be no dbufs in
976 * the range to be freed, because receive modifies each
977 * block at most once, and in offset order. If this is
978 * not the case, it can lead to performance problems,
979 * so note that we unexpectedly took the slow path.
ea97f8ce 980 */
b663a23d 981 atomic_inc_64(&zfs_free_range_recv_miss);
ea97f8ce
MA
982 }
983
8951cb8d
AR
984 db = avl_find(&dn->dn_dbufs, db_search, &where);
985 ASSERT3P(db, ==, NULL);
986 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
987
988 for (; db != NULL; db = db_next) {
989 db_next = AVL_NEXT(&dn->dn_dbufs, db);
428870ff 990 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
b128c09f 991
8951cb8d
AR
992 if (db->db_level != 0 || db->db_blkid > end_blkid) {
993 break;
994 }
995 ASSERT3U(db->db_blkid, >=, start_blkid);
34dc7c2f
BB
996
997 /* found a level 0 buffer in the range */
13fe0198
MA
998 mutex_enter(&db->db_mtx);
999 if (dbuf_undirty(db, tx)) {
1000 /* mutex has been dropped and dbuf destroyed */
34dc7c2f 1001 continue;
13fe0198 1002 }
34dc7c2f 1003
34dc7c2f 1004 if (db->db_state == DB_UNCACHED ||
b128c09f 1005 db->db_state == DB_NOFILL ||
34dc7c2f
BB
1006 db->db_state == DB_EVICTING) {
1007 ASSERT(db->db.db_data == NULL);
1008 mutex_exit(&db->db_mtx);
1009 continue;
1010 }
1011 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1012 /* will be handled in dbuf_read_done or dbuf_rele */
1013 db->db_freed_in_flight = TRUE;
1014 mutex_exit(&db->db_mtx);
1015 continue;
1016 }
1017 if (refcount_count(&db->db_holds) == 0) {
1018 ASSERT(db->db_buf);
1019 dbuf_clear(db);
1020 continue;
1021 }
1022 /* The dbuf is referenced */
1023
1024 if (db->db_last_dirty != NULL) {
1025 dbuf_dirty_record_t *dr = db->db_last_dirty;
1026
1027 if (dr->dr_txg == txg) {
1028 /*
1029 * This buffer is "in-use", re-adjust the file
1030 * size to reflect that this buffer may
1031 * contain new data when we sync.
1032 */
428870ff
BB
1033 if (db->db_blkid != DMU_SPILL_BLKID &&
1034 db->db_blkid > dn->dn_maxblkid)
34dc7c2f
BB
1035 dn->dn_maxblkid = db->db_blkid;
1036 dbuf_unoverride(dr);
1037 } else {
1038 /*
1039 * This dbuf is not dirty in the open context.
1040 * Either uncache it (if its not referenced in
1041 * the open context) or reset its contents to
1042 * empty.
1043 */
1044 dbuf_fix_old_data(db, txg);
1045 }
1046 }
1047 /* clear the contents if its cached */
1048 if (db->db_state == DB_CACHED) {
1049 ASSERT(db->db.db_data != NULL);
1050 arc_release(db->db_buf, db);
1051 bzero(db->db.db_data, db->db.db_size);
1052 arc_buf_freeze(db->db_buf);
1053 }
1054
1055 mutex_exit(&db->db_mtx);
1056 }
8951cb8d
AR
1057
1058out:
1059 kmem_free(db_search, sizeof (dmu_buf_impl_t));
34dc7c2f
BB
1060 mutex_exit(&dn->dn_dbufs_mtx);
1061}
1062
1063static int
1064dbuf_block_freeable(dmu_buf_impl_t *db)
1065{
1066 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
1067 uint64_t birth_txg = 0;
1068
1069 /*
1070 * We don't need any locking to protect db_blkptr:
1071 * If it's syncing, then db_last_dirty will be set
1072 * so we'll ignore db_blkptr.
b0bc7a84
MG
1073 *
1074 * This logic ensures that only block births for
1075 * filled blocks are considered.
34dc7c2f
BB
1076 */
1077 ASSERT(MUTEX_HELD(&db->db_mtx));
b0bc7a84
MG
1078 if (db->db_last_dirty && (db->db_blkptr == NULL ||
1079 !BP_IS_HOLE(db->db_blkptr))) {
34dc7c2f 1080 birth_txg = db->db_last_dirty->dr_txg;
b0bc7a84 1081 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
34dc7c2f 1082 birth_txg = db->db_blkptr->blk_birth;
b0bc7a84 1083 }
34dc7c2f 1084
572e2857 1085 /*
b0bc7a84 1086 * If this block don't exist or is in a snapshot, it can't be freed.
572e2857
BB
1087 * Don't pass the bp to dsl_dataset_block_freeable() since we
1088 * are holding the db_mtx lock and might deadlock if we are
1089 * prefetching a dedup-ed block.
1090 */
b0bc7a84 1091 if (birth_txg != 0)
34dc7c2f 1092 return (ds == NULL ||
572e2857 1093 dsl_dataset_block_freeable(ds, NULL, birth_txg));
34dc7c2f 1094 else
b0bc7a84 1095 return (B_FALSE);
34dc7c2f
BB
1096}
1097
1098void
1099dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1100{
1101 arc_buf_t *buf, *obuf;
1102 int osize = db->db.db_size;
1103 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
572e2857 1104 dnode_t *dn;
34dc7c2f 1105
428870ff 1106 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 1107
572e2857
BB
1108 DB_DNODE_ENTER(db);
1109 dn = DB_DNODE(db);
1110
34dc7c2f 1111 /* XXX does *this* func really need the lock? */
572e2857 1112 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
34dc7c2f
BB
1113
1114 /*
b0bc7a84 1115 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
34dc7c2f
BB
1116 * is OK, because there can be no other references to the db
1117 * when we are changing its size, so no concurrent DB_FILL can
1118 * be happening.
1119 */
1120 /*
1121 * XXX we should be doing a dbuf_read, checking the return
1122 * value and returning that up to our callers
1123 */
b0bc7a84 1124 dmu_buf_will_dirty(&db->db, tx);
34dc7c2f
BB
1125
1126 /* create the data buffer for the new block */
572e2857 1127 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
34dc7c2f
BB
1128
1129 /* copy old block data to the new block */
1130 obuf = db->db_buf;
1131 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1132 /* zero the remainder */
1133 if (size > osize)
1134 bzero((uint8_t *)buf->b_data + osize, size - osize);
1135
1136 mutex_enter(&db->db_mtx);
1137 dbuf_set_data(db, buf);
13fe0198 1138 VERIFY(arc_buf_remove_ref(obuf, db));
34dc7c2f
BB
1139 db->db.db_size = size;
1140
1141 if (db->db_level == 0) {
1142 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1143 db->db_last_dirty->dt.dl.dr_data = buf;
1144 }
1145 mutex_exit(&db->db_mtx);
1146
572e2857
BB
1147 dnode_willuse_space(dn, size-osize, tx);
1148 DB_DNODE_EXIT(db);
34dc7c2f
BB
1149}
1150
428870ff
BB
1151void
1152dbuf_release_bp(dmu_buf_impl_t *db)
1153{
b0bc7a84 1154 ASSERTV(objset_t *os = db->db_objset);
428870ff
BB
1155
1156 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1157 ASSERT(arc_released(os->os_phys_buf) ||
1158 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1159 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1160
294f6806 1161 (void) arc_release(db->db_buf, db);
428870ff
BB
1162}
1163
34dc7c2f
BB
1164dbuf_dirty_record_t *
1165dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1166{
572e2857
BB
1167 dnode_t *dn;
1168 objset_t *os;
34dc7c2f
BB
1169 dbuf_dirty_record_t **drp, *dr;
1170 int drop_struct_lock = FALSE;
b128c09f 1171 boolean_t do_free_accounting = B_FALSE;
34dc7c2f
BB
1172 int txgoff = tx->tx_txg & TXG_MASK;
1173
1174 ASSERT(tx->tx_txg != 0);
1175 ASSERT(!refcount_is_zero(&db->db_holds));
1176 DMU_TX_DIRTY_BUF(tx, db);
1177
572e2857
BB
1178 DB_DNODE_ENTER(db);
1179 dn = DB_DNODE(db);
34dc7c2f
BB
1180 /*
1181 * Shouldn't dirty a regular buffer in syncing context. Private
1182 * objects may be dirtied in syncing context, but only if they
1183 * were already pre-dirtied in open context.
34dc7c2f
BB
1184 */
1185 ASSERT(!dmu_tx_is_syncing(tx) ||
1186 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
9babb374
BB
1187 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1188 dn->dn_objset->os_dsl_dataset == NULL);
34dc7c2f
BB
1189 /*
1190 * We make this assert for private objects as well, but after we
1191 * check if we're already dirty. They are allowed to re-dirty
1192 * in syncing context.
1193 */
1194 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1195 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1196 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1197
1198 mutex_enter(&db->db_mtx);
1199 /*
1200 * XXX make this true for indirects too? The problem is that
1201 * transactions created with dmu_tx_create_assigned() from
1202 * syncing context don't bother holding ahead.
1203 */
1204 ASSERT(db->db_level != 0 ||
b128c09f
BB
1205 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1206 db->db_state == DB_NOFILL);
34dc7c2f
BB
1207
1208 mutex_enter(&dn->dn_mtx);
1209 /*
1210 * Don't set dirtyctx to SYNC if we're just modifying this as we
1211 * initialize the objset.
1212 */
1213 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1214 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1215 dn->dn_dirtyctx =
1216 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1217 ASSERT(dn->dn_dirtyctx_firstset == NULL);
79c76d5b 1218 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
34dc7c2f
BB
1219 }
1220 mutex_exit(&dn->dn_mtx);
1221
428870ff
BB
1222 if (db->db_blkid == DMU_SPILL_BLKID)
1223 dn->dn_have_spill = B_TRUE;
1224
34dc7c2f
BB
1225 /*
1226 * If this buffer is already dirty, we're done.
1227 */
1228 drp = &db->db_last_dirty;
1229 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1230 db->db.db_object == DMU_META_DNODE_OBJECT);
1231 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1232 drp = &dr->dr_next;
1233 if (dr && dr->dr_txg == tx->tx_txg) {
572e2857
BB
1234 DB_DNODE_EXIT(db);
1235
428870ff 1236 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
34dc7c2f
BB
1237 /*
1238 * If this buffer has already been written out,
1239 * we now need to reset its state.
1240 */
1241 dbuf_unoverride(dr);
428870ff
BB
1242 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1243 db->db_state != DB_NOFILL)
34dc7c2f
BB
1244 arc_buf_thaw(db->db_buf);
1245 }
1246 mutex_exit(&db->db_mtx);
1247 return (dr);
1248 }
1249
1250 /*
1251 * Only valid if not already dirty.
1252 */
9babb374
BB
1253 ASSERT(dn->dn_object == 0 ||
1254 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
34dc7c2f
BB
1255 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1256
1257 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1258 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1259 dn->dn_phys->dn_nlevels > db->db_level ||
1260 dn->dn_next_nlevels[txgoff] > db->db_level ||
1261 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1262 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1263
1264 /*
1265 * We should only be dirtying in syncing context if it's the
9babb374
BB
1266 * mos or we're initializing the os or it's a special object.
1267 * However, we are allowed to dirty in syncing context provided
1268 * we already dirtied it in open context. Hence we must make
1269 * this assertion only if we're not already dirty.
34dc7c2f 1270 */
572e2857 1271 os = dn->dn_objset;
9babb374
BB
1272 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1273 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
34dc7c2f
BB
1274 ASSERT(db->db.db_size != 0);
1275
1276 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1277
428870ff 1278 if (db->db_blkid != DMU_BONUS_BLKID) {
34dc7c2f
BB
1279 /*
1280 * Update the accounting.
b128c09f
BB
1281 * Note: we delay "free accounting" until after we drop
1282 * the db_mtx. This keeps us from grabbing other locks
428870ff 1283 * (and possibly deadlocking) in bp_get_dsize() while
b128c09f 1284 * also holding the db_mtx.
34dc7c2f 1285 */
34dc7c2f 1286 dnode_willuse_space(dn, db->db.db_size, tx);
b128c09f 1287 do_free_accounting = dbuf_block_freeable(db);
34dc7c2f
BB
1288 }
1289
1290 /*
1291 * If this buffer is dirty in an old transaction group we need
1292 * to make a copy of it so that the changes we make in this
1293 * transaction group won't leak out when we sync the older txg.
1294 */
79c76d5b 1295 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
98f72a53 1296 list_link_init(&dr->dr_dirty_node);
34dc7c2f
BB
1297 if (db->db_level == 0) {
1298 void *data_old = db->db_buf;
1299
b128c09f 1300 if (db->db_state != DB_NOFILL) {
428870ff 1301 if (db->db_blkid == DMU_BONUS_BLKID) {
b128c09f
BB
1302 dbuf_fix_old_data(db, tx->tx_txg);
1303 data_old = db->db.db_data;
1304 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1305 /*
1306 * Release the data buffer from the cache so
1307 * that we can modify it without impacting
1308 * possible other users of this cached data
1309 * block. Note that indirect blocks and
1310 * private objects are not released until the
1311 * syncing state (since they are only modified
1312 * then).
1313 */
1314 arc_release(db->db_buf, db);
1315 dbuf_fix_old_data(db, tx->tx_txg);
1316 data_old = db->db_buf;
1317 }
1318 ASSERT(data_old != NULL);
34dc7c2f 1319 }
34dc7c2f
BB
1320 dr->dt.dl.dr_data = data_old;
1321 } else {
1322 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1323 list_create(&dr->dt.di.dr_children,
1324 sizeof (dbuf_dirty_record_t),
1325 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1326 }
e8b96c60
MA
1327 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1328 dr->dr_accounted = db->db.db_size;
34dc7c2f
BB
1329 dr->dr_dbuf = db;
1330 dr->dr_txg = tx->tx_txg;
1331 dr->dr_next = *drp;
1332 *drp = dr;
1333
1334 /*
1335 * We could have been freed_in_flight between the dbuf_noread
1336 * and dbuf_dirty. We win, as though the dbuf_noread() had
1337 * happened after the free.
1338 */
428870ff
BB
1339 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1340 db->db_blkid != DMU_SPILL_BLKID) {
34dc7c2f 1341 mutex_enter(&dn->dn_mtx);
9bd274dd
MA
1342 if (dn->dn_free_ranges[txgoff] != NULL) {
1343 range_tree_clear(dn->dn_free_ranges[txgoff],
1344 db->db_blkid, 1);
1345 }
34dc7c2f
BB
1346 mutex_exit(&dn->dn_mtx);
1347 db->db_freed_in_flight = FALSE;
1348 }
1349
1350 /*
1351 * This buffer is now part of this txg
1352 */
1353 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1354 db->db_dirtycnt += 1;
1355 ASSERT3U(db->db_dirtycnt, <=, 3);
1356
1357 mutex_exit(&db->db_mtx);
1358
428870ff
BB
1359 if (db->db_blkid == DMU_BONUS_BLKID ||
1360 db->db_blkid == DMU_SPILL_BLKID) {
34dc7c2f
BB
1361 mutex_enter(&dn->dn_mtx);
1362 ASSERT(!list_link_active(&dr->dr_dirty_node));
1363 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1364 mutex_exit(&dn->dn_mtx);
1365 dnode_setdirty(dn, tx);
572e2857 1366 DB_DNODE_EXIT(db);
34dc7c2f 1367 return (dr);
b128c09f
BB
1368 } else if (do_free_accounting) {
1369 blkptr_t *bp = db->db_blkptr;
1370 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
428870ff 1371 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
b128c09f
BB
1372 /*
1373 * This is only a guess -- if the dbuf is dirty
1374 * in a previous txg, we don't know how much
1375 * space it will use on disk yet. We should
1376 * really have the struct_rwlock to access
1377 * db_blkptr, but since this is just a guess,
1378 * it's OK if we get an odd answer.
1379 */
572e2857 1380 ddt_prefetch(os->os_spa, bp);
b128c09f 1381 dnode_willuse_space(dn, -willfree, tx);
34dc7c2f
BB
1382 }
1383
1384 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1385 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1386 drop_struct_lock = TRUE;
1387 }
1388
b128c09f
BB
1389 if (db->db_level == 0) {
1390 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1391 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1392 }
1393
34dc7c2f
BB
1394 if (db->db_level+1 < dn->dn_nlevels) {
1395 dmu_buf_impl_t *parent = db->db_parent;
1396 dbuf_dirty_record_t *di;
1397 int parent_held = FALSE;
1398
1399 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1400 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1401
1402 parent = dbuf_hold_level(dn, db->db_level+1,
1403 db->db_blkid >> epbs, FTAG);
428870ff 1404 ASSERT(parent != NULL);
34dc7c2f
BB
1405 parent_held = TRUE;
1406 }
1407 if (drop_struct_lock)
1408 rw_exit(&dn->dn_struct_rwlock);
1409 ASSERT3U(db->db_level+1, ==, parent->db_level);
1410 di = dbuf_dirty(parent, tx);
1411 if (parent_held)
1412 dbuf_rele(parent, FTAG);
1413
1414 mutex_enter(&db->db_mtx);
e8b96c60
MA
1415 /*
1416 * Since we've dropped the mutex, it's possible that
1417 * dbuf_undirty() might have changed this out from under us.
1418 */
34dc7c2f
BB
1419 if (db->db_last_dirty == dr ||
1420 dn->dn_object == DMU_META_DNODE_OBJECT) {
1421 mutex_enter(&di->dt.di.dr_mtx);
1422 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1423 ASSERT(!list_link_active(&dr->dr_dirty_node));
1424 list_insert_tail(&di->dt.di.dr_children, dr);
1425 mutex_exit(&di->dt.di.dr_mtx);
1426 dr->dr_parent = di;
1427 }
1428 mutex_exit(&db->db_mtx);
1429 } else {
1430 ASSERT(db->db_level+1 == dn->dn_nlevels);
1431 ASSERT(db->db_blkid < dn->dn_nblkptr);
572e2857 1432 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
34dc7c2f
BB
1433 mutex_enter(&dn->dn_mtx);
1434 ASSERT(!list_link_active(&dr->dr_dirty_node));
1435 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1436 mutex_exit(&dn->dn_mtx);
1437 if (drop_struct_lock)
1438 rw_exit(&dn->dn_struct_rwlock);
1439 }
1440
1441 dnode_setdirty(dn, tx);
572e2857 1442 DB_DNODE_EXIT(db);
34dc7c2f
BB
1443 return (dr);
1444}
1445
13fe0198 1446/*
e49f1e20
WA
1447 * Undirty a buffer in the transaction group referenced by the given
1448 * transaction. Return whether this evicted the dbuf.
13fe0198
MA
1449 */
1450static boolean_t
34dc7c2f
BB
1451dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1452{
572e2857 1453 dnode_t *dn;
34dc7c2f
BB
1454 uint64_t txg = tx->tx_txg;
1455 dbuf_dirty_record_t *dr, **drp;
1456
1457 ASSERT(txg != 0);
428870ff 1458 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
13fe0198
MA
1459 ASSERT0(db->db_level);
1460 ASSERT(MUTEX_HELD(&db->db_mtx));
34dc7c2f 1461
34dc7c2f
BB
1462 /*
1463 * If this buffer is not dirty, we're done.
1464 */
1465 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1466 if (dr->dr_txg <= txg)
1467 break;
13fe0198
MA
1468 if (dr == NULL || dr->dr_txg < txg)
1469 return (B_FALSE);
34dc7c2f 1470 ASSERT(dr->dr_txg == txg);
428870ff 1471 ASSERT(dr->dr_dbuf == db);
34dc7c2f 1472
572e2857
BB
1473 DB_DNODE_ENTER(db);
1474 dn = DB_DNODE(db);
1475
34dc7c2f
BB
1476 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1477
1478 ASSERT(db->db.db_size != 0);
1479
e8b96c60
MA
1480 /*
1481 * Any space we accounted for in dp_dirty_* will be cleaned up by
1482 * dsl_pool_sync(). This is relatively rare so the discrepancy
1483 * is not a big deal.
1484 */
34dc7c2f
BB
1485
1486 *drp = dr->dr_next;
1487
ef3c1dea
GR
1488 /*
1489 * Note that there are three places in dbuf_dirty()
1490 * where this dirty record may be put on a list.
1491 * Make sure to do a list_remove corresponding to
1492 * every one of those list_insert calls.
1493 */
34dc7c2f
BB
1494 if (dr->dr_parent) {
1495 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1496 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1497 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
ef3c1dea
GR
1498 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1499 db->db_level+1 == dn->dn_nlevels) {
b128c09f 1500 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
34dc7c2f
BB
1501 mutex_enter(&dn->dn_mtx);
1502 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1503 mutex_exit(&dn->dn_mtx);
1504 }
572e2857 1505 DB_DNODE_EXIT(db);
34dc7c2f 1506
13fe0198
MA
1507 if (db->db_state != DB_NOFILL) {
1508 dbuf_unoverride(dr);
34dc7c2f 1509
34dc7c2f 1510 ASSERT(db->db_buf != NULL);
13fe0198
MA
1511 ASSERT(dr->dt.dl.dr_data != NULL);
1512 if (dr->dt.dl.dr_data != db->db_buf)
1513 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
34dc7c2f 1514 }
58c4aa00
JL
1515
1516 if (db->db_level != 0) {
1517 mutex_destroy(&dr->dt.di.dr_mtx);
1518 list_destroy(&dr->dt.di.dr_children);
1519 }
1520
34dc7c2f
BB
1521 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1522
1523 ASSERT(db->db_dirtycnt > 0);
1524 db->db_dirtycnt -= 1;
1525
1526 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1527 arc_buf_t *buf = db->db_buf;
1528
428870ff 1529 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
0c66c32d 1530 dbuf_clear_data(db);
13fe0198 1531 VERIFY(arc_buf_remove_ref(buf, db));
34dc7c2f 1532 dbuf_evict(db);
13fe0198 1533 return (B_TRUE);
34dc7c2f
BB
1534 }
1535
13fe0198 1536 return (B_FALSE);
34dc7c2f
BB
1537}
1538
34dc7c2f 1539void
b0bc7a84 1540dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
34dc7c2f 1541{
b0bc7a84 1542 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
34dc7c2f
BB
1543 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1544
1545 ASSERT(tx->tx_txg != 0);
1546 ASSERT(!refcount_is_zero(&db->db_holds));
1547
572e2857
BB
1548 DB_DNODE_ENTER(db);
1549 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
34dc7c2f 1550 rf |= DB_RF_HAVESTRUCT;
572e2857 1551 DB_DNODE_EXIT(db);
34dc7c2f
BB
1552 (void) dbuf_read(db, NULL, rf);
1553 (void) dbuf_dirty(db, tx);
1554}
1555
b128c09f
BB
1556void
1557dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1558{
1559 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1560
1561 db->db_state = DB_NOFILL;
1562
1563 dmu_buf_will_fill(db_fake, tx);
1564}
1565
34dc7c2f
BB
1566void
1567dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1568{
1569 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1570
428870ff 1571 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
1572 ASSERT(tx->tx_txg != 0);
1573 ASSERT(db->db_level == 0);
1574 ASSERT(!refcount_is_zero(&db->db_holds));
1575
1576 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1577 dmu_tx_private_ok(tx));
1578
1579 dbuf_noread(db);
1580 (void) dbuf_dirty(db, tx);
1581}
1582
1583#pragma weak dmu_buf_fill_done = dbuf_fill_done
1584/* ARGSUSED */
1585void
1586dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1587{
1588 mutex_enter(&db->db_mtx);
1589 DBUF_VERIFY(db);
1590
1591 if (db->db_state == DB_FILL) {
1592 if (db->db_level == 0 && db->db_freed_in_flight) {
428870ff 1593 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
1594 /* we were freed while filling */
1595 /* XXX dbuf_undirty? */
1596 bzero(db->db.db_data, db->db.db_size);
1597 db->db_freed_in_flight = FALSE;
1598 }
1599 db->db_state = DB_CACHED;
1600 cv_broadcast(&db->db_changed);
1601 }
1602 mutex_exit(&db->db_mtx);
1603}
1604
9b67f605
MA
1605void
1606dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1607 bp_embedded_type_t etype, enum zio_compress comp,
1608 int uncompressed_size, int compressed_size, int byteorder,
1609 dmu_tx_t *tx)
1610{
1611 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1612 struct dirty_leaf *dl;
1613 dmu_object_type_t type;
1614
1615 DB_DNODE_ENTER(db);
1616 type = DB_DNODE(db)->dn_type;
1617 DB_DNODE_EXIT(db);
1618
1619 ASSERT0(db->db_level);
1620 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1621
1622 dmu_buf_will_not_fill(dbuf, tx);
1623
1624 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1625 dl = &db->db_last_dirty->dt.dl;
1626 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1627 data, comp, uncompressed_size, compressed_size);
1628 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1629 BP_SET_TYPE(&dl->dr_overridden_by, type);
1630 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1631 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1632
1633 dl->dr_override_state = DR_OVERRIDDEN;
1634 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1635}
1636
9babb374
BB
1637/*
1638 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1639 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1640 */
1641void
1642dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1643{
1644 ASSERT(!refcount_is_zero(&db->db_holds));
428870ff 1645 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
9babb374
BB
1646 ASSERT(db->db_level == 0);
1647 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1648 ASSERT(buf != NULL);
1649 ASSERT(arc_buf_size(buf) == db->db.db_size);
1650 ASSERT(tx->tx_txg != 0);
1651
1652 arc_return_buf(buf, db);
1653 ASSERT(arc_released(buf));
1654
1655 mutex_enter(&db->db_mtx);
1656
1657 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1658 cv_wait(&db->db_changed, &db->db_mtx);
1659
1660 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1661
1662 if (db->db_state == DB_CACHED &&
1663 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1664 mutex_exit(&db->db_mtx);
1665 (void) dbuf_dirty(db, tx);
1666 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
13fe0198 1667 VERIFY(arc_buf_remove_ref(buf, db));
428870ff 1668 xuio_stat_wbuf_copied();
9babb374
BB
1669 return;
1670 }
1671
428870ff 1672 xuio_stat_wbuf_nocopy();
9babb374
BB
1673 if (db->db_state == DB_CACHED) {
1674 dbuf_dirty_record_t *dr = db->db_last_dirty;
1675
1676 ASSERT(db->db_buf != NULL);
1677 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1678 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1679 if (!arc_released(db->db_buf)) {
1680 ASSERT(dr->dt.dl.dr_override_state ==
1681 DR_OVERRIDDEN);
1682 arc_release(db->db_buf, db);
1683 }
1684 dr->dt.dl.dr_data = buf;
13fe0198 1685 VERIFY(arc_buf_remove_ref(db->db_buf, db));
9babb374
BB
1686 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1687 arc_release(db->db_buf, db);
13fe0198 1688 VERIFY(arc_buf_remove_ref(db->db_buf, db));
9babb374
BB
1689 }
1690 db->db_buf = NULL;
1691 }
1692 ASSERT(db->db_buf == NULL);
1693 dbuf_set_data(db, buf);
1694 db->db_state = DB_FILL;
1695 mutex_exit(&db->db_mtx);
1696 (void) dbuf_dirty(db, tx);
b0bc7a84 1697 dmu_buf_fill_done(&db->db, tx);
9babb374
BB
1698}
1699
34dc7c2f
BB
1700/*
1701 * "Clear" the contents of this dbuf. This will mark the dbuf
e8b96c60 1702 * EVICTING and clear *most* of its references. Unfortunately,
34dc7c2f
BB
1703 * when we are not holding the dn_dbufs_mtx, we can't clear the
1704 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1705 * in this case. For callers from the DMU we will usually see:
bd089c54 1706 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
34dc7c2f 1707 * For the arc callback, we will usually see:
572e2857 1708 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
34dc7c2f 1709 * Sometimes, though, we will get a mix of these two:
bd089c54 1710 * DMU: dbuf_clear()->arc_clear_callback()
34dc7c2f 1711 * ARC: dbuf_do_evict()->dbuf_destroy()
bd089c54
MA
1712 *
1713 * This routine will dissociate the dbuf from the arc, by calling
1714 * arc_clear_callback(), but will not evict the data from the ARC.
34dc7c2f
BB
1715 */
1716void
1717dbuf_clear(dmu_buf_impl_t *db)
1718{
572e2857 1719 dnode_t *dn;
34dc7c2f 1720 dmu_buf_impl_t *parent = db->db_parent;
572e2857 1721 dmu_buf_impl_t *dndb;
bd089c54 1722 boolean_t dbuf_gone = B_FALSE;
34dc7c2f
BB
1723
1724 ASSERT(MUTEX_HELD(&db->db_mtx));
1725 ASSERT(refcount_is_zero(&db->db_holds));
1726
1727 dbuf_evict_user(db);
1728
1729 if (db->db_state == DB_CACHED) {
1730 ASSERT(db->db.db_data != NULL);
428870ff 1731 if (db->db_blkid == DMU_BONUS_BLKID) {
34dc7c2f 1732 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
d164b209 1733 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
34dc7c2f
BB
1734 }
1735 db->db.db_data = NULL;
1736 db->db_state = DB_UNCACHED;
1737 }
1738
b128c09f 1739 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
34dc7c2f
BB
1740 ASSERT(db->db_data_pending == NULL);
1741
1742 db->db_state = DB_EVICTING;
1743 db->db_blkptr = NULL;
1744
572e2857
BB
1745 DB_DNODE_ENTER(db);
1746 dn = DB_DNODE(db);
1747 dndb = dn->dn_dbuf;
428870ff 1748 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
8951cb8d 1749 avl_remove(&dn->dn_dbufs, db);
73ad4a9f 1750 atomic_dec_32(&dn->dn_dbufs_count);
572e2857
BB
1751 membar_producer();
1752 DB_DNODE_EXIT(db);
1753 /*
1754 * Decrementing the dbuf count means that the hold corresponding
1755 * to the removed dbuf is no longer discounted in dnode_move(),
1756 * so the dnode cannot be moved until after we release the hold.
1757 * The membar_producer() ensures visibility of the decremented
1758 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1759 * release any lock.
1760 */
34dc7c2f 1761 dnode_rele(dn, db);
572e2857
BB
1762 db->db_dnode_handle = NULL;
1763 } else {
1764 DB_DNODE_EXIT(db);
34dc7c2f
BB
1765 }
1766
1767 if (db->db_buf)
bd089c54 1768 dbuf_gone = arc_clear_callback(db->db_buf);
34dc7c2f
BB
1769
1770 if (!dbuf_gone)
1771 mutex_exit(&db->db_mtx);
1772
1773 /*
572e2857 1774 * If this dbuf is referenced from an indirect dbuf,
34dc7c2f
BB
1775 * decrement the ref count on the indirect dbuf.
1776 */
1777 if (parent && parent != dndb)
1778 dbuf_rele(parent, db);
1779}
1780
bf701a83
BB
1781__attribute__((always_inline))
1782static inline int
34dc7c2f 1783dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
fc5bb51f 1784 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
34dc7c2f
BB
1785{
1786 int nlevels, epbs;
1787
1788 *parentp = NULL;
1789 *bpp = NULL;
1790
428870ff
BB
1791 ASSERT(blkid != DMU_BONUS_BLKID);
1792
1793 if (blkid == DMU_SPILL_BLKID) {
1794 mutex_enter(&dn->dn_mtx);
1795 if (dn->dn_have_spill &&
1796 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1797 *bpp = &dn->dn_phys->dn_spill;
1798 else
1799 *bpp = NULL;
1800 dbuf_add_ref(dn->dn_dbuf, NULL);
1801 *parentp = dn->dn_dbuf;
1802 mutex_exit(&dn->dn_mtx);
1803 return (0);
1804 }
34dc7c2f
BB
1805
1806 if (dn->dn_phys->dn_nlevels == 0)
1807 nlevels = 1;
1808 else
1809 nlevels = dn->dn_phys->dn_nlevels;
1810
1811 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1812
1813 ASSERT3U(level * epbs, <, 64);
1814 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1815 if (level >= nlevels ||
1816 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1817 /* the buffer has no parent yet */
2e528b49 1818 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1819 } else if (level < nlevels-1) {
1820 /* this block is referenced from an indirect block */
fc5bb51f
BB
1821 int err;
1822 if (dh == NULL) {
1823 err = dbuf_hold_impl(dn, level+1, blkid >> epbs,
1824 fail_sparse, NULL, parentp);
d1d7e268 1825 } else {
fc5bb51f
BB
1826 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
1827 blkid >> epbs, fail_sparse, NULL,
1828 parentp, dh->dh_depth + 1);
1829 err = __dbuf_hold_impl(dh + 1);
1830 }
34dc7c2f
BB
1831 if (err)
1832 return (err);
1833 err = dbuf_read(*parentp, NULL,
1834 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1835 if (err) {
1836 dbuf_rele(*parentp, NULL);
1837 *parentp = NULL;
1838 return (err);
1839 }
1840 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1841 (blkid & ((1ULL << epbs) - 1));
1842 return (0);
1843 } else {
1844 /* the block is referenced from the dnode */
1845 ASSERT3U(level, ==, nlevels-1);
1846 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1847 blkid < dn->dn_phys->dn_nblkptr);
1848 if (dn->dn_dbuf) {
1849 dbuf_add_ref(dn->dn_dbuf, NULL);
1850 *parentp = dn->dn_dbuf;
1851 }
1852 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1853 return (0);
1854 }
1855}
1856
1857static dmu_buf_impl_t *
1858dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1859 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1860{
428870ff 1861 objset_t *os = dn->dn_objset;
34dc7c2f
BB
1862 dmu_buf_impl_t *db, *odb;
1863
1864 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1865 ASSERT(dn->dn_type != DMU_OT_NONE);
1866
79c76d5b 1867 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
34dc7c2f
BB
1868
1869 db->db_objset = os;
1870 db->db.db_object = dn->dn_object;
1871 db->db_level = level;
1872 db->db_blkid = blkid;
1873 db->db_last_dirty = NULL;
1874 db->db_dirtycnt = 0;
572e2857 1875 db->db_dnode_handle = dn->dn_handle;
34dc7c2f
BB
1876 db->db_parent = parent;
1877 db->db_blkptr = blkptr;
1878
0c66c32d 1879 db->db_user = NULL;
34dc7c2f
BB
1880 db->db_immediate_evict = 0;
1881 db->db_freed_in_flight = 0;
1882
428870ff 1883 if (blkid == DMU_BONUS_BLKID) {
34dc7c2f
BB
1884 ASSERT3P(parent, ==, dn->dn_dbuf);
1885 db->db.db_size = DN_MAX_BONUSLEN -
1886 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1887 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
428870ff 1888 db->db.db_offset = DMU_BONUS_BLKID;
34dc7c2f
BB
1889 db->db_state = DB_UNCACHED;
1890 /* the bonus dbuf is not placed in the hash table */
d164b209 1891 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
34dc7c2f 1892 return (db);
428870ff
BB
1893 } else if (blkid == DMU_SPILL_BLKID) {
1894 db->db.db_size = (blkptr != NULL) ?
1895 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1896 db->db.db_offset = 0;
34dc7c2f
BB
1897 } else {
1898 int blocksize =
e8b96c60 1899 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
34dc7c2f
BB
1900 db->db.db_size = blocksize;
1901 db->db.db_offset = db->db_blkid * blocksize;
1902 }
1903
1904 /*
1905 * Hold the dn_dbufs_mtx while we get the new dbuf
1906 * in the hash table *and* added to the dbufs list.
1907 * This prevents a possible deadlock with someone
1908 * trying to look up this dbuf before its added to the
1909 * dn_dbufs list.
1910 */
1911 mutex_enter(&dn->dn_dbufs_mtx);
1912 db->db_state = DB_EVICTING;
1913 if ((odb = dbuf_hash_insert(db)) != NULL) {
1914 /* someone else inserted it first */
1915 kmem_cache_free(dbuf_cache, db);
1916 mutex_exit(&dn->dn_dbufs_mtx);
1917 return (odb);
1918 }
8951cb8d 1919 avl_add(&dn->dn_dbufs, db);
b663a23d
MA
1920 if (db->db_level == 0 && db->db_blkid >=
1921 dn->dn_unlisted_l0_blkid)
1922 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
34dc7c2f
BB
1923 db->db_state = DB_UNCACHED;
1924 mutex_exit(&dn->dn_dbufs_mtx);
d164b209 1925 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
34dc7c2f
BB
1926
1927 if (parent && parent != dn->dn_dbuf)
1928 dbuf_add_ref(parent, db);
1929
1930 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1931 refcount_count(&dn->dn_holds) > 0);
1932 (void) refcount_add(&dn->dn_holds, db);
73ad4a9f 1933 atomic_inc_32(&dn->dn_dbufs_count);
34dc7c2f
BB
1934
1935 dprintf_dbuf(db, "db=%p\n", db);
1936
1937 return (db);
1938}
1939
1940static int
1941dbuf_do_evict(void *private)
1942{
bd089c54 1943 dmu_buf_impl_t *db = private;
34dc7c2f
BB
1944
1945 if (!MUTEX_HELD(&db->db_mtx))
1946 mutex_enter(&db->db_mtx);
1947
1948 ASSERT(refcount_is_zero(&db->db_holds));
1949
1950 if (db->db_state != DB_EVICTING) {
1951 ASSERT(db->db_state == DB_CACHED);
1952 DBUF_VERIFY(db);
1953 db->db_buf = NULL;
1954 dbuf_evict(db);
1955 } else {
1956 mutex_exit(&db->db_mtx);
1957 dbuf_destroy(db);
1958 }
1959 return (0);
1960}
1961
1962static void
1963dbuf_destroy(dmu_buf_impl_t *db)
1964{
1965 ASSERT(refcount_is_zero(&db->db_holds));
1966
428870ff 1967 if (db->db_blkid != DMU_BONUS_BLKID) {
34dc7c2f
BB
1968 /*
1969 * If this dbuf is still on the dn_dbufs list,
1970 * remove it from that list.
1971 */
572e2857
BB
1972 if (db->db_dnode_handle != NULL) {
1973 dnode_t *dn;
34dc7c2f 1974
572e2857
BB
1975 DB_DNODE_ENTER(db);
1976 dn = DB_DNODE(db);
34dc7c2f 1977 mutex_enter(&dn->dn_dbufs_mtx);
8951cb8d 1978 avl_remove(&dn->dn_dbufs, db);
73ad4a9f 1979 atomic_dec_32(&dn->dn_dbufs_count);
34dc7c2f 1980 mutex_exit(&dn->dn_dbufs_mtx);
572e2857
BB
1981 DB_DNODE_EXIT(db);
1982 /*
1983 * Decrementing the dbuf count means that the hold
1984 * corresponding to the removed dbuf is no longer
1985 * discounted in dnode_move(), so the dnode cannot be
1986 * moved until after we release the hold.
1987 */
34dc7c2f 1988 dnode_rele(dn, db);
572e2857 1989 db->db_dnode_handle = NULL;
34dc7c2f
BB
1990 }
1991 dbuf_hash_remove(db);
1992 }
1993 db->db_parent = NULL;
1994 db->db_buf = NULL;
1995
34dc7c2f
BB
1996 ASSERT(db->db.db_data == NULL);
1997 ASSERT(db->db_hash_next == NULL);
1998 ASSERT(db->db_blkptr == NULL);
1999 ASSERT(db->db_data_pending == NULL);
2000
2001 kmem_cache_free(dbuf_cache, db);
d164b209 2002 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
34dc7c2f
BB
2003}
2004
2005void
e8b96c60 2006dbuf_prefetch(dnode_t *dn, uint64_t blkid, zio_priority_t prio)
34dc7c2f
BB
2007{
2008 dmu_buf_impl_t *db = NULL;
2009 blkptr_t *bp = NULL;
2010
428870ff 2011 ASSERT(blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
2012 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2013
2014 if (dnode_block_freed(dn, blkid))
2015 return;
2016
2017 /* dbuf_find() returns with db_mtx held */
6ebebace 2018 if ((db = dbuf_find(dn->dn_objset, dn->dn_object, 0, blkid))) {
572e2857
BB
2019 /*
2020 * This dbuf is already in the cache. We assume that
2021 * it is already CACHED, or else about to be either
2022 * read or filled.
2023 */
34dc7c2f 2024 mutex_exit(&db->db_mtx);
572e2857 2025 return;
34dc7c2f
BB
2026 }
2027
fc5bb51f 2028 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp, NULL) == 0) {
9b67f605 2029 if (bp && !BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
428870ff 2030 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
34dc7c2f 2031 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
5dbd68a3 2032 zbookmark_phys_t zb;
428870ff
BB
2033
2034 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
2035 dn->dn_object, 0, blkid);
34dc7c2f 2036
294f6806 2037 (void) arc_read(NULL, dn->dn_objset->os_spa,
e8b96c60 2038 bp, NULL, NULL, prio,
34dc7c2f
BB
2039 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2040 &aflags, &zb);
2041 }
2042 if (db)
2043 dbuf_rele(db, NULL);
2044 }
2045}
2046
d1d7e268 2047#define DBUF_HOLD_IMPL_MAX_DEPTH 20
fc5bb51f 2048
34dc7c2f
BB
2049/*
2050 * Returns with db_holds incremented, and db_mtx not held.
2051 * Note: dn_struct_rwlock must be held.
2052 */
fc5bb51f
BB
2053static int
2054__dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
34dc7c2f 2055{
fc5bb51f
BB
2056 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
2057 dh->dh_parent = NULL;
34dc7c2f 2058
fc5bb51f
BB
2059 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
2060 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
2061 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
34dc7c2f 2062
fc5bb51f 2063 *(dh->dh_dbp) = NULL;
34dc7c2f
BB
2064top:
2065 /* dbuf_find() returns with db_mtx held */
6ebebace
JG
2066 dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2067 dh->dh_level, dh->dh_blkid);
fc5bb51f
BB
2068
2069 if (dh->dh_db == NULL) {
2070 dh->dh_bp = NULL;
2071
2072 ASSERT3P(dh->dh_parent, ==, NULL);
2073 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2074 dh->dh_fail_sparse, &dh->dh_parent,
2075 &dh->dh_bp, dh);
2076 if (dh->dh_fail_sparse) {
d1d7e268
MK
2077 if (dh->dh_err == 0 &&
2078 dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
2e528b49 2079 dh->dh_err = SET_ERROR(ENOENT);
fc5bb51f
BB
2080 if (dh->dh_err) {
2081 if (dh->dh_parent)
2082 dbuf_rele(dh->dh_parent, NULL);
2083 return (dh->dh_err);
34dc7c2f
BB
2084 }
2085 }
fc5bb51f
BB
2086 if (dh->dh_err && dh->dh_err != ENOENT)
2087 return (dh->dh_err);
2088 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2089 dh->dh_parent, dh->dh_bp);
34dc7c2f
BB
2090 }
2091
fc5bb51f
BB
2092 if (dh->dh_db->db_buf && refcount_is_zero(&dh->dh_db->db_holds)) {
2093 arc_buf_add_ref(dh->dh_db->db_buf, dh->dh_db);
2094 if (dh->dh_db->db_buf->b_data == NULL) {
2095 dbuf_clear(dh->dh_db);
2096 if (dh->dh_parent) {
2097 dbuf_rele(dh->dh_parent, NULL);
2098 dh->dh_parent = NULL;
34dc7c2f
BB
2099 }
2100 goto top;
2101 }
fc5bb51f 2102 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
34dc7c2f
BB
2103 }
2104
fc5bb51f 2105 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
34dc7c2f
BB
2106
2107 /*
2108 * If this buffer is currently syncing out, and we are are
2109 * still referencing it from db_data, we need to make a copy
2110 * of it in case we decide we want to dirty it again in this txg.
2111 */
fc5bb51f
BB
2112 if (dh->dh_db->db_level == 0 &&
2113 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
2114 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
2115 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
2116 dh->dh_dr = dh->dh_db->db_data_pending;
2117
2118 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
2119 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
2120
2121 dbuf_set_data(dh->dh_db,
2122 arc_buf_alloc(dh->dh_dn->dn_objset->os_spa,
2123 dh->dh_db->db.db_size, dh->dh_db, dh->dh_type));
2124 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
2125 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
34dc7c2f
BB
2126 }
2127 }
2128
fc5bb51f 2129 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
fc5bb51f
BB
2130 DBUF_VERIFY(dh->dh_db);
2131 mutex_exit(&dh->dh_db->db_mtx);
34dc7c2f
BB
2132
2133 /* NOTE: we can't rele the parent until after we drop the db_mtx */
fc5bb51f
BB
2134 if (dh->dh_parent)
2135 dbuf_rele(dh->dh_parent, NULL);
34dc7c2f 2136
fc5bb51f
BB
2137 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
2138 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
2139 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
2140 *(dh->dh_dbp) = dh->dh_db;
34dc7c2f
BB
2141
2142 return (0);
2143}
2144
fc5bb51f
BB
2145/*
2146 * The following code preserves the recursive function dbuf_hold_impl()
2147 * but moves the local variables AND function arguments to the heap to
2148 * minimize the stack frame size. Enough space is initially allocated
2149 * on the stack for 20 levels of recursion.
2150 */
2151int
2152dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2153 void *tag, dmu_buf_impl_t **dbp)
2154{
2155 struct dbuf_hold_impl_data *dh;
2156 int error;
2157
d1d7e268 2158 dh = kmem_zalloc(sizeof (struct dbuf_hold_impl_data) *
79c76d5b 2159 DBUF_HOLD_IMPL_MAX_DEPTH, KM_SLEEP);
fc5bb51f
BB
2160 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse, tag, dbp, 0);
2161
2162 error = __dbuf_hold_impl(dh);
2163
d1d7e268 2164 kmem_free(dh, sizeof (struct dbuf_hold_impl_data) *
fc5bb51f
BB
2165 DBUF_HOLD_IMPL_MAX_DEPTH);
2166
2167 return (error);
2168}
2169
2170static void
2171__dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2172 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2173 void *tag, dmu_buf_impl_t **dbp, int depth)
2174{
2175 dh->dh_dn = dn;
2176 dh->dh_level = level;
2177 dh->dh_blkid = blkid;
2178 dh->dh_fail_sparse = fail_sparse;
2179 dh->dh_tag = tag;
2180 dh->dh_dbp = dbp;
2181 dh->dh_depth = depth;
2182}
2183
34dc7c2f
BB
2184dmu_buf_impl_t *
2185dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2186{
2187 dmu_buf_impl_t *db;
2188 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
2189 return (err ? NULL : db);
2190}
2191
2192dmu_buf_impl_t *
2193dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2194{
2195 dmu_buf_impl_t *db;
2196 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2197 return (err ? NULL : db);
2198}
2199
2200void
2201dbuf_create_bonus(dnode_t *dn)
2202{
2203 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2204
2205 ASSERT(dn->dn_bonus == NULL);
428870ff
BB
2206 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2207}
2208
2209int
2210dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2211{
2212 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
572e2857
BB
2213 dnode_t *dn;
2214
428870ff 2215 if (db->db_blkid != DMU_SPILL_BLKID)
2e528b49 2216 return (SET_ERROR(ENOTSUP));
428870ff
BB
2217 if (blksz == 0)
2218 blksz = SPA_MINBLOCKSIZE;
f1512ee6
MA
2219 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2220 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
428870ff 2221
572e2857
BB
2222 DB_DNODE_ENTER(db);
2223 dn = DB_DNODE(db);
2224 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
428870ff 2225 dbuf_new_size(db, blksz, tx);
572e2857
BB
2226 rw_exit(&dn->dn_struct_rwlock);
2227 DB_DNODE_EXIT(db);
428870ff
BB
2228
2229 return (0);
2230}
2231
2232void
2233dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2234{
2235 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
34dc7c2f
BB
2236}
2237
2238#pragma weak dmu_buf_add_ref = dbuf_add_ref
2239void
2240dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2241{
1fde1e37 2242 VERIFY(refcount_add(&db->db_holds, tag) > 1);
34dc7c2f
BB
2243}
2244
6ebebace
JG
2245#pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2246boolean_t
2247dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2248 void *tag)
2249{
2250 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2251 dmu_buf_impl_t *found_db;
2252 boolean_t result = B_FALSE;
2253
d617648c 2254 if (blkid == DMU_BONUS_BLKID)
6ebebace
JG
2255 found_db = dbuf_find_bonus(os, obj);
2256 else
2257 found_db = dbuf_find(os, obj, 0, blkid);
2258
2259 if (found_db != NULL) {
2260 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2261 (void) refcount_add(&db->db_holds, tag);
2262 result = B_TRUE;
2263 }
d617648c 2264 mutex_exit(&found_db->db_mtx);
6ebebace
JG
2265 }
2266 return (result);
2267}
2268
572e2857
BB
2269/*
2270 * If you call dbuf_rele() you had better not be referencing the dnode handle
2271 * unless you have some other direct or indirect hold on the dnode. (An indirect
2272 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2273 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2274 * dnode's parent dbuf evicting its dnode handles.
2275 */
34dc7c2f
BB
2276void
2277dbuf_rele(dmu_buf_impl_t *db, void *tag)
428870ff
BB
2278{
2279 mutex_enter(&db->db_mtx);
2280 dbuf_rele_and_unlock(db, tag);
2281}
2282
b0bc7a84
MG
2283void
2284dmu_buf_rele(dmu_buf_t *db, void *tag)
2285{
2286 dbuf_rele((dmu_buf_impl_t *)db, tag);
2287}
2288
428870ff
BB
2289/*
2290 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2291 * db_dirtycnt and db_holds to be updated atomically.
2292 */
2293void
2294dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
34dc7c2f
BB
2295{
2296 int64_t holds;
2297
428870ff 2298 ASSERT(MUTEX_HELD(&db->db_mtx));
34dc7c2f
BB
2299 DBUF_VERIFY(db);
2300
572e2857
BB
2301 /*
2302 * Remove the reference to the dbuf before removing its hold on the
2303 * dnode so we can guarantee in dnode_move() that a referenced bonus
2304 * buffer has a corresponding dnode hold.
2305 */
34dc7c2f
BB
2306 holds = refcount_remove(&db->db_holds, tag);
2307 ASSERT(holds >= 0);
2308
2309 /*
2310 * We can't freeze indirects if there is a possibility that they
2311 * may be modified in the current syncing context.
2312 */
2313 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2314 arc_buf_freeze(db->db_buf);
2315
2316 if (holds == db->db_dirtycnt &&
2317 db->db_level == 0 && db->db_immediate_evict)
2318 dbuf_evict_user(db);
2319
2320 if (holds == 0) {
428870ff 2321 if (db->db_blkid == DMU_BONUS_BLKID) {
4c7b7eed 2322 dnode_t *dn;
572e2857
BB
2323
2324 /*
4c7b7eed
JG
2325 * If the dnode moves here, we cannot cross this
2326 * barrier until the move completes.
572e2857
BB
2327 */
2328 DB_DNODE_ENTER(db);
4c7b7eed
JG
2329
2330 dn = DB_DNODE(db);
2331 atomic_dec_32(&dn->dn_dbufs_count);
2332
2333 /*
2334 * Decrementing the dbuf count means that the bonus
2335 * buffer's dnode hold is no longer discounted in
2336 * dnode_move(). The dnode cannot move until after
2337 * the dnode_rele_and_unlock() below.
2338 */
572e2857 2339 DB_DNODE_EXIT(db);
4c7b7eed
JG
2340
2341 /*
2342 * Do not reference db after its lock is dropped.
2343 * Another thread may evict it.
2344 */
2345 mutex_exit(&db->db_mtx);
2346
572e2857 2347 /*
4c7b7eed
JG
2348 * If the dnode has been freed, evict the bonus
2349 * buffer immediately. The data in the bonus
2350 * buffer is no longer relevant and this prevents
2351 * a stale bonus buffer from being associated
2352 * with this dnode_t should the dnode_t be reused
2353 * prior to being destroyed.
572e2857 2354 */
4c7b7eed
JG
2355 mutex_enter(&dn->dn_mtx);
2356 if (dn->dn_type == DMU_OT_NONE ||
2357 dn->dn_free_txg != 0) {
2358 /*
2359 * Drop dn_mtx. It is a leaf lock and
2360 * cannot be held when dnode_evict_bonus()
2361 * acquires other locks in order to
2362 * perform the eviction.
2363 *
2364 * Freed dnodes cannot be reused until the
2365 * last hold is released. Since this bonus
2366 * buffer has a hold, the dnode will remain
2367 * in the free state, even without dn_mtx
2368 * held, until the dnode_rele_and_unlock()
2369 * below.
2370 */
2371 mutex_exit(&dn->dn_mtx);
2372 dnode_evict_bonus(dn);
2373 mutex_enter(&dn->dn_mtx);
2374 }
2375 dnode_rele_and_unlock(dn, db);
34dc7c2f
BB
2376 } else if (db->db_buf == NULL) {
2377 /*
2378 * This is a special case: we never associated this
2379 * dbuf with any data allocated from the ARC.
2380 */
b128c09f
BB
2381 ASSERT(db->db_state == DB_UNCACHED ||
2382 db->db_state == DB_NOFILL);
34dc7c2f
BB
2383 dbuf_evict(db);
2384 } else if (arc_released(db->db_buf)) {
2385 arc_buf_t *buf = db->db_buf;
2386 /*
2387 * This dbuf has anonymous data associated with it.
2388 */
0c66c32d 2389 dbuf_clear_data(db);
13fe0198 2390 VERIFY(arc_buf_remove_ref(buf, db));
34dc7c2f
BB
2391 dbuf_evict(db);
2392 } else {
13fe0198 2393 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
1eb5bfa3
GW
2394
2395 /*
2396 * A dbuf will be eligible for eviction if either the
2397 * 'primarycache' property is set or a duplicate
2398 * copy of this buffer is already cached in the arc.
2399 *
2400 * In the case of the 'primarycache' a buffer
2401 * is considered for eviction if it matches the
2402 * criteria set in the property.
2403 *
2404 * To decide if our buffer is considered a
2405 * duplicate, we must call into the arc to determine
2406 * if multiple buffers are referencing the same
2407 * block on-disk. If so, then we simply evict
2408 * ourselves.
2409 */
bd089c54
MA
2410 if (!DBUF_IS_CACHEABLE(db)) {
2411 if (db->db_blkptr != NULL &&
2412 !BP_IS_HOLE(db->db_blkptr) &&
2413 !BP_IS_EMBEDDED(db->db_blkptr)) {
2414 spa_t *spa =
2415 dmu_objset_spa(db->db_objset);
2416 blkptr_t bp = *db->db_blkptr;
2417 dbuf_clear(db);
2418 arc_freed(spa, &bp);
2419 } else {
2420 dbuf_clear(db);
2421 }
0c66c32d
JG
2422 } else if (db->db_objset->os_evicting ||
2423 arc_buf_eviction_needed(db->db_buf)) {
b128c09f 2424 dbuf_clear(db);
bd089c54 2425 } else {
b128c09f 2426 mutex_exit(&db->db_mtx);
bd089c54 2427 }
34dc7c2f
BB
2428 }
2429 } else {
2430 mutex_exit(&db->db_mtx);
2431 }
2432}
2433
2434#pragma weak dmu_buf_refcount = dbuf_refcount
2435uint64_t
2436dbuf_refcount(dmu_buf_impl_t *db)
2437{
2438 return (refcount_count(&db->db_holds));
2439}
2440
2441void *
0c66c32d
JG
2442dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2443 dmu_buf_user_t *new_user)
34dc7c2f 2444{
0c66c32d
JG
2445 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2446
2447 mutex_enter(&db->db_mtx);
2448 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2449 if (db->db_user == old_user)
2450 db->db_user = new_user;
2451 else
2452 old_user = db->db_user;
2453 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2454 mutex_exit(&db->db_mtx);
2455
2456 return (old_user);
34dc7c2f
BB
2457}
2458
2459void *
0c66c32d 2460dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
34dc7c2f 2461{
0c66c32d 2462 return (dmu_buf_replace_user(db_fake, NULL, user));
34dc7c2f
BB
2463}
2464
2465void *
0c66c32d 2466dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
34dc7c2f
BB
2467{
2468 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
34dc7c2f 2469
0c66c32d
JG
2470 db->db_immediate_evict = TRUE;
2471 return (dmu_buf_set_user(db_fake, user));
2472}
34dc7c2f 2473
0c66c32d
JG
2474void *
2475dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2476{
2477 return (dmu_buf_replace_user(db_fake, user, NULL));
34dc7c2f
BB
2478}
2479
2480void *
2481dmu_buf_get_user(dmu_buf_t *db_fake)
2482{
2483 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
34dc7c2f 2484
0c66c32d
JG
2485 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2486 return (db->db_user);
2487}
2488
2489void
2490dmu_buf_user_evict_wait()
2491{
2492 taskq_wait(dbu_evict_taskq);
34dc7c2f
BB
2493}
2494
9babb374
BB
2495boolean_t
2496dmu_buf_freeable(dmu_buf_t *dbuf)
2497{
2498 boolean_t res = B_FALSE;
2499 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2500
2501 if (db->db_blkptr)
2502 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
428870ff 2503 db->db_blkptr, db->db_blkptr->blk_birth);
9babb374
BB
2504
2505 return (res);
2506}
2507
03c6040b
GW
2508blkptr_t *
2509dmu_buf_get_blkptr(dmu_buf_t *db)
2510{
2511 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2512 return (dbi->db_blkptr);
2513}
2514
34dc7c2f
BB
2515static void
2516dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2517{
2518 /* ASSERT(dmu_tx_is_syncing(tx) */
2519 ASSERT(MUTEX_HELD(&db->db_mtx));
2520
2521 if (db->db_blkptr != NULL)
2522 return;
2523
428870ff
BB
2524 if (db->db_blkid == DMU_SPILL_BLKID) {
2525 db->db_blkptr = &dn->dn_phys->dn_spill;
2526 BP_ZERO(db->db_blkptr);
2527 return;
2528 }
34dc7c2f
BB
2529 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2530 /*
2531 * This buffer was allocated at a time when there was
2532 * no available blkptrs from the dnode, or it was
2533 * inappropriate to hook it in (i.e., nlevels mis-match).
2534 */
2535 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2536 ASSERT(db->db_parent == NULL);
2537 db->db_parent = dn->dn_dbuf;
2538 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2539 DBUF_VERIFY(db);
2540 } else {
2541 dmu_buf_impl_t *parent = db->db_parent;
2542 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2543
2544 ASSERT(dn->dn_phys->dn_nlevels > 1);
2545 if (parent == NULL) {
2546 mutex_exit(&db->db_mtx);
2547 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2548 (void) dbuf_hold_impl(dn, db->db_level+1,
2549 db->db_blkid >> epbs, FALSE, db, &parent);
2550 rw_exit(&dn->dn_struct_rwlock);
2551 mutex_enter(&db->db_mtx);
2552 db->db_parent = parent;
2553 }
2554 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2555 (db->db_blkid & ((1ULL << epbs) - 1));
2556 DBUF_VERIFY(db);
2557 }
2558}
2559
d1d7e268
MK
2560/*
2561 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
60948de1
BB
2562 * is critical the we not allow the compiler to inline this function in to
2563 * dbuf_sync_list() thereby drastically bloating the stack usage.
2564 */
2565noinline static void
34dc7c2f
BB
2566dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2567{
2568 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857 2569 dnode_t *dn;
34dc7c2f
BB
2570 zio_t *zio;
2571
2572 ASSERT(dmu_tx_is_syncing(tx));
2573
2574 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2575
2576 mutex_enter(&db->db_mtx);
2577
2578 ASSERT(db->db_level > 0);
2579 DBUF_VERIFY(db);
2580
e49f1e20 2581 /* Read the block if it hasn't been read yet. */
34dc7c2f
BB
2582 if (db->db_buf == NULL) {
2583 mutex_exit(&db->db_mtx);
2584 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2585 mutex_enter(&db->db_mtx);
2586 }
2587 ASSERT3U(db->db_state, ==, DB_CACHED);
34dc7c2f
BB
2588 ASSERT(db->db_buf != NULL);
2589
572e2857
BB
2590 DB_DNODE_ENTER(db);
2591 dn = DB_DNODE(db);
e49f1e20 2592 /* Indirect block size must match what the dnode thinks it is. */
572e2857 2593 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
34dc7c2f 2594 dbuf_check_blkptr(dn, db);
572e2857 2595 DB_DNODE_EXIT(db);
34dc7c2f 2596
e49f1e20 2597 /* Provide the pending dirty record to child dbufs */
34dc7c2f
BB
2598 db->db_data_pending = dr;
2599
34dc7c2f 2600 mutex_exit(&db->db_mtx);
b128c09f 2601 dbuf_write(dr, db->db_buf, tx);
34dc7c2f
BB
2602
2603 zio = dr->dr_zio;
2604 mutex_enter(&dr->dt.di.dr_mtx);
2605 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2606 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2607 mutex_exit(&dr->dt.di.dr_mtx);
2608 zio_nowait(zio);
2609}
2610
d1d7e268
MK
2611/*
2612 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
60948de1
BB
2613 * critical the we not allow the compiler to inline this function in to
2614 * dbuf_sync_list() thereby drastically bloating the stack usage.
2615 */
2616noinline static void
34dc7c2f
BB
2617dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2618{
2619 arc_buf_t **datap = &dr->dt.dl.dr_data;
2620 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857
BB
2621 dnode_t *dn;
2622 objset_t *os;
34dc7c2f 2623 uint64_t txg = tx->tx_txg;
34dc7c2f
BB
2624
2625 ASSERT(dmu_tx_is_syncing(tx));
2626
2627 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2628
2629 mutex_enter(&db->db_mtx);
2630 /*
2631 * To be synced, we must be dirtied. But we
2632 * might have been freed after the dirty.
2633 */
2634 if (db->db_state == DB_UNCACHED) {
2635 /* This buffer has been freed since it was dirtied */
2636 ASSERT(db->db.db_data == NULL);
2637 } else if (db->db_state == DB_FILL) {
2638 /* This buffer was freed and is now being re-filled */
2639 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2640 } else {
b128c09f 2641 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
34dc7c2f
BB
2642 }
2643 DBUF_VERIFY(db);
2644
572e2857
BB
2645 DB_DNODE_ENTER(db);
2646 dn = DB_DNODE(db);
2647
428870ff
BB
2648 if (db->db_blkid == DMU_SPILL_BLKID) {
2649 mutex_enter(&dn->dn_mtx);
2650 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2651 mutex_exit(&dn->dn_mtx);
2652 }
2653
34dc7c2f
BB
2654 /*
2655 * If this is a bonus buffer, simply copy the bonus data into the
2656 * dnode. It will be written out when the dnode is synced (and it
2657 * will be synced, since it must have been dirty for dbuf_sync to
2658 * be called).
2659 */
428870ff 2660 if (db->db_blkid == DMU_BONUS_BLKID) {
34dc7c2f
BB
2661 dbuf_dirty_record_t **drp;
2662
2663 ASSERT(*datap != NULL);
c99c9001 2664 ASSERT0(db->db_level);
34dc7c2f
BB
2665 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2666 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
572e2857
BB
2667 DB_DNODE_EXIT(db);
2668
34dc7c2f
BB
2669 if (*datap != db->db.db_data) {
2670 zio_buf_free(*datap, DN_MAX_BONUSLEN);
d164b209 2671 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
34dc7c2f
BB
2672 }
2673 db->db_data_pending = NULL;
2674 drp = &db->db_last_dirty;
2675 while (*drp != dr)
2676 drp = &(*drp)->dr_next;
2677 ASSERT(dr->dr_next == NULL);
428870ff 2678 ASSERT(dr->dr_dbuf == db);
34dc7c2f 2679 *drp = dr->dr_next;
753972fc
BB
2680 if (dr->dr_dbuf->db_level != 0) {
2681 mutex_destroy(&dr->dt.di.dr_mtx);
2682 list_destroy(&dr->dt.di.dr_children);
2683 }
34dc7c2f
BB
2684 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2685 ASSERT(db->db_dirtycnt > 0);
2686 db->db_dirtycnt -= 1;
428870ff 2687 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
34dc7c2f
BB
2688 return;
2689 }
2690
572e2857
BB
2691 os = dn->dn_objset;
2692
34dc7c2f
BB
2693 /*
2694 * This function may have dropped the db_mtx lock allowing a dmu_sync
2695 * operation to sneak in. As a result, we need to ensure that we
2696 * don't check the dr_override_state until we have returned from
2697 * dbuf_check_blkptr.
2698 */
2699 dbuf_check_blkptr(dn, db);
2700
2701 /*
572e2857 2702 * If this buffer is in the middle of an immediate write,
34dc7c2f
BB
2703 * wait for the synchronous IO to complete.
2704 */
2705 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2706 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2707 cv_wait(&db->db_changed, &db->db_mtx);
2708 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2709 }
2710
9babb374
BB
2711 if (db->db_state != DB_NOFILL &&
2712 dn->dn_object != DMU_META_DNODE_OBJECT &&
2713 refcount_count(&db->db_holds) > 1 &&
428870ff 2714 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
9babb374
BB
2715 *datap == db->db_buf) {
2716 /*
2717 * If this buffer is currently "in use" (i.e., there
2718 * are active holds and db_data still references it),
2719 * then make a copy before we start the write so that
2720 * any modifications from the open txg will not leak
2721 * into this write.
2722 *
2723 * NOTE: this copy does not need to be made for
2724 * objects only modified in the syncing context (e.g.
2725 * DNONE_DNODE blocks).
2726 */
2727 int blksz = arc_buf_size(*datap);
2728 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2729 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2730 bcopy(db->db.db_data, (*datap)->b_data, blksz);
b128c09f 2731 }
34dc7c2f
BB
2732 db->db_data_pending = dr;
2733
2734 mutex_exit(&db->db_mtx);
2735
b128c09f 2736 dbuf_write(dr, *datap, tx);
34dc7c2f
BB
2737
2738 ASSERT(!list_link_active(&dr->dr_dirty_node));
572e2857 2739 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
34dc7c2f 2740 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
572e2857
BB
2741 DB_DNODE_EXIT(db);
2742 } else {
2743 /*
2744 * Although zio_nowait() does not "wait for an IO", it does
2745 * initiate the IO. If this is an empty write it seems plausible
2746 * that the IO could actually be completed before the nowait
2747 * returns. We need to DB_DNODE_EXIT() first in case
2748 * zio_nowait() invalidates the dbuf.
2749 */
2750 DB_DNODE_EXIT(db);
34dc7c2f 2751 zio_nowait(dr->dr_zio);
572e2857 2752 }
34dc7c2f
BB
2753}
2754
2755void
2756dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2757{
2758 dbuf_dirty_record_t *dr;
2759
c65aa5b2 2760 while ((dr = list_head(list))) {
34dc7c2f
BB
2761 if (dr->dr_zio != NULL) {
2762 /*
2763 * If we find an already initialized zio then we
2764 * are processing the meta-dnode, and we have finished.
2765 * The dbufs for all dnodes are put back on the list
2766 * during processing, so that we can zio_wait()
2767 * these IOs after initiating all child IOs.
2768 */
2769 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2770 DMU_META_DNODE_OBJECT);
2771 break;
2772 }
2773 list_remove(list, dr);
2774 if (dr->dr_dbuf->db_level > 0)
2775 dbuf_sync_indirect(dr, tx);
2776 else
2777 dbuf_sync_leaf(dr, tx);
2778 }
2779}
2780
34dc7c2f
BB
2781/* ARGSUSED */
2782static void
2783dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2784{
2785 dmu_buf_impl_t *db = vdb;
572e2857 2786 dnode_t *dn;
b128c09f 2787 blkptr_t *bp = zio->io_bp;
34dc7c2f 2788 blkptr_t *bp_orig = &zio->io_bp_orig;
428870ff
BB
2789 spa_t *spa = zio->io_spa;
2790 int64_t delta;
34dc7c2f 2791 uint64_t fill = 0;
428870ff 2792 int i;
34dc7c2f 2793
9b67f605 2794 ASSERT3P(db->db_blkptr, ==, bp);
b128c09f 2795
572e2857
BB
2796 DB_DNODE_ENTER(db);
2797 dn = DB_DNODE(db);
428870ff
BB
2798 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2799 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2800 zio->io_prev_space_delta = delta;
34dc7c2f 2801
b0bc7a84
MG
2802 if (bp->blk_birth != 0) {
2803 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2804 BP_GET_TYPE(bp) == dn->dn_type) ||
2805 (db->db_blkid == DMU_SPILL_BLKID &&
9b67f605
MA
2806 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
2807 BP_IS_EMBEDDED(bp));
b0bc7a84 2808 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
34dc7c2f
BB
2809 }
2810
2811 mutex_enter(&db->db_mtx);
2812
428870ff
BB
2813#ifdef ZFS_DEBUG
2814 if (db->db_blkid == DMU_SPILL_BLKID) {
428870ff
BB
2815 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2816 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2817 db->db_blkptr == &dn->dn_phys->dn_spill);
2818 }
2819#endif
2820
34dc7c2f
BB
2821 if (db->db_level == 0) {
2822 mutex_enter(&dn->dn_mtx);
428870ff
BB
2823 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2824 db->db_blkid != DMU_SPILL_BLKID)
34dc7c2f
BB
2825 dn->dn_phys->dn_maxblkid = db->db_blkid;
2826 mutex_exit(&dn->dn_mtx);
2827
2828 if (dn->dn_type == DMU_OT_DNODE) {
2829 dnode_phys_t *dnp = db->db.db_data;
2830 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2831 i--, dnp++) {
2832 if (dnp->dn_type != DMU_OT_NONE)
2833 fill++;
2834 }
2835 } else {
b0bc7a84
MG
2836 if (BP_IS_HOLE(bp)) {
2837 fill = 0;
2838 } else {
2839 fill = 1;
2840 }
34dc7c2f
BB
2841 }
2842 } else {
b128c09f 2843 blkptr_t *ibp = db->db.db_data;
34dc7c2f 2844 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
b128c09f
BB
2845 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2846 if (BP_IS_HOLE(ibp))
34dc7c2f 2847 continue;
9b67f605 2848 fill += BP_GET_FILL(ibp);
34dc7c2f
BB
2849 }
2850 }
572e2857 2851 DB_DNODE_EXIT(db);
34dc7c2f 2852
9b67f605
MA
2853 if (!BP_IS_EMBEDDED(bp))
2854 bp->blk_fill = fill;
34dc7c2f
BB
2855
2856 mutex_exit(&db->db_mtx);
34dc7c2f
BB
2857}
2858
e8b96c60
MA
2859/*
2860 * The SPA will call this callback several times for each zio - once
2861 * for every physical child i/o (zio->io_phys_children times). This
2862 * allows the DMU to monitor the progress of each logical i/o. For example,
2863 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2864 * block. There may be a long delay before all copies/fragments are completed,
2865 * so this callback allows us to retire dirty space gradually, as the physical
2866 * i/os complete.
2867 */
2868/* ARGSUSED */
2869static void
2870dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
2871{
2872 dmu_buf_impl_t *db = arg;
2873 objset_t *os = db->db_objset;
2874 dsl_pool_t *dp = dmu_objset_pool(os);
2875 dbuf_dirty_record_t *dr;
2876 int delta = 0;
2877
2878 dr = db->db_data_pending;
2879 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
2880
2881 /*
2882 * The callback will be called io_phys_children times. Retire one
2883 * portion of our dirty space each time we are called. Any rounding
2884 * error will be cleaned up by dsl_pool_sync()'s call to
2885 * dsl_pool_undirty_space().
2886 */
2887 delta = dr->dr_accounted / zio->io_phys_children;
2888 dsl_pool_undirty_space(dp, delta, zio->io_txg);
2889}
2890
34dc7c2f
BB
2891/* ARGSUSED */
2892static void
2893dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2894{
2895 dmu_buf_impl_t *db = vdb;
428870ff 2896 blkptr_t *bp_orig = &zio->io_bp_orig;
b0bc7a84
MG
2897 blkptr_t *bp = db->db_blkptr;
2898 objset_t *os = db->db_objset;
2899 dmu_tx_t *tx = os->os_synctx;
34dc7c2f
BB
2900 dbuf_dirty_record_t **drp, *dr;
2901
c99c9001 2902 ASSERT0(zio->io_error);
428870ff
BB
2903 ASSERT(db->db_blkptr == bp);
2904
03c6040b
GW
2905 /*
2906 * For nopwrites and rewrites we ensure that the bp matches our
2907 * original and bypass all the accounting.
2908 */
2909 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
428870ff
BB
2910 ASSERT(BP_EQUAL(bp, bp_orig));
2911 } else {
b0bc7a84 2912 dsl_dataset_t *ds = os->os_dsl_dataset;
428870ff
BB
2913 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2914 dsl_dataset_block_born(ds, bp, tx);
2915 }
34dc7c2f
BB
2916
2917 mutex_enter(&db->db_mtx);
2918
428870ff
BB
2919 DBUF_VERIFY(db);
2920
34dc7c2f
BB
2921 drp = &db->db_last_dirty;
2922 while ((dr = *drp) != db->db_data_pending)
2923 drp = &dr->dr_next;
2924 ASSERT(!list_link_active(&dr->dr_dirty_node));
428870ff 2925 ASSERT(dr->dr_dbuf == db);
34dc7c2f
BB
2926 ASSERT(dr->dr_next == NULL);
2927 *drp = dr->dr_next;
2928
428870ff
BB
2929#ifdef ZFS_DEBUG
2930 if (db->db_blkid == DMU_SPILL_BLKID) {
572e2857
BB
2931 dnode_t *dn;
2932
2933 DB_DNODE_ENTER(db);
2934 dn = DB_DNODE(db);
428870ff
BB
2935 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2936 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2937 db->db_blkptr == &dn->dn_phys->dn_spill);
572e2857 2938 DB_DNODE_EXIT(db);
428870ff
BB
2939 }
2940#endif
2941
34dc7c2f 2942 if (db->db_level == 0) {
428870ff 2943 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 2944 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
b128c09f
BB
2945 if (db->db_state != DB_NOFILL) {
2946 if (dr->dt.dl.dr_data != db->db_buf)
2947 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
13fe0198 2948 db));
428870ff 2949 else if (!arc_released(db->db_buf))
b128c09f 2950 arc_set_callback(db->db_buf, dbuf_do_evict, db);
b128c09f 2951 }
34dc7c2f 2952 } else {
572e2857
BB
2953 dnode_t *dn;
2954
2955 DB_DNODE_ENTER(db);
2956 dn = DB_DNODE(db);
34dc7c2f 2957 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
b0bc7a84 2958 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
34dc7c2f 2959 if (!BP_IS_HOLE(db->db_blkptr)) {
1fde1e37
BB
2960 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
2961 SPA_BLKPTRSHIFT);
b0bc7a84
MG
2962 ASSERT3U(db->db_blkid, <=,
2963 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
34dc7c2f
BB
2964 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2965 db->db.db_size);
9b67f605
MA
2966 if (!arc_released(db->db_buf))
2967 arc_set_callback(db->db_buf, dbuf_do_evict, db);
34dc7c2f 2968 }
572e2857 2969 DB_DNODE_EXIT(db);
34dc7c2f
BB
2970 mutex_destroy(&dr->dt.di.dr_mtx);
2971 list_destroy(&dr->dt.di.dr_children);
2972 }
2973 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2974
2975 cv_broadcast(&db->db_changed);
2976 ASSERT(db->db_dirtycnt > 0);
2977 db->db_dirtycnt -= 1;
2978 db->db_data_pending = NULL;
b0bc7a84 2979 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
428870ff
BB
2980}
2981
2982static void
2983dbuf_write_nofill_ready(zio_t *zio)
2984{
2985 dbuf_write_ready(zio, NULL, zio->io_private);
2986}
2987
2988static void
2989dbuf_write_nofill_done(zio_t *zio)
2990{
2991 dbuf_write_done(zio, NULL, zio->io_private);
2992}
2993
2994static void
2995dbuf_write_override_ready(zio_t *zio)
2996{
2997 dbuf_dirty_record_t *dr = zio->io_private;
2998 dmu_buf_impl_t *db = dr->dr_dbuf;
2999
3000 dbuf_write_ready(zio, NULL, db);
3001}
3002
3003static void
3004dbuf_write_override_done(zio_t *zio)
3005{
3006 dbuf_dirty_record_t *dr = zio->io_private;
3007 dmu_buf_impl_t *db = dr->dr_dbuf;
3008 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3009
3010 mutex_enter(&db->db_mtx);
3011 if (!BP_EQUAL(zio->io_bp, obp)) {
3012 if (!BP_IS_HOLE(obp))
3013 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3014 arc_release(dr->dt.dl.dr_data, db);
3015 }
34dc7c2f
BB
3016 mutex_exit(&db->db_mtx);
3017
428870ff
BB
3018 dbuf_write_done(zio, NULL, db);
3019}
3020
e49f1e20 3021/* Issue I/O to commit a dirty buffer to disk. */
428870ff
BB
3022static void
3023dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3024{
3025 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857
BB
3026 dnode_t *dn;
3027 objset_t *os;
428870ff
BB
3028 dmu_buf_impl_t *parent = db->db_parent;
3029 uint64_t txg = tx->tx_txg;
5dbd68a3 3030 zbookmark_phys_t zb;
428870ff
BB
3031 zio_prop_t zp;
3032 zio_t *zio;
3033 int wp_flag = 0;
34dc7c2f 3034
572e2857
BB
3035 DB_DNODE_ENTER(db);
3036 dn = DB_DNODE(db);
3037 os = dn->dn_objset;
3038
428870ff
BB
3039 if (db->db_state != DB_NOFILL) {
3040 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3041 /*
3042 * Private object buffers are released here rather
3043 * than in dbuf_dirty() since they are only modified
3044 * in the syncing context and we don't want the
3045 * overhead of making multiple copies of the data.
3046 */
3047 if (BP_IS_HOLE(db->db_blkptr)) {
3048 arc_buf_thaw(data);
3049 } else {
3050 dbuf_release_bp(db);
3051 }
3052 }
3053 }
3054
3055 if (parent != dn->dn_dbuf) {
e49f1e20
WA
3056 /* Our parent is an indirect block. */
3057 /* We have a dirty parent that has been scheduled for write. */
428870ff 3058 ASSERT(parent && parent->db_data_pending);
e49f1e20 3059 /* Our parent's buffer is one level closer to the dnode. */
428870ff 3060 ASSERT(db->db_level == parent->db_level-1);
e49f1e20
WA
3061 /*
3062 * We're about to modify our parent's db_data by modifying
3063 * our block pointer, so the parent must be released.
3064 */
428870ff
BB
3065 ASSERT(arc_released(parent->db_buf));
3066 zio = parent->db_data_pending->dr_zio;
3067 } else {
e49f1e20 3068 /* Our parent is the dnode itself. */
428870ff
BB
3069 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3070 db->db_blkid != DMU_SPILL_BLKID) ||
3071 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3072 if (db->db_blkid != DMU_SPILL_BLKID)
3073 ASSERT3P(db->db_blkptr, ==,
3074 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3075 zio = dn->dn_zio;
3076 }
3077
3078 ASSERT(db->db_level == 0 || data == db->db_buf);
3079 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3080 ASSERT(zio);
3081
3082 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3083 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3084 db->db.db_object, db->db_level, db->db_blkid);
3085
3086 if (db->db_blkid == DMU_SPILL_BLKID)
3087 wp_flag = WP_SPILL;
3088 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3089
3090 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
572e2857 3091 DB_DNODE_EXIT(db);
428870ff 3092
9b67f605
MA
3093 if (db->db_level == 0 &&
3094 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3095 /*
3096 * The BP for this block has been provided by open context
3097 * (by dmu_sync() or dmu_buf_write_embedded()).
3098 */
3099 void *contents = (data != NULL) ? data->b_data : NULL;
3100
428870ff 3101 dr->dr_zio = zio_write(zio, os->os_spa, txg,
9b67f605 3102 db->db_blkptr, contents, db->db.db_size, &zp,
e8b96c60
MA
3103 dbuf_write_override_ready, NULL, dbuf_write_override_done,
3104 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
428870ff
BB
3105 mutex_enter(&db->db_mtx);
3106 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3107 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
03c6040b 3108 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
428870ff
BB
3109 mutex_exit(&db->db_mtx);
3110 } else if (db->db_state == DB_NOFILL) {
3111 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
3112 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3113 db->db_blkptr, NULL, db->db.db_size, &zp,
e8b96c60 3114 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db,
428870ff
BB
3115 ZIO_PRIORITY_ASYNC_WRITE,
3116 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3117 } else {
3118 ASSERT(arc_released(data));
3119 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3a17a7a9
SK
3120 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
3121 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
e8b96c60
MA
3122 dbuf_write_physdone, dbuf_write_done, db,
3123 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
428870ff 3124 }
34dc7c2f 3125}
c28b2279
BB
3126
3127#if defined(_KERNEL) && defined(HAVE_SPL)
8f576c23
BB
3128EXPORT_SYMBOL(dbuf_find);
3129EXPORT_SYMBOL(dbuf_is_metadata);
3130EXPORT_SYMBOL(dbuf_evict);
3131EXPORT_SYMBOL(dbuf_loan_arcbuf);
3132EXPORT_SYMBOL(dbuf_whichblock);
3133EXPORT_SYMBOL(dbuf_read);
3134EXPORT_SYMBOL(dbuf_unoverride);
3135EXPORT_SYMBOL(dbuf_free_range);
3136EXPORT_SYMBOL(dbuf_new_size);
3137EXPORT_SYMBOL(dbuf_release_bp);
3138EXPORT_SYMBOL(dbuf_dirty);
c28b2279 3139EXPORT_SYMBOL(dmu_buf_will_dirty);
8f576c23
BB
3140EXPORT_SYMBOL(dmu_buf_will_not_fill);
3141EXPORT_SYMBOL(dmu_buf_will_fill);
3142EXPORT_SYMBOL(dmu_buf_fill_done);
4047414a 3143EXPORT_SYMBOL(dmu_buf_rele);
8f576c23
BB
3144EXPORT_SYMBOL(dbuf_assign_arcbuf);
3145EXPORT_SYMBOL(dbuf_clear);
3146EXPORT_SYMBOL(dbuf_prefetch);
3147EXPORT_SYMBOL(dbuf_hold_impl);
3148EXPORT_SYMBOL(dbuf_hold);
3149EXPORT_SYMBOL(dbuf_hold_level);
3150EXPORT_SYMBOL(dbuf_create_bonus);
3151EXPORT_SYMBOL(dbuf_spill_set_blksz);
3152EXPORT_SYMBOL(dbuf_rm_spill);
3153EXPORT_SYMBOL(dbuf_add_ref);
3154EXPORT_SYMBOL(dbuf_rele);
3155EXPORT_SYMBOL(dbuf_rele_and_unlock);
3156EXPORT_SYMBOL(dbuf_refcount);
3157EXPORT_SYMBOL(dbuf_sync_list);
3158EXPORT_SYMBOL(dmu_buf_set_user);
3159EXPORT_SYMBOL(dmu_buf_set_user_ie);
8f576c23
BB
3160EXPORT_SYMBOL(dmu_buf_get_user);
3161EXPORT_SYMBOL(dmu_buf_freeable);
0f699108 3162EXPORT_SYMBOL(dmu_buf_get_blkptr);
c28b2279 3163#endif