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