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