xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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 			ASSERT(db->db_data != NULL);
1225 			(void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);
1226 
1227 			offset += tocpy;
1228 			size -= tocpy;
1229 			buf = (char *)buf + tocpy;
1230 		}
1231 		dmu_buf_rele_array(dbp, numbufs, FTAG);
1232 	}
1233 	return (err);
1234 }
1235 
1236 int
1237 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1238     void *buf, uint32_t flags)
1239 {
1240 	dnode_t *dn;
1241 	int err;
1242 
1243 	err = dnode_hold(os, object, FTAG, &dn);
1244 	if (err != 0)
1245 		return (err);
1246 
1247 	err = dmu_read_impl(dn, offset, size, buf, flags);
1248 	dnode_rele(dn, FTAG);
1249 	return (err);
1250 }
1251 
1252 int
1253 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,
1254     uint32_t flags)
1255 {
1256 	return (dmu_read_impl(dn, offset, size, buf, flags));
1257 }
1258 
1259 static void
1260 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,
1261     const void *buf, dmu_tx_t *tx)
1262 {
1263 	int i;
1264 
1265 	for (i = 0; i < numbufs; i++) {
1266 		uint64_t tocpy;
1267 		int64_t bufoff;
1268 		dmu_buf_t *db = dbp[i];
1269 
1270 		ASSERT(size > 0);
1271 
1272 		bufoff = offset - db->db_offset;
1273 		tocpy = MIN(db->db_size - bufoff, size);
1274 
1275 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1276 
1277 		if (tocpy == db->db_size)
1278 			dmu_buf_will_fill(db, tx, B_FALSE);
1279 		else
1280 			dmu_buf_will_dirty(db, tx);
1281 
1282 		ASSERT(db->db_data != NULL);
1283 		(void) memcpy((char *)db->db_data + bufoff, buf, tocpy);
1284 
1285 		if (tocpy == db->db_size)
1286 			dmu_buf_fill_done(db, tx, B_FALSE);
1287 
1288 		offset += tocpy;
1289 		size -= tocpy;
1290 		buf = (char *)buf + tocpy;
1291 	}
1292 }
1293 
1294 void
1295 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1296     const void *buf, dmu_tx_t *tx)
1297 {
1298 	dmu_buf_t **dbp;
1299 	int numbufs;
1300 
1301 	if (size == 0)
1302 		return;
1303 
1304 	VERIFY0(dmu_buf_hold_array(os, object, offset, size,
1305 	    FALSE, FTAG, &numbufs, &dbp));
1306 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1307 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1308 }
1309 
1310 /*
1311  * This interface is not used internally by ZFS but is provided for
1312  * use by Lustre which is built on the DMU interfaces.
1313  */
1314 int
1315 dmu_write_by_dnode_flags(dnode_t *dn, uint64_t offset, uint64_t size,
1316     const void *buf, dmu_tx_t *tx, uint32_t flags)
1317 {
1318 	dmu_buf_t **dbp;
1319 	int numbufs;
1320 	int error;
1321 
1322 	if (size == 0)
1323 		return (0);
1324 
1325 	/* Allow Direct I/O when requested and properly aligned */
1326 	if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) &&
1327 	    zfs_dio_aligned(offset, size, dn->dn_datablksz)) {
1328 		abd_t *data = abd_get_from_buf((void *)buf, size);
1329 		error = dmu_write_abd(dn, offset, size, data, DMU_DIRECTIO, tx);
1330 		abd_free(data);
1331 		return (error);
1332 	}
1333 
1334 	VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,
1335 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH));
1336 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1337 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1338 	return (0);
1339 }
1340 
1341 int
1342 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,
1343     const void *buf, dmu_tx_t *tx)
1344 {
1345 	return (dmu_write_by_dnode_flags(dn, offset, size, buf, tx, 0));
1346 }
1347 
1348 void
1349 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1350     dmu_tx_t *tx)
1351 {
1352 	dmu_buf_t **dbp;
1353 	int numbufs, i;
1354 
1355 	if (size == 0)
1356 		return;
1357 
1358 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
1359 	    FALSE, FTAG, &numbufs, &dbp));
1360 
1361 	for (i = 0; i < numbufs; i++) {
1362 		dmu_buf_t *db = dbp[i];
1363 
1364 		dmu_buf_will_not_fill(db, tx);
1365 	}
1366 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1367 }
1368 
1369 void
1370 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
1371     void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
1372     int compressed_size, int byteorder, dmu_tx_t *tx)
1373 {
1374 	dmu_buf_t *db;
1375 
1376 	ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
1377 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
1378 	VERIFY0(dmu_buf_hold_noread(os, object, offset,
1379 	    FTAG, &db));
1380 
1381 	dmu_buf_write_embedded(db,
1382 	    data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
1383 	    uncompressed_size, compressed_size, byteorder, tx);
1384 
1385 	dmu_buf_rele(db, FTAG);
1386 }
1387 
1388 void
1389 dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1390     dmu_tx_t *tx)
1391 {
1392 	int numbufs, i;
1393 	dmu_buf_t **dbp;
1394 
1395 	VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG,
1396 	    &numbufs, &dbp));
1397 	for (i = 0; i < numbufs; i++)
1398 		dmu_buf_redact(dbp[i], tx);
1399 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1400 }
1401 
1402 #ifdef _KERNEL
1403 int
1404 dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size)
1405 {
1406 	dmu_buf_t **dbp;
1407 	int numbufs, i, err;
1408 
1409 	if (uio->uio_extflg & UIO_DIRECT)
1410 		return (dmu_read_uio_direct(dn, uio, size));
1411 
1412 	/*
1413 	 * NB: we could do this block-at-a-time, but it's nice
1414 	 * to be reading in parallel.
1415 	 */
1416 	err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size,
1417 	    TRUE, FTAG, &numbufs, &dbp, 0);
1418 	if (err)
1419 		return (err);
1420 
1421 	for (i = 0; i < numbufs; i++) {
1422 		uint64_t tocpy;
1423 		int64_t bufoff;
1424 		dmu_buf_t *db = dbp[i];
1425 
1426 		ASSERT(size > 0);
1427 
1428 		bufoff = zfs_uio_offset(uio) - db->db_offset;
1429 		tocpy = MIN(db->db_size - bufoff, size);
1430 
1431 		ASSERT(db->db_data != NULL);
1432 		err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,
1433 		    UIO_READ, uio);
1434 
1435 		if (err)
1436 			break;
1437 
1438 		size -= tocpy;
1439 	}
1440 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1441 
1442 	return (err);
1443 }
1444 
1445 /*
1446  * Read 'size' bytes into the uio buffer.
1447  * From object zdb->db_object.
1448  * Starting at zfs_uio_offset(uio).
1449  *
1450  * If the caller already has a dbuf in the target object
1451  * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1452  * because we don't have to find the dnode_t for the object.
1453  */
1454 int
1455 dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size)
1456 {
1457 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1458 	int err;
1459 
1460 	if (size == 0)
1461 		return (0);
1462 
1463 	DB_DNODE_ENTER(db);
1464 	err = dmu_read_uio_dnode(DB_DNODE(db), uio, size);
1465 	DB_DNODE_EXIT(db);
1466 
1467 	return (err);
1468 }
1469 
1470 /*
1471  * Read 'size' bytes into the uio buffer.
1472  * From the specified object
1473  * Starting at offset zfs_uio_offset(uio).
1474  */
1475 int
1476 dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size)
1477 {
1478 	dnode_t *dn;
1479 	int err;
1480 
1481 	if (size == 0)
1482 		return (0);
1483 
1484 	err = dnode_hold(os, object, FTAG, &dn);
1485 	if (err)
1486 		return (err);
1487 
1488 	err = dmu_read_uio_dnode(dn, uio, size);
1489 
1490 	dnode_rele(dn, FTAG);
1491 
1492 	return (err);
1493 }
1494 
1495 int
1496 dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx)
1497 {
1498 	dmu_buf_t **dbp;
1499 	int numbufs;
1500 	int err = 0;
1501 	uint64_t write_size;
1502 
1503 top:
1504 	write_size = size;
1505 
1506 	/*
1507 	 * We only allow Direct I/O writes to happen if we are block
1508 	 * sized aligned. Otherwise, we pass the write off to the ARC.
1509 	 */
1510 	if ((uio->uio_extflg & UIO_DIRECT) &&
1511 	    (write_size >= dn->dn_datablksz)) {
1512 		if (zfs_dio_aligned(zfs_uio_offset(uio), write_size,
1513 		    dn->dn_datablksz)) {
1514 			return (dmu_write_uio_direct(dn, uio, size, tx));
1515 		} else if (write_size > dn->dn_datablksz &&
1516 		    zfs_dio_offset_aligned(zfs_uio_offset(uio),
1517 		    dn->dn_datablksz)) {
1518 			write_size =
1519 			    dn->dn_datablksz * (write_size / dn->dn_datablksz);
1520 			err = dmu_write_uio_direct(dn, uio, write_size, tx);
1521 			if (err == 0) {
1522 				size -= write_size;
1523 				goto top;
1524 			} else {
1525 				return (err);
1526 			}
1527 		} else {
1528 			write_size =
1529 			    P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz);
1530 		}
1531 	}
1532 
1533 	err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size,
1534 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1535 	if (err)
1536 		return (err);
1537 
1538 	for (int i = 0; i < numbufs; i++) {
1539 		uint64_t tocpy;
1540 		int64_t bufoff;
1541 		dmu_buf_t *db = dbp[i];
1542 
1543 		ASSERT(write_size > 0);
1544 
1545 		offset_t off = zfs_uio_offset(uio);
1546 		bufoff = off - db->db_offset;
1547 		tocpy = MIN(db->db_size - bufoff, write_size);
1548 
1549 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1550 
1551 		if (tocpy == db->db_size)
1552 			dmu_buf_will_fill(db, tx, B_TRUE);
1553 		else
1554 			dmu_buf_will_dirty(db, tx);
1555 
1556 		ASSERT(db->db_data != NULL);
1557 		err = zfs_uio_fault_move((char *)db->db_data + bufoff,
1558 		    tocpy, UIO_WRITE, uio);
1559 
1560 		if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {
1561 			/* The fill was reverted.  Undo any uio progress. */
1562 			zfs_uio_advance(uio, off - zfs_uio_offset(uio));
1563 		}
1564 
1565 		if (err)
1566 			break;
1567 
1568 		write_size -= tocpy;
1569 		size -= tocpy;
1570 	}
1571 
1572 	IMPLY(err == 0, write_size == 0);
1573 
1574 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1575 
1576 	if ((uio->uio_extflg & UIO_DIRECT) && size > 0) {
1577 		goto top;
1578 	}
1579 
1580 	return (err);
1581 }
1582 
1583 /*
1584  * Write 'size' bytes from the uio buffer.
1585  * To object zdb->db_object.
1586  * Starting at offset zfs_uio_offset(uio).
1587  *
1588  * If the caller already has a dbuf in the target object
1589  * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1590  * because we don't have to find the dnode_t for the object.
1591  */
1592 int
1593 dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,
1594     dmu_tx_t *tx)
1595 {
1596 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1597 	int err;
1598 
1599 	if (size == 0)
1600 		return (0);
1601 
1602 	DB_DNODE_ENTER(db);
1603 	err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx);
1604 	DB_DNODE_EXIT(db);
1605 
1606 	return (err);
1607 }
1608 
1609 /*
1610  * Write 'size' bytes from the uio buffer.
1611  * To the specified object.
1612  * Starting at offset zfs_uio_offset(uio).
1613  */
1614 int
1615 dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,
1616     dmu_tx_t *tx)
1617 {
1618 	dnode_t *dn;
1619 	int err;
1620 
1621 	if (size == 0)
1622 		return (0);
1623 
1624 	err = dnode_hold(os, object, FTAG, &dn);
1625 	if (err)
1626 		return (err);
1627 
1628 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1629 
1630 	dnode_rele(dn, FTAG);
1631 
1632 	return (err);
1633 }
1634 #endif /* _KERNEL */
1635 
1636 static void
1637 dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps,
1638     uint64_t *l1sz, uint64_t *l2sz)
1639 {
1640 	int cached_flags;
1641 
1642 	if (bps == NULL)
1643 		return;
1644 
1645 	for (size_t blk_off = 0; blk_off < nbps; blk_off++) {
1646 		blkptr_t *bp = &bps[blk_off];
1647 
1648 		if (BP_IS_HOLE(bp))
1649 			continue;
1650 
1651 		cached_flags = arc_cached(spa, bp);
1652 		if (cached_flags == 0)
1653 			continue;
1654 
1655 		if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) ==
1656 		    ARC_CACHED_IN_L2)
1657 			*l2sz += BP_GET_LSIZE(bp);
1658 		else
1659 			*l1sz += BP_GET_LSIZE(bp);
1660 	}
1661 }
1662 
1663 /*
1664  * Estimate DMU object cached size.
1665  */
1666 int
1667 dmu_object_cached_size(objset_t *os, uint64_t object,
1668     uint64_t *l1sz, uint64_t *l2sz)
1669 {
1670 	dnode_t *dn;
1671 	dmu_object_info_t doi;
1672 	int err = 0;
1673 
1674 	*l1sz = *l2sz = 0;
1675 
1676 	if (dnode_hold(os, object, FTAG, &dn) != 0)
1677 		return (0);
1678 
1679 	if (dn->dn_nlevels < 2) {
1680 		dnode_rele(dn, FTAG);
1681 		return (0);
1682 	}
1683 
1684 	dmu_object_info_from_dnode(dn, &doi);
1685 
1686 	for (uint64_t off = 0; off < doi.doi_max_offset;
1687 	    off += dmu_prefetch_max) {
1688 		/* dbuf_read doesn't prefetch L1 blocks. */
1689 		dmu_prefetch_by_dnode(dn, 1, off,
1690 		    dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ);
1691 	}
1692 
1693 	/*
1694 	 * Hold all valid L1 blocks, asking ARC the status of each BP
1695 	 * contained in each such L1 block.
1696 	 */
1697 	uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);
1698 	uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps);
1699 
1700 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1701 	for (uint64_t blk = 0; blk < l1blks; blk++) {
1702 		dmu_buf_impl_t *db = NULL;
1703 
1704 		if (issig()) {
1705 			/*
1706 			 * On interrupt, get out, and bubble up EINTR
1707 			 */
1708 			err = EINTR;
1709 			break;
1710 		}
1711 
1712 		/*
1713 		 * If we get an i/o error here, the L1 can't be read,
1714 		 * and nothing under it could be cached, so we just
1715 		 * continue. Ignoring the error from dbuf_hold_impl
1716 		 * or from dbuf_read is then a reasonable choice.
1717 		 */
1718 		err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db);
1719 		if (err != 0) {
1720 			/*
1721 			 * ignore error and continue
1722 			 */
1723 			err = 0;
1724 			continue;
1725 		}
1726 
1727 		err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1728 		if (err == 0) {
1729 			dmu_cached_bps(dmu_objset_spa(os), db->db.db_data,
1730 			    nbps, l1sz, l2sz);
1731 		}
1732 		/*
1733 		 * error may be ignored, and we continue
1734 		 */
1735 		err = 0;
1736 		dbuf_rele(db, FTAG);
1737 	}
1738 	rw_exit(&dn->dn_struct_rwlock);
1739 
1740 	dnode_rele(dn, FTAG);
1741 	return (err);
1742 }
1743 
1744 /*
1745  * Allocate a loaned anonymous arc buffer.
1746  */
1747 arc_buf_t *
1748 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1749 {
1750 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1751 
1752 	return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));
1753 }
1754 
1755 /*
1756  * Free a loaned arc buffer.
1757  */
1758 void
1759 dmu_return_arcbuf(arc_buf_t *buf)
1760 {
1761 	arc_return_buf(buf, FTAG);
1762 	arc_buf_destroy(buf, FTAG);
1763 }
1764 
1765 /*
1766  * A "lightweight" write is faster than a regular write (e.g.
1767  * dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the
1768  * CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t.  However, the
1769  * data can not be read or overwritten until the transaction's txg has been
1770  * synced.  This makes it appropriate for workloads that are known to be
1771  * (temporarily) write-only, like "zfs receive".
1772  *
1773  * A single block is written, starting at the specified offset in bytes.  If
1774  * the call is successful, it returns 0 and the provided abd has been
1775  * consumed (the caller should not free it).
1776  */
1777 int
1778 dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,
1779     const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx)
1780 {
1781 	dbuf_dirty_record_t *dr =
1782 	    dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx);
1783 	if (dr == NULL)
1784 		return (SET_ERROR(EIO));
1785 	dr->dt.dll.dr_abd = abd;
1786 	dr->dt.dll.dr_props = *zp;
1787 	dr->dt.dll.dr_flags = flags;
1788 	return (0);
1789 }
1790 
1791 /*
1792  * When possible directly assign passed loaned arc buffer to a dbuf.
1793  * If this is not possible copy the contents of passed arc buf via
1794  * dmu_write().
1795  */
1796 int
1797 dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,
1798     dmu_tx_t *tx)
1799 {
1800 	dmu_buf_impl_t *db;
1801 	objset_t *os = dn->dn_objset;
1802 	uint64_t object = dn->dn_object;
1803 	uint32_t blksz = (uint32_t)arc_buf_lsize(buf);
1804 	uint64_t blkid;
1805 
1806 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1807 	blkid = dbuf_whichblock(dn, 0, offset);
1808 	db = dbuf_hold(dn, blkid, FTAG);
1809 	rw_exit(&dn->dn_struct_rwlock);
1810 	if (db == NULL)
1811 		return (SET_ERROR(EIO));
1812 
1813 	/*
1814 	 * We can only assign if the offset is aligned and the arc buf is the
1815 	 * same size as the dbuf.
1816 	 */
1817 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
1818 		zfs_racct_write(os->os_spa, blksz, 1, 0);
1819 		dbuf_assign_arcbuf(db, buf, tx);
1820 		dbuf_rele(db, FTAG);
1821 	} else {
1822 		/* compressed bufs must always be assignable to their dbuf */
1823 		ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);
1824 		ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));
1825 
1826 		dbuf_rele(db, FTAG);
1827 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
1828 		dmu_return_arcbuf(buf);
1829 	}
1830 
1831 	return (0);
1832 }
1833 
1834 int
1835 dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1836     dmu_tx_t *tx)
1837 {
1838 	int err;
1839 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1840 
1841 	DB_DNODE_ENTER(db);
1842 	err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx);
1843 	DB_DNODE_EXIT(db);
1844 
1845 	return (err);
1846 }
1847 
1848 void
1849 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1850 {
1851 	(void) buf;
1852 	dmu_sync_arg_t *dsa = varg;
1853 
1854 	if (zio->io_error == 0) {
1855 		dbuf_dirty_record_t *dr = dsa->dsa_dr;
1856 		blkptr_t *bp = zio->io_bp;
1857 
1858 		if (BP_IS_HOLE(bp)) {
1859 			dmu_buf_t *db = NULL;
1860 			if (dr)
1861 				db = &(dr->dr_dbuf->db);
1862 			else
1863 				db = dsa->dsa_zgd->zgd_db;
1864 			/*
1865 			 * A block of zeros may compress to a hole, but the
1866 			 * block size still needs to be known for replay.
1867 			 */
1868 			BP_SET_LSIZE(bp, db->db_size);
1869 		} else if (!BP_IS_EMBEDDED(bp)) {
1870 			ASSERT(BP_GET_LEVEL(bp) == 0);
1871 			BP_SET_FILL(bp, 1);
1872 		}
1873 	}
1874 }
1875 
1876 static void
1877 dmu_sync_late_arrival_ready(zio_t *zio)
1878 {
1879 	dmu_sync_ready(zio, NULL, zio->io_private);
1880 }
1881 
1882 void
1883 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1884 {
1885 	(void) buf;
1886 	dmu_sync_arg_t *dsa = varg;
1887 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1888 	dmu_buf_impl_t *db = dr->dr_dbuf;
1889 	zgd_t *zgd = dsa->dsa_zgd;
1890 
1891 	/*
1892 	 * Record the vdev(s) backing this blkptr so they can be flushed after
1893 	 * the writes for the lwb have completed.
1894 	 */
1895 	if (zgd && zio->io_error == 0) {
1896 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1897 	}
1898 
1899 	mutex_enter(&db->db_mtx);
1900 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1901 	if (zio->io_error == 0) {
1902 		ASSERT0(dr->dt.dl.dr_has_raw_params);
1903 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
1904 		if (dr->dt.dl.dr_nopwrite) {
1905 			blkptr_t *bp = zio->io_bp;
1906 			blkptr_t *bp_orig = &zio->io_bp_orig;
1907 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
1908 
1909 			ASSERT(BP_EQUAL(bp, bp_orig));
1910 			VERIFY(BP_EQUAL(bp, db->db_blkptr));
1911 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
1912 			VERIFY(zio_checksum_table[chksum].ci_flags &
1913 			    ZCHECKSUM_FLAG_NOPWRITE);
1914 		}
1915 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1916 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1917 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1918 
1919 		/*
1920 		 * Old style holes are filled with all zeros, whereas
1921 		 * new-style holes maintain their lsize, type, level,
1922 		 * and birth time (see zio_write_compress). While we
1923 		 * need to reset the BP_SET_LSIZE() call that happened
1924 		 * in dmu_sync_ready for old style holes, we do *not*
1925 		 * want to wipe out the information contained in new
1926 		 * style holes. Thus, only zero out the block pointer if
1927 		 * it's an old style hole.
1928 		 */
1929 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
1930 		    BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0)
1931 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1932 	} else {
1933 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1934 	}
1935 
1936 	cv_broadcast(&db->db_changed);
1937 	mutex_exit(&db->db_mtx);
1938 
1939 	if (dsa->dsa_done)
1940 		dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1941 
1942 	kmem_free(dsa, sizeof (*dsa));
1943 }
1944 
1945 static void
1946 dmu_sync_late_arrival_done(zio_t *zio)
1947 {
1948 	blkptr_t *bp = zio->io_bp;
1949 	dmu_sync_arg_t *dsa = zio->io_private;
1950 	zgd_t *zgd = dsa->dsa_zgd;
1951 
1952 	if (zio->io_error == 0) {
1953 		/*
1954 		 * Record the vdev(s) backing this blkptr so they can be
1955 		 * flushed after the writes for the lwb have completed.
1956 		 */
1957 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1958 
1959 		if (!BP_IS_HOLE(bp)) {
1960 			blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig;
1961 			ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));
1962 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1963 			ASSERT(BP_GET_LOGICAL_BIRTH(zio->io_bp) == zio->io_txg);
1964 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1965 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1966 		}
1967 	}
1968 
1969 	dmu_tx_commit(dsa->dsa_tx);
1970 
1971 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1972 
1973 	abd_free(zio->io_abd);
1974 	kmem_free(dsa, sizeof (*dsa));
1975 }
1976 
1977 static int
1978 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1979     zio_prop_t *zp, zbookmark_phys_t *zb)
1980 {
1981 	dmu_sync_arg_t *dsa;
1982 	dmu_tx_t *tx;
1983 	int error;
1984 
1985 	error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL,
1986 	    DB_RF_CANFAIL | DB_RF_NOPREFETCH);
1987 	if (error != 0)
1988 		return (error);
1989 
1990 	tx = dmu_tx_create(os);
1991 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
1992 	/*
1993 	 * This transaction does not produce any dirty data or log blocks, so
1994 	 * it should not be throttled.  All other cases wait for TXG sync, by
1995 	 * which time the log block we are writing will be obsolete, so we can
1996 	 * skip waiting and just return error here instead.
1997 	 */
1998 	if (dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE) != 0) {
1999 		dmu_tx_abort(tx);
2000 		/* Make zl_get_data do txg_waited_synced() */
2001 		return (SET_ERROR(EIO));
2002 	}
2003 
2004 	/*
2005 	 * In order to prevent the zgd's lwb from being free'd prior to
2006 	 * dmu_sync_late_arrival_done() being called, we have to ensure
2007 	 * the lwb's "max txg" takes this tx's txg into account.
2008 	 */
2009 	zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));
2010 
2011 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2012 	dsa->dsa_dr = NULL;
2013 	dsa->dsa_done = done;
2014 	dsa->dsa_zgd = zgd;
2015 	dsa->dsa_tx = tx;
2016 
2017 	/*
2018 	 * Since we are currently syncing this txg, it's nontrivial to
2019 	 * determine what BP to nopwrite against, so we disable nopwrite.
2020 	 *
2021 	 * When syncing, the db_blkptr is initially the BP of the previous
2022 	 * txg.  We can not nopwrite against it because it will be changed
2023 	 * (this is similar to the non-late-arrival case where the dbuf is
2024 	 * dirty in a future txg).
2025 	 *
2026 	 * Then dbuf_write_ready() sets bp_blkptr to the location we will write.
2027 	 * We can not nopwrite against it because although the BP will not
2028 	 * (typically) be changed, the data has not yet been persisted to this
2029 	 * location.
2030 	 *
2031 	 * Finally, when dbuf_write_done() is called, it is theoretically
2032 	 * possible to always nopwrite, because the data that was written in
2033 	 * this txg is the same data that we are trying to write.  However we
2034 	 * would need to check that this dbuf is not dirty in any future
2035 	 * txg's (as we do in the normal dmu_sync() path). For simplicity, we
2036 	 * don't nopwrite in this case.
2037 	 */
2038 	zp->zp_nopwrite = B_FALSE;
2039 
2040 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
2041 	    abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),
2042 	    zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,
2043 	    dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,
2044 	    dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
2045 
2046 	return (0);
2047 }
2048 
2049 /*
2050  * Intent log support: sync the block associated with db to disk.
2051  * N.B. and XXX: the caller is responsible for making sure that the
2052  * data isn't changing while dmu_sync() is writing it.
2053  *
2054  * Return values:
2055  *
2056  *	EEXIST: this txg has already been synced, so there's nothing to do.
2057  *		The caller should not log the write.
2058  *
2059  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
2060  *		The caller should not log the write.
2061  *
2062  *	EALREADY: this block is already in the process of being synced.
2063  *		The caller should track its progress (somehow).
2064  *
2065  *	EIO: could not do the I/O.
2066  *		The caller should do a txg_wait_synced().
2067  *
2068  *	0: the I/O has been initiated.
2069  *		The caller should log this blkptr in the done callback.
2070  *		It is possible that the I/O will fail, in which case
2071  *		the error will be reported to the done callback and
2072  *		propagated to pio from zio_done().
2073  */
2074 int
2075 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
2076 {
2077 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
2078 	objset_t *os = db->db_objset;
2079 	dsl_dataset_t *ds = os->os_dsl_dataset;
2080 	dbuf_dirty_record_t *dr, *dr_next;
2081 	dmu_sync_arg_t *dsa;
2082 	zbookmark_phys_t zb;
2083 	zio_prop_t zp;
2084 
2085 	ASSERT(pio != NULL);
2086 	ASSERT(txg != 0);
2087 
2088 	SET_BOOKMARK(&zb, ds->ds_object,
2089 	    db->db.db_object, db->db_level, db->db_blkid);
2090 
2091 	DB_DNODE_ENTER(db);
2092 	dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp);
2093 	DB_DNODE_EXIT(db);
2094 
2095 	/*
2096 	 * If we're frozen (running ziltest), we always need to generate a bp.
2097 	 */
2098 	if (txg > spa_freeze_txg(os->os_spa))
2099 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2100 
2101 	/*
2102 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
2103 	 * and us.  If we determine that this txg is not yet syncing,
2104 	 * but it begins to sync a moment later, that's OK because the
2105 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
2106 	 */
2107 	mutex_enter(&db->db_mtx);
2108 
2109 	if (txg <= spa_last_synced_txg(os->os_spa)) {
2110 		/*
2111 		 * This txg has already synced.  There's nothing to do.
2112 		 */
2113 		mutex_exit(&db->db_mtx);
2114 		return (SET_ERROR(EEXIST));
2115 	}
2116 
2117 	if (txg <= spa_syncing_txg(os->os_spa)) {
2118 		/*
2119 		 * This txg is currently syncing, so we can't mess with
2120 		 * the dirty record anymore; just write a new log block.
2121 		 */
2122 		mutex_exit(&db->db_mtx);
2123 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2124 	}
2125 
2126 	dr = dbuf_find_dirty_eq(db, txg);
2127 
2128 	if (dr == NULL) {
2129 		/*
2130 		 * There's no dr for this dbuf, so it must have been freed.
2131 		 * There's no need to log writes to freed blocks, so we're done.
2132 		 */
2133 		mutex_exit(&db->db_mtx);
2134 		return (SET_ERROR(ENOENT));
2135 	}
2136 
2137 	dr_next = list_next(&db->db_dirty_records, dr);
2138 	ASSERT(dr_next == NULL || dr_next->dr_txg < txg);
2139 
2140 	if (db->db_blkptr != NULL) {
2141 		/*
2142 		 * We need to fill in zgd_bp with the current blkptr so that
2143 		 * the nopwrite code can check if we're writing the same
2144 		 * data that's already on disk.  We can only nopwrite if we
2145 		 * are sure that after making the copy, db_blkptr will not
2146 		 * change until our i/o completes.  We ensure this by
2147 		 * holding the db_mtx, and only allowing nopwrite if the
2148 		 * block is not already dirty (see below).  This is verified
2149 		 * by dmu_sync_done(), which VERIFYs that the db_blkptr has
2150 		 * not changed.
2151 		 */
2152 		*zgd->zgd_bp = *db->db_blkptr;
2153 	}
2154 
2155 	/*
2156 	 * Assume the on-disk data is X, the current syncing data (in
2157 	 * txg - 1) is Y, and the current in-memory data is Z (currently
2158 	 * in dmu_sync).
2159 	 *
2160 	 * We usually want to perform a nopwrite if X and Z are the
2161 	 * same.  However, if Y is different (i.e. the BP is going to
2162 	 * change before this write takes effect), then a nopwrite will
2163 	 * be incorrect - we would override with X, which could have
2164 	 * been freed when Y was written.
2165 	 *
2166 	 * (Note that this is not a concern when we are nop-writing from
2167 	 * syncing context, because X and Y must be identical, because
2168 	 * all previous txgs have been synced.)
2169 	 *
2170 	 * Therefore, we disable nopwrite if the current BP could change
2171 	 * before this TXG.  There are two ways it could change: by
2172 	 * being dirty (dr_next is non-NULL), or by being freed
2173 	 * (dnode_block_freed()).  This behavior is verified by
2174 	 * zio_done(), which VERIFYs that the override BP is identical
2175 	 * to the on-disk BP.
2176 	 */
2177 	if (dr_next != NULL) {
2178 		zp.zp_nopwrite = B_FALSE;
2179 	} else {
2180 		DB_DNODE_ENTER(db);
2181 		if (dnode_block_freed(DB_DNODE(db), db->db_blkid))
2182 			zp.zp_nopwrite = B_FALSE;
2183 		DB_DNODE_EXIT(db);
2184 	}
2185 
2186 	ASSERT(dr->dr_txg == txg);
2187 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
2188 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2189 		/*
2190 		 * We have already issued a sync write for this buffer,
2191 		 * or this buffer has already been synced.  It could not
2192 		 * have been dirtied since, or we would have cleared the state.
2193 		 */
2194 		mutex_exit(&db->db_mtx);
2195 		return (SET_ERROR(EALREADY));
2196 	}
2197 
2198 	ASSERT0(dr->dt.dl.dr_has_raw_params);
2199 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2200 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
2201 	mutex_exit(&db->db_mtx);
2202 
2203 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2204 	dsa->dsa_dr = dr;
2205 	dsa->dsa_done = done;
2206 	dsa->dsa_zgd = zgd;
2207 	dsa->dsa_tx = NULL;
2208 
2209 	zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp,
2210 	    dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db),
2211 	    dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL,
2212 	    dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL,
2213 	    &zb));
2214 
2215 	return (0);
2216 }
2217 
2218 int
2219 dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx)
2220 {
2221 	dnode_t *dn;
2222 	int err;
2223 
2224 	err = dnode_hold(os, object, FTAG, &dn);
2225 	if (err)
2226 		return (err);
2227 	err = dnode_set_nlevels(dn, nlevels, tx);
2228 	dnode_rele(dn, FTAG);
2229 	return (err);
2230 }
2231 
2232 int
2233 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
2234     dmu_tx_t *tx)
2235 {
2236 	dnode_t *dn;
2237 	int err;
2238 
2239 	err = dnode_hold(os, object, FTAG, &dn);
2240 	if (err)
2241 		return (err);
2242 	err = dnode_set_blksz(dn, size, ibs, tx);
2243 	dnode_rele(dn, FTAG);
2244 	return (err);
2245 }
2246 
2247 int
2248 dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid,
2249     dmu_tx_t *tx)
2250 {
2251 	dnode_t *dn;
2252 	int err;
2253 
2254 	err = dnode_hold(os, object, FTAG, &dn);
2255 	if (err)
2256 		return (err);
2257 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2258 	dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE);
2259 	rw_exit(&dn->dn_struct_rwlock);
2260 	dnode_rele(dn, FTAG);
2261 	return (0);
2262 }
2263 
2264 void
2265 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
2266     dmu_tx_t *tx)
2267 {
2268 	dnode_t *dn;
2269 
2270 	/*
2271 	 * Send streams include each object's checksum function.  This
2272 	 * check ensures that the receiving system can understand the
2273 	 * checksum function transmitted.
2274 	 */
2275 	ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
2276 
2277 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2278 	ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
2279 	dn->dn_checksum = checksum;
2280 	dnode_setdirty(dn, tx);
2281 	dnode_rele(dn, FTAG);
2282 }
2283 
2284 void
2285 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
2286     dmu_tx_t *tx)
2287 {
2288 	dnode_t *dn;
2289 
2290 	/*
2291 	 * Send streams include each object's compression function.  This
2292 	 * check ensures that the receiving system can understand the
2293 	 * compression function transmitted.
2294 	 */
2295 	ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
2296 
2297 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2298 	dn->dn_compress = compress;
2299 	dnode_setdirty(dn, tx);
2300 	dnode_rele(dn, FTAG);
2301 }
2302 
2303 /*
2304  * When the "redundant_metadata" property is set to "most", only indirect
2305  * blocks of this level and higher will have an additional ditto block.
2306  */
2307 static const int zfs_redundant_metadata_most_ditto_level = 2;
2308 
2309 void
2310 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
2311 {
2312 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
2313 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
2314 	    (wp & WP_SPILL));
2315 	enum zio_checksum checksum = os->os_checksum;
2316 	enum zio_compress compress = os->os_compress;
2317 	uint8_t complevel = os->os_complevel;
2318 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
2319 	boolean_t dedup = B_FALSE;
2320 	boolean_t nopwrite = B_FALSE;
2321 	boolean_t dedup_verify = os->os_dedup_verify;
2322 	boolean_t encrypt = B_FALSE;
2323 	int copies = os->os_copies;
2324 
2325 	/*
2326 	 * We maintain different write policies for each of the following
2327 	 * types of data:
2328 	 *	 1. metadata
2329 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
2330 	 *	 3. all other level 0 blocks
2331 	 */
2332 	if (ismd) {
2333 		/*
2334 		 * XXX -- we should design a compression algorithm
2335 		 * that specializes in arrays of bps.
2336 		 */
2337 		compress = zio_compress_select(os->os_spa,
2338 		    ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
2339 
2340 		/*
2341 		 * Metadata always gets checksummed.  If the data
2342 		 * checksum is multi-bit correctable, and it's not a
2343 		 * ZBT-style checksum, then it's suitable for metadata
2344 		 * as well.  Otherwise, the metadata checksum defaults
2345 		 * to fletcher4.
2346 		 */
2347 		if (!(zio_checksum_table[checksum].ci_flags &
2348 		    ZCHECKSUM_FLAG_METADATA) ||
2349 		    (zio_checksum_table[checksum].ci_flags &
2350 		    ZCHECKSUM_FLAG_EMBEDDED))
2351 			checksum = ZIO_CHECKSUM_FLETCHER_4;
2352 
2353 		switch (os->os_redundant_metadata) {
2354 		case ZFS_REDUNDANT_METADATA_ALL:
2355 			copies++;
2356 			break;
2357 		case ZFS_REDUNDANT_METADATA_MOST:
2358 			if (level >= zfs_redundant_metadata_most_ditto_level ||
2359 			    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))
2360 				copies++;
2361 			break;
2362 		case ZFS_REDUNDANT_METADATA_SOME:
2363 			if (DMU_OT_IS_CRITICAL(type))
2364 				copies++;
2365 			break;
2366 		case ZFS_REDUNDANT_METADATA_NONE:
2367 			break;
2368 		}
2369 
2370 		if (dmu_ddt_copies > 0) {
2371 			/*
2372 			 * If this tuneable is set, and this is a write for a
2373 			 * dedup entry store (zap or log), then we treat it
2374 			 * something like ZFS_REDUNDANT_METADATA_MOST on a
2375 			 * regular dataset: this many copies, and one more for
2376 			 * "higher" indirect blocks. This specific exception is
2377 			 * necessary because dedup objects are stored in the
2378 			 * MOS, which always has the highest possible copies.
2379 			 */
2380 			dmu_object_type_t stype =
2381 			    dn ? dn->dn_storage_type : DMU_OT_NONE;
2382 			if (stype == DMU_OT_NONE)
2383 				stype = type;
2384 			if (stype == DMU_OT_DDT_ZAP) {
2385 				copies = dmu_ddt_copies;
2386 				if (level >=
2387 				    zfs_redundant_metadata_most_ditto_level)
2388 					copies++;
2389 			}
2390 		}
2391 	} else if (wp & WP_NOFILL) {
2392 		ASSERT(level == 0);
2393 
2394 		/*
2395 		 * If we're writing preallocated blocks, we aren't actually
2396 		 * writing them so don't set any policy properties.  These
2397 		 * blocks are currently only used by an external subsystem
2398 		 * outside of zfs (i.e. dump) and not written by the zio
2399 		 * pipeline.
2400 		 */
2401 		compress = ZIO_COMPRESS_OFF;
2402 		checksum = ZIO_CHECKSUM_OFF;
2403 	} else {
2404 		compress = zio_compress_select(os->os_spa, dn->dn_compress,
2405 		    compress);
2406 		complevel = zio_complevel_select(os->os_spa, compress,
2407 		    complevel, complevel);
2408 
2409 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
2410 		    zio_checksum_select(dn->dn_checksum, checksum) :
2411 		    dedup_checksum;
2412 
2413 		/*
2414 		 * Determine dedup setting.  If we are in dmu_sync(),
2415 		 * we won't actually dedup now because that's all
2416 		 * done in syncing context; but we do want to use the
2417 		 * dedup checksum.  If the checksum is not strong
2418 		 * enough to ensure unique signatures, force
2419 		 * dedup_verify.
2420 		 */
2421 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
2422 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
2423 			if (!(zio_checksum_table[checksum].ci_flags &
2424 			    ZCHECKSUM_FLAG_DEDUP))
2425 				dedup_verify = B_TRUE;
2426 		}
2427 
2428 		/*
2429 		 * Enable nopwrite if we have secure enough checksum
2430 		 * algorithm (see comment in zio_nop_write) and
2431 		 * compression is enabled.  We don't enable nopwrite if
2432 		 * dedup is enabled as the two features are mutually
2433 		 * exclusive.
2434 		 */
2435 		nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
2436 		    ZCHECKSUM_FLAG_NOPWRITE) &&
2437 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
2438 	}
2439 
2440 	/*
2441 	 * All objects in an encrypted objset are protected from modification
2442 	 * via a MAC. Encrypted objects store their IV and salt in the last DVA
2443 	 * in the bp, so we cannot use all copies. Encrypted objects are also
2444 	 * not subject to nopwrite since writing the same data will still
2445 	 * result in a new ciphertext. Only encrypted blocks can be dedup'd
2446 	 * to avoid ambiguity in the dedup code since the DDT does not store
2447 	 * object types.
2448 	 */
2449 	if (os->os_encrypted && (wp & WP_NOFILL) == 0) {
2450 		encrypt = B_TRUE;
2451 
2452 		if (DMU_OT_IS_ENCRYPTED(type)) {
2453 			copies = MIN(copies, SPA_DVAS_PER_BP - 1);
2454 			nopwrite = B_FALSE;
2455 		} else {
2456 			dedup = B_FALSE;
2457 		}
2458 
2459 		if (level <= 0 &&
2460 		    (type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) {
2461 			compress = ZIO_COMPRESS_EMPTY;
2462 		}
2463 	}
2464 
2465 	zp->zp_compress = compress;
2466 	zp->zp_complevel = complevel;
2467 	zp->zp_checksum = checksum;
2468 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
2469 	zp->zp_level = level;
2470 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
2471 	zp->zp_dedup = dedup;
2472 	zp->zp_dedup_verify = dedup && dedup_verify;
2473 	zp->zp_nopwrite = nopwrite;
2474 	zp->zp_encrypt = encrypt;
2475 	zp->zp_byteorder = ZFS_HOST_BYTEORDER;
2476 	zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE;
2477 	memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN);
2478 	memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN);
2479 	memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN);
2480 	zp->zp_zpl_smallblk = DMU_OT_IS_FILE(zp->zp_type) ?
2481 	    os->os_zpl_special_smallblock : 0;
2482 	zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE;
2483 
2484 	ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);
2485 }
2486 
2487 /*
2488  * Reports the location of data and holes in an object.  In order to
2489  * accurately report holes all dirty data must be synced to disk.  This
2490  * causes extremely poor performance when seeking for holes in a dirty file.
2491  * As a compromise, only provide hole data when the dnode is clean.  When
2492  * a dnode is dirty report the dnode as having no holes by returning EBUSY
2493  * which is always safe to do.
2494  */
2495 int
2496 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
2497 {
2498 	dnode_t *dn;
2499 	int restarted = 0, err;
2500 
2501 restart:
2502 	err = dnode_hold(os, object, FTAG, &dn);
2503 	if (err)
2504 		return (err);
2505 
2506 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
2507 
2508 	if (dnode_is_dirty(dn)) {
2509 		/*
2510 		 * If the zfs_dmu_offset_next_sync module option is enabled
2511 		 * then hole reporting has been requested.  Dirty dnodes
2512 		 * must be synced to disk to accurately report holes.
2513 		 *
2514 		 * Provided a RL_READER rangelock spanning 0-UINT64_MAX is
2515 		 * held by the caller only a single restart will be required.
2516 		 * We tolerate callers which do not hold the rangelock by
2517 		 * returning EBUSY and not reporting holes after one restart.
2518 		 */
2519 		if (zfs_dmu_offset_next_sync) {
2520 			rw_exit(&dn->dn_struct_rwlock);
2521 			dnode_rele(dn, FTAG);
2522 
2523 			if (restarted)
2524 				return (SET_ERROR(EBUSY));
2525 
2526 			txg_wait_synced(dmu_objset_pool(os), 0);
2527 			restarted = 1;
2528 			goto restart;
2529 		}
2530 
2531 		err = SET_ERROR(EBUSY);
2532 	} else {
2533 		err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |
2534 		    (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
2535 	}
2536 
2537 	rw_exit(&dn->dn_struct_rwlock);
2538 	dnode_rele(dn, FTAG);
2539 
2540 	return (err);
2541 }
2542 
2543 int
2544 dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2545     blkptr_t *bps, size_t *nbpsp)
2546 {
2547 	dmu_buf_t **dbp, *dbuf;
2548 	dmu_buf_impl_t *db;
2549 	blkptr_t *bp;
2550 	int error, numbufs;
2551 
2552 	error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2553 	    &numbufs, &dbp);
2554 	if (error != 0) {
2555 		if (error == ESRCH) {
2556 			error = SET_ERROR(ENXIO);
2557 		}
2558 		return (error);
2559 	}
2560 
2561 	ASSERT3U(numbufs, <=, *nbpsp);
2562 
2563 	for (int i = 0; i < numbufs; i++) {
2564 		dbuf = dbp[i];
2565 		db = (dmu_buf_impl_t *)dbuf;
2566 
2567 		mutex_enter(&db->db_mtx);
2568 
2569 		if (!list_is_empty(&db->db_dirty_records)) {
2570 			dbuf_dirty_record_t *dr;
2571 
2572 			dr = list_head(&db->db_dirty_records);
2573 			if (dr->dt.dl.dr_brtwrite) {
2574 				/*
2575 				 * This is very special case where we clone a
2576 				 * block and in the same transaction group we
2577 				 * read its BP (most likely to clone the clone).
2578 				 */
2579 				bp = &dr->dt.dl.dr_overridden_by;
2580 			} else {
2581 				/*
2582 				 * The block was modified in the same
2583 				 * transaction group.
2584 				 */
2585 				mutex_exit(&db->db_mtx);
2586 				error = SET_ERROR(EAGAIN);
2587 				goto out;
2588 			}
2589 		} else {
2590 			bp = db->db_blkptr;
2591 		}
2592 
2593 		mutex_exit(&db->db_mtx);
2594 
2595 		if (bp == NULL) {
2596 			/*
2597 			 * The file size was increased, but the block was never
2598 			 * written, otherwise we would either have the block
2599 			 * pointer or the dirty record and would not get here.
2600 			 * It is effectively a hole, so report it as such.
2601 			 */
2602 			BP_ZERO(&bps[i]);
2603 			continue;
2604 		}
2605 		/*
2606 		 * Make sure we clone only data blocks.
2607 		 */
2608 		if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) {
2609 			error = SET_ERROR(EINVAL);
2610 			goto out;
2611 		}
2612 
2613 		/*
2614 		 * If the block was allocated in transaction group that is not
2615 		 * yet synced, we could clone it, but we couldn't write this
2616 		 * operation into ZIL, or it may be impossible to replay, since
2617 		 * the block may appear not yet allocated at that point.
2618 		 */
2619 		if (BP_GET_BIRTH(bp) > spa_freeze_txg(os->os_spa)) {
2620 			error = SET_ERROR(EINVAL);
2621 			goto out;
2622 		}
2623 		if (BP_GET_BIRTH(bp) > spa_last_synced_txg(os->os_spa)) {
2624 			error = SET_ERROR(EAGAIN);
2625 			goto out;
2626 		}
2627 
2628 		bps[i] = *bp;
2629 	}
2630 
2631 	*nbpsp = numbufs;
2632 out:
2633 	dmu_buf_rele_array(dbp, numbufs, FTAG);
2634 
2635 	return (error);
2636 }
2637 
2638 int
2639 dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2640     dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)
2641 {
2642 	spa_t *spa;
2643 	dmu_buf_t **dbp, *dbuf;
2644 	dmu_buf_impl_t *db;
2645 	struct dirty_leaf *dl;
2646 	dbuf_dirty_record_t *dr;
2647 	const blkptr_t *bp;
2648 	int error = 0, i, numbufs;
2649 
2650 	spa = os->os_spa;
2651 
2652 	VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2653 	    &numbufs, &dbp));
2654 	ASSERT3U(nbps, ==, numbufs);
2655 
2656 	/*
2657 	 * Before we start cloning make sure that the dbufs sizes match new BPs
2658 	 * sizes. If they don't, that's a no-go, as we are not able to shrink
2659 	 * dbufs.
2660 	 */
2661 	for (i = 0; i < numbufs; i++) {
2662 		dbuf = dbp[i];
2663 		db = (dmu_buf_impl_t *)dbuf;
2664 		bp = &bps[i];
2665 
2666 		ASSERT3U(db->db.db_object, !=, DMU_META_DNODE_OBJECT);
2667 		ASSERT0(db->db_level);
2668 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2669 		ASSERT(db->db_blkid != DMU_SPILL_BLKID);
2670 
2671 		if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) {
2672 			error = SET_ERROR(EXDEV);
2673 			goto out;
2674 		}
2675 	}
2676 
2677 	for (i = 0; i < numbufs; i++) {
2678 		dbuf = dbp[i];
2679 		db = (dmu_buf_impl_t *)dbuf;
2680 		bp = &bps[i];
2681 
2682 		dmu_buf_will_clone_or_dio(dbuf, tx);
2683 
2684 		mutex_enter(&db->db_mtx);
2685 
2686 		dr = list_head(&db->db_dirty_records);
2687 		VERIFY(dr != NULL);
2688 		ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2689 		dl = &dr->dt.dl;
2690 		ASSERT0(dl->dr_has_raw_params);
2691 		dl->dr_overridden_by = *bp;
2692 		if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) {
2693 			if (!BP_IS_EMBEDDED(bp)) {
2694 				BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg,
2695 				    BP_GET_BIRTH(bp));
2696 			} else {
2697 				BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by,
2698 				    dr->dr_txg);
2699 			}
2700 		}
2701 		dl->dr_brtwrite = B_TRUE;
2702 		dl->dr_override_state = DR_OVERRIDDEN;
2703 
2704 		mutex_exit(&db->db_mtx);
2705 
2706 		/*
2707 		 * When data in embedded into BP there is no need to create
2708 		 * BRT entry as there is no data block. Just copy the BP as
2709 		 * it contains the data.
2710 		 */
2711 		if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
2712 			brt_pending_add(spa, bp, tx);
2713 		}
2714 	}
2715 out:
2716 	dmu_buf_rele_array(dbp, numbufs, FTAG);
2717 
2718 	return (error);
2719 }
2720 
2721 void
2722 __dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2723 {
2724 	dnode_phys_t *dnp = dn->dn_phys;
2725 
2726 	doi->doi_data_block_size = dn->dn_datablksz;
2727 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
2728 	    1ULL << dn->dn_indblkshift : 0;
2729 	doi->doi_type = dn->dn_type;
2730 	doi->doi_bonus_type = dn->dn_bonustype;
2731 	doi->doi_bonus_size = dn->dn_bonuslen;
2732 	doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT;
2733 	doi->doi_indirection = dn->dn_nlevels;
2734 	doi->doi_checksum = dn->dn_checksum;
2735 	doi->doi_compress = dn->dn_compress;
2736 	doi->doi_nblkptr = dn->dn_nblkptr;
2737 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
2738 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
2739 	doi->doi_fill_count = 0;
2740 	for (int i = 0; i < dnp->dn_nblkptr; i++)
2741 		doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
2742 }
2743 
2744 void
2745 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2746 {
2747 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
2748 	mutex_enter(&dn->dn_mtx);
2749 
2750 	__dmu_object_info_from_dnode(dn, doi);
2751 
2752 	mutex_exit(&dn->dn_mtx);
2753 	rw_exit(&dn->dn_struct_rwlock);
2754 }
2755 
2756 /*
2757  * Get information on a DMU object.
2758  * If doi is NULL, just indicates whether the object exists.
2759  */
2760 int
2761 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
2762 {
2763 	dnode_t *dn;
2764 	int err = dnode_hold(os, object, FTAG, &dn);
2765 
2766 	if (err)
2767 		return (err);
2768 
2769 	if (doi != NULL)
2770 		dmu_object_info_from_dnode(dn, doi);
2771 
2772 	dnode_rele(dn, FTAG);
2773 	return (0);
2774 }
2775 
2776 /*
2777  * As above, but faster; can be used when you have a held dbuf in hand.
2778  */
2779 void
2780 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
2781 {
2782 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2783 
2784 	DB_DNODE_ENTER(db);
2785 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
2786 	DB_DNODE_EXIT(db);
2787 }
2788 
2789 /*
2790  * Faster still when you only care about the size.
2791  */
2792 void
2793 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
2794     u_longlong_t *nblk512)
2795 {
2796 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2797 	dnode_t *dn;
2798 
2799 	DB_DNODE_ENTER(db);
2800 	dn = DB_DNODE(db);
2801 
2802 	*blksize = dn->dn_datablksz;
2803 	/* add in number of slots used for the dnode itself */
2804 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
2805 	    SPA_MINBLOCKSHIFT) + dn->dn_num_slots;
2806 	DB_DNODE_EXIT(db);
2807 }
2808 
2809 void
2810 dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize)
2811 {
2812 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2813 
2814 	DB_DNODE_ENTER(db);
2815 	*dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT;
2816 	DB_DNODE_EXIT(db);
2817 }
2818 
2819 void
2820 byteswap_uint64_array(void *vbuf, size_t size)
2821 {
2822 	uint64_t *buf = vbuf;
2823 	size_t count = size >> 3;
2824 	int i;
2825 
2826 	ASSERT((size & 7) == 0);
2827 
2828 	for (i = 0; i < count; i++)
2829 		buf[i] = BSWAP_64(buf[i]);
2830 }
2831 
2832 void
2833 byteswap_uint32_array(void *vbuf, size_t size)
2834 {
2835 	uint32_t *buf = vbuf;
2836 	size_t count = size >> 2;
2837 	int i;
2838 
2839 	ASSERT((size & 3) == 0);
2840 
2841 	for (i = 0; i < count; i++)
2842 		buf[i] = BSWAP_32(buf[i]);
2843 }
2844 
2845 void
2846 byteswap_uint16_array(void *vbuf, size_t size)
2847 {
2848 	uint16_t *buf = vbuf;
2849 	size_t count = size >> 1;
2850 	int i;
2851 
2852 	ASSERT((size & 1) == 0);
2853 
2854 	for (i = 0; i < count; i++)
2855 		buf[i] = BSWAP_16(buf[i]);
2856 }
2857 
2858 void
2859 byteswap_uint8_array(void *vbuf, size_t size)
2860 {
2861 	(void) vbuf, (void) size;
2862 }
2863 
2864 void
2865 dmu_init(void)
2866 {
2867 	abd_init();
2868 	zfs_dbgmsg_init();
2869 	sa_cache_init();
2870 	dmu_objset_init();
2871 	dnode_init();
2872 	zfetch_init();
2873 	dmu_tx_init();
2874 	l2arc_init();
2875 	arc_init();
2876 	dbuf_init();
2877 }
2878 
2879 void
2880 dmu_fini(void)
2881 {
2882 	arc_fini(); /* arc depends on l2arc, so arc must go first */
2883 	l2arc_fini();
2884 	dmu_tx_fini();
2885 	zfetch_fini();
2886 	dbuf_fini();
2887 	dnode_fini();
2888 	dmu_objset_fini();
2889 	sa_cache_fini();
2890 	zfs_dbgmsg_fini();
2891 	abd_fini();
2892 }
2893 
2894 EXPORT_SYMBOL(dmu_bonus_hold);
2895 EXPORT_SYMBOL(dmu_bonus_hold_by_dnode);
2896 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus);
2897 EXPORT_SYMBOL(dmu_buf_rele_array);
2898 EXPORT_SYMBOL(dmu_prefetch);
2899 EXPORT_SYMBOL(dmu_prefetch_by_dnode);
2900 EXPORT_SYMBOL(dmu_prefetch_dnode);
2901 EXPORT_SYMBOL(dmu_free_range);
2902 EXPORT_SYMBOL(dmu_free_long_range);
2903 EXPORT_SYMBOL(dmu_free_long_object);
2904 EXPORT_SYMBOL(dmu_read);
2905 EXPORT_SYMBOL(dmu_read_by_dnode);
2906 EXPORT_SYMBOL(dmu_read_uio);
2907 EXPORT_SYMBOL(dmu_read_uio_dbuf);
2908 EXPORT_SYMBOL(dmu_read_uio_dnode);
2909 EXPORT_SYMBOL(dmu_write);
2910 EXPORT_SYMBOL(dmu_write_by_dnode);
2911 EXPORT_SYMBOL(dmu_write_by_dnode_flags);
2912 EXPORT_SYMBOL(dmu_write_uio);
2913 EXPORT_SYMBOL(dmu_write_uio_dbuf);
2914 EXPORT_SYMBOL(dmu_write_uio_dnode);
2915 EXPORT_SYMBOL(dmu_prealloc);
2916 EXPORT_SYMBOL(dmu_object_info);
2917 EXPORT_SYMBOL(dmu_object_info_from_dnode);
2918 EXPORT_SYMBOL(dmu_object_info_from_db);
2919 EXPORT_SYMBOL(dmu_object_size_from_db);
2920 EXPORT_SYMBOL(dmu_object_dnsize_from_db);
2921 EXPORT_SYMBOL(dmu_object_set_nlevels);
2922 EXPORT_SYMBOL(dmu_object_set_blocksize);
2923 EXPORT_SYMBOL(dmu_object_set_maxblkid);
2924 EXPORT_SYMBOL(dmu_object_set_checksum);
2925 EXPORT_SYMBOL(dmu_object_set_compress);
2926 EXPORT_SYMBOL(dmu_offset_next);
2927 EXPORT_SYMBOL(dmu_write_policy);
2928 EXPORT_SYMBOL(dmu_sync);
2929 EXPORT_SYMBOL(dmu_request_arcbuf);
2930 EXPORT_SYMBOL(dmu_return_arcbuf);
2931 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode);
2932 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf);
2933 EXPORT_SYMBOL(dmu_buf_hold);
2934 EXPORT_SYMBOL(dmu_ot);
2935 
2936 ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW,
2937 	"Enable NOP writes");
2938 
2939 ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW,
2940 	"Percentage of dirtied blocks from frees in one TXG");
2941 
2942 ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW,
2943 	"Enable forcing txg sync to find holes");
2944 
2945 ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
2946 	"Limit one prefetch call to this size");
2947 
2948 ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW,
2949 	"Override copies= for dedup objects");
2950