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