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