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