xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu.c (revision 0dc974a9a2e66d676505db23524ebff105fb36a9)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/dmu.h>
27 #include <sys/dmu_impl.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/dmu_zfetch.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/zio_checksum.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 };
89 
90 int
91 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
92     void *tag, dmu_buf_t **dbp)
93 {
94 	dnode_t *dn;
95 	uint64_t blkid;
96 	dmu_buf_impl_t *db;
97 	int err;
98 
99 	err = dnode_hold(os->os, object, FTAG, &dn);
100 	if (err)
101 		return (err);
102 	blkid = dbuf_whichblock(dn, offset);
103 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
104 	db = dbuf_hold(dn, blkid, tag);
105 	rw_exit(&dn->dn_struct_rwlock);
106 	if (db == NULL) {
107 		err = EIO;
108 	} else {
109 		err = dbuf_read(db, NULL, DB_RF_CANFAIL);
110 		if (err) {
111 			dbuf_rele(db, tag);
112 			db = NULL;
113 		}
114 	}
115 
116 	dnode_rele(dn, FTAG);
117 	*dbp = &db->db;
118 	return (err);
119 }
120 
121 int
122 dmu_bonus_max(void)
123 {
124 	return (DN_MAX_BONUSLEN);
125 }
126 
127 int
128 dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx)
129 {
130 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
131 
132 	if (dn->dn_bonus != (dmu_buf_impl_t *)db)
133 		return (EINVAL);
134 	if (newsize < 0 || newsize > db->db_size)
135 		return (EINVAL);
136 	dnode_setbonuslen(dn, newsize, tx);
137 	return (0);
138 }
139 
140 /*
141  * returns ENOENT, EIO, or 0.
142  */
143 int
144 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
145 {
146 	dnode_t *dn;
147 	dmu_buf_impl_t *db;
148 	int error;
149 
150 	error = dnode_hold(os->os, object, FTAG, &dn);
151 	if (error)
152 		return (error);
153 
154 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
155 	if (dn->dn_bonus == NULL) {
156 		rw_exit(&dn->dn_struct_rwlock);
157 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
158 		if (dn->dn_bonus == NULL)
159 			dbuf_create_bonus(dn);
160 	}
161 	db = dn->dn_bonus;
162 	rw_exit(&dn->dn_struct_rwlock);
163 
164 	/* as long as the bonus buf is held, the dnode will be held */
165 	if (refcount_add(&db->db_holds, tag) == 1)
166 		VERIFY(dnode_add_ref(dn, db));
167 
168 	dnode_rele(dn, FTAG);
169 
170 	VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
171 
172 	*dbp = &db->db;
173 	return (0);
174 }
175 
176 /*
177  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
178  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
179  * and can induce severe lock contention when writing to several files
180  * whose dnodes are in the same block.
181  */
182 static int
183 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset,
184     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
185 {
186 	dmu_buf_t **dbp;
187 	uint64_t blkid, nblks, i;
188 	uint32_t flags;
189 	int err;
190 	zio_t *zio;
191 
192 	ASSERT(length <= DMU_MAX_ACCESS);
193 
194 	flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
195 	if (length > zfetch_array_rd_sz)
196 		flags |= DB_RF_NOPREFETCH;
197 
198 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
199 	if (dn->dn_datablkshift) {
200 		int blkshift = dn->dn_datablkshift;
201 		nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
202 		    P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
203 	} else {
204 		if (offset + length > dn->dn_datablksz) {
205 			zfs_panic_recover("zfs: accessing past end of object "
206 			    "%llx/%llx (size=%u access=%llu+%llu)",
207 			    (longlong_t)dn->dn_objset->
208 			    os_dsl_dataset->ds_object,
209 			    (longlong_t)dn->dn_object, dn->dn_datablksz,
210 			    (longlong_t)offset, (longlong_t)length);
211 			return (EIO);
212 		}
213 		nblks = 1;
214 	}
215 	dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
216 
217 	zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, TRUE);
218 	blkid = dbuf_whichblock(dn, offset);
219 	for (i = 0; i < nblks; i++) {
220 		dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
221 		if (db == NULL) {
222 			rw_exit(&dn->dn_struct_rwlock);
223 			dmu_buf_rele_array(dbp, nblks, tag);
224 			zio_nowait(zio);
225 			return (EIO);
226 		}
227 		/* initiate async i/o */
228 		if (read) {
229 			rw_exit(&dn->dn_struct_rwlock);
230 			(void) dbuf_read(db, zio, flags);
231 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
232 		}
233 		dbp[i] = &db->db;
234 	}
235 	rw_exit(&dn->dn_struct_rwlock);
236 
237 	/* wait for async i/o */
238 	err = zio_wait(zio);
239 	if (err) {
240 		dmu_buf_rele_array(dbp, nblks, tag);
241 		return (err);
242 	}
243 
244 	/* wait for other io to complete */
245 	if (read) {
246 		for (i = 0; i < nblks; i++) {
247 			dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
248 			mutex_enter(&db->db_mtx);
249 			while (db->db_state == DB_READ ||
250 			    db->db_state == DB_FILL)
251 				cv_wait(&db->db_changed, &db->db_mtx);
252 			if (db->db_state == DB_UNCACHED)
253 				err = EIO;
254 			mutex_exit(&db->db_mtx);
255 			if (err) {
256 				dmu_buf_rele_array(dbp, nblks, tag);
257 				return (err);
258 			}
259 		}
260 	}
261 
262 	*numbufsp = nblks;
263 	*dbpp = dbp;
264 	return (0);
265 }
266 
267 static int
268 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
269     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
270 {
271 	dnode_t *dn;
272 	int err;
273 
274 	err = dnode_hold(os->os, object, FTAG, &dn);
275 	if (err)
276 		return (err);
277 
278 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
279 	    numbufsp, dbpp);
280 
281 	dnode_rele(dn, FTAG);
282 
283 	return (err);
284 }
285 
286 int
287 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
288     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
289 {
290 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
291 	int err;
292 
293 	err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
294 	    numbufsp, dbpp);
295 
296 	return (err);
297 }
298 
299 void
300 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
301 {
302 	int i;
303 	dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
304 
305 	if (numbufs == 0)
306 		return;
307 
308 	for (i = 0; i < numbufs; i++) {
309 		if (dbp[i])
310 			dbuf_rele(dbp[i], tag);
311 	}
312 
313 	kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
314 }
315 
316 void
317 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
318 {
319 	dnode_t *dn;
320 	uint64_t blkid;
321 	int nblks, i, err;
322 
323 	if (zfs_prefetch_disable)
324 		return;
325 
326 	if (len == 0) {  /* they're interested in the bonus buffer */
327 		dn = os->os->os_meta_dnode;
328 
329 		if (object == 0 || object >= DN_MAX_OBJECT)
330 			return;
331 
332 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
333 		blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
334 		dbuf_prefetch(dn, blkid);
335 		rw_exit(&dn->dn_struct_rwlock);
336 		return;
337 	}
338 
339 	/*
340 	 * XXX - Note, if the dnode for the requested object is not
341 	 * already cached, we will do a *synchronous* read in the
342 	 * dnode_hold() call.  The same is true for any indirects.
343 	 */
344 	err = dnode_hold(os->os, object, FTAG, &dn);
345 	if (err != 0)
346 		return;
347 
348 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
349 	if (dn->dn_datablkshift) {
350 		int blkshift = dn->dn_datablkshift;
351 		nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
352 		    P2ALIGN(offset, 1<<blkshift)) >> blkshift;
353 	} else {
354 		nblks = (offset < dn->dn_datablksz);
355 	}
356 
357 	if (nblks != 0) {
358 		blkid = dbuf_whichblock(dn, offset);
359 		for (i = 0; i < nblks; i++)
360 			dbuf_prefetch(dn, blkid+i);
361 	}
362 
363 	rw_exit(&dn->dn_struct_rwlock);
364 
365 	dnode_rele(dn, FTAG);
366 }
367 
368 static int
369 get_next_chunk(dnode_t *dn, uint64_t *offset, uint64_t limit)
370 {
371 	uint64_t len = limit - *offset;
372 	uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT;
373 	uint64_t dn_used;
374 	int err;
375 
376 	ASSERT(limit <= *offset);
377 
378 	dn_used = dn->dn_phys->dn_used <<
379 	    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES ? 0 : DEV_BSHIFT);
380 	if (len <= chunk_len || dn_used <= chunk_len) {
381 		*offset = limit;
382 		return (0);
383 	}
384 
385 	while (*offset > limit) {
386 		uint64_t initial_offset = *offset;
387 		uint64_t delta;
388 
389 		/* skip over allocated data */
390 		err = dnode_next_offset(dn,
391 		    DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
392 		if (err == ESRCH)
393 			*offset = limit;
394 		else if (err)
395 			return (err);
396 
397 		ASSERT3U(*offset, <=, initial_offset);
398 		delta = initial_offset - *offset;
399 		if (delta >= chunk_len) {
400 			*offset += delta - chunk_len;
401 			return (0);
402 		}
403 		chunk_len -= delta;
404 
405 		/* skip over unallocated data */
406 		err = dnode_next_offset(dn,
407 		    DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
408 		if (err == ESRCH)
409 			*offset = limit;
410 		else if (err)
411 			return (err);
412 
413 		if (*offset < limit)
414 			*offset = limit;
415 		ASSERT3U(*offset, <, initial_offset);
416 	}
417 	return (0);
418 }
419 
420 static int
421 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
422     uint64_t length, boolean_t free_dnode)
423 {
424 	dmu_tx_t *tx;
425 	uint64_t object_size, start, end, len;
426 	boolean_t trunc = (length == DMU_OBJECT_END);
427 	int align, err;
428 
429 	align = 1 << dn->dn_datablkshift;
430 	ASSERT(align > 0);
431 	object_size = align == 1 ? dn->dn_datablksz :
432 	    (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
433 
434 	if (trunc || (end = offset + length) > object_size)
435 		end = object_size;
436 	if (end <= offset)
437 		return (0);
438 	length = end - offset;
439 
440 	while (length) {
441 		start = end;
442 		err = get_next_chunk(dn, &start, offset);
443 		if (err)
444 			return (err);
445 		len = trunc ? DMU_OBJECT_END : end - start;
446 
447 		tx = dmu_tx_create(os);
448 		dmu_tx_hold_free(tx, dn->dn_object, start, len);
449 		err = dmu_tx_assign(tx, TXG_WAIT);
450 		if (err) {
451 			dmu_tx_abort(tx);
452 			return (err);
453 		}
454 
455 		dnode_free_range(dn, start, trunc ? -1 : len, tx);
456 
457 		if (start == 0 && trunc && free_dnode)
458 			dnode_free(dn, tx);
459 
460 		length -= end - start;
461 
462 		dmu_tx_commit(tx);
463 		end = start;
464 		trunc = FALSE;
465 	}
466 	return (0);
467 }
468 
469 int
470 dmu_free_long_range(objset_t *os, uint64_t object,
471     uint64_t offset, uint64_t length)
472 {
473 	dnode_t *dn;
474 	int err;
475 
476 	err = dnode_hold(os->os, object, FTAG, &dn);
477 	if (err != 0)
478 		return (err);
479 	err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
480 	dnode_rele(dn, FTAG);
481 	return (err);
482 }
483 
484 int
485 dmu_free_object(objset_t *os, uint64_t object)
486 {
487 	dnode_t *dn;
488 	dmu_tx_t *tx;
489 	int err;
490 
491 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
492 	    FTAG, &dn);
493 	if (err != 0)
494 		return (err);
495 	if (dn->dn_nlevels == 1) {
496 		tx = dmu_tx_create(os);
497 		dmu_tx_hold_bonus(tx, object);
498 		dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
499 		err = dmu_tx_assign(tx, TXG_WAIT);
500 		if (err == 0) {
501 			dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
502 			dnode_free(dn, tx);
503 			dmu_tx_commit(tx);
504 		} else {
505 			dmu_tx_abort(tx);
506 		}
507 	} else {
508 		err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
509 	}
510 	dnode_rele(dn, FTAG);
511 	return (err);
512 }
513 
514 int
515 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
516     uint64_t size, dmu_tx_t *tx)
517 {
518 	dnode_t *dn;
519 	int err = dnode_hold(os->os, object, FTAG, &dn);
520 	if (err)
521 		return (err);
522 	ASSERT(offset < UINT64_MAX);
523 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
524 	dnode_free_range(dn, offset, size, tx);
525 	dnode_rele(dn, FTAG);
526 	return (0);
527 }
528 
529 int
530 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
531     void *buf)
532 {
533 	dnode_t *dn;
534 	dmu_buf_t **dbp;
535 	int numbufs, i, err;
536 
537 	err = dnode_hold(os->os, object, FTAG, &dn);
538 	if (err)
539 		return (err);
540 
541 	/*
542 	 * Deal with odd block sizes, where there can't be data past the first
543 	 * block.  If we ever do the tail block optimization, we will need to
544 	 * handle that here as well.
545 	 */
546 	if (dn->dn_datablkshift == 0) {
547 		int newsz = offset > dn->dn_datablksz ? 0 :
548 		    MIN(size, dn->dn_datablksz - offset);
549 		bzero((char *)buf + newsz, size - newsz);
550 		size = newsz;
551 	}
552 
553 	while (size > 0) {
554 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
555 
556 		/*
557 		 * NB: we could do this block-at-a-time, but it's nice
558 		 * to be reading in parallel.
559 		 */
560 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
561 		    TRUE, FTAG, &numbufs, &dbp);
562 		if (err)
563 			break;
564 
565 		for (i = 0; i < numbufs; i++) {
566 			int tocpy;
567 			int bufoff;
568 			dmu_buf_t *db = dbp[i];
569 
570 			ASSERT(size > 0);
571 
572 			bufoff = offset - db->db_offset;
573 			tocpy = (int)MIN(db->db_size - bufoff, size);
574 
575 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
576 
577 			offset += tocpy;
578 			size -= tocpy;
579 			buf = (char *)buf + tocpy;
580 		}
581 		dmu_buf_rele_array(dbp, numbufs, FTAG);
582 	}
583 	dnode_rele(dn, FTAG);
584 	return (err);
585 }
586 
587 void
588 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
589     const void *buf, dmu_tx_t *tx)
590 {
591 	dmu_buf_t **dbp;
592 	int numbufs, i;
593 
594 	if (size == 0)
595 		return;
596 
597 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
598 	    FALSE, FTAG, &numbufs, &dbp));
599 
600 	for (i = 0; i < numbufs; i++) {
601 		int tocpy;
602 		int bufoff;
603 		dmu_buf_t *db = dbp[i];
604 
605 		ASSERT(size > 0);
606 
607 		bufoff = offset - db->db_offset;
608 		tocpy = (int)MIN(db->db_size - bufoff, size);
609 
610 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
611 
612 		if (tocpy == db->db_size)
613 			dmu_buf_will_fill(db, tx);
614 		else
615 			dmu_buf_will_dirty(db, tx);
616 
617 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
618 
619 		if (tocpy == db->db_size)
620 			dmu_buf_fill_done(db, tx);
621 
622 		offset += tocpy;
623 		size -= tocpy;
624 		buf = (char *)buf + tocpy;
625 	}
626 	dmu_buf_rele_array(dbp, numbufs, FTAG);
627 }
628 
629 #ifdef _KERNEL
630 int
631 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
632 {
633 	dmu_buf_t **dbp;
634 	int numbufs, i, err;
635 
636 	/*
637 	 * NB: we could do this block-at-a-time, but it's nice
638 	 * to be reading in parallel.
639 	 */
640 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
641 	    &numbufs, &dbp);
642 	if (err)
643 		return (err);
644 
645 	for (i = 0; i < numbufs; i++) {
646 		int tocpy;
647 		int bufoff;
648 		dmu_buf_t *db = dbp[i];
649 
650 		ASSERT(size > 0);
651 
652 		bufoff = uio->uio_loffset - db->db_offset;
653 		tocpy = (int)MIN(db->db_size - bufoff, size);
654 
655 		err = uiomove((char *)db->db_data + bufoff, tocpy,
656 		    UIO_READ, uio);
657 		if (err)
658 			break;
659 
660 		size -= tocpy;
661 	}
662 	dmu_buf_rele_array(dbp, numbufs, FTAG);
663 
664 	return (err);
665 }
666 
667 int
668 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
669     dmu_tx_t *tx)
670 {
671 	dmu_buf_t **dbp;
672 	int numbufs, i;
673 	int err = 0;
674 
675 	if (size == 0)
676 		return (0);
677 
678 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
679 	    FALSE, FTAG, &numbufs, &dbp);
680 	if (err)
681 		return (err);
682 
683 	for (i = 0; i < numbufs; i++) {
684 		int tocpy;
685 		int bufoff;
686 		dmu_buf_t *db = dbp[i];
687 
688 		ASSERT(size > 0);
689 
690 		bufoff = uio->uio_loffset - db->db_offset;
691 		tocpy = (int)MIN(db->db_size - bufoff, size);
692 
693 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
694 
695 		if (tocpy == db->db_size)
696 			dmu_buf_will_fill(db, tx);
697 		else
698 			dmu_buf_will_dirty(db, tx);
699 
700 		/*
701 		 * XXX uiomove could block forever (eg. nfs-backed
702 		 * pages).  There needs to be a uiolockdown() function
703 		 * to lock the pages in memory, so that uiomove won't
704 		 * block.
705 		 */
706 		err = uiomove((char *)db->db_data + bufoff, tocpy,
707 		    UIO_WRITE, uio);
708 
709 		if (tocpy == db->db_size)
710 			dmu_buf_fill_done(db, tx);
711 
712 		if (err)
713 			break;
714 
715 		size -= tocpy;
716 	}
717 	dmu_buf_rele_array(dbp, numbufs, FTAG);
718 	return (err);
719 }
720 
721 int
722 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
723     page_t *pp, dmu_tx_t *tx)
724 {
725 	dmu_buf_t **dbp;
726 	int numbufs, i;
727 	int err;
728 
729 	if (size == 0)
730 		return (0);
731 
732 	err = dmu_buf_hold_array(os, object, offset, size,
733 	    FALSE, FTAG, &numbufs, &dbp);
734 	if (err)
735 		return (err);
736 
737 	for (i = 0; i < numbufs; i++) {
738 		int tocpy, copied, thiscpy;
739 		int bufoff;
740 		dmu_buf_t *db = dbp[i];
741 		caddr_t va;
742 
743 		ASSERT(size > 0);
744 		ASSERT3U(db->db_size, >=, PAGESIZE);
745 
746 		bufoff = offset - db->db_offset;
747 		tocpy = (int)MIN(db->db_size - bufoff, size);
748 
749 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
750 
751 		if (tocpy == db->db_size)
752 			dmu_buf_will_fill(db, tx);
753 		else
754 			dmu_buf_will_dirty(db, tx);
755 
756 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
757 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
758 			thiscpy = MIN(PAGESIZE, tocpy - copied);
759 			va = zfs_map_page(pp, S_READ);
760 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
761 			zfs_unmap_page(pp, va);
762 			pp = pp->p_next;
763 			bufoff += PAGESIZE;
764 		}
765 
766 		if (tocpy == db->db_size)
767 			dmu_buf_fill_done(db, tx);
768 
769 		if (err)
770 			break;
771 
772 		offset += tocpy;
773 		size -= tocpy;
774 	}
775 	dmu_buf_rele_array(dbp, numbufs, FTAG);
776 	return (err);
777 }
778 #endif
779 
780 typedef struct {
781 	dbuf_dirty_record_t	*dr;
782 	dmu_sync_cb_t		*done;
783 	void			*arg;
784 } dmu_sync_arg_t;
785 
786 /* ARGSUSED */
787 static void
788 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
789 {
790 	dmu_sync_arg_t *in = varg;
791 	dbuf_dirty_record_t *dr = in->dr;
792 	dmu_buf_impl_t *db = dr->dr_dbuf;
793 	dmu_sync_cb_t *done = in->done;
794 
795 	if (!BP_IS_HOLE(zio->io_bp)) {
796 		zio->io_bp->blk_fill = 1;
797 		BP_SET_TYPE(zio->io_bp, db->db_dnode->dn_type);
798 		BP_SET_LEVEL(zio->io_bp, 0);
799 	}
800 
801 	mutex_enter(&db->db_mtx);
802 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
803 	dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
804 	dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
805 	cv_broadcast(&db->db_changed);
806 	mutex_exit(&db->db_mtx);
807 
808 	if (done)
809 		done(&(db->db), in->arg);
810 
811 	kmem_free(in, sizeof (dmu_sync_arg_t));
812 }
813 
814 /*
815  * Intent log support: sync the block associated with db to disk.
816  * N.B. and XXX: the caller is responsible for making sure that the
817  * data isn't changing while dmu_sync() is writing it.
818  *
819  * Return values:
820  *
821  *	EEXIST: this txg has already been synced, so there's nothing to to.
822  *		The caller should not log the write.
823  *
824  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
825  *		The caller should not log the write.
826  *
827  *	EALREADY: this block is already in the process of being synced.
828  *		The caller should track its progress (somehow).
829  *
830  *	EINPROGRESS: the IO has been initiated.
831  *		The caller should log this blkptr in the callback.
832  *
833  *	0: completed.  Sets *bp to the blkptr just written.
834  *		The caller should log this blkptr immediately.
835  */
836 int
837 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
838     blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
839 {
840 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
841 	objset_impl_t *os = db->db_objset;
842 	dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
843 	tx_state_t *tx = &dp->dp_tx;
844 	dbuf_dirty_record_t *dr;
845 	dmu_sync_arg_t *in;
846 	zbookmark_t zb;
847 	writeprops_t wp = { 0 };
848 	zio_t *zio;
849 	int zio_flags;
850 	int err;
851 
852 	ASSERT(BP_IS_HOLE(bp));
853 	ASSERT(txg != 0);
854 
855 
856 	dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
857 	    txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
858 
859 	/*
860 	 * XXX - would be nice if we could do this without suspending...
861 	 */
862 	txg_suspend(dp);
863 
864 	/*
865 	 * If this txg already synced, there's nothing to do.
866 	 */
867 	if (txg <= tx->tx_synced_txg) {
868 		txg_resume(dp);
869 		/*
870 		 * If we're running ziltest, we need the blkptr regardless.
871 		 */
872 		if (txg > spa_freeze_txg(dp->dp_spa)) {
873 			/* if db_blkptr == NULL, this was an empty write */
874 			if (db->db_blkptr)
875 				*bp = *db->db_blkptr; /* structure assignment */
876 			return (0);
877 		}
878 		return (EEXIST);
879 	}
880 
881 	mutex_enter(&db->db_mtx);
882 
883 	if (txg == tx->tx_syncing_txg) {
884 		while (db->db_data_pending) {
885 			/*
886 			 * IO is in-progress.  Wait for it to finish.
887 			 * XXX - would be nice to be able to somehow "attach"
888 			 * this zio to the parent zio passed in.
889 			 */
890 			cv_wait(&db->db_changed, &db->db_mtx);
891 			if (!db->db_data_pending &&
892 			    db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
893 				/*
894 				 * IO was compressed away
895 				 */
896 				*bp = *db->db_blkptr; /* structure assignment */
897 				mutex_exit(&db->db_mtx);
898 				txg_resume(dp);
899 				return (0);
900 			}
901 			ASSERT(db->db_data_pending ||
902 			    (db->db_blkptr && db->db_blkptr->blk_birth == txg));
903 		}
904 
905 		if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
906 			/*
907 			 * IO is already completed.
908 			 */
909 			*bp = *db->db_blkptr; /* structure assignment */
910 			mutex_exit(&db->db_mtx);
911 			txg_resume(dp);
912 			return (0);
913 		}
914 	}
915 
916 	dr = db->db_last_dirty;
917 	while (dr && dr->dr_txg > txg)
918 		dr = dr->dr_next;
919 	if (dr == NULL || dr->dr_txg < txg) {
920 		/*
921 		 * This dbuf isn't dirty, must have been free_range'd.
922 		 * There's no need to log writes to freed blocks, so we're done.
923 		 */
924 		mutex_exit(&db->db_mtx);
925 		txg_resume(dp);
926 		return (ENOENT);
927 	}
928 
929 	ASSERT(dr->dr_txg == txg);
930 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
931 		/*
932 		 * We have already issued a sync write for this buffer.
933 		 */
934 		mutex_exit(&db->db_mtx);
935 		txg_resume(dp);
936 		return (EALREADY);
937 	} else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
938 		/*
939 		 * This buffer has already been synced.  It could not
940 		 * have been dirtied since, or we would have cleared the state.
941 		 */
942 		*bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
943 		mutex_exit(&db->db_mtx);
944 		txg_resume(dp);
945 		return (0);
946 	}
947 
948 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
949 	in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
950 	in->dr = dr;
951 	in->done = done;
952 	in->arg = arg;
953 	mutex_exit(&db->db_mtx);
954 	txg_resume(dp);
955 
956 	zb.zb_objset = os->os_dsl_dataset->ds_object;
957 	zb.zb_object = db->db.db_object;
958 	zb.zb_level = db->db_level;
959 	zb.zb_blkid = db->db_blkid;
960 	zio_flags = ZIO_FLAG_MUSTSUCCEED;
961 	if (dmu_ot[db->db_dnode->dn_type].ot_metadata || zb.zb_level != 0)
962 		zio_flags |= ZIO_FLAG_METADATA;
963 	wp.wp_type = db->db_dnode->dn_type;
964 	wp.wp_copies = os->os_copies;
965 	wp.wp_level = db->db_level;
966 	wp.wp_dnchecksum = db->db_dnode->dn_checksum;
967 	wp.wp_oschecksum = os->os_checksum;
968 	wp.wp_dncompress = db->db_dnode->dn_compress;
969 	wp.wp_oscompress = os->os_compress;
970 	zio = arc_write(pio, os->os_spa, &wp,
971 	    DBUF_IS_L2CACHEABLE(db), txg, bp, dr->dt.dl.dr_data, NULL,
972 	    dmu_sync_done, in, ZIO_PRIORITY_SYNC_WRITE, zio_flags, &zb);
973 
974 	if (pio) {
975 		zio_nowait(zio);
976 		err = EINPROGRESS;
977 	} else {
978 		err = zio_wait(zio);
979 		ASSERT(err == 0);
980 	}
981 	return (err);
982 }
983 
984 int
985 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
986 	dmu_tx_t *tx)
987 {
988 	dnode_t *dn;
989 	int err;
990 
991 	err = dnode_hold(os->os, object, FTAG, &dn);
992 	if (err)
993 		return (err);
994 	err = dnode_set_blksz(dn, size, ibs, tx);
995 	dnode_rele(dn, FTAG);
996 	return (err);
997 }
998 
999 void
1000 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1001 	dmu_tx_t *tx)
1002 {
1003 	dnode_t *dn;
1004 
1005 	/* XXX assumes dnode_hold will not get an i/o error */
1006 	(void) dnode_hold(os->os, object, FTAG, &dn);
1007 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1008 	dn->dn_checksum = checksum;
1009 	dnode_setdirty(dn, tx);
1010 	dnode_rele(dn, FTAG);
1011 }
1012 
1013 void
1014 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1015 	dmu_tx_t *tx)
1016 {
1017 	dnode_t *dn;
1018 
1019 	/* XXX assumes dnode_hold will not get an i/o error */
1020 	(void) dnode_hold(os->os, object, FTAG, &dn);
1021 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1022 	dn->dn_compress = compress;
1023 	dnode_setdirty(dn, tx);
1024 	dnode_rele(dn, FTAG);
1025 }
1026 
1027 int
1028 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1029 {
1030 	dnode_t *dn;
1031 	int i, err;
1032 
1033 	err = dnode_hold(os->os, object, FTAG, &dn);
1034 	if (err)
1035 		return (err);
1036 	/*
1037 	 * Sync any current changes before
1038 	 * we go trundling through the block pointers.
1039 	 */
1040 	for (i = 0; i < TXG_SIZE; i++) {
1041 		if (list_link_active(&dn->dn_dirty_link[i]))
1042 			break;
1043 	}
1044 	if (i != TXG_SIZE) {
1045 		dnode_rele(dn, FTAG);
1046 		txg_wait_synced(dmu_objset_pool(os), 0);
1047 		err = dnode_hold(os->os, object, FTAG, &dn);
1048 		if (err)
1049 			return (err);
1050 	}
1051 
1052 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1053 	dnode_rele(dn, FTAG);
1054 
1055 	return (err);
1056 }
1057 
1058 void
1059 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1060 {
1061 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1062 	mutex_enter(&dn->dn_mtx);
1063 
1064 	doi->doi_data_block_size = dn->dn_datablksz;
1065 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1066 	    1ULL << dn->dn_indblkshift : 0;
1067 	doi->doi_indirection = dn->dn_nlevels;
1068 	doi->doi_checksum = dn->dn_checksum;
1069 	doi->doi_compress = dn->dn_compress;
1070 	doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1071 	    SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1072 	doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1073 	doi->doi_type = dn->dn_type;
1074 	doi->doi_bonus_size = dn->dn_bonuslen;
1075 	doi->doi_bonus_type = dn->dn_bonustype;
1076 
1077 	mutex_exit(&dn->dn_mtx);
1078 	rw_exit(&dn->dn_struct_rwlock);
1079 }
1080 
1081 /*
1082  * Get information on a DMU object.
1083  * If doi is NULL, just indicates whether the object exists.
1084  */
1085 int
1086 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1087 {
1088 	dnode_t *dn;
1089 	int err = dnode_hold(os->os, object, FTAG, &dn);
1090 
1091 	if (err)
1092 		return (err);
1093 
1094 	if (doi != NULL)
1095 		dmu_object_info_from_dnode(dn, doi);
1096 
1097 	dnode_rele(dn, FTAG);
1098 	return (0);
1099 }
1100 
1101 /*
1102  * As above, but faster; can be used when you have a held dbuf in hand.
1103  */
1104 void
1105 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1106 {
1107 	dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1108 }
1109 
1110 /*
1111  * Faster still when you only care about the size.
1112  * This is specifically optimized for zfs_getattr().
1113  */
1114 void
1115 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1116 {
1117 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1118 
1119 	*blksize = dn->dn_datablksz;
1120 	/* add 1 for dnode space */
1121 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1122 	    SPA_MINBLOCKSHIFT) + 1;
1123 }
1124 
1125 void
1126 byteswap_uint64_array(void *vbuf, size_t size)
1127 {
1128 	uint64_t *buf = vbuf;
1129 	size_t count = size >> 3;
1130 	int i;
1131 
1132 	ASSERT((size & 7) == 0);
1133 
1134 	for (i = 0; i < count; i++)
1135 		buf[i] = BSWAP_64(buf[i]);
1136 }
1137 
1138 void
1139 byteswap_uint32_array(void *vbuf, size_t size)
1140 {
1141 	uint32_t *buf = vbuf;
1142 	size_t count = size >> 2;
1143 	int i;
1144 
1145 	ASSERT((size & 3) == 0);
1146 
1147 	for (i = 0; i < count; i++)
1148 		buf[i] = BSWAP_32(buf[i]);
1149 }
1150 
1151 void
1152 byteswap_uint16_array(void *vbuf, size_t size)
1153 {
1154 	uint16_t *buf = vbuf;
1155 	size_t count = size >> 1;
1156 	int i;
1157 
1158 	ASSERT((size & 1) == 0);
1159 
1160 	for (i = 0; i < count; i++)
1161 		buf[i] = BSWAP_16(buf[i]);
1162 }
1163 
1164 /* ARGSUSED */
1165 void
1166 byteswap_uint8_array(void *vbuf, size_t size)
1167 {
1168 }
1169 
1170 void
1171 dmu_init(void)
1172 {
1173 	dbuf_init();
1174 	dnode_init();
1175 	arc_init();
1176 	l2arc_init();
1177 }
1178 
1179 void
1180 dmu_fini(void)
1181 {
1182 	arc_fini();
1183 	dnode_fini();
1184 	dbuf_fini();
1185 	l2arc_fini();
1186 }
1187