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