xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision edf345e6b8342e8627ec20ce821a977a62cee19d)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2294d1a210STim Haley  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*edf345e6SMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25aad02571SSaso Kiselkov /* Copyright (c) 2013 by Saso Kiselkov. All rights reserved. */
26810e43b2SBill Pijewski /* Copyright (c) 2013, Joyent, Inc. All rights reserved. */
27aad02571SSaso Kiselkov 
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>
391d452cf5Sahrens #include <sys/dsl_synctask.h>
40a2eea2e1Sahrens #include <sys/dsl_prop.h>
41fa9e4066Sahrens #include <sys/dmu_zfetch.h>
42fa9e4066Sahrens #include <sys/zfs_ioctl.h>
43fa9e4066Sahrens #include <sys/zap.h>
44ea8dc4b6Seschrock #include <sys/zio_checksum.h>
4580901aeaSGeorge Wilson #include <sys/zio_compress.h>
460a586ceaSMark Shellenbaum #include <sys/sa.h>
4744eda4d7Smaybee #ifdef _KERNEL
4844eda4d7Smaybee #include <sys/vmsystm.h>
490fab61baSJonathan W Adams #include <sys/zfs_znode.h>
5044eda4d7Smaybee #endif
51fa9e4066Sahrens 
5280901aeaSGeorge Wilson /*
5380901aeaSGeorge Wilson  * Enable/disable nopwrite feature.
5480901aeaSGeorge Wilson  */
5580901aeaSGeorge Wilson int zfs_nopwrite_enabled = 1;
5680901aeaSGeorge Wilson 
57fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
58ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"unallocated"		},
59ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"object directory"	},
60ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"object array"		},
61ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"packed nvlist"		},
62ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"packed nvlist size"	},
63ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj"			},
64ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj header"		},
65ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map header"	},
66ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map"		},
67ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"ZIL intent log"	},
68ad135b5dSChristopher Siden 	{	DMU_BSWAP_DNODE,	TRUE,	"DMU dnode"		},
69ad135b5dSChristopher Siden 	{	DMU_BSWAP_OBJSET,	TRUE,	"DMU objset"		},
70ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL directory"		},
71ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL directory child map"},
72ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset snap map"	},
73ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL props"		},
74ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL dataset"		},
75ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZNODE,	TRUE,	"ZFS znode"		},
76ad135b5dSChristopher Siden 	{	DMU_BSWAP_OLDACL,	TRUE,	"ZFS V0 ACL"		},
77ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"ZFS plain file"	},
78ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS directory"		},
79ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS master node"	},
80ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS delete queue"	},
81ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"zvol object"		},
82ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"zvol prop"		},
83ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"other uint8[]"		},
84ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	FALSE,	"other uint64[]"	},
85ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"other ZAP"		},
86ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"persistent error log"	},
87ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"SPA history"		},
88ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA history offsets"	},
89ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"Pool properties"	},
90ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL permissions"	},
91ad135b5dSChristopher Siden 	{	DMU_BSWAP_ACL,		TRUE,	"ZFS ACL"		},
92ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"ZFS SYSACL"		},
93ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"FUID table"		},
94ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"FUID table size"	},
95ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset next clones"},
96ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan work queue"	},
97ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group used"	},
98ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group quota"	},
99ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"snapshot refcount tags"},
100ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT ZAP algorithm"	},
101ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT statistics"	},
102ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"System attributes"	},
103ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA master node"	},
104ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr registration"	},
105ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr layouts"	},
106ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan translations"	},
107ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"deduplicated block"	},
108ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL deadlist map"	},
109ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL deadlist map hdr"	},
110ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dir clones"	},
111ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj subobj"		}
112ad135b5dSChristopher Siden };
113ad135b5dSChristopher Siden 
114ad135b5dSChristopher Siden const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
115ad135b5dSChristopher Siden 	{	byteswap_uint8_array,	"uint8"		},
116ad135b5dSChristopher Siden 	{	byteswap_uint16_array,	"uint16"	},
117ad135b5dSChristopher Siden 	{	byteswap_uint32_array,	"uint32"	},
118ad135b5dSChristopher Siden 	{	byteswap_uint64_array,	"uint64"	},
119ad135b5dSChristopher Siden 	{	zap_byteswap,		"zap"		},
120ad135b5dSChristopher Siden 	{	dnode_buf_byteswap,	"dnode"		},
121ad135b5dSChristopher Siden 	{	dmu_objset_byteswap,	"objset"	},
122ad135b5dSChristopher Siden 	{	zfs_znode_byteswap,	"znode"		},
123ad135b5dSChristopher Siden 	{	zfs_oldacl_byteswap,	"oldacl"	},
124ad135b5dSChristopher Siden 	{	zfs_acl_byteswap,	"acl"		}
1253f9d6ad7SLin Ling };
126fa9e4066Sahrens 
127fa9e4066Sahrens int
128ea8dc4b6Seschrock dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
12947cb52daSJeff Bonwick     void *tag, dmu_buf_t **dbp, int flags)
130fa9e4066Sahrens {
131fa9e4066Sahrens 	dnode_t *dn;
132fa9e4066Sahrens 	uint64_t blkid;
133fa9e4066Sahrens 	dmu_buf_impl_t *db;
134ea8dc4b6Seschrock 	int err;
13547cb52daSJeff Bonwick 	int db_flags = DB_RF_CANFAIL;
13647cb52daSJeff Bonwick 
13747cb52daSJeff Bonwick 	if (flags & DMU_READ_NO_PREFETCH)
13847cb52daSJeff Bonwick 		db_flags |= DB_RF_NOPREFETCH;
139fa9e4066Sahrens 
140503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
141ea8dc4b6Seschrock 	if (err)
142ea8dc4b6Seschrock 		return (err);
143fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
144fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
145ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
146fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
147ea8dc4b6Seschrock 	if (db == NULL) {
148be6fd75aSMatthew Ahrens 		err = SET_ERROR(EIO);
149ea8dc4b6Seschrock 	} else {
15047cb52daSJeff Bonwick 		err = dbuf_read(db, NULL, db_flags);
151ea8dc4b6Seschrock 		if (err) {
152ea8dc4b6Seschrock 			dbuf_rele(db, tag);
153ea8dc4b6Seschrock 			db = NULL;
154ea8dc4b6Seschrock 		}
155fa9e4066Sahrens 	}
156fa9e4066Sahrens 
157ea8dc4b6Seschrock 	dnode_rele(dn, FTAG);
158744947dcSTom Erickson 	*dbp = &db->db; /* NULL db plus first field offset is NULL */
159ea8dc4b6Seschrock 	return (err);
160fa9e4066Sahrens }
161fa9e4066Sahrens 
162fa9e4066Sahrens int
163fa9e4066Sahrens dmu_bonus_max(void)
164fa9e4066Sahrens {
165fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
166fa9e4066Sahrens }
167fa9e4066Sahrens 
1681934e92fSmaybee int
169744947dcSTom Erickson dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
1701934e92fSmaybee {
171744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
172744947dcSTom Erickson 	dnode_t *dn;
173744947dcSTom Erickson 	int error;
1741934e92fSmaybee 
175744947dcSTom Erickson 	DB_DNODE_ENTER(db);
176744947dcSTom Erickson 	dn = DB_DNODE(db);
177744947dcSTom Erickson 
178744947dcSTom Erickson 	if (dn->dn_bonus != db) {
179be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
180744947dcSTom Erickson 	} else if (newsize < 0 || newsize > db_fake->db_size) {
181be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
182744947dcSTom Erickson 	} else {
1831934e92fSmaybee 		dnode_setbonuslen(dn, newsize, tx);
184744947dcSTom Erickson 		error = 0;
185744947dcSTom Erickson 	}
186744947dcSTom Erickson 
187744947dcSTom Erickson 	DB_DNODE_EXIT(db);
188744947dcSTom Erickson 	return (error);
1891934e92fSmaybee }
1901934e92fSmaybee 
1910a586ceaSMark Shellenbaum int
192744947dcSTom Erickson dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
1930a586ceaSMark Shellenbaum {
194744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
195744947dcSTom Erickson 	dnode_t *dn;
196744947dcSTom Erickson 	int error;
1970a586ceaSMark Shellenbaum 
198744947dcSTom Erickson 	DB_DNODE_ENTER(db);
199744947dcSTom Erickson 	dn = DB_DNODE(db);
2000a586ceaSMark Shellenbaum 
201ad135b5dSChristopher Siden 	if (!DMU_OT_IS_VALID(type)) {
202be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
203744947dcSTom Erickson 	} else if (dn->dn_bonus != db) {
204be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
205744947dcSTom Erickson 	} else {
2060a586ceaSMark Shellenbaum 		dnode_setbonus_type(dn, type, tx);
207744947dcSTom Erickson 		error = 0;
208744947dcSTom Erickson 	}
209744947dcSTom Erickson 
210744947dcSTom Erickson 	DB_DNODE_EXIT(db);
211744947dcSTom Erickson 	return (error);
212744947dcSTom Erickson }
213744947dcSTom Erickson 
214744947dcSTom Erickson dmu_object_type_t
215744947dcSTom Erickson dmu_get_bonustype(dmu_buf_t *db_fake)
216744947dcSTom Erickson {
217744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
218744947dcSTom Erickson 	dnode_t *dn;
219744947dcSTom Erickson 	dmu_object_type_t type;
220744947dcSTom Erickson 
221744947dcSTom Erickson 	DB_DNODE_ENTER(db);
222744947dcSTom Erickson 	dn = DB_DNODE(db);
223744947dcSTom Erickson 	type = dn->dn_bonustype;
224744947dcSTom Erickson 	DB_DNODE_EXIT(db);
225744947dcSTom Erickson 
226744947dcSTom Erickson 	return (type);
2270a586ceaSMark Shellenbaum }
2280a586ceaSMark Shellenbaum 
2290a586ceaSMark Shellenbaum int
2300a586ceaSMark Shellenbaum dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
2310a586ceaSMark Shellenbaum {
2320a586ceaSMark Shellenbaum 	dnode_t *dn;
2330a586ceaSMark Shellenbaum 	int error;
2340a586ceaSMark Shellenbaum 
2350a586ceaSMark Shellenbaum 	error = dnode_hold(os, object, FTAG, &dn);
2360a586ceaSMark Shellenbaum 	dbuf_rm_spill(dn, tx);
23706e0070dSMark Shellenbaum 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
23806e0070dSMark Shellenbaum 	dnode_rm_spill(dn, tx);
23906e0070dSMark Shellenbaum 	rw_exit(&dn->dn_struct_rwlock);
2400a586ceaSMark Shellenbaum 	dnode_rele(dn, FTAG);
2410a586ceaSMark Shellenbaum 	return (error);
2420a586ceaSMark Shellenbaum }
2430a586ceaSMark Shellenbaum 
244fa9e4066Sahrens /*
245ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
246fa9e4066Sahrens  */
247ea8dc4b6Seschrock int
248ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
249fa9e4066Sahrens {
250ea8dc4b6Seschrock 	dnode_t *dn;
251fa9e4066Sahrens 	dmu_buf_impl_t *db;
2521934e92fSmaybee 	int error;
253fa9e4066Sahrens 
254503ad85cSMatthew Ahrens 	error = dnode_hold(os, object, FTAG, &dn);
2551934e92fSmaybee 	if (error)
2561934e92fSmaybee 		return (error);
257fa9e4066Sahrens 
258fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
259ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
260fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
261ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
262ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
2631934e92fSmaybee 			dbuf_create_bonus(dn);
264fa9e4066Sahrens 	}
265ea8dc4b6Seschrock 	db = dn->dn_bonus;
2661934e92fSmaybee 
2671934e92fSmaybee 	/* as long as the bonus buf is held, the dnode will be held */
268744947dcSTom Erickson 	if (refcount_add(&db->db_holds, tag) == 1) {
2691934e92fSmaybee 		VERIFY(dnode_add_ref(dn, db));
270744947dcSTom Erickson 		(void) atomic_inc_32_nv(&dn->dn_dbufs_count);
271744947dcSTom Erickson 	}
272744947dcSTom Erickson 
273744947dcSTom Erickson 	/*
274744947dcSTom Erickson 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
275744947dcSTom Erickson 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
276744947dcSTom Erickson 	 * a dnode hold for every dbuf.
277744947dcSTom Erickson 	 */
278744947dcSTom Erickson 	rw_exit(&dn->dn_struct_rwlock);
2791934e92fSmaybee 
280fa9e4066Sahrens 	dnode_rele(dn, FTAG);
281ea8dc4b6Seschrock 
28247cb52daSJeff Bonwick 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
283ea8dc4b6Seschrock 
284ea8dc4b6Seschrock 	*dbp = &db->db;
285ea8dc4b6Seschrock 	return (0);
286fa9e4066Sahrens }
287fa9e4066Sahrens 
28813506d1eSmaybee /*
2890a586ceaSMark Shellenbaum  * returns ENOENT, EIO, or 0.
2900a586ceaSMark Shellenbaum  *
2910a586ceaSMark Shellenbaum  * This interface will allocate a blank spill dbuf when a spill blk
2920a586ceaSMark Shellenbaum  * doesn't already exist on the dnode.
2930a586ceaSMark Shellenbaum  *
2940a586ceaSMark Shellenbaum  * if you only want to find an already existing spill db, then
2950a586ceaSMark Shellenbaum  * dmu_spill_hold_existing() should be used.
2960a586ceaSMark Shellenbaum  */
2970a586ceaSMark Shellenbaum int
2980a586ceaSMark Shellenbaum dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
2990a586ceaSMark Shellenbaum {
3000a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
3010a586ceaSMark Shellenbaum 	int err;
3020a586ceaSMark Shellenbaum 
3030a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3040a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3050a586ceaSMark Shellenbaum 
3060a586ceaSMark Shellenbaum 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
3070a586ceaSMark Shellenbaum 
3080a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3090a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
3100a586ceaSMark Shellenbaum 
3110a586ceaSMark Shellenbaum 	ASSERT(db != NULL);
3121d8ccc7bSMark Shellenbaum 	err = dbuf_read(db, NULL, flags);
3131d8ccc7bSMark Shellenbaum 	if (err == 0)
3140a586ceaSMark Shellenbaum 		*dbp = &db->db;
3151d8ccc7bSMark Shellenbaum 	else
3161d8ccc7bSMark Shellenbaum 		dbuf_rele(db, tag);
3170a586ceaSMark Shellenbaum 	return (err);
3180a586ceaSMark Shellenbaum }
3190a586ceaSMark Shellenbaum 
3200a586ceaSMark Shellenbaum int
3210a586ceaSMark Shellenbaum dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3220a586ceaSMark Shellenbaum {
323744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
324744947dcSTom Erickson 	dnode_t *dn;
3250a586ceaSMark Shellenbaum 	int err;
3260a586ceaSMark Shellenbaum 
327744947dcSTom Erickson 	DB_DNODE_ENTER(db);
328744947dcSTom Erickson 	dn = DB_DNODE(db);
329744947dcSTom Erickson 
330744947dcSTom Erickson 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
331be6fd75aSMatthew Ahrens 		err = SET_ERROR(EINVAL);
332744947dcSTom Erickson 	} else {
3330a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3340a586ceaSMark Shellenbaum 
3350a586ceaSMark Shellenbaum 		if (!dn->dn_have_spill) {
336be6fd75aSMatthew Ahrens 			err = SET_ERROR(ENOENT);
337744947dcSTom Erickson 		} else {
3381d8ccc7bSMark Shellenbaum 			err = dmu_spill_hold_by_dnode(dn,
3391d8ccc7bSMark Shellenbaum 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
340744947dcSTom Erickson 		}
341744947dcSTom Erickson 
3420a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
343744947dcSTom Erickson 	}
344744947dcSTom Erickson 
345744947dcSTom Erickson 	DB_DNODE_EXIT(db);
3460a586ceaSMark Shellenbaum 	return (err);
3470a586ceaSMark Shellenbaum }
3480a586ceaSMark Shellenbaum 
3490a586ceaSMark Shellenbaum int
3500a586ceaSMark Shellenbaum dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3510a586ceaSMark Shellenbaum {
352744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
353744947dcSTom Erickson 	dnode_t *dn;
354744947dcSTom Erickson 	int err;
355744947dcSTom Erickson 
356744947dcSTom Erickson 	DB_DNODE_ENTER(db);
357744947dcSTom Erickson 	dn = DB_DNODE(db);
358744947dcSTom Erickson 	err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
359744947dcSTom Erickson 	DB_DNODE_EXIT(db);
360744947dcSTom Erickson 
361744947dcSTom Erickson 	return (err);
3620a586ceaSMark Shellenbaum }
3630a586ceaSMark Shellenbaum 
3640a586ceaSMark Shellenbaum /*
36513506d1eSmaybee  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
36613506d1eSmaybee  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
36713506d1eSmaybee  * and can induce severe lock contention when writing to several files
36813506d1eSmaybee  * whose dnodes are in the same block.
36913506d1eSmaybee  */
37013506d1eSmaybee static int
3717bfdf011SNeil Perrin dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
3727bfdf011SNeil Perrin     int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
373fa9e4066Sahrens {
374fa9e4066Sahrens 	dmu_buf_t **dbp;
375fa9e4066Sahrens 	uint64_t blkid, nblks, i;
3767bfdf011SNeil Perrin 	uint32_t dbuf_flags;
377ea8dc4b6Seschrock 	int err;
378ea8dc4b6Seschrock 	zio_t *zio;
379ea8dc4b6Seschrock 
380ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
381fa9e4066Sahrens 
382c87b8fc5SMark J Musante 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;
3837bfdf011SNeil Perrin 	if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz)
3847bfdf011SNeil Perrin 		dbuf_flags |= DB_RF_NOPREFETCH;
385ea8dc4b6Seschrock 
386fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
387fa9e4066Sahrens 	if (dn->dn_datablkshift) {
388fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
389fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
390fa9e4066Sahrens 		    P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
391fa9e4066Sahrens 	} else {
3920125049cSahrens 		if (offset + length > dn->dn_datablksz) {
3930125049cSahrens 			zfs_panic_recover("zfs: accessing past end of object "
3940125049cSahrens 			    "%llx/%llx (size=%u access=%llu+%llu)",
3950125049cSahrens 			    (longlong_t)dn->dn_objset->
3960125049cSahrens 			    os_dsl_dataset->ds_object,
3970125049cSahrens 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
3980125049cSahrens 			    (longlong_t)offset, (longlong_t)length);
399c87b8fc5SMark J Musante 			rw_exit(&dn->dn_struct_rwlock);
400be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
4010125049cSahrens 		}
402fa9e4066Sahrens 		nblks = 1;
403fa9e4066Sahrens 	}
404ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
405fa9e4066Sahrens 
406e14bb325SJeff Bonwick 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
407fa9e4066Sahrens 	blkid = dbuf_whichblock(dn, offset);
408fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
409ea8dc4b6Seschrock 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
410ea8dc4b6Seschrock 		if (db == NULL) {
411ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
412ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
413ea8dc4b6Seschrock 			zio_nowait(zio);
414be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
415ea8dc4b6Seschrock 		}
416ea8dc4b6Seschrock 		/* initiate async i/o */
41713506d1eSmaybee 		if (read) {
4187bfdf011SNeil Perrin 			(void) dbuf_read(db, zio, dbuf_flags);
419ea8dc4b6Seschrock 		}
420ea8dc4b6Seschrock 		dbp[i] = &db->db;
421fa9e4066Sahrens 	}
422fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
423fa9e4066Sahrens 
424ea8dc4b6Seschrock 	/* wait for async i/o */
425ea8dc4b6Seschrock 	err = zio_wait(zio);
426ea8dc4b6Seschrock 	if (err) {
427ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
428ea8dc4b6Seschrock 		return (err);
429ea8dc4b6Seschrock 	}
430ea8dc4b6Seschrock 
431ea8dc4b6Seschrock 	/* wait for other io to complete */
432ea8dc4b6Seschrock 	if (read) {
433ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
434ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
435ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
436ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
437ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
438ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
439ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
440be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
441ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
442ea8dc4b6Seschrock 			if (err) {
443ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
444ea8dc4b6Seschrock 				return (err);
445ea8dc4b6Seschrock 			}
446ea8dc4b6Seschrock 		}
447ea8dc4b6Seschrock 	}
448ea8dc4b6Seschrock 
449ea8dc4b6Seschrock 	*numbufsp = nblks;
450ea8dc4b6Seschrock 	*dbpp = dbp;
451ea8dc4b6Seschrock 	return (0);
452fa9e4066Sahrens }
453fa9e4066Sahrens 
454a2eea2e1Sahrens static int
45513506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
45613506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
45713506d1eSmaybee {
45813506d1eSmaybee 	dnode_t *dn;
45913506d1eSmaybee 	int err;
46013506d1eSmaybee 
461503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
46213506d1eSmaybee 	if (err)
46313506d1eSmaybee 		return (err);
46413506d1eSmaybee 
46513506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
4667bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
46713506d1eSmaybee 
46813506d1eSmaybee 	dnode_rele(dn, FTAG);
46913506d1eSmaybee 
47013506d1eSmaybee 	return (err);
47113506d1eSmaybee }
47213506d1eSmaybee 
47313506d1eSmaybee int
474744947dcSTom Erickson dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
47513506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
47613506d1eSmaybee {
477744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
478744947dcSTom Erickson 	dnode_t *dn;
47913506d1eSmaybee 	int err;
48013506d1eSmaybee 
481744947dcSTom Erickson 	DB_DNODE_ENTER(db);
482744947dcSTom Erickson 	dn = DB_DNODE(db);
48313506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
4847bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
485744947dcSTom Erickson 	DB_DNODE_EXIT(db);
48613506d1eSmaybee 
48713506d1eSmaybee 	return (err);
48813506d1eSmaybee }
48913506d1eSmaybee 
490fa9e4066Sahrens void
491ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
492fa9e4066Sahrens {
493fa9e4066Sahrens 	int i;
494fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
495fa9e4066Sahrens 
496fa9e4066Sahrens 	if (numbufs == 0)
497fa9e4066Sahrens 		return;
498fa9e4066Sahrens 
499ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
500ea8dc4b6Seschrock 		if (dbp[i])
501ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
502ea8dc4b6Seschrock 	}
503fa9e4066Sahrens 
504fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
505fa9e4066Sahrens }
506fa9e4066Sahrens 
50769962b56SMatthew Ahrens /*
50869962b56SMatthew Ahrens  * Issue prefetch i/os for the given blocks.
50969962b56SMatthew Ahrens  *
51069962b56SMatthew Ahrens  * Note: The assumption is that we *know* these blocks will be needed
51169962b56SMatthew Ahrens  * almost immediately.  Therefore, the prefetch i/os will be issued at
51269962b56SMatthew Ahrens  * ZIO_PRIORITY_SYNC_READ
51369962b56SMatthew Ahrens  *
51469962b56SMatthew Ahrens  * Note: indirect blocks and other metadata will be read synchronously,
51569962b56SMatthew Ahrens  * causing this function to block if they are not already cached.
51669962b56SMatthew Ahrens  */
517fa9e4066Sahrens void
518fa9e4066Sahrens dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
519fa9e4066Sahrens {
520fa9e4066Sahrens 	dnode_t *dn;
521fa9e4066Sahrens 	uint64_t blkid;
52269962b56SMatthew Ahrens 	int nblks, err;
523fa9e4066Sahrens 
524416e0cd8Sek110237 	if (zfs_prefetch_disable)
525416e0cd8Sek110237 		return;
526416e0cd8Sek110237 
527fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
528744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
529fa9e4066Sahrens 
530fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
531fa9e4066Sahrens 			return;
532fa9e4066Sahrens 
533fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
534fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
53569962b56SMatthew Ahrens 		dbuf_prefetch(dn, blkid, ZIO_PRIORITY_SYNC_READ);
536fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
537fa9e4066Sahrens 		return;
538fa9e4066Sahrens 	}
539fa9e4066Sahrens 
540fa9e4066Sahrens 	/*
541fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
542fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
543fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
544fa9e4066Sahrens 	 */
545503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
546ea8dc4b6Seschrock 	if (err != 0)
547fa9e4066Sahrens 		return;
548fa9e4066Sahrens 
549fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
550fa9e4066Sahrens 	if (dn->dn_datablkshift) {
551fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
552fa9e4066Sahrens 		nblks = (P2ROUNDUP(offset + len, 1 << blkshift) -
553fa9e4066Sahrens 		    P2ALIGN(offset, 1 << blkshift)) >> blkshift;
554fa9e4066Sahrens 	} else {
555fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
556fa9e4066Sahrens 	}
557fa9e4066Sahrens 
558fa9e4066Sahrens 	if (nblks != 0) {
559fa9e4066Sahrens 		blkid = dbuf_whichblock(dn, offset);
56069962b56SMatthew Ahrens 		for (int i = 0; i < nblks; i++)
56169962b56SMatthew Ahrens 			dbuf_prefetch(dn, blkid + i, ZIO_PRIORITY_SYNC_READ);
562fa9e4066Sahrens 	}
563fa9e4066Sahrens 
564fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
565fa9e4066Sahrens 
566fa9e4066Sahrens 	dnode_rele(dn, FTAG);
567fa9e4066Sahrens }
568fa9e4066Sahrens 
56976256205SMark Maybee /*
57076256205SMark Maybee  * Get the next "chunk" of file data to free.  We traverse the file from
57176256205SMark Maybee  * the end so that the file gets shorter over time (if we crashes in the
57276256205SMark Maybee  * middle, this will leave us in a better state).  We find allocated file
57376256205SMark Maybee  * data by simply searching the allocated level 1 indirects.
574713d6c20SMatthew Ahrens  *
575713d6c20SMatthew Ahrens  * On input, *start should be the first offset that does not need to be
576713d6c20SMatthew Ahrens  * freed (e.g. "offset + length").  On return, *start will be the first
577713d6c20SMatthew Ahrens  * offset that should be freed.
57876256205SMark Maybee  */
579cdb0ab79Smaybee static int
580713d6c20SMatthew Ahrens get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum)
581cdb0ab79Smaybee {
582713d6c20SMatthew Ahrens 	uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
583713d6c20SMatthew Ahrens 	/* bytes of data covered by a level-1 indirect block */
58476256205SMark Maybee 	uint64_t iblkrange =
5851c8564a7SMark Maybee 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
586cdb0ab79Smaybee 
587713d6c20SMatthew Ahrens 	ASSERT3U(minimum, <=, *start);
588cdb0ab79Smaybee 
589713d6c20SMatthew Ahrens 	if (*start - minimum <= iblkrange * maxblks) {
590713d6c20SMatthew Ahrens 		*start = minimum;
591cdb0ab79Smaybee 		return (0);
592cdb0ab79Smaybee 	}
59376256205SMark Maybee 	ASSERT(ISP2(iblkrange));
594cdb0ab79Smaybee 
595713d6c20SMatthew Ahrens 	for (uint64_t blks = 0; *start > minimum && blks < maxblks; blks++) {
5961c8564a7SMark Maybee 		int err;
597cdb0ab79Smaybee 
598713d6c20SMatthew Ahrens 		/*
599713d6c20SMatthew Ahrens 		 * dnode_next_offset(BACKWARDS) will find an allocated L1
600713d6c20SMatthew Ahrens 		 * indirect block at or before the input offset.  We must
601713d6c20SMatthew Ahrens 		 * decrement *start so that it is at the end of the region
602713d6c20SMatthew Ahrens 		 * to search.
603713d6c20SMatthew Ahrens 		 */
604713d6c20SMatthew Ahrens 		(*start)--;
605cdb0ab79Smaybee 		err = dnode_next_offset(dn,
60676256205SMark Maybee 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
607cdb0ab79Smaybee 
608713d6c20SMatthew Ahrens 		/* if there are no indirect blocks before start, we are done */
60976256205SMark Maybee 		if (err == ESRCH) {
610713d6c20SMatthew Ahrens 			*start = minimum;
611713d6c20SMatthew Ahrens 			break;
612713d6c20SMatthew Ahrens 		} else if (err != 0) {
613cdb0ab79Smaybee 			return (err);
61476256205SMark Maybee 		}
615cdb0ab79Smaybee 
616713d6c20SMatthew Ahrens 		/* set start to the beginning of this L1 indirect */
61776256205SMark Maybee 		*start = P2ALIGN(*start, iblkrange);
618cdb0ab79Smaybee 	}
619713d6c20SMatthew Ahrens 	if (*start < minimum)
620713d6c20SMatthew Ahrens 		*start = minimum;
621cdb0ab79Smaybee 	return (0);
622cdb0ab79Smaybee }
623cdb0ab79Smaybee 
624cdb0ab79Smaybee static int
625cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
626713d6c20SMatthew Ahrens     uint64_t length)
627cdb0ab79Smaybee {
628713d6c20SMatthew Ahrens 	uint64_t object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
629713d6c20SMatthew Ahrens 	int err;
630cdb0ab79Smaybee 
631713d6c20SMatthew Ahrens 	if (offset >= object_size)
632cdb0ab79Smaybee 		return (0);
633cdb0ab79Smaybee 
634713d6c20SMatthew Ahrens 	if (length == DMU_OBJECT_END || offset + length > object_size)
635713d6c20SMatthew Ahrens 		length = object_size - offset;
636713d6c20SMatthew Ahrens 
637713d6c20SMatthew Ahrens 	while (length != 0) {
638713d6c20SMatthew Ahrens 		uint64_t chunk_end, chunk_begin;
639713d6c20SMatthew Ahrens 
640713d6c20SMatthew Ahrens 		chunk_end = chunk_begin = offset + length;
641713d6c20SMatthew Ahrens 
642713d6c20SMatthew Ahrens 		/* move chunk_begin backwards to the beginning of this chunk */
643713d6c20SMatthew Ahrens 		err = get_next_chunk(dn, &chunk_begin, offset);
644cdb0ab79Smaybee 		if (err)
645cdb0ab79Smaybee 			return (err);
646713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, >=, offset);
647713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, <=, chunk_end);
648cdb0ab79Smaybee 
649713d6c20SMatthew Ahrens 		dmu_tx_t *tx = dmu_tx_create(os);
650713d6c20SMatthew Ahrens 		dmu_tx_hold_free(tx, dn->dn_object,
651713d6c20SMatthew Ahrens 		    chunk_begin, chunk_end - chunk_begin);
652cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
653cdb0ab79Smaybee 		if (err) {
654cdb0ab79Smaybee 			dmu_tx_abort(tx);
655cdb0ab79Smaybee 			return (err);
656cdb0ab79Smaybee 		}
657713d6c20SMatthew Ahrens 		dnode_free_range(dn, chunk_begin, chunk_end - chunk_begin, tx);
658cdb0ab79Smaybee 		dmu_tx_commit(tx);
659713d6c20SMatthew Ahrens 
660713d6c20SMatthew Ahrens 		length -= chunk_end - chunk_begin;
661cdb0ab79Smaybee 	}
662cdb0ab79Smaybee 	return (0);
663cdb0ab79Smaybee }
664cdb0ab79Smaybee 
665cdb0ab79Smaybee int
666cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object,
667cdb0ab79Smaybee     uint64_t offset, uint64_t length)
668cdb0ab79Smaybee {
669cdb0ab79Smaybee 	dnode_t *dn;
670cdb0ab79Smaybee 	int err;
671cdb0ab79Smaybee 
672503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
673cdb0ab79Smaybee 	if (err != 0)
674cdb0ab79Smaybee 		return (err);
675713d6c20SMatthew Ahrens 	err = dmu_free_long_range_impl(os, dn, offset, length);
6765253393bSMatthew Ahrens 
6775253393bSMatthew Ahrens 	/*
6785253393bSMatthew Ahrens 	 * It is important to zero out the maxblkid when freeing the entire
6795253393bSMatthew Ahrens 	 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
6805253393bSMatthew Ahrens 	 * will take the fast path, and (b) dnode_reallocate() can verify
6815253393bSMatthew Ahrens 	 * that the entire file has been freed.
6825253393bSMatthew Ahrens 	 */
68343466aaeSMax Grossman 	if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
6845253393bSMatthew Ahrens 		dn->dn_maxblkid = 0;
6855253393bSMatthew Ahrens 
686cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
687cdb0ab79Smaybee 	return (err);
688cdb0ab79Smaybee }
689cdb0ab79Smaybee 
690cdb0ab79Smaybee int
691713d6c20SMatthew Ahrens dmu_free_long_object(objset_t *os, uint64_t object)
692cdb0ab79Smaybee {
693cdb0ab79Smaybee 	dmu_tx_t *tx;
694cdb0ab79Smaybee 	int err;
695cdb0ab79Smaybee 
696713d6c20SMatthew Ahrens 	err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
697cdb0ab79Smaybee 	if (err != 0)
698cdb0ab79Smaybee 		return (err);
699713d6c20SMatthew Ahrens 
700cdb0ab79Smaybee 	tx = dmu_tx_create(os);
701cdb0ab79Smaybee 	dmu_tx_hold_bonus(tx, object);
702713d6c20SMatthew Ahrens 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
703cdb0ab79Smaybee 	err = dmu_tx_assign(tx, TXG_WAIT);
704cdb0ab79Smaybee 	if (err == 0) {
705713d6c20SMatthew Ahrens 		err = dmu_object_free(os, object, tx);
706cdb0ab79Smaybee 		dmu_tx_commit(tx);
707cdb0ab79Smaybee 	} else {
708cdb0ab79Smaybee 		dmu_tx_abort(tx);
709cdb0ab79Smaybee 	}
710713d6c20SMatthew Ahrens 
711cdb0ab79Smaybee 	return (err);
712cdb0ab79Smaybee }
713cdb0ab79Smaybee 
714ea8dc4b6Seschrock int
715fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
716fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
717fa9e4066Sahrens {
718ea8dc4b6Seschrock 	dnode_t *dn;
719503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
720ea8dc4b6Seschrock 	if (err)
721ea8dc4b6Seschrock 		return (err);
722fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
723fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
724fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
725fa9e4066Sahrens 	dnode_rele(dn, FTAG);
726ea8dc4b6Seschrock 	return (0);
727fa9e4066Sahrens }
728fa9e4066Sahrens 
729ea8dc4b6Seschrock int
730ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
7317bfdf011SNeil Perrin     void *buf, uint32_t flags)
732fa9e4066Sahrens {
733fa9e4066Sahrens 	dnode_t *dn;
734fa9e4066Sahrens 	dmu_buf_t **dbp;
735c87b8fc5SMark J Musante 	int numbufs, err;
736fa9e4066Sahrens 
737503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
738ea8dc4b6Seschrock 	if (err)
739ea8dc4b6Seschrock 		return (err);
740feb08c6bSbillm 
741feb08c6bSbillm 	/*
742feb08c6bSbillm 	 * Deal with odd block sizes, where there can't be data past the first
743feb08c6bSbillm 	 * block.  If we ever do the tail block optimization, we will need to
744feb08c6bSbillm 	 * handle that here as well.
745feb08c6bSbillm 	 */
746c87b8fc5SMark J Musante 	if (dn->dn_maxblkid == 0) {
747fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
748fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
749fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
750fa9e4066Sahrens 		size = newsz;
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 
753fa9e4066Sahrens 	while (size > 0) {
754fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
755c87b8fc5SMark J Musante 		int i;
756fa9e4066Sahrens 
757fa9e4066Sahrens 		/*
758fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
759fa9e4066Sahrens 		 * to be reading in parallel.
760fa9e4066Sahrens 		 */
761a2eea2e1Sahrens 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
7627bfdf011SNeil Perrin 		    TRUE, FTAG, &numbufs, &dbp, flags);
763ea8dc4b6Seschrock 		if (err)
7641934e92fSmaybee 			break;
765fa9e4066Sahrens 
766fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
767fa9e4066Sahrens 			int tocpy;
768fa9e4066Sahrens 			int bufoff;
769fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
770fa9e4066Sahrens 
771fa9e4066Sahrens 			ASSERT(size > 0);
772fa9e4066Sahrens 
773fa9e4066Sahrens 			bufoff = offset - db->db_offset;
774fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
775fa9e4066Sahrens 
776fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
777fa9e4066Sahrens 
778fa9e4066Sahrens 			offset += tocpy;
779fa9e4066Sahrens 			size -= tocpy;
780fa9e4066Sahrens 			buf = (char *)buf + tocpy;
781fa9e4066Sahrens 		}
782ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
783fa9e4066Sahrens 	}
784a2eea2e1Sahrens 	dnode_rele(dn, FTAG);
7851934e92fSmaybee 	return (err);
786fa9e4066Sahrens }
787fa9e4066Sahrens 
788fa9e4066Sahrens void
789fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
790fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
791fa9e4066Sahrens {
792fa9e4066Sahrens 	dmu_buf_t **dbp;
793fa9e4066Sahrens 	int numbufs, i;
794fa9e4066Sahrens 
79513506d1eSmaybee 	if (size == 0)
79613506d1eSmaybee 		return;
79713506d1eSmaybee 
798ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
799ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
800fa9e4066Sahrens 
801fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
802fa9e4066Sahrens 		int tocpy;
803fa9e4066Sahrens 		int bufoff;
804fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
805fa9e4066Sahrens 
806fa9e4066Sahrens 		ASSERT(size > 0);
807fa9e4066Sahrens 
808fa9e4066Sahrens 		bufoff = offset - db->db_offset;
809fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
810fa9e4066Sahrens 
811fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
812fa9e4066Sahrens 
813fa9e4066Sahrens 		if (tocpy == db->db_size)
814fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
815fa9e4066Sahrens 		else
816fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
817fa9e4066Sahrens 
818fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
819fa9e4066Sahrens 
820fa9e4066Sahrens 		if (tocpy == db->db_size)
821fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
822fa9e4066Sahrens 
823fa9e4066Sahrens 		offset += tocpy;
824fa9e4066Sahrens 		size -= tocpy;
825fa9e4066Sahrens 		buf = (char *)buf + tocpy;
826fa9e4066Sahrens 	}
827ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
828fa9e4066Sahrens }
829fa9e4066Sahrens 
83082c9918fSTim Haley void
83182c9918fSTim Haley dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
83282c9918fSTim Haley     dmu_tx_t *tx)
83382c9918fSTim Haley {
83482c9918fSTim Haley 	dmu_buf_t **dbp;
83582c9918fSTim Haley 	int numbufs, i;
83682c9918fSTim Haley 
83782c9918fSTim Haley 	if (size == 0)
83882c9918fSTim Haley 		return;
83982c9918fSTim Haley 
84082c9918fSTim Haley 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
84182c9918fSTim Haley 	    FALSE, FTAG, &numbufs, &dbp));
84282c9918fSTim Haley 
84382c9918fSTim Haley 	for (i = 0; i < numbufs; i++) {
84482c9918fSTim Haley 		dmu_buf_t *db = dbp[i];
84582c9918fSTim Haley 
84682c9918fSTim Haley 		dmu_buf_will_not_fill(db, tx);
84782c9918fSTim Haley 	}
84882c9918fSTim Haley 	dmu_buf_rele_array(dbp, numbufs, FTAG);
84982c9918fSTim Haley }
85082c9918fSTim Haley 
851c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
852c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * DMU support for xuio
853c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
854c242f9a0Schunli zhang - Sun Microsystems - Irvine United States kstat_t *xuio_ksp = NULL;
855c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
856c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
857c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_init(xuio_t *xuio, int nblk)
858c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
859c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv;
860c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
861c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
862c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iovcnt = nblk;
863c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
864c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
865c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
866c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->cnt = nblk;
867c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
868c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->iovp = uio->uio_iov;
869c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIO_XUZC_PRIV(xuio) = priv;
870c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
871c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
872c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
873c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
874c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
875c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
876c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
877c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
878c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
879c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
880c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_fini(xuio_t *xuio)
881c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
882c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
883c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int nblk = priv->cnt;
884c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
885c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
886c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
887c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv, sizeof (dmu_xuio_t));
888c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
889c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
890c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
891c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
892c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
893c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
894c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
895c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
896c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
897c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * and increase priv->next by 1.
898c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
899c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
900c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
901c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
902c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	struct iovec *iov;
903c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
904c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
905c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int i = priv->next++;
906c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
907c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
908c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(off + n <= arc_buf_size(abuf));
909c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov = uio->uio_iov + i;
910c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_base = (char *)abuf->b_data + off;
911c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_len = n;
912c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = abuf;
913c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
914c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
915c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
916c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
917c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_cnt(xuio_t *xuio)
918c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
919c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
920c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->cnt);
921c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
922c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
923c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_buf_t *
924c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_arcbuf(xuio_t *xuio, int i)
925c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
926c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
927c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
928c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
929c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->bufs[i]);
930c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
931c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
932c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
933c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_clear(xuio_t *xuio, int i)
934c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
935c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
936c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
937c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
938c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = NULL;
939c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
940c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
941c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
942c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_init(void)
943c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
944c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
945c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
946c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_FLAG_VIRTUAL);
947c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
948c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp->ks_data = &xuio_stats;
949c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_install(xuio_ksp);
950c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
951c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
952c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
953c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
954c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_fini(void)
955c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
956c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
957c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_delete(xuio_ksp);
958c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp = NULL;
959c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
960c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
961c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
962c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
963c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_copied()
964c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
965c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
966c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
967c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
968c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
969c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_nocopy()
970c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
971c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
972c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
973c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
974fa9e4066Sahrens #ifdef _KERNEL
975fa9e4066Sahrens int
976feb08c6bSbillm dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
977feb08c6bSbillm {
978feb08c6bSbillm 	dmu_buf_t **dbp;
979feb08c6bSbillm 	int numbufs, i, err;
980c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_t *xuio = NULL;
981feb08c6bSbillm 
982feb08c6bSbillm 	/*
983feb08c6bSbillm 	 * NB: we could do this block-at-a-time, but it's nice
984feb08c6bSbillm 	 * to be reading in parallel.
985feb08c6bSbillm 	 */
986feb08c6bSbillm 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
987feb08c6bSbillm 	    &numbufs, &dbp);
988feb08c6bSbillm 	if (err)
989feb08c6bSbillm 		return (err);
990feb08c6bSbillm 
991c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (uio->uio_extflg == UIO_XUIO)
992c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio = (xuio_t *)uio;
993c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
994feb08c6bSbillm 	for (i = 0; i < numbufs; i++) {
995feb08c6bSbillm 		int tocpy;
996feb08c6bSbillm 		int bufoff;
997feb08c6bSbillm 		dmu_buf_t *db = dbp[i];
998feb08c6bSbillm 
999feb08c6bSbillm 		ASSERT(size > 0);
1000feb08c6bSbillm 
1001feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1002feb08c6bSbillm 		tocpy = (int)MIN(db->db_size - bufoff, size);
1003feb08c6bSbillm 
1004c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (xuio) {
1005c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
1006c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *dbuf_abuf = dbi->db_buf;
1007c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1008c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1009c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (!err) {
1010c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_resid -= tocpy;
1011c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_loffset += tocpy;
1012c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			}
1013c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1014c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (abuf == dbuf_abuf)
1015c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1016c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			else
1017c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1018c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		} else {
1019feb08c6bSbillm 			err = uiomove((char *)db->db_data + bufoff, tocpy,
1020feb08c6bSbillm 			    UIO_READ, uio);
1021c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		}
1022feb08c6bSbillm 		if (err)
1023feb08c6bSbillm 			break;
1024feb08c6bSbillm 
1025feb08c6bSbillm 		size -= tocpy;
1026feb08c6bSbillm 	}
1027feb08c6bSbillm 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1028feb08c6bSbillm 
1029feb08c6bSbillm 	return (err);
1030feb08c6bSbillm }
1031feb08c6bSbillm 
103294d1a210STim Haley static int
103394d1a210STim Haley dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1034fa9e4066Sahrens {
1035fa9e4066Sahrens 	dmu_buf_t **dbp;
103694d1a210STim Haley 	int numbufs;
1037fa9e4066Sahrens 	int err = 0;
103894d1a210STim Haley 	int i;
1039fa9e4066Sahrens 
104094d1a210STim Haley 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
104194d1a210STim Haley 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1042ea8dc4b6Seschrock 	if (err)
1043ea8dc4b6Seschrock 		return (err);
1044fa9e4066Sahrens 
1045fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
1046fa9e4066Sahrens 		int tocpy;
1047fa9e4066Sahrens 		int bufoff;
1048fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 		ASSERT(size > 0);
1051fa9e4066Sahrens 
1052feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1053fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
1054fa9e4066Sahrens 
1055fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1056fa9e4066Sahrens 
1057fa9e4066Sahrens 		if (tocpy == db->db_size)
1058fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
1059fa9e4066Sahrens 		else
1060fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
1061fa9e4066Sahrens 
1062fa9e4066Sahrens 		/*
1063fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
1064fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
1065fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
1066fa9e4066Sahrens 		 * block.
1067fa9e4066Sahrens 		 */
1068fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
1069fa9e4066Sahrens 		    UIO_WRITE, uio);
1070fa9e4066Sahrens 
1071fa9e4066Sahrens 		if (tocpy == db->db_size)
1072fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 		if (err)
1075fa9e4066Sahrens 			break;
1076fa9e4066Sahrens 
1077fa9e4066Sahrens 		size -= tocpy;
1078fa9e4066Sahrens 	}
107994d1a210STim Haley 
1080ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1081fa9e4066Sahrens 	return (err);
1082fa9e4066Sahrens }
108344eda4d7Smaybee 
108444eda4d7Smaybee int
108594d1a210STim Haley dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
108694d1a210STim Haley     dmu_tx_t *tx)
108794d1a210STim Haley {
1088744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1089744947dcSTom Erickson 	dnode_t *dn;
1090744947dcSTom Erickson 	int err;
1091744947dcSTom Erickson 
109294d1a210STim Haley 	if (size == 0)
109394d1a210STim Haley 		return (0);
109494d1a210STim Haley 
1095744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1096744947dcSTom Erickson 	dn = DB_DNODE(db);
1097744947dcSTom Erickson 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1098744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1099744947dcSTom Erickson 
1100744947dcSTom Erickson 	return (err);
110194d1a210STim Haley }
110294d1a210STim Haley 
110394d1a210STim Haley int
110494d1a210STim Haley dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
110594d1a210STim Haley     dmu_tx_t *tx)
110694d1a210STim Haley {
110794d1a210STim Haley 	dnode_t *dn;
110894d1a210STim Haley 	int err;
110994d1a210STim Haley 
111094d1a210STim Haley 	if (size == 0)
111194d1a210STim Haley 		return (0);
111294d1a210STim Haley 
111394d1a210STim Haley 	err = dnode_hold(os, object, FTAG, &dn);
111494d1a210STim Haley 	if (err)
111594d1a210STim Haley 		return (err);
111694d1a210STim Haley 
111794d1a210STim Haley 	err = dmu_write_uio_dnode(dn, uio, size, tx);
111894d1a210STim Haley 
111994d1a210STim Haley 	dnode_rele(dn, FTAG);
112094d1a210STim Haley 
112194d1a210STim Haley 	return (err);
112294d1a210STim Haley }
112394d1a210STim Haley 
112494d1a210STim Haley int
112544eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
112644eda4d7Smaybee     page_t *pp, dmu_tx_t *tx)
112744eda4d7Smaybee {
112844eda4d7Smaybee 	dmu_buf_t **dbp;
112944eda4d7Smaybee 	int numbufs, i;
113044eda4d7Smaybee 	int err;
113144eda4d7Smaybee 
113244eda4d7Smaybee 	if (size == 0)
113344eda4d7Smaybee 		return (0);
113444eda4d7Smaybee 
113544eda4d7Smaybee 	err = dmu_buf_hold_array(os, object, offset, size,
113644eda4d7Smaybee 	    FALSE, FTAG, &numbufs, &dbp);
113744eda4d7Smaybee 	if (err)
113844eda4d7Smaybee 		return (err);
113944eda4d7Smaybee 
114044eda4d7Smaybee 	for (i = 0; i < numbufs; i++) {
114144eda4d7Smaybee 		int tocpy, copied, thiscpy;
114244eda4d7Smaybee 		int bufoff;
114344eda4d7Smaybee 		dmu_buf_t *db = dbp[i];
114444eda4d7Smaybee 		caddr_t va;
114544eda4d7Smaybee 
114644eda4d7Smaybee 		ASSERT(size > 0);
114744eda4d7Smaybee 		ASSERT3U(db->db_size, >=, PAGESIZE);
114844eda4d7Smaybee 
114944eda4d7Smaybee 		bufoff = offset - db->db_offset;
115044eda4d7Smaybee 		tocpy = (int)MIN(db->db_size - bufoff, size);
115144eda4d7Smaybee 
115244eda4d7Smaybee 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
115344eda4d7Smaybee 
115444eda4d7Smaybee 		if (tocpy == db->db_size)
115544eda4d7Smaybee 			dmu_buf_will_fill(db, tx);
115644eda4d7Smaybee 		else
115744eda4d7Smaybee 			dmu_buf_will_dirty(db, tx);
115844eda4d7Smaybee 
115944eda4d7Smaybee 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
116044eda4d7Smaybee 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
116144eda4d7Smaybee 			thiscpy = MIN(PAGESIZE, tocpy - copied);
11620fab61baSJonathan W Adams 			va = zfs_map_page(pp, S_READ);
116344eda4d7Smaybee 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
11640fab61baSJonathan W Adams 			zfs_unmap_page(pp, va);
116544eda4d7Smaybee 			pp = pp->p_next;
116644eda4d7Smaybee 			bufoff += PAGESIZE;
116744eda4d7Smaybee 		}
116844eda4d7Smaybee 
116944eda4d7Smaybee 		if (tocpy == db->db_size)
117044eda4d7Smaybee 			dmu_buf_fill_done(db, tx);
117144eda4d7Smaybee 
117244eda4d7Smaybee 		offset += tocpy;
117344eda4d7Smaybee 		size -= tocpy;
117444eda4d7Smaybee 	}
117544eda4d7Smaybee 	dmu_buf_rele_array(dbp, numbufs, FTAG);
117644eda4d7Smaybee 	return (err);
117744eda4d7Smaybee }
1178fa9e4066Sahrens #endif
1179fa9e4066Sahrens 
11802fdbea25SAleksandr Guzovskiy /*
11812fdbea25SAleksandr Guzovskiy  * Allocate a loaned anonymous arc buffer.
11822fdbea25SAleksandr Guzovskiy  */
11832fdbea25SAleksandr Guzovskiy arc_buf_t *
11842fdbea25SAleksandr Guzovskiy dmu_request_arcbuf(dmu_buf_t *handle, int size)
11852fdbea25SAleksandr Guzovskiy {
1186744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
11872fdbea25SAleksandr Guzovskiy 
118843466aaeSMax Grossman 	return (arc_loan_buf(db->db_objset->os_spa, size));
11892fdbea25SAleksandr Guzovskiy }
11902fdbea25SAleksandr Guzovskiy 
11912fdbea25SAleksandr Guzovskiy /*
11922fdbea25SAleksandr Guzovskiy  * Free a loaned arc buffer.
11932fdbea25SAleksandr Guzovskiy  */
11942fdbea25SAleksandr Guzovskiy void
11952fdbea25SAleksandr Guzovskiy dmu_return_arcbuf(arc_buf_t *buf)
11962fdbea25SAleksandr Guzovskiy {
11972fdbea25SAleksandr Guzovskiy 	arc_return_buf(buf, FTAG);
11983b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(buf, FTAG));
11992fdbea25SAleksandr Guzovskiy }
12002fdbea25SAleksandr Guzovskiy 
12012fdbea25SAleksandr Guzovskiy /*
12022fdbea25SAleksandr Guzovskiy  * When possible directly assign passed loaned arc buffer to a dbuf.
12032fdbea25SAleksandr Guzovskiy  * If this is not possible copy the contents of passed arc buf via
12042fdbea25SAleksandr Guzovskiy  * dmu_write().
12052fdbea25SAleksandr Guzovskiy  */
12062fdbea25SAleksandr Guzovskiy void
12072fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
12082fdbea25SAleksandr Guzovskiy     dmu_tx_t *tx)
12092fdbea25SAleksandr Guzovskiy {
1210744947dcSTom Erickson 	dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1211744947dcSTom Erickson 	dnode_t *dn;
12122fdbea25SAleksandr Guzovskiy 	dmu_buf_impl_t *db;
12132fdbea25SAleksandr Guzovskiy 	uint32_t blksz = (uint32_t)arc_buf_size(buf);
12142fdbea25SAleksandr Guzovskiy 	uint64_t blkid;
12152fdbea25SAleksandr Guzovskiy 
1216744947dcSTom Erickson 	DB_DNODE_ENTER(dbuf);
1217744947dcSTom Erickson 	dn = DB_DNODE(dbuf);
12182fdbea25SAleksandr Guzovskiy 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
12192fdbea25SAleksandr Guzovskiy 	blkid = dbuf_whichblock(dn, offset);
12202fdbea25SAleksandr Guzovskiy 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
12212fdbea25SAleksandr Guzovskiy 	rw_exit(&dn->dn_struct_rwlock);
1222744947dcSTom Erickson 	DB_DNODE_EXIT(dbuf);
12232fdbea25SAleksandr Guzovskiy 
12242fdbea25SAleksandr Guzovskiy 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
12252fdbea25SAleksandr Guzovskiy 		dbuf_assign_arcbuf(db, buf, tx);
12262fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
12272fdbea25SAleksandr Guzovskiy 	} else {
1228744947dcSTom Erickson 		objset_t *os;
1229744947dcSTom Erickson 		uint64_t object;
1230744947dcSTom Erickson 
1231744947dcSTom Erickson 		DB_DNODE_ENTER(dbuf);
1232744947dcSTom Erickson 		dn = DB_DNODE(dbuf);
1233744947dcSTom Erickson 		os = dn->dn_objset;
1234744947dcSTom Erickson 		object = dn->dn_object;
1235744947dcSTom Erickson 		DB_DNODE_EXIT(dbuf);
1236744947dcSTom Erickson 
12372fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
1238744947dcSTom Erickson 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
12392fdbea25SAleksandr Guzovskiy 		dmu_return_arcbuf(buf);
1240c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
12412fdbea25SAleksandr Guzovskiy 	}
12422fdbea25SAleksandr Guzovskiy }
12432fdbea25SAleksandr Guzovskiy 
1244c5c6ffa0Smaybee typedef struct {
1245b24ab676SJeff Bonwick 	dbuf_dirty_record_t	*dsa_dr;
1246b24ab676SJeff Bonwick 	dmu_sync_cb_t		*dsa_done;
1247b24ab676SJeff Bonwick 	zgd_t			*dsa_zgd;
1248b24ab676SJeff Bonwick 	dmu_tx_t		*dsa_tx;
1249c717a561Smaybee } dmu_sync_arg_t;
1250c5c6ffa0Smaybee 
1251c5c6ffa0Smaybee /* ARGSUSED */
1252c5c6ffa0Smaybee static void
1253e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1254e14bb325SJeff Bonwick {
1255b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1256b24ab676SJeff Bonwick 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1257e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1258975c32a0SNeil Perrin 
1259b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
1260b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp)) {
1261b24ab676SJeff Bonwick 			/*
1262b24ab676SJeff Bonwick 			 * A block of zeros may compress to a hole, but the
1263b24ab676SJeff Bonwick 			 * block size still needs to be known for replay.
1264b24ab676SJeff Bonwick 			 */
1265b24ab676SJeff Bonwick 			BP_SET_LSIZE(bp, db->db_size);
1266b24ab676SJeff Bonwick 		} else {
1267e14bb325SJeff Bonwick 			ASSERT(BP_GET_LEVEL(bp) == 0);
1268e14bb325SJeff Bonwick 			bp->blk_fill = 1;
1269e14bb325SJeff Bonwick 		}
1270e14bb325SJeff Bonwick 	}
1271b24ab676SJeff Bonwick }
1272b24ab676SJeff Bonwick 
1273b24ab676SJeff Bonwick static void
1274b24ab676SJeff Bonwick dmu_sync_late_arrival_ready(zio_t *zio)
1275b24ab676SJeff Bonwick {
1276b24ab676SJeff Bonwick 	dmu_sync_ready(zio, NULL, zio->io_private);
1277b24ab676SJeff Bonwick }
1278e14bb325SJeff Bonwick 
1279e14bb325SJeff Bonwick /* ARGSUSED */
1280e14bb325SJeff Bonwick static void
1281c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1282c5c6ffa0Smaybee {
1283b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1284b24ab676SJeff Bonwick 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1285c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1286c5c6ffa0Smaybee 
1287b50a0fe0SNeil Perrin 	mutex_enter(&db->db_mtx);
1288b50a0fe0SNeil Perrin 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1289b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
129080901aeaSGeorge Wilson 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
129180901aeaSGeorge Wilson 		if (dr->dt.dl.dr_nopwrite) {
129280901aeaSGeorge Wilson 			blkptr_t *bp = zio->io_bp;
129380901aeaSGeorge Wilson 			blkptr_t *bp_orig = &zio->io_bp_orig;
129480901aeaSGeorge Wilson 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
129580901aeaSGeorge Wilson 
129680901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
129780901aeaSGeorge Wilson 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
129880901aeaSGeorge Wilson 			ASSERT(zio_checksum_table[chksum].ci_dedup);
129980901aeaSGeorge Wilson 		}
1300b24ab676SJeff Bonwick 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1301b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1302b24ab676SJeff Bonwick 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1303b50a0fe0SNeil Perrin 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by))
1304b50a0fe0SNeil Perrin 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1305b24ab676SJeff Bonwick 	} else {
1306b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1307b24ab676SJeff Bonwick 	}
1308c5c6ffa0Smaybee 	cv_broadcast(&db->db_changed);
1309b50a0fe0SNeil Perrin 	mutex_exit(&db->db_mtx);
1310b50a0fe0SNeil Perrin 
1311b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1312c717a561Smaybee 
1313b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1314b24ab676SJeff Bonwick }
1315b24ab676SJeff Bonwick 
1316b24ab676SJeff Bonwick static void
1317b24ab676SJeff Bonwick dmu_sync_late_arrival_done(zio_t *zio)
1318b24ab676SJeff Bonwick {
1319b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1320b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = zio->io_private;
132180901aeaSGeorge Wilson 	blkptr_t *bp_orig = &zio->io_bp_orig;
1322b24ab676SJeff Bonwick 
1323b24ab676SJeff Bonwick 	if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
132480901aeaSGeorge Wilson 		/*
132580901aeaSGeorge Wilson 		 * If we didn't allocate a new block (i.e. ZIO_FLAG_NOPWRITE)
132680901aeaSGeorge Wilson 		 * then there is nothing to do here. Otherwise, free the
132780901aeaSGeorge Wilson 		 * newly allocated block in this txg.
132880901aeaSGeorge Wilson 		 */
132980901aeaSGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
133080901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
133180901aeaSGeorge Wilson 		} else {
133280901aeaSGeorge Wilson 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1333b24ab676SJeff Bonwick 			ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1334b24ab676SJeff Bonwick 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1335b24ab676SJeff Bonwick 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1336b24ab676SJeff Bonwick 		}
133780901aeaSGeorge Wilson 	}
1338b24ab676SJeff Bonwick 
1339b24ab676SJeff Bonwick 	dmu_tx_commit(dsa->dsa_tx);
1340b24ab676SJeff Bonwick 
1341b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1342b24ab676SJeff Bonwick 
1343b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1344b24ab676SJeff Bonwick }
1345b24ab676SJeff Bonwick 
1346b24ab676SJeff Bonwick static int
1347b24ab676SJeff Bonwick dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1348b24ab676SJeff Bonwick     zio_prop_t *zp, zbookmark_t *zb)
1349b24ab676SJeff Bonwick {
1350b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1351b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1352b24ab676SJeff Bonwick 
1353b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1354b24ab676SJeff Bonwick 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
13556e1f5caaSNeil Perrin 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1356b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1357be6fd75aSMatthew Ahrens 		/* Make zl_get_data do txg_waited_synced() */
1358be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1359b24ab676SJeff Bonwick 	}
1360b24ab676SJeff Bonwick 
1361b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1362b24ab676SJeff Bonwick 	dsa->dsa_dr = NULL;
1363b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1364b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1365b24ab676SJeff Bonwick 	dsa->dsa_tx = tx;
1366b24ab676SJeff Bonwick 
1367b24ab676SJeff Bonwick 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1368b24ab676SJeff Bonwick 	    zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp,
136969962b56SMatthew Ahrens 	    dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done, dsa,
1370b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1371b24ab676SJeff Bonwick 
1372b24ab676SJeff Bonwick 	return (0);
1373c5c6ffa0Smaybee }
1374c5c6ffa0Smaybee 
1375fa9e4066Sahrens /*
1376c5c6ffa0Smaybee  * Intent log support: sync the block associated with db to disk.
1377c5c6ffa0Smaybee  * N.B. and XXX: the caller is responsible for making sure that the
1378c5c6ffa0Smaybee  * data isn't changing while dmu_sync() is writing it.
1379fa9e4066Sahrens  *
1380fa9e4066Sahrens  * Return values:
1381fa9e4066Sahrens  *
138280901aeaSGeorge Wilson  *	EEXIST: this txg has already been synced, so there's nothing to do.
1383fa9e4066Sahrens  *		The caller should not log the write.
1384fa9e4066Sahrens  *
1385fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1386fa9e4066Sahrens  *		The caller should not log the write.
1387fa9e4066Sahrens  *
1388c5c6ffa0Smaybee  *	EALREADY: this block is already in the process of being synced.
1389c5c6ffa0Smaybee  *		The caller should track its progress (somehow).
1390fa9e4066Sahrens  *
1391b24ab676SJeff Bonwick  *	EIO: could not do the I/O.
1392b24ab676SJeff Bonwick  *		The caller should do a txg_wait_synced().
1393fa9e4066Sahrens  *
1394b24ab676SJeff Bonwick  *	0: the I/O has been initiated.
1395b24ab676SJeff Bonwick  *		The caller should log this blkptr in the done callback.
1396b24ab676SJeff Bonwick  *		It is possible that the I/O will fail, in which case
1397b24ab676SJeff Bonwick  *		the error will be reported to the done callback and
1398b24ab676SJeff Bonwick  *		propagated to pio from zio_done().
1399fa9e4066Sahrens  */
1400fa9e4066Sahrens int
1401b24ab676SJeff Bonwick dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1402fa9e4066Sahrens {
1403b24ab676SJeff Bonwick 	blkptr_t *bp = zgd->zgd_bp;
1404b24ab676SJeff Bonwick 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1405503ad85cSMatthew Ahrens 	objset_t *os = db->db_objset;
1406b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
1407c717a561Smaybee 	dbuf_dirty_record_t *dr;
1408b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1409ea8dc4b6Seschrock 	zbookmark_t zb;
1410b24ab676SJeff Bonwick 	zio_prop_t zp;
1411744947dcSTom Erickson 	dnode_t *dn;
1412fa9e4066Sahrens 
1413b24ab676SJeff Bonwick 	ASSERT(pio != NULL);
1414fa9e4066Sahrens 	ASSERT(txg != 0);
1415fa9e4066Sahrens 
1416b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, ds->ds_object,
1417b24ab676SJeff Bonwick 	    db->db.db_object, db->db_level, db->db_blkid);
1418b24ab676SJeff Bonwick 
1419744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1420744947dcSTom Erickson 	dn = DB_DNODE(db);
1421744947dcSTom Erickson 	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1422744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1423fa9e4066Sahrens 
1424fa9e4066Sahrens 	/*
1425b24ab676SJeff Bonwick 	 * If we're frozen (running ziltest), we always need to generate a bp.
1426ea8dc4b6Seschrock 	 */
1427b24ab676SJeff Bonwick 	if (txg > spa_freeze_txg(os->os_spa))
1428b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1429ea8dc4b6Seschrock 
1430ea8dc4b6Seschrock 	/*
1431b24ab676SJeff Bonwick 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1432b24ab676SJeff Bonwick 	 * and us.  If we determine that this txg is not yet syncing,
1433b24ab676SJeff Bonwick 	 * but it begins to sync a moment later, that's OK because the
1434b24ab676SJeff Bonwick 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1435fa9e4066Sahrens 	 */
1436b24ab676SJeff Bonwick 	mutex_enter(&db->db_mtx);
1437b24ab676SJeff Bonwick 
1438b24ab676SJeff Bonwick 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1439fa9e4066Sahrens 		/*
1440b24ab676SJeff Bonwick 		 * This txg has already synced.  There's nothing to do.
1441fa9e4066Sahrens 		 */
1442b24ab676SJeff Bonwick 		mutex_exit(&db->db_mtx);
1443be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1444fa9e4066Sahrens 	}
1445fa9e4066Sahrens 
1446b24ab676SJeff Bonwick 	if (txg <= spa_syncing_txg(os->os_spa)) {
1447c5c6ffa0Smaybee 		/*
1448b24ab676SJeff Bonwick 		 * This txg is currently syncing, so we can't mess with
1449b24ab676SJeff Bonwick 		 * the dirty record anymore; just write a new log block.
1450c5c6ffa0Smaybee 		 */
145113506d1eSmaybee 		mutex_exit(&db->db_mtx);
1452b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1453c5c6ffa0Smaybee 	}
1454c5c6ffa0Smaybee 
1455c717a561Smaybee 	dr = db->db_last_dirty;
1456b24ab676SJeff Bonwick 	while (dr && dr->dr_txg != txg)
1457c717a561Smaybee 		dr = dr->dr_next;
1458b24ab676SJeff Bonwick 
1459b24ab676SJeff Bonwick 	if (dr == NULL) {
1460c5c6ffa0Smaybee 		/*
1461b24ab676SJeff Bonwick 		 * There's no dr for this dbuf, so it must have been freed.
1462fa9e4066Sahrens 		 * There's no need to log writes to freed blocks, so we're done.
1463fa9e4066Sahrens 		 */
1464fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1465be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1466fa9e4066Sahrens 	}
1467fa9e4066Sahrens 
146880901aeaSGeorge Wilson 	ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg);
146980901aeaSGeorge Wilson 
147080901aeaSGeorge Wilson 	/*
147180901aeaSGeorge Wilson 	 * Assume the on-disk data is X, the current syncing data is Y,
147280901aeaSGeorge Wilson 	 * and the current in-memory data is Z (currently in dmu_sync).
147380901aeaSGeorge Wilson 	 * X and Z are identical but Y is has been modified. Normally,
147480901aeaSGeorge Wilson 	 * when X and Z are the same we will perform a nopwrite but if Y
147580901aeaSGeorge Wilson 	 * is different we must disable nopwrite since the resulting write
147680901aeaSGeorge Wilson 	 * of Y to disk can free the block containing X. If we allowed a
147780901aeaSGeorge Wilson 	 * nopwrite to occur the block pointing to Z would reference a freed
147880901aeaSGeorge Wilson 	 * block. Since this is a rare case we simplify this by disabling
147980901aeaSGeorge Wilson 	 * nopwrite if the current dmu_sync-ing dbuf has been modified in
148080901aeaSGeorge Wilson 	 * a previous transaction.
148180901aeaSGeorge Wilson 	 */
148280901aeaSGeorge Wilson 	if (dr->dr_next)
148380901aeaSGeorge Wilson 		zp.zp_nopwrite = B_FALSE;
148480901aeaSGeorge Wilson 
1485c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
1486b24ab676SJeff Bonwick 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1487b24ab676SJeff Bonwick 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1488c5c6ffa0Smaybee 		/*
1489b24ab676SJeff Bonwick 		 * We have already issued a sync write for this buffer,
1490b24ab676SJeff Bonwick 		 * or this buffer has already been synced.  It could not
1491c717a561Smaybee 		 * have been dirtied since, or we would have cleared the state.
1492c717a561Smaybee 		 */
1493c717a561Smaybee 		mutex_exit(&db->db_mtx);
1494be6fd75aSMatthew Ahrens 		return (SET_ERROR(EALREADY));
1495c717a561Smaybee 	}
1496c717a561Smaybee 
1497b24ab676SJeff Bonwick 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1498c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1499fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1500fa9e4066Sahrens 
1501b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1502b24ab676SJeff Bonwick 	dsa->dsa_dr = dr;
1503b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1504b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1505b24ab676SJeff Bonwick 	dsa->dsa_tx = NULL;
1506e14bb325SJeff Bonwick 
1507b24ab676SJeff Bonwick 	zio_nowait(arc_write(pio, os->os_spa, txg,
1508aad02571SSaso Kiselkov 	    bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db),
150969962b56SMatthew Ahrens 	    DBUF_IS_L2COMPRESSIBLE(db), &zp, dmu_sync_ready,
151069962b56SMatthew Ahrens 	    NULL, dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE,
151169962b56SMatthew Ahrens 	    ZIO_FLAG_CANFAIL, &zb));
1512e14bb325SJeff Bonwick 
1513b24ab676SJeff Bonwick 	return (0);
1514fa9e4066Sahrens }
1515fa9e4066Sahrens 
1516fa9e4066Sahrens int
1517fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1518fa9e4066Sahrens 	dmu_tx_t *tx)
1519fa9e4066Sahrens {
1520ea8dc4b6Seschrock 	dnode_t *dn;
1521ea8dc4b6Seschrock 	int err;
1522ea8dc4b6Seschrock 
1523503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1524ea8dc4b6Seschrock 	if (err)
1525ea8dc4b6Seschrock 		return (err);
1526ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1527fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1528fa9e4066Sahrens 	return (err);
1529fa9e4066Sahrens }
1530fa9e4066Sahrens 
1531fa9e4066Sahrens void
1532fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1533fa9e4066Sahrens 	dmu_tx_t *tx)
1534fa9e4066Sahrens {
1535ea8dc4b6Seschrock 	dnode_t *dn;
1536ea8dc4b6Seschrock 
1537ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1538503ad85cSMatthew Ahrens 	(void) dnode_hold(os, object, FTAG, &dn);
1539fa9e4066Sahrens 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1540fa9e4066Sahrens 	dn->dn_checksum = checksum;
1541fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1542fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1543fa9e4066Sahrens }
1544fa9e4066Sahrens 
1545fa9e4066Sahrens void
1546fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1547fa9e4066Sahrens 	dmu_tx_t *tx)
1548fa9e4066Sahrens {
1549ea8dc4b6Seschrock 	dnode_t *dn;
1550ea8dc4b6Seschrock 
1551ea8dc4b6Seschrock 	/* XXX assumes dnode_hold will not get an i/o error */
1552503ad85cSMatthew Ahrens 	(void) dnode_hold(os, object, FTAG, &dn);
1553fa9e4066Sahrens 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1554fa9e4066Sahrens 	dn->dn_compress = compress;
1555fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1556fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
1559b24ab676SJeff Bonwick int zfs_mdcomp_disable = 0;
1560b24ab676SJeff Bonwick 
1561*edf345e6SMatthew Ahrens /*
1562*edf345e6SMatthew Ahrens  * When the "redundant_metadata" property is set to "most", only indirect
1563*edf345e6SMatthew Ahrens  * blocks of this level and higher will have an additional ditto block.
1564*edf345e6SMatthew Ahrens  */
1565*edf345e6SMatthew Ahrens int zfs_redundant_metadata_most_ditto_level = 2;
1566*edf345e6SMatthew Ahrens 
1567b24ab676SJeff Bonwick void
1568b24ab676SJeff Bonwick dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1569b24ab676SJeff Bonwick {
1570b24ab676SJeff Bonwick 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1571ad135b5dSChristopher Siden 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
15721d8ccc7bSMark Shellenbaum 	    (wp & WP_SPILL));
1573b24ab676SJeff Bonwick 	enum zio_checksum checksum = os->os_checksum;
1574b24ab676SJeff Bonwick 	enum zio_compress compress = os->os_compress;
1575b24ab676SJeff Bonwick 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
15767540df39SGeorge Wilson 	boolean_t dedup = B_FALSE;
15777540df39SGeorge Wilson 	boolean_t nopwrite = B_FALSE;
1578b24ab676SJeff Bonwick 	boolean_t dedup_verify = os->os_dedup_verify;
1579b24ab676SJeff Bonwick 	int copies = os->os_copies;
1580b24ab676SJeff Bonwick 
1581b24ab676SJeff Bonwick 	/*
158280901aeaSGeorge Wilson 	 * We maintain different write policies for each of the following
158380901aeaSGeorge Wilson 	 * types of data:
158480901aeaSGeorge Wilson 	 *	 1. metadata
158580901aeaSGeorge Wilson 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
158680901aeaSGeorge Wilson 	 *	 3. all other level 0 blocks
1587b24ab676SJeff Bonwick 	 */
1588b24ab676SJeff Bonwick 	if (ismd) {
1589b24ab676SJeff Bonwick 		/*
159080901aeaSGeorge Wilson 		 * XXX -- we should design a compression algorithm
159180901aeaSGeorge Wilson 		 * that specializes in arrays of bps.
159280901aeaSGeorge Wilson 		 */
159380901aeaSGeorge Wilson 		compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY :
159480901aeaSGeorge Wilson 		    ZIO_COMPRESS_LZJB;
159580901aeaSGeorge Wilson 
159680901aeaSGeorge Wilson 		/*
1597b24ab676SJeff Bonwick 		 * Metadata always gets checksummed.  If the data
1598b24ab676SJeff Bonwick 		 * checksum is multi-bit correctable, and it's not a
1599b24ab676SJeff Bonwick 		 * ZBT-style checksum, then it's suitable for metadata
1600b24ab676SJeff Bonwick 		 * as well.  Otherwise, the metadata checksum defaults
1601b24ab676SJeff Bonwick 		 * to fletcher4.
1602b24ab676SJeff Bonwick 		 */
1603b24ab676SJeff Bonwick 		if (zio_checksum_table[checksum].ci_correctable < 1 ||
16046e1f5caaSNeil Perrin 		    zio_checksum_table[checksum].ci_eck)
1605b24ab676SJeff Bonwick 			checksum = ZIO_CHECKSUM_FLETCHER_4;
1606*edf345e6SMatthew Ahrens 
1607*edf345e6SMatthew Ahrens 		if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||
1608*edf345e6SMatthew Ahrens 		    (os->os_redundant_metadata ==
1609*edf345e6SMatthew Ahrens 		    ZFS_REDUNDANT_METADATA_MOST &&
1610*edf345e6SMatthew Ahrens 		    (level >= zfs_redundant_metadata_most_ditto_level ||
1611*edf345e6SMatthew Ahrens 		    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))))
1612*edf345e6SMatthew Ahrens 			copies++;
161380901aeaSGeorge Wilson 	} else if (wp & WP_NOFILL) {
161480901aeaSGeorge Wilson 		ASSERT(level == 0);
1615b24ab676SJeff Bonwick 
1616b24ab676SJeff Bonwick 		/*
161780901aeaSGeorge Wilson 		 * If we're writing preallocated blocks, we aren't actually
161880901aeaSGeorge Wilson 		 * writing them so don't set any policy properties.  These
161980901aeaSGeorge Wilson 		 * blocks are currently only used by an external subsystem
162080901aeaSGeorge Wilson 		 * outside of zfs (i.e. dump) and not written by the zio
162180901aeaSGeorge Wilson 		 * pipeline.
1622b24ab676SJeff Bonwick 		 */
162380901aeaSGeorge Wilson 		compress = ZIO_COMPRESS_OFF;
1624810e43b2SBill Pijewski 		checksum = ZIO_CHECKSUM_NOPARITY;
1625b24ab676SJeff Bonwick 	} else {
1626b24ab676SJeff Bonwick 		compress = zio_compress_select(dn->dn_compress, compress);
162780901aeaSGeorge Wilson 
162880901aeaSGeorge Wilson 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
162980901aeaSGeorge Wilson 		    zio_checksum_select(dn->dn_checksum, checksum) :
163080901aeaSGeorge Wilson 		    dedup_checksum;
163180901aeaSGeorge Wilson 
163280901aeaSGeorge Wilson 		/*
163380901aeaSGeorge Wilson 		 * Determine dedup setting.  If we are in dmu_sync(),
163480901aeaSGeorge Wilson 		 * we won't actually dedup now because that's all
163580901aeaSGeorge Wilson 		 * done in syncing context; but we do want to use the
163680901aeaSGeorge Wilson 		 * dedup checkum.  If the checksum is not strong
163780901aeaSGeorge Wilson 		 * enough to ensure unique signatures, force
163880901aeaSGeorge Wilson 		 * dedup_verify.
163980901aeaSGeorge Wilson 		 */
164080901aeaSGeorge Wilson 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
164180901aeaSGeorge Wilson 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
164280901aeaSGeorge Wilson 			if (!zio_checksum_table[checksum].ci_dedup)
164380901aeaSGeorge Wilson 				dedup_verify = B_TRUE;
1644b24ab676SJeff Bonwick 		}
1645b24ab676SJeff Bonwick 
1646b24ab676SJeff Bonwick 		/*
164780901aeaSGeorge Wilson 		 * Enable nopwrite if we have a cryptographically secure
164880901aeaSGeorge Wilson 		 * checksum that has no known collisions (i.e. SHA-256)
164980901aeaSGeorge Wilson 		 * and compression is enabled.  We don't enable nopwrite if
165080901aeaSGeorge Wilson 		 * dedup is enabled as the two features are mutually exclusive.
1651b24ab676SJeff Bonwick 		 */
165280901aeaSGeorge Wilson 		nopwrite = (!dedup && zio_checksum_table[checksum].ci_dedup &&
165380901aeaSGeorge Wilson 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
1654b24ab676SJeff Bonwick 	}
1655b24ab676SJeff Bonwick 
1656b24ab676SJeff Bonwick 	zp->zp_checksum = checksum;
1657b24ab676SJeff Bonwick 	zp->zp_compress = compress;
16580a586ceaSMark Shellenbaum 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1659b24ab676SJeff Bonwick 	zp->zp_level = level;
1660*edf345e6SMatthew Ahrens 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
1661b24ab676SJeff Bonwick 	zp->zp_dedup = dedup;
1662b24ab676SJeff Bonwick 	zp->zp_dedup_verify = dedup && dedup_verify;
166380901aeaSGeorge Wilson 	zp->zp_nopwrite = nopwrite;
1664b24ab676SJeff Bonwick }
1665b24ab676SJeff Bonwick 
166644cd46caSbillm int
1667fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1668fa9e4066Sahrens {
1669fa9e4066Sahrens 	dnode_t *dn;
1670fa9e4066Sahrens 	int i, err;
1671fa9e4066Sahrens 
1672503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1673ea8dc4b6Seschrock 	if (err)
1674ea8dc4b6Seschrock 		return (err);
1675fa9e4066Sahrens 	/*
1676fa9e4066Sahrens 	 * Sync any current changes before
1677fa9e4066Sahrens 	 * we go trundling through the block pointers.
1678fa9e4066Sahrens 	 */
1679fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1680c543ec06Sahrens 		if (list_link_active(&dn->dn_dirty_link[i]))
1681fa9e4066Sahrens 			break;
1682fa9e4066Sahrens 	}
1683fa9e4066Sahrens 	if (i != TXG_SIZE) {
1684fa9e4066Sahrens 		dnode_rele(dn, FTAG);
1685fa9e4066Sahrens 		txg_wait_synced(dmu_objset_pool(os), 0);
1686503ad85cSMatthew Ahrens 		err = dnode_hold(os, object, FTAG, &dn);
1687ea8dc4b6Seschrock 		if (err)
1688ea8dc4b6Seschrock 			return (err);
1689fa9e4066Sahrens 	}
1690fa9e4066Sahrens 
1691cdb0ab79Smaybee 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1692fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1693fa9e4066Sahrens 
1694fa9e4066Sahrens 	return (err);
1695fa9e4066Sahrens }
1696fa9e4066Sahrens 
1697fa9e4066Sahrens void
1698fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1699fa9e4066Sahrens {
1700b24ab676SJeff Bonwick 	dnode_phys_t *dnp;
1701b24ab676SJeff Bonwick 
1702fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1703fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1704fa9e4066Sahrens 
1705b24ab676SJeff Bonwick 	dnp = dn->dn_phys;
1706b24ab676SJeff Bonwick 
1707fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1708fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1709fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1710b24ab676SJeff Bonwick 	doi->doi_type = dn->dn_type;
1711b24ab676SJeff Bonwick 	doi->doi_bonus_type = dn->dn_bonustype;
1712b24ab676SJeff Bonwick 	doi->doi_bonus_size = dn->dn_bonuslen;
1713fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1714fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1715fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
1716b24ab676SJeff Bonwick 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1717d0475637SMatthew Ahrens 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1718b24ab676SJeff Bonwick 	doi->doi_fill_count = 0;
1719b24ab676SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1720b24ab676SJeff Bonwick 		doi->doi_fill_count += dnp->dn_blkptr[i].blk_fill;
1721fa9e4066Sahrens 
1722fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1723fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1724fa9e4066Sahrens }
1725fa9e4066Sahrens 
1726fa9e4066Sahrens /*
1727fa9e4066Sahrens  * Get information on a DMU object.
1728fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1729fa9e4066Sahrens  */
1730fa9e4066Sahrens int
1731fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1732fa9e4066Sahrens {
1733ea8dc4b6Seschrock 	dnode_t *dn;
1734503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
1735fa9e4066Sahrens 
1736ea8dc4b6Seschrock 	if (err)
1737ea8dc4b6Seschrock 		return (err);
1738fa9e4066Sahrens 
1739fa9e4066Sahrens 	if (doi != NULL)
1740fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
1741fa9e4066Sahrens 
1742fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1743fa9e4066Sahrens 	return (0);
1744fa9e4066Sahrens }
1745fa9e4066Sahrens 
1746fa9e4066Sahrens /*
1747fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
1748fa9e4066Sahrens  */
1749fa9e4066Sahrens void
1750744947dcSTom Erickson dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
1751fa9e4066Sahrens {
1752744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1753744947dcSTom Erickson 
1754744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1755744947dcSTom Erickson 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
1756744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1757fa9e4066Sahrens }
1758fa9e4066Sahrens 
1759fa9e4066Sahrens /*
1760fa9e4066Sahrens  * Faster still when you only care about the size.
1761fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
1762fa9e4066Sahrens  */
1763fa9e4066Sahrens void
1764744947dcSTom Erickson dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
1765744947dcSTom Erickson     u_longlong_t *nblk512)
1766fa9e4066Sahrens {
1767744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1768744947dcSTom Erickson 	dnode_t *dn;
1769744947dcSTom Erickson 
1770744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1771744947dcSTom Erickson 	dn = DB_DNODE(db);
1772fa9e4066Sahrens 
1773fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
177499653d4eSeschrock 	/* add 1 for dnode space */
177599653d4eSeschrock 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
177699653d4eSeschrock 	    SPA_MINBLOCKSHIFT) + 1;
1777744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1778fa9e4066Sahrens }
1779fa9e4066Sahrens 
1780fa9e4066Sahrens void
1781fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
1782fa9e4066Sahrens {
1783fa9e4066Sahrens 	uint64_t *buf = vbuf;
1784fa9e4066Sahrens 	size_t count = size >> 3;
1785fa9e4066Sahrens 	int i;
1786fa9e4066Sahrens 
1787fa9e4066Sahrens 	ASSERT((size & 7) == 0);
1788fa9e4066Sahrens 
1789fa9e4066Sahrens 	for (i = 0; i < count; i++)
1790fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
1791fa9e4066Sahrens }
1792fa9e4066Sahrens 
1793fa9e4066Sahrens void
1794fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
1795fa9e4066Sahrens {
1796fa9e4066Sahrens 	uint32_t *buf = vbuf;
1797fa9e4066Sahrens 	size_t count = size >> 2;
1798fa9e4066Sahrens 	int i;
1799fa9e4066Sahrens 
1800fa9e4066Sahrens 	ASSERT((size & 3) == 0);
1801fa9e4066Sahrens 
1802fa9e4066Sahrens 	for (i = 0; i < count; i++)
1803fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
1804fa9e4066Sahrens }
1805fa9e4066Sahrens 
1806fa9e4066Sahrens void
1807fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
1808fa9e4066Sahrens {
1809fa9e4066Sahrens 	uint16_t *buf = vbuf;
1810fa9e4066Sahrens 	size_t count = size >> 1;
1811fa9e4066Sahrens 	int i;
1812fa9e4066Sahrens 
1813fa9e4066Sahrens 	ASSERT((size & 1) == 0);
1814fa9e4066Sahrens 
1815fa9e4066Sahrens 	for (i = 0; i < count; i++)
1816fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
1817fa9e4066Sahrens }
1818fa9e4066Sahrens 
1819fa9e4066Sahrens /* ARGSUSED */
1820fa9e4066Sahrens void
1821fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
1822fa9e4066Sahrens {
1823fa9e4066Sahrens }
1824fa9e4066Sahrens 
1825fa9e4066Sahrens void
1826fa9e4066Sahrens dmu_init(void)
1827fa9e4066Sahrens {
18283f9d6ad7SLin Ling 	zfs_dbgmsg_init();
1829744947dcSTom Erickson 	sa_cache_init();
1830744947dcSTom Erickson 	xuio_stat_init();
1831744947dcSTom Erickson 	dmu_objset_init();
1832fa9e4066Sahrens 	dnode_init();
1833744947dcSTom Erickson 	dbuf_init();
18347cbf8b43SRich Morris 	zfetch_init();
1835fa94a07fSbrendan 	l2arc_init();
1836ce636f8bSMatthew Ahrens 	arc_init();
1837fa9e4066Sahrens }
1838fa9e4066Sahrens 
1839fa9e4066Sahrens void
1840fa9e4066Sahrens dmu_fini(void)
1841fa9e4066Sahrens {
18423e30c24aSWill Andrews 	arc_fini(); /* arc depends on l2arc, so arc must go first */
1843ce636f8bSMatthew Ahrens 	l2arc_fini();
18447cbf8b43SRich Morris 	zfetch_fini();
1845fa9e4066Sahrens 	dbuf_fini();
1846744947dcSTom Erickson 	dnode_fini();
1847744947dcSTom Erickson 	dmu_objset_fini();
1848c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_stat_fini();
18490a586ceaSMark Shellenbaum 	sa_cache_fini();
18503f9d6ad7SLin Ling 	zfs_dbgmsg_fini();
1851fa9e4066Sahrens }
1852