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