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