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