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