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