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