xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu.c (revision 66e150d7d3c0cb2de3c45c74612784ffd3e73de6)
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 = *offset - limit;
372 	uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT;
373 	uint64_t subchunk =
374 	    dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
375 
376 	ASSERT(limit <= *offset);
377 
378 	if (len <= chunk_len) {
379 		*offset = limit;
380 		return (0);
381 	}
382 
383 	ASSERT(ISP2(subchunk));
384 
385 	while (*offset > limit) {
386 		uint64_t initial_offset = P2ROUNDUP(*offset, subchunk);
387 		uint64_t delta;
388 		int err;
389 
390 		/* skip over allocated data */
391 		err = dnode_next_offset(dn,
392 		    DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
393 		if (err == ESRCH)
394 			*offset = limit;
395 		else if (err)
396 			return (err);
397 
398 		ASSERT3U(*offset, <=, initial_offset);
399 		*offset = P2ALIGN(*offset, subchunk);
400 		delta = initial_offset - *offset;
401 		if (delta >= chunk_len) {
402 			*offset += delta - chunk_len;
403 			return (0);
404 		}
405 		chunk_len -= delta;
406 
407 		/* skip over unallocated data */
408 		err = dnode_next_offset(dn,
409 		    DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
410 		if (err == ESRCH)
411 			*offset = limit;
412 		else if (err)
413 			return (err);
414 
415 		if (*offset < limit)
416 			*offset = limit;
417 		ASSERT3U(*offset, <, initial_offset);
418 	}
419 	return (0);
420 }
421 
422 static int
423 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
424     uint64_t length, boolean_t free_dnode)
425 {
426 	dmu_tx_t *tx;
427 	uint64_t object_size, start, end, len;
428 	boolean_t trunc = (length == DMU_OBJECT_END);
429 	int align, err;
430 
431 	align = 1 << dn->dn_datablkshift;
432 	ASSERT(align > 0);
433 	object_size = align == 1 ? dn->dn_datablksz :
434 	    (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
435 
436 	if (trunc || (end = offset + length) > object_size)
437 		end = object_size;
438 	if (end <= offset)
439 		return (0);
440 	length = end - offset;
441 
442 	while (length) {
443 		start = end;
444 		err = get_next_chunk(dn, &start, offset);
445 		if (err)
446 			return (err);
447 		len = trunc ? DMU_OBJECT_END : end - start;
448 
449 		tx = dmu_tx_create(os);
450 		dmu_tx_hold_free(tx, dn->dn_object, start, len);
451 		err = dmu_tx_assign(tx, TXG_WAIT);
452 		if (err) {
453 			dmu_tx_abort(tx);
454 			return (err);
455 		}
456 
457 		dnode_free_range(dn, start, trunc ? -1 : len, tx);
458 
459 		if (start == 0 && free_dnode) {
460 			ASSERT(trunc);
461 			dnode_free(dn, tx);
462 		}
463 
464 		length -= end - start;
465 
466 		dmu_tx_commit(tx);
467 		end = start;
468 	}
469 	return (0);
470 }
471 
472 int
473 dmu_free_long_range(objset_t *os, uint64_t object,
474     uint64_t offset, uint64_t length)
475 {
476 	dnode_t *dn;
477 	int err;
478 
479 	err = dnode_hold(os->os, object, FTAG, &dn);
480 	if (err != 0)
481 		return (err);
482 	err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
483 	dnode_rele(dn, FTAG);
484 	return (err);
485 }
486 
487 int
488 dmu_free_object(objset_t *os, uint64_t object)
489 {
490 	dnode_t *dn;
491 	dmu_tx_t *tx;
492 	int err;
493 
494 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
495 	    FTAG, &dn);
496 	if (err != 0)
497 		return (err);
498 	if (dn->dn_nlevels == 1) {
499 		tx = dmu_tx_create(os);
500 		dmu_tx_hold_bonus(tx, object);
501 		dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
502 		err = dmu_tx_assign(tx, TXG_WAIT);
503 		if (err == 0) {
504 			dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
505 			dnode_free(dn, tx);
506 			dmu_tx_commit(tx);
507 		} else {
508 			dmu_tx_abort(tx);
509 		}
510 	} else {
511 		err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
512 	}
513 	dnode_rele(dn, FTAG);
514 	return (err);
515 }
516 
517 int
518 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
519     uint64_t size, dmu_tx_t *tx)
520 {
521 	dnode_t *dn;
522 	int err = dnode_hold(os->os, object, FTAG, &dn);
523 	if (err)
524 		return (err);
525 	ASSERT(offset < UINT64_MAX);
526 	ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
527 	dnode_free_range(dn, offset, size, tx);
528 	dnode_rele(dn, FTAG);
529 	return (0);
530 }
531 
532 int
533 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
534     void *buf)
535 {
536 	dnode_t *dn;
537 	dmu_buf_t **dbp;
538 	int numbufs, i, err;
539 
540 	err = dnode_hold(os->os, object, FTAG, &dn);
541 	if (err)
542 		return (err);
543 
544 	/*
545 	 * Deal with odd block sizes, where there can't be data past the first
546 	 * block.  If we ever do the tail block optimization, we will need to
547 	 * handle that here as well.
548 	 */
549 	if (dn->dn_datablkshift == 0) {
550 		int newsz = offset > dn->dn_datablksz ? 0 :
551 		    MIN(size, dn->dn_datablksz - offset);
552 		bzero((char *)buf + newsz, size - newsz);
553 		size = newsz;
554 	}
555 
556 	while (size > 0) {
557 		uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
558 
559 		/*
560 		 * NB: we could do this block-at-a-time, but it's nice
561 		 * to be reading in parallel.
562 		 */
563 		err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
564 		    TRUE, FTAG, &numbufs, &dbp);
565 		if (err)
566 			break;
567 
568 		for (i = 0; i < numbufs; i++) {
569 			int tocpy;
570 			int bufoff;
571 			dmu_buf_t *db = dbp[i];
572 
573 			ASSERT(size > 0);
574 
575 			bufoff = offset - db->db_offset;
576 			tocpy = (int)MIN(db->db_size - bufoff, size);
577 
578 			bcopy((char *)db->db_data + bufoff, buf, tocpy);
579 
580 			offset += tocpy;
581 			size -= tocpy;
582 			buf = (char *)buf + tocpy;
583 		}
584 		dmu_buf_rele_array(dbp, numbufs, FTAG);
585 	}
586 	dnode_rele(dn, FTAG);
587 	return (err);
588 }
589 
590 void
591 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
592     const void *buf, dmu_tx_t *tx)
593 {
594 	dmu_buf_t **dbp;
595 	int numbufs, i;
596 
597 	if (size == 0)
598 		return;
599 
600 	VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
601 	    FALSE, FTAG, &numbufs, &dbp));
602 
603 	for (i = 0; i < numbufs; i++) {
604 		int tocpy;
605 		int bufoff;
606 		dmu_buf_t *db = dbp[i];
607 
608 		ASSERT(size > 0);
609 
610 		bufoff = offset - db->db_offset;
611 		tocpy = (int)MIN(db->db_size - bufoff, size);
612 
613 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
614 
615 		if (tocpy == db->db_size)
616 			dmu_buf_will_fill(db, tx);
617 		else
618 			dmu_buf_will_dirty(db, tx);
619 
620 		bcopy(buf, (char *)db->db_data + bufoff, tocpy);
621 
622 		if (tocpy == db->db_size)
623 			dmu_buf_fill_done(db, tx);
624 
625 		offset += tocpy;
626 		size -= tocpy;
627 		buf = (char *)buf + tocpy;
628 	}
629 	dmu_buf_rele_array(dbp, numbufs, FTAG);
630 }
631 
632 #ifdef _KERNEL
633 int
634 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
635 {
636 	dmu_buf_t **dbp;
637 	int numbufs, i, err;
638 
639 	/*
640 	 * NB: we could do this block-at-a-time, but it's nice
641 	 * to be reading in parallel.
642 	 */
643 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
644 	    &numbufs, &dbp);
645 	if (err)
646 		return (err);
647 
648 	for (i = 0; i < numbufs; i++) {
649 		int tocpy;
650 		int bufoff;
651 		dmu_buf_t *db = dbp[i];
652 
653 		ASSERT(size > 0);
654 
655 		bufoff = uio->uio_loffset - db->db_offset;
656 		tocpy = (int)MIN(db->db_size - bufoff, size);
657 
658 		err = uiomove((char *)db->db_data + bufoff, tocpy,
659 		    UIO_READ, uio);
660 		if (err)
661 			break;
662 
663 		size -= tocpy;
664 	}
665 	dmu_buf_rele_array(dbp, numbufs, FTAG);
666 
667 	return (err);
668 }
669 
670 int
671 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
672     dmu_tx_t *tx)
673 {
674 	dmu_buf_t **dbp;
675 	int numbufs, i;
676 	int err = 0;
677 
678 	if (size == 0)
679 		return (0);
680 
681 	err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
682 	    FALSE, FTAG, &numbufs, &dbp);
683 	if (err)
684 		return (err);
685 
686 	for (i = 0; i < numbufs; i++) {
687 		int tocpy;
688 		int bufoff;
689 		dmu_buf_t *db = dbp[i];
690 
691 		ASSERT(size > 0);
692 
693 		bufoff = uio->uio_loffset - db->db_offset;
694 		tocpy = (int)MIN(db->db_size - bufoff, size);
695 
696 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
697 
698 		if (tocpy == db->db_size)
699 			dmu_buf_will_fill(db, tx);
700 		else
701 			dmu_buf_will_dirty(db, tx);
702 
703 		/*
704 		 * XXX uiomove could block forever (eg. nfs-backed
705 		 * pages).  There needs to be a uiolockdown() function
706 		 * to lock the pages in memory, so that uiomove won't
707 		 * block.
708 		 */
709 		err = uiomove((char *)db->db_data + bufoff, tocpy,
710 		    UIO_WRITE, uio);
711 
712 		if (tocpy == db->db_size)
713 			dmu_buf_fill_done(db, tx);
714 
715 		if (err)
716 			break;
717 
718 		size -= tocpy;
719 	}
720 	dmu_buf_rele_array(dbp, numbufs, FTAG);
721 	return (err);
722 }
723 
724 int
725 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
726     page_t *pp, dmu_tx_t *tx)
727 {
728 	dmu_buf_t **dbp;
729 	int numbufs, i;
730 	int err;
731 
732 	if (size == 0)
733 		return (0);
734 
735 	err = dmu_buf_hold_array(os, object, offset, size,
736 	    FALSE, FTAG, &numbufs, &dbp);
737 	if (err)
738 		return (err);
739 
740 	for (i = 0; i < numbufs; i++) {
741 		int tocpy, copied, thiscpy;
742 		int bufoff;
743 		dmu_buf_t *db = dbp[i];
744 		caddr_t va;
745 
746 		ASSERT(size > 0);
747 		ASSERT3U(db->db_size, >=, PAGESIZE);
748 
749 		bufoff = offset - db->db_offset;
750 		tocpy = (int)MIN(db->db_size - bufoff, size);
751 
752 		ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
753 
754 		if (tocpy == db->db_size)
755 			dmu_buf_will_fill(db, tx);
756 		else
757 			dmu_buf_will_dirty(db, tx);
758 
759 		for (copied = 0; copied < tocpy; copied += PAGESIZE) {
760 			ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
761 			thiscpy = MIN(PAGESIZE, tocpy - copied);
762 			va = zfs_map_page(pp, S_READ);
763 			bcopy(va, (char *)db->db_data + bufoff, thiscpy);
764 			zfs_unmap_page(pp, va);
765 			pp = pp->p_next;
766 			bufoff += PAGESIZE;
767 		}
768 
769 		if (tocpy == db->db_size)
770 			dmu_buf_fill_done(db, tx);
771 
772 		if (err)
773 			break;
774 
775 		offset += tocpy;
776 		size -= tocpy;
777 	}
778 	dmu_buf_rele_array(dbp, numbufs, FTAG);
779 	return (err);
780 }
781 #endif
782 
783 typedef struct {
784 	dbuf_dirty_record_t	*dr;
785 	dmu_sync_cb_t		*done;
786 	void			*arg;
787 } dmu_sync_arg_t;
788 
789 /* ARGSUSED */
790 static void
791 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
792 {
793 	dmu_sync_arg_t *in = varg;
794 	dbuf_dirty_record_t *dr = in->dr;
795 	dmu_buf_impl_t *db = dr->dr_dbuf;
796 	dmu_sync_cb_t *done = in->done;
797 
798 	if (!BP_IS_HOLE(zio->io_bp)) {
799 		zio->io_bp->blk_fill = 1;
800 		BP_SET_TYPE(zio->io_bp, db->db_dnode->dn_type);
801 		BP_SET_LEVEL(zio->io_bp, 0);
802 	}
803 
804 	mutex_enter(&db->db_mtx);
805 	ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
806 	dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
807 	dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
808 	cv_broadcast(&db->db_changed);
809 	mutex_exit(&db->db_mtx);
810 
811 	if (done)
812 		done(&(db->db), in->arg);
813 
814 	kmem_free(in, sizeof (dmu_sync_arg_t));
815 }
816 
817 /*
818  * Intent log support: sync the block associated with db to disk.
819  * N.B. and XXX: the caller is responsible for making sure that the
820  * data isn't changing while dmu_sync() is writing it.
821  *
822  * Return values:
823  *
824  *	EEXIST: this txg has already been synced, so there's nothing to to.
825  *		The caller should not log the write.
826  *
827  *	ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
828  *		The caller should not log the write.
829  *
830  *	EALREADY: this block is already in the process of being synced.
831  *		The caller should track its progress (somehow).
832  *
833  *	EINPROGRESS: the IO has been initiated.
834  *		The caller should log this blkptr in the callback.
835  *
836  *	0: completed.  Sets *bp to the blkptr just written.
837  *		The caller should log this blkptr immediately.
838  */
839 int
840 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
841     blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
842 {
843 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
844 	objset_impl_t *os = db->db_objset;
845 	dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
846 	tx_state_t *tx = &dp->dp_tx;
847 	dbuf_dirty_record_t *dr;
848 	dmu_sync_arg_t *in;
849 	zbookmark_t zb;
850 	writeprops_t wp = { 0 };
851 	zio_t *zio;
852 	int zio_flags;
853 	int err;
854 
855 	ASSERT(BP_IS_HOLE(bp));
856 	ASSERT(txg != 0);
857 
858 
859 	dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
860 	    txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
861 
862 	/*
863 	 * XXX - would be nice if we could do this without suspending...
864 	 */
865 	txg_suspend(dp);
866 
867 	/*
868 	 * If this txg already synced, there's nothing to do.
869 	 */
870 	if (txg <= tx->tx_synced_txg) {
871 		txg_resume(dp);
872 		/*
873 		 * If we're running ziltest, we need the blkptr regardless.
874 		 */
875 		if (txg > spa_freeze_txg(dp->dp_spa)) {
876 			/* if db_blkptr == NULL, this was an empty write */
877 			if (db->db_blkptr)
878 				*bp = *db->db_blkptr; /* structure assignment */
879 			return (0);
880 		}
881 		return (EEXIST);
882 	}
883 
884 	mutex_enter(&db->db_mtx);
885 
886 	if (txg == tx->tx_syncing_txg) {
887 		while (db->db_data_pending) {
888 			/*
889 			 * IO is in-progress.  Wait for it to finish.
890 			 * XXX - would be nice to be able to somehow "attach"
891 			 * this zio to the parent zio passed in.
892 			 */
893 			cv_wait(&db->db_changed, &db->db_mtx);
894 			if (!db->db_data_pending &&
895 			    db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
896 				/*
897 				 * IO was compressed away
898 				 */
899 				*bp = *db->db_blkptr; /* structure assignment */
900 				mutex_exit(&db->db_mtx);
901 				txg_resume(dp);
902 				return (0);
903 			}
904 			ASSERT(db->db_data_pending ||
905 			    (db->db_blkptr && db->db_blkptr->blk_birth == txg));
906 		}
907 
908 		if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
909 			/*
910 			 * IO is already completed.
911 			 */
912 			*bp = *db->db_blkptr; /* structure assignment */
913 			mutex_exit(&db->db_mtx);
914 			txg_resume(dp);
915 			return (0);
916 		}
917 	}
918 
919 	dr = db->db_last_dirty;
920 	while (dr && dr->dr_txg > txg)
921 		dr = dr->dr_next;
922 	if (dr == NULL || dr->dr_txg < txg) {
923 		/*
924 		 * This dbuf isn't dirty, must have been free_range'd.
925 		 * There's no need to log writes to freed blocks, so we're done.
926 		 */
927 		mutex_exit(&db->db_mtx);
928 		txg_resume(dp);
929 		return (ENOENT);
930 	}
931 
932 	ASSERT(dr->dr_txg == txg);
933 	if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
934 		/*
935 		 * We have already issued a sync write for this buffer.
936 		 */
937 		mutex_exit(&db->db_mtx);
938 		txg_resume(dp);
939 		return (EALREADY);
940 	} else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
941 		/*
942 		 * This buffer has already been synced.  It could not
943 		 * have been dirtied since, or we would have cleared the state.
944 		 */
945 		*bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
946 		mutex_exit(&db->db_mtx);
947 		txg_resume(dp);
948 		return (0);
949 	}
950 
951 	dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
952 	in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
953 	in->dr = dr;
954 	in->done = done;
955 	in->arg = arg;
956 	mutex_exit(&db->db_mtx);
957 	txg_resume(dp);
958 
959 	zb.zb_objset = os->os_dsl_dataset->ds_object;
960 	zb.zb_object = db->db.db_object;
961 	zb.zb_level = db->db_level;
962 	zb.zb_blkid = db->db_blkid;
963 	zio_flags = ZIO_FLAG_MUSTSUCCEED;
964 	if (dmu_ot[db->db_dnode->dn_type].ot_metadata || zb.zb_level != 0)
965 		zio_flags |= ZIO_FLAG_METADATA;
966 	wp.wp_type = db->db_dnode->dn_type;
967 	wp.wp_copies = os->os_copies;
968 	wp.wp_level = db->db_level;
969 	wp.wp_dnchecksum = db->db_dnode->dn_checksum;
970 	wp.wp_oschecksum = os->os_checksum;
971 	wp.wp_dncompress = db->db_dnode->dn_compress;
972 	wp.wp_oscompress = os->os_compress;
973 	zio = arc_write(pio, os->os_spa, &wp,
974 	    DBUF_IS_L2CACHEABLE(db), txg, bp, dr->dt.dl.dr_data, NULL,
975 	    dmu_sync_done, in, ZIO_PRIORITY_SYNC_WRITE, zio_flags, &zb);
976 
977 	if (pio) {
978 		zio_nowait(zio);
979 		err = EINPROGRESS;
980 	} else {
981 		err = zio_wait(zio);
982 		ASSERT(err == 0);
983 	}
984 	return (err);
985 }
986 
987 int
988 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
989 	dmu_tx_t *tx)
990 {
991 	dnode_t *dn;
992 	int err;
993 
994 	err = dnode_hold(os->os, object, FTAG, &dn);
995 	if (err)
996 		return (err);
997 	err = dnode_set_blksz(dn, size, ibs, tx);
998 	dnode_rele(dn, FTAG);
999 	return (err);
1000 }
1001 
1002 void
1003 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1004 	dmu_tx_t *tx)
1005 {
1006 	dnode_t *dn;
1007 
1008 	/* XXX assumes dnode_hold will not get an i/o error */
1009 	(void) dnode_hold(os->os, object, FTAG, &dn);
1010 	ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1011 	dn->dn_checksum = checksum;
1012 	dnode_setdirty(dn, tx);
1013 	dnode_rele(dn, FTAG);
1014 }
1015 
1016 void
1017 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1018 	dmu_tx_t *tx)
1019 {
1020 	dnode_t *dn;
1021 
1022 	/* XXX assumes dnode_hold will not get an i/o error */
1023 	(void) dnode_hold(os->os, object, FTAG, &dn);
1024 	ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1025 	dn->dn_compress = compress;
1026 	dnode_setdirty(dn, tx);
1027 	dnode_rele(dn, FTAG);
1028 }
1029 
1030 int
1031 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1032 {
1033 	dnode_t *dn;
1034 	int i, err;
1035 
1036 	err = dnode_hold(os->os, object, FTAG, &dn);
1037 	if (err)
1038 		return (err);
1039 	/*
1040 	 * Sync any current changes before
1041 	 * we go trundling through the block pointers.
1042 	 */
1043 	for (i = 0; i < TXG_SIZE; i++) {
1044 		if (list_link_active(&dn->dn_dirty_link[i]))
1045 			break;
1046 	}
1047 	if (i != TXG_SIZE) {
1048 		dnode_rele(dn, FTAG);
1049 		txg_wait_synced(dmu_objset_pool(os), 0);
1050 		err = dnode_hold(os->os, object, FTAG, &dn);
1051 		if (err)
1052 			return (err);
1053 	}
1054 
1055 	err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1056 	dnode_rele(dn, FTAG);
1057 
1058 	return (err);
1059 }
1060 
1061 void
1062 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1063 {
1064 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1065 	mutex_enter(&dn->dn_mtx);
1066 
1067 	doi->doi_data_block_size = dn->dn_datablksz;
1068 	doi->doi_metadata_block_size = dn->dn_indblkshift ?
1069 	    1ULL << dn->dn_indblkshift : 0;
1070 	doi->doi_indirection = dn->dn_nlevels;
1071 	doi->doi_checksum = dn->dn_checksum;
1072 	doi->doi_compress = dn->dn_compress;
1073 	doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1074 	    SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1075 	doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1076 	doi->doi_type = dn->dn_type;
1077 	doi->doi_bonus_size = dn->dn_bonuslen;
1078 	doi->doi_bonus_type = dn->dn_bonustype;
1079 
1080 	mutex_exit(&dn->dn_mtx);
1081 	rw_exit(&dn->dn_struct_rwlock);
1082 }
1083 
1084 /*
1085  * Get information on a DMU object.
1086  * If doi is NULL, just indicates whether the object exists.
1087  */
1088 int
1089 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1090 {
1091 	dnode_t *dn;
1092 	int err = dnode_hold(os->os, object, FTAG, &dn);
1093 
1094 	if (err)
1095 		return (err);
1096 
1097 	if (doi != NULL)
1098 		dmu_object_info_from_dnode(dn, doi);
1099 
1100 	dnode_rele(dn, FTAG);
1101 	return (0);
1102 }
1103 
1104 /*
1105  * As above, but faster; can be used when you have a held dbuf in hand.
1106  */
1107 void
1108 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1109 {
1110 	dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1111 }
1112 
1113 /*
1114  * Faster still when you only care about the size.
1115  * This is specifically optimized for zfs_getattr().
1116  */
1117 void
1118 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1119 {
1120 	dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1121 
1122 	*blksize = dn->dn_datablksz;
1123 	/* add 1 for dnode space */
1124 	*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1125 	    SPA_MINBLOCKSHIFT) + 1;
1126 }
1127 
1128 void
1129 byteswap_uint64_array(void *vbuf, size_t size)
1130 {
1131 	uint64_t *buf = vbuf;
1132 	size_t count = size >> 3;
1133 	int i;
1134 
1135 	ASSERT((size & 7) == 0);
1136 
1137 	for (i = 0; i < count; i++)
1138 		buf[i] = BSWAP_64(buf[i]);
1139 }
1140 
1141 void
1142 byteswap_uint32_array(void *vbuf, size_t size)
1143 {
1144 	uint32_t *buf = vbuf;
1145 	size_t count = size >> 2;
1146 	int i;
1147 
1148 	ASSERT((size & 3) == 0);
1149 
1150 	for (i = 0; i < count; i++)
1151 		buf[i] = BSWAP_32(buf[i]);
1152 }
1153 
1154 void
1155 byteswap_uint16_array(void *vbuf, size_t size)
1156 {
1157 	uint16_t *buf = vbuf;
1158 	size_t count = size >> 1;
1159 	int i;
1160 
1161 	ASSERT((size & 1) == 0);
1162 
1163 	for (i = 0; i < count; i++)
1164 		buf[i] = BSWAP_16(buf[i]);
1165 }
1166 
1167 /* ARGSUSED */
1168 void
1169 byteswap_uint8_array(void *vbuf, size_t size)
1170 {
1171 }
1172 
1173 void
1174 dmu_init(void)
1175 {
1176 	dbuf_init();
1177 	dnode_init();
1178 	arc_init();
1179 	l2arc_init();
1180 }
1181 
1182 void
1183 dmu_fini(void)
1184 {
1185 	arc_fini();
1186 	dnode_fini();
1187 	dbuf_fini();
1188 	l2arc_fini();
1189 }
1190