xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 12294:2a74b443e6b1)
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 /*
2212178SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25*12294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
26*12294SMark.Musante@Sun.COM 
274543Smarks #include <sys/cred.h>
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/dmu_objset.h>
30789Sahrens #include <sys/dsl_dir.h>
31789Sahrens #include <sys/dsl_dataset.h>
32789Sahrens #include <sys/dsl_prop.h>
33789Sahrens #include <sys/dsl_pool.h>
342199Sahrens #include <sys/dsl_synctask.h>
354543Smarks #include <sys/dsl_deleg.h>
36789Sahrens #include <sys/dnode.h>
37789Sahrens #include <sys/dbuf.h>
382885Sahrens #include <sys/zvol.h>
39789Sahrens #include <sys/dmu_tx.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
42789Sahrens #include <sys/dmu_impl.h>
434543Smarks #include <sys/zfs_ioctl.h>
4411165SMatthew.Ahrens@Sun.COM #include <sys/sunddi.h>
4511935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
46789Sahrens 
47789Sahrens spa_t *
48789Sahrens dmu_objset_spa(objset_t *os)
49789Sahrens {
5010298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
51789Sahrens }
52789Sahrens 
53789Sahrens zilog_t *
54789Sahrens dmu_objset_zil(objset_t *os)
55789Sahrens {
5610298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
57789Sahrens }
58789Sahrens 
59789Sahrens dsl_pool_t *
60789Sahrens dmu_objset_pool(objset_t *os)
61789Sahrens {
62789Sahrens 	dsl_dataset_t *ds;
63789Sahrens 
6410298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
65789Sahrens 		return (ds->ds_dir->dd_pool);
66789Sahrens 	else
6710298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
68789Sahrens }
69789Sahrens 
70789Sahrens dsl_dataset_t *
71789Sahrens dmu_objset_ds(objset_t *os)
72789Sahrens {
7310298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
74789Sahrens }
75789Sahrens 
76789Sahrens dmu_objset_type_t
77789Sahrens dmu_objset_type(objset_t *os)
78789Sahrens {
7910298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
80789Sahrens }
81789Sahrens 
82789Sahrens void
83789Sahrens dmu_objset_name(objset_t *os, char *buf)
84789Sahrens {
8510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
86789Sahrens }
87789Sahrens 
88789Sahrens uint64_t
89789Sahrens dmu_objset_id(objset_t *os)
90789Sahrens {
9110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
92789Sahrens 
93789Sahrens 	return (ds ? ds->ds_object : 0);
94789Sahrens }
95789Sahrens 
9610310SNeil.Perrin@Sun.COM uint64_t
97*12294SMark.Musante@Sun.COM dmu_objset_syncprop(objset_t *os)
98*12294SMark.Musante@Sun.COM {
99*12294SMark.Musante@Sun.COM 	return (os->os_sync);
100*12294SMark.Musante@Sun.COM }
101*12294SMark.Musante@Sun.COM 
102*12294SMark.Musante@Sun.COM uint64_t
10310310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
10410310SNeil.Perrin@Sun.COM {
10510310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
10610310SNeil.Perrin@Sun.COM }
10710310SNeil.Perrin@Sun.COM 
108789Sahrens static void
109789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
110789Sahrens {
11110298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
112789Sahrens 
113789Sahrens 	/*
114789Sahrens 	 * Inheritance should have been done by now.
115789Sahrens 	 */
116789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
117789Sahrens 
11810298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
119789Sahrens }
120789Sahrens 
121789Sahrens static void
122789Sahrens compression_changed_cb(void *arg, uint64_t newval)
123789Sahrens {
12410298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
125789Sahrens 
126789Sahrens 	/*
127789Sahrens 	 * Inheritance and range checking should have been done by now.
128789Sahrens 	 */
129789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
130789Sahrens 
13110298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
132789Sahrens }
133789Sahrens 
1343835Sahrens static void
1353835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1363835Sahrens {
13710298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1383835Sahrens 
1393835Sahrens 	/*
1403835Sahrens 	 * Inheritance and range checking should have been done by now.
1413835Sahrens 	 */
1423835Sahrens 	ASSERT(newval > 0);
14310298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1443835Sahrens 
14510298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1463835Sahrens }
1473835Sahrens 
1487237Sek110237 static void
14910922SJeff.Bonwick@Sun.COM dedup_changed_cb(void *arg, uint64_t newval)
15010922SJeff.Bonwick@Sun.COM {
15110922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
15210922SJeff.Bonwick@Sun.COM 	spa_t *spa = os->os_spa;
15310922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum;
15410922SJeff.Bonwick@Sun.COM 
15510922SJeff.Bonwick@Sun.COM 	/*
15610922SJeff.Bonwick@Sun.COM 	 * Inheritance should have been done by now.
15710922SJeff.Bonwick@Sun.COM 	 */
15810922SJeff.Bonwick@Sun.COM 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
15910922SJeff.Bonwick@Sun.COM 
16010922SJeff.Bonwick@Sun.COM 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
16110922SJeff.Bonwick@Sun.COM 
16210922SJeff.Bonwick@Sun.COM 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
16310922SJeff.Bonwick@Sun.COM 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
16410922SJeff.Bonwick@Sun.COM }
16510922SJeff.Bonwick@Sun.COM 
16610922SJeff.Bonwick@Sun.COM static void
1677237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1687237Sek110237 {
16910298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1707237Sek110237 
1717237Sek110237 	/*
1727237Sek110237 	 * Inheritance and range checking should have been done by now.
1737237Sek110237 	 */
1747237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1757237Sek110237 	    newval == ZFS_CACHE_METADATA);
1767237Sek110237 
17710298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1787237Sek110237 }
1797237Sek110237 
1807237Sek110237 static void
1817237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1827237Sek110237 {
18310298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1847237Sek110237 
1857237Sek110237 	/*
1867237Sek110237 	 * Inheritance and range checking should have been done by now.
1877237Sek110237 	 */
1887237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1897237Sek110237 	    newval == ZFS_CACHE_METADATA);
1907237Sek110237 
19110298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
1927237Sek110237 }
1937237Sek110237 
19410310SNeil.Perrin@Sun.COM static void
195*12294SMark.Musante@Sun.COM sync_changed_cb(void *arg, uint64_t newval)
196*12294SMark.Musante@Sun.COM {
197*12294SMark.Musante@Sun.COM 	objset_t *os = arg;
198*12294SMark.Musante@Sun.COM 
199*12294SMark.Musante@Sun.COM 	/*
200*12294SMark.Musante@Sun.COM 	 * Inheritance and range checking should have been done by now.
201*12294SMark.Musante@Sun.COM 	 */
202*12294SMark.Musante@Sun.COM 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
203*12294SMark.Musante@Sun.COM 	    newval == ZFS_SYNC_DISABLED);
204*12294SMark.Musante@Sun.COM 
205*12294SMark.Musante@Sun.COM 	os->os_sync = newval;
206*12294SMark.Musante@Sun.COM 	if (os->os_zil)
207*12294SMark.Musante@Sun.COM 		zil_set_sync(os->os_zil, newval);
208*12294SMark.Musante@Sun.COM }
209*12294SMark.Musante@Sun.COM 
210*12294SMark.Musante@Sun.COM static void
21110310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
21210310SNeil.Perrin@Sun.COM {
21310310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
21410310SNeil.Perrin@Sun.COM 
21510310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
21610310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
21710310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
21810310SNeil.Perrin@Sun.COM 	if (os->os_zil)
21910310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
22010310SNeil.Perrin@Sun.COM }
22110310SNeil.Perrin@Sun.COM 
222789Sahrens void
223789Sahrens dmu_objset_byteswap(void *buf, size_t size)
224789Sahrens {
225789Sahrens 	objset_phys_t *osp = buf;
226789Sahrens 
2279396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
228789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
229789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
230789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
2319396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
2329396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
2339396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
2349396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
2359396SMatthew.Ahrens@Sun.COM 	}
236789Sahrens }
237789Sahrens 
2381544Seschrock int
2391544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
24010298SMatthew.Ahrens@Sun.COM     objset_t **osp)
241789Sahrens {
24210298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2437046Sahrens 	int i, err;
244789Sahrens 
2454787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2464787Sahrens 
24710298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
24810298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
24910298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
25010298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
25110298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2522391Smaybee 		uint32_t aflags = ARC_WAIT;
2531544Seschrock 		zbookmark_t zb;
25410922SJeff.Bonwick@Sun.COM 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
25510922SJeff.Bonwick@Sun.COM 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
25610922SJeff.Bonwick@Sun.COM 
25710298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2587237Sek110237 			aflags |= ARC_L2CACHE;
2591544Seschrock 
26010298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2617046Sahrens 		/*
2627046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2637046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2647046Sahrens 		 * could change, and we'll need a lock.
2657046Sahrens 		 */
26610298SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
26710298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2682391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2691544Seschrock 		if (err) {
27010298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2717294Sperrin 			/* convert checksum errors into IO errors */
2727294Sperrin 			if (err == ECKSUM)
2737294Sperrin 				err = EIO;
2741544Seschrock 			return (err);
2751544Seschrock 		}
2769396SMatthew.Ahrens@Sun.COM 
2779396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2789396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
27910298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2809396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
28110298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2829396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2839396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
28410298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
28510298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
28610298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
28710298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
28810298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
2899396SMatthew.Ahrens@Sun.COM 		}
2909396SMatthew.Ahrens@Sun.COM 
29110298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
29210298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
293789Sahrens 	} else {
2949396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2959396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
29610298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
29710298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
29810298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
29910298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
300789Sahrens 	}
301789Sahrens 
302789Sahrens 	/*
303789Sahrens 	 * Note: the changed_cb will be called once before the register
304789Sahrens 	 * func returns, thus changing the checksum/compression from the
3057237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
3067237Sek110237 	 * checksum/compression/copies.
307789Sahrens 	 */
3087237Sek110237 	if (ds) {
3097237Sek110237 		err = dsl_prop_register(ds, "primarycache",
31010298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
3111544Seschrock 		if (err == 0)
3127237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
31310298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
3147237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
3157237Sek110237 			if (err == 0)
3167237Sek110237 				err = dsl_prop_register(ds, "checksum",
31710298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
3187237Sek110237 			if (err == 0)
3197237Sek110237 				err = dsl_prop_register(ds, "compression",
32010298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
3217237Sek110237 			if (err == 0)
3227237Sek110237 				err = dsl_prop_register(ds, "copies",
32310298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
32410310SNeil.Perrin@Sun.COM 			if (err == 0)
32510922SJeff.Bonwick@Sun.COM 				err = dsl_prop_register(ds, "dedup",
32610922SJeff.Bonwick@Sun.COM 				    dedup_changed_cb, os);
32710922SJeff.Bonwick@Sun.COM 			if (err == 0)
32810310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
32910310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
330*12294SMark.Musante@Sun.COM 			if (err == 0)
331*12294SMark.Musante@Sun.COM 				err = dsl_prop_register(ds, "sync",
332*12294SMark.Musante@Sun.COM 				    sync_changed_cb, os);
3337237Sek110237 		}
3341544Seschrock 		if (err) {
33510298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
33610298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
33710298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
3381544Seschrock 			return (err);
3391544Seschrock 		}
3402082Seschrock 	} else if (ds == NULL) {
341789Sahrens 		/* It's the meta-objset. */
34210298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
34310298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
34410298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
34510922SJeff.Bonwick@Sun.COM 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
34610922SJeff.Bonwick@Sun.COM 		os->os_dedup_verify = 0;
34710922SJeff.Bonwick@Sun.COM 		os->os_logbias = 0;
348*12294SMark.Musante@Sun.COM 		os->os_sync = 0;
34910298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
35010298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
351789Sahrens 	}
352789Sahrens 
35310298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
35410298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
355789Sahrens 
356789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
35710298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
358789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
35910298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
360789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
361789Sahrens 	}
36210298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
363789Sahrens 	    offsetof(dnode_t, dn_link));
36410298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
365789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
366789Sahrens 
36710298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
36810298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
36910298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3702856Snd150628 
37110298SMatthew.Ahrens@Sun.COM 	os->os_meta_dnode = dnode_special_open(os,
37210298SMatthew.Ahrens@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
37310298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
37410298SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode = dnode_special_open(os,
37510298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
37610298SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode = dnode_special_open(os,
37710298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3789396SMatthew.Ahrens@Sun.COM 	}
379789Sahrens 
3804787Sahrens 	/*
3814787Sahrens 	 * We should be the only thread trying to do this because we
3824787Sahrens 	 * have ds_opening_lock
3834787Sahrens 	 */
3844787Sahrens 	if (ds) {
38510298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
38610298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
38710298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
38810298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
389789Sahrens 	}
390789Sahrens 
39110298SMatthew.Ahrens@Sun.COM 	*osp = os;
3925367Sahrens 	return (0);
3935367Sahrens }
3945367Sahrens 
3955367Sahrens int
39610298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3975367Sahrens {
39810298SMatthew.Ahrens@Sun.COM 	int err = 0;
39910298SMatthew.Ahrens@Sun.COM 
40010298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
40110298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
40210298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
40310298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
40410298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
40510298SMatthew.Ahrens@Sun.COM 	}
40610298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
40710298SMatthew.Ahrens@Sun.COM 	return (err);
40810298SMatthew.Ahrens@Sun.COM }
40910298SMatthew.Ahrens@Sun.COM 
41010298SMatthew.Ahrens@Sun.COM /* called from zpl */
41110298SMatthew.Ahrens@Sun.COM int
41210298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
41310298SMatthew.Ahrens@Sun.COM {
41410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
4155367Sahrens 	int err;
4165367Sahrens 
41710298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
4185367Sahrens 	if (err)
41910298SMatthew.Ahrens@Sun.COM 		return (err);
42010298SMatthew.Ahrens@Sun.COM 
42110298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
42210298SMatthew.Ahrens@Sun.COM 	if (err)
42310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
42410298SMatthew.Ahrens@Sun.COM 
4255367Sahrens 	return (err);
4265367Sahrens }
4275367Sahrens 
428789Sahrens /* called from zpl */
429789Sahrens int
43010298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
43110298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
432789Sahrens {
433789Sahrens 	dsl_dataset_t *ds;
434789Sahrens 	int err;
435789Sahrens 
43610298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
43710298SMatthew.Ahrens@Sun.COM 	if (err)
43810298SMatthew.Ahrens@Sun.COM 		return (err);
4395367Sahrens 
44010298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
441789Sahrens 	if (err) {
44210298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
44310588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
44410298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
44510298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
44610588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
44710588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
44810588SEric.Taylor@Sun.COM 		return (EROFS);
449789Sahrens 	}
4505367Sahrens 	return (err);
451789Sahrens }
452789Sahrens 
453789Sahrens void
45410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
455789Sahrens {
45610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
45710298SMatthew.Ahrens@Sun.COM }
4586689Smaybee 
45910298SMatthew.Ahrens@Sun.COM void
46010298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
46110298SMatthew.Ahrens@Sun.COM {
46210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
463789Sahrens }
464789Sahrens 
4651646Sperrin int
4664944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4671544Seschrock {
4681544Seschrock 	dnode_t *dn;
4691596Sahrens 
47010298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4711596Sahrens 
4721596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
47310298SMatthew.Ahrens@Sun.COM 	list_remove(&os->os_dnodes, os->os_meta_dnode);
47410298SMatthew.Ahrens@Sun.COM 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
4751544Seschrock 
4761544Seschrock 	/*
4771596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4781596Sahrens 	 * because dnode_add_ref() only works if you already have a
4791596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4801596Sahrens 	 * skip.
4811544Seschrock 	 */
48210298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
4834944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
48410298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
4851596Sahrens 		continue;
4861596Sahrens 
4871596Sahrens 	while (dn) {
4881596Sahrens 		dnode_t *next_dn = dn;
4891596Sahrens 
4901596Sahrens 		do {
49110298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
4924944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4931596Sahrens 
49410298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
4954944Smaybee 		dnode_evict_dbufs(dn);
4961596Sahrens 		dnode_rele(dn, FTAG);
49710298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
4981596Sahrens 		dn = next_dn;
4991544Seschrock 	}
50010298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
50110298SMatthew.Ahrens@Sun.COM 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
5021544Seschrock }
5031544Seschrock 
5041544Seschrock void
50510298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
506789Sahrens {
50710298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
508789Sahrens 
50910922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
51010922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
511789Sahrens 
5127237Sek110237 	if (ds) {
5137237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
5147237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
51510298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
5167237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
51710298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
5187237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
51910298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
52010922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
52110922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
52210310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
52310310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
524*12294SMark.Musante@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "sync",
525*12294SMark.Musante@Sun.COM 			    sync_changed_cb, os));
5267237Sek110237 		}
5277237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
52810298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
5297237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
53010298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
531789Sahrens 	}
532789Sahrens 
53311935SMark.Shellenbaum@Sun.COM 	if (os->os_sa)
53411935SMark.Shellenbaum@Sun.COM 		sa_tear_down(os);
53511935SMark.Shellenbaum@Sun.COM 
5361544Seschrock 	/*
5371544Seschrock 	 * We should need only a single pass over the dnode list, since
5381544Seschrock 	 * nothing can be added to the list at this point.
5391544Seschrock 	 */
54010298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5411544Seschrock 
54210298SMatthew.Ahrens@Sun.COM 	dnode_special_close(os->os_meta_dnode);
54310298SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode) {
54410298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_userused_dnode);
54510298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_groupused_dnode);
5469396SMatthew.Ahrens@Sun.COM 	}
54710298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
548789Sahrens 
54910298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
550789Sahrens 
55110298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
55210298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
55310298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
55410298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
55510298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
556789Sahrens }
557789Sahrens 
55810373Schris.kirby@sun.com timestruc_t
55910373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
56010373Schris.kirby@sun.com {
56110373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
56210373Schris.kirby@sun.com }
56310373Schris.kirby@sun.com 
564789Sahrens /* called from dsl for meta-objset */
56510298SMatthew.Ahrens@Sun.COM objset_t *
5663547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5673547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
568789Sahrens {
56910298SMatthew.Ahrens@Sun.COM 	objset_t *os;
570789Sahrens 	dnode_t *mdn;
571789Sahrens 
572789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5734787Sahrens 	if (ds)
5744787Sahrens 		mutex_enter(&ds->ds_opening_lock);
57510298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
5764787Sahrens 	if (ds)
5774787Sahrens 		mutex_exit(&ds->ds_opening_lock);
57810298SMatthew.Ahrens@Sun.COM 	mdn = os->os_meta_dnode;
579789Sahrens 
580789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
581789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
582789Sahrens 
583789Sahrens 	/*
584789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
585789Sahrens 	 * later, because then we could do it in quescing context while
586789Sahrens 	 * we are also accessing it in open context.
587789Sahrens 	 *
588789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
589789Sahrens 	 * because the MOS is only updated in syncing context.
590789Sahrens 	 * This is most fortunate: the MOS is the only objset that
591789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
592789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
593789Sahrens 	 */
5941544Seschrock 	if (ds != NULL) {
5951544Seschrock 		int levels = 1;
5961544Seschrock 
5971544Seschrock 		/*
5981544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5991544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
6001544Seschrock 		 */
6011544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
6021544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
6031544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
6041544Seschrock 			levels++;
6051544Seschrock 
606789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
6071544Seschrock 		    mdn->dn_nlevels = levels;
6081544Seschrock 	}
609789Sahrens 
610789Sahrens 	ASSERT(type != DMU_OST_NONE);
611789Sahrens 	ASSERT(type != DMU_OST_ANY);
612789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
61310298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
61410298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
61510298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
61610298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
6179396SMatthew.Ahrens@Sun.COM 	}
618789Sahrens 
619789Sahrens 	dsl_dataset_dirty(ds, tx);
620789Sahrens 
62110298SMatthew.Ahrens@Sun.COM 	return (os);
622789Sahrens }
623789Sahrens 
624789Sahrens struct oscarg {
6254543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
626789Sahrens 	void *userarg;
62710272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
628789Sahrens 	const char *lastname;
629789Sahrens 	dmu_objset_type_t type;
6306492Stimh 	uint64_t flags;
631789Sahrens };
632789Sahrens 
6334543Smarks /*ARGSUSED*/
634789Sahrens static int
6352199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
636789Sahrens {
6372199Sahrens 	dsl_dir_t *dd = arg1;
6382199Sahrens 	struct oscarg *oa = arg2;
6392199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6402199Sahrens 	int err;
6412199Sahrens 	uint64_t ddobj;
6422199Sahrens 
6432199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6442199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6452199Sahrens 	if (err != ENOENT)
6462199Sahrens 		return (err ? err : EEXIST);
6472199Sahrens 
64810272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
64910272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
65010272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6512199Sahrens 			return (EXDEV);
6522199Sahrens 
65310272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
65410272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6552199Sahrens 			return (EINVAL);
6562199Sahrens 	}
6574543Smarks 
6582199Sahrens 	return (0);
6592199Sahrens }
6602199Sahrens 
6612199Sahrens static void
6624543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6632199Sahrens {
6642199Sahrens 	dsl_dir_t *dd = arg1;
6652199Sahrens 	struct oscarg *oa = arg2;
6662199Sahrens 	uint64_t dsobj;
667789Sahrens 
668789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
669789Sahrens 
6702199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
67110272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
672789Sahrens 
67310272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
67410272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
67510272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
67610298SMatthew.Ahrens@Sun.COM 		objset_t *os;
677789Sahrens 
67810272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
67910272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
68010272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
68110272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
68210272SMatthew.Ahrens@Sun.COM 
68310298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6843547Smaybee 		    ds, bp, oa->type, tx);
685789Sahrens 
686789Sahrens 		if (oa->userfunc)
68710298SMatthew.Ahrens@Sun.COM 			oa->userfunc(os, oa->userarg, cr, tx);
68810272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
689789Sahrens 	}
6904543Smarks 
6914543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6924543Smarks 	    tx, cr, "dataset = %llu", dsobj);
693789Sahrens }
694789Sahrens 
695789Sahrens int
69610272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6974543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
698789Sahrens {
6992199Sahrens 	dsl_dir_t *pdd;
700789Sahrens 	const char *tail;
701789Sahrens 	int err = 0;
7022199Sahrens 	struct oscarg oa = { 0 };
703789Sahrens 
7042199Sahrens 	ASSERT(strchr(name, '@') == NULL);
7052199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
7061544Seschrock 	if (err)
7071544Seschrock 		return (err);
708789Sahrens 	if (tail == NULL) {
7092199Sahrens 		dsl_dir_close(pdd, FTAG);
710789Sahrens 		return (EEXIST);
711789Sahrens 	}
712789Sahrens 
7132199Sahrens 	oa.userfunc = func;
7142199Sahrens 	oa.userarg = arg;
7152199Sahrens 	oa.lastname = tail;
7162199Sahrens 	oa.type = type;
7176492Stimh 	oa.flags = flags;
7184543Smarks 
71910272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
72010272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
72110272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
72210272SMatthew.Ahrens@Sun.COM 	return (err);
72310272SMatthew.Ahrens@Sun.COM }
72410272SMatthew.Ahrens@Sun.COM 
72510272SMatthew.Ahrens@Sun.COM int
72610272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
72710272SMatthew.Ahrens@Sun.COM {
72810272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
72910272SMatthew.Ahrens@Sun.COM 	const char *tail;
73010272SMatthew.Ahrens@Sun.COM 	int err = 0;
73110272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
73210272SMatthew.Ahrens@Sun.COM 
73310272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
73410272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
73510272SMatthew.Ahrens@Sun.COM 	if (err)
73610272SMatthew.Ahrens@Sun.COM 		return (err);
73710272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
73810272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
73910272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
740789Sahrens 	}
74110272SMatthew.Ahrens@Sun.COM 
74210272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
74310272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
74410272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
74510272SMatthew.Ahrens@Sun.COM 
7462199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7472199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7482199Sahrens 	dsl_dir_close(pdd, FTAG);
749789Sahrens 	return (err);
750789Sahrens }
751789Sahrens 
752789Sahrens int
75310242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
754789Sahrens {
75510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
756789Sahrens 	int error;
757789Sahrens 
758789Sahrens 	/*
75910272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
76010272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
76110272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
76210272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
76310272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
764789Sahrens 	 */
76510298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
766789Sahrens 	if (error == 0) {
76710298SMatthew.Ahrens@Sun.COM 		objset_t *os;
76810298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
76910298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
77010298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
77110272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
772789Sahrens 	}
773789Sahrens 
7745367Sahrens 	return (error);
775789Sahrens }
776789Sahrens 
7772199Sahrens struct snaparg {
7782199Sahrens 	dsl_sync_task_group_t *dstg;
7792199Sahrens 	char *snapname;
7802199Sahrens 	char failed[MAXPATHLEN];
78111620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
7829355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7835367Sahrens };
7845367Sahrens 
7859355SMatthew.Ahrens@Sun.COM static int
7869355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7879355SMatthew.Ahrens@Sun.COM {
7889355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7899355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7909355SMatthew.Ahrens@Sun.COM 
7919355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7929355SMatthew.Ahrens@Sun.COM 
79310298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
7949355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7959355SMatthew.Ahrens@Sun.COM }
7969355SMatthew.Ahrens@Sun.COM 
7979355SMatthew.Ahrens@Sun.COM static void
7989355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7999355SMatthew.Ahrens@Sun.COM {
8009355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
80110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
8029355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8039355SMatthew.Ahrens@Sun.COM 
8049355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
8059355SMatthew.Ahrens@Sun.COM 
80611022STom.Erickson@Sun.COM 	if (sn->props) {
80711022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
80811022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
80911022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
81011022STom.Erickson@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, cr, tx);
81111022STom.Erickson@Sun.COM 	}
8129355SMatthew.Ahrens@Sun.COM }
8132199Sahrens 
8142199Sahrens static int
81511209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
8162199Sahrens {
8172199Sahrens 	struct snaparg *sn = arg;
8182199Sahrens 	objset_t *os;
8192199Sahrens 	int err;
82011620SMatthew.Ahrens@Sun.COM 	char *cp;
82111620SMatthew.Ahrens@Sun.COM 
82211620SMatthew.Ahrens@Sun.COM 	/*
82311620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
82411620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
82511620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
82611620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
82711620SMatthew.Ahrens@Sun.COM 	 */
82811620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
82911620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
83011620SMatthew.Ahrens@Sun.COM 		return (0);
8312199Sahrens 
8322199Sahrens 	(void) strcpy(sn->failed, name);
8332199Sahrens 
8344543Smarks 	/*
83511620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
83611620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
83711620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
8384543Smarks 	 */
83911620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
8404543Smarks 		return (err);
8414543Smarks 
84210298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
8432199Sahrens 	if (err != 0)
8442199Sahrens 		return (err);
8452199Sahrens 
84611620SMatthew.Ahrens@Sun.COM 	/*
84711620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
84811620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
84911620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
85011620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
85111620SMatthew.Ahrens@Sun.COM 	 */
85210298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
85310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
85411620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
8553637Srm160521 	}
8563637Srm160521 
8573637Srm160521 	/*
8582199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8592199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8602199Sahrens 	 * a side effect.
8612199Sahrens 	 */
8622199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8632199Sahrens 	if (err == 0) {
8649355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
8659355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
8663637Srm160521 	} else {
86710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
8682199Sahrens 	}
8693637Srm160521 
8702199Sahrens 	return (err);
8712199Sahrens }
8722199Sahrens 
8732199Sahrens int
8749355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
8759355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
8762199Sahrens {
8772199Sahrens 	dsl_sync_task_t *dst;
8789355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8792199Sahrens 	spa_t *spa;
8802199Sahrens 	int err;
8812199Sahrens 
8822199Sahrens 	(void) strcpy(sn.failed, fsname);
8832199Sahrens 
8844603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8852199Sahrens 	if (err)
8862199Sahrens 		return (err);
8872199Sahrens 
8882199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8892199Sahrens 	sn.snapname = snapname;
8909355SMatthew.Ahrens@Sun.COM 	sn.props = props;
89111620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
8922199Sahrens 
8932417Sahrens 	if (recursive) {
8942417Sahrens 		err = dmu_objset_find(fsname,
8952417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8962417Sahrens 	} else {
8972199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8982417Sahrens 	}
8992199Sahrens 
9009355SMatthew.Ahrens@Sun.COM 	if (err == 0)
9019355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
9022199Sahrens 
9032199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
9042199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
9059355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
90610298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
9072199Sahrens 		if (dst->dst_err)
9085367Sahrens 			dsl_dataset_name(ds, sn.failed);
9099355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
91010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
9112199Sahrens 	}
9125367Sahrens 
9132199Sahrens 	if (err)
9142199Sahrens 		(void) strcpy(fsname, sn.failed);
9152199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
9162199Sahrens 	spa_close(spa, FTAG);
9172199Sahrens 	return (err);
9182199Sahrens }
9192199Sahrens 
920789Sahrens static void
9219396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
922789Sahrens {
9233547Smaybee 	dnode_t *dn;
924789Sahrens 
9253547Smaybee 	while (dn = list_head(list)) {
9263547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
9273547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
9283547Smaybee 		/*
9299396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
9309396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
9313547Smaybee 		 */
9323547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
9333547Smaybee 		ASSERT(dn->dn_zio);
934789Sahrens 
9353547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
9363547Smaybee 		list_remove(list, dn);
9379396SMatthew.Ahrens@Sun.COM 
9389396SMatthew.Ahrens@Sun.COM 		if (newlist) {
9399396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
9409396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
9419396SMatthew.Ahrens@Sun.COM 		}
9429396SMatthew.Ahrens@Sun.COM 
9433547Smaybee 		dnode_sync(dn, tx);
9443547Smaybee 	}
9453547Smaybee }
9462981Sahrens 
9473547Smaybee /* ARGSUSED */
9483547Smaybee static void
94910922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
9503547Smaybee {
9517754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
95210298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
9533547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
9542981Sahrens 
9557754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
9567754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
9577754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9585329Sgw25295 
9593547Smaybee 	/*
9609396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
9619396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
9629396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
9639396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
9643547Smaybee 	 */
9659396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
9667754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
9673547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
96810922SJeff.Bonwick@Sun.COM }
96910922SJeff.Bonwick@Sun.COM 
97010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
97110922SJeff.Bonwick@Sun.COM static void
97210922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
97310922SJeff.Bonwick@Sun.COM {
97410922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
97510922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
97610922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
9775329Sgw25295 
9787754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
97910922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
9807754SJeff.Bonwick@Sun.COM 	} else {
98110922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
98210922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
98310922SJeff.Bonwick@Sun.COM 
98410922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
98510922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
9865329Sgw25295 	}
987789Sahrens }
988789Sahrens 
989789Sahrens /* called from dsl */
990789Sahrens void
99110298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
992789Sahrens {
993789Sahrens 	int txgoff;
9941544Seschrock 	zbookmark_t zb;
99510922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
9963547Smaybee 	zio_t *zio;
9973547Smaybee 	list_t *list;
9989396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9993547Smaybee 	dbuf_dirty_record_t *dr;
10003547Smaybee 
10013547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1002789Sahrens 
1003789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1004789Sahrens 	/* XXX the write_done callback should really give us the tx... */
1005789Sahrens 	os->os_synctx = tx;
1006789Sahrens 
10073882Sahrens 	if (os->os_dsl_dataset == NULL) {
10083882Sahrens 		/*
10093882Sahrens 		 * This is the MOS.  If we have upgraded,
10103882Sahrens 		 * spa_max_replication() could change, so reset
10113882Sahrens 		 * os_copies here.
10123882Sahrens 		 */
10133882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
10143882Sahrens 	}
10153882Sahrens 
10163547Smaybee 	/*
10173547Smaybee 	 * Create the root block IO
10183547Smaybee 	 */
10197046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
10209396SMatthew.Ahrens@Sun.COM 
102110922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
102210922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
102310922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
102410922SJeff.Bonwick@Sun.COM 
102510922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
102610922SJeff.Bonwick@Sun.COM 
102710922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
102810922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
102910922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
10307754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
10313547Smaybee 
10323547Smaybee 	/*
10339396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
10343547Smaybee 	 */
10353547Smaybee 	os->os_meta_dnode->dn_zio = zio;
10363547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
1037789Sahrens 
10389396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
10399396SMatthew.Ahrens@Sun.COM 
10409396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
10419396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
10429396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
10439396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
10449396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
10459396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
10469396SMatthew.Ahrens@Sun.COM 	}
10479396SMatthew.Ahrens@Sun.COM 
1048789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1049789Sahrens 
10509396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
10519396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
10529396SMatthew.Ahrens@Sun.COM 		/*
10539396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
10549396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
10559396SMatthew.Ahrens@Sun.COM 		 */
10569396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
10579396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
10589396SMatthew.Ahrens@Sun.COM 	}
10599396SMatthew.Ahrens@Sun.COM 
10609396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
10619396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1062789Sahrens 
10633547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
10643547Smaybee 	while (dr = list_head(list)) {
10653547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
10663547Smaybee 		list_remove(list, dr);
10673547Smaybee 		if (dr->dr_zio)
10683547Smaybee 			zio_nowait(dr->dr_zio);
10693547Smaybee 	}
1070789Sahrens 	/*
1071789Sahrens 	 * Free intent log blocks up to this tx.
1072789Sahrens 	 */
1073789Sahrens 	zil_sync(os->os_zil, tx);
10747046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
10753547Smaybee 	zio_nowait(zio);
1076789Sahrens }
1077789Sahrens 
107810922SJeff.Bonwick@Sun.COM boolean_t
107910922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
108010922SJeff.Bonwick@Sun.COM {
108110922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
108210922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
108310922SJeff.Bonwick@Sun.COM }
108410922SJeff.Bonwick@Sun.COM 
10859396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
10869396SMatthew.Ahrens@Sun.COM 
10879396SMatthew.Ahrens@Sun.COM void
10889396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
10899396SMatthew.Ahrens@Sun.COM {
10909396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
10919396SMatthew.Ahrens@Sun.COM }
10929396SMatthew.Ahrens@Sun.COM 
10939396SMatthew.Ahrens@Sun.COM boolean_t
109410298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
10959396SMatthew.Ahrens@Sun.COM {
10969396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
10979396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
10989396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
10999396SMatthew.Ahrens@Sun.COM }
11009396SMatthew.Ahrens@Sun.COM 
110110407SMatthew.Ahrens@Sun.COM static void
110211935SMark.Shellenbaum@Sun.COM do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
110311935SMark.Shellenbaum@Sun.COM     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
110410407SMatthew.Ahrens@Sun.COM {
110511935SMark.Shellenbaum@Sun.COM 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
110611935SMark.Shellenbaum@Sun.COM 		int64_t delta = DNODE_SIZE + used;
110710407SMatthew.Ahrens@Sun.COM 		if (subtract)
110810407SMatthew.Ahrens@Sun.COM 			delta = -delta;
110910801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
111010407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
111110801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
111210407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
111310407SMatthew.Ahrens@Sun.COM 	}
111410407SMatthew.Ahrens@Sun.COM }
111510407SMatthew.Ahrens@Sun.COM 
11169396SMatthew.Ahrens@Sun.COM void
111711935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
11189396SMatthew.Ahrens@Sun.COM {
11199396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
11209396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
11219396SMatthew.Ahrens@Sun.COM 
11229396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
11239396SMatthew.Ahrens@Sun.COM 
11249396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
11259396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
11269396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
11279396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
11289396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
11299396SMatthew.Ahrens@Sun.COM 
11309396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
11319396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
113210298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11339396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
11349396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
113510298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11369396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
11379396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
11389396SMatthew.Ahrens@Sun.COM 		}
11399396SMatthew.Ahrens@Sun.COM 
11409396SMatthew.Ahrens@Sun.COM 		/*
114110407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
114211935SMark.Shellenbaum@Sun.COM 		 * net delta is zero.  Otherwise
114310407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
114410407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
114510407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
11469396SMatthew.Ahrens@Sun.COM 		 */
11479396SMatthew.Ahrens@Sun.COM 
11489396SMatthew.Ahrens@Sun.COM 		/*
11499396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
11509396SMatthew.Ahrens@Sun.COM 		 */
11519396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
115211935SMark.Shellenbaum@Sun.COM 		ASSERT(dn->dn_id_flags);
115311935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_OLD_EXIST)  {
115411935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
115511935SMark.Shellenbaum@Sun.COM 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
115611935SMark.Shellenbaum@Sun.COM 		}
115711935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
115811935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
115911935SMark.Shellenbaum@Sun.COM 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
116011935SMark.Shellenbaum@Sun.COM 			    dn->dn_newgid, B_FALSE, tx);
116111935SMark.Shellenbaum@Sun.COM 		}
116211935SMark.Shellenbaum@Sun.COM 
116311935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = 0;
116411935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = 0;
116511935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
116611935SMark.Shellenbaum@Sun.COM 			dn->dn_olduid = dn->dn_newuid;
116711935SMark.Shellenbaum@Sun.COM 			dn->dn_oldgid = dn->dn_newgid;
116811935SMark.Shellenbaum@Sun.COM 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
116911935SMark.Shellenbaum@Sun.COM 			if (dn->dn_bonuslen == 0)
117011935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
117111935SMark.Shellenbaum@Sun.COM 			else
117211935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
117311935SMark.Shellenbaum@Sun.COM 		}
117411935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST|DN_ID_SYNC);
11759396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
11769396SMatthew.Ahrens@Sun.COM 
11779396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
11789396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
11799396SMatthew.Ahrens@Sun.COM 	}
11809396SMatthew.Ahrens@Sun.COM }
11819396SMatthew.Ahrens@Sun.COM 
118212178SMark.Shellenbaum@Sun.COM /*
118312178SMark.Shellenbaum@Sun.COM  * Returns a pointer to data to find uid/gid from
118412178SMark.Shellenbaum@Sun.COM  *
118512178SMark.Shellenbaum@Sun.COM  * If a dirty record for transaction group that is syncing can't
118612178SMark.Shellenbaum@Sun.COM  * be found then NULL is returned.  In the NULL case it is assumed
118712178SMark.Shellenbaum@Sun.COM  * the uid/gid aren't changing.
118812178SMark.Shellenbaum@Sun.COM  */
118912178SMark.Shellenbaum@Sun.COM static void *
119012178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
119112178SMark.Shellenbaum@Sun.COM {
119212178SMark.Shellenbaum@Sun.COM 	dbuf_dirty_record_t *dr, **drp;
119312178SMark.Shellenbaum@Sun.COM 	void *data;
119412178SMark.Shellenbaum@Sun.COM 
119512178SMark.Shellenbaum@Sun.COM 	if (db->db_dirtycnt == 0)
119612178SMark.Shellenbaum@Sun.COM 		return (db->db.db_data);  /* Nothing is changing */
119712178SMark.Shellenbaum@Sun.COM 
119812178SMark.Shellenbaum@Sun.COM 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
119912178SMark.Shellenbaum@Sun.COM 		if (dr->dr_txg == tx->tx_txg)
120012178SMark.Shellenbaum@Sun.COM 			break;
120112178SMark.Shellenbaum@Sun.COM 
120212178SMark.Shellenbaum@Sun.COM 	if (dr == NULL)
120312178SMark.Shellenbaum@Sun.COM 		data = NULL;
120412178SMark.Shellenbaum@Sun.COM 	else if (dr->dr_dbuf->db_dnode->dn_bonuslen == 0 &&
120512178SMark.Shellenbaum@Sun.COM 	    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
120612178SMark.Shellenbaum@Sun.COM 		data = dr->dt.dl.dr_data->b_data;
120712178SMark.Shellenbaum@Sun.COM 	else
120812178SMark.Shellenbaum@Sun.COM 		data = dr->dt.dl.dr_data;
120912178SMark.Shellenbaum@Sun.COM 	return (data);
121012178SMark.Shellenbaum@Sun.COM }
121112178SMark.Shellenbaum@Sun.COM 
121211935SMark.Shellenbaum@Sun.COM void
121312178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
121411935SMark.Shellenbaum@Sun.COM {
121511935SMark.Shellenbaum@Sun.COM 	objset_t *os = dn->dn_objset;
121611935SMark.Shellenbaum@Sun.COM 	void *data = NULL;
121712178SMark.Shellenbaum@Sun.COM 	dmu_buf_impl_t *db = NULL;
121811935SMark.Shellenbaum@Sun.COM 	uint64_t *user, *group;
121911935SMark.Shellenbaum@Sun.COM 	int flags = dn->dn_id_flags;
122011935SMark.Shellenbaum@Sun.COM 	int error;
122112178SMark.Shellenbaum@Sun.COM 	boolean_t have_spill = B_FALSE;
122211935SMark.Shellenbaum@Sun.COM 
122311935SMark.Shellenbaum@Sun.COM 	if (!dmu_objset_userused_enabled(dn->dn_objset))
122411935SMark.Shellenbaum@Sun.COM 		return;
122511935SMark.Shellenbaum@Sun.COM 
122611935SMark.Shellenbaum@Sun.COM 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
122711935SMark.Shellenbaum@Sun.COM 	    DN_ID_CHKED_SPILL)))
122811935SMark.Shellenbaum@Sun.COM 		return;
122911935SMark.Shellenbaum@Sun.COM 
123011935SMark.Shellenbaum@Sun.COM 	if (before && dn->dn_bonuslen != 0)
123111935SMark.Shellenbaum@Sun.COM 		data = DN_BONUS(dn->dn_phys);
123212178SMark.Shellenbaum@Sun.COM 	else if (!before && dn->dn_bonuslen != 0) {
123312178SMark.Shellenbaum@Sun.COM 		if (dn->dn_bonus) {
123412178SMark.Shellenbaum@Sun.COM 			db = dn->dn_bonus;
123512178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
123612178SMark.Shellenbaum@Sun.COM 			data = dmu_objset_userquota_find_data(db, tx);
123712178SMark.Shellenbaum@Sun.COM 		} else {
123812178SMark.Shellenbaum@Sun.COM 			data = DN_BONUS(dn->dn_phys);
123912178SMark.Shellenbaum@Sun.COM 		}
124012178SMark.Shellenbaum@Sun.COM 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
124111935SMark.Shellenbaum@Sun.COM 			int rf = 0;
124211935SMark.Shellenbaum@Sun.COM 
124311935SMark.Shellenbaum@Sun.COM 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
124411935SMark.Shellenbaum@Sun.COM 				rf |= DB_RF_HAVESTRUCT;
124512178SMark.Shellenbaum@Sun.COM 			error = dmu_spill_hold_by_dnode(dn, rf,
124612178SMark.Shellenbaum@Sun.COM 			    FTAG, (dmu_buf_t **)&db);
124711935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
124812178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
124912178SMark.Shellenbaum@Sun.COM 			data = (before) ? db->db.db_data :
125012178SMark.Shellenbaum@Sun.COM 			    dmu_objset_userquota_find_data(db, tx);
125112178SMark.Shellenbaum@Sun.COM 			have_spill = B_TRUE;
125211935SMark.Shellenbaum@Sun.COM 	} else {
125311935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
125411935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
125511935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
125611935SMark.Shellenbaum@Sun.COM 		return;
125711935SMark.Shellenbaum@Sun.COM 	}
125811935SMark.Shellenbaum@Sun.COM 
125911935SMark.Shellenbaum@Sun.COM 	if (before) {
126012178SMark.Shellenbaum@Sun.COM 		ASSERT(data);
126111935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_olduid;
126211935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_oldgid;
126312178SMark.Shellenbaum@Sun.COM 	} else if (data) {
126411935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_newuid;
126511935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_newgid;
126611935SMark.Shellenbaum@Sun.COM 	}
126711935SMark.Shellenbaum@Sun.COM 
126812178SMark.Shellenbaum@Sun.COM 	/*
126912178SMark.Shellenbaum@Sun.COM 	 * Must always call the callback in case the object
127012178SMark.Shellenbaum@Sun.COM 	 * type has changed and that type isn't an object type to track
127112178SMark.Shellenbaum@Sun.COM 	 */
127211935SMark.Shellenbaum@Sun.COM 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
127311935SMark.Shellenbaum@Sun.COM 	    user, group);
127411935SMark.Shellenbaum@Sun.COM 
127512178SMark.Shellenbaum@Sun.COM 	/*
127612178SMark.Shellenbaum@Sun.COM 	 * Preserve existing uid/gid when the callback can't determine
127712178SMark.Shellenbaum@Sun.COM 	 * what the new uid/gid are and the callback returned EEXIST.
127812178SMark.Shellenbaum@Sun.COM 	 * The EEXIST error tells us to just use the existing uid/gid.
127912178SMark.Shellenbaum@Sun.COM 	 * If we don't know what the old values are then just assign
128012178SMark.Shellenbaum@Sun.COM 	 * them to 0, since that is a new file  being created.
128112178SMark.Shellenbaum@Sun.COM 	 */
128212178SMark.Shellenbaum@Sun.COM 	if (!before && data == NULL && error == EEXIST) {
128312178SMark.Shellenbaum@Sun.COM 		if (flags & DN_ID_OLD_EXIST) {
128412178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = dn->dn_olduid;
128512178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = dn->dn_oldgid;
128612178SMark.Shellenbaum@Sun.COM 		} else {
128712178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = 0;
128812178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = 0;
128912178SMark.Shellenbaum@Sun.COM 		}
129012178SMark.Shellenbaum@Sun.COM 		error = 0;
129112178SMark.Shellenbaum@Sun.COM 	}
129212178SMark.Shellenbaum@Sun.COM 
129312178SMark.Shellenbaum@Sun.COM 	if (db)
129412178SMark.Shellenbaum@Sun.COM 		mutex_exit(&db->db_mtx);
129512178SMark.Shellenbaum@Sun.COM 
129611935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
129711935SMark.Shellenbaum@Sun.COM 	if (error == 0 && before)
129811935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
129911935SMark.Shellenbaum@Sun.COM 	if (error == 0 && !before)
130011935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
130111935SMark.Shellenbaum@Sun.COM 
130212178SMark.Shellenbaum@Sun.COM 	if (have_spill) {
130311935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
130411935SMark.Shellenbaum@Sun.COM 	} else {
130511935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
130611935SMark.Shellenbaum@Sun.COM 	}
130711935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
130812178SMark.Shellenbaum@Sun.COM 	if (have_spill)
130912178SMark.Shellenbaum@Sun.COM 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
131011935SMark.Shellenbaum@Sun.COM }
131111935SMark.Shellenbaum@Sun.COM 
13129396SMatthew.Ahrens@Sun.COM boolean_t
13139396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
13149396SMatthew.Ahrens@Sun.COM {
131510298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
13169396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
13179396SMatthew.Ahrens@Sun.COM }
13189396SMatthew.Ahrens@Sun.COM 
13199396SMatthew.Ahrens@Sun.COM int
13209396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
13219396SMatthew.Ahrens@Sun.COM {
13229396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
13239396SMatthew.Ahrens@Sun.COM 	int err = 0;
13249396SMatthew.Ahrens@Sun.COM 
13259396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
13269396SMatthew.Ahrens@Sun.COM 		return (0);
132710298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
13289396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
13299396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
13309396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
13319396SMatthew.Ahrens@Sun.COM 
13329396SMatthew.Ahrens@Sun.COM 	/*
13339396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
13349396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
13359396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
13369396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
13379396SMatthew.Ahrens@Sun.COM 	 * independently.
13389396SMatthew.Ahrens@Sun.COM 	 */
13399396SMatthew.Ahrens@Sun.COM 
13409396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
13419951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
13429396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
13439396SMatthew.Ahrens@Sun.COM 		int objerr;
13449396SMatthew.Ahrens@Sun.COM 
13459396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
13469396SMatthew.Ahrens@Sun.COM 			return (EINTR);
13479396SMatthew.Ahrens@Sun.COM 
13489396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13499396SMatthew.Ahrens@Sun.COM 		if (objerr)
13509396SMatthew.Ahrens@Sun.COM 			continue;
13519951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
13529396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
13539396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
13549396SMatthew.Ahrens@Sun.COM 		if (objerr) {
13559396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
13569396SMatthew.Ahrens@Sun.COM 			continue;
13579396SMatthew.Ahrens@Sun.COM 		}
13589396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
13599396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
13609396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
13619396SMatthew.Ahrens@Sun.COM 	}
13629396SMatthew.Ahrens@Sun.COM 
136310298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
13649396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
13659396SMatthew.Ahrens@Sun.COM 	return (0);
13669396SMatthew.Ahrens@Sun.COM }
13679396SMatthew.Ahrens@Sun.COM 
1368789Sahrens void
13692885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
13702885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
13712885Sahrens {
137210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
13732885Sahrens 	    usedobjsp, availobjsp);
13742885Sahrens }
13752885Sahrens 
13762885Sahrens uint64_t
13772885Sahrens dmu_objset_fsid_guid(objset_t *os)
13782885Sahrens {
137910298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
13802885Sahrens }
13812885Sahrens 
13822885Sahrens void
13832885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1384789Sahrens {
138510298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
138610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
138710298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
13882885Sahrens }
13892885Sahrens 
13902885Sahrens void
13912885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
13922885Sahrens {
139310298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
139410298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
13952885Sahrens 
139610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
139710298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
13982885Sahrens 
13992885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
140010298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
14019396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
14029396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1403789Sahrens }
1404789Sahrens 
1405789Sahrens int
1406789Sahrens dmu_objset_is_snapshot(objset_t *os)
1407789Sahrens {
140810298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
140910298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1410789Sahrens 	else
1411789Sahrens 		return (B_FALSE);
1412789Sahrens }
1413789Sahrens 
1414789Sahrens int
14156492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
14166492Stimh     boolean_t *conflict)
14176492Stimh {
141810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
14196492Stimh 	uint64_t ignored;
14206492Stimh 
14216492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
14226492Stimh 		return (ENOENT);
14236492Stimh 
14246492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
14256492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
14266492Stimh 	    real, maxlen, conflict));
14276492Stimh }
14286492Stimh 
14296492Stimh int
1430789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
14315663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1432789Sahrens {
143310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1434789Sahrens 	zap_cursor_t cursor;
1435789Sahrens 	zap_attribute_t attr;
1436789Sahrens 
1437789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1438789Sahrens 		return (ENOENT);
1439789Sahrens 
1440789Sahrens 	zap_cursor_init_serialized(&cursor,
1441789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1442789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1443789Sahrens 
1444885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1445885Sahrens 		zap_cursor_fini(&cursor);
1446885Sahrens 		return (ENOENT);
1447885Sahrens 	}
1448885Sahrens 
1449885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1450885Sahrens 		zap_cursor_fini(&cursor);
1451885Sahrens 		return (ENAMETOOLONG);
1452885Sahrens 	}
1453885Sahrens 
1454885Sahrens 	(void) strcpy(name, attr.za_name);
1455885Sahrens 	if (idp)
1456885Sahrens 		*idp = attr.za_first_integer;
14575663Sck153898 	if (case_conflict)
14585663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1459885Sahrens 	zap_cursor_advance(&cursor);
1460885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1461885Sahrens 	zap_cursor_fini(&cursor);
1462885Sahrens 
1463885Sahrens 	return (0);
1464885Sahrens }
1465885Sahrens 
1466885Sahrens int
1467885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1468885Sahrens     uint64_t *idp, uint64_t *offp)
1469885Sahrens {
147010298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1471885Sahrens 	zap_cursor_t cursor;
1472885Sahrens 	zap_attribute_t attr;
1473885Sahrens 
1474885Sahrens 	/* there is no next dir on a snapshot! */
147510298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1476885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1477885Sahrens 		return (ENOENT);
1478885Sahrens 
1479885Sahrens 	zap_cursor_init_serialized(&cursor,
1480885Sahrens 	    dd->dd_pool->dp_meta_objset,
1481885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1482885Sahrens 
1483885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1484885Sahrens 		zap_cursor_fini(&cursor);
1485885Sahrens 		return (ENOENT);
1486885Sahrens 	}
1487885Sahrens 
1488885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1489885Sahrens 		zap_cursor_fini(&cursor);
1490789Sahrens 		return (ENAMETOOLONG);
1491885Sahrens 	}
1492789Sahrens 
1493789Sahrens 	(void) strcpy(name, attr.za_name);
1494885Sahrens 	if (idp)
1495885Sahrens 		*idp = attr.za_first_integer;
1496789Sahrens 	zap_cursor_advance(&cursor);
1497789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1498885Sahrens 	zap_cursor_fini(&cursor);
1499789Sahrens 
1500789Sahrens 	return (0);
1501789Sahrens }
1502789Sahrens 
15037046Sahrens struct findarg {
150411209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
15057046Sahrens 	void *arg;
15067046Sahrens };
15077046Sahrens 
15087046Sahrens /* ARGSUSED */
15097046Sahrens static int
15107046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
15117046Sahrens {
15127046Sahrens 	struct findarg *fa = arg;
151311209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
15147046Sahrens }
15157046Sahrens 
1516789Sahrens /*
1517789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
15187046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1519789Sahrens  */
15202199Sahrens int
152111209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
152211209SMatthew.Ahrens@Sun.COM     int flags)
1523789Sahrens {
15247046Sahrens 	struct findarg fa;
15257046Sahrens 	fa.func = func;
15267046Sahrens 	fa.arg = arg;
15277046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
15287046Sahrens }
15297046Sahrens 
15307046Sahrens /*
15317046Sahrens  * Find all objsets under name, call func on each
15327046Sahrens  */
15337046Sahrens int
15347046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
15357046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
15367046Sahrens {
1537789Sahrens 	dsl_dir_t *dd;
15387046Sahrens 	dsl_pool_t *dp;
15397046Sahrens 	dsl_dataset_t *ds;
1540789Sahrens 	zap_cursor_t zc;
15413978Smmusante 	zap_attribute_t *attr;
1542789Sahrens 	char *child;
15437046Sahrens 	uint64_t thisobj;
15447046Sahrens 	int err;
1545789Sahrens 
15467046Sahrens 	if (name == NULL)
15477046Sahrens 		name = spa_name(spa);
15487046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
15491544Seschrock 	if (err)
15502199Sahrens 		return (err);
1551789Sahrens 
15527046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
15537046Sahrens 	if (dd->dd_myname[0] == '$') {
15547046Sahrens 		dsl_dir_close(dd, FTAG);
15557046Sahrens 		return (0);
15567046Sahrens 	}
15577046Sahrens 
15587046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
15593978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
15607046Sahrens 	dp = dd->dd_pool;
1561789Sahrens 
1562789Sahrens 	/*
1563789Sahrens 	 * Iterate over all children.
1564789Sahrens 	 */
15652417Sahrens 	if (flags & DS_FIND_CHILDREN) {
15667046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
15672417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
15683978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
15692417Sahrens 		    (void) zap_cursor_advance(&zc)) {
15703978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
15713978Smmusante 			ASSERT(attr->za_num_integers == 1);
1572789Sahrens 
157311165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
15747046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
157511165SMatthew.Ahrens@Sun.COM 			strfree(child);
15762417Sahrens 			if (err)
15772417Sahrens 				break;
15782417Sahrens 		}
15792417Sahrens 		zap_cursor_fini(&zc);
15802199Sahrens 
15812417Sahrens 		if (err) {
15822417Sahrens 			dsl_dir_close(dd, FTAG);
15833978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
15842417Sahrens 			return (err);
15852417Sahrens 		}
1586789Sahrens 	}
1587789Sahrens 
1588789Sahrens 	/*
1589789Sahrens 	 * Iterate over all snapshots.
1590789Sahrens 	 */
15917046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
15927046Sahrens 		if (!dsl_pool_sync_context(dp))
15937046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
15947046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
15957046Sahrens 		if (!dsl_pool_sync_context(dp))
15967046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1597789Sahrens 
15987046Sahrens 		if (err == 0) {
15997046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
16007046Sahrens 			dsl_dataset_rele(ds, FTAG);
1601789Sahrens 
16027046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16037046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16047046Sahrens 			    (void) zap_cursor_advance(&zc)) {
16057046Sahrens 				ASSERT(attr->za_integer_length ==
16067046Sahrens 				    sizeof (uint64_t));
16077046Sahrens 				ASSERT(attr->za_num_integers == 1);
1608789Sahrens 
160911165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
161011165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
16117046Sahrens 				err = func(spa, attr->za_first_integer,
16127046Sahrens 				    child, arg);
161311165SMatthew.Ahrens@Sun.COM 				strfree(child);
16147046Sahrens 				if (err)
16157046Sahrens 					break;
16167046Sahrens 			}
16177046Sahrens 			zap_cursor_fini(&zc);
1618789Sahrens 		}
1619789Sahrens 	}
1620789Sahrens 
1621789Sahrens 	dsl_dir_close(dd, FTAG);
16223978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1623789Sahrens 
16242199Sahrens 	if (err)
16252199Sahrens 		return (err);
16262199Sahrens 
1627789Sahrens 	/*
1628789Sahrens 	 * Apply to self if appropriate.
1629789Sahrens 	 */
16307046Sahrens 	err = func(spa, thisobj, name, arg);
16312199Sahrens 	return (err);
1632789Sahrens }
16335326Sek110237 
16348415SRichard.Morris@Sun.COM /* ARGSUSED */
16358415SRichard.Morris@Sun.COM int
163611209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
16378415SRichard.Morris@Sun.COM {
16388415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
16398415SRichard.Morris@Sun.COM 
16408415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
16418415SRichard.Morris@Sun.COM 		return (0);
16428415SRichard.Morris@Sun.COM 
16438415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
16448415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
164510298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
16468415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
16478415SRichard.Morris@Sun.COM 			zbookmark_t zb;
16488415SRichard.Morris@Sun.COM 
164910922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
165010922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
16518415SRichard.Morris@Sun.COM 
16528415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
16538415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
16548415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
16558415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
16568415SRichard.Morris@Sun.COM 			    &aflags, &zb);
16578415SRichard.Morris@Sun.COM 		}
16588415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
16598415SRichard.Morris@Sun.COM 	}
16608415SRichard.Morris@Sun.COM 
16618415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
16628415SRichard.Morris@Sun.COM 	return (0);
16638415SRichard.Morris@Sun.COM }
16648415SRichard.Morris@Sun.COM 
16655326Sek110237 void
16665326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
16675326Sek110237 {
166810298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
166910298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
16705326Sek110237 }
16715326Sek110237 
16725326Sek110237 void *
16735326Sek110237 dmu_objset_get_user(objset_t *os)
16745326Sek110237 {
167510298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
167610298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
16775326Sek110237 }
1678