xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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 https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2016, Nexenta Systems, Inc. All rights reserved.
27  * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
28  * Copyright (c) 2019 Datto Inc.
29  * Copyright (c) 2019, 2023, Klara Inc.
30  * Copyright (c) 2019, Allan Jude
31  * Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
32  * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
33  */
34 
35 #include <sys/dmu.h>
36 #include <sys/dmu_impl.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dbuf.h>
39 #include <sys/dnode.h>
40 #include <sys/zfs_context.h>
41 #include <sys/dmu_objset.h>
42 #include <sys/dmu_traverse.h>
43 #include <sys/dsl_dataset.h>
44 #include <sys/dsl_dir.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dsl_synctask.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dmu_zfetch.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zap.h>
51 #include <sys/zio_checksum.h>
52 #include <sys/zio_compress.h>
53 #include <sys/sa.h>
54 #include <sys/zfeature.h>
55 #include <sys/abd.h>
56 #include <sys/brt.h>
57 #include <sys/trace_zfs.h>
58 #include <sys/zfs_racct.h>
59 #include <sys/zfs_rlock.h>
60 #ifdef _KERNEL
61 #include <sys/vmsystm.h>
62 #include <sys/zfs_znode.h>
63 #endif
64 
65 /*
66  * Enable/disable nopwrite feature.
67  */
68 static int zfs_nopwrite_enabled = 1;
69 
70 /*
71  * Tunable to control percentage of dirtied L1 blocks from frees allowed into
72  * one TXG. After this threshold is crossed, additional dirty blocks from frees
73  * will wait until the next TXG.
74  * A value of zero will disable this throttle.
75  */
76 static uint_t zfs_per_txg_dirty_frees_percent = 30;
77 
78 /*
79  * Enable/disable forcing txg sync when dirty checking for holes with lseek().
80  * By default this is enabled to ensure accurate hole reporting, it can result
81  * in a significant performance penalty for lseek(SEEK_HOLE) heavy workloads.
82  * Disabling this option will result in holes never being reported in dirty
83  * files which is always safe.
84  */
85 static int zfs_dmu_offset_next_sync = 1;
86 
87 /*
88  * Limit the amount we can prefetch with one call to this amount.  This
89  * helps to limit the amount of memory that can be used by prefetching.
90  * Larger objects should be prefetched a bit at a time.
91  */
92 #ifdef _ILP32
93 uint_t dmu_prefetch_max = 8 * 1024 * 1024;
94 #else
95 uint_t dmu_prefetch_max = 8 * SPA_MAXBLOCKSIZE;
96 #endif
97 
98 /*
99  * Override copies= for dedup state objects. 0 means the traditional behaviour
100  * (ie the default for the containing objset ie 3 for the MOS).
101  */
102 uint_t dmu_ddt_copies = 0;
103 
104 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
105 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, FALSE, "unallocated"		},
106 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "object directory"	},
107 	{DMU_BSWAP_UINT64, TRUE,  TRUE,  FALSE, "object array"		},
108 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, FALSE, "packed nvlist"		},
109 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "packed nvlist size"	},
110 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "bpobj"			},
111 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "bpobj header"		},
112 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "SPA space map header"	},
113 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "SPA space map"		},
114 	{DMU_BSWAP_UINT64, TRUE,  FALSE, TRUE,  "ZIL intent log"	},
115 	{DMU_BSWAP_DNODE,  TRUE,  FALSE, TRUE,  "DMU dnode"		},
116 	{DMU_BSWAP_OBJSET, TRUE,  TRUE,  FALSE, "DMU objset"		},
117 	{DMU_BSWAP_UINT64, TRUE,  TRUE,  FALSE, "DSL directory"		},
118 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL directory child map"},
119 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL dataset snap map"	},
120 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL props"		},
121 	{DMU_BSWAP_UINT64, TRUE,  TRUE,  FALSE, "DSL dataset"		},
122 	{DMU_BSWAP_ZNODE,  TRUE,  FALSE, FALSE, "ZFS znode"		},
123 	{DMU_BSWAP_OLDACL, TRUE,  FALSE, TRUE,  "ZFS V0 ACL"		},
124 	{DMU_BSWAP_UINT8,  FALSE, FALSE, TRUE,  "ZFS plain file"	},
125 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,  "ZFS directory"		},
126 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "ZFS master node"	},
127 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,  "ZFS delete queue"	},
128 	{DMU_BSWAP_UINT8,  FALSE, FALSE, TRUE,  "zvol object"		},
129 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "zvol prop"		},
130 	{DMU_BSWAP_UINT8,  FALSE, FALSE, TRUE,  "other uint8[]"		},
131 	{DMU_BSWAP_UINT64, FALSE, FALSE, TRUE,  "other uint64[]"	},
132 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "other ZAP"		},
133 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "persistent error log"	},
134 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, FALSE, "SPA history"		},
135 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "SPA history offsets"	},
136 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "Pool properties"	},
137 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL permissions"	},
138 	{DMU_BSWAP_ACL,    TRUE,  FALSE, TRUE,  "ZFS ACL"		},
139 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, TRUE,  "ZFS SYSACL"		},
140 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, TRUE,  "FUID table"		},
141 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "FUID table size"	},
142 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL dataset next clones"},
143 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "scan work queue"	},
144 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,  "ZFS user/group/project used" },
145 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,  "ZFS user/group/project quota"},
146 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "snapshot refcount tags"},
147 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "DDT ZAP algorithm"	},
148 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "DDT statistics"	},
149 	{DMU_BSWAP_UINT8,  TRUE,  FALSE, TRUE,	"System attributes"	},
150 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,	"SA master node"	},
151 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,	"SA attr registration"	},
152 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, TRUE,	"SA attr layouts"	},
153 	{DMU_BSWAP_ZAP,    TRUE,  FALSE, FALSE, "scan translations"	},
154 	{DMU_BSWAP_UINT8,  FALSE, FALSE, TRUE,  "deduplicated block"	},
155 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL deadlist map"	},
156 	{DMU_BSWAP_UINT64, TRUE,  TRUE,  FALSE, "DSL deadlist map hdr"	},
157 	{DMU_BSWAP_ZAP,    TRUE,  TRUE,  FALSE, "DSL dir clones"	},
158 	{DMU_BSWAP_UINT64, TRUE,  FALSE, FALSE, "bpobj subobj"		}
159 };
160 
161 dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
162 	{	byteswap_uint8_array,	"uint8"		},
163 	{	byteswap_uint16_array,	"uint16"	},
164 	{	byteswap_uint32_array,	"uint32"	},
165 	{	byteswap_uint64_array,	"uint64"	},
166 	{	zap_byteswap,		"zap"		},
167 	{	dnode_buf_byteswap,	"dnode"		},
168 	{	dmu_objset_byteswap,	"objset"	},
169 	{	zfs_znode_byteswap,	"znode"		},
170 	{	zfs_oldacl_byteswap,	"oldacl"	},
171 	{	zfs_acl_byteswap,	"acl"		}
172 };
173 
174 int
175 dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset,
176     const void *tag, dmu_buf_t **dbp)
177 {
178 	uint64_t blkid;
179 	dmu_buf_impl_t *db;
180 
181 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
182 	blkid = dbuf_whichblock(dn, 0, offset);
183 	db = dbuf_hold(dn, blkid, tag);
184 	rw_exit(&dn->dn_struct_rwlock);
185 
186 	if (db == NULL) {
187 		*dbp = NULL;
188 		return (SET_ERROR(EIO));
189 	}
190 
191 	*dbp = &db->db;
192 	return (0);
193 }
194 
195 int
196 dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,
197     const void *tag, dmu_buf_t **dbp)
198 {
199 	dnode_t *dn;
200 	uint64_t blkid;
201 	dmu_buf_impl_t *db;
202 	int err;
203 
204 	err = dnode_hold(os, object, FTAG, &dn);
205 	if (err)
206 		return (err);
207 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
208 	blkid = dbuf_whichblock(dn, 0, offset);
209 	db = dbuf_hold(dn, blkid, tag);
210 	rw_exit(&dn->dn_struct_rwlock);
211 	dnode_rele(dn, FTAG);
212 
213 	if (db == NULL) {
214 		*dbp = NULL;
215 		return (SET_ERROR(EIO));
216 	}
217 
218 	*dbp = &db->db;
219 	return (err);
220 }
221 
222 int
223 dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset,
224     const void *tag, dmu_buf_t **dbp, int flags)
225 {
226 	int err;
227 	int db_flags = DB_RF_CANFAIL;
228 
229 	if (flags & DMU_READ_NO_PREFETCH)
230 		db_flags |= DB_RF_NOPREFETCH;
231 	if (flags & DMU_READ_NO_DECRYPT)
232 		db_flags |= DB_RF_NO_DECRYPT;
233 
234 	err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp);
235 	if (err == 0) {
236 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
237 		err = dbuf_read(db, NULL, db_flags);
238 		if (err != 0) {
239 			dbuf_rele(db, tag);
240 			*dbp = NULL;
241 		}
242 	}
243 
244 	return (err);
245 }
246 
247 int
248 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
249     const void *tag, dmu_buf_t **dbp, int flags)
250 {
251 	int err;
252 	int db_flags = DB_RF_CANFAIL;
253 
254 	if (flags & DMU_READ_NO_PREFETCH)
255 		db_flags |= DB_RF_NOPREFETCH;
256 	if (flags & DMU_READ_NO_DECRYPT)
257 		db_flags |= DB_RF_NO_DECRYPT;
258 
259 	err = dmu_buf_hold_noread(os, object, offset, tag, dbp);
260 	if (err == 0) {
261 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
262 		err = dbuf_read(db, NULL, db_flags);
263 		if (err != 0) {
264 			dbuf_rele(db, tag);
265 			*dbp = NULL;
266 		}
267 	}
268 
269 	return (err);
270 }
271 
272 int
273 dmu_bonus_max(void)
274 {
275 	return (DN_OLD_MAX_BONUSLEN);
276 }
277 
278 int
279 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
280 {
281 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
282 	dnode_t *dn;
283 	int error;
284 
285 	if (newsize < 0 || newsize > db_fake->db_size)
286 		return (SET_ERROR(EINVAL));
287 
288 	DB_DNODE_ENTER(db);
289 	dn = DB_DNODE(db);
290 
291 	if (dn->dn_bonus != db) {
292 		error = SET_ERROR(EINVAL);
293 	} else {
294 		dnode_setbonuslen(dn, newsize, tx);
295 		error = 0;
296 	}
297 
298 	DB_DNODE_EXIT(db);
299 	return (error);
300 }
301 
302 int
303 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
304 {
305 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
306 	dnode_t *dn;
307 	int error;
308 
309 	if (!DMU_OT_IS_VALID(type))
310 		return (SET_ERROR(EINVAL));
311 
312 	DB_DNODE_ENTER(db);
313 	dn = DB_DNODE(db);
314 
315 	if (dn->dn_bonus != db) {
316 		error = SET_ERROR(EINVAL);
317 	} else {
318 		dnode_setbonus_type(dn, type, tx);
319 		error = 0;
320 	}
321 
322 	DB_DNODE_EXIT(db);
323 	return (error);
324 }
325 
326 dmu_object_type_t
327 dmu_get_bonustype(dmu_buf_t *db_fake)
328 {
329 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
330 	dmu_object_type_t type;
331 
332 	DB_DNODE_ENTER(db);
333 	type = DB_DNODE(db)->dn_bonustype;
334 	DB_DNODE_EXIT(db);
335 
336 	return (type);
337 }
338 
339 int
340 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
341 {
342 	dnode_t *dn;
343 	int error;
344 
345 	error = dnode_hold(os, object, FTAG, &dn);
346 	dbuf_rm_spill(dn, tx);
347 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
348 	dnode_rm_spill(dn, tx);
349 	rw_exit(&dn->dn_struct_rwlock);
350 	dnode_rele(dn, FTAG);
351 	return (error);
352 }
353 
354 /*
355  * Lookup and hold the bonus buffer for the provided dnode.  If the dnode
356  * has not yet been allocated a new bonus dbuf a will be allocated.
357  * Returns ENOENT, EIO, or 0.
358  */
359 int dmu_bonus_hold_by_dnode(dnode_t *dn, const void *tag, dmu_buf_t **dbp,
360     uint32_t flags)
361 {
362 	dmu_buf_impl_t *db;
363 	int error;
364 	uint32_t db_flags = DB_RF_MUST_SUCCEED;
365 
366 	if (flags & DMU_READ_NO_PREFETCH)
367 		db_flags |= DB_RF_NOPREFETCH;
368 	if (flags & DMU_READ_NO_DECRYPT)
369 		db_flags |= DB_RF_NO_DECRYPT;
370 
371 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
372 	if (dn->dn_bonus == NULL) {
373 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
374 			rw_exit(&dn->dn_struct_rwlock);
375 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
376 		}
377 		if (dn->dn_bonus == NULL)
378 			dbuf_create_bonus(dn);
379 	}
380 	db = dn->dn_bonus;
381 
382 	/* as long as the bonus buf is held, the dnode will be held */
383 	if (zfs_refcount_add(&db->db_holds, tag) == 1) {
384 		VERIFY(dnode_add_ref(dn, db));
385 		atomic_inc_32(&dn->dn_dbufs_count);
386 	}
387 
388 	/*
389 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
390 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
391 	 * a dnode hold for every dbuf.
392 	 */
393 	rw_exit(&dn->dn_struct_rwlock);
394 
395 	error = dbuf_read(db, NULL, db_flags);
396 	if (error) {
397 		dnode_evict_bonus(dn);
398 		dbuf_rele(db, tag);
399 		*dbp = NULL;
400 		return (error);
401 	}
402 
403 	*dbp = &db->db;
404 	return (0);
405 }
406 
407 int
408 dmu_bonus_hold(objset_t *os, uint64_t object, const void *tag, dmu_buf_t **dbp)
409 {
410 	dnode_t *dn;
411 	int error;
412 
413 	error = dnode_hold(os, object, FTAG, &dn);
414 	if (error)
415 		return (error);
416 
417 	error = dmu_bonus_hold_by_dnode(dn, tag, dbp, DMU_READ_NO_PREFETCH);
418 	dnode_rele(dn, FTAG);
419 
420 	return (error);
421 }
422 
423 /*
424  * returns ENOENT, EIO, or 0.
425  *
426  * This interface will allocate a blank spill dbuf when a spill blk
427  * doesn't already exist on the dnode.
428  *
429  * if you only want to find an already existing spill db, then
430  * dmu_spill_hold_existing() should be used.
431  */
432 int
433 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, const void *tag,
434     dmu_buf_t **dbp)
435 {
436 	dmu_buf_impl_t *db = NULL;
437 	int err;
438 
439 	if ((flags & DB_RF_HAVESTRUCT) == 0)
440 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
441 
442 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
443 
444 	if ((flags & DB_RF_HAVESTRUCT) == 0)
445 		rw_exit(&dn->dn_struct_rwlock);
446 
447 	if (db == NULL) {
448 		*dbp = NULL;
449 		return (SET_ERROR(EIO));
450 	}
451 	err = dbuf_read(db, NULL, flags);
452 	if (err == 0)
453 		*dbp = &db->db;
454 	else {
455 		dbuf_rele(db, tag);
456 		*dbp = NULL;
457 	}
458 	return (err);
459 }
460 
461 int
462 dmu_spill_hold_existing(dmu_buf_t *bonus, const void *tag, dmu_buf_t **dbp)
463 {
464 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
465 	dnode_t *dn;
466 	int err;
467 
468 	DB_DNODE_ENTER(db);
469 	dn = DB_DNODE(db);
470 
471 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
472 		err = SET_ERROR(EINVAL);
473 	} else {
474 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
475 
476 		if (!dn->dn_have_spill) {
477 			err = SET_ERROR(ENOENT);
478 		} else {
479 			err = dmu_spill_hold_by_dnode(dn,
480 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
481 		}
482 
483 		rw_exit(&dn->dn_struct_rwlock);
484 	}
485 
486 	DB_DNODE_EXIT(db);
487 	return (err);
488 }
489 
490 int
491 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, uint32_t flags, const void *tag,
492     dmu_buf_t **dbp)
493 {
494 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
495 	int err;
496 	uint32_t db_flags = DB_RF_CANFAIL;
497 
498 	if (flags & DMU_READ_NO_DECRYPT)
499 		db_flags |= DB_RF_NO_DECRYPT;
500 
501 	DB_DNODE_ENTER(db);
502 	err = dmu_spill_hold_by_dnode(DB_DNODE(db), db_flags, tag, dbp);
503 	DB_DNODE_EXIT(db);
504 
505 	return (err);
506 }
507 
508 /*
509  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
510  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
511  * and can induce severe lock contention when writing to several files
512  * whose dnodes are in the same block.
513  */
514 int
515 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
516     boolean_t read, const void *tag, int *numbufsp, dmu_buf_t ***dbpp,
517     uint32_t flags)
518 {
519 	dmu_buf_t **dbp;
520 	zstream_t *zs = NULL;
521 	uint64_t blkid, nblks, i;
522 	uint32_t dbuf_flags;
523 	int err;
524 	zio_t *zio = NULL;
525 	boolean_t missed = B_FALSE;
526 
527 	ASSERT(!read || length <= DMU_MAX_ACCESS);
528 
529 	/*
530 	 * Note: We directly notify the prefetch code of this read, so that
531 	 * we can tell it about the multi-block read.  dbuf_read() only knows
532 	 * about the one block it is accessing.
533 	 */
534 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT |
535 	    DB_RF_NOPREFETCH;
536 
537 	if ((flags & DMU_READ_NO_DECRYPT) != 0)
538 		dbuf_flags |= DB_RF_NO_DECRYPT;
539 
540 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
541 	if (dn->dn_datablkshift) {
542 		int blkshift = dn->dn_datablkshift;
543 		nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
544 		    P2ALIGN_TYPED(offset, 1ULL << blkshift, uint64_t))
545 		    >> blkshift;
546 	} else {
547 		if (offset + length > dn->dn_datablksz) {
548 			zfs_panic_recover("zfs: accessing past end of object "
549 			    "%llx/%llx (size=%u access=%llu+%llu)",
550 			    (longlong_t)dn->dn_objset->
551 			    os_dsl_dataset->ds_object,
552 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
553 			    (longlong_t)offset, (longlong_t)length);
554 			rw_exit(&dn->dn_struct_rwlock);
555 			return (SET_ERROR(EIO));
556 		}
557 		nblks = 1;
558 	}
559 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
560 
561 	if (read)
562 		zio = zio_root(dn->dn_objset->os_spa, NULL, NULL,
563 		    ZIO_FLAG_CANFAIL);
564 	blkid = dbuf_whichblock(dn, 0, offset);
565 	if ((flags & DMU_READ_NO_PREFETCH) == 0) {
566 		/*
567 		 * Prepare the zfetch before initiating the demand reads, so
568 		 * that if multiple threads block on same indirect block, we
569 		 * base predictions on the original less racy request order.
570 		 */
571 		zs = dmu_zfetch_prepare(&dn->dn_zfetch, blkid, nblks, read,
572 		    B_TRUE);
573 	}
574 	for (i = 0; i < nblks; i++) {
575 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);
576 		if (db == NULL) {
577 			if (zs) {
578 				dmu_zfetch_run(&dn->dn_zfetch, zs, missed,
579 				    B_TRUE);
580 			}
581 			rw_exit(&dn->dn_struct_rwlock);
582 			dmu_buf_rele_array(dbp, nblks, tag);
583 			if (read)
584 				zio_nowait(zio);
585 			return (SET_ERROR(EIO));
586 		}
587 
588 		/*
589 		 * Initiate async demand data read.
590 		 * We check the db_state after calling dbuf_read() because
591 		 * (1) dbuf_read() may change the state to CACHED due to a
592 		 * hit in the ARC, and (2) on a cache miss, a child will
593 		 * have been added to "zio" but not yet completed, so the
594 		 * state will not yet be CACHED.
595 		 */
596 		if (read) {
597 			if (i == nblks - 1 && blkid + i < dn->dn_maxblkid &&
598 			    offset + length < db->db.db_offset +
599 			    db->db.db_size) {
600 				if (offset <= db->db.db_offset)
601 					dbuf_flags |= DB_RF_PARTIAL_FIRST;
602 				else
603 					dbuf_flags |= DB_RF_PARTIAL_MORE;
604 			}
605 			(void) dbuf_read(db, zio, dbuf_flags);
606 			if (db->db_state != DB_CACHED)
607 				missed = B_TRUE;
608 		}
609 		dbp[i] = &db->db;
610 	}
611 
612 	/*
613 	 * If we are doing O_DIRECT we still hold the dbufs, even for reads,
614 	 * but we do not issue any reads here. We do not want to account for
615 	 * writes in this case.
616 	 *
617 	 * O_DIRECT write/read accounting takes place in
618 	 * dmu_{write/read}_abd().
619 	 */
620 	if (!read && ((flags & DMU_DIRECTIO) == 0))
621 		zfs_racct_write(dn->dn_objset->os_spa, length, nblks, flags);
622 
623 	if (zs)
624 		dmu_zfetch_run(&dn->dn_zfetch, zs, missed, B_TRUE);
625 	rw_exit(&dn->dn_struct_rwlock);
626 
627 	if (read) {
628 		/* wait for async read i/o */
629 		err = zio_wait(zio);
630 		if (err) {
631 			dmu_buf_rele_array(dbp, nblks, tag);
632 			return (err);
633 		}
634 
635 		/* wait for other io to complete */
636 		for (i = 0; i < nblks; i++) {
637 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
638 			mutex_enter(&db->db_mtx);
639 			while (db->db_state == DB_READ ||
640 			    db->db_state == DB_FILL)
641 				cv_wait(&db->db_changed, &db->db_mtx);
642 			if (db->db_state == DB_UNCACHED)
643 				err = SET_ERROR(EIO);
644 			mutex_exit(&db->db_mtx);
645 			if (err) {
646 				dmu_buf_rele_array(dbp, nblks, tag);
647 				return (err);
648 			}
649 		}
650 	}
651 
652 	*numbufsp = nblks;
653 	*dbpp = dbp;
654 	return (0);
655 }
656 
657 int
658 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
659     uint64_t length, int read, const void *tag, int *numbufsp,
660     dmu_buf_t ***dbpp)
661 {
662 	dnode_t *dn;
663 	int err;
664 
665 	err = dnode_hold(os, object, FTAG, &dn);
666 	if (err)
667 		return (err);
668 
669 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
670 	    numbufsp, dbpp, DMU_READ_PREFETCH);
671 
672 	dnode_rele(dn, FTAG);
673 
674 	return (err);
675 }
676 
677 int
678 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
679     uint64_t length, boolean_t read, const void *tag, int *numbufsp,
680     dmu_buf_t ***dbpp)
681 {
682 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
683 	int err;
684 
685 	DB_DNODE_ENTER(db);
686 	err = dmu_buf_hold_array_by_dnode(DB_DNODE(db), offset, length, read,
687 	    tag, numbufsp, dbpp, DMU_READ_PREFETCH);
688 	DB_DNODE_EXIT(db);
689 
690 	return (err);
691 }
692 
693 void
694 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, const void *tag)
695 {
696 	int i;
697 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
698 
699 	if (numbufs == 0)
700 		return;
701 
702 	for (i = 0; i < numbufs; i++) {
703 		if (dbp[i])
704 			dbuf_rele(dbp[i], tag);
705 	}
706 
707 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
708 }
709 
710 /*
711  * Issue prefetch I/Os for the given blocks.  If level is greater than 0, the
712  * indirect blocks prefetched will be those that point to the blocks containing
713  * the data starting at offset, and continuing to offset + len.  If the range
714  * is too long, prefetch the first dmu_prefetch_max bytes as requested, while
715  * for the rest only a higher level, also fitting within dmu_prefetch_max.  It
716  * should primarily help random reads, since for long sequential reads there is
717  * a speculative prefetcher.
718  *
719  * Note that if the indirect blocks above the blocks being prefetched are not
720  * in cache, they will be asynchronously read in.  Dnode read by dnode_hold()
721  * is currently synchronous.
722  */
723 void
724 dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,
725     uint64_t len, zio_priority_t pri)
726 {
727 	dnode_t *dn;
728 
729 	if (dmu_prefetch_max == 0 || len == 0) {
730 		dmu_prefetch_dnode(os, object, pri);
731 		return;
732 	}
733 
734 	if (dnode_hold(os, object, FTAG, &dn) != 0)
735 		return;
736 
737 	dmu_prefetch_by_dnode(dn, level, offset, len, pri);
738 
739 	dnode_rele(dn, FTAG);
740 }
741 
742 void
743 dmu_prefetch_by_dnode(dnode_t *dn, int64_t level, uint64_t offset,
744     uint64_t len, zio_priority_t pri)
745 {
746 	int64_t level2 = level;
747 	uint64_t start, end, start2, end2;
748 
749 	/*
750 	 * Depending on len we may do two prefetches: blocks [start, end) at
751 	 * level, and following blocks [start2, end2) at higher level2.
752 	 */
753 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
754 	if (dn->dn_datablkshift != 0) {
755 		/*
756 		 * The object has multiple blocks.  Calculate the full range
757 		 * of blocks [start, end2) and then split it into two parts,
758 		 * so that the first [start, end) fits into dmu_prefetch_max.
759 		 */
760 		start = dbuf_whichblock(dn, level, offset);
761 		end2 = dbuf_whichblock(dn, level, offset + len - 1) + 1;
762 		uint8_t ibs = dn->dn_indblkshift;
763 		uint8_t bs = (level == 0) ? dn->dn_datablkshift : ibs;
764 		uint_t limit = P2ROUNDUP(dmu_prefetch_max, 1 << bs) >> bs;
765 		start2 = end = MIN(end2, start + limit);
766 
767 		/*
768 		 * Find level2 where [start2, end2) fits into dmu_prefetch_max.
769 		 */
770 		uint8_t ibps = ibs - SPA_BLKPTRSHIFT;
771 		limit = P2ROUNDUP(dmu_prefetch_max, 1 << ibs) >> ibs;
772 		do {
773 			level2++;
774 			start2 = P2ROUNDUP(start2, 1 << ibps) >> ibps;
775 			end2 = P2ROUNDUP(end2, 1 << ibps) >> ibps;
776 		} while (end2 - start2 > limit);
777 	} else {
778 		/* There is only one block.  Prefetch it or nothing. */
779 		start = start2 = end2 = 0;
780 		end = start + (level == 0 && offset < dn->dn_datablksz);
781 	}
782 
783 	for (uint64_t i = start; i < end; i++)
784 		dbuf_prefetch(dn, level, i, pri, 0);
785 	for (uint64_t i = start2; i < end2; i++)
786 		dbuf_prefetch(dn, level2, i, pri, 0);
787 	rw_exit(&dn->dn_struct_rwlock);
788 }
789 
790 typedef struct {
791 	kmutex_t	dpa_lock;
792 	kcondvar_t	dpa_cv;
793 	uint64_t	dpa_pending_io;
794 } dmu_prefetch_arg_t;
795 
796 static void
797 dmu_prefetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t issued)
798 {
799 	(void) level; (void) blkid; (void)issued;
800 	dmu_prefetch_arg_t *dpa = arg;
801 
802 	ASSERT0(level);
803 
804 	mutex_enter(&dpa->dpa_lock);
805 	ASSERT3U(dpa->dpa_pending_io, >, 0);
806 	if (--dpa->dpa_pending_io == 0)
807 		cv_broadcast(&dpa->dpa_cv);
808 	mutex_exit(&dpa->dpa_lock);
809 }
810 
811 static void
812 dmu_prefetch_wait_by_dnode(dnode_t *dn, uint64_t offset, uint64_t len)
813 {
814 	dmu_prefetch_arg_t dpa;
815 
816 	mutex_init(&dpa.dpa_lock, NULL, MUTEX_DEFAULT, NULL);
817 	cv_init(&dpa.dpa_cv, NULL, CV_DEFAULT, NULL);
818 
819 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
820 
821 	uint64_t start = dbuf_whichblock(dn, 0, offset);
822 	uint64_t end = dbuf_whichblock(dn, 0, offset + len - 1) + 1;
823 	dpa.dpa_pending_io = end - start;
824 
825 	for (uint64_t blk = start; blk < end; blk++) {
826 		(void) dbuf_prefetch_impl(dn, 0, blk, ZIO_PRIORITY_ASYNC_READ,
827 		    0, dmu_prefetch_done, &dpa);
828 	}
829 
830 	rw_exit(&dn->dn_struct_rwlock);
831 
832 	/* wait for prefetch L0 reads to finish */
833 	mutex_enter(&dpa.dpa_lock);
834 	while (dpa.dpa_pending_io > 0) {
835 		cv_wait(&dpa.dpa_cv, &dpa.dpa_lock);
836 
837 	}
838 	mutex_exit(&dpa.dpa_lock);
839 
840 	mutex_destroy(&dpa.dpa_lock);
841 	cv_destroy(&dpa.dpa_cv);
842 }
843 
844 /*
845  * Issue prefetch I/Os for the given L0 block range and wait for the I/O
846  * to complete. This does not enforce dmu_prefetch_max and will prefetch
847  * the entire range. The blocks are read from disk into the ARC but no
848  * decompression occurs (i.e., the dbuf cache is not required).
849  */
850 int
851 dmu_prefetch_wait(objset_t *os, uint64_t object, uint64_t offset, uint64_t size)
852 {
853 	dnode_t *dn;
854 	int err = 0;
855 
856 	err = dnode_hold(os, object, FTAG, &dn);
857 	if (err != 0)
858 		return (err);
859 
860 	/*
861 	 * Chunk the requests (16 indirects worth) so that we can be interrupted
862 	 */
863 	uint64_t chunksize;
864 	if (dn->dn_indblkshift) {
865 		uint64_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);
866 		chunksize = (nbps * 16) << dn->dn_datablkshift;
867 	} else {
868 		chunksize = dn->dn_datablksz;
869 	}
870 
871 	while (size > 0) {
872 		uint64_t mylen = MIN(size, chunksize);
873 
874 		dmu_prefetch_wait_by_dnode(dn, offset, mylen);
875 
876 		offset += mylen;
877 		size -= mylen;
878 
879 		if (issig()) {
880 			err = SET_ERROR(EINTR);
881 			break;
882 		}
883 	}
884 
885 	dnode_rele(dn, FTAG);
886 
887 	return (err);
888 }
889 
890 /*
891  * Issue prefetch I/Os for the given object's dnode.
892  */
893 void
894 dmu_prefetch_dnode(objset_t *os, uint64_t object, zio_priority_t pri)
895 {
896 	if (object == 0 || object >= DN_MAX_OBJECT)
897 		return;
898 
899 	dnode_t *dn = DMU_META_DNODE(os);
900 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
901 	uint64_t blkid = dbuf_whichblock(dn, 0, object * sizeof (dnode_phys_t));
902 	dbuf_prefetch(dn, 0, blkid, pri, 0);
903 	rw_exit(&dn->dn_struct_rwlock);
904 }
905 
906 /*
907  * Get the next "chunk" of file data to free.  We traverse the file from
908  * the end so that the file gets shorter over time (if we crash in the
909  * middle, this will leave us in a better state).  We find allocated file
910  * data by simply searching the allocated level 1 indirects.
911  *
912  * On input, *start should be the first offset that does not need to be
913  * freed (e.g. "offset + length").  On return, *start will be the first
914  * offset that should be freed and l1blks is set to the number of level 1
915  * indirect blocks found within the chunk.
916  */
917 static int
918 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks)
919 {
920 	uint64_t blks;
921 	uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
922 	/* bytes of data covered by a level-1 indirect block */
923 	uint64_t iblkrange = (uint64_t)dn->dn_datablksz *
924 	    EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
925 
926 	ASSERT3U(minimum, <=, *start);
927 
928 	/* dn_nlevels == 1 means we don't have any L1 blocks */
929 	if (dn->dn_nlevels <= 1) {
930 		*l1blks = 0;
931 		*start = minimum;
932 		return (0);
933 	}
934 
935 	/*
936 	 * Check if we can free the entire range assuming that all of the
937 	 * L1 blocks in this range have data. If we can, we use this
938 	 * worst case value as an estimate so we can avoid having to look
939 	 * at the object's actual data.
940 	 */
941 	uint64_t total_l1blks =
942 	    (roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) /
943 	    iblkrange;
944 	if (total_l1blks <= maxblks) {
945 		*l1blks = total_l1blks;
946 		*start = minimum;
947 		return (0);
948 	}
949 	ASSERT(ISP2(iblkrange));
950 
951 	for (blks = 0; *start > minimum && blks < maxblks; blks++) {
952 		int err;
953 
954 		/*
955 		 * dnode_next_offset(BACKWARDS) will find an allocated L1
956 		 * indirect block at or before the input offset.  We must
957 		 * decrement *start so that it is at the end of the region
958 		 * to search.
959 		 */
960 		(*start)--;
961 
962 		err = dnode_next_offset(dn,
963 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
964 
965 		/* if there are no indirect blocks before start, we are done */
966 		if (err == ESRCH) {
967 			*start = minimum;
968 			break;
969 		} else if (err != 0) {
970 			*l1blks = blks;
971 			return (err);
972 		}
973 
974 		/* set start to the beginning of this L1 indirect */
975 		*start = P2ALIGN_TYPED(*start, iblkrange, uint64_t);
976 	}
977 	if (*start < minimum)
978 		*start = minimum;
979 	*l1blks = blks;
980 
981 	return (0);
982 }
983 
984 /*
985  * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,
986  * otherwise return false.
987  * Used below in dmu_free_long_range_impl() to enable abort when unmounting
988  */
989 static boolean_t
990 dmu_objset_zfs_unmounting(objset_t *os)
991 {
992 #ifdef _KERNEL
993 	if (dmu_objset_type(os) == DMU_OST_ZFS)
994 		return (zfs_get_vfs_flag_unmounted(os));
995 #else
996 	(void) os;
997 #endif
998 	return (B_FALSE);
999 }
1000 
1001 static int
1002 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
1003     uint64_t length)
1004 {
1005 	uint64_t object_size;
1006 	int err;
1007 	uint64_t dirty_frees_threshold;
1008 	dsl_pool_t *dp = dmu_objset_pool(os);
1009 
1010 	if (dn == NULL)
1011 		return (SET_ERROR(EINVAL));
1012 
1013 	object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1014 	if (offset >= object_size)
1015 		return (0);
1016 
1017 	if (zfs_per_txg_dirty_frees_percent <= 100)
1018 		dirty_frees_threshold =
1019 		    zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100;
1020 	else
1021 		dirty_frees_threshold = zfs_dirty_data_max / 20;
1022 
1023 	if (length == DMU_OBJECT_END || offset + length > object_size)
1024 		length = object_size - offset;
1025 
1026 	while (length != 0) {
1027 		uint64_t chunk_end, chunk_begin, chunk_len;
1028 		uint64_t l1blks;
1029 		dmu_tx_t *tx;
1030 
1031 		if (dmu_objset_zfs_unmounting(dn->dn_objset))
1032 			return (SET_ERROR(EINTR));
1033 
1034 		chunk_end = chunk_begin = offset + length;
1035 
1036 		/* move chunk_begin backwards to the beginning of this chunk */
1037 		err = get_next_chunk(dn, &chunk_begin, offset, &l1blks);
1038 		if (err)
1039 			return (err);
1040 		ASSERT3U(chunk_begin, >=, offset);
1041 		ASSERT3U(chunk_begin, <=, chunk_end);
1042 
1043 		chunk_len = chunk_end - chunk_begin;
1044 
1045 		tx = dmu_tx_create(os);
1046 		dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len);
1047 
1048 		/*
1049 		 * Mark this transaction as typically resulting in a net
1050 		 * reduction in space used.
1051 		 */
1052 		dmu_tx_mark_netfree(tx);
1053 		err = dmu_tx_assign(tx, TXG_WAIT);
1054 		if (err) {
1055 			dmu_tx_abort(tx);
1056 			return (err);
1057 		}
1058 
1059 		uint64_t txg = dmu_tx_get_txg(tx);
1060 
1061 		mutex_enter(&dp->dp_lock);
1062 		uint64_t long_free_dirty =
1063 		    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK];
1064 		mutex_exit(&dp->dp_lock);
1065 
1066 		/*
1067 		 * To avoid filling up a TXG with just frees, wait for
1068 		 * the next TXG to open before freeing more chunks if
1069 		 * we have reached the threshold of frees.
1070 		 */
1071 		if (dirty_frees_threshold != 0 &&
1072 		    long_free_dirty >= dirty_frees_threshold) {
1073 			DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay);
1074 			dmu_tx_commit(tx);
1075 			txg_wait_open(dp, 0, B_TRUE);
1076 			continue;
1077 		}
1078 
1079 		/*
1080 		 * In order to prevent unnecessary write throttling, for each
1081 		 * TXG, we track the cumulative size of L1 blocks being dirtied
1082 		 * in dnode_free_range() below. We compare this number to a
1083 		 * tunable threshold, past which we prevent new L1 dirty freeing
1084 		 * blocks from being added into the open TXG. See
1085 		 * dmu_free_long_range_impl() for details. The threshold
1086 		 * prevents write throttle activation due to dirty freeing L1
1087 		 * blocks taking up a large percentage of zfs_dirty_data_max.
1088 		 */
1089 		mutex_enter(&dp->dp_lock);
1090 		dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] +=
1091 		    l1blks << dn->dn_indblkshift;
1092 		mutex_exit(&dp->dp_lock);
1093 		DTRACE_PROBE3(free__long__range,
1094 		    uint64_t, long_free_dirty, uint64_t, chunk_len,
1095 		    uint64_t, txg);
1096 		dnode_free_range(dn, chunk_begin, chunk_len, tx);
1097 
1098 		dmu_tx_commit(tx);
1099 
1100 		length -= chunk_len;
1101 	}
1102 	return (0);
1103 }
1104 
1105 int
1106 dmu_free_long_range(objset_t *os, uint64_t object,
1107     uint64_t offset, uint64_t length)
1108 {
1109 	dnode_t *dn;
1110 	int err;
1111 
1112 	err = dnode_hold(os, object, FTAG, &dn);
1113 	if (err != 0)
1114 		return (err);
1115 	err = dmu_free_long_range_impl(os, dn, offset, length);
1116 
1117 	/*
1118 	 * It is important to zero out the maxblkid when freeing the entire
1119 	 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
1120 	 * will take the fast path, and (b) dnode_reallocate() can verify
1121 	 * that the entire file has been freed.
1122 	 */
1123 	if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
1124 		dn->dn_maxblkid = 0;
1125 
1126 	dnode_rele(dn, FTAG);
1127 	return (err);
1128 }
1129 
1130 int
1131 dmu_free_long_object(objset_t *os, uint64_t object)
1132 {
1133 	dmu_tx_t *tx;
1134 	int err;
1135 
1136 	err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
1137 	if (err != 0)
1138 		return (err);
1139 
1140 	tx = dmu_tx_create(os);
1141 	dmu_tx_hold_bonus(tx, object);
1142 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1143 	dmu_tx_mark_netfree(tx);
1144 	err = dmu_tx_assign(tx, TXG_WAIT);
1145 	if (err == 0) {
1146 		err = dmu_object_free(os, object, tx);
1147 		dmu_tx_commit(tx);
1148 	} else {
1149 		dmu_tx_abort(tx);
1150 	}
1151 
1152 	return (err);
1153 }
1154 
1155 int
1156 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
1157     uint64_t size, dmu_tx_t *tx)
1158 {
1159 	dnode_t *dn;
1160 	int err = dnode_hold(os, object, FTAG, &dn);
1161 	if (err)
1162 		return (err);
1163 	ASSERT(offset < UINT64_MAX);
1164 	ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset);
1165 	dnode_free_range(dn, offset, size, tx);
1166 	dnode_rele(dn, FTAG);
1167 	return (0);
1168 }
1169 
1170 static int
1171 dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,
1172     void *buf, uint32_t flags)
1173 {
1174 	dmu_buf_t **dbp;
1175 	int numbufs, err = 0;
1176 
1177 	/*
1178 	 * Deal with odd block sizes, where there can't be data past the first
1179 	 * block. If we ever do the tail block optimization, we will need to
1180 	 * handle that here as well.
1181 	 */
1182 	if (dn->dn_maxblkid == 0) {
1183 		uint64_t newsz = offset > dn->dn_datablksz ? 0 :
1184 		    MIN(size, dn->dn_datablksz - offset);
1185 		memset((char *)buf + newsz, 0, size - newsz);
1186 		size = newsz;
1187 	}
1188 
1189 	if (size == 0)
1190 		return (0);
1191 
1192 	/* Allow Direct I/O when requested and properly aligned */
1193 	if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned(buf) &&
1194 	    zfs_dio_aligned(offset, size, PAGESIZE)) {
1195 		abd_t *data = abd_get_from_buf(buf, size);
1196 		err = dmu_read_abd(dn, offset, size, data, flags);
1197 		abd_free(data);
1198 		return (err);
1199 	}
1200 
1201 	while (size > 0) {
1202 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
1203 		int i;
1204 
1205 		/*
1206 		 * NB: we could do this block-at-a-time, but it's nice
1207 		 * to be reading in parallel.
1208 		 */
1209 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
1210 		    TRUE, FTAG, &numbufs, &dbp, flags);
1211 		if (err)
1212 			break;
1213 
1214 		for (i = 0; i < numbufs; i++) {
1215 			uint64_t tocpy;
1216 			int64_t bufoff;
1217 			dmu_buf_t *db = dbp[i];
1218 
1219 			ASSERT(size > 0);
1220 
1221 			bufoff = offset - db->db_offset;
1222 			tocpy = MIN(db->db_size - bufoff, size);
1223 
1224 			(void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);
1225 
1226 			offset += tocpy;
1227 			size -= tocpy;
1228 			buf = (char *)buf + tocpy;
1229 		}
1230 		dmu_buf_rele_array(dbp, numbufs, FTAG);
1231 	}
1232 	return (err);
1233 }
1234 
1235 int
1236 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1237     void *buf, uint32_t flags)
1238 {
1239 	dnode_t *dn;
1240 	int err;
1241 
1242 	err = dnode_hold(os, object, FTAG, &dn);
1243 	if (err != 0)
1244 		return (err);
1245 
1246 	err = dmu_read_impl(dn, offset, size, buf, flags);
1247 	dnode_rele(dn, FTAG);
1248 	return (err);
1249 }
1250 
1251 int
1252 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,
1253     uint32_t flags)
1254 {
1255 	return (dmu_read_impl(dn, offset, size, buf, flags));
1256 }
1257 
1258 static void
1259 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,
1260     const void *buf, dmu_tx_t *tx)
1261 {
1262 	int i;
1263 
1264 	for (i = 0; i < numbufs; i++) {
1265 		uint64_t tocpy;
1266 		int64_t bufoff;
1267 		dmu_buf_t *db = dbp[i];
1268 
1269 		ASSERT(size > 0);
1270 
1271 		bufoff = offset - db->db_offset;
1272 		tocpy = MIN(db->db_size - bufoff, size);
1273 
1274 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1275 
1276 		if (tocpy == db->db_size)
1277 			dmu_buf_will_fill(db, tx, B_FALSE);
1278 		else
1279 			dmu_buf_will_dirty(db, tx);
1280 
1281 		(void) memcpy((char *)db->db_data + bufoff, buf, tocpy);
1282 
1283 		if (tocpy == db->db_size)
1284 			dmu_buf_fill_done(db, tx, B_FALSE);
1285 
1286 		offset += tocpy;
1287 		size -= tocpy;
1288 		buf = (char *)buf + tocpy;
1289 	}
1290 }
1291 
1292 void
1293 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1294     const void *buf, dmu_tx_t *tx)
1295 {
1296 	dmu_buf_t **dbp;
1297 	int numbufs;
1298 
1299 	if (size == 0)
1300 		return;
1301 
1302 	VERIFY0(dmu_buf_hold_array(os, object, offset, size,
1303 	    FALSE, FTAG, &numbufs, &dbp));
1304 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1305 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1306 }
1307 
1308 /*
1309  * This interface is not used internally by ZFS but is provided for
1310  * use by Lustre which is built on the DMU interfaces.
1311  */
1312 int
1313 dmu_write_by_dnode_flags(dnode_t *dn, uint64_t offset, uint64_t size,
1314     const void *buf, dmu_tx_t *tx, uint32_t flags)
1315 {
1316 	dmu_buf_t **dbp;
1317 	int numbufs;
1318 	int error;
1319 
1320 	if (size == 0)
1321 		return (0);
1322 
1323 	/* Allow Direct I/O when requested and properly aligned */
1324 	if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) &&
1325 	    zfs_dio_aligned(offset, size, dn->dn_datablksz)) {
1326 		abd_t *data = abd_get_from_buf((void *)buf, size);
1327 		error = dmu_write_abd(dn, offset, size, data, DMU_DIRECTIO, tx);
1328 		abd_free(data);
1329 		return (error);
1330 	}
1331 
1332 	VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,
1333 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH));
1334 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1335 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1336 	return (0);
1337 }
1338 
1339 int
1340 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,
1341     const void *buf, dmu_tx_t *tx)
1342 {
1343 	return (dmu_write_by_dnode_flags(dn, offset, size, buf, tx, 0));
1344 }
1345 
1346 void
1347 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1348     dmu_tx_t *tx)
1349 {
1350 	dmu_buf_t **dbp;
1351 	int numbufs, i;
1352 
1353 	if (size == 0)
1354 		return;
1355 
1356 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
1357 	    FALSE, FTAG, &numbufs, &dbp));
1358 
1359 	for (i = 0; i < numbufs; i++) {
1360 		dmu_buf_t *db = dbp[i];
1361 
1362 		dmu_buf_will_not_fill(db, tx);
1363 	}
1364 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1365 }
1366 
1367 void
1368 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
1369     void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
1370     int compressed_size, int byteorder, dmu_tx_t *tx)
1371 {
1372 	dmu_buf_t *db;
1373 
1374 	ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
1375 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
1376 	VERIFY0(dmu_buf_hold_noread(os, object, offset,
1377 	    FTAG, &db));
1378 
1379 	dmu_buf_write_embedded(db,
1380 	    data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
1381 	    uncompressed_size, compressed_size, byteorder, tx);
1382 
1383 	dmu_buf_rele(db, FTAG);
1384 }
1385 
1386 void
1387 dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1388     dmu_tx_t *tx)
1389 {
1390 	int numbufs, i;
1391 	dmu_buf_t **dbp;
1392 
1393 	VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG,
1394 	    &numbufs, &dbp));
1395 	for (i = 0; i < numbufs; i++)
1396 		dmu_buf_redact(dbp[i], tx);
1397 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1398 }
1399 
1400 #ifdef _KERNEL
1401 int
1402 dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size)
1403 {
1404 	dmu_buf_t **dbp;
1405 	int numbufs, i, err;
1406 
1407 	if (uio->uio_extflg & UIO_DIRECT)
1408 		return (dmu_read_uio_direct(dn, uio, size));
1409 
1410 	/*
1411 	 * NB: we could do this block-at-a-time, but it's nice
1412 	 * to be reading in parallel.
1413 	 */
1414 	err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size,
1415 	    TRUE, FTAG, &numbufs, &dbp, 0);
1416 	if (err)
1417 		return (err);
1418 
1419 	for (i = 0; i < numbufs; i++) {
1420 		uint64_t tocpy;
1421 		int64_t bufoff;
1422 		dmu_buf_t *db = dbp[i];
1423 
1424 		ASSERT(size > 0);
1425 
1426 		bufoff = zfs_uio_offset(uio) - db->db_offset;
1427 		tocpy = MIN(db->db_size - bufoff, size);
1428 
1429 		err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,
1430 		    UIO_READ, uio);
1431 
1432 		if (err)
1433 			break;
1434 
1435 		size -= tocpy;
1436 	}
1437 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1438 
1439 	return (err);
1440 }
1441 
1442 /*
1443  * Read 'size' bytes into the uio buffer.
1444  * From object zdb->db_object.
1445  * Starting at zfs_uio_offset(uio).
1446  *
1447  * If the caller already has a dbuf in the target object
1448  * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1449  * because we don't have to find the dnode_t for the object.
1450  */
1451 int
1452 dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size)
1453 {
1454 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1455 	int err;
1456 
1457 	if (size == 0)
1458 		return (0);
1459 
1460 	DB_DNODE_ENTER(db);
1461 	err = dmu_read_uio_dnode(DB_DNODE(db), uio, size);
1462 	DB_DNODE_EXIT(db);
1463 
1464 	return (err);
1465 }
1466 
1467 /*
1468  * Read 'size' bytes into the uio buffer.
1469  * From the specified object
1470  * Starting at offset zfs_uio_offset(uio).
1471  */
1472 int
1473 dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size)
1474 {
1475 	dnode_t *dn;
1476 	int err;
1477 
1478 	if (size == 0)
1479 		return (0);
1480 
1481 	err = dnode_hold(os, object, FTAG, &dn);
1482 	if (err)
1483 		return (err);
1484 
1485 	err = dmu_read_uio_dnode(dn, uio, size);
1486 
1487 	dnode_rele(dn, FTAG);
1488 
1489 	return (err);
1490 }
1491 
1492 int
1493 dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx)
1494 {
1495 	dmu_buf_t **dbp;
1496 	int numbufs;
1497 	int err = 0;
1498 	uint64_t write_size;
1499 
1500 top:
1501 	write_size = size;
1502 
1503 	/*
1504 	 * We only allow Direct I/O writes to happen if we are block
1505 	 * sized aligned. Otherwise, we pass the write off to the ARC.
1506 	 */
1507 	if ((uio->uio_extflg & UIO_DIRECT) &&
1508 	    (write_size >= dn->dn_datablksz)) {
1509 		if (zfs_dio_aligned(zfs_uio_offset(uio), write_size,
1510 		    dn->dn_datablksz)) {
1511 			return (dmu_write_uio_direct(dn, uio, size, tx));
1512 		} else if (write_size > dn->dn_datablksz &&
1513 		    zfs_dio_offset_aligned(zfs_uio_offset(uio),
1514 		    dn->dn_datablksz)) {
1515 			write_size =
1516 			    dn->dn_datablksz * (write_size / dn->dn_datablksz);
1517 			err = dmu_write_uio_direct(dn, uio, write_size, tx);
1518 			if (err == 0) {
1519 				size -= write_size;
1520 				goto top;
1521 			} else {
1522 				return (err);
1523 			}
1524 		} else {
1525 			write_size =
1526 			    P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz);
1527 		}
1528 	}
1529 
1530 	err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size,
1531 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1532 	if (err)
1533 		return (err);
1534 
1535 	for (int i = 0; i < numbufs; i++) {
1536 		uint64_t tocpy;
1537 		int64_t bufoff;
1538 		dmu_buf_t *db = dbp[i];
1539 
1540 		ASSERT(write_size > 0);
1541 
1542 		offset_t off = zfs_uio_offset(uio);
1543 		bufoff = off - db->db_offset;
1544 		tocpy = MIN(db->db_size - bufoff, write_size);
1545 
1546 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1547 
1548 		if (tocpy == db->db_size)
1549 			dmu_buf_will_fill(db, tx, B_TRUE);
1550 		else
1551 			dmu_buf_will_dirty(db, tx);
1552 
1553 		err = zfs_uio_fault_move((char *)db->db_data + bufoff,
1554 		    tocpy, UIO_WRITE, uio);
1555 
1556 		if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {
1557 			/* The fill was reverted.  Undo any uio progress. */
1558 			zfs_uio_advance(uio, off - zfs_uio_offset(uio));
1559 		}
1560 
1561 		if (err)
1562 			break;
1563 
1564 		write_size -= tocpy;
1565 		size -= tocpy;
1566 	}
1567 
1568 	IMPLY(err == 0, write_size == 0);
1569 
1570 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1571 
1572 	if ((uio->uio_extflg & UIO_DIRECT) && size > 0) {
1573 		goto top;
1574 	}
1575 
1576 	return (err);
1577 }
1578 
1579 /*
1580  * Write 'size' bytes from the uio buffer.
1581  * To object zdb->db_object.
1582  * Starting at offset zfs_uio_offset(uio).
1583  *
1584  * If the caller already has a dbuf in the target object
1585  * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1586  * because we don't have to find the dnode_t for the object.
1587  */
1588 int
1589 dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,
1590     dmu_tx_t *tx)
1591 {
1592 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1593 	int err;
1594 
1595 	if (size == 0)
1596 		return (0);
1597 
1598 	DB_DNODE_ENTER(db);
1599 	err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx);
1600 	DB_DNODE_EXIT(db);
1601 
1602 	return (err);
1603 }
1604 
1605 /*
1606  * Write 'size' bytes from the uio buffer.
1607  * To the specified object.
1608  * Starting at offset zfs_uio_offset(uio).
1609  */
1610 int
1611 dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,
1612     dmu_tx_t *tx)
1613 {
1614 	dnode_t *dn;
1615 	int err;
1616 
1617 	if (size == 0)
1618 		return (0);
1619 
1620 	err = dnode_hold(os, object, FTAG, &dn);
1621 	if (err)
1622 		return (err);
1623 
1624 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1625 
1626 	dnode_rele(dn, FTAG);
1627 
1628 	return (err);
1629 }
1630 #endif /* _KERNEL */
1631 
1632 static void
1633 dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps,
1634     uint64_t *l1sz, uint64_t *l2sz)
1635 {
1636 	int cached_flags;
1637 
1638 	if (bps == NULL)
1639 		return;
1640 
1641 	for (size_t blk_off = 0; blk_off < nbps; blk_off++) {
1642 		blkptr_t *bp = &bps[blk_off];
1643 
1644 		if (BP_IS_HOLE(bp))
1645 			continue;
1646 
1647 		cached_flags = arc_cached(spa, bp);
1648 		if (cached_flags == 0)
1649 			continue;
1650 
1651 		if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) ==
1652 		    ARC_CACHED_IN_L2)
1653 			*l2sz += BP_GET_LSIZE(bp);
1654 		else
1655 			*l1sz += BP_GET_LSIZE(bp);
1656 	}
1657 }
1658 
1659 /*
1660  * Estimate DMU object cached size.
1661  */
1662 int
1663 dmu_object_cached_size(objset_t *os, uint64_t object,
1664     uint64_t *l1sz, uint64_t *l2sz)
1665 {
1666 	dnode_t *dn;
1667 	dmu_object_info_t doi;
1668 	int err = 0;
1669 
1670 	*l1sz = *l2sz = 0;
1671 
1672 	if (dnode_hold(os, object, FTAG, &dn) != 0)
1673 		return (0);
1674 
1675 	if (dn->dn_nlevels < 2) {
1676 		dnode_rele(dn, FTAG);
1677 		return (0);
1678 	}
1679 
1680 	dmu_object_info_from_dnode(dn, &doi);
1681 
1682 	for (uint64_t off = 0; off < doi.doi_max_offset;
1683 	    off += dmu_prefetch_max) {
1684 		/* dbuf_read doesn't prefetch L1 blocks. */
1685 		dmu_prefetch_by_dnode(dn, 1, off,
1686 		    dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ);
1687 	}
1688 
1689 	/*
1690 	 * Hold all valid L1 blocks, asking ARC the status of each BP
1691 	 * contained in each such L1 block.
1692 	 */
1693 	uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);
1694 	uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps);
1695 
1696 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1697 	for (uint64_t blk = 0; blk < l1blks; blk++) {
1698 		dmu_buf_impl_t *db = NULL;
1699 
1700 		if (issig()) {
1701 			/*
1702 			 * On interrupt, get out, and bubble up EINTR
1703 			 */
1704 			err = EINTR;
1705 			break;
1706 		}
1707 
1708 		/*
1709 		 * If we get an i/o error here, the L1 can't be read,
1710 		 * and nothing under it could be cached, so we just
1711 		 * continue. Ignoring the error from dbuf_hold_impl
1712 		 * or from dbuf_read is then a reasonable choice.
1713 		 */
1714 		err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db);
1715 		if (err != 0) {
1716 			/*
1717 			 * ignore error and continue
1718 			 */
1719 			err = 0;
1720 			continue;
1721 		}
1722 
1723 		err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1724 		if (err == 0) {
1725 			dmu_cached_bps(dmu_objset_spa(os), db->db.db_data,
1726 			    nbps, l1sz, l2sz);
1727 		}
1728 		/*
1729 		 * error may be ignored, and we continue
1730 		 */
1731 		err = 0;
1732 		dbuf_rele(db, FTAG);
1733 	}
1734 	rw_exit(&dn->dn_struct_rwlock);
1735 
1736 	dnode_rele(dn, FTAG);
1737 	return (err);
1738 }
1739 
1740 /*
1741  * Allocate a loaned anonymous arc buffer.
1742  */
1743 arc_buf_t *
1744 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1745 {
1746 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1747 
1748 	return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));
1749 }
1750 
1751 /*
1752  * Free a loaned arc buffer.
1753  */
1754 void
1755 dmu_return_arcbuf(arc_buf_t *buf)
1756 {
1757 	arc_return_buf(buf, FTAG);
1758 	arc_buf_destroy(buf, FTAG);
1759 }
1760 
1761 /*
1762  * A "lightweight" write is faster than a regular write (e.g.
1763  * dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the
1764  * CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t.  However, the
1765  * data can not be read or overwritten until the transaction's txg has been
1766  * synced.  This makes it appropriate for workloads that are known to be
1767  * (temporarily) write-only, like "zfs receive".
1768  *
1769  * A single block is written, starting at the specified offset in bytes.  If
1770  * the call is successful, it returns 0 and the provided abd has been
1771  * consumed (the caller should not free it).
1772  */
1773 int
1774 dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,
1775     const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx)
1776 {
1777 	dbuf_dirty_record_t *dr =
1778 	    dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx);
1779 	if (dr == NULL)
1780 		return (SET_ERROR(EIO));
1781 	dr->dt.dll.dr_abd = abd;
1782 	dr->dt.dll.dr_props = *zp;
1783 	dr->dt.dll.dr_flags = flags;
1784 	return (0);
1785 }
1786 
1787 /*
1788  * When possible directly assign passed loaned arc buffer to a dbuf.
1789  * If this is not possible copy the contents of passed arc buf via
1790  * dmu_write().
1791  */
1792 int
1793 dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,
1794     dmu_tx_t *tx)
1795 {
1796 	dmu_buf_impl_t *db;
1797 	objset_t *os = dn->dn_objset;
1798 	uint64_t object = dn->dn_object;
1799 	uint32_t blksz = (uint32_t)arc_buf_lsize(buf);
1800 	uint64_t blkid;
1801 
1802 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1803 	blkid = dbuf_whichblock(dn, 0, offset);
1804 	db = dbuf_hold(dn, blkid, FTAG);
1805 	rw_exit(&dn->dn_struct_rwlock);
1806 	if (db == NULL)
1807 		return (SET_ERROR(EIO));
1808 
1809 	/*
1810 	 * We can only assign if the offset is aligned and the arc buf is the
1811 	 * same size as the dbuf.
1812 	 */
1813 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
1814 		zfs_racct_write(os->os_spa, blksz, 1, 0);
1815 		dbuf_assign_arcbuf(db, buf, tx);
1816 		dbuf_rele(db, FTAG);
1817 	} else {
1818 		/* compressed bufs must always be assignable to their dbuf */
1819 		ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);
1820 		ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));
1821 
1822 		dbuf_rele(db, FTAG);
1823 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
1824 		dmu_return_arcbuf(buf);
1825 	}
1826 
1827 	return (0);
1828 }
1829 
1830 int
1831 dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1832     dmu_tx_t *tx)
1833 {
1834 	int err;
1835 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1836 
1837 	DB_DNODE_ENTER(db);
1838 	err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx);
1839 	DB_DNODE_EXIT(db);
1840 
1841 	return (err);
1842 }
1843 
1844 void
1845 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1846 {
1847 	(void) buf;
1848 	dmu_sync_arg_t *dsa = varg;
1849 
1850 	if (zio->io_error == 0) {
1851 		dbuf_dirty_record_t *dr = dsa->dsa_dr;
1852 		blkptr_t *bp = zio->io_bp;
1853 
1854 		if (BP_IS_HOLE(bp)) {
1855 			dmu_buf_t *db = NULL;
1856 			if (dr)
1857 				db = &(dr->dr_dbuf->db);
1858 			else
1859 				db = dsa->dsa_zgd->zgd_db;
1860 			/*
1861 			 * A block of zeros may compress to a hole, but the
1862 			 * block size still needs to be known for replay.
1863 			 */
1864 			BP_SET_LSIZE(bp, db->db_size);
1865 		} else if (!BP_IS_EMBEDDED(bp)) {
1866 			ASSERT(BP_GET_LEVEL(bp) == 0);
1867 			BP_SET_FILL(bp, 1);
1868 		}
1869 	}
1870 }
1871 
1872 static void
1873 dmu_sync_late_arrival_ready(zio_t *zio)
1874 {
1875 	dmu_sync_ready(zio, NULL, zio->io_private);
1876 }
1877 
1878 void
1879 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1880 {
1881 	(void) buf;
1882 	dmu_sync_arg_t *dsa = varg;
1883 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1884 	dmu_buf_impl_t *db = dr->dr_dbuf;
1885 	zgd_t *zgd = dsa->dsa_zgd;
1886 
1887 	/*
1888 	 * Record the vdev(s) backing this blkptr so they can be flushed after
1889 	 * the writes for the lwb have completed.
1890 	 */
1891 	if (zgd && zio->io_error == 0) {
1892 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1893 	}
1894 
1895 	mutex_enter(&db->db_mtx);
1896 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1897 	if (zio->io_error == 0) {
1898 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
1899 		if (dr->dt.dl.dr_nopwrite) {
1900 			blkptr_t *bp = zio->io_bp;
1901 			blkptr_t *bp_orig = &zio->io_bp_orig;
1902 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
1903 
1904 			ASSERT(BP_EQUAL(bp, bp_orig));
1905 			VERIFY(BP_EQUAL(bp, db->db_blkptr));
1906 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
1907 			VERIFY(zio_checksum_table[chksum].ci_flags &
1908 			    ZCHECKSUM_FLAG_NOPWRITE);
1909 		}
1910 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1911 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1912 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1913 
1914 		/*
1915 		 * Old style holes are filled with all zeros, whereas
1916 		 * new-style holes maintain their lsize, type, level,
1917 		 * and birth time (see zio_write_compress). While we
1918 		 * need to reset the BP_SET_LSIZE() call that happened
1919 		 * in dmu_sync_ready for old style holes, we do *not*
1920 		 * want to wipe out the information contained in new
1921 		 * style holes. Thus, only zero out the block pointer if
1922 		 * it's an old style hole.
1923 		 */
1924 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
1925 		    BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0)
1926 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1927 	} else {
1928 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1929 	}
1930 
1931 	cv_broadcast(&db->db_changed);
1932 	mutex_exit(&db->db_mtx);
1933 
1934 	if (dsa->dsa_done)
1935 		dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1936 
1937 	kmem_free(dsa, sizeof (*dsa));
1938 }
1939 
1940 static void
1941 dmu_sync_late_arrival_done(zio_t *zio)
1942 {
1943 	blkptr_t *bp = zio->io_bp;
1944 	dmu_sync_arg_t *dsa = zio->io_private;
1945 	zgd_t *zgd = dsa->dsa_zgd;
1946 
1947 	if (zio->io_error == 0) {
1948 		/*
1949 		 * Record the vdev(s) backing this blkptr so they can be
1950 		 * flushed after the writes for the lwb have completed.
1951 		 */
1952 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1953 
1954 		if (!BP_IS_HOLE(bp)) {
1955 			blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig;
1956 			ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));
1957 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1958 			ASSERT(BP_GET_LOGICAL_BIRTH(zio->io_bp) == zio->io_txg);
1959 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1960 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1961 		}
1962 	}
1963 
1964 	dmu_tx_commit(dsa->dsa_tx);
1965 
1966 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1967 
1968 	abd_free(zio->io_abd);
1969 	kmem_free(dsa, sizeof (*dsa));
1970 }
1971 
1972 static int
1973 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1974     zio_prop_t *zp, zbookmark_phys_t *zb)
1975 {
1976 	dmu_sync_arg_t *dsa;
1977 	dmu_tx_t *tx;
1978 	int error;
1979 
1980 	error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL,
1981 	    DB_RF_CANFAIL | DB_RF_NOPREFETCH);
1982 	if (error != 0)
1983 		return (error);
1984 
1985 	tx = dmu_tx_create(os);
1986 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
1987 	/*
1988 	 * This transaction does not produce any dirty data or log blocks, so
1989 	 * it should not be throttled.  All other cases wait for TXG sync, by
1990 	 * which time the log block we are writing will be obsolete, so we can
1991 	 * skip waiting and just return error here instead.
1992 	 */
1993 	if (dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE) != 0) {
1994 		dmu_tx_abort(tx);
1995 		/* Make zl_get_data do txg_waited_synced() */
1996 		return (SET_ERROR(EIO));
1997 	}
1998 
1999 	/*
2000 	 * In order to prevent the zgd's lwb from being free'd prior to
2001 	 * dmu_sync_late_arrival_done() being called, we have to ensure
2002 	 * the lwb's "max txg" takes this tx's txg into account.
2003 	 */
2004 	zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));
2005 
2006 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2007 	dsa->dsa_dr = NULL;
2008 	dsa->dsa_done = done;
2009 	dsa->dsa_zgd = zgd;
2010 	dsa->dsa_tx = tx;
2011 
2012 	/*
2013 	 * Since we are currently syncing this txg, it's nontrivial to
2014 	 * determine what BP to nopwrite against, so we disable nopwrite.
2015 	 *
2016 	 * When syncing, the db_blkptr is initially the BP of the previous
2017 	 * txg.  We can not nopwrite against it because it will be changed
2018 	 * (this is similar to the non-late-arrival case where the dbuf is
2019 	 * dirty in a future txg).
2020 	 *
2021 	 * Then dbuf_write_ready() sets bp_blkptr to the location we will write.
2022 	 * We can not nopwrite against it because although the BP will not
2023 	 * (typically) be changed, the data has not yet been persisted to this
2024 	 * location.
2025 	 *
2026 	 * Finally, when dbuf_write_done() is called, it is theoretically
2027 	 * possible to always nopwrite, because the data that was written in
2028 	 * this txg is the same data that we are trying to write.  However we
2029 	 * would need to check that this dbuf is not dirty in any future
2030 	 * txg's (as we do in the normal dmu_sync() path). For simplicity, we
2031 	 * don't nopwrite in this case.
2032 	 */
2033 	zp->zp_nopwrite = B_FALSE;
2034 
2035 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
2036 	    abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),
2037 	    zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,
2038 	    dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,
2039 	    dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
2040 
2041 	return (0);
2042 }
2043 
2044 /*
2045  * Intent log support: sync the block associated with db to disk.
2046  * N.B. and XXX: the caller is responsible for making sure that the
2047  * data isn't changing while dmu_sync() is writing it.
2048  *
2049  * Return values:
2050  *
2051  *	EEXIST: this txg has already been synced, so there's nothing to do.
2052  *		The caller should not log the write.
2053  *
2054  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
2055  *		The caller should not log the write.
2056  *
2057  *	EALREADY: this block is already in the process of being synced.
2058  *		The caller should track its progress (somehow).
2059  *
2060  *	EIO: could not do the I/O.
2061  *		The caller should do a txg_wait_synced().
2062  *
2063  *	0: the I/O has been initiated.
2064  *		The caller should log this blkptr in the done callback.
2065  *		It is possible that the I/O will fail, in which case
2066  *		the error will be reported to the done callback and
2067  *		propagated to pio from zio_done().
2068  */
2069 int
2070 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
2071 {
2072 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
2073 	objset_t *os = db->db_objset;
2074 	dsl_dataset_t *ds = os->os_dsl_dataset;
2075 	dbuf_dirty_record_t *dr, *dr_next;
2076 	dmu_sync_arg_t *dsa;
2077 	zbookmark_phys_t zb;
2078 	zio_prop_t zp;
2079 
2080 	ASSERT(pio != NULL);
2081 	ASSERT(txg != 0);
2082 
2083 	SET_BOOKMARK(&zb, ds->ds_object,
2084 	    db->db.db_object, db->db_level, db->db_blkid);
2085 
2086 	DB_DNODE_ENTER(db);
2087 	dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp);
2088 	DB_DNODE_EXIT(db);
2089 
2090 	/*
2091 	 * If we're frozen (running ziltest), we always need to generate a bp.
2092 	 */
2093 	if (txg > spa_freeze_txg(os->os_spa))
2094 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2095 
2096 	/*
2097 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
2098 	 * and us.  If we determine that this txg is not yet syncing,
2099 	 * but it begins to sync a moment later, that's OK because the
2100 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
2101 	 */
2102 	mutex_enter(&db->db_mtx);
2103 
2104 	if (txg <= spa_last_synced_txg(os->os_spa)) {
2105 		/*
2106 		 * This txg has already synced.  There's nothing to do.
2107 		 */
2108 		mutex_exit(&db->db_mtx);
2109 		return (SET_ERROR(EEXIST));
2110 	}
2111 
2112 	if (txg <= spa_syncing_txg(os->os_spa)) {
2113 		/*
2114 		 * This txg is currently syncing, so we can't mess with
2115 		 * the dirty record anymore; just write a new log block.
2116 		 */
2117 		mutex_exit(&db->db_mtx);
2118 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2119 	}
2120 
2121 	dr = dbuf_find_dirty_eq(db, txg);
2122 
2123 	if (dr == NULL) {
2124 		/*
2125 		 * There's no dr for this dbuf, so it must have been freed.
2126 		 * There's no need to log writes to freed blocks, so we're done.
2127 		 */
2128 		mutex_exit(&db->db_mtx);
2129 		return (SET_ERROR(ENOENT));
2130 	}
2131 
2132 	dr_next = list_next(&db->db_dirty_records, dr);
2133 	ASSERT(dr_next == NULL || dr_next->dr_txg < txg);
2134 
2135 	if (db->db_blkptr != NULL) {
2136 		/*
2137 		 * We need to fill in zgd_bp with the current blkptr so that
2138 		 * the nopwrite code can check if we're writing the same
2139 		 * data that's already on disk.  We can only nopwrite if we
2140 		 * are sure that after making the copy, db_blkptr will not
2141 		 * change until our i/o completes.  We ensure this by
2142 		 * holding the db_mtx, and only allowing nopwrite if the
2143 		 * block is not already dirty (see below).  This is verified
2144 		 * by dmu_sync_done(), which VERIFYs that the db_blkptr has
2145 		 * not changed.
2146 		 */
2147 		*zgd->zgd_bp = *db->db_blkptr;
2148 	}
2149 
2150 	/*
2151 	 * Assume the on-disk data is X, the current syncing data (in
2152 	 * txg - 1) is Y, and the current in-memory data is Z (currently
2153 	 * in dmu_sync).
2154 	 *
2155 	 * We usually want to perform a nopwrite if X and Z are the
2156 	 * same.  However, if Y is different (i.e. the BP is going to
2157 	 * change before this write takes effect), then a nopwrite will
2158 	 * be incorrect - we would override with X, which could have
2159 	 * been freed when Y was written.
2160 	 *
2161 	 * (Note that this is not a concern when we are nop-writing from
2162 	 * syncing context, because X and Y must be identical, because
2163 	 * all previous txgs have been synced.)
2164 	 *
2165 	 * Therefore, we disable nopwrite if the current BP could change
2166 	 * before this TXG.  There are two ways it could change: by
2167 	 * being dirty (dr_next is non-NULL), or by being freed
2168 	 * (dnode_block_freed()).  This behavior is verified by
2169 	 * zio_done(), which VERIFYs that the override BP is identical
2170 	 * to the on-disk BP.
2171 	 */
2172 	if (dr_next != NULL) {
2173 		zp.zp_nopwrite = B_FALSE;
2174 	} else {
2175 		DB_DNODE_ENTER(db);
2176 		if (dnode_block_freed(DB_DNODE(db), db->db_blkid))
2177 			zp.zp_nopwrite = B_FALSE;
2178 		DB_DNODE_EXIT(db);
2179 	}
2180 
2181 	ASSERT(dr->dr_txg == txg);
2182 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
2183 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2184 		/*
2185 		 * We have already issued a sync write for this buffer,
2186 		 * or this buffer has already been synced.  It could not
2187 		 * have been dirtied since, or we would have cleared the state.
2188 		 */
2189 		mutex_exit(&db->db_mtx);
2190 		return (SET_ERROR(EALREADY));
2191 	}
2192 
2193 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2194 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
2195 	mutex_exit(&db->db_mtx);
2196 
2197 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2198 	dsa->dsa_dr = dr;
2199 	dsa->dsa_done = done;
2200 	dsa->dsa_zgd = zgd;
2201 	dsa->dsa_tx = NULL;
2202 
2203 	zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp,
2204 	    dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db),
2205 	    dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL,
2206 	    dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL,
2207 	    &zb));
2208 
2209 	return (0);
2210 }
2211 
2212 int
2213 dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx)
2214 {
2215 	dnode_t *dn;
2216 	int err;
2217 
2218 	err = dnode_hold(os, object, FTAG, &dn);
2219 	if (err)
2220 		return (err);
2221 	err = dnode_set_nlevels(dn, nlevels, tx);
2222 	dnode_rele(dn, FTAG);
2223 	return (err);
2224 }
2225 
2226 int
2227 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
2228     dmu_tx_t *tx)
2229 {
2230 	dnode_t *dn;
2231 	int err;
2232 
2233 	err = dnode_hold(os, object, FTAG, &dn);
2234 	if (err)
2235 		return (err);
2236 	err = dnode_set_blksz(dn, size, ibs, tx);
2237 	dnode_rele(dn, FTAG);
2238 	return (err);
2239 }
2240 
2241 int
2242 dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid,
2243     dmu_tx_t *tx)
2244 {
2245 	dnode_t *dn;
2246 	int err;
2247 
2248 	err = dnode_hold(os, object, FTAG, &dn);
2249 	if (err)
2250 		return (err);
2251 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2252 	dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE);
2253 	rw_exit(&dn->dn_struct_rwlock);
2254 	dnode_rele(dn, FTAG);
2255 	return (0);
2256 }
2257 
2258 void
2259 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
2260     dmu_tx_t *tx)
2261 {
2262 	dnode_t *dn;
2263 
2264 	/*
2265 	 * Send streams include each object's checksum function.  This
2266 	 * check ensures that the receiving system can understand the
2267 	 * checksum function transmitted.
2268 	 */
2269 	ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
2270 
2271 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2272 	ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
2273 	dn->dn_checksum = checksum;
2274 	dnode_setdirty(dn, tx);
2275 	dnode_rele(dn, FTAG);
2276 }
2277 
2278 void
2279 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
2280     dmu_tx_t *tx)
2281 {
2282 	dnode_t *dn;
2283 
2284 	/*
2285 	 * Send streams include each object's compression function.  This
2286 	 * check ensures that the receiving system can understand the
2287 	 * compression function transmitted.
2288 	 */
2289 	ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
2290 
2291 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2292 	dn->dn_compress = compress;
2293 	dnode_setdirty(dn, tx);
2294 	dnode_rele(dn, FTAG);
2295 }
2296 
2297 /*
2298  * When the "redundant_metadata" property is set to "most", only indirect
2299  * blocks of this level and higher will have an additional ditto block.
2300  */
2301 static const int zfs_redundant_metadata_most_ditto_level = 2;
2302 
2303 void
2304 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
2305 {
2306 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
2307 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
2308 	    (wp & WP_SPILL));
2309 	enum zio_checksum checksum = os->os_checksum;
2310 	enum zio_compress compress = os->os_compress;
2311 	uint8_t complevel = os->os_complevel;
2312 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
2313 	boolean_t dedup = B_FALSE;
2314 	boolean_t nopwrite = B_FALSE;
2315 	boolean_t dedup_verify = os->os_dedup_verify;
2316 	boolean_t encrypt = B_FALSE;
2317 	int copies = os->os_copies;
2318 
2319 	/*
2320 	 * We maintain different write policies for each of the following
2321 	 * types of data:
2322 	 *	 1. metadata
2323 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
2324 	 *	 3. all other level 0 blocks
2325 	 */
2326 	if (ismd) {
2327 		/*
2328 		 * XXX -- we should design a compression algorithm
2329 		 * that specializes in arrays of bps.
2330 		 */
2331 		compress = zio_compress_select(os->os_spa,
2332 		    ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
2333 
2334 		/*
2335 		 * Metadata always gets checksummed.  If the data
2336 		 * checksum is multi-bit correctable, and it's not a
2337 		 * ZBT-style checksum, then it's suitable for metadata
2338 		 * as well.  Otherwise, the metadata checksum defaults
2339 		 * to fletcher4.
2340 		 */
2341 		if (!(zio_checksum_table[checksum].ci_flags &
2342 		    ZCHECKSUM_FLAG_METADATA) ||
2343 		    (zio_checksum_table[checksum].ci_flags &
2344 		    ZCHECKSUM_FLAG_EMBEDDED))
2345 			checksum = ZIO_CHECKSUM_FLETCHER_4;
2346 
2347 		switch (os->os_redundant_metadata) {
2348 		case ZFS_REDUNDANT_METADATA_ALL:
2349 			copies++;
2350 			break;
2351 		case ZFS_REDUNDANT_METADATA_MOST:
2352 			if (level >= zfs_redundant_metadata_most_ditto_level ||
2353 			    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))
2354 				copies++;
2355 			break;
2356 		case ZFS_REDUNDANT_METADATA_SOME:
2357 			if (DMU_OT_IS_CRITICAL(type))
2358 				copies++;
2359 			break;
2360 		case ZFS_REDUNDANT_METADATA_NONE:
2361 			break;
2362 		}
2363 
2364 		if (dmu_ddt_copies > 0) {
2365 			/*
2366 			 * If this tuneable is set, and this is a write for a
2367 			 * dedup entry store (zap or log), then we treat it
2368 			 * something like ZFS_REDUNDANT_METADATA_MOST on a
2369 			 * regular dataset: this many copies, and one more for
2370 			 * "higher" indirect blocks. This specific exception is
2371 			 * necessary because dedup objects are stored in the
2372 			 * MOS, which always has the highest possible copies.
2373 			 */
2374 			dmu_object_type_t stype =
2375 			    dn ? dn->dn_storage_type : DMU_OT_NONE;
2376 			if (stype == DMU_OT_NONE)
2377 				stype = type;
2378 			if (stype == DMU_OT_DDT_ZAP) {
2379 				copies = dmu_ddt_copies;
2380 				if (level >=
2381 				    zfs_redundant_metadata_most_ditto_level)
2382 					copies++;
2383 			}
2384 		}
2385 	} else if (wp & WP_NOFILL) {
2386 		ASSERT(level == 0);
2387 
2388 		/*
2389 		 * If we're writing preallocated blocks, we aren't actually
2390 		 * writing them so don't set any policy properties.  These
2391 		 * blocks are currently only used by an external subsystem
2392 		 * outside of zfs (i.e. dump) and not written by the zio
2393 		 * pipeline.
2394 		 */
2395 		compress = ZIO_COMPRESS_OFF;
2396 		checksum = ZIO_CHECKSUM_OFF;
2397 	} else {
2398 		compress = zio_compress_select(os->os_spa, dn->dn_compress,
2399 		    compress);
2400 		complevel = zio_complevel_select(os->os_spa, compress,
2401 		    complevel, complevel);
2402 
2403 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
2404 		    zio_checksum_select(dn->dn_checksum, checksum) :
2405 		    dedup_checksum;
2406 
2407 		/*
2408 		 * Determine dedup setting.  If we are in dmu_sync(),
2409 		 * we won't actually dedup now because that's all
2410 		 * done in syncing context; but we do want to use the
2411 		 * dedup checksum.  If the checksum is not strong
2412 		 * enough to ensure unique signatures, force
2413 		 * dedup_verify.
2414 		 */
2415 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
2416 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
2417 			if (!(zio_checksum_table[checksum].ci_flags &
2418 			    ZCHECKSUM_FLAG_DEDUP))
2419 				dedup_verify = B_TRUE;
2420 		}
2421 
2422 		/*
2423 		 * Enable nopwrite if we have secure enough checksum
2424 		 * algorithm (see comment in zio_nop_write) and
2425 		 * compression is enabled.  We don't enable nopwrite if
2426 		 * dedup is enabled as the two features are mutually
2427 		 * exclusive.
2428 		 */
2429 		nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
2430 		    ZCHECKSUM_FLAG_NOPWRITE) &&
2431 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
2432 	}
2433 
2434 	/*
2435 	 * All objects in an encrypted objset are protected from modification
2436 	 * via a MAC. Encrypted objects store their IV and salt in the last DVA
2437 	 * in the bp, so we cannot use all copies. Encrypted objects are also
2438 	 * not subject to nopwrite since writing the same data will still
2439 	 * result in a new ciphertext. Only encrypted blocks can be dedup'd
2440 	 * to avoid ambiguity in the dedup code since the DDT does not store
2441 	 * object types.
2442 	 */
2443 	if (os->os_encrypted && (wp & WP_NOFILL) == 0) {
2444 		encrypt = B_TRUE;
2445 
2446 		if (DMU_OT_IS_ENCRYPTED(type)) {
2447 			copies = MIN(copies, SPA_DVAS_PER_BP - 1);
2448 			nopwrite = B_FALSE;
2449 		} else {
2450 			dedup = B_FALSE;
2451 		}
2452 
2453 		if (level <= 0 &&
2454 		    (type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) {
2455 			compress = ZIO_COMPRESS_EMPTY;
2456 		}
2457 	}
2458 
2459 	zp->zp_compress = compress;
2460 	zp->zp_complevel = complevel;
2461 	zp->zp_checksum = checksum;
2462 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
2463 	zp->zp_level = level;
2464 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
2465 	zp->zp_dedup = dedup;
2466 	zp->zp_dedup_verify = dedup && dedup_verify;
2467 	zp->zp_nopwrite = nopwrite;
2468 	zp->zp_encrypt = encrypt;
2469 	zp->zp_byteorder = ZFS_HOST_BYTEORDER;
2470 	zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE;
2471 	memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN);
2472 	memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN);
2473 	memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN);
2474 	zp->zp_zpl_smallblk = DMU_OT_IS_FILE(zp->zp_type) ?
2475 	    os->os_zpl_special_smallblock : 0;
2476 	zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE;
2477 
2478 	ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);
2479 }
2480 
2481 /*
2482  * Reports the location of data and holes in an object.  In order to
2483  * accurately report holes all dirty data must be synced to disk.  This
2484  * causes extremely poor performance when seeking for holes in a dirty file.
2485  * As a compromise, only provide hole data when the dnode is clean.  When
2486  * a dnode is dirty report the dnode as having no holes by returning EBUSY
2487  * which is always safe to do.
2488  */
2489 int
2490 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
2491 {
2492 	dnode_t *dn;
2493 	int restarted = 0, err;
2494 
2495 restart:
2496 	err = dnode_hold(os, object, FTAG, &dn);
2497 	if (err)
2498 		return (err);
2499 
2500 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
2501 
2502 	if (dnode_is_dirty(dn)) {
2503 		/*
2504 		 * If the zfs_dmu_offset_next_sync module option is enabled
2505 		 * then hole reporting has been requested.  Dirty dnodes
2506 		 * must be synced to disk to accurately report holes.
2507 		 *
2508 		 * Provided a RL_READER rangelock spanning 0-UINT64_MAX is
2509 		 * held by the caller only a single restart will be required.
2510 		 * We tolerate callers which do not hold the rangelock by
2511 		 * returning EBUSY and not reporting holes after one restart.
2512 		 */
2513 		if (zfs_dmu_offset_next_sync) {
2514 			rw_exit(&dn->dn_struct_rwlock);
2515 			dnode_rele(dn, FTAG);
2516 
2517 			if (restarted)
2518 				return (SET_ERROR(EBUSY));
2519 
2520 			txg_wait_synced(dmu_objset_pool(os), 0);
2521 			restarted = 1;
2522 			goto restart;
2523 		}
2524 
2525 		err = SET_ERROR(EBUSY);
2526 	} else {
2527 		err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |
2528 		    (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
2529 	}
2530 
2531 	rw_exit(&dn->dn_struct_rwlock);
2532 	dnode_rele(dn, FTAG);
2533 
2534 	return (err);
2535 }
2536 
2537 int
2538 dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2539     blkptr_t *bps, size_t *nbpsp)
2540 {
2541 	dmu_buf_t **dbp, *dbuf;
2542 	dmu_buf_impl_t *db;
2543 	blkptr_t *bp;
2544 	int error, numbufs;
2545 
2546 	error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2547 	    &numbufs, &dbp);
2548 	if (error != 0) {
2549 		if (error == ESRCH) {
2550 			error = SET_ERROR(ENXIO);
2551 		}
2552 		return (error);
2553 	}
2554 
2555 	ASSERT3U(numbufs, <=, *nbpsp);
2556 
2557 	for (int i = 0; i < numbufs; i++) {
2558 		dbuf = dbp[i];
2559 		db = (dmu_buf_impl_t *)dbuf;
2560 
2561 		mutex_enter(&db->db_mtx);
2562 
2563 		if (!list_is_empty(&db->db_dirty_records)) {
2564 			dbuf_dirty_record_t *dr;
2565 
2566 			dr = list_head(&db->db_dirty_records);
2567 			if (dr->dt.dl.dr_brtwrite) {
2568 				/*
2569 				 * This is very special case where we clone a
2570 				 * block and in the same transaction group we
2571 				 * read its BP (most likely to clone the clone).
2572 				 */
2573 				bp = &dr->dt.dl.dr_overridden_by;
2574 			} else {
2575 				/*
2576 				 * The block was modified in the same
2577 				 * transaction group.
2578 				 */
2579 				mutex_exit(&db->db_mtx);
2580 				error = SET_ERROR(EAGAIN);
2581 				goto out;
2582 			}
2583 		} else {
2584 			bp = db->db_blkptr;
2585 		}
2586 
2587 		mutex_exit(&db->db_mtx);
2588 
2589 		if (bp == NULL) {
2590 			/*
2591 			 * The file size was increased, but the block was never
2592 			 * written, otherwise we would either have the block
2593 			 * pointer or the dirty record and would not get here.
2594 			 * It is effectively a hole, so report it as such.
2595 			 */
2596 			BP_ZERO(&bps[i]);
2597 			continue;
2598 		}
2599 		/*
2600 		 * Make sure we clone only data blocks.
2601 		 */
2602 		if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) {
2603 			error = SET_ERROR(EINVAL);
2604 			goto out;
2605 		}
2606 
2607 		/*
2608 		 * If the block was allocated in transaction group that is not
2609 		 * yet synced, we could clone it, but we couldn't write this
2610 		 * operation into ZIL, or it may be impossible to replay, since
2611 		 * the block may appear not yet allocated at that point.
2612 		 */
2613 		if (BP_GET_BIRTH(bp) > spa_freeze_txg(os->os_spa)) {
2614 			error = SET_ERROR(EINVAL);
2615 			goto out;
2616 		}
2617 		if (BP_GET_BIRTH(bp) > spa_last_synced_txg(os->os_spa)) {
2618 			error = SET_ERROR(EAGAIN);
2619 			goto out;
2620 		}
2621 
2622 		bps[i] = *bp;
2623 	}
2624 
2625 	*nbpsp = numbufs;
2626 out:
2627 	dmu_buf_rele_array(dbp, numbufs, FTAG);
2628 
2629 	return (error);
2630 }
2631 
2632 int
2633 dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2634     dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)
2635 {
2636 	spa_t *spa;
2637 	dmu_buf_t **dbp, *dbuf;
2638 	dmu_buf_impl_t *db;
2639 	struct dirty_leaf *dl;
2640 	dbuf_dirty_record_t *dr;
2641 	const blkptr_t *bp;
2642 	int error = 0, i, numbufs;
2643 
2644 	spa = os->os_spa;
2645 
2646 	VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2647 	    &numbufs, &dbp));
2648 	ASSERT3U(nbps, ==, numbufs);
2649 
2650 	/*
2651 	 * Before we start cloning make sure that the dbufs sizes match new BPs
2652 	 * sizes. If they don't, that's a no-go, as we are not able to shrink
2653 	 * dbufs.
2654 	 */
2655 	for (i = 0; i < numbufs; i++) {
2656 		dbuf = dbp[i];
2657 		db = (dmu_buf_impl_t *)dbuf;
2658 		bp = &bps[i];
2659 
2660 		ASSERT0(db->db_level);
2661 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2662 		ASSERT(db->db_blkid != DMU_SPILL_BLKID);
2663 
2664 		if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) {
2665 			error = SET_ERROR(EXDEV);
2666 			goto out;
2667 		}
2668 	}
2669 
2670 	for (i = 0; i < numbufs; i++) {
2671 		dbuf = dbp[i];
2672 		db = (dmu_buf_impl_t *)dbuf;
2673 		bp = &bps[i];
2674 
2675 		ASSERT0(db->db_level);
2676 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2677 		ASSERT(db->db_blkid != DMU_SPILL_BLKID);
2678 		ASSERT(BP_IS_HOLE(bp) || dbuf->db_size == BP_GET_LSIZE(bp));
2679 
2680 		dmu_buf_will_clone_or_dio(dbuf, tx);
2681 
2682 		mutex_enter(&db->db_mtx);
2683 
2684 		dr = list_head(&db->db_dirty_records);
2685 		VERIFY(dr != NULL);
2686 		ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2687 		dl = &dr->dt.dl;
2688 		dl->dr_overridden_by = *bp;
2689 		if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) {
2690 			if (!BP_IS_EMBEDDED(bp)) {
2691 				BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg,
2692 				    BP_GET_BIRTH(bp));
2693 			} else {
2694 				BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by,
2695 				    dr->dr_txg);
2696 			}
2697 		}
2698 		dl->dr_brtwrite = B_TRUE;
2699 		dl->dr_override_state = DR_OVERRIDDEN;
2700 
2701 		mutex_exit(&db->db_mtx);
2702 
2703 		/*
2704 		 * When data in embedded into BP there is no need to create
2705 		 * BRT entry as there is no data block. Just copy the BP as
2706 		 * it contains the data.
2707 		 */
2708 		if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
2709 			brt_pending_add(spa, bp, tx);
2710 		}
2711 	}
2712 out:
2713 	dmu_buf_rele_array(dbp, numbufs, FTAG);
2714 
2715 	return (error);
2716 }
2717 
2718 void
2719 __dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2720 {
2721 	dnode_phys_t *dnp = dn->dn_phys;
2722 
2723 	doi->doi_data_block_size = dn->dn_datablksz;
2724 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
2725 	    1ULL << dn->dn_indblkshift : 0;
2726 	doi->doi_type = dn->dn_type;
2727 	doi->doi_bonus_type = dn->dn_bonustype;
2728 	doi->doi_bonus_size = dn->dn_bonuslen;
2729 	doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT;
2730 	doi->doi_indirection = dn->dn_nlevels;
2731 	doi->doi_checksum = dn->dn_checksum;
2732 	doi->doi_compress = dn->dn_compress;
2733 	doi->doi_nblkptr = dn->dn_nblkptr;
2734 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
2735 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
2736 	doi->doi_fill_count = 0;
2737 	for (int i = 0; i < dnp->dn_nblkptr; i++)
2738 		doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
2739 }
2740 
2741 void
2742 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2743 {
2744 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
2745 	mutex_enter(&dn->dn_mtx);
2746 
2747 	__dmu_object_info_from_dnode(dn, doi);
2748 
2749 	mutex_exit(&dn->dn_mtx);
2750 	rw_exit(&dn->dn_struct_rwlock);
2751 }
2752 
2753 /*
2754  * Get information on a DMU object.
2755  * If doi is NULL, just indicates whether the object exists.
2756  */
2757 int
2758 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
2759 {
2760 	dnode_t *dn;
2761 	int err = dnode_hold(os, object, FTAG, &dn);
2762 
2763 	if (err)
2764 		return (err);
2765 
2766 	if (doi != NULL)
2767 		dmu_object_info_from_dnode(dn, doi);
2768 
2769 	dnode_rele(dn, FTAG);
2770 	return (0);
2771 }
2772 
2773 /*
2774  * As above, but faster; can be used when you have a held dbuf in hand.
2775  */
2776 void
2777 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
2778 {
2779 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2780 
2781 	DB_DNODE_ENTER(db);
2782 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
2783 	DB_DNODE_EXIT(db);
2784 }
2785 
2786 /*
2787  * Faster still when you only care about the size.
2788  */
2789 void
2790 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
2791     u_longlong_t *nblk512)
2792 {
2793 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2794 	dnode_t *dn;
2795 
2796 	DB_DNODE_ENTER(db);
2797 	dn = DB_DNODE(db);
2798 
2799 	*blksize = dn->dn_datablksz;
2800 	/* add in number of slots used for the dnode itself */
2801 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
2802 	    SPA_MINBLOCKSHIFT) + dn->dn_num_slots;
2803 	DB_DNODE_EXIT(db);
2804 }
2805 
2806 void
2807 dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize)
2808 {
2809 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2810 
2811 	DB_DNODE_ENTER(db);
2812 	*dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT;
2813 	DB_DNODE_EXIT(db);
2814 }
2815 
2816 void
2817 byteswap_uint64_array(void *vbuf, size_t size)
2818 {
2819 	uint64_t *buf = vbuf;
2820 	size_t count = size >> 3;
2821 	int i;
2822 
2823 	ASSERT((size & 7) == 0);
2824 
2825 	for (i = 0; i < count; i++)
2826 		buf[i] = BSWAP_64(buf[i]);
2827 }
2828 
2829 void
2830 byteswap_uint32_array(void *vbuf, size_t size)
2831 {
2832 	uint32_t *buf = vbuf;
2833 	size_t count = size >> 2;
2834 	int i;
2835 
2836 	ASSERT((size & 3) == 0);
2837 
2838 	for (i = 0; i < count; i++)
2839 		buf[i] = BSWAP_32(buf[i]);
2840 }
2841 
2842 void
2843 byteswap_uint16_array(void *vbuf, size_t size)
2844 {
2845 	uint16_t *buf = vbuf;
2846 	size_t count = size >> 1;
2847 	int i;
2848 
2849 	ASSERT((size & 1) == 0);
2850 
2851 	for (i = 0; i < count; i++)
2852 		buf[i] = BSWAP_16(buf[i]);
2853 }
2854 
2855 void
2856 byteswap_uint8_array(void *vbuf, size_t size)
2857 {
2858 	(void) vbuf, (void) size;
2859 }
2860 
2861 void
2862 dmu_init(void)
2863 {
2864 	abd_init();
2865 	zfs_dbgmsg_init();
2866 	sa_cache_init();
2867 	dmu_objset_init();
2868 	dnode_init();
2869 	zfetch_init();
2870 	dmu_tx_init();
2871 	l2arc_init();
2872 	arc_init();
2873 	dbuf_init();
2874 }
2875 
2876 void
2877 dmu_fini(void)
2878 {
2879 	arc_fini(); /* arc depends on l2arc, so arc must go first */
2880 	l2arc_fini();
2881 	dmu_tx_fini();
2882 	zfetch_fini();
2883 	dbuf_fini();
2884 	dnode_fini();
2885 	dmu_objset_fini();
2886 	sa_cache_fini();
2887 	zfs_dbgmsg_fini();
2888 	abd_fini();
2889 }
2890 
2891 EXPORT_SYMBOL(dmu_bonus_hold);
2892 EXPORT_SYMBOL(dmu_bonus_hold_by_dnode);
2893 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus);
2894 EXPORT_SYMBOL(dmu_buf_rele_array);
2895 EXPORT_SYMBOL(dmu_prefetch);
2896 EXPORT_SYMBOL(dmu_prefetch_by_dnode);
2897 EXPORT_SYMBOL(dmu_prefetch_dnode);
2898 EXPORT_SYMBOL(dmu_free_range);
2899 EXPORT_SYMBOL(dmu_free_long_range);
2900 EXPORT_SYMBOL(dmu_free_long_object);
2901 EXPORT_SYMBOL(dmu_read);
2902 EXPORT_SYMBOL(dmu_read_by_dnode);
2903 EXPORT_SYMBOL(dmu_read_uio);
2904 EXPORT_SYMBOL(dmu_read_uio_dbuf);
2905 EXPORT_SYMBOL(dmu_read_uio_dnode);
2906 EXPORT_SYMBOL(dmu_write);
2907 EXPORT_SYMBOL(dmu_write_by_dnode);
2908 EXPORT_SYMBOL(dmu_write_by_dnode_flags);
2909 EXPORT_SYMBOL(dmu_write_uio);
2910 EXPORT_SYMBOL(dmu_write_uio_dbuf);
2911 EXPORT_SYMBOL(dmu_write_uio_dnode);
2912 EXPORT_SYMBOL(dmu_prealloc);
2913 EXPORT_SYMBOL(dmu_object_info);
2914 EXPORT_SYMBOL(dmu_object_info_from_dnode);
2915 EXPORT_SYMBOL(dmu_object_info_from_db);
2916 EXPORT_SYMBOL(dmu_object_size_from_db);
2917 EXPORT_SYMBOL(dmu_object_dnsize_from_db);
2918 EXPORT_SYMBOL(dmu_object_set_nlevels);
2919 EXPORT_SYMBOL(dmu_object_set_blocksize);
2920 EXPORT_SYMBOL(dmu_object_set_maxblkid);
2921 EXPORT_SYMBOL(dmu_object_set_checksum);
2922 EXPORT_SYMBOL(dmu_object_set_compress);
2923 EXPORT_SYMBOL(dmu_offset_next);
2924 EXPORT_SYMBOL(dmu_write_policy);
2925 EXPORT_SYMBOL(dmu_sync);
2926 EXPORT_SYMBOL(dmu_request_arcbuf);
2927 EXPORT_SYMBOL(dmu_return_arcbuf);
2928 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode);
2929 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf);
2930 EXPORT_SYMBOL(dmu_buf_hold);
2931 EXPORT_SYMBOL(dmu_ot);
2932 
2933 ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW,
2934 	"Enable NOP writes");
2935 
2936 ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW,
2937 	"Percentage of dirtied blocks from frees in one TXG");
2938 
2939 ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW,
2940 	"Enable forcing txg sync to find holes");
2941 
2942 /* CSTYLED */
2943 ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
2944 	"Limit one prefetch call to this size");
2945 
2946 /* CSTYLED */
2947 ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW,
2948 	"Override copies= for dedup objects");
2949