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