xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 10588:dc03f981ea18)
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 /*
229355SMatthew.Ahrens@Sun.COM  * Copyright 2009 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/zio_checksum.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
42789Sahrens #include <sys/dmu_impl.h>
434543Smarks #include <sys/zfs_ioctl.h>
44789Sahrens 
45789Sahrens spa_t *
46789Sahrens dmu_objset_spa(objset_t *os)
47789Sahrens {
4810298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
49789Sahrens }
50789Sahrens 
51789Sahrens zilog_t *
52789Sahrens dmu_objset_zil(objset_t *os)
53789Sahrens {
5410298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
55789Sahrens }
56789Sahrens 
57789Sahrens dsl_pool_t *
58789Sahrens dmu_objset_pool(objset_t *os)
59789Sahrens {
60789Sahrens 	dsl_dataset_t *ds;
61789Sahrens 
6210298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
63789Sahrens 		return (ds->ds_dir->dd_pool);
64789Sahrens 	else
6510298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
66789Sahrens }
67789Sahrens 
68789Sahrens dsl_dataset_t *
69789Sahrens dmu_objset_ds(objset_t *os)
70789Sahrens {
7110298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
72789Sahrens }
73789Sahrens 
74789Sahrens dmu_objset_type_t
75789Sahrens dmu_objset_type(objset_t *os)
76789Sahrens {
7710298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
78789Sahrens }
79789Sahrens 
80789Sahrens void
81789Sahrens dmu_objset_name(objset_t *os, char *buf)
82789Sahrens {
8310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
84789Sahrens }
85789Sahrens 
86789Sahrens uint64_t
87789Sahrens dmu_objset_id(objset_t *os)
88789Sahrens {
8910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
90789Sahrens 
91789Sahrens 	return (ds ? ds->ds_object : 0);
92789Sahrens }
93789Sahrens 
9410310SNeil.Perrin@Sun.COM uint64_t
9510310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
9610310SNeil.Perrin@Sun.COM {
9710310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
9810310SNeil.Perrin@Sun.COM }
9910310SNeil.Perrin@Sun.COM 
100789Sahrens static void
101789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
102789Sahrens {
10310298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
104789Sahrens 
105789Sahrens 	/*
106789Sahrens 	 * Inheritance should have been done by now.
107789Sahrens 	 */
108789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
109789Sahrens 
11010298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
111789Sahrens }
112789Sahrens 
113789Sahrens static void
114789Sahrens compression_changed_cb(void *arg, uint64_t newval)
115789Sahrens {
11610298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
117789Sahrens 
118789Sahrens 	/*
119789Sahrens 	 * Inheritance and range checking should have been done by now.
120789Sahrens 	 */
121789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
122789Sahrens 
12310298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
124789Sahrens }
125789Sahrens 
1263835Sahrens static void
1273835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1283835Sahrens {
12910298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1303835Sahrens 
1313835Sahrens 	/*
1323835Sahrens 	 * Inheritance and range checking should have been done by now.
1333835Sahrens 	 */
1343835Sahrens 	ASSERT(newval > 0);
13510298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1363835Sahrens 
13710298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1383835Sahrens }
1393835Sahrens 
1407237Sek110237 static void
1417237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1427237Sek110237 {
14310298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1447237Sek110237 
1457237Sek110237 	/*
1467237Sek110237 	 * Inheritance and range checking should have been done by now.
1477237Sek110237 	 */
1487237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1497237Sek110237 	    newval == ZFS_CACHE_METADATA);
1507237Sek110237 
15110298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1527237Sek110237 }
1537237Sek110237 
1547237Sek110237 static void
1557237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1567237Sek110237 {
15710298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1587237Sek110237 
1597237Sek110237 	/*
1607237Sek110237 	 * Inheritance and range checking should have been done by now.
1617237Sek110237 	 */
1627237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1637237Sek110237 	    newval == ZFS_CACHE_METADATA);
1647237Sek110237 
16510298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
1667237Sek110237 }
1677237Sek110237 
16810310SNeil.Perrin@Sun.COM static void
16910310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
17010310SNeil.Perrin@Sun.COM {
17110310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
17210310SNeil.Perrin@Sun.COM 
17310310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
17410310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
17510310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
17610310SNeil.Perrin@Sun.COM 	if (os->os_zil)
17710310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
17810310SNeil.Perrin@Sun.COM }
17910310SNeil.Perrin@Sun.COM 
180789Sahrens void
181789Sahrens dmu_objset_byteswap(void *buf, size_t size)
182789Sahrens {
183789Sahrens 	objset_phys_t *osp = buf;
184789Sahrens 
1859396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
186789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
187789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
188789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
1899396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
1909396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
1919396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
1929396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
1939396SMatthew.Ahrens@Sun.COM 	}
194789Sahrens }
195789Sahrens 
1961544Seschrock int
1971544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
19810298SMatthew.Ahrens@Sun.COM     objset_t **osp)
199789Sahrens {
20010298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2017046Sahrens 	int i, err;
202789Sahrens 
2034787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2044787Sahrens 
20510298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
20610298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
20710298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
20810298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
20910298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2102391Smaybee 		uint32_t aflags = ARC_WAIT;
2111544Seschrock 		zbookmark_t zb;
2121544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
2131544Seschrock 		zb.zb_object = 0;
2141544Seschrock 		zb.zb_level = -1;
2151544Seschrock 		zb.zb_blkid = 0;
21610298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2177237Sek110237 			aflags |= ARC_L2CACHE;
2181544Seschrock 
21910298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2207046Sahrens 		/*
2217046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2227046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2237046Sahrens 		 * could change, and we'll need a lock.
2247046Sahrens 		 */
22510298SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
22610298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2272391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2281544Seschrock 		if (err) {
22910298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2307294Sperrin 			/* convert checksum errors into IO errors */
2317294Sperrin 			if (err == ECKSUM)
2327294Sperrin 				err = EIO;
2331544Seschrock 			return (err);
2341544Seschrock 		}
2359396SMatthew.Ahrens@Sun.COM 
2369396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2379396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
23810298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2399396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
24010298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2419396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2429396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
24310298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
24410298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
24510298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
24610298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
24710298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
2489396SMatthew.Ahrens@Sun.COM 		}
2499396SMatthew.Ahrens@Sun.COM 
25010298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
25110298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
252789Sahrens 	} else {
2539396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2549396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
25510298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
25610298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
25710298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
25810298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
259789Sahrens 	}
260789Sahrens 
261789Sahrens 	/*
262789Sahrens 	 * Note: the changed_cb will be called once before the register
263789Sahrens 	 * func returns, thus changing the checksum/compression from the
2647237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2657237Sek110237 	 * checksum/compression/copies.
266789Sahrens 	 */
2677237Sek110237 	if (ds) {
2687237Sek110237 		err = dsl_prop_register(ds, "primarycache",
26910298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
2701544Seschrock 		if (err == 0)
2717237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
27210298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
2737237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2747237Sek110237 			if (err == 0)
2757237Sek110237 				err = dsl_prop_register(ds, "checksum",
27610298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
2777237Sek110237 			if (err == 0)
2787237Sek110237 				err = dsl_prop_register(ds, "compression",
27910298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
2807237Sek110237 			if (err == 0)
2817237Sek110237 				err = dsl_prop_register(ds, "copies",
28210298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
28310310SNeil.Perrin@Sun.COM 			if (err == 0)
28410310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
28510310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
2867237Sek110237 		}
2871544Seschrock 		if (err) {
28810298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
28910298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
29010298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2911544Seschrock 			return (err);
2921544Seschrock 		}
2932082Seschrock 	} else if (ds == NULL) {
294789Sahrens 		/* It's the meta-objset. */
29510298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
29610298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
29710298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
29810298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
29910298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
300789Sahrens 	}
301789Sahrens 
30210298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
30310298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
304789Sahrens 
305789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
30610298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
307789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
30810298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
309789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
310789Sahrens 	}
31110298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
312789Sahrens 	    offsetof(dnode_t, dn_link));
31310298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
314789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
315789Sahrens 
31610298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
31710298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
31810298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3192856Snd150628 
32010298SMatthew.Ahrens@Sun.COM 	os->os_meta_dnode = dnode_special_open(os,
32110298SMatthew.Ahrens@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
32210298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
32310298SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode = dnode_special_open(os,
32410298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
32510298SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode = dnode_special_open(os,
32610298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3279396SMatthew.Ahrens@Sun.COM 	}
328789Sahrens 
3294787Sahrens 	/*
3304787Sahrens 	 * We should be the only thread trying to do this because we
3314787Sahrens 	 * have ds_opening_lock
3324787Sahrens 	 */
3334787Sahrens 	if (ds) {
33410298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
33510298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
33610298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
33710298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
338789Sahrens 	}
339789Sahrens 
34010298SMatthew.Ahrens@Sun.COM 	*osp = os;
3415367Sahrens 	return (0);
3425367Sahrens }
3435367Sahrens 
3445367Sahrens int
34510298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3465367Sahrens {
34710298SMatthew.Ahrens@Sun.COM 	int err = 0;
34810298SMatthew.Ahrens@Sun.COM 
34910298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
35010298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
35110298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
35210298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
35310298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
35410298SMatthew.Ahrens@Sun.COM 	}
35510298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
35610298SMatthew.Ahrens@Sun.COM 	return (err);
35710298SMatthew.Ahrens@Sun.COM }
35810298SMatthew.Ahrens@Sun.COM 
35910298SMatthew.Ahrens@Sun.COM /* called from zpl */
36010298SMatthew.Ahrens@Sun.COM int
36110298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
36210298SMatthew.Ahrens@Sun.COM {
36310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
3645367Sahrens 	int err;
3655367Sahrens 
36610298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
3675367Sahrens 	if (err)
36810298SMatthew.Ahrens@Sun.COM 		return (err);
36910298SMatthew.Ahrens@Sun.COM 
37010298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
37110298SMatthew.Ahrens@Sun.COM 	if (err)
37210298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
37310298SMatthew.Ahrens@Sun.COM 
3745367Sahrens 	return (err);
3755367Sahrens }
3765367Sahrens 
377789Sahrens /* called from zpl */
378789Sahrens int
37910298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
38010298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
381789Sahrens {
382789Sahrens 	dsl_dataset_t *ds;
383789Sahrens 	int err;
384789Sahrens 
38510298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
38610298SMatthew.Ahrens@Sun.COM 	if (err)
38710298SMatthew.Ahrens@Sun.COM 		return (err);
3885367Sahrens 
38910298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
390789Sahrens 	if (err) {
39110298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
392*10588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
39310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
39410298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
395*10588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
396*10588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
397*10588SEric.Taylor@Sun.COM 		return (EROFS);
398789Sahrens 	}
3995367Sahrens 	return (err);
400789Sahrens }
401789Sahrens 
402789Sahrens void
40310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
404789Sahrens {
40510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
40610298SMatthew.Ahrens@Sun.COM }
4076689Smaybee 
40810298SMatthew.Ahrens@Sun.COM void
40910298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
41010298SMatthew.Ahrens@Sun.COM {
41110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
412789Sahrens }
413789Sahrens 
4141646Sperrin int
4154944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4161544Seschrock {
4171544Seschrock 	dnode_t *dn;
4181596Sahrens 
41910298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4201596Sahrens 
4211596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
42210298SMatthew.Ahrens@Sun.COM 	list_remove(&os->os_dnodes, os->os_meta_dnode);
42310298SMatthew.Ahrens@Sun.COM 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
4241544Seschrock 
4251544Seschrock 	/*
4261596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4271596Sahrens 	 * because dnode_add_ref() only works if you already have a
4281596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4291596Sahrens 	 * skip.
4301544Seschrock 	 */
43110298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
4324944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
43310298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
4341596Sahrens 		continue;
4351596Sahrens 
4361596Sahrens 	while (dn) {
4371596Sahrens 		dnode_t *next_dn = dn;
4381596Sahrens 
4391596Sahrens 		do {
44010298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
4414944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4421596Sahrens 
44310298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
4444944Smaybee 		dnode_evict_dbufs(dn);
4451596Sahrens 		dnode_rele(dn, FTAG);
44610298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
4471596Sahrens 		dn = next_dn;
4481544Seschrock 	}
44910298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
45010298SMatthew.Ahrens@Sun.COM 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
4511544Seschrock }
4521544Seschrock 
4531544Seschrock void
45410298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
455789Sahrens {
45610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
4572082Seschrock 	int i;
458789Sahrens 
459789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
46010298SMatthew.Ahrens@Sun.COM 		ASSERT(list_head(&os->os_dirty_dnodes[i]) == NULL);
46110298SMatthew.Ahrens@Sun.COM 		ASSERT(list_head(&os->os_free_dnodes[i]) == NULL);
462789Sahrens 	}
463789Sahrens 
4647237Sek110237 	if (ds) {
4657237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4667237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
46710298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
4687237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
46910298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
4707237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
47110298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
47210310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
47310310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
4747237Sek110237 		}
4757237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
47610298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
4777237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
47810298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
479789Sahrens 	}
480789Sahrens 
4811544Seschrock 	/*
4821544Seschrock 	 * We should need only a single pass over the dnode list, since
4831544Seschrock 	 * nothing can be added to the list at this point.
4841544Seschrock 	 */
48510298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
4861544Seschrock 
48710298SMatthew.Ahrens@Sun.COM 	dnode_special_close(os->os_meta_dnode);
48810298SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode) {
48910298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_userused_dnode);
49010298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_groupused_dnode);
4919396SMatthew.Ahrens@Sun.COM 	}
49210298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
493789Sahrens 
49410298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
495789Sahrens 
49610298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
49710298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
49810298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
49910298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
50010298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
501789Sahrens }
502789Sahrens 
50310373Schris.kirby@sun.com timestruc_t
50410373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
50510373Schris.kirby@sun.com {
50610373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
50710373Schris.kirby@sun.com }
50810373Schris.kirby@sun.com 
509789Sahrens /* called from dsl for meta-objset */
51010298SMatthew.Ahrens@Sun.COM objset_t *
5113547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5123547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
513789Sahrens {
51410298SMatthew.Ahrens@Sun.COM 	objset_t *os;
515789Sahrens 	dnode_t *mdn;
516789Sahrens 
517789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5184787Sahrens 	if (ds)
5194787Sahrens 		mutex_enter(&ds->ds_opening_lock);
52010298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
5214787Sahrens 	if (ds)
5224787Sahrens 		mutex_exit(&ds->ds_opening_lock);
52310298SMatthew.Ahrens@Sun.COM 	mdn = os->os_meta_dnode;
524789Sahrens 
525789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
526789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
527789Sahrens 
528789Sahrens 	/*
529789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
530789Sahrens 	 * later, because then we could do it in quescing context while
531789Sahrens 	 * we are also accessing it in open context.
532789Sahrens 	 *
533789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
534789Sahrens 	 * because the MOS is only updated in syncing context.
535789Sahrens 	 * This is most fortunate: the MOS is the only objset that
536789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
537789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
538789Sahrens 	 */
5391544Seschrock 	if (ds != NULL) {
5401544Seschrock 		int levels = 1;
5411544Seschrock 
5421544Seschrock 		/*
5431544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5441544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5451544Seschrock 		 */
5461544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5471544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5481544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5491544Seschrock 			levels++;
5501544Seschrock 
551789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5521544Seschrock 		    mdn->dn_nlevels = levels;
5531544Seschrock 	}
554789Sahrens 
555789Sahrens 	ASSERT(type != DMU_OST_NONE);
556789Sahrens 	ASSERT(type != DMU_OST_ANY);
557789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
55810298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
55910298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
56010298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
56110298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
5629396SMatthew.Ahrens@Sun.COM 	}
563789Sahrens 
564789Sahrens 	dsl_dataset_dirty(ds, tx);
565789Sahrens 
56610298SMatthew.Ahrens@Sun.COM 	return (os);
567789Sahrens }
568789Sahrens 
569789Sahrens struct oscarg {
5704543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
571789Sahrens 	void *userarg;
57210272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
573789Sahrens 	const char *lastname;
574789Sahrens 	dmu_objset_type_t type;
5756492Stimh 	uint64_t flags;
576789Sahrens };
577789Sahrens 
5784543Smarks /*ARGSUSED*/
579789Sahrens static int
5802199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
581789Sahrens {
5822199Sahrens 	dsl_dir_t *dd = arg1;
5832199Sahrens 	struct oscarg *oa = arg2;
5842199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
5852199Sahrens 	int err;
5862199Sahrens 	uint64_t ddobj;
5872199Sahrens 
5882199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5892199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5902199Sahrens 	if (err != ENOENT)
5912199Sahrens 		return (err ? err : EEXIST);
5922199Sahrens 
59310272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
59410272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
59510272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
5962199Sahrens 			return (EXDEV);
5972199Sahrens 
59810272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
59910272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6002199Sahrens 			return (EINVAL);
6012199Sahrens 	}
6024543Smarks 
6032199Sahrens 	return (0);
6042199Sahrens }
6052199Sahrens 
6062199Sahrens static void
6074543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6082199Sahrens {
6092199Sahrens 	dsl_dir_t *dd = arg1;
6102199Sahrens 	struct oscarg *oa = arg2;
6112199Sahrens 	uint64_t dsobj;
612789Sahrens 
613789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
614789Sahrens 
6152199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
61610272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
617789Sahrens 
61810272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
61910272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
62010272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
62110298SMatthew.Ahrens@Sun.COM 		objset_t *os;
622789Sahrens 
62310272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
62410272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
62510272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
62610272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
62710272SMatthew.Ahrens@Sun.COM 
62810298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6293547Smaybee 		    ds, bp, oa->type, tx);
630789Sahrens 
631789Sahrens 		if (oa->userfunc)
63210298SMatthew.Ahrens@Sun.COM 			oa->userfunc(os, oa->userarg, cr, tx);
63310272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
634789Sahrens 	}
6354543Smarks 
6364543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6374543Smarks 	    tx, cr, "dataset = %llu", dsobj);
638789Sahrens }
639789Sahrens 
640789Sahrens int
64110272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6424543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
643789Sahrens {
6442199Sahrens 	dsl_dir_t *pdd;
645789Sahrens 	const char *tail;
646789Sahrens 	int err = 0;
6472199Sahrens 	struct oscarg oa = { 0 };
648789Sahrens 
6492199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6502199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6511544Seschrock 	if (err)
6521544Seschrock 		return (err);
653789Sahrens 	if (tail == NULL) {
6542199Sahrens 		dsl_dir_close(pdd, FTAG);
655789Sahrens 		return (EEXIST);
656789Sahrens 	}
657789Sahrens 
6582199Sahrens 	oa.userfunc = func;
6592199Sahrens 	oa.userarg = arg;
6602199Sahrens 	oa.lastname = tail;
6612199Sahrens 	oa.type = type;
6626492Stimh 	oa.flags = flags;
6634543Smarks 
66410272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
66510272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
66610272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
66710272SMatthew.Ahrens@Sun.COM 	return (err);
66810272SMatthew.Ahrens@Sun.COM }
66910272SMatthew.Ahrens@Sun.COM 
67010272SMatthew.Ahrens@Sun.COM int
67110272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
67210272SMatthew.Ahrens@Sun.COM {
67310272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
67410272SMatthew.Ahrens@Sun.COM 	const char *tail;
67510272SMatthew.Ahrens@Sun.COM 	int err = 0;
67610272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
67710272SMatthew.Ahrens@Sun.COM 
67810272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
67910272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
68010272SMatthew.Ahrens@Sun.COM 	if (err)
68110272SMatthew.Ahrens@Sun.COM 		return (err);
68210272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
68310272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
68410272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
685789Sahrens 	}
68610272SMatthew.Ahrens@Sun.COM 
68710272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
68810272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
68910272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
69010272SMatthew.Ahrens@Sun.COM 
6912199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6922199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6932199Sahrens 	dsl_dir_close(pdd, FTAG);
694789Sahrens 	return (err);
695789Sahrens }
696789Sahrens 
697789Sahrens int
69810242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
699789Sahrens {
70010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
701789Sahrens 	int error;
702789Sahrens 
703789Sahrens 	/*
70410272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
70510272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
70610272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
70710272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
70810272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
709789Sahrens 	 */
71010298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
711789Sahrens 	if (error == 0) {
71210298SMatthew.Ahrens@Sun.COM 		objset_t *os;
71310298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
71410298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
71510298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
71610272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
717789Sahrens 	}
718789Sahrens 
7195367Sahrens 	return (error);
720789Sahrens }
721789Sahrens 
7222199Sahrens struct snaparg {
7232199Sahrens 	dsl_sync_task_group_t *dstg;
7242199Sahrens 	char *snapname;
7252199Sahrens 	char failed[MAXPATHLEN];
7264543Smarks 	boolean_t checkperms;
7279355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7285367Sahrens };
7295367Sahrens 
7309355SMatthew.Ahrens@Sun.COM static int
7319355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7329355SMatthew.Ahrens@Sun.COM {
7339355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7349355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7359355SMatthew.Ahrens@Sun.COM 
7369355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7379355SMatthew.Ahrens@Sun.COM 
73810298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
7399355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7409355SMatthew.Ahrens@Sun.COM }
7419355SMatthew.Ahrens@Sun.COM 
7429355SMatthew.Ahrens@Sun.COM static void
7439355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7449355SMatthew.Ahrens@Sun.COM {
7459355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
74610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
7479355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7489355SMatthew.Ahrens@Sun.COM 
7499355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7509355SMatthew.Ahrens@Sun.COM 
7519355SMatthew.Ahrens@Sun.COM 	if (sn->props)
7529355SMatthew.Ahrens@Sun.COM 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
7539355SMatthew.Ahrens@Sun.COM }
7542199Sahrens 
7552199Sahrens static int
7562199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7572199Sahrens {
7582199Sahrens 	struct snaparg *sn = arg;
7592199Sahrens 	objset_t *os;
7602199Sahrens 	int err;
7612199Sahrens 
7622199Sahrens 	(void) strcpy(sn->failed, name);
7632199Sahrens 
7644543Smarks 	/*
7654543Smarks 	 * Check permissions only when requested.  This only applies when
7664543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
7674543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
7684543Smarks 	 */
7694543Smarks 	if (sn->checkperms == B_TRUE &&
7704543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
7714543Smarks 		return (err);
7724543Smarks 
77310298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
7742199Sahrens 	if (err != 0)
7752199Sahrens 		return (err);
7762199Sahrens 
7776689Smaybee 	/* If the objset is in an inconsistent state, return busy */
77810298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
77910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
7803637Srm160521 		return (EBUSY);
7813637Srm160521 	}
7823637Srm160521 
7833637Srm160521 	/*
7842199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7852199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
7862199Sahrens 	 * a side effect.
7872199Sahrens 	 */
7882199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7892199Sahrens 	if (err == 0) {
7909355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
7919355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
7923637Srm160521 	} else {
79310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
7942199Sahrens 	}
7953637Srm160521 
7962199Sahrens 	return (err);
7972199Sahrens }
7982199Sahrens 
7992199Sahrens int
8009355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
8019355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
8022199Sahrens {
8032199Sahrens 	dsl_sync_task_t *dst;
8049355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8052199Sahrens 	spa_t *spa;
8062199Sahrens 	int err;
8072199Sahrens 
8082199Sahrens 	(void) strcpy(sn.failed, fsname);
8092199Sahrens 
8104603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8112199Sahrens 	if (err)
8122199Sahrens 		return (err);
8132199Sahrens 
8142199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8152199Sahrens 	sn.snapname = snapname;
8169355SMatthew.Ahrens@Sun.COM 	sn.props = props;
8172199Sahrens 
8182417Sahrens 	if (recursive) {
8194543Smarks 		sn.checkperms = B_TRUE;
8202417Sahrens 		err = dmu_objset_find(fsname,
8212417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8222417Sahrens 	} else {
8234543Smarks 		sn.checkperms = B_FALSE;
8242199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8252417Sahrens 	}
8262199Sahrens 
8279355SMatthew.Ahrens@Sun.COM 	if (err == 0)
8289355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
8292199Sahrens 
8302199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8312199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8329355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
83310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
8342199Sahrens 		if (dst->dst_err)
8355367Sahrens 			dsl_dataset_name(ds, sn.failed);
8369355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
83710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
8382199Sahrens 	}
8395367Sahrens 
8402199Sahrens 	if (err)
8412199Sahrens 		(void) strcpy(fsname, sn.failed);
8422199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8432199Sahrens 	spa_close(spa, FTAG);
8442199Sahrens 	return (err);
8452199Sahrens }
8462199Sahrens 
847789Sahrens static void
8489396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
849789Sahrens {
8503547Smaybee 	dnode_t *dn;
851789Sahrens 
8523547Smaybee 	while (dn = list_head(list)) {
8533547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8543547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8553547Smaybee 		/*
8569396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
8579396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
8583547Smaybee 		 */
8593547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
8603547Smaybee 		ASSERT(dn->dn_zio);
861789Sahrens 
8623547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
8633547Smaybee 		list_remove(list, dn);
8649396SMatthew.Ahrens@Sun.COM 
8659396SMatthew.Ahrens@Sun.COM 		if (newlist) {
8669396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
8679396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
8689396SMatthew.Ahrens@Sun.COM 		}
8699396SMatthew.Ahrens@Sun.COM 
8703547Smaybee 		dnode_sync(dn, tx);
8713547Smaybee 	}
8723547Smaybee }
8732981Sahrens 
8743547Smaybee /* ARGSUSED */
8753547Smaybee static void
8763547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
8773547Smaybee {
8787754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
8797754SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
88010298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
8813547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8822981Sahrens 
8837754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
8847754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
8857754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
8865329Sgw25295 
8873547Smaybee 	/*
8889396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
8899396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
8909396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
8919396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
8923547Smaybee 	 */
8939396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
8947754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
8953547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8965329Sgw25295 
8977754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
8987754SJeff.Bonwick@Sun.COM 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
8997754SJeff.Bonwick@Sun.COM 	} else {
9005329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
9016992Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9027754SJeff.Bonwick@Sun.COM 			    &zio->io_bp_orig, zio, os->os_synctx);
9035329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
9045329Sgw25295 	}
905789Sahrens }
906789Sahrens 
907789Sahrens /* called from dsl */
908789Sahrens void
90910298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
910789Sahrens {
911789Sahrens 	int txgoff;
9121544Seschrock 	zbookmark_t zb;
9137046Sahrens 	writeprops_t wp = { 0 };
9143547Smaybee 	zio_t *zio;
9153547Smaybee 	list_t *list;
9169396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9173547Smaybee 	dbuf_dirty_record_t *dr;
9183547Smaybee 
9193547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
920789Sahrens 
921789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
922789Sahrens 	/* XXX the write_done callback should really give us the tx... */
923789Sahrens 	os->os_synctx = tx;
924789Sahrens 
9253882Sahrens 	if (os->os_dsl_dataset == NULL) {
9263882Sahrens 		/*
9273882Sahrens 		 * This is the MOS.  If we have upgraded,
9283882Sahrens 		 * spa_max_replication() could change, so reset
9293882Sahrens 		 * os_copies here.
9303882Sahrens 		 */
9313882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9323882Sahrens 	}
9333882Sahrens 
9343547Smaybee 	/*
9353547Smaybee 	 * Create the root block IO
9363547Smaybee 	 */
9373547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
9383547Smaybee 	zb.zb_object = 0;
9397754SJeff.Bonwick@Sun.COM 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
9403547Smaybee 	zb.zb_blkid = 0;
9417754SJeff.Bonwick@Sun.COM 
9427754SJeff.Bonwick@Sun.COM 	wp.wp_type = DMU_OT_OBJSET;
9437754SJeff.Bonwick@Sun.COM 	wp.wp_level = 0;	/* on-disk BP level; see above */
9447754SJeff.Bonwick@Sun.COM 	wp.wp_copies = os->os_copies;
9457754SJeff.Bonwick@Sun.COM 	wp.wp_oschecksum = os->os_checksum;
9467754SJeff.Bonwick@Sun.COM 	wp.wp_oscompress = os->os_compress;
9477754SJeff.Bonwick@Sun.COM 
9484787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
9496992Smaybee 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9503547Smaybee 		    os->os_rootbp, pio, tx);
9514787Sahrens 	}
9527754SJeff.Bonwick@Sun.COM 
9537046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9549396SMatthew.Ahrens@Sun.COM 
9557754SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
9567754SJeff.Bonwick@Sun.COM 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
9577754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
9583547Smaybee 
9593547Smaybee 	/*
9609396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
9613547Smaybee 	 */
9623547Smaybee 	os->os_meta_dnode->dn_zio = zio;
9633547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
964789Sahrens 
9659396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
9669396SMatthew.Ahrens@Sun.COM 
9679396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
9689396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
9699396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
9709396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
9719396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
9729396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
9739396SMatthew.Ahrens@Sun.COM 	}
9749396SMatthew.Ahrens@Sun.COM 
975789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
976789Sahrens 
9779396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
9789396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
9799396SMatthew.Ahrens@Sun.COM 		/*
9809396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
9819396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
9829396SMatthew.Ahrens@Sun.COM 		 */
9839396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
9849396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
9859396SMatthew.Ahrens@Sun.COM 	}
9869396SMatthew.Ahrens@Sun.COM 
9879396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
9889396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
989789Sahrens 
9903547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
9913547Smaybee 	while (dr = list_head(list)) {
9923547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
9933547Smaybee 		list_remove(list, dr);
9943547Smaybee 		if (dr->dr_zio)
9953547Smaybee 			zio_nowait(dr->dr_zio);
9963547Smaybee 	}
997789Sahrens 	/*
998789Sahrens 	 * Free intent log blocks up to this tx.
999789Sahrens 	 */
1000789Sahrens 	zil_sync(os->os_zil, tx);
10017046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
10023547Smaybee 	zio_nowait(zio);
1003789Sahrens }
1004789Sahrens 
10059396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
10069396SMatthew.Ahrens@Sun.COM 
10079396SMatthew.Ahrens@Sun.COM void
10089396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
10099396SMatthew.Ahrens@Sun.COM {
10109396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
10119396SMatthew.Ahrens@Sun.COM }
10129396SMatthew.Ahrens@Sun.COM 
10139396SMatthew.Ahrens@Sun.COM boolean_t
101410298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
10159396SMatthew.Ahrens@Sun.COM {
10169396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
10179396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
10189396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
10199396SMatthew.Ahrens@Sun.COM }
10209396SMatthew.Ahrens@Sun.COM 
102110407SMatthew.Ahrens@Sun.COM static void
102210407SMatthew.Ahrens@Sun.COM do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
102310407SMatthew.Ahrens@Sun.COM     boolean_t subtract, dmu_tx_t *tx)
102410407SMatthew.Ahrens@Sun.COM {
102510407SMatthew.Ahrens@Sun.COM 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
102610407SMatthew.Ahrens@Sun.COM 	uint64_t user, group;
102710407SMatthew.Ahrens@Sun.COM 
102810407SMatthew.Ahrens@Sun.COM 	ASSERT(dnp->dn_type != 0 ||
102910407SMatthew.Ahrens@Sun.COM 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
103010407SMatthew.Ahrens@Sun.COM 	    DN_USED_BYTES(dnp) == 0));
103110407SMatthew.Ahrens@Sun.COM 
103210407SMatthew.Ahrens@Sun.COM 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
103310407SMatthew.Ahrens@Sun.COM 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
103410407SMatthew.Ahrens@Sun.COM 	    DN_BONUS(dnp), &user, &group)) {
103510407SMatthew.Ahrens@Sun.COM 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
103610407SMatthew.Ahrens@Sun.COM 		if (subtract)
103710407SMatthew.Ahrens@Sun.COM 			delta = -delta;
103810407SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_increment_int(os, DMU_USERUSED_OBJECT,
103910407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
104010407SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_increment_int(os, DMU_GROUPUSED_OBJECT,
104110407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
104210407SMatthew.Ahrens@Sun.COM 	}
104310407SMatthew.Ahrens@Sun.COM }
104410407SMatthew.Ahrens@Sun.COM 
10459396SMatthew.Ahrens@Sun.COM void
104610298SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
10479396SMatthew.Ahrens@Sun.COM {
10489396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
10499396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
10509396SMatthew.Ahrens@Sun.COM 
10519396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
10529396SMatthew.Ahrens@Sun.COM 
10539396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
10549396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
10559396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys);
10569396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
10579396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
10589396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
10599396SMatthew.Ahrens@Sun.COM 
10609396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
10619396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
106210298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
10639396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
10649396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
106510298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
10669396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
10679396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
10689396SMatthew.Ahrens@Sun.COM 		}
10699396SMatthew.Ahrens@Sun.COM 
10709396SMatthew.Ahrens@Sun.COM 		/*
107110407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
107210407SMatthew.Ahrens@Sun.COM 		 * net delta (due to phys-oldphys) is zero.  Otherwise
107310407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
107410407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
107510407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
10769396SMatthew.Ahrens@Sun.COM 		 */
107710407SMatthew.Ahrens@Sun.COM 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
107810407SMatthew.Ahrens@Sun.COM 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
10799396SMatthew.Ahrens@Sun.COM 
10809396SMatthew.Ahrens@Sun.COM 		/*
10819396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
10829396SMatthew.Ahrens@Sun.COM 		 */
10839396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
10849396SMatthew.Ahrens@Sun.COM 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
10859396SMatthew.Ahrens@Sun.COM 		dn->dn_oldphys = NULL;
10869396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
10879396SMatthew.Ahrens@Sun.COM 
10889396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
10899396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
10909396SMatthew.Ahrens@Sun.COM 	}
10919396SMatthew.Ahrens@Sun.COM }
10929396SMatthew.Ahrens@Sun.COM 
10939396SMatthew.Ahrens@Sun.COM boolean_t
10949396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
10959396SMatthew.Ahrens@Sun.COM {
109610298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
10979396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
10989396SMatthew.Ahrens@Sun.COM }
10999396SMatthew.Ahrens@Sun.COM 
11009396SMatthew.Ahrens@Sun.COM int
11019396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
11029396SMatthew.Ahrens@Sun.COM {
11039396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
11049396SMatthew.Ahrens@Sun.COM 	int err = 0;
11059396SMatthew.Ahrens@Sun.COM 
11069396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
11079396SMatthew.Ahrens@Sun.COM 		return (0);
110810298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
11099396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
11109396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
11119396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
11129396SMatthew.Ahrens@Sun.COM 
11139396SMatthew.Ahrens@Sun.COM 	/*
11149396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
11159396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
11169396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
11179396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
11189396SMatthew.Ahrens@Sun.COM 	 * independently.
11199396SMatthew.Ahrens@Sun.COM 	 */
11209396SMatthew.Ahrens@Sun.COM 
11219396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
11229951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
11239396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
11249396SMatthew.Ahrens@Sun.COM 		int objerr;
11259396SMatthew.Ahrens@Sun.COM 
11269396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
11279396SMatthew.Ahrens@Sun.COM 			return (EINTR);
11289396SMatthew.Ahrens@Sun.COM 
11299396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
11309396SMatthew.Ahrens@Sun.COM 		if (objerr)
11319396SMatthew.Ahrens@Sun.COM 			continue;
11329951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
11339396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
11349396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
11359396SMatthew.Ahrens@Sun.COM 		if (objerr) {
11369396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
11379396SMatthew.Ahrens@Sun.COM 			continue;
11389396SMatthew.Ahrens@Sun.COM 		}
11399396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
11409396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
11419396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
11429396SMatthew.Ahrens@Sun.COM 	}
11439396SMatthew.Ahrens@Sun.COM 
114410298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
11459396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
11469396SMatthew.Ahrens@Sun.COM 	return (0);
11479396SMatthew.Ahrens@Sun.COM }
11489396SMatthew.Ahrens@Sun.COM 
1149789Sahrens void
11502885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
11512885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
11522885Sahrens {
115310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
11542885Sahrens 	    usedobjsp, availobjsp);
11552885Sahrens }
11562885Sahrens 
11572885Sahrens uint64_t
11582885Sahrens dmu_objset_fsid_guid(objset_t *os)
11592885Sahrens {
116010298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
11612885Sahrens }
11622885Sahrens 
11632885Sahrens void
11642885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1165789Sahrens {
116610298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
116710298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
116810298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
11692885Sahrens }
11702885Sahrens 
11712885Sahrens void
11722885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
11732885Sahrens {
117410298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
117510298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
11762885Sahrens 
117710298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
117810298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
11792885Sahrens 
11802885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
118110298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
11829396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
11839396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1184789Sahrens }
1185789Sahrens 
1186789Sahrens int
1187789Sahrens dmu_objset_is_snapshot(objset_t *os)
1188789Sahrens {
118910298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
119010298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1191789Sahrens 	else
1192789Sahrens 		return (B_FALSE);
1193789Sahrens }
1194789Sahrens 
1195789Sahrens int
11966492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
11976492Stimh     boolean_t *conflict)
11986492Stimh {
119910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
12006492Stimh 	uint64_t ignored;
12016492Stimh 
12026492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
12036492Stimh 		return (ENOENT);
12046492Stimh 
12056492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
12066492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
12076492Stimh 	    real, maxlen, conflict));
12086492Stimh }
12096492Stimh 
12106492Stimh int
1211789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
12125663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1213789Sahrens {
121410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1215789Sahrens 	zap_cursor_t cursor;
1216789Sahrens 	zap_attribute_t attr;
1217789Sahrens 
1218789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1219789Sahrens 		return (ENOENT);
1220789Sahrens 
1221789Sahrens 	zap_cursor_init_serialized(&cursor,
1222789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1223789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1224789Sahrens 
1225885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1226885Sahrens 		zap_cursor_fini(&cursor);
1227885Sahrens 		return (ENOENT);
1228885Sahrens 	}
1229885Sahrens 
1230885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1231885Sahrens 		zap_cursor_fini(&cursor);
1232885Sahrens 		return (ENAMETOOLONG);
1233885Sahrens 	}
1234885Sahrens 
1235885Sahrens 	(void) strcpy(name, attr.za_name);
1236885Sahrens 	if (idp)
1237885Sahrens 		*idp = attr.za_first_integer;
12385663Sck153898 	if (case_conflict)
12395663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1240885Sahrens 	zap_cursor_advance(&cursor);
1241885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1242885Sahrens 	zap_cursor_fini(&cursor);
1243885Sahrens 
1244885Sahrens 	return (0);
1245885Sahrens }
1246885Sahrens 
1247885Sahrens int
1248885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1249885Sahrens     uint64_t *idp, uint64_t *offp)
1250885Sahrens {
125110298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1252885Sahrens 	zap_cursor_t cursor;
1253885Sahrens 	zap_attribute_t attr;
1254885Sahrens 
1255885Sahrens 	/* there is no next dir on a snapshot! */
125610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1257885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1258885Sahrens 		return (ENOENT);
1259885Sahrens 
1260885Sahrens 	zap_cursor_init_serialized(&cursor,
1261885Sahrens 	    dd->dd_pool->dp_meta_objset,
1262885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1263885Sahrens 
1264885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1265885Sahrens 		zap_cursor_fini(&cursor);
1266885Sahrens 		return (ENOENT);
1267885Sahrens 	}
1268885Sahrens 
1269885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1270885Sahrens 		zap_cursor_fini(&cursor);
1271789Sahrens 		return (ENAMETOOLONG);
1272885Sahrens 	}
1273789Sahrens 
1274789Sahrens 	(void) strcpy(name, attr.za_name);
1275885Sahrens 	if (idp)
1276885Sahrens 		*idp = attr.za_first_integer;
1277789Sahrens 	zap_cursor_advance(&cursor);
1278789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1279885Sahrens 	zap_cursor_fini(&cursor);
1280789Sahrens 
1281789Sahrens 	return (0);
1282789Sahrens }
1283789Sahrens 
12847046Sahrens struct findarg {
12857046Sahrens 	int (*func)(char *, void *);
12867046Sahrens 	void *arg;
12877046Sahrens };
12887046Sahrens 
12897046Sahrens /* ARGSUSED */
12907046Sahrens static int
12917046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
12927046Sahrens {
12937046Sahrens 	struct findarg *fa = arg;
12947046Sahrens 	return (fa->func((char *)dsname, fa->arg));
12957046Sahrens }
12967046Sahrens 
1297789Sahrens /*
1298789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
12997046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1300789Sahrens  */
13012199Sahrens int
13022199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1303789Sahrens {
13047046Sahrens 	struct findarg fa;
13057046Sahrens 	fa.func = func;
13067046Sahrens 	fa.arg = arg;
13077046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
13087046Sahrens }
13097046Sahrens 
13107046Sahrens /*
13117046Sahrens  * Find all objsets under name, call func on each
13127046Sahrens  */
13137046Sahrens int
13147046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
13157046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
13167046Sahrens {
1317789Sahrens 	dsl_dir_t *dd;
13187046Sahrens 	dsl_pool_t *dp;
13197046Sahrens 	dsl_dataset_t *ds;
1320789Sahrens 	zap_cursor_t zc;
13213978Smmusante 	zap_attribute_t *attr;
1322789Sahrens 	char *child;
13237046Sahrens 	uint64_t thisobj;
13247046Sahrens 	int err;
1325789Sahrens 
13267046Sahrens 	if (name == NULL)
13277046Sahrens 		name = spa_name(spa);
13287046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
13291544Seschrock 	if (err)
13302199Sahrens 		return (err);
1331789Sahrens 
13327046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
13337046Sahrens 	if (dd->dd_myname[0] == '$') {
13347046Sahrens 		dsl_dir_close(dd, FTAG);
13357046Sahrens 		return (0);
13367046Sahrens 	}
13377046Sahrens 
13387046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
13393978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13407046Sahrens 	dp = dd->dd_pool;
1341789Sahrens 
1342789Sahrens 	/*
1343789Sahrens 	 * Iterate over all children.
1344789Sahrens 	 */
13452417Sahrens 	if (flags & DS_FIND_CHILDREN) {
13467046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13472417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
13483978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13492417Sahrens 		    (void) zap_cursor_advance(&zc)) {
13503978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
13513978Smmusante 			ASSERT(attr->za_num_integers == 1);
1352789Sahrens 
13532417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13547046Sahrens 			(void) strcpy(child, name);
13552417Sahrens 			(void) strcat(child, "/");
13563978Smmusante 			(void) strcat(child, attr->za_name);
13577046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13582417Sahrens 			kmem_free(child, MAXPATHLEN);
13592417Sahrens 			if (err)
13602417Sahrens 				break;
13612417Sahrens 		}
13622417Sahrens 		zap_cursor_fini(&zc);
13632199Sahrens 
13642417Sahrens 		if (err) {
13652417Sahrens 			dsl_dir_close(dd, FTAG);
13663978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13672417Sahrens 			return (err);
13682417Sahrens 		}
1369789Sahrens 	}
1370789Sahrens 
1371789Sahrens 	/*
1372789Sahrens 	 * Iterate over all snapshots.
1373789Sahrens 	 */
13747046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
13757046Sahrens 		if (!dsl_pool_sync_context(dp))
13767046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
13777046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
13787046Sahrens 		if (!dsl_pool_sync_context(dp))
13797046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1380789Sahrens 
13817046Sahrens 		if (err == 0) {
13827046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
13837046Sahrens 			dsl_dataset_rele(ds, FTAG);
1384789Sahrens 
13857046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
13867046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
13877046Sahrens 			    (void) zap_cursor_advance(&zc)) {
13887046Sahrens 				ASSERT(attr->za_integer_length ==
13897046Sahrens 				    sizeof (uint64_t));
13907046Sahrens 				ASSERT(attr->za_num_integers == 1);
1391789Sahrens 
13927046Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13937046Sahrens 				(void) strcpy(child, name);
13947046Sahrens 				(void) strcat(child, "@");
13957046Sahrens 				(void) strcat(child, attr->za_name);
13967046Sahrens 				err = func(spa, attr->za_first_integer,
13977046Sahrens 				    child, arg);
13987046Sahrens 				kmem_free(child, MAXPATHLEN);
13997046Sahrens 				if (err)
14007046Sahrens 					break;
14017046Sahrens 			}
14027046Sahrens 			zap_cursor_fini(&zc);
1403789Sahrens 		}
1404789Sahrens 	}
1405789Sahrens 
1406789Sahrens 	dsl_dir_close(dd, FTAG);
14073978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1408789Sahrens 
14092199Sahrens 	if (err)
14102199Sahrens 		return (err);
14112199Sahrens 
1412789Sahrens 	/*
1413789Sahrens 	 * Apply to self if appropriate.
1414789Sahrens 	 */
14157046Sahrens 	err = func(spa, thisobj, name, arg);
14162199Sahrens 	return (err);
1417789Sahrens }
14185326Sek110237 
14198415SRichard.Morris@Sun.COM /* ARGSUSED */
14208415SRichard.Morris@Sun.COM int
14218415SRichard.Morris@Sun.COM dmu_objset_prefetch(char *name, void *arg)
14228415SRichard.Morris@Sun.COM {
14238415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
14248415SRichard.Morris@Sun.COM 
14258415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
14268415SRichard.Morris@Sun.COM 		return (0);
14278415SRichard.Morris@Sun.COM 
14288415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14298415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
143010298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
14318415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14328415SRichard.Morris@Sun.COM 			zbookmark_t zb;
14338415SRichard.Morris@Sun.COM 
14348415SRichard.Morris@Sun.COM 			zb.zb_objset = ds->ds_object;
14358415SRichard.Morris@Sun.COM 			zb.zb_object = 0;
14368415SRichard.Morris@Sun.COM 			zb.zb_level = -1;
14378415SRichard.Morris@Sun.COM 			zb.zb_blkid = 0;
14388415SRichard.Morris@Sun.COM 
14398415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14408415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
14418415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
14428415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14438415SRichard.Morris@Sun.COM 			    &aflags, &zb);
14448415SRichard.Morris@Sun.COM 		}
14458415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
14468415SRichard.Morris@Sun.COM 	}
14478415SRichard.Morris@Sun.COM 
14488415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
14498415SRichard.Morris@Sun.COM 	return (0);
14508415SRichard.Morris@Sun.COM }
14518415SRichard.Morris@Sun.COM 
14525326Sek110237 void
14535326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
14545326Sek110237 {
145510298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
145610298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
14575326Sek110237 }
14585326Sek110237 
14595326Sek110237 void *
14605326Sek110237 dmu_objset_get_user(objset_t *os)
14615326Sek110237 {
146210298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
146310298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
14645326Sek110237 }
1465