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