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