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