]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - zfs/module/zfs/dbuf.c
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.10-1, zfs to 0.6.5.10-1ubuntu2
[mirror_ubuntu-bionic-kernel.git] / zfs / 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_user_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_user_immediate_evict = FALSE;
1884 db->db_freed_in_flight = FALSE;
1885 db->db_pending_evict = FALSE;
1886
1887 if (blkid == DMU_BONUS_BLKID) {
1888 ASSERT3P(parent, ==, dn->dn_dbuf);
1889 db->db.db_size = DN_MAX_BONUSLEN -
1890 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1891 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1892 db->db.db_offset = DMU_BONUS_BLKID;
1893 db->db_state = DB_UNCACHED;
1894 /* the bonus dbuf is not placed in the hash table */
1895 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1896 return (db);
1897 } else if (blkid == DMU_SPILL_BLKID) {
1898 db->db.db_size = (blkptr != NULL) ?
1899 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1900 db->db.db_offset = 0;
1901 } else {
1902 int blocksize =
1903 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
1904 db->db.db_size = blocksize;
1905 db->db.db_offset = db->db_blkid * blocksize;
1906 }
1907
1908 /*
1909 * Hold the dn_dbufs_mtx while we get the new dbuf
1910 * in the hash table *and* added to the dbufs list.
1911 * This prevents a possible deadlock with someone
1912 * trying to look up this dbuf before its added to the
1913 * dn_dbufs list.
1914 */
1915 mutex_enter(&dn->dn_dbufs_mtx);
1916 db->db_state = DB_EVICTING;
1917 if ((odb = dbuf_hash_insert(db)) != NULL) {
1918 /* someone else inserted it first */
1919 kmem_cache_free(dbuf_cache, db);
1920 mutex_exit(&dn->dn_dbufs_mtx);
1921 return (odb);
1922 }
1923 avl_add(&dn->dn_dbufs, db);
1924 if (db->db_level == 0 && db->db_blkid >=
1925 dn->dn_unlisted_l0_blkid)
1926 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
1927 db->db_state = DB_UNCACHED;
1928 mutex_exit(&dn->dn_dbufs_mtx);
1929 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1930
1931 if (parent && parent != dn->dn_dbuf)
1932 dbuf_add_ref(parent, db);
1933
1934 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1935 refcount_count(&dn->dn_holds) > 0);
1936 (void) refcount_add(&dn->dn_holds, db);
1937 atomic_inc_32(&dn->dn_dbufs_count);
1938
1939 dprintf_dbuf(db, "db=%p\n", db);
1940
1941 return (db);
1942 }
1943
1944 static int
1945 dbuf_do_evict(void *private)
1946 {
1947 dmu_buf_impl_t *db = private;
1948
1949 if (!MUTEX_HELD(&db->db_mtx))
1950 mutex_enter(&db->db_mtx);
1951
1952 ASSERT(refcount_is_zero(&db->db_holds));
1953
1954 if (db->db_state != DB_EVICTING) {
1955 ASSERT(db->db_state == DB_CACHED);
1956 DBUF_VERIFY(db);
1957 db->db_buf = NULL;
1958 dbuf_evict(db);
1959 } else {
1960 mutex_exit(&db->db_mtx);
1961 dbuf_destroy(db);
1962 }
1963 return (0);
1964 }
1965
1966 static void
1967 dbuf_destroy(dmu_buf_impl_t *db)
1968 {
1969 ASSERT(refcount_is_zero(&db->db_holds));
1970
1971 if (db->db_blkid != DMU_BONUS_BLKID) {
1972 /*
1973 * If this dbuf is still on the dn_dbufs list,
1974 * remove it from that list.
1975 */
1976 if (db->db_dnode_handle != NULL) {
1977 dnode_t *dn;
1978
1979 DB_DNODE_ENTER(db);
1980 dn = DB_DNODE(db);
1981 mutex_enter(&dn->dn_dbufs_mtx);
1982 avl_remove(&dn->dn_dbufs, db);
1983 atomic_dec_32(&dn->dn_dbufs_count);
1984 mutex_exit(&dn->dn_dbufs_mtx);
1985 DB_DNODE_EXIT(db);
1986 /*
1987 * Decrementing the dbuf count means that the hold
1988 * corresponding to the removed dbuf is no longer
1989 * discounted in dnode_move(), so the dnode cannot be
1990 * moved until after we release the hold.
1991 */
1992 dnode_rele(dn, db);
1993 db->db_dnode_handle = NULL;
1994 }
1995 dbuf_hash_remove(db);
1996 }
1997 db->db_parent = NULL;
1998 db->db_buf = NULL;
1999
2000 ASSERT(db->db.db_data == NULL);
2001 ASSERT(db->db_hash_next == NULL);
2002 ASSERT(db->db_blkptr == NULL);
2003 ASSERT(db->db_data_pending == NULL);
2004
2005 kmem_cache_free(dbuf_cache, db);
2006 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2007 }
2008
2009 void
2010 dbuf_prefetch(dnode_t *dn, uint64_t blkid, zio_priority_t prio)
2011 {
2012 dmu_buf_impl_t *db = NULL;
2013 blkptr_t *bp = NULL;
2014
2015 ASSERT(blkid != DMU_BONUS_BLKID);
2016 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2017
2018 if (dnode_block_freed(dn, blkid))
2019 return;
2020
2021 /* dbuf_find() returns with db_mtx held */
2022 if ((db = dbuf_find(dn->dn_objset, dn->dn_object, 0, blkid))) {
2023 /*
2024 * This dbuf is already in the cache. We assume that
2025 * it is already CACHED, or else about to be either
2026 * read or filled.
2027 */
2028 mutex_exit(&db->db_mtx);
2029 return;
2030 }
2031
2032 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp, NULL) == 0) {
2033 if (bp && !BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
2034 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2035 arc_flags_t aflags =
2036 ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2037 zbookmark_phys_t zb;
2038
2039 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
2040 dn->dn_object, 0, blkid);
2041
2042 (void) arc_read(NULL, dn->dn_objset->os_spa,
2043 bp, NULL, NULL, prio,
2044 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2045 &aflags, &zb);
2046 }
2047 if (db)
2048 dbuf_rele(db, NULL);
2049 }
2050 }
2051
2052 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2053
2054 /*
2055 * Returns with db_holds incremented, and db_mtx not held.
2056 * Note: dn_struct_rwlock must be held.
2057 */
2058 static int
2059 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
2060 {
2061 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
2062 dh->dh_parent = NULL;
2063
2064 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
2065 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
2066 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
2067
2068 *(dh->dh_dbp) = NULL;
2069 top:
2070 /* dbuf_find() returns with db_mtx held */
2071 dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2072 dh->dh_level, dh->dh_blkid);
2073
2074 if (dh->dh_db == NULL) {
2075 dh->dh_bp = NULL;
2076
2077 ASSERT3P(dh->dh_parent, ==, NULL);
2078 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2079 dh->dh_fail_sparse, &dh->dh_parent,
2080 &dh->dh_bp, dh);
2081 if (dh->dh_fail_sparse) {
2082 if (dh->dh_err == 0 &&
2083 dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
2084 dh->dh_err = SET_ERROR(ENOENT);
2085 if (dh->dh_err) {
2086 if (dh->dh_parent)
2087 dbuf_rele(dh->dh_parent, NULL);
2088 return (dh->dh_err);
2089 }
2090 }
2091 if (dh->dh_err && dh->dh_err != ENOENT)
2092 return (dh->dh_err);
2093 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2094 dh->dh_parent, dh->dh_bp);
2095 }
2096
2097 if (dh->dh_db->db_buf && refcount_is_zero(&dh->dh_db->db_holds)) {
2098 arc_buf_add_ref(dh->dh_db->db_buf, dh->dh_db);
2099 if (dh->dh_db->db_buf->b_data == NULL) {
2100 dbuf_clear(dh->dh_db);
2101 if (dh->dh_parent) {
2102 dbuf_rele(dh->dh_parent, NULL);
2103 dh->dh_parent = NULL;
2104 }
2105 goto top;
2106 }
2107 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
2108 }
2109
2110 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
2111
2112 /*
2113 * If this buffer is currently syncing out, and we are are
2114 * still referencing it from db_data, we need to make a copy
2115 * of it in case we decide we want to dirty it again in this txg.
2116 */
2117 if (dh->dh_db->db_level == 0 &&
2118 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
2119 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
2120 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
2121 dh->dh_dr = dh->dh_db->db_data_pending;
2122
2123 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
2124 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
2125
2126 dbuf_set_data(dh->dh_db,
2127 arc_buf_alloc(dh->dh_dn->dn_objset->os_spa,
2128 dh->dh_db->db.db_size, dh->dh_db, dh->dh_type));
2129 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
2130 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
2131 }
2132 }
2133
2134 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
2135 DBUF_VERIFY(dh->dh_db);
2136 mutex_exit(&dh->dh_db->db_mtx);
2137
2138 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2139 if (dh->dh_parent)
2140 dbuf_rele(dh->dh_parent, NULL);
2141
2142 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
2143 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
2144 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
2145 *(dh->dh_dbp) = dh->dh_db;
2146
2147 return (0);
2148 }
2149
2150 /*
2151 * The following code preserves the recursive function dbuf_hold_impl()
2152 * but moves the local variables AND function arguments to the heap to
2153 * minimize the stack frame size. Enough space is initially allocated
2154 * on the stack for 20 levels of recursion.
2155 */
2156 int
2157 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2158 void *tag, dmu_buf_impl_t **dbp)
2159 {
2160 struct dbuf_hold_impl_data *dh;
2161 int error;
2162
2163 dh = kmem_zalloc(sizeof (struct dbuf_hold_impl_data) *
2164 DBUF_HOLD_IMPL_MAX_DEPTH, KM_SLEEP);
2165 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse, tag, dbp, 0);
2166
2167 error = __dbuf_hold_impl(dh);
2168
2169 kmem_free(dh, sizeof (struct dbuf_hold_impl_data) *
2170 DBUF_HOLD_IMPL_MAX_DEPTH);
2171
2172 return (error);
2173 }
2174
2175 static void
2176 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2177 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2178 void *tag, dmu_buf_impl_t **dbp, int depth)
2179 {
2180 dh->dh_dn = dn;
2181 dh->dh_level = level;
2182 dh->dh_blkid = blkid;
2183 dh->dh_fail_sparse = fail_sparse;
2184 dh->dh_tag = tag;
2185 dh->dh_dbp = dbp;
2186 dh->dh_depth = depth;
2187 }
2188
2189 dmu_buf_impl_t *
2190 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2191 {
2192 dmu_buf_impl_t *db;
2193 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
2194 return (err ? NULL : db);
2195 }
2196
2197 dmu_buf_impl_t *
2198 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2199 {
2200 dmu_buf_impl_t *db;
2201 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2202 return (err ? NULL : db);
2203 }
2204
2205 void
2206 dbuf_create_bonus(dnode_t *dn)
2207 {
2208 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2209
2210 ASSERT(dn->dn_bonus == NULL);
2211 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2212 }
2213
2214 int
2215 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2216 {
2217 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2218 dnode_t *dn;
2219
2220 if (db->db_blkid != DMU_SPILL_BLKID)
2221 return (SET_ERROR(ENOTSUP));
2222 if (blksz == 0)
2223 blksz = SPA_MINBLOCKSIZE;
2224 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2225 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2226
2227 DB_DNODE_ENTER(db);
2228 dn = DB_DNODE(db);
2229 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2230 dbuf_new_size(db, blksz, tx);
2231 rw_exit(&dn->dn_struct_rwlock);
2232 DB_DNODE_EXIT(db);
2233
2234 return (0);
2235 }
2236
2237 void
2238 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2239 {
2240 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2241 }
2242
2243 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2244 void
2245 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2246 {
2247 VERIFY(refcount_add(&db->db_holds, tag) > 1);
2248 }
2249
2250 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2251 boolean_t
2252 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2253 void *tag)
2254 {
2255 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2256 dmu_buf_impl_t *found_db;
2257 boolean_t result = B_FALSE;
2258
2259 if (blkid == DMU_BONUS_BLKID)
2260 found_db = dbuf_find_bonus(os, obj);
2261 else
2262 found_db = dbuf_find(os, obj, 0, blkid);
2263
2264 if (found_db != NULL) {
2265 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2266 (void) refcount_add(&db->db_holds, tag);
2267 result = B_TRUE;
2268 }
2269 mutex_exit(&found_db->db_mtx);
2270 }
2271 return (result);
2272 }
2273
2274 /*
2275 * If you call dbuf_rele() you had better not be referencing the dnode handle
2276 * unless you have some other direct or indirect hold on the dnode. (An indirect
2277 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2278 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2279 * dnode's parent dbuf evicting its dnode handles.
2280 */
2281 void
2282 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2283 {
2284 mutex_enter(&db->db_mtx);
2285 dbuf_rele_and_unlock(db, tag);
2286 }
2287
2288 void
2289 dmu_buf_rele(dmu_buf_t *db, void *tag)
2290 {
2291 dbuf_rele((dmu_buf_impl_t *)db, tag);
2292 }
2293
2294 /*
2295 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2296 * db_dirtycnt and db_holds to be updated atomically.
2297 */
2298 void
2299 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2300 {
2301 int64_t holds;
2302
2303 ASSERT(MUTEX_HELD(&db->db_mtx));
2304 DBUF_VERIFY(db);
2305
2306 /*
2307 * Remove the reference to the dbuf before removing its hold on the
2308 * dnode so we can guarantee in dnode_move() that a referenced bonus
2309 * buffer has a corresponding dnode hold.
2310 */
2311 holds = refcount_remove(&db->db_holds, tag);
2312 ASSERT(holds >= 0);
2313
2314 /*
2315 * We can't freeze indirects if there is a possibility that they
2316 * may be modified in the current syncing context.
2317 */
2318 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2319 arc_buf_freeze(db->db_buf);
2320
2321 if (holds == db->db_dirtycnt &&
2322 db->db_level == 0 && db->db_user_immediate_evict)
2323 dbuf_evict_user(db);
2324
2325 if (holds == 0) {
2326 if (db->db_blkid == DMU_BONUS_BLKID) {
2327 dnode_t *dn;
2328 boolean_t evict_dbuf = db->db_pending_evict;
2329
2330 /*
2331 * If the dnode moves here, we cannot cross this
2332 * barrier until the move completes.
2333 */
2334 DB_DNODE_ENTER(db);
2335
2336 dn = DB_DNODE(db);
2337 atomic_dec_32(&dn->dn_dbufs_count);
2338
2339 /*
2340 * Decrementing the dbuf count means that the bonus
2341 * buffer's dnode hold is no longer discounted in
2342 * dnode_move(). The dnode cannot move until after
2343 * the dnode_rele() below.
2344 */
2345 DB_DNODE_EXIT(db);
2346
2347 /*
2348 * Do not reference db after its lock is dropped.
2349 * Another thread may evict it.
2350 */
2351 mutex_exit(&db->db_mtx);
2352
2353 if (evict_dbuf)
2354 dnode_evict_bonus(dn);
2355
2356 dnode_rele(dn, db);
2357 } else if (db->db_buf == NULL) {
2358 /*
2359 * This is a special case: we never associated this
2360 * dbuf with any data allocated from the ARC.
2361 */
2362 ASSERT(db->db_state == DB_UNCACHED ||
2363 db->db_state == DB_NOFILL);
2364 dbuf_evict(db);
2365 } else if (arc_released(db->db_buf)) {
2366 arc_buf_t *buf = db->db_buf;
2367 /*
2368 * This dbuf has anonymous data associated with it.
2369 */
2370 dbuf_clear_data(db);
2371 VERIFY(arc_buf_remove_ref(buf, db));
2372 dbuf_evict(db);
2373 } else {
2374 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2375
2376 /*
2377 * A dbuf will be eligible for eviction if either the
2378 * 'primarycache' property is set or a duplicate
2379 * copy of this buffer is already cached in the arc.
2380 *
2381 * In the case of the 'primarycache' a buffer
2382 * is considered for eviction if it matches the
2383 * criteria set in the property.
2384 *
2385 * To decide if our buffer is considered a
2386 * duplicate, we must call into the arc to determine
2387 * if multiple buffers are referencing the same
2388 * block on-disk. If so, then we simply evict
2389 * ourselves.
2390 */
2391 if (!DBUF_IS_CACHEABLE(db)) {
2392 if (db->db_blkptr != NULL &&
2393 !BP_IS_HOLE(db->db_blkptr) &&
2394 !BP_IS_EMBEDDED(db->db_blkptr)) {
2395 spa_t *spa =
2396 dmu_objset_spa(db->db_objset);
2397 blkptr_t bp = *db->db_blkptr;
2398 dbuf_clear(db);
2399 arc_freed(spa, &bp);
2400 } else {
2401 dbuf_clear(db);
2402 }
2403 } else if (db->db_pending_evict ||
2404 arc_buf_eviction_needed(db->db_buf)) {
2405 dbuf_clear(db);
2406 } else {
2407 mutex_exit(&db->db_mtx);
2408 }
2409 }
2410 } else {
2411 mutex_exit(&db->db_mtx);
2412 }
2413 }
2414
2415 #pragma weak dmu_buf_refcount = dbuf_refcount
2416 uint64_t
2417 dbuf_refcount(dmu_buf_impl_t *db)
2418 {
2419 return (refcount_count(&db->db_holds));
2420 }
2421
2422 void *
2423 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2424 dmu_buf_user_t *new_user)
2425 {
2426 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2427
2428 mutex_enter(&db->db_mtx);
2429 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2430 if (db->db_user == old_user)
2431 db->db_user = new_user;
2432 else
2433 old_user = db->db_user;
2434 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2435 mutex_exit(&db->db_mtx);
2436
2437 return (old_user);
2438 }
2439
2440 void *
2441 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2442 {
2443 return (dmu_buf_replace_user(db_fake, NULL, user));
2444 }
2445
2446 void *
2447 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2448 {
2449 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2450
2451 db->db_user_immediate_evict = TRUE;
2452 return (dmu_buf_set_user(db_fake, user));
2453 }
2454
2455 void *
2456 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2457 {
2458 return (dmu_buf_replace_user(db_fake, user, NULL));
2459 }
2460
2461 void *
2462 dmu_buf_get_user(dmu_buf_t *db_fake)
2463 {
2464 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2465
2466 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2467 return (db->db_user);
2468 }
2469
2470 void
2471 dmu_buf_user_evict_wait()
2472 {
2473 taskq_wait(dbu_evict_taskq);
2474 }
2475
2476 boolean_t
2477 dmu_buf_freeable(dmu_buf_t *dbuf)
2478 {
2479 boolean_t res = B_FALSE;
2480 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2481
2482 if (db->db_blkptr)
2483 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2484 db->db_blkptr, db->db_blkptr->blk_birth);
2485
2486 return (res);
2487 }
2488
2489 blkptr_t *
2490 dmu_buf_get_blkptr(dmu_buf_t *db)
2491 {
2492 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2493 return (dbi->db_blkptr);
2494 }
2495
2496 static void
2497 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2498 {
2499 /* ASSERT(dmu_tx_is_syncing(tx) */
2500 ASSERT(MUTEX_HELD(&db->db_mtx));
2501
2502 if (db->db_blkptr != NULL)
2503 return;
2504
2505 if (db->db_blkid == DMU_SPILL_BLKID) {
2506 db->db_blkptr = &dn->dn_phys->dn_spill;
2507 BP_ZERO(db->db_blkptr);
2508 return;
2509 }
2510 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2511 /*
2512 * This buffer was allocated at a time when there was
2513 * no available blkptrs from the dnode, or it was
2514 * inappropriate to hook it in (i.e., nlevels mis-match).
2515 */
2516 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2517 ASSERT(db->db_parent == NULL);
2518 db->db_parent = dn->dn_dbuf;
2519 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2520 DBUF_VERIFY(db);
2521 } else {
2522 dmu_buf_impl_t *parent = db->db_parent;
2523 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2524
2525 ASSERT(dn->dn_phys->dn_nlevels > 1);
2526 if (parent == NULL) {
2527 mutex_exit(&db->db_mtx);
2528 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2529 (void) dbuf_hold_impl(dn, db->db_level+1,
2530 db->db_blkid >> epbs, FALSE, db, &parent);
2531 rw_exit(&dn->dn_struct_rwlock);
2532 mutex_enter(&db->db_mtx);
2533 db->db_parent = parent;
2534 }
2535 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2536 (db->db_blkid & ((1ULL << epbs) - 1));
2537 DBUF_VERIFY(db);
2538 }
2539 }
2540
2541 /*
2542 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2543 * is critical the we not allow the compiler to inline this function in to
2544 * dbuf_sync_list() thereby drastically bloating the stack usage.
2545 */
2546 noinline static void
2547 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2548 {
2549 dmu_buf_impl_t *db = dr->dr_dbuf;
2550 dnode_t *dn;
2551 zio_t *zio;
2552
2553 ASSERT(dmu_tx_is_syncing(tx));
2554
2555 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2556
2557 mutex_enter(&db->db_mtx);
2558
2559 ASSERT(db->db_level > 0);
2560 DBUF_VERIFY(db);
2561
2562 /* Read the block if it hasn't been read yet. */
2563 if (db->db_buf == NULL) {
2564 mutex_exit(&db->db_mtx);
2565 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2566 mutex_enter(&db->db_mtx);
2567 }
2568 ASSERT3U(db->db_state, ==, DB_CACHED);
2569 ASSERT(db->db_buf != NULL);
2570
2571 DB_DNODE_ENTER(db);
2572 dn = DB_DNODE(db);
2573 /* Indirect block size must match what the dnode thinks it is. */
2574 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2575 dbuf_check_blkptr(dn, db);
2576 DB_DNODE_EXIT(db);
2577
2578 /* Provide the pending dirty record to child dbufs */
2579 db->db_data_pending = dr;
2580
2581 mutex_exit(&db->db_mtx);
2582 dbuf_write(dr, db->db_buf, tx);
2583
2584 zio = dr->dr_zio;
2585 mutex_enter(&dr->dt.di.dr_mtx);
2586 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
2587 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2588 mutex_exit(&dr->dt.di.dr_mtx);
2589 zio_nowait(zio);
2590 }
2591
2592 /*
2593 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2594 * critical the we not allow the compiler to inline this function in to
2595 * dbuf_sync_list() thereby drastically bloating the stack usage.
2596 */
2597 noinline static void
2598 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2599 {
2600 arc_buf_t **datap = &dr->dt.dl.dr_data;
2601 dmu_buf_impl_t *db = dr->dr_dbuf;
2602 dnode_t *dn;
2603 objset_t *os;
2604 uint64_t txg = tx->tx_txg;
2605
2606 ASSERT(dmu_tx_is_syncing(tx));
2607
2608 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2609
2610 mutex_enter(&db->db_mtx);
2611 /*
2612 * To be synced, we must be dirtied. But we
2613 * might have been freed after the dirty.
2614 */
2615 if (db->db_state == DB_UNCACHED) {
2616 /* This buffer has been freed since it was dirtied */
2617 ASSERT(db->db.db_data == NULL);
2618 } else if (db->db_state == DB_FILL) {
2619 /* This buffer was freed and is now being re-filled */
2620 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2621 } else {
2622 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2623 }
2624 DBUF_VERIFY(db);
2625
2626 DB_DNODE_ENTER(db);
2627 dn = DB_DNODE(db);
2628
2629 if (db->db_blkid == DMU_SPILL_BLKID) {
2630 mutex_enter(&dn->dn_mtx);
2631 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
2632 /*
2633 * In the previous transaction group, the bonus buffer
2634 * was entirely used to store the attributes for the
2635 * dnode which overrode the dn_spill field. However,
2636 * when adding more attributes to the file a spill
2637 * block was required to hold the extra attributes.
2638 *
2639 * Make sure to clear the garbage left in the dn_spill
2640 * field from the previous attributes in the bonus
2641 * buffer. Otherwise, after writing out the spill
2642 * block to the new allocated dva, it will free
2643 * the old block pointed to by the invalid dn_spill.
2644 */
2645 db->db_blkptr = NULL;
2646 }
2647 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2648 mutex_exit(&dn->dn_mtx);
2649 }
2650
2651 /*
2652 * If this is a bonus buffer, simply copy the bonus data into the
2653 * dnode. It will be written out when the dnode is synced (and it
2654 * will be synced, since it must have been dirty for dbuf_sync to
2655 * be called).
2656 */
2657 if (db->db_blkid == DMU_BONUS_BLKID) {
2658 dbuf_dirty_record_t **drp;
2659
2660 ASSERT(*datap != NULL);
2661 ASSERT0(db->db_level);
2662 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2663 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2664 DB_DNODE_EXIT(db);
2665
2666 if (*datap != db->db.db_data) {
2667 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2668 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2669 }
2670 db->db_data_pending = NULL;
2671 drp = &db->db_last_dirty;
2672 while (*drp != dr)
2673 drp = &(*drp)->dr_next;
2674 ASSERT(dr->dr_next == NULL);
2675 ASSERT(dr->dr_dbuf == db);
2676 *drp = dr->dr_next;
2677 if (dr->dr_dbuf->db_level != 0) {
2678 mutex_destroy(&dr->dt.di.dr_mtx);
2679 list_destroy(&dr->dt.di.dr_children);
2680 }
2681 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2682 ASSERT(db->db_dirtycnt > 0);
2683 db->db_dirtycnt -= 1;
2684 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2685 return;
2686 }
2687
2688 os = dn->dn_objset;
2689
2690 /*
2691 * This function may have dropped the db_mtx lock allowing a dmu_sync
2692 * operation to sneak in. As a result, we need to ensure that we
2693 * don't check the dr_override_state until we have returned from
2694 * dbuf_check_blkptr.
2695 */
2696 dbuf_check_blkptr(dn, db);
2697
2698 /*
2699 * If this buffer is in the middle of an immediate write,
2700 * wait for the synchronous IO to complete.
2701 */
2702 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2703 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2704 cv_wait(&db->db_changed, &db->db_mtx);
2705 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2706 }
2707
2708 if (db->db_state != DB_NOFILL &&
2709 dn->dn_object != DMU_META_DNODE_OBJECT &&
2710 refcount_count(&db->db_holds) > 1 &&
2711 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2712 *datap == db->db_buf) {
2713 /*
2714 * If this buffer is currently "in use" (i.e., there
2715 * are active holds and db_data still references it),
2716 * then make a copy before we start the write so that
2717 * any modifications from the open txg will not leak
2718 * into this write.
2719 *
2720 * NOTE: this copy does not need to be made for
2721 * objects only modified in the syncing context (e.g.
2722 * DNONE_DNODE blocks).
2723 */
2724 int blksz = arc_buf_size(*datap);
2725 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2726 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2727 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2728 }
2729 db->db_data_pending = dr;
2730
2731 mutex_exit(&db->db_mtx);
2732
2733 dbuf_write(dr, *datap, tx);
2734
2735 ASSERT(!list_link_active(&dr->dr_dirty_node));
2736 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2737 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2738 DB_DNODE_EXIT(db);
2739 } else {
2740 /*
2741 * Although zio_nowait() does not "wait for an IO", it does
2742 * initiate the IO. If this is an empty write it seems plausible
2743 * that the IO could actually be completed before the nowait
2744 * returns. We need to DB_DNODE_EXIT() first in case
2745 * zio_nowait() invalidates the dbuf.
2746 */
2747 DB_DNODE_EXIT(db);
2748 zio_nowait(dr->dr_zio);
2749 }
2750 }
2751
2752 void
2753 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
2754 {
2755 dbuf_dirty_record_t *dr;
2756
2757 while ((dr = list_head(list))) {
2758 if (dr->dr_zio != NULL) {
2759 /*
2760 * If we find an already initialized zio then we
2761 * are processing the meta-dnode, and we have finished.
2762 * The dbufs for all dnodes are put back on the list
2763 * during processing, so that we can zio_wait()
2764 * these IOs after initiating all child IOs.
2765 */
2766 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2767 DMU_META_DNODE_OBJECT);
2768 break;
2769 }
2770 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
2771 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
2772 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
2773 }
2774 list_remove(list, dr);
2775 if (dr->dr_dbuf->db_level > 0)
2776 dbuf_sync_indirect(dr, tx);
2777 else
2778 dbuf_sync_leaf(dr, tx);
2779 }
2780 }
2781
2782 /* ARGSUSED */
2783 static void
2784 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2785 {
2786 dmu_buf_impl_t *db = vdb;
2787 dnode_t *dn;
2788 blkptr_t *bp = zio->io_bp;
2789 blkptr_t *bp_orig = &zio->io_bp_orig;
2790 spa_t *spa = zio->io_spa;
2791 int64_t delta;
2792 uint64_t fill = 0;
2793 int i;
2794
2795 ASSERT3P(db->db_blkptr, ==, bp);
2796
2797 DB_DNODE_ENTER(db);
2798 dn = DB_DNODE(db);
2799 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2800 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2801 zio->io_prev_space_delta = delta;
2802
2803 if (bp->blk_birth != 0) {
2804 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2805 BP_GET_TYPE(bp) == dn->dn_type) ||
2806 (db->db_blkid == DMU_SPILL_BLKID &&
2807 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
2808 BP_IS_EMBEDDED(bp));
2809 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2810 }
2811
2812 mutex_enter(&db->db_mtx);
2813
2814 #ifdef ZFS_DEBUG
2815 if (db->db_blkid == DMU_SPILL_BLKID) {
2816 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2817 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2818 db->db_blkptr == &dn->dn_phys->dn_spill);
2819 }
2820 #endif
2821
2822 if (db->db_level == 0) {
2823 mutex_enter(&dn->dn_mtx);
2824 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2825 db->db_blkid != DMU_SPILL_BLKID)
2826 dn->dn_phys->dn_maxblkid = db->db_blkid;
2827 mutex_exit(&dn->dn_mtx);
2828
2829 if (dn->dn_type == DMU_OT_DNODE) {
2830 dnode_phys_t *dnp = db->db.db_data;
2831 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2832 i--, dnp++) {
2833 if (dnp->dn_type != DMU_OT_NONE)
2834 fill++;
2835 }
2836 } else {
2837 if (BP_IS_HOLE(bp)) {
2838 fill = 0;
2839 } else {
2840 fill = 1;
2841 }
2842 }
2843 } else {
2844 blkptr_t *ibp = db->db.db_data;
2845 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2846 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2847 if (BP_IS_HOLE(ibp))
2848 continue;
2849 fill += BP_GET_FILL(ibp);
2850 }
2851 }
2852 DB_DNODE_EXIT(db);
2853
2854 if (!BP_IS_EMBEDDED(bp))
2855 bp->blk_fill = fill;
2856
2857 mutex_exit(&db->db_mtx);
2858 }
2859
2860 /*
2861 * The SPA will call this callback several times for each zio - once
2862 * for every physical child i/o (zio->io_phys_children times). This
2863 * allows the DMU to monitor the progress of each logical i/o. For example,
2864 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2865 * block. There may be a long delay before all copies/fragments are completed,
2866 * so this callback allows us to retire dirty space gradually, as the physical
2867 * i/os complete.
2868 */
2869 /* ARGSUSED */
2870 static void
2871 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
2872 {
2873 dmu_buf_impl_t *db = arg;
2874 objset_t *os = db->db_objset;
2875 dsl_pool_t *dp = dmu_objset_pool(os);
2876 dbuf_dirty_record_t *dr;
2877 int delta = 0;
2878
2879 dr = db->db_data_pending;
2880 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
2881
2882 /*
2883 * The callback will be called io_phys_children times. Retire one
2884 * portion of our dirty space each time we are called. Any rounding
2885 * error will be cleaned up by dsl_pool_sync()'s call to
2886 * dsl_pool_undirty_space().
2887 */
2888 delta = dr->dr_accounted / zio->io_phys_children;
2889 dsl_pool_undirty_space(dp, delta, zio->io_txg);
2890 }
2891
2892 /* ARGSUSED */
2893 static void
2894 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2895 {
2896 dmu_buf_impl_t *db = vdb;
2897 blkptr_t *bp_orig = &zio->io_bp_orig;
2898 blkptr_t *bp = db->db_blkptr;
2899 objset_t *os = db->db_objset;
2900 dmu_tx_t *tx = os->os_synctx;
2901 dbuf_dirty_record_t **drp, *dr;
2902
2903 ASSERT0(zio->io_error);
2904 ASSERT(db->db_blkptr == bp);
2905
2906 /*
2907 * For nopwrites and rewrites we ensure that the bp matches our
2908 * original and bypass all the accounting.
2909 */
2910 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2911 ASSERT(BP_EQUAL(bp, bp_orig));
2912 } else {
2913 dsl_dataset_t *ds = os->os_dsl_dataset;
2914 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2915 dsl_dataset_block_born(ds, bp, tx);
2916 }
2917
2918 mutex_enter(&db->db_mtx);
2919
2920 DBUF_VERIFY(db);
2921
2922 drp = &db->db_last_dirty;
2923 while ((dr = *drp) != db->db_data_pending)
2924 drp = &dr->dr_next;
2925 ASSERT(!list_link_active(&dr->dr_dirty_node));
2926 ASSERT(dr->dr_dbuf == db);
2927 ASSERT(dr->dr_next == NULL);
2928 *drp = dr->dr_next;
2929
2930 #ifdef ZFS_DEBUG
2931 if (db->db_blkid == DMU_SPILL_BLKID) {
2932 dnode_t *dn;
2933
2934 DB_DNODE_ENTER(db);
2935 dn = DB_DNODE(db);
2936 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2937 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2938 db->db_blkptr == &dn->dn_phys->dn_spill);
2939 DB_DNODE_EXIT(db);
2940 }
2941 #endif
2942
2943 if (db->db_level == 0) {
2944 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2945 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2946 if (db->db_state != DB_NOFILL) {
2947 if (dr->dt.dl.dr_data != db->db_buf)
2948 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2949 db));
2950 else if (!arc_released(db->db_buf))
2951 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2952 }
2953 } else {
2954 dnode_t *dn;
2955
2956 DB_DNODE_ENTER(db);
2957 dn = DB_DNODE(db);
2958 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2959 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
2960 if (!BP_IS_HOLE(db->db_blkptr)) {
2961 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
2962 SPA_BLKPTRSHIFT);
2963 ASSERT3U(db->db_blkid, <=,
2964 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
2965 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2966 db->db.db_size);
2967 if (!arc_released(db->db_buf))
2968 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2969 }
2970 DB_DNODE_EXIT(db);
2971 mutex_destroy(&dr->dt.di.dr_mtx);
2972 list_destroy(&dr->dt.di.dr_children);
2973 }
2974 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2975
2976 cv_broadcast(&db->db_changed);
2977 ASSERT(db->db_dirtycnt > 0);
2978 db->db_dirtycnt -= 1;
2979 db->db_data_pending = NULL;
2980 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
2981 }
2982
2983 static void
2984 dbuf_write_nofill_ready(zio_t *zio)
2985 {
2986 dbuf_write_ready(zio, NULL, zio->io_private);
2987 }
2988
2989 static void
2990 dbuf_write_nofill_done(zio_t *zio)
2991 {
2992 dbuf_write_done(zio, NULL, zio->io_private);
2993 }
2994
2995 static void
2996 dbuf_write_override_ready(zio_t *zio)
2997 {
2998 dbuf_dirty_record_t *dr = zio->io_private;
2999 dmu_buf_impl_t *db = dr->dr_dbuf;
3000
3001 dbuf_write_ready(zio, NULL, db);
3002 }
3003
3004 static void
3005 dbuf_write_override_done(zio_t *zio)
3006 {
3007 dbuf_dirty_record_t *dr = zio->io_private;
3008 dmu_buf_impl_t *db = dr->dr_dbuf;
3009 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3010
3011 mutex_enter(&db->db_mtx);
3012 if (!BP_EQUAL(zio->io_bp, obp)) {
3013 if (!BP_IS_HOLE(obp))
3014 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3015 arc_release(dr->dt.dl.dr_data, db);
3016 }
3017 mutex_exit(&db->db_mtx);
3018
3019 dbuf_write_done(zio, NULL, db);
3020 }
3021
3022 /* Issue I/O to commit a dirty buffer to disk. */
3023 static void
3024 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3025 {
3026 dmu_buf_impl_t *db = dr->dr_dbuf;
3027 dnode_t *dn;
3028 objset_t *os;
3029 dmu_buf_impl_t *parent = db->db_parent;
3030 uint64_t txg = tx->tx_txg;
3031 zbookmark_phys_t zb;
3032 zio_prop_t zp;
3033 zio_t *zio;
3034 int wp_flag = 0;
3035
3036 DB_DNODE_ENTER(db);
3037 dn = DB_DNODE(db);
3038 os = dn->dn_objset;
3039
3040 if (db->db_state != DB_NOFILL) {
3041 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3042 /*
3043 * Private object buffers are released here rather
3044 * than in dbuf_dirty() since they are only modified
3045 * in the syncing context and we don't want the
3046 * overhead of making multiple copies of the data.
3047 */
3048 if (BP_IS_HOLE(db->db_blkptr)) {
3049 arc_buf_thaw(data);
3050 } else {
3051 dbuf_release_bp(db);
3052 }
3053 }
3054 }
3055
3056 if (parent != dn->dn_dbuf) {
3057 /* Our parent is an indirect block. */
3058 /* We have a dirty parent that has been scheduled for write. */
3059 ASSERT(parent && parent->db_data_pending);
3060 /* Our parent's buffer is one level closer to the dnode. */
3061 ASSERT(db->db_level == parent->db_level-1);
3062 /*
3063 * We're about to modify our parent's db_data by modifying
3064 * our block pointer, so the parent must be released.
3065 */
3066 ASSERT(arc_released(parent->db_buf));
3067 zio = parent->db_data_pending->dr_zio;
3068 } else {
3069 /* Our parent is the dnode itself. */
3070 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3071 db->db_blkid != DMU_SPILL_BLKID) ||
3072 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3073 if (db->db_blkid != DMU_SPILL_BLKID)
3074 ASSERT3P(db->db_blkptr, ==,
3075 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3076 zio = dn->dn_zio;
3077 }
3078
3079 ASSERT(db->db_level == 0 || data == db->db_buf);
3080 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3081 ASSERT(zio);
3082
3083 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3084 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3085 db->db.db_object, db->db_level, db->db_blkid);
3086
3087 if (db->db_blkid == DMU_SPILL_BLKID)
3088 wp_flag = WP_SPILL;
3089 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3090
3091 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3092 DB_DNODE_EXIT(db);
3093
3094 if (db->db_level == 0 &&
3095 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3096 /*
3097 * The BP for this block has been provided by open context
3098 * (by dmu_sync() or dmu_buf_write_embedded()).
3099 */
3100 void *contents = (data != NULL) ? data->b_data : NULL;
3101
3102 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3103 db->db_blkptr, contents, db->db.db_size, &zp,
3104 dbuf_write_override_ready, NULL, dbuf_write_override_done,
3105 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3106 mutex_enter(&db->db_mtx);
3107 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3108 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3109 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3110 mutex_exit(&db->db_mtx);
3111 } else if (db->db_state == DB_NOFILL) {
3112 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
3113 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3114 db->db_blkptr, NULL, db->db.db_size, &zp,
3115 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db,
3116 ZIO_PRIORITY_ASYNC_WRITE,
3117 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3118 } else {
3119 ASSERT(arc_released(data));
3120 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3121 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
3122 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
3123 dbuf_write_physdone, dbuf_write_done, db,
3124 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3125 }
3126 }
3127
3128 #if defined(_KERNEL) && defined(HAVE_SPL)
3129 EXPORT_SYMBOL(dbuf_find);
3130 EXPORT_SYMBOL(dbuf_is_metadata);
3131 EXPORT_SYMBOL(dbuf_evict);
3132 EXPORT_SYMBOL(dbuf_loan_arcbuf);
3133 EXPORT_SYMBOL(dbuf_whichblock);
3134 EXPORT_SYMBOL(dbuf_read);
3135 EXPORT_SYMBOL(dbuf_unoverride);
3136 EXPORT_SYMBOL(dbuf_free_range);
3137 EXPORT_SYMBOL(dbuf_new_size);
3138 EXPORT_SYMBOL(dbuf_release_bp);
3139 EXPORT_SYMBOL(dbuf_dirty);
3140 EXPORT_SYMBOL(dmu_buf_will_dirty);
3141 EXPORT_SYMBOL(dmu_buf_will_not_fill);
3142 EXPORT_SYMBOL(dmu_buf_will_fill);
3143 EXPORT_SYMBOL(dmu_buf_fill_done);
3144 EXPORT_SYMBOL(dmu_buf_rele);
3145 EXPORT_SYMBOL(dbuf_assign_arcbuf);
3146 EXPORT_SYMBOL(dbuf_clear);
3147 EXPORT_SYMBOL(dbuf_prefetch);
3148 EXPORT_SYMBOL(dbuf_hold_impl);
3149 EXPORT_SYMBOL(dbuf_hold);
3150 EXPORT_SYMBOL(dbuf_hold_level);
3151 EXPORT_SYMBOL(dbuf_create_bonus);
3152 EXPORT_SYMBOL(dbuf_spill_set_blksz);
3153 EXPORT_SYMBOL(dbuf_rm_spill);
3154 EXPORT_SYMBOL(dbuf_add_ref);
3155 EXPORT_SYMBOL(dbuf_rele);
3156 EXPORT_SYMBOL(dbuf_rele_and_unlock);
3157 EXPORT_SYMBOL(dbuf_refcount);
3158 EXPORT_SYMBOL(dbuf_sync_list);
3159 EXPORT_SYMBOL(dmu_buf_set_user);
3160 EXPORT_SYMBOL(dmu_buf_set_user_ie);
3161 EXPORT_SYMBOL(dmu_buf_get_user);
3162 EXPORT_SYMBOL(dmu_buf_freeable);
3163 EXPORT_SYMBOL(dmu_buf_get_blkptr);
3164 #endif