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