xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision ce636f8b38e8c9ff484e880d9abb27251a882860)
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.
23ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/dmu.h>
27fa9e4066Sahrens #include <sys/dmu_impl.h>
28fa9e4066Sahrens #include <sys/dmu_tx.h>
29fa9e4066Sahrens #include <sys/dbuf.h>
30fa9e4066Sahrens #include <sys/dnode.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
33fa9e4066Sahrens #include <sys/dmu_traverse.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/dsl_dir.h>
36fa9e4066Sahrens #include <sys/dsl_pool.h>
371d452cf5Sahrens #include <sys/dsl_synctask.h>
38a2eea2e1Sahrens #include <sys/dsl_prop.h>
39fa9e4066Sahrens #include <sys/dmu_zfetch.h>
40fa9e4066Sahrens #include <sys/zfs_ioctl.h>
41fa9e4066Sahrens #include <sys/zap.h>
42ea8dc4b6Seschrock #include <sys/zio_checksum.h>
430a586ceaSMark Shellenbaum #include <sys/sa.h>
4444eda4d7Smaybee #ifdef _KERNEL
4544eda4d7Smaybee #include <sys/vmsystm.h>
460fab61baSJonathan W Adams #include <sys/zfs_znode.h>
4744eda4d7Smaybee #endif
48fa9e4066Sahrens 
49fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
50ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"unallocated"		},
51ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"object directory"	},
52ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"object array"		},
53ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"packed nvlist"		},
54ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"packed nvlist size"	},
55ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj"			},
56ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj header"		},
57ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map header"	},
58ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map"		},
59ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"ZIL intent log"	},
60ad135b5dSChristopher Siden 	{	DMU_BSWAP_DNODE,	TRUE,	"DMU dnode"		},
61ad135b5dSChristopher Siden 	{	DMU_BSWAP_OBJSET,	TRUE,	"DMU objset"		},
62ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL directory"		},
63ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL directory child map"},
64ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset snap map"	},
65ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL props"		},
66ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL dataset"		},
67ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZNODE,	TRUE,	"ZFS znode"		},
68ad135b5dSChristopher Siden 	{	DMU_BSWAP_OLDACL,	TRUE,	"ZFS V0 ACL"		},
69ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"ZFS plain file"	},
70ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS directory"		},
71ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS master node"	},
72ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS delete queue"	},
73ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"zvol object"		},
74ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"zvol prop"		},
75ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"other uint8[]"		},
76ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	FALSE,	"other uint64[]"	},
77ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"other ZAP"		},
78ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"persistent error log"	},
79ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"SPA history"		},
80ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA history offsets"	},
81ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"Pool properties"	},
82ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL permissions"	},
83ad135b5dSChristopher Siden 	{	DMU_BSWAP_ACL,		TRUE,	"ZFS ACL"		},
84ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"ZFS SYSACL"		},
85ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"FUID table"		},
86ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"FUID table size"	},
87ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset next clones"},
88ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan work queue"	},
89ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group used"	},
90ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group quota"	},
91ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"snapshot refcount tags"},
92ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT ZAP algorithm"	},
93ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT statistics"	},
94ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"System attributes"	},
95ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA master node"	},
96ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr registration"	},
97ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr layouts"	},
98ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan translations"	},
99ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"deduplicated block"	},
100ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL deadlist map"	},
101ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL deadlist map hdr"	},
102ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dir clones"	},
103ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj subobj"		}
104ad135b5dSChristopher Siden };
105ad135b5dSChristopher Siden 
106ad135b5dSChristopher Siden const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
107ad135b5dSChristopher Siden 	{	byteswap_uint8_array,	"uint8"		},
108ad135b5dSChristopher Siden 	{	byteswap_uint16_array,	"uint16"	},
109ad135b5dSChristopher Siden 	{	byteswap_uint32_array,	"uint32"	},
110ad135b5dSChristopher Siden 	{	byteswap_uint64_array,	"uint64"	},
111ad135b5dSChristopher Siden 	{	zap_byteswap,		"zap"		},
112ad135b5dSChristopher Siden 	{	dnode_buf_byteswap,	"dnode"		},
113ad135b5dSChristopher Siden 	{	dmu_objset_byteswap,	"objset"	},
114ad135b5dSChristopher Siden 	{	zfs_znode_byteswap,	"znode"		},
115ad135b5dSChristopher Siden 	{	zfs_oldacl_byteswap,	"oldacl"	},
116ad135b5dSChristopher Siden 	{	zfs_acl_byteswap,	"acl"		}
1173f9d6ad7SLin Ling };
118fa9e4066Sahrens 
119fa9e4066Sahrens int
120ea8dc4b6Seschrock dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
12147cb52daSJeff Bonwick     void *tag, dmu_buf_t **dbp, int flags)
122fa9e4066Sahrens {
123fa9e4066Sahrens 	dnode_t *dn;
124fa9e4066Sahrens 	uint64_t blkid;
125fa9e4066Sahrens 	dmu_buf_impl_t *db;
126ea8dc4b6Seschrock 	int err;
12747cb52daSJeff Bonwick 	int db_flags = DB_RF_CANFAIL;
12847cb52daSJeff Bonwick 
12947cb52daSJeff Bonwick 	if (flags & DMU_READ_NO_PREFETCH)
13047cb52daSJeff Bonwick 		db_flags |= DB_RF_NOPREFETCH;
131fa9e4066Sahrens 
132503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
133ea8dc4b6Seschrock 	if (err)
134ea8dc4b6Seschrock 		return (err);
135fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
136fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
137ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
138fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
139ea8dc4b6Seschrock 	if (db == NULL) {
140ea8dc4b6Seschrock 		err = EIO;
141ea8dc4b6Seschrock 	} else {
14247cb52daSJeff Bonwick 		err = dbuf_read(db, NULL, db_flags);
143ea8dc4b6Seschrock 		if (err) {
144ea8dc4b6Seschrock 			dbuf_rele(db, tag);
145ea8dc4b6Seschrock 			db = NULL;
146ea8dc4b6Seschrock 		}
147fa9e4066Sahrens 	}
148fa9e4066Sahrens 
149ea8dc4b6Seschrock 	dnode_rele(dn, FTAG);
150744947dcSTom Erickson 	*dbp = &db->db; /* NULL db plus first field offset is NULL */
151ea8dc4b6Seschrock 	return (err);
152fa9e4066Sahrens }
153fa9e4066Sahrens 
154fa9e4066Sahrens int
155fa9e4066Sahrens dmu_bonus_max(void)
156fa9e4066Sahrens {
157fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
158fa9e4066Sahrens }
159fa9e4066Sahrens 
1601934e92fSmaybee int
161744947dcSTom Erickson dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
1621934e92fSmaybee {
163744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
164744947dcSTom Erickson 	dnode_t *dn;
165744947dcSTom Erickson 	int error;
1661934e92fSmaybee 
167744947dcSTom Erickson 	DB_DNODE_ENTER(db);
168744947dcSTom Erickson 	dn = DB_DNODE(db);
169744947dcSTom Erickson 
170744947dcSTom Erickson 	if (dn->dn_bonus != db) {
171744947dcSTom Erickson 		error = EINVAL;
172744947dcSTom Erickson 	} else if (newsize < 0 || newsize > db_fake->db_size) {
173744947dcSTom Erickson 		error = EINVAL;
174744947dcSTom Erickson 	} else {
1751934e92fSmaybee 		dnode_setbonuslen(dn, newsize, tx);
176744947dcSTom Erickson 		error = 0;
177744947dcSTom Erickson 	}
178744947dcSTom Erickson 
179744947dcSTom Erickson 	DB_DNODE_EXIT(db);
180744947dcSTom Erickson 	return (error);
1811934e92fSmaybee }
1821934e92fSmaybee 
1830a586ceaSMark Shellenbaum int
184744947dcSTom Erickson dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
1850a586ceaSMark Shellenbaum {
186744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
187744947dcSTom Erickson 	dnode_t *dn;
188744947dcSTom Erickson 	int error;
1890a586ceaSMark Shellenbaum 
190744947dcSTom Erickson 	DB_DNODE_ENTER(db);
191744947dcSTom Erickson 	dn = DB_DNODE(db);
1920a586ceaSMark Shellenbaum 
193ad135b5dSChristopher Siden 	if (!DMU_OT_IS_VALID(type)) {
194744947dcSTom Erickson 		error = EINVAL;
195744947dcSTom Erickson 	} else if (dn->dn_bonus != db) {
196744947dcSTom Erickson 		error = EINVAL;
197744947dcSTom Erickson 	} else {
1980a586ceaSMark Shellenbaum 		dnode_setbonus_type(dn, type, tx);
199744947dcSTom Erickson 		error = 0;
200744947dcSTom Erickson 	}
201744947dcSTom Erickson 
202744947dcSTom Erickson 	DB_DNODE_EXIT(db);
203744947dcSTom Erickson 	return (error);
204744947dcSTom Erickson }
205744947dcSTom Erickson 
206744947dcSTom Erickson dmu_object_type_t
207744947dcSTom Erickson dmu_get_bonustype(dmu_buf_t *db_fake)
208744947dcSTom Erickson {
209744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
210744947dcSTom Erickson 	dnode_t *dn;
211744947dcSTom Erickson 	dmu_object_type_t type;
212744947dcSTom Erickson 
213744947dcSTom Erickson 	DB_DNODE_ENTER(db);
214744947dcSTom Erickson 	dn = DB_DNODE(db);
215744947dcSTom Erickson 	type = dn->dn_bonustype;
216744947dcSTom Erickson 	DB_DNODE_EXIT(db);
217744947dcSTom Erickson 
218744947dcSTom Erickson 	return (type);
2190a586ceaSMark Shellenbaum }
2200a586ceaSMark Shellenbaum 
2210a586ceaSMark Shellenbaum int
2220a586ceaSMark Shellenbaum dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
2230a586ceaSMark Shellenbaum {
2240a586ceaSMark Shellenbaum 	dnode_t *dn;
2250a586ceaSMark Shellenbaum 	int error;
2260a586ceaSMark Shellenbaum 
2270a586ceaSMark Shellenbaum 	error = dnode_hold(os, object, FTAG, &dn);
2280a586ceaSMark Shellenbaum 	dbuf_rm_spill(dn, tx);
22906e0070dSMark Shellenbaum 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
23006e0070dSMark Shellenbaum 	dnode_rm_spill(dn, tx);
23106e0070dSMark Shellenbaum 	rw_exit(&dn->dn_struct_rwlock);
2320a586ceaSMark Shellenbaum 	dnode_rele(dn, FTAG);
2330a586ceaSMark Shellenbaum 	return (error);
2340a586ceaSMark Shellenbaum }
2350a586ceaSMark Shellenbaum 
236fa9e4066Sahrens /*
237ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
238fa9e4066Sahrens  */
239ea8dc4b6Seschrock int
240ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
241fa9e4066Sahrens {
242ea8dc4b6Seschrock 	dnode_t *dn;
243fa9e4066Sahrens 	dmu_buf_impl_t *db;
2441934e92fSmaybee 	int error;
245fa9e4066Sahrens 
246503ad85cSMatthew Ahrens 	error = dnode_hold(os, object, FTAG, &dn);
2471934e92fSmaybee 	if (error)
2481934e92fSmaybee 		return (error);
249fa9e4066Sahrens 
250fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
251ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
252fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
253ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
254ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
2551934e92fSmaybee 			dbuf_create_bonus(dn);
256fa9e4066Sahrens 	}
257ea8dc4b6Seschrock 	db = dn->dn_bonus;
2581934e92fSmaybee 
2591934e92fSmaybee 	/* as long as the bonus buf is held, the dnode will be held */
260744947dcSTom Erickson 	if (refcount_add(&db->db_holds, tag) == 1) {
2611934e92fSmaybee 		VERIFY(dnode_add_ref(dn, db));
262744947dcSTom Erickson 		(void) atomic_inc_32_nv(&dn->dn_dbufs_count);
263744947dcSTom Erickson 	}
264744947dcSTom Erickson 
265744947dcSTom Erickson 	/*
266744947dcSTom Erickson 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
267744947dcSTom Erickson 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
268744947dcSTom Erickson 	 * a dnode hold for every dbuf.
269744947dcSTom Erickson 	 */
270744947dcSTom Erickson 	rw_exit(&dn->dn_struct_rwlock);
2711934e92fSmaybee 
272fa9e4066Sahrens 	dnode_rele(dn, FTAG);
273ea8dc4b6Seschrock 
27447cb52daSJeff Bonwick 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
275ea8dc4b6Seschrock 
276ea8dc4b6Seschrock 	*dbp = &db->db;
277ea8dc4b6Seschrock 	return (0);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
28013506d1eSmaybee /*
2810a586ceaSMark Shellenbaum  * returns ENOENT, EIO, or 0.
2820a586ceaSMark Shellenbaum  *
2830a586ceaSMark Shellenbaum  * This interface will allocate a blank spill dbuf when a spill blk
2840a586ceaSMark Shellenbaum  * doesn't already exist on the dnode.
2850a586ceaSMark Shellenbaum  *
2860a586ceaSMark Shellenbaum  * if you only want to find an already existing spill db, then
2870a586ceaSMark Shellenbaum  * dmu_spill_hold_existing() should be used.
2880a586ceaSMark Shellenbaum  */
2890a586ceaSMark Shellenbaum int
2900a586ceaSMark Shellenbaum dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
2910a586ceaSMark Shellenbaum {
2920a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
2930a586ceaSMark Shellenbaum 	int err;
2940a586ceaSMark Shellenbaum 
2950a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
2960a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
2970a586ceaSMark Shellenbaum 
2980a586ceaSMark Shellenbaum 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
2990a586ceaSMark Shellenbaum 
3000a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3010a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
3020a586ceaSMark Shellenbaum 
3030a586ceaSMark Shellenbaum 	ASSERT(db != NULL);
3041d8ccc7bSMark Shellenbaum 	err = dbuf_read(db, NULL, flags);
3051d8ccc7bSMark Shellenbaum 	if (err == 0)
3060a586ceaSMark Shellenbaum 		*dbp = &db->db;
3071d8ccc7bSMark Shellenbaum 	else
3081d8ccc7bSMark Shellenbaum 		dbuf_rele(db, tag);
3090a586ceaSMark Shellenbaum 	return (err);
3100a586ceaSMark Shellenbaum }
3110a586ceaSMark Shellenbaum 
3120a586ceaSMark Shellenbaum int
3130a586ceaSMark Shellenbaum dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3140a586ceaSMark Shellenbaum {
315744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
316744947dcSTom Erickson 	dnode_t *dn;
3170a586ceaSMark Shellenbaum 	int err;
3180a586ceaSMark Shellenbaum 
319744947dcSTom Erickson 	DB_DNODE_ENTER(db);
320744947dcSTom Erickson 	dn = DB_DNODE(db);
321744947dcSTom Erickson 
322744947dcSTom Erickson 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
323744947dcSTom Erickson 		err = EINVAL;
324744947dcSTom Erickson 	} else {
3250a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3260a586ceaSMark Shellenbaum 
3270a586ceaSMark Shellenbaum 		if (!dn->dn_have_spill) {
328744947dcSTom Erickson 			err = ENOENT;
329744947dcSTom Erickson 		} else {
3301d8ccc7bSMark Shellenbaum 			err = dmu_spill_hold_by_dnode(dn,
3311d8ccc7bSMark Shellenbaum 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
332744947dcSTom Erickson 		}
333744947dcSTom Erickson 
3340a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
335744947dcSTom Erickson 	}
336744947dcSTom Erickson 
337744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3380a586ceaSMark Shellenbaum 	return (err);
3390a586ceaSMark Shellenbaum }
3400a586ceaSMark Shellenbaum 
3410a586ceaSMark Shellenbaum int
3420a586ceaSMark Shellenbaum dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3430a586ceaSMark Shellenbaum {
344744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
345744947dcSTom Erickson 	dnode_t *dn;
346744947dcSTom Erickson 	int err;
347744947dcSTom Erickson 
348744947dcSTom Erickson 	DB_DNODE_ENTER(db);
349744947dcSTom Erickson 	dn = DB_DNODE(db);
350744947dcSTom Erickson 	err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
351744947dcSTom Erickson 	DB_DNODE_EXIT(db);
352744947dcSTom Erickson 
353744947dcSTom Erickson 	return (err);
3540a586ceaSMark Shellenbaum }
3550a586ceaSMark Shellenbaum 
3560a586ceaSMark Shellenbaum /*
35713506d1eSmaybee  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
35813506d1eSmaybee  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
35913506d1eSmaybee  * and can induce severe lock contention when writing to several files
36013506d1eSmaybee  * whose dnodes are in the same block.
36113506d1eSmaybee  */
36213506d1eSmaybee static int
3637bfdf011SNeil Perrin dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
3647bfdf011SNeil Perrin     int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
365fa9e4066Sahrens {
36605715f94SMark Maybee 	dsl_pool_t *dp = NULL;
367fa9e4066Sahrens 	dmu_buf_t **dbp;
368fa9e4066Sahrens 	uint64_t blkid, nblks, i;
3697bfdf011SNeil Perrin 	uint32_t dbuf_flags;
370ea8dc4b6Seschrock 	int err;
371ea8dc4b6Seschrock 	zio_t *zio;
37205715f94SMark Maybee 	hrtime_t start;
373ea8dc4b6Seschrock 
374ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
375fa9e4066Sahrens 
376c87b8fc5SMark J Musante 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;
3777bfdf011SNeil Perrin 	if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz)
3787bfdf011SNeil Perrin 		dbuf_flags |= DB_RF_NOPREFETCH;
379ea8dc4b6Seschrock 
380fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
381fa9e4066Sahrens 	if (dn->dn_datablkshift) {
382fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
383fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
384fa9e4066Sahrens 		    P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
385fa9e4066Sahrens 	} else {
3860125049cSahrens 		if (offset + length > dn->dn_datablksz) {
3870125049cSahrens 			zfs_panic_recover("zfs: accessing past end of object "
3880125049cSahrens 			    "%llx/%llx (size=%u access=%llu+%llu)",
3890125049cSahrens 			    (longlong_t)dn->dn_objset->
3900125049cSahrens 			    os_dsl_dataset->ds_object,
3910125049cSahrens 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
3920125049cSahrens 			    (longlong_t)offset, (longlong_t)length);
393c87b8fc5SMark J Musante 			rw_exit(&dn->dn_struct_rwlock);
3940125049cSahrens 			return (EIO);
3950125049cSahrens 		}
396fa9e4066Sahrens 		nblks = 1;
397fa9e4066Sahrens 	}
398ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
399fa9e4066Sahrens 
40005715f94SMark Maybee 	if (dn->dn_objset->os_dsl_dataset)
40105715f94SMark Maybee 		dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
40205715f94SMark Maybee 	if (dp && dsl_pool_sync_context(dp))
40305715f94SMark Maybee 		start = gethrtime();
404e14bb325SJeff Bonwick 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
405fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
406fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
407ea8dc4b6Seschrock 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
408ea8dc4b6Seschrock 		if (db == NULL) {
409ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
410ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
411ea8dc4b6Seschrock 			zio_nowait(zio);
412ea8dc4b6Seschrock 			return (EIO);
413ea8dc4b6Seschrock 		}
414ea8dc4b6Seschrock 		/* initiate async i/o */
41513506d1eSmaybee 		if (read) {
4167bfdf011SNeil Perrin 			(void) dbuf_read(db, zio, dbuf_flags);
417ea8dc4b6Seschrock 		}
418ea8dc4b6Seschrock 		dbp[i] = &db->db;
419fa9e4066Sahrens 	}
420fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
421fa9e4066Sahrens 
422ea8dc4b6Seschrock 	/* wait for async i/o */
423ea8dc4b6Seschrock 	err = zio_wait(zio);
42405715f94SMark Maybee 	/* track read overhead when we are in sync context */
42505715f94SMark Maybee 	if (dp && dsl_pool_sync_context(dp))
42605715f94SMark Maybee 		dp->dp_read_overhead += gethrtime() - start;
427ea8dc4b6Seschrock 	if (err) {
428ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
429ea8dc4b6Seschrock 		return (err);
430ea8dc4b6Seschrock 	}
431ea8dc4b6Seschrock 
432ea8dc4b6Seschrock 	/* wait for other io to complete */
433ea8dc4b6Seschrock 	if (read) {
434ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
435ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
436ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
437ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
438ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
439ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
440ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
441ea8dc4b6Seschrock 				err = EIO;
442ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
443ea8dc4b6Seschrock 			if (err) {
444ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
445ea8dc4b6Seschrock 				return (err);
446ea8dc4b6Seschrock 			}
447ea8dc4b6Seschrock 		}
448ea8dc4b6Seschrock 	}
449ea8dc4b6Seschrock 
450ea8dc4b6Seschrock 	*numbufsp = nblks;
451ea8dc4b6Seschrock 	*dbpp = dbp;
452ea8dc4b6Seschrock 	return (0);
453fa9e4066Sahrens }
454fa9e4066Sahrens 
455a2eea2e1Sahrens static int
45613506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
45713506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
45813506d1eSmaybee {
45913506d1eSmaybee 	dnode_t *dn;
46013506d1eSmaybee 	int err;
46113506d1eSmaybee 
462503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
46313506d1eSmaybee 	if (err)
46413506d1eSmaybee 		return (err);
46513506d1eSmaybee 
46613506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
4677bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
46813506d1eSmaybee 
46913506d1eSmaybee 	dnode_rele(dn, FTAG);
47013506d1eSmaybee 
47113506d1eSmaybee 	return (err);
47213506d1eSmaybee }
47313506d1eSmaybee 
47413506d1eSmaybee int
475744947dcSTom Erickson dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
47613506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
47713506d1eSmaybee {
478744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
479744947dcSTom Erickson 	dnode_t *dn;
48013506d1eSmaybee 	int err;
48113506d1eSmaybee 
482744947dcSTom Erickson 	DB_DNODE_ENTER(db);
483744947dcSTom Erickson 	dn = DB_DNODE(db);
48413506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
4857bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
486744947dcSTom Erickson 	DB_DNODE_EXIT(db);
48713506d1eSmaybee 
48813506d1eSmaybee 	return (err);
48913506d1eSmaybee }
49013506d1eSmaybee 
491fa9e4066Sahrens void
492ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
493fa9e4066Sahrens {
494fa9e4066Sahrens 	int i;
495fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
496fa9e4066Sahrens 
497fa9e4066Sahrens 	if (numbufs == 0)
498fa9e4066Sahrens 		return;
499fa9e4066Sahrens 
500ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
501ea8dc4b6Seschrock 		if (dbp[i])
502ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
503ea8dc4b6Seschrock 	}
504fa9e4066Sahrens 
505fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
506fa9e4066Sahrens }
507fa9e4066Sahrens 
508fa9e4066Sahrens void
509fa9e4066Sahrens dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
510fa9e4066Sahrens {
511fa9e4066Sahrens 	dnode_t *dn;
512fa9e4066Sahrens 	uint64_t blkid;
513ea8dc4b6Seschrock 	int nblks, i, err;
514fa9e4066Sahrens 
515416e0cd8Sek110237 	if (zfs_prefetch_disable)
516416e0cd8Sek110237 		return;
517416e0cd8Sek110237 
518fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
519744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
520fa9e4066Sahrens 
521fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
522fa9e4066Sahrens 			return;
523fa9e4066Sahrens 
524fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
525fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
526fa9e4066Sahrens 		dbuf_prefetch(dn, blkid);
527fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
528fa9e4066Sahrens 		return;
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 
531fa9e4066Sahrens 	/*
532fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
533fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
534fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
535fa9e4066Sahrens 	 */
536503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
537ea8dc4b6Seschrock 	if (err != 0)
538fa9e4066Sahrens 		return;
539fa9e4066Sahrens 
540fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
541fa9e4066Sahrens 	if (dn->dn_datablkshift) {
542fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
543fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
544fa9e4066Sahrens 		    P2ALIGN(offset, 1<<blkshift)) >> blkshift;
545fa9e4066Sahrens 	} else {
546fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
547fa9e4066Sahrens 	}
548fa9e4066Sahrens 
549fa9e4066Sahrens 	if (nblks != 0) {
550fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, offset);
551fa9e4066Sahrens 		for (i = 0; i < nblks; i++)
552fa9e4066Sahrens 			dbuf_prefetch(dn, blkid+i);
553fa9e4066Sahrens 	}
554fa9e4066Sahrens 
555fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
556fa9e4066Sahrens 
557fa9e4066Sahrens 	dnode_rele(dn, FTAG);
558fa9e4066Sahrens }
559fa9e4066Sahrens 
56076256205SMark Maybee /*
56176256205SMark Maybee  * Get the next "chunk" of file data to free.  We traverse the file from
56276256205SMark Maybee  * the end so that the file gets shorter over time (if we crashes in the
56376256205SMark Maybee  * middle, this will leave us in a better state).  We find allocated file
56476256205SMark Maybee  * data by simply searching the allocated level 1 indirects.
56576256205SMark Maybee  */
566cdb0ab79Smaybee static int
56776256205SMark Maybee get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit)
568cdb0ab79Smaybee {
56976256205SMark Maybee 	uint64_t len = *start - limit;
57076256205SMark Maybee 	uint64_t blkcnt = 0;
57176256205SMark Maybee 	uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1));
57276256205SMark Maybee 	uint64_t iblkrange =
5731c8564a7SMark Maybee 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
574cdb0ab79Smaybee 
57576256205SMark Maybee 	ASSERT(limit <= *start);
576cdb0ab79Smaybee 
57776256205SMark Maybee 	if (len <= iblkrange * maxblks) {
57876256205SMark Maybee 		*start = limit;
579cdb0ab79Smaybee 		return (0);
580cdb0ab79Smaybee 	}
58176256205SMark Maybee 	ASSERT(ISP2(iblkrange));
582cdb0ab79Smaybee 
58376256205SMark Maybee 	while (*start > limit && blkcnt < maxblks) {
5841c8564a7SMark Maybee 		int err;
585cdb0ab79Smaybee 
58676256205SMark Maybee 		/* find next allocated L1 indirect */
587cdb0ab79Smaybee 		err = dnode_next_offset(dn,
58876256205SMark Maybee 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
589cdb0ab79Smaybee 
59076256205SMark Maybee 		/* if there are no more, then we are done */
59176256205SMark Maybee 		if (err == ESRCH) {
59276256205SMark Maybee 			*start = limit;
593cdb0ab79Smaybee 			return (0);
59476256205SMark Maybee 		} else if (err) {
595cdb0ab79Smaybee 			return (err);
59676256205SMark Maybee 		}
59776256205SMark Maybee 		blkcnt += 1;
598cdb0ab79Smaybee 
59976256205SMark Maybee 		/* reset offset to end of "next" block back */
60076256205SMark Maybee 		*start = P2ALIGN(*start, iblkrange);
60176256205SMark Maybee 		if (*start <= limit)
60276256205SMark Maybee 			*start = limit;
60376256205SMark Maybee 		else
60476256205SMark Maybee 			*start -= 1;
605cdb0ab79Smaybee 	}
606cdb0ab79Smaybee 	return (0);
607cdb0ab79Smaybee }
608cdb0ab79Smaybee 
609cdb0ab79Smaybee static int
610cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
611cdb0ab79Smaybee     uint64_t length, boolean_t free_dnode)
612cdb0ab79Smaybee {
613cdb0ab79Smaybee 	dmu_tx_t *tx;
614cdb0ab79Smaybee 	uint64_t object_size, start, end, len;
615cdb0ab79Smaybee 	boolean_t trunc = (length == DMU_OBJECT_END);
616cdb0ab79Smaybee 	int align, err;
617cdb0ab79Smaybee 
618cdb0ab79Smaybee 	align = 1 << dn->dn_datablkshift;
619cdb0ab79Smaybee 	ASSERT(align > 0);
620cdb0ab79Smaybee 	object_size = align == 1 ? dn->dn_datablksz :
621cdb0ab79Smaybee 	    (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
622cdb0ab79Smaybee 
62314843421SMatthew Ahrens 	end = offset + length;
62414843421SMatthew Ahrens 	if (trunc || end > object_size)
625cdb0ab79Smaybee 		end = object_size;
626cdb0ab79Smaybee 	if (end <= offset)
627cdb0ab79Smaybee 		return (0);
628cdb0ab79Smaybee 	length = end - offset;
629cdb0ab79Smaybee 
630cdb0ab79Smaybee 	while (length) {
631cdb0ab79Smaybee 		start = end;
63214843421SMatthew Ahrens 		/* assert(offset <= start) */
633cdb0ab79Smaybee 		err = get_next_chunk(dn, &start, offset);
634cdb0ab79Smaybee 		if (err)
635cdb0ab79Smaybee 			return (err);
636cdb0ab79Smaybee 		len = trunc ? DMU_OBJECT_END : end - start;
637cdb0ab79Smaybee 
638cdb0ab79Smaybee 		tx = dmu_tx_create(os);
639cdb0ab79Smaybee 		dmu_tx_hold_free(tx, dn->dn_object, start, len);
640cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
641cdb0ab79Smaybee 		if (err) {
642cdb0ab79Smaybee 			dmu_tx_abort(tx);
643cdb0ab79Smaybee 			return (err);
644cdb0ab79Smaybee 		}
645cdb0ab79Smaybee 
646cdb0ab79Smaybee 		dnode_free_range(dn, start, trunc ? -1 : len, tx);
647cdb0ab79Smaybee 
6481c8564a7SMark Maybee 		if (start == 0 && free_dnode) {
6491c8564a7SMark Maybee 			ASSERT(trunc);
650cdb0ab79Smaybee 			dnode_free(dn, tx);
6511c8564a7SMark Maybee 		}
652cdb0ab79Smaybee 
653cdb0ab79Smaybee 		length -= end - start;
654cdb0ab79Smaybee 
655cdb0ab79Smaybee 		dmu_tx_commit(tx);
656cdb0ab79Smaybee 		end = start;
657cdb0ab79Smaybee 	}
658cdb0ab79Smaybee 	return (0);
659cdb0ab79Smaybee }
660cdb0ab79Smaybee 
661cdb0ab79Smaybee int
662cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object,
663cdb0ab79Smaybee     uint64_t offset, uint64_t length)
664cdb0ab79Smaybee {
665cdb0ab79Smaybee 	dnode_t *dn;
666cdb0ab79Smaybee 	int err;
667cdb0ab79Smaybee 
668503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
669cdb0ab79Smaybee 	if (err != 0)
670cdb0ab79Smaybee 		return (err);
671cdb0ab79Smaybee 	err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
672cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
673cdb0ab79Smaybee 	return (err);
674cdb0ab79Smaybee }
675cdb0ab79Smaybee 
676cdb0ab79Smaybee int
677cdb0ab79Smaybee dmu_free_object(objset_t *os, uint64_t object)
678cdb0ab79Smaybee {
679cdb0ab79Smaybee 	dnode_t *dn;
680cdb0ab79Smaybee 	dmu_tx_t *tx;
681cdb0ab79Smaybee 	int err;
682cdb0ab79Smaybee 
683503ad85cSMatthew Ahrens 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
684cdb0ab79Smaybee 	    FTAG, &dn);
685cdb0ab79Smaybee 	if (err != 0)
686cdb0ab79Smaybee 		return (err);
687cdb0ab79Smaybee 	if (dn->dn_nlevels == 1) {
688cdb0ab79Smaybee 		tx = dmu_tx_create(os);
689cdb0ab79Smaybee 		dmu_tx_hold_bonus(tx, object);
690cdb0ab79Smaybee 		dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
691cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
692cdb0ab79Smaybee 		if (err == 0) {
693cdb0ab79Smaybee 			dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
694cdb0ab79Smaybee 			dnode_free(dn, tx);
695cdb0ab79Smaybee 			dmu_tx_commit(tx);
696cdb0ab79Smaybee 		} else {
697cdb0ab79Smaybee 			dmu_tx_abort(tx);
698cdb0ab79Smaybee 		}
699cdb0ab79Smaybee 	} else {
700cdb0ab79Smaybee 		err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
701cdb0ab79Smaybee 	}
702cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
703cdb0ab79Smaybee 	return (err);
704cdb0ab79Smaybee }
705cdb0ab79Smaybee 
706ea8dc4b6Seschrock int
707fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
708fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
709fa9e4066Sahrens {
710ea8dc4b6Seschrock 	dnode_t *dn;
711503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
712ea8dc4b6Seschrock 	if (err)
713ea8dc4b6Seschrock 		return (err);
714fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
715fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
716fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
717fa9e4066Sahrens 	dnode_rele(dn, FTAG);
718ea8dc4b6Seschrock 	return (0);
719fa9e4066Sahrens }
720fa9e4066Sahrens 
721ea8dc4b6Seschrock int
722ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
7237bfdf011SNeil Perrin     void *buf, uint32_t flags)
724fa9e4066Sahrens {
725fa9e4066Sahrens 	dnode_t *dn;
726fa9e4066Sahrens 	dmu_buf_t **dbp;
727c87b8fc5SMark J Musante 	int numbufs, err;
728fa9e4066Sahrens 
729503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
730ea8dc4b6Seschrock 	if (err)
731ea8dc4b6Seschrock 		return (err);
732feb08c6bSbillm 
733feb08c6bSbillm 	/*
734feb08c6bSbillm 	 * Deal with odd block sizes, where there can't be data past the first
735feb08c6bSbillm 	 * block.  If we ever do the tail block optimization, we will need to
736feb08c6bSbillm 	 * handle that here as well.
737feb08c6bSbillm 	 */
738c87b8fc5SMark J Musante 	if (dn->dn_maxblkid == 0) {
739fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
740fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
741fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
742fa9e4066Sahrens 		size = newsz;
743fa9e4066Sahrens 	}
744fa9e4066Sahrens 
745fa9e4066Sahrens 	while (size > 0) {
746fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
747c87b8fc5SMark J Musante 		int i;
748fa9e4066Sahrens 
749fa9e4066Sahrens 		/*
750fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
751fa9e4066Sahrens 		 * to be reading in parallel.
752fa9e4066Sahrens 		 */
753a2eea2e1Sahrens 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
7547bfdf011SNeil Perrin 		    TRUE, FTAG, &numbufs, &dbp, flags);
755ea8dc4b6Seschrock 		if (err)
7561934e92fSmaybee 			break;
757fa9e4066Sahrens 
758fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
759fa9e4066Sahrens 			int tocpy;
760fa9e4066Sahrens 			int bufoff;
761fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
762fa9e4066Sahrens 
763fa9e4066Sahrens 			ASSERT(size > 0);
764fa9e4066Sahrens 
765fa9e4066Sahrens 			bufoff = offset - db->db_offset;
766fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
767fa9e4066Sahrens 
768fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
769fa9e4066Sahrens 
770fa9e4066Sahrens 			offset += tocpy;
771fa9e4066Sahrens 			size -= tocpy;
772fa9e4066Sahrens 			buf = (char *)buf + tocpy;
773fa9e4066Sahrens 		}
774ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
775fa9e4066Sahrens 	}
776a2eea2e1Sahrens 	dnode_rele(dn, FTAG);
7771934e92fSmaybee 	return (err);
778fa9e4066Sahrens }
779fa9e4066Sahrens 
780fa9e4066Sahrens void
781fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
782fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
783fa9e4066Sahrens {
784fa9e4066Sahrens 	dmu_buf_t **dbp;
785fa9e4066Sahrens 	int numbufs, i;
786fa9e4066Sahrens 
78713506d1eSmaybee 	if (size == 0)
78813506d1eSmaybee 		return;
78913506d1eSmaybee 
790ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
791ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
792fa9e4066Sahrens 
793fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
794fa9e4066Sahrens 		int tocpy;
795fa9e4066Sahrens 		int bufoff;
796fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
797fa9e4066Sahrens 
798fa9e4066Sahrens 		ASSERT(size > 0);
799fa9e4066Sahrens 
800fa9e4066Sahrens 		bufoff = offset - db->db_offset;
801fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
802fa9e4066Sahrens 
803fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
804fa9e4066Sahrens 
805fa9e4066Sahrens 		if (tocpy == db->db_size)
806fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
807fa9e4066Sahrens 		else
808fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
809fa9e4066Sahrens 
810fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
811fa9e4066Sahrens 
812fa9e4066Sahrens 		if (tocpy == db->db_size)
813fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
814fa9e4066Sahrens 
815fa9e4066Sahrens 		offset += tocpy;
816fa9e4066Sahrens 		size -= tocpy;
817fa9e4066Sahrens 		buf = (char *)buf + tocpy;
818fa9e4066Sahrens 	}
819ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
820fa9e4066Sahrens }
821fa9e4066Sahrens 
82282c9918fSTim Haley void
82382c9918fSTim Haley dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
82482c9918fSTim Haley     dmu_tx_t *tx)
82582c9918fSTim Haley {
82682c9918fSTim Haley 	dmu_buf_t **dbp;
82782c9918fSTim Haley 	int numbufs, i;
82882c9918fSTim Haley 
82982c9918fSTim Haley 	if (size == 0)
83082c9918fSTim Haley 		return;
83182c9918fSTim Haley 
83282c9918fSTim Haley 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
83382c9918fSTim Haley 	    FALSE, FTAG, &numbufs, &dbp));
83482c9918fSTim Haley 
83582c9918fSTim Haley 	for (i = 0; i < numbufs; i++) {
83682c9918fSTim Haley 		dmu_buf_t *db = dbp[i];
83782c9918fSTim Haley 
83882c9918fSTim Haley 		dmu_buf_will_not_fill(db, tx);
83982c9918fSTim Haley 	}
84082c9918fSTim Haley 	dmu_buf_rele_array(dbp, numbufs, FTAG);
84182c9918fSTim Haley }
84282c9918fSTim Haley 
843c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
844c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * DMU support for xuio
845c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
846c242f9a0Schunli zhang - Sun Microsystems - Irvine United States kstat_t *xuio_ksp = NULL;
847c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
848c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
849c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_init(xuio_t *xuio, int nblk)
850c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
851c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv;
852c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
853c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
854c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iovcnt = nblk;
855c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
856c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
857c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
858c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->cnt = nblk;
859c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
860c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->iovp = uio->uio_iov;
861c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIO_XUZC_PRIV(xuio) = priv;
862c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
863c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
864c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
865c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
866c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
867c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
868c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
869c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
870c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
871c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
872c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_fini(xuio_t *xuio)
873c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
874c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
875c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int nblk = priv->cnt;
876c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
877c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
878c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
879c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv, sizeof (dmu_xuio_t));
880c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
881c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
882c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
883c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
884c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
885c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
886c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
887c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
888c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
889c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * and increase priv->next by 1.
890c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
891c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
892c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
893c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
894c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	struct iovec *iov;
895c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
896c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
897c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int i = priv->next++;
898c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
899c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
900c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(off + n <= arc_buf_size(abuf));
901c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov = uio->uio_iov + i;
902c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_base = (char *)abuf->b_data + off;
903c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_len = n;
904c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = abuf;
905c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
906c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
907c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
908c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
909c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_cnt(xuio_t *xuio)
910c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
911c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
912c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->cnt);
913c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
914c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
915c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_buf_t *
916c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_arcbuf(xuio_t *xuio, int i)
917c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
918c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
919c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
920c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
921c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->bufs[i]);
922c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
923c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
924c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
925c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_clear(xuio_t *xuio, int i)
926c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
927c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
928c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
929c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
930c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = NULL;
931c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
932c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
933c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
934c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_init(void)
935c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
936c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
937c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
938c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_FLAG_VIRTUAL);
939c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
940c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp->ks_data = &xuio_stats;
941c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_install(xuio_ksp);
942c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
943c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
944c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
945c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
946c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_fini(void)
947c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
948c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
949c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_delete(xuio_ksp);
950c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp = NULL;
951c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
952c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
953c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
954c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
955c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_copied()
956c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
957c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
958c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
959c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
960c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
961c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_nocopy()
962c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
963c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
964c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
965c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
966fa9e4066Sahrens #ifdef _KERNEL
967fa9e4066Sahrens int
968feb08c6bSbillm dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
969feb08c6bSbillm {
970feb08c6bSbillm 	dmu_buf_t **dbp;
971feb08c6bSbillm 	int numbufs, i, err;
972c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_t *xuio = NULL;
973feb08c6bSbillm 
974feb08c6bSbillm 	/*
975feb08c6bSbillm 	 * NB: we could do this block-at-a-time, but it's nice
976feb08c6bSbillm 	 * to be reading in parallel.
977feb08c6bSbillm 	 */
978feb08c6bSbillm 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
979feb08c6bSbillm 	    &numbufs, &dbp);
980feb08c6bSbillm 	if (err)
981feb08c6bSbillm 		return (err);
982feb08c6bSbillm 
983c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (uio->uio_extflg == UIO_XUIO)
984c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio = (xuio_t *)uio;
985c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
986feb08c6bSbillm 	for (i = 0; i < numbufs; i++) {
987feb08c6bSbillm 		int tocpy;
988feb08c6bSbillm 		int bufoff;
989feb08c6bSbillm 		dmu_buf_t *db = dbp[i];
990feb08c6bSbillm 
991feb08c6bSbillm 		ASSERT(size > 0);
992feb08c6bSbillm 
993feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
994feb08c6bSbillm 		tocpy = (int)MIN(db->db_size - bufoff, size);
995feb08c6bSbillm 
996c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (xuio) {
997c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
998c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *dbuf_abuf = dbi->db_buf;
999c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1000c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1001c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (!err) {
1002c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_resid -= tocpy;
1003c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_loffset += tocpy;
1004c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			}
1005c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1006c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (abuf == dbuf_abuf)
1007c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1008c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			else
1009c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1010c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		} else {
1011feb08c6bSbillm 			err = uiomove((char *)db->db_data + bufoff, tocpy,
1012feb08c6bSbillm 			    UIO_READ, uio);
1013c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		}
1014feb08c6bSbillm 		if (err)
1015feb08c6bSbillm 			break;
1016feb08c6bSbillm 
1017feb08c6bSbillm 		size -= tocpy;
1018feb08c6bSbillm 	}
1019feb08c6bSbillm 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1020feb08c6bSbillm 
1021feb08c6bSbillm 	return (err);
1022feb08c6bSbillm }
1023feb08c6bSbillm 
102494d1a210STim Haley static int
102594d1a210STim Haley dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1026fa9e4066Sahrens {
1027fa9e4066Sahrens 	dmu_buf_t **dbp;
102894d1a210STim Haley 	int numbufs;
1029fa9e4066Sahrens 	int err = 0;
103094d1a210STim Haley 	int i;
1031fa9e4066Sahrens 
103294d1a210STim Haley 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
103394d1a210STim Haley 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1034ea8dc4b6Seschrock 	if (err)
1035ea8dc4b6Seschrock 		return (err);
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
1038fa9e4066Sahrens 		int tocpy;
1039fa9e4066Sahrens 		int bufoff;
1040fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 		ASSERT(size > 0);
1043fa9e4066Sahrens 
1044feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1045fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
1046fa9e4066Sahrens 
1047fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1048fa9e4066Sahrens 
1049fa9e4066Sahrens 		if (tocpy == db->db_size)
1050fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
1051fa9e4066Sahrens 		else
1052fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 		/*
1055fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
1056fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
1057fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
1058fa9e4066Sahrens 		 * block.
1059fa9e4066Sahrens 		 */
1060fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
1061fa9e4066Sahrens 		    UIO_WRITE, uio);
1062fa9e4066Sahrens 
1063fa9e4066Sahrens 		if (tocpy == db->db_size)
1064fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 		if (err)
1067fa9e4066Sahrens 			break;
1068fa9e4066Sahrens 
1069fa9e4066Sahrens 		size -= tocpy;
1070fa9e4066Sahrens 	}
107194d1a210STim Haley 
1072ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1073fa9e4066Sahrens 	return (err);
1074fa9e4066Sahrens }
107544eda4d7Smaybee 
107644eda4d7Smaybee int
107794d1a210STim Haley dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
107894d1a210STim Haley     dmu_tx_t *tx)
107994d1a210STim Haley {
1080744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1081744947dcSTom Erickson 	dnode_t *dn;
1082744947dcSTom Erickson 	int err;
1083744947dcSTom Erickson 
108494d1a210STim Haley 	if (size == 0)
108594d1a210STim Haley 		return (0);
108694d1a210STim Haley 
1087744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1088744947dcSTom Erickson 	dn = DB_DNODE(db);
1089744947dcSTom Erickson 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1090744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1091744947dcSTom Erickson 
1092744947dcSTom Erickson 	return (err);
109394d1a210STim Haley }
109494d1a210STim Haley 
109594d1a210STim Haley int
109694d1a210STim Haley dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
109794d1a210STim Haley     dmu_tx_t *tx)
109894d1a210STim Haley {
109994d1a210STim Haley 	dnode_t *dn;
110094d1a210STim Haley 	int err;
110194d1a210STim Haley 
110294d1a210STim Haley 	if (size == 0)
110394d1a210STim Haley 		return (0);
110494d1a210STim Haley 
110594d1a210STim Haley 	err = dnode_hold(os, object, FTAG, &dn);
110694d1a210STim Haley 	if (err)
110794d1a210STim Haley 		return (err);
110894d1a210STim Haley 
110994d1a210STim Haley 	err = dmu_write_uio_dnode(dn, uio, size, tx);
111094d1a210STim Haley 
111194d1a210STim Haley 	dnode_rele(dn, FTAG);
111294d1a210STim Haley 
111394d1a210STim Haley 	return (err);
111494d1a210STim Haley }
111594d1a210STim Haley 
111694d1a210STim Haley int
111744eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
111844eda4d7Smaybee     page_t *pp, dmu_tx_t *tx)
111944eda4d7Smaybee {
112044eda4d7Smaybee 	dmu_buf_t **dbp;
112144eda4d7Smaybee 	int numbufs, i;
112244eda4d7Smaybee 	int err;
112344eda4d7Smaybee 
112444eda4d7Smaybee 	if (size == 0)
112544eda4d7Smaybee 		return (0);
112644eda4d7Smaybee 
112744eda4d7Smaybee 	err = dmu_buf_hold_array(os, object, offset, size,
112844eda4d7Smaybee 	    FALSE, FTAG, &numbufs, &dbp);
112944eda4d7Smaybee 	if (err)
113044eda4d7Smaybee 		return (err);
113144eda4d7Smaybee 
113244eda4d7Smaybee 	for (i = 0; i < numbufs; i++) {
113344eda4d7Smaybee 		int tocpy, copied, thiscpy;
113444eda4d7Smaybee 		int bufoff;
113544eda4d7Smaybee 		dmu_buf_t *db = dbp[i];
113644eda4d7Smaybee 		caddr_t va;
113744eda4d7Smaybee 
113844eda4d7Smaybee 		ASSERT(size > 0);
113944eda4d7Smaybee 		ASSERT3U(db->db_size, >=, PAGESIZE);
114044eda4d7Smaybee 
114144eda4d7Smaybee 		bufoff = offset - db->db_offset;
114244eda4d7Smaybee 		tocpy = (int)MIN(db->db_size - bufoff, size);
114344eda4d7Smaybee 
114444eda4d7Smaybee 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
114544eda4d7Smaybee 
114644eda4d7Smaybee 		if (tocpy == db->db_size)
114744eda4d7Smaybee 			dmu_buf_will_fill(db, tx);
114844eda4d7Smaybee 		else
114944eda4d7Smaybee 			dmu_buf_will_dirty(db, tx);
115044eda4d7Smaybee 
115144eda4d7Smaybee 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
115244eda4d7Smaybee 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
115344eda4d7Smaybee 			thiscpy = MIN(PAGESIZE, tocpy - copied);
11540fab61baSJonathan W Adams 			va = zfs_map_page(pp, S_READ);
115544eda4d7Smaybee 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
11560fab61baSJonathan W Adams 			zfs_unmap_page(pp, va);
115744eda4d7Smaybee 			pp = pp->p_next;
115844eda4d7Smaybee 			bufoff += PAGESIZE;
115944eda4d7Smaybee 		}
116044eda4d7Smaybee 
116144eda4d7Smaybee 		if (tocpy == db->db_size)
116244eda4d7Smaybee 			dmu_buf_fill_done(db, tx);
116344eda4d7Smaybee 
116444eda4d7Smaybee 		offset += tocpy;
116544eda4d7Smaybee 		size -= tocpy;
116644eda4d7Smaybee 	}
116744eda4d7Smaybee 	dmu_buf_rele_array(dbp, numbufs, FTAG);
116844eda4d7Smaybee 	return (err);
116944eda4d7Smaybee }
1170fa9e4066Sahrens #endif
1171fa9e4066Sahrens 
11722fdbea25SAleksandr Guzovskiy /*
11732fdbea25SAleksandr Guzovskiy  * Allocate a loaned anonymous arc buffer.
11742fdbea25SAleksandr Guzovskiy  */
11752fdbea25SAleksandr Guzovskiy arc_buf_t *
11762fdbea25SAleksandr Guzovskiy dmu_request_arcbuf(dmu_buf_t *handle, int size)
11772fdbea25SAleksandr Guzovskiy {
1178744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1179744947dcSTom Erickson 	spa_t *spa;
11802fdbea25SAleksandr Guzovskiy 
1181744947dcSTom Erickson 	DB_GET_SPA(&spa, db);
1182744947dcSTom Erickson 	return (arc_loan_buf(spa, size));
11832fdbea25SAleksandr Guzovskiy }
11842fdbea25SAleksandr Guzovskiy 
11852fdbea25SAleksandr Guzovskiy /*
11862fdbea25SAleksandr Guzovskiy  * Free a loaned arc buffer.
11872fdbea25SAleksandr Guzovskiy  */
11882fdbea25SAleksandr Guzovskiy void
11892fdbea25SAleksandr Guzovskiy dmu_return_arcbuf(arc_buf_t *buf)
11902fdbea25SAleksandr Guzovskiy {
11912fdbea25SAleksandr Guzovskiy 	arc_return_buf(buf, FTAG);
11922fdbea25SAleksandr Guzovskiy 	VERIFY(arc_buf_remove_ref(buf, FTAG) == 1);
11932fdbea25SAleksandr Guzovskiy }
11942fdbea25SAleksandr Guzovskiy 
11952fdbea25SAleksandr Guzovskiy /*
11962fdbea25SAleksandr Guzovskiy  * When possible directly assign passed loaned arc buffer to a dbuf.
11972fdbea25SAleksandr Guzovskiy  * If this is not possible copy the contents of passed arc buf via
11982fdbea25SAleksandr Guzovskiy  * dmu_write().
11992fdbea25SAleksandr Guzovskiy  */
12002fdbea25SAleksandr Guzovskiy void
12012fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
12022fdbea25SAleksandr Guzovskiy     dmu_tx_t *tx)
12032fdbea25SAleksandr Guzovskiy {
1204744947dcSTom Erickson 	dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1205744947dcSTom Erickson 	dnode_t *dn;
12062fdbea25SAleksandr Guzovskiy 	dmu_buf_impl_t *db;
12072fdbea25SAleksandr Guzovskiy 	uint32_t blksz = (uint32_t)arc_buf_size(buf);
12082fdbea25SAleksandr Guzovskiy 	uint64_t blkid;
12092fdbea25SAleksandr Guzovskiy 
1210744947dcSTom Erickson 	DB_DNODE_ENTER(dbuf);
1211744947dcSTom Erickson 	dn = DB_DNODE(dbuf);
12122fdbea25SAleksandr Guzovskiy 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
12132fdbea25SAleksandr Guzovskiy 	blkid = dbuf_whichblock(dn, offset);
12142fdbea25SAleksandr Guzovskiy 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
12152fdbea25SAleksandr Guzovskiy 	rw_exit(&dn->dn_struct_rwlock);
1216744947dcSTom Erickson 	DB_DNODE_EXIT(dbuf);
12172fdbea25SAleksandr Guzovskiy 
12182fdbea25SAleksandr Guzovskiy 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
12192fdbea25SAleksandr Guzovskiy 		dbuf_assign_arcbuf(db, buf, tx);
12202fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
12212fdbea25SAleksandr Guzovskiy 	} else {
1222744947dcSTom Erickson 		objset_t *os;
1223744947dcSTom Erickson 		uint64_t object;
1224744947dcSTom Erickson 
1225744947dcSTom Erickson 		DB_DNODE_ENTER(dbuf);
1226744947dcSTom Erickson 		dn = DB_DNODE(dbuf);
1227744947dcSTom Erickson 		os = dn->dn_objset;
1228744947dcSTom Erickson 		object = dn->dn_object;
1229744947dcSTom Erickson 		DB_DNODE_EXIT(dbuf);
1230744947dcSTom Erickson 
12312fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
1232744947dcSTom Erickson 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
12332fdbea25SAleksandr Guzovskiy 		dmu_return_arcbuf(buf);
1234c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
12352fdbea25SAleksandr Guzovskiy 	}
12362fdbea25SAleksandr Guzovskiy }
12372fdbea25SAleksandr Guzovskiy 
1238c5c6ffa0Smaybee typedef struct {
1239b24ab676SJeff Bonwick 	dbuf_dirty_record_t	*dsa_dr;
1240b24ab676SJeff Bonwick 	dmu_sync_cb_t		*dsa_done;
1241b24ab676SJeff Bonwick 	zgd_t			*dsa_zgd;
1242b24ab676SJeff Bonwick 	dmu_tx_t		*dsa_tx;
1243c717a561Smaybee } dmu_sync_arg_t;
1244c5c6ffa0Smaybee 
1245c5c6ffa0Smaybee /* ARGSUSED */
1246c5c6ffa0Smaybee static void
1247e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1248e14bb325SJeff Bonwick {
1249b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1250b24ab676SJeff Bonwick 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1251e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1252975c32a0SNeil Perrin 
1253b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
1254b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp)) {
1255b24ab676SJeff Bonwick 			/*
1256b24ab676SJeff Bonwick 			 * A block of zeros may compress to a hole, but the
1257b24ab676SJeff Bonwick 			 * block size still needs to be known for replay.
1258b24ab676SJeff Bonwick 			 */
1259b24ab676SJeff Bonwick 			BP_SET_LSIZE(bp, db->db_size);
1260b24ab676SJeff Bonwick 		} else {
1261e14bb325SJeff Bonwick 			ASSERT(BP_GET_LEVEL(bp) == 0);
1262e14bb325SJeff Bonwick 			bp->blk_fill = 1;
1263e14bb325SJeff Bonwick 		}
1264e14bb325SJeff Bonwick 	}
1265b24ab676SJeff Bonwick }
1266b24ab676SJeff Bonwick 
1267b24ab676SJeff Bonwick static void
1268b24ab676SJeff Bonwick dmu_sync_late_arrival_ready(zio_t *zio)
1269b24ab676SJeff Bonwick {
1270b24ab676SJeff Bonwick 	dmu_sync_ready(zio, NULL, zio->io_private);
1271b24ab676SJeff Bonwick }
1272e14bb325SJeff Bonwick 
1273e14bb325SJeff Bonwick /* ARGSUSED */
1274e14bb325SJeff Bonwick static void
1275c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1276c5c6ffa0Smaybee {
1277b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1278b24ab676SJeff Bonwick 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1279c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1280c5c6ffa0Smaybee 
1281b50a0fe0SNeil Perrin 	mutex_enter(&db->db_mtx);
1282b50a0fe0SNeil Perrin 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1283b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
1284b24ab676SJeff Bonwick 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1285b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1286b24ab676SJeff Bonwick 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1287b50a0fe0SNeil Perrin 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by))
1288b50a0fe0SNeil Perrin 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1289b24ab676SJeff Bonwick 	} else {
1290b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1291b24ab676SJeff Bonwick 	}
1292c5c6ffa0Smaybee 	cv_broadcast(&db->db_changed);
1293b50a0fe0SNeil Perrin 	mutex_exit(&db->db_mtx);
1294b50a0fe0SNeil Perrin 
1295b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1296c717a561Smaybee 
1297b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1298b24ab676SJeff Bonwick }
1299b24ab676SJeff Bonwick 
1300b24ab676SJeff Bonwick static void
1301b24ab676SJeff Bonwick dmu_sync_late_arrival_done(zio_t *zio)
1302b24ab676SJeff Bonwick {
1303b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1304b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = zio->io_private;
1305b24ab676SJeff Bonwick 
1306b24ab676SJeff Bonwick 	if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
1307b24ab676SJeff Bonwick 		ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1308b24ab676SJeff Bonwick 		ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1309b24ab676SJeff Bonwick 		zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1310b24ab676SJeff Bonwick 	}
1311b24ab676SJeff Bonwick 
1312b24ab676SJeff Bonwick 	dmu_tx_commit(dsa->dsa_tx);
1313b24ab676SJeff Bonwick 
1314b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1315b24ab676SJeff Bonwick 
1316b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1317b24ab676SJeff Bonwick }
1318b24ab676SJeff Bonwick 
1319b24ab676SJeff Bonwick static int
1320b24ab676SJeff Bonwick dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1321b24ab676SJeff Bonwick     zio_prop_t *zp, zbookmark_t *zb)
1322b24ab676SJeff Bonwick {
1323b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1324b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1325b24ab676SJeff Bonwick 
1326b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1327b24ab676SJeff Bonwick 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
13286e1f5caaSNeil Perrin 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1329b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1330b24ab676SJeff Bonwick 		return (EIO);	/* Make zl_get_data do txg_waited_synced() */
1331b24ab676SJeff Bonwick 	}
1332b24ab676SJeff Bonwick 
1333b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1334b24ab676SJeff Bonwick 	dsa->dsa_dr = NULL;
1335b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1336b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1337b24ab676SJeff Bonwick 	dsa->dsa_tx = tx;
1338b24ab676SJeff Bonwick 
1339b24ab676SJeff Bonwick 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1340b24ab676SJeff Bonwick 	    zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp,
1341b24ab676SJeff Bonwick 	    dmu_sync_late_arrival_ready, dmu_sync_late_arrival_done, dsa,
1342b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1343b24ab676SJeff Bonwick 
1344b24ab676SJeff Bonwick 	return (0);
1345c5c6ffa0Smaybee }
1346c5c6ffa0Smaybee 
1347fa9e4066Sahrens /*
1348c5c6ffa0Smaybee  * Intent log support: sync the block associated with db to disk.
1349c5c6ffa0Smaybee  * N.B. and XXX: the caller is responsible for making sure that the
1350c5c6ffa0Smaybee  * data isn't changing while dmu_sync() is writing it.
1351fa9e4066Sahrens  *
1352fa9e4066Sahrens  * Return values:
1353fa9e4066Sahrens  *
1354c5c6ffa0Smaybee  *	EEXIST: this txg has already been synced, so there's nothing to to.
1355fa9e4066Sahrens  *		The caller should not log the write.
1356fa9e4066Sahrens  *
1357fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1358fa9e4066Sahrens  *		The caller should not log the write.
1359fa9e4066Sahrens  *
1360c5c6ffa0Smaybee  *	EALREADY: this block is already in the process of being synced.
1361c5c6ffa0Smaybee  *		The caller should track its progress (somehow).
1362fa9e4066Sahrens  *
1363b24ab676SJeff Bonwick  *	EIO: could not do the I/O.
1364b24ab676SJeff Bonwick  *		The caller should do a txg_wait_synced().
1365fa9e4066Sahrens  *
1366b24ab676SJeff Bonwick  *	0: the I/O has been initiated.
1367b24ab676SJeff Bonwick  *		The caller should log this blkptr in the done callback.
1368b24ab676SJeff Bonwick  *		It is possible that the I/O will fail, in which case
1369b24ab676SJeff Bonwick  *		the error will be reported to the done callback and
1370b24ab676SJeff Bonwick  *		propagated to pio from zio_done().
1371fa9e4066Sahrens  */
1372fa9e4066Sahrens int
1373b24ab676SJeff Bonwick dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1374fa9e4066Sahrens {
1375b24ab676SJeff Bonwick 	blkptr_t *bp = zgd->zgd_bp;
1376b24ab676SJeff Bonwick 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1377503ad85cSMatthew Ahrens 	objset_t *os = db->db_objset;
1378b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
1379c717a561Smaybee 	dbuf_dirty_record_t *dr;
1380b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1381ea8dc4b6Seschrock 	zbookmark_t zb;
1382b24ab676SJeff Bonwick 	zio_prop_t zp;
1383744947dcSTom Erickson 	dnode_t *dn;
1384fa9e4066Sahrens 
1385b24ab676SJeff Bonwick 	ASSERT(pio != NULL);
1386fa9e4066Sahrens 	ASSERT(BP_IS_HOLE(bp));
1387fa9e4066Sahrens 	ASSERT(txg != 0);
1388fa9e4066Sahrens 
1389b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, ds->ds_object,
1390b24ab676SJeff Bonwick 	    db->db.db_object, db->db_level, db->db_blkid);
1391b24ab676SJeff Bonwick 
1392744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1393744947dcSTom Erickson 	dn = DB_DNODE(db);
1394744947dcSTom Erickson 	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1395744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 	/*
1398b24ab676SJeff Bonwick 	 * If we're frozen (running ziltest), we always need to generate a bp.
1399ea8dc4b6Seschrock 	 */
1400b24ab676SJeff Bonwick 	if (txg > spa_freeze_txg(os->os_spa))
1401b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1402ea8dc4b6Seschrock 
1403ea8dc4b6Seschrock 	/*
1404b24ab676SJeff Bonwick 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1405b24ab676SJeff Bonwick 	 * and us.  If we determine that this txg is not yet syncing,
1406b24ab676SJeff Bonwick 	 * but it begins to sync a moment later, that's OK because the
1407b24ab676SJeff Bonwick 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1408fa9e4066Sahrens 	 */
1409b24ab676SJeff Bonwick 	mutex_enter(&db->db_mtx);
1410b24ab676SJeff Bonwick 
1411b24ab676SJeff Bonwick 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1412fa9e4066Sahrens 		/*
1413b24ab676SJeff Bonwick 		 * This txg has already synced.  There's nothing to do.
1414fa9e4066Sahrens 		 */
1415b24ab676SJeff Bonwick 		mutex_exit(&db->db_mtx);
1416c5c6ffa0Smaybee 		return (EEXIST);
1417fa9e4066Sahrens 	}
1418fa9e4066Sahrens 
1419b24ab676SJeff Bonwick 	if (txg <= spa_syncing_txg(os->os_spa)) {
1420c5c6ffa0Smaybee 		/*
1421b24ab676SJeff Bonwick 		 * This txg is currently syncing, so we can't mess with
1422b24ab676SJeff Bonwick 		 * the dirty record anymore; just write a new log block.
1423c5c6ffa0Smaybee 		 */
142413506d1eSmaybee 		mutex_exit(&db->db_mtx);
1425b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1426c5c6ffa0Smaybee 	}
1427c5c6ffa0Smaybee 
1428c717a561Smaybee 	dr = db->db_last_dirty;
1429b24ab676SJeff Bonwick 	while (dr && dr->dr_txg != txg)
1430c717a561Smaybee 		dr = dr->dr_next;
1431b24ab676SJeff Bonwick 
1432b24ab676SJeff Bonwick 	if (dr == NULL) {
1433c5c6ffa0Smaybee 		/*
1434b24ab676SJeff Bonwick 		 * There's no dr for this dbuf, so it must have been freed.
1435fa9e4066Sahrens 		 * There's no need to log writes to freed blocks, so we're done.
1436fa9e4066Sahrens 		 */
1437fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1438fa9e4066Sahrens 		return (ENOENT);
1439fa9e4066Sahrens 	}
1440fa9e4066Sahrens 
1441c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
1442b24ab676SJeff Bonwick 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1443b24ab676SJeff Bonwick 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1444c5c6ffa0Smaybee 		/*
1445b24ab676SJeff Bonwick 		 * We have already issued a sync write for this buffer,
1446b24ab676SJeff Bonwick 		 * or this buffer has already been synced.  It could not
1447c717a561Smaybee 		 * have been dirtied since, or we would have cleared the state.
1448c717a561Smaybee 		 */
1449c717a561Smaybee 		mutex_exit(&db->db_mtx);
1450b24ab676SJeff Bonwick 		return (EALREADY);
1451c717a561Smaybee 	}
1452c717a561Smaybee 
1453b24ab676SJeff Bonwick 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1454c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1455fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1456fa9e4066Sahrens 
1457b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1458b24ab676SJeff Bonwick 	dsa->dsa_dr = dr;
1459b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1460b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1461b24ab676SJeff Bonwick 	dsa->dsa_tx = NULL;
1462e14bb325SJeff Bonwick 
1463b24ab676SJeff Bonwick 	zio_nowait(arc_write(pio, os->os_spa, txg,
1464b24ab676SJeff Bonwick 	    bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db), &zp,
1465b24ab676SJeff Bonwick 	    dmu_sync_ready, dmu_sync_done, dsa,
1466b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb));
1467e14bb325SJeff Bonwick 
1468b24ab676SJeff Bonwick 	return (0);
1469fa9e4066Sahrens }
1470fa9e4066Sahrens 
1471fa9e4066Sahrens int
1472fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1473fa9e4066Sahrens 	dmu_tx_t *tx)
1474fa9e4066Sahrens {
1475ea8dc4b6Seschrock 	dnode_t *dn;
1476ea8dc4b6Seschrock 	int err;
1477ea8dc4b6Seschrock 
1478503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1479ea8dc4b6Seschrock 	if (err)
1480ea8dc4b6Seschrock 		return (err);
1481ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1482fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1483fa9e4066Sahrens 	return (err);
1484fa9e4066Sahrens }
1485fa9e4066Sahrens 
1486fa9e4066Sahrens void
1487fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1488fa9e4066Sahrens 	dmu_tx_t *tx)
1489fa9e4066Sahrens {
1490ea8dc4b6Seschrock 	dnode_t *dn;
1491ea8dc4b6Seschrock 
1492ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1493503ad85cSMatthew Ahrens 	(void) dnode_hold(os, object, FTAG, &dn);
1494fa9e4066Sahrens 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1495fa9e4066Sahrens 	dn->dn_checksum = checksum;
1496fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1497fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1498fa9e4066Sahrens }
1499fa9e4066Sahrens 
1500fa9e4066Sahrens void
1501fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1502fa9e4066Sahrens 	dmu_tx_t *tx)
1503fa9e4066Sahrens {
1504ea8dc4b6Seschrock 	dnode_t *dn;
1505ea8dc4b6Seschrock 
1506ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1507503ad85cSMatthew Ahrens 	(void) dnode_hold(os, object, FTAG, &dn);
1508fa9e4066Sahrens 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1509fa9e4066Sahrens 	dn->dn_compress = compress;
1510fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1511fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1512fa9e4066Sahrens }
1513fa9e4066Sahrens 
1514b24ab676SJeff Bonwick int zfs_mdcomp_disable = 0;
1515b24ab676SJeff Bonwick 
1516b24ab676SJeff Bonwick void
1517b24ab676SJeff Bonwick dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1518b24ab676SJeff Bonwick {
1519b24ab676SJeff Bonwick 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1520ad135b5dSChristopher Siden 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
15211d8ccc7bSMark Shellenbaum 	    (wp & WP_SPILL));
1522b24ab676SJeff Bonwick 	enum zio_checksum checksum = os->os_checksum;
1523b24ab676SJeff Bonwick 	enum zio_compress compress = os->os_compress;
1524b24ab676SJeff Bonwick 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
1525b24ab676SJeff Bonwick 	boolean_t dedup;
1526b24ab676SJeff Bonwick 	boolean_t dedup_verify = os->os_dedup_verify;
1527b24ab676SJeff Bonwick 	int copies = os->os_copies;
1528b24ab676SJeff Bonwick 
1529b24ab676SJeff Bonwick 	/*
1530b24ab676SJeff Bonwick 	 * Determine checksum setting.
1531b24ab676SJeff Bonwick 	 */
1532b24ab676SJeff Bonwick 	if (ismd) {
1533b24ab676SJeff Bonwick 		/*
1534b24ab676SJeff Bonwick 		 * Metadata always gets checksummed.  If the data
1535b24ab676SJeff Bonwick 		 * checksum is multi-bit correctable, and it's not a
1536b24ab676SJeff Bonwick 		 * ZBT-style checksum, then it's suitable for metadata
1537b24ab676SJeff Bonwick 		 * as well.  Otherwise, the metadata checksum defaults
1538b24ab676SJeff Bonwick 		 * to fletcher4.
1539b24ab676SJeff Bonwick 		 */
1540b24ab676SJeff Bonwick 		if (zio_checksum_table[checksum].ci_correctable < 1 ||
15416e1f5caaSNeil Perrin 		    zio_checksum_table[checksum].ci_eck)
1542b24ab676SJeff Bonwick 			checksum = ZIO_CHECKSUM_FLETCHER_4;
1543b24ab676SJeff Bonwick 	} else {
1544b24ab676SJeff Bonwick 		checksum = zio_checksum_select(dn->dn_checksum, checksum);
1545b24ab676SJeff Bonwick 	}
1546b24ab676SJeff Bonwick 
1547b24ab676SJeff Bonwick 	/*
1548b24ab676SJeff Bonwick 	 * Determine compression setting.
1549b24ab676SJeff Bonwick 	 */
1550b24ab676SJeff Bonwick 	if (ismd) {
1551b24ab676SJeff Bonwick 		/*
1552b24ab676SJeff Bonwick 		 * XXX -- we should design a compression algorithm
1553b24ab676SJeff Bonwick 		 * that specializes in arrays of bps.
1554b24ab676SJeff Bonwick 		 */
1555b24ab676SJeff Bonwick 		compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY :
1556b24ab676SJeff Bonwick 		    ZIO_COMPRESS_LZJB;
1557b24ab676SJeff Bonwick 	} else {
1558b24ab676SJeff Bonwick 		compress = zio_compress_select(dn->dn_compress, compress);
1559b24ab676SJeff Bonwick 	}
1560b24ab676SJeff Bonwick 
1561b24ab676SJeff Bonwick 	/*
1562b24ab676SJeff Bonwick 	 * Determine dedup setting.  If we are in dmu_sync(), we won't
1563b24ab676SJeff Bonwick 	 * actually dedup now because that's all done in syncing context;
1564b24ab676SJeff Bonwick 	 * but we do want to use the dedup checkum.  If the checksum is not
1565b24ab676SJeff Bonwick 	 * strong enough to ensure unique signatures, force dedup_verify.
1566b24ab676SJeff Bonwick 	 */
1567b24ab676SJeff Bonwick 	dedup = (!ismd && dedup_checksum != ZIO_CHECKSUM_OFF);
1568b24ab676SJeff Bonwick 	if (dedup) {
1569b24ab676SJeff Bonwick 		checksum = dedup_checksum;
1570b24ab676SJeff Bonwick 		if (!zio_checksum_table[checksum].ci_dedup)
1571b24ab676SJeff Bonwick 			dedup_verify = 1;
1572b24ab676SJeff Bonwick 	}
1573b24ab676SJeff Bonwick 
1574b24ab676SJeff Bonwick 	if (wp & WP_DMU_SYNC)
1575b24ab676SJeff Bonwick 		dedup = 0;
1576b24ab676SJeff Bonwick 
1577b24ab676SJeff Bonwick 	if (wp & WP_NOFILL) {
1578b24ab676SJeff Bonwick 		ASSERT(!ismd && level == 0);
1579b24ab676SJeff Bonwick 		checksum = ZIO_CHECKSUM_OFF;
1580b24ab676SJeff Bonwick 		compress = ZIO_COMPRESS_OFF;
1581b24ab676SJeff Bonwick 		dedup = B_FALSE;
1582b24ab676SJeff Bonwick 	}
1583b24ab676SJeff Bonwick 
1584b24ab676SJeff Bonwick 	zp->zp_checksum = checksum;
1585b24ab676SJeff Bonwick 	zp->zp_compress = compress;
15860a586ceaSMark Shellenbaum 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1587b24ab676SJeff Bonwick 	zp->zp_level = level;
1588b24ab676SJeff Bonwick 	zp->zp_copies = MIN(copies + ismd, spa_max_replication(os->os_spa));
1589b24ab676SJeff Bonwick 	zp->zp_dedup = dedup;
1590b24ab676SJeff Bonwick 	zp->zp_dedup_verify = dedup && dedup_verify;
1591b24ab676SJeff Bonwick }
1592b24ab676SJeff Bonwick 
159344cd46caSbillm int
1594fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1595fa9e4066Sahrens {
1596fa9e4066Sahrens 	dnode_t *dn;
1597fa9e4066Sahrens 	int i, err;
1598fa9e4066Sahrens 
1599503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1600ea8dc4b6Seschrock 	if (err)
1601ea8dc4b6Seschrock 		return (err);
1602fa9e4066Sahrens 	/*
1603fa9e4066Sahrens 	 * Sync any current changes before
1604fa9e4066Sahrens 	 * we go trundling through the block pointers.
1605fa9e4066Sahrens 	 */
1606fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1607c543ec06Sahrens 		if (list_link_active(&dn->dn_dirty_link[i]))
1608fa9e4066Sahrens 			break;
1609fa9e4066Sahrens 	}
1610fa9e4066Sahrens 	if (i != TXG_SIZE) {
1611fa9e4066Sahrens 		dnode_rele(dn, FTAG);
1612fa9e4066Sahrens 		txg_wait_synced(dmu_objset_pool(os), 0);
1613503ad85cSMatthew Ahrens 		err = dnode_hold(os, object, FTAG, &dn);
1614ea8dc4b6Seschrock 		if (err)
1615ea8dc4b6Seschrock 			return (err);
1616fa9e4066Sahrens 	}
1617fa9e4066Sahrens 
1618cdb0ab79Smaybee 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1619fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1620fa9e4066Sahrens 
1621fa9e4066Sahrens 	return (err);
1622fa9e4066Sahrens }
1623fa9e4066Sahrens 
1624fa9e4066Sahrens void
1625fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1626fa9e4066Sahrens {
1627b24ab676SJeff Bonwick 	dnode_phys_t *dnp;
1628b24ab676SJeff Bonwick 
1629fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1630fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1631fa9e4066Sahrens 
1632b24ab676SJeff Bonwick 	dnp = dn->dn_phys;
1633b24ab676SJeff Bonwick 
1634fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1635fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1636fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1637b24ab676SJeff Bonwick 	doi->doi_type = dn->dn_type;
1638b24ab676SJeff Bonwick 	doi->doi_bonus_type = dn->dn_bonustype;
1639b24ab676SJeff Bonwick 	doi->doi_bonus_size = dn->dn_bonuslen;
1640fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1641fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1642fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
1643b24ab676SJeff Bonwick 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1644b24ab676SJeff Bonwick 	doi->doi_max_offset = (dnp->dn_maxblkid + 1) * dn->dn_datablksz;
1645b24ab676SJeff Bonwick 	doi->doi_fill_count = 0;
1646b24ab676SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1647b24ab676SJeff Bonwick 		doi->doi_fill_count += dnp->dn_blkptr[i].blk_fill;
1648fa9e4066Sahrens 
1649fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1650fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1651fa9e4066Sahrens }
1652fa9e4066Sahrens 
1653fa9e4066Sahrens /*
1654fa9e4066Sahrens  * Get information on a DMU object.
1655fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1656fa9e4066Sahrens  */
1657fa9e4066Sahrens int
1658fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1659fa9e4066Sahrens {
1660ea8dc4b6Seschrock 	dnode_t *dn;
1661503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
1662fa9e4066Sahrens 
1663ea8dc4b6Seschrock 	if (err)
1664ea8dc4b6Seschrock 		return (err);
1665fa9e4066Sahrens 
1666fa9e4066Sahrens 	if (doi != NULL)
1667fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
1668fa9e4066Sahrens 
1669fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1670fa9e4066Sahrens 	return (0);
1671fa9e4066Sahrens }
1672fa9e4066Sahrens 
1673fa9e4066Sahrens /*
1674fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
1675fa9e4066Sahrens  */
1676fa9e4066Sahrens void
1677744947dcSTom Erickson dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
1678fa9e4066Sahrens {
1679744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1680744947dcSTom Erickson 
1681744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1682744947dcSTom Erickson 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
1683744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1684fa9e4066Sahrens }
1685fa9e4066Sahrens 
1686fa9e4066Sahrens /*
1687fa9e4066Sahrens  * Faster still when you only care about the size.
1688fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
1689fa9e4066Sahrens  */
1690fa9e4066Sahrens void
1691744947dcSTom Erickson dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
1692744947dcSTom Erickson     u_longlong_t *nblk512)
1693fa9e4066Sahrens {
1694744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1695744947dcSTom Erickson 	dnode_t *dn;
1696744947dcSTom Erickson 
1697744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1698744947dcSTom Erickson 	dn = DB_DNODE(db);
1699fa9e4066Sahrens 
1700fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
170199653d4eSeschrock 	/* add 1 for dnode space */
170299653d4eSeschrock 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
170399653d4eSeschrock 	    SPA_MINBLOCKSHIFT) + 1;
1704744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1705fa9e4066Sahrens }
1706fa9e4066Sahrens 
1707fa9e4066Sahrens void
1708fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
1709fa9e4066Sahrens {
1710fa9e4066Sahrens 	uint64_t *buf = vbuf;
1711fa9e4066Sahrens 	size_t count = size >> 3;
1712fa9e4066Sahrens 	int i;
1713fa9e4066Sahrens 
1714fa9e4066Sahrens 	ASSERT((size & 7) == 0);
1715fa9e4066Sahrens 
1716fa9e4066Sahrens 	for (i = 0; i < count; i++)
1717fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
1718fa9e4066Sahrens }
1719fa9e4066Sahrens 
1720fa9e4066Sahrens void
1721fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
1722fa9e4066Sahrens {
1723fa9e4066Sahrens 	uint32_t *buf = vbuf;
1724fa9e4066Sahrens 	size_t count = size >> 2;
1725fa9e4066Sahrens 	int i;
1726fa9e4066Sahrens 
1727fa9e4066Sahrens 	ASSERT((size & 3) == 0);
1728fa9e4066Sahrens 
1729fa9e4066Sahrens 	for (i = 0; i < count; i++)
1730fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
1731fa9e4066Sahrens }
1732fa9e4066Sahrens 
1733fa9e4066Sahrens void
1734fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
1735fa9e4066Sahrens {
1736fa9e4066Sahrens 	uint16_t *buf = vbuf;
1737fa9e4066Sahrens 	size_t count = size >> 1;
1738fa9e4066Sahrens 	int i;
1739fa9e4066Sahrens 
1740fa9e4066Sahrens 	ASSERT((size & 1) == 0);
1741fa9e4066Sahrens 
1742fa9e4066Sahrens 	for (i = 0; i < count; i++)
1743fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
1744fa9e4066Sahrens }
1745fa9e4066Sahrens 
1746fa9e4066Sahrens /* ARGSUSED */
1747fa9e4066Sahrens void
1748fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
1749fa9e4066Sahrens {
1750fa9e4066Sahrens }
1751fa9e4066Sahrens 
1752fa9e4066Sahrens void
1753fa9e4066Sahrens dmu_init(void)
1754fa9e4066Sahrens {
17553f9d6ad7SLin Ling 	zfs_dbgmsg_init();
1756744947dcSTom Erickson 	sa_cache_init();
1757744947dcSTom Erickson 	xuio_stat_init();
1758744947dcSTom Erickson 	dmu_objset_init();
1759fa9e4066Sahrens 	dnode_init();
1760744947dcSTom Erickson 	dbuf_init();
17617cbf8b43SRich Morris 	zfetch_init();
1762fa94a07fSbrendan 	l2arc_init();
1763*ce636f8bSMatthew Ahrens 	arc_init();
1764fa9e4066Sahrens }
1765fa9e4066Sahrens 
1766fa9e4066Sahrens void
1767fa9e4066Sahrens dmu_fini(void)
1768fa9e4066Sahrens {
1769fa9e4066Sahrens 	arc_fini();
1770*ce636f8bSMatthew Ahrens 	l2arc_fini();
17717cbf8b43SRich Morris 	zfetch_fini();
1772fa9e4066Sahrens 	dbuf_fini();
1773744947dcSTom Erickson 	dnode_fini();
1774744947dcSTom Erickson 	dmu_objset_fini();
1775c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_stat_fini();
17760a586ceaSMark Shellenbaum 	sa_cache_fini();
17773f9d6ad7SLin Ling 	zfs_dbgmsg_fini();
1778fa9e4066Sahrens }
1779