xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision 9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2294d1a210STim Haley  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23cf6106c8SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25aad02571SSaso Kiselkov /* Copyright (c) 2013 by Saso Kiselkov. All rights reserved. */
26810e43b2SBill Pijewski /* Copyright (c) 2013, Joyent, Inc. All rights reserved. */
27b8289d24SDaniil Lunev /* Copyright (c) 2014, Nexenta Systems, Inc. All rights reserved. */
28aad02571SSaso Kiselkov 
29fa9e4066Sahrens #include <sys/dmu.h>
30fa9e4066Sahrens #include <sys/dmu_impl.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/dbuf.h>
33fa9e4066Sahrens #include <sys/dnode.h>
34fa9e4066Sahrens #include <sys/zfs_context.h>
35fa9e4066Sahrens #include <sys/dmu_objset.h>
36fa9e4066Sahrens #include <sys/dmu_traverse.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/dsl_pool.h>
401d452cf5Sahrens #include <sys/dsl_synctask.h>
41a2eea2e1Sahrens #include <sys/dsl_prop.h>
42fa9e4066Sahrens #include <sys/dmu_zfetch.h>
43fa9e4066Sahrens #include <sys/zfs_ioctl.h>
44fa9e4066Sahrens #include <sys/zap.h>
45ea8dc4b6Seschrock #include <sys/zio_checksum.h>
4680901aeaSGeorge Wilson #include <sys/zio_compress.h>
470a586ceaSMark Shellenbaum #include <sys/sa.h>
48b8289d24SDaniil Lunev #include <sys/zfeature.h>
4944eda4d7Smaybee #ifdef _KERNEL
5044eda4d7Smaybee #include <sys/vmsystm.h>
510fab61baSJonathan W Adams #include <sys/zfs_znode.h>
5244eda4d7Smaybee #endif
53fa9e4066Sahrens 
5480901aeaSGeorge Wilson /*
5580901aeaSGeorge Wilson  * Enable/disable nopwrite feature.
5680901aeaSGeorge Wilson  */
5780901aeaSGeorge Wilson int zfs_nopwrite_enabled = 1;
5880901aeaSGeorge Wilson 
59fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
60ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"unallocated"		},
61ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"object directory"	},
62ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"object array"		},
63ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"packed nvlist"		},
64ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"packed nvlist size"	},
65ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj"			},
66ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj header"		},
67ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map header"	},
68ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map"		},
69ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"ZIL intent log"	},
70ad135b5dSChristopher Siden 	{	DMU_BSWAP_DNODE,	TRUE,	"DMU dnode"		},
71ad135b5dSChristopher Siden 	{	DMU_BSWAP_OBJSET,	TRUE,	"DMU objset"		},
72ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL directory"		},
73ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL directory child map"},
74ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset snap map"	},
75ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL props"		},
76ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL dataset"		},
77ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZNODE,	TRUE,	"ZFS znode"		},
78ad135b5dSChristopher Siden 	{	DMU_BSWAP_OLDACL,	TRUE,	"ZFS V0 ACL"		},
79ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"ZFS plain file"	},
80ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS directory"		},
81ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS master node"	},
82ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS delete queue"	},
83ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"zvol object"		},
84ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"zvol prop"		},
85ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"other uint8[]"		},
86ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	FALSE,	"other uint64[]"	},
87ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"other ZAP"		},
88ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"persistent error log"	},
89ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"SPA history"		},
90ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA history offsets"	},
91ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"Pool properties"	},
92ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL permissions"	},
93ad135b5dSChristopher Siden 	{	DMU_BSWAP_ACL,		TRUE,	"ZFS ACL"		},
94ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"ZFS SYSACL"		},
95ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"FUID table"		},
96ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"FUID table size"	},
97ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset next clones"},
98ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan work queue"	},
99ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group used"	},
100ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group quota"	},
101ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"snapshot refcount tags"},
102ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT ZAP algorithm"	},
103ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT statistics"	},
104ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"System attributes"	},
105ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA master node"	},
106ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr registration"	},
107ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr layouts"	},
108ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan translations"	},
109ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"deduplicated block"	},
110ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL deadlist map"	},
111ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL deadlist map hdr"	},
112ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dir clones"	},
113ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj subobj"		}
114ad135b5dSChristopher Siden };
115ad135b5dSChristopher Siden 
116ad135b5dSChristopher Siden const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
117ad135b5dSChristopher Siden 	{	byteswap_uint8_array,	"uint8"		},
118ad135b5dSChristopher Siden 	{	byteswap_uint16_array,	"uint16"	},
119ad135b5dSChristopher Siden 	{	byteswap_uint32_array,	"uint32"	},
120ad135b5dSChristopher Siden 	{	byteswap_uint64_array,	"uint64"	},
121ad135b5dSChristopher Siden 	{	zap_byteswap,		"zap"		},
122ad135b5dSChristopher Siden 	{	dnode_buf_byteswap,	"dnode"		},
123ad135b5dSChristopher Siden 	{	dmu_objset_byteswap,	"objset"	},
124ad135b5dSChristopher Siden 	{	zfs_znode_byteswap,	"znode"		},
125ad135b5dSChristopher Siden 	{	zfs_oldacl_byteswap,	"oldacl"	},
126ad135b5dSChristopher Siden 	{	zfs_acl_byteswap,	"acl"		}
1273f9d6ad7SLin Ling };
128fa9e4066Sahrens 
129fa9e4066Sahrens int
1305d7b4d43SMatthew Ahrens dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,
1315d7b4d43SMatthew Ahrens     void *tag, dmu_buf_t **dbp)
132fa9e4066Sahrens {
133fa9e4066Sahrens 	dnode_t *dn;
134fa9e4066Sahrens 	uint64_t blkid;
135fa9e4066Sahrens 	dmu_buf_impl_t *db;
136ea8dc4b6Seschrock 	int err;
137fa9e4066Sahrens 
138503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
139ea8dc4b6Seschrock 	if (err)
140ea8dc4b6Seschrock 		return (err);
141a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
142fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
143ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
144fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1455d7b4d43SMatthew Ahrens 	dnode_rele(dn, FTAG);
1465d7b4d43SMatthew Ahrens 
147ea8dc4b6Seschrock 	if (db == NULL) {
1485d7b4d43SMatthew Ahrens 		*dbp = NULL;
1495d7b4d43SMatthew Ahrens 		return (SET_ERROR(EIO));
1505d7b4d43SMatthew Ahrens 	}
1515d7b4d43SMatthew Ahrens 
1525d7b4d43SMatthew Ahrens 	*dbp = &db->db;
1535d7b4d43SMatthew Ahrens 	return (err);
1545d7b4d43SMatthew Ahrens }
1555d7b4d43SMatthew Ahrens 
1565d7b4d43SMatthew Ahrens int
1575d7b4d43SMatthew Ahrens dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
1585d7b4d43SMatthew Ahrens     void *tag, dmu_buf_t **dbp, int flags)
1595d7b4d43SMatthew Ahrens {
1605d7b4d43SMatthew Ahrens 	int err;
1615d7b4d43SMatthew Ahrens 	int db_flags = DB_RF_CANFAIL;
1625d7b4d43SMatthew Ahrens 
1635d7b4d43SMatthew Ahrens 	if (flags & DMU_READ_NO_PREFETCH)
1645d7b4d43SMatthew Ahrens 		db_flags |= DB_RF_NOPREFETCH;
1655d7b4d43SMatthew Ahrens 
1665d7b4d43SMatthew Ahrens 	err = dmu_buf_hold_noread(os, object, offset, tag, dbp);
1675d7b4d43SMatthew Ahrens 	if (err == 0) {
1685d7b4d43SMatthew Ahrens 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
16947cb52daSJeff Bonwick 		err = dbuf_read(db, NULL, db_flags);
1705d7b4d43SMatthew Ahrens 		if (err != 0) {
171ea8dc4b6Seschrock 			dbuf_rele(db, tag);
1725d7b4d43SMatthew Ahrens 			*dbp = NULL;
173ea8dc4b6Seschrock 		}
174fa9e4066Sahrens 	}
175fa9e4066Sahrens 
176ea8dc4b6Seschrock 	return (err);
177fa9e4066Sahrens }
178fa9e4066Sahrens 
179fa9e4066Sahrens int
180fa9e4066Sahrens dmu_bonus_max(void)
181fa9e4066Sahrens {
182fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
183fa9e4066Sahrens }
184fa9e4066Sahrens 
1851934e92fSmaybee int
186744947dcSTom Erickson dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
1871934e92fSmaybee {
188744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
189744947dcSTom Erickson 	dnode_t *dn;
190744947dcSTom Erickson 	int error;
1911934e92fSmaybee 
192744947dcSTom Erickson 	DB_DNODE_ENTER(db);
193744947dcSTom Erickson 	dn = DB_DNODE(db);
194744947dcSTom Erickson 
195744947dcSTom Erickson 	if (dn->dn_bonus != db) {
196be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
197744947dcSTom Erickson 	} else if (newsize < 0 || newsize > db_fake->db_size) {
198be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
199744947dcSTom Erickson 	} else {
2001934e92fSmaybee 		dnode_setbonuslen(dn, newsize, tx);
201744947dcSTom Erickson 		error = 0;
202744947dcSTom Erickson 	}
203744947dcSTom Erickson 
204744947dcSTom Erickson 	DB_DNODE_EXIT(db);
205744947dcSTom Erickson 	return (error);
2061934e92fSmaybee }
2071934e92fSmaybee 
2080a586ceaSMark Shellenbaum int
209744947dcSTom Erickson dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
2100a586ceaSMark Shellenbaum {
211744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
212744947dcSTom Erickson 	dnode_t *dn;
213744947dcSTom Erickson 	int error;
2140a586ceaSMark Shellenbaum 
215744947dcSTom Erickson 	DB_DNODE_ENTER(db);
216744947dcSTom Erickson 	dn = DB_DNODE(db);
2170a586ceaSMark Shellenbaum 
218ad135b5dSChristopher Siden 	if (!DMU_OT_IS_VALID(type)) {
219be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
220744947dcSTom Erickson 	} else if (dn->dn_bonus != db) {
221be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
222744947dcSTom Erickson 	} else {
2230a586ceaSMark Shellenbaum 		dnode_setbonus_type(dn, type, tx);
224744947dcSTom Erickson 		error = 0;
225744947dcSTom Erickson 	}
226744947dcSTom Erickson 
227744947dcSTom Erickson 	DB_DNODE_EXIT(db);
228744947dcSTom Erickson 	return (error);
229744947dcSTom Erickson }
230744947dcSTom Erickson 
231744947dcSTom Erickson dmu_object_type_t
232744947dcSTom Erickson dmu_get_bonustype(dmu_buf_t *db_fake)
233744947dcSTom Erickson {
234744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
235744947dcSTom Erickson 	dnode_t *dn;
236744947dcSTom Erickson 	dmu_object_type_t type;
237744947dcSTom Erickson 
238744947dcSTom Erickson 	DB_DNODE_ENTER(db);
239744947dcSTom Erickson 	dn = DB_DNODE(db);
240744947dcSTom Erickson 	type = dn->dn_bonustype;
241744947dcSTom Erickson 	DB_DNODE_EXIT(db);
242744947dcSTom Erickson 
243744947dcSTom Erickson 	return (type);
2440a586ceaSMark Shellenbaum }
2450a586ceaSMark Shellenbaum 
2460a586ceaSMark Shellenbaum int
2470a586ceaSMark Shellenbaum dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
2480a586ceaSMark Shellenbaum {
2490a586ceaSMark Shellenbaum 	dnode_t *dn;
2500a586ceaSMark Shellenbaum 	int error;
2510a586ceaSMark Shellenbaum 
2520a586ceaSMark Shellenbaum 	error = dnode_hold(os, object, FTAG, &dn);
2530a586ceaSMark Shellenbaum 	dbuf_rm_spill(dn, tx);
25406e0070dSMark Shellenbaum 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
25506e0070dSMark Shellenbaum 	dnode_rm_spill(dn, tx);
25606e0070dSMark Shellenbaum 	rw_exit(&dn->dn_struct_rwlock);
2570a586ceaSMark Shellenbaum 	dnode_rele(dn, FTAG);
2580a586ceaSMark Shellenbaum 	return (error);
2590a586ceaSMark Shellenbaum }
2600a586ceaSMark Shellenbaum 
261fa9e4066Sahrens /*
262ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
263fa9e4066Sahrens  */
264ea8dc4b6Seschrock int
265ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
266fa9e4066Sahrens {
267ea8dc4b6Seschrock 	dnode_t *dn;
268fa9e4066Sahrens 	dmu_buf_impl_t *db;
2691934e92fSmaybee 	int error;
270fa9e4066Sahrens 
271503ad85cSMatthew Ahrens 	error = dnode_hold(os, object, FTAG, &dn);
2721934e92fSmaybee 	if (error)
2731934e92fSmaybee 		return (error);
274fa9e4066Sahrens 
275fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
276ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
277fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
278ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
279ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
2801934e92fSmaybee 			dbuf_create_bonus(dn);
281fa9e4066Sahrens 	}
282ea8dc4b6Seschrock 	db = dn->dn_bonus;
2831934e92fSmaybee 
2841934e92fSmaybee 	/* as long as the bonus buf is held, the dnode will be held */
285744947dcSTom Erickson 	if (refcount_add(&db->db_holds, tag) == 1) {
2861934e92fSmaybee 		VERIFY(dnode_add_ref(dn, db));
287640c1670SJosef 'Jeff' Sipek 		atomic_inc_32(&dn->dn_dbufs_count);
288744947dcSTom Erickson 	}
289744947dcSTom Erickson 
290744947dcSTom Erickson 	/*
291744947dcSTom Erickson 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
292744947dcSTom Erickson 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
293744947dcSTom Erickson 	 * a dnode hold for every dbuf.
294744947dcSTom Erickson 	 */
295744947dcSTom Erickson 	rw_exit(&dn->dn_struct_rwlock);
2961934e92fSmaybee 
297fa9e4066Sahrens 	dnode_rele(dn, FTAG);
298ea8dc4b6Seschrock 
29947cb52daSJeff Bonwick 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
300ea8dc4b6Seschrock 
301ea8dc4b6Seschrock 	*dbp = &db->db;
302ea8dc4b6Seschrock 	return (0);
303fa9e4066Sahrens }
304fa9e4066Sahrens 
30513506d1eSmaybee /*
3060a586ceaSMark Shellenbaum  * returns ENOENT, EIO, or 0.
3070a586ceaSMark Shellenbaum  *
3080a586ceaSMark Shellenbaum  * This interface will allocate a blank spill dbuf when a spill blk
3090a586ceaSMark Shellenbaum  * doesn't already exist on the dnode.
3100a586ceaSMark Shellenbaum  *
3110a586ceaSMark Shellenbaum  * if you only want to find an already existing spill db, then
3120a586ceaSMark Shellenbaum  * dmu_spill_hold_existing() should be used.
3130a586ceaSMark Shellenbaum  */
3140a586ceaSMark Shellenbaum int
3150a586ceaSMark Shellenbaum dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
3160a586ceaSMark Shellenbaum {
3170a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
3180a586ceaSMark Shellenbaum 	int err;
3190a586ceaSMark Shellenbaum 
3200a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3210a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3220a586ceaSMark Shellenbaum 
3230a586ceaSMark Shellenbaum 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
3240a586ceaSMark Shellenbaum 
3250a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3260a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
3270a586ceaSMark Shellenbaum 
3280a586ceaSMark Shellenbaum 	ASSERT(db != NULL);
3291d8ccc7bSMark Shellenbaum 	err = dbuf_read(db, NULL, flags);
3301d8ccc7bSMark Shellenbaum 	if (err == 0)
3310a586ceaSMark Shellenbaum 		*dbp = &db->db;
3321d8ccc7bSMark Shellenbaum 	else
3331d8ccc7bSMark Shellenbaum 		dbuf_rele(db, tag);
3340a586ceaSMark Shellenbaum 	return (err);
3350a586ceaSMark Shellenbaum }
3360a586ceaSMark Shellenbaum 
3370a586ceaSMark Shellenbaum int
3380a586ceaSMark Shellenbaum dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3390a586ceaSMark Shellenbaum {
340744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
341744947dcSTom Erickson 	dnode_t *dn;
3420a586ceaSMark Shellenbaum 	int err;
3430a586ceaSMark Shellenbaum 
344744947dcSTom Erickson 	DB_DNODE_ENTER(db);
345744947dcSTom Erickson 	dn = DB_DNODE(db);
346744947dcSTom Erickson 
347744947dcSTom Erickson 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
348be6fd75aSMatthew Ahrens 		err = SET_ERROR(EINVAL);
349744947dcSTom Erickson 	} else {
3500a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3510a586ceaSMark Shellenbaum 
3520a586ceaSMark Shellenbaum 		if (!dn->dn_have_spill) {
353be6fd75aSMatthew Ahrens 			err = SET_ERROR(ENOENT);
354744947dcSTom Erickson 		} else {
3551d8ccc7bSMark Shellenbaum 			err = dmu_spill_hold_by_dnode(dn,
3561d8ccc7bSMark Shellenbaum 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
357744947dcSTom Erickson 		}
358744947dcSTom Erickson 
3590a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
360744947dcSTom Erickson 	}
361744947dcSTom Erickson 
362744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3630a586ceaSMark Shellenbaum 	return (err);
3640a586ceaSMark Shellenbaum }
3650a586ceaSMark Shellenbaum 
3660a586ceaSMark Shellenbaum int
3670a586ceaSMark Shellenbaum dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3680a586ceaSMark Shellenbaum {
369744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
370744947dcSTom Erickson 	dnode_t *dn;
371744947dcSTom Erickson 	int err;
372744947dcSTom Erickson 
373744947dcSTom Erickson 	DB_DNODE_ENTER(db);
374744947dcSTom Erickson 	dn = DB_DNODE(db);
375744947dcSTom Erickson 	err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
376744947dcSTom Erickson 	DB_DNODE_EXIT(db);
377744947dcSTom Erickson 
378744947dcSTom Erickson 	return (err);
3790a586ceaSMark Shellenbaum }
3800a586ceaSMark Shellenbaum 
3810a586ceaSMark Shellenbaum /*
38213506d1eSmaybee  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
38313506d1eSmaybee  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
38413506d1eSmaybee  * and can induce severe lock contention when writing to several files
38513506d1eSmaybee  * whose dnodes are in the same block.
38613506d1eSmaybee  */
38713506d1eSmaybee static int
3887bfdf011SNeil Perrin dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
389cf6106c8SMatthew Ahrens     boolean_t read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
390fa9e4066Sahrens {
391fa9e4066Sahrens 	dmu_buf_t **dbp;
392fa9e4066Sahrens 	uint64_t blkid, nblks, i;
3937bfdf011SNeil Perrin 	uint32_t dbuf_flags;
394ea8dc4b6Seschrock 	int err;
395ea8dc4b6Seschrock 	zio_t *zio;
396ea8dc4b6Seschrock 
397ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
398fa9e4066Sahrens 
399cf6106c8SMatthew Ahrens 	/*
400cf6106c8SMatthew Ahrens 	 * Note: We directly notify the prefetch code of this read, so that
401cf6106c8SMatthew Ahrens 	 * we can tell it about the multi-block read.  dbuf_read() only knows
402cf6106c8SMatthew Ahrens 	 * about the one block it is accessing.
403cf6106c8SMatthew Ahrens 	 */
404cf6106c8SMatthew Ahrens 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT |
405cf6106c8SMatthew Ahrens 	    DB_RF_NOPREFETCH;
406ea8dc4b6Seschrock 
407fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
408fa9e4066Sahrens 	if (dn->dn_datablkshift) {
409fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
410fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
411fa9e4066Sahrens 		    P2ALIGN(offset, 1ULL << blkshift)) >> blkshift;
412fa9e4066Sahrens 	} else {
4130125049cSahrens 		if (offset + length > dn->dn_datablksz) {
4140125049cSahrens 			zfs_panic_recover("zfs: accessing past end of object "
4150125049cSahrens 			    "%llx/%llx (size=%u access=%llu+%llu)",
4160125049cSahrens 			    (longlong_t)dn->dn_objset->
4170125049cSahrens 			    os_dsl_dataset->ds_object,
4180125049cSahrens 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
4190125049cSahrens 			    (longlong_t)offset, (longlong_t)length);
420c87b8fc5SMark J Musante 			rw_exit(&dn->dn_struct_rwlock);
421be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
4220125049cSahrens 		}
423fa9e4066Sahrens 		nblks = 1;
424fa9e4066Sahrens 	}
425ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
426fa9e4066Sahrens 
427e14bb325SJeff Bonwick 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
428a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
429fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
430ea8dc4b6Seschrock 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);
431ea8dc4b6Seschrock 		if (db == NULL) {
432ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
433ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
434ea8dc4b6Seschrock 			zio_nowait(zio);
435be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
436ea8dc4b6Seschrock 		}
437cf6106c8SMatthew Ahrens 
438ea8dc4b6Seschrock 		/* initiate async i/o */
439cf6106c8SMatthew Ahrens 		if (read)
4407bfdf011SNeil Perrin 			(void) dbuf_read(db, zio, dbuf_flags);
441ea8dc4b6Seschrock 		dbp[i] = &db->db;
442fa9e4066Sahrens 	}
443cf6106c8SMatthew Ahrens 
444cf6106c8SMatthew Ahrens 	if ((flags & DMU_READ_NO_PREFETCH) == 0 && read &&
44563280274SGeorge Wilson 	    length <= zfetch_array_rd_sz) {
446cf6106c8SMatthew Ahrens 		dmu_zfetch(&dn->dn_zfetch, blkid, nblks);
447cf6106c8SMatthew Ahrens 	}
448fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
449fa9e4066Sahrens 
450ea8dc4b6Seschrock 	/* wait for async i/o */
451ea8dc4b6Seschrock 	err = zio_wait(zio);
452ea8dc4b6Seschrock 	if (err) {
453ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
454ea8dc4b6Seschrock 		return (err);
455ea8dc4b6Seschrock 	}
456ea8dc4b6Seschrock 
457ea8dc4b6Seschrock 	/* wait for other io to complete */
458ea8dc4b6Seschrock 	if (read) {
459ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
460ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
461ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
462ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
463ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
464ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
465ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
466be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
467ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
468ea8dc4b6Seschrock 			if (err) {
469ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
470ea8dc4b6Seschrock 				return (err);
471ea8dc4b6Seschrock 			}
472ea8dc4b6Seschrock 		}
473ea8dc4b6Seschrock 	}
474ea8dc4b6Seschrock 
475ea8dc4b6Seschrock 	*numbufsp = nblks;
476ea8dc4b6Seschrock 	*dbpp = dbp;
477ea8dc4b6Seschrock 	return (0);
478fa9e4066Sahrens }
479fa9e4066Sahrens 
480a2eea2e1Sahrens static int
48113506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
48213506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
48313506d1eSmaybee {
48413506d1eSmaybee 	dnode_t *dn;
48513506d1eSmaybee 	int err;
48613506d1eSmaybee 
487503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
48813506d1eSmaybee 	if (err)
48913506d1eSmaybee 		return (err);
49013506d1eSmaybee 
49113506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
4927bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
49313506d1eSmaybee 
49413506d1eSmaybee 	dnode_rele(dn, FTAG);
49513506d1eSmaybee 
49613506d1eSmaybee 	return (err);
49713506d1eSmaybee }
49813506d1eSmaybee 
49913506d1eSmaybee int
500744947dcSTom Erickson dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
501cf6106c8SMatthew Ahrens     uint64_t length, boolean_t read, void *tag, int *numbufsp,
502cf6106c8SMatthew Ahrens     dmu_buf_t ***dbpp)
50313506d1eSmaybee {
504744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
505744947dcSTom Erickson 	dnode_t *dn;
50613506d1eSmaybee 	int err;
50713506d1eSmaybee 
508744947dcSTom Erickson 	DB_DNODE_ENTER(db);
509744947dcSTom Erickson 	dn = DB_DNODE(db);
51013506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
5117bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
512744947dcSTom Erickson 	DB_DNODE_EXIT(db);
51313506d1eSmaybee 
51413506d1eSmaybee 	return (err);
51513506d1eSmaybee }
51613506d1eSmaybee 
517fa9e4066Sahrens void
518ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
519fa9e4066Sahrens {
520fa9e4066Sahrens 	int i;
521fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
522fa9e4066Sahrens 
523fa9e4066Sahrens 	if (numbufs == 0)
524fa9e4066Sahrens 		return;
525fa9e4066Sahrens 
526ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
527ea8dc4b6Seschrock 		if (dbp[i])
528ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
529ea8dc4b6Seschrock 	}
530fa9e4066Sahrens 
531fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
532fa9e4066Sahrens }
533fa9e4066Sahrens 
53469962b56SMatthew Ahrens /*
535a2cdcdd2SPaul Dagnelie  * Issue prefetch i/os for the given blocks.  If level is greater than 0, the
536a2cdcdd2SPaul Dagnelie  * indirect blocks prefeteched will be those that point to the blocks containing
537a2cdcdd2SPaul Dagnelie  * the data starting at offset, and continuing to offset + len.
53869962b56SMatthew Ahrens  *
539a2cdcdd2SPaul Dagnelie  * Note that if the indirect blocks above the blocks being prefetched are not in
540a2cdcdd2SPaul Dagnelie  * cache, they will be asychronously read in.
54169962b56SMatthew Ahrens  */
542fa9e4066Sahrens void
543a2cdcdd2SPaul Dagnelie dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,
544a2cdcdd2SPaul Dagnelie     uint64_t len, zio_priority_t pri)
545fa9e4066Sahrens {
546fa9e4066Sahrens 	dnode_t *dn;
547fa9e4066Sahrens 	uint64_t blkid;
54869962b56SMatthew Ahrens 	int nblks, err;
549fa9e4066Sahrens 
550fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
551744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
552fa9e4066Sahrens 
553fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
554fa9e4066Sahrens 			return;
555fa9e4066Sahrens 
556fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
557a2cdcdd2SPaul Dagnelie 		blkid = dbuf_whichblock(dn, level,
558a2cdcdd2SPaul Dagnelie 		    object * sizeof (dnode_phys_t));
559a2cdcdd2SPaul Dagnelie 		dbuf_prefetch(dn, level, blkid, pri, 0);
560fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
561fa9e4066Sahrens 		return;
562fa9e4066Sahrens 	}
563fa9e4066Sahrens 
564fa9e4066Sahrens 	/*
565fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
566fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
567fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
568fa9e4066Sahrens 	 */
569503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
570ea8dc4b6Seschrock 	if (err != 0)
571fa9e4066Sahrens 		return;
572fa9e4066Sahrens 
573fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
574a2cdcdd2SPaul Dagnelie 	/*
575a2cdcdd2SPaul Dagnelie 	 * offset + len - 1 is the last byte we want to prefetch for, and offset
576a2cdcdd2SPaul Dagnelie 	 * is the first.  Then dbuf_whichblk(dn, level, off + len - 1) is the
577a2cdcdd2SPaul Dagnelie 	 * last block we want to prefetch, and dbuf_whichblock(dn, level,
578a2cdcdd2SPaul Dagnelie 	 * offset)  is the first.  Then the number we need to prefetch is the
579a2cdcdd2SPaul Dagnelie 	 * last - first + 1.
580a2cdcdd2SPaul Dagnelie 	 */
581a2cdcdd2SPaul Dagnelie 	if (level > 0 || dn->dn_datablkshift != 0) {
582a2cdcdd2SPaul Dagnelie 		nblks = dbuf_whichblock(dn, level, offset + len - 1) -
583a2cdcdd2SPaul Dagnelie 		    dbuf_whichblock(dn, level, offset) + 1;
584fa9e4066Sahrens 	} else {
585fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
586fa9e4066Sahrens 	}
587fa9e4066Sahrens 
588fa9e4066Sahrens 	if (nblks != 0) {
589a2cdcdd2SPaul Dagnelie 		blkid = dbuf_whichblock(dn, level, offset);
59069962b56SMatthew Ahrens 		for (int i = 0; i < nblks; i++)
591a2cdcdd2SPaul Dagnelie 			dbuf_prefetch(dn, level, blkid + i, pri, 0);
592fa9e4066Sahrens 	}
593fa9e4066Sahrens 
594fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
595fa9e4066Sahrens 
596fa9e4066Sahrens 	dnode_rele(dn, FTAG);
597fa9e4066Sahrens }
598fa9e4066Sahrens 
59976256205SMark Maybee /*
60076256205SMark Maybee  * Get the next "chunk" of file data to free.  We traverse the file from
60176256205SMark Maybee  * the end so that the file gets shorter over time (if we crashes in the
60276256205SMark Maybee  * middle, this will leave us in a better state).  We find allocated file
60376256205SMark Maybee  * data by simply searching the allocated level 1 indirects.
604713d6c20SMatthew Ahrens  *
605713d6c20SMatthew Ahrens  * On input, *start should be the first offset that does not need to be
606713d6c20SMatthew Ahrens  * freed (e.g. "offset + length").  On return, *start will be the first
607713d6c20SMatthew Ahrens  * offset that should be freed.
60876256205SMark Maybee  */
609cdb0ab79Smaybee static int
610713d6c20SMatthew Ahrens get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum)
611cdb0ab79Smaybee {
612713d6c20SMatthew Ahrens 	uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
613713d6c20SMatthew Ahrens 	/* bytes of data covered by a level-1 indirect block */
61476256205SMark Maybee 	uint64_t iblkrange =
6151c8564a7SMark Maybee 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
616cdb0ab79Smaybee 
617713d6c20SMatthew Ahrens 	ASSERT3U(minimum, <=, *start);
618cdb0ab79Smaybee 
619713d6c20SMatthew Ahrens 	if (*start - minimum <= iblkrange * maxblks) {
620713d6c20SMatthew Ahrens 		*start = minimum;
621cdb0ab79Smaybee 		return (0);
622cdb0ab79Smaybee 	}
62376256205SMark Maybee 	ASSERT(ISP2(iblkrange));
624cdb0ab79Smaybee 
625713d6c20SMatthew Ahrens 	for (uint64_t blks = 0; *start > minimum && blks < maxblks; blks++) {
6261c8564a7SMark Maybee 		int err;
627cdb0ab79Smaybee 
628713d6c20SMatthew Ahrens 		/*
629713d6c20SMatthew Ahrens 		 * dnode_next_offset(BACKWARDS) will find an allocated L1
630713d6c20SMatthew Ahrens 		 * indirect block at or before the input offset.  We must
631713d6c20SMatthew Ahrens 		 * decrement *start so that it is at the end of the region
632713d6c20SMatthew Ahrens 		 * to search.
633713d6c20SMatthew Ahrens 		 */
634713d6c20SMatthew Ahrens 		(*start)--;
635cdb0ab79Smaybee 		err = dnode_next_offset(dn,
63676256205SMark Maybee 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
637cdb0ab79Smaybee 
638713d6c20SMatthew Ahrens 		/* if there are no indirect blocks before start, we are done */
63976256205SMark Maybee 		if (err == ESRCH) {
640713d6c20SMatthew Ahrens 			*start = minimum;
641713d6c20SMatthew Ahrens 			break;
642713d6c20SMatthew Ahrens 		} else if (err != 0) {
643cdb0ab79Smaybee 			return (err);
64476256205SMark Maybee 		}
645cdb0ab79Smaybee 
646713d6c20SMatthew Ahrens 		/* set start to the beginning of this L1 indirect */
64776256205SMark Maybee 		*start = P2ALIGN(*start, iblkrange);
648cdb0ab79Smaybee 	}
649713d6c20SMatthew Ahrens 	if (*start < minimum)
650713d6c20SMatthew Ahrens 		*start = minimum;
651cdb0ab79Smaybee 	return (0);
652cdb0ab79Smaybee }
653cdb0ab79Smaybee 
654cdb0ab79Smaybee static int
655cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
656713d6c20SMatthew Ahrens     uint64_t length)
657cdb0ab79Smaybee {
658713d6c20SMatthew Ahrens 	uint64_t object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
659713d6c20SMatthew Ahrens 	int err;
660cdb0ab79Smaybee 
661713d6c20SMatthew Ahrens 	if (offset >= object_size)
662cdb0ab79Smaybee 		return (0);
663cdb0ab79Smaybee 
664713d6c20SMatthew Ahrens 	if (length == DMU_OBJECT_END || offset + length > object_size)
665713d6c20SMatthew Ahrens 		length = object_size - offset;
666713d6c20SMatthew Ahrens 
667713d6c20SMatthew Ahrens 	while (length != 0) {
668713d6c20SMatthew Ahrens 		uint64_t chunk_end, chunk_begin;
669713d6c20SMatthew Ahrens 
670713d6c20SMatthew Ahrens 		chunk_end = chunk_begin = offset + length;
671713d6c20SMatthew Ahrens 
672713d6c20SMatthew Ahrens 		/* move chunk_begin backwards to the beginning of this chunk */
673713d6c20SMatthew Ahrens 		err = get_next_chunk(dn, &chunk_begin, offset);
674cdb0ab79Smaybee 		if (err)
675cdb0ab79Smaybee 			return (err);
676713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, >=, offset);
677713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, <=, chunk_end);
678cdb0ab79Smaybee 
679713d6c20SMatthew Ahrens 		dmu_tx_t *tx = dmu_tx_create(os);
680713d6c20SMatthew Ahrens 		dmu_tx_hold_free(tx, dn->dn_object,
681713d6c20SMatthew Ahrens 		    chunk_begin, chunk_end - chunk_begin);
6824bb73804SMatthew Ahrens 
6834bb73804SMatthew Ahrens 		/*
6844bb73804SMatthew Ahrens 		 * Mark this transaction as typically resulting in a net
6854bb73804SMatthew Ahrens 		 * reduction in space used.
6864bb73804SMatthew Ahrens 		 */
6874bb73804SMatthew Ahrens 		dmu_tx_mark_netfree(tx);
688cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
689cdb0ab79Smaybee 		if (err) {
690cdb0ab79Smaybee 			dmu_tx_abort(tx);
691cdb0ab79Smaybee 			return (err);
692cdb0ab79Smaybee 		}
693713d6c20SMatthew Ahrens 		dnode_free_range(dn, chunk_begin, chunk_end - chunk_begin, tx);
694cdb0ab79Smaybee 		dmu_tx_commit(tx);
695713d6c20SMatthew Ahrens 
696713d6c20SMatthew Ahrens 		length -= chunk_end - chunk_begin;
697cdb0ab79Smaybee 	}
698cdb0ab79Smaybee 	return (0);
699cdb0ab79Smaybee }
700cdb0ab79Smaybee 
701cdb0ab79Smaybee int
702cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object,
703cdb0ab79Smaybee     uint64_t offset, uint64_t length)
704cdb0ab79Smaybee {
705cdb0ab79Smaybee 	dnode_t *dn;
706cdb0ab79Smaybee 	int err;
707cdb0ab79Smaybee 
708503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
709cdb0ab79Smaybee 	if (err != 0)
710cdb0ab79Smaybee 		return (err);
711713d6c20SMatthew Ahrens 	err = dmu_free_long_range_impl(os, dn, offset, length);
7125253393bSMatthew Ahrens 
7135253393bSMatthew Ahrens 	/*
7145253393bSMatthew Ahrens 	 * It is important to zero out the maxblkid when freeing the entire
7155253393bSMatthew Ahrens 	 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
7165253393bSMatthew Ahrens 	 * will take the fast path, and (b) dnode_reallocate() can verify
7175253393bSMatthew Ahrens 	 * that the entire file has been freed.
7185253393bSMatthew Ahrens 	 */
71943466aaeSMax Grossman 	if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
7205253393bSMatthew Ahrens 		dn->dn_maxblkid = 0;
7215253393bSMatthew Ahrens 
722cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
723cdb0ab79Smaybee 	return (err);
724cdb0ab79Smaybee }
725cdb0ab79Smaybee 
726cdb0ab79Smaybee int
727713d6c20SMatthew Ahrens dmu_free_long_object(objset_t *os, uint64_t object)
728cdb0ab79Smaybee {
729cdb0ab79Smaybee 	dmu_tx_t *tx;
730cdb0ab79Smaybee 	int err;
731cdb0ab79Smaybee 
732713d6c20SMatthew Ahrens 	err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
733cdb0ab79Smaybee 	if (err != 0)
734cdb0ab79Smaybee 		return (err);
735713d6c20SMatthew Ahrens 
736cdb0ab79Smaybee 	tx = dmu_tx_create(os);
737cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, object);
738713d6c20SMatthew Ahrens 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
7394bb73804SMatthew Ahrens 	dmu_tx_mark_netfree(tx);
740cdb0ab79Smaybee 	err = dmu_tx_assign(tx, TXG_WAIT);
741cdb0ab79Smaybee 	if (err == 0) {
742713d6c20SMatthew Ahrens 		err = dmu_object_free(os, object, tx);
743cdb0ab79Smaybee 		dmu_tx_commit(tx);
744cdb0ab79Smaybee 	} else {
745cdb0ab79Smaybee 		dmu_tx_abort(tx);
746cdb0ab79Smaybee 	}
747713d6c20SMatthew Ahrens 
748cdb0ab79Smaybee 	return (err);
749cdb0ab79Smaybee }
750cdb0ab79Smaybee 
751ea8dc4b6Seschrock int
752fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
753fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
754fa9e4066Sahrens {
755ea8dc4b6Seschrock 	dnode_t *dn;
756503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
757ea8dc4b6Seschrock 	if (err)
758ea8dc4b6Seschrock 		return (err);
759fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
760fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
761fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
762fa9e4066Sahrens 	dnode_rele(dn, FTAG);
763ea8dc4b6Seschrock 	return (0);
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766ea8dc4b6Seschrock int
767ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
7687bfdf011SNeil Perrin     void *buf, uint32_t flags)
769fa9e4066Sahrens {
770fa9e4066Sahrens 	dnode_t *dn;
771fa9e4066Sahrens 	dmu_buf_t **dbp;
772c87b8fc5SMark J Musante 	int numbufs, err;
773fa9e4066Sahrens 
774503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
775ea8dc4b6Seschrock 	if (err)
776ea8dc4b6Seschrock 		return (err);
777feb08c6bSbillm 
778feb08c6bSbillm 	/*
779feb08c6bSbillm 	 * Deal with odd block sizes, where there can't be data past the first
780feb08c6bSbillm 	 * block.  If we ever do the tail block optimization, we will need to
781feb08c6bSbillm 	 * handle that here as well.
782feb08c6bSbillm 	 */
783c87b8fc5SMark J Musante 	if (dn->dn_maxblkid == 0) {
784fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
785fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
786fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
787fa9e4066Sahrens 		size = newsz;
788fa9e4066Sahrens 	}
789fa9e4066Sahrens 
790fa9e4066Sahrens 	while (size > 0) {
791fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
792c87b8fc5SMark J Musante 		int i;
793fa9e4066Sahrens 
794fa9e4066Sahrens 		/*
795fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
796fa9e4066Sahrens 		 * to be reading in parallel.
797fa9e4066Sahrens 		 */
798a2eea2e1Sahrens 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
7997bfdf011SNeil Perrin 		    TRUE, FTAG, &numbufs, &dbp, flags);
800ea8dc4b6Seschrock 		if (err)
8011934e92fSmaybee 			break;
802fa9e4066Sahrens 
803fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
804fa9e4066Sahrens 			int tocpy;
805fa9e4066Sahrens 			int bufoff;
806fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
807fa9e4066Sahrens 
808fa9e4066Sahrens 			ASSERT(size > 0);
809fa9e4066Sahrens 
810fa9e4066Sahrens 			bufoff = offset - db->db_offset;
811fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
812fa9e4066Sahrens 
813fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
814fa9e4066Sahrens 
815fa9e4066Sahrens 			offset += tocpy;
816fa9e4066Sahrens 			size -= tocpy;
817fa9e4066Sahrens 			buf = (char *)buf + tocpy;
818fa9e4066Sahrens 		}
819ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
820fa9e4066Sahrens 	}
821a2eea2e1Sahrens 	dnode_rele(dn, FTAG);
8221934e92fSmaybee 	return (err);
823fa9e4066Sahrens }
824fa9e4066Sahrens 
825fa9e4066Sahrens void
826fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
827fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
828fa9e4066Sahrens {
829fa9e4066Sahrens 	dmu_buf_t **dbp;
830fa9e4066Sahrens 	int numbufs, i;
831fa9e4066Sahrens 
83213506d1eSmaybee 	if (size == 0)
83313506d1eSmaybee 		return;
83413506d1eSmaybee 
835ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
836ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
837fa9e4066Sahrens 
838fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
839fa9e4066Sahrens 		int tocpy;
840fa9e4066Sahrens 		int bufoff;
841fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
842fa9e4066Sahrens 
843fa9e4066Sahrens 		ASSERT(size > 0);
844fa9e4066Sahrens 
845fa9e4066Sahrens 		bufoff = offset - db->db_offset;
846fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
847fa9e4066Sahrens 
848fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
849fa9e4066Sahrens 
850fa9e4066Sahrens 		if (tocpy == db->db_size)
851fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
852fa9e4066Sahrens 		else
853fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
854fa9e4066Sahrens 
855fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
856fa9e4066Sahrens 
857fa9e4066Sahrens 		if (tocpy == db->db_size)
858fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
859fa9e4066Sahrens 
860fa9e4066Sahrens 		offset += tocpy;
861fa9e4066Sahrens 		size -= tocpy;
862fa9e4066Sahrens 		buf = (char *)buf + tocpy;
863fa9e4066Sahrens 	}
864ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
865fa9e4066Sahrens }
866fa9e4066Sahrens 
86782c9918fSTim Haley void
86882c9918fSTim Haley dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
86982c9918fSTim Haley     dmu_tx_t *tx)
87082c9918fSTim Haley {
87182c9918fSTim Haley 	dmu_buf_t **dbp;
87282c9918fSTim Haley 	int numbufs, i;
87382c9918fSTim Haley 
87482c9918fSTim Haley 	if (size == 0)
87582c9918fSTim Haley 		return;
87682c9918fSTim Haley 
87782c9918fSTim Haley 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
87882c9918fSTim Haley 	    FALSE, FTAG, &numbufs, &dbp));
87982c9918fSTim Haley 
88082c9918fSTim Haley 	for (i = 0; i < numbufs; i++) {
88182c9918fSTim Haley 		dmu_buf_t *db = dbp[i];
88282c9918fSTim Haley 
88382c9918fSTim Haley 		dmu_buf_will_not_fill(db, tx);
88482c9918fSTim Haley 	}
88582c9918fSTim Haley 	dmu_buf_rele_array(dbp, numbufs, FTAG);
88682c9918fSTim Haley }
88782c9918fSTim Haley 
8885d7b4d43SMatthew Ahrens void
8895d7b4d43SMatthew Ahrens dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
8905d7b4d43SMatthew Ahrens     void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
8915d7b4d43SMatthew Ahrens     int compressed_size, int byteorder, dmu_tx_t *tx)
8925d7b4d43SMatthew Ahrens {
8935d7b4d43SMatthew Ahrens 	dmu_buf_t *db;
8945d7b4d43SMatthew Ahrens 
8955d7b4d43SMatthew Ahrens 	ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
8965d7b4d43SMatthew Ahrens 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
8975d7b4d43SMatthew Ahrens 	VERIFY0(dmu_buf_hold_noread(os, object, offset,
8985d7b4d43SMatthew Ahrens 	    FTAG, &db));
8995d7b4d43SMatthew Ahrens 
9005d7b4d43SMatthew Ahrens 	dmu_buf_write_embedded(db,
9015d7b4d43SMatthew Ahrens 	    data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
9025d7b4d43SMatthew Ahrens 	    uncompressed_size, compressed_size, byteorder, tx);
9035d7b4d43SMatthew Ahrens 
9045d7b4d43SMatthew Ahrens 	dmu_buf_rele(db, FTAG);
9055d7b4d43SMatthew Ahrens }
9065d7b4d43SMatthew Ahrens 
907c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
908c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * DMU support for xuio
909c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
910c242f9a0Schunli zhang - Sun Microsystems - Irvine United States kstat_t *xuio_ksp = NULL;
911c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
912c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
913c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_init(xuio_t *xuio, int nblk)
914c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
915c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv;
916c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
917c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
918c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iovcnt = nblk;
919c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
920c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
921c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
922c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->cnt = nblk;
923c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
924c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->iovp = uio->uio_iov;
925c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIO_XUZC_PRIV(xuio) = priv;
926c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
927c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
928c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
929c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
930c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
931c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
932c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
933c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
934c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
935c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
936c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_fini(xuio_t *xuio)
937c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
938c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
939c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int nblk = priv->cnt;
940c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
941c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
942c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
943c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv, sizeof (dmu_xuio_t));
944c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
945c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
946c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
947c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
948c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
949c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
950c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
951c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
952c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
953c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * and increase priv->next by 1.
954c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
955c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
956c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
957c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
958c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	struct iovec *iov;
959c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
960c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
961c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int i = priv->next++;
962c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
963c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
964c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(off + n <= arc_buf_size(abuf));
965c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov = uio->uio_iov + i;
966c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_base = (char *)abuf->b_data + off;
967c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_len = n;
968c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = abuf;
969c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
970c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
971c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
972c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
973c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_cnt(xuio_t *xuio)
974c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
975c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
976c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->cnt);
977c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
978c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
979c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_buf_t *
980c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_arcbuf(xuio_t *xuio, int i)
981c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
982c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
983c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
984c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
985c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->bufs[i]);
986c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
987c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
988c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
989c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_clear(xuio_t *xuio, int i)
990c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
991c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
992c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
993c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
994c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = NULL;
995c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
996c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
997c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
998c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_init(void)
999c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1000c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
1001c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
1002c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_FLAG_VIRTUAL);
1003c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
1004c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp->ks_data = &xuio_stats;
1005c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_install(xuio_ksp);
1006c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
1007c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1008c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1009c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
1010c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_fini(void)
1011c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1012c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
1013c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_delete(xuio_ksp);
1014c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp = NULL;
1015c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
1016c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1017c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1018c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
1019c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_copied()
1020c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1021c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1022c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1023c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1024c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
1025c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_nocopy()
1026c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1027c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
1028c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1029c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1030fa9e4066Sahrens #ifdef _KERNEL
1031f8554bb9SMatthew Ahrens static int
1032f8554bb9SMatthew Ahrens dmu_read_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size)
1033feb08c6bSbillm {
1034feb08c6bSbillm 	dmu_buf_t **dbp;
1035feb08c6bSbillm 	int numbufs, i, err;
1036c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_t *xuio = NULL;
1037feb08c6bSbillm 
1038feb08c6bSbillm 	/*
1039feb08c6bSbillm 	 * NB: we could do this block-at-a-time, but it's nice
1040feb08c6bSbillm 	 * to be reading in parallel.
1041feb08c6bSbillm 	 */
1042f8554bb9SMatthew Ahrens 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
1043f8554bb9SMatthew Ahrens 	    TRUE, FTAG, &numbufs, &dbp, 0);
1044feb08c6bSbillm 	if (err)
1045feb08c6bSbillm 		return (err);
1046feb08c6bSbillm 
1047c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (uio->uio_extflg == UIO_XUIO)
1048c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio = (xuio_t *)uio;
1049c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1050feb08c6bSbillm 	for (i = 0; i < numbufs; i++) {
1051feb08c6bSbillm 		int tocpy;
1052feb08c6bSbillm 		int bufoff;
1053feb08c6bSbillm 		dmu_buf_t *db = dbp[i];
1054feb08c6bSbillm 
1055feb08c6bSbillm 		ASSERT(size > 0);
1056feb08c6bSbillm 
1057feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1058feb08c6bSbillm 		tocpy = (int)MIN(db->db_size - bufoff, size);
1059feb08c6bSbillm 
1060c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (xuio) {
1061c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
1062c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *dbuf_abuf = dbi->db_buf;
1063c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1064c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1065c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (!err) {
1066c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_resid -= tocpy;
1067c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_loffset += tocpy;
1068c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			}
1069c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1070c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (abuf == dbuf_abuf)
1071c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1072c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			else
1073c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1074c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		} else {
1075feb08c6bSbillm 			err = uiomove((char *)db->db_data + bufoff, tocpy,
1076feb08c6bSbillm 			    UIO_READ, uio);
1077c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		}
1078feb08c6bSbillm 		if (err)
1079feb08c6bSbillm 			break;
1080feb08c6bSbillm 
1081feb08c6bSbillm 		size -= tocpy;
1082feb08c6bSbillm 	}
1083feb08c6bSbillm 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1084feb08c6bSbillm 
1085feb08c6bSbillm 	return (err);
1086feb08c6bSbillm }
1087feb08c6bSbillm 
1088f8554bb9SMatthew Ahrens /*
1089f8554bb9SMatthew Ahrens  * Read 'size' bytes into the uio buffer.
1090f8554bb9SMatthew Ahrens  * From object zdb->db_object.
1091f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1092f8554bb9SMatthew Ahrens  *
1093f8554bb9SMatthew Ahrens  * If the caller already has a dbuf in the target object
1094f8554bb9SMatthew Ahrens  * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1095f8554bb9SMatthew Ahrens  * because we don't have to find the dnode_t for the object.
1096f8554bb9SMatthew Ahrens  */
1097f8554bb9SMatthew Ahrens int
1098f8554bb9SMatthew Ahrens dmu_read_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size)
1099f8554bb9SMatthew Ahrens {
1100f8554bb9SMatthew Ahrens 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1101f8554bb9SMatthew Ahrens 	dnode_t *dn;
1102f8554bb9SMatthew Ahrens 	int err;
1103f8554bb9SMatthew Ahrens 
1104f8554bb9SMatthew Ahrens 	if (size == 0)
1105f8554bb9SMatthew Ahrens 		return (0);
1106f8554bb9SMatthew Ahrens 
1107f8554bb9SMatthew Ahrens 	DB_DNODE_ENTER(db);
1108f8554bb9SMatthew Ahrens 	dn = DB_DNODE(db);
1109f8554bb9SMatthew Ahrens 	err = dmu_read_uio_dnode(dn, uio, size);
1110f8554bb9SMatthew Ahrens 	DB_DNODE_EXIT(db);
1111f8554bb9SMatthew Ahrens 
1112f8554bb9SMatthew Ahrens 	return (err);
1113f8554bb9SMatthew Ahrens }
1114f8554bb9SMatthew Ahrens 
1115f8554bb9SMatthew Ahrens /*
1116f8554bb9SMatthew Ahrens  * Read 'size' bytes into the uio buffer.
1117f8554bb9SMatthew Ahrens  * From the specified object
1118f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1119f8554bb9SMatthew Ahrens  */
1120f8554bb9SMatthew Ahrens int
1121f8554bb9SMatthew Ahrens dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
1122f8554bb9SMatthew Ahrens {
1123f8554bb9SMatthew Ahrens 	dnode_t *dn;
1124f8554bb9SMatthew Ahrens 	int err;
1125f8554bb9SMatthew Ahrens 
1126f8554bb9SMatthew Ahrens 	if (size == 0)
1127f8554bb9SMatthew Ahrens 		return (0);
1128f8554bb9SMatthew Ahrens 
1129f8554bb9SMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1130f8554bb9SMatthew Ahrens 	if (err)
1131f8554bb9SMatthew Ahrens 		return (err);
1132f8554bb9SMatthew Ahrens 
1133f8554bb9SMatthew Ahrens 	err = dmu_read_uio_dnode(dn, uio, size);
1134f8554bb9SMatthew Ahrens 
1135f8554bb9SMatthew Ahrens 	dnode_rele(dn, FTAG);
1136f8554bb9SMatthew Ahrens 
1137f8554bb9SMatthew Ahrens 	return (err);
1138f8554bb9SMatthew Ahrens }
1139f8554bb9SMatthew Ahrens 
114094d1a210STim Haley static int
114194d1a210STim Haley dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1142fa9e4066Sahrens {
1143fa9e4066Sahrens 	dmu_buf_t **dbp;
114494d1a210STim Haley 	int numbufs;
1145fa9e4066Sahrens 	int err = 0;
114694d1a210STim Haley 	int i;
1147fa9e4066Sahrens 
114894d1a210STim Haley 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
114994d1a210STim Haley 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1150ea8dc4b6Seschrock 	if (err)
1151ea8dc4b6Seschrock 		return (err);
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
1154fa9e4066Sahrens 		int tocpy;
1155fa9e4066Sahrens 		int bufoff;
1156fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
1157fa9e4066Sahrens 
1158fa9e4066Sahrens 		ASSERT(size > 0);
1159fa9e4066Sahrens 
1160feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1161fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
1162fa9e4066Sahrens 
1163fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1164fa9e4066Sahrens 
1165fa9e4066Sahrens 		if (tocpy == db->db_size)
1166fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
1167fa9e4066Sahrens 		else
1168fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
1169fa9e4066Sahrens 
1170fa9e4066Sahrens 		/*
1171fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
1172fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
1173fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
1174fa9e4066Sahrens 		 * block.
1175fa9e4066Sahrens 		 */
1176fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
1177fa9e4066Sahrens 		    UIO_WRITE, uio);
1178fa9e4066Sahrens 
1179fa9e4066Sahrens 		if (tocpy == db->db_size)
1180fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
1181fa9e4066Sahrens 
1182fa9e4066Sahrens 		if (err)
1183fa9e4066Sahrens 			break;
1184fa9e4066Sahrens 
1185fa9e4066Sahrens 		size -= tocpy;
1186fa9e4066Sahrens 	}
118794d1a210STim Haley 
1188ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1189fa9e4066Sahrens 	return (err);
1190fa9e4066Sahrens }
119144eda4d7Smaybee 
1192f8554bb9SMatthew Ahrens /*
1193f8554bb9SMatthew Ahrens  * Write 'size' bytes from the uio buffer.
1194f8554bb9SMatthew Ahrens  * To object zdb->db_object.
1195f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1196f8554bb9SMatthew Ahrens  *
1197f8554bb9SMatthew Ahrens  * If the caller already has a dbuf in the target object
1198f8554bb9SMatthew Ahrens  * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1199f8554bb9SMatthew Ahrens  * because we don't have to find the dnode_t for the object.
1200f8554bb9SMatthew Ahrens  */
120144eda4d7Smaybee int
120294d1a210STim Haley dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
120394d1a210STim Haley     dmu_tx_t *tx)
120494d1a210STim Haley {
1205744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1206744947dcSTom Erickson 	dnode_t *dn;
1207744947dcSTom Erickson 	int err;
1208744947dcSTom Erickson 
120994d1a210STim Haley 	if (size == 0)
121094d1a210STim Haley 		return (0);
121194d1a210STim Haley 
1212744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1213744947dcSTom Erickson 	dn = DB_DNODE(db);
1214744947dcSTom Erickson 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1215744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1216744947dcSTom Erickson 
1217744947dcSTom Erickson 	return (err);
121894d1a210STim Haley }
121994d1a210STim Haley 
1220f8554bb9SMatthew Ahrens /*
1221f8554bb9SMatthew Ahrens  * Write 'size' bytes from the uio buffer.
1222f8554bb9SMatthew Ahrens  * To the specified object.
1223f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1224f8554bb9SMatthew Ahrens  */
122594d1a210STim Haley int
122694d1a210STim Haley dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
122794d1a210STim Haley     dmu_tx_t *tx)
122894d1a210STim Haley {
122994d1a210STim Haley 	dnode_t *dn;
123094d1a210STim Haley 	int err;
123194d1a210STim Haley 
123294d1a210STim Haley 	if (size == 0)
123394d1a210STim Haley 		return (0);
123494d1a210STim Haley 
123594d1a210STim Haley 	err = dnode_hold(os, object, FTAG, &dn);
123694d1a210STim Haley 	if (err)
123794d1a210STim Haley 		return (err);
123894d1a210STim Haley 
123994d1a210STim Haley 	err = dmu_write_uio_dnode(dn, uio, size, tx);
124094d1a210STim Haley 
124194d1a210STim Haley 	dnode_rele(dn, FTAG);
124294d1a210STim Haley 
124394d1a210STim Haley 	return (err);
124494d1a210STim Haley }
124594d1a210STim Haley 
124694d1a210STim Haley int
124744eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
124844eda4d7Smaybee     page_t *pp, dmu_tx_t *tx)
124944eda4d7Smaybee {
125044eda4d7Smaybee 	dmu_buf_t **dbp;
125144eda4d7Smaybee 	int numbufs, i;
125244eda4d7Smaybee 	int err;
125344eda4d7Smaybee 
125444eda4d7Smaybee 	if (size == 0)
125544eda4d7Smaybee 		return (0);
125644eda4d7Smaybee 
125744eda4d7Smaybee 	err = dmu_buf_hold_array(os, object, offset, size,
125844eda4d7Smaybee 	    FALSE, FTAG, &numbufs, &dbp);
125944eda4d7Smaybee 	if (err)
126044eda4d7Smaybee 		return (err);
126144eda4d7Smaybee 
126244eda4d7Smaybee 	for (i = 0; i < numbufs; i++) {
126344eda4d7Smaybee 		int tocpy, copied, thiscpy;
126444eda4d7Smaybee 		int bufoff;
126544eda4d7Smaybee 		dmu_buf_t *db = dbp[i];
126644eda4d7Smaybee 		caddr_t va;
126744eda4d7Smaybee 
126844eda4d7Smaybee 		ASSERT(size > 0);
126944eda4d7Smaybee 		ASSERT3U(db->db_size, >=, PAGESIZE);
127044eda4d7Smaybee 
127144eda4d7Smaybee 		bufoff = offset - db->db_offset;
127244eda4d7Smaybee 		tocpy = (int)MIN(db->db_size - bufoff, size);
127344eda4d7Smaybee 
127444eda4d7Smaybee 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
127544eda4d7Smaybee 
127644eda4d7Smaybee 		if (tocpy == db->db_size)
127744eda4d7Smaybee 			dmu_buf_will_fill(db, tx);
127844eda4d7Smaybee 		else
127944eda4d7Smaybee 			dmu_buf_will_dirty(db, tx);
128044eda4d7Smaybee 
128144eda4d7Smaybee 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
128244eda4d7Smaybee 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
128344eda4d7Smaybee 			thiscpy = MIN(PAGESIZE, tocpy - copied);
12840fab61baSJonathan W Adams 			va = zfs_map_page(pp, S_READ);
128544eda4d7Smaybee 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
12860fab61baSJonathan W Adams 			zfs_unmap_page(pp, va);
128744eda4d7Smaybee 			pp = pp->p_next;
128844eda4d7Smaybee 			bufoff += PAGESIZE;
128944eda4d7Smaybee 		}
129044eda4d7Smaybee 
129144eda4d7Smaybee 		if (tocpy == db->db_size)
129244eda4d7Smaybee 			dmu_buf_fill_done(db, tx);
129344eda4d7Smaybee 
129444eda4d7Smaybee 		offset += tocpy;
129544eda4d7Smaybee 		size -= tocpy;
129644eda4d7Smaybee 	}
129744eda4d7Smaybee 	dmu_buf_rele_array(dbp, numbufs, FTAG);
129844eda4d7Smaybee 	return (err);
129944eda4d7Smaybee }
1300fa9e4066Sahrens #endif
1301fa9e4066Sahrens 
13022fdbea25SAleksandr Guzovskiy /*
13032fdbea25SAleksandr Guzovskiy  * Allocate a loaned anonymous arc buffer.
13042fdbea25SAleksandr Guzovskiy  */
13052fdbea25SAleksandr Guzovskiy arc_buf_t *
13062fdbea25SAleksandr Guzovskiy dmu_request_arcbuf(dmu_buf_t *handle, int size)
13072fdbea25SAleksandr Guzovskiy {
1308744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
13092fdbea25SAleksandr Guzovskiy 
131043466aaeSMax Grossman 	return (arc_loan_buf(db->db_objset->os_spa, size));
13112fdbea25SAleksandr Guzovskiy }
13122fdbea25SAleksandr Guzovskiy 
13132fdbea25SAleksandr Guzovskiy /*
13142fdbea25SAleksandr Guzovskiy  * Free a loaned arc buffer.
13152fdbea25SAleksandr Guzovskiy  */
13162fdbea25SAleksandr Guzovskiy void
13172fdbea25SAleksandr Guzovskiy dmu_return_arcbuf(arc_buf_t *buf)
13182fdbea25SAleksandr Guzovskiy {
13192fdbea25SAleksandr Guzovskiy 	arc_return_buf(buf, FTAG);
13203b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(buf, FTAG));
13212fdbea25SAleksandr Guzovskiy }
13222fdbea25SAleksandr Guzovskiy 
13232fdbea25SAleksandr Guzovskiy /*
13242fdbea25SAleksandr Guzovskiy  * When possible directly assign passed loaned arc buffer to a dbuf.
13252fdbea25SAleksandr Guzovskiy  * If this is not possible copy the contents of passed arc buf via
13262fdbea25SAleksandr Guzovskiy  * dmu_write().
13272fdbea25SAleksandr Guzovskiy  */
13282fdbea25SAleksandr Guzovskiy void
13292fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
13302fdbea25SAleksandr Guzovskiy     dmu_tx_t *tx)
13312fdbea25SAleksandr Guzovskiy {
1332744947dcSTom Erickson 	dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1333744947dcSTom Erickson 	dnode_t *dn;
13342fdbea25SAleksandr Guzovskiy 	dmu_buf_impl_t *db;
13352fdbea25SAleksandr Guzovskiy 	uint32_t blksz = (uint32_t)arc_buf_size(buf);
13362fdbea25SAleksandr Guzovskiy 	uint64_t blkid;
13372fdbea25SAleksandr Guzovskiy 
1338744947dcSTom Erickson 	DB_DNODE_ENTER(dbuf);
1339744947dcSTom Erickson 	dn = DB_DNODE(dbuf);
13402fdbea25SAleksandr Guzovskiy 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1341a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
13422fdbea25SAleksandr Guzovskiy 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
13432fdbea25SAleksandr Guzovskiy 	rw_exit(&dn->dn_struct_rwlock);
1344744947dcSTom Erickson 	DB_DNODE_EXIT(dbuf);
13452fdbea25SAleksandr Guzovskiy 
13468a904709SMatthew Ahrens 	/*
13478a904709SMatthew Ahrens 	 * We can only assign if the offset is aligned, the arc buf is the
13488a904709SMatthew Ahrens 	 * same size as the dbuf, and the dbuf is not metadata.  It
13498a904709SMatthew Ahrens 	 * can't be metadata because the loaned arc buf comes from the
13508a904709SMatthew Ahrens 	 * user-data kmem arena.
13518a904709SMatthew Ahrens 	 */
13528a904709SMatthew Ahrens 	if (offset == db->db.db_offset && blksz == db->db.db_size &&
13538a904709SMatthew Ahrens 	    DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA) {
13542fdbea25SAleksandr Guzovskiy 		dbuf_assign_arcbuf(db, buf, tx);
13552fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
13562fdbea25SAleksandr Guzovskiy 	} else {
1357744947dcSTom Erickson 		objset_t *os;
1358744947dcSTom Erickson 		uint64_t object;
1359744947dcSTom Erickson 
1360744947dcSTom Erickson 		DB_DNODE_ENTER(dbuf);
1361744947dcSTom Erickson 		dn = DB_DNODE(dbuf);
1362744947dcSTom Erickson 		os = dn->dn_objset;
1363744947dcSTom Erickson 		object = dn->dn_object;
1364744947dcSTom Erickson 		DB_DNODE_EXIT(dbuf);
1365744947dcSTom Erickson 
13662fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
1367744947dcSTom Erickson 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
13682fdbea25SAleksandr Guzovskiy 		dmu_return_arcbuf(buf);
1369c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
13702fdbea25SAleksandr Guzovskiy 	}
13712fdbea25SAleksandr Guzovskiy }
13722fdbea25SAleksandr Guzovskiy 
1373c5c6ffa0Smaybee typedef struct {
1374b24ab676SJeff Bonwick 	dbuf_dirty_record_t	*dsa_dr;
1375b24ab676SJeff Bonwick 	dmu_sync_cb_t		*dsa_done;
1376b24ab676SJeff Bonwick 	zgd_t			*dsa_zgd;
1377b24ab676SJeff Bonwick 	dmu_tx_t		*dsa_tx;
1378c717a561Smaybee } dmu_sync_arg_t;
1379c5c6ffa0Smaybee 
1380c5c6ffa0Smaybee /* ARGSUSED */
1381c5c6ffa0Smaybee static void
1382e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1383e14bb325SJeff Bonwick {
1384b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1385b24ab676SJeff Bonwick 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1386e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1387975c32a0SNeil Perrin 
1388b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
1389b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp)) {
1390b24ab676SJeff Bonwick 			/*
1391b24ab676SJeff Bonwick 			 * A block of zeros may compress to a hole, but the
1392b24ab676SJeff Bonwick 			 * block size still needs to be known for replay.
1393b24ab676SJeff Bonwick 			 */
1394b24ab676SJeff Bonwick 			BP_SET_LSIZE(bp, db->db_size);
13955d7b4d43SMatthew Ahrens 		} else if (!BP_IS_EMBEDDED(bp)) {
1396e14bb325SJeff Bonwick 			ASSERT(BP_GET_LEVEL(bp) == 0);
1397e14bb325SJeff Bonwick 			bp->blk_fill = 1;
1398e14bb325SJeff Bonwick 		}
1399e14bb325SJeff Bonwick 	}
1400b24ab676SJeff Bonwick }
1401b24ab676SJeff Bonwick 
1402b24ab676SJeff Bonwick static void
1403b24ab676SJeff Bonwick dmu_sync_late_arrival_ready(zio_t *zio)
1404b24ab676SJeff Bonwick {
1405b24ab676SJeff Bonwick 	dmu_sync_ready(zio, NULL, zio->io_private);
1406b24ab676SJeff Bonwick }
1407e14bb325SJeff Bonwick 
1408e14bb325SJeff Bonwick /* ARGSUSED */
1409e14bb325SJeff Bonwick static void
1410c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1411c5c6ffa0Smaybee {
1412b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1413b24ab676SJeff Bonwick 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1414c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1415c5c6ffa0Smaybee 
1416b50a0fe0SNeil Perrin 	mutex_enter(&db->db_mtx);
1417b50a0fe0SNeil Perrin 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1418b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
141980901aeaSGeorge Wilson 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
142080901aeaSGeorge Wilson 		if (dr->dt.dl.dr_nopwrite) {
142180901aeaSGeorge Wilson 			blkptr_t *bp = zio->io_bp;
142280901aeaSGeorge Wilson 			blkptr_t *bp_orig = &zio->io_bp_orig;
142380901aeaSGeorge Wilson 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
142480901aeaSGeorge Wilson 
142580901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
142680901aeaSGeorge Wilson 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
142745818ee1SMatthew Ahrens 			ASSERT(zio_checksum_table[chksum].ci_flags &
142845818ee1SMatthew Ahrens 			    ZCHECKSUM_FLAG_NOPWRITE);
142980901aeaSGeorge Wilson 		}
1430b24ab676SJeff Bonwick 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1431b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1432b24ab676SJeff Bonwick 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
143370163ac5SPrakash Surya 
143470163ac5SPrakash Surya 		/*
143570163ac5SPrakash Surya 		 * Old style holes are filled with all zeros, whereas
143670163ac5SPrakash Surya 		 * new-style holes maintain their lsize, type, level,
143770163ac5SPrakash Surya 		 * and birth time (see zio_write_compress). While we
143870163ac5SPrakash Surya 		 * need to reset the BP_SET_LSIZE() call that happened
143970163ac5SPrakash Surya 		 * in dmu_sync_ready for old style holes, we do *not*
144070163ac5SPrakash Surya 		 * want to wipe out the information contained in new
144170163ac5SPrakash Surya 		 * style holes. Thus, only zero out the block pointer if
144270163ac5SPrakash Surya 		 * it's an old style hole.
144370163ac5SPrakash Surya 		 */
144470163ac5SPrakash Surya 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
144570163ac5SPrakash Surya 		    dr->dt.dl.dr_overridden_by.blk_birth == 0)
1446b50a0fe0SNeil Perrin 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1447b24ab676SJeff Bonwick 	} else {
1448b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1449b24ab676SJeff Bonwick 	}
1450c5c6ffa0Smaybee 	cv_broadcast(&db->db_changed);
1451b50a0fe0SNeil Perrin 	mutex_exit(&db->db_mtx);
1452b50a0fe0SNeil Perrin 
1453b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1454c717a561Smaybee 
1455b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1456b24ab676SJeff Bonwick }
1457b24ab676SJeff Bonwick 
1458b24ab676SJeff Bonwick static void
1459b24ab676SJeff Bonwick dmu_sync_late_arrival_done(zio_t *zio)
1460b24ab676SJeff Bonwick {
1461b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1462b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = zio->io_private;
146380901aeaSGeorge Wilson 	blkptr_t *bp_orig = &zio->io_bp_orig;
1464b24ab676SJeff Bonwick 
1465b24ab676SJeff Bonwick 	if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
146680901aeaSGeorge Wilson 		/*
146780901aeaSGeorge Wilson 		 * If we didn't allocate a new block (i.e. ZIO_FLAG_NOPWRITE)
146880901aeaSGeorge Wilson 		 * then there is nothing to do here. Otherwise, free the
146980901aeaSGeorge Wilson 		 * newly allocated block in this txg.
147080901aeaSGeorge Wilson 		 */
147180901aeaSGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
147280901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
147380901aeaSGeorge Wilson 		} else {
147480901aeaSGeorge Wilson 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1475b24ab676SJeff Bonwick 			ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1476b24ab676SJeff Bonwick 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1477b24ab676SJeff Bonwick 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1478b24ab676SJeff Bonwick 		}
147980901aeaSGeorge Wilson 	}
1480b24ab676SJeff Bonwick 
1481b24ab676SJeff Bonwick 	dmu_tx_commit(dsa->dsa_tx);
1482b24ab676SJeff Bonwick 
1483b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1484b24ab676SJeff Bonwick 
1485b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1486b24ab676SJeff Bonwick }
1487b24ab676SJeff Bonwick 
1488b24ab676SJeff Bonwick static int
1489b24ab676SJeff Bonwick dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
14907802d7bfSMatthew Ahrens     zio_prop_t *zp, zbookmark_phys_t *zb)
1491b24ab676SJeff Bonwick {
1492b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1493b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1494b24ab676SJeff Bonwick 
1495b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1496b24ab676SJeff Bonwick 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
14976e1f5caaSNeil Perrin 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1498b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1499be6fd75aSMatthew Ahrens 		/* Make zl_get_data do txg_waited_synced() */
1500be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1501b24ab676SJeff Bonwick 	}
1502b24ab676SJeff Bonwick 
1503b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1504b24ab676SJeff Bonwick 	dsa->dsa_dr = NULL;
1505b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1506b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1507b24ab676SJeff Bonwick 	dsa->dsa_tx = tx;
1508b24ab676SJeff Bonwick 
1509b24ab676SJeff Bonwick 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1510b24ab676SJeff Bonwick 	    zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp,
151169962b56SMatthew Ahrens 	    dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done, dsa,
1512b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1513b24ab676SJeff Bonwick 
1514b24ab676SJeff Bonwick 	return (0);
1515c5c6ffa0Smaybee }
1516c5c6ffa0Smaybee 
1517fa9e4066Sahrens /*
1518c5c6ffa0Smaybee  * Intent log support: sync the block associated with db to disk.
1519c5c6ffa0Smaybee  * N.B. and XXX: the caller is responsible for making sure that the
1520c5c6ffa0Smaybee  * data isn't changing while dmu_sync() is writing it.
1521fa9e4066Sahrens  *
1522fa9e4066Sahrens  * Return values:
1523fa9e4066Sahrens  *
152480901aeaSGeorge Wilson  *	EEXIST: this txg has already been synced, so there's nothing to do.
1525fa9e4066Sahrens  *		The caller should not log the write.
1526fa9e4066Sahrens  *
1527fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1528fa9e4066Sahrens  *		The caller should not log the write.
1529fa9e4066Sahrens  *
1530c5c6ffa0Smaybee  *	EALREADY: this block is already in the process of being synced.
1531c5c6ffa0Smaybee  *		The caller should track its progress (somehow).
1532fa9e4066Sahrens  *
1533b24ab676SJeff Bonwick  *	EIO: could not do the I/O.
1534b24ab676SJeff Bonwick  *		The caller should do a txg_wait_synced().
1535fa9e4066Sahrens  *
1536b24ab676SJeff Bonwick  *	0: the I/O has been initiated.
1537b24ab676SJeff Bonwick  *		The caller should log this blkptr in the done callback.
1538b24ab676SJeff Bonwick  *		It is possible that the I/O will fail, in which case
1539b24ab676SJeff Bonwick  *		the error will be reported to the done callback and
1540b24ab676SJeff Bonwick  *		propagated to pio from zio_done().
1541fa9e4066Sahrens  */
1542fa9e4066Sahrens int
1543b24ab676SJeff Bonwick dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1544fa9e4066Sahrens {
1545b24ab676SJeff Bonwick 	blkptr_t *bp = zgd->zgd_bp;
1546b24ab676SJeff Bonwick 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1547503ad85cSMatthew Ahrens 	objset_t *os = db->db_objset;
1548b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
1549c717a561Smaybee 	dbuf_dirty_record_t *dr;
1550b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
15517802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1552b24ab676SJeff Bonwick 	zio_prop_t zp;
1553744947dcSTom Erickson 	dnode_t *dn;
1554fa9e4066Sahrens 
1555b24ab676SJeff Bonwick 	ASSERT(pio != NULL);
1556fa9e4066Sahrens 	ASSERT(txg != 0);
1557fa9e4066Sahrens 
1558b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, ds->ds_object,
1559b24ab676SJeff Bonwick 	    db->db.db_object, db->db_level, db->db_blkid);
1560b24ab676SJeff Bonwick 
1561744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1562744947dcSTom Erickson 	dn = DB_DNODE(db);
1563744947dcSTom Erickson 	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1564744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1565fa9e4066Sahrens 
1566fa9e4066Sahrens 	/*
1567b24ab676SJeff Bonwick 	 * If we're frozen (running ziltest), we always need to generate a bp.
1568ea8dc4b6Seschrock 	 */
1569b24ab676SJeff Bonwick 	if (txg > spa_freeze_txg(os->os_spa))
1570b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1571ea8dc4b6Seschrock 
1572ea8dc4b6Seschrock 	/*
1573b24ab676SJeff Bonwick 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1574b24ab676SJeff Bonwick 	 * and us.  If we determine that this txg is not yet syncing,
1575b24ab676SJeff Bonwick 	 * but it begins to sync a moment later, that's OK because the
1576b24ab676SJeff Bonwick 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1577fa9e4066Sahrens 	 */
1578b24ab676SJeff Bonwick 	mutex_enter(&db->db_mtx);
1579b24ab676SJeff Bonwick 
1580b24ab676SJeff Bonwick 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1581fa9e4066Sahrens 		/*
1582b24ab676SJeff Bonwick 		 * This txg has already synced.  There's nothing to do.
1583fa9e4066Sahrens 		 */
1584b24ab676SJeff Bonwick 		mutex_exit(&db->db_mtx);
1585be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1586fa9e4066Sahrens 	}
1587fa9e4066Sahrens 
1588b24ab676SJeff Bonwick 	if (txg <= spa_syncing_txg(os->os_spa)) {
1589c5c6ffa0Smaybee 		/*
1590b24ab676SJeff Bonwick 		 * This txg is currently syncing, so we can't mess with
1591b24ab676SJeff Bonwick 		 * the dirty record anymore; just write a new log block.
1592c5c6ffa0Smaybee 		 */
159313506d1eSmaybee 		mutex_exit(&db->db_mtx);
1594b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1595c5c6ffa0Smaybee 	}
1596c5c6ffa0Smaybee 
1597c717a561Smaybee 	dr = db->db_last_dirty;
1598b24ab676SJeff Bonwick 	while (dr && dr->dr_txg != txg)
1599c717a561Smaybee 		dr = dr->dr_next;
1600b24ab676SJeff Bonwick 
1601b24ab676SJeff Bonwick 	if (dr == NULL) {
1602c5c6ffa0Smaybee 		/*
1603b24ab676SJeff Bonwick 		 * There's no dr for this dbuf, so it must have been freed.
1604fa9e4066Sahrens 		 * There's no need to log writes to freed blocks, so we're done.
1605fa9e4066Sahrens 		 */
1606fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1607be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1608fa9e4066Sahrens 	}
1609fa9e4066Sahrens 
161080901aeaSGeorge Wilson 	ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg);
161180901aeaSGeorge Wilson 
161280901aeaSGeorge Wilson 	/*
161334e8acefSMatthew Ahrens 	 * Assume the on-disk data is X, the current syncing data (in
161434e8acefSMatthew Ahrens 	 * txg - 1) is Y, and the current in-memory data is Z (currently
161534e8acefSMatthew Ahrens 	 * in dmu_sync).
161634e8acefSMatthew Ahrens 	 *
161734e8acefSMatthew Ahrens 	 * We usually want to perform a nopwrite if X and Z are the
161834e8acefSMatthew Ahrens 	 * same.  However, if Y is different (i.e. the BP is going to
161934e8acefSMatthew Ahrens 	 * change before this write takes effect), then a nopwrite will
162034e8acefSMatthew Ahrens 	 * be incorrect - we would override with X, which could have
162134e8acefSMatthew Ahrens 	 * been freed when Y was written.
162234e8acefSMatthew Ahrens 	 *
162334e8acefSMatthew Ahrens 	 * (Note that this is not a concern when we are nop-writing from
162434e8acefSMatthew Ahrens 	 * syncing context, because X and Y must be identical, because
162534e8acefSMatthew Ahrens 	 * all previous txgs have been synced.)
162634e8acefSMatthew Ahrens 	 *
162734e8acefSMatthew Ahrens 	 * Therefore, we disable nopwrite if the current BP could change
162834e8acefSMatthew Ahrens 	 * before this TXG.  There are two ways it could change: by
162934e8acefSMatthew Ahrens 	 * being dirty (dr_next is non-NULL), or by being freed
163034e8acefSMatthew Ahrens 	 * (dnode_block_freed()).  This behavior is verified by
163134e8acefSMatthew Ahrens 	 * zio_done(), which VERIFYs that the override BP is identical
163234e8acefSMatthew Ahrens 	 * to the on-disk BP.
163380901aeaSGeorge Wilson 	 */
163434e8acefSMatthew Ahrens 	DB_DNODE_ENTER(db);
163534e8acefSMatthew Ahrens 	dn = DB_DNODE(db);
163634e8acefSMatthew Ahrens 	if (dr->dr_next != NULL || dnode_block_freed(dn, db->db_blkid))
163780901aeaSGeorge Wilson 		zp.zp_nopwrite = B_FALSE;
163834e8acefSMatthew Ahrens 	DB_DNODE_EXIT(db);
163980901aeaSGeorge Wilson 
1640c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
1641b24ab676SJeff Bonwick 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1642b24ab676SJeff Bonwick 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1643c5c6ffa0Smaybee 		/*
1644b24ab676SJeff Bonwick 		 * We have already issued a sync write for this buffer,
1645b24ab676SJeff Bonwick 		 * or this buffer has already been synced.  It could not
1646c717a561Smaybee 		 * have been dirtied since, or we would have cleared the state.
1647c717a561Smaybee 		 */
1648c717a561Smaybee 		mutex_exit(&db->db_mtx);
1649be6fd75aSMatthew Ahrens 		return (SET_ERROR(EALREADY));
1650c717a561Smaybee 	}
1651c717a561Smaybee 
1652b24ab676SJeff Bonwick 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1653c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1654fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1655fa9e4066Sahrens 
1656b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1657b24ab676SJeff Bonwick 	dsa->dsa_dr = dr;
1658b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1659b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1660b24ab676SJeff Bonwick 	dsa->dsa_tx = NULL;
1661e14bb325SJeff Bonwick 
1662b24ab676SJeff Bonwick 	zio_nowait(arc_write(pio, os->os_spa, txg,
1663aad02571SSaso Kiselkov 	    bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db),
166469962b56SMatthew Ahrens 	    DBUF_IS_L2COMPRESSIBLE(db), &zp, dmu_sync_ready,
166569962b56SMatthew Ahrens 	    NULL, dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE,
166669962b56SMatthew Ahrens 	    ZIO_FLAG_CANFAIL, &zb));
1667e14bb325SJeff Bonwick 
1668b24ab676SJeff Bonwick 	return (0);
1669fa9e4066Sahrens }
1670fa9e4066Sahrens 
1671fa9e4066Sahrens int
1672fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1673fa9e4066Sahrens     dmu_tx_t *tx)
1674fa9e4066Sahrens {
1675ea8dc4b6Seschrock 	dnode_t *dn;
1676ea8dc4b6Seschrock 	int err;
1677ea8dc4b6Seschrock 
1678503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1679ea8dc4b6Seschrock 	if (err)
1680ea8dc4b6Seschrock 		return (err);
1681ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1682fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1683fa9e4066Sahrens 	return (err);
1684fa9e4066Sahrens }
1685fa9e4066Sahrens 
1686fa9e4066Sahrens void
1687fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1688fa9e4066Sahrens     dmu_tx_t *tx)
1689fa9e4066Sahrens {
1690ea8dc4b6Seschrock 	dnode_t *dn;
1691ea8dc4b6Seschrock 
16925d7b4d43SMatthew Ahrens 	/*
16935d7b4d43SMatthew Ahrens 	 * Send streams include each object's checksum function.  This
16945d7b4d43SMatthew Ahrens 	 * check ensures that the receiving system can understand the
16955d7b4d43SMatthew Ahrens 	 * checksum function transmitted.
16965d7b4d43SMatthew Ahrens 	 */
16975d7b4d43SMatthew Ahrens 	ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
16985d7b4d43SMatthew Ahrens 
16995d7b4d43SMatthew Ahrens 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
17005d7b4d43SMatthew Ahrens 	ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
1701fa9e4066Sahrens 	dn->dn_checksum = checksum;
1702fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1703fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1704fa9e4066Sahrens }
1705fa9e4066Sahrens 
1706fa9e4066Sahrens void
1707fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1708fa9e4066Sahrens     dmu_tx_t *tx)
1709fa9e4066Sahrens {
1710ea8dc4b6Seschrock 	dnode_t *dn;
1711ea8dc4b6Seschrock 
17125d7b4d43SMatthew Ahrens 	/*
17135d7b4d43SMatthew Ahrens 	 * Send streams include each object's compression function.  This
17145d7b4d43SMatthew Ahrens 	 * check ensures that the receiving system can understand the
17155d7b4d43SMatthew Ahrens 	 * compression function transmitted.
17165d7b4d43SMatthew Ahrens 	 */
17175d7b4d43SMatthew Ahrens 	ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
17185d7b4d43SMatthew Ahrens 
17195d7b4d43SMatthew Ahrens 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
1720fa9e4066Sahrens 	dn->dn_compress = compress;
1721fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1722fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1723fa9e4066Sahrens }
1724fa9e4066Sahrens 
1725b24ab676SJeff Bonwick int zfs_mdcomp_disable = 0;
1726b24ab676SJeff Bonwick 
1727edf345e6SMatthew Ahrens /*
1728edf345e6SMatthew Ahrens  * When the "redundant_metadata" property is set to "most", only indirect
1729edf345e6SMatthew Ahrens  * blocks of this level and higher will have an additional ditto block.
1730edf345e6SMatthew Ahrens  */
1731edf345e6SMatthew Ahrens int zfs_redundant_metadata_most_ditto_level = 2;
1732edf345e6SMatthew Ahrens 
1733b24ab676SJeff Bonwick void
1734b24ab676SJeff Bonwick dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1735b24ab676SJeff Bonwick {
1736b24ab676SJeff Bonwick 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1737ad135b5dSChristopher Siden 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
17381d8ccc7bSMark Shellenbaum 	    (wp & WP_SPILL));
1739b24ab676SJeff Bonwick 	enum zio_checksum checksum = os->os_checksum;
1740b24ab676SJeff Bonwick 	enum zio_compress compress = os->os_compress;
1741b24ab676SJeff Bonwick 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
17427540df39SGeorge Wilson 	boolean_t dedup = B_FALSE;
17437540df39SGeorge Wilson 	boolean_t nopwrite = B_FALSE;
1744b24ab676SJeff Bonwick 	boolean_t dedup_verify = os->os_dedup_verify;
1745b24ab676SJeff Bonwick 	int copies = os->os_copies;
1746b24ab676SJeff Bonwick 
1747b24ab676SJeff Bonwick 	/*
174880901aeaSGeorge Wilson 	 * We maintain different write policies for each of the following
174980901aeaSGeorge Wilson 	 * types of data:
175080901aeaSGeorge Wilson 	 *	 1. metadata
175180901aeaSGeorge Wilson 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
175280901aeaSGeorge Wilson 	 *	 3. all other level 0 blocks
1753b24ab676SJeff Bonwick 	 */
1754b24ab676SJeff Bonwick 	if (ismd) {
1755db1741f5SJustin T. Gibbs 		if (zfs_mdcomp_disable) {
1756db1741f5SJustin T. Gibbs 			compress = ZIO_COMPRESS_EMPTY;
1757db1741f5SJustin T. Gibbs 		} else {
1758b24ab676SJeff Bonwick 			/*
175980901aeaSGeorge Wilson 			 * XXX -- we should design a compression algorithm
176080901aeaSGeorge Wilson 			 * that specializes in arrays of bps.
176180901aeaSGeorge Wilson 			 */
1762db1741f5SJustin T. Gibbs 			compress = zio_compress_select(os->os_spa,
1763db1741f5SJustin T. Gibbs 			    ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
1764b8289d24SDaniil Lunev 		}
176580901aeaSGeorge Wilson 
176680901aeaSGeorge Wilson 		/*
1767b24ab676SJeff Bonwick 		 * Metadata always gets checksummed.  If the data
1768b24ab676SJeff Bonwick 		 * checksum is multi-bit correctable, and it's not a
1769b24ab676SJeff Bonwick 		 * ZBT-style checksum, then it's suitable for metadata
1770b24ab676SJeff Bonwick 		 * as well.  Otherwise, the metadata checksum defaults
1771b24ab676SJeff Bonwick 		 * to fletcher4.
1772b24ab676SJeff Bonwick 		 */
177345818ee1SMatthew Ahrens 		if (!(zio_checksum_table[checksum].ci_flags &
177445818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_METADATA) ||
177545818ee1SMatthew Ahrens 		    (zio_checksum_table[checksum].ci_flags &
177645818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_EMBEDDED))
1777b24ab676SJeff Bonwick 			checksum = ZIO_CHECKSUM_FLETCHER_4;
1778edf345e6SMatthew Ahrens 
1779edf345e6SMatthew Ahrens 		if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||
1780edf345e6SMatthew Ahrens 		    (os->os_redundant_metadata ==
1781edf345e6SMatthew Ahrens 		    ZFS_REDUNDANT_METADATA_MOST &&
1782edf345e6SMatthew Ahrens 		    (level >= zfs_redundant_metadata_most_ditto_level ||
1783edf345e6SMatthew Ahrens 		    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))))
1784edf345e6SMatthew Ahrens 			copies++;
178580901aeaSGeorge Wilson 	} else if (wp & WP_NOFILL) {
178680901aeaSGeorge Wilson 		ASSERT(level == 0);
1787b24ab676SJeff Bonwick 
1788b24ab676SJeff Bonwick 		/*
178980901aeaSGeorge Wilson 		 * If we're writing preallocated blocks, we aren't actually
179080901aeaSGeorge Wilson 		 * writing them so don't set any policy properties.  These
179180901aeaSGeorge Wilson 		 * blocks are currently only used by an external subsystem
179280901aeaSGeorge Wilson 		 * outside of zfs (i.e. dump) and not written by the zio
179380901aeaSGeorge Wilson 		 * pipeline.
1794b24ab676SJeff Bonwick 		 */
179580901aeaSGeorge Wilson 		compress = ZIO_COMPRESS_OFF;
1796810e43b2SBill Pijewski 		checksum = ZIO_CHECKSUM_NOPARITY;
1797b24ab676SJeff Bonwick 	} else {
1798db1741f5SJustin T. Gibbs 		compress = zio_compress_select(os->os_spa, dn->dn_compress,
1799db1741f5SJustin T. Gibbs 		    compress);
180080901aeaSGeorge Wilson 
180180901aeaSGeorge Wilson 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
180280901aeaSGeorge Wilson 		    zio_checksum_select(dn->dn_checksum, checksum) :
180380901aeaSGeorge Wilson 		    dedup_checksum;
180480901aeaSGeorge Wilson 
180580901aeaSGeorge Wilson 		/*
180680901aeaSGeorge Wilson 		 * Determine dedup setting.  If we are in dmu_sync(),
180780901aeaSGeorge Wilson 		 * we won't actually dedup now because that's all
180880901aeaSGeorge Wilson 		 * done in syncing context; but we do want to use the
180980901aeaSGeorge Wilson 		 * dedup checkum.  If the checksum is not strong
181080901aeaSGeorge Wilson 		 * enough to ensure unique signatures, force
181180901aeaSGeorge Wilson 		 * dedup_verify.
181280901aeaSGeorge Wilson 		 */
181380901aeaSGeorge Wilson 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
181480901aeaSGeorge Wilson 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
181545818ee1SMatthew Ahrens 			if (!(zio_checksum_table[checksum].ci_flags &
181645818ee1SMatthew Ahrens 			    ZCHECKSUM_FLAG_DEDUP))
181780901aeaSGeorge Wilson 				dedup_verify = B_TRUE;
1818b24ab676SJeff Bonwick 		}
1819b24ab676SJeff Bonwick 
1820b24ab676SJeff Bonwick 		/*
182145818ee1SMatthew Ahrens 		 * Enable nopwrite if we have secure enough checksum
182245818ee1SMatthew Ahrens 		 * algorithm (see comment in zio_nop_write) and
182345818ee1SMatthew Ahrens 		 * compression is enabled.  We don't enable nopwrite if
182445818ee1SMatthew Ahrens 		 * dedup is enabled as the two features are mutually
182545818ee1SMatthew Ahrens 		 * exclusive.
1826b24ab676SJeff Bonwick 		 */
182745818ee1SMatthew Ahrens 		nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
182845818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_NOPWRITE) &&
182980901aeaSGeorge Wilson 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
1830b24ab676SJeff Bonwick 	}
1831b24ab676SJeff Bonwick 
1832b24ab676SJeff Bonwick 	zp->zp_checksum = checksum;
1833b24ab676SJeff Bonwick 	zp->zp_compress = compress;
18340a586ceaSMark Shellenbaum 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1835b24ab676SJeff Bonwick 	zp->zp_level = level;
1836edf345e6SMatthew Ahrens 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
1837b24ab676SJeff Bonwick 	zp->zp_dedup = dedup;
1838b24ab676SJeff Bonwick 	zp->zp_dedup_verify = dedup && dedup_verify;
183980901aeaSGeorge Wilson 	zp->zp_nopwrite = nopwrite;
1840b24ab676SJeff Bonwick }
1841b24ab676SJeff Bonwick 
184244cd46caSbillm int
1843fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1844fa9e4066Sahrens {
1845fa9e4066Sahrens 	dnode_t *dn;
18462bcf0248SMax Grossman 	int err;
1847fa9e4066Sahrens 
1848fa9e4066Sahrens 	/*
1849fa9e4066Sahrens 	 * Sync any current changes before
1850fa9e4066Sahrens 	 * we go trundling through the block pointers.
1851fa9e4066Sahrens 	 */
18522bcf0248SMax Grossman 	err = dmu_object_wait_synced(os, object);
18532bcf0248SMax Grossman 	if (err) {
18542bcf0248SMax Grossman 		return (err);
1855fa9e4066Sahrens 	}
18562bcf0248SMax Grossman 
1857503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
18582bcf0248SMax Grossman 	if (err) {
1859ea8dc4b6Seschrock 		return (err);
1860fa9e4066Sahrens 	}
1861fa9e4066Sahrens 
1862cdb0ab79Smaybee 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1863fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1864fa9e4066Sahrens 
1865fa9e4066Sahrens 	return (err);
1866fa9e4066Sahrens }
1867fa9e4066Sahrens 
18682bcf0248SMax Grossman /*
18692bcf0248SMax Grossman  * Given the ZFS object, if it contains any dirty nodes
18702bcf0248SMax Grossman  * this function flushes all dirty blocks to disk. This
18712bcf0248SMax Grossman  * ensures the DMU object info is updated. A more efficient
18722bcf0248SMax Grossman  * future version might just find the TXG with the maximum
18732bcf0248SMax Grossman  * ID and wait for that to be synced.
18742bcf0248SMax Grossman  */
18752bcf0248SMax Grossman int
1876*9a686fbcSPaul Dagnelie dmu_object_wait_synced(objset_t *os, uint64_t object)
1877*9a686fbcSPaul Dagnelie {
18782bcf0248SMax Grossman 	dnode_t *dn;
18792bcf0248SMax Grossman 	int error, i;
18802bcf0248SMax Grossman 
18812bcf0248SMax Grossman 	error = dnode_hold(os, object, FTAG, &dn);
18822bcf0248SMax Grossman 	if (error) {
18832bcf0248SMax Grossman 		return (error);
18842bcf0248SMax Grossman 	}
18852bcf0248SMax Grossman 
18862bcf0248SMax Grossman 	for (i = 0; i < TXG_SIZE; i++) {
18872bcf0248SMax Grossman 		if (list_link_active(&dn->dn_dirty_link[i])) {
18882bcf0248SMax Grossman 			break;
18892bcf0248SMax Grossman 		}
18902bcf0248SMax Grossman 	}
18912bcf0248SMax Grossman 	dnode_rele(dn, FTAG);
18922bcf0248SMax Grossman 	if (i != TXG_SIZE) {
18932bcf0248SMax Grossman 		txg_wait_synced(dmu_objset_pool(os), 0);
18942bcf0248SMax Grossman 	}
18952bcf0248SMax Grossman 
18962bcf0248SMax Grossman 	return (0);
18972bcf0248SMax Grossman }
18982bcf0248SMax Grossman 
1899fa9e4066Sahrens void
1900fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1901fa9e4066Sahrens {
1902b24ab676SJeff Bonwick 	dnode_phys_t *dnp;
1903b24ab676SJeff Bonwick 
1904fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1905fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1906fa9e4066Sahrens 
1907b24ab676SJeff Bonwick 	dnp = dn->dn_phys;
1908b24ab676SJeff Bonwick 
1909fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1910fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1911fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1912b24ab676SJeff Bonwick 	doi->doi_type = dn->dn_type;
1913b24ab676SJeff Bonwick 	doi->doi_bonus_type = dn->dn_bonustype;
1914b24ab676SJeff Bonwick 	doi->doi_bonus_size = dn->dn_bonuslen;
1915fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1916fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1917fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
1918e77d42eaSMatthew Ahrens 	doi->doi_nblkptr = dn->dn_nblkptr;
1919b24ab676SJeff Bonwick 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1920d0475637SMatthew Ahrens 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1921b24ab676SJeff Bonwick 	doi->doi_fill_count = 0;
1922b24ab676SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
19235d7b4d43SMatthew Ahrens 		doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
1924fa9e4066Sahrens 
1925fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1926fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1927fa9e4066Sahrens }
1928fa9e4066Sahrens 
1929fa9e4066Sahrens /*
1930fa9e4066Sahrens  * Get information on a DMU object.
1931fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1932fa9e4066Sahrens  */
1933fa9e4066Sahrens int
1934fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1935fa9e4066Sahrens {
1936ea8dc4b6Seschrock 	dnode_t *dn;
1937503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
1938fa9e4066Sahrens 
1939ea8dc4b6Seschrock 	if (err)
1940ea8dc4b6Seschrock 		return (err);
1941fa9e4066Sahrens 
1942fa9e4066Sahrens 	if (doi != NULL)
1943fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
1944fa9e4066Sahrens 
1945fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1946fa9e4066Sahrens 	return (0);
1947fa9e4066Sahrens }
1948fa9e4066Sahrens 
1949fa9e4066Sahrens /*
1950fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
1951fa9e4066Sahrens  */
1952fa9e4066Sahrens void
1953744947dcSTom Erickson dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
1954fa9e4066Sahrens {
1955744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1956744947dcSTom Erickson 
1957744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1958744947dcSTom Erickson 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
1959744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1960fa9e4066Sahrens }
1961fa9e4066Sahrens 
1962fa9e4066Sahrens /*
1963fa9e4066Sahrens  * Faster still when you only care about the size.
1964fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
1965fa9e4066Sahrens  */
1966fa9e4066Sahrens void
1967744947dcSTom Erickson dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
1968744947dcSTom Erickson     u_longlong_t *nblk512)
1969fa9e4066Sahrens {
1970744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1971744947dcSTom Erickson 	dnode_t *dn;
1972744947dcSTom Erickson 
1973744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1974744947dcSTom Erickson 	dn = DB_DNODE(db);
1975fa9e4066Sahrens 
1976fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
197799653d4eSeschrock 	/* add 1 for dnode space */
197899653d4eSeschrock 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
197999653d4eSeschrock 	    SPA_MINBLOCKSHIFT) + 1;
1980744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1981fa9e4066Sahrens }
1982fa9e4066Sahrens 
1983fa9e4066Sahrens void
1984fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
1985fa9e4066Sahrens {
1986fa9e4066Sahrens 	uint64_t *buf = vbuf;
1987fa9e4066Sahrens 	size_t count = size >> 3;
1988fa9e4066Sahrens 	int i;
1989fa9e4066Sahrens 
1990fa9e4066Sahrens 	ASSERT((size & 7) == 0);
1991fa9e4066Sahrens 
1992fa9e4066Sahrens 	for (i = 0; i < count; i++)
1993fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
1994fa9e4066Sahrens }
1995fa9e4066Sahrens 
1996fa9e4066Sahrens void
1997fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
1998fa9e4066Sahrens {
1999fa9e4066Sahrens 	uint32_t *buf = vbuf;
2000fa9e4066Sahrens 	size_t count = size >> 2;
2001fa9e4066Sahrens 	int i;
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	ASSERT((size & 3) == 0);
2004fa9e4066Sahrens 
2005fa9e4066Sahrens 	for (i = 0; i < count; i++)
2006fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
2007fa9e4066Sahrens }
2008fa9e4066Sahrens 
2009fa9e4066Sahrens void
2010fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
2011fa9e4066Sahrens {
2012fa9e4066Sahrens 	uint16_t *buf = vbuf;
2013fa9e4066Sahrens 	size_t count = size >> 1;
2014fa9e4066Sahrens 	int i;
2015fa9e4066Sahrens 
2016fa9e4066Sahrens 	ASSERT((size & 1) == 0);
2017fa9e4066Sahrens 
2018fa9e4066Sahrens 	for (i = 0; i < count; i++)
2019fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
2020fa9e4066Sahrens }
2021fa9e4066Sahrens 
2022fa9e4066Sahrens /* ARGSUSED */
2023fa9e4066Sahrens void
2024fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
2025fa9e4066Sahrens {
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens void
2029fa9e4066Sahrens dmu_init(void)
2030fa9e4066Sahrens {
20313f9d6ad7SLin Ling 	zfs_dbgmsg_init();
2032744947dcSTom Erickson 	sa_cache_init();
2033744947dcSTom Erickson 	xuio_stat_init();
2034744947dcSTom Erickson 	dmu_objset_init();
2035fa9e4066Sahrens 	dnode_init();
2036744947dcSTom Erickson 	dbuf_init();
20377cbf8b43SRich Morris 	zfetch_init();
2038fa94a07fSbrendan 	l2arc_init();
2039ce636f8bSMatthew Ahrens 	arc_init();
2040fa9e4066Sahrens }
2041fa9e4066Sahrens 
2042fa9e4066Sahrens void
2043fa9e4066Sahrens dmu_fini(void)
2044fa9e4066Sahrens {
20453e30c24aSWill Andrews 	arc_fini(); /* arc depends on l2arc, so arc must go first */
2046ce636f8bSMatthew Ahrens 	l2arc_fini();
20477cbf8b43SRich Morris 	zfetch_fini();
2048fa9e4066Sahrens 	dbuf_fini();
2049744947dcSTom Erickson 	dnode_fini();
2050744947dcSTom Erickson 	dmu_objset_fini();
2051c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_stat_fini();
20520a586ceaSMark Shellenbaum 	sa_cache_fini();
20533f9d6ad7SLin Ling 	zfs_dbgmsg_fini();
2054fa9e4066Sahrens }
2055