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