xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision 03270635d68df6a0392fb8f4b7c04acad764648b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 /*
25  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
29  * Copyright (c) 2018 DilOS
30  */
31 
32 #include <sys/dmu.h>
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dbuf.h>
36 #include <sys/dnode.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_synctask.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dmu_zfetch.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/zap.h>
48 #include <sys/zio_checksum.h>
49 #include <sys/zio_compress.h>
50 #include <sys/sa.h>
51 #include <sys/zfeature.h>
52 #include <sys/abd.h>
53 #ifdef _KERNEL
54 #include <sys/vmsystm.h>
55 #include <sys/zfs_znode.h>
56 #endif
57 
58 static xuio_stats_t xuio_stats = {
59 	{ "onloan_read_buf",	KSTAT_DATA_UINT64 },
60 	{ "onloan_write_buf",	KSTAT_DATA_UINT64 },
61 	{ "read_buf_copied",	KSTAT_DATA_UINT64 },
62 	{ "read_buf_nocopy",	KSTAT_DATA_UINT64 },
63 	{ "write_buf_copied",	KSTAT_DATA_UINT64 },
64 	{ "write_buf_nocopy",	KSTAT_DATA_UINT64 }
65 };
66 
67 #define	XUIOSTAT_INCR(stat, val)	\
68 	atomic_add_64(&xuio_stats.stat.value.ui64, (val))
69 #define	XUIOSTAT_BUMP(stat)	XUIOSTAT_INCR(stat, 1)
70 
71 /*
72  * Enable/disable nopwrite feature.
73  */
74 int zfs_nopwrite_enabled = 1;
75 
76 /*
77  * Tunable to control percentage of dirtied blocks from frees in one TXG.
78  * After this threshold is crossed, additional dirty blocks from frees
79  * wait until the next TXG.
80  * A value of zero will disable this throttle.
81  */
82 uint32_t zfs_per_txg_dirty_frees_percent = 30;
83 
84 /*
85  * This can be used for testing, to ensure that certain actions happen
86  * while in the middle of a remap (which might otherwise complete too
87  * quickly).
88  */
89 int zfs_object_remap_one_indirect_delay_ticks = 0;
90 
91 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
92 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "unallocated"		},
93 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "object directory"		},
94 	{ DMU_BSWAP_UINT64, TRUE,  TRUE,   "object array"		},
95 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "packed nvlist"		},
96 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "packed nvlist size"		},
97 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "bpobj"			},
98 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "bpobj header"		},
99 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "SPA space map header"	},
100 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "SPA space map"		},
101 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "ZIL intent log"		},
102 	{ DMU_BSWAP_DNODE,  TRUE,  FALSE,  "DMU dnode"			},
103 	{ DMU_BSWAP_OBJSET, TRUE,  TRUE,   "DMU objset"			},
104 	{ DMU_BSWAP_UINT64, TRUE,  TRUE,   "DSL directory"		},
105 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL directory child map"	},
106 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL dataset snap map"	},
107 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL props"			},
108 	{ DMU_BSWAP_UINT64, TRUE,  TRUE,   "DSL dataset"		},
109 	{ DMU_BSWAP_ZNODE,  TRUE,  FALSE,  "ZFS znode"			},
110 	{ DMU_BSWAP_OLDACL, TRUE,  FALSE,  "ZFS V0 ACL"			},
111 	{ DMU_BSWAP_UINT8,  FALSE, FALSE,  "ZFS plain file"		},
112 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "ZFS directory"		},
113 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "ZFS master node"		},
114 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "ZFS delete queue"		},
115 	{ DMU_BSWAP_UINT8,  FALSE, FALSE,  "zvol object"		},
116 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "zvol prop"			},
117 	{ DMU_BSWAP_UINT8,  FALSE, FALSE,  "other uint8[]"		},
118 	{ DMU_BSWAP_UINT64, FALSE, FALSE,  "other uint64[]"		},
119 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "other ZAP"			},
120 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "persistent error log"	},
121 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "SPA history"		},
122 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "SPA history offsets"	},
123 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "Pool properties"		},
124 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL permissions"		},
125 	{ DMU_BSWAP_ACL,    TRUE,  FALSE,  "ZFS ACL"			},
126 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "ZFS SYSACL"			},
127 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "FUID table"			},
128 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "FUID table size"		},
129 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL dataset next clones"	},
130 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "scan work queue"		},
131 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "ZFS user/group used"	},
132 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "ZFS user/group quota"	},
133 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "snapshot refcount tags"	},
134 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "DDT ZAP algorithm"		},
135 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "DDT statistics"		},
136 	{ DMU_BSWAP_UINT8,  TRUE,  FALSE,  "System attributes"		},
137 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "SA master node"		},
138 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "SA attr registration"	},
139 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "SA attr layouts"		},
140 	{ DMU_BSWAP_ZAP,    TRUE,  FALSE,  "scan translations"		},
141 	{ DMU_BSWAP_UINT8,  FALSE, FALSE,  "deduplicated block"		},
142 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL deadlist map"		},
143 	{ DMU_BSWAP_UINT64, TRUE,  TRUE,   "DSL deadlist map hdr"	},
144 	{ DMU_BSWAP_ZAP,    TRUE,  TRUE,   "DSL dir clones"		},
145 	{ DMU_BSWAP_UINT64, TRUE,  FALSE,  "bpobj subobj"		}
146 };
147 
148 const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
149 	{	byteswap_uint8_array,	"uint8"		},
150 	{	byteswap_uint16_array,	"uint16"	},
151 	{	byteswap_uint32_array,	"uint32"	},
152 	{	byteswap_uint64_array,	"uint64"	},
153 	{	zap_byteswap,		"zap"		},
154 	{	dnode_buf_byteswap,	"dnode"		},
155 	{	dmu_objset_byteswap,	"objset"	},
156 	{	zfs_znode_byteswap,	"znode"		},
157 	{	zfs_oldacl_byteswap,	"oldacl"	},
158 	{	zfs_acl_byteswap,	"acl"		}
159 };
160 
161 int
162 dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset,
163     void *tag, dmu_buf_t **dbp)
164 {
165 	uint64_t blkid;
166 	dmu_buf_impl_t *db;
167 
168 	blkid = dbuf_whichblock(dn, 0, offset);
169 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
170 	db = dbuf_hold(dn, blkid, tag);
171 	rw_exit(&dn->dn_struct_rwlock);
172 
173 	if (db == NULL) {
174 		*dbp = NULL;
175 		return (SET_ERROR(EIO));
176 	}
177 
178 	*dbp = &db->db;
179 	return (0);
180 }
181 int
182 dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,
183     void *tag, dmu_buf_t **dbp)
184 {
185 	dnode_t *dn;
186 	uint64_t blkid;
187 	dmu_buf_impl_t *db;
188 	int err;
189 
190 	err = dnode_hold(os, object, FTAG, &dn);
191 	if (err)
192 		return (err);
193 	blkid = dbuf_whichblock(dn, 0, offset);
194 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
195 	db = dbuf_hold(dn, blkid, tag);
196 	rw_exit(&dn->dn_struct_rwlock);
197 	dnode_rele(dn, FTAG);
198 
199 	if (db == NULL) {
200 		*dbp = NULL;
201 		return (SET_ERROR(EIO));
202 	}
203 
204 	*dbp = &db->db;
205 	return (err);
206 }
207 
208 int
209 dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset,
210     void *tag, dmu_buf_t **dbp, int flags)
211 {
212 	int err;
213 	int db_flags = DB_RF_CANFAIL;
214 
215 	if (flags & DMU_READ_NO_PREFETCH)
216 		db_flags |= DB_RF_NOPREFETCH;
217 
218 	err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp);
219 	if (err == 0) {
220 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
221 		err = dbuf_read(db, NULL, db_flags);
222 		if (err != 0) {
223 			dbuf_rele(db, tag);
224 			*dbp = NULL;
225 		}
226 	}
227 
228 	return (err);
229 }
230 
231 int
232 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
233     void *tag, dmu_buf_t **dbp, int flags)
234 {
235 	int err;
236 	int db_flags = DB_RF_CANFAIL;
237 
238 	if (flags & DMU_READ_NO_PREFETCH)
239 		db_flags |= DB_RF_NOPREFETCH;
240 
241 	err = dmu_buf_hold_noread(os, object, offset, tag, dbp);
242 	if (err == 0) {
243 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
244 		err = dbuf_read(db, NULL, db_flags);
245 		if (err != 0) {
246 			dbuf_rele(db, tag);
247 			*dbp = NULL;
248 		}
249 	}
250 
251 	return (err);
252 }
253 
254 int
255 dmu_bonus_max(void)
256 {
257 	return (DN_MAX_BONUSLEN);
258 }
259 
260 int
261 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
262 {
263 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
264 	dnode_t *dn;
265 	int error;
266 
267 	DB_DNODE_ENTER(db);
268 	dn = DB_DNODE(db);
269 
270 	if (dn->dn_bonus != db) {
271 		error = SET_ERROR(EINVAL);
272 	} else if (newsize < 0 || newsize > db_fake->db_size) {
273 		error = SET_ERROR(EINVAL);
274 	} else {
275 		dnode_setbonuslen(dn, newsize, tx);
276 		error = 0;
277 	}
278 
279 	DB_DNODE_EXIT(db);
280 	return (error);
281 }
282 
283 int
284 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
285 {
286 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
287 	dnode_t *dn;
288 	int error;
289 
290 	DB_DNODE_ENTER(db);
291 	dn = DB_DNODE(db);
292 
293 	if (!DMU_OT_IS_VALID(type)) {
294 		error = SET_ERROR(EINVAL);
295 	} else if (dn->dn_bonus != db) {
296 		error = SET_ERROR(EINVAL);
297 	} else {
298 		dnode_setbonus_type(dn, type, tx);
299 		error = 0;
300 	}
301 
302 	DB_DNODE_EXIT(db);
303 	return (error);
304 }
305 
306 dmu_object_type_t
307 dmu_get_bonustype(dmu_buf_t *db_fake)
308 {
309 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
310 	dnode_t *dn;
311 	dmu_object_type_t type;
312 
313 	DB_DNODE_ENTER(db);
314 	dn = DB_DNODE(db);
315 	type = dn->dn_bonustype;
316 	DB_DNODE_EXIT(db);
317 
318 	return (type);
319 }
320 
321 int
322 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
323 {
324 	dnode_t *dn;
325 	int error;
326 
327 	error = dnode_hold(os, object, FTAG, &dn);
328 	dbuf_rm_spill(dn, tx);
329 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
330 	dnode_rm_spill(dn, tx);
331 	rw_exit(&dn->dn_struct_rwlock);
332 	dnode_rele(dn, FTAG);
333 	return (error);
334 }
335 
336 /*
337  * returns ENOENT, EIO, or 0.
338  */
339 int
340 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
341 {
342 	dnode_t *dn;
343 	dmu_buf_impl_t *db;
344 	int error;
345 
346 	error = dnode_hold(os, object, FTAG, &dn);
347 	if (error)
348 		return (error);
349 
350 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
351 	if (dn->dn_bonus == NULL) {
352 		rw_exit(&dn->dn_struct_rwlock);
353 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
354 		if (dn->dn_bonus == NULL)
355 			dbuf_create_bonus(dn);
356 	}
357 	db = dn->dn_bonus;
358 
359 	/* as long as the bonus buf is held, the dnode will be held */
360 	if (refcount_add(&db->db_holds, tag) == 1) {
361 		VERIFY(dnode_add_ref(dn, db));
362 		atomic_inc_32(&dn->dn_dbufs_count);
363 	}
364 
365 	/*
366 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
367 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
368 	 * a dnode hold for every dbuf.
369 	 */
370 	rw_exit(&dn->dn_struct_rwlock);
371 
372 	dnode_rele(dn, FTAG);
373 
374 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
375 
376 	*dbp = &db->db;
377 	return (0);
378 }
379 
380 /*
381  * returns ENOENT, EIO, or 0.
382  *
383  * This interface will allocate a blank spill dbuf when a spill blk
384  * doesn't already exist on the dnode.
385  *
386  * if you only want to find an already existing spill db, then
387  * dmu_spill_hold_existing() should be used.
388  */
389 int
390 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
391 {
392 	dmu_buf_impl_t *db = NULL;
393 	int err;
394 
395 	if ((flags & DB_RF_HAVESTRUCT) == 0)
396 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
397 
398 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
399 
400 	if ((flags & DB_RF_HAVESTRUCT) == 0)
401 		rw_exit(&dn->dn_struct_rwlock);
402 
403 	ASSERT(db != NULL);
404 	err = dbuf_read(db, NULL, flags);
405 	if (err == 0)
406 		*dbp = &db->db;
407 	else
408 		dbuf_rele(db, tag);
409 	return (err);
410 }
411 
412 int
413 dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
414 {
415 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
416 	dnode_t *dn;
417 	int err;
418 
419 	DB_DNODE_ENTER(db);
420 	dn = DB_DNODE(db);
421 
422 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
423 		err = SET_ERROR(EINVAL);
424 	} else {
425 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
426 
427 		if (!dn->dn_have_spill) {
428 			err = SET_ERROR(ENOENT);
429 		} else {
430 			err = dmu_spill_hold_by_dnode(dn,
431 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
432 		}
433 
434 		rw_exit(&dn->dn_struct_rwlock);
435 	}
436 
437 	DB_DNODE_EXIT(db);
438 	return (err);
439 }
440 
441 int
442 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
443 {
444 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
445 	dnode_t *dn;
446 	int err;
447 
448 	DB_DNODE_ENTER(db);
449 	dn = DB_DNODE(db);
450 	err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
451 	DB_DNODE_EXIT(db);
452 
453 	return (err);
454 }
455 
456 /*
457  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
458  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
459  * and can induce severe lock contention when writing to several files
460  * whose dnodes are in the same block.
461  */
462 int
463 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
464     boolean_t read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
465 {
466 	dmu_buf_t **dbp;
467 	uint64_t blkid, nblks, i;
468 	uint32_t dbuf_flags;
469 	int err;
470 	zio_t *zio;
471 
472 	ASSERT(length <= DMU_MAX_ACCESS);
473 
474 	/*
475 	 * Note: We directly notify the prefetch code of this read, so that
476 	 * we can tell it about the multi-block read.  dbuf_read() only knows
477 	 * about the one block it is accessing.
478 	 */
479 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT |
480 	    DB_RF_NOPREFETCH;
481 
482 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
483 	if (dn->dn_datablkshift) {
484 		int blkshift = dn->dn_datablkshift;
485 		nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
486 		    P2ALIGN(offset, 1ULL << blkshift)) >> blkshift;
487 	} else {
488 		if (offset + length > dn->dn_datablksz) {
489 			zfs_panic_recover("zfs: accessing past end of object "
490 			    "%llx/%llx (size=%u access=%llu+%llu)",
491 			    (longlong_t)dn->dn_objset->
492 			    os_dsl_dataset->ds_object,
493 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
494 			    (longlong_t)offset, (longlong_t)length);
495 			rw_exit(&dn->dn_struct_rwlock);
496 			return (SET_ERROR(EIO));
497 		}
498 		nblks = 1;
499 	}
500 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
501 
502 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
503 	blkid = dbuf_whichblock(dn, 0, offset);
504 	for (i = 0; i < nblks; i++) {
505 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);
506 		if (db == NULL) {
507 			rw_exit(&dn->dn_struct_rwlock);
508 			dmu_buf_rele_array(dbp, nblks, tag);
509 			zio_nowait(zio);
510 			return (SET_ERROR(EIO));
511 		}
512 
513 		/* initiate async i/o */
514 		if (read)
515 			(void) dbuf_read(db, zio, dbuf_flags);
516 		dbp[i] = &db->db;
517 	}
518 
519 	if ((flags & DMU_READ_NO_PREFETCH) == 0 &&
520 	    DNODE_META_IS_CACHEABLE(dn) && length <= zfetch_array_rd_sz) {
521 		dmu_zfetch(&dn->dn_zfetch, blkid, nblks,
522 		    read && DNODE_IS_CACHEABLE(dn));
523 	}
524 	rw_exit(&dn->dn_struct_rwlock);
525 
526 	/* wait for async i/o */
527 	err = zio_wait(zio);
528 	if (err) {
529 		dmu_buf_rele_array(dbp, nblks, tag);
530 		return (err);
531 	}
532 
533 	/* wait for other io to complete */
534 	if (read) {
535 		for (i = 0; i < nblks; i++) {
536 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
537 			mutex_enter(&db->db_mtx);
538 			while (db->db_state == DB_READ ||
539 			    db->db_state == DB_FILL)
540 				cv_wait(&db->db_changed, &db->db_mtx);
541 			if (db->db_state == DB_UNCACHED)
542 				err = SET_ERROR(EIO);
543 			mutex_exit(&db->db_mtx);
544 			if (err) {
545 				dmu_buf_rele_array(dbp, nblks, tag);
546 				return (err);
547 			}
548 		}
549 	}
550 
551 	*numbufsp = nblks;
552 	*dbpp = dbp;
553 	return (0);
554 }
555 
556 static int
557 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
558     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
559 {
560 	dnode_t *dn;
561 	int err;
562 
563 	err = dnode_hold(os, object, FTAG, &dn);
564 	if (err)
565 		return (err);
566 
567 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
568 	    numbufsp, dbpp, DMU_READ_PREFETCH);
569 
570 	dnode_rele(dn, FTAG);
571 
572 	return (err);
573 }
574 
575 int
576 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
577     uint64_t length, boolean_t read, void *tag, int *numbufsp,
578     dmu_buf_t ***dbpp)
579 {
580 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
581 	dnode_t *dn;
582 	int err;
583 
584 	DB_DNODE_ENTER(db);
585 	dn = DB_DNODE(db);
586 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
587 	    numbufsp, dbpp, DMU_READ_PREFETCH);
588 	DB_DNODE_EXIT(db);
589 
590 	return (err);
591 }
592 
593 void
594 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
595 {
596 	int i;
597 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
598 
599 	if (numbufs == 0)
600 		return;
601 
602 	for (i = 0; i < numbufs; i++) {
603 		if (dbp[i])
604 			dbuf_rele(dbp[i], tag);
605 	}
606 
607 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
608 }
609 
610 /*
611  * Issue prefetch i/os for the given blocks.  If level is greater than 0, the
612  * indirect blocks prefeteched will be those that point to the blocks containing
613  * the data starting at offset, and continuing to offset + len.
614  *
615  * Note that if the indirect blocks above the blocks being prefetched are not in
616  * cache, they will be asychronously read in.
617  */
618 void
619 dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,
620     uint64_t len, zio_priority_t pri)
621 {
622 	dnode_t *dn;
623 	uint64_t blkid;
624 	int nblks, err;
625 
626 	if (len == 0) {  /* they're interested in the bonus buffer */
627 		dn = DMU_META_DNODE(os);
628 
629 		if (object == 0 || object >= DN_MAX_OBJECT)
630 			return;
631 
632 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
633 		blkid = dbuf_whichblock(dn, level,
634 		    object * sizeof (dnode_phys_t));
635 		dbuf_prefetch(dn, level, blkid, pri, 0);
636 		rw_exit(&dn->dn_struct_rwlock);
637 		return;
638 	}
639 
640 	/*
641 	 * XXX - Note, if the dnode for the requested object is not
642 	 * already cached, we will do a *synchronous* read in the
643 	 * dnode_hold() call.  The same is true for any indirects.
644 	 */
645 	err = dnode_hold(os, object, FTAG, &dn);
646 	if (err != 0)
647 		return;
648 
649 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
650 	/*
651 	 * offset + len - 1 is the last byte we want to prefetch for, and offset
652 	 * is the first.  Then dbuf_whichblk(dn, level, off + len - 1) is the
653 	 * last block we want to prefetch, and dbuf_whichblock(dn, level,
654 	 * offset)  is the first.  Then the number we need to prefetch is the
655 	 * last - first + 1.
656 	 */
657 	if (level > 0 || dn->dn_datablkshift != 0) {
658 		nblks = dbuf_whichblock(dn, level, offset + len - 1) -
659 		    dbuf_whichblock(dn, level, offset) + 1;
660 	} else {
661 		nblks = (offset < dn->dn_datablksz);
662 	}
663 
664 	if (nblks != 0) {
665 		blkid = dbuf_whichblock(dn, level, offset);
666 		for (int i = 0; i < nblks; i++)
667 			dbuf_prefetch(dn, level, blkid + i, pri, 0);
668 	}
669 
670 	rw_exit(&dn->dn_struct_rwlock);
671 
672 	dnode_rele(dn, FTAG);
673 }
674 
675 /*
676  * Get the next "chunk" of file data to free.  We traverse the file from
677  * the end so that the file gets shorter over time (if we crashes in the
678  * middle, this will leave us in a better state).  We find allocated file
679  * data by simply searching the allocated level 1 indirects.
680  *
681  * On input, *start should be the first offset that does not need to be
682  * freed (e.g. "offset + length").  On return, *start will be the first
683  * offset that should be freed.
684  */
685 static int
686 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum)
687 {
688 	uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
689 	/* bytes of data covered by a level-1 indirect block */
690 	uint64_t iblkrange =
691 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
692 
693 	ASSERT3U(minimum, <=, *start);
694 
695 	if (*start - minimum <= iblkrange * maxblks) {
696 		*start = minimum;
697 		return (0);
698 	}
699 	ASSERT(ISP2(iblkrange));
700 
701 	for (uint64_t blks = 0; *start > minimum && blks < maxblks; blks++) {
702 		int err;
703 
704 		/*
705 		 * dnode_next_offset(BACKWARDS) will find an allocated L1
706 		 * indirect block at or before the input offset.  We must
707 		 * decrement *start so that it is at the end of the region
708 		 * to search.
709 		 */
710 		(*start)--;
711 		err = dnode_next_offset(dn,
712 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
713 
714 		/* if there are no indirect blocks before start, we are done */
715 		if (err == ESRCH) {
716 			*start = minimum;
717 			break;
718 		} else if (err != 0) {
719 			return (err);
720 		}
721 
722 		/* set start to the beginning of this L1 indirect */
723 		*start = P2ALIGN(*start, iblkrange);
724 	}
725 	if (*start < minimum)
726 		*start = minimum;
727 	return (0);
728 }
729 
730 /*
731  * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,
732  * otherwise return false.
733  * Used below in dmu_free_long_range_impl() to enable abort when unmounting
734  */
735 /*ARGSUSED*/
736 static boolean_t
737 dmu_objset_zfs_unmounting(objset_t *os)
738 {
739 #ifdef _KERNEL
740 	if (dmu_objset_type(os) == DMU_OST_ZFS)
741 		return (zfs_get_vfs_flag_unmounted(os));
742 #endif
743 	return (B_FALSE);
744 }
745 
746 static int
747 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
748     uint64_t length)
749 {
750 	uint64_t object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
751 	int err;
752 	uint64_t dirty_frees_threshold;
753 	dsl_pool_t *dp = dmu_objset_pool(os);
754 
755 	if (offset >= object_size)
756 		return (0);
757 
758 	if (zfs_per_txg_dirty_frees_percent <= 100)
759 		dirty_frees_threshold =
760 		    zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100;
761 	else
762 		dirty_frees_threshold = zfs_dirty_data_max / 4;
763 
764 	if (length == DMU_OBJECT_END || offset + length > object_size)
765 		length = object_size - offset;
766 
767 	while (length != 0) {
768 		uint64_t chunk_end, chunk_begin, chunk_len;
769 		uint64_t long_free_dirty_all_txgs = 0;
770 		dmu_tx_t *tx;
771 
772 		if (dmu_objset_zfs_unmounting(dn->dn_objset))
773 			return (SET_ERROR(EINTR));
774 
775 		chunk_end = chunk_begin = offset + length;
776 
777 		/* move chunk_begin backwards to the beginning of this chunk */
778 		err = get_next_chunk(dn, &chunk_begin, offset);
779 		if (err)
780 			return (err);
781 		ASSERT3U(chunk_begin, >=, offset);
782 		ASSERT3U(chunk_begin, <=, chunk_end);
783 
784 		chunk_len = chunk_end - chunk_begin;
785 
786 		mutex_enter(&dp->dp_lock);
787 		for (int t = 0; t < TXG_SIZE; t++) {
788 			long_free_dirty_all_txgs +=
789 			    dp->dp_long_free_dirty_pertxg[t];
790 		}
791 		mutex_exit(&dp->dp_lock);
792 
793 		/*
794 		 * To avoid filling up a TXG with just frees wait for
795 		 * the next TXG to open before freeing more chunks if
796 		 * we have reached the threshold of frees
797 		 */
798 		if (dirty_frees_threshold != 0 &&
799 		    long_free_dirty_all_txgs >= dirty_frees_threshold) {
800 			txg_wait_open(dp, 0);
801 			continue;
802 		}
803 
804 		tx = dmu_tx_create(os);
805 		dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len);
806 
807 		/*
808 		 * Mark this transaction as typically resulting in a net
809 		 * reduction in space used.
810 		 */
811 		dmu_tx_mark_netfree(tx);
812 		err = dmu_tx_assign(tx, TXG_WAIT);
813 		if (err) {
814 			dmu_tx_abort(tx);
815 			return (err);
816 		}
817 
818 		mutex_enter(&dp->dp_lock);
819 		dp->dp_long_free_dirty_pertxg[dmu_tx_get_txg(tx) & TXG_MASK] +=
820 		    chunk_len;
821 		mutex_exit(&dp->dp_lock);
822 		DTRACE_PROBE3(free__long__range,
823 		    uint64_t, long_free_dirty_all_txgs, uint64_t, chunk_len,
824 		    uint64_t, dmu_tx_get_txg(tx));
825 		dnode_free_range(dn, chunk_begin, chunk_len, tx);
826 		dmu_tx_commit(tx);
827 
828 		length -= chunk_len;
829 	}
830 	return (0);
831 }
832 
833 int
834 dmu_free_long_range(objset_t *os, uint64_t object,
835     uint64_t offset, uint64_t length)
836 {
837 	dnode_t *dn;
838 	int err;
839 
840 	err = dnode_hold(os, object, FTAG, &dn);
841 	if (err != 0)
842 		return (err);
843 	err = dmu_free_long_range_impl(os, dn, offset, length);
844 
845 	/*
846 	 * It is important to zero out the maxblkid when freeing the entire
847 	 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
848 	 * will take the fast path, and (b) dnode_reallocate() can verify
849 	 * that the entire file has been freed.
850 	 */
851 	if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
852 		dn->dn_maxblkid = 0;
853 
854 	dnode_rele(dn, FTAG);
855 	return (err);
856 }
857 
858 int
859 dmu_free_long_object(objset_t *os, uint64_t object)
860 {
861 	dmu_tx_t *tx;
862 	int err;
863 
864 	err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
865 	if (err != 0)
866 		return (err);
867 
868 	tx = dmu_tx_create(os);
869 	dmu_tx_hold_bonus(tx, object);
870 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
871 	dmu_tx_mark_netfree(tx);
872 	err = dmu_tx_assign(tx, TXG_WAIT);
873 	if (err == 0) {
874 		err = dmu_object_free(os, object, tx);
875 		dmu_tx_commit(tx);
876 	} else {
877 		dmu_tx_abort(tx);
878 	}
879 
880 	return (err);
881 }
882 
883 int
884 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
885     uint64_t size, dmu_tx_t *tx)
886 {
887 	dnode_t *dn;
888 	int err = dnode_hold(os, object, FTAG, &dn);
889 	if (err)
890 		return (err);
891 	ASSERT(offset < UINT64_MAX);
892 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
893 	dnode_free_range(dn, offset, size, tx);
894 	dnode_rele(dn, FTAG);
895 	return (0);
896 }
897 
898 static int
899 dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,
900     void *buf, uint32_t flags)
901 {
902 	dmu_buf_t **dbp;
903 	int numbufs, err = 0;
904 
905 	/*
906 	 * Deal with odd block sizes, where there can't be data past the first
907 	 * block.  If we ever do the tail block optimization, we will need to
908 	 * handle that here as well.
909 	 */
910 	if (dn->dn_maxblkid == 0) {
911 		int newsz = offset > dn->dn_datablksz ? 0 :
912 		    MIN(size, dn->dn_datablksz - offset);
913 		bzero((char *)buf + newsz, size - newsz);
914 		size = newsz;
915 	}
916 
917 	while (size > 0) {
918 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
919 		int i;
920 
921 		/*
922 		 * NB: we could do this block-at-a-time, but it's nice
923 		 * to be reading in parallel.
924 		 */
925 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
926 		    TRUE, FTAG, &numbufs, &dbp, flags);
927 		if (err)
928 			break;
929 
930 		for (i = 0; i < numbufs; i++) {
931 			int tocpy;
932 			int bufoff;
933 			dmu_buf_t *db = dbp[i];
934 
935 			ASSERT(size > 0);
936 
937 			bufoff = offset - db->db_offset;
938 			tocpy = (int)MIN(db->db_size - bufoff, size);
939 
940 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
941 
942 			offset += tocpy;
943 			size -= tocpy;
944 			buf = (char *)buf + tocpy;
945 		}
946 		dmu_buf_rele_array(dbp, numbufs, FTAG);
947 	}
948 	return (err);
949 }
950 
951 int
952 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
953     void *buf, uint32_t flags)
954 {
955 	dnode_t *dn;
956 	int err;
957 
958 	err = dnode_hold(os, object, FTAG, &dn);
959 	if (err != 0)
960 		return (err);
961 
962 	err = dmu_read_impl(dn, offset, size, buf, flags);
963 	dnode_rele(dn, FTAG);
964 	return (err);
965 }
966 
967 int
968 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,
969     uint32_t flags)
970 {
971 	return (dmu_read_impl(dn, offset, size, buf, flags));
972 }
973 
974 static void
975 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,
976     const void *buf, dmu_tx_t *tx)
977 {
978 	int i;
979 
980 	for (i = 0; i < numbufs; i++) {
981 		int tocpy;
982 		int bufoff;
983 		dmu_buf_t *db = dbp[i];
984 
985 		ASSERT(size > 0);
986 
987 		bufoff = offset - db->db_offset;
988 		tocpy = (int)MIN(db->db_size - bufoff, size);
989 
990 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
991 
992 		if (tocpy == db->db_size)
993 			dmu_buf_will_fill(db, tx);
994 		else
995 			dmu_buf_will_dirty(db, tx);
996 
997 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
998 
999 		if (tocpy == db->db_size)
1000 			dmu_buf_fill_done(db, tx);
1001 
1002 		offset += tocpy;
1003 		size -= tocpy;
1004 		buf = (char *)buf + tocpy;
1005 	}
1006 }
1007 
1008 void
1009 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1010     const void *buf, dmu_tx_t *tx)
1011 {
1012 	dmu_buf_t **dbp;
1013 	int numbufs;
1014 
1015 	if (size == 0)
1016 		return;
1017 
1018 	VERIFY0(dmu_buf_hold_array(os, object, offset, size,
1019 	    FALSE, FTAG, &numbufs, &dbp));
1020 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1021 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1022 }
1023 
1024 void
1025 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,
1026     const void *buf, dmu_tx_t *tx)
1027 {
1028 	dmu_buf_t **dbp;
1029 	int numbufs;
1030 
1031 	if (size == 0)
1032 		return;
1033 
1034 	VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,
1035 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH));
1036 	dmu_write_impl(dbp, numbufs, offset, size, buf, tx);
1037 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1038 }
1039 
1040 static int
1041 dmu_object_remap_one_indirect(objset_t *os, dnode_t *dn,
1042     uint64_t last_removal_txg, uint64_t offset)
1043 {
1044 	uint64_t l1blkid = dbuf_whichblock(dn, 1, offset);
1045 	int err = 0;
1046 
1047 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1048 	dmu_buf_impl_t *dbuf = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1049 	ASSERT3P(dbuf, !=, NULL);
1050 
1051 	/*
1052 	 * If the block hasn't been written yet, this default will ensure
1053 	 * we don't try to remap it.
1054 	 */
1055 	uint64_t birth = UINT64_MAX;
1056 	ASSERT3U(last_removal_txg, !=, UINT64_MAX);
1057 	if (dbuf->db_blkptr != NULL)
1058 		birth = dbuf->db_blkptr->blk_birth;
1059 	rw_exit(&dn->dn_struct_rwlock);
1060 
1061 	/*
1062 	 * If this L1 was already written after the last removal, then we've
1063 	 * already tried to remap it.
1064 	 */
1065 	if (birth <= last_removal_txg &&
1066 	    dbuf_read(dbuf, NULL, DB_RF_MUST_SUCCEED) == 0 &&
1067 	    dbuf_can_remap(dbuf)) {
1068 		dmu_tx_t *tx = dmu_tx_create(os);
1069 		dmu_tx_hold_remap_l1indirect(tx, dn->dn_object);
1070 		err = dmu_tx_assign(tx, TXG_WAIT);
1071 		if (err == 0) {
1072 			(void) dbuf_dirty(dbuf, tx);
1073 			dmu_tx_commit(tx);
1074 		} else {
1075 			dmu_tx_abort(tx);
1076 		}
1077 	}
1078 
1079 	dbuf_rele(dbuf, FTAG);
1080 
1081 	delay(zfs_object_remap_one_indirect_delay_ticks);
1082 
1083 	return (err);
1084 }
1085 
1086 /*
1087  * Remap all blockpointers in the object, if possible, so that they reference
1088  * only concrete vdevs.
1089  *
1090  * To do this, iterate over the L0 blockpointers and remap any that reference
1091  * an indirect vdev. Note that we only examine L0 blockpointers; since we
1092  * cannot guarantee that we can remap all blockpointer anyways (due to split
1093  * blocks), we do not want to make the code unnecessarily complicated to
1094  * catch the unlikely case that there is an L1 block on an indirect vdev that
1095  * contains no indirect blockpointers.
1096  */
1097 int
1098 dmu_object_remap_indirects(objset_t *os, uint64_t object,
1099     uint64_t last_removal_txg)
1100 {
1101 	uint64_t offset, l1span;
1102 	int err;
1103 	dnode_t *dn;
1104 
1105 	err = dnode_hold(os, object, FTAG, &dn);
1106 	if (err != 0) {
1107 		return (err);
1108 	}
1109 
1110 	if (dn->dn_nlevels <= 1) {
1111 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1112 			err = SET_ERROR(EINTR);
1113 		}
1114 
1115 		/*
1116 		 * If the dnode has no indirect blocks, we cannot dirty them.
1117 		 * We still want to remap the blkptr(s) in the dnode if
1118 		 * appropriate, so mark it as dirty.
1119 		 */
1120 		if (err == 0 && dnode_needs_remap(dn)) {
1121 			dmu_tx_t *tx = dmu_tx_create(os);
1122 			dmu_tx_hold_bonus(tx, dn->dn_object);
1123 			if ((err = dmu_tx_assign(tx, TXG_WAIT)) == 0) {
1124 				dnode_setdirty(dn, tx);
1125 				dmu_tx_commit(tx);
1126 			} else {
1127 				dmu_tx_abort(tx);
1128 			}
1129 		}
1130 
1131 		dnode_rele(dn, FTAG);
1132 		return (err);
1133 	}
1134 
1135 	offset = 0;
1136 	l1span = 1ULL << (dn->dn_indblkshift - SPA_BLKPTRSHIFT +
1137 	    dn->dn_datablkshift);
1138 	/*
1139 	 * Find the next L1 indirect that is not a hole.
1140 	 */
1141 	while (dnode_next_offset(dn, 0, &offset, 2, 1, 0) == 0) {
1142 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1143 			err = SET_ERROR(EINTR);
1144 			break;
1145 		}
1146 		if ((err = dmu_object_remap_one_indirect(os, dn,
1147 		    last_removal_txg, offset)) != 0) {
1148 			break;
1149 		}
1150 		offset += l1span;
1151 	}
1152 
1153 	dnode_rele(dn, FTAG);
1154 	return (err);
1155 }
1156 
1157 void
1158 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1159     dmu_tx_t *tx)
1160 {
1161 	dmu_buf_t **dbp;
1162 	int numbufs, i;
1163 
1164 	if (size == 0)
1165 		return;
1166 
1167 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
1168 	    FALSE, FTAG, &numbufs, &dbp));
1169 
1170 	for (i = 0; i < numbufs; i++) {
1171 		dmu_buf_t *db = dbp[i];
1172 
1173 		dmu_buf_will_not_fill(db, tx);
1174 	}
1175 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1176 }
1177 
1178 void
1179 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
1180     void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
1181     int compressed_size, int byteorder, dmu_tx_t *tx)
1182 {
1183 	dmu_buf_t *db;
1184 
1185 	ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
1186 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
1187 	VERIFY0(dmu_buf_hold_noread(os, object, offset,
1188 	    FTAG, &db));
1189 
1190 	dmu_buf_write_embedded(db,
1191 	    data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
1192 	    uncompressed_size, compressed_size, byteorder, tx);
1193 
1194 	dmu_buf_rele(db, FTAG);
1195 }
1196 
1197 /*
1198  * DMU support for xuio
1199  */
1200 kstat_t *xuio_ksp = NULL;
1201 
1202 int
1203 dmu_xuio_init(xuio_t *xuio, int nblk)
1204 {
1205 	dmu_xuio_t *priv;
1206 	uio_t *uio = &xuio->xu_uio;
1207 
1208 	uio->uio_iovcnt = nblk;
1209 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
1210 
1211 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
1212 	priv->cnt = nblk;
1213 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
1214 	priv->iovp = uio->uio_iov;
1215 	XUIO_XUZC_PRIV(xuio) = priv;
1216 
1217 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
1218 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
1219 	else
1220 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
1221 
1222 	return (0);
1223 }
1224 
1225 void
1226 dmu_xuio_fini(xuio_t *xuio)
1227 {
1228 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1229 	int nblk = priv->cnt;
1230 
1231 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
1232 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
1233 	kmem_free(priv, sizeof (dmu_xuio_t));
1234 
1235 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
1236 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
1237 	else
1238 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
1239 }
1240 
1241 /*
1242  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
1243  * and increase priv->next by 1.
1244  */
1245 int
1246 dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
1247 {
1248 	struct iovec *iov;
1249 	uio_t *uio = &xuio->xu_uio;
1250 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1251 	int i = priv->next++;
1252 
1253 	ASSERT(i < priv->cnt);
1254 	ASSERT(off + n <= arc_buf_lsize(abuf));
1255 	iov = uio->uio_iov + i;
1256 	iov->iov_base = (char *)abuf->b_data + off;
1257 	iov->iov_len = n;
1258 	priv->bufs[i] = abuf;
1259 	return (0);
1260 }
1261 
1262 int
1263 dmu_xuio_cnt(xuio_t *xuio)
1264 {
1265 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1266 	return (priv->cnt);
1267 }
1268 
1269 arc_buf_t *
1270 dmu_xuio_arcbuf(xuio_t *xuio, int i)
1271 {
1272 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1273 
1274 	ASSERT(i < priv->cnt);
1275 	return (priv->bufs[i]);
1276 }
1277 
1278 void
1279 dmu_xuio_clear(xuio_t *xuio, int i)
1280 {
1281 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1282 
1283 	ASSERT(i < priv->cnt);
1284 	priv->bufs[i] = NULL;
1285 }
1286 
1287 static void
1288 xuio_stat_init(void)
1289 {
1290 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
1291 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
1292 	    KSTAT_FLAG_VIRTUAL);
1293 	if (xuio_ksp != NULL) {
1294 		xuio_ksp->ks_data = &xuio_stats;
1295 		kstat_install(xuio_ksp);
1296 	}
1297 }
1298 
1299 static void
1300 xuio_stat_fini(void)
1301 {
1302 	if (xuio_ksp != NULL) {
1303 		kstat_delete(xuio_ksp);
1304 		xuio_ksp = NULL;
1305 	}
1306 }
1307 
1308 void
1309 xuio_stat_wbuf_copied(void)
1310 {
1311 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1312 }
1313 
1314 void
1315 xuio_stat_wbuf_nocopy(void)
1316 {
1317 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
1318 }
1319 
1320 #ifdef _KERNEL
1321 int
1322 dmu_read_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size)
1323 {
1324 	dmu_buf_t **dbp;
1325 	int numbufs, i, err;
1326 	xuio_t *xuio = NULL;
1327 
1328 	/*
1329 	 * NB: we could do this block-at-a-time, but it's nice
1330 	 * to be reading in parallel.
1331 	 */
1332 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
1333 	    TRUE, FTAG, &numbufs, &dbp, 0);
1334 	if (err)
1335 		return (err);
1336 
1337 	if (uio->uio_extflg == UIO_XUIO)
1338 		xuio = (xuio_t *)uio;
1339 
1340 	for (i = 0; i < numbufs; i++) {
1341 		int tocpy;
1342 		int bufoff;
1343 		dmu_buf_t *db = dbp[i];
1344 
1345 		ASSERT(size > 0);
1346 
1347 		bufoff = uio->uio_loffset - db->db_offset;
1348 		tocpy = (int)MIN(db->db_size - bufoff, size);
1349 
1350 		if (xuio) {
1351 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
1352 			arc_buf_t *dbuf_abuf = dbi->db_buf;
1353 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1354 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1355 			if (!err) {
1356 				uio->uio_resid -= tocpy;
1357 				uio->uio_loffset += tocpy;
1358 			}
1359 
1360 			if (abuf == dbuf_abuf)
1361 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1362 			else
1363 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1364 		} else {
1365 			err = uiomove((char *)db->db_data + bufoff, tocpy,
1366 			    UIO_READ, uio);
1367 		}
1368 		if (err)
1369 			break;
1370 
1371 		size -= tocpy;
1372 	}
1373 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1374 
1375 	return (err);
1376 }
1377 
1378 /*
1379  * Read 'size' bytes into the uio buffer.
1380  * From object zdb->db_object.
1381  * Starting at offset uio->uio_loffset.
1382  *
1383  * If the caller already has a dbuf in the target object
1384  * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1385  * because we don't have to find the dnode_t for the object.
1386  */
1387 int
1388 dmu_read_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size)
1389 {
1390 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1391 	dnode_t *dn;
1392 	int err;
1393 
1394 	if (size == 0)
1395 		return (0);
1396 
1397 	DB_DNODE_ENTER(db);
1398 	dn = DB_DNODE(db);
1399 	err = dmu_read_uio_dnode(dn, uio, size);
1400 	DB_DNODE_EXIT(db);
1401 
1402 	return (err);
1403 }
1404 
1405 /*
1406  * Read 'size' bytes into the uio buffer.
1407  * From the specified object
1408  * Starting at offset uio->uio_loffset.
1409  */
1410 int
1411 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
1412 {
1413 	dnode_t *dn;
1414 	int err;
1415 
1416 	if (size == 0)
1417 		return (0);
1418 
1419 	err = dnode_hold(os, object, FTAG, &dn);
1420 	if (err)
1421 		return (err);
1422 
1423 	err = dmu_read_uio_dnode(dn, uio, size);
1424 
1425 	dnode_rele(dn, FTAG);
1426 
1427 	return (err);
1428 }
1429 
1430 int
1431 dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1432 {
1433 	dmu_buf_t **dbp;
1434 	int numbufs;
1435 	int err = 0;
1436 	int i;
1437 
1438 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
1439 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1440 	if (err)
1441 		return (err);
1442 
1443 	for (i = 0; i < numbufs; i++) {
1444 		int tocpy;
1445 		int bufoff;
1446 		dmu_buf_t *db = dbp[i];
1447 
1448 		ASSERT(size > 0);
1449 
1450 		bufoff = uio->uio_loffset - db->db_offset;
1451 		tocpy = (int)MIN(db->db_size - bufoff, size);
1452 
1453 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1454 
1455 		if (tocpy == db->db_size)
1456 			dmu_buf_will_fill(db, tx);
1457 		else
1458 			dmu_buf_will_dirty(db, tx);
1459 
1460 		/*
1461 		 * XXX uiomove could block forever (eg. nfs-backed
1462 		 * pages).  There needs to be a uiolockdown() function
1463 		 * to lock the pages in memory, so that uiomove won't
1464 		 * block.
1465 		 */
1466 		err = uiomove((char *)db->db_data + bufoff, tocpy,
1467 		    UIO_WRITE, uio);
1468 
1469 		if (tocpy == db->db_size)
1470 			dmu_buf_fill_done(db, tx);
1471 
1472 		if (err)
1473 			break;
1474 
1475 		size -= tocpy;
1476 	}
1477 
1478 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1479 	return (err);
1480 }
1481 
1482 /*
1483  * Write 'size' bytes from the uio buffer.
1484  * To object zdb->db_object.
1485  * Starting at offset uio->uio_loffset.
1486  *
1487  * If the caller already has a dbuf in the target object
1488  * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1489  * because we don't have to find the dnode_t for the object.
1490  */
1491 int
1492 dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
1493     dmu_tx_t *tx)
1494 {
1495 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1496 	dnode_t *dn;
1497 	int err;
1498 
1499 	if (size == 0)
1500 		return (0);
1501 
1502 	DB_DNODE_ENTER(db);
1503 	dn = DB_DNODE(db);
1504 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1505 	DB_DNODE_EXIT(db);
1506 
1507 	return (err);
1508 }
1509 
1510 /*
1511  * Write 'size' bytes from the uio buffer.
1512  * To the specified object.
1513  * Starting at offset uio->uio_loffset.
1514  */
1515 int
1516 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
1517     dmu_tx_t *tx)
1518 {
1519 	dnode_t *dn;
1520 	int err;
1521 
1522 	if (size == 0)
1523 		return (0);
1524 
1525 	err = dnode_hold(os, object, FTAG, &dn);
1526 	if (err)
1527 		return (err);
1528 
1529 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1530 
1531 	dnode_rele(dn, FTAG);
1532 
1533 	return (err);
1534 }
1535 
1536 int
1537 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1538     page_t *pp, dmu_tx_t *tx)
1539 {
1540 	dmu_buf_t **dbp;
1541 	int numbufs, i;
1542 	int err;
1543 
1544 	if (size == 0)
1545 		return (0);
1546 
1547 	err = dmu_buf_hold_array(os, object, offset, size,
1548 	    FALSE, FTAG, &numbufs, &dbp);
1549 	if (err)
1550 		return (err);
1551 
1552 	for (i = 0; i < numbufs; i++) {
1553 		int tocpy, copied, thiscpy;
1554 		int bufoff;
1555 		dmu_buf_t *db = dbp[i];
1556 		caddr_t va;
1557 
1558 		ASSERT(size > 0);
1559 		ASSERT3U(db->db_size, >=, PAGESIZE);
1560 
1561 		bufoff = offset - db->db_offset;
1562 		tocpy = (int)MIN(db->db_size - bufoff, size);
1563 
1564 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1565 
1566 		if (tocpy == db->db_size)
1567 			dmu_buf_will_fill(db, tx);
1568 		else
1569 			dmu_buf_will_dirty(db, tx);
1570 
1571 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
1572 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
1573 			thiscpy = MIN(PAGESIZE, tocpy - copied);
1574 			va = zfs_map_page(pp, S_READ);
1575 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
1576 			zfs_unmap_page(pp, va);
1577 			pp = pp->p_next;
1578 			bufoff += PAGESIZE;
1579 		}
1580 
1581 		if (tocpy == db->db_size)
1582 			dmu_buf_fill_done(db, tx);
1583 
1584 		offset += tocpy;
1585 		size -= tocpy;
1586 	}
1587 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1588 	return (err);
1589 }
1590 #endif
1591 
1592 /*
1593  * Allocate a loaned anonymous arc buffer.
1594  */
1595 arc_buf_t *
1596 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1597 {
1598 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1599 
1600 	return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));
1601 }
1602 
1603 /*
1604  * Free a loaned arc buffer.
1605  */
1606 void
1607 dmu_return_arcbuf(arc_buf_t *buf)
1608 {
1609 	arc_return_buf(buf, FTAG);
1610 	arc_buf_destroy(buf, FTAG);
1611 }
1612 
1613 /*
1614  * When possible directly assign passed loaned arc buffer to a dbuf.
1615  * If this is not possible copy the contents of passed arc buf via
1616  * dmu_write().
1617  */
1618 void
1619 dmu_assign_arcbuf_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,
1620     dmu_tx_t *tx)
1621 {
1622 	dmu_buf_impl_t *db;
1623 	uint32_t blksz = (uint32_t)arc_buf_lsize(buf);
1624 	uint64_t blkid;
1625 
1626 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1627 	blkid = dbuf_whichblock(dn, 0, offset);
1628 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
1629 	rw_exit(&dn->dn_struct_rwlock);
1630 
1631 	/*
1632 	 * We can only assign if the offset is aligned, the arc buf is the
1633 	 * same size as the dbuf, and the dbuf is not metadata.
1634 	 */
1635 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
1636 		dbuf_assign_arcbuf(db, buf, tx);
1637 		dbuf_rele(db, FTAG);
1638 	} else {
1639 		objset_t *os;
1640 		uint64_t object;
1641 
1642 		/* compressed bufs must always be assignable to their dbuf */
1643 		ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);
1644 		ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));
1645 
1646 		os = dn->dn_objset;
1647 		object = dn->dn_object;
1648 
1649 		dbuf_rele(db, FTAG);
1650 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
1651 		dmu_return_arcbuf(buf);
1652 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1653 	}
1654 }
1655 
1656 void
1657 dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1658     dmu_tx_t *tx)
1659 {
1660 	dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1661 
1662 	DB_DNODE_ENTER(dbuf);
1663 	dmu_assign_arcbuf_dnode(DB_DNODE(dbuf), offset, buf, tx);
1664 	DB_DNODE_EXIT(dbuf);
1665 }
1666 
1667 typedef struct {
1668 	dbuf_dirty_record_t	*dsa_dr;
1669 	dmu_sync_cb_t		*dsa_done;
1670 	zgd_t			*dsa_zgd;
1671 	dmu_tx_t		*dsa_tx;
1672 } dmu_sync_arg_t;
1673 
1674 /* ARGSUSED */
1675 static void
1676 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1677 {
1678 	dmu_sync_arg_t *dsa = varg;
1679 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1680 	blkptr_t *bp = zio->io_bp;
1681 
1682 	if (zio->io_error == 0) {
1683 		if (BP_IS_HOLE(bp)) {
1684 			/*
1685 			 * A block of zeros may compress to a hole, but the
1686 			 * block size still needs to be known for replay.
1687 			 */
1688 			BP_SET_LSIZE(bp, db->db_size);
1689 		} else if (!BP_IS_EMBEDDED(bp)) {
1690 			ASSERT(BP_GET_LEVEL(bp) == 0);
1691 			bp->blk_fill = 1;
1692 		}
1693 	}
1694 }
1695 
1696 static void
1697 dmu_sync_late_arrival_ready(zio_t *zio)
1698 {
1699 	dmu_sync_ready(zio, NULL, zio->io_private);
1700 }
1701 
1702 /* ARGSUSED */
1703 static void
1704 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1705 {
1706 	dmu_sync_arg_t *dsa = varg;
1707 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1708 	dmu_buf_impl_t *db = dr->dr_dbuf;
1709 	zgd_t *zgd = dsa->dsa_zgd;
1710 
1711 	/*
1712 	 * Record the vdev(s) backing this blkptr so they can be flushed after
1713 	 * the writes for the lwb have completed.
1714 	 */
1715 	if (zio->io_error == 0) {
1716 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1717 	}
1718 
1719 	mutex_enter(&db->db_mtx);
1720 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1721 	if (zio->io_error == 0) {
1722 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
1723 		if (dr->dt.dl.dr_nopwrite) {
1724 			blkptr_t *bp = zio->io_bp;
1725 			blkptr_t *bp_orig = &zio->io_bp_orig;
1726 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
1727 
1728 			ASSERT(BP_EQUAL(bp, bp_orig));
1729 			VERIFY(BP_EQUAL(bp, db->db_blkptr));
1730 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
1731 			ASSERT(zio_checksum_table[chksum].ci_flags &
1732 			    ZCHECKSUM_FLAG_NOPWRITE);
1733 		}
1734 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1735 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1736 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1737 
1738 		/*
1739 		 * Old style holes are filled with all zeros, whereas
1740 		 * new-style holes maintain their lsize, type, level,
1741 		 * and birth time (see zio_write_compress). While we
1742 		 * need to reset the BP_SET_LSIZE() call that happened
1743 		 * in dmu_sync_ready for old style holes, we do *not*
1744 		 * want to wipe out the information contained in new
1745 		 * style holes. Thus, only zero out the block pointer if
1746 		 * it's an old style hole.
1747 		 */
1748 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
1749 		    dr->dt.dl.dr_overridden_by.blk_birth == 0)
1750 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1751 	} else {
1752 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1753 	}
1754 	cv_broadcast(&db->db_changed);
1755 	mutex_exit(&db->db_mtx);
1756 
1757 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1758 
1759 	kmem_free(dsa, sizeof (*dsa));
1760 }
1761 
1762 static void
1763 dmu_sync_late_arrival_done(zio_t *zio)
1764 {
1765 	blkptr_t *bp = zio->io_bp;
1766 	dmu_sync_arg_t *dsa = zio->io_private;
1767 	blkptr_t *bp_orig = &zio->io_bp_orig;
1768 	zgd_t *zgd = dsa->dsa_zgd;
1769 
1770 	if (zio->io_error == 0) {
1771 		/*
1772 		 * Record the vdev(s) backing this blkptr so they can be
1773 		 * flushed after the writes for the lwb have completed.
1774 		 */
1775 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1776 
1777 		if (!BP_IS_HOLE(bp)) {
1778 			ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));
1779 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1780 			ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1781 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1782 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1783 		}
1784 	}
1785 
1786 	dmu_tx_commit(dsa->dsa_tx);
1787 
1788 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1789 
1790 	abd_put(zio->io_abd);
1791 	kmem_free(dsa, sizeof (*dsa));
1792 }
1793 
1794 static int
1795 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1796     zio_prop_t *zp, zbookmark_phys_t *zb)
1797 {
1798 	dmu_sync_arg_t *dsa;
1799 	dmu_tx_t *tx;
1800 
1801 	tx = dmu_tx_create(os);
1802 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
1803 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1804 		dmu_tx_abort(tx);
1805 		/* Make zl_get_data do txg_waited_synced() */
1806 		return (SET_ERROR(EIO));
1807 	}
1808 
1809 	/*
1810 	 * In order to prevent the zgd's lwb from being free'd prior to
1811 	 * dmu_sync_late_arrival_done() being called, we have to ensure
1812 	 * the lwb's "max txg" takes this tx's txg into account.
1813 	 */
1814 	zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));
1815 
1816 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1817 	dsa->dsa_dr = NULL;
1818 	dsa->dsa_done = done;
1819 	dsa->dsa_zgd = zgd;
1820 	dsa->dsa_tx = tx;
1821 
1822 	/*
1823 	 * Since we are currently syncing this txg, it's nontrivial to
1824 	 * determine what BP to nopwrite against, so we disable nopwrite.
1825 	 *
1826 	 * When syncing, the db_blkptr is initially the BP of the previous
1827 	 * txg.  We can not nopwrite against it because it will be changed
1828 	 * (this is similar to the non-late-arrival case where the dbuf is
1829 	 * dirty in a future txg).
1830 	 *
1831 	 * Then dbuf_write_ready() sets bp_blkptr to the location we will write.
1832 	 * We can not nopwrite against it because although the BP will not
1833 	 * (typically) be changed, the data has not yet been persisted to this
1834 	 * location.
1835 	 *
1836 	 * Finally, when dbuf_write_done() is called, it is theoretically
1837 	 * possible to always nopwrite, because the data that was written in
1838 	 * this txg is the same data that we are trying to write.  However we
1839 	 * would need to check that this dbuf is not dirty in any future
1840 	 * txg's (as we do in the normal dmu_sync() path). For simplicity, we
1841 	 * don't nopwrite in this case.
1842 	 */
1843 	zp->zp_nopwrite = B_FALSE;
1844 
1845 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1846 	    abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),
1847 	    zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,
1848 	    dmu_sync_late_arrival_ready, NULL, NULL, dmu_sync_late_arrival_done,
1849 	    dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1850 
1851 	return (0);
1852 }
1853 
1854 /*
1855  * Intent log support: sync the block associated with db to disk.
1856  * N.B. and XXX: the caller is responsible for making sure that the
1857  * data isn't changing while dmu_sync() is writing it.
1858  *
1859  * Return values:
1860  *
1861  *	EEXIST: this txg has already been synced, so there's nothing to do.
1862  *		The caller should not log the write.
1863  *
1864  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1865  *		The caller should not log the write.
1866  *
1867  *	EALREADY: this block is already in the process of being synced.
1868  *		The caller should track its progress (somehow).
1869  *
1870  *	EIO: could not do the I/O.
1871  *		The caller should do a txg_wait_synced().
1872  *
1873  *	0: the I/O has been initiated.
1874  *		The caller should log this blkptr in the done callback.
1875  *		It is possible that the I/O will fail, in which case
1876  *		the error will be reported to the done callback and
1877  *		propagated to pio from zio_done().
1878  */
1879 int
1880 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1881 {
1882 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1883 	objset_t *os = db->db_objset;
1884 	dsl_dataset_t *ds = os->os_dsl_dataset;
1885 	dbuf_dirty_record_t *dr;
1886 	dmu_sync_arg_t *dsa;
1887 	zbookmark_phys_t zb;
1888 	zio_prop_t zp;
1889 	dnode_t *dn;
1890 
1891 	ASSERT(pio != NULL);
1892 	ASSERT(txg != 0);
1893 
1894 	SET_BOOKMARK(&zb, ds->ds_object,
1895 	    db->db.db_object, db->db_level, db->db_blkid);
1896 
1897 	DB_DNODE_ENTER(db);
1898 	dn = DB_DNODE(db);
1899 	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1900 	DB_DNODE_EXIT(db);
1901 
1902 	/*
1903 	 * If we're frozen (running ziltest), we always need to generate a bp.
1904 	 */
1905 	if (txg > spa_freeze_txg(os->os_spa))
1906 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1907 
1908 	/*
1909 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1910 	 * and us.  If we determine that this txg is not yet syncing,
1911 	 * but it begins to sync a moment later, that's OK because the
1912 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1913 	 */
1914 	mutex_enter(&db->db_mtx);
1915 
1916 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1917 		/*
1918 		 * This txg has already synced.  There's nothing to do.
1919 		 */
1920 		mutex_exit(&db->db_mtx);
1921 		return (SET_ERROR(EEXIST));
1922 	}
1923 
1924 	if (txg <= spa_syncing_txg(os->os_spa)) {
1925 		/*
1926 		 * This txg is currently syncing, so we can't mess with
1927 		 * the dirty record anymore; just write a new log block.
1928 		 */
1929 		mutex_exit(&db->db_mtx);
1930 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1931 	}
1932 
1933 	dr = db->db_last_dirty;
1934 	while (dr && dr->dr_txg != txg)
1935 		dr = dr->dr_next;
1936 
1937 	if (dr == NULL) {
1938 		/*
1939 		 * There's no dr for this dbuf, so it must have been freed.
1940 		 * There's no need to log writes to freed blocks, so we're done.
1941 		 */
1942 		mutex_exit(&db->db_mtx);
1943 		return (SET_ERROR(ENOENT));
1944 	}
1945 
1946 	ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg);
1947 
1948 	if (db->db_blkptr != NULL) {
1949 		/*
1950 		 * We need to fill in zgd_bp with the current blkptr so that
1951 		 * the nopwrite code can check if we're writing the same
1952 		 * data that's already on disk.  We can only nopwrite if we
1953 		 * are sure that after making the copy, db_blkptr will not
1954 		 * change until our i/o completes.  We ensure this by
1955 		 * holding the db_mtx, and only allowing nopwrite if the
1956 		 * block is not already dirty (see below).  This is verified
1957 		 * by dmu_sync_done(), which VERIFYs that the db_blkptr has
1958 		 * not changed.
1959 		 */
1960 		*zgd->zgd_bp = *db->db_blkptr;
1961 	}
1962 
1963 	/*
1964 	 * Assume the on-disk data is X, the current syncing data (in
1965 	 * txg - 1) is Y, and the current in-memory data is Z (currently
1966 	 * in dmu_sync).
1967 	 *
1968 	 * We usually want to perform a nopwrite if X and Z are the
1969 	 * same.  However, if Y is different (i.e. the BP is going to
1970 	 * change before this write takes effect), then a nopwrite will
1971 	 * be incorrect - we would override with X, which could have
1972 	 * been freed when Y was written.
1973 	 *
1974 	 * (Note that this is not a concern when we are nop-writing from
1975 	 * syncing context, because X and Y must be identical, because
1976 	 * all previous txgs have been synced.)
1977 	 *
1978 	 * Therefore, we disable nopwrite if the current BP could change
1979 	 * before this TXG.  There are two ways it could change: by
1980 	 * being dirty (dr_next is non-NULL), or by being freed
1981 	 * (dnode_block_freed()).  This behavior is verified by
1982 	 * zio_done(), which VERIFYs that the override BP is identical
1983 	 * to the on-disk BP.
1984 	 */
1985 	DB_DNODE_ENTER(db);
1986 	dn = DB_DNODE(db);
1987 	if (dr->dr_next != NULL || dnode_block_freed(dn, db->db_blkid))
1988 		zp.zp_nopwrite = B_FALSE;
1989 	DB_DNODE_EXIT(db);
1990 
1991 	ASSERT(dr->dr_txg == txg);
1992 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1993 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1994 		/*
1995 		 * We have already issued a sync write for this buffer,
1996 		 * or this buffer has already been synced.  It could not
1997 		 * have been dirtied since, or we would have cleared the state.
1998 		 */
1999 		mutex_exit(&db->db_mtx);
2000 		return (SET_ERROR(EALREADY));
2001 	}
2002 
2003 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2004 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
2005 	mutex_exit(&db->db_mtx);
2006 
2007 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2008 	dsa->dsa_dr = dr;
2009 	dsa->dsa_done = done;
2010 	dsa->dsa_zgd = zgd;
2011 	dsa->dsa_tx = NULL;
2012 
2013 	zio_nowait(arc_write(pio, os->os_spa, txg,
2014 	    zgd->zgd_bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db),
2015 	    &zp, dmu_sync_ready, NULL, NULL, dmu_sync_done, dsa,
2016 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb));
2017 
2018 	return (0);
2019 }
2020 
2021 int
2022 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
2023     dmu_tx_t *tx)
2024 {
2025 	dnode_t *dn;
2026 	int err;
2027 
2028 	err = dnode_hold(os, object, FTAG, &dn);
2029 	if (err)
2030 		return (err);
2031 	err = dnode_set_blksz(dn, size, ibs, tx);
2032 	dnode_rele(dn, FTAG);
2033 	return (err);
2034 }
2035 
2036 void
2037 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
2038     dmu_tx_t *tx)
2039 {
2040 	dnode_t *dn;
2041 
2042 	/*
2043 	 * Send streams include each object's checksum function.  This
2044 	 * check ensures that the receiving system can understand the
2045 	 * checksum function transmitted.
2046 	 */
2047 	ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
2048 
2049 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2050 	ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
2051 	dn->dn_checksum = checksum;
2052 	dnode_setdirty(dn, tx);
2053 	dnode_rele(dn, FTAG);
2054 }
2055 
2056 void
2057 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
2058     dmu_tx_t *tx)
2059 {
2060 	dnode_t *dn;
2061 
2062 	/*
2063 	 * Send streams include each object's compression function.  This
2064 	 * check ensures that the receiving system can understand the
2065 	 * compression function transmitted.
2066 	 */
2067 	ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
2068 
2069 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
2070 	dn->dn_compress = compress;
2071 	dnode_setdirty(dn, tx);
2072 	dnode_rele(dn, FTAG);
2073 }
2074 
2075 int zfs_mdcomp_disable = 0;
2076 
2077 /*
2078  * When the "redundant_metadata" property is set to "most", only indirect
2079  * blocks of this level and higher will have an additional ditto block.
2080  */
2081 int zfs_redundant_metadata_most_ditto_level = 2;
2082 
2083 void
2084 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
2085 {
2086 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
2087 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
2088 	    (wp & WP_SPILL));
2089 	enum zio_checksum checksum = os->os_checksum;
2090 	enum zio_compress compress = os->os_compress;
2091 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
2092 	boolean_t dedup = B_FALSE;
2093 	boolean_t nopwrite = B_FALSE;
2094 	boolean_t dedup_verify = os->os_dedup_verify;
2095 	int copies = os->os_copies;
2096 
2097 	/*
2098 	 * We maintain different write policies for each of the following
2099 	 * types of data:
2100 	 *	 1. metadata
2101 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
2102 	 *	 3. all other level 0 blocks
2103 	 */
2104 	if (ismd) {
2105 		if (zfs_mdcomp_disable) {
2106 			compress = ZIO_COMPRESS_EMPTY;
2107 		} else {
2108 			/*
2109 			 * XXX -- we should design a compression algorithm
2110 			 * that specializes in arrays of bps.
2111 			 */
2112 			compress = zio_compress_select(os->os_spa,
2113 			    ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
2114 		}
2115 
2116 		/*
2117 		 * Metadata always gets checksummed.  If the data
2118 		 * checksum is multi-bit correctable, and it's not a
2119 		 * ZBT-style checksum, then it's suitable for metadata
2120 		 * as well.  Otherwise, the metadata checksum defaults
2121 		 * to fletcher4.
2122 		 */
2123 		if (!(zio_checksum_table[checksum].ci_flags &
2124 		    ZCHECKSUM_FLAG_METADATA) ||
2125 		    (zio_checksum_table[checksum].ci_flags &
2126 		    ZCHECKSUM_FLAG_EMBEDDED))
2127 			checksum = ZIO_CHECKSUM_FLETCHER_4;
2128 
2129 		if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||
2130 		    (os->os_redundant_metadata ==
2131 		    ZFS_REDUNDANT_METADATA_MOST &&
2132 		    (level >= zfs_redundant_metadata_most_ditto_level ||
2133 		    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))))
2134 			copies++;
2135 	} else if (wp & WP_NOFILL) {
2136 		ASSERT(level == 0);
2137 
2138 		/*
2139 		 * If we're writing preallocated blocks, we aren't actually
2140 		 * writing them so don't set any policy properties.  These
2141 		 * blocks are currently only used by an external subsystem
2142 		 * outside of zfs (i.e. dump) and not written by the zio
2143 		 * pipeline.
2144 		 */
2145 		compress = ZIO_COMPRESS_OFF;
2146 		checksum = ZIO_CHECKSUM_NOPARITY;
2147 	} else {
2148 		compress = zio_compress_select(os->os_spa, dn->dn_compress,
2149 		    compress);
2150 
2151 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
2152 		    zio_checksum_select(dn->dn_checksum, checksum) :
2153 		    dedup_checksum;
2154 
2155 		/*
2156 		 * Determine dedup setting.  If we are in dmu_sync(),
2157 		 * we won't actually dedup now because that's all
2158 		 * done in syncing context; but we do want to use the
2159 		 * dedup checkum.  If the checksum is not strong
2160 		 * enough to ensure unique signatures, force
2161 		 * dedup_verify.
2162 		 */
2163 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
2164 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
2165 			if (!(zio_checksum_table[checksum].ci_flags &
2166 			    ZCHECKSUM_FLAG_DEDUP))
2167 				dedup_verify = B_TRUE;
2168 		}
2169 
2170 		/*
2171 		 * Enable nopwrite if we have secure enough checksum
2172 		 * algorithm (see comment in zio_nop_write) and
2173 		 * compression is enabled.  We don't enable nopwrite if
2174 		 * dedup is enabled as the two features are mutually
2175 		 * exclusive.
2176 		 */
2177 		nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
2178 		    ZCHECKSUM_FLAG_NOPWRITE) &&
2179 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
2180 	}
2181 
2182 	zp->zp_checksum = checksum;
2183 	zp->zp_compress = compress;
2184 	ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);
2185 
2186 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
2187 	zp->zp_level = level;
2188 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
2189 	zp->zp_dedup = dedup;
2190 	zp->zp_dedup_verify = dedup && dedup_verify;
2191 	zp->zp_nopwrite = nopwrite;
2192 }
2193 
2194 int
2195 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
2196 {
2197 	dnode_t *dn;
2198 	int err;
2199 
2200 	/*
2201 	 * Sync any current changes before
2202 	 * we go trundling through the block pointers.
2203 	 */
2204 	err = dmu_object_wait_synced(os, object);
2205 	if (err) {
2206 		return (err);
2207 	}
2208 
2209 	err = dnode_hold(os, object, FTAG, &dn);
2210 	if (err) {
2211 		return (err);
2212 	}
2213 
2214 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
2215 	dnode_rele(dn, FTAG);
2216 
2217 	return (err);
2218 }
2219 
2220 /*
2221  * Given the ZFS object, if it contains any dirty nodes
2222  * this function flushes all dirty blocks to disk. This
2223  * ensures the DMU object info is updated. A more efficient
2224  * future version might just find the TXG with the maximum
2225  * ID and wait for that to be synced.
2226  */
2227 int
2228 dmu_object_wait_synced(objset_t *os, uint64_t object)
2229 {
2230 	dnode_t *dn;
2231 	int error, i;
2232 
2233 	error = dnode_hold(os, object, FTAG, &dn);
2234 	if (error) {
2235 		return (error);
2236 	}
2237 
2238 	for (i = 0; i < TXG_SIZE; i++) {
2239 		if (list_link_active(&dn->dn_dirty_link[i])) {
2240 			break;
2241 		}
2242 	}
2243 	dnode_rele(dn, FTAG);
2244 	if (i != TXG_SIZE) {
2245 		txg_wait_synced(dmu_objset_pool(os), 0);
2246 	}
2247 
2248 	return (0);
2249 }
2250 
2251 void
2252 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2253 {
2254 	dnode_phys_t *dnp;
2255 
2256 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
2257 	mutex_enter(&dn->dn_mtx);
2258 
2259 	dnp = dn->dn_phys;
2260 
2261 	doi->doi_data_block_size = dn->dn_datablksz;
2262 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
2263 	    1ULL << dn->dn_indblkshift : 0;
2264 	doi->doi_type = dn->dn_type;
2265 	doi->doi_bonus_type = dn->dn_bonustype;
2266 	doi->doi_bonus_size = dn->dn_bonuslen;
2267 	doi->doi_indirection = dn->dn_nlevels;
2268 	doi->doi_checksum = dn->dn_checksum;
2269 	doi->doi_compress = dn->dn_compress;
2270 	doi->doi_nblkptr = dn->dn_nblkptr;
2271 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
2272 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
2273 	doi->doi_fill_count = 0;
2274 	for (int i = 0; i < dnp->dn_nblkptr; i++)
2275 		doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
2276 
2277 	mutex_exit(&dn->dn_mtx);
2278 	rw_exit(&dn->dn_struct_rwlock);
2279 }
2280 
2281 /*
2282  * Get information on a DMU object.
2283  * If doi is NULL, just indicates whether the object exists.
2284  */
2285 int
2286 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
2287 {
2288 	dnode_t *dn;
2289 	int err = dnode_hold(os, object, FTAG, &dn);
2290 
2291 	if (err)
2292 		return (err);
2293 
2294 	if (doi != NULL)
2295 		dmu_object_info_from_dnode(dn, doi);
2296 
2297 	dnode_rele(dn, FTAG);
2298 	return (0);
2299 }
2300 
2301 /*
2302  * As above, but faster; can be used when you have a held dbuf in hand.
2303  */
2304 void
2305 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
2306 {
2307 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2308 
2309 	DB_DNODE_ENTER(db);
2310 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
2311 	DB_DNODE_EXIT(db);
2312 }
2313 
2314 /*
2315  * Faster still when you only care about the size.
2316  * This is specifically optimized for zfs_getattr().
2317  */
2318 void
2319 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
2320     u_longlong_t *nblk512)
2321 {
2322 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2323 	dnode_t *dn;
2324 
2325 	DB_DNODE_ENTER(db);
2326 	dn = DB_DNODE(db);
2327 
2328 	*blksize = dn->dn_datablksz;
2329 	/* add 1 for dnode space */
2330 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
2331 	    SPA_MINBLOCKSHIFT) + 1;
2332 	DB_DNODE_EXIT(db);
2333 }
2334 
2335 void
2336 byteswap_uint64_array(void *vbuf, size_t size)
2337 {
2338 	uint64_t *buf = vbuf;
2339 	size_t count = size >> 3;
2340 	int i;
2341 
2342 	ASSERT((size & 7) == 0);
2343 
2344 	for (i = 0; i < count; i++)
2345 		buf[i] = BSWAP_64(buf[i]);
2346 }
2347 
2348 void
2349 byteswap_uint32_array(void *vbuf, size_t size)
2350 {
2351 	uint32_t *buf = vbuf;
2352 	size_t count = size >> 2;
2353 	int i;
2354 
2355 	ASSERT((size & 3) == 0);
2356 
2357 	for (i = 0; i < count; i++)
2358 		buf[i] = BSWAP_32(buf[i]);
2359 }
2360 
2361 void
2362 byteswap_uint16_array(void *vbuf, size_t size)
2363 {
2364 	uint16_t *buf = vbuf;
2365 	size_t count = size >> 1;
2366 	int i;
2367 
2368 	ASSERT((size & 1) == 0);
2369 
2370 	for (i = 0; i < count; i++)
2371 		buf[i] = BSWAP_16(buf[i]);
2372 }
2373 
2374 /* ARGSUSED */
2375 void
2376 byteswap_uint8_array(void *vbuf, size_t size)
2377 {
2378 }
2379 
2380 void
2381 dmu_init(void)
2382 {
2383 	abd_init();
2384 	zfs_dbgmsg_init();
2385 	sa_cache_init();
2386 	xuio_stat_init();
2387 	dmu_objset_init();
2388 	dnode_init();
2389 	zfetch_init();
2390 	l2arc_init();
2391 	arc_init();
2392 	dbuf_init();
2393 }
2394 
2395 void
2396 dmu_fini(void)
2397 {
2398 	arc_fini(); /* arc depends on l2arc, so arc must go first */
2399 	l2arc_fini();
2400 	zfetch_fini();
2401 	dbuf_fini();
2402 	dnode_fini();
2403 	dmu_objset_fini();
2404 	xuio_stat_fini();
2405 	sa_cache_fini();
2406 	zfs_dbgmsg_fini();
2407 	abd_fini();
2408 }
2409