xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision eb721827)
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.
238df0bcf0SPaul Dagnelie  * Copyright (c) 2011, 2016 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. */
27*eb721827SAlek Pinchuk /* Copyright 2016 Nexenta Systems, Inc. All rights reserved. */
28aad02571SSaso Kiselkov 
29fa9e4066Sahrens #include <sys/dmu.h>
30fa9e4066Sahrens #include <sys/dmu_impl.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/dbuf.h>
33fa9e4066Sahrens #include <sys/dnode.h>
34fa9e4066Sahrens #include <sys/zfs_context.h>
35fa9e4066Sahrens #include <sys/dmu_objset.h>
36fa9e4066Sahrens #include <sys/dmu_traverse.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/dsl_pool.h>
401d452cf5Sahrens #include <sys/dsl_synctask.h>
41a2eea2e1Sahrens #include <sys/dsl_prop.h>
42fa9e4066Sahrens #include <sys/dmu_zfetch.h>
43fa9e4066Sahrens #include <sys/zfs_ioctl.h>
44fa9e4066Sahrens #include <sys/zap.h>
45ea8dc4b6Seschrock #include <sys/zio_checksum.h>
4680901aeaSGeorge Wilson #include <sys/zio_compress.h>
470a586ceaSMark Shellenbaum #include <sys/sa.h>
48b8289d24SDaniil Lunev #include <sys/zfeature.h>
4944eda4d7Smaybee #ifdef _KERNEL
5044eda4d7Smaybee #include <sys/vmsystm.h>
510fab61baSJonathan W Adams #include <sys/zfs_znode.h>
5244eda4d7Smaybee #endif
53fa9e4066Sahrens 
5480901aeaSGeorge Wilson /*
5580901aeaSGeorge Wilson  * Enable/disable nopwrite feature.
5680901aeaSGeorge Wilson  */
5780901aeaSGeorge Wilson int zfs_nopwrite_enabled = 1;
5880901aeaSGeorge Wilson 
59fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
60ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"unallocated"		},
61ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"object directory"	},
62ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"object array"		},
63ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"packed nvlist"		},
64ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"packed nvlist size"	},
65ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj"			},
66ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj header"		},
67ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map header"	},
68ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA space map"		},
69ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"ZIL intent log"	},
70ad135b5dSChristopher Siden 	{	DMU_BSWAP_DNODE,	TRUE,	"DMU dnode"		},
71ad135b5dSChristopher Siden 	{	DMU_BSWAP_OBJSET,	TRUE,	"DMU objset"		},
72ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL directory"		},
73ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL directory child map"},
74ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset snap map"	},
75ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL props"		},
76ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL dataset"		},
77ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZNODE,	TRUE,	"ZFS znode"		},
78ad135b5dSChristopher Siden 	{	DMU_BSWAP_OLDACL,	TRUE,	"ZFS V0 ACL"		},
79ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"ZFS plain file"	},
80ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS directory"		},
81ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS master node"	},
82ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS delete queue"	},
83ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"zvol object"		},
84ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"zvol prop"		},
85ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"other uint8[]"		},
86ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	FALSE,	"other uint64[]"	},
87ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"other ZAP"		},
88ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"persistent error log"	},
89ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"SPA history"		},
90ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"SPA history offsets"	},
91ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"Pool properties"	},
92ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL permissions"	},
93ad135b5dSChristopher Siden 	{	DMU_BSWAP_ACL,		TRUE,	"ZFS ACL"		},
94ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"ZFS SYSACL"		},
95ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"FUID table"		},
96ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"FUID table size"	},
97ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dataset next clones"},
98ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan work queue"	},
99ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group used"	},
100ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"ZFS user/group quota"	},
101ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"snapshot refcount tags"},
102ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT ZAP algorithm"	},
103ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DDT statistics"	},
104ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	TRUE,	"System attributes"	},
105ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA master node"	},
106ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr registration"	},
107ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"SA attr layouts"	},
108ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"scan translations"	},
109ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT8,	FALSE,	"deduplicated block"	},
110ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL deadlist map"	},
111ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"DSL deadlist map hdr"	},
112ad135b5dSChristopher Siden 	{	DMU_BSWAP_ZAP,		TRUE,	"DSL dir clones"	},
113ad135b5dSChristopher Siden 	{	DMU_BSWAP_UINT64,	TRUE,	"bpobj subobj"		}
114ad135b5dSChristopher Siden };
115ad135b5dSChristopher Siden 
116ad135b5dSChristopher Siden const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
117ad135b5dSChristopher Siden 	{	byteswap_uint8_array,	"uint8"		},
118ad135b5dSChristopher Siden 	{	byteswap_uint16_array,	"uint16"	},
119ad135b5dSChristopher Siden 	{	byteswap_uint32_array,	"uint32"	},
120ad135b5dSChristopher Siden 	{	byteswap_uint64_array,	"uint64"	},
121ad135b5dSChristopher Siden 	{	zap_byteswap,		"zap"		},
122ad135b5dSChristopher Siden 	{	dnode_buf_byteswap,	"dnode"		},
123ad135b5dSChristopher Siden 	{	dmu_objset_byteswap,	"objset"	},
124ad135b5dSChristopher Siden 	{	zfs_znode_byteswap,	"znode"		},
125ad135b5dSChristopher Siden 	{	zfs_oldacl_byteswap,	"oldacl"	},
126ad135b5dSChristopher Siden 	{	zfs_acl_byteswap,	"acl"		}
1273f9d6ad7SLin Ling };
128fa9e4066Sahrens 
12979d72832SMatthew Ahrens int
13079d72832SMatthew Ahrens dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset,
13179d72832SMatthew Ahrens     void *tag, dmu_buf_t **dbp)
13279d72832SMatthew Ahrens {
13379d72832SMatthew Ahrens 	uint64_t blkid;
13479d72832SMatthew Ahrens 	dmu_buf_impl_t *db;
13579d72832SMatthew Ahrens 
13679d72832SMatthew Ahrens 	blkid = dbuf_whichblock(dn, 0, offset);
13779d72832SMatthew Ahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
13879d72832SMatthew Ahrens 	db = dbuf_hold(dn, blkid, tag);
13979d72832SMatthew Ahrens 	rw_exit(&dn->dn_struct_rwlock);
14079d72832SMatthew Ahrens 
14179d72832SMatthew Ahrens 	if (db == NULL) {
14279d72832SMatthew Ahrens 		*dbp = NULL;
14379d72832SMatthew Ahrens 		return (SET_ERROR(EIO));
14479d72832SMatthew Ahrens 	}
14579d72832SMatthew Ahrens 
14679d72832SMatthew Ahrens 	*dbp = &db->db;
14779d72832SMatthew Ahrens 	return (0);
14879d72832SMatthew Ahrens }
149fa9e4066Sahrens int
1505d7b4d43SMatthew Ahrens dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,
1515d7b4d43SMatthew Ahrens     void *tag, dmu_buf_t **dbp)
152fa9e4066Sahrens {
153fa9e4066Sahrens 	dnode_t *dn;
154fa9e4066Sahrens 	uint64_t blkid;
155fa9e4066Sahrens 	dmu_buf_impl_t *db;
156ea8dc4b6Seschrock 	int err;
157fa9e4066Sahrens 
158503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
159ea8dc4b6Seschrock 	if (err)
160ea8dc4b6Seschrock 		return (err);
161a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
162fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
163ea8dc4b6Seschrock 	db = dbuf_hold(dn, blkid, tag);
164fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1655d7b4d43SMatthew Ahrens 	dnode_rele(dn, FTAG);
1665d7b4d43SMatthew Ahrens 
167ea8dc4b6Seschrock 	if (db == NULL) {
1685d7b4d43SMatthew Ahrens 		*dbp = NULL;
1695d7b4d43SMatthew Ahrens 		return (SET_ERROR(EIO));
1705d7b4d43SMatthew Ahrens 	}
1715d7b4d43SMatthew Ahrens 
1725d7b4d43SMatthew Ahrens 	*dbp = &db->db;
1735d7b4d43SMatthew Ahrens 	return (err);
1745d7b4d43SMatthew Ahrens }
1755d7b4d43SMatthew Ahrens 
17679d72832SMatthew Ahrens int
17779d72832SMatthew Ahrens dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset,
17879d72832SMatthew Ahrens     void *tag, dmu_buf_t **dbp, int flags)
17979d72832SMatthew Ahrens {
18079d72832SMatthew Ahrens 	int err;
18179d72832SMatthew Ahrens 	int db_flags = DB_RF_CANFAIL;
18279d72832SMatthew Ahrens 
18379d72832SMatthew Ahrens 	if (flags & DMU_READ_NO_PREFETCH)
18479d72832SMatthew Ahrens 		db_flags |= DB_RF_NOPREFETCH;
18579d72832SMatthew Ahrens 
18679d72832SMatthew Ahrens 	err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp);
18779d72832SMatthew Ahrens 	if (err == 0) {
18879d72832SMatthew Ahrens 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
18979d72832SMatthew Ahrens 		err = dbuf_read(db, NULL, db_flags);
19079d72832SMatthew Ahrens 		if (err != 0) {
19179d72832SMatthew Ahrens 			dbuf_rele(db, tag);
19279d72832SMatthew Ahrens 			*dbp = NULL;
19379d72832SMatthew Ahrens 		}
19479d72832SMatthew Ahrens 	}
19579d72832SMatthew Ahrens 
19679d72832SMatthew Ahrens 	return (err);
19779d72832SMatthew Ahrens }
19879d72832SMatthew Ahrens 
1995d7b4d43SMatthew Ahrens int
2005d7b4d43SMatthew Ahrens dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
2015d7b4d43SMatthew Ahrens     void *tag, dmu_buf_t **dbp, int flags)
2025d7b4d43SMatthew Ahrens {
2035d7b4d43SMatthew Ahrens 	int err;
2045d7b4d43SMatthew Ahrens 	int db_flags = DB_RF_CANFAIL;
2055d7b4d43SMatthew Ahrens 
2065d7b4d43SMatthew Ahrens 	if (flags & DMU_READ_NO_PREFETCH)
2075d7b4d43SMatthew Ahrens 		db_flags |= DB_RF_NOPREFETCH;
2085d7b4d43SMatthew Ahrens 
2095d7b4d43SMatthew Ahrens 	err = dmu_buf_hold_noread(os, object, offset, tag, dbp);
2105d7b4d43SMatthew Ahrens 	if (err == 0) {
2115d7b4d43SMatthew Ahrens 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
21247cb52daSJeff Bonwick 		err = dbuf_read(db, NULL, db_flags);
2135d7b4d43SMatthew Ahrens 		if (err != 0) {
214ea8dc4b6Seschrock 			dbuf_rele(db, tag);
2155d7b4d43SMatthew Ahrens 			*dbp = NULL;
216ea8dc4b6Seschrock 		}
217ea8dc4b6Seschrock 	}
218fa9e4066Sahrens 
219ea8dc4b6Seschrock 	return (err);
220fa9e4066Sahrens }
221fa9e4066Sahrens 
222fa9e4066Sahrens int
223fa9e4066Sahrens dmu_bonus_max(void)
224fa9e4066Sahrens {
225fa9e4066Sahrens 	return (DN_MAX_BONUSLEN);
226fa9e4066Sahrens }
227fa9e4066Sahrens 
2281934e92fSmaybee int
229744947dcSTom Erickson dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
2301934e92fSmaybee {
231744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
232744947dcSTom Erickson 	dnode_t *dn;
233744947dcSTom Erickson 	int error;
2341934e92fSmaybee 
235744947dcSTom Erickson 	DB_DNODE_ENTER(db);
236744947dcSTom Erickson 	dn = DB_DNODE(db);
237744947dcSTom Erickson 
238744947dcSTom Erickson 	if (dn->dn_bonus != db) {
239be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
240744947dcSTom Erickson 	} else if (newsize < 0 || newsize > db_fake->db_size) {
241be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
242744947dcSTom Erickson 	} else {
243744947dcSTom Erickson 		dnode_setbonuslen(dn, newsize, tx);
244744947dcSTom Erickson 		error = 0;
245744947dcSTom Erickson 	}
246744947dcSTom Erickson 
247744947dcSTom Erickson 	DB_DNODE_EXIT(db);
248744947dcSTom Erickson 	return (error);
2491934e92fSmaybee }
2501934e92fSmaybee 
2510a586ceaSMark Shellenbaum int
252744947dcSTom Erickson dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
2530a586ceaSMark Shellenbaum {
254744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
255744947dcSTom Erickson 	dnode_t *dn;
256744947dcSTom Erickson 	int error;
2570a586ceaSMark Shellenbaum 
258744947dcSTom Erickson 	DB_DNODE_ENTER(db);
259744947dcSTom Erickson 	dn = DB_DNODE(db);
260744947dcSTom Erickson 
261ad135b5dSChristopher Siden 	if (!DMU_OT_IS_VALID(type)) {
262be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
263744947dcSTom Erickson 	} else if (dn->dn_bonus != db) {
264be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
265744947dcSTom Erickson 	} else {
266744947dcSTom Erickson 		dnode_setbonus_type(dn, type, tx);
267744947dcSTom Erickson 		error = 0;
268744947dcSTom Erickson 	}
2690a586ceaSMark Shellenbaum 
270744947dcSTom Erickson 	DB_DNODE_EXIT(db);
271744947dcSTom Erickson 	return (error);
272744947dcSTom Erickson }
2730a586ceaSMark Shellenbaum 
274744947dcSTom Erickson dmu_object_type_t
275744947dcSTom Erickson dmu_get_bonustype(dmu_buf_t *db_fake)
276744947dcSTom Erickson {
277744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
278744947dcSTom Erickson 	dnode_t *dn;
279744947dcSTom Erickson 	dmu_object_type_t type;
280744947dcSTom Erickson 
281744947dcSTom Erickson 	DB_DNODE_ENTER(db);
282744947dcSTom Erickson 	dn = DB_DNODE(db);
283744947dcSTom Erickson 	type = dn->dn_bonustype;
284744947dcSTom Erickson 	DB_DNODE_EXIT(db);
285744947dcSTom Erickson 
286744947dcSTom Erickson 	return (type);
2870a586ceaSMark Shellenbaum }
2880a586ceaSMark Shellenbaum 
2890a586ceaSMark Shellenbaum int
2900a586ceaSMark Shellenbaum dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
2910a586ceaSMark Shellenbaum {
2920a586ceaSMark Shellenbaum 	dnode_t *dn;
2930a586ceaSMark Shellenbaum 	int error;
2940a586ceaSMark Shellenbaum 
2950a586ceaSMark Shellenbaum 	error = dnode_hold(os, object, FTAG, &dn);
2960a586ceaSMark Shellenbaum 	dbuf_rm_spill(dn, tx);
29706e0070dSMark Shellenbaum 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
29806e0070dSMark Shellenbaum 	dnode_rm_spill(dn, tx);
29906e0070dSMark Shellenbaum 	rw_exit(&dn->dn_struct_rwlock);
3000a586ceaSMark Shellenbaum 	dnode_rele(dn, FTAG);
3010a586ceaSMark Shellenbaum 	return (error);
3020a586ceaSMark Shellenbaum }
3030a586ceaSMark Shellenbaum 
304fa9e4066Sahrens /*
305ea8dc4b6Seschrock  * returns ENOENT, EIO, or 0.
306fa9e4066Sahrens  */
307ea8dc4b6Seschrock int
308ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
309fa9e4066Sahrens {
310ea8dc4b6Seschrock 	dnode_t *dn;
311fa9e4066Sahrens 	dmu_buf_impl_t *db;
3121934e92fSmaybee 	int error;
313fa9e4066Sahrens 
314503ad85cSMatthew Ahrens 	error = dnode_hold(os, object, FTAG, &dn);
3151934e92fSmaybee 	if (error)
3161934e92fSmaybee 		return (error);
317fa9e4066Sahrens 
318ea8dc4b6Seschrock 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
319ea8dc4b6Seschrock 	if (dn->dn_bonus == NULL) {
320fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
321ea8dc4b6Seschrock 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
322ea8dc4b6Seschrock 		if (dn->dn_bonus == NULL)
3231934e92fSmaybee 			dbuf_create_bonus(dn);
324fa9e4066Sahrens 	}
325ea8dc4b6Seschrock 	db = dn->dn_bonus;
3261934e92fSmaybee 
3271934e92fSmaybee 	/* as long as the bonus buf is held, the dnode will be held */
328744947dcSTom Erickson 	if (refcount_add(&db->db_holds, tag) == 1) {
3291934e92fSmaybee 		VERIFY(dnode_add_ref(dn, db));
330640c1670SJosef 'Jeff' Sipek 		atomic_inc_32(&dn->dn_dbufs_count);
331744947dcSTom Erickson 	}
332744947dcSTom Erickson 
333744947dcSTom Erickson 	/*
334744947dcSTom Erickson 	 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
335744947dcSTom Erickson 	 * hold and incrementing the dbuf count to ensure that dnode_move() sees
336744947dcSTom Erickson 	 * a dnode hold for every dbuf.
337744947dcSTom Erickson 	 */
338744947dcSTom Erickson 	rw_exit(&dn->dn_struct_rwlock);
3391934e92fSmaybee 
340fa9e4066Sahrens 	dnode_rele(dn, FTAG);
341ea8dc4b6Seschrock 
34247cb52daSJeff Bonwick 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
343ea8dc4b6Seschrock 
344ea8dc4b6Seschrock 	*dbp = &db->db;
345ea8dc4b6Seschrock 	return (0);
346fa9e4066Sahrens }
347fa9e4066Sahrens 
3480a586ceaSMark Shellenbaum /*
3490a586ceaSMark Shellenbaum  * returns ENOENT, EIO, or 0.
3500a586ceaSMark Shellenbaum  *
3510a586ceaSMark Shellenbaum  * This interface will allocate a blank spill dbuf when a spill blk
3520a586ceaSMark Shellenbaum  * doesn't already exist on the dnode.
3530a586ceaSMark Shellenbaum  *
3540a586ceaSMark Shellenbaum  * if you only want to find an already existing spill db, then
3550a586ceaSMark Shellenbaum  * dmu_spill_hold_existing() should be used.
3560a586ceaSMark Shellenbaum  */
3570a586ceaSMark Shellenbaum int
3580a586ceaSMark Shellenbaum dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
3590a586ceaSMark Shellenbaum {
3600a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
3610a586ceaSMark Shellenbaum 	int err;
3620a586ceaSMark Shellenbaum 
3630a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3640a586ceaSMark Shellenbaum 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
3650a586ceaSMark Shellenbaum 
3660a586ceaSMark Shellenbaum 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
3670a586ceaSMark Shellenbaum 
3680a586ceaSMark Shellenbaum 	if ((flags & DB_RF_HAVESTRUCT) == 0)
3690a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
3700a586ceaSMark Shellenbaum 
3710a586ceaSMark Shellenbaum 	ASSERT(db != NULL);
3721d8ccc7bSMark Shellenbaum 	err = dbuf_read(db, NULL, flags);
3731d8ccc7bSMark Shellenbaum 	if (err == 0)
3741d8ccc7bSMark Shellenbaum 		*dbp = &db->db;
3751d8ccc7bSMark Shellenbaum 	else
3761d8ccc7bSMark Shellenbaum 		dbuf_rele(db, tag);
3770a586ceaSMark Shellenbaum 	return (err);
3780a586ceaSMark Shellenbaum }
3790a586ceaSMark Shellenbaum 
3800a586ceaSMark Shellenbaum int
3810a586ceaSMark Shellenbaum dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
3820a586ceaSMark Shellenbaum {
383744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
384744947dcSTom Erickson 	dnode_t *dn;
3850a586ceaSMark Shellenbaum 	int err;
3860a586ceaSMark Shellenbaum 
387744947dcSTom Erickson 	DB_DNODE_ENTER(db);
388744947dcSTom Erickson 	dn = DB_DNODE(db);
389744947dcSTom Erickson 
390744947dcSTom Erickson 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
391be6fd75aSMatthew Ahrens 		err = SET_ERROR(EINVAL);
392744947dcSTom Erickson 	} else {
393744947dcSTom Erickson 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
394744947dcSTom Erickson 
395744947dcSTom Erickson 		if (!dn->dn_have_spill) {
396be6fd75aSMatthew Ahrens 			err = SET_ERROR(ENOENT);
397744947dcSTom Erickson 		} else {
398744947dcSTom Erickson 			err = dmu_spill_hold_by_dnode(dn,
399744947dcSTom Erickson 			    DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
400744947dcSTom Erickson 		}
4010a586ceaSMark Shellenbaum 
4020a586ceaSMark Shellenbaum 		rw_exit(&dn->dn_struct_rwlock);
4030a586ceaSMark Shellenbaum 	}
404744947dcSTom Erickson 
405744947dcSTom Erickson 	DB_DNODE_EXIT(db);
4060a586ceaSMark Shellenbaum 	return (err);
4070a586ceaSMark Shellenbaum }
4080a586ceaSMark Shellenbaum 
4090a586ceaSMark Shellenbaum int
4100a586ceaSMark Shellenbaum dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
4110a586ceaSMark Shellenbaum {
412744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
413744947dcSTom Erickson 	dnode_t *dn;
414744947dcSTom Erickson 	int err;
415744947dcSTom Erickson 
416744947dcSTom Erickson 	DB_DNODE_ENTER(db);
417744947dcSTom Erickson 	dn = DB_DNODE(db);
418744947dcSTom Erickson 	err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
419744947dcSTom Erickson 	DB_DNODE_EXIT(db);
420744947dcSTom Erickson 
421744947dcSTom Erickson 	return (err);
4220a586ceaSMark Shellenbaum }
4230a586ceaSMark Shellenbaum 
42413506d1eSmaybee /*
42513506d1eSmaybee  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
42613506d1eSmaybee  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
42713506d1eSmaybee  * and can induce severe lock contention when writing to several files
42813506d1eSmaybee  * whose dnodes are in the same block.
42913506d1eSmaybee  */
43013506d1eSmaybee static int
4317bfdf011SNeil Perrin dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
432cf6106c8SMatthew Ahrens     boolean_t read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
433fa9e4066Sahrens {
434fa9e4066Sahrens 	dmu_buf_t **dbp;
435fa9e4066Sahrens 	uint64_t blkid, nblks, i;
4367bfdf011SNeil Perrin 	uint32_t dbuf_flags;
437ea8dc4b6Seschrock 	int err;
438ea8dc4b6Seschrock 	zio_t *zio;
439ea8dc4b6Seschrock 
440ea8dc4b6Seschrock 	ASSERT(length <= DMU_MAX_ACCESS);
441fa9e4066Sahrens 
442cf6106c8SMatthew Ahrens 	/*
443cf6106c8SMatthew Ahrens 	 * Note: We directly notify the prefetch code of this read, so that
444cf6106c8SMatthew Ahrens 	 * we can tell it about the multi-block read.  dbuf_read() only knows
445cf6106c8SMatthew Ahrens 	 * about the one block it is accessing.
446cf6106c8SMatthew Ahrens 	 */
447cf6106c8SMatthew Ahrens 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT |
448cf6106c8SMatthew Ahrens 	    DB_RF_NOPREFETCH;
449ea8dc4b6Seschrock 
450fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
451fa9e4066Sahrens 	if (dn->dn_datablkshift) {
452fa9e4066Sahrens 		int blkshift = dn->dn_datablkshift;
453cf6106c8SMatthew Ahrens 		nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
454cf6106c8SMatthew Ahrens 		    P2ALIGN(offset, 1ULL << blkshift)) >> blkshift;
455fa9e4066Sahrens 	} else {
4560125049cSahrens 		if (offset + length > dn->dn_datablksz) {
4570125049cSahrens 			zfs_panic_recover("zfs: accessing past end of object "
4580125049cSahrens 			    "%llx/%llx (size=%u access=%llu+%llu)",
4590125049cSahrens 			    (longlong_t)dn->dn_objset->
4600125049cSahrens 			    os_dsl_dataset->ds_object,
4610125049cSahrens 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
4620125049cSahrens 			    (longlong_t)offset, (longlong_t)length);
463c87b8fc5SMark J Musante 			rw_exit(&dn->dn_struct_rwlock);
464be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
4650125049cSahrens 		}
466fa9e4066Sahrens 		nblks = 1;
467fa9e4066Sahrens 	}
468ea8dc4b6Seschrock 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
469fa9e4066Sahrens 
470e14bb325SJeff Bonwick 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
471a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
472fa9e4066Sahrens 	for (i = 0; i < nblks; i++) {
473cf6106c8SMatthew Ahrens 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);
474ea8dc4b6Seschrock 		if (db == NULL) {
475ea8dc4b6Seschrock 			rw_exit(&dn->dn_struct_rwlock);
476ea8dc4b6Seschrock 			dmu_buf_rele_array(dbp, nblks, tag);
477ea8dc4b6Seschrock 			zio_nowait(zio);
478be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
479ea8dc4b6Seschrock 		}
480cf6106c8SMatthew Ahrens 
481ea8dc4b6Seschrock 		/* initiate async i/o */
482cf6106c8SMatthew Ahrens 		if (read)
4837bfdf011SNeil Perrin 			(void) dbuf_read(db, zio, dbuf_flags);
484ea8dc4b6Seschrock 		dbp[i] = &db->db;
485fa9e4066Sahrens 	}
486cf6106c8SMatthew Ahrens 
487cb92f413SAlexander Motin 	if ((flags & DMU_READ_NO_PREFETCH) == 0 &&
488cb92f413SAlexander Motin 	    DNODE_META_IS_CACHEABLE(dn) && length <= zfetch_array_rd_sz) {
489cb92f413SAlexander Motin 		dmu_zfetch(&dn->dn_zfetch, blkid, nblks,
490cb92f413SAlexander Motin 		    read && DNODE_IS_CACHEABLE(dn));
491cf6106c8SMatthew Ahrens 	}
492fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
493fa9e4066Sahrens 
494ea8dc4b6Seschrock 	/* wait for async i/o */
495ea8dc4b6Seschrock 	err = zio_wait(zio);
496ea8dc4b6Seschrock 	if (err) {
497ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, nblks, tag);
498ea8dc4b6Seschrock 		return (err);
499ea8dc4b6Seschrock 	}
500fa9e4066Sahrens 
501ea8dc4b6Seschrock 	/* wait for other io to complete */
502ea8dc4b6Seschrock 	if (read) {
503ea8dc4b6Seschrock 		for (i = 0; i < nblks; i++) {
504ea8dc4b6Seschrock 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
505ea8dc4b6Seschrock 			mutex_enter(&db->db_mtx);
506ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
507ea8dc4b6Seschrock 			    db->db_state == DB_FILL)
508ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
509ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
510be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
511ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
512ea8dc4b6Seschrock 			if (err) {
513ea8dc4b6Seschrock 				dmu_buf_rele_array(dbp, nblks, tag);
514ea8dc4b6Seschrock 				return (err);
515ea8dc4b6Seschrock 			}
516ea8dc4b6Seschrock 		}
517ea8dc4b6Seschrock 	}
518fa9e4066Sahrens 
519ea8dc4b6Seschrock 	*numbufsp = nblks;
520ea8dc4b6Seschrock 	*dbpp = dbp;
521ea8dc4b6Seschrock 	return (0);
522fa9e4066Sahrens }
523fa9e4066Sahrens 
524a2eea2e1Sahrens static int
52513506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
52613506d1eSmaybee     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
52713506d1eSmaybee {
52813506d1eSmaybee 	dnode_t *dn;
52913506d1eSmaybee 	int err;
53013506d1eSmaybee 
531503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
53213506d1eSmaybee 	if (err)
53313506d1eSmaybee 		return (err);
53413506d1eSmaybee 
53513506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
5367bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
53713506d1eSmaybee 
53813506d1eSmaybee 	dnode_rele(dn, FTAG);
53913506d1eSmaybee 
54013506d1eSmaybee 	return (err);
54113506d1eSmaybee }
54213506d1eSmaybee 
54313506d1eSmaybee int
544744947dcSTom Erickson dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
545cf6106c8SMatthew Ahrens     uint64_t length, boolean_t read, void *tag, int *numbufsp,
546cf6106c8SMatthew Ahrens     dmu_buf_t ***dbpp)
54713506d1eSmaybee {
548744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
549744947dcSTom Erickson 	dnode_t *dn;
55013506d1eSmaybee 	int err;
55113506d1eSmaybee 
552744947dcSTom Erickson 	DB_DNODE_ENTER(db);
553744947dcSTom Erickson 	dn = DB_DNODE(db);
55413506d1eSmaybee 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
5557bfdf011SNeil Perrin 	    numbufsp, dbpp, DMU_READ_PREFETCH);
556744947dcSTom Erickson 	DB_DNODE_EXIT(db);
55713506d1eSmaybee 
55813506d1eSmaybee 	return (err);
55913506d1eSmaybee }
56013506d1eSmaybee 
561fa9e4066Sahrens void
562ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
563fa9e4066Sahrens {
564fa9e4066Sahrens 	int i;
565fa9e4066Sahrens 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
566fa9e4066Sahrens 
567fa9e4066Sahrens 	if (numbufs == 0)
568fa9e4066Sahrens 		return;
569fa9e4066Sahrens 
570ea8dc4b6Seschrock 	for (i = 0; i < numbufs; i++) {
571ea8dc4b6Seschrock 		if (dbp[i])
572ea8dc4b6Seschrock 			dbuf_rele(dbp[i], tag);
573ea8dc4b6Seschrock 	}
574fa9e4066Sahrens 
575fa9e4066Sahrens 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
576fa9e4066Sahrens }
577fa9e4066Sahrens 
57869962b56SMatthew Ahrens /*
579a2cdcdd2SPaul Dagnelie  * Issue prefetch i/os for the given blocks.  If level is greater than 0, the
580a2cdcdd2SPaul Dagnelie  * indirect blocks prefeteched will be those that point to the blocks containing
581a2cdcdd2SPaul Dagnelie  * the data starting at offset, and continuing to offset + len.
58269962b56SMatthew Ahrens  *
583a2cdcdd2SPaul Dagnelie  * Note that if the indirect blocks above the blocks being prefetched are not in
584a2cdcdd2SPaul Dagnelie  * cache, they will be asychronously read in.
58569962b56SMatthew Ahrens  */
586fa9e4066Sahrens void
587a2cdcdd2SPaul Dagnelie dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,
588a2cdcdd2SPaul Dagnelie     uint64_t len, zio_priority_t pri)
589fa9e4066Sahrens {
590fa9e4066Sahrens 	dnode_t *dn;
591fa9e4066Sahrens 	uint64_t blkid;
59269962b56SMatthew Ahrens 	int nblks, err;
593fa9e4066Sahrens 
594fa9e4066Sahrens 	if (len == 0) {  /* they're interested in the bonus buffer */
595744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
596fa9e4066Sahrens 
597fa9e4066Sahrens 		if (object == 0 || object >= DN_MAX_OBJECT)
598fa9e4066Sahrens 			return;
599fa9e4066Sahrens 
600fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
601a2cdcdd2SPaul Dagnelie 		blkid = dbuf_whichblock(dn, level,
602a2cdcdd2SPaul Dagnelie 		    object * sizeof (dnode_phys_t));
603a2cdcdd2SPaul Dagnelie 		dbuf_prefetch(dn, level, blkid, pri, 0);
604fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
605fa9e4066Sahrens 		return;
606fa9e4066Sahrens 	}
607fa9e4066Sahrens 
608fa9e4066Sahrens 	/*
609fa9e4066Sahrens 	 * XXX - Note, if the dnode for the requested object is not
610fa9e4066Sahrens 	 * already cached, we will do a *synchronous* read in the
611fa9e4066Sahrens 	 * dnode_hold() call.  The same is true for any indirects.
612fa9e4066Sahrens 	 */
613503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
614ea8dc4b6Seschrock 	if (err != 0)
615fa9e4066Sahrens 		return;
616fa9e4066Sahrens 
617fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
618a2cdcdd2SPaul Dagnelie 	/*
619a2cdcdd2SPaul Dagnelie 	 * offset + len - 1 is the last byte we want to prefetch for, and offset
620a2cdcdd2SPaul Dagnelie 	 * is the first.  Then dbuf_whichblk(dn, level, off + len - 1) is the
621a2cdcdd2SPaul Dagnelie 	 * last block we want to prefetch, and dbuf_whichblock(dn, level,
622a2cdcdd2SPaul Dagnelie 	 * offset)  is the first.  Then the number we need to prefetch is the
623a2cdcdd2SPaul Dagnelie 	 * last - first + 1.
624a2cdcdd2SPaul Dagnelie 	 */
625a2cdcdd2SPaul Dagnelie 	if (level > 0 || dn->dn_datablkshift != 0) {
626a2cdcdd2SPaul Dagnelie 		nblks = dbuf_whichblock(dn, level, offset + len - 1) -
627a2cdcdd2SPaul Dagnelie 		    dbuf_whichblock(dn, level, offset) + 1;
628fa9e4066Sahrens 	} else {
629fa9e4066Sahrens 		nblks = (offset < dn->dn_datablksz);
630fa9e4066Sahrens 	}
631fa9e4066Sahrens 
632fa9e4066Sahrens 	if (nblks != 0) {
633a2cdcdd2SPaul Dagnelie 		blkid = dbuf_whichblock(dn, level, offset);
63469962b56SMatthew Ahrens 		for (int i = 0; i < nblks; i++)
635a2cdcdd2SPaul Dagnelie 			dbuf_prefetch(dn, level, blkid + i, pri, 0);
636fa9e4066Sahrens 	}
637fa9e4066Sahrens 
638fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
639fa9e4066Sahrens 
640fa9e4066Sahrens 	dnode_rele(dn, FTAG);
641fa9e4066Sahrens }
642fa9e4066Sahrens 
64376256205SMark Maybee /*
64476256205SMark Maybee  * Get the next "chunk" of file data to free.  We traverse the file from
64576256205SMark Maybee  * the end so that the file gets shorter over time (if we crashes in the
64676256205SMark Maybee  * middle, this will leave us in a better state).  We find allocated file
64776256205SMark Maybee  * data by simply searching the allocated level 1 indirects.
648713d6c20SMatthew Ahrens  *
649713d6c20SMatthew Ahrens  * On input, *start should be the first offset that does not need to be
650713d6c20SMatthew Ahrens  * freed (e.g. "offset + length").  On return, *start will be the first
651713d6c20SMatthew Ahrens  * offset that should be freed.
65276256205SMark Maybee  */
653cdb0ab79Smaybee static int
654713d6c20SMatthew Ahrens get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum)
655cdb0ab79Smaybee {
656713d6c20SMatthew Ahrens 	uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
657713d6c20SMatthew Ahrens 	/* bytes of data covered by a level-1 indirect block */
65876256205SMark Maybee 	uint64_t iblkrange =
6591c8564a7SMark Maybee 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
660cdb0ab79Smaybee 
661713d6c20SMatthew Ahrens 	ASSERT3U(minimum, <=, *start);
662cdb0ab79Smaybee 
663713d6c20SMatthew Ahrens 	if (*start - minimum <= iblkrange * maxblks) {
664713d6c20SMatthew Ahrens 		*start = minimum;
665cdb0ab79Smaybee 		return (0);
666cdb0ab79Smaybee 	}
66776256205SMark Maybee 	ASSERT(ISP2(iblkrange));
668cdb0ab79Smaybee 
669713d6c20SMatthew Ahrens 	for (uint64_t blks = 0; *start > minimum && blks < maxblks; blks++) {
6701c8564a7SMark Maybee 		int err;
671cdb0ab79Smaybee 
672713d6c20SMatthew Ahrens 		/*
673713d6c20SMatthew Ahrens 		 * dnode_next_offset(BACKWARDS) will find an allocated L1
674713d6c20SMatthew Ahrens 		 * indirect block at or before the input offset.  We must
675713d6c20SMatthew Ahrens 		 * decrement *start so that it is at the end of the region
676713d6c20SMatthew Ahrens 		 * to search.
677713d6c20SMatthew Ahrens 		 */
678713d6c20SMatthew Ahrens 		(*start)--;
679cdb0ab79Smaybee 		err = dnode_next_offset(dn,
68076256205SMark Maybee 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
681cdb0ab79Smaybee 
682713d6c20SMatthew Ahrens 		/* if there are no indirect blocks before start, we are done */
68376256205SMark Maybee 		if (err == ESRCH) {
684713d6c20SMatthew Ahrens 			*start = minimum;
685713d6c20SMatthew Ahrens 			break;
686713d6c20SMatthew Ahrens 		} else if (err != 0) {
687cdb0ab79Smaybee 			return (err);
68876256205SMark Maybee 		}
68976256205SMark Maybee 
690713d6c20SMatthew Ahrens 		/* set start to the beginning of this L1 indirect */
69176256205SMark Maybee 		*start = P2ALIGN(*start, iblkrange);
692cdb0ab79Smaybee 	}
693713d6c20SMatthew Ahrens 	if (*start < minimum)
694713d6c20SMatthew Ahrens 		*start = minimum;
695cdb0ab79Smaybee 	return (0);
696cdb0ab79Smaybee }
697cdb0ab79Smaybee 
698*eb721827SAlek Pinchuk /*
699*eb721827SAlek Pinchuk  * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,
700*eb721827SAlek Pinchuk  * otherwise return false.
701*eb721827SAlek Pinchuk  * Used below in dmu_free_long_range_impl() to enable abort when unmounting
702*eb721827SAlek Pinchuk  */
703*eb721827SAlek Pinchuk /*ARGSUSED*/
704*eb721827SAlek Pinchuk static boolean_t
705*eb721827SAlek Pinchuk dmu_objset_zfs_unmounting(objset_t *os)
706*eb721827SAlek Pinchuk {
707*eb721827SAlek Pinchuk #ifdef _KERNEL
708*eb721827SAlek Pinchuk 	if (dmu_objset_type(os) == DMU_OST_ZFS)
709*eb721827SAlek Pinchuk 		return (zfs_get_vfs_flag_unmounted(os));
710*eb721827SAlek Pinchuk #endif
711*eb721827SAlek Pinchuk 	return (B_FALSE);
712*eb721827SAlek Pinchuk }
713*eb721827SAlek Pinchuk 
714cdb0ab79Smaybee static int
715cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
716713d6c20SMatthew Ahrens     uint64_t length)
717cdb0ab79Smaybee {
718713d6c20SMatthew Ahrens 	uint64_t object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
719713d6c20SMatthew Ahrens 	int err;
720713d6c20SMatthew Ahrens 
721713d6c20SMatthew Ahrens 	if (offset >= object_size)
722cdb0ab79Smaybee 		return (0);
723cdb0ab79Smaybee 
724713d6c20SMatthew Ahrens 	if (length == DMU_OBJECT_END || offset + length > object_size)
725713d6c20SMatthew Ahrens 		length = object_size - offset;
726713d6c20SMatthew Ahrens 
727713d6c20SMatthew Ahrens 	while (length != 0) {
728713d6c20SMatthew Ahrens 		uint64_t chunk_end, chunk_begin;
729713d6c20SMatthew Ahrens 
730*eb721827SAlek Pinchuk 		if (dmu_objset_zfs_unmounting(dn->dn_objset))
731*eb721827SAlek Pinchuk 			return (SET_ERROR(EINTR));
732*eb721827SAlek Pinchuk 
733713d6c20SMatthew Ahrens 		chunk_end = chunk_begin = offset + length;
734713d6c20SMatthew Ahrens 
735713d6c20SMatthew Ahrens 		/* move chunk_begin backwards to the beginning of this chunk */
736713d6c20SMatthew Ahrens 		err = get_next_chunk(dn, &chunk_begin, offset);
737cdb0ab79Smaybee 		if (err)
738cdb0ab79Smaybee 			return (err);
739713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, >=, offset);
740713d6c20SMatthew Ahrens 		ASSERT3U(chunk_begin, <=, chunk_end);
741cdb0ab79Smaybee 
742713d6c20SMatthew Ahrens 		dmu_tx_t *tx = dmu_tx_create(os);
743713d6c20SMatthew Ahrens 		dmu_tx_hold_free(tx, dn->dn_object,
744713d6c20SMatthew Ahrens 		    chunk_begin, chunk_end - chunk_begin);
7454bb73804SMatthew Ahrens 
7464bb73804SMatthew Ahrens 		/*
7474bb73804SMatthew Ahrens 		 * Mark this transaction as typically resulting in a net
7484bb73804SMatthew Ahrens 		 * reduction in space used.
7494bb73804SMatthew Ahrens 		 */
7504bb73804SMatthew Ahrens 		dmu_tx_mark_netfree(tx);
751cdb0ab79Smaybee 		err = dmu_tx_assign(tx, TXG_WAIT);
752cdb0ab79Smaybee 		if (err) {
753cdb0ab79Smaybee 			dmu_tx_abort(tx);
754cdb0ab79Smaybee 			return (err);
755cdb0ab79Smaybee 		}
756713d6c20SMatthew Ahrens 		dnode_free_range(dn, chunk_begin, chunk_end - chunk_begin, tx);
757cdb0ab79Smaybee 		dmu_tx_commit(tx);
758713d6c20SMatthew Ahrens 
759713d6c20SMatthew Ahrens 		length -= chunk_end - chunk_begin;
760cdb0ab79Smaybee 	}
761cdb0ab79Smaybee 	return (0);
762cdb0ab79Smaybee }
763cdb0ab79Smaybee 
764cdb0ab79Smaybee int
765cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object,
766cdb0ab79Smaybee     uint64_t offset, uint64_t length)
767cdb0ab79Smaybee {
768cdb0ab79Smaybee 	dnode_t *dn;
769cdb0ab79Smaybee 	int err;
770cdb0ab79Smaybee 
771503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
772cdb0ab79Smaybee 	if (err != 0)
773cdb0ab79Smaybee 		return (err);
774713d6c20SMatthew Ahrens 	err = dmu_free_long_range_impl(os, dn, offset, length);
7755253393bSMatthew Ahrens 
7765253393bSMatthew Ahrens 	/*
7775253393bSMatthew Ahrens 	 * It is important to zero out the maxblkid when freeing the entire
7785253393bSMatthew Ahrens 	 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
7795253393bSMatthew Ahrens 	 * will take the fast path, and (b) dnode_reallocate() can verify
7805253393bSMatthew Ahrens 	 * that the entire file has been freed.
7815253393bSMatthew Ahrens 	 */
78243466aaeSMax Grossman 	if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
7835253393bSMatthew Ahrens 		dn->dn_maxblkid = 0;
7845253393bSMatthew Ahrens 
785cdb0ab79Smaybee 	dnode_rele(dn, FTAG);
786cdb0ab79Smaybee 	return (err);
787cdb0ab79Smaybee }
788cdb0ab79Smaybee 
789cdb0ab79Smaybee int
790713d6c20SMatthew Ahrens dmu_free_long_object(objset_t *os, uint64_t object)
791cdb0ab79Smaybee {
792cdb0ab79Smaybee 	dmu_tx_t *tx;
793cdb0ab79Smaybee 	int err;
794cdb0ab79Smaybee 
795713d6c20SMatthew Ahrens 	err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
796cdb0ab79Smaybee 	if (err != 0)
797cdb0ab79Smaybee 		return (err);
798713d6c20SMatthew Ahrens 
799713d6c20SMatthew Ahrens 	tx = dmu_tx_create(os);
800713d6c20SMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
801713d6c20SMatthew Ahrens 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
8024bb73804SMatthew Ahrens 	dmu_tx_mark_netfree(tx);
803713d6c20SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
804713d6c20SMatthew Ahrens 	if (err == 0) {
805713d6c20SMatthew Ahrens 		err = dmu_object_free(os, object, tx);
806713d6c20SMatthew Ahrens 		dmu_tx_commit(tx);
807cdb0ab79Smaybee 	} else {
808713d6c20SMatthew Ahrens 		dmu_tx_abort(tx);
809cdb0ab79Smaybee 	}
810713d6c20SMatthew Ahrens 
811cdb0ab79Smaybee 	return (err);
812cdb0ab79Smaybee }
813cdb0ab79Smaybee 
814ea8dc4b6Seschrock int
815fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
816fa9e4066Sahrens     uint64_t size, dmu_tx_t *tx)
817fa9e4066Sahrens {
818ea8dc4b6Seschrock 	dnode_t *dn;
819503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
820ea8dc4b6Seschrock 	if (err)
821ea8dc4b6Seschrock 		return (err);
822fa9e4066Sahrens 	ASSERT(offset < UINT64_MAX);
823fa9e4066Sahrens 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
824fa9e4066Sahrens 	dnode_free_range(dn, offset, size, tx);
825fa9e4066Sahrens 	dnode_rele(dn, FTAG);
826ea8dc4b6Seschrock 	return (0);
827fa9e4066Sahrens }
828fa9e4066Sahrens 
829ea8dc4b6Seschrock int
830ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
8317bfdf011SNeil Perrin     void *buf, uint32_t flags)
832fa9e4066Sahrens {
833fa9e4066Sahrens 	dnode_t *dn;
834fa9e4066Sahrens 	dmu_buf_t **dbp;
835c87b8fc5SMark J Musante 	int numbufs, err;
836fa9e4066Sahrens 
837503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
838ea8dc4b6Seschrock 	if (err)
839ea8dc4b6Seschrock 		return (err);
840feb08c6bSbillm 
841feb08c6bSbillm 	/*
842feb08c6bSbillm 	 * Deal with odd block sizes, where there can't be data past the first
843feb08c6bSbillm 	 * block.  If we ever do the tail block optimization, we will need to
844feb08c6bSbillm 	 * handle that here as well.
845feb08c6bSbillm 	 */
846c87b8fc5SMark J Musante 	if (dn->dn_maxblkid == 0) {
847fa9e4066Sahrens 		int newsz = offset > dn->dn_datablksz ? 0 :
848fa9e4066Sahrens 		    MIN(size, dn->dn_datablksz - offset);
849fa9e4066Sahrens 		bzero((char *)buf + newsz, size - newsz);
850fa9e4066Sahrens 		size = newsz;
851fa9e4066Sahrens 	}
852fa9e4066Sahrens 
853fa9e4066Sahrens 	while (size > 0) {
854fa9e4066Sahrens 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
855c87b8fc5SMark J Musante 		int i;
856fa9e4066Sahrens 
857fa9e4066Sahrens 		/*
858fa9e4066Sahrens 		 * NB: we could do this block-at-a-time, but it's nice
859fa9e4066Sahrens 		 * to be reading in parallel.
860fa9e4066Sahrens 		 */
861a2eea2e1Sahrens 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
8627bfdf011SNeil Perrin 		    TRUE, FTAG, &numbufs, &dbp, flags);
863ea8dc4b6Seschrock 		if (err)
8641934e92fSmaybee 			break;
865fa9e4066Sahrens 
866fa9e4066Sahrens 		for (i = 0; i < numbufs; i++) {
867fa9e4066Sahrens 			int tocpy;
868fa9e4066Sahrens 			int bufoff;
869fa9e4066Sahrens 			dmu_buf_t *db = dbp[i];
870fa9e4066Sahrens 
871fa9e4066Sahrens 			ASSERT(size > 0);
872fa9e4066Sahrens 
873fa9e4066Sahrens 			bufoff = offset - db->db_offset;
874fa9e4066Sahrens 			tocpy = (int)MIN(db->db_size - bufoff, size);
875fa9e4066Sahrens 
876fa9e4066Sahrens 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
877fa9e4066Sahrens 
878fa9e4066Sahrens 			offset += tocpy;
879fa9e4066Sahrens 			size -= tocpy;
880fa9e4066Sahrens 			buf = (char *)buf + tocpy;
881fa9e4066Sahrens 		}
882ea8dc4b6Seschrock 		dmu_buf_rele_array(dbp, numbufs, FTAG);
883fa9e4066Sahrens 	}
884a2eea2e1Sahrens 	dnode_rele(dn, FTAG);
8851934e92fSmaybee 	return (err);
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888fa9e4066Sahrens void
889fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
890fa9e4066Sahrens     const void *buf, dmu_tx_t *tx)
891fa9e4066Sahrens {
892fa9e4066Sahrens 	dmu_buf_t **dbp;
893fa9e4066Sahrens 	int numbufs, i;
894fa9e4066Sahrens 
89513506d1eSmaybee 	if (size == 0)
89613506d1eSmaybee 		return;
89713506d1eSmaybee 
898ea8dc4b6Seschrock 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
899ea8dc4b6Seschrock 	    FALSE, FTAG, &numbufs, &dbp));
900fa9e4066Sahrens 
901fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
902fa9e4066Sahrens 		int tocpy;
903fa9e4066Sahrens 		int bufoff;
904fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
905fa9e4066Sahrens 
906fa9e4066Sahrens 		ASSERT(size > 0);
907fa9e4066Sahrens 
908fa9e4066Sahrens 		bufoff = offset - db->db_offset;
909fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
910fa9e4066Sahrens 
911fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
912fa9e4066Sahrens 
913fa9e4066Sahrens 		if (tocpy == db->db_size)
914fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
915fa9e4066Sahrens 		else
916fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
917fa9e4066Sahrens 
918fa9e4066Sahrens 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
919fa9e4066Sahrens 
920fa9e4066Sahrens 		if (tocpy == db->db_size)
921fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
922fa9e4066Sahrens 
923fa9e4066Sahrens 		offset += tocpy;
924fa9e4066Sahrens 		size -= tocpy;
925fa9e4066Sahrens 		buf = (char *)buf + tocpy;
926fa9e4066Sahrens 	}
927ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
928fa9e4066Sahrens }
929fa9e4066Sahrens 
93082c9918fSTim Haley void
93182c9918fSTim Haley dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
93282c9918fSTim Haley     dmu_tx_t *tx)
93382c9918fSTim Haley {
93482c9918fSTim Haley 	dmu_buf_t **dbp;
93582c9918fSTim Haley 	int numbufs, i;
93682c9918fSTim Haley 
93782c9918fSTim Haley 	if (size == 0)
93882c9918fSTim Haley 		return;
93982c9918fSTim Haley 
94082c9918fSTim Haley 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
94182c9918fSTim Haley 	    FALSE, FTAG, &numbufs, &dbp));
94282c9918fSTim Haley 
94382c9918fSTim Haley 	for (i = 0; i < numbufs; i++) {
94482c9918fSTim Haley 		dmu_buf_t *db = dbp[i];
94582c9918fSTim Haley 
94682c9918fSTim Haley 		dmu_buf_will_not_fill(db, tx);
94782c9918fSTim Haley 	}
94882c9918fSTim Haley 	dmu_buf_rele_array(dbp, numbufs, FTAG);
94982c9918fSTim Haley }
95082c9918fSTim Haley 
9515d7b4d43SMatthew Ahrens void
9525d7b4d43SMatthew Ahrens dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
9535d7b4d43SMatthew Ahrens     void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
9545d7b4d43SMatthew Ahrens     int compressed_size, int byteorder, dmu_tx_t *tx)
9555d7b4d43SMatthew Ahrens {
9565d7b4d43SMatthew Ahrens 	dmu_buf_t *db;
9575d7b4d43SMatthew Ahrens 
9585d7b4d43SMatthew Ahrens 	ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
9595d7b4d43SMatthew Ahrens 	ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
9605d7b4d43SMatthew Ahrens 	VERIFY0(dmu_buf_hold_noread(os, object, offset,
9615d7b4d43SMatthew Ahrens 	    FTAG, &db));
9625d7b4d43SMatthew Ahrens 
9635d7b4d43SMatthew Ahrens 	dmu_buf_write_embedded(db,
9645d7b4d43SMatthew Ahrens 	    data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
9655d7b4d43SMatthew Ahrens 	    uncompressed_size, compressed_size, byteorder, tx);
9665d7b4d43SMatthew Ahrens 
9675d7b4d43SMatthew Ahrens 	dmu_buf_rele(db, FTAG);
9685d7b4d43SMatthew Ahrens }
9695d7b4d43SMatthew Ahrens 
970c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
971c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * DMU support for xuio
972c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
973c242f9a0Schunli zhang - Sun Microsystems - Irvine United States kstat_t *xuio_ksp = NULL;
974c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
975c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
976c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_init(xuio_t *xuio, int nblk)
977c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
978c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv;
979c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
980c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
981c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iovcnt = nblk;
982c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
983c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
984c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
985c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->cnt = nblk;
986c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
987c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->iovp = uio->uio_iov;
988c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIO_XUZC_PRIV(xuio) = priv;
989c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
990c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
991c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
992c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
993c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
994c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
995c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
996c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
997c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
998c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
999c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_fini(xuio_t *xuio)
1000c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1001c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1002c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int nblk = priv->cnt;
1003c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1004c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
1005c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
1006c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kmem_free(priv, sizeof (dmu_xuio_t));
1007c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1008c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
1009c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
1010c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	else
1011c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
1012c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1013c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1014c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /*
1015c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
1016c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * and increase priv->next by 1.
1017c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  */
1018c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
1019c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
1020c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1021c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	struct iovec *iov;
1022c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uio_t *uio = &xuio->xu_uio;
1023c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1024c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int i = priv->next++;
1025c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1026c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
1027c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(off + n <= arc_buf_size(abuf));
1028c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov = uio->uio_iov + i;
1029c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_base = (char *)abuf->b_data + off;
1030c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	iov->iov_len = n;
1031c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = abuf;
1032c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (0);
1033c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1034c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1035c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
1036c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_cnt(xuio_t *xuio)
1037c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1038c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1039c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->cnt);
1040c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1041c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1042c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_buf_t *
1043c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_arcbuf(xuio_t *xuio, int i)
1044c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1045c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1046c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1047c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
1048c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (priv->bufs[i]);
1049c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1050c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1051c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
1052c242f9a0Schunli zhang - Sun Microsystems - Irvine United States dmu_xuio_clear(xuio_t *xuio, int i)
1053c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1054c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
1055c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1056c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(i < priv->cnt);
1057c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	priv->bufs[i] = NULL;
1058c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1059c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1060c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
1061c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_init(void)
1062c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1063c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
1064c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
1065c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    KSTAT_FLAG_VIRTUAL);
1066c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
1067c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp->ks_data = &xuio_stats;
1068c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_install(xuio_ksp);
1069c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
1070c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1071c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1072c242f9a0Schunli zhang - Sun Microsystems - Irvine United States static void
1073c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_fini(void)
1074c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1075c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (xuio_ksp != NULL) {
1076c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		kstat_delete(xuio_ksp);
1077c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio_ksp = NULL;
1078c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
1079c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1080c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1081c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
1082c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_copied()
1083c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1084c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1085c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1086c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1087c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void
1088c242f9a0Schunli zhang - Sun Microsystems - Irvine United States xuio_stat_wbuf_nocopy()
1089c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
1090c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
1091c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
1092c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1093fa9e4066Sahrens #ifdef _KERNEL
1094f8554bb9SMatthew Ahrens static int
1095f8554bb9SMatthew Ahrens dmu_read_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size)
1096feb08c6bSbillm {
1097feb08c6bSbillm 	dmu_buf_t **dbp;
1098feb08c6bSbillm 	int numbufs, i, err;
1099c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_t *xuio = NULL;
1100feb08c6bSbillm 
1101feb08c6bSbillm 	/*
1102feb08c6bSbillm 	 * NB: we could do this block-at-a-time, but it's nice
1103feb08c6bSbillm 	 * to be reading in parallel.
1104feb08c6bSbillm 	 */
1105f8554bb9SMatthew Ahrens 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
1106f8554bb9SMatthew Ahrens 	    TRUE, FTAG, &numbufs, &dbp, 0);
1107feb08c6bSbillm 	if (err)
1108feb08c6bSbillm 		return (err);
1109feb08c6bSbillm 
1110c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (uio->uio_extflg == UIO_XUIO)
1111c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		xuio = (xuio_t *)uio;
1112c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1113feb08c6bSbillm 	for (i = 0; i < numbufs; i++) {
1114feb08c6bSbillm 		int tocpy;
1115feb08c6bSbillm 		int bufoff;
1116feb08c6bSbillm 		dmu_buf_t *db = dbp[i];
1117feb08c6bSbillm 
1118feb08c6bSbillm 		ASSERT(size > 0);
1119feb08c6bSbillm 
1120feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1121feb08c6bSbillm 		tocpy = (int)MIN(db->db_size - bufoff, size);
1122feb08c6bSbillm 
1123c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (xuio) {
1124c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
1125c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *dbuf_abuf = dbi->db_buf;
1126c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1127c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1128c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (!err) {
1129c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_resid -= tocpy;
1130c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				uio->uio_loffset += tocpy;
1131c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			}
1132c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
1133c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			if (abuf == dbuf_abuf)
1134c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1135c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			else
1136c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1137c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		} else {
1138c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			err = uiomove((char *)db->db_data + bufoff, tocpy,
1139c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			    UIO_READ, uio);
1140c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		}
1141feb08c6bSbillm 		if (err)
1142feb08c6bSbillm 			break;
1143feb08c6bSbillm 
1144feb08c6bSbillm 		size -= tocpy;
1145feb08c6bSbillm 	}
1146feb08c6bSbillm 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1147feb08c6bSbillm 
1148feb08c6bSbillm 	return (err);
1149feb08c6bSbillm }
1150feb08c6bSbillm 
1151f8554bb9SMatthew Ahrens /*
1152f8554bb9SMatthew Ahrens  * Read 'size' bytes into the uio buffer.
1153f8554bb9SMatthew Ahrens  * From object zdb->db_object.
1154f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1155f8554bb9SMatthew Ahrens  *
1156f8554bb9SMatthew Ahrens  * If the caller already has a dbuf in the target object
1157f8554bb9SMatthew Ahrens  * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1158f8554bb9SMatthew Ahrens  * because we don't have to find the dnode_t for the object.
1159f8554bb9SMatthew Ahrens  */
1160f8554bb9SMatthew Ahrens int
1161f8554bb9SMatthew Ahrens dmu_read_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size)
1162f8554bb9SMatthew Ahrens {
1163f8554bb9SMatthew Ahrens 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1164f8554bb9SMatthew Ahrens 	dnode_t *dn;
1165f8554bb9SMatthew Ahrens 	int err;
1166f8554bb9SMatthew Ahrens 
1167f8554bb9SMatthew Ahrens 	if (size == 0)
1168f8554bb9SMatthew Ahrens 		return (0);
1169f8554bb9SMatthew Ahrens 
1170f8554bb9SMatthew Ahrens 	DB_DNODE_ENTER(db);
1171f8554bb9SMatthew Ahrens 	dn = DB_DNODE(db);
1172f8554bb9SMatthew Ahrens 	err = dmu_read_uio_dnode(dn, uio, size);
1173f8554bb9SMatthew Ahrens 	DB_DNODE_EXIT(db);
1174f8554bb9SMatthew Ahrens 
1175f8554bb9SMatthew Ahrens 	return (err);
1176f8554bb9SMatthew Ahrens }
1177f8554bb9SMatthew Ahrens 
1178f8554bb9SMatthew Ahrens /*
1179f8554bb9SMatthew Ahrens  * Read 'size' bytes into the uio buffer.
1180f8554bb9SMatthew Ahrens  * From the specified object
1181f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1182f8554bb9SMatthew Ahrens  */
1183f8554bb9SMatthew Ahrens int
1184f8554bb9SMatthew Ahrens dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
1185f8554bb9SMatthew Ahrens {
1186f8554bb9SMatthew Ahrens 	dnode_t *dn;
1187f8554bb9SMatthew Ahrens 	int err;
1188f8554bb9SMatthew Ahrens 
1189f8554bb9SMatthew Ahrens 	if (size == 0)
1190f8554bb9SMatthew Ahrens 		return (0);
1191f8554bb9SMatthew Ahrens 
1192f8554bb9SMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1193f8554bb9SMatthew Ahrens 	if (err)
1194f8554bb9SMatthew Ahrens 		return (err);
1195f8554bb9SMatthew Ahrens 
1196f8554bb9SMatthew Ahrens 	err = dmu_read_uio_dnode(dn, uio, size);
1197f8554bb9SMatthew Ahrens 
1198f8554bb9SMatthew Ahrens 	dnode_rele(dn, FTAG);
1199f8554bb9SMatthew Ahrens 
1200f8554bb9SMatthew Ahrens 	return (err);
1201f8554bb9SMatthew Ahrens }
1202f8554bb9SMatthew Ahrens 
120394d1a210STim Haley static int
120494d1a210STim Haley dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1205fa9e4066Sahrens {
1206fa9e4066Sahrens 	dmu_buf_t **dbp;
120794d1a210STim Haley 	int numbufs;
1208fa9e4066Sahrens 	int err = 0;
120994d1a210STim Haley 	int i;
1210fa9e4066Sahrens 
121194d1a210STim Haley 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
121294d1a210STim Haley 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1213ea8dc4b6Seschrock 	if (err)
1214ea8dc4b6Seschrock 		return (err);
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	for (i = 0; i < numbufs; i++) {
1217fa9e4066Sahrens 		int tocpy;
1218fa9e4066Sahrens 		int bufoff;
1219fa9e4066Sahrens 		dmu_buf_t *db = dbp[i];
1220fa9e4066Sahrens 
1221fa9e4066Sahrens 		ASSERT(size > 0);
1222fa9e4066Sahrens 
1223feb08c6bSbillm 		bufoff = uio->uio_loffset - db->db_offset;
1224fa9e4066Sahrens 		tocpy = (int)MIN(db->db_size - bufoff, size);
1225fa9e4066Sahrens 
1226fa9e4066Sahrens 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 		if (tocpy == db->db_size)
1229fa9e4066Sahrens 			dmu_buf_will_fill(db, tx);
1230fa9e4066Sahrens 		else
1231fa9e4066Sahrens 			dmu_buf_will_dirty(db, tx);
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 		/*
1234fa9e4066Sahrens 		 * XXX uiomove could block forever (eg. nfs-backed
1235fa9e4066Sahrens 		 * pages).  There needs to be a uiolockdown() function
1236fa9e4066Sahrens 		 * to lock the pages in memory, so that uiomove won't
1237fa9e4066Sahrens 		 * block.
1238fa9e4066Sahrens 		 */
1239fa9e4066Sahrens 		err = uiomove((char *)db->db_data + bufoff, tocpy,
1240fa9e4066Sahrens 		    UIO_WRITE, uio);
1241fa9e4066Sahrens 
1242fa9e4066Sahrens 		if (tocpy == db->db_size)
1243fa9e4066Sahrens 			dmu_buf_fill_done(db, tx);
1244fa9e4066Sahrens 
1245fa9e4066Sahrens 		if (err)
1246fa9e4066Sahrens 			break;
1247fa9e4066Sahrens 
1248fa9e4066Sahrens 		size -= tocpy;
1249fa9e4066Sahrens 	}
125094d1a210STim Haley 
1251ea8dc4b6Seschrock 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1252fa9e4066Sahrens 	return (err);
1253fa9e4066Sahrens }
125444eda4d7Smaybee 
1255f8554bb9SMatthew Ahrens /*
1256f8554bb9SMatthew Ahrens  * Write 'size' bytes from the uio buffer.
1257f8554bb9SMatthew Ahrens  * To object zdb->db_object.
1258f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1259f8554bb9SMatthew Ahrens  *
1260f8554bb9SMatthew Ahrens  * If the caller already has a dbuf in the target object
1261f8554bb9SMatthew Ahrens  * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1262f8554bb9SMatthew Ahrens  * because we don't have to find the dnode_t for the object.
1263f8554bb9SMatthew Ahrens  */
126494d1a210STim Haley int
126594d1a210STim Haley dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
126694d1a210STim Haley     dmu_tx_t *tx)
126794d1a210STim Haley {
1268744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1269744947dcSTom Erickson 	dnode_t *dn;
1270744947dcSTom Erickson 	int err;
1271744947dcSTom Erickson 
127294d1a210STim Haley 	if (size == 0)
127394d1a210STim Haley 		return (0);
127494d1a210STim Haley 
1275744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1276744947dcSTom Erickson 	dn = DB_DNODE(db);
1277744947dcSTom Erickson 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1278744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1279744947dcSTom Erickson 
1280744947dcSTom Erickson 	return (err);
128194d1a210STim Haley }
128294d1a210STim Haley 
1283f8554bb9SMatthew Ahrens /*
1284f8554bb9SMatthew Ahrens  * Write 'size' bytes from the uio buffer.
1285f8554bb9SMatthew Ahrens  * To the specified object.
1286f8554bb9SMatthew Ahrens  * Starting at offset uio->uio_loffset.
1287f8554bb9SMatthew Ahrens  */
128894d1a210STim Haley int
128994d1a210STim Haley dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
129094d1a210STim Haley     dmu_tx_t *tx)
129194d1a210STim Haley {
129294d1a210STim Haley 	dnode_t *dn;
129394d1a210STim Haley 	int err;
129494d1a210STim Haley 
129594d1a210STim Haley 	if (size == 0)
129694d1a210STim Haley 		return (0);
129794d1a210STim Haley 
129894d1a210STim Haley 	err = dnode_hold(os, object, FTAG, &dn);
129994d1a210STim Haley 	if (err)
130094d1a210STim Haley 		return (err);
130194d1a210STim Haley 
130294d1a210STim Haley 	err = dmu_write_uio_dnode(dn, uio, size, tx);
130394d1a210STim Haley 
130494d1a210STim Haley 	dnode_rele(dn, FTAG);
130594d1a210STim Haley 
130694d1a210STim Haley 	return (err);
130794d1a210STim Haley }
130894d1a210STim Haley 
130944eda4d7Smaybee int
131044eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
131144eda4d7Smaybee     page_t *pp, dmu_tx_t *tx)
131244eda4d7Smaybee {
131344eda4d7Smaybee 	dmu_buf_t **dbp;
131444eda4d7Smaybee 	int numbufs, i;
131544eda4d7Smaybee 	int err;
131644eda4d7Smaybee 
131744eda4d7Smaybee 	if (size == 0)
131844eda4d7Smaybee 		return (0);
131944eda4d7Smaybee 
132044eda4d7Smaybee 	err = dmu_buf_hold_array(os, object, offset, size,
132144eda4d7Smaybee 	    FALSE, FTAG, &numbufs, &dbp);
132244eda4d7Smaybee 	if (err)
132344eda4d7Smaybee 		return (err);
132444eda4d7Smaybee 
132544eda4d7Smaybee 	for (i = 0; i < numbufs; i++) {
132644eda4d7Smaybee 		int tocpy, copied, thiscpy;
132744eda4d7Smaybee 		int bufoff;
132844eda4d7Smaybee 		dmu_buf_t *db = dbp[i];
132944eda4d7Smaybee 		caddr_t va;
133044eda4d7Smaybee 
133144eda4d7Smaybee 		ASSERT(size > 0);
133244eda4d7Smaybee 		ASSERT3U(db->db_size, >=, PAGESIZE);
133344eda4d7Smaybee 
133444eda4d7Smaybee 		bufoff = offset - db->db_offset;
133544eda4d7Smaybee 		tocpy = (int)MIN(db->db_size - bufoff, size);
133644eda4d7Smaybee 
133744eda4d7Smaybee 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
133844eda4d7Smaybee 
133944eda4d7Smaybee 		if (tocpy == db->db_size)
134044eda4d7Smaybee 			dmu_buf_will_fill(db, tx);
134144eda4d7Smaybee 		else
134244eda4d7Smaybee 			dmu_buf_will_dirty(db, tx);
134344eda4d7Smaybee 
134444eda4d7Smaybee 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
134544eda4d7Smaybee 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
134644eda4d7Smaybee 			thiscpy = MIN(PAGESIZE, tocpy - copied);
13470fab61baSJonathan W Adams 			va = zfs_map_page(pp, S_READ);
134844eda4d7Smaybee 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
13490fab61baSJonathan W Adams 			zfs_unmap_page(pp, va);
135044eda4d7Smaybee 			pp = pp->p_next;
135144eda4d7Smaybee 			bufoff += PAGESIZE;
135244eda4d7Smaybee 		}
135344eda4d7Smaybee 
135444eda4d7Smaybee 		if (tocpy == db->db_size)
135544eda4d7Smaybee 			dmu_buf_fill_done(db, tx);
135644eda4d7Smaybee 
135744eda4d7Smaybee 		offset += tocpy;
135844eda4d7Smaybee 		size -= tocpy;
135944eda4d7Smaybee 	}
136044eda4d7Smaybee 	dmu_buf_rele_array(dbp, numbufs, FTAG);
136144eda4d7Smaybee 	return (err);
136244eda4d7Smaybee }
1363fa9e4066Sahrens #endif
1364fa9e4066Sahrens 
13652fdbea25SAleksandr Guzovskiy /*
13662fdbea25SAleksandr Guzovskiy  * Allocate a loaned anonymous arc buffer.
13672fdbea25SAleksandr Guzovskiy  */
13682fdbea25SAleksandr Guzovskiy arc_buf_t *
13692fdbea25SAleksandr Guzovskiy dmu_request_arcbuf(dmu_buf_t *handle, int size)
13702fdbea25SAleksandr Guzovskiy {
1371744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
13722fdbea25SAleksandr Guzovskiy 
137343466aaeSMax Grossman 	return (arc_loan_buf(db->db_objset->os_spa, size));
13742fdbea25SAleksandr Guzovskiy }
13752fdbea25SAleksandr Guzovskiy 
13762fdbea25SAleksandr Guzovskiy /*
13772fdbea25SAleksandr Guzovskiy  * Free a loaned arc buffer.
13782fdbea25SAleksandr Guzovskiy  */
13792fdbea25SAleksandr Guzovskiy void
13802fdbea25SAleksandr Guzovskiy dmu_return_arcbuf(arc_buf_t *buf)
13812fdbea25SAleksandr Guzovskiy {
13822fdbea25SAleksandr Guzovskiy 	arc_return_buf(buf, FTAG);
1383dcbf3bd6SGeorge Wilson 	arc_buf_destroy(buf, FTAG);
13842fdbea25SAleksandr Guzovskiy }
13852fdbea25SAleksandr Guzovskiy 
13862fdbea25SAleksandr Guzovskiy /*
13872fdbea25SAleksandr Guzovskiy  * When possible directly assign passed loaned arc buffer to a dbuf.
13882fdbea25SAleksandr Guzovskiy  * If this is not possible copy the contents of passed arc buf via
13892fdbea25SAleksandr Guzovskiy  * dmu_write().
13902fdbea25SAleksandr Guzovskiy  */
13912fdbea25SAleksandr Guzovskiy void
13922fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
13932fdbea25SAleksandr Guzovskiy     dmu_tx_t *tx)
13942fdbea25SAleksandr Guzovskiy {
1395744947dcSTom Erickson 	dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1396744947dcSTom Erickson 	dnode_t *dn;
13972fdbea25SAleksandr Guzovskiy 	dmu_buf_impl_t *db;
13982fdbea25SAleksandr Guzovskiy 	uint32_t blksz = (uint32_t)arc_buf_size(buf);
13992fdbea25SAleksandr Guzovskiy 	uint64_t blkid;
14002fdbea25SAleksandr Guzovskiy 
1401744947dcSTom Erickson 	DB_DNODE_ENTER(dbuf);
1402744947dcSTom Erickson 	dn = DB_DNODE(dbuf);
14032fdbea25SAleksandr Guzovskiy 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1404a2cdcdd2SPaul Dagnelie 	blkid = dbuf_whichblock(dn, 0, offset);
14052fdbea25SAleksandr Guzovskiy 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
14062fdbea25SAleksandr Guzovskiy 	rw_exit(&dn->dn_struct_rwlock);
1407744947dcSTom Erickson 	DB_DNODE_EXIT(dbuf);
14082fdbea25SAleksandr Guzovskiy 
14098a904709SMatthew Ahrens 	/*
14108a904709SMatthew Ahrens 	 * We can only assign if the offset is aligned, the arc buf is the
14118a904709SMatthew Ahrens 	 * same size as the dbuf, and the dbuf is not metadata.  It
14128a904709SMatthew Ahrens 	 * can't be metadata because the loaned arc buf comes from the
14138a904709SMatthew Ahrens 	 * user-data kmem arena.
14148a904709SMatthew Ahrens 	 */
14158a904709SMatthew Ahrens 	if (offset == db->db.db_offset && blksz == db->db.db_size &&
14168a904709SMatthew Ahrens 	    DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA) {
14172fdbea25SAleksandr Guzovskiy 		dbuf_assign_arcbuf(db, buf, tx);
14182fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
14192fdbea25SAleksandr Guzovskiy 	} else {
1420744947dcSTom Erickson 		objset_t *os;
1421744947dcSTom Erickson 		uint64_t object;
1422744947dcSTom Erickson 
1423744947dcSTom Erickson 		DB_DNODE_ENTER(dbuf);
1424744947dcSTom Erickson 		dn = DB_DNODE(dbuf);
1425744947dcSTom Erickson 		os = dn->dn_objset;
1426744947dcSTom Erickson 		object = dn->dn_object;
1427744947dcSTom Erickson 		DB_DNODE_EXIT(dbuf);
1428744947dcSTom Erickson 
14292fdbea25SAleksandr Guzovskiy 		dbuf_rele(db, FTAG);
1430744947dcSTom Erickson 		dmu_write(os, object, offset, blksz, buf->b_data, tx);
14312fdbea25SAleksandr Guzovskiy 		dmu_return_arcbuf(buf);
1432c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
14332fdbea25SAleksandr Guzovskiy 	}
14342fdbea25SAleksandr Guzovskiy }
14352fdbea25SAleksandr Guzovskiy 
1436c5c6ffa0Smaybee typedef struct {
1437b24ab676SJeff Bonwick 	dbuf_dirty_record_t	*dsa_dr;
1438b24ab676SJeff Bonwick 	dmu_sync_cb_t		*dsa_done;
1439b24ab676SJeff Bonwick 	zgd_t			*dsa_zgd;
1440b24ab676SJeff Bonwick 	dmu_tx_t		*dsa_tx;
1441c717a561Smaybee } dmu_sync_arg_t;
1442c5c6ffa0Smaybee 
1443e14bb325SJeff Bonwick /* ARGSUSED */
1444e14bb325SJeff Bonwick static void
1445e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1446e14bb325SJeff Bonwick {
1447b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1448b24ab676SJeff Bonwick 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1449e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1450975c32a0SNeil Perrin 
1451b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
1452b24ab676SJeff Bonwick 		if (BP_IS_HOLE(bp)) {
1453b24ab676SJeff Bonwick 			/*
1454b24ab676SJeff Bonwick 			 * A block of zeros may compress to a hole, but the
1455b24ab676SJeff Bonwick 			 * block size still needs to be known for replay.
1456b24ab676SJeff Bonwick 			 */
1457b24ab676SJeff Bonwick 			BP_SET_LSIZE(bp, db->db_size);
14585d7b4d43SMatthew Ahrens 		} else if (!BP_IS_EMBEDDED(bp)) {
1459b24ab676SJeff Bonwick 			ASSERT(BP_GET_LEVEL(bp) == 0);
1460b24ab676SJeff Bonwick 			bp->blk_fill = 1;
1461b24ab676SJeff Bonwick 		}
1462e14bb325SJeff Bonwick 	}
1463e14bb325SJeff Bonwick }
1464e14bb325SJeff Bonwick 
1465b24ab676SJeff Bonwick static void
1466b24ab676SJeff Bonwick dmu_sync_late_arrival_ready(zio_t *zio)
1467b24ab676SJeff Bonwick {
1468b24ab676SJeff Bonwick 	dmu_sync_ready(zio, NULL, zio->io_private);
1469b24ab676SJeff Bonwick }
1470b24ab676SJeff Bonwick 
1471c5c6ffa0Smaybee /* ARGSUSED */
1472c5c6ffa0Smaybee static void
1473c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1474c5c6ffa0Smaybee {
1475b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = varg;
1476b24ab676SJeff Bonwick 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1477c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1478c5c6ffa0Smaybee 
1479b50a0fe0SNeil Perrin 	mutex_enter(&db->db_mtx);
1480b50a0fe0SNeil Perrin 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1481b24ab676SJeff Bonwick 	if (zio->io_error == 0) {
148280901aeaSGeorge Wilson 		dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
148380901aeaSGeorge Wilson 		if (dr->dt.dl.dr_nopwrite) {
148480901aeaSGeorge Wilson 			blkptr_t *bp = zio->io_bp;
148580901aeaSGeorge Wilson 			blkptr_t *bp_orig = &zio->io_bp_orig;
148680901aeaSGeorge Wilson 			uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
148780901aeaSGeorge Wilson 
148880901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
148980901aeaSGeorge Wilson 			ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
149045818ee1SMatthew Ahrens 			ASSERT(zio_checksum_table[chksum].ci_flags &
149145818ee1SMatthew Ahrens 			    ZCHECKSUM_FLAG_NOPWRITE);
149280901aeaSGeorge Wilson 		}
1493b24ab676SJeff Bonwick 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1494b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1495b24ab676SJeff Bonwick 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
149670163ac5SPrakash Surya 
149770163ac5SPrakash Surya 		/*
149870163ac5SPrakash Surya 		 * Old style holes are filled with all zeros, whereas
149970163ac5SPrakash Surya 		 * new-style holes maintain their lsize, type, level,
150070163ac5SPrakash Surya 		 * and birth time (see zio_write_compress). While we
150170163ac5SPrakash Surya 		 * need to reset the BP_SET_LSIZE() call that happened
150270163ac5SPrakash Surya 		 * in dmu_sync_ready for old style holes, we do *not*
150370163ac5SPrakash Surya 		 * want to wipe out the information contained in new
150470163ac5SPrakash Surya 		 * style holes. Thus, only zero out the block pointer if
150570163ac5SPrakash Surya 		 * it's an old style hole.
150670163ac5SPrakash Surya 		 */
150770163ac5SPrakash Surya 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
150870163ac5SPrakash Surya 		    dr->dt.dl.dr_overridden_by.blk_birth == 0)
1509b24ab676SJeff Bonwick 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1510b24ab676SJeff Bonwick 	} else {
1511b24ab676SJeff Bonwick 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1512b24ab676SJeff Bonwick 	}
1513c5c6ffa0Smaybee 	cv_broadcast(&db->db_changed);
1514b50a0fe0SNeil Perrin 	mutex_exit(&db->db_mtx);
1515b50a0fe0SNeil Perrin 
1516b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1517b24ab676SJeff Bonwick 
1518b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1519b24ab676SJeff Bonwick }
1520b24ab676SJeff Bonwick 
1521b24ab676SJeff Bonwick static void
1522b24ab676SJeff Bonwick dmu_sync_late_arrival_done(zio_t *zio)
1523b24ab676SJeff Bonwick {
1524b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1525b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa = zio->io_private;
152680901aeaSGeorge Wilson 	blkptr_t *bp_orig = &zio->io_bp_orig;
1527b24ab676SJeff Bonwick 
1528b24ab676SJeff Bonwick 	if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
152980901aeaSGeorge Wilson 		/*
153080901aeaSGeorge Wilson 		 * If we didn't allocate a new block (i.e. ZIO_FLAG_NOPWRITE)
153180901aeaSGeorge Wilson 		 * then there is nothing to do here. Otherwise, free the
153280901aeaSGeorge Wilson 		 * newly allocated block in this txg.
153380901aeaSGeorge Wilson 		 */
153480901aeaSGeorge Wilson 		if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
153580901aeaSGeorge Wilson 			ASSERT(BP_EQUAL(bp, bp_orig));
153680901aeaSGeorge Wilson 		} else {
153780901aeaSGeorge Wilson 			ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
153880901aeaSGeorge Wilson 			ASSERT(zio->io_bp->blk_birth == zio->io_txg);
153980901aeaSGeorge Wilson 			ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
154080901aeaSGeorge Wilson 			zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
154180901aeaSGeorge Wilson 		}
1542b24ab676SJeff Bonwick 	}
1543b24ab676SJeff Bonwick 
1544b24ab676SJeff Bonwick 	dmu_tx_commit(dsa->dsa_tx);
1545b24ab676SJeff Bonwick 
1546b24ab676SJeff Bonwick 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1547b24ab676SJeff Bonwick 
1548b24ab676SJeff Bonwick 	kmem_free(dsa, sizeof (*dsa));
1549b24ab676SJeff Bonwick }
1550b24ab676SJeff Bonwick 
1551b24ab676SJeff Bonwick static int
1552b24ab676SJeff Bonwick dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
15537802d7bfSMatthew Ahrens     zio_prop_t *zp, zbookmark_phys_t *zb)
1554b24ab676SJeff Bonwick {
1555b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
1556b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1557b24ab676SJeff Bonwick 
1558b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1559b24ab676SJeff Bonwick 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
15606e1f5caaSNeil Perrin 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1561b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1562be6fd75aSMatthew Ahrens 		/* Make zl_get_data do txg_waited_synced() */
1563be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1564b24ab676SJeff Bonwick 	}
1565b24ab676SJeff Bonwick 
1566b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1567b24ab676SJeff Bonwick 	dsa->dsa_dr = NULL;
1568b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1569b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1570b24ab676SJeff Bonwick 	dsa->dsa_tx = tx;
1571c717a561Smaybee 
15728df0bcf0SPaul Dagnelie 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx),
15738df0bcf0SPaul Dagnelie 	    zgd->zgd_bp, zgd->zgd_db->db_data, zgd->zgd_db->db_size,
15748df0bcf0SPaul Dagnelie 	    zp, dmu_sync_late_arrival_ready, NULL,
15758df0bcf0SPaul Dagnelie 	    NULL, dmu_sync_late_arrival_done, dsa, ZIO_PRIORITY_SYNC_WRITE,
15768df0bcf0SPaul Dagnelie 	    ZIO_FLAG_CANFAIL, zb));
1577b24ab676SJeff Bonwick 
1578b24ab676SJeff Bonwick 	return (0);
1579c5c6ffa0Smaybee }
1580c5c6ffa0Smaybee 
1581fa9e4066Sahrens /*
1582c5c6ffa0Smaybee  * Intent log support: sync the block associated with db to disk.
1583c5c6ffa0Smaybee  * N.B. and XXX: the caller is responsible for making sure that the
1584c5c6ffa0Smaybee  * data isn't changing while dmu_sync() is writing it.
1585fa9e4066Sahrens  *
1586fa9e4066Sahrens  * Return values:
1587fa9e4066Sahrens  *
158880901aeaSGeorge Wilson  *	EEXIST: this txg has already been synced, so there's nothing to do.
1589fa9e4066Sahrens  *		The caller should not log the write.
1590fa9e4066Sahrens  *
1591fa9e4066Sahrens  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1592fa9e4066Sahrens  *		The caller should not log the write.
1593fa9e4066Sahrens  *
1594c5c6ffa0Smaybee  *	EALREADY: this block is already in the process of being synced.
1595c5c6ffa0Smaybee  *		The caller should track its progress (somehow).
1596fa9e4066Sahrens  *
1597b24ab676SJeff Bonwick  *	EIO: could not do the I/O.
1598b24ab676SJeff Bonwick  *		The caller should do a txg_wait_synced().
1599fa9e4066Sahrens  *
1600b24ab676SJeff Bonwick  *	0: the I/O has been initiated.
1601b24ab676SJeff Bonwick  *		The caller should log this blkptr in the done callback.
1602b24ab676SJeff Bonwick  *		It is possible that the I/O will fail, in which case
1603b24ab676SJeff Bonwick  *		the error will be reported to the done callback and
1604b24ab676SJeff Bonwick  *		propagated to pio from zio_done().
1605fa9e4066Sahrens  */
1606fa9e4066Sahrens int
1607b24ab676SJeff Bonwick dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1608fa9e4066Sahrens {
1609b24ab676SJeff Bonwick 	blkptr_t *bp = zgd->zgd_bp;
1610b24ab676SJeff Bonwick 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1611503ad85cSMatthew Ahrens 	objset_t *os = db->db_objset;
1612b24ab676SJeff Bonwick 	dsl_dataset_t *ds = os->os_dsl_dataset;
1613c717a561Smaybee 	dbuf_dirty_record_t *dr;
1614b24ab676SJeff Bonwick 	dmu_sync_arg_t *dsa;
16157802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1616b24ab676SJeff Bonwick 	zio_prop_t zp;
1617744947dcSTom Erickson 	dnode_t *dn;
1618fa9e4066Sahrens 
1619b24ab676SJeff Bonwick 	ASSERT(pio != NULL);
1620fa9e4066Sahrens 	ASSERT(txg != 0);
1621fa9e4066Sahrens 
1622b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, ds->ds_object,
1623b24ab676SJeff Bonwick 	    db->db.db_object, db->db_level, db->db_blkid);
1624b24ab676SJeff Bonwick 
1625744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1626744947dcSTom Erickson 	dn = DB_DNODE(db);
1627744947dcSTom Erickson 	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1628744947dcSTom Erickson 	DB_DNODE_EXIT(db);
1629fa9e4066Sahrens 
1630ea8dc4b6Seschrock 	/*
1631b24ab676SJeff Bonwick 	 * If we're frozen (running ziltest), we always need to generate a bp.
1632ea8dc4b6Seschrock 	 */
1633b24ab676SJeff Bonwick 	if (txg > spa_freeze_txg(os->os_spa))
1634b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1635ea8dc4b6Seschrock 
1636fa9e4066Sahrens 	/*
1637b24ab676SJeff Bonwick 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1638b24ab676SJeff Bonwick 	 * and us.  If we determine that this txg is not yet syncing,
1639b24ab676SJeff Bonwick 	 * but it begins to sync a moment later, that's OK because the
1640b24ab676SJeff Bonwick 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1641fa9e4066Sahrens 	 */
1642b24ab676SJeff Bonwick 	mutex_enter(&db->db_mtx);
1643b24ab676SJeff Bonwick 
1644b24ab676SJeff Bonwick 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1645fa9e4066Sahrens 		/*
1646b24ab676SJeff Bonwick 		 * This txg has already synced.  There's nothing to do.
1647fa9e4066Sahrens 		 */
1648b24ab676SJeff Bonwick 		mutex_exit(&db->db_mtx);
1649be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1650fa9e4066Sahrens 	}
1651fa9e4066Sahrens 
1652b24ab676SJeff Bonwick 	if (txg <= spa_syncing_txg(os->os_spa)) {
1653b24ab676SJeff Bonwick 		/*
1654b24ab676SJeff Bonwick 		 * This txg is currently syncing, so we can't mess with
1655b24ab676SJeff Bonwick 		 * the dirty record anymore; just write a new log block.
1656b24ab676SJeff Bonwick 		 */
1657b24ab676SJeff Bonwick 		mutex_exit(&db->db_mtx);
1658b24ab676SJeff Bonwick 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1659fa9e4066Sahrens 	}
1660fa9e4066Sahrens 
1661c717a561Smaybee 	dr = db->db_last_dirty;
1662b24ab676SJeff Bonwick 	while (dr && dr->dr_txg != txg)
1663c717a561Smaybee 		dr = dr->dr_next;
1664b24ab676SJeff Bonwick 
1665b24ab676SJeff Bonwick 	if (dr == NULL) {
1666c5c6ffa0Smaybee 		/*
1667b24ab676SJeff Bonwick 		 * There's no dr for this dbuf, so it must have been freed.
1668c5c6ffa0Smaybee 		 * There's no need to log writes to freed blocks, so we're done.
1669c5c6ffa0Smaybee 		 */
1670c5c6ffa0Smaybee 		mutex_exit(&db->db_mtx);
1671be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1672c5c6ffa0Smaybee 	}
1673c5c6ffa0Smaybee 
167480901aeaSGeorge Wilson 	ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg);
167580901aeaSGeorge Wilson 
167680901aeaSGeorge Wilson 	/*
167734e8acefSMatthew Ahrens 	 * Assume the on-disk data is X, the current syncing data (in
167834e8acefSMatthew Ahrens 	 * txg - 1) is Y, and the current in-memory data is Z (currently
167934e8acefSMatthew Ahrens 	 * in dmu_sync).
168034e8acefSMatthew Ahrens 	 *
168134e8acefSMatthew Ahrens 	 * We usually want to perform a nopwrite if X and Z are the
168234e8acefSMatthew Ahrens 	 * same.  However, if Y is different (i.e. the BP is going to
168334e8acefSMatthew Ahrens 	 * change before this write takes effect), then a nopwrite will
168434e8acefSMatthew Ahrens 	 * be incorrect - we would override with X, which could have
168534e8acefSMatthew Ahrens 	 * been freed when Y was written.
168634e8acefSMatthew Ahrens 	 *
168734e8acefSMatthew Ahrens 	 * (Note that this is not a concern when we are nop-writing from
168834e8acefSMatthew Ahrens 	 * syncing context, because X and Y must be identical, because
168934e8acefSMatthew Ahrens 	 * all previous txgs have been synced.)
169034e8acefSMatthew Ahrens 	 *
169134e8acefSMatthew Ahrens 	 * Therefore, we disable nopwrite if the current BP could change
169234e8acefSMatthew Ahrens 	 * before this TXG.  There are two ways it could change: by
169334e8acefSMatthew Ahrens 	 * being dirty (dr_next is non-NULL), or by being freed
169434e8acefSMatthew Ahrens 	 * (dnode_block_freed()).  This behavior is verified by
169534e8acefSMatthew Ahrens 	 * zio_done(), which VERIFYs that the override BP is identical
169634e8acefSMatthew Ahrens 	 * to the on-disk BP.
169780901aeaSGeorge Wilson 	 */
169834e8acefSMatthew Ahrens 	DB_DNODE_ENTER(db);
169934e8acefSMatthew Ahrens 	dn = DB_DNODE(db);
170034e8acefSMatthew Ahrens 	if (dr->dr_next != NULL || dnode_block_freed(dn, db->db_blkid))
170180901aeaSGeorge Wilson 		zp.zp_nopwrite = B_FALSE;
170234e8acefSMatthew Ahrens 	DB_DNODE_EXIT(db);
170380901aeaSGeorge Wilson 
1704c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
1705b24ab676SJeff Bonwick 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1706b24ab676SJeff Bonwick 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1707c717a561Smaybee 		/*
1708b24ab676SJeff Bonwick 		 * We have already issued a sync write for this buffer,
1709b24ab676SJeff Bonwick 		 * or this buffer has already been synced.  It could not
1710c717a561Smaybee 		 * have been dirtied since, or we would have cleared the state.
1711c717a561Smaybee 		 */
1712c717a561Smaybee 		mutex_exit(&db->db_mtx);
1713be6fd75aSMatthew Ahrens 		return (SET_ERROR(EALREADY));
1714c717a561Smaybee 	}
1715c717a561Smaybee 
1716b24ab676SJeff Bonwick 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1717c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1718fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1719e14bb325SJeff Bonwick 
1720b24ab676SJeff Bonwick 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1721b24ab676SJeff Bonwick 	dsa->dsa_dr = dr;
1722b24ab676SJeff Bonwick 	dsa->dsa_done = done;
1723b24ab676SJeff Bonwick 	dsa->dsa_zgd = zgd;
1724b24ab676SJeff Bonwick 	dsa->dsa_tx = NULL;
1725e14bb325SJeff Bonwick 
1726b24ab676SJeff Bonwick 	zio_nowait(arc_write(pio, os->os_spa, txg,
1727aad02571SSaso Kiselkov 	    bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db),
1728dcbf3bd6SGeorge Wilson 	    &zp, dmu_sync_ready, NULL, NULL, dmu_sync_done, dsa,
17298df0bcf0SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb));
1730e14bb325SJeff Bonwick 
1731b24ab676SJeff Bonwick 	return (0);
1732fa9e4066Sahrens }
1733fa9e4066Sahrens 
1734fa9e4066Sahrens int
1735fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
17369a686fbcSPaul Dagnelie     dmu_tx_t *tx)
1737fa9e4066Sahrens {
1738ea8dc4b6Seschrock 	dnode_t *dn;
1739ea8dc4b6Seschrock 	int err;
1740ea8dc4b6Seschrock 
1741503ad85cSMatthew Ahrens 	err = dnode_hold(os, object, FTAG, &dn);
1742ea8dc4b6Seschrock 	if (err)
1743ea8dc4b6Seschrock 		return (err);
1744ea8dc4b6Seschrock 	err = dnode_set_blksz(dn, size, ibs, tx);
1745fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1746fa9e4066Sahrens 	return (err);
1747fa9e4066Sahrens }
1748fa9e4066Sahrens 
1749fa9e4066Sahrens void
1750fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
17519a686fbcSPaul Dagnelie     dmu_tx_t *tx)
1752fa9e4066Sahrens {
1753ea8dc4b6Seschrock 	dnode_t *dn;
1754ea8dc4b6Seschrock 
17555d7b4d43SMatthew Ahrens 	/*
17565d7b4d43SMatthew Ahrens 	 * Send streams include each object's checksum function.  This
17575d7b4d43SMatthew Ahrens 	 * check ensures that the receiving system can understand the
17585d7b4d43SMatthew Ahrens 	 * checksum function transmitted.
17595d7b4d43SMatthew Ahrens 	 */
17605d7b4d43SMatthew Ahrens 	ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
17615d7b4d43SMatthew Ahrens 
17625d7b4d43SMatthew Ahrens 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
17635d7b4d43SMatthew Ahrens 	ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
1764fa9e4066Sahrens 	dn->dn_checksum = checksum;
1765fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1766fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1767fa9e4066Sahrens }
1768fa9e4066Sahrens 
1769fa9e4066Sahrens void
1770fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
17719a686fbcSPaul Dagnelie     dmu_tx_t *tx)
1772fa9e4066Sahrens {
1773ea8dc4b6Seschrock 	dnode_t *dn;
1774ea8dc4b6Seschrock 
17755d7b4d43SMatthew Ahrens 	/*
17765d7b4d43SMatthew Ahrens 	 * Send streams include each object's compression function.  This
17775d7b4d43SMatthew Ahrens 	 * check ensures that the receiving system can understand the
17785d7b4d43SMatthew Ahrens 	 * compression function transmitted.
17795d7b4d43SMatthew Ahrens 	 */
17805d7b4d43SMatthew Ahrens 	ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
17815d7b4d43SMatthew Ahrens 
17825d7b4d43SMatthew Ahrens 	VERIFY0(dnode_hold(os, object, FTAG, &dn));
1783fa9e4066Sahrens 	dn->dn_compress = compress;
1784fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1785fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1786fa9e4066Sahrens }
1787fa9e4066Sahrens 
1788b24ab676SJeff Bonwick int zfs_mdcomp_disable = 0;
1789b24ab676SJeff Bonwick 
1790edf345e6SMatthew Ahrens /*
1791edf345e6SMatthew Ahrens  * When the "redundant_metadata" property is set to "most", only indirect
1792edf345e6SMatthew Ahrens  * blocks of this level and higher will have an additional ditto block.
1793edf345e6SMatthew Ahrens  */
1794edf345e6SMatthew Ahrens int zfs_redundant_metadata_most_ditto_level = 2;
1795edf345e6SMatthew Ahrens 
1796b24ab676SJeff Bonwick void
1797b24ab676SJeff Bonwick dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1798b24ab676SJeff Bonwick {
1799b24ab676SJeff Bonwick 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1800ad135b5dSChristopher Siden 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
18011d8ccc7bSMark Shellenbaum 	    (wp & WP_SPILL));
1802b24ab676SJeff Bonwick 	enum zio_checksum checksum = os->os_checksum;
1803b24ab676SJeff Bonwick 	enum zio_compress compress = os->os_compress;
1804b24ab676SJeff Bonwick 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
18057540df39SGeorge Wilson 	boolean_t dedup = B_FALSE;
18067540df39SGeorge Wilson 	boolean_t nopwrite = B_FALSE;
1807b24ab676SJeff Bonwick 	boolean_t dedup_verify = os->os_dedup_verify;
1808b24ab676SJeff Bonwick 	int copies = os->os_copies;
1809b24ab676SJeff Bonwick 
1810b24ab676SJeff Bonwick 	/*
181180901aeaSGeorge Wilson 	 * We maintain different write policies for each of the following
181280901aeaSGeorge Wilson 	 * types of data:
181380901aeaSGeorge Wilson 	 *	 1. metadata
181480901aeaSGeorge Wilson 	 *	 2. preallocated blocks (i.e. level-0 blocks of a dump device)
181580901aeaSGeorge Wilson 	 *	 3. all other level 0 blocks
1816b24ab676SJeff Bonwick 	 */
1817b24ab676SJeff Bonwick 	if (ismd) {
1818b8289d24SDaniil Lunev 		if (zfs_mdcomp_disable) {
1819b8289d24SDaniil Lunev 			compress = ZIO_COMPRESS_EMPTY;
1820b8289d24SDaniil Lunev 		} else {
1821db1741f5SJustin T. Gibbs 			/*
1822db1741f5SJustin T. Gibbs 			 * XXX -- we should design a compression algorithm
1823db1741f5SJustin T. Gibbs 			 * that specializes in arrays of bps.
1824db1741f5SJustin T. Gibbs 			 */
1825db1741f5SJustin T. Gibbs 			compress = zio_compress_select(os->os_spa,
1826db1741f5SJustin T. Gibbs 			    ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
1827b8289d24SDaniil Lunev 		}
182880901aeaSGeorge Wilson 
1829b24ab676SJeff Bonwick 		/*
1830b24ab676SJeff Bonwick 		 * Metadata always gets checksummed.  If the data
1831b24ab676SJeff Bonwick 		 * checksum is multi-bit correctable, and it's not a
1832b24ab676SJeff Bonwick 		 * ZBT-style checksum, then it's suitable for metadata
1833b24ab676SJeff Bonwick 		 * as well.  Otherwise, the metadata checksum defaults
1834b24ab676SJeff Bonwick 		 * to fletcher4.
1835b24ab676SJeff Bonwick 		 */
183645818ee1SMatthew Ahrens 		if (!(zio_checksum_table[checksum].ci_flags &
183745818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_METADATA) ||
183845818ee1SMatthew Ahrens 		    (zio_checksum_table[checksum].ci_flags &
183945818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_EMBEDDED))
1840b24ab676SJeff Bonwick 			checksum = ZIO_CHECKSUM_FLETCHER_4;
1841edf345e6SMatthew Ahrens 
1842edf345e6SMatthew Ahrens 		if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||
1843edf345e6SMatthew Ahrens 		    (os->os_redundant_metadata ==
1844edf345e6SMatthew Ahrens 		    ZFS_REDUNDANT_METADATA_MOST &&
1845edf345e6SMatthew Ahrens 		    (level >= zfs_redundant_metadata_most_ditto_level ||
1846edf345e6SMatthew Ahrens 		    DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))))
1847edf345e6SMatthew Ahrens 			copies++;
184880901aeaSGeorge Wilson 	} else if (wp & WP_NOFILL) {
184980901aeaSGeorge Wilson 		ASSERT(level == 0);
185080901aeaSGeorge Wilson 
1851b24ab676SJeff Bonwick 		/*
185280901aeaSGeorge Wilson 		 * If we're writing preallocated blocks, we aren't actually
185380901aeaSGeorge Wilson 		 * writing them so don't set any policy properties.  These
185480901aeaSGeorge Wilson 		 * blocks are currently only used by an external subsystem
185580901aeaSGeorge Wilson 		 * outside of zfs (i.e. dump) and not written by the zio
185680901aeaSGeorge Wilson 		 * pipeline.
1857b24ab676SJeff Bonwick 		 */
185880901aeaSGeorge Wilson 		compress = ZIO_COMPRESS_OFF;
1859810e43b2SBill Pijewski 		checksum = ZIO_CHECKSUM_NOPARITY;
1860b24ab676SJeff Bonwick 	} else {
1861db1741f5SJustin T. Gibbs 		compress = zio_compress_select(os->os_spa, dn->dn_compress,
1862db1741f5SJustin T. Gibbs 		    compress);
1863b24ab676SJeff Bonwick 
186480901aeaSGeorge Wilson 		checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
186580901aeaSGeorge Wilson 		    zio_checksum_select(dn->dn_checksum, checksum) :
186680901aeaSGeorge Wilson 		    dedup_checksum;
1867b24ab676SJeff Bonwick 
186880901aeaSGeorge Wilson 		/*
186980901aeaSGeorge Wilson 		 * Determine dedup setting.  If we are in dmu_sync(),
187080901aeaSGeorge Wilson 		 * we won't actually dedup now because that's all
187180901aeaSGeorge Wilson 		 * done in syncing context; but we do want to use the
187280901aeaSGeorge Wilson 		 * dedup checkum.  If the checksum is not strong
187380901aeaSGeorge Wilson 		 * enough to ensure unique signatures, force
187480901aeaSGeorge Wilson 		 * dedup_verify.
187580901aeaSGeorge Wilson 		 */
187680901aeaSGeorge Wilson 		if (dedup_checksum != ZIO_CHECKSUM_OFF) {
187780901aeaSGeorge Wilson 			dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
187845818ee1SMatthew Ahrens 			if (!(zio_checksum_table[checksum].ci_flags &
187945818ee1SMatthew Ahrens 			    ZCHECKSUM_FLAG_DEDUP))
188080901aeaSGeorge Wilson 				dedup_verify = B_TRUE;
188180901aeaSGeorge Wilson 		}
1882b24ab676SJeff Bonwick 
188380901aeaSGeorge Wilson 		/*
188445818ee1SMatthew Ahrens 		 * Enable nopwrite if we have secure enough checksum
188545818ee1SMatthew Ahrens 		 * algorithm (see comment in zio_nop_write) and
188645818ee1SMatthew Ahrens 		 * compression is enabled.  We don't enable nopwrite if
188745818ee1SMatthew Ahrens 		 * dedup is enabled as the two features are mutually
188845818ee1SMatthew Ahrens 		 * exclusive.
188980901aeaSGeorge Wilson 		 */
189045818ee1SMatthew Ahrens 		nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
189145818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_NOPWRITE) &&
189280901aeaSGeorge Wilson 		    compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
1893b24ab676SJeff Bonwick 	}
1894b24ab676SJeff Bonwick 
1895b24ab676SJeff Bonwick 	zp->zp_checksum = checksum;
1896b24ab676SJeff Bonwick 	zp->zp_compress = compress;
18970a586ceaSMark Shellenbaum 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1898b24ab676SJeff Bonwick 	zp->zp_level = level;
1899edf345e6SMatthew Ahrens 	zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
1900b24ab676SJeff Bonwick 	zp->zp_dedup = dedup;
1901b24ab676SJeff Bonwick 	zp->zp_dedup_verify = dedup && dedup_verify;
190280901aeaSGeorge Wilson 	zp->zp_nopwrite = nopwrite;
1903b24ab676SJeff Bonwick }
1904b24ab676SJeff Bonwick 
1905fa9e4066Sahrens int
1906fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1907fa9e4066Sahrens {
1908fa9e4066Sahrens 	dnode_t *dn;
19092bcf0248SMax Grossman 	int err;
1910fa9e4066Sahrens 
1911fa9e4066Sahrens 	/*
1912fa9e4066Sahrens 	 * Sync any current changes before
1913fa9e4066Sahrens 	 * we go trundling through the block pointers.
1914fa9e4066Sahrens 	 */
19152bcf0248SMax Grossman 	err = dmu_object_wait_synced(os, object);
19162bcf0248SMax Grossman 	if (err) {
19172bcf0248SMax Grossman 		return (err);
1918fa9e4066Sahrens 	}
19192bcf0248SMax Grossman 
19202bcf0248SMax Grossman 	err = dnode_hold(os, object, FTAG, &dn);
19212bcf0248SMax Grossman 	if (err) {
19222bcf0248SMax Grossman 		return (err);
1923fa9e4066Sahrens 	}
1924fa9e4066Sahrens 
1925cdb0ab79Smaybee 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1926fa9e4066Sahrens 	dnode_rele(dn, FTAG);
1927fa9e4066Sahrens 
1928fa9e4066Sahrens 	return (err);
1929fa9e4066Sahrens }
1930fa9e4066Sahrens 
19312bcf0248SMax Grossman /*
19322bcf0248SMax Grossman  * Given the ZFS object, if it contains any dirty nodes
19332bcf0248SMax Grossman  * this function flushes all dirty blocks to disk. This
19342bcf0248SMax Grossman  * ensures the DMU object info is updated. A more efficient
19352bcf0248SMax Grossman  * future version might just find the TXG with the maximum
19362bcf0248SMax Grossman  * ID and wait for that to be synced.
19372bcf0248SMax Grossman  */
19382bcf0248SMax Grossman int
19399a686fbcSPaul Dagnelie dmu_object_wait_synced(objset_t *os, uint64_t object)
19409a686fbcSPaul Dagnelie {
19412bcf0248SMax Grossman 	dnode_t *dn;
19422bcf0248SMax Grossman 	int error, i;
19432bcf0248SMax Grossman 
19442bcf0248SMax Grossman 	error = dnode_hold(os, object, FTAG, &dn);
19452bcf0248SMax Grossman 	if (error) {
19462bcf0248SMax Grossman 		return (error);
19472bcf0248SMax Grossman 	}
19482bcf0248SMax Grossman 
19492bcf0248SMax Grossman 	for (i = 0; i < TXG_SIZE; i++) {
19502bcf0248SMax Grossman 		if (list_link_active(&dn->dn_dirty_link[i])) {
19512bcf0248SMax Grossman 			break;
19522bcf0248SMax Grossman 		}
19532bcf0248SMax Grossman 	}
19542bcf0248SMax Grossman 	dnode_rele(dn, FTAG);
19552bcf0248SMax Grossman 	if (i != TXG_SIZE) {
19562bcf0248SMax Grossman 		txg_wait_synced(dmu_objset_pool(os), 0);
19572bcf0248SMax Grossman 	}
19582bcf0248SMax Grossman 
19592bcf0248SMax Grossman 	return (0);
19602bcf0248SMax Grossman }
19612bcf0248SMax Grossman 
1962fa9e4066Sahrens void
1963fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1964fa9e4066Sahrens {
1965b24ab676SJeff Bonwick 	dnode_phys_t *dnp;
1966b24ab676SJeff Bonwick 
1967fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1968fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1969fa9e4066Sahrens 
1970b24ab676SJeff Bonwick 	dnp = dn->dn_phys;
1971b24ab676SJeff Bonwick 
1972fa9e4066Sahrens 	doi->doi_data_block_size = dn->dn_datablksz;
1973fa9e4066Sahrens 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1974fa9e4066Sahrens 	    1ULL << dn->dn_indblkshift : 0;
1975b24ab676SJeff Bonwick 	doi->doi_type = dn->dn_type;
1976b24ab676SJeff Bonwick 	doi->doi_bonus_type = dn->dn_bonustype;
1977b24ab676SJeff Bonwick 	doi->doi_bonus_size = dn->dn_bonuslen;
1978fa9e4066Sahrens 	doi->doi_indirection = dn->dn_nlevels;
1979fa9e4066Sahrens 	doi->doi_checksum = dn->dn_checksum;
1980fa9e4066Sahrens 	doi->doi_compress = dn->dn_compress;
1981e77d42eaSMatthew Ahrens 	doi->doi_nblkptr = dn->dn_nblkptr;
1982b24ab676SJeff Bonwick 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1983d0475637SMatthew Ahrens 	doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1984b24ab676SJeff Bonwick 	doi->doi_fill_count = 0;
1985b24ab676SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
19865d7b4d43SMatthew Ahrens 		doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
1987fa9e4066Sahrens 
1988fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1989fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1990fa9e4066Sahrens }
1991fa9e4066Sahrens 
1992fa9e4066Sahrens /*
1993fa9e4066Sahrens  * Get information on a DMU object.
1994fa9e4066Sahrens  * If doi is NULL, just indicates whether the object exists.
1995fa9e4066Sahrens  */
1996fa9e4066Sahrens int
1997fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1998fa9e4066Sahrens {
1999ea8dc4b6Seschrock 	dnode_t *dn;
2000503ad85cSMatthew Ahrens 	int err = dnode_hold(os, object, FTAG, &dn);
2001fa9e4066Sahrens 
2002ea8dc4b6Seschrock 	if (err)
2003ea8dc4b6Seschrock 		return (err);
2004fa9e4066Sahrens 
2005fa9e4066Sahrens 	if (doi != NULL)
2006fa9e4066Sahrens 		dmu_object_info_from_dnode(dn, doi);
2007fa9e4066Sahrens 
2008fa9e4066Sahrens 	dnode_rele(dn, FTAG);
2009fa9e4066Sahrens 	return (0);
2010fa9e4066Sahrens }
2011fa9e4066Sahrens 
2012fa9e4066Sahrens /*
2013fa9e4066Sahrens  * As above, but faster; can be used when you have a held dbuf in hand.
2014fa9e4066Sahrens  */
2015fa9e4066Sahrens void
2016744947dcSTom Erickson dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
2017fa9e4066Sahrens {
2018744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2019744947dcSTom Erickson 
2020744947dcSTom Erickson 	DB_DNODE_ENTER(db);
2021744947dcSTom Erickson 	dmu_object_info_from_dnode(DB_DNODE(db), doi);
2022744947dcSTom Erickson 	DB_DNODE_EXIT(db);
2023fa9e4066Sahrens }
2024fa9e4066Sahrens 
2025fa9e4066Sahrens /*
2026fa9e4066Sahrens  * Faster still when you only care about the size.
2027fa9e4066Sahrens  * This is specifically optimized for zfs_getattr().
2028fa9e4066Sahrens  */
2029fa9e4066Sahrens void
2030744947dcSTom Erickson dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
2031744947dcSTom Erickson     u_longlong_t *nblk512)
2032fa9e4066Sahrens {
2033744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2034744947dcSTom Erickson 	dnode_t *dn;
2035744947dcSTom Erickson 
2036744947dcSTom Erickson 	DB_DNODE_ENTER(db);
2037744947dcSTom Erickson 	dn = DB_DNODE(db);
2038fa9e4066Sahrens 
2039fa9e4066Sahrens 	*blksize = dn->dn_datablksz;
204099653d4eSeschrock 	/* add 1 for dnode space */
204199653d4eSeschrock 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
204299653d4eSeschrock 	    SPA_MINBLOCKSHIFT) + 1;
2043744947dcSTom Erickson 	DB_DNODE_EXIT(db);
2044fa9e4066Sahrens }
2045fa9e4066Sahrens 
2046fa9e4066Sahrens void
2047fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size)
2048fa9e4066Sahrens {
2049fa9e4066Sahrens 	uint64_t *buf = vbuf;
2050fa9e4066Sahrens 	size_t count = size >> 3;
2051fa9e4066Sahrens 	int i;
2052fa9e4066Sahrens 
2053fa9e4066Sahrens 	ASSERT((size & 7) == 0);
2054fa9e4066Sahrens 
2055fa9e4066Sahrens 	for (i = 0; i < count; i++)
2056fa9e4066Sahrens 		buf[i] = BSWAP_64(buf[i]);
2057fa9e4066Sahrens }
2058fa9e4066Sahrens 
2059fa9e4066Sahrens void
2060fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size)
2061fa9e4066Sahrens {
2062fa9e4066Sahrens 	uint32_t *buf = vbuf;
2063fa9e4066Sahrens 	size_t count = size >> 2;
2064fa9e4066Sahrens 	int i;
2065fa9e4066Sahrens 
2066fa9e4066Sahrens 	ASSERT((size & 3) == 0);
2067fa9e4066Sahrens 
2068fa9e4066Sahrens 	for (i = 0; i < count; i++)
2069fa9e4066Sahrens 		buf[i] = BSWAP_32(buf[i]);
2070fa9e4066Sahrens }
2071fa9e4066Sahrens 
2072fa9e4066Sahrens void
2073fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size)
2074fa9e4066Sahrens {
2075fa9e4066Sahrens 	uint16_t *buf = vbuf;
2076fa9e4066Sahrens 	size_t count = size >> 1;
2077fa9e4066Sahrens 	int i;
2078fa9e4066Sahrens 
2079fa9e4066Sahrens 	ASSERT((size & 1) == 0);
2080fa9e4066Sahrens 
2081fa9e4066Sahrens 	for (i = 0; i < count; i++)
2082fa9e4066Sahrens 		buf[i] = BSWAP_16(buf[i]);
2083fa9e4066Sahrens }
2084fa9e4066Sahrens 
2085fa9e4066Sahrens /* ARGSUSED */
2086fa9e4066Sahrens void
2087fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size)
2088fa9e4066Sahrens {
2089fa9e4066Sahrens }
2090fa9e4066Sahrens 
2091fa9e4066Sahrens void
2092fa9e4066Sahrens dmu_init(void)
2093fa9e4066Sahrens {
20943f9d6ad7SLin Ling 	zfs_dbgmsg_init();
2095744947dcSTom Erickson 	sa_cache_init();
2096744947dcSTom Erickson 	xuio_stat_init();
2097744947dcSTom Erickson 	dmu_objset_init();
2098fa9e4066Sahrens 	dnode_init();
20997cbf8b43SRich Morris 	zfetch_init();
2100fa94a07fSbrendan 	l2arc_init();
2101ce636f8bSMatthew Ahrens 	arc_init();
2102dcbf3bd6SGeorge Wilson 	dbuf_init();
2103fa9e4066Sahrens }
2104fa9e4066Sahrens 
2105fa9e4066Sahrens void
2106fa9e4066Sahrens dmu_fini(void)
2107fa9e4066Sahrens {
21083e30c24aSWill Andrews 	arc_fini(); /* arc depends on l2arc, so arc must go first */
2109ce636f8bSMatthew Ahrens 	l2arc_fini();
21107cbf8b43SRich Morris 	zfetch_fini();
2111fa9e4066Sahrens 	dbuf_fini();
2112744947dcSTom Erickson 	dnode_fini();
2113744947dcSTom Erickson 	dmu_objset_fini();
2114c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	xuio_stat_fini();
21150a586ceaSMark Shellenbaum 	sa_cache_fini();
21163f9d6ad7SLin Ling 	zfs_dbgmsg_fini();
2117fa9e4066Sahrens }
2118