xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision 82c9918f28a2d8d9e93769e16f7fa7bee347e80f)
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 /*
22cdb0ab79Smaybee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
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>
4344eda4d7Smaybee #ifdef _KERNEL
4444eda4d7Smaybee #include <sys/vmsystm.h>
450fab61baSJonathan W Adams #include <sys/zfs_znode.h>
4644eda4d7Smaybee #endif
47fa9e4066Sahrens 
48fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
49fa9e4066Sahrens 	{	byteswap_uint8_array,	TRUE,	"unallocated"		},
50fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"object directory"	},
51fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"object array"		},
52fa9e4066Sahrens 	{	byteswap_uint8_array,	TRUE,	"packed nvlist"		},
53fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"packed nvlist size"	},
54fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"bplist"		},
55fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"bplist header"		},
56fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"SPA space map header"	},
57fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"SPA space map"		},
58fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"ZIL intent log"	},
59fa9e4066Sahrens 	{	dnode_buf_byteswap,	TRUE,	"DMU dnode"		},
60fa9e4066Sahrens 	{	dmu_objset_byteswap,	TRUE,	"DMU objset"		},
61fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"DSL directory"		},
62fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL directory child map"},
63fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL dataset snap map"	},
64fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL props"		},
65fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"DSL dataset"		},
66fa9e4066Sahrens 	{	zfs_znode_byteswap,	TRUE,	"ZFS znode"		},
67da6c28aaSamw 	{	zfs_oldacl_byteswap,	TRUE,	"ZFS V0 ACL"		},
68fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"ZFS plain file"	},
69fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS directory"		},
70fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS master node"	},
71fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS delete queue"	},
72fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"zvol object"		},
73fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"zvol prop"		},
74fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"other uint8[]"		},
75fa9e4066Sahrens 	{	byteswap_uint64_array,	FALSE,	"other uint64[]"	},
76fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"other ZAP"		},
77ea8dc4b6Seschrock 	{	zap_byteswap,		TRUE,	"persistent error log"	},
7806eeb2adSek110237 	{	byteswap_uint8_array,	TRUE,	"SPA history"		},
7906eeb2adSek110237 	{	byteswap_uint64_array,	TRUE,	"SPA history offsets"	},
80b1b8ab34Slling 	{	zap_byteswap,		TRUE,	"Pool properties"	},
81da6c28aaSamw 	{	zap_byteswap,		TRUE,	"DSL permissions"	},
82da6c28aaSamw 	{	zfs_acl_byteswap,	TRUE,	"ZFS ACL"		},
83da6c28aaSamw 	{	byteswap_uint8_array,	TRUE,	"ZFS SYSACL"		},
84da6c28aaSamw 	{	byteswap_uint8_array,	TRUE,	"FUID table"		},
85add89791Smarks 	{	byteswap_uint64_array,	TRUE,	"FUID table size"	},
86088f3894Sahrens 	{	zap_byteswap,		TRUE,	"DSL dataset next clones"},
87088f3894Sahrens 	{	zap_byteswap,		TRUE,	"scrub work queue"	},
88fa9e4066Sahrens };
89fa9e4066Sahrens 
90fa9e4066Sahrens int
91ea8dc4b6Seschrock dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
92ea8dc4b6Seschrock     void *tag, dmu_buf_t **dbp)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	dnode_t *dn;
95fa9e4066Sahrens 	uint64_t blkid;
96fa9e4066Sahrens 	dmu_buf_impl_t *db;
97ea8dc4b6Seschrock 	int err;
98fa9e4066Sahrens 
99ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
100ea8dc4b6Seschrock 	if (err)
101ea8dc4b6Seschrock 		return (err);
102fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
103fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
104ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
105fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
106ea8dc4b6Seschrock 	if (db == NULL) {
107ea8dc4b6Seschrock 		err = EIO;
108ea8dc4b6Seschrock 	} else {
109ea8dc4b6Seschrock 		err = dbuf_read(db, NULL, DB_RF_CANFAIL);
110ea8dc4b6Seschrock 		if (err) {
111ea8dc4b6Seschrock 			dbuf_rele(db, tag);
112ea8dc4b6Seschrock 			db = NULL;
113ea8dc4b6Seschrock 		}
114fa9e4066Sahrens 	}
115fa9e4066Sahrens 
116ea8dc4b6Seschrock 	dnode_rele(dn, FTAG);
117ea8dc4b6Seschrock 	*dbp = &db->db;
118ea8dc4b6Seschrock 	return (err);
119fa9e4066Sahrens }
120fa9e4066Sahrens 
121fa9e4066Sahrens int
122fa9e4066Sahrens dmu_bonus_max(void)
123fa9e4066Sahrens {
124fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
125fa9e4066Sahrens }
126fa9e4066Sahrens 
1271934e92fSmaybee int
1281934e92fSmaybee dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx)
1291934e92fSmaybee {
1301934e92fSmaybee 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1311934e92fSmaybee 
1321934e92fSmaybee 	if (dn->dn_bonus != (dmu_buf_impl_t *)db)
1331934e92fSmaybee 		return (EINVAL);
1341934e92fSmaybee 	if (newsize < 0 || newsize > db->db_size)
1351934e92fSmaybee 		return (EINVAL);
1361934e92fSmaybee 	dnode_setbonuslen(dn, newsize, tx);
1371934e92fSmaybee 	return (0);
1381934e92fSmaybee }
1391934e92fSmaybee 
140fa9e4066Sahrens /*
141ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
142fa9e4066Sahrens  */
143ea8dc4b6Seschrock int
144ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
145fa9e4066Sahrens {
146ea8dc4b6Seschrock 	dnode_t *dn;
147fa9e4066Sahrens 	dmu_buf_impl_t *db;
1481934e92fSmaybee 	int error;
149fa9e4066Sahrens 
1501934e92fSmaybee 	error = dnode_hold(os->os, object, FTAG, &dn);
1511934e92fSmaybee 	if (error)
1521934e92fSmaybee 		return (error);
153fa9e4066Sahrens 
154fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
155ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
156fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
157ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
158ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
1591934e92fSmaybee 			dbuf_create_bonus(dn);
160fa9e4066Sahrens 	}
161ea8dc4b6Seschrock 	db = dn->dn_bonus;
162ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
1631934e92fSmaybee 
1641934e92fSmaybee 	/* as long as the bonus buf is held, the dnode will be held */
1651934e92fSmaybee 	if (refcount_add(&db->db_holds, tag) == 1)
1661934e92fSmaybee 		VERIFY(dnode_add_ref(dn, db));
1671934e92fSmaybee 
168fa9e4066Sahrens 	dnode_rele(dn, FTAG);
169ea8dc4b6Seschrock 
170ea8dc4b6Seschrock 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
171ea8dc4b6Seschrock 
172ea8dc4b6Seschrock 	*dbp = &db->db;
173ea8dc4b6Seschrock 	return (0);
174fa9e4066Sahrens }
175fa9e4066Sahrens 
17613506d1eSmaybee /*
17713506d1eSmaybee  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
17813506d1eSmaybee  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
17913506d1eSmaybee  * and can induce severe lock contention when writing to several files
18013506d1eSmaybee  * whose dnodes are in the same block.
18113506d1eSmaybee  */
18213506d1eSmaybee static int
18313506d1eSmaybee dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset,
184ea8dc4b6Seschrock     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
185fa9e4066Sahrens {
18605715f94SMark Maybee 	dsl_pool_t *dp = NULL;
187fa9e4066Sahrens 	dmu_buf_t **dbp;
188fa9e4066Sahrens 	uint64_t blkid, nblks, i;
189ea8dc4b6Seschrock 	uint32_t flags;
190ea8dc4b6Seschrock 	int err;
191ea8dc4b6Seschrock 	zio_t *zio;
19205715f94SMark Maybee 	hrtime_t start;
193ea8dc4b6Seschrock 
194ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
195fa9e4066Sahrens 
196ea8dc4b6Seschrock 	flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
197e1930233Sbonwick 	if (length > zfetch_array_rd_sz)
198ea8dc4b6Seschrock 		flags |= DB_RF_NOPREFETCH;
199ea8dc4b6Seschrock 
200fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
201fa9e4066Sahrens 	if (dn->dn_datablkshift) {
202fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
203fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
204fa9e4066Sahrens 		    P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
205fa9e4066Sahrens 	} else {
2060125049cSahrens 		if (offset + length > dn->dn_datablksz) {
2070125049cSahrens 			zfs_panic_recover("zfs: accessing past end of object "
2080125049cSahrens 			    "%llx/%llx (size=%u access=%llu+%llu)",
2090125049cSahrens 			    (longlong_t)dn->dn_objset->
2100125049cSahrens 			    os_dsl_dataset->ds_object,
2110125049cSahrens 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
2120125049cSahrens 			    (longlong_t)offset, (longlong_t)length);
2130125049cSahrens 			return (EIO);
2140125049cSahrens 		}
215fa9e4066Sahrens 		nblks = 1;
216fa9e4066Sahrens 	}
217ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
218fa9e4066Sahrens 
21905715f94SMark Maybee 	if (dn->dn_objset->os_dsl_dataset)
22005715f94SMark Maybee 		dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
22105715f94SMark Maybee 	if (dp && dsl_pool_sync_context(dp))
22205715f94SMark Maybee 		start = gethrtime();
223e14bb325SJeff Bonwick 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
224fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
225fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
226ea8dc4b6Seschrock 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
227ea8dc4b6Seschrock 		if (db == NULL) {
228ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
229ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
230ea8dc4b6Seschrock 			zio_nowait(zio);
231ea8dc4b6Seschrock 			return (EIO);
232ea8dc4b6Seschrock 		}
233ea8dc4b6Seschrock 		/* initiate async i/o */
23413506d1eSmaybee 		if (read) {
235ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
236ea8dc4b6Seschrock 			(void) dbuf_read(db, zio, flags);
237ea8dc4b6Seschrock 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
238ea8dc4b6Seschrock 		}
239ea8dc4b6Seschrock 		dbp[i] = &db->db;
240fa9e4066Sahrens 	}
241fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
242fa9e4066Sahrens 
243ea8dc4b6Seschrock 	/* wait for async i/o */
244ea8dc4b6Seschrock 	err = zio_wait(zio);
24505715f94SMark Maybee 	/* track read overhead when we are in sync context */
24605715f94SMark Maybee 	if (dp && dsl_pool_sync_context(dp))
24705715f94SMark Maybee 		dp->dp_read_overhead += gethrtime() - start;
248ea8dc4b6Seschrock 	if (err) {
249ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
250ea8dc4b6Seschrock 		return (err);
251ea8dc4b6Seschrock 	}
252ea8dc4b6Seschrock 
253ea8dc4b6Seschrock 	/* wait for other io to complete */
254ea8dc4b6Seschrock 	if (read) {
255ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
256ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
257ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
258ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
259ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
260ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
261ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
262ea8dc4b6Seschrock 				err = EIO;
263ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
264ea8dc4b6Seschrock 			if (err) {
265ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
266ea8dc4b6Seschrock 				return (err);
267ea8dc4b6Seschrock 			}
268ea8dc4b6Seschrock 		}
269ea8dc4b6Seschrock 	}
270ea8dc4b6Seschrock 
271ea8dc4b6Seschrock 	*numbufsp = nblks;
272ea8dc4b6Seschrock 	*dbpp = dbp;
273ea8dc4b6Seschrock 	return (0);
274fa9e4066Sahrens }
275fa9e4066Sahrens 
276a2eea2e1Sahrens static int
27713506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
27813506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
27913506d1eSmaybee {
28013506d1eSmaybee 	dnode_t *dn;
28113506d1eSmaybee 	int err;
28213506d1eSmaybee 
28313506d1eSmaybee 	err = dnode_hold(os->os, object, FTAG, &dn);
28413506d1eSmaybee 	if (err)
28513506d1eSmaybee 		return (err);
28613506d1eSmaybee 
28713506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
28813506d1eSmaybee 	    numbufsp, dbpp);
28913506d1eSmaybee 
29013506d1eSmaybee 	dnode_rele(dn, FTAG);
29113506d1eSmaybee 
29213506d1eSmaybee 	return (err);
29313506d1eSmaybee }
29413506d1eSmaybee 
29513506d1eSmaybee int
29613506d1eSmaybee dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
29713506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
29813506d1eSmaybee {
29913506d1eSmaybee 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
30013506d1eSmaybee 	int err;
30113506d1eSmaybee 
30213506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
30313506d1eSmaybee 	    numbufsp, dbpp);
30413506d1eSmaybee 
30513506d1eSmaybee 	return (err);
30613506d1eSmaybee }
30713506d1eSmaybee 
308fa9e4066Sahrens void
309ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
310fa9e4066Sahrens {
311fa9e4066Sahrens 	int i;
312fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
313fa9e4066Sahrens 
314fa9e4066Sahrens 	if (numbufs == 0)
315fa9e4066Sahrens 		return;
316fa9e4066Sahrens 
317ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
318ea8dc4b6Seschrock 		if (dbp[i])
319ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
320ea8dc4b6Seschrock 	}
321fa9e4066Sahrens 
322fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
323fa9e4066Sahrens }
324fa9e4066Sahrens 
325fa9e4066Sahrens void
326fa9e4066Sahrens dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
327fa9e4066Sahrens {
328fa9e4066Sahrens 	dnode_t *dn;
329fa9e4066Sahrens 	uint64_t blkid;
330ea8dc4b6Seschrock 	int nblks, i, err;
331fa9e4066Sahrens 
332416e0cd8Sek110237 	if (zfs_prefetch_disable)
333416e0cd8Sek110237 		return;
334416e0cd8Sek110237 
335fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
336fa9e4066Sahrens 		dn = os->os->os_meta_dnode;
337fa9e4066Sahrens 
338fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
339fa9e4066Sahrens 			return;
340fa9e4066Sahrens 
341fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
342fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
343fa9e4066Sahrens 		dbuf_prefetch(dn, blkid);
344fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
345fa9e4066Sahrens 		return;
346fa9e4066Sahrens 	}
347fa9e4066Sahrens 
348fa9e4066Sahrens 	/*
349fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
350fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
351fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
352fa9e4066Sahrens 	 */
353ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
354ea8dc4b6Seschrock 	if (err != 0)
355fa9e4066Sahrens 		return;
356fa9e4066Sahrens 
357fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
358fa9e4066Sahrens 	if (dn->dn_datablkshift) {
359fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
360fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
361fa9e4066Sahrens 		    P2ALIGN(offset, 1<<blkshift)) >> blkshift;
362fa9e4066Sahrens 	} else {
363fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
364fa9e4066Sahrens 	}
365fa9e4066Sahrens 
366fa9e4066Sahrens 	if (nblks != 0) {
367fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, offset);
368fa9e4066Sahrens 		for (i = 0; i < nblks; i++)
369fa9e4066Sahrens 			dbuf_prefetch(dn, blkid+i);
370fa9e4066Sahrens 	}
371fa9e4066Sahrens 
372fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
373fa9e4066Sahrens 
374fa9e4066Sahrens 	dnode_rele(dn, FTAG);
375fa9e4066Sahrens }
376fa9e4066Sahrens 
377cdb0ab79Smaybee static int
378cdb0ab79Smaybee get_next_chunk(dnode_t *dn, uint64_t *offset, uint64_t limit)
379cdb0ab79Smaybee {
3801c8564a7SMark Maybee 	uint64_t len = *offset - limit;
381cdb0ab79Smaybee 	uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT;
3821c8564a7SMark Maybee 	uint64_t subchunk =
3831c8564a7SMark Maybee 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
384cdb0ab79Smaybee 
385cdb0ab79Smaybee 	ASSERT(limit <= *offset);
386cdb0ab79Smaybee 
3871c8564a7SMark Maybee 	if (len <= chunk_len) {
388cdb0ab79Smaybee 		*offset = limit;
389cdb0ab79Smaybee 		return (0);
390cdb0ab79Smaybee 	}
391cdb0ab79Smaybee 
3921c8564a7SMark Maybee 	ASSERT(ISP2(subchunk));
3931c8564a7SMark Maybee 
394cdb0ab79Smaybee 	while (*offset > limit) {
3951c8564a7SMark Maybee 		uint64_t initial_offset = P2ROUNDUP(*offset, subchunk);
396cdb0ab79Smaybee 		uint64_t delta;
3971c8564a7SMark Maybee 		int err;
398cdb0ab79Smaybee 
399cdb0ab79Smaybee 		/* skip over allocated data */
400cdb0ab79Smaybee 		err = dnode_next_offset(dn,
401cdb0ab79Smaybee 		    DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
402cdb0ab79Smaybee 		if (err == ESRCH)
403cdb0ab79Smaybee 			*offset = limit;
404cdb0ab79Smaybee 		else if (err)
405cdb0ab79Smaybee 			return (err);
406cdb0ab79Smaybee 
407cdb0ab79Smaybee 		ASSERT3U(*offset, <=, initial_offset);
4081c8564a7SMark Maybee 		*offset = P2ALIGN(*offset, subchunk);
409cdb0ab79Smaybee 		delta = initial_offset - *offset;
410cdb0ab79Smaybee 		if (delta >= chunk_len) {
411cdb0ab79Smaybee 			*offset += delta - chunk_len;
412cdb0ab79Smaybee 			return (0);
413cdb0ab79Smaybee 		}
414cdb0ab79Smaybee 		chunk_len -= delta;
415cdb0ab79Smaybee 
416cdb0ab79Smaybee 		/* skip over unallocated data */
417cdb0ab79Smaybee 		err = dnode_next_offset(dn,
418cdb0ab79Smaybee 		    DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
419cdb0ab79Smaybee 		if (err == ESRCH)
420cdb0ab79Smaybee 			*offset = limit;
421cdb0ab79Smaybee 		else if (err)
422cdb0ab79Smaybee 			return (err);
423cdb0ab79Smaybee 
424cdb0ab79Smaybee 		if (*offset < limit)
425cdb0ab79Smaybee 			*offset = limit;
426cdb0ab79Smaybee 		ASSERT3U(*offset, <, initial_offset);
427cdb0ab79Smaybee 	}
428cdb0ab79Smaybee 	return (0);
429cdb0ab79Smaybee }
430cdb0ab79Smaybee 
431cdb0ab79Smaybee static int
432cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
433cdb0ab79Smaybee     uint64_t length, boolean_t free_dnode)
434cdb0ab79Smaybee {
435cdb0ab79Smaybee 	dmu_tx_t *tx;
436cdb0ab79Smaybee 	uint64_t object_size, start, end, len;
437cdb0ab79Smaybee 	boolean_t trunc = (length == DMU_OBJECT_END);
438cdb0ab79Smaybee 	int align, err;
439cdb0ab79Smaybee 
440cdb0ab79Smaybee 	align = 1 << dn->dn_datablkshift;
441cdb0ab79Smaybee 	ASSERT(align > 0);
442cdb0ab79Smaybee 	object_size = align == 1 ? dn->dn_datablksz :
443cdb0ab79Smaybee 	    (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
444cdb0ab79Smaybee 
445cdb0ab79Smaybee 	if (trunc || (end = offset + length) > object_size)
446cdb0ab79Smaybee 		end = object_size;
447cdb0ab79Smaybee 	if (end <= offset)
448cdb0ab79Smaybee 		return (0);
449cdb0ab79Smaybee 	length = end - offset;
450cdb0ab79Smaybee 
451cdb0ab79Smaybee 	while (length) {
452cdb0ab79Smaybee 		start = end;
453cdb0ab79Smaybee 		err = get_next_chunk(dn, &start, offset);
454cdb0ab79Smaybee 		if (err)
455cdb0ab79Smaybee 			return (err);
456cdb0ab79Smaybee 		len = trunc ? DMU_OBJECT_END : end - start;
457cdb0ab79Smaybee 
458cdb0ab79Smaybee 		tx = dmu_tx_create(os);
459cdb0ab79Smaybee 		dmu_tx_hold_free(tx, dn->dn_object, start, len);
460cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
461cdb0ab79Smaybee 		if (err) {
462cdb0ab79Smaybee 			dmu_tx_abort(tx);
463cdb0ab79Smaybee 			return (err);
464cdb0ab79Smaybee 		}
465cdb0ab79Smaybee 
466cdb0ab79Smaybee 		dnode_free_range(dn, start, trunc ? -1 : len, tx);
467cdb0ab79Smaybee 
4681c8564a7SMark Maybee 		if (start == 0 && free_dnode) {
4691c8564a7SMark Maybee 			ASSERT(trunc);
470cdb0ab79Smaybee 			dnode_free(dn, tx);
4711c8564a7SMark Maybee 		}
472cdb0ab79Smaybee 
473cdb0ab79Smaybee 		length -= end - start;
474cdb0ab79Smaybee 
475cdb0ab79Smaybee 		dmu_tx_commit(tx);
476cdb0ab79Smaybee 		end = start;
477cdb0ab79Smaybee 	}
478cdb0ab79Smaybee 	return (0);
479cdb0ab79Smaybee }
480cdb0ab79Smaybee 
481cdb0ab79Smaybee int
482cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object,
483cdb0ab79Smaybee     uint64_t offset, uint64_t length)
484cdb0ab79Smaybee {
485cdb0ab79Smaybee 	dnode_t *dn;
486cdb0ab79Smaybee 	int err;
487cdb0ab79Smaybee 
488cdb0ab79Smaybee 	err = dnode_hold(os->os, object, FTAG, &dn);
489cdb0ab79Smaybee 	if (err != 0)
490cdb0ab79Smaybee 		return (err);
491cdb0ab79Smaybee 	err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
492cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
493cdb0ab79Smaybee 	return (err);
494cdb0ab79Smaybee }
495cdb0ab79Smaybee 
496cdb0ab79Smaybee int
497cdb0ab79Smaybee dmu_free_object(objset_t *os, uint64_t object)
498cdb0ab79Smaybee {
499cdb0ab79Smaybee 	dnode_t *dn;
500cdb0ab79Smaybee 	dmu_tx_t *tx;
501cdb0ab79Smaybee 	int err;
502cdb0ab79Smaybee 
503cdb0ab79Smaybee 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
504cdb0ab79Smaybee 	    FTAG, &dn);
505cdb0ab79Smaybee 	if (err != 0)
506cdb0ab79Smaybee 		return (err);
507cdb0ab79Smaybee 	if (dn->dn_nlevels == 1) {
508cdb0ab79Smaybee 		tx = dmu_tx_create(os);
509cdb0ab79Smaybee 		dmu_tx_hold_bonus(tx, object);
510cdb0ab79Smaybee 		dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
511cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
512cdb0ab79Smaybee 		if (err == 0) {
513cdb0ab79Smaybee 			dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
514cdb0ab79Smaybee 			dnode_free(dn, tx);
515cdb0ab79Smaybee 			dmu_tx_commit(tx);
516cdb0ab79Smaybee 		} else {
517cdb0ab79Smaybee 			dmu_tx_abort(tx);
518cdb0ab79Smaybee 		}
519cdb0ab79Smaybee 	} else {
520cdb0ab79Smaybee 		err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
521cdb0ab79Smaybee 	}
522cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
523cdb0ab79Smaybee 	return (err);
524cdb0ab79Smaybee }
525cdb0ab79Smaybee 
526ea8dc4b6Seschrock int
527fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
528fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
529fa9e4066Sahrens {
530ea8dc4b6Seschrock 	dnode_t *dn;
531ea8dc4b6Seschrock 	int err = dnode_hold(os->os, object, FTAG, &dn);
532ea8dc4b6Seschrock 	if (err)
533ea8dc4b6Seschrock 		return (err);
534fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
535fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
536fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
537fa9e4066Sahrens 	dnode_rele(dn, FTAG);
538ea8dc4b6Seschrock 	return (0);
539fa9e4066Sahrens }
540fa9e4066Sahrens 
541ea8dc4b6Seschrock int
542ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
543ea8dc4b6Seschrock     void *buf)
544fa9e4066Sahrens {
545fa9e4066Sahrens 	dnode_t *dn;
546fa9e4066Sahrens 	dmu_buf_t **dbp;
547ea8dc4b6Seschrock 	int numbufs, i, err;
548fa9e4066Sahrens 
549ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
550ea8dc4b6Seschrock 	if (err)
551ea8dc4b6Seschrock 		return (err);
552feb08c6bSbillm 
553feb08c6bSbillm 	/*
554feb08c6bSbillm 	 * Deal with odd block sizes, where there can't be data past the first
555feb08c6bSbillm 	 * block.  If we ever do the tail block optimization, we will need to
556feb08c6bSbillm 	 * handle that here as well.
557feb08c6bSbillm 	 */
558fa9e4066Sahrens 	if (dn->dn_datablkshift == 0) {
559fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
560fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
561fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
562fa9e4066Sahrens 		size = newsz;
563fa9e4066Sahrens 	}
564fa9e4066Sahrens 
565fa9e4066Sahrens 	while (size > 0) {
566fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
567fa9e4066Sahrens 
568fa9e4066Sahrens 		/*
569fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
570fa9e4066Sahrens 		 * to be reading in parallel.
571fa9e4066Sahrens 		 */
572a2eea2e1Sahrens 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
573ea8dc4b6Seschrock 		    TRUE, FTAG, &numbufs, &dbp);
574ea8dc4b6Seschrock 		if (err)
5751934e92fSmaybee 			break;
576fa9e4066Sahrens 
577fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
578fa9e4066Sahrens 			int tocpy;
579fa9e4066Sahrens 			int bufoff;
580fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
581fa9e4066Sahrens 
582fa9e4066Sahrens 			ASSERT(size > 0);
583fa9e4066Sahrens 
584fa9e4066Sahrens 			bufoff = offset - db->db_offset;
585fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
586fa9e4066Sahrens 
587fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
588fa9e4066Sahrens 
589fa9e4066Sahrens 			offset += tocpy;
590fa9e4066Sahrens 			size -= tocpy;
591fa9e4066Sahrens 			buf = (char *)buf + tocpy;
592fa9e4066Sahrens 		}
593ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
594fa9e4066Sahrens 	}
595a2eea2e1Sahrens 	dnode_rele(dn, FTAG);
5961934e92fSmaybee 	return (err);
597fa9e4066Sahrens }
598fa9e4066Sahrens 
599fa9e4066Sahrens void
600fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
601fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
602fa9e4066Sahrens {
603fa9e4066Sahrens 	dmu_buf_t **dbp;
604fa9e4066Sahrens 	int numbufs, i;
605fa9e4066Sahrens 
60613506d1eSmaybee 	if (size == 0)
60713506d1eSmaybee 		return;
60813506d1eSmaybee 
609ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
610ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
611fa9e4066Sahrens 
612fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
613fa9e4066Sahrens 		int tocpy;
614fa9e4066Sahrens 		int bufoff;
615fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
616fa9e4066Sahrens 
617fa9e4066Sahrens 		ASSERT(size > 0);
618fa9e4066Sahrens 
619fa9e4066Sahrens 		bufoff = offset - db->db_offset;
620fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
621fa9e4066Sahrens 
622fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
623fa9e4066Sahrens 
624fa9e4066Sahrens 		if (tocpy == db->db_size)
625fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
626fa9e4066Sahrens 		else
627fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
628fa9e4066Sahrens 
629fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
630fa9e4066Sahrens 
631fa9e4066Sahrens 		if (tocpy == db->db_size)
632fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
633fa9e4066Sahrens 
634fa9e4066Sahrens 		offset += tocpy;
635fa9e4066Sahrens 		size -= tocpy;
636fa9e4066Sahrens 		buf = (char *)buf + tocpy;
637fa9e4066Sahrens 	}
638ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
639fa9e4066Sahrens }
640fa9e4066Sahrens 
641*82c9918fSTim Haley void
642*82c9918fSTim Haley dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
643*82c9918fSTim Haley     dmu_tx_t *tx)
644*82c9918fSTim Haley {
645*82c9918fSTim Haley 	dmu_buf_t **dbp;
646*82c9918fSTim Haley 	int numbufs, i;
647*82c9918fSTim Haley 
648*82c9918fSTim Haley 	if (size == 0)
649*82c9918fSTim Haley 		return;
650*82c9918fSTim Haley 
651*82c9918fSTim Haley 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
652*82c9918fSTim Haley 	    FALSE, FTAG, &numbufs, &dbp));
653*82c9918fSTim Haley 
654*82c9918fSTim Haley 	for (i = 0; i < numbufs; i++) {
655*82c9918fSTim Haley 		dmu_buf_t *db = dbp[i];
656*82c9918fSTim Haley 
657*82c9918fSTim Haley 		dmu_buf_will_not_fill(db, tx);
658*82c9918fSTim Haley 	}
659*82c9918fSTim Haley 	dmu_buf_rele_array(dbp, numbufs, FTAG);
660*82c9918fSTim Haley }
661*82c9918fSTim Haley 
662fa9e4066Sahrens #ifdef _KERNEL
663fa9e4066Sahrens int
664feb08c6bSbillm dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
665feb08c6bSbillm {
666feb08c6bSbillm 	dmu_buf_t **dbp;
667feb08c6bSbillm 	int numbufs, i, err;
668feb08c6bSbillm 
669feb08c6bSbillm 	/*
670feb08c6bSbillm 	 * NB: we could do this block-at-a-time, but it's nice
671feb08c6bSbillm 	 * to be reading in parallel.
672feb08c6bSbillm 	 */
673feb08c6bSbillm 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
674feb08c6bSbillm 	    &numbufs, &dbp);
675feb08c6bSbillm 	if (err)
676feb08c6bSbillm 		return (err);
677feb08c6bSbillm 
678feb08c6bSbillm 	for (i = 0; i < numbufs; i++) {
679feb08c6bSbillm 		int tocpy;
680feb08c6bSbillm 		int bufoff;
681feb08c6bSbillm 		dmu_buf_t *db = dbp[i];
682feb08c6bSbillm 
683feb08c6bSbillm 		ASSERT(size > 0);
684feb08c6bSbillm 
685feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
686feb08c6bSbillm 		tocpy = (int)MIN(db->db_size - bufoff, size);
687feb08c6bSbillm 
688feb08c6bSbillm 		err = uiomove((char *)db->db_data + bufoff, tocpy,
689feb08c6bSbillm 		    UIO_READ, uio);
690feb08c6bSbillm 		if (err)
691feb08c6bSbillm 			break;
692feb08c6bSbillm 
693feb08c6bSbillm 		size -= tocpy;
694feb08c6bSbillm 	}
695feb08c6bSbillm 	dmu_buf_rele_array(dbp, numbufs, FTAG);
696feb08c6bSbillm 
697feb08c6bSbillm 	return (err);
698feb08c6bSbillm }
699feb08c6bSbillm 
700feb08c6bSbillm int
701feb08c6bSbillm dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
702feb08c6bSbillm     dmu_tx_t *tx)
703fa9e4066Sahrens {
704fa9e4066Sahrens 	dmu_buf_t **dbp;
705fa9e4066Sahrens 	int numbufs, i;
706fa9e4066Sahrens 	int err = 0;
707fa9e4066Sahrens 
70813506d1eSmaybee 	if (size == 0)
70913506d1eSmaybee 		return (0);
71013506d1eSmaybee 
711feb08c6bSbillm 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
712ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp);
713ea8dc4b6Seschrock 	if (err)
714ea8dc4b6Seschrock 		return (err);
715fa9e4066Sahrens 
716fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
717fa9e4066Sahrens 		int tocpy;
718fa9e4066Sahrens 		int bufoff;
719fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
720fa9e4066Sahrens 
721fa9e4066Sahrens 		ASSERT(size > 0);
722fa9e4066Sahrens 
723feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
724fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
725fa9e4066Sahrens 
726fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
727fa9e4066Sahrens 
728fa9e4066Sahrens 		if (tocpy == db->db_size)
729fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
730fa9e4066Sahrens 		else
731fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
732fa9e4066Sahrens 
733fa9e4066Sahrens 		/*
734fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
735fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
736fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
737fa9e4066Sahrens 		 * block.
738fa9e4066Sahrens 		 */
739fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
740fa9e4066Sahrens 		    UIO_WRITE, uio);
741fa9e4066Sahrens 
742fa9e4066Sahrens 		if (tocpy == db->db_size)
743fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
744fa9e4066Sahrens 
745fa9e4066Sahrens 		if (err)
746fa9e4066Sahrens 			break;
747fa9e4066Sahrens 
748fa9e4066Sahrens 		size -= tocpy;
749fa9e4066Sahrens 	}
750ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
751fa9e4066Sahrens 	return (err);
752fa9e4066Sahrens }
75344eda4d7Smaybee 
75444eda4d7Smaybee int
75544eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
75644eda4d7Smaybee     page_t *pp, dmu_tx_t *tx)
75744eda4d7Smaybee {
75844eda4d7Smaybee 	dmu_buf_t **dbp;
75944eda4d7Smaybee 	int numbufs, i;
76044eda4d7Smaybee 	int err;
76144eda4d7Smaybee 
76244eda4d7Smaybee 	if (size == 0)
76344eda4d7Smaybee 		return (0);
76444eda4d7Smaybee 
76544eda4d7Smaybee 	err = dmu_buf_hold_array(os, object, offset, size,
76644eda4d7Smaybee 	    FALSE, FTAG, &numbufs, &dbp);
76744eda4d7Smaybee 	if (err)
76844eda4d7Smaybee 		return (err);
76944eda4d7Smaybee 
77044eda4d7Smaybee 	for (i = 0; i < numbufs; i++) {
77144eda4d7Smaybee 		int tocpy, copied, thiscpy;
77244eda4d7Smaybee 		int bufoff;
77344eda4d7Smaybee 		dmu_buf_t *db = dbp[i];
77444eda4d7Smaybee 		caddr_t va;
77544eda4d7Smaybee 
77644eda4d7Smaybee 		ASSERT(size > 0);
77744eda4d7Smaybee 		ASSERT3U(db->db_size, >=, PAGESIZE);
77844eda4d7Smaybee 
77944eda4d7Smaybee 		bufoff = offset - db->db_offset;
78044eda4d7Smaybee 		tocpy = (int)MIN(db->db_size - bufoff, size);
78144eda4d7Smaybee 
78244eda4d7Smaybee 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
78344eda4d7Smaybee 
78444eda4d7Smaybee 		if (tocpy == db->db_size)
78544eda4d7Smaybee 			dmu_buf_will_fill(db, tx);
78644eda4d7Smaybee 		else
78744eda4d7Smaybee 			dmu_buf_will_dirty(db, tx);
78844eda4d7Smaybee 
78944eda4d7Smaybee 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
79044eda4d7Smaybee 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
79144eda4d7Smaybee 			thiscpy = MIN(PAGESIZE, tocpy - copied);
7920fab61baSJonathan W Adams 			va = zfs_map_page(pp, S_READ);
79344eda4d7Smaybee 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
7940fab61baSJonathan W Adams 			zfs_unmap_page(pp, va);
79544eda4d7Smaybee 			pp = pp->p_next;
79644eda4d7Smaybee 			bufoff += PAGESIZE;
79744eda4d7Smaybee 		}
79844eda4d7Smaybee 
79944eda4d7Smaybee 		if (tocpy == db->db_size)
80044eda4d7Smaybee 			dmu_buf_fill_done(db, tx);
80144eda4d7Smaybee 
80244eda4d7Smaybee 		if (err)
80344eda4d7Smaybee 			break;
80444eda4d7Smaybee 
80544eda4d7Smaybee 		offset += tocpy;
80644eda4d7Smaybee 		size -= tocpy;
80744eda4d7Smaybee 	}
80844eda4d7Smaybee 	dmu_buf_rele_array(dbp, numbufs, FTAG);
80944eda4d7Smaybee 	return (err);
81044eda4d7Smaybee }
811fa9e4066Sahrens #endif
812fa9e4066Sahrens 
813c5c6ffa0Smaybee typedef struct {
814c717a561Smaybee 	dbuf_dirty_record_t	*dr;
815c5c6ffa0Smaybee 	dmu_sync_cb_t		*done;
816c5c6ffa0Smaybee 	void			*arg;
817c717a561Smaybee } dmu_sync_arg_t;
818c5c6ffa0Smaybee 
819c5c6ffa0Smaybee /* ARGSUSED */
820c5c6ffa0Smaybee static void
821e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
822e14bb325SJeff Bonwick {
823e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
824e14bb325SJeff Bonwick 
825e14bb325SJeff Bonwick 	if (!BP_IS_HOLE(bp)) {
826e14bb325SJeff Bonwick 		dmu_sync_arg_t *in = varg;
827e14bb325SJeff Bonwick 		dbuf_dirty_record_t *dr = in->dr;
828e14bb325SJeff Bonwick 		dmu_buf_impl_t *db = dr->dr_dbuf;
829e14bb325SJeff Bonwick 		ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type);
830e14bb325SJeff Bonwick 		ASSERT(BP_GET_LEVEL(bp) == 0);
831e14bb325SJeff Bonwick 		bp->blk_fill = 1;
832e14bb325SJeff Bonwick 	}
833e14bb325SJeff Bonwick }
834e14bb325SJeff Bonwick 
835e14bb325SJeff Bonwick /* ARGSUSED */
836e14bb325SJeff Bonwick static void
837c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
838c5c6ffa0Smaybee {
839c717a561Smaybee 	dmu_sync_arg_t *in = varg;
840c717a561Smaybee 	dbuf_dirty_record_t *dr = in->dr;
841c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
842c5c6ffa0Smaybee 	dmu_sync_cb_t *done = in->done;
843c5c6ffa0Smaybee 
844c5c6ffa0Smaybee 	mutex_enter(&db->db_mtx);
845c717a561Smaybee 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
846c717a561Smaybee 	dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
847c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
848c5c6ffa0Smaybee 	cv_broadcast(&db->db_changed);
849c5c6ffa0Smaybee 	mutex_exit(&db->db_mtx);
850c5c6ffa0Smaybee 
851c5c6ffa0Smaybee 	if (done)
852c717a561Smaybee 		done(&(db->db), in->arg);
853c717a561Smaybee 
854c717a561Smaybee 	kmem_free(in, sizeof (dmu_sync_arg_t));
855c5c6ffa0Smaybee }
856c5c6ffa0Smaybee 
857fa9e4066Sahrens /*
858c5c6ffa0Smaybee  * Intent log support: sync the block associated with db to disk.
859c5c6ffa0Smaybee  * N.B. and XXX: the caller is responsible for making sure that the
860c5c6ffa0Smaybee  * data isn't changing while dmu_sync() is writing it.
861fa9e4066Sahrens  *
862fa9e4066Sahrens  * Return values:
863fa9e4066Sahrens  *
864c5c6ffa0Smaybee  *	EEXIST: this txg has already been synced, so there's nothing to to.
865fa9e4066Sahrens  *		The caller should not log the write.
866fa9e4066Sahrens  *
867fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
868fa9e4066Sahrens  *		The caller should not log the write.
869fa9e4066Sahrens  *
870c5c6ffa0Smaybee  *	EALREADY: this block is already in the process of being synced.
871c5c6ffa0Smaybee  *		The caller should track its progress (somehow).
872fa9e4066Sahrens  *
873c5c6ffa0Smaybee  *	EINPROGRESS: the IO has been initiated.
874c5c6ffa0Smaybee  *		The caller should log this blkptr in the callback.
875fa9e4066Sahrens  *
876c5c6ffa0Smaybee  *	0: completed.  Sets *bp to the blkptr just written.
877c5c6ffa0Smaybee  *		The caller should log this blkptr immediately.
878fa9e4066Sahrens  */
879fa9e4066Sahrens int
880c5c6ffa0Smaybee dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
881c5c6ffa0Smaybee     blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
882fa9e4066Sahrens {
883c5c6ffa0Smaybee 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
884c5c6ffa0Smaybee 	objset_impl_t *os = db->db_objset;
885c5c6ffa0Smaybee 	dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
886fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
887c717a561Smaybee 	dbuf_dirty_record_t *dr;
888c717a561Smaybee 	dmu_sync_arg_t *in;
889ea8dc4b6Seschrock 	zbookmark_t zb;
890088f3894Sahrens 	writeprops_t wp = { 0 };
891c717a561Smaybee 	zio_t *zio;
892c5c6ffa0Smaybee 	int err;
893fa9e4066Sahrens 
894fa9e4066Sahrens 	ASSERT(BP_IS_HOLE(bp));
895fa9e4066Sahrens 	ASSERT(txg != 0);
896fa9e4066Sahrens 
897fa9e4066Sahrens 	dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
898fa9e4066Sahrens 	    txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
899fa9e4066Sahrens 
900fa9e4066Sahrens 	/*
901c5c6ffa0Smaybee 	 * XXX - would be nice if we could do this without suspending...
902ea8dc4b6Seschrock 	 */
903c5c6ffa0Smaybee 	txg_suspend(dp);
904ea8dc4b6Seschrock 
905ea8dc4b6Seschrock 	/*
906fa9e4066Sahrens 	 * If this txg already synced, there's nothing to do.
907fa9e4066Sahrens 	 */
908fa9e4066Sahrens 	if (txg <= tx->tx_synced_txg) {
909c5c6ffa0Smaybee 		txg_resume(dp);
910fa9e4066Sahrens 		/*
911fa9e4066Sahrens 		 * If we're running ziltest, we need the blkptr regardless.
912fa9e4066Sahrens 		 */
913fa9e4066Sahrens 		if (txg > spa_freeze_txg(dp->dp_spa)) {
914fa9e4066Sahrens 			/* if db_blkptr == NULL, this was an empty write */
915fa9e4066Sahrens 			if (db->db_blkptr)
916fa9e4066Sahrens 				*bp = *db->db_blkptr; /* structure assignment */
917fa9e4066Sahrens 			return (0);
918fa9e4066Sahrens 		}
919c5c6ffa0Smaybee 		return (EEXIST);
920fa9e4066Sahrens 	}
921fa9e4066Sahrens 
922fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
923fa9e4066Sahrens 
924c5c6ffa0Smaybee 	if (txg == tx->tx_syncing_txg) {
925c5c6ffa0Smaybee 		while (db->db_data_pending) {
926c5c6ffa0Smaybee 			/*
927c5c6ffa0Smaybee 			 * IO is in-progress.  Wait for it to finish.
928c5c6ffa0Smaybee 			 * XXX - would be nice to be able to somehow "attach"
929c5c6ffa0Smaybee 			 * this zio to the parent zio passed in.
930c5c6ffa0Smaybee 			 */
931c5c6ffa0Smaybee 			cv_wait(&db->db_changed, &db->db_mtx);
93213506d1eSmaybee 			if (!db->db_data_pending &&
93313506d1eSmaybee 			    db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
93413506d1eSmaybee 				/*
93513506d1eSmaybee 				 * IO was compressed away
93613506d1eSmaybee 				 */
93713506d1eSmaybee 				*bp = *db->db_blkptr; /* structure assignment */
93813506d1eSmaybee 				mutex_exit(&db->db_mtx);
93913506d1eSmaybee 				txg_resume(dp);
94013506d1eSmaybee 				return (0);
94113506d1eSmaybee 			}
942c5c6ffa0Smaybee 			ASSERT(db->db_data_pending ||
943c5c6ffa0Smaybee 			    (db->db_blkptr && db->db_blkptr->blk_birth == txg));
944c5c6ffa0Smaybee 		}
945c5c6ffa0Smaybee 
946c5c6ffa0Smaybee 		if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
947c5c6ffa0Smaybee 			/*
948c5c6ffa0Smaybee 			 * IO is already completed.
949c5c6ffa0Smaybee 			 */
950c5c6ffa0Smaybee 			*bp = *db->db_blkptr; /* structure assignment */
951c5c6ffa0Smaybee 			mutex_exit(&db->db_mtx);
952c5c6ffa0Smaybee 			txg_resume(dp);
953c5c6ffa0Smaybee 			return (0);
954c5c6ffa0Smaybee 		}
955c5c6ffa0Smaybee 	}
956c5c6ffa0Smaybee 
957c717a561Smaybee 	dr = db->db_last_dirty;
958c717a561Smaybee 	while (dr && dr->dr_txg > txg)
959c717a561Smaybee 		dr = dr->dr_next;
960c717a561Smaybee 	if (dr == NULL || dr->dr_txg < txg) {
961c5c6ffa0Smaybee 		/*
962c5c6ffa0Smaybee 		 * This dbuf isn't dirty, must have been free_range'd.
963fa9e4066Sahrens 		 * There's no need to log writes to freed blocks, so we're done.
964fa9e4066Sahrens 		 */
965fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
966c5c6ffa0Smaybee 		txg_resume(dp);
967fa9e4066Sahrens 		return (ENOENT);
968fa9e4066Sahrens 	}
969fa9e4066Sahrens 
970c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
971c717a561Smaybee 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
972c5c6ffa0Smaybee 		/*
973c717a561Smaybee 		 * We have already issued a sync write for this buffer.
974c5c6ffa0Smaybee 		 */
975c717a561Smaybee 		mutex_exit(&db->db_mtx);
976c717a561Smaybee 		txg_resume(dp);
977c717a561Smaybee 		return (EALREADY);
978c717a561Smaybee 	} else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
979c717a561Smaybee 		/*
980c717a561Smaybee 		 * This buffer has already been synced.  It could not
981c717a561Smaybee 		 * have been dirtied since, or we would have cleared the state.
982c717a561Smaybee 		 */
983c717a561Smaybee 		*bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
984c717a561Smaybee 		mutex_exit(&db->db_mtx);
985c717a561Smaybee 		txg_resume(dp);
986c717a561Smaybee 		return (0);
987c717a561Smaybee 	}
988c717a561Smaybee 
989c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
990c717a561Smaybee 	in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
991c717a561Smaybee 	in->dr = dr;
992c5c6ffa0Smaybee 	in->done = done;
993c5c6ffa0Smaybee 	in->arg = arg;
994fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
995c5c6ffa0Smaybee 	txg_resume(dp);
996fa9e4066Sahrens 
997c5c6ffa0Smaybee 	zb.zb_objset = os->os_dsl_dataset->ds_object;
998ea8dc4b6Seschrock 	zb.zb_object = db->db.db_object;
999ea8dc4b6Seschrock 	zb.zb_level = db->db_level;
1000ea8dc4b6Seschrock 	zb.zb_blkid = db->db_blkid;
1001e14bb325SJeff Bonwick 
1002088f3894Sahrens 	wp.wp_type = db->db_dnode->dn_type;
1003088f3894Sahrens 	wp.wp_level = db->db_level;
1004e14bb325SJeff Bonwick 	wp.wp_copies = os->os_copies;
1005088f3894Sahrens 	wp.wp_dnchecksum = db->db_dnode->dn_checksum;
1006088f3894Sahrens 	wp.wp_oschecksum = os->os_checksum;
1007088f3894Sahrens 	wp.wp_dncompress = db->db_dnode->dn_compress;
1008088f3894Sahrens 	wp.wp_oscompress = os->os_compress;
1009e14bb325SJeff Bonwick 
1010e14bb325SJeff Bonwick 	ASSERT(BP_IS_HOLE(bp));
1011e14bb325SJeff Bonwick 
1012e14bb325SJeff Bonwick 	zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db),
1013e14bb325SJeff Bonwick 	    txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in,
1014e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1015c717a561Smaybee 	if (pio) {
1016c717a561Smaybee 		zio_nowait(zio);
1017c717a561Smaybee 		err = EINPROGRESS;
1018c717a561Smaybee 	} else {
1019c717a561Smaybee 		err = zio_wait(zio);
1020c717a561Smaybee 		ASSERT(err == 0);
1021c717a561Smaybee 	}
1022c717a561Smaybee 	return (err);
1023fa9e4066Sahrens }
1024fa9e4066Sahrens 
1025fa9e4066Sahrens int
1026fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1027fa9e4066Sahrens 	dmu_tx_t *tx)
1028fa9e4066Sahrens {
1029ea8dc4b6Seschrock 	dnode_t *dn;
1030ea8dc4b6Seschrock 	int err;
1031ea8dc4b6Seschrock 
1032ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
1033ea8dc4b6Seschrock 	if (err)
1034ea8dc4b6Seschrock 		return (err);
1035ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1036fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1037fa9e4066Sahrens 	return (err);
1038fa9e4066Sahrens }
1039fa9e4066Sahrens 
1040fa9e4066Sahrens void
1041fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1042fa9e4066Sahrens 	dmu_tx_t *tx)
1043fa9e4066Sahrens {
1044ea8dc4b6Seschrock 	dnode_t *dn;
1045ea8dc4b6Seschrock 
1046ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1047ea8dc4b6Seschrock 	(void) dnode_hold(os->os, object, FTAG, &dn);
1048fa9e4066Sahrens 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1049fa9e4066Sahrens 	dn->dn_checksum = checksum;
1050fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1051fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1052fa9e4066Sahrens }
1053fa9e4066Sahrens 
1054fa9e4066Sahrens void
1055fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1056fa9e4066Sahrens 	dmu_tx_t *tx)
1057fa9e4066Sahrens {
1058ea8dc4b6Seschrock 	dnode_t *dn;
1059ea8dc4b6Seschrock 
1060ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1061ea8dc4b6Seschrock 	(void) dnode_hold(os->os, object, FTAG, &dn);
1062fa9e4066Sahrens 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1063fa9e4066Sahrens 	dn->dn_compress = compress;
1064fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1065fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1066fa9e4066Sahrens }
1067fa9e4066Sahrens 
106844cd46caSbillm int
1069fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1070fa9e4066Sahrens {
1071fa9e4066Sahrens 	dnode_t *dn;
1072fa9e4066Sahrens 	int i, err;
1073fa9e4066Sahrens 
1074ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
1075ea8dc4b6Seschrock 	if (err)
1076ea8dc4b6Seschrock 		return (err);
1077fa9e4066Sahrens 	/*
1078fa9e4066Sahrens 	 * Sync any current changes before
1079fa9e4066Sahrens 	 * we go trundling through the block pointers.
1080fa9e4066Sahrens 	 */
1081fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1082c543ec06Sahrens 		if (list_link_active(&dn->dn_dirty_link[i]))
1083fa9e4066Sahrens 			break;
1084fa9e4066Sahrens 	}
1085fa9e4066Sahrens 	if (i != TXG_SIZE) {
1086fa9e4066Sahrens 		dnode_rele(dn, FTAG);
1087fa9e4066Sahrens 		txg_wait_synced(dmu_objset_pool(os), 0);
1088ea8dc4b6Seschrock 		err = dnode_hold(os->os, object, FTAG, &dn);
1089ea8dc4b6Seschrock 		if (err)
1090ea8dc4b6Seschrock 			return (err);
1091fa9e4066Sahrens 	}
1092fa9e4066Sahrens 
1093cdb0ab79Smaybee 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1094fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1095fa9e4066Sahrens 
1096fa9e4066Sahrens 	return (err);
1097fa9e4066Sahrens }
1098fa9e4066Sahrens 
1099fa9e4066Sahrens void
1100fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1101fa9e4066Sahrens {
1102fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1103fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1104fa9e4066Sahrens 
1105fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1106fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1107fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1108fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1109fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1110fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
111199653d4eSeschrock 	doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
111299653d4eSeschrock 	    SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1113fa9e4066Sahrens 	doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1114fa9e4066Sahrens 	doi->doi_type = dn->dn_type;
1115fa9e4066Sahrens 	doi->doi_bonus_size = dn->dn_bonuslen;
1116fa9e4066Sahrens 	doi->doi_bonus_type = dn->dn_bonustype;
1117fa9e4066Sahrens 
1118fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1119fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1120fa9e4066Sahrens }
1121fa9e4066Sahrens 
1122fa9e4066Sahrens /*
1123fa9e4066Sahrens  * Get information on a DMU object.
1124fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1125fa9e4066Sahrens  */
1126fa9e4066Sahrens int
1127fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1128fa9e4066Sahrens {
1129ea8dc4b6Seschrock 	dnode_t *dn;
1130ea8dc4b6Seschrock 	int err = dnode_hold(os->os, object, FTAG, &dn);
1131fa9e4066Sahrens 
1132ea8dc4b6Seschrock 	if (err)
1133ea8dc4b6Seschrock 		return (err);
1134fa9e4066Sahrens 
1135fa9e4066Sahrens 	if (doi != NULL)
1136fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
1137fa9e4066Sahrens 
1138fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1139fa9e4066Sahrens 	return (0);
1140fa9e4066Sahrens }
1141fa9e4066Sahrens 
1142fa9e4066Sahrens /*
1143fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
1144fa9e4066Sahrens  */
1145fa9e4066Sahrens void
1146fa9e4066Sahrens dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1147fa9e4066Sahrens {
1148fa9e4066Sahrens 	dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1149fa9e4066Sahrens }
1150fa9e4066Sahrens 
1151fa9e4066Sahrens /*
1152fa9e4066Sahrens  * Faster still when you only care about the size.
1153fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
1154fa9e4066Sahrens  */
1155fa9e4066Sahrens void
1156fa9e4066Sahrens dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1157fa9e4066Sahrens {
1158fa9e4066Sahrens 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1159fa9e4066Sahrens 
1160fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
116199653d4eSeschrock 	/* add 1 for dnode space */
116299653d4eSeschrock 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
116399653d4eSeschrock 	    SPA_MINBLOCKSHIFT) + 1;
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
1166fa9e4066Sahrens void
1167fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
1168fa9e4066Sahrens {
1169fa9e4066Sahrens 	uint64_t *buf = vbuf;
1170fa9e4066Sahrens 	size_t count = size >> 3;
1171fa9e4066Sahrens 	int i;
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	ASSERT((size & 7) == 0);
1174fa9e4066Sahrens 
1175fa9e4066Sahrens 	for (i = 0; i < count; i++)
1176fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
1177fa9e4066Sahrens }
1178fa9e4066Sahrens 
1179fa9e4066Sahrens void
1180fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
1181fa9e4066Sahrens {
1182fa9e4066Sahrens 	uint32_t *buf = vbuf;
1183fa9e4066Sahrens 	size_t count = size >> 2;
1184fa9e4066Sahrens 	int i;
1185fa9e4066Sahrens 
1186fa9e4066Sahrens 	ASSERT((size & 3) == 0);
1187fa9e4066Sahrens 
1188fa9e4066Sahrens 	for (i = 0; i < count; i++)
1189fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
1190fa9e4066Sahrens }
1191fa9e4066Sahrens 
1192fa9e4066Sahrens void
1193fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
1194fa9e4066Sahrens {
1195fa9e4066Sahrens 	uint16_t *buf = vbuf;
1196fa9e4066Sahrens 	size_t count = size >> 1;
1197fa9e4066Sahrens 	int i;
1198fa9e4066Sahrens 
1199fa9e4066Sahrens 	ASSERT((size & 1) == 0);
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	for (i = 0; i < count; i++)
1202fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
1203fa9e4066Sahrens }
1204fa9e4066Sahrens 
1205fa9e4066Sahrens /* ARGSUSED */
1206fa9e4066Sahrens void
1207fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
1208fa9e4066Sahrens {
1209fa9e4066Sahrens }
1210fa9e4066Sahrens 
1211fa9e4066Sahrens void
1212fa9e4066Sahrens dmu_init(void)
1213fa9e4066Sahrens {
1214fa9e4066Sahrens 	dbuf_init();
1215fa9e4066Sahrens 	dnode_init();
1216fa9e4066Sahrens 	arc_init();
1217fa94a07fSbrendan 	l2arc_init();
1218fa9e4066Sahrens }
1219fa9e4066Sahrens 
1220fa9e4066Sahrens void
1221fa9e4066Sahrens dmu_fini(void)
1222fa9e4066Sahrens {
1223fa9e4066Sahrens 	arc_fini();
1224fa9e4066Sahrens 	dnode_fini();
1225fa9e4066Sahrens 	dbuf_fini();
1226fa94a07fSbrendan 	l2arc_fini();
1227fa9e4066Sahrens }
1228