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