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