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