xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 60425338a8e9a5ded7e559e227eedd42d30c8967)
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 2006 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/zfs_context.h>
29 #include <sys/dmu_objset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_prop.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_synctask.h>
35 #include <sys/dnode.h>
36 #include <sys/dbuf.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/zio_checksum.h>
39 #include <sys/zap.h>
40 #include <sys/zil.h>
41 #include <sys/dmu_impl.h>
42 
43 
44 spa_t *
45 dmu_objset_spa(objset_t *os)
46 {
47 	return (os->os->os_spa);
48 }
49 
50 zilog_t *
51 dmu_objset_zil(objset_t *os)
52 {
53 	return (os->os->os_zil);
54 }
55 
56 dsl_pool_t *
57 dmu_objset_pool(objset_t *os)
58 {
59 	dsl_dataset_t *ds;
60 
61 	if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir)
62 		return (ds->ds_dir->dd_pool);
63 	else
64 		return (spa_get_dsl(os->os->os_spa));
65 }
66 
67 dsl_dataset_t *
68 dmu_objset_ds(objset_t *os)
69 {
70 	return (os->os->os_dsl_dataset);
71 }
72 
73 dmu_objset_type_t
74 dmu_objset_type(objset_t *os)
75 {
76 	return (os->os->os_phys->os_type);
77 }
78 
79 void
80 dmu_objset_name(objset_t *os, char *buf)
81 {
82 	dsl_dataset_name(os->os->os_dsl_dataset, buf);
83 }
84 
85 uint64_t
86 dmu_objset_id(objset_t *os)
87 {
88 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
89 
90 	return (ds ? ds->ds_object : 0);
91 }
92 
93 static void
94 checksum_changed_cb(void *arg, uint64_t newval)
95 {
96 	objset_impl_t *osi = arg;
97 
98 	/*
99 	 * Inheritance should have been done by now.
100 	 */
101 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
102 
103 	osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
104 }
105 
106 static void
107 compression_changed_cb(void *arg, uint64_t newval)
108 {
109 	objset_impl_t *osi = arg;
110 
111 	/*
112 	 * Inheritance and range checking should have been done by now.
113 	 */
114 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
115 
116 	osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
117 }
118 
119 void
120 dmu_objset_byteswap(void *buf, size_t size)
121 {
122 	objset_phys_t *osp = buf;
123 
124 	ASSERT(size == sizeof (objset_phys_t));
125 	dnode_byteswap(&osp->os_meta_dnode);
126 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
127 	osp->os_type = BSWAP_64(osp->os_type);
128 }
129 
130 int
131 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
132     objset_impl_t **osip)
133 {
134 	objset_impl_t *winner, *osi;
135 	int i, err, checksum;
136 
137 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
138 	osi->os.os = osi;
139 	osi->os_dsl_dataset = ds;
140 	osi->os_spa = spa;
141 	if (bp)
142 		osi->os_rootbp = *bp;
143 	osi->os_phys = zio_buf_alloc(sizeof (objset_phys_t));
144 	if (!BP_IS_HOLE(&osi->os_rootbp)) {
145 		uint32_t aflags = ARC_WAIT;
146 		zbookmark_t zb;
147 		zb.zb_objset = ds ? ds->ds_object : 0;
148 		zb.zb_object = 0;
149 		zb.zb_level = -1;
150 		zb.zb_blkid = 0;
151 
152 		dprintf_bp(&osi->os_rootbp, "reading %s", "");
153 		err = arc_read(NULL, spa, &osi->os_rootbp,
154 		    dmu_ot[DMU_OT_OBJSET].ot_byteswap,
155 		    arc_bcopy_func, osi->os_phys,
156 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
157 		if (err) {
158 			zio_buf_free(osi->os_phys, sizeof (objset_phys_t));
159 			kmem_free(osi, sizeof (objset_impl_t));
160 			return (err);
161 		}
162 	} else {
163 		bzero(osi->os_phys, sizeof (objset_phys_t));
164 	}
165 
166 	/*
167 	 * Note: the changed_cb will be called once before the register
168 	 * func returns, thus changing the checksum/compression from the
169 	 * default (fletcher2/off).  Snapshots don't need to know, and
170 	 * registering would complicate clone promotion.
171 	 */
172 	if (ds && ds->ds_phys->ds_num_children == 0) {
173 		err = dsl_prop_register(ds, "checksum",
174 		    checksum_changed_cb, osi);
175 		if (err == 0)
176 			err = dsl_prop_register(ds, "compression",
177 			    compression_changed_cb, osi);
178 		if (err) {
179 			zio_buf_free(osi->os_phys, sizeof (objset_phys_t));
180 			kmem_free(osi, sizeof (objset_impl_t));
181 			return (err);
182 		}
183 	} else if (ds == NULL) {
184 		/* It's the meta-objset. */
185 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
186 		osi->os_compress = ZIO_COMPRESS_LZJB;
187 	}
188 
189 	osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header);
190 
191 	/*
192 	 * Metadata always gets compressed and checksummed.
193 	 * If the data checksum is multi-bit correctable, and it's not
194 	 * a ZBT-style checksum, then it's suitable for metadata as well.
195 	 * Otherwise, the metadata checksum defaults to fletcher4.
196 	 */
197 	checksum = osi->os_checksum;
198 
199 	if (zio_checksum_table[checksum].ci_correctable &&
200 	    !zio_checksum_table[checksum].ci_zbt)
201 		osi->os_md_checksum = checksum;
202 	else
203 		osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4;
204 	osi->os_md_compress = ZIO_COMPRESS_LZJB;
205 
206 	for (i = 0; i < TXG_SIZE; i++) {
207 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
208 		    offsetof(dnode_t, dn_dirty_link[i]));
209 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
210 		    offsetof(dnode_t, dn_dirty_link[i]));
211 	}
212 	list_create(&osi->os_dnodes, sizeof (dnode_t),
213 	    offsetof(dnode_t, dn_link));
214 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
215 	    offsetof(dmu_buf_impl_t, db_link));
216 
217 	osi->os_meta_dnode = dnode_special_open(osi,
218 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
219 
220 	if (ds != NULL) {
221 		winner = dsl_dataset_set_user_ptr(ds, osi, dmu_objset_evict);
222 		if (winner) {
223 			dmu_objset_evict(ds, osi);
224 			osi = winner;
225 		}
226 	}
227 
228 	*osip = osi;
229 	return (0);
230 }
231 
232 /* called from zpl */
233 int
234 dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
235     objset_t **osp)
236 {
237 	dsl_dataset_t *ds;
238 	int err;
239 	objset_t *os;
240 	objset_impl_t *osi;
241 
242 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
243 	err = dsl_dataset_open(name, mode, os, &ds);
244 	if (err) {
245 		kmem_free(os, sizeof (objset_t));
246 		return (err);
247 	}
248 
249 	osi = dsl_dataset_get_user_ptr(ds);
250 	if (osi == NULL) {
251 		blkptr_t bp;
252 
253 		dsl_dataset_get_blkptr(ds, &bp);
254 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
255 		    ds, &bp, &osi);
256 		if (err) {
257 			dsl_dataset_close(ds, mode, os);
258 			kmem_free(os, sizeof (objset_t));
259 			return (err);
260 		}
261 	}
262 
263 	os->os = osi;
264 	os->os_mode = mode;
265 
266 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) {
267 		dmu_objset_close(os);
268 		return (EINVAL);
269 	}
270 	*osp = os;
271 	return (0);
272 }
273 
274 void
275 dmu_objset_close(objset_t *os)
276 {
277 	dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os);
278 	kmem_free(os, sizeof (objset_t));
279 }
280 
281 int
282 dmu_objset_evict_dbufs(objset_t *os, int try)
283 {
284 	objset_impl_t *osi = os->os;
285 	dnode_t *dn;
286 
287 	mutex_enter(&osi->os_lock);
288 
289 	/* process the mdn last, since the other dnodes have holds on it */
290 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
291 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
292 
293 	/*
294 	 * Find the first dnode with holds.  We have to do this dance
295 	 * because dnode_add_ref() only works if you already have a
296 	 * hold.  If there are no holds then it has no dbufs so OK to
297 	 * skip.
298 	 */
299 	for (dn = list_head(&osi->os_dnodes);
300 	    dn && refcount_is_zero(&dn->dn_holds);
301 	    dn = list_next(&osi->os_dnodes, dn))
302 		continue;
303 	if (dn)
304 		dnode_add_ref(dn, FTAG);
305 
306 	while (dn) {
307 		dnode_t *next_dn = dn;
308 
309 		do {
310 			next_dn = list_next(&osi->os_dnodes, next_dn);
311 		} while (next_dn && refcount_is_zero(&next_dn->dn_holds));
312 		if (next_dn)
313 			dnode_add_ref(next_dn, FTAG);
314 
315 		mutex_exit(&osi->os_lock);
316 		if (dnode_evict_dbufs(dn, try)) {
317 			dnode_rele(dn, FTAG);
318 			if (next_dn)
319 				dnode_rele(next_dn, FTAG);
320 			return (1);
321 		}
322 		dnode_rele(dn, FTAG);
323 		mutex_enter(&osi->os_lock);
324 		dn = next_dn;
325 	}
326 	mutex_exit(&osi->os_lock);
327 	return (0);
328 }
329 
330 void
331 dmu_objset_evict(dsl_dataset_t *ds, void *arg)
332 {
333 	objset_impl_t *osi = arg;
334 	objset_t os;
335 	int i;
336 
337 	for (i = 0; i < TXG_SIZE; i++) {
338 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
339 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
340 	}
341 
342 	if (ds && ds->ds_phys->ds_num_children == 0) {
343 		VERIFY(0 == dsl_prop_unregister(ds, "checksum",
344 		    checksum_changed_cb, osi));
345 		VERIFY(0 == dsl_prop_unregister(ds, "compression",
346 		    compression_changed_cb, osi));
347 	}
348 
349 	/*
350 	 * We should need only a single pass over the dnode list, since
351 	 * nothing can be added to the list at this point.
352 	 */
353 	os.os = osi;
354 	(void) dmu_objset_evict_dbufs(&os, 0);
355 
356 	ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
357 	ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
358 	ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
359 
360 	dnode_special_close(osi->os_meta_dnode);
361 	zil_free(osi->os_zil);
362 
363 	zio_buf_free(osi->os_phys, sizeof (objset_phys_t));
364 	kmem_free(osi, sizeof (objset_impl_t));
365 }
366 
367 /* called from dsl for meta-objset */
368 objset_impl_t *
369 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, dmu_objset_type_t type,
370     dmu_tx_t *tx)
371 {
372 	objset_impl_t *osi;
373 	dnode_t *mdn;
374 
375 	ASSERT(dmu_tx_is_syncing(tx));
376 	VERIFY(0 == dmu_objset_open_impl(spa, ds, NULL, &osi));
377 	mdn = osi->os_meta_dnode;
378 
379 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
380 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
381 
382 	/*
383 	 * We don't want to have to increase the meta-dnode's nlevels
384 	 * later, because then we could do it in quescing context while
385 	 * we are also accessing it in open context.
386 	 *
387 	 * This precaution is not necessary for the MOS (ds == NULL),
388 	 * because the MOS is only updated in syncing context.
389 	 * This is most fortunate: the MOS is the only objset that
390 	 * needs to be synced multiple times as spa_sync() iterates
391 	 * to convergence, so minimizing its dn_nlevels matters.
392 	 */
393 	if (ds != NULL) {
394 		int levels = 1;
395 
396 		/*
397 		 * Determine the number of levels necessary for the meta-dnode
398 		 * to contain DN_MAX_OBJECT dnodes.
399 		 */
400 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
401 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
402 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
403 			levels++;
404 
405 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
406 		    mdn->dn_nlevels = levels;
407 	}
408 
409 	ASSERT(type != DMU_OST_NONE);
410 	ASSERT(type != DMU_OST_ANY);
411 	ASSERT(type < DMU_OST_NUMTYPES);
412 	osi->os_phys->os_type = type;
413 
414 	dsl_dataset_dirty(ds, tx);
415 
416 	return (osi);
417 }
418 
419 struct oscarg {
420 	void (*userfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
421 	void *userarg;
422 	dsl_dataset_t *clone_parent;
423 	const char *lastname;
424 	dmu_objset_type_t type;
425 };
426 
427 /* ARGSUSED */
428 static int
429 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
430 {
431 	dsl_dir_t *dd = arg1;
432 	struct oscarg *oa = arg2;
433 	objset_t *mos = dd->dd_pool->dp_meta_objset;
434 	int err;
435 	uint64_t ddobj;
436 
437 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
438 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
439 	if (err != ENOENT)
440 		return (err ? err : EEXIST);
441 
442 	if (oa->clone_parent != NULL) {
443 		/*
444 		 * You can't clone across pools.
445 		 */
446 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
447 			return (EXDEV);
448 
449 		/*
450 		 * You can only clone snapshots, not the head datasets.
451 		 */
452 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
453 			return (EINVAL);
454 	}
455 	return (0);
456 }
457 
458 static void
459 dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
460 {
461 	dsl_dir_t *dd = arg1;
462 	struct oscarg *oa = arg2;
463 	dsl_dataset_t *ds;
464 	blkptr_t bp;
465 	uint64_t dsobj;
466 
467 	ASSERT(dmu_tx_is_syncing(tx));
468 
469 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
470 	    oa->clone_parent, tx);
471 
472 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
473 	    DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds));
474 	dsl_dataset_get_blkptr(ds, &bp);
475 	if (BP_IS_HOLE(&bp)) {
476 		objset_impl_t *osi;
477 
478 		/* This is an empty dmu_objset; not a clone. */
479 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
480 		    ds, oa->type, tx);
481 
482 		if (oa->userfunc)
483 			oa->userfunc(&osi->os, oa->userarg, tx);
484 	}
485 	dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG);
486 }
487 
488 int
489 dmu_objset_create(const char *name, dmu_objset_type_t type,
490     objset_t *clone_parent,
491     void (*func)(objset_t *os, void *arg, dmu_tx_t *tx), void *arg)
492 {
493 	dsl_dir_t *pdd;
494 	const char *tail;
495 	int err = 0;
496 	struct oscarg oa = { 0 };
497 
498 	ASSERT(strchr(name, '@') == NULL);
499 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
500 	if (err)
501 		return (err);
502 	if (tail == NULL) {
503 		dsl_dir_close(pdd, FTAG);
504 		return (EEXIST);
505 	}
506 
507 	dprintf("name=%s\n", name);
508 
509 	oa.userfunc = func;
510 	oa.userarg = arg;
511 	oa.lastname = tail;
512 	oa.type = type;
513 	if (clone_parent != NULL) {
514 		/*
515 		 * You can't clone to a different type.
516 		 */
517 		if (clone_parent->os->os_phys->os_type != type) {
518 			dsl_dir_close(pdd, FTAG);
519 			return (EINVAL);
520 		}
521 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
522 	}
523 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
524 	    dmu_objset_create_sync, pdd, &oa, 5);
525 	dsl_dir_close(pdd, FTAG);
526 	return (err);
527 }
528 
529 int
530 dmu_objset_destroy(const char *name)
531 {
532 	objset_t *os;
533 	int error;
534 
535 	/*
536 	 * If it looks like we'll be able to destroy it, and there's
537 	 * an unplayed replay log sitting around, destroy the log.
538 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
539 	 * but the replay log objset is modified in open context.
540 	 */
541 	error = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_EXCLUSIVE, &os);
542 	if (error == 0) {
543 		zil_destroy(dmu_objset_zil(os), B_FALSE);
544 		dmu_objset_close(os);
545 	}
546 
547 	return (dsl_dataset_destroy(name));
548 }
549 
550 int
551 dmu_objset_rollback(const char *name)
552 {
553 	int err;
554 	objset_t *os;
555 
556 	err = dmu_objset_open(name, DMU_OST_ANY,
557 	    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os);
558 	if (err == 0) {
559 		err = zil_suspend(dmu_objset_zil(os));
560 		if (err == 0)
561 			zil_resume(dmu_objset_zil(os));
562 		if (err == 0) {
563 			/* XXX uncache everything? */
564 			err = dsl_dataset_rollback(os->os->os_dsl_dataset);
565 		}
566 		dmu_objset_close(os);
567 	}
568 	return (err);
569 }
570 
571 struct snaparg {
572 	dsl_sync_task_group_t *dstg;
573 	char *snapname;
574 	char failed[MAXPATHLEN];
575 };
576 
577 static int
578 dmu_objset_snapshot_one(char *name, void *arg)
579 {
580 	struct snaparg *sn = arg;
581 	objset_t *os;
582 	int err;
583 
584 	(void) strcpy(sn->failed, name);
585 
586 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
587 	if (err != 0)
588 		return (err);
589 
590 	/*
591 	 * NB: we need to wait for all in-flight changes to get to disk,
592 	 * so that we snapshot those changes.  zil_suspend does this as
593 	 * a side effect.
594 	 */
595 	err = zil_suspend(dmu_objset_zil(os));
596 	if (err == 0) {
597 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
598 		    dsl_dataset_snapshot_sync, os, sn->snapname, 3);
599 	}
600 	return (err);
601 }
602 
603 int
604 dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
605 {
606 	dsl_sync_task_t *dst;
607 	struct snaparg sn = { 0 };
608 	char *cp;
609 	spa_t *spa;
610 	int err;
611 
612 	(void) strcpy(sn.failed, fsname);
613 
614 	cp = strchr(fsname, '/');
615 	if (cp) {
616 		*cp = '\0';
617 		err = spa_open(fsname, &spa, FTAG);
618 		*cp = '/';
619 	} else {
620 		err = spa_open(fsname, &spa, FTAG);
621 	}
622 	if (err)
623 		return (err);
624 
625 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
626 	sn.snapname = snapname;
627 
628 	if (recursive)
629 		err = dmu_objset_find(fsname, dmu_objset_snapshot_one, &sn, 0);
630 	else
631 		err = dmu_objset_snapshot_one(fsname, &sn);
632 
633 	if (err)
634 		goto out;
635 
636 	err = dsl_sync_task_group_wait(sn.dstg);
637 
638 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
639 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
640 		objset_t *os = dst->dst_arg1;
641 		if (dst->dst_err)
642 			dmu_objset_name(os, sn.failed);
643 		zil_resume(dmu_objset_zil(os));
644 		dmu_objset_close(os);
645 	}
646 out:
647 	if (err)
648 		(void) strcpy(fsname, sn.failed);
649 	dsl_sync_task_group_destroy(sn.dstg);
650 	spa_close(spa, FTAG);
651 	return (err);
652 }
653 
654 static void
655 dmu_objset_sync_dnodes(objset_impl_t *os, list_t *list, dmu_tx_t *tx)
656 {
657 	dnode_t *dn = list_head(list);
658 	int level, err;
659 
660 	for (level = 0; dn = list_head(list); level++) {
661 		zio_t *zio;
662 		zio = zio_root(os->os_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
663 
664 		ASSERT3U(level, <=, DN_MAX_LEVELS);
665 
666 		while (dn) {
667 			dnode_t *next = list_next(list, dn);
668 
669 			list_remove(list, dn);
670 			if (dnode_sync(dn, level, zio, tx) == 0) {
671 				/*
672 				 * This dnode requires syncing at higher
673 				 * levels; put it back onto the list.
674 				 */
675 				if (next)
676 					list_insert_before(list, next, dn);
677 				else
678 					list_insert_tail(list, dn);
679 			}
680 			dn = next;
681 		}
682 		err = zio_wait(zio);
683 		ASSERT(err == 0);
684 	}
685 }
686 
687 /* ARGSUSED */
688 static void
689 killer(zio_t *zio, arc_buf_t *abuf, void *arg)
690 {
691 	objset_impl_t *os = arg;
692 	objset_phys_t *osphys = zio->io_data;
693 	dnode_phys_t *dnp = &osphys->os_meta_dnode;
694 	int i;
695 
696 	ASSERT3U(zio->io_error, ==, 0);
697 
698 	/*
699 	 * Update rootbp fill count.
700 	 */
701 	os->os_rootbp.blk_fill = 1;	/* count the meta-dnode */
702 	for (i = 0; i < dnp->dn_nblkptr; i++)
703 		os->os_rootbp.blk_fill += dnp->dn_blkptr[i].blk_fill;
704 
705 	BP_SET_TYPE(zio->io_bp, DMU_OT_OBJSET);
706 	BP_SET_LEVEL(zio->io_bp, 0);
707 
708 	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp),
709 	    BP_IDENTITY(&zio->io_bp_orig))) {
710 		dsl_dataset_block_kill(os->os_dsl_dataset, &zio->io_bp_orig,
711 		    os->os_synctx);
712 		dsl_dataset_block_born(os->os_dsl_dataset, zio->io_bp,
713 		    os->os_synctx);
714 	}
715 }
716 
717 
718 /* called from dsl */
719 void
720 dmu_objset_sync(objset_impl_t *os, dmu_tx_t *tx)
721 {
722 	extern taskq_t *dbuf_tq;
723 	int txgoff;
724 	list_t *dirty_list;
725 	int err;
726 	zbookmark_t zb;
727 	arc_buf_t *abuf =
728 	    arc_buf_alloc(os->os_spa, sizeof (objset_phys_t), FTAG);
729 
730 	ASSERT(dmu_tx_is_syncing(tx));
731 	ASSERT(os->os_synctx == NULL);
732 	/* XXX the write_done callback should really give us the tx... */
733 	os->os_synctx = tx;
734 
735 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
736 
737 	txgoff = tx->tx_txg & TXG_MASK;
738 
739 	dmu_objset_sync_dnodes(os, &os->os_free_dnodes[txgoff], tx);
740 	dmu_objset_sync_dnodes(os, &os->os_dirty_dnodes[txgoff], tx);
741 
742 	/*
743 	 * Free intent log blocks up to this tx.
744 	 */
745 	zil_sync(os->os_zil, tx);
746 
747 	/*
748 	 * Sync meta-dnode
749 	 */
750 	dirty_list = &os->os_dirty_dnodes[txgoff];
751 	ASSERT(list_head(dirty_list) == NULL);
752 	list_insert_tail(dirty_list, os->os_meta_dnode);
753 	dmu_objset_sync_dnodes(os, dirty_list, tx);
754 
755 	/*
756 	 * Sync the root block.
757 	 */
758 	bcopy(os->os_phys, abuf->b_data, sizeof (objset_phys_t));
759 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
760 	zb.zb_object = 0;
761 	zb.zb_level = -1;
762 	zb.zb_blkid = 0;
763 	err = arc_write(NULL, os->os_spa, os->os_md_checksum,
764 	    os->os_md_compress,
765 	    dmu_get_replication_level(os->os_spa, &zb, DMU_OT_OBJSET),
766 	    tx->tx_txg, &os->os_rootbp, abuf, killer, os,
767 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, ARC_WAIT, &zb);
768 	ASSERT(err == 0);
769 	VERIFY(arc_buf_remove_ref(abuf, FTAG) == 1);
770 
771 	dsl_dataset_set_blkptr(os->os_dsl_dataset, &os->os_rootbp, tx);
772 
773 	ASSERT3P(os->os_synctx, ==, tx);
774 	taskq_wait(dbuf_tq);
775 	os->os_synctx = NULL;
776 }
777 
778 void
779 dmu_objset_stats(objset_t *os, dmu_objset_stats_t *dds)
780 {
781 	if (os->os->os_dsl_dataset != NULL) {
782 		dsl_dataset_stats(os->os->os_dsl_dataset, dds);
783 	} else {
784 		ASSERT(os->os->os_phys->os_type == DMU_OST_META);
785 		bzero(dds, sizeof (*dds));
786 	}
787 	dds->dds_type = os->os->os_phys->os_type;
788 }
789 
790 int
791 dmu_objset_is_snapshot(objset_t *os)
792 {
793 	if (os->os->os_dsl_dataset != NULL)
794 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
795 	else
796 		return (B_FALSE);
797 }
798 
799 int
800 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
801     uint64_t *idp, uint64_t *offp)
802 {
803 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
804 	zap_cursor_t cursor;
805 	zap_attribute_t attr;
806 
807 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
808 		return (ENOENT);
809 
810 	zap_cursor_init_serialized(&cursor,
811 	    ds->ds_dir->dd_pool->dp_meta_objset,
812 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
813 
814 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
815 		zap_cursor_fini(&cursor);
816 		return (ENOENT);
817 	}
818 
819 	if (strlen(attr.za_name) + 1 > namelen) {
820 		zap_cursor_fini(&cursor);
821 		return (ENAMETOOLONG);
822 	}
823 
824 	(void) strcpy(name, attr.za_name);
825 	if (idp)
826 		*idp = attr.za_first_integer;
827 	zap_cursor_advance(&cursor);
828 	*offp = zap_cursor_serialize(&cursor);
829 	zap_cursor_fini(&cursor);
830 
831 	return (0);
832 }
833 
834 int
835 dmu_dir_list_next(objset_t *os, int namelen, char *name,
836     uint64_t *idp, uint64_t *offp)
837 {
838 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
839 	zap_cursor_t cursor;
840 	zap_attribute_t attr;
841 
842 	/* there is no next dir on a snapshot! */
843 	if (os->os->os_dsl_dataset->ds_object !=
844 	    dd->dd_phys->dd_head_dataset_obj)
845 		return (ENOENT);
846 
847 	zap_cursor_init_serialized(&cursor,
848 	    dd->dd_pool->dp_meta_objset,
849 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
850 
851 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
852 		zap_cursor_fini(&cursor);
853 		return (ENOENT);
854 	}
855 
856 	if (strlen(attr.za_name) + 1 > namelen) {
857 		zap_cursor_fini(&cursor);
858 		return (ENAMETOOLONG);
859 	}
860 
861 	(void) strcpy(name, attr.za_name);
862 	if (idp)
863 		*idp = attr.za_first_integer;
864 	zap_cursor_advance(&cursor);
865 	*offp = zap_cursor_serialize(&cursor);
866 	zap_cursor_fini(&cursor);
867 
868 	return (0);
869 }
870 
871 /*
872  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
873  */
874 int
875 dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
876 {
877 	dsl_dir_t *dd;
878 	objset_t *os;
879 	uint64_t snapobj;
880 	zap_cursor_t zc;
881 	zap_attribute_t attr;
882 	char *child;
883 	int do_self, err;
884 
885 	err = dsl_dir_open(name, FTAG, &dd, NULL);
886 	if (err)
887 		return (err);
888 
889 	/* NB: the $MOS dir doesn't have a head dataset */
890 	do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
891 
892 	/*
893 	 * Iterate over all children.
894 	 */
895 	for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
896 	    dd->dd_phys->dd_child_dir_zapobj);
897 	    zap_cursor_retrieve(&zc, &attr) == 0;
898 	    (void) zap_cursor_advance(&zc)) {
899 		ASSERT(attr.za_integer_length == sizeof (uint64_t));
900 		ASSERT(attr.za_num_integers == 1);
901 
902 		/*
903 		 * No separating '/' because parent's name ends in /.
904 		 */
905 		child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
906 		/* XXX could probably just use name here */
907 		dsl_dir_name(dd, child);
908 		(void) strcat(child, "/");
909 		(void) strcat(child, attr.za_name);
910 		err = dmu_objset_find(child, func, arg, flags);
911 		kmem_free(child, MAXPATHLEN);
912 		if (err)
913 			break;
914 	}
915 	zap_cursor_fini(&zc);
916 
917 	if (err) {
918 		dsl_dir_close(dd, FTAG);
919 		return (err);
920 	}
921 
922 	/*
923 	 * Iterate over all snapshots.
924 	 */
925 	if ((flags & DS_FIND_SNAPSHOTS) &&
926 	    dmu_objset_open(name, DMU_OST_ANY,
927 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
928 
929 		snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
930 		dmu_objset_close(os);
931 
932 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
933 		    zap_cursor_retrieve(&zc, &attr) == 0;
934 		    (void) zap_cursor_advance(&zc)) {
935 			ASSERT(attr.za_integer_length == sizeof (uint64_t));
936 			ASSERT(attr.za_num_integers == 1);
937 
938 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
939 			/* XXX could probably just use name here */
940 			dsl_dir_name(dd, child);
941 			(void) strcat(child, "@");
942 			(void) strcat(child, attr.za_name);
943 			err = func(child, arg);
944 			kmem_free(child, MAXPATHLEN);
945 			if (err)
946 				break;
947 		}
948 		zap_cursor_fini(&zc);
949 	}
950 
951 	dsl_dir_close(dd, FTAG);
952 
953 	if (err)
954 		return (err);
955 
956 	/*
957 	 * Apply to self if appropriate.
958 	 */
959 	if (do_self)
960 		err = func(name, arg);
961 	return (err);
962 }
963