xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 11935:538c866aaac6)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2211620SMatthew.Ahrens@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264543Smarks #include <sys/cred.h>
27789Sahrens #include <sys/zfs_context.h>
28789Sahrens #include <sys/dmu_objset.h>
29789Sahrens #include <sys/dsl_dir.h>
30789Sahrens #include <sys/dsl_dataset.h>
31789Sahrens #include <sys/dsl_prop.h>
32789Sahrens #include <sys/dsl_pool.h>
332199Sahrens #include <sys/dsl_synctask.h>
344543Smarks #include <sys/dsl_deleg.h>
35789Sahrens #include <sys/dnode.h>
36789Sahrens #include <sys/dbuf.h>
372885Sahrens #include <sys/zvol.h>
38789Sahrens #include <sys/dmu_tx.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/zil.h>
41789Sahrens #include <sys/dmu_impl.h>
424543Smarks #include <sys/zfs_ioctl.h>
4311165SMatthew.Ahrens@Sun.COM #include <sys/sunddi.h>
44*11935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
45789Sahrens 
46789Sahrens spa_t *
47789Sahrens dmu_objset_spa(objset_t *os)
48789Sahrens {
4910298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
50789Sahrens }
51789Sahrens 
52789Sahrens zilog_t *
53789Sahrens dmu_objset_zil(objset_t *os)
54789Sahrens {
5510298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
56789Sahrens }
57789Sahrens 
58789Sahrens dsl_pool_t *
59789Sahrens dmu_objset_pool(objset_t *os)
60789Sahrens {
61789Sahrens 	dsl_dataset_t *ds;
62789Sahrens 
6310298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
64789Sahrens 		return (ds->ds_dir->dd_pool);
65789Sahrens 	else
6610298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
67789Sahrens }
68789Sahrens 
69789Sahrens dsl_dataset_t *
70789Sahrens dmu_objset_ds(objset_t *os)
71789Sahrens {
7210298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
73789Sahrens }
74789Sahrens 
75789Sahrens dmu_objset_type_t
76789Sahrens dmu_objset_type(objset_t *os)
77789Sahrens {
7810298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
79789Sahrens }
80789Sahrens 
81789Sahrens void
82789Sahrens dmu_objset_name(objset_t *os, char *buf)
83789Sahrens {
8410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
85789Sahrens }
86789Sahrens 
87789Sahrens uint64_t
88789Sahrens dmu_objset_id(objset_t *os)
89789Sahrens {
9010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
91789Sahrens 
92789Sahrens 	return (ds ? ds->ds_object : 0);
93789Sahrens }
94789Sahrens 
9510310SNeil.Perrin@Sun.COM uint64_t
9610310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
9710310SNeil.Perrin@Sun.COM {
9810310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
9910310SNeil.Perrin@Sun.COM }
10010310SNeil.Perrin@Sun.COM 
101789Sahrens static void
102789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
103789Sahrens {
10410298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
105789Sahrens 
106789Sahrens 	/*
107789Sahrens 	 * Inheritance should have been done by now.
108789Sahrens 	 */
109789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
110789Sahrens 
11110298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
112789Sahrens }
113789Sahrens 
114789Sahrens static void
115789Sahrens compression_changed_cb(void *arg, uint64_t newval)
116789Sahrens {
11710298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
118789Sahrens 
119789Sahrens 	/*
120789Sahrens 	 * Inheritance and range checking should have been done by now.
121789Sahrens 	 */
122789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
123789Sahrens 
12410298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
125789Sahrens }
126789Sahrens 
1273835Sahrens static void
1283835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1293835Sahrens {
13010298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1313835Sahrens 
1323835Sahrens 	/*
1333835Sahrens 	 * Inheritance and range checking should have been done by now.
1343835Sahrens 	 */
1353835Sahrens 	ASSERT(newval > 0);
13610298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1373835Sahrens 
13810298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1393835Sahrens }
1403835Sahrens 
1417237Sek110237 static void
14210922SJeff.Bonwick@Sun.COM dedup_changed_cb(void *arg, uint64_t newval)
14310922SJeff.Bonwick@Sun.COM {
14410922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
14510922SJeff.Bonwick@Sun.COM 	spa_t *spa = os->os_spa;
14610922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum;
14710922SJeff.Bonwick@Sun.COM 
14810922SJeff.Bonwick@Sun.COM 	/*
14910922SJeff.Bonwick@Sun.COM 	 * Inheritance should have been done by now.
15010922SJeff.Bonwick@Sun.COM 	 */
15110922SJeff.Bonwick@Sun.COM 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
15210922SJeff.Bonwick@Sun.COM 
15310922SJeff.Bonwick@Sun.COM 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
15410922SJeff.Bonwick@Sun.COM 
15510922SJeff.Bonwick@Sun.COM 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
15610922SJeff.Bonwick@Sun.COM 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
15710922SJeff.Bonwick@Sun.COM }
15810922SJeff.Bonwick@Sun.COM 
15910922SJeff.Bonwick@Sun.COM static void
1607237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1617237Sek110237 {
16210298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1637237Sek110237 
1647237Sek110237 	/*
1657237Sek110237 	 * Inheritance and range checking should have been done by now.
1667237Sek110237 	 */
1677237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1687237Sek110237 	    newval == ZFS_CACHE_METADATA);
1697237Sek110237 
17010298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1717237Sek110237 }
1727237Sek110237 
1737237Sek110237 static void
1747237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1757237Sek110237 {
17610298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1777237Sek110237 
1787237Sek110237 	/*
1797237Sek110237 	 * Inheritance and range checking should have been done by now.
1807237Sek110237 	 */
1817237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1827237Sek110237 	    newval == ZFS_CACHE_METADATA);
1837237Sek110237 
18410298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
1857237Sek110237 }
1867237Sek110237 
18710310SNeil.Perrin@Sun.COM static void
18810310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
18910310SNeil.Perrin@Sun.COM {
19010310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
19110310SNeil.Perrin@Sun.COM 
19210310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
19310310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
19410310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
19510310SNeil.Perrin@Sun.COM 	if (os->os_zil)
19610310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
19710310SNeil.Perrin@Sun.COM }
19810310SNeil.Perrin@Sun.COM 
199789Sahrens void
200789Sahrens dmu_objset_byteswap(void *buf, size_t size)
201789Sahrens {
202789Sahrens 	objset_phys_t *osp = buf;
203789Sahrens 
2049396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
205789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
206789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
207789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
2089396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
2099396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
2109396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
2119396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
2129396SMatthew.Ahrens@Sun.COM 	}
213789Sahrens }
214789Sahrens 
2151544Seschrock int
2161544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
21710298SMatthew.Ahrens@Sun.COM     objset_t **osp)
218789Sahrens {
21910298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2207046Sahrens 	int i, err;
221789Sahrens 
2224787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2234787Sahrens 
22410298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
22510298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
22610298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
22710298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
22810298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2292391Smaybee 		uint32_t aflags = ARC_WAIT;
2301544Seschrock 		zbookmark_t zb;
23110922SJeff.Bonwick@Sun.COM 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
23210922SJeff.Bonwick@Sun.COM 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
23310922SJeff.Bonwick@Sun.COM 
23410298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2357237Sek110237 			aflags |= ARC_L2CACHE;
2361544Seschrock 
23710298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2387046Sahrens 		/*
2397046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2407046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2417046Sahrens 		 * could change, and we'll need a lock.
2427046Sahrens 		 */
24310298SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
24410298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2452391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2461544Seschrock 		if (err) {
24710298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2487294Sperrin 			/* convert checksum errors into IO errors */
2497294Sperrin 			if (err == ECKSUM)
2507294Sperrin 				err = EIO;
2511544Seschrock 			return (err);
2521544Seschrock 		}
2539396SMatthew.Ahrens@Sun.COM 
2549396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2559396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
25610298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2579396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
25810298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2599396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2609396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
26110298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
26210298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
26310298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
26410298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
26510298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
2669396SMatthew.Ahrens@Sun.COM 		}
2679396SMatthew.Ahrens@Sun.COM 
26810298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
26910298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
270789Sahrens 	} else {
2719396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2729396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
27310298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
27410298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
27510298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
27610298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
277789Sahrens 	}
278789Sahrens 
279789Sahrens 	/*
280789Sahrens 	 * Note: the changed_cb will be called once before the register
281789Sahrens 	 * func returns, thus changing the checksum/compression from the
2827237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2837237Sek110237 	 * checksum/compression/copies.
284789Sahrens 	 */
2857237Sek110237 	if (ds) {
2867237Sek110237 		err = dsl_prop_register(ds, "primarycache",
28710298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
2881544Seschrock 		if (err == 0)
2897237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
29010298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
2917237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2927237Sek110237 			if (err == 0)
2937237Sek110237 				err = dsl_prop_register(ds, "checksum",
29410298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
2957237Sek110237 			if (err == 0)
2967237Sek110237 				err = dsl_prop_register(ds, "compression",
29710298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
2987237Sek110237 			if (err == 0)
2997237Sek110237 				err = dsl_prop_register(ds, "copies",
30010298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
30110310SNeil.Perrin@Sun.COM 			if (err == 0)
30210922SJeff.Bonwick@Sun.COM 				err = dsl_prop_register(ds, "dedup",
30310922SJeff.Bonwick@Sun.COM 				    dedup_changed_cb, os);
30410922SJeff.Bonwick@Sun.COM 			if (err == 0)
30510310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
30610310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
3077237Sek110237 		}
3081544Seschrock 		if (err) {
30910298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
31010298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
31110298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
3121544Seschrock 			return (err);
3131544Seschrock 		}
3142082Seschrock 	} else if (ds == NULL) {
315789Sahrens 		/* It's the meta-objset. */
31610298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
31710298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
31810298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
31910922SJeff.Bonwick@Sun.COM 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
32010922SJeff.Bonwick@Sun.COM 		os->os_dedup_verify = 0;
32110922SJeff.Bonwick@Sun.COM 		os->os_logbias = 0;
32210298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
32310298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
324789Sahrens 	}
325789Sahrens 
32610298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
32710298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
328789Sahrens 
329789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
33010298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
331789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
33210298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
333789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
334789Sahrens 	}
33510298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
336789Sahrens 	    offsetof(dnode_t, dn_link));
33710298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
338789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
339789Sahrens 
34010298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
34110298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
34210298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3432856Snd150628 
34410298SMatthew.Ahrens@Sun.COM 	os->os_meta_dnode = dnode_special_open(os,
34510298SMatthew.Ahrens@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
34610298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
34710298SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode = dnode_special_open(os,
34810298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
34910298SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode = dnode_special_open(os,
35010298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3519396SMatthew.Ahrens@Sun.COM 	}
352789Sahrens 
3534787Sahrens 	/*
3544787Sahrens 	 * We should be the only thread trying to do this because we
3554787Sahrens 	 * have ds_opening_lock
3564787Sahrens 	 */
3574787Sahrens 	if (ds) {
35810298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
35910298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
36010298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
36110298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
362789Sahrens 	}
363789Sahrens 
36410298SMatthew.Ahrens@Sun.COM 	*osp = os;
3655367Sahrens 	return (0);
3665367Sahrens }
3675367Sahrens 
3685367Sahrens int
36910298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3705367Sahrens {
37110298SMatthew.Ahrens@Sun.COM 	int err = 0;
37210298SMatthew.Ahrens@Sun.COM 
37310298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
37410298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
37510298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
37610298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
37710298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
37810298SMatthew.Ahrens@Sun.COM 	}
37910298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
38010298SMatthew.Ahrens@Sun.COM 	return (err);
38110298SMatthew.Ahrens@Sun.COM }
38210298SMatthew.Ahrens@Sun.COM 
38310298SMatthew.Ahrens@Sun.COM /* called from zpl */
38410298SMatthew.Ahrens@Sun.COM int
38510298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
38610298SMatthew.Ahrens@Sun.COM {
38710298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
3885367Sahrens 	int err;
3895367Sahrens 
39010298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
3915367Sahrens 	if (err)
39210298SMatthew.Ahrens@Sun.COM 		return (err);
39310298SMatthew.Ahrens@Sun.COM 
39410298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
39510298SMatthew.Ahrens@Sun.COM 	if (err)
39610298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
39710298SMatthew.Ahrens@Sun.COM 
3985367Sahrens 	return (err);
3995367Sahrens }
4005367Sahrens 
401789Sahrens /* called from zpl */
402789Sahrens int
40310298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
40410298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
405789Sahrens {
406789Sahrens 	dsl_dataset_t *ds;
407789Sahrens 	int err;
408789Sahrens 
40910298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
41010298SMatthew.Ahrens@Sun.COM 	if (err)
41110298SMatthew.Ahrens@Sun.COM 		return (err);
4125367Sahrens 
41310298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
414789Sahrens 	if (err) {
41510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
41610588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
41710298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
41810298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
41910588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
42010588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
42110588SEric.Taylor@Sun.COM 		return (EROFS);
422789Sahrens 	}
4235367Sahrens 	return (err);
424789Sahrens }
425789Sahrens 
426789Sahrens void
42710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
428789Sahrens {
42910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
43010298SMatthew.Ahrens@Sun.COM }
4316689Smaybee 
43210298SMatthew.Ahrens@Sun.COM void
43310298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
43410298SMatthew.Ahrens@Sun.COM {
43510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
436789Sahrens }
437789Sahrens 
4381646Sperrin int
4394944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4401544Seschrock {
4411544Seschrock 	dnode_t *dn;
4421596Sahrens 
44310298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4441596Sahrens 
4451596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
44610298SMatthew.Ahrens@Sun.COM 	list_remove(&os->os_dnodes, os->os_meta_dnode);
44710298SMatthew.Ahrens@Sun.COM 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
4481544Seschrock 
4491544Seschrock 	/*
4501596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4511596Sahrens 	 * because dnode_add_ref() only works if you already have a
4521596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4531596Sahrens 	 * skip.
4541544Seschrock 	 */
45510298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
4564944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
45710298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
4581596Sahrens 		continue;
4591596Sahrens 
4601596Sahrens 	while (dn) {
4611596Sahrens 		dnode_t *next_dn = dn;
4621596Sahrens 
4631596Sahrens 		do {
46410298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
4654944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4661596Sahrens 
46710298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
4684944Smaybee 		dnode_evict_dbufs(dn);
4691596Sahrens 		dnode_rele(dn, FTAG);
47010298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
4711596Sahrens 		dn = next_dn;
4721544Seschrock 	}
47310298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
47410298SMatthew.Ahrens@Sun.COM 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
4751544Seschrock }
4761544Seschrock 
4771544Seschrock void
47810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
479789Sahrens {
48010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
481789Sahrens 
48210922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
48310922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
484789Sahrens 
4857237Sek110237 	if (ds) {
4867237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4877237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
48810298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
4897237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
49010298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
4917237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
49210298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
49310922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
49410922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
49510310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
49610310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
4977237Sek110237 		}
4987237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
49910298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
5007237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
50110298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
502789Sahrens 	}
503789Sahrens 
504*11935SMark.Shellenbaum@Sun.COM 	if (os->os_sa)
505*11935SMark.Shellenbaum@Sun.COM 		sa_tear_down(os);
506*11935SMark.Shellenbaum@Sun.COM 
5071544Seschrock 	/*
5081544Seschrock 	 * We should need only a single pass over the dnode list, since
5091544Seschrock 	 * nothing can be added to the list at this point.
5101544Seschrock 	 */
51110298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5121544Seschrock 
51310298SMatthew.Ahrens@Sun.COM 	dnode_special_close(os->os_meta_dnode);
51410298SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode) {
51510298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_userused_dnode);
51610298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_groupused_dnode);
5179396SMatthew.Ahrens@Sun.COM 	}
51810298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
519789Sahrens 
52010298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
521789Sahrens 
52210298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
52310298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
52410298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
52510298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
52610298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
527789Sahrens }
528789Sahrens 
52910373Schris.kirby@sun.com timestruc_t
53010373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
53110373Schris.kirby@sun.com {
53210373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
53310373Schris.kirby@sun.com }
53410373Schris.kirby@sun.com 
535789Sahrens /* called from dsl for meta-objset */
53610298SMatthew.Ahrens@Sun.COM objset_t *
5373547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5383547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
539789Sahrens {
54010298SMatthew.Ahrens@Sun.COM 	objset_t *os;
541789Sahrens 	dnode_t *mdn;
542789Sahrens 
543789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5444787Sahrens 	if (ds)
5454787Sahrens 		mutex_enter(&ds->ds_opening_lock);
54610298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
5474787Sahrens 	if (ds)
5484787Sahrens 		mutex_exit(&ds->ds_opening_lock);
54910298SMatthew.Ahrens@Sun.COM 	mdn = os->os_meta_dnode;
550789Sahrens 
551789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
552789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
553789Sahrens 
554789Sahrens 	/*
555789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
556789Sahrens 	 * later, because then we could do it in quescing context while
557789Sahrens 	 * we are also accessing it in open context.
558789Sahrens 	 *
559789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
560789Sahrens 	 * because the MOS is only updated in syncing context.
561789Sahrens 	 * This is most fortunate: the MOS is the only objset that
562789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
563789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
564789Sahrens 	 */
5651544Seschrock 	if (ds != NULL) {
5661544Seschrock 		int levels = 1;
5671544Seschrock 
5681544Seschrock 		/*
5691544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5701544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5711544Seschrock 		 */
5721544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5731544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5741544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5751544Seschrock 			levels++;
5761544Seschrock 
577789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5781544Seschrock 		    mdn->dn_nlevels = levels;
5791544Seschrock 	}
580789Sahrens 
581789Sahrens 	ASSERT(type != DMU_OST_NONE);
582789Sahrens 	ASSERT(type != DMU_OST_ANY);
583789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
58410298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
58510298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
58610298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
58710298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
5889396SMatthew.Ahrens@Sun.COM 	}
589789Sahrens 
590789Sahrens 	dsl_dataset_dirty(ds, tx);
591789Sahrens 
59210298SMatthew.Ahrens@Sun.COM 	return (os);
593789Sahrens }
594789Sahrens 
595789Sahrens struct oscarg {
5964543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
597789Sahrens 	void *userarg;
59810272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
599789Sahrens 	const char *lastname;
600789Sahrens 	dmu_objset_type_t type;
6016492Stimh 	uint64_t flags;
602789Sahrens };
603789Sahrens 
6044543Smarks /*ARGSUSED*/
605789Sahrens static int
6062199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
607789Sahrens {
6082199Sahrens 	dsl_dir_t *dd = arg1;
6092199Sahrens 	struct oscarg *oa = arg2;
6102199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6112199Sahrens 	int err;
6122199Sahrens 	uint64_t ddobj;
6132199Sahrens 
6142199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6152199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6162199Sahrens 	if (err != ENOENT)
6172199Sahrens 		return (err ? err : EEXIST);
6182199Sahrens 
61910272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
62010272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
62110272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6222199Sahrens 			return (EXDEV);
6232199Sahrens 
62410272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
62510272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6262199Sahrens 			return (EINVAL);
6272199Sahrens 	}
6284543Smarks 
6292199Sahrens 	return (0);
6302199Sahrens }
6312199Sahrens 
6322199Sahrens static void
6334543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6342199Sahrens {
6352199Sahrens 	dsl_dir_t *dd = arg1;
6362199Sahrens 	struct oscarg *oa = arg2;
6372199Sahrens 	uint64_t dsobj;
638789Sahrens 
639789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
640789Sahrens 
6412199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
64210272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
643789Sahrens 
64410272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
64510272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
64610272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
64710298SMatthew.Ahrens@Sun.COM 		objset_t *os;
648789Sahrens 
64910272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
65010272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
65110272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
65210272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
65310272SMatthew.Ahrens@Sun.COM 
65410298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6553547Smaybee 		    ds, bp, oa->type, tx);
656789Sahrens 
657789Sahrens 		if (oa->userfunc)
65810298SMatthew.Ahrens@Sun.COM 			oa->userfunc(os, oa->userarg, cr, tx);
65910272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
660789Sahrens 	}
6614543Smarks 
6624543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6634543Smarks 	    tx, cr, "dataset = %llu", dsobj);
664789Sahrens }
665789Sahrens 
666789Sahrens int
66710272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6684543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
669789Sahrens {
6702199Sahrens 	dsl_dir_t *pdd;
671789Sahrens 	const char *tail;
672789Sahrens 	int err = 0;
6732199Sahrens 	struct oscarg oa = { 0 };
674789Sahrens 
6752199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6762199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6771544Seschrock 	if (err)
6781544Seschrock 		return (err);
679789Sahrens 	if (tail == NULL) {
6802199Sahrens 		dsl_dir_close(pdd, FTAG);
681789Sahrens 		return (EEXIST);
682789Sahrens 	}
683789Sahrens 
6842199Sahrens 	oa.userfunc = func;
6852199Sahrens 	oa.userarg = arg;
6862199Sahrens 	oa.lastname = tail;
6872199Sahrens 	oa.type = type;
6886492Stimh 	oa.flags = flags;
6894543Smarks 
69010272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
69110272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
69210272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
69310272SMatthew.Ahrens@Sun.COM 	return (err);
69410272SMatthew.Ahrens@Sun.COM }
69510272SMatthew.Ahrens@Sun.COM 
69610272SMatthew.Ahrens@Sun.COM int
69710272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
69810272SMatthew.Ahrens@Sun.COM {
69910272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
70010272SMatthew.Ahrens@Sun.COM 	const char *tail;
70110272SMatthew.Ahrens@Sun.COM 	int err = 0;
70210272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
70310272SMatthew.Ahrens@Sun.COM 
70410272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
70510272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
70610272SMatthew.Ahrens@Sun.COM 	if (err)
70710272SMatthew.Ahrens@Sun.COM 		return (err);
70810272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
70910272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
71010272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
711789Sahrens 	}
71210272SMatthew.Ahrens@Sun.COM 
71310272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
71410272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
71510272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
71610272SMatthew.Ahrens@Sun.COM 
7172199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7182199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7192199Sahrens 	dsl_dir_close(pdd, FTAG);
720789Sahrens 	return (err);
721789Sahrens }
722789Sahrens 
723789Sahrens int
72410242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
725789Sahrens {
72610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
727789Sahrens 	int error;
728789Sahrens 
729789Sahrens 	/*
73010272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
73110272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
73210272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
73310272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
73410272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
735789Sahrens 	 */
73610298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
737789Sahrens 	if (error == 0) {
73810298SMatthew.Ahrens@Sun.COM 		objset_t *os;
73910298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
74010298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
74110298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
74210272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
743789Sahrens 	}
744789Sahrens 
7455367Sahrens 	return (error);
746789Sahrens }
747789Sahrens 
7482199Sahrens struct snaparg {
7492199Sahrens 	dsl_sync_task_group_t *dstg;
7502199Sahrens 	char *snapname;
7512199Sahrens 	char failed[MAXPATHLEN];
75211620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
7539355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7545367Sahrens };
7555367Sahrens 
7569355SMatthew.Ahrens@Sun.COM static int
7579355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7589355SMatthew.Ahrens@Sun.COM {
7599355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7609355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7619355SMatthew.Ahrens@Sun.COM 
7629355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7639355SMatthew.Ahrens@Sun.COM 
76410298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
7659355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7669355SMatthew.Ahrens@Sun.COM }
7679355SMatthew.Ahrens@Sun.COM 
7689355SMatthew.Ahrens@Sun.COM static void
7699355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7709355SMatthew.Ahrens@Sun.COM {
7719355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
77210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
7739355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7749355SMatthew.Ahrens@Sun.COM 
7759355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7769355SMatthew.Ahrens@Sun.COM 
77711022STom.Erickson@Sun.COM 	if (sn->props) {
77811022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
77911022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
78011022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
78111022STom.Erickson@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, cr, tx);
78211022STom.Erickson@Sun.COM 	}
7839355SMatthew.Ahrens@Sun.COM }
7842199Sahrens 
7852199Sahrens static int
78611209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
7872199Sahrens {
7882199Sahrens 	struct snaparg *sn = arg;
7892199Sahrens 	objset_t *os;
7902199Sahrens 	int err;
79111620SMatthew.Ahrens@Sun.COM 	char *cp;
79211620SMatthew.Ahrens@Sun.COM 
79311620SMatthew.Ahrens@Sun.COM 	/*
79411620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
79511620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
79611620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
79711620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
79811620SMatthew.Ahrens@Sun.COM 	 */
79911620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
80011620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
80111620SMatthew.Ahrens@Sun.COM 		return (0);
8022199Sahrens 
8032199Sahrens 	(void) strcpy(sn->failed, name);
8042199Sahrens 
8054543Smarks 	/*
80611620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
80711620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
80811620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
8094543Smarks 	 */
81011620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
8114543Smarks 		return (err);
8124543Smarks 
81310298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
8142199Sahrens 	if (err != 0)
8152199Sahrens 		return (err);
8162199Sahrens 
81711620SMatthew.Ahrens@Sun.COM 	/*
81811620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
81911620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
82011620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
82111620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
82211620SMatthew.Ahrens@Sun.COM 	 */
82310298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
82410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
82511620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
8263637Srm160521 	}
8273637Srm160521 
8283637Srm160521 	/*
8292199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8302199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8312199Sahrens 	 * a side effect.
8322199Sahrens 	 */
8332199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8342199Sahrens 	if (err == 0) {
8359355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
8369355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
8373637Srm160521 	} else {
83810298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
8392199Sahrens 	}
8403637Srm160521 
8412199Sahrens 	return (err);
8422199Sahrens }
8432199Sahrens 
8442199Sahrens int
8459355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
8469355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
8472199Sahrens {
8482199Sahrens 	dsl_sync_task_t *dst;
8499355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8502199Sahrens 	spa_t *spa;
8512199Sahrens 	int err;
8522199Sahrens 
8532199Sahrens 	(void) strcpy(sn.failed, fsname);
8542199Sahrens 
8554603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8562199Sahrens 	if (err)
8572199Sahrens 		return (err);
8582199Sahrens 
8592199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8602199Sahrens 	sn.snapname = snapname;
8619355SMatthew.Ahrens@Sun.COM 	sn.props = props;
86211620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
8632199Sahrens 
8642417Sahrens 	if (recursive) {
8652417Sahrens 		err = dmu_objset_find(fsname,
8662417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8672417Sahrens 	} else {
8682199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8692417Sahrens 	}
8702199Sahrens 
8719355SMatthew.Ahrens@Sun.COM 	if (err == 0)
8729355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
8732199Sahrens 
8742199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8752199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8769355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
87710298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
8782199Sahrens 		if (dst->dst_err)
8795367Sahrens 			dsl_dataset_name(ds, sn.failed);
8809355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
88110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
8822199Sahrens 	}
8835367Sahrens 
8842199Sahrens 	if (err)
8852199Sahrens 		(void) strcpy(fsname, sn.failed);
8862199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8872199Sahrens 	spa_close(spa, FTAG);
8882199Sahrens 	return (err);
8892199Sahrens }
8902199Sahrens 
891789Sahrens static void
8929396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
893789Sahrens {
8943547Smaybee 	dnode_t *dn;
895789Sahrens 
8963547Smaybee 	while (dn = list_head(list)) {
8973547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8983547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8993547Smaybee 		/*
9009396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
9019396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
9023547Smaybee 		 */
9033547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
9043547Smaybee 		ASSERT(dn->dn_zio);
905789Sahrens 
9063547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
9073547Smaybee 		list_remove(list, dn);
9089396SMatthew.Ahrens@Sun.COM 
9099396SMatthew.Ahrens@Sun.COM 		if (newlist) {
9109396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
9119396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
9129396SMatthew.Ahrens@Sun.COM 		}
9139396SMatthew.Ahrens@Sun.COM 
9143547Smaybee 		dnode_sync(dn, tx);
9153547Smaybee 	}
9163547Smaybee }
9172981Sahrens 
9183547Smaybee /* ARGSUSED */
9193547Smaybee static void
92010922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
9213547Smaybee {
9227754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
92310298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
9243547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
9252981Sahrens 
9267754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
9277754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
9287754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9295329Sgw25295 
9303547Smaybee 	/*
9319396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
9329396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
9339396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
9349396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
9353547Smaybee 	 */
9369396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
9377754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
9383547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
93910922SJeff.Bonwick@Sun.COM }
94010922SJeff.Bonwick@Sun.COM 
94110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
94210922SJeff.Bonwick@Sun.COM static void
94310922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
94410922SJeff.Bonwick@Sun.COM {
94510922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
94610922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
94710922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
9485329Sgw25295 
9497754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
95010922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
9517754SJeff.Bonwick@Sun.COM 	} else {
95210922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
95310922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
95410922SJeff.Bonwick@Sun.COM 
95510922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
95610922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
9575329Sgw25295 	}
958789Sahrens }
959789Sahrens 
960789Sahrens /* called from dsl */
961789Sahrens void
96210298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
963789Sahrens {
964789Sahrens 	int txgoff;
9651544Seschrock 	zbookmark_t zb;
96610922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
9673547Smaybee 	zio_t *zio;
9683547Smaybee 	list_t *list;
9699396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9703547Smaybee 	dbuf_dirty_record_t *dr;
9713547Smaybee 
9723547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
973789Sahrens 
974789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
975789Sahrens 	/* XXX the write_done callback should really give us the tx... */
976789Sahrens 	os->os_synctx = tx;
977789Sahrens 
9783882Sahrens 	if (os->os_dsl_dataset == NULL) {
9793882Sahrens 		/*
9803882Sahrens 		 * This is the MOS.  If we have upgraded,
9813882Sahrens 		 * spa_max_replication() could change, so reset
9823882Sahrens 		 * os_copies here.
9833882Sahrens 		 */
9843882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9853882Sahrens 	}
9863882Sahrens 
9873547Smaybee 	/*
9883547Smaybee 	 * Create the root block IO
9893547Smaybee 	 */
9907046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9919396SMatthew.Ahrens@Sun.COM 
99210922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
99310922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
99410922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
99510922SJeff.Bonwick@Sun.COM 
99610922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
99710922SJeff.Bonwick@Sun.COM 
99810922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
99910922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
100010922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
10017754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
10023547Smaybee 
10033547Smaybee 	/*
10049396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
10053547Smaybee 	 */
10063547Smaybee 	os->os_meta_dnode->dn_zio = zio;
10073547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
1008789Sahrens 
10099396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
10109396SMatthew.Ahrens@Sun.COM 
10119396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
10129396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
10139396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
10149396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
10159396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
10169396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
10179396SMatthew.Ahrens@Sun.COM 	}
10189396SMatthew.Ahrens@Sun.COM 
1019789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1020789Sahrens 
10219396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
10229396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
10239396SMatthew.Ahrens@Sun.COM 		/*
10249396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
10259396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
10269396SMatthew.Ahrens@Sun.COM 		 */
10279396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
10289396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
10299396SMatthew.Ahrens@Sun.COM 	}
10309396SMatthew.Ahrens@Sun.COM 
10319396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
10329396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1033789Sahrens 
10343547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
10353547Smaybee 	while (dr = list_head(list)) {
10363547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
10373547Smaybee 		list_remove(list, dr);
10383547Smaybee 		if (dr->dr_zio)
10393547Smaybee 			zio_nowait(dr->dr_zio);
10403547Smaybee 	}
1041789Sahrens 	/*
1042789Sahrens 	 * Free intent log blocks up to this tx.
1043789Sahrens 	 */
1044789Sahrens 	zil_sync(os->os_zil, tx);
10457046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
10463547Smaybee 	zio_nowait(zio);
1047789Sahrens }
1048789Sahrens 
104910922SJeff.Bonwick@Sun.COM boolean_t
105010922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
105110922SJeff.Bonwick@Sun.COM {
105210922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
105310922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
105410922SJeff.Bonwick@Sun.COM }
105510922SJeff.Bonwick@Sun.COM 
10569396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
10579396SMatthew.Ahrens@Sun.COM 
10589396SMatthew.Ahrens@Sun.COM void
10599396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
10609396SMatthew.Ahrens@Sun.COM {
10619396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
10629396SMatthew.Ahrens@Sun.COM }
10639396SMatthew.Ahrens@Sun.COM 
10649396SMatthew.Ahrens@Sun.COM boolean_t
106510298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
10669396SMatthew.Ahrens@Sun.COM {
10679396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
10689396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
10699396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
10709396SMatthew.Ahrens@Sun.COM }
10719396SMatthew.Ahrens@Sun.COM 
107210407SMatthew.Ahrens@Sun.COM static void
1073*11935SMark.Shellenbaum@Sun.COM do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1074*11935SMark.Shellenbaum@Sun.COM     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
107510407SMatthew.Ahrens@Sun.COM {
1076*11935SMark.Shellenbaum@Sun.COM 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1077*11935SMark.Shellenbaum@Sun.COM 		int64_t delta = DNODE_SIZE + used;
107810407SMatthew.Ahrens@Sun.COM 		if (subtract)
107910407SMatthew.Ahrens@Sun.COM 			delta = -delta;
108010801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
108110407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
108210801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
108310407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
108410407SMatthew.Ahrens@Sun.COM 	}
108510407SMatthew.Ahrens@Sun.COM }
108610407SMatthew.Ahrens@Sun.COM 
10879396SMatthew.Ahrens@Sun.COM void
1088*11935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
10899396SMatthew.Ahrens@Sun.COM {
10909396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
10919396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
10929396SMatthew.Ahrens@Sun.COM 
10939396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
10949396SMatthew.Ahrens@Sun.COM 
10959396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
10969396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
10979396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
10989396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
10999396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
11009396SMatthew.Ahrens@Sun.COM 
11019396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
11029396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
110310298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11049396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
11059396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
110610298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11079396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
11089396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
11099396SMatthew.Ahrens@Sun.COM 		}
11109396SMatthew.Ahrens@Sun.COM 
11119396SMatthew.Ahrens@Sun.COM 		/*
111210407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
1113*11935SMark.Shellenbaum@Sun.COM 		 * net delta is zero.  Otherwise
111410407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
111510407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
111610407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
11179396SMatthew.Ahrens@Sun.COM 		 */
11189396SMatthew.Ahrens@Sun.COM 
11199396SMatthew.Ahrens@Sun.COM 		/*
11209396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
11219396SMatthew.Ahrens@Sun.COM 		 */
11229396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
1123*11935SMark.Shellenbaum@Sun.COM 		ASSERT(dn->dn_id_flags);
1124*11935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_OLD_EXIST)  {
1125*11935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1126*11935SMark.Shellenbaum@Sun.COM 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1127*11935SMark.Shellenbaum@Sun.COM 		}
1128*11935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1129*11935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1130*11935SMark.Shellenbaum@Sun.COM 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1131*11935SMark.Shellenbaum@Sun.COM 			    dn->dn_newgid, B_FALSE, tx);
1132*11935SMark.Shellenbaum@Sun.COM 		}
1133*11935SMark.Shellenbaum@Sun.COM 
1134*11935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = 0;
1135*11935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = 0;
1136*11935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1137*11935SMark.Shellenbaum@Sun.COM 			dn->dn_olduid = dn->dn_newuid;
1138*11935SMark.Shellenbaum@Sun.COM 			dn->dn_oldgid = dn->dn_newgid;
1139*11935SMark.Shellenbaum@Sun.COM 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1140*11935SMark.Shellenbaum@Sun.COM 			if (dn->dn_bonuslen == 0)
1141*11935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1142*11935SMark.Shellenbaum@Sun.COM 			else
1143*11935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1144*11935SMark.Shellenbaum@Sun.COM 		}
1145*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST|DN_ID_SYNC);
11469396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
11479396SMatthew.Ahrens@Sun.COM 
11489396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
11499396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
11509396SMatthew.Ahrens@Sun.COM 	}
11519396SMatthew.Ahrens@Sun.COM }
11529396SMatthew.Ahrens@Sun.COM 
1153*11935SMark.Shellenbaum@Sun.COM void
1154*11935SMark.Shellenbaum@Sun.COM dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before)
1155*11935SMark.Shellenbaum@Sun.COM {
1156*11935SMark.Shellenbaum@Sun.COM 	objset_t *os = dn->dn_objset;
1157*11935SMark.Shellenbaum@Sun.COM 	void *data = NULL;
1158*11935SMark.Shellenbaum@Sun.COM 	dmu_buf_t *spilldb = NULL;
1159*11935SMark.Shellenbaum@Sun.COM 	uint64_t *user, *group;
1160*11935SMark.Shellenbaum@Sun.COM 	int flags = dn->dn_id_flags;
1161*11935SMark.Shellenbaum@Sun.COM 	int error;
1162*11935SMark.Shellenbaum@Sun.COM 
1163*11935SMark.Shellenbaum@Sun.COM 	if (!dmu_objset_userused_enabled(dn->dn_objset))
1164*11935SMark.Shellenbaum@Sun.COM 		return;
1165*11935SMark.Shellenbaum@Sun.COM 
1166*11935SMark.Shellenbaum@Sun.COM 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1167*11935SMark.Shellenbaum@Sun.COM 	    DN_ID_CHKED_SPILL)))
1168*11935SMark.Shellenbaum@Sun.COM 		return;
1169*11935SMark.Shellenbaum@Sun.COM 
1170*11935SMark.Shellenbaum@Sun.COM 	if (before && dn->dn_bonuslen != 0)
1171*11935SMark.Shellenbaum@Sun.COM 		data = DN_BONUS(dn->dn_phys);
1172*11935SMark.Shellenbaum@Sun.COM 	else if (!before && dn->dn_bonuslen != 0)
1173*11935SMark.Shellenbaum@Sun.COM 		data = dn->dn_bonus != NULL ?
1174*11935SMark.Shellenbaum@Sun.COM 		    dn->dn_bonus->db.db_data : DN_BONUS(dn->dn_phys);
1175*11935SMark.Shellenbaum@Sun.COM 	else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1176*11935SMark.Shellenbaum@Sun.COM 			int rf = 0;
1177*11935SMark.Shellenbaum@Sun.COM 
1178*11935SMark.Shellenbaum@Sun.COM 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1179*11935SMark.Shellenbaum@Sun.COM 				rf |= DB_RF_HAVESTRUCT;
1180*11935SMark.Shellenbaum@Sun.COM 			error = dmu_spill_hold_by_dnode(dn, rf, FTAG, &spilldb);
1181*11935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
1182*11935SMark.Shellenbaum@Sun.COM 			data = spilldb->db_data;
1183*11935SMark.Shellenbaum@Sun.COM 	} else {
1184*11935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
1185*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1186*11935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
1187*11935SMark.Shellenbaum@Sun.COM 		return;
1188*11935SMark.Shellenbaum@Sun.COM 	}
1189*11935SMark.Shellenbaum@Sun.COM 
1190*11935SMark.Shellenbaum@Sun.COM 	if (before) {
1191*11935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_olduid;
1192*11935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_oldgid;
1193*11935SMark.Shellenbaum@Sun.COM 	} else {
1194*11935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_newuid;
1195*11935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_newgid;
1196*11935SMark.Shellenbaum@Sun.COM 	}
1197*11935SMark.Shellenbaum@Sun.COM 
1198*11935SMark.Shellenbaum@Sun.COM 	ASSERT(data);
1199*11935SMark.Shellenbaum@Sun.COM 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1200*11935SMark.Shellenbaum@Sun.COM 	    user, group);
1201*11935SMark.Shellenbaum@Sun.COM 
1202*11935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
1203*11935SMark.Shellenbaum@Sun.COM 	if (error == 0 && before)
1204*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1205*11935SMark.Shellenbaum@Sun.COM 	if (error == 0 && !before)
1206*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1207*11935SMark.Shellenbaum@Sun.COM 
1208*11935SMark.Shellenbaum@Sun.COM 	if (spilldb) {
1209*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1210*11935SMark.Shellenbaum@Sun.COM 	} else {
1211*11935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1212*11935SMark.Shellenbaum@Sun.COM 	}
1213*11935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
1214*11935SMark.Shellenbaum@Sun.COM 	if (spilldb)
1215*11935SMark.Shellenbaum@Sun.COM 		dmu_buf_rele(spilldb, FTAG);
1216*11935SMark.Shellenbaum@Sun.COM }
1217*11935SMark.Shellenbaum@Sun.COM 
12189396SMatthew.Ahrens@Sun.COM boolean_t
12199396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
12209396SMatthew.Ahrens@Sun.COM {
122110298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
12229396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
12239396SMatthew.Ahrens@Sun.COM }
12249396SMatthew.Ahrens@Sun.COM 
12259396SMatthew.Ahrens@Sun.COM int
12269396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
12279396SMatthew.Ahrens@Sun.COM {
12289396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
12299396SMatthew.Ahrens@Sun.COM 	int err = 0;
12309396SMatthew.Ahrens@Sun.COM 
12319396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
12329396SMatthew.Ahrens@Sun.COM 		return (0);
123310298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
12349396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
12359396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
12369396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
12379396SMatthew.Ahrens@Sun.COM 
12389396SMatthew.Ahrens@Sun.COM 	/*
12399396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
12409396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
12419396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
12429396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
12439396SMatthew.Ahrens@Sun.COM 	 * independently.
12449396SMatthew.Ahrens@Sun.COM 	 */
12459396SMatthew.Ahrens@Sun.COM 
12469396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
12479951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
12489396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
12499396SMatthew.Ahrens@Sun.COM 		int objerr;
12509396SMatthew.Ahrens@Sun.COM 
12519396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
12529396SMatthew.Ahrens@Sun.COM 			return (EINTR);
12539396SMatthew.Ahrens@Sun.COM 
12549396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
12559396SMatthew.Ahrens@Sun.COM 		if (objerr)
12569396SMatthew.Ahrens@Sun.COM 			continue;
12579951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
12589396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
12599396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
12609396SMatthew.Ahrens@Sun.COM 		if (objerr) {
12619396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
12629396SMatthew.Ahrens@Sun.COM 			continue;
12639396SMatthew.Ahrens@Sun.COM 		}
12649396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
12659396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
12669396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
12679396SMatthew.Ahrens@Sun.COM 	}
12689396SMatthew.Ahrens@Sun.COM 
126910298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
12709396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
12719396SMatthew.Ahrens@Sun.COM 	return (0);
12729396SMatthew.Ahrens@Sun.COM }
12739396SMatthew.Ahrens@Sun.COM 
1274789Sahrens void
12752885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
12762885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
12772885Sahrens {
127810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
12792885Sahrens 	    usedobjsp, availobjsp);
12802885Sahrens }
12812885Sahrens 
12822885Sahrens uint64_t
12832885Sahrens dmu_objset_fsid_guid(objset_t *os)
12842885Sahrens {
128510298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
12862885Sahrens }
12872885Sahrens 
12882885Sahrens void
12892885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1290789Sahrens {
129110298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
129210298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
129310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
12942885Sahrens }
12952885Sahrens 
12962885Sahrens void
12972885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
12982885Sahrens {
129910298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
130010298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
13012885Sahrens 
130210298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
130310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
13042885Sahrens 
13052885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
130610298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
13079396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
13089396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1309789Sahrens }
1310789Sahrens 
1311789Sahrens int
1312789Sahrens dmu_objset_is_snapshot(objset_t *os)
1313789Sahrens {
131410298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
131510298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1316789Sahrens 	else
1317789Sahrens 		return (B_FALSE);
1318789Sahrens }
1319789Sahrens 
1320789Sahrens int
13216492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
13226492Stimh     boolean_t *conflict)
13236492Stimh {
132410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
13256492Stimh 	uint64_t ignored;
13266492Stimh 
13276492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
13286492Stimh 		return (ENOENT);
13296492Stimh 
13306492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
13316492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
13326492Stimh 	    real, maxlen, conflict));
13336492Stimh }
13346492Stimh 
13356492Stimh int
1336789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
13375663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1338789Sahrens {
133910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1340789Sahrens 	zap_cursor_t cursor;
1341789Sahrens 	zap_attribute_t attr;
1342789Sahrens 
1343789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1344789Sahrens 		return (ENOENT);
1345789Sahrens 
1346789Sahrens 	zap_cursor_init_serialized(&cursor,
1347789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1348789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1349789Sahrens 
1350885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1351885Sahrens 		zap_cursor_fini(&cursor);
1352885Sahrens 		return (ENOENT);
1353885Sahrens 	}
1354885Sahrens 
1355885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1356885Sahrens 		zap_cursor_fini(&cursor);
1357885Sahrens 		return (ENAMETOOLONG);
1358885Sahrens 	}
1359885Sahrens 
1360885Sahrens 	(void) strcpy(name, attr.za_name);
1361885Sahrens 	if (idp)
1362885Sahrens 		*idp = attr.za_first_integer;
13635663Sck153898 	if (case_conflict)
13645663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1365885Sahrens 	zap_cursor_advance(&cursor);
1366885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1367885Sahrens 	zap_cursor_fini(&cursor);
1368885Sahrens 
1369885Sahrens 	return (0);
1370885Sahrens }
1371885Sahrens 
1372885Sahrens int
1373885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1374885Sahrens     uint64_t *idp, uint64_t *offp)
1375885Sahrens {
137610298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1377885Sahrens 	zap_cursor_t cursor;
1378885Sahrens 	zap_attribute_t attr;
1379885Sahrens 
1380885Sahrens 	/* there is no next dir on a snapshot! */
138110298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1382885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1383885Sahrens 		return (ENOENT);
1384885Sahrens 
1385885Sahrens 	zap_cursor_init_serialized(&cursor,
1386885Sahrens 	    dd->dd_pool->dp_meta_objset,
1387885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1388885Sahrens 
1389885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1390885Sahrens 		zap_cursor_fini(&cursor);
1391885Sahrens 		return (ENOENT);
1392885Sahrens 	}
1393885Sahrens 
1394885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1395885Sahrens 		zap_cursor_fini(&cursor);
1396789Sahrens 		return (ENAMETOOLONG);
1397885Sahrens 	}
1398789Sahrens 
1399789Sahrens 	(void) strcpy(name, attr.za_name);
1400885Sahrens 	if (idp)
1401885Sahrens 		*idp = attr.za_first_integer;
1402789Sahrens 	zap_cursor_advance(&cursor);
1403789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1404885Sahrens 	zap_cursor_fini(&cursor);
1405789Sahrens 
1406789Sahrens 	return (0);
1407789Sahrens }
1408789Sahrens 
14097046Sahrens struct findarg {
141011209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
14117046Sahrens 	void *arg;
14127046Sahrens };
14137046Sahrens 
14147046Sahrens /* ARGSUSED */
14157046Sahrens static int
14167046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
14177046Sahrens {
14187046Sahrens 	struct findarg *fa = arg;
141911209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
14207046Sahrens }
14217046Sahrens 
1422789Sahrens /*
1423789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
14247046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1425789Sahrens  */
14262199Sahrens int
142711209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
142811209SMatthew.Ahrens@Sun.COM     int flags)
1429789Sahrens {
14307046Sahrens 	struct findarg fa;
14317046Sahrens 	fa.func = func;
14327046Sahrens 	fa.arg = arg;
14337046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
14347046Sahrens }
14357046Sahrens 
14367046Sahrens /*
14377046Sahrens  * Find all objsets under name, call func on each
14387046Sahrens  */
14397046Sahrens int
14407046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
14417046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
14427046Sahrens {
1443789Sahrens 	dsl_dir_t *dd;
14447046Sahrens 	dsl_pool_t *dp;
14457046Sahrens 	dsl_dataset_t *ds;
1446789Sahrens 	zap_cursor_t zc;
14473978Smmusante 	zap_attribute_t *attr;
1448789Sahrens 	char *child;
14497046Sahrens 	uint64_t thisobj;
14507046Sahrens 	int err;
1451789Sahrens 
14527046Sahrens 	if (name == NULL)
14537046Sahrens 		name = spa_name(spa);
14547046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
14551544Seschrock 	if (err)
14562199Sahrens 		return (err);
1457789Sahrens 
14587046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
14597046Sahrens 	if (dd->dd_myname[0] == '$') {
14607046Sahrens 		dsl_dir_close(dd, FTAG);
14617046Sahrens 		return (0);
14627046Sahrens 	}
14637046Sahrens 
14647046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
14653978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
14667046Sahrens 	dp = dd->dd_pool;
1467789Sahrens 
1468789Sahrens 	/*
1469789Sahrens 	 * Iterate over all children.
1470789Sahrens 	 */
14712417Sahrens 	if (flags & DS_FIND_CHILDREN) {
14727046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
14732417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
14743978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
14752417Sahrens 		    (void) zap_cursor_advance(&zc)) {
14763978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
14773978Smmusante 			ASSERT(attr->za_num_integers == 1);
1478789Sahrens 
147911165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
14807046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
148111165SMatthew.Ahrens@Sun.COM 			strfree(child);
14822417Sahrens 			if (err)
14832417Sahrens 				break;
14842417Sahrens 		}
14852417Sahrens 		zap_cursor_fini(&zc);
14862199Sahrens 
14872417Sahrens 		if (err) {
14882417Sahrens 			dsl_dir_close(dd, FTAG);
14893978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
14902417Sahrens 			return (err);
14912417Sahrens 		}
1492789Sahrens 	}
1493789Sahrens 
1494789Sahrens 	/*
1495789Sahrens 	 * Iterate over all snapshots.
1496789Sahrens 	 */
14977046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
14987046Sahrens 		if (!dsl_pool_sync_context(dp))
14997046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
15007046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
15017046Sahrens 		if (!dsl_pool_sync_context(dp))
15027046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1503789Sahrens 
15047046Sahrens 		if (err == 0) {
15057046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
15067046Sahrens 			dsl_dataset_rele(ds, FTAG);
1507789Sahrens 
15087046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
15097046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
15107046Sahrens 			    (void) zap_cursor_advance(&zc)) {
15117046Sahrens 				ASSERT(attr->za_integer_length ==
15127046Sahrens 				    sizeof (uint64_t));
15137046Sahrens 				ASSERT(attr->za_num_integers == 1);
1514789Sahrens 
151511165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
151611165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
15177046Sahrens 				err = func(spa, attr->za_first_integer,
15187046Sahrens 				    child, arg);
151911165SMatthew.Ahrens@Sun.COM 				strfree(child);
15207046Sahrens 				if (err)
15217046Sahrens 					break;
15227046Sahrens 			}
15237046Sahrens 			zap_cursor_fini(&zc);
1524789Sahrens 		}
1525789Sahrens 	}
1526789Sahrens 
1527789Sahrens 	dsl_dir_close(dd, FTAG);
15283978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1529789Sahrens 
15302199Sahrens 	if (err)
15312199Sahrens 		return (err);
15322199Sahrens 
1533789Sahrens 	/*
1534789Sahrens 	 * Apply to self if appropriate.
1535789Sahrens 	 */
15367046Sahrens 	err = func(spa, thisobj, name, arg);
15372199Sahrens 	return (err);
1538789Sahrens }
15395326Sek110237 
15408415SRichard.Morris@Sun.COM /* ARGSUSED */
15418415SRichard.Morris@Sun.COM int
154211209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
15438415SRichard.Morris@Sun.COM {
15448415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
15458415SRichard.Morris@Sun.COM 
15468415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
15478415SRichard.Morris@Sun.COM 		return (0);
15488415SRichard.Morris@Sun.COM 
15498415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
15508415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
155110298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
15528415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
15538415SRichard.Morris@Sun.COM 			zbookmark_t zb;
15548415SRichard.Morris@Sun.COM 
155510922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
155610922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
15578415SRichard.Morris@Sun.COM 
15588415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
15598415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
15608415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
15618415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
15628415SRichard.Morris@Sun.COM 			    &aflags, &zb);
15638415SRichard.Morris@Sun.COM 		}
15648415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
15658415SRichard.Morris@Sun.COM 	}
15668415SRichard.Morris@Sun.COM 
15678415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
15688415SRichard.Morris@Sun.COM 	return (0);
15698415SRichard.Morris@Sun.COM }
15708415SRichard.Morris@Sun.COM 
15715326Sek110237 void
15725326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
15735326Sek110237 {
157410298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
157510298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
15765326Sek110237 }
15775326Sek110237 
15785326Sek110237 void *
15795326Sek110237 dmu_objset_get_user(objset_t *os)
15805326Sek110237 {
158110298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
158210298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
15835326Sek110237 }
1584