xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <sys/dmu.h>
26 #include <sys/dmu_impl.h>
27 #include <sys/dmu_tx.h>
28 #include <sys/dbuf.h>
29 #include <sys/dnode.h>
30 #include <sys/zfs_context.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_traverse.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_prop.h>
38 #include <sys/dmu_zfetch.h>
39 #include <sys/zfs_ioctl.h>
40 #include <sys/zap.h>
41 #include <sys/zio_checksum.h>
42 #include <sys/sa.h>
43 #ifdef _KERNEL
44 #include <sys/vmsystm.h>
45 #include <sys/zfs_znode.h>
46 #endif
47 
48 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
49 	{	byteswap_uint8_array,	TRUE,	"unallocated"		},
50 	{	zap_byteswap,		TRUE,	"object directory"	},
51 	{	byteswap_uint64_array,	TRUE,	"object array"		},
52 	{	byteswap_uint8_array,	TRUE,	"packed nvlist"		},
53 	{	byteswap_uint64_array,	TRUE,	"packed nvlist size"	},
54 	{	byteswap_uint64_array,	TRUE,	"bplist"		},
55 	{	byteswap_uint64_array,	TRUE,	"bplist header"		},
56 	{	byteswap_uint64_array,	TRUE,	"SPA space map header"	},
57 	{	byteswap_uint64_array,	TRUE,	"SPA space map"		},
58 	{	byteswap_uint64_array,	TRUE,	"ZIL intent log"	},
59 	{	dnode_buf_byteswap,	TRUE,	"DMU dnode"		},
60 	{	dmu_objset_byteswap,	TRUE,	"DMU objset"		},
61 	{	byteswap_uint64_array,	TRUE,	"DSL directory"		},
62 	{	zap_byteswap,		TRUE,	"DSL directory child map"},
63 	{	zap_byteswap,		TRUE,	"DSL dataset snap map"	},
64 	{	zap_byteswap,		TRUE,	"DSL props"		},
65 	{	byteswap_uint64_array,	TRUE,	"DSL dataset"		},
66 	{	zfs_znode_byteswap,	TRUE,	"ZFS znode"		},
67 	{	zfs_oldacl_byteswap,	TRUE,	"ZFS V0 ACL"		},
68 	{	byteswap_uint8_array,	FALSE,	"ZFS plain file"	},
69 	{	zap_byteswap,		TRUE,	"ZFS directory"		},
70 	{	zap_byteswap,		TRUE,	"ZFS master node"	},
71 	{	zap_byteswap,		TRUE,	"ZFS delete queue"	},
72 	{	byteswap_uint8_array,	FALSE,	"zvol object"		},
73 	{	zap_byteswap,		TRUE,	"zvol prop"		},
74 	{	byteswap_uint8_array,	FALSE,	"other uint8[]"		},
75 	{	byteswap_uint64_array,	FALSE,	"other uint64[]"	},
76 	{	zap_byteswap,		TRUE,	"other ZAP"		},
77 	{	zap_byteswap,		TRUE,	"persistent error log"	},
78 	{	byteswap_uint8_array,	TRUE,	"SPA history"		},
79 	{	byteswap_uint64_array,	TRUE,	"SPA history offsets"	},
80 	{	zap_byteswap,		TRUE,	"Pool properties"	},
81 	{	zap_byteswap,		TRUE,	"DSL permissions"	},
82 	{	zfs_acl_byteswap,	TRUE,	"ZFS ACL"		},
83 	{	byteswap_uint8_array,	TRUE,	"ZFS SYSACL"		},
84 	{	byteswap_uint8_array,	TRUE,	"FUID table"		},
85 	{	byteswap_uint64_array,	TRUE,	"FUID table size"	},
86 	{	zap_byteswap,		TRUE,	"DSL dataset next clones"},
87 	{	zap_byteswap,		TRUE,	"scan work queue"	},
88 	{	zap_byteswap,		TRUE,	"ZFS user/group used"	},
89 	{	zap_byteswap,		TRUE,	"ZFS user/group quota"	},
90 	{	zap_byteswap,		TRUE,	"snapshot refcount tags"},
91 	{	zap_byteswap,		TRUE,	"DDT ZAP algorithm"	},
92 	{	zap_byteswap,		TRUE,	"DDT statistics"	},
93 	{	byteswap_uint8_array,	TRUE,	"System attributes"	},
94 	{	zap_byteswap,		TRUE,	"SA master node"	},
95 	{	zap_byteswap,		TRUE,	"SA attr registration"	},
96 	{	zap_byteswap,		TRUE,	"SA attr layouts"	},
97 	{	zap_byteswap,		TRUE,	"scan translations"	},
98 	{	byteswap_uint8_array,	FALSE,	"deduplicated block"	},
99 };
100 
101 int
102 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
103     void *tag, dmu_buf_t **dbp, int flags)
104 {
105 	dnode_t *dn;
106 	uint64_t blkid;
107 	dmu_buf_impl_t *db;
108 	int err;
109 	int db_flags = DB_RF_CANFAIL;
110 
111 	if (flags & DMU_READ_NO_PREFETCH)
112 		db_flags |= DB_RF_NOPREFETCH;
113 
114 	err = dnode_hold(os, object, FTAG, &dn);
115 	if (err)
116 		return (err);
117 	blkid = dbuf_whichblock(dn, offset);
118 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
119 	db = dbuf_hold(dn, blkid, tag);
120 	rw_exit(&dn->dn_struct_rwlock);
121 	if (db == NULL) {
122 		err = EIO;
123 	} else {
124 		err = dbuf_read(db, NULL, db_flags);
125 		if (err) {
126 			dbuf_rele(db, tag);
127 			db = NULL;
128 		}
129 	}
130 
131 	dnode_rele(dn, FTAG);
132 	*dbp = &db->db;
133 	return (err);
134 }
135 
136 int
137 dmu_bonus_max(void)
138 {
139 	return (DN_MAX_BONUSLEN);
140 }
141 
142 int
143 dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx)
144 {
145 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
146 
147 	if (dn->dn_bonus != (dmu_buf_impl_t *)db)
148 		return (EINVAL);
149 	if (newsize < 0 || newsize > db->db_size)
150 		return (EINVAL);
151 	dnode_setbonuslen(dn, newsize, tx);
152 	return (0);
153 }
154 
155 int
156 dmu_set_bonustype(dmu_buf_t *db, dmu_object_type_t type, dmu_tx_t *tx)
157 {
158 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
159 
160 	if (type > DMU_OT_NUMTYPES)
161 		return (EINVAL);
162 
163 	if (dn->dn_bonus != (dmu_buf_impl_t *)db)
164 		return (EINVAL);
165 
166 	dnode_setbonus_type(dn, type, tx);
167 	return (0);
168 }
169 
170 int
171 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
172 {
173 	dnode_t *dn;
174 	int error;
175 
176 	error = dnode_hold(os, object, FTAG, &dn);
177 	dbuf_rm_spill(dn, tx);
178 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
179 	dnode_rm_spill(dn, tx);
180 	rw_exit(&dn->dn_struct_rwlock);
181 	dnode_rele(dn, FTAG);
182 	return (error);
183 }
184 
185 /*
186  * returns ENOENT, EIO, or 0.
187  */
188 int
189 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
190 {
191 	dnode_t *dn;
192 	dmu_buf_impl_t *db;
193 	int error;
194 
195 	error = dnode_hold(os, object, FTAG, &dn);
196 	if (error)
197 		return (error);
198 
199 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
200 	if (dn->dn_bonus == NULL) {
201 		rw_exit(&dn->dn_struct_rwlock);
202 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
203 		if (dn->dn_bonus == NULL)
204 			dbuf_create_bonus(dn);
205 	}
206 	db = dn->dn_bonus;
207 	rw_exit(&dn->dn_struct_rwlock);
208 
209 	/* as long as the bonus buf is held, the dnode will be held */
210 	if (refcount_add(&db->db_holds, tag) == 1)
211 		VERIFY(dnode_add_ref(dn, db));
212 
213 	dnode_rele(dn, FTAG);
214 
215 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
216 
217 	*dbp = &db->db;
218 	return (0);
219 }
220 
221 /*
222  * returns ENOENT, EIO, or 0.
223  *
224  * This interface will allocate a blank spill dbuf when a spill blk
225  * doesn't already exist on the dnode.
226  *
227  * if you only want to find an already existing spill db, then
228  * dmu_spill_hold_existing() should be used.
229  */
230 int
231 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
232 {
233 	dmu_buf_impl_t *db = NULL;
234 	int err;
235 
236 	if ((flags & DB_RF_HAVESTRUCT) == 0)
237 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
238 
239 	db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
240 
241 	if ((flags & DB_RF_HAVESTRUCT) == 0)
242 		rw_exit(&dn->dn_struct_rwlock);
243 
244 	ASSERT(db != NULL);
245 	err = dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | flags);
246 	*dbp = &db->db;
247 	return (err);
248 }
249 
250 int
251 dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
252 {
253 	dnode_t *dn = ((dmu_buf_impl_t *)bonus)->db_dnode;
254 	int err;
255 
256 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA)
257 		return (EINVAL);
258 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
259 
260 	if (!dn->dn_have_spill) {
261 		rw_exit(&dn->dn_struct_rwlock);
262 		return (ENOENT);
263 	}
264 	err = dmu_spill_hold_by_dnode(dn, DB_RF_HAVESTRUCT, tag, dbp);
265 	rw_exit(&dn->dn_struct_rwlock);
266 	return (err);
267 }
268 
269 int
270 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
271 {
272 	return (dmu_spill_hold_by_dnode(((dmu_buf_impl_t *)bonus)->db_dnode,
273 	    0, tag, dbp));
274 }
275 
276 /*
277  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
278  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
279  * and can induce severe lock contention when writing to several files
280  * whose dnodes are in the same block.
281  */
282 static int
283 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
284     int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
285 {
286 	dsl_pool_t *dp = NULL;
287 	dmu_buf_t **dbp;
288 	uint64_t blkid, nblks, i;
289 	uint32_t dbuf_flags;
290 	int err;
291 	zio_t *zio;
292 	hrtime_t start;
293 
294 	ASSERT(length <= DMU_MAX_ACCESS);
295 
296 	dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;
297 	if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz)
298 		dbuf_flags |= DB_RF_NOPREFETCH;
299 
300 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
301 	if (dn->dn_datablkshift) {
302 		int blkshift = dn->dn_datablkshift;
303 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
304 		    P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
305 	} else {
306 		if (offset + length > dn->dn_datablksz) {
307 			zfs_panic_recover("zfs: accessing past end of object "
308 			    "%llx/%llx (size=%u access=%llu+%llu)",
309 			    (longlong_t)dn->dn_objset->
310 			    os_dsl_dataset->ds_object,
311 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
312 			    (longlong_t)offset, (longlong_t)length);
313 			rw_exit(&dn->dn_struct_rwlock);
314 			return (EIO);
315 		}
316 		nblks = 1;
317 	}
318 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
319 
320 	if (dn->dn_objset->os_dsl_dataset)
321 		dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
322 	if (dp && dsl_pool_sync_context(dp))
323 		start = gethrtime();
324 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
325 	blkid = dbuf_whichblock(dn, offset);
326 	for (i = 0; i < nblks; i++) {
327 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
328 		if (db == NULL) {
329 			rw_exit(&dn->dn_struct_rwlock);
330 			dmu_buf_rele_array(dbp, nblks, tag);
331 			zio_nowait(zio);
332 			return (EIO);
333 		}
334 		/* initiate async i/o */
335 		if (read) {
336 			(void) dbuf_read(db, zio, dbuf_flags);
337 		}
338 		dbp[i] = &db->db;
339 	}
340 	rw_exit(&dn->dn_struct_rwlock);
341 
342 	/* wait for async i/o */
343 	err = zio_wait(zio);
344 	/* track read overhead when we are in sync context */
345 	if (dp && dsl_pool_sync_context(dp))
346 		dp->dp_read_overhead += gethrtime() - start;
347 	if (err) {
348 		dmu_buf_rele_array(dbp, nblks, tag);
349 		return (err);
350 	}
351 
352 	/* wait for other io to complete */
353 	if (read) {
354 		for (i = 0; i < nblks; i++) {
355 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
356 			mutex_enter(&db->db_mtx);
357 			while (db->db_state == DB_READ ||
358 			    db->db_state == DB_FILL)
359 				cv_wait(&db->db_changed, &db->db_mtx);
360 			if (db->db_state == DB_UNCACHED)
361 				err = EIO;
362 			mutex_exit(&db->db_mtx);
363 			if (err) {
364 				dmu_buf_rele_array(dbp, nblks, tag);
365 				return (err);
366 			}
367 		}
368 	}
369 
370 	*numbufsp = nblks;
371 	*dbpp = dbp;
372 	return (0);
373 }
374 
375 static int
376 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
377     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
378 {
379 	dnode_t *dn;
380 	int err;
381 
382 	err = dnode_hold(os, object, FTAG, &dn);
383 	if (err)
384 		return (err);
385 
386 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
387 	    numbufsp, dbpp, DMU_READ_PREFETCH);
388 
389 	dnode_rele(dn, FTAG);
390 
391 	return (err);
392 }
393 
394 int
395 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
396     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
397 {
398 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
399 	int err;
400 
401 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
402 	    numbufsp, dbpp, DMU_READ_PREFETCH);
403 
404 	return (err);
405 }
406 
407 void
408 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
409 {
410 	int i;
411 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
412 
413 	if (numbufs == 0)
414 		return;
415 
416 	for (i = 0; i < numbufs; i++) {
417 		if (dbp[i])
418 			dbuf_rele(dbp[i], tag);
419 	}
420 
421 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
422 }
423 
424 void
425 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
426 {
427 	dnode_t *dn;
428 	uint64_t blkid;
429 	int nblks, i, err;
430 
431 	if (zfs_prefetch_disable)
432 		return;
433 
434 	if (len == 0) {  /* they're interested in the bonus buffer */
435 		dn = os->os_meta_dnode;
436 
437 		if (object == 0 || object >= DN_MAX_OBJECT)
438 			return;
439 
440 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
441 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
442 		dbuf_prefetch(dn, blkid);
443 		rw_exit(&dn->dn_struct_rwlock);
444 		return;
445 	}
446 
447 	/*
448 	 * XXX - Note, if the dnode for the requested object is not
449 	 * already cached, we will do a *synchronous* read in the
450 	 * dnode_hold() call.  The same is true for any indirects.
451 	 */
452 	err = dnode_hold(os, object, FTAG, &dn);
453 	if (err != 0)
454 		return;
455 
456 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
457 	if (dn->dn_datablkshift) {
458 		int blkshift = dn->dn_datablkshift;
459 		nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
460 		    P2ALIGN(offset, 1<<blkshift)) >> blkshift;
461 	} else {
462 		nblks = (offset < dn->dn_datablksz);
463 	}
464 
465 	if (nblks != 0) {
466 		blkid = dbuf_whichblock(dn, offset);
467 		for (i = 0; i < nblks; i++)
468 			dbuf_prefetch(dn, blkid+i);
469 	}
470 
471 	rw_exit(&dn->dn_struct_rwlock);
472 
473 	dnode_rele(dn, FTAG);
474 }
475 
476 /*
477  * Get the next "chunk" of file data to free.  We traverse the file from
478  * the end so that the file gets shorter over time (if we crashes in the
479  * middle, this will leave us in a better state).  We find allocated file
480  * data by simply searching the allocated level 1 indirects.
481  */
482 static int
483 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit)
484 {
485 	uint64_t len = *start - limit;
486 	uint64_t blkcnt = 0;
487 	uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1));
488 	uint64_t iblkrange =
489 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
490 
491 	ASSERT(limit <= *start);
492 
493 	if (len <= iblkrange * maxblks) {
494 		*start = limit;
495 		return (0);
496 	}
497 	ASSERT(ISP2(iblkrange));
498 
499 	while (*start > limit && blkcnt < maxblks) {
500 		int err;
501 
502 		/* find next allocated L1 indirect */
503 		err = dnode_next_offset(dn,
504 		    DNODE_FIND_BACKWARDS, start, 2, 1, 0);
505 
506 		/* if there are no more, then we are done */
507 		if (err == ESRCH) {
508 			*start = limit;
509 			return (0);
510 		} else if (err) {
511 			return (err);
512 		}
513 		blkcnt += 1;
514 
515 		/* reset offset to end of "next" block back */
516 		*start = P2ALIGN(*start, iblkrange);
517 		if (*start <= limit)
518 			*start = limit;
519 		else
520 			*start -= 1;
521 	}
522 	return (0);
523 }
524 
525 static int
526 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
527     uint64_t length, boolean_t free_dnode)
528 {
529 	dmu_tx_t *tx;
530 	uint64_t object_size, start, end, len;
531 	boolean_t trunc = (length == DMU_OBJECT_END);
532 	int align, err;
533 
534 	align = 1 << dn->dn_datablkshift;
535 	ASSERT(align > 0);
536 	object_size = align == 1 ? dn->dn_datablksz :
537 	    (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
538 
539 	end = offset + length;
540 	if (trunc || end > object_size)
541 		end = object_size;
542 	if (end <= offset)
543 		return (0);
544 	length = end - offset;
545 
546 	while (length) {
547 		start = end;
548 		/* assert(offset <= start) */
549 		err = get_next_chunk(dn, &start, offset);
550 		if (err)
551 			return (err);
552 		len = trunc ? DMU_OBJECT_END : end - start;
553 
554 		tx = dmu_tx_create(os);
555 		dmu_tx_hold_free(tx, dn->dn_object, start, len);
556 		err = dmu_tx_assign(tx, TXG_WAIT);
557 		if (err) {
558 			dmu_tx_abort(tx);
559 			return (err);
560 		}
561 
562 		dnode_free_range(dn, start, trunc ? -1 : len, tx);
563 
564 		if (start == 0 && free_dnode) {
565 			ASSERT(trunc);
566 			dnode_free(dn, tx);
567 		}
568 
569 		length -= end - start;
570 
571 		dmu_tx_commit(tx);
572 		end = start;
573 	}
574 	return (0);
575 }
576 
577 int
578 dmu_free_long_range(objset_t *os, uint64_t object,
579     uint64_t offset, uint64_t length)
580 {
581 	dnode_t *dn;
582 	int err;
583 
584 	err = dnode_hold(os, object, FTAG, &dn);
585 	if (err != 0)
586 		return (err);
587 	err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
588 	dnode_rele(dn, FTAG);
589 	return (err);
590 }
591 
592 int
593 dmu_free_object(objset_t *os, uint64_t object)
594 {
595 	dnode_t *dn;
596 	dmu_tx_t *tx;
597 	int err;
598 
599 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
600 	    FTAG, &dn);
601 	if (err != 0)
602 		return (err);
603 	if (dn->dn_nlevels == 1) {
604 		tx = dmu_tx_create(os);
605 		dmu_tx_hold_bonus(tx, object);
606 		dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
607 		err = dmu_tx_assign(tx, TXG_WAIT);
608 		if (err == 0) {
609 			dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
610 			dnode_free(dn, tx);
611 			dmu_tx_commit(tx);
612 		} else {
613 			dmu_tx_abort(tx);
614 		}
615 	} else {
616 		err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
617 	}
618 	dnode_rele(dn, FTAG);
619 	return (err);
620 }
621 
622 int
623 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
624     uint64_t size, dmu_tx_t *tx)
625 {
626 	dnode_t *dn;
627 	int err = dnode_hold(os, object, FTAG, &dn);
628 	if (err)
629 		return (err);
630 	ASSERT(offset < UINT64_MAX);
631 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
632 	dnode_free_range(dn, offset, size, tx);
633 	dnode_rele(dn, FTAG);
634 	return (0);
635 }
636 
637 int
638 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
639     void *buf, uint32_t flags)
640 {
641 	dnode_t *dn;
642 	dmu_buf_t **dbp;
643 	int numbufs, err;
644 
645 	err = dnode_hold(os, object, FTAG, &dn);
646 	if (err)
647 		return (err);
648 
649 	/*
650 	 * Deal with odd block sizes, where there can't be data past the first
651 	 * block.  If we ever do the tail block optimization, we will need to
652 	 * handle that here as well.
653 	 */
654 	if (dn->dn_maxblkid == 0) {
655 		int newsz = offset > dn->dn_datablksz ? 0 :
656 		    MIN(size, dn->dn_datablksz - offset);
657 		bzero((char *)buf + newsz, size - newsz);
658 		size = newsz;
659 	}
660 
661 	while (size > 0) {
662 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
663 		int i;
664 
665 		/*
666 		 * NB: we could do this block-at-a-time, but it's nice
667 		 * to be reading in parallel.
668 		 */
669 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
670 		    TRUE, FTAG, &numbufs, &dbp, flags);
671 		if (err)
672 			break;
673 
674 		for (i = 0; i < numbufs; i++) {
675 			int tocpy;
676 			int bufoff;
677 			dmu_buf_t *db = dbp[i];
678 
679 			ASSERT(size > 0);
680 
681 			bufoff = offset - db->db_offset;
682 			tocpy = (int)MIN(db->db_size - bufoff, size);
683 
684 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
685 
686 			offset += tocpy;
687 			size -= tocpy;
688 			buf = (char *)buf + tocpy;
689 		}
690 		dmu_buf_rele_array(dbp, numbufs, FTAG);
691 	}
692 	dnode_rele(dn, FTAG);
693 	return (err);
694 }
695 
696 void
697 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
698     const void *buf, dmu_tx_t *tx)
699 {
700 	dmu_buf_t **dbp;
701 	int numbufs, i;
702 
703 	if (size == 0)
704 		return;
705 
706 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
707 	    FALSE, FTAG, &numbufs, &dbp));
708 
709 	for (i = 0; i < numbufs; i++) {
710 		int tocpy;
711 		int bufoff;
712 		dmu_buf_t *db = dbp[i];
713 
714 		ASSERT(size > 0);
715 
716 		bufoff = offset - db->db_offset;
717 		tocpy = (int)MIN(db->db_size - bufoff, size);
718 
719 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
720 
721 		if (tocpy == db->db_size)
722 			dmu_buf_will_fill(db, tx);
723 		else
724 			dmu_buf_will_dirty(db, tx);
725 
726 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
727 
728 		if (tocpy == db->db_size)
729 			dmu_buf_fill_done(db, tx);
730 
731 		offset += tocpy;
732 		size -= tocpy;
733 		buf = (char *)buf + tocpy;
734 	}
735 	dmu_buf_rele_array(dbp, numbufs, FTAG);
736 }
737 
738 void
739 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
740     dmu_tx_t *tx)
741 {
742 	dmu_buf_t **dbp;
743 	int numbufs, i;
744 
745 	if (size == 0)
746 		return;
747 
748 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
749 	    FALSE, FTAG, &numbufs, &dbp));
750 
751 	for (i = 0; i < numbufs; i++) {
752 		dmu_buf_t *db = dbp[i];
753 
754 		dmu_buf_will_not_fill(db, tx);
755 	}
756 	dmu_buf_rele_array(dbp, numbufs, FTAG);
757 }
758 
759 /*
760  * DMU support for xuio
761  */
762 kstat_t *xuio_ksp = NULL;
763 
764 int
765 dmu_xuio_init(xuio_t *xuio, int nblk)
766 {
767 	dmu_xuio_t *priv;
768 	uio_t *uio = &xuio->xu_uio;
769 
770 	uio->uio_iovcnt = nblk;
771 	uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
772 
773 	priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
774 	priv->cnt = nblk;
775 	priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
776 	priv->iovp = uio->uio_iov;
777 	XUIO_XUZC_PRIV(xuio) = priv;
778 
779 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
780 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
781 	else
782 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
783 
784 	return (0);
785 }
786 
787 void
788 dmu_xuio_fini(xuio_t *xuio)
789 {
790 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
791 	int nblk = priv->cnt;
792 
793 	kmem_free(priv->iovp, nblk * sizeof (iovec_t));
794 	kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
795 	kmem_free(priv, sizeof (dmu_xuio_t));
796 
797 	if (XUIO_XUZC_RW(xuio) == UIO_READ)
798 		XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
799 	else
800 		XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
801 }
802 
803 /*
804  * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
805  * and increase priv->next by 1.
806  */
807 int
808 dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
809 {
810 	struct iovec *iov;
811 	uio_t *uio = &xuio->xu_uio;
812 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
813 	int i = priv->next++;
814 
815 	ASSERT(i < priv->cnt);
816 	ASSERT(off + n <= arc_buf_size(abuf));
817 	iov = uio->uio_iov + i;
818 	iov->iov_base = (char *)abuf->b_data + off;
819 	iov->iov_len = n;
820 	priv->bufs[i] = abuf;
821 	return (0);
822 }
823 
824 int
825 dmu_xuio_cnt(xuio_t *xuio)
826 {
827 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
828 	return (priv->cnt);
829 }
830 
831 arc_buf_t *
832 dmu_xuio_arcbuf(xuio_t *xuio, int i)
833 {
834 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
835 
836 	ASSERT(i < priv->cnt);
837 	return (priv->bufs[i]);
838 }
839 
840 void
841 dmu_xuio_clear(xuio_t *xuio, int i)
842 {
843 	dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
844 
845 	ASSERT(i < priv->cnt);
846 	priv->bufs[i] = NULL;
847 }
848 
849 static void
850 xuio_stat_init(void)
851 {
852 	xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
853 	    KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
854 	    KSTAT_FLAG_VIRTUAL);
855 	if (xuio_ksp != NULL) {
856 		xuio_ksp->ks_data = &xuio_stats;
857 		kstat_install(xuio_ksp);
858 	}
859 }
860 
861 static void
862 xuio_stat_fini(void)
863 {
864 	if (xuio_ksp != NULL) {
865 		kstat_delete(xuio_ksp);
866 		xuio_ksp = NULL;
867 	}
868 }
869 
870 void
871 xuio_stat_wbuf_copied()
872 {
873 	XUIOSTAT_BUMP(xuiostat_wbuf_copied);
874 }
875 
876 void
877 xuio_stat_wbuf_nocopy()
878 {
879 	XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
880 }
881 
882 #ifdef _KERNEL
883 int
884 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
885 {
886 	dmu_buf_t **dbp;
887 	int numbufs, i, err;
888 	xuio_t *xuio = NULL;
889 
890 	/*
891 	 * NB: we could do this block-at-a-time, but it's nice
892 	 * to be reading in parallel.
893 	 */
894 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
895 	    &numbufs, &dbp);
896 	if (err)
897 		return (err);
898 
899 	if (uio->uio_extflg == UIO_XUIO)
900 		xuio = (xuio_t *)uio;
901 
902 	for (i = 0; i < numbufs; i++) {
903 		int tocpy;
904 		int bufoff;
905 		dmu_buf_t *db = dbp[i];
906 
907 		ASSERT(size > 0);
908 
909 		bufoff = uio->uio_loffset - db->db_offset;
910 		tocpy = (int)MIN(db->db_size - bufoff, size);
911 
912 		if (xuio) {
913 			dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
914 			arc_buf_t *dbuf_abuf = dbi->db_buf;
915 			arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
916 			err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
917 			if (!err) {
918 				uio->uio_resid -= tocpy;
919 				uio->uio_loffset += tocpy;
920 			}
921 
922 			if (abuf == dbuf_abuf)
923 				XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
924 			else
925 				XUIOSTAT_BUMP(xuiostat_rbuf_copied);
926 		} else {
927 			err = uiomove((char *)db->db_data + bufoff, tocpy,
928 			    UIO_READ, uio);
929 		}
930 		if (err)
931 			break;
932 
933 		size -= tocpy;
934 	}
935 	dmu_buf_rele_array(dbp, numbufs, FTAG);
936 
937 	return (err);
938 }
939 
940 static int
941 dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
942 {
943 	dmu_buf_t **dbp;
944 	int numbufs;
945 	int err = 0;
946 	int i;
947 
948 	err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
949 	    FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
950 	if (err)
951 		return (err);
952 
953 	for (i = 0; i < numbufs; i++) {
954 		int tocpy;
955 		int bufoff;
956 		dmu_buf_t *db = dbp[i];
957 
958 		ASSERT(size > 0);
959 
960 		bufoff = uio->uio_loffset - db->db_offset;
961 		tocpy = (int)MIN(db->db_size - bufoff, size);
962 
963 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
964 
965 		if (tocpy == db->db_size)
966 			dmu_buf_will_fill(db, tx);
967 		else
968 			dmu_buf_will_dirty(db, tx);
969 
970 		/*
971 		 * XXX uiomove could block forever (eg. nfs-backed
972 		 * pages).  There needs to be a uiolockdown() function
973 		 * to lock the pages in memory, so that uiomove won't
974 		 * block.
975 		 */
976 		err = uiomove((char *)db->db_data + bufoff, tocpy,
977 		    UIO_WRITE, uio);
978 
979 		if (tocpy == db->db_size)
980 			dmu_buf_fill_done(db, tx);
981 
982 		if (err)
983 			break;
984 
985 		size -= tocpy;
986 	}
987 
988 	dmu_buf_rele_array(dbp, numbufs, FTAG);
989 	return (err);
990 }
991 
992 int
993 dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
994     dmu_tx_t *tx)
995 {
996 	if (size == 0)
997 		return (0);
998 
999 	return (dmu_write_uio_dnode(((dmu_buf_impl_t *)zdb)->db_dnode,
1000 	    uio, size, tx));
1001 }
1002 
1003 int
1004 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
1005     dmu_tx_t *tx)
1006 {
1007 	dnode_t *dn;
1008 	int err;
1009 
1010 	if (size == 0)
1011 		return (0);
1012 
1013 	err = dnode_hold(os, object, FTAG, &dn);
1014 	if (err)
1015 		return (err);
1016 
1017 	err = dmu_write_uio_dnode(dn, uio, size, tx);
1018 
1019 	dnode_rele(dn, FTAG);
1020 
1021 	return (err);
1022 }
1023 
1024 int
1025 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1026     page_t *pp, dmu_tx_t *tx)
1027 {
1028 	dmu_buf_t **dbp;
1029 	int numbufs, i;
1030 	int err;
1031 
1032 	if (size == 0)
1033 		return (0);
1034 
1035 	err = dmu_buf_hold_array(os, object, offset, size,
1036 	    FALSE, FTAG, &numbufs, &dbp);
1037 	if (err)
1038 		return (err);
1039 
1040 	for (i = 0; i < numbufs; i++) {
1041 		int tocpy, copied, thiscpy;
1042 		int bufoff;
1043 		dmu_buf_t *db = dbp[i];
1044 		caddr_t va;
1045 
1046 		ASSERT(size > 0);
1047 		ASSERT3U(db->db_size, >=, PAGESIZE);
1048 
1049 		bufoff = offset - db->db_offset;
1050 		tocpy = (int)MIN(db->db_size - bufoff, size);
1051 
1052 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1053 
1054 		if (tocpy == db->db_size)
1055 			dmu_buf_will_fill(db, tx);
1056 		else
1057 			dmu_buf_will_dirty(db, tx);
1058 
1059 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
1060 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
1061 			thiscpy = MIN(PAGESIZE, tocpy - copied);
1062 			va = zfs_map_page(pp, S_READ);
1063 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
1064 			zfs_unmap_page(pp, va);
1065 			pp = pp->p_next;
1066 			bufoff += PAGESIZE;
1067 		}
1068 
1069 		if (tocpy == db->db_size)
1070 			dmu_buf_fill_done(db, tx);
1071 
1072 		offset += tocpy;
1073 		size -= tocpy;
1074 	}
1075 	dmu_buf_rele_array(dbp, numbufs, FTAG);
1076 	return (err);
1077 }
1078 #endif
1079 
1080 /*
1081  * Allocate a loaned anonymous arc buffer.
1082  */
1083 arc_buf_t *
1084 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1085 {
1086 	dnode_t *dn = ((dmu_buf_impl_t *)handle)->db_dnode;
1087 
1088 	return (arc_loan_buf(dn->dn_objset->os_spa, size));
1089 }
1090 
1091 /*
1092  * Free a loaned arc buffer.
1093  */
1094 void
1095 dmu_return_arcbuf(arc_buf_t *buf)
1096 {
1097 	arc_return_buf(buf, FTAG);
1098 	VERIFY(arc_buf_remove_ref(buf, FTAG) == 1);
1099 }
1100 
1101 /*
1102  * When possible directly assign passed loaned arc buffer to a dbuf.
1103  * If this is not possible copy the contents of passed arc buf via
1104  * dmu_write().
1105  */
1106 void
1107 dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1108     dmu_tx_t *tx)
1109 {
1110 	dnode_t *dn = ((dmu_buf_impl_t *)handle)->db_dnode;
1111 	dmu_buf_impl_t *db;
1112 	uint32_t blksz = (uint32_t)arc_buf_size(buf);
1113 	uint64_t blkid;
1114 
1115 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1116 	blkid = dbuf_whichblock(dn, offset);
1117 	VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
1118 	rw_exit(&dn->dn_struct_rwlock);
1119 
1120 	if (offset == db->db.db_offset && blksz == db->db.db_size) {
1121 		dbuf_assign_arcbuf(db, buf, tx);
1122 		dbuf_rele(db, FTAG);
1123 	} else {
1124 		dbuf_rele(db, FTAG);
1125 		dmu_write(dn->dn_objset, dn->dn_object, offset, blksz,
1126 		    buf->b_data, tx);
1127 		dmu_return_arcbuf(buf);
1128 		XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1129 	}
1130 }
1131 
1132 typedef struct {
1133 	dbuf_dirty_record_t	*dsa_dr;
1134 	dmu_sync_cb_t		*dsa_done;
1135 	zgd_t			*dsa_zgd;
1136 	dmu_tx_t		*dsa_tx;
1137 } dmu_sync_arg_t;
1138 
1139 /* ARGSUSED */
1140 static void
1141 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1142 {
1143 	dmu_sync_arg_t *dsa = varg;
1144 	dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1145 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1146 	blkptr_t *bp = zio->io_bp;
1147 
1148 	if (zio->io_error == 0) {
1149 		if (BP_IS_HOLE(bp)) {
1150 			/*
1151 			 * A block of zeros may compress to a hole, but the
1152 			 * block size still needs to be known for replay.
1153 			 */
1154 			BP_SET_LSIZE(bp, db->db_size);
1155 		} else {
1156 			ASSERT(BP_GET_TYPE(bp) == dn->dn_type);
1157 			ASSERT(BP_GET_LEVEL(bp) == 0);
1158 			bp->blk_fill = 1;
1159 		}
1160 	}
1161 }
1162 
1163 static void
1164 dmu_sync_late_arrival_ready(zio_t *zio)
1165 {
1166 	dmu_sync_ready(zio, NULL, zio->io_private);
1167 }
1168 
1169 /* ARGSUSED */
1170 static void
1171 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1172 {
1173 	dmu_sync_arg_t *dsa = varg;
1174 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
1175 	dmu_buf_impl_t *db = dr->dr_dbuf;
1176 
1177 	mutex_enter(&db->db_mtx);
1178 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1179 	if (zio->io_error == 0) {
1180 		dr->dt.dl.dr_overridden_by = *zio->io_bp;
1181 		dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1182 		dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1183 		if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by))
1184 			BP_ZERO(&dr->dt.dl.dr_overridden_by);
1185 	} else {
1186 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1187 	}
1188 	cv_broadcast(&db->db_changed);
1189 	mutex_exit(&db->db_mtx);
1190 
1191 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1192 
1193 	kmem_free(dsa, sizeof (*dsa));
1194 }
1195 
1196 static void
1197 dmu_sync_late_arrival_done(zio_t *zio)
1198 {
1199 	blkptr_t *bp = zio->io_bp;
1200 	dmu_sync_arg_t *dsa = zio->io_private;
1201 
1202 	if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
1203 		ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1204 		ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1205 		zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1206 	}
1207 
1208 	dmu_tx_commit(dsa->dsa_tx);
1209 
1210 	dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1211 
1212 	kmem_free(dsa, sizeof (*dsa));
1213 }
1214 
1215 static int
1216 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1217     zio_prop_t *zp, zbookmark_t *zb)
1218 {
1219 	dmu_sync_arg_t *dsa;
1220 	dmu_tx_t *tx;
1221 
1222 	tx = dmu_tx_create(os);
1223 	dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
1224 	if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1225 		dmu_tx_abort(tx);
1226 		return (EIO);	/* Make zl_get_data do txg_waited_synced() */
1227 	}
1228 
1229 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1230 	dsa->dsa_dr = NULL;
1231 	dsa->dsa_done = done;
1232 	dsa->dsa_zgd = zgd;
1233 	dsa->dsa_tx = tx;
1234 
1235 	zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1236 	    zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp,
1237 	    dmu_sync_late_arrival_ready, dmu_sync_late_arrival_done, dsa,
1238 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1239 
1240 	return (0);
1241 }
1242 
1243 /*
1244  * Intent log support: sync the block associated with db to disk.
1245  * N.B. and XXX: the caller is responsible for making sure that the
1246  * data isn't changing while dmu_sync() is writing it.
1247  *
1248  * Return values:
1249  *
1250  *	EEXIST: this txg has already been synced, so there's nothing to to.
1251  *		The caller should not log the write.
1252  *
1253  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1254  *		The caller should not log the write.
1255  *
1256  *	EALREADY: this block is already in the process of being synced.
1257  *		The caller should track its progress (somehow).
1258  *
1259  *	EIO: could not do the I/O.
1260  *		The caller should do a txg_wait_synced().
1261  *
1262  *	0: the I/O has been initiated.
1263  *		The caller should log this blkptr in the done callback.
1264  *		It is possible that the I/O will fail, in which case
1265  *		the error will be reported to the done callback and
1266  *		propagated to pio from zio_done().
1267  */
1268 int
1269 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1270 {
1271 	blkptr_t *bp = zgd->zgd_bp;
1272 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1273 	objset_t *os = db->db_objset;
1274 	dsl_dataset_t *ds = os->os_dsl_dataset;
1275 	dbuf_dirty_record_t *dr;
1276 	dmu_sync_arg_t *dsa;
1277 	zbookmark_t zb;
1278 	zio_prop_t zp;
1279 
1280 	ASSERT(pio != NULL);
1281 	ASSERT(BP_IS_HOLE(bp));
1282 	ASSERT(txg != 0);
1283 
1284 	SET_BOOKMARK(&zb, ds->ds_object,
1285 	    db->db.db_object, db->db_level, db->db_blkid);
1286 
1287 	dmu_write_policy(os, db->db_dnode, db->db_level, WP_DMU_SYNC, &zp);
1288 
1289 	/*
1290 	 * If we're frozen (running ziltest), we always need to generate a bp.
1291 	 */
1292 	if (txg > spa_freeze_txg(os->os_spa))
1293 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1294 
1295 	/*
1296 	 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1297 	 * and us.  If we determine that this txg is not yet syncing,
1298 	 * but it begins to sync a moment later, that's OK because the
1299 	 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1300 	 */
1301 	mutex_enter(&db->db_mtx);
1302 
1303 	if (txg <= spa_last_synced_txg(os->os_spa)) {
1304 		/*
1305 		 * This txg has already synced.  There's nothing to do.
1306 		 */
1307 		mutex_exit(&db->db_mtx);
1308 		return (EEXIST);
1309 	}
1310 
1311 	if (txg <= spa_syncing_txg(os->os_spa)) {
1312 		/*
1313 		 * This txg is currently syncing, so we can't mess with
1314 		 * the dirty record anymore; just write a new log block.
1315 		 */
1316 		mutex_exit(&db->db_mtx);
1317 		return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1318 	}
1319 
1320 	dr = db->db_last_dirty;
1321 	while (dr && dr->dr_txg != txg)
1322 		dr = dr->dr_next;
1323 
1324 	if (dr == NULL) {
1325 		/*
1326 		 * There's no dr for this dbuf, so it must have been freed.
1327 		 * There's no need to log writes to freed blocks, so we're done.
1328 		 */
1329 		mutex_exit(&db->db_mtx);
1330 		return (ENOENT);
1331 	}
1332 
1333 	ASSERT(dr->dr_txg == txg);
1334 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1335 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1336 		/*
1337 		 * We have already issued a sync write for this buffer,
1338 		 * or this buffer has already been synced.  It could not
1339 		 * have been dirtied since, or we would have cleared the state.
1340 		 */
1341 		mutex_exit(&db->db_mtx);
1342 		return (EALREADY);
1343 	}
1344 
1345 	ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1346 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1347 	mutex_exit(&db->db_mtx);
1348 
1349 	dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1350 	dsa->dsa_dr = dr;
1351 	dsa->dsa_done = done;
1352 	dsa->dsa_zgd = zgd;
1353 	dsa->dsa_tx = NULL;
1354 
1355 	zio_nowait(arc_write(pio, os->os_spa, txg,
1356 	    bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db), &zp,
1357 	    dmu_sync_ready, dmu_sync_done, dsa,
1358 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb));
1359 
1360 	return (0);
1361 }
1362 
1363 int
1364 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1365 	dmu_tx_t *tx)
1366 {
1367 	dnode_t *dn;
1368 	int err;
1369 
1370 	err = dnode_hold(os, object, FTAG, &dn);
1371 	if (err)
1372 		return (err);
1373 	err = dnode_set_blksz(dn, size, ibs, tx);
1374 	dnode_rele(dn, FTAG);
1375 	return (err);
1376 }
1377 
1378 void
1379 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1380 	dmu_tx_t *tx)
1381 {
1382 	dnode_t *dn;
1383 
1384 	/* XXX assumes dnode_hold will not get an i/o error */
1385 	(void) dnode_hold(os, object, FTAG, &dn);
1386 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1387 	dn->dn_checksum = checksum;
1388 	dnode_setdirty(dn, tx);
1389 	dnode_rele(dn, FTAG);
1390 }
1391 
1392 void
1393 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1394 	dmu_tx_t *tx)
1395 {
1396 	dnode_t *dn;
1397 
1398 	/* XXX assumes dnode_hold will not get an i/o error */
1399 	(void) dnode_hold(os, object, FTAG, &dn);
1400 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1401 	dn->dn_compress = compress;
1402 	dnode_setdirty(dn, tx);
1403 	dnode_rele(dn, FTAG);
1404 }
1405 
1406 int zfs_mdcomp_disable = 0;
1407 
1408 void
1409 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1410 {
1411 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1412 	boolean_t ismd = (level > 0 || dmu_ot[type].ot_metadata);
1413 	enum zio_checksum checksum = os->os_checksum;
1414 	enum zio_compress compress = os->os_compress;
1415 	enum zio_checksum dedup_checksum = os->os_dedup_checksum;
1416 	boolean_t dedup;
1417 	boolean_t dedup_verify = os->os_dedup_verify;
1418 	int copies = os->os_copies;
1419 
1420 	/*
1421 	 * Determine checksum setting.
1422 	 */
1423 	if (ismd) {
1424 		/*
1425 		 * Metadata always gets checksummed.  If the data
1426 		 * checksum is multi-bit correctable, and it's not a
1427 		 * ZBT-style checksum, then it's suitable for metadata
1428 		 * as well.  Otherwise, the metadata checksum defaults
1429 		 * to fletcher4.
1430 		 */
1431 		if (zio_checksum_table[checksum].ci_correctable < 1 ||
1432 		    zio_checksum_table[checksum].ci_eck)
1433 			checksum = ZIO_CHECKSUM_FLETCHER_4;
1434 	} else {
1435 		checksum = zio_checksum_select(dn->dn_checksum, checksum);
1436 	}
1437 
1438 	/*
1439 	 * Determine compression setting.
1440 	 */
1441 	if (ismd) {
1442 		/*
1443 		 * XXX -- we should design a compression algorithm
1444 		 * that specializes in arrays of bps.
1445 		 */
1446 		compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY :
1447 		    ZIO_COMPRESS_LZJB;
1448 	} else {
1449 		compress = zio_compress_select(dn->dn_compress, compress);
1450 	}
1451 
1452 	/*
1453 	 * Determine dedup setting.  If we are in dmu_sync(), we won't
1454 	 * actually dedup now because that's all done in syncing context;
1455 	 * but we do want to use the dedup checkum.  If the checksum is not
1456 	 * strong enough to ensure unique signatures, force dedup_verify.
1457 	 */
1458 	dedup = (!ismd && dedup_checksum != ZIO_CHECKSUM_OFF);
1459 	if (dedup) {
1460 		checksum = dedup_checksum;
1461 		if (!zio_checksum_table[checksum].ci_dedup)
1462 			dedup_verify = 1;
1463 	}
1464 
1465 	if (wp & WP_DMU_SYNC)
1466 		dedup = 0;
1467 
1468 	if (wp & WP_NOFILL) {
1469 		ASSERT(!ismd && level == 0);
1470 		checksum = ZIO_CHECKSUM_OFF;
1471 		compress = ZIO_COMPRESS_OFF;
1472 		dedup = B_FALSE;
1473 	}
1474 
1475 	zp->zp_checksum = checksum;
1476 	zp->zp_compress = compress;
1477 	zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1478 	zp->zp_level = level;
1479 	zp->zp_copies = MIN(copies + ismd, spa_max_replication(os->os_spa));
1480 	zp->zp_dedup = dedup;
1481 	zp->zp_dedup_verify = dedup && dedup_verify;
1482 }
1483 
1484 int
1485 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1486 {
1487 	dnode_t *dn;
1488 	int i, err;
1489 
1490 	err = dnode_hold(os, object, FTAG, &dn);
1491 	if (err)
1492 		return (err);
1493 	/*
1494 	 * Sync any current changes before
1495 	 * we go trundling through the block pointers.
1496 	 */
1497 	for (i = 0; i < TXG_SIZE; i++) {
1498 		if (list_link_active(&dn->dn_dirty_link[i]))
1499 			break;
1500 	}
1501 	if (i != TXG_SIZE) {
1502 		dnode_rele(dn, FTAG);
1503 		txg_wait_synced(dmu_objset_pool(os), 0);
1504 		err = dnode_hold(os, object, FTAG, &dn);
1505 		if (err)
1506 			return (err);
1507 	}
1508 
1509 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1510 	dnode_rele(dn, FTAG);
1511 
1512 	return (err);
1513 }
1514 
1515 void
1516 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1517 {
1518 	dnode_phys_t *dnp;
1519 
1520 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1521 	mutex_enter(&dn->dn_mtx);
1522 
1523 	dnp = dn->dn_phys;
1524 
1525 	doi->doi_data_block_size = dn->dn_datablksz;
1526 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1527 	    1ULL << dn->dn_indblkshift : 0;
1528 	doi->doi_type = dn->dn_type;
1529 	doi->doi_bonus_type = dn->dn_bonustype;
1530 	doi->doi_bonus_size = dn->dn_bonuslen;
1531 	doi->doi_indirection = dn->dn_nlevels;
1532 	doi->doi_checksum = dn->dn_checksum;
1533 	doi->doi_compress = dn->dn_compress;
1534 	doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1535 	doi->doi_max_offset = (dnp->dn_maxblkid + 1) * dn->dn_datablksz;
1536 	doi->doi_fill_count = 0;
1537 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1538 		doi->doi_fill_count += dnp->dn_blkptr[i].blk_fill;
1539 
1540 	mutex_exit(&dn->dn_mtx);
1541 	rw_exit(&dn->dn_struct_rwlock);
1542 }
1543 
1544 /*
1545  * Get information on a DMU object.
1546  * If doi is NULL, just indicates whether the object exists.
1547  */
1548 int
1549 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1550 {
1551 	dnode_t *dn;
1552 	int err = dnode_hold(os, object, FTAG, &dn);
1553 
1554 	if (err)
1555 		return (err);
1556 
1557 	if (doi != NULL)
1558 		dmu_object_info_from_dnode(dn, doi);
1559 
1560 	dnode_rele(dn, FTAG);
1561 	return (0);
1562 }
1563 
1564 /*
1565  * As above, but faster; can be used when you have a held dbuf in hand.
1566  */
1567 void
1568 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1569 {
1570 	dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1571 }
1572 
1573 /*
1574  * Faster still when you only care about the size.
1575  * This is specifically optimized for zfs_getattr().
1576  */
1577 void
1578 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1579 {
1580 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1581 
1582 	*blksize = dn->dn_datablksz;
1583 	/* add 1 for dnode space */
1584 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1585 	    SPA_MINBLOCKSHIFT) + 1;
1586 }
1587 
1588 void
1589 byteswap_uint64_array(void *vbuf, size_t size)
1590 {
1591 	uint64_t *buf = vbuf;
1592 	size_t count = size >> 3;
1593 	int i;
1594 
1595 	ASSERT((size & 7) == 0);
1596 
1597 	for (i = 0; i < count; i++)
1598 		buf[i] = BSWAP_64(buf[i]);
1599 }
1600 
1601 void
1602 byteswap_uint32_array(void *vbuf, size_t size)
1603 {
1604 	uint32_t *buf = vbuf;
1605 	size_t count = size >> 2;
1606 	int i;
1607 
1608 	ASSERT((size & 3) == 0);
1609 
1610 	for (i = 0; i < count; i++)
1611 		buf[i] = BSWAP_32(buf[i]);
1612 }
1613 
1614 void
1615 byteswap_uint16_array(void *vbuf, size_t size)
1616 {
1617 	uint16_t *buf = vbuf;
1618 	size_t count = size >> 1;
1619 	int i;
1620 
1621 	ASSERT((size & 1) == 0);
1622 
1623 	for (i = 0; i < count; i++)
1624 		buf[i] = BSWAP_16(buf[i]);
1625 }
1626 
1627 /* ARGSUSED */
1628 void
1629 byteswap_uint8_array(void *vbuf, size_t size)
1630 {
1631 }
1632 
1633 void
1634 dmu_init(void)
1635 {
1636 	zfs_dbgmsg_init();
1637 	dbuf_init();
1638 	dnode_init();
1639 	zfetch_init();
1640 	arc_init();
1641 	l2arc_init();
1642 	xuio_stat_init();
1643 	sa_cache_init();
1644 }
1645 
1646 void
1647 dmu_fini(void)
1648 {
1649 	arc_fini();
1650 	zfetch_fini();
1651 	dnode_fini();
1652 	dbuf_fini();
1653 	l2arc_fini();
1654 	xuio_stat_fini();
1655 	sa_cache_fini();
1656 	zfs_dbgmsg_fini();
1657 }
1658