xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6*ea8dc4b6Seschrock  * 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 /*
22*ea8dc4b6Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/dmu.h>
29fa9e4066Sahrens #include <sys/dmu_impl.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dbuf.h>
32fa9e4066Sahrens #include <sys/dnode.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens #include <sys/dmu_objset.h>
35fa9e4066Sahrens #include <sys/dmu_traverse.h>
36fa9e4066Sahrens #include <sys/dsl_dataset.h>
37fa9e4066Sahrens #include <sys/dsl_dir.h>
38fa9e4066Sahrens #include <sys/dsl_pool.h>
39fa9e4066Sahrens #include <sys/dmu_zfetch.h>
40fa9e4066Sahrens #include <sys/zfs_ioctl.h>
41fa9e4066Sahrens #include <sys/zap.h>
42*ea8dc4b6Seschrock #include <sys/zio_checksum.h>
43fa9e4066Sahrens 
44fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
45fa9e4066Sahrens 	{	byteswap_uint8_array,	TRUE,	"unallocated"		},
46fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"object directory"	},
47fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"object array"		},
48fa9e4066Sahrens 	{	byteswap_uint8_array,	TRUE,	"packed nvlist"		},
49fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"packed nvlist size"	},
50fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"bplist"		},
51fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"bplist header"		},
52fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"SPA space map header"	},
53fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"SPA space map"		},
54fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"ZIL intent log"	},
55fa9e4066Sahrens 	{	dnode_buf_byteswap,	TRUE,	"DMU dnode"		},
56fa9e4066Sahrens 	{	dmu_objset_byteswap,	TRUE,	"DMU objset"		},
57fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"DSL directory"		},
58fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL directory child map"},
59fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL dataset snap map"	},
60fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"DSL props"		},
61fa9e4066Sahrens 	{	byteswap_uint64_array,	TRUE,	"DSL dataset"		},
62fa9e4066Sahrens 	{	zfs_znode_byteswap,	TRUE,	"ZFS znode"		},
63fa9e4066Sahrens 	{	zfs_acl_byteswap,	TRUE,	"ZFS ACL"		},
64fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"ZFS plain file"	},
65fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS directory"		},
66fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS master node"	},
67fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"ZFS delete queue"	},
68fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"zvol object"		},
69fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"zvol prop"		},
70fa9e4066Sahrens 	{	byteswap_uint8_array,	FALSE,	"other uint8[]"		},
71fa9e4066Sahrens 	{	byteswap_uint64_array,	FALSE,	"other uint64[]"	},
72fa9e4066Sahrens 	{	zap_byteswap,		TRUE,	"other ZAP"		},
73*ea8dc4b6Seschrock 	{	zap_byteswap,		TRUE,	"persistent error log"	},
74fa9e4066Sahrens };
75fa9e4066Sahrens 
76fa9e4066Sahrens int
77*ea8dc4b6Seschrock dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
78*ea8dc4b6Seschrock     void *tag, dmu_buf_t **dbp)
79fa9e4066Sahrens {
80fa9e4066Sahrens 	dnode_t *dn;
81fa9e4066Sahrens 	uint64_t blkid;
82fa9e4066Sahrens 	dmu_buf_impl_t *db;
83*ea8dc4b6Seschrock 	int err;
84fa9e4066Sahrens 
85fa9e4066Sahrens 	/* dataset_verify(dd); */
86fa9e4066Sahrens 
87*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
88*ea8dc4b6Seschrock 	if (err)
89*ea8dc4b6Seschrock 		return (err);
90fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
91fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
92*ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
93fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
94*ea8dc4b6Seschrock 	if (db == NULL) {
95*ea8dc4b6Seschrock 		err = EIO;
96*ea8dc4b6Seschrock 	} else {
97*ea8dc4b6Seschrock 		err = dbuf_read(db, NULL, DB_RF_CANFAIL);
98*ea8dc4b6Seschrock 		if (err) {
99*ea8dc4b6Seschrock 			dbuf_rele(db, tag);
100*ea8dc4b6Seschrock 			db = NULL;
101*ea8dc4b6Seschrock 		}
102fa9e4066Sahrens 	}
103fa9e4066Sahrens 
104*ea8dc4b6Seschrock 	dnode_rele(dn, FTAG);
105*ea8dc4b6Seschrock 	*dbp = &db->db;
106*ea8dc4b6Seschrock 	return (err);
107fa9e4066Sahrens }
108fa9e4066Sahrens 
109fa9e4066Sahrens int
110fa9e4066Sahrens dmu_bonus_max(void)
111fa9e4066Sahrens {
112fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
113fa9e4066Sahrens }
114fa9e4066Sahrens 
115fa9e4066Sahrens /*
116*ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
117fa9e4066Sahrens  */
118*ea8dc4b6Seschrock int
119*ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
120fa9e4066Sahrens {
121*ea8dc4b6Seschrock 	dnode_t *dn;
122*ea8dc4b6Seschrock 	int err, count;
123fa9e4066Sahrens 	dmu_buf_impl_t *db;
124fa9e4066Sahrens 
125*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
126*ea8dc4b6Seschrock 	if (err)
127*ea8dc4b6Seschrock 		return (err);
128fa9e4066Sahrens 
129fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
130*ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
131fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
132*ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
133*ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
134*ea8dc4b6Seschrock 			dn->dn_bonus = dbuf_create_bonus(dn);
135fa9e4066Sahrens 	}
136*ea8dc4b6Seschrock 	db = dn->dn_bonus;
137*ea8dc4b6Seschrock 	rw_exit(&dn->dn_struct_rwlock);
138*ea8dc4b6Seschrock 	mutex_enter(&db->db_mtx);
139*ea8dc4b6Seschrock 	count = refcount_add(&db->db_holds, tag);
140*ea8dc4b6Seschrock 	mutex_exit(&db->db_mtx);
141*ea8dc4b6Seschrock 	if (count == 1)
142*ea8dc4b6Seschrock 		dnode_add_ref(dn, db);
143fa9e4066Sahrens 	dnode_rele(dn, FTAG);
144*ea8dc4b6Seschrock 
145*ea8dc4b6Seschrock 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
146*ea8dc4b6Seschrock 
147*ea8dc4b6Seschrock 	*dbp = &db->db;
148*ea8dc4b6Seschrock 	return (0);
149fa9e4066Sahrens }
150fa9e4066Sahrens 
151*ea8dc4b6Seschrock int
152*ea8dc4b6Seschrock dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
153*ea8dc4b6Seschrock     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
154fa9e4066Sahrens {
155*ea8dc4b6Seschrock 	dnode_t *dn;
156fa9e4066Sahrens 	dmu_buf_t **dbp;
157fa9e4066Sahrens 	uint64_t blkid, nblks, i;
158*ea8dc4b6Seschrock 	uint32_t flags;
159*ea8dc4b6Seschrock 	int err;
160*ea8dc4b6Seschrock 	zio_t *zio;
161*ea8dc4b6Seschrock 
162*ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
163fa9e4066Sahrens 
164fa9e4066Sahrens 	if (length == 0) {
165fa9e4066Sahrens 		if (numbufsp)
166fa9e4066Sahrens 			*numbufsp = 0;
167*ea8dc4b6Seschrock 		*dbpp = NULL;
168*ea8dc4b6Seschrock 		return (0);
169fa9e4066Sahrens 	}
170fa9e4066Sahrens 
171*ea8dc4b6Seschrock 	flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
172*ea8dc4b6Seschrock 	if (length >= zfetch_array_rd_sz)
173*ea8dc4b6Seschrock 		flags |= DB_RF_NOPREFETCH;
174*ea8dc4b6Seschrock 
175*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
176*ea8dc4b6Seschrock 	if (err)
177*ea8dc4b6Seschrock 		return (err);
178*ea8dc4b6Seschrock 
179fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
180fa9e4066Sahrens 	if (dn->dn_datablkshift) {
181fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
182fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
183fa9e4066Sahrens 			P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
184fa9e4066Sahrens 	} else {
185fa9e4066Sahrens 		ASSERT3U(offset + length, <=, dn->dn_datablksz);
186fa9e4066Sahrens 		nblks = 1;
187fa9e4066Sahrens 	}
188*ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
189fa9e4066Sahrens 
190*ea8dc4b6Seschrock 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, TRUE);
191fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
192fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
193*ea8dc4b6Seschrock 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
194*ea8dc4b6Seschrock 		if (db == NULL) {
195*ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
196*ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
197*ea8dc4b6Seschrock 			dnode_rele(dn, FTAG);
198*ea8dc4b6Seschrock 			zio_nowait(zio);
199*ea8dc4b6Seschrock 			return (EIO);
200*ea8dc4b6Seschrock 		}
201*ea8dc4b6Seschrock 		/* initiate async i/o */
202*ea8dc4b6Seschrock 		if (read && db->db_state == DB_UNCACHED) {
203*ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
204*ea8dc4b6Seschrock 			(void) dbuf_read(db, zio, flags);
205*ea8dc4b6Seschrock 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
206*ea8dc4b6Seschrock 		}
207*ea8dc4b6Seschrock 		dbp[i] = &db->db;
208fa9e4066Sahrens 	}
209fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
210fa9e4066Sahrens 	dnode_rele(dn, FTAG);
211fa9e4066Sahrens 
212*ea8dc4b6Seschrock 	/* wait for async i/o */
213*ea8dc4b6Seschrock 	err = zio_wait(zio);
214*ea8dc4b6Seschrock 	if (err) {
215*ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
216*ea8dc4b6Seschrock 		return (err);
217*ea8dc4b6Seschrock 	}
218*ea8dc4b6Seschrock 
219*ea8dc4b6Seschrock 	/* wait for other io to complete */
220*ea8dc4b6Seschrock 	if (read) {
221*ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
222*ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
223*ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
224*ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
225*ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
226*ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
227*ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
228*ea8dc4b6Seschrock 				err = EIO;
229*ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
230*ea8dc4b6Seschrock 			if (err) {
231*ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
232*ea8dc4b6Seschrock 				return (err);
233*ea8dc4b6Seschrock 			}
234*ea8dc4b6Seschrock 		}
235*ea8dc4b6Seschrock 	}
236*ea8dc4b6Seschrock 
237*ea8dc4b6Seschrock 	*numbufsp = nblks;
238*ea8dc4b6Seschrock 	*dbpp = dbp;
239*ea8dc4b6Seschrock 	return (0);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242fa9e4066Sahrens void
243*ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
244fa9e4066Sahrens {
245fa9e4066Sahrens 	int i;
246fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
247fa9e4066Sahrens 
248fa9e4066Sahrens 	if (numbufs == 0)
249fa9e4066Sahrens 		return;
250fa9e4066Sahrens 
251*ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
252*ea8dc4b6Seschrock 		if (dbp[i])
253*ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
254*ea8dc4b6Seschrock 	}
255fa9e4066Sahrens 
256fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
257fa9e4066Sahrens }
258fa9e4066Sahrens 
259fa9e4066Sahrens void
260fa9e4066Sahrens dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
261fa9e4066Sahrens {
262fa9e4066Sahrens 	dnode_t *dn;
263fa9e4066Sahrens 	uint64_t blkid;
264*ea8dc4b6Seschrock 	int nblks, i, err;
265fa9e4066Sahrens 
266fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
267fa9e4066Sahrens 		dn = os->os->os_meta_dnode;
268fa9e4066Sahrens 
269fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
270fa9e4066Sahrens 			return;
271fa9e4066Sahrens 
272fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
273fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
274fa9e4066Sahrens 		dbuf_prefetch(dn, blkid);
275fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
276fa9e4066Sahrens 		return;
277fa9e4066Sahrens 	}
278fa9e4066Sahrens 
279fa9e4066Sahrens 	/*
280fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
281fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
282fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
283fa9e4066Sahrens 	 */
284*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
285*ea8dc4b6Seschrock 	if (err != 0)
286fa9e4066Sahrens 		return;
287fa9e4066Sahrens 
288fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
289fa9e4066Sahrens 	if (dn->dn_datablkshift) {
290fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
291fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
292fa9e4066Sahrens 			P2ALIGN(offset, 1<<blkshift)) >> blkshift;
293fa9e4066Sahrens 	} else {
294fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
295fa9e4066Sahrens 	}
296fa9e4066Sahrens 
297fa9e4066Sahrens 	if (nblks != 0) {
298fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, offset);
299fa9e4066Sahrens 		for (i = 0; i < nblks; i++)
300fa9e4066Sahrens 			dbuf_prefetch(dn, blkid+i);
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
304fa9e4066Sahrens 
305fa9e4066Sahrens 	dnode_rele(dn, FTAG);
306fa9e4066Sahrens }
307fa9e4066Sahrens 
308*ea8dc4b6Seschrock int
309fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
310fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
311fa9e4066Sahrens {
312*ea8dc4b6Seschrock 	dnode_t *dn;
313*ea8dc4b6Seschrock 	int err = dnode_hold(os->os, object, FTAG, &dn);
314*ea8dc4b6Seschrock 	if (err)
315*ea8dc4b6Seschrock 		return (err);
316fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
317fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
318fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
319fa9e4066Sahrens 	dnode_rele(dn, FTAG);
320*ea8dc4b6Seschrock 	return (0);
321fa9e4066Sahrens }
322fa9e4066Sahrens 
323*ea8dc4b6Seschrock int
324*ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
325*ea8dc4b6Seschrock     void *buf)
326fa9e4066Sahrens {
327fa9e4066Sahrens 	dnode_t *dn;
328fa9e4066Sahrens 	dmu_buf_t **dbp;
329*ea8dc4b6Seschrock 	int numbufs, i, err;
330fa9e4066Sahrens 
331*ea8dc4b6Seschrock 	/*
332*ea8dc4b6Seschrock 	 * Deal with odd block sizes, where there can't be data past the
333*ea8dc4b6Seschrock 	 * first block.
334*ea8dc4b6Seschrock 	 */
335*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
336*ea8dc4b6Seschrock 	if (err)
337*ea8dc4b6Seschrock 		return (err);
338fa9e4066Sahrens 	if (dn->dn_datablkshift == 0) {
339fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
340fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
341fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
342fa9e4066Sahrens 		size = newsz;
343fa9e4066Sahrens 	}
344fa9e4066Sahrens 	dnode_rele(dn, FTAG);
345fa9e4066Sahrens 
346fa9e4066Sahrens 	while (size > 0) {
347fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
348fa9e4066Sahrens 		int err;
349fa9e4066Sahrens 
350fa9e4066Sahrens 		/*
351fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
352fa9e4066Sahrens 		 * to be reading in parallel.
353fa9e4066Sahrens 		 */
354*ea8dc4b6Seschrock 		err = dmu_buf_hold_array(os, object, offset, mylen,
355*ea8dc4b6Seschrock 		    TRUE, FTAG, &numbufs, &dbp);
356*ea8dc4b6Seschrock 		if (err)
357fa9e4066Sahrens 			return (err);
358fa9e4066Sahrens 
359fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
360fa9e4066Sahrens 			int tocpy;
361fa9e4066Sahrens 			int bufoff;
362fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
363fa9e4066Sahrens 
364fa9e4066Sahrens 			ASSERT(size > 0);
365fa9e4066Sahrens 
366fa9e4066Sahrens 			bufoff = offset - db->db_offset;
367fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
368fa9e4066Sahrens 
369fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
370fa9e4066Sahrens 
371fa9e4066Sahrens 			offset += tocpy;
372fa9e4066Sahrens 			size -= tocpy;
373fa9e4066Sahrens 			buf = (char *)buf + tocpy;
374fa9e4066Sahrens 		}
375*ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
376fa9e4066Sahrens 	}
377fa9e4066Sahrens 	return (0);
378fa9e4066Sahrens }
379fa9e4066Sahrens 
380fa9e4066Sahrens void
381fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
382fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
383fa9e4066Sahrens {
384fa9e4066Sahrens 	dmu_buf_t **dbp;
385fa9e4066Sahrens 	int numbufs, i;
386fa9e4066Sahrens 
387*ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
388*ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
389fa9e4066Sahrens 
390fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
391fa9e4066Sahrens 		int tocpy;
392fa9e4066Sahrens 		int bufoff;
393fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
394fa9e4066Sahrens 
395fa9e4066Sahrens 		ASSERT(size > 0);
396fa9e4066Sahrens 
397fa9e4066Sahrens 		bufoff = offset - db->db_offset;
398fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
399fa9e4066Sahrens 
400fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
401fa9e4066Sahrens 
402fa9e4066Sahrens 		if (tocpy == db->db_size)
403fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
404fa9e4066Sahrens 		else
405fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
406fa9e4066Sahrens 
407fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
408fa9e4066Sahrens 
409fa9e4066Sahrens 		if (tocpy == db->db_size)
410fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
411fa9e4066Sahrens 
412fa9e4066Sahrens 		offset += tocpy;
413fa9e4066Sahrens 		size -= tocpy;
414fa9e4066Sahrens 		buf = (char *)buf + tocpy;
415fa9e4066Sahrens 	}
416*ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
417fa9e4066Sahrens }
418fa9e4066Sahrens 
419fa9e4066Sahrens #ifdef _KERNEL
420fa9e4066Sahrens int
421fa9e4066Sahrens dmu_write_uio(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
422fa9e4066Sahrens     uio_t *uio, dmu_tx_t *tx)
423fa9e4066Sahrens {
424fa9e4066Sahrens 	dmu_buf_t **dbp;
425fa9e4066Sahrens 	int numbufs, i;
426fa9e4066Sahrens 	int err = 0;
427fa9e4066Sahrens 
428*ea8dc4b6Seschrock 	err = dmu_buf_hold_array(os, object, offset, size,
429*ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp);
430*ea8dc4b6Seschrock 	if (err)
431*ea8dc4b6Seschrock 		return (err);
432fa9e4066Sahrens 
433fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
434fa9e4066Sahrens 		int tocpy;
435fa9e4066Sahrens 		int bufoff;
436fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
437fa9e4066Sahrens 
438fa9e4066Sahrens 		ASSERT(size > 0);
439fa9e4066Sahrens 
440fa9e4066Sahrens 		bufoff = offset - db->db_offset;
441fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
442fa9e4066Sahrens 
443fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
444fa9e4066Sahrens 
445fa9e4066Sahrens 		if (tocpy == db->db_size)
446fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
447fa9e4066Sahrens 		else
448fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
449fa9e4066Sahrens 
450fa9e4066Sahrens 		/*
451fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
452fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
453fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
454fa9e4066Sahrens 		 * block.
455fa9e4066Sahrens 		 */
456fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
457fa9e4066Sahrens 		    UIO_WRITE, uio);
458fa9e4066Sahrens 
459fa9e4066Sahrens 		if (tocpy == db->db_size)
460fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
461fa9e4066Sahrens 
462fa9e4066Sahrens 		if (err)
463fa9e4066Sahrens 			break;
464fa9e4066Sahrens 
465fa9e4066Sahrens 		offset += tocpy;
466fa9e4066Sahrens 		size -= tocpy;
467fa9e4066Sahrens 	}
468*ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
469fa9e4066Sahrens 	return (err);
470fa9e4066Sahrens }
471fa9e4066Sahrens #endif
472fa9e4066Sahrens 
473fa9e4066Sahrens struct backuparg {
474fa9e4066Sahrens 	dmu_replay_record_t *drr;
475fa9e4066Sahrens 	vnode_t *vp;
476fa9e4066Sahrens 	objset_t *os;
477*ea8dc4b6Seschrock 	zio_cksum_t zc;
478fa9e4066Sahrens 	int err;
479fa9e4066Sahrens };
480fa9e4066Sahrens 
481fa9e4066Sahrens static int
482fa9e4066Sahrens dump_bytes(struct backuparg *ba, void *buf, int len)
483fa9e4066Sahrens {
484fa9e4066Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
485fa9e4066Sahrens 	ASSERT3U(len % 8, ==, 0);
486*ea8dc4b6Seschrock 
487*ea8dc4b6Seschrock 	fletcher_4_incremental_native(buf, len, &ba->zc);
488fa9e4066Sahrens 	ba->err = vn_rdwr(UIO_WRITE, ba->vp,
489fa9e4066Sahrens 	    (caddr_t)buf, len,
490fa9e4066Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM_INFINITY, CRED(), &resid);
491fa9e4066Sahrens 	return (ba->err);
492fa9e4066Sahrens }
493fa9e4066Sahrens 
494fa9e4066Sahrens static int
495fa9e4066Sahrens dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
496fa9e4066Sahrens     uint64_t length)
497fa9e4066Sahrens {
498fa9e4066Sahrens 	/* write a FREE record */
499fa9e4066Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
500fa9e4066Sahrens 	ba->drr->drr_type = DRR_FREE;
501fa9e4066Sahrens 	ba->drr->drr_u.drr_free.drr_object = object;
502fa9e4066Sahrens 	ba->drr->drr_u.drr_free.drr_offset = offset;
503fa9e4066Sahrens 	ba->drr->drr_u.drr_free.drr_length = length;
504fa9e4066Sahrens 
505fa9e4066Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
506fa9e4066Sahrens 		return (EINTR);
507fa9e4066Sahrens 	return (0);
508fa9e4066Sahrens }
509fa9e4066Sahrens 
510fa9e4066Sahrens static int
511fa9e4066Sahrens dump_data(struct backuparg *ba, dmu_object_type_t type,
512fa9e4066Sahrens     uint64_t object, uint64_t offset, int blksz, void *data)
513fa9e4066Sahrens {
514fa9e4066Sahrens 	/* write a DATA record */
515fa9e4066Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
516fa9e4066Sahrens 	ba->drr->drr_type = DRR_WRITE;
517fa9e4066Sahrens 	ba->drr->drr_u.drr_write.drr_object = object;
518fa9e4066Sahrens 	ba->drr->drr_u.drr_write.drr_type = type;
519fa9e4066Sahrens 	ba->drr->drr_u.drr_write.drr_offset = offset;
520fa9e4066Sahrens 	ba->drr->drr_u.drr_write.drr_length = blksz;
521fa9e4066Sahrens 
522fa9e4066Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
523fa9e4066Sahrens 		return (EINTR);
524fa9e4066Sahrens 	if (dump_bytes(ba, data, blksz))
525fa9e4066Sahrens 		return (EINTR);
526fa9e4066Sahrens 	return (0);
527fa9e4066Sahrens }
528fa9e4066Sahrens 
529fa9e4066Sahrens static int
530fa9e4066Sahrens dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
531fa9e4066Sahrens {
532fa9e4066Sahrens 	/* write a FREEOBJECTS record */
533fa9e4066Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
534fa9e4066Sahrens 	ba->drr->drr_type = DRR_FREEOBJECTS;
535fa9e4066Sahrens 	ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj;
536fa9e4066Sahrens 	ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs;
537fa9e4066Sahrens 
538fa9e4066Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
539fa9e4066Sahrens 		return (EINTR);
540fa9e4066Sahrens 	return (0);
541fa9e4066Sahrens }
542fa9e4066Sahrens 
543fa9e4066Sahrens static int
544fa9e4066Sahrens dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
545fa9e4066Sahrens {
546fa9e4066Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
547fa9e4066Sahrens 		return (dump_freeobjects(ba, object, 1));
548fa9e4066Sahrens 
549fa9e4066Sahrens 	/* write an OBJECT record */
550fa9e4066Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
551fa9e4066Sahrens 	ba->drr->drr_type = DRR_OBJECT;
552fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_object = object;
553fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_type = dnp->dn_type;
554fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype;
555fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_blksz =
556fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
557fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen;
558fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum;
559fa9e4066Sahrens 	ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress;
560fa9e4066Sahrens 
561fa9e4066Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
562fa9e4066Sahrens 		return (EINTR);
563fa9e4066Sahrens 
564fa9e4066Sahrens 	if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)))
565fa9e4066Sahrens 		return (EINTR);
566fa9e4066Sahrens 
567fa9e4066Sahrens 	/* free anything past the end of the file */
568fa9e4066Sahrens 	if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
569fa9e4066Sahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
570fa9e4066Sahrens 		return (EINTR);
571fa9e4066Sahrens 	if (ba->err)
572fa9e4066Sahrens 		return (EINTR);
573fa9e4066Sahrens 	return (0);
574fa9e4066Sahrens }
575fa9e4066Sahrens 
576fa9e4066Sahrens #define	BP_SPAN(dnp, level) \
577fa9e4066Sahrens 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
578fa9e4066Sahrens 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
579fa9e4066Sahrens 
580fa9e4066Sahrens static int
581fa9e4066Sahrens backup_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
582fa9e4066Sahrens {
583fa9e4066Sahrens 	struct backuparg *ba = arg;
584fa9e4066Sahrens 	uint64_t object = bc->bc_bookmark.zb_object;
585fa9e4066Sahrens 	int level = bc->bc_bookmark.zb_level;
586fa9e4066Sahrens 	uint64_t blkid = bc->bc_bookmark.zb_blkid;
587fa9e4066Sahrens 	blkptr_t *bp = bc->bc_blkptr.blk_birth ? &bc->bc_blkptr : NULL;
588fa9e4066Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
589fa9e4066Sahrens 	void *data = bc->bc_data;
590fa9e4066Sahrens 	int err = 0;
591fa9e4066Sahrens 
592*ea8dc4b6Seschrock 	if (issig(JUSTLOOKING) && issig(FORREAL))
593fa9e4066Sahrens 		return (EINTR);
594fa9e4066Sahrens 
595fa9e4066Sahrens 	ASSERT(data || bp == NULL);
596fa9e4066Sahrens 
597fa9e4066Sahrens 	if (bp == NULL && object == 0) {
598fa9e4066Sahrens 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
599fa9e4066Sahrens 		uint64_t dnobj = (blkid * span) >> DNODE_SHIFT;
600fa9e4066Sahrens 		err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
601fa9e4066Sahrens 	} else if (bp == NULL) {
602fa9e4066Sahrens 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
603fa9e4066Sahrens 		err = dump_free(ba, object, blkid * span, span);
604fa9e4066Sahrens 	} else if (data && level == 0 && type == DMU_OT_DNODE) {
605fa9e4066Sahrens 		dnode_phys_t *blk = data;
606fa9e4066Sahrens 		int i;
607fa9e4066Sahrens 		int blksz = BP_GET_LSIZE(bp);
608fa9e4066Sahrens 
609fa9e4066Sahrens 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
610fa9e4066Sahrens 			uint64_t dnobj =
611fa9e4066Sahrens 			    (blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
612fa9e4066Sahrens 			err = dump_dnode(ba, dnobj, blk+i);
613fa9e4066Sahrens 			if (err)
614fa9e4066Sahrens 				break;
615fa9e4066Sahrens 		}
616fa9e4066Sahrens 	} else if (level == 0 &&
617fa9e4066Sahrens 	    type != DMU_OT_DNODE && type != DMU_OT_OBJSET) {
618fa9e4066Sahrens 		int blksz = BP_GET_LSIZE(bp);
619fa9e4066Sahrens 		if (data == NULL) {
620fa9e4066Sahrens 			arc_buf_t *abuf;
621*ea8dc4b6Seschrock 			zbookmark_t zb;
622fa9e4066Sahrens 
623*ea8dc4b6Seschrock 			zb.zb_objset = ba->os->os->os_dsl_dataset->ds_object;
624*ea8dc4b6Seschrock 			zb.zb_object = object;
625*ea8dc4b6Seschrock 			zb.zb_level = level;
626*ea8dc4b6Seschrock 			zb.zb_blkid = blkid;
627fa9e4066Sahrens 			(void) arc_read(NULL, spa, bp,
628fa9e4066Sahrens 			    dmu_ot[type].ot_byteswap, arc_getbuf_func, &abuf,
629fa9e4066Sahrens 			    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_MUSTSUCCEED,
630*ea8dc4b6Seschrock 			    ARC_WAIT, &zb);
631fa9e4066Sahrens 
632fa9e4066Sahrens 			if (abuf) {
633fa9e4066Sahrens 				err = dump_data(ba, type, object, blkid * blksz,
634fa9e4066Sahrens 				    blksz, abuf->b_data);
635*ea8dc4b6Seschrock 				(void) arc_buf_remove_ref(abuf, &abuf);
636fa9e4066Sahrens 			}
637fa9e4066Sahrens 		} else {
638fa9e4066Sahrens 			err = dump_data(ba, type, object, blkid * blksz,
639fa9e4066Sahrens 			    blksz, data);
640fa9e4066Sahrens 		}
641fa9e4066Sahrens 	}
642fa9e4066Sahrens 
643fa9e4066Sahrens 	ASSERT(err == 0 || err == EINTR);
644fa9e4066Sahrens 	return (err);
645fa9e4066Sahrens }
646fa9e4066Sahrens 
647fa9e4066Sahrens int
648fa9e4066Sahrens dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, vnode_t *vp)
649fa9e4066Sahrens {
650fa9e4066Sahrens 	dsl_dataset_t *ds = tosnap->os->os_dsl_dataset;
651fa9e4066Sahrens 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os->os_dsl_dataset : NULL;
652fa9e4066Sahrens 	dmu_replay_record_t *drr;
653fa9e4066Sahrens 	struct backuparg ba;
654fa9e4066Sahrens 	int err;
655fa9e4066Sahrens 
656fa9e4066Sahrens 	/* tosnap must be a snapshot */
657fa9e4066Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
658fa9e4066Sahrens 		return (EINVAL);
659fa9e4066Sahrens 
660fa9e4066Sahrens 	/* fromsnap must be an earlier snapshot from the same fs as tosnap */
661fa9e4066Sahrens 	if (fromds && (ds->ds_dir != fromds->ds_dir ||
662fa9e4066Sahrens 	    fromds->ds_phys->ds_creation_txg >=
663fa9e4066Sahrens 	    ds->ds_phys->ds_creation_txg))
664fa9e4066Sahrens 		return (EXDEV);
665fa9e4066Sahrens 
666fa9e4066Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
667fa9e4066Sahrens 	drr->drr_type = DRR_BEGIN;
668fa9e4066Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
669fa9e4066Sahrens 	drr->drr_u.drr_begin.drr_version = DMU_BACKUP_VERSION;
670fa9e4066Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
671fa9e4066Sahrens 	    ds->ds_phys->ds_creation_time;
672fa9e4066Sahrens 	drr->drr_u.drr_begin.drr_type = tosnap->os->os_phys->os_type;
673fa9e4066Sahrens 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
674fa9e4066Sahrens 	if (fromds)
675fa9e4066Sahrens 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
676fa9e4066Sahrens 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
677fa9e4066Sahrens 
678fa9e4066Sahrens 	ba.drr = drr;
679fa9e4066Sahrens 	ba.vp = vp;
680fa9e4066Sahrens 	ba.os = tosnap;
681*ea8dc4b6Seschrock 	ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
682fa9e4066Sahrens 
683fa9e4066Sahrens 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
684fa9e4066Sahrens 		kmem_free(drr, sizeof (dmu_replay_record_t));
685fa9e4066Sahrens 		return (ba.err);
686fa9e4066Sahrens 	}
687fa9e4066Sahrens 
688fa9e4066Sahrens 	err = traverse_dsl_dataset(ds,
689fa9e4066Sahrens 	    fromds ? fromds->ds_phys->ds_creation_txg : 0,
690fa9e4066Sahrens 	    ADVANCE_PRE | ADVANCE_HOLES | ADVANCE_DATA | ADVANCE_NOLOCK,
691fa9e4066Sahrens 	    backup_cb, &ba);
692fa9e4066Sahrens 
693fa9e4066Sahrens 	if (err) {
694fa9e4066Sahrens 		if (err == EINTR && ba.err)
695fa9e4066Sahrens 			err = ba.err;
696fa9e4066Sahrens 		return (err);
697fa9e4066Sahrens 	}
698fa9e4066Sahrens 
699fa9e4066Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
700fa9e4066Sahrens 	drr->drr_type = DRR_END;
701*ea8dc4b6Seschrock 	drr->drr_u.drr_end.drr_checksum = ba.zc;
702fa9e4066Sahrens 
703fa9e4066Sahrens 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)))
704fa9e4066Sahrens 		return (ba.err);
705fa9e4066Sahrens 
706fa9e4066Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
707fa9e4066Sahrens 
708fa9e4066Sahrens 	return (0);
709fa9e4066Sahrens }
710fa9e4066Sahrens 
711fa9e4066Sahrens struct restorearg {
712fa9e4066Sahrens 	int err;
713fa9e4066Sahrens 	int byteswap;
714fa9e4066Sahrens 	vnode_t *vp;
715fa9e4066Sahrens 	char *buf;
716fa9e4066Sahrens 	uint64_t voff;
717fa9e4066Sahrens 	int buflen; /* number of valid bytes in buf */
718fa9e4066Sahrens 	int bufoff; /* next offset to read */
719fa9e4066Sahrens 	int bufsize; /* amount of memory allocated for buf */
720*ea8dc4b6Seschrock 	zio_cksum_t zc;
721fa9e4066Sahrens };
722fa9e4066Sahrens 
723fa9e4066Sahrens static int
724fa9e4066Sahrens replay_incremental_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
725fa9e4066Sahrens {
726fa9e4066Sahrens 	struct drr_begin *drrb = arg;
727fa9e4066Sahrens 	dsl_dataset_t *ds = NULL;
728fa9e4066Sahrens 	dsl_dataset_t *ds_prev = NULL;
729fa9e4066Sahrens 	const char *snapname;
730fa9e4066Sahrens 	int err = EINVAL;
731fa9e4066Sahrens 	uint64_t val;
732fa9e4066Sahrens 
733fa9e4066Sahrens 	/* this must be a filesytem */
734fa9e4066Sahrens 	if (dd->dd_phys->dd_head_dataset_obj == 0)
735fa9e4066Sahrens 		goto die;
736fa9e4066Sahrens 
737*ea8dc4b6Seschrock 	err = dsl_dataset_open_obj(dd->dd_pool,
738*ea8dc4b6Seschrock 	    dd->dd_phys->dd_head_dataset_obj,
739*ea8dc4b6Seschrock 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &ds);
740*ea8dc4b6Seschrock 	if (err)
741*ea8dc4b6Seschrock 		goto die;
742fa9e4066Sahrens 
743fa9e4066Sahrens 	if (ds == NULL) {
744fa9e4066Sahrens 		err = EBUSY;
745fa9e4066Sahrens 		goto die;
746fa9e4066Sahrens 	}
747fa9e4066Sahrens 
748fa9e4066Sahrens 	/* must already be a snapshot of this fs */
749fa9e4066Sahrens 	if (ds->ds_phys->ds_prev_snap_obj == 0) {
750fa9e4066Sahrens 		err = ENODEV;
751fa9e4066Sahrens 		goto die;
752fa9e4066Sahrens 	}
753fa9e4066Sahrens 
754fa9e4066Sahrens 	/* most recent snapshot must match fromguid */
755*ea8dc4b6Seschrock 	err = dsl_dataset_open_obj(dd->dd_pool,
756fa9e4066Sahrens 	    ds->ds_phys->ds_prev_snap_obj, NULL,
757*ea8dc4b6Seschrock 	    DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds_prev);
758*ea8dc4b6Seschrock 	if (err)
759*ea8dc4b6Seschrock 		goto die;
760fa9e4066Sahrens 	if (ds_prev->ds_phys->ds_guid != drrb->drr_fromguid) {
761fa9e4066Sahrens 		err = ENODEV;
762fa9e4066Sahrens 		goto die;
763fa9e4066Sahrens 	}
764fa9e4066Sahrens 
765fa9e4066Sahrens 	/* must not have any changes since most recent snapshot */
766fa9e4066Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >
767fa9e4066Sahrens 	    ds_prev->ds_phys->ds_creation_txg) {
768fa9e4066Sahrens 		err = ETXTBSY;
769fa9e4066Sahrens 		goto die;
770fa9e4066Sahrens 	}
771fa9e4066Sahrens 
772fa9e4066Sahrens 	/* new snapshot name must not exist */
773fa9e4066Sahrens 	snapname = strrchr(drrb->drr_toname, '@');
774fa9e4066Sahrens 	if (snapname == NULL) {
775fa9e4066Sahrens 		err = EEXIST;
776fa9e4066Sahrens 		goto die;
777fa9e4066Sahrens 	}
778fa9e4066Sahrens 	snapname++;
779fa9e4066Sahrens 	err = zap_lookup(dd->dd_pool->dp_meta_objset,
780fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, snapname, 8, 1, &val);
781fa9e4066Sahrens 	if (err != ENOENT) {
782fa9e4066Sahrens 		if (err == 0)
783fa9e4066Sahrens 			err = EEXIST;
784fa9e4066Sahrens 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
785fa9e4066Sahrens 		dsl_dataset_close(ds_prev, DS_MODE_STANDARD, FTAG);
786fa9e4066Sahrens 		return (err);
787fa9e4066Sahrens 	}
788fa9e4066Sahrens 
789fa9e4066Sahrens 	dsl_dataset_close(ds_prev, DS_MODE_STANDARD, FTAG);
790fa9e4066Sahrens 
791fa9e4066Sahrens 	/* The point of no (unsuccessful) return. */
792fa9e4066Sahrens 
793fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
794fa9e4066Sahrens 	ds->ds_phys->ds_restoring = TRUE;
795fa9e4066Sahrens 
796fa9e4066Sahrens 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
797fa9e4066Sahrens 	return (0);
798fa9e4066Sahrens 
799fa9e4066Sahrens die:
800fa9e4066Sahrens 	if (ds_prev)
801fa9e4066Sahrens 		dsl_dataset_close(ds_prev, DS_MODE_STANDARD, FTAG);
802fa9e4066Sahrens 	if (ds)
803fa9e4066Sahrens 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
804fa9e4066Sahrens 	return (err);
805fa9e4066Sahrens }
806fa9e4066Sahrens 
807fa9e4066Sahrens static int
808fa9e4066Sahrens replay_full_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
809fa9e4066Sahrens {
810fa9e4066Sahrens 	struct drr_begin *drrb = arg;
811fa9e4066Sahrens 	int err;
812fa9e4066Sahrens 	char *fsfullname, *fslastname, *cp;
813fa9e4066Sahrens 	dsl_dataset_t *ds;
814fa9e4066Sahrens 
815fa9e4066Sahrens 	fsfullname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
816fa9e4066Sahrens 	(void) strncpy(fsfullname, drrb->drr_toname, MAXNAMELEN);
817fa9e4066Sahrens 	cp = strchr(fsfullname, '@');
818fa9e4066Sahrens 	if (cp == NULL) {
819fa9e4066Sahrens 		kmem_free(fsfullname, MAXNAMELEN);
820fa9e4066Sahrens 		return (EINVAL);
821fa9e4066Sahrens 	}
822fa9e4066Sahrens 	*cp = '\0';
823fa9e4066Sahrens 	fslastname = strrchr(fsfullname, '/');
824fa9e4066Sahrens 	if (fslastname == NULL) {
825fa9e4066Sahrens 		kmem_free(fsfullname, MAXNAMELEN);
826fa9e4066Sahrens 		return (EINVAL);
827fa9e4066Sahrens 	}
828fa9e4066Sahrens 	fslastname++;
829fa9e4066Sahrens 
830fa9e4066Sahrens 	err = dsl_dataset_create_sync(dd, fsfullname, fslastname, NULL, tx);
831fa9e4066Sahrens 	if (err) {
832fa9e4066Sahrens 		kmem_free(fsfullname, MAXNAMELEN);
833fa9e4066Sahrens 		return (err);
834fa9e4066Sahrens 	}
835fa9e4066Sahrens 
836fa9e4066Sahrens 	/* the point of no (unsuccessful) return */
837fa9e4066Sahrens 
838*ea8dc4b6Seschrock 	VERIFY(0 == dsl_dataset_open_spa(dd->dd_pool->dp_spa, fsfullname,
839*ea8dc4b6Seschrock 	    DS_MODE_EXCLUSIVE, FTAG, &ds));
840fa9e4066Sahrens 	kmem_free(fsfullname, MAXNAMELEN);
841fa9e4066Sahrens 
842fa9e4066Sahrens 	(void) dmu_objset_create_impl(dsl_dataset_get_spa(ds),
843fa9e4066Sahrens 	    ds, drrb->drr_type, tx);
844fa9e4066Sahrens 
845fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
846fa9e4066Sahrens 	ds->ds_phys->ds_restoring = TRUE;
847fa9e4066Sahrens 
848fa9e4066Sahrens 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
849fa9e4066Sahrens 	return (0);
850fa9e4066Sahrens }
851fa9e4066Sahrens 
852fa9e4066Sahrens static int
853fa9e4066Sahrens replay_end_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
854fa9e4066Sahrens {
855fa9e4066Sahrens 	struct drr_begin *drrb = arg;
856fa9e4066Sahrens 	int err;
857fa9e4066Sahrens 	char *snapname;
858fa9e4066Sahrens 	dsl_dataset_t *ds;
859fa9e4066Sahrens 
860fa9e4066Sahrens 	/* XXX verify that drr_toname is in dd */
861fa9e4066Sahrens 
862fa9e4066Sahrens 	snapname = strchr(drrb->drr_toname, '@');
863fa9e4066Sahrens 	if (snapname == NULL)
864fa9e4066Sahrens 		return (EINVAL);
865fa9e4066Sahrens 	snapname++;
866fa9e4066Sahrens 
867fa9e4066Sahrens 	/* create snapshot */
868fa9e4066Sahrens 	err = dsl_dataset_snapshot_sync(dd, snapname, tx);
869fa9e4066Sahrens 	if (err)
870fa9e4066Sahrens 		return (err);
871fa9e4066Sahrens 
872fa9e4066Sahrens 	/* set snapshot's creation time and guid */
873*ea8dc4b6Seschrock 	VERIFY(0 == dsl_dataset_open_spa(dd->dd_pool->dp_spa, drrb->drr_toname,
874*ea8dc4b6Seschrock 	    DS_MODE_PRIMARY | DS_MODE_READONLY | DS_MODE_RESTORE, FTAG, &ds));
875fa9e4066Sahrens 
876fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
877fa9e4066Sahrens 	ds->ds_phys->ds_creation_time = drrb->drr_creation_time;
878fa9e4066Sahrens 	ds->ds_phys->ds_guid = drrb->drr_toguid;
879fa9e4066Sahrens 	ds->ds_phys->ds_restoring = FALSE;
880fa9e4066Sahrens 
881fa9e4066Sahrens 	dsl_dataset_close(ds, DS_MODE_PRIMARY, FTAG);
882fa9e4066Sahrens 
883*ea8dc4b6Seschrock 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
884*ea8dc4b6Seschrock 	    dd->dd_phys->dd_head_dataset_obj,
885*ea8dc4b6Seschrock 	    NULL, DS_MODE_STANDARD | DS_MODE_RESTORE, FTAG, &ds));
886fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
887fa9e4066Sahrens 	ds->ds_phys->ds_restoring = FALSE;
888fa9e4066Sahrens 	dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
889fa9e4066Sahrens 
890fa9e4066Sahrens 	return (0);
891fa9e4066Sahrens }
892fa9e4066Sahrens 
893fa9e4066Sahrens void *
894fa9e4066Sahrens restore_read(struct restorearg *ra, int len)
895fa9e4066Sahrens {
896fa9e4066Sahrens 	void *rv;
897fa9e4066Sahrens 
898fa9e4066Sahrens 	/* some things will require 8-byte alignment, so everything must */
899fa9e4066Sahrens 	ASSERT3U(len % 8, ==, 0);
900fa9e4066Sahrens 
901fa9e4066Sahrens 	while (ra->buflen - ra->bufoff < len) {
902fa9e4066Sahrens 		ssize_t resid;
903fa9e4066Sahrens 		int leftover = ra->buflen - ra->bufoff;
904fa9e4066Sahrens 
905fa9e4066Sahrens 		(void) memmove(ra->buf, ra->buf + ra->bufoff, leftover);
906fa9e4066Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
907fa9e4066Sahrens 		    (caddr_t)ra->buf + leftover, ra->bufsize - leftover,
908fa9e4066Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
909fa9e4066Sahrens 		    RLIM_INFINITY, CRED(), &resid);
910fa9e4066Sahrens 
911fa9e4066Sahrens 		ra->voff += ra->bufsize - leftover - resid;
912fa9e4066Sahrens 		ra->buflen = ra->bufsize - resid;
913fa9e4066Sahrens 		ra->bufoff = 0;
914fa9e4066Sahrens 		if (resid == ra->bufsize - leftover)
915fa9e4066Sahrens 			ra->err = EINVAL;
916fa9e4066Sahrens 		if (ra->err)
917fa9e4066Sahrens 			return (NULL);
918*ea8dc4b6Seschrock 		/* Could compute checksum here? */
919fa9e4066Sahrens 	}
920fa9e4066Sahrens 
921fa9e4066Sahrens 	ASSERT3U(ra->bufoff % 8, ==, 0);
922fa9e4066Sahrens 	ASSERT3U(ra->buflen - ra->bufoff, >=, len);
923fa9e4066Sahrens 	rv = ra->buf + ra->bufoff;
924fa9e4066Sahrens 	ra->bufoff += len;
925*ea8dc4b6Seschrock 	if (ra->byteswap)
926*ea8dc4b6Seschrock 		fletcher_4_incremental_byteswap(rv, len, &ra->zc);
927*ea8dc4b6Seschrock 	else
928*ea8dc4b6Seschrock 		fletcher_4_incremental_native(rv, len, &ra->zc);
929fa9e4066Sahrens 	return (rv);
930fa9e4066Sahrens }
931fa9e4066Sahrens 
932fa9e4066Sahrens static void
933fa9e4066Sahrens backup_byteswap(dmu_replay_record_t *drr)
934fa9e4066Sahrens {
935fa9e4066Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
936fa9e4066Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
937fa9e4066Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
938fa9e4066Sahrens 	switch (drr->drr_type) {
939fa9e4066Sahrens 	case DRR_BEGIN:
940fa9e4066Sahrens 		DO64(drr_begin.drr_magic);
941fa9e4066Sahrens 		DO64(drr_begin.drr_version);
942fa9e4066Sahrens 		DO64(drr_begin.drr_creation_time);
943fa9e4066Sahrens 		DO32(drr_begin.drr_type);
944fa9e4066Sahrens 		DO64(drr_begin.drr_toguid);
945fa9e4066Sahrens 		DO64(drr_begin.drr_fromguid);
946fa9e4066Sahrens 		break;
947fa9e4066Sahrens 	case DRR_OBJECT:
948fa9e4066Sahrens 		DO64(drr_object.drr_object);
949fa9e4066Sahrens 		/* DO64(drr_object.drr_allocation_txg); */
950fa9e4066Sahrens 		DO32(drr_object.drr_type);
951fa9e4066Sahrens 		DO32(drr_object.drr_bonustype);
952fa9e4066Sahrens 		DO32(drr_object.drr_blksz);
953fa9e4066Sahrens 		DO32(drr_object.drr_bonuslen);
954fa9e4066Sahrens 		break;
955fa9e4066Sahrens 	case DRR_FREEOBJECTS:
956fa9e4066Sahrens 		DO64(drr_freeobjects.drr_firstobj);
957fa9e4066Sahrens 		DO64(drr_freeobjects.drr_numobjs);
958fa9e4066Sahrens 		break;
959fa9e4066Sahrens 	case DRR_WRITE:
960fa9e4066Sahrens 		DO64(drr_write.drr_object);
961fa9e4066Sahrens 		DO32(drr_write.drr_type);
962fa9e4066Sahrens 		DO64(drr_write.drr_offset);
963fa9e4066Sahrens 		DO64(drr_write.drr_length);
964fa9e4066Sahrens 		break;
965fa9e4066Sahrens 	case DRR_FREE:
966fa9e4066Sahrens 		DO64(drr_free.drr_object);
967fa9e4066Sahrens 		DO64(drr_free.drr_offset);
968fa9e4066Sahrens 		DO64(drr_free.drr_length);
969fa9e4066Sahrens 		break;
970fa9e4066Sahrens 	case DRR_END:
971*ea8dc4b6Seschrock 		DO64(drr_end.drr_checksum.zc_word[0]);
972*ea8dc4b6Seschrock 		DO64(drr_end.drr_checksum.zc_word[1]);
973*ea8dc4b6Seschrock 		DO64(drr_end.drr_checksum.zc_word[2]);
974*ea8dc4b6Seschrock 		DO64(drr_end.drr_checksum.zc_word[3]);
975fa9e4066Sahrens 		break;
976fa9e4066Sahrens 	}
977fa9e4066Sahrens #undef DO64
978fa9e4066Sahrens #undef DO32
979fa9e4066Sahrens }
980fa9e4066Sahrens 
981fa9e4066Sahrens static int
982fa9e4066Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
983fa9e4066Sahrens {
984fa9e4066Sahrens 	int err;
985fa9e4066Sahrens 	dmu_tx_t *tx;
986fa9e4066Sahrens 
987fa9e4066Sahrens 	err = dmu_object_info(os, drro->drr_object, NULL);
988fa9e4066Sahrens 
989fa9e4066Sahrens 	if (err != 0 && err != ENOENT)
990fa9e4066Sahrens 		return (EINVAL);
991fa9e4066Sahrens 
992fa9e4066Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
993fa9e4066Sahrens 	    drro->drr_type >= DMU_OT_NUMTYPES ||
994fa9e4066Sahrens 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
995fa9e4066Sahrens 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
996fa9e4066Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
997fa9e4066Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
998fa9e4066Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
999fa9e4066Sahrens 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
1000fa9e4066Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1001fa9e4066Sahrens 		return (EINVAL);
1002fa9e4066Sahrens 	}
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	tx = dmu_tx_create(os);
1005fa9e4066Sahrens 
1006fa9e4066Sahrens 	if (err == ENOENT) {
1007fa9e4066Sahrens 		/* currently free, want to be allocated */
1008fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1009fa9e4066Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1);
1010fa9e4066Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
1011fa9e4066Sahrens 		if (err) {
1012fa9e4066Sahrens 			dmu_tx_abort(tx);
1013fa9e4066Sahrens 			return (err);
1014fa9e4066Sahrens 		}
1015fa9e4066Sahrens 		err = dmu_object_claim(os, drro->drr_object,
1016fa9e4066Sahrens 		    drro->drr_type, drro->drr_blksz,
1017fa9e4066Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1018fa9e4066Sahrens 	} else {
1019fa9e4066Sahrens 		/* currently allocated, want to be allocated */
1020fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, drro->drr_object);
1021fa9e4066Sahrens 		/*
1022fa9e4066Sahrens 		 * We may change blocksize, so need to
1023fa9e4066Sahrens 		 * hold_write
1024fa9e4066Sahrens 		 */
1025fa9e4066Sahrens 		dmu_tx_hold_write(tx, drro->drr_object, 0, 1);
1026fa9e4066Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
1027fa9e4066Sahrens 		if (err) {
1028fa9e4066Sahrens 			dmu_tx_abort(tx);
1029fa9e4066Sahrens 			return (err);
1030fa9e4066Sahrens 		}
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 		err = dmu_object_reclaim(os, drro->drr_object,
1033fa9e4066Sahrens 		    drro->drr_type, drro->drr_blksz,
1034fa9e4066Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1035fa9e4066Sahrens 	}
1036fa9e4066Sahrens 	if (err) {
1037fa9e4066Sahrens 		dmu_tx_commit(tx);
1038fa9e4066Sahrens 		return (EINVAL);
1039fa9e4066Sahrens 	}
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
1042fa9e4066Sahrens 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	if (drro->drr_bonuslen) {
1045fa9e4066Sahrens 		dmu_buf_t *db;
1046fa9e4066Sahrens 		void *data;
1047*ea8dc4b6Seschrock 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
1048fa9e4066Sahrens 		dmu_buf_will_dirty(db, tx);
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 		ASSERT3U(db->db_size, ==, drro->drr_bonuslen);
1051fa9e4066Sahrens 		data = restore_read(ra, P2ROUNDUP(db->db_size, 8));
1052fa9e4066Sahrens 		if (data == NULL) {
1053fa9e4066Sahrens 			dmu_tx_commit(tx);
1054fa9e4066Sahrens 			return (ra->err);
1055fa9e4066Sahrens 		}
1056fa9e4066Sahrens 		bcopy(data, db->db_data, db->db_size);
1057fa9e4066Sahrens 		if (ra->byteswap) {
1058fa9e4066Sahrens 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
1059fa9e4066Sahrens 			    drro->drr_bonuslen);
1060fa9e4066Sahrens 		}
1061*ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1062fa9e4066Sahrens 	}
1063fa9e4066Sahrens 	dmu_tx_commit(tx);
1064fa9e4066Sahrens 	return (0);
1065fa9e4066Sahrens }
1066fa9e4066Sahrens 
1067fa9e4066Sahrens /* ARGSUSED */
1068fa9e4066Sahrens static int
1069fa9e4066Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
1070fa9e4066Sahrens     struct drr_freeobjects *drrfo)
1071fa9e4066Sahrens {
1072fa9e4066Sahrens 	uint64_t obj;
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1075fa9e4066Sahrens 		return (EINVAL);
1076fa9e4066Sahrens 
1077fa9e4066Sahrens 	for (obj = drrfo->drr_firstobj;
1078fa9e4066Sahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs; obj++) {
1079fa9e4066Sahrens 		dmu_tx_t *tx;
1080fa9e4066Sahrens 		int err;
1081fa9e4066Sahrens 
1082fa9e4066Sahrens 		if (dmu_object_info(os, obj, NULL) != 0)
1083fa9e4066Sahrens 			continue;
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 		tx = dmu_tx_create(os);
1086fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, obj);
1087fa9e4066Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
1088fa9e4066Sahrens 		if (err) {
1089fa9e4066Sahrens 			dmu_tx_abort(tx);
1090fa9e4066Sahrens 			return (err);
1091fa9e4066Sahrens 		}
1092fa9e4066Sahrens 		err = dmu_object_free(os, obj, tx);
1093fa9e4066Sahrens 		dmu_tx_commit(tx);
1094fa9e4066Sahrens 		if (err && err != ENOENT)
1095fa9e4066Sahrens 			return (EINVAL);
1096fa9e4066Sahrens 	}
1097fa9e4066Sahrens 	return (0);
1098fa9e4066Sahrens }
1099fa9e4066Sahrens 
1100fa9e4066Sahrens static int
1101fa9e4066Sahrens restore_write(struct restorearg *ra, objset_t *os,
1102fa9e4066Sahrens     struct drr_write *drrw)
1103fa9e4066Sahrens {
1104fa9e4066Sahrens 	dmu_tx_t *tx;
1105fa9e4066Sahrens 	void *data;
1106fa9e4066Sahrens 	int err;
1107fa9e4066Sahrens 
1108fa9e4066Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
1109fa9e4066Sahrens 	    drrw->drr_type >= DMU_OT_NUMTYPES)
1110fa9e4066Sahrens 		return (EINVAL);
1111fa9e4066Sahrens 
1112fa9e4066Sahrens 	data = restore_read(ra, drrw->drr_length);
1113fa9e4066Sahrens 	if (data == NULL)
1114fa9e4066Sahrens 		return (ra->err);
1115fa9e4066Sahrens 
1116fa9e4066Sahrens 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
1117fa9e4066Sahrens 		return (EINVAL);
1118fa9e4066Sahrens 
1119fa9e4066Sahrens 	tx = dmu_tx_create(os);
1120fa9e4066Sahrens 
1121fa9e4066Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
1122fa9e4066Sahrens 	    drrw->drr_offset, drrw->drr_length);
1123fa9e4066Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
1124fa9e4066Sahrens 	if (err) {
1125fa9e4066Sahrens 		dmu_tx_abort(tx);
1126fa9e4066Sahrens 		return (err);
1127fa9e4066Sahrens 	}
1128fa9e4066Sahrens 	if (ra->byteswap)
1129fa9e4066Sahrens 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
1130fa9e4066Sahrens 	dmu_write(os, drrw->drr_object,
1131fa9e4066Sahrens 	    drrw->drr_offset, drrw->drr_length, data, tx);
1132fa9e4066Sahrens 	dmu_tx_commit(tx);
1133fa9e4066Sahrens 	return (0);
1134fa9e4066Sahrens }
1135fa9e4066Sahrens 
1136fa9e4066Sahrens /* ARGSUSED */
1137fa9e4066Sahrens static int
1138fa9e4066Sahrens restore_free(struct restorearg *ra, objset_t *os,
1139fa9e4066Sahrens     struct drr_free *drrf)
1140fa9e4066Sahrens {
1141fa9e4066Sahrens 	dmu_tx_t *tx;
1142fa9e4066Sahrens 	int err;
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 	if (drrf->drr_length != -1ULL &&
1145fa9e4066Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1146fa9e4066Sahrens 		return (EINVAL);
1147fa9e4066Sahrens 
1148fa9e4066Sahrens 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
1149fa9e4066Sahrens 		return (EINVAL);
1150fa9e4066Sahrens 
1151fa9e4066Sahrens 	tx = dmu_tx_create(os);
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 	dmu_tx_hold_free(tx, drrf->drr_object,
1154fa9e4066Sahrens 	    drrf->drr_offset, drrf->drr_length);
1155fa9e4066Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
1156fa9e4066Sahrens 	if (err) {
1157fa9e4066Sahrens 		dmu_tx_abort(tx);
1158fa9e4066Sahrens 		return (err);
1159fa9e4066Sahrens 	}
1160*ea8dc4b6Seschrock 	err = dmu_free_range(os, drrf->drr_object,
1161fa9e4066Sahrens 	    drrf->drr_offset, drrf->drr_length, tx);
1162fa9e4066Sahrens 	dmu_tx_commit(tx);
1163*ea8dc4b6Seschrock 	return (err);
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
1166fa9e4066Sahrens int
1167*ea8dc4b6Seschrock dmu_recvbackup(char *tosnap, struct drr_begin *drrb, uint64_t *sizep,
1168fa9e4066Sahrens     vnode_t *vp, uint64_t voffset)
1169fa9e4066Sahrens {
1170fa9e4066Sahrens 	struct restorearg ra;
1171fa9e4066Sahrens 	dmu_replay_record_t *drr;
1172*ea8dc4b6Seschrock 	char *cp;
1173fa9e4066Sahrens 	dsl_dir_t *dd = NULL;
1174fa9e4066Sahrens 	objset_t *os = NULL;
1175*ea8dc4b6Seschrock 	zio_cksum_t pzc;
1176fa9e4066Sahrens 
1177fa9e4066Sahrens 	bzero(&ra, sizeof (ra));
1178fa9e4066Sahrens 	ra.vp = vp;
1179fa9e4066Sahrens 	ra.voff = voffset;
1180fa9e4066Sahrens 	ra.bufsize = 1<<20;
1181fa9e4066Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1182fa9e4066Sahrens 
1183fa9e4066Sahrens 	if (drrb->drr_magic == DMU_BACKUP_MAGIC) {
1184fa9e4066Sahrens 		ra.byteswap = FALSE;
1185fa9e4066Sahrens 	} else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1186fa9e4066Sahrens 		ra.byteswap = TRUE;
1187fa9e4066Sahrens 	} else {
1188fa9e4066Sahrens 		ra.err = EINVAL;
1189fa9e4066Sahrens 		goto out;
1190fa9e4066Sahrens 	}
1191fa9e4066Sahrens 
1192*ea8dc4b6Seschrock 	/*
1193*ea8dc4b6Seschrock 	 * NB: this assumes that struct drr_begin will be the largest in
1194*ea8dc4b6Seschrock 	 * dmu_replay_record_t's drr_u, and thus we don't need to pad it
1195*ea8dc4b6Seschrock 	 * with zeros to make it the same length as we wrote out.
1196*ea8dc4b6Seschrock 	 */
1197*ea8dc4b6Seschrock 	((dmu_replay_record_t *)ra.buf)->drr_type = DRR_BEGIN;
1198*ea8dc4b6Seschrock 	((dmu_replay_record_t *)ra.buf)->drr_pad = 0;
1199*ea8dc4b6Seschrock 	((dmu_replay_record_t *)ra.buf)->drr_u.drr_begin = *drrb;
1200*ea8dc4b6Seschrock 	if (ra.byteswap) {
1201*ea8dc4b6Seschrock 		fletcher_4_incremental_byteswap(ra.buf,
1202*ea8dc4b6Seschrock 		    sizeof (dmu_replay_record_t), &ra.zc);
1203*ea8dc4b6Seschrock 	} else {
1204*ea8dc4b6Seschrock 		fletcher_4_incremental_native(ra.buf,
1205*ea8dc4b6Seschrock 		    sizeof (dmu_replay_record_t), &ra.zc);
1206*ea8dc4b6Seschrock 	}
1207*ea8dc4b6Seschrock 	(void) strcpy(drrb->drr_toname, tosnap); /* for the sync funcs */
1208*ea8dc4b6Seschrock 
1209fa9e4066Sahrens 	if (ra.byteswap) {
1210fa9e4066Sahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
1211fa9e4066Sahrens 		drrb->drr_version = BSWAP_64(drrb->drr_version);
1212fa9e4066Sahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
1213fa9e4066Sahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
1214fa9e4066Sahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
1215fa9e4066Sahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
1216fa9e4066Sahrens 	}
1217fa9e4066Sahrens 
1218fa9e4066Sahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1219fa9e4066Sahrens 
1220fa9e4066Sahrens 	if (drrb->drr_version != DMU_BACKUP_VERSION ||
1221fa9e4066Sahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
1222fa9e4066Sahrens 	    strchr(drrb->drr_toname, '@') == NULL) {
1223fa9e4066Sahrens 		ra.err = EINVAL;
1224fa9e4066Sahrens 		goto out;
1225fa9e4066Sahrens 	}
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	/*
1228fa9e4066Sahrens 	 * Process the begin in syncing context.
1229fa9e4066Sahrens 	 */
1230fa9e4066Sahrens 	if (drrb->drr_fromguid) {
1231fa9e4066Sahrens 		/* incremental backup */
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 		cp = strchr(tosnap, '@');
1234fa9e4066Sahrens 		*cp = '\0';
1235*ea8dc4b6Seschrock 		ra.err = dsl_dir_open(tosnap, FTAG, &dd, NULL);
1236fa9e4066Sahrens 		*cp = '@';
1237*ea8dc4b6Seschrock 		if (ra.err)
1238fa9e4066Sahrens 			goto out;
1239fa9e4066Sahrens 
1240fa9e4066Sahrens 		ra.err = dsl_dir_sync_task(dd, replay_incremental_sync,
1241fa9e4066Sahrens 		    drrb, 1<<20);
1242fa9e4066Sahrens 	} else {
1243fa9e4066Sahrens 		/* full backup */
1244fa9e4066Sahrens 		const char *tail;
1245fa9e4066Sahrens 
1246fa9e4066Sahrens 		cp = strchr(tosnap, '@');
1247fa9e4066Sahrens 		*cp = '\0';
1248*ea8dc4b6Seschrock 		ra.err = dsl_dir_open(tosnap, FTAG, &dd, &tail);
1249fa9e4066Sahrens 		*cp = '@';
1250*ea8dc4b6Seschrock 		if (ra.err)
1251fa9e4066Sahrens 			goto out;
1252fa9e4066Sahrens 		if (tail == NULL) {
1253fa9e4066Sahrens 			ra.err = EEXIST;
1254fa9e4066Sahrens 			goto out;
1255fa9e4066Sahrens 		}
1256fa9e4066Sahrens 
1257fa9e4066Sahrens 		ra.err = dsl_dir_sync_task(dd, replay_full_sync,
1258fa9e4066Sahrens 		    drrb, 1<<20);
1259fa9e4066Sahrens 	}
1260fa9e4066Sahrens 	if (ra.err)
1261fa9e4066Sahrens 		goto out;
1262fa9e4066Sahrens 
1263fa9e4066Sahrens 	/*
1264fa9e4066Sahrens 	 * Open the objset we are modifying.
1265fa9e4066Sahrens 	 */
1266fa9e4066Sahrens 
1267fa9e4066Sahrens 	cp = strchr(tosnap, '@');
1268fa9e4066Sahrens 	*cp = '\0';
1269fa9e4066Sahrens 	ra.err = dmu_objset_open(tosnap, DMU_OST_ANY,
1270fa9e4066Sahrens 	    DS_MODE_PRIMARY | DS_MODE_RESTORE, &os);
1271fa9e4066Sahrens 	*cp = '@';
1272fa9e4066Sahrens 	ASSERT3U(ra.err, ==, 0);
1273fa9e4066Sahrens 
1274fa9e4066Sahrens 	/*
1275fa9e4066Sahrens 	 * Read records and process them.
1276fa9e4066Sahrens 	 */
1277*ea8dc4b6Seschrock 	pzc = ra.zc;
1278fa9e4066Sahrens 	while (ra.err == 0 &&
1279fa9e4066Sahrens 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
1280*ea8dc4b6Seschrock 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1281fa9e4066Sahrens 			ra.err = EINTR;
1282fa9e4066Sahrens 			goto out;
1283fa9e4066Sahrens 		}
1284fa9e4066Sahrens 
1285fa9e4066Sahrens 		if (ra.byteswap)
1286fa9e4066Sahrens 			backup_byteswap(drr);
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 		switch (drr->drr_type) {
1289fa9e4066Sahrens 		case DRR_OBJECT:
1290fa9e4066Sahrens 		{
1291fa9e4066Sahrens 			/*
1292fa9e4066Sahrens 			 * We need to make a copy of the record header,
1293fa9e4066Sahrens 			 * because restore_{object,write} may need to
1294fa9e4066Sahrens 			 * restore_read(), which will invalidate drr.
1295fa9e4066Sahrens 			 */
1296fa9e4066Sahrens 			struct drr_object drro = drr->drr_u.drr_object;
1297fa9e4066Sahrens 			ra.err = restore_object(&ra, os, &drro);
1298fa9e4066Sahrens 			break;
1299fa9e4066Sahrens 		}
1300fa9e4066Sahrens 		case DRR_FREEOBJECTS:
1301fa9e4066Sahrens 		{
1302fa9e4066Sahrens 			struct drr_freeobjects drrfo =
1303fa9e4066Sahrens 			    drr->drr_u.drr_freeobjects;
1304fa9e4066Sahrens 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1305fa9e4066Sahrens 			break;
1306fa9e4066Sahrens 		}
1307fa9e4066Sahrens 		case DRR_WRITE:
1308fa9e4066Sahrens 		{
1309fa9e4066Sahrens 			struct drr_write drrw = drr->drr_u.drr_write;
1310fa9e4066Sahrens 			ra.err = restore_write(&ra, os, &drrw);
1311fa9e4066Sahrens 			break;
1312fa9e4066Sahrens 		}
1313fa9e4066Sahrens 		case DRR_FREE:
1314fa9e4066Sahrens 		{
1315fa9e4066Sahrens 			struct drr_free drrf = drr->drr_u.drr_free;
1316fa9e4066Sahrens 			ra.err = restore_free(&ra, os, &drrf);
1317fa9e4066Sahrens 			break;
1318fa9e4066Sahrens 		}
1319fa9e4066Sahrens 		case DRR_END:
1320*ea8dc4b6Seschrock 		{
1321*ea8dc4b6Seschrock 			struct drr_end drre = drr->drr_u.drr_end;
1322*ea8dc4b6Seschrock 			/*
1323*ea8dc4b6Seschrock 			 * We compare against the *previous* checksum
1324*ea8dc4b6Seschrock 			 * value, because the stored checksum is of
1325*ea8dc4b6Seschrock 			 * everything before the DRR_END record.
1326*ea8dc4b6Seschrock 			 */
1327*ea8dc4b6Seschrock 			if (drre.drr_checksum.zc_word[0] != 0 &&
1328*ea8dc4b6Seschrock 			    ((drre.drr_checksum.zc_word[0] - pzc.zc_word[0]) |
1329*ea8dc4b6Seschrock 			    (drre.drr_checksum.zc_word[1] - pzc.zc_word[1]) |
1330*ea8dc4b6Seschrock 			    (drre.drr_checksum.zc_word[2] - pzc.zc_word[2]) |
1331*ea8dc4b6Seschrock 			    (drre.drr_checksum.zc_word[3] - pzc.zc_word[3]))) {
1332*ea8dc4b6Seschrock 				ra.err = ECKSUM;
1333*ea8dc4b6Seschrock 				goto out;
1334*ea8dc4b6Seschrock 			}
1335*ea8dc4b6Seschrock 
1336fa9e4066Sahrens 			/*
1337fa9e4066Sahrens 			 * dd may be the parent of the dd we are
1338fa9e4066Sahrens 			 * restoring into (eg. if it's a full backup).
1339fa9e4066Sahrens 			 */
1340fa9e4066Sahrens 			ra.err = dsl_dir_sync_task(dmu_objset_ds(os)->
1341fa9e4066Sahrens 			    ds_dir, replay_end_sync, drrb, 1<<20);
1342fa9e4066Sahrens 			goto out;
1343*ea8dc4b6Seschrock 		}
1344fa9e4066Sahrens 		default:
1345fa9e4066Sahrens 			ra.err = EINVAL;
1346fa9e4066Sahrens 			goto out;
1347fa9e4066Sahrens 		}
1348*ea8dc4b6Seschrock 		pzc = ra.zc;
1349fa9e4066Sahrens 	}
1350fa9e4066Sahrens 
1351fa9e4066Sahrens out:
1352fa9e4066Sahrens 	if (os)
1353fa9e4066Sahrens 		dmu_objset_close(os);
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	/*
1356fa9e4066Sahrens 	 * Make sure we don't rollback/destroy unless we actually
1357fa9e4066Sahrens 	 * processed the begin properly.  'os' will only be set if this
1358fa9e4066Sahrens 	 * is the case.
1359fa9e4066Sahrens 	 */
1360fa9e4066Sahrens 	if (ra.err && os && dd && tosnap && strchr(tosnap, '@')) {
1361fa9e4066Sahrens 		/*
1362fa9e4066Sahrens 		 * rollback or destroy what we created, so we don't
1363fa9e4066Sahrens 		 * leave it in the restoring state.
1364fa9e4066Sahrens 		 */
1365fa9e4066Sahrens 		txg_wait_synced(dd->dd_pool, 0);
1366fa9e4066Sahrens 		if (drrb->drr_fromguid) {
1367fa9e4066Sahrens 			/* incremental: rollback to most recent snapshot */
1368fa9e4066Sahrens 			(void) dsl_dir_sync_task(dd,
1369fa9e4066Sahrens 			    dsl_dataset_rollback_sync, NULL, 0);
1370fa9e4066Sahrens 		} else {
1371fa9e4066Sahrens 			/* full: destroy whole fs */
1372fa9e4066Sahrens 			cp = strchr(tosnap, '@');
1373fa9e4066Sahrens 			*cp = '\0';
1374fa9e4066Sahrens 			cp = strchr(tosnap, '/');
1375fa9e4066Sahrens 			if (cp) {
1376fa9e4066Sahrens 				(void) dsl_dir_sync_task(dd,
1377fa9e4066Sahrens 				    dsl_dir_destroy_sync, cp+1, 0);
1378fa9e4066Sahrens 			}
1379fa9e4066Sahrens 			cp = strchr(tosnap, '\0');
1380fa9e4066Sahrens 			*cp = '@';
1381fa9e4066Sahrens 		}
1382fa9e4066Sahrens 
1383fa9e4066Sahrens 	}
1384fa9e4066Sahrens 
1385fa9e4066Sahrens 	if (dd)
1386fa9e4066Sahrens 		dsl_dir_close(dd, FTAG);
1387fa9e4066Sahrens 	kmem_free(ra.buf, ra.bufsize);
1388fa9e4066Sahrens 	if (sizep)
1389fa9e4066Sahrens 		*sizep = ra.voff;
1390fa9e4066Sahrens 	return (ra.err);
1391fa9e4066Sahrens }
1392fa9e4066Sahrens 
1393fa9e4066Sahrens /*
1394fa9e4066Sahrens  * Intent log support: sync the block at <os, object, offset> to disk.
1395fa9e4066Sahrens  * N.B. and XXX: the caller is responsible for serializing dmu_sync()s
1396fa9e4066Sahrens  * of the same block, and for making sure that the data isn't changing
1397fa9e4066Sahrens  * while dmu_sync() is writing it.
1398fa9e4066Sahrens  *
1399fa9e4066Sahrens  * Return values:
1400fa9e4066Sahrens  *
1401fa9e4066Sahrens  *	EALREADY: this txg has already been synced, so there's nothing to to.
1402fa9e4066Sahrens  *		The caller should not log the write.
1403fa9e4066Sahrens  *
1404fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1405fa9e4066Sahrens  *		The caller should not log the write.
1406fa9e4066Sahrens  *
1407fa9e4066Sahrens  *	EINPROGRESS: the block is in the process of being synced by the
1408fa9e4066Sahrens  *		usual mechanism (spa_sync()), so we can't sync it here.
1409fa9e4066Sahrens  *		The caller should txg_wait_synced() and not log the write.
1410fa9e4066Sahrens  *
1411fa9e4066Sahrens  *	EBUSY: another thread is trying to dmu_sync() the same dbuf.
1412fa9e4066Sahrens  *		(This case cannot arise under the current locking rules.)
1413fa9e4066Sahrens  *		The caller should txg_wait_synced() and not log the write.
1414fa9e4066Sahrens  *
1415fa9e4066Sahrens  *	ESTALE: the block was dirtied or freed while we were writing it,
1416fa9e4066Sahrens  *		so the data is no longer valid.
1417fa9e4066Sahrens  *		The caller should txg_wait_synced() and not log the write.
1418fa9e4066Sahrens  *
1419fa9e4066Sahrens  *	0: success.  Sets *bp to the blkptr just written, and sets
1420fa9e4066Sahrens  *		*blkoff to the data's offset within that block.
1421fa9e4066Sahrens  *		The caller should log this blkptr/blkoff in its lr_write_t.
1422fa9e4066Sahrens  */
1423fa9e4066Sahrens int
1424fa9e4066Sahrens dmu_sync(objset_t *os, uint64_t object, uint64_t offset, uint64_t *blkoff,
1425fa9e4066Sahrens     blkptr_t *bp, uint64_t txg)
1426fa9e4066Sahrens {
1427fa9e4066Sahrens 	dsl_pool_t *dp = os->os->os_dsl_dataset->ds_dir->dd_pool;
1428fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
1429fa9e4066Sahrens 	dmu_buf_impl_t *db;
1430fa9e4066Sahrens 	blkptr_t *blk;
1431fa9e4066Sahrens 	int err;
1432*ea8dc4b6Seschrock 	zbookmark_t zb;
1433fa9e4066Sahrens 
1434fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&tx->tx_suspend));
1435fa9e4066Sahrens 	ASSERT(BP_IS_HOLE(bp));
1436fa9e4066Sahrens 	ASSERT(txg != 0);
1437fa9e4066Sahrens 
1438fa9e4066Sahrens 	dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
1439fa9e4066Sahrens 	    txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
1440fa9e4066Sahrens 
1441fa9e4066Sahrens 	/*
1442*ea8dc4b6Seschrock 	 * XXX why is this routine using dmu_buf_*() and casting between
1443*ea8dc4b6Seschrock 	 * dmu_buf_impl_t and dmu_buf_t?
1444*ea8dc4b6Seschrock 	 */
1445*ea8dc4b6Seschrock 
1446*ea8dc4b6Seschrock 	/*
1447fa9e4066Sahrens 	 * If this txg already synced, there's nothing to do.
1448fa9e4066Sahrens 	 */
1449fa9e4066Sahrens 	if (txg <= tx->tx_synced_txg) {
1450fa9e4066Sahrens 		/*
1451fa9e4066Sahrens 		 * If we're running ziltest, we need the blkptr regardless.
1452fa9e4066Sahrens 		 */
1453fa9e4066Sahrens 		if (txg > spa_freeze_txg(dp->dp_spa)) {
1454*ea8dc4b6Seschrock 			err = dmu_buf_hold(os, object, offset,
1455*ea8dc4b6Seschrock 			    FTAG, (dmu_buf_t **)&db);
1456*ea8dc4b6Seschrock 			if (err)
1457*ea8dc4b6Seschrock 				return (err);
1458fa9e4066Sahrens 			/* if db_blkptr == NULL, this was an empty write */
1459fa9e4066Sahrens 			if (db->db_blkptr)
1460fa9e4066Sahrens 				*bp = *db->db_blkptr; /* structure assignment */
1461fa9e4066Sahrens 			else
1462fa9e4066Sahrens 				bzero(bp, sizeof (blkptr_t));
1463fa9e4066Sahrens 			*blkoff = offset - db->db.db_offset;
1464fa9e4066Sahrens 			ASSERT3U(*blkoff, <, db->db.db_size);
1465*ea8dc4b6Seschrock 			dmu_buf_rele((dmu_buf_t *)db, FTAG);
1466fa9e4066Sahrens 			return (0);
1467fa9e4066Sahrens 		}
1468fa9e4066Sahrens 		return (EALREADY);
1469fa9e4066Sahrens 	}
1470fa9e4066Sahrens 
1471fa9e4066Sahrens 	/*
1472fa9e4066Sahrens 	 * If this txg is in the middle of syncing, just wait for it.
1473fa9e4066Sahrens 	 */
1474fa9e4066Sahrens 	if (txg == tx->tx_syncing_txg) {
1475fa9e4066Sahrens 		ASSERT(txg != tx->tx_open_txg);
1476fa9e4066Sahrens 		return (EINPROGRESS);
1477fa9e4066Sahrens 	}
1478fa9e4066Sahrens 
1479*ea8dc4b6Seschrock 	err = dmu_buf_hold(os, object, offset, FTAG, (dmu_buf_t **)&db);
1480*ea8dc4b6Seschrock 	if (err)
1481*ea8dc4b6Seschrock 		return (err);
1482fa9e4066Sahrens 
1483fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
1484fa9e4066Sahrens 
1485fa9e4066Sahrens 	/*
1486fa9e4066Sahrens 	 * If this dbuf isn't dirty, must have been free_range'd.
1487fa9e4066Sahrens 	 * There's no need to log writes to freed blocks, so we're done.
1488fa9e4066Sahrens 	 */
1489fa9e4066Sahrens 	if (!list_link_active(&db->db_dirty_node[txg&TXG_MASK])) {
1490fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1491*ea8dc4b6Seschrock 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1492fa9e4066Sahrens 		return (ENOENT);
1493fa9e4066Sahrens 	}
1494fa9e4066Sahrens 
1495fa9e4066Sahrens 	blk = db->db_d.db_overridden_by[txg&TXG_MASK];
1496fa9e4066Sahrens 
1497fa9e4066Sahrens 	/*
1498fa9e4066Sahrens 	 * If we already did a dmu_sync() of this dbuf in this txg,
1499fa9e4066Sahrens 	 * free the old block before writing the new one.
1500fa9e4066Sahrens 	 */
1501fa9e4066Sahrens 	if (blk != NULL) {
1502fa9e4066Sahrens 		ASSERT(blk != IN_DMU_SYNC);
1503fa9e4066Sahrens 		if (blk == IN_DMU_SYNC) {
1504fa9e4066Sahrens 			mutex_exit(&db->db_mtx);
1505*ea8dc4b6Seschrock 			dmu_buf_rele((dmu_buf_t *)db, FTAG);
1506fa9e4066Sahrens 			return (EBUSY);
1507fa9e4066Sahrens 		}
1508fa9e4066Sahrens 		arc_release(db->db_d.db_data_old[txg&TXG_MASK], db);
1509fa9e4066Sahrens 		if (!BP_IS_HOLE(blk)) {
1510fa9e4066Sahrens 			(void) arc_free(NULL, os->os->os_spa, txg, blk,
1511fa9e4066Sahrens 			    NULL, NULL, ARC_WAIT);
1512fa9e4066Sahrens 		}
1513fa9e4066Sahrens 		kmem_free(blk, sizeof (blkptr_t));
1514fa9e4066Sahrens 	}
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	db->db_d.db_overridden_by[txg&TXG_MASK] = IN_DMU_SYNC;
1517fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1518fa9e4066Sahrens 
1519fa9e4066Sahrens 	blk = kmem_alloc(sizeof (blkptr_t), KM_SLEEP);
1520fa9e4066Sahrens 	blk->blk_birth = 0; /* mark as invalid */
1521fa9e4066Sahrens 
1522*ea8dc4b6Seschrock 	zb.zb_objset = os->os->os_dsl_dataset->ds_object;
1523*ea8dc4b6Seschrock 	zb.zb_object = db->db.db_object;
1524*ea8dc4b6Seschrock 	zb.zb_level = db->db_level;
1525*ea8dc4b6Seschrock 	zb.zb_blkid = db->db_blkid;
1526fa9e4066Sahrens 	err = arc_write(NULL, os->os->os_spa,
1527fa9e4066Sahrens 	    zio_checksum_select(db->db_dnode->dn_checksum, os->os->os_checksum),
1528fa9e4066Sahrens 	    zio_compress_select(db->db_dnode->dn_compress, os->os->os_compress),
1529fa9e4066Sahrens 	    txg, blk, db->db_d.db_data_old[txg&TXG_MASK], NULL, NULL,
1530*ea8dc4b6Seschrock 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, ARC_WAIT, &zb);
1531fa9e4066Sahrens 	ASSERT(err == 0);
1532fa9e4066Sahrens 
1533fa9e4066Sahrens 	if (!BP_IS_HOLE(blk)) {
1534fa9e4066Sahrens 		blk->blk_fill = 1;
1535fa9e4066Sahrens 		BP_SET_TYPE(blk, db->db_dnode->dn_type);
1536fa9e4066Sahrens 		BP_SET_LEVEL(blk, 0);
1537fa9e4066Sahrens 	}
1538fa9e4066Sahrens 
1539fa9e4066Sahrens 	/* copy the block pointer back to caller */
1540fa9e4066Sahrens 	*bp = *blk; /* structure assignment */
1541fa9e4066Sahrens 	*blkoff = offset - db->db.db_offset;
1542fa9e4066Sahrens 	ASSERT3U(*blkoff, <, db->db.db_size);
1543fa9e4066Sahrens 
1544fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
1545fa9e4066Sahrens 	if (db->db_d.db_overridden_by[txg&TXG_MASK] != IN_DMU_SYNC) {
1546fa9e4066Sahrens 		/* we were dirtied/freed during the sync */
1547fa9e4066Sahrens 		ASSERT3P(db->db_d.db_overridden_by[txg&TXG_MASK], ==, NULL);
1548fa9e4066Sahrens 		arc_release(db->db_d.db_data_old[txg&TXG_MASK], db);
1549fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1550*ea8dc4b6Seschrock 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1551fa9e4066Sahrens 		/* Note that this block does not free on disk until txg syncs */
1552fa9e4066Sahrens 
1553fa9e4066Sahrens 		/*
1554fa9e4066Sahrens 		 * XXX can we use ARC_NOWAIT here?
1555fa9e4066Sahrens 		 * XXX should we be ignoring the return code?
1556fa9e4066Sahrens 		 */
1557fa9e4066Sahrens 		if (!BP_IS_HOLE(blk)) {
1558fa9e4066Sahrens 			(void) arc_free(NULL, os->os->os_spa, txg, blk,
1559fa9e4066Sahrens 			    NULL, NULL, ARC_WAIT);
1560fa9e4066Sahrens 		}
1561fa9e4066Sahrens 		kmem_free(blk, sizeof (blkptr_t));
1562fa9e4066Sahrens 		return (ESTALE);
1563fa9e4066Sahrens 	}
1564fa9e4066Sahrens 
1565fa9e4066Sahrens 	db->db_d.db_overridden_by[txg&TXG_MASK] = blk;
1566fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1567*ea8dc4b6Seschrock 	dmu_buf_rele((dmu_buf_t *)db, FTAG);
1568fa9e4066Sahrens 	ASSERT3U(txg, >, tx->tx_syncing_txg);
1569fa9e4066Sahrens 	return (0);
1570fa9e4066Sahrens }
1571fa9e4066Sahrens 
1572fa9e4066Sahrens uint64_t
1573fa9e4066Sahrens dmu_object_max_nonzero_offset(objset_t *os, uint64_t object)
1574fa9e4066Sahrens {
1575*ea8dc4b6Seschrock 	dnode_t *dn;
1576*ea8dc4b6Seschrock 
1577*ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1578*ea8dc4b6Seschrock 	(void) dnode_hold(os->os, object, FTAG, &dn);
1579fa9e4066Sahrens 	uint64_t rv = dnode_max_nonzero_offset(dn);
1580fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1581fa9e4066Sahrens 	return (rv);
1582fa9e4066Sahrens }
1583fa9e4066Sahrens 
1584fa9e4066Sahrens int
1585fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1586fa9e4066Sahrens 	dmu_tx_t *tx)
1587fa9e4066Sahrens {
1588*ea8dc4b6Seschrock 	dnode_t *dn;
1589*ea8dc4b6Seschrock 	int err;
1590*ea8dc4b6Seschrock 
1591*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
1592*ea8dc4b6Seschrock 	if (err)
1593*ea8dc4b6Seschrock 		return (err);
1594*ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1595fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1596fa9e4066Sahrens 	return (err);
1597fa9e4066Sahrens }
1598fa9e4066Sahrens 
1599fa9e4066Sahrens void
1600fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1601fa9e4066Sahrens 	dmu_tx_t *tx)
1602fa9e4066Sahrens {
1603*ea8dc4b6Seschrock 	dnode_t *dn;
1604*ea8dc4b6Seschrock 
1605*ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1606*ea8dc4b6Seschrock 	(void) dnode_hold(os->os, object, FTAG, &dn);
1607fa9e4066Sahrens 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1608fa9e4066Sahrens 	dn->dn_checksum = checksum;
1609fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1610fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1611fa9e4066Sahrens }
1612fa9e4066Sahrens 
1613fa9e4066Sahrens void
1614fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1615fa9e4066Sahrens 	dmu_tx_t *tx)
1616fa9e4066Sahrens {
1617*ea8dc4b6Seschrock 	dnode_t *dn;
1618*ea8dc4b6Seschrock 
1619*ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1620*ea8dc4b6Seschrock 	(void) dnode_hold(os->os, object, FTAG, &dn);
1621fa9e4066Sahrens 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1622fa9e4066Sahrens 	dn->dn_compress = compress;
1623fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1624fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1625fa9e4066Sahrens }
1626fa9e4066Sahrens 
1627fa9e4066Sahrens int
1628fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1629fa9e4066Sahrens {
1630fa9e4066Sahrens 	dnode_t *dn;
1631fa9e4066Sahrens 	int i, err;
1632fa9e4066Sahrens 
1633*ea8dc4b6Seschrock 	err = dnode_hold(os->os, object, FTAG, &dn);
1634*ea8dc4b6Seschrock 	if (err)
1635*ea8dc4b6Seschrock 		return (err);
1636fa9e4066Sahrens 	/*
1637fa9e4066Sahrens 	 * Sync any current changes before
1638fa9e4066Sahrens 	 * we go trundling through the block pointers.
1639fa9e4066Sahrens 	 */
1640fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1641fa9e4066Sahrens 		if (dn->dn_dirtyblksz[i])
1642fa9e4066Sahrens 			break;
1643fa9e4066Sahrens 	}
1644fa9e4066Sahrens 	if (i != TXG_SIZE) {
1645fa9e4066Sahrens 		dnode_rele(dn, FTAG);
1646fa9e4066Sahrens 		txg_wait_synced(dmu_objset_pool(os), 0);
1647*ea8dc4b6Seschrock 		err = dnode_hold(os->os, object, FTAG, &dn);
1648*ea8dc4b6Seschrock 		if (err)
1649*ea8dc4b6Seschrock 			return (err);
1650fa9e4066Sahrens 	}
1651fa9e4066Sahrens 
1652fa9e4066Sahrens 	err = dnode_next_offset(dn, hole, off, 1, 1);
1653fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1654fa9e4066Sahrens 
1655fa9e4066Sahrens 	return (err);
1656fa9e4066Sahrens }
1657fa9e4066Sahrens 
1658fa9e4066Sahrens void
1659fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1660fa9e4066Sahrens {
1661fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1662fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1663fa9e4066Sahrens 
1664fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1665fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1666fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1667fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1668fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1669fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
1670fa9e4066Sahrens 	doi->doi_physical_blks = dn->dn_phys->dn_secphys;
1671fa9e4066Sahrens 	doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1672fa9e4066Sahrens 	doi->doi_type = dn->dn_type;
1673fa9e4066Sahrens 	doi->doi_bonus_size = dn->dn_bonuslen;
1674fa9e4066Sahrens 	doi->doi_bonus_type = dn->dn_bonustype;
1675fa9e4066Sahrens 
1676fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1677fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1678fa9e4066Sahrens }
1679fa9e4066Sahrens 
1680fa9e4066Sahrens /*
1681fa9e4066Sahrens  * Get information on a DMU object.
1682fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1683fa9e4066Sahrens  */
1684fa9e4066Sahrens int
1685fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1686fa9e4066Sahrens {
1687*ea8dc4b6Seschrock 	dnode_t *dn;
1688*ea8dc4b6Seschrock 	int err = dnode_hold(os->os, object, FTAG, &dn);
1689fa9e4066Sahrens 
1690*ea8dc4b6Seschrock 	if (err)
1691*ea8dc4b6Seschrock 		return (err);
1692fa9e4066Sahrens 
1693fa9e4066Sahrens 	if (doi != NULL)
1694fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
1695fa9e4066Sahrens 
1696fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1697fa9e4066Sahrens 	return (0);
1698fa9e4066Sahrens }
1699fa9e4066Sahrens 
1700fa9e4066Sahrens /*
1701fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
1702fa9e4066Sahrens  */
1703fa9e4066Sahrens void
1704fa9e4066Sahrens dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1705fa9e4066Sahrens {
1706fa9e4066Sahrens 	dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1707fa9e4066Sahrens }
1708fa9e4066Sahrens 
1709fa9e4066Sahrens /*
1710fa9e4066Sahrens  * Faster still when you only care about the size.
1711fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
1712fa9e4066Sahrens  */
1713fa9e4066Sahrens void
1714fa9e4066Sahrens dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1715fa9e4066Sahrens {
1716fa9e4066Sahrens 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1717fa9e4066Sahrens 
1718fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
1719fa9e4066Sahrens 	*nblk512 = dn->dn_phys->dn_secphys + 1;	/* add 1 for dnode space */
1720fa9e4066Sahrens }
1721fa9e4066Sahrens 
1722*ea8dc4b6Seschrock /*
1723*ea8dc4b6Seschrock  * Given a bookmark, return the name of the dataset, object, and range in
1724*ea8dc4b6Seschrock  * human-readable format.
1725*ea8dc4b6Seschrock  */
1726*ea8dc4b6Seschrock int
1727*ea8dc4b6Seschrock spa_bookmark_name(spa_t *spa, zbookmark_t *zb, char *dsname, size_t dslen,
1728*ea8dc4b6Seschrock     char *objname, size_t objlen, char *range, size_t rangelen)
1729*ea8dc4b6Seschrock {
1730*ea8dc4b6Seschrock 	dsl_pool_t *dp;
1731*ea8dc4b6Seschrock 	dsl_dataset_t *ds = NULL;
1732*ea8dc4b6Seschrock 	objset_t *os = NULL;
1733*ea8dc4b6Seschrock 	dnode_t *dn = NULL;
1734*ea8dc4b6Seschrock 	int err, shift;
1735*ea8dc4b6Seschrock 
1736*ea8dc4b6Seschrock 	if (dslen < MAXNAMELEN || objlen < 32 || rangelen < 64)
1737*ea8dc4b6Seschrock 		return (ENOSPC);
1738*ea8dc4b6Seschrock 
1739*ea8dc4b6Seschrock 	dp = spa_get_dsl(spa);
1740*ea8dc4b6Seschrock 	if (zb->zb_objset != 0) {
1741*ea8dc4b6Seschrock 		rw_enter(&dp->dp_config_rwlock, RW_READER);
1742*ea8dc4b6Seschrock 		err = dsl_dataset_open_obj(dp, zb->zb_objset,
1743*ea8dc4b6Seschrock 		    NULL, DS_MODE_NONE, FTAG, &ds);
1744*ea8dc4b6Seschrock 		if (err) {
1745*ea8dc4b6Seschrock 			rw_exit(&dp->dp_config_rwlock);
1746*ea8dc4b6Seschrock 			return (err);
1747*ea8dc4b6Seschrock 		}
1748*ea8dc4b6Seschrock 		dsl_dataset_name(ds, dsname);
1749*ea8dc4b6Seschrock 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
1750*ea8dc4b6Seschrock 		rw_exit(&dp->dp_config_rwlock);
1751*ea8dc4b6Seschrock 
1752*ea8dc4b6Seschrock 		err = dmu_objset_open(dsname, DMU_OST_ANY, DS_MODE_NONE, &os);
1753*ea8dc4b6Seschrock 		if (err)
1754*ea8dc4b6Seschrock 			goto out;
1755*ea8dc4b6Seschrock 
1756*ea8dc4b6Seschrock 	} else {
1757*ea8dc4b6Seschrock 		dsl_dataset_name(NULL, dsname);
1758*ea8dc4b6Seschrock 		os = dp->dp_meta_objset;
1759*ea8dc4b6Seschrock 	}
1760*ea8dc4b6Seschrock 
1761*ea8dc4b6Seschrock 
1762*ea8dc4b6Seschrock 	if (zb->zb_object == DMU_META_DNODE_OBJECT) {
1763*ea8dc4b6Seschrock 		(void) strncpy(objname, "mdn", objlen);
1764*ea8dc4b6Seschrock 	} else {
1765*ea8dc4b6Seschrock 		(void) snprintf(objname, objlen, "%lld",
1766*ea8dc4b6Seschrock 		    (longlong_t)zb->zb_object);
1767*ea8dc4b6Seschrock 	}
1768*ea8dc4b6Seschrock 
1769*ea8dc4b6Seschrock 	err = dnode_hold(os->os, zb->zb_object, FTAG, &dn);
1770*ea8dc4b6Seschrock 	if (err)
1771*ea8dc4b6Seschrock 		goto out;
1772*ea8dc4b6Seschrock 
1773*ea8dc4b6Seschrock 	shift = (dn->dn_datablkshift?dn->dn_datablkshift:SPA_MAXBLOCKSHIFT) +
1774*ea8dc4b6Seschrock 	    zb->zb_level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
1775*ea8dc4b6Seschrock 	(void) snprintf(range, rangelen, "%llu-%llu",
1776*ea8dc4b6Seschrock 	    (u_longlong_t)(zb->zb_blkid << shift),
1777*ea8dc4b6Seschrock 	    (u_longlong_t)((zb->zb_blkid+1) << shift));
1778*ea8dc4b6Seschrock 
1779*ea8dc4b6Seschrock out:
1780*ea8dc4b6Seschrock 	if (dn)
1781*ea8dc4b6Seschrock 		dnode_rele(dn, FTAG);
1782*ea8dc4b6Seschrock 	if (os && os != dp->dp_meta_objset)
1783*ea8dc4b6Seschrock 		dmu_objset_close(os);
1784*ea8dc4b6Seschrock 	return (err);
1785*ea8dc4b6Seschrock }
1786*ea8dc4b6Seschrock 
1787fa9e4066Sahrens void
1788fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
1789fa9e4066Sahrens {
1790fa9e4066Sahrens 	uint64_t *buf = vbuf;
1791fa9e4066Sahrens 	size_t count = size >> 3;
1792fa9e4066Sahrens 	int i;
1793fa9e4066Sahrens 
1794fa9e4066Sahrens 	ASSERT((size & 7) == 0);
1795fa9e4066Sahrens 
1796fa9e4066Sahrens 	for (i = 0; i < count; i++)
1797fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
1798fa9e4066Sahrens }
1799fa9e4066Sahrens 
1800fa9e4066Sahrens void
1801fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
1802fa9e4066Sahrens {
1803fa9e4066Sahrens 	uint32_t *buf = vbuf;
1804fa9e4066Sahrens 	size_t count = size >> 2;
1805fa9e4066Sahrens 	int i;
1806fa9e4066Sahrens 
1807fa9e4066Sahrens 	ASSERT((size & 3) == 0);
1808fa9e4066Sahrens 
1809fa9e4066Sahrens 	for (i = 0; i < count; i++)
1810fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
1811fa9e4066Sahrens }
1812fa9e4066Sahrens 
1813fa9e4066Sahrens void
1814fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
1815fa9e4066Sahrens {
1816fa9e4066Sahrens 	uint16_t *buf = vbuf;
1817fa9e4066Sahrens 	size_t count = size >> 1;
1818fa9e4066Sahrens 	int i;
1819fa9e4066Sahrens 
1820fa9e4066Sahrens 	ASSERT((size & 1) == 0);
1821fa9e4066Sahrens 
1822fa9e4066Sahrens 	for (i = 0; i < count; i++)
1823fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
1824fa9e4066Sahrens }
1825fa9e4066Sahrens 
1826fa9e4066Sahrens /* ARGSUSED */
1827fa9e4066Sahrens void
1828fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
1829fa9e4066Sahrens {
1830fa9e4066Sahrens }
1831fa9e4066Sahrens 
1832fa9e4066Sahrens void
1833fa9e4066Sahrens dmu_init(void)
1834fa9e4066Sahrens {
1835fa9e4066Sahrens 	dbuf_init();
1836fa9e4066Sahrens 	dnode_init();
1837fa9e4066Sahrens 	arc_init();
1838fa9e4066Sahrens }
1839fa9e4066Sahrens 
1840fa9e4066Sahrens void
1841fa9e4066Sahrens dmu_fini(void)
1842fa9e4066Sahrens {
1843fa9e4066Sahrens 	arc_fini();
1844fa9e4066Sahrens 	dnode_fini();
1845fa9e4066Sahrens 	dbuf_fini();
1846fa9e4066Sahrens }
1847