xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 10298:a0d52501437c)
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 {
48*10298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
49789Sahrens }
50789Sahrens 
51789Sahrens zilog_t *
52789Sahrens dmu_objset_zil(objset_t *os)
53789Sahrens {
54*10298SMatthew.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 
62*10298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
63789Sahrens 		return (ds->ds_dir->dd_pool);
64789Sahrens 	else
65*10298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
66789Sahrens }
67789Sahrens 
68789Sahrens dsl_dataset_t *
69789Sahrens dmu_objset_ds(objset_t *os)
70789Sahrens {
71*10298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
72789Sahrens }
73789Sahrens 
74789Sahrens dmu_objset_type_t
75789Sahrens dmu_objset_type(objset_t *os)
76789Sahrens {
77*10298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
78789Sahrens }
79789Sahrens 
80789Sahrens void
81789Sahrens dmu_objset_name(objset_t *os, char *buf)
82789Sahrens {
83*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
84789Sahrens }
85789Sahrens 
86789Sahrens uint64_t
87789Sahrens dmu_objset_id(objset_t *os)
88789Sahrens {
89*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
90789Sahrens 
91789Sahrens 	return (ds ? ds->ds_object : 0);
92789Sahrens }
93789Sahrens 
94789Sahrens static void
95789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
96789Sahrens {
97*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
98789Sahrens 
99789Sahrens 	/*
100789Sahrens 	 * Inheritance should have been done by now.
101789Sahrens 	 */
102789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
103789Sahrens 
104*10298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
105789Sahrens }
106789Sahrens 
107789Sahrens static void
108789Sahrens compression_changed_cb(void *arg, uint64_t newval)
109789Sahrens {
110*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
111789Sahrens 
112789Sahrens 	/*
113789Sahrens 	 * Inheritance and range checking should have been done by now.
114789Sahrens 	 */
115789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
116789Sahrens 
117*10298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
118789Sahrens }
119789Sahrens 
1203835Sahrens static void
1213835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1223835Sahrens {
123*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1243835Sahrens 
1253835Sahrens 	/*
1263835Sahrens 	 * Inheritance and range checking should have been done by now.
1273835Sahrens 	 */
1283835Sahrens 	ASSERT(newval > 0);
129*10298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1303835Sahrens 
131*10298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1323835Sahrens }
1333835Sahrens 
1347237Sek110237 static void
1357237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1367237Sek110237 {
137*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1387237Sek110237 
1397237Sek110237 	/*
1407237Sek110237 	 * Inheritance and range checking should have been done by now.
1417237Sek110237 	 */
1427237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1437237Sek110237 	    newval == ZFS_CACHE_METADATA);
1447237Sek110237 
145*10298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1467237Sek110237 }
1477237Sek110237 
1487237Sek110237 static void
1497237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1507237Sek110237 {
151*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1527237Sek110237 
1537237Sek110237 	/*
1547237Sek110237 	 * Inheritance and range checking should have been done by now.
1557237Sek110237 	 */
1567237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1577237Sek110237 	    newval == ZFS_CACHE_METADATA);
1587237Sek110237 
159*10298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
1607237Sek110237 }
1617237Sek110237 
162789Sahrens void
163789Sahrens dmu_objset_byteswap(void *buf, size_t size)
164789Sahrens {
165789Sahrens 	objset_phys_t *osp = buf;
166789Sahrens 
1679396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
168789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
169789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
170789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
1719396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
1729396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
1739396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
1749396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
1759396SMatthew.Ahrens@Sun.COM 	}
176789Sahrens }
177789Sahrens 
1781544Seschrock int
1791544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
180*10298SMatthew.Ahrens@Sun.COM     objset_t **osp)
181789Sahrens {
182*10298SMatthew.Ahrens@Sun.COM 	objset_t *os;
1837046Sahrens 	int i, err;
184789Sahrens 
1854787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
1864787Sahrens 
187*10298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
188*10298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
189*10298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
190*10298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
191*10298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
1922391Smaybee 		uint32_t aflags = ARC_WAIT;
1931544Seschrock 		zbookmark_t zb;
1941544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
1951544Seschrock 		zb.zb_object = 0;
1961544Seschrock 		zb.zb_level = -1;
1971544Seschrock 		zb.zb_blkid = 0;
198*10298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
1997237Sek110237 			aflags |= ARC_L2CACHE;
2001544Seschrock 
201*10298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2027046Sahrens 		/*
2037046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2047046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2057046Sahrens 		 * could change, and we'll need a lock.
2067046Sahrens 		 */
207*10298SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
208*10298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2092391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2101544Seschrock 		if (err) {
211*10298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2127294Sperrin 			/* convert checksum errors into IO errors */
2137294Sperrin 			if (err == ECKSUM)
2147294Sperrin 				err = EIO;
2151544Seschrock 			return (err);
2161544Seschrock 		}
2179396SMatthew.Ahrens@Sun.COM 
2189396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2199396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
220*10298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2219396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
222*10298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2239396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2249396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
225*10298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
226*10298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
227*10298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
228*10298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
229*10298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
2309396SMatthew.Ahrens@Sun.COM 		}
2319396SMatthew.Ahrens@Sun.COM 
232*10298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
233*10298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
234789Sahrens 	} else {
2359396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2369396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
237*10298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
238*10298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
239*10298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
240*10298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
241789Sahrens 	}
242789Sahrens 
243789Sahrens 	/*
244789Sahrens 	 * Note: the changed_cb will be called once before the register
245789Sahrens 	 * func returns, thus changing the checksum/compression from the
2467237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2477237Sek110237 	 * checksum/compression/copies.
248789Sahrens 	 */
2497237Sek110237 	if (ds) {
2507237Sek110237 		err = dsl_prop_register(ds, "primarycache",
251*10298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
2521544Seschrock 		if (err == 0)
2537237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
254*10298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
2557237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2567237Sek110237 			if (err == 0)
2577237Sek110237 				err = dsl_prop_register(ds, "checksum",
258*10298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
2597237Sek110237 			if (err == 0)
2607237Sek110237 				err = dsl_prop_register(ds, "compression",
261*10298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
2627237Sek110237 			if (err == 0)
2637237Sek110237 				err = dsl_prop_register(ds, "copies",
264*10298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
2657237Sek110237 		}
2661544Seschrock 		if (err) {
267*10298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
268*10298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
269*10298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2701544Seschrock 			return (err);
2711544Seschrock 		}
2722082Seschrock 	} else if (ds == NULL) {
273789Sahrens 		/* It's the meta-objset. */
274*10298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
275*10298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
276*10298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
277*10298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
278*10298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
279789Sahrens 	}
280789Sahrens 
281*10298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
282*10298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
283789Sahrens 
284789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
285*10298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
286789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
287*10298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
288789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
289789Sahrens 	}
290*10298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
291789Sahrens 	    offsetof(dnode_t, dn_link));
292*10298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
293789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
294789Sahrens 
295*10298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
296*10298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
297*10298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
2982856Snd150628 
299*10298SMatthew.Ahrens@Sun.COM 	os->os_meta_dnode = dnode_special_open(os,
300*10298SMatthew.Ahrens@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
301*10298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
302*10298SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode = dnode_special_open(os,
303*10298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
304*10298SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode = dnode_special_open(os,
305*10298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3069396SMatthew.Ahrens@Sun.COM 	}
307789Sahrens 
3084787Sahrens 	/*
3094787Sahrens 	 * We should be the only thread trying to do this because we
3104787Sahrens 	 * have ds_opening_lock
3114787Sahrens 	 */
3124787Sahrens 	if (ds) {
313*10298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
314*10298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
315*10298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
316*10298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
317789Sahrens 	}
318789Sahrens 
319*10298SMatthew.Ahrens@Sun.COM 	*osp = os;
3205367Sahrens 	return (0);
3215367Sahrens }
3225367Sahrens 
3235367Sahrens int
324*10298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3255367Sahrens {
326*10298SMatthew.Ahrens@Sun.COM 	int err = 0;
327*10298SMatthew.Ahrens@Sun.COM 
328*10298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
329*10298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
330*10298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
331*10298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
332*10298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
333*10298SMatthew.Ahrens@Sun.COM 	}
334*10298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
335*10298SMatthew.Ahrens@Sun.COM 	return (err);
336*10298SMatthew.Ahrens@Sun.COM }
337*10298SMatthew.Ahrens@Sun.COM 
338*10298SMatthew.Ahrens@Sun.COM /* called from zpl */
339*10298SMatthew.Ahrens@Sun.COM int
340*10298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
341*10298SMatthew.Ahrens@Sun.COM {
342*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
3435367Sahrens 	int err;
3445367Sahrens 
345*10298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
3465367Sahrens 	if (err)
347*10298SMatthew.Ahrens@Sun.COM 		return (err);
348*10298SMatthew.Ahrens@Sun.COM 
349*10298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
350*10298SMatthew.Ahrens@Sun.COM 	if (err)
351*10298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
352*10298SMatthew.Ahrens@Sun.COM 
3535367Sahrens 	return (err);
3545367Sahrens }
3555367Sahrens 
356789Sahrens /* called from zpl */
357789Sahrens int
358*10298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
359*10298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
360789Sahrens {
361789Sahrens 	dsl_dataset_t *ds;
362789Sahrens 	int err;
363789Sahrens 
364*10298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
365*10298SMatthew.Ahrens@Sun.COM 	if (err)
366*10298SMatthew.Ahrens@Sun.COM 		return (err);
3675367Sahrens 
368*10298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
369789Sahrens 	if (err) {
370*10298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
371*10298SMatthew.Ahrens@Sun.COM 	} else if ((type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) ||
372*10298SMatthew.Ahrens@Sun.COM 	    (!readonly && dsl_dataset_is_snapshot(ds))) {
373*10298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
374*10298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
375789Sahrens 	}
376789Sahrens 
3775367Sahrens 	return (err);
378789Sahrens }
379789Sahrens 
380789Sahrens void
381*10298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
382789Sahrens {
383*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
384*10298SMatthew.Ahrens@Sun.COM }
3856689Smaybee 
386*10298SMatthew.Ahrens@Sun.COM void
387*10298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
388*10298SMatthew.Ahrens@Sun.COM {
389*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
390789Sahrens }
391789Sahrens 
3921646Sperrin int
3934944Smaybee dmu_objset_evict_dbufs(objset_t *os)
3941544Seschrock {
3951544Seschrock 	dnode_t *dn;
3961596Sahrens 
397*10298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
3981596Sahrens 
3991596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
400*10298SMatthew.Ahrens@Sun.COM 	list_remove(&os->os_dnodes, os->os_meta_dnode);
401*10298SMatthew.Ahrens@Sun.COM 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
4021544Seschrock 
4031544Seschrock 	/*
4041596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4051596Sahrens 	 * because dnode_add_ref() only works if you already have a
4061596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4071596Sahrens 	 * skip.
4081544Seschrock 	 */
409*10298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
4104944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
411*10298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
4121596Sahrens 		continue;
4131596Sahrens 
4141596Sahrens 	while (dn) {
4151596Sahrens 		dnode_t *next_dn = dn;
4161596Sahrens 
4171596Sahrens 		do {
418*10298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
4194944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4201596Sahrens 
421*10298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
4224944Smaybee 		dnode_evict_dbufs(dn);
4231596Sahrens 		dnode_rele(dn, FTAG);
424*10298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
4251596Sahrens 		dn = next_dn;
4261544Seschrock 	}
427*10298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
428*10298SMatthew.Ahrens@Sun.COM 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
4291544Seschrock }
4301544Seschrock 
4311544Seschrock void
432*10298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
433789Sahrens {
434*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
4352082Seschrock 	int i;
436789Sahrens 
437789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
438*10298SMatthew.Ahrens@Sun.COM 		ASSERT(list_head(&os->os_dirty_dnodes[i]) == NULL);
439*10298SMatthew.Ahrens@Sun.COM 		ASSERT(list_head(&os->os_free_dnodes[i]) == NULL);
440789Sahrens 	}
441789Sahrens 
4427237Sek110237 	if (ds) {
4437237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4447237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
445*10298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
4467237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
447*10298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
4487237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
449*10298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
4507237Sek110237 		}
4517237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
452*10298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
4537237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
454*10298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
455789Sahrens 	}
456789Sahrens 
4571544Seschrock 	/*
4581544Seschrock 	 * We should need only a single pass over the dnode list, since
4591544Seschrock 	 * nothing can be added to the list at this point.
4601544Seschrock 	 */
461*10298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
4621544Seschrock 
463*10298SMatthew.Ahrens@Sun.COM 	dnode_special_close(os->os_meta_dnode);
464*10298SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode) {
465*10298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_userused_dnode);
466*10298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_groupused_dnode);
4679396SMatthew.Ahrens@Sun.COM 	}
468*10298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
469789Sahrens 
470*10298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
471789Sahrens 
472*10298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
473*10298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
474*10298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
475*10298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
476*10298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
477789Sahrens }
478789Sahrens 
479789Sahrens /* called from dsl for meta-objset */
480*10298SMatthew.Ahrens@Sun.COM objset_t *
4813547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
4823547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
483789Sahrens {
484*10298SMatthew.Ahrens@Sun.COM 	objset_t *os;
485789Sahrens 	dnode_t *mdn;
486789Sahrens 
487789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
4884787Sahrens 	if (ds)
4894787Sahrens 		mutex_enter(&ds->ds_opening_lock);
490*10298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
4914787Sahrens 	if (ds)
4924787Sahrens 		mutex_exit(&ds->ds_opening_lock);
493*10298SMatthew.Ahrens@Sun.COM 	mdn = os->os_meta_dnode;
494789Sahrens 
495789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
496789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
497789Sahrens 
498789Sahrens 	/*
499789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
500789Sahrens 	 * later, because then we could do it in quescing context while
501789Sahrens 	 * we are also accessing it in open context.
502789Sahrens 	 *
503789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
504789Sahrens 	 * because the MOS is only updated in syncing context.
505789Sahrens 	 * This is most fortunate: the MOS is the only objset that
506789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
507789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
508789Sahrens 	 */
5091544Seschrock 	if (ds != NULL) {
5101544Seschrock 		int levels = 1;
5111544Seschrock 
5121544Seschrock 		/*
5131544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5141544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5151544Seschrock 		 */
5161544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5171544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5181544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5191544Seschrock 			levels++;
5201544Seschrock 
521789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5221544Seschrock 		    mdn->dn_nlevels = levels;
5231544Seschrock 	}
524789Sahrens 
525789Sahrens 	ASSERT(type != DMU_OST_NONE);
526789Sahrens 	ASSERT(type != DMU_OST_ANY);
527789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
528*10298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
529*10298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
530*10298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
531*10298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
5329396SMatthew.Ahrens@Sun.COM 	}
533789Sahrens 
534789Sahrens 	dsl_dataset_dirty(ds, tx);
535789Sahrens 
536*10298SMatthew.Ahrens@Sun.COM 	return (os);
537789Sahrens }
538789Sahrens 
539789Sahrens struct oscarg {
5404543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
541789Sahrens 	void *userarg;
54210272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
543789Sahrens 	const char *lastname;
544789Sahrens 	dmu_objset_type_t type;
5456492Stimh 	uint64_t flags;
546789Sahrens };
547789Sahrens 
5484543Smarks /*ARGSUSED*/
549789Sahrens static int
5502199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
551789Sahrens {
5522199Sahrens 	dsl_dir_t *dd = arg1;
5532199Sahrens 	struct oscarg *oa = arg2;
5542199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
5552199Sahrens 	int err;
5562199Sahrens 	uint64_t ddobj;
5572199Sahrens 
5582199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5592199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5602199Sahrens 	if (err != ENOENT)
5612199Sahrens 		return (err ? err : EEXIST);
5622199Sahrens 
56310272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
56410272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
56510272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
5662199Sahrens 			return (EXDEV);
5672199Sahrens 
56810272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
56910272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
5702199Sahrens 			return (EINVAL);
5712199Sahrens 	}
5724543Smarks 
5732199Sahrens 	return (0);
5742199Sahrens }
5752199Sahrens 
5762199Sahrens static void
5774543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
5782199Sahrens {
5792199Sahrens 	dsl_dir_t *dd = arg1;
5802199Sahrens 	struct oscarg *oa = arg2;
5812199Sahrens 	uint64_t dsobj;
582789Sahrens 
583789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
584789Sahrens 
5852199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
58610272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
587789Sahrens 
58810272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
58910272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
59010272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
591*10298SMatthew.Ahrens@Sun.COM 		objset_t *os;
592789Sahrens 
59310272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
59410272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
59510272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
59610272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
59710272SMatthew.Ahrens@Sun.COM 
598*10298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
5993547Smaybee 		    ds, bp, oa->type, tx);
600789Sahrens 
601789Sahrens 		if (oa->userfunc)
602*10298SMatthew.Ahrens@Sun.COM 			oa->userfunc(os, oa->userarg, cr, tx);
60310272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
604789Sahrens 	}
6054543Smarks 
6064543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6074543Smarks 	    tx, cr, "dataset = %llu", dsobj);
608789Sahrens }
609789Sahrens 
610789Sahrens int
61110272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6124543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
613789Sahrens {
6142199Sahrens 	dsl_dir_t *pdd;
615789Sahrens 	const char *tail;
616789Sahrens 	int err = 0;
6172199Sahrens 	struct oscarg oa = { 0 };
618789Sahrens 
6192199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6202199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6211544Seschrock 	if (err)
6221544Seschrock 		return (err);
623789Sahrens 	if (tail == NULL) {
6242199Sahrens 		dsl_dir_close(pdd, FTAG);
625789Sahrens 		return (EEXIST);
626789Sahrens 	}
627789Sahrens 
6282199Sahrens 	oa.userfunc = func;
6292199Sahrens 	oa.userarg = arg;
6302199Sahrens 	oa.lastname = tail;
6312199Sahrens 	oa.type = type;
6326492Stimh 	oa.flags = flags;
6334543Smarks 
63410272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
63510272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
63610272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
63710272SMatthew.Ahrens@Sun.COM 	return (err);
63810272SMatthew.Ahrens@Sun.COM }
63910272SMatthew.Ahrens@Sun.COM 
64010272SMatthew.Ahrens@Sun.COM int
64110272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
64210272SMatthew.Ahrens@Sun.COM {
64310272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
64410272SMatthew.Ahrens@Sun.COM 	const char *tail;
64510272SMatthew.Ahrens@Sun.COM 	int err = 0;
64610272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
64710272SMatthew.Ahrens@Sun.COM 
64810272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
64910272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
65010272SMatthew.Ahrens@Sun.COM 	if (err)
65110272SMatthew.Ahrens@Sun.COM 		return (err);
65210272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
65310272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
65410272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
655789Sahrens 	}
65610272SMatthew.Ahrens@Sun.COM 
65710272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
65810272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
65910272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
66010272SMatthew.Ahrens@Sun.COM 
6612199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6622199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6632199Sahrens 	dsl_dir_close(pdd, FTAG);
664789Sahrens 	return (err);
665789Sahrens }
666789Sahrens 
667789Sahrens int
66810242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
669789Sahrens {
670*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
671789Sahrens 	int error;
672789Sahrens 
673789Sahrens 	/*
67410272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
67510272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
67610272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
67710272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
67810272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
679789Sahrens 	 */
680*10298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
681789Sahrens 	if (error == 0) {
682*10298SMatthew.Ahrens@Sun.COM 		objset_t *os;
683*10298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
684*10298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
685*10298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
68610272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
687789Sahrens 	}
688789Sahrens 
6895367Sahrens 	return (error);
690789Sahrens }
691789Sahrens 
6922199Sahrens struct snaparg {
6932199Sahrens 	dsl_sync_task_group_t *dstg;
6942199Sahrens 	char *snapname;
6952199Sahrens 	char failed[MAXPATHLEN];
6964543Smarks 	boolean_t checkperms;
6979355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
6985367Sahrens };
6995367Sahrens 
7009355SMatthew.Ahrens@Sun.COM static int
7019355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7029355SMatthew.Ahrens@Sun.COM {
7039355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7049355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7059355SMatthew.Ahrens@Sun.COM 
7069355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7079355SMatthew.Ahrens@Sun.COM 
708*10298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
7099355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7109355SMatthew.Ahrens@Sun.COM }
7119355SMatthew.Ahrens@Sun.COM 
7129355SMatthew.Ahrens@Sun.COM static void
7139355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7149355SMatthew.Ahrens@Sun.COM {
7159355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
716*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
7179355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7189355SMatthew.Ahrens@Sun.COM 
7199355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7209355SMatthew.Ahrens@Sun.COM 
7219355SMatthew.Ahrens@Sun.COM 	if (sn->props)
7229355SMatthew.Ahrens@Sun.COM 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
7239355SMatthew.Ahrens@Sun.COM }
7242199Sahrens 
7252199Sahrens static int
7262199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7272199Sahrens {
7282199Sahrens 	struct snaparg *sn = arg;
7292199Sahrens 	objset_t *os;
7302199Sahrens 	int err;
7312199Sahrens 
7322199Sahrens 	(void) strcpy(sn->failed, name);
7332199Sahrens 
7344543Smarks 	/*
7354543Smarks 	 * Check permissions only when requested.  This only applies when
7364543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
7374543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
7384543Smarks 	 */
7394543Smarks 	if (sn->checkperms == B_TRUE &&
7404543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
7414543Smarks 		return (err);
7424543Smarks 
743*10298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
7442199Sahrens 	if (err != 0)
7452199Sahrens 		return (err);
7462199Sahrens 
7476689Smaybee 	/* If the objset is in an inconsistent state, return busy */
748*10298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
749*10298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
7503637Srm160521 		return (EBUSY);
7513637Srm160521 	}
7523637Srm160521 
7533637Srm160521 	/*
7542199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7552199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
7562199Sahrens 	 * a side effect.
7572199Sahrens 	 */
7582199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7592199Sahrens 	if (err == 0) {
7609355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
7619355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
7623637Srm160521 	} else {
763*10298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
7642199Sahrens 	}
7653637Srm160521 
7662199Sahrens 	return (err);
7672199Sahrens }
7682199Sahrens 
7692199Sahrens int
7709355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
7719355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
7722199Sahrens {
7732199Sahrens 	dsl_sync_task_t *dst;
7749355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
7752199Sahrens 	spa_t *spa;
7762199Sahrens 	int err;
7772199Sahrens 
7782199Sahrens 	(void) strcpy(sn.failed, fsname);
7792199Sahrens 
7804603Sahrens 	err = spa_open(fsname, &spa, FTAG);
7812199Sahrens 	if (err)
7822199Sahrens 		return (err);
7832199Sahrens 
7842199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
7852199Sahrens 	sn.snapname = snapname;
7869355SMatthew.Ahrens@Sun.COM 	sn.props = props;
7872199Sahrens 
7882417Sahrens 	if (recursive) {
7894543Smarks 		sn.checkperms = B_TRUE;
7902417Sahrens 		err = dmu_objset_find(fsname,
7912417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
7922417Sahrens 	} else {
7934543Smarks 		sn.checkperms = B_FALSE;
7942199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
7952417Sahrens 	}
7962199Sahrens 
7979355SMatthew.Ahrens@Sun.COM 	if (err == 0)
7989355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
7992199Sahrens 
8002199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8012199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8029355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
803*10298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
8042199Sahrens 		if (dst->dst_err)
8055367Sahrens 			dsl_dataset_name(ds, sn.failed);
8069355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
807*10298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
8082199Sahrens 	}
8095367Sahrens 
8102199Sahrens 	if (err)
8112199Sahrens 		(void) strcpy(fsname, sn.failed);
8122199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8132199Sahrens 	spa_close(spa, FTAG);
8142199Sahrens 	return (err);
8152199Sahrens }
8162199Sahrens 
817789Sahrens static void
8189396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
819789Sahrens {
8203547Smaybee 	dnode_t *dn;
821789Sahrens 
8223547Smaybee 	while (dn = list_head(list)) {
8233547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8243547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8253547Smaybee 		/*
8269396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
8279396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
8283547Smaybee 		 */
8293547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
8303547Smaybee 		ASSERT(dn->dn_zio);
831789Sahrens 
8323547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
8333547Smaybee 		list_remove(list, dn);
8349396SMatthew.Ahrens@Sun.COM 
8359396SMatthew.Ahrens@Sun.COM 		if (newlist) {
8369396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
8379396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
8389396SMatthew.Ahrens@Sun.COM 		}
8399396SMatthew.Ahrens@Sun.COM 
8403547Smaybee 		dnode_sync(dn, tx);
8413547Smaybee 	}
8423547Smaybee }
8432981Sahrens 
8443547Smaybee /* ARGSUSED */
8453547Smaybee static void
8463547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
8473547Smaybee {
8487754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
8497754SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
850*10298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
8513547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8522981Sahrens 
8537754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
8547754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
8557754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
8565329Sgw25295 
8573547Smaybee 	/*
8589396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
8599396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
8609396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
8619396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
8623547Smaybee 	 */
8639396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
8647754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
8653547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8665329Sgw25295 
8677754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
8687754SJeff.Bonwick@Sun.COM 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
8697754SJeff.Bonwick@Sun.COM 	} else {
8705329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
8716992Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
8727754SJeff.Bonwick@Sun.COM 			    &zio->io_bp_orig, zio, os->os_synctx);
8735329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
8745329Sgw25295 	}
875789Sahrens }
876789Sahrens 
877789Sahrens /* called from dsl */
878789Sahrens void
879*10298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
880789Sahrens {
881789Sahrens 	int txgoff;
8821544Seschrock 	zbookmark_t zb;
8837046Sahrens 	writeprops_t wp = { 0 };
8843547Smaybee 	zio_t *zio;
8853547Smaybee 	list_t *list;
8869396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
8873547Smaybee 	dbuf_dirty_record_t *dr;
8883547Smaybee 
8893547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
890789Sahrens 
891789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
892789Sahrens 	/* XXX the write_done callback should really give us the tx... */
893789Sahrens 	os->os_synctx = tx;
894789Sahrens 
8953882Sahrens 	if (os->os_dsl_dataset == NULL) {
8963882Sahrens 		/*
8973882Sahrens 		 * This is the MOS.  If we have upgraded,
8983882Sahrens 		 * spa_max_replication() could change, so reset
8993882Sahrens 		 * os_copies here.
9003882Sahrens 		 */
9013882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9023882Sahrens 	}
9033882Sahrens 
9043547Smaybee 	/*
9053547Smaybee 	 * Create the root block IO
9063547Smaybee 	 */
9073547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
9083547Smaybee 	zb.zb_object = 0;
9097754SJeff.Bonwick@Sun.COM 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
9103547Smaybee 	zb.zb_blkid = 0;
9117754SJeff.Bonwick@Sun.COM 
9127754SJeff.Bonwick@Sun.COM 	wp.wp_type = DMU_OT_OBJSET;
9137754SJeff.Bonwick@Sun.COM 	wp.wp_level = 0;	/* on-disk BP level; see above */
9147754SJeff.Bonwick@Sun.COM 	wp.wp_copies = os->os_copies;
9157754SJeff.Bonwick@Sun.COM 	wp.wp_oschecksum = os->os_checksum;
9167754SJeff.Bonwick@Sun.COM 	wp.wp_oscompress = os->os_compress;
9177754SJeff.Bonwick@Sun.COM 
9184787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
9196992Smaybee 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9203547Smaybee 		    os->os_rootbp, pio, tx);
9214787Sahrens 	}
9227754SJeff.Bonwick@Sun.COM 
9237046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9249396SMatthew.Ahrens@Sun.COM 
9257754SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
9267754SJeff.Bonwick@Sun.COM 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
9277754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
9283547Smaybee 
9293547Smaybee 	/*
9309396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
9313547Smaybee 	 */
9323547Smaybee 	os->os_meta_dnode->dn_zio = zio;
9333547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
934789Sahrens 
9359396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
9369396SMatthew.Ahrens@Sun.COM 
9379396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
9389396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
9399396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
9409396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
9419396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
9429396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
9439396SMatthew.Ahrens@Sun.COM 	}
9449396SMatthew.Ahrens@Sun.COM 
945789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
946789Sahrens 
9479396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
9489396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
9499396SMatthew.Ahrens@Sun.COM 		/*
9509396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
9519396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
9529396SMatthew.Ahrens@Sun.COM 		 */
9539396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
9549396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
9559396SMatthew.Ahrens@Sun.COM 	}
9569396SMatthew.Ahrens@Sun.COM 
9579396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
9589396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
959789Sahrens 
9603547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
9613547Smaybee 	while (dr = list_head(list)) {
9623547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
9633547Smaybee 		list_remove(list, dr);
9643547Smaybee 		if (dr->dr_zio)
9653547Smaybee 			zio_nowait(dr->dr_zio);
9663547Smaybee 	}
967789Sahrens 	/*
968789Sahrens 	 * Free intent log blocks up to this tx.
969789Sahrens 	 */
970789Sahrens 	zil_sync(os->os_zil, tx);
9717046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
9723547Smaybee 	zio_nowait(zio);
973789Sahrens }
974789Sahrens 
9759396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
9769396SMatthew.Ahrens@Sun.COM 
9779396SMatthew.Ahrens@Sun.COM void
9789396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
9799396SMatthew.Ahrens@Sun.COM {
9809396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
9819396SMatthew.Ahrens@Sun.COM }
9829396SMatthew.Ahrens@Sun.COM 
9839396SMatthew.Ahrens@Sun.COM boolean_t
984*10298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
9859396SMatthew.Ahrens@Sun.COM {
9869396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
9879396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
9889396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
9899396SMatthew.Ahrens@Sun.COM }
9909396SMatthew.Ahrens@Sun.COM 
9919396SMatthew.Ahrens@Sun.COM void
992*10298SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
9939396SMatthew.Ahrens@Sun.COM {
9949396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
9959396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
9969396SMatthew.Ahrens@Sun.COM 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
9979396SMatthew.Ahrens@Sun.COM 
9989396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
9999396SMatthew.Ahrens@Sun.COM 
10009396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
10019396SMatthew.Ahrens@Sun.COM 		dmu_object_type_t bonustype;
10029396SMatthew.Ahrens@Sun.COM 
10039396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
10049396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys);
10059396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
10069396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
10079396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
10089396SMatthew.Ahrens@Sun.COM 
10099396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
10109396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1011*10298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
10129396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
10139396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1014*10298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
10159396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
10169396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
10179396SMatthew.Ahrens@Sun.COM 		}
10189396SMatthew.Ahrens@Sun.COM 
10199396SMatthew.Ahrens@Sun.COM 		/*
10209396SMatthew.Ahrens@Sun.COM 		 * If the object was not previously
10219396SMatthew.Ahrens@Sun.COM 		 * accounted, pretend that it was free.
10229396SMatthew.Ahrens@Sun.COM 		 */
10239396SMatthew.Ahrens@Sun.COM 		if (!(dn->dn_oldphys->dn_flags &
10249396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED)) {
10259396SMatthew.Ahrens@Sun.COM 			bzero(dn->dn_oldphys, sizeof (dnode_phys_t));
10269396SMatthew.Ahrens@Sun.COM 		}
10279396SMatthew.Ahrens@Sun.COM 
10289396SMatthew.Ahrens@Sun.COM 		/*
10299396SMatthew.Ahrens@Sun.COM 		 * If the object was freed, use the previous bonustype.
10309396SMatthew.Ahrens@Sun.COM 		 */
10319396SMatthew.Ahrens@Sun.COM 		bonustype = dn->dn_phys->dn_bonustype ?
10329396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype;
10339396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type != 0 ||
10349396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_phys), zerobuf,
10359396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
10369396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys) == 0));
10379396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys->dn_type != 0 ||
10389396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf,
10399396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
10409396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys) == 0));
1041*10298SMatthew.Ahrens@Sun.COM 		used_cbs[os->os_phys->os_type](os, bonustype,
10429396SMatthew.Ahrens@Sun.COM 		    DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys),
10439396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys),
10449396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys), tx);
10459396SMatthew.Ahrens@Sun.COM 
10469396SMatthew.Ahrens@Sun.COM 		/*
10479396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
10489396SMatthew.Ahrens@Sun.COM 		 */
10499396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
10509396SMatthew.Ahrens@Sun.COM 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
10519396SMatthew.Ahrens@Sun.COM 		dn->dn_oldphys = NULL;
10529396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
10539396SMatthew.Ahrens@Sun.COM 
10549396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
10559396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
10569396SMatthew.Ahrens@Sun.COM 	}
10579396SMatthew.Ahrens@Sun.COM }
10589396SMatthew.Ahrens@Sun.COM 
10599396SMatthew.Ahrens@Sun.COM boolean_t
10609396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
10619396SMatthew.Ahrens@Sun.COM {
1062*10298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
10639396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
10649396SMatthew.Ahrens@Sun.COM }
10659396SMatthew.Ahrens@Sun.COM 
10669396SMatthew.Ahrens@Sun.COM int
10679396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
10689396SMatthew.Ahrens@Sun.COM {
10699396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
10709396SMatthew.Ahrens@Sun.COM 	int err = 0;
10719396SMatthew.Ahrens@Sun.COM 
10729396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
10739396SMatthew.Ahrens@Sun.COM 		return (0);
1074*10298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
10759396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
10769396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
10779396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
10789396SMatthew.Ahrens@Sun.COM 
10799396SMatthew.Ahrens@Sun.COM 	/*
10809396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
10819396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
10829396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
10839396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
10849396SMatthew.Ahrens@Sun.COM 	 * independently.
10859396SMatthew.Ahrens@Sun.COM 	 */
10869396SMatthew.Ahrens@Sun.COM 
10879396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
10889951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
10899396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
10909396SMatthew.Ahrens@Sun.COM 		int objerr;
10919396SMatthew.Ahrens@Sun.COM 
10929396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
10939396SMatthew.Ahrens@Sun.COM 			return (EINTR);
10949396SMatthew.Ahrens@Sun.COM 
10959396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
10969396SMatthew.Ahrens@Sun.COM 		if (objerr)
10979396SMatthew.Ahrens@Sun.COM 			continue;
10989951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
10999396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
11009396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
11019396SMatthew.Ahrens@Sun.COM 		if (objerr) {
11029396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
11039396SMatthew.Ahrens@Sun.COM 			continue;
11049396SMatthew.Ahrens@Sun.COM 		}
11059396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
11069396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
11079396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
11089396SMatthew.Ahrens@Sun.COM 	}
11099396SMatthew.Ahrens@Sun.COM 
1110*10298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
11119396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
11129396SMatthew.Ahrens@Sun.COM 	return (0);
11139396SMatthew.Ahrens@Sun.COM }
11149396SMatthew.Ahrens@Sun.COM 
1115789Sahrens void
11162885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
11172885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
11182885Sahrens {
1119*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
11202885Sahrens 	    usedobjsp, availobjsp);
11212885Sahrens }
11222885Sahrens 
11232885Sahrens uint64_t
11242885Sahrens dmu_objset_fsid_guid(objset_t *os)
11252885Sahrens {
1126*10298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
11272885Sahrens }
11282885Sahrens 
11292885Sahrens void
11302885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1131789Sahrens {
1132*10298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
1133*10298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
1134*10298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
11352885Sahrens }
11362885Sahrens 
11372885Sahrens void
11382885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
11392885Sahrens {
1140*10298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
1141*10298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
11422885Sahrens 
1143*10298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
1144*10298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
11452885Sahrens 
11462885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1147*10298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
11489396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
11499396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1150789Sahrens }
1151789Sahrens 
1152789Sahrens int
1153789Sahrens dmu_objset_is_snapshot(objset_t *os)
1154789Sahrens {
1155*10298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
1156*10298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1157789Sahrens 	else
1158789Sahrens 		return (B_FALSE);
1159789Sahrens }
1160789Sahrens 
1161789Sahrens int
11626492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
11636492Stimh     boolean_t *conflict)
11646492Stimh {
1165*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
11666492Stimh 	uint64_t ignored;
11676492Stimh 
11686492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
11696492Stimh 		return (ENOENT);
11706492Stimh 
11716492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
11726492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
11736492Stimh 	    real, maxlen, conflict));
11746492Stimh }
11756492Stimh 
11766492Stimh int
1177789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
11785663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1179789Sahrens {
1180*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1181789Sahrens 	zap_cursor_t cursor;
1182789Sahrens 	zap_attribute_t attr;
1183789Sahrens 
1184789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1185789Sahrens 		return (ENOENT);
1186789Sahrens 
1187789Sahrens 	zap_cursor_init_serialized(&cursor,
1188789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1189789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1190789Sahrens 
1191885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1192885Sahrens 		zap_cursor_fini(&cursor);
1193885Sahrens 		return (ENOENT);
1194885Sahrens 	}
1195885Sahrens 
1196885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1197885Sahrens 		zap_cursor_fini(&cursor);
1198885Sahrens 		return (ENAMETOOLONG);
1199885Sahrens 	}
1200885Sahrens 
1201885Sahrens 	(void) strcpy(name, attr.za_name);
1202885Sahrens 	if (idp)
1203885Sahrens 		*idp = attr.za_first_integer;
12045663Sck153898 	if (case_conflict)
12055663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1206885Sahrens 	zap_cursor_advance(&cursor);
1207885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1208885Sahrens 	zap_cursor_fini(&cursor);
1209885Sahrens 
1210885Sahrens 	return (0);
1211885Sahrens }
1212885Sahrens 
1213885Sahrens int
1214885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1215885Sahrens     uint64_t *idp, uint64_t *offp)
1216885Sahrens {
1217*10298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1218885Sahrens 	zap_cursor_t cursor;
1219885Sahrens 	zap_attribute_t attr;
1220885Sahrens 
1221885Sahrens 	/* there is no next dir on a snapshot! */
1222*10298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1223885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1224885Sahrens 		return (ENOENT);
1225885Sahrens 
1226885Sahrens 	zap_cursor_init_serialized(&cursor,
1227885Sahrens 	    dd->dd_pool->dp_meta_objset,
1228885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1229885Sahrens 
1230885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1231885Sahrens 		zap_cursor_fini(&cursor);
1232885Sahrens 		return (ENOENT);
1233885Sahrens 	}
1234885Sahrens 
1235885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1236885Sahrens 		zap_cursor_fini(&cursor);
1237789Sahrens 		return (ENAMETOOLONG);
1238885Sahrens 	}
1239789Sahrens 
1240789Sahrens 	(void) strcpy(name, attr.za_name);
1241885Sahrens 	if (idp)
1242885Sahrens 		*idp = attr.za_first_integer;
1243789Sahrens 	zap_cursor_advance(&cursor);
1244789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1245885Sahrens 	zap_cursor_fini(&cursor);
1246789Sahrens 
1247789Sahrens 	return (0);
1248789Sahrens }
1249789Sahrens 
12507046Sahrens struct findarg {
12517046Sahrens 	int (*func)(char *, void *);
12527046Sahrens 	void *arg;
12537046Sahrens };
12547046Sahrens 
12557046Sahrens /* ARGSUSED */
12567046Sahrens static int
12577046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
12587046Sahrens {
12597046Sahrens 	struct findarg *fa = arg;
12607046Sahrens 	return (fa->func((char *)dsname, fa->arg));
12617046Sahrens }
12627046Sahrens 
1263789Sahrens /*
1264789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
12657046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1266789Sahrens  */
12672199Sahrens int
12682199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1269789Sahrens {
12707046Sahrens 	struct findarg fa;
12717046Sahrens 	fa.func = func;
12727046Sahrens 	fa.arg = arg;
12737046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
12747046Sahrens }
12757046Sahrens 
12767046Sahrens /*
12777046Sahrens  * Find all objsets under name, call func on each
12787046Sahrens  */
12797046Sahrens int
12807046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
12817046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
12827046Sahrens {
1283789Sahrens 	dsl_dir_t *dd;
12847046Sahrens 	dsl_pool_t *dp;
12857046Sahrens 	dsl_dataset_t *ds;
1286789Sahrens 	zap_cursor_t zc;
12873978Smmusante 	zap_attribute_t *attr;
1288789Sahrens 	char *child;
12897046Sahrens 	uint64_t thisobj;
12907046Sahrens 	int err;
1291789Sahrens 
12927046Sahrens 	if (name == NULL)
12937046Sahrens 		name = spa_name(spa);
12947046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
12951544Seschrock 	if (err)
12962199Sahrens 		return (err);
1297789Sahrens 
12987046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
12997046Sahrens 	if (dd->dd_myname[0] == '$') {
13007046Sahrens 		dsl_dir_close(dd, FTAG);
13017046Sahrens 		return (0);
13027046Sahrens 	}
13037046Sahrens 
13047046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
13053978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13067046Sahrens 	dp = dd->dd_pool;
1307789Sahrens 
1308789Sahrens 	/*
1309789Sahrens 	 * Iterate over all children.
1310789Sahrens 	 */
13112417Sahrens 	if (flags & DS_FIND_CHILDREN) {
13127046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13132417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
13143978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13152417Sahrens 		    (void) zap_cursor_advance(&zc)) {
13163978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
13173978Smmusante 			ASSERT(attr->za_num_integers == 1);
1318789Sahrens 
13192417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13207046Sahrens 			(void) strcpy(child, name);
13212417Sahrens 			(void) strcat(child, "/");
13223978Smmusante 			(void) strcat(child, attr->za_name);
13237046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13242417Sahrens 			kmem_free(child, MAXPATHLEN);
13252417Sahrens 			if (err)
13262417Sahrens 				break;
13272417Sahrens 		}
13282417Sahrens 		zap_cursor_fini(&zc);
13292199Sahrens 
13302417Sahrens 		if (err) {
13312417Sahrens 			dsl_dir_close(dd, FTAG);
13323978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13332417Sahrens 			return (err);
13342417Sahrens 		}
1335789Sahrens 	}
1336789Sahrens 
1337789Sahrens 	/*
1338789Sahrens 	 * Iterate over all snapshots.
1339789Sahrens 	 */
13407046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
13417046Sahrens 		if (!dsl_pool_sync_context(dp))
13427046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
13437046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
13447046Sahrens 		if (!dsl_pool_sync_context(dp))
13457046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1346789Sahrens 
13477046Sahrens 		if (err == 0) {
13487046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
13497046Sahrens 			dsl_dataset_rele(ds, FTAG);
1350789Sahrens 
13517046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
13527046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
13537046Sahrens 			    (void) zap_cursor_advance(&zc)) {
13547046Sahrens 				ASSERT(attr->za_integer_length ==
13557046Sahrens 				    sizeof (uint64_t));
13567046Sahrens 				ASSERT(attr->za_num_integers == 1);
1357789Sahrens 
13587046Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13597046Sahrens 				(void) strcpy(child, name);
13607046Sahrens 				(void) strcat(child, "@");
13617046Sahrens 				(void) strcat(child, attr->za_name);
13627046Sahrens 				err = func(spa, attr->za_first_integer,
13637046Sahrens 				    child, arg);
13647046Sahrens 				kmem_free(child, MAXPATHLEN);
13657046Sahrens 				if (err)
13667046Sahrens 					break;
13677046Sahrens 			}
13687046Sahrens 			zap_cursor_fini(&zc);
1369789Sahrens 		}
1370789Sahrens 	}
1371789Sahrens 
1372789Sahrens 	dsl_dir_close(dd, FTAG);
13733978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1374789Sahrens 
13752199Sahrens 	if (err)
13762199Sahrens 		return (err);
13772199Sahrens 
1378789Sahrens 	/*
1379789Sahrens 	 * Apply to self if appropriate.
1380789Sahrens 	 */
13817046Sahrens 	err = func(spa, thisobj, name, arg);
13822199Sahrens 	return (err);
1383789Sahrens }
13845326Sek110237 
13858415SRichard.Morris@Sun.COM /* ARGSUSED */
13868415SRichard.Morris@Sun.COM int
13878415SRichard.Morris@Sun.COM dmu_objset_prefetch(char *name, void *arg)
13888415SRichard.Morris@Sun.COM {
13898415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
13908415SRichard.Morris@Sun.COM 
13918415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
13928415SRichard.Morris@Sun.COM 		return (0);
13938415SRichard.Morris@Sun.COM 
13948415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
13958415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
1396*10298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
13978415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
13988415SRichard.Morris@Sun.COM 			zbookmark_t zb;
13998415SRichard.Morris@Sun.COM 
14008415SRichard.Morris@Sun.COM 			zb.zb_objset = ds->ds_object;
14018415SRichard.Morris@Sun.COM 			zb.zb_object = 0;
14028415SRichard.Morris@Sun.COM 			zb.zb_level = -1;
14038415SRichard.Morris@Sun.COM 			zb.zb_blkid = 0;
14048415SRichard.Morris@Sun.COM 
14058415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14068415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
14078415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
14088415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14098415SRichard.Morris@Sun.COM 			    &aflags, &zb);
14108415SRichard.Morris@Sun.COM 		}
14118415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
14128415SRichard.Morris@Sun.COM 	}
14138415SRichard.Morris@Sun.COM 
14148415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
14158415SRichard.Morris@Sun.COM 	return (0);
14168415SRichard.Morris@Sun.COM }
14178415SRichard.Morris@Sun.COM 
14185326Sek110237 void
14195326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
14205326Sek110237 {
1421*10298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1422*10298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
14235326Sek110237 }
14245326Sek110237 
14255326Sek110237 void *
14265326Sek110237 dmu_objset_get_user(objset_t *os)
14275326Sek110237 {
1428*10298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1429*10298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
14305326Sek110237 }
1431