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