xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 12827:1eab9192da8b)
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 
2512294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
2612294SMark.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>
4411935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
45789Sahrens 
4612684STom.Erickson@Sun.COM /*
4712684STom.Erickson@Sun.COM  * Needed to close a window in dnode_move() that allows the objset to be freed
4812684STom.Erickson@Sun.COM  * before it can be safely accessed.
4912684STom.Erickson@Sun.COM  */
5012684STom.Erickson@Sun.COM krwlock_t os_lock;
5112684STom.Erickson@Sun.COM 
5212684STom.Erickson@Sun.COM void
5312684STom.Erickson@Sun.COM dmu_objset_init(void)
5412684STom.Erickson@Sun.COM {
5512684STom.Erickson@Sun.COM 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
5612684STom.Erickson@Sun.COM }
5712684STom.Erickson@Sun.COM 
5812684STom.Erickson@Sun.COM void
5912684STom.Erickson@Sun.COM dmu_objset_fini(void)
6012684STom.Erickson@Sun.COM {
6112684STom.Erickson@Sun.COM 	rw_destroy(&os_lock);
6212684STom.Erickson@Sun.COM }
6312684STom.Erickson@Sun.COM 
64789Sahrens spa_t *
65789Sahrens dmu_objset_spa(objset_t *os)
66789Sahrens {
6710298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
68789Sahrens }
69789Sahrens 
70789Sahrens zilog_t *
71789Sahrens dmu_objset_zil(objset_t *os)
72789Sahrens {
7310298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
74789Sahrens }
75789Sahrens 
76789Sahrens dsl_pool_t *
77789Sahrens dmu_objset_pool(objset_t *os)
78789Sahrens {
79789Sahrens 	dsl_dataset_t *ds;
80789Sahrens 
8110298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
82789Sahrens 		return (ds->ds_dir->dd_pool);
83789Sahrens 	else
8410298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
85789Sahrens }
86789Sahrens 
87789Sahrens dsl_dataset_t *
88789Sahrens dmu_objset_ds(objset_t *os)
89789Sahrens {
9010298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
91789Sahrens }
92789Sahrens 
93789Sahrens dmu_objset_type_t
94789Sahrens dmu_objset_type(objset_t *os)
95789Sahrens {
9610298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
97789Sahrens }
98789Sahrens 
99789Sahrens void
100789Sahrens dmu_objset_name(objset_t *os, char *buf)
101789Sahrens {
10210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
103789Sahrens }
104789Sahrens 
105789Sahrens uint64_t
106789Sahrens dmu_objset_id(objset_t *os)
107789Sahrens {
10810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
109789Sahrens 
110789Sahrens 	return (ds ? ds->ds_object : 0);
111789Sahrens }
112789Sahrens 
11310310SNeil.Perrin@Sun.COM uint64_t
11412294SMark.Musante@Sun.COM dmu_objset_syncprop(objset_t *os)
11512294SMark.Musante@Sun.COM {
11612294SMark.Musante@Sun.COM 	return (os->os_sync);
11712294SMark.Musante@Sun.COM }
11812294SMark.Musante@Sun.COM 
11912294SMark.Musante@Sun.COM uint64_t
12010310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
12110310SNeil.Perrin@Sun.COM {
12210310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
12310310SNeil.Perrin@Sun.COM }
12410310SNeil.Perrin@Sun.COM 
125789Sahrens static void
126789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
127789Sahrens {
12810298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
129789Sahrens 
130789Sahrens 	/*
131789Sahrens 	 * Inheritance should have been done by now.
132789Sahrens 	 */
133789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
134789Sahrens 
13510298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
136789Sahrens }
137789Sahrens 
138789Sahrens static void
139789Sahrens compression_changed_cb(void *arg, uint64_t newval)
140789Sahrens {
14110298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
142789Sahrens 
143789Sahrens 	/*
144789Sahrens 	 * Inheritance and range checking should have been done by now.
145789Sahrens 	 */
146789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
147789Sahrens 
14810298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
149789Sahrens }
150789Sahrens 
1513835Sahrens static void
1523835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1533835Sahrens {
15410298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1553835Sahrens 
1563835Sahrens 	/*
1573835Sahrens 	 * Inheritance and range checking should have been done by now.
1583835Sahrens 	 */
1593835Sahrens 	ASSERT(newval > 0);
16010298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1613835Sahrens 
16210298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1633835Sahrens }
1643835Sahrens 
1657237Sek110237 static void
16610922SJeff.Bonwick@Sun.COM dedup_changed_cb(void *arg, uint64_t newval)
16710922SJeff.Bonwick@Sun.COM {
16810922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
16910922SJeff.Bonwick@Sun.COM 	spa_t *spa = os->os_spa;
17010922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum;
17110922SJeff.Bonwick@Sun.COM 
17210922SJeff.Bonwick@Sun.COM 	/*
17310922SJeff.Bonwick@Sun.COM 	 * Inheritance should have been done by now.
17410922SJeff.Bonwick@Sun.COM 	 */
17510922SJeff.Bonwick@Sun.COM 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
17610922SJeff.Bonwick@Sun.COM 
17710922SJeff.Bonwick@Sun.COM 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
17810922SJeff.Bonwick@Sun.COM 
17910922SJeff.Bonwick@Sun.COM 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
18010922SJeff.Bonwick@Sun.COM 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
18110922SJeff.Bonwick@Sun.COM }
18210922SJeff.Bonwick@Sun.COM 
18310922SJeff.Bonwick@Sun.COM static void
1847237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1857237Sek110237 {
18610298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1877237Sek110237 
1887237Sek110237 	/*
1897237Sek110237 	 * Inheritance and range checking should have been done by now.
1907237Sek110237 	 */
1917237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1927237Sek110237 	    newval == ZFS_CACHE_METADATA);
1937237Sek110237 
19410298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1957237Sek110237 }
1967237Sek110237 
1977237Sek110237 static void
1987237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1997237Sek110237 {
20010298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
2017237Sek110237 
2027237Sek110237 	/*
2037237Sek110237 	 * Inheritance and range checking should have been done by now.
2047237Sek110237 	 */
2057237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2067237Sek110237 	    newval == ZFS_CACHE_METADATA);
2077237Sek110237 
20810298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
2097237Sek110237 }
2107237Sek110237 
21110310SNeil.Perrin@Sun.COM static void
21212294SMark.Musante@Sun.COM sync_changed_cb(void *arg, uint64_t newval)
21312294SMark.Musante@Sun.COM {
21412294SMark.Musante@Sun.COM 	objset_t *os = arg;
21512294SMark.Musante@Sun.COM 
21612294SMark.Musante@Sun.COM 	/*
21712294SMark.Musante@Sun.COM 	 * Inheritance and range checking should have been done by now.
21812294SMark.Musante@Sun.COM 	 */
21912294SMark.Musante@Sun.COM 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22012294SMark.Musante@Sun.COM 	    newval == ZFS_SYNC_DISABLED);
22112294SMark.Musante@Sun.COM 
22212294SMark.Musante@Sun.COM 	os->os_sync = newval;
22312294SMark.Musante@Sun.COM 	if (os->os_zil)
22412294SMark.Musante@Sun.COM 		zil_set_sync(os->os_zil, newval);
22512294SMark.Musante@Sun.COM }
22612294SMark.Musante@Sun.COM 
22712294SMark.Musante@Sun.COM static void
22810310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
22910310SNeil.Perrin@Sun.COM {
23010310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
23110310SNeil.Perrin@Sun.COM 
23210310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
23310310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
23410310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
23510310SNeil.Perrin@Sun.COM 	if (os->os_zil)
23610310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
23710310SNeil.Perrin@Sun.COM }
23810310SNeil.Perrin@Sun.COM 
239789Sahrens void
240789Sahrens dmu_objset_byteswap(void *buf, size_t size)
241789Sahrens {
242789Sahrens 	objset_phys_t *osp = buf;
243789Sahrens 
2449396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
245789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
246789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
247789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
2489396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
2499396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
2509396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
2519396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
2529396SMatthew.Ahrens@Sun.COM 	}
253789Sahrens }
254789Sahrens 
2551544Seschrock int
2561544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
25710298SMatthew.Ahrens@Sun.COM     objset_t **osp)
258789Sahrens {
25910298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2607046Sahrens 	int i, err;
261789Sahrens 
2624787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2634787Sahrens 
26410298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
26510298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
26610298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
26710298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
26810298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2692391Smaybee 		uint32_t aflags = ARC_WAIT;
2701544Seschrock 		zbookmark_t zb;
27110922SJeff.Bonwick@Sun.COM 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
27210922SJeff.Bonwick@Sun.COM 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
27310922SJeff.Bonwick@Sun.COM 
27410298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2757237Sek110237 			aflags |= ARC_L2CACHE;
2761544Seschrock 
27710298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2787046Sahrens 		/*
27912296SLin.Ling@Sun.COM 		 * XXX when bprewrite scrub can change the bp,
2807046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2817046Sahrens 		 * could change, and we'll need a lock.
2827046Sahrens 		 */
28312296SLin.Ling@Sun.COM 		err = dsl_read_nolock(NULL, spa, os->os_rootbp,
28410298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2852391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2861544Seschrock 		if (err) {
28710298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2887294Sperrin 			/* convert checksum errors into IO errors */
2897294Sperrin 			if (err == ECKSUM)
2907294Sperrin 				err = EIO;
2911544Seschrock 			return (err);
2921544Seschrock 		}
2939396SMatthew.Ahrens@Sun.COM 
2949396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2959396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
29610298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2979396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
29810298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2999396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
3009396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
30110298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
30210298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
30310298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
30410298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
30510298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
3069396SMatthew.Ahrens@Sun.COM 		}
3079396SMatthew.Ahrens@Sun.COM 
30810298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
30910298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
310789Sahrens 	} else {
3119396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
3129396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
31310298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
31410298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
31510298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
31610298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
317789Sahrens 	}
318789Sahrens 
319789Sahrens 	/*
320789Sahrens 	 * Note: the changed_cb will be called once before the register
321789Sahrens 	 * func returns, thus changing the checksum/compression from the
3227237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
3237237Sek110237 	 * checksum/compression/copies.
324789Sahrens 	 */
3257237Sek110237 	if (ds) {
3267237Sek110237 		err = dsl_prop_register(ds, "primarycache",
32710298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
3281544Seschrock 		if (err == 0)
3297237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
33010298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
3317237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
3327237Sek110237 			if (err == 0)
3337237Sek110237 				err = dsl_prop_register(ds, "checksum",
33410298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
3357237Sek110237 			if (err == 0)
3367237Sek110237 				err = dsl_prop_register(ds, "compression",
33710298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
3387237Sek110237 			if (err == 0)
3397237Sek110237 				err = dsl_prop_register(ds, "copies",
34010298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
34110310SNeil.Perrin@Sun.COM 			if (err == 0)
34210922SJeff.Bonwick@Sun.COM 				err = dsl_prop_register(ds, "dedup",
34310922SJeff.Bonwick@Sun.COM 				    dedup_changed_cb, os);
34410922SJeff.Bonwick@Sun.COM 			if (err == 0)
34510310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
34610310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
34712294SMark.Musante@Sun.COM 			if (err == 0)
34812294SMark.Musante@Sun.COM 				err = dsl_prop_register(ds, "sync",
34912294SMark.Musante@Sun.COM 				    sync_changed_cb, os);
3507237Sek110237 		}
3511544Seschrock 		if (err) {
35210298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
35310298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
35410298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
3551544Seschrock 			return (err);
3561544Seschrock 		}
3572082Seschrock 	} else if (ds == NULL) {
358789Sahrens 		/* It's the meta-objset. */
35910298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
36010298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
36110298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
36210922SJeff.Bonwick@Sun.COM 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
36310922SJeff.Bonwick@Sun.COM 		os->os_dedup_verify = 0;
36410922SJeff.Bonwick@Sun.COM 		os->os_logbias = 0;
36512294SMark.Musante@Sun.COM 		os->os_sync = 0;
36610298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
36710298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
368789Sahrens 	}
369789Sahrens 
370*12827SMatthew.Ahrens@Sun.COM 	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
371*12827SMatthew.Ahrens@Sun.COM 		os->os_zil_header = os->os_phys->os_zil_header;
37210298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
373789Sahrens 
374789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
37510298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
376789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
37710298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
378789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
379789Sahrens 	}
38010298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
381789Sahrens 	    offsetof(dnode_t, dn_link));
38210298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
383789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
384789Sahrens 
38510298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
38610298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
38710298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3882856Snd150628 
38912684STom.Erickson@Sun.COM 	DMU_META_DNODE(os) = dnode_special_open(os,
39012684STom.Erickson@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
39112684STom.Erickson@Sun.COM 	    &os->os_meta_dnode);
39210298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
39312684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
39412684STom.Erickson@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
39512684STom.Erickson@Sun.COM 		    &os->os_userused_dnode);
39612684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
39712684STom.Erickson@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
39812684STom.Erickson@Sun.COM 		    &os->os_groupused_dnode);
3999396SMatthew.Ahrens@Sun.COM 	}
400789Sahrens 
4014787Sahrens 	/*
4024787Sahrens 	 * We should be the only thread trying to do this because we
4034787Sahrens 	 * have ds_opening_lock
4044787Sahrens 	 */
4054787Sahrens 	if (ds) {
40610298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
40710298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
40810298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
40910298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
410789Sahrens 	}
411789Sahrens 
41210298SMatthew.Ahrens@Sun.COM 	*osp = os;
4135367Sahrens 	return (0);
4145367Sahrens }
4155367Sahrens 
4165367Sahrens int
41710298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4185367Sahrens {
41910298SMatthew.Ahrens@Sun.COM 	int err = 0;
42010298SMatthew.Ahrens@Sun.COM 
42110298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
42210298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
42310298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
42410298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
42510298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
42610298SMatthew.Ahrens@Sun.COM 	}
42710298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
42810298SMatthew.Ahrens@Sun.COM 	return (err);
42910298SMatthew.Ahrens@Sun.COM }
43010298SMatthew.Ahrens@Sun.COM 
43110298SMatthew.Ahrens@Sun.COM /* called from zpl */
43210298SMatthew.Ahrens@Sun.COM int
43310298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
43410298SMatthew.Ahrens@Sun.COM {
43510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
4365367Sahrens 	int err;
4375367Sahrens 
43810298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
4395367Sahrens 	if (err)
44010298SMatthew.Ahrens@Sun.COM 		return (err);
44110298SMatthew.Ahrens@Sun.COM 
44210298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
44310298SMatthew.Ahrens@Sun.COM 	if (err)
44410298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
44510298SMatthew.Ahrens@Sun.COM 
4465367Sahrens 	return (err);
4475367Sahrens }
4485367Sahrens 
449789Sahrens /* called from zpl */
450789Sahrens int
45110298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
45210298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
453789Sahrens {
454789Sahrens 	dsl_dataset_t *ds;
455789Sahrens 	int err;
456789Sahrens 
45710298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
45810298SMatthew.Ahrens@Sun.COM 	if (err)
45910298SMatthew.Ahrens@Sun.COM 		return (err);
4605367Sahrens 
46110298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
462789Sahrens 	if (err) {
46310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
46410588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
46510298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
46610298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
46710588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
46810588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
46910588SEric.Taylor@Sun.COM 		return (EROFS);
470789Sahrens 	}
4715367Sahrens 	return (err);
472789Sahrens }
473789Sahrens 
474789Sahrens void
47510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
476789Sahrens {
47710298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
47810298SMatthew.Ahrens@Sun.COM }
4796689Smaybee 
48010298SMatthew.Ahrens@Sun.COM void
48110298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
48210298SMatthew.Ahrens@Sun.COM {
48310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
484789Sahrens }
485789Sahrens 
4861646Sperrin int
4874944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4881544Seschrock {
4891544Seschrock 	dnode_t *dn;
4901596Sahrens 
49110298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4921596Sahrens 
4931596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
49412684STom.Erickson@Sun.COM 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
49512684STom.Erickson@Sun.COM 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
4961544Seschrock 
4971544Seschrock 	/*
4981596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4991596Sahrens 	 * because dnode_add_ref() only works if you already have a
5001596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
5011596Sahrens 	 * skip.
5021544Seschrock 	 */
50310298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
5044944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
50510298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
5061596Sahrens 		continue;
5071596Sahrens 
5081596Sahrens 	while (dn) {
5091596Sahrens 		dnode_t *next_dn = dn;
5101596Sahrens 
5111596Sahrens 		do {
51210298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
5134944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
5141596Sahrens 
51510298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
5164944Smaybee 		dnode_evict_dbufs(dn);
5171596Sahrens 		dnode_rele(dn, FTAG);
51810298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
5191596Sahrens 		dn = next_dn;
5201544Seschrock 	}
52112684STom.Erickson@Sun.COM 	dn = list_head(&os->os_dnodes);
52210298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
52312684STom.Erickson@Sun.COM 	return (dn != DMU_META_DNODE(os));
5241544Seschrock }
5251544Seschrock 
5261544Seschrock void
52710298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
528789Sahrens {
52910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
530789Sahrens 
53110922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
53210922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
533789Sahrens 
5347237Sek110237 	if (ds) {
5357237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
5367237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
53710298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
5387237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
53910298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
5407237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
54110298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
54210922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
54310922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
54410310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
54510310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
54612294SMark.Musante@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "sync",
54712294SMark.Musante@Sun.COM 			    sync_changed_cb, os));
5487237Sek110237 		}
5497237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
55010298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
5517237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
55210298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
553789Sahrens 	}
554789Sahrens 
55511935SMark.Shellenbaum@Sun.COM 	if (os->os_sa)
55611935SMark.Shellenbaum@Sun.COM 		sa_tear_down(os);
55711935SMark.Shellenbaum@Sun.COM 
5581544Seschrock 	/*
5591544Seschrock 	 * We should need only a single pass over the dnode list, since
5601544Seschrock 	 * nothing can be added to the list at this point.
5611544Seschrock 	 */
56210298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5631544Seschrock 
56412684STom.Erickson@Sun.COM 	dnode_special_close(&os->os_meta_dnode);
56512684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os)) {
56612684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_userused_dnode);
56712684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_groupused_dnode);
5689396SMatthew.Ahrens@Sun.COM 	}
56910298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
570789Sahrens 
57110298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
572789Sahrens 
57310298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
57412684STom.Erickson@Sun.COM 
57512684STom.Erickson@Sun.COM 	/*
57612684STom.Erickson@Sun.COM 	 * This is a barrier to prevent the objset from going away in
57712684STom.Erickson@Sun.COM 	 * dnode_move() until we can safely ensure that the objset is still in
57812684STom.Erickson@Sun.COM 	 * use. We consider the objset valid before the barrier and invalid
57912684STom.Erickson@Sun.COM 	 * after the barrier.
58012684STom.Erickson@Sun.COM 	 */
58112684STom.Erickson@Sun.COM 	rw_enter(&os_lock, RW_READER);
58212684STom.Erickson@Sun.COM 	rw_exit(&os_lock);
58312684STom.Erickson@Sun.COM 
58410298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
58510298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
58610298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
58710298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
588789Sahrens }
589789Sahrens 
59010373Schris.kirby@sun.com timestruc_t
59110373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
59210373Schris.kirby@sun.com {
59310373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
59410373Schris.kirby@sun.com }
59510373Schris.kirby@sun.com 
596789Sahrens /* called from dsl for meta-objset */
59710298SMatthew.Ahrens@Sun.COM objset_t *
5983547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5993547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
600789Sahrens {
60110298SMatthew.Ahrens@Sun.COM 	objset_t *os;
602789Sahrens 	dnode_t *mdn;
603789Sahrens 
604789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6054787Sahrens 	if (ds)
6064787Sahrens 		mutex_enter(&ds->ds_opening_lock);
60710298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
6084787Sahrens 	if (ds)
6094787Sahrens 		mutex_exit(&ds->ds_opening_lock);
61012684STom.Erickson@Sun.COM 	mdn = DMU_META_DNODE(os);
611789Sahrens 
612789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
613789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
614789Sahrens 
615789Sahrens 	/*
616789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
617789Sahrens 	 * later, because then we could do it in quescing context while
618789Sahrens 	 * we are also accessing it in open context.
619789Sahrens 	 *
620789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
621789Sahrens 	 * because the MOS is only updated in syncing context.
622789Sahrens 	 * This is most fortunate: the MOS is the only objset that
623789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
624789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
625789Sahrens 	 */
6261544Seschrock 	if (ds != NULL) {
6271544Seschrock 		int levels = 1;
6281544Seschrock 
6291544Seschrock 		/*
6301544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
6311544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
6321544Seschrock 		 */
6331544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
6341544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
6351544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
6361544Seschrock 			levels++;
6371544Seschrock 
638789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
6391544Seschrock 		    mdn->dn_nlevels = levels;
6401544Seschrock 	}
641789Sahrens 
642789Sahrens 	ASSERT(type != DMU_OST_NONE);
643789Sahrens 	ASSERT(type != DMU_OST_ANY);
644789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
64510298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
64610298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
64710298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
64810298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
6499396SMatthew.Ahrens@Sun.COM 	}
650789Sahrens 
651789Sahrens 	dsl_dataset_dirty(ds, tx);
652789Sahrens 
65310298SMatthew.Ahrens@Sun.COM 	return (os);
654789Sahrens }
655789Sahrens 
656789Sahrens struct oscarg {
6574543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
658789Sahrens 	void *userarg;
65910272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
660789Sahrens 	const char *lastname;
661789Sahrens 	dmu_objset_type_t type;
6626492Stimh 	uint64_t flags;
66312296SLin.Ling@Sun.COM 	cred_t *cr;
664789Sahrens };
665789Sahrens 
6664543Smarks /*ARGSUSED*/
667789Sahrens static int
6682199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
669789Sahrens {
6702199Sahrens 	dsl_dir_t *dd = arg1;
6712199Sahrens 	struct oscarg *oa = arg2;
6722199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6732199Sahrens 	int err;
6742199Sahrens 	uint64_t ddobj;
6752199Sahrens 
6762199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6772199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6782199Sahrens 	if (err != ENOENT)
6792199Sahrens 		return (err ? err : EEXIST);
6802199Sahrens 
68110272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
68210272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
68310272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6842199Sahrens 			return (EXDEV);
6852199Sahrens 
68610272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
68710272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6882199Sahrens 			return (EINVAL);
6892199Sahrens 	}
6904543Smarks 
6912199Sahrens 	return (0);
6922199Sahrens }
6932199Sahrens 
6942199Sahrens static void
69512296SLin.Ling@Sun.COM dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
6962199Sahrens {
6972199Sahrens 	dsl_dir_t *dd = arg1;
6982199Sahrens 	struct oscarg *oa = arg2;
6992199Sahrens 	uint64_t dsobj;
700*12827SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
701*12827SMatthew.Ahrens@Sun.COM 	objset_t *os;
702789Sahrens 
703789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
704789Sahrens 
7052199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
70612296SLin.Ling@Sun.COM 	    oa->clone_origin, oa->flags, oa->cr, tx);
707789Sahrens 
708*12827SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds));
709*12827SMatthew.Ahrens@Sun.COM 
71010272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
71110272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
712789Sahrens 
71310272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
71410272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
71510272SMatthew.Ahrens@Sun.COM 
71610298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
7173547Smaybee 		    ds, bp, oa->type, tx);
718789Sahrens 
719789Sahrens 		if (oa->userfunc)
72012296SLin.Ling@Sun.COM 			oa->userfunc(os, oa->userarg, oa->cr, tx);
721*12827SMatthew.Ahrens@Sun.COM 	} else {
722*12827SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
723*12827SMatthew.Ahrens@Sun.COM 		bzero(&os->os_zil_header, sizeof (os->os_zil_header));
724*12827SMatthew.Ahrens@Sun.COM 		dsl_dataset_dirty(ds, tx);
725789Sahrens 	}
726*12827SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(ds, FTAG);
7274543Smarks 
72812296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_CREATE, dd->dd_pool->dp_spa,
72912296SLin.Ling@Sun.COM 	    tx, "dataset = %llu", dsobj);
730789Sahrens }
731789Sahrens 
732789Sahrens int
73310272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
7344543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
735789Sahrens {
7362199Sahrens 	dsl_dir_t *pdd;
737789Sahrens 	const char *tail;
738789Sahrens 	int err = 0;
7392199Sahrens 	struct oscarg oa = { 0 };
740789Sahrens 
7412199Sahrens 	ASSERT(strchr(name, '@') == NULL);
7422199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
7431544Seschrock 	if (err)
7441544Seschrock 		return (err);
745789Sahrens 	if (tail == NULL) {
7462199Sahrens 		dsl_dir_close(pdd, FTAG);
747789Sahrens 		return (EEXIST);
748789Sahrens 	}
749789Sahrens 
7502199Sahrens 	oa.userfunc = func;
7512199Sahrens 	oa.userarg = arg;
7522199Sahrens 	oa.lastname = tail;
7532199Sahrens 	oa.type = type;
7546492Stimh 	oa.flags = flags;
75512296SLin.Ling@Sun.COM 	oa.cr = CRED();
7564543Smarks 
75710272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
75810272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
75910272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
76010272SMatthew.Ahrens@Sun.COM 	return (err);
76110272SMatthew.Ahrens@Sun.COM }
76210272SMatthew.Ahrens@Sun.COM 
76310272SMatthew.Ahrens@Sun.COM int
76410272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
76510272SMatthew.Ahrens@Sun.COM {
76610272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
76710272SMatthew.Ahrens@Sun.COM 	const char *tail;
76810272SMatthew.Ahrens@Sun.COM 	int err = 0;
76910272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
77010272SMatthew.Ahrens@Sun.COM 
77110272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
77210272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
77310272SMatthew.Ahrens@Sun.COM 	if (err)
77410272SMatthew.Ahrens@Sun.COM 		return (err);
77510272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
77610272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
77710272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
778789Sahrens 	}
77910272SMatthew.Ahrens@Sun.COM 
78010272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
78110272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
78210272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
78312296SLin.Ling@Sun.COM 	oa.cr = CRED();
78410272SMatthew.Ahrens@Sun.COM 
7852199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7862199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7872199Sahrens 	dsl_dir_close(pdd, FTAG);
788789Sahrens 	return (err);
789789Sahrens }
790789Sahrens 
791789Sahrens int
79210242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
793789Sahrens {
79410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
795789Sahrens 	int error;
796789Sahrens 
797789Sahrens 	/*
79810272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
79910272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
80010272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
80110272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
80210272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
803789Sahrens 	 */
80410298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
805789Sahrens 	if (error == 0) {
80610298SMatthew.Ahrens@Sun.COM 		objset_t *os;
80710298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
80810298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
80910298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
81010272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
811789Sahrens 	}
812789Sahrens 
8135367Sahrens 	return (error);
814789Sahrens }
815789Sahrens 
8162199Sahrens struct snaparg {
8172199Sahrens 	dsl_sync_task_group_t *dstg;
8182199Sahrens 	char *snapname;
8192199Sahrens 	char failed[MAXPATHLEN];
82011620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
821*12827SMatthew.Ahrens@Sun.COM 	boolean_t needsuspend;
8229355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
8235367Sahrens };
8245367Sahrens 
8259355SMatthew.Ahrens@Sun.COM static int
8269355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
8279355SMatthew.Ahrens@Sun.COM {
8289355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
8299355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8309355SMatthew.Ahrens@Sun.COM 
8319355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
8329355SMatthew.Ahrens@Sun.COM 
83310298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
8349355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
8359355SMatthew.Ahrens@Sun.COM }
8369355SMatthew.Ahrens@Sun.COM 
8379355SMatthew.Ahrens@Sun.COM static void
83812296SLin.Ling@Sun.COM snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
8399355SMatthew.Ahrens@Sun.COM {
8409355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
84110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
8429355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8439355SMatthew.Ahrens@Sun.COM 
84412296SLin.Ling@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, tx);
8459355SMatthew.Ahrens@Sun.COM 
84611022STom.Erickson@Sun.COM 	if (sn->props) {
84711022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
84811022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
84911022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
85012296SLin.Ling@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, tx);
85111022STom.Erickson@Sun.COM 	}
8529355SMatthew.Ahrens@Sun.COM }
8532199Sahrens 
8542199Sahrens static int
85511209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
8562199Sahrens {
8572199Sahrens 	struct snaparg *sn = arg;
8582199Sahrens 	objset_t *os;
8592199Sahrens 	int err;
86011620SMatthew.Ahrens@Sun.COM 	char *cp;
86111620SMatthew.Ahrens@Sun.COM 
86211620SMatthew.Ahrens@Sun.COM 	/*
86311620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
86411620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
86511620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
86611620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
86711620SMatthew.Ahrens@Sun.COM 	 */
86811620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
86911620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
87011620SMatthew.Ahrens@Sun.COM 		return (0);
8712199Sahrens 
8722199Sahrens 	(void) strcpy(sn->failed, name);
8732199Sahrens 
8744543Smarks 	/*
87511620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
87611620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
87711620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
8784543Smarks 	 */
87911620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
8804543Smarks 		return (err);
8814543Smarks 
88210298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
8832199Sahrens 	if (err != 0)
8842199Sahrens 		return (err);
8852199Sahrens 
88611620SMatthew.Ahrens@Sun.COM 	/*
88711620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
88811620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
88911620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
89011620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
89111620SMatthew.Ahrens@Sun.COM 	 */
89210298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
89310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
89411620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
8953637Srm160521 	}
8963637Srm160521 
897*12827SMatthew.Ahrens@Sun.COM 	if (sn->needsuspend) {
898*12827SMatthew.Ahrens@Sun.COM 		err = zil_suspend(dmu_objset_zil(os));
899*12827SMatthew.Ahrens@Sun.COM 		if (err) {
900*12827SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, sn);
901*12827SMatthew.Ahrens@Sun.COM 			return (err);
902*12827SMatthew.Ahrens@Sun.COM 		}
9032199Sahrens 	}
904*12827SMatthew.Ahrens@Sun.COM 	dsl_sync_task_create(sn->dstg, snapshot_check, snapshot_sync,
905*12827SMatthew.Ahrens@Sun.COM 	    os, sn, 3);
9063637Srm160521 
907*12827SMatthew.Ahrens@Sun.COM 	return (0);
9082199Sahrens }
9092199Sahrens 
9102199Sahrens int
9119355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
9129355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
9132199Sahrens {
9142199Sahrens 	dsl_sync_task_t *dst;
9159355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
9162199Sahrens 	spa_t *spa;
9172199Sahrens 	int err;
9182199Sahrens 
9192199Sahrens 	(void) strcpy(sn.failed, fsname);
9202199Sahrens 
9214603Sahrens 	err = spa_open(fsname, &spa, FTAG);
9222199Sahrens 	if (err)
9232199Sahrens 		return (err);
9242199Sahrens 
9252199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9262199Sahrens 	sn.snapname = snapname;
9279355SMatthew.Ahrens@Sun.COM 	sn.props = props;
92811620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
929*12827SMatthew.Ahrens@Sun.COM 	sn.needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
9302199Sahrens 
9312417Sahrens 	if (recursive) {
9322417Sahrens 		err = dmu_objset_find(fsname,
9332417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
9342417Sahrens 	} else {
9352199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
9362417Sahrens 	}
9372199Sahrens 
9389355SMatthew.Ahrens@Sun.COM 	if (err == 0)
9399355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
9402199Sahrens 
9412199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
9422199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
9439355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
94410298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
9452199Sahrens 		if (dst->dst_err)
9465367Sahrens 			dsl_dataset_name(ds, sn.failed);
947*12827SMatthew.Ahrens@Sun.COM 		if (sn.needsuspend)
948*12827SMatthew.Ahrens@Sun.COM 			zil_resume(dmu_objset_zil(os));
94910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
9502199Sahrens 	}
9515367Sahrens 
9522199Sahrens 	if (err)
9532199Sahrens 		(void) strcpy(fsname, sn.failed);
9542199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
9552199Sahrens 	spa_close(spa, FTAG);
9562199Sahrens 	return (err);
9572199Sahrens }
9582199Sahrens 
959789Sahrens static void
9609396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
961789Sahrens {
9623547Smaybee 	dnode_t *dn;
963789Sahrens 
9643547Smaybee 	while (dn = list_head(list)) {
9653547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
9663547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
9673547Smaybee 		/*
9689396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
9699396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
9703547Smaybee 		 */
9713547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
9723547Smaybee 		ASSERT(dn->dn_zio);
973789Sahrens 
9743547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
9753547Smaybee 		list_remove(list, dn);
9769396SMatthew.Ahrens@Sun.COM 
9779396SMatthew.Ahrens@Sun.COM 		if (newlist) {
9789396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
9799396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
9809396SMatthew.Ahrens@Sun.COM 		}
9819396SMatthew.Ahrens@Sun.COM 
9823547Smaybee 		dnode_sync(dn, tx);
9833547Smaybee 	}
9843547Smaybee }
9852981Sahrens 
9863547Smaybee /* ARGSUSED */
9873547Smaybee static void
98810922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
9893547Smaybee {
9907754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
99110298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
9923547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
9932981Sahrens 
9947754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
9957754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
9967754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9975329Sgw25295 
9983547Smaybee 	/*
9999396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
10009396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
10019396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
10029396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
10033547Smaybee 	 */
10049396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
10057754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10063547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
100710922SJeff.Bonwick@Sun.COM }
100810922SJeff.Bonwick@Sun.COM 
100910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
101010922SJeff.Bonwick@Sun.COM static void
101110922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
101210922SJeff.Bonwick@Sun.COM {
101310922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
101410922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
101510922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
10165329Sgw25295 
10177754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
101810922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
10197754SJeff.Bonwick@Sun.COM 	} else {
102010922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
102110922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
102210922SJeff.Bonwick@Sun.COM 
102310922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
102410922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
10255329Sgw25295 	}
1026789Sahrens }
1027789Sahrens 
1028789Sahrens /* called from dsl */
1029789Sahrens void
103010298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1031789Sahrens {
1032789Sahrens 	int txgoff;
10331544Seschrock 	zbookmark_t zb;
103410922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
10353547Smaybee 	zio_t *zio;
10363547Smaybee 	list_t *list;
10379396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
10383547Smaybee 	dbuf_dirty_record_t *dr;
10393547Smaybee 
10403547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1041789Sahrens 
1042789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1043789Sahrens 	/* XXX the write_done callback should really give us the tx... */
1044789Sahrens 	os->os_synctx = tx;
1045789Sahrens 
10463882Sahrens 	if (os->os_dsl_dataset == NULL) {
10473882Sahrens 		/*
10483882Sahrens 		 * This is the MOS.  If we have upgraded,
10493882Sahrens 		 * spa_max_replication() could change, so reset
10503882Sahrens 		 * os_copies here.
10513882Sahrens 		 */
10523882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
10533882Sahrens 	}
10543882Sahrens 
10553547Smaybee 	/*
10563547Smaybee 	 * Create the root block IO
10573547Smaybee 	 */
105810922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
105910922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
106010922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
106112296SLin.Ling@Sun.COM 	VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
106212296SLin.Ling@Sun.COM 	    os->os_rootbp, os->os_spa, &zb));
106310922SJeff.Bonwick@Sun.COM 
106410922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
106510922SJeff.Bonwick@Sun.COM 
106610922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
106710922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
106810922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
10697754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
10703547Smaybee 
10713547Smaybee 	/*
10729396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
10733547Smaybee 	 */
107412684STom.Erickson@Sun.COM 	DMU_META_DNODE(os)->dn_zio = zio;
107512684STom.Erickson@Sun.COM 	dnode_sync(DMU_META_DNODE(os), tx);
1076789Sahrens 
10779396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
10789396SMatthew.Ahrens@Sun.COM 
107912684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os) &&
108012684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
108112684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
108212684STom.Erickson@Sun.COM 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
108312684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
108412684STom.Erickson@Sun.COM 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
10859396SMatthew.Ahrens@Sun.COM 	}
10869396SMatthew.Ahrens@Sun.COM 
1087789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1088789Sahrens 
10899396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
10909396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
10919396SMatthew.Ahrens@Sun.COM 		/*
10929396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
10939396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
10949396SMatthew.Ahrens@Sun.COM 		 */
10959396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
10969396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
10979396SMatthew.Ahrens@Sun.COM 	}
10989396SMatthew.Ahrens@Sun.COM 
10999396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
11009396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1101789Sahrens 
110212684STom.Erickson@Sun.COM 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
11033547Smaybee 	while (dr = list_head(list)) {
11043547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
11053547Smaybee 		list_remove(list, dr);
11063547Smaybee 		if (dr->dr_zio)
11073547Smaybee 			zio_nowait(dr->dr_zio);
11083547Smaybee 	}
1109789Sahrens 	/*
1110789Sahrens 	 * Free intent log blocks up to this tx.
1111789Sahrens 	 */
1112789Sahrens 	zil_sync(os->os_zil, tx);
11137046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
11143547Smaybee 	zio_nowait(zio);
1115789Sahrens }
1116789Sahrens 
111710922SJeff.Bonwick@Sun.COM boolean_t
111810922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
111910922SJeff.Bonwick@Sun.COM {
112010922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
112110922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
112210922SJeff.Bonwick@Sun.COM }
112310922SJeff.Bonwick@Sun.COM 
112412722SSanjeev.Bagewadi@Sun.COM boolean_t
112512722SSanjeev.Bagewadi@Sun.COM dmu_objset_is_dirty_anywhere(objset_t *os)
112612722SSanjeev.Bagewadi@Sun.COM {
112712722SSanjeev.Bagewadi@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
112812722SSanjeev.Bagewadi@Sun.COM 		if (dmu_objset_is_dirty(os, t))
112912722SSanjeev.Bagewadi@Sun.COM 			return (B_TRUE);
113012722SSanjeev.Bagewadi@Sun.COM 	return (B_FALSE);
113112722SSanjeev.Bagewadi@Sun.COM }
113212722SSanjeev.Bagewadi@Sun.COM 
113312684STom.Erickson@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
11349396SMatthew.Ahrens@Sun.COM 
11359396SMatthew.Ahrens@Sun.COM void
11369396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
11379396SMatthew.Ahrens@Sun.COM {
11389396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
11399396SMatthew.Ahrens@Sun.COM }
11409396SMatthew.Ahrens@Sun.COM 
11419396SMatthew.Ahrens@Sun.COM boolean_t
114210298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
11439396SMatthew.Ahrens@Sun.COM {
11449396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
114512684STom.Erickson@Sun.COM 	    used_cbs[os->os_phys->os_type] != NULL &&
114612684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os) != NULL);
11479396SMatthew.Ahrens@Sun.COM }
11489396SMatthew.Ahrens@Sun.COM 
114910407SMatthew.Ahrens@Sun.COM static void
115011935SMark.Shellenbaum@Sun.COM do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
115111935SMark.Shellenbaum@Sun.COM     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
115210407SMatthew.Ahrens@Sun.COM {
115311935SMark.Shellenbaum@Sun.COM 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
115411935SMark.Shellenbaum@Sun.COM 		int64_t delta = DNODE_SIZE + used;
115510407SMatthew.Ahrens@Sun.COM 		if (subtract)
115610407SMatthew.Ahrens@Sun.COM 			delta = -delta;
115710801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
115810407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
115910801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
116010407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
116110407SMatthew.Ahrens@Sun.COM 	}
116210407SMatthew.Ahrens@Sun.COM }
116310407SMatthew.Ahrens@Sun.COM 
11649396SMatthew.Ahrens@Sun.COM void
116511935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
11669396SMatthew.Ahrens@Sun.COM {
11679396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
11689396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
11699396SMatthew.Ahrens@Sun.COM 
11709396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
11719396SMatthew.Ahrens@Sun.COM 
11729396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
117312493SMark.Shellenbaum@Oracle.COM 		int flags;
11749396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
11759396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
11769396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
11779396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
11789396SMatthew.Ahrens@Sun.COM 
11799396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
118012684STom.Erickson@Sun.COM 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
118110298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11829396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
11839396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
118410298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11859396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
11869396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
11879396SMatthew.Ahrens@Sun.COM 		}
11889396SMatthew.Ahrens@Sun.COM 
11899396SMatthew.Ahrens@Sun.COM 		/*
119010407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
119111935SMark.Shellenbaum@Sun.COM 		 * net delta is zero.  Otherwise
119210407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
119310407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
119410407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
11959396SMatthew.Ahrens@Sun.COM 		 */
11969396SMatthew.Ahrens@Sun.COM 
119712493SMark.Shellenbaum@Oracle.COM 		flags = dn->dn_id_flags;
119812493SMark.Shellenbaum@Oracle.COM 		ASSERT(flags);
119912493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_OLD_EXIST)  {
120011935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
120111935SMark.Shellenbaum@Sun.COM 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
120211935SMark.Shellenbaum@Sun.COM 		}
120312493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_NEW_EXIST) {
120411935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
120511935SMark.Shellenbaum@Sun.COM 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
120611935SMark.Shellenbaum@Sun.COM 			    dn->dn_newgid, B_FALSE, tx);
120711935SMark.Shellenbaum@Sun.COM 		}
120811935SMark.Shellenbaum@Sun.COM 
120912493SMark.Shellenbaum@Oracle.COM 		mutex_enter(&dn->dn_mtx);
121011935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = 0;
121111935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = 0;
121211935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
121311935SMark.Shellenbaum@Sun.COM 			dn->dn_olduid = dn->dn_newuid;
121411935SMark.Shellenbaum@Sun.COM 			dn->dn_oldgid = dn->dn_newgid;
121511935SMark.Shellenbaum@Sun.COM 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
121611935SMark.Shellenbaum@Sun.COM 			if (dn->dn_bonuslen == 0)
121711935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
121811935SMark.Shellenbaum@Sun.COM 			else
121911935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
122011935SMark.Shellenbaum@Sun.COM 		}
122112432SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
12229396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
12239396SMatthew.Ahrens@Sun.COM 
12249396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
12259396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
12269396SMatthew.Ahrens@Sun.COM 	}
12279396SMatthew.Ahrens@Sun.COM }
12289396SMatthew.Ahrens@Sun.COM 
122912178SMark.Shellenbaum@Sun.COM /*
123012178SMark.Shellenbaum@Sun.COM  * Returns a pointer to data to find uid/gid from
123112178SMark.Shellenbaum@Sun.COM  *
123212178SMark.Shellenbaum@Sun.COM  * If a dirty record for transaction group that is syncing can't
123312178SMark.Shellenbaum@Sun.COM  * be found then NULL is returned.  In the NULL case it is assumed
123412178SMark.Shellenbaum@Sun.COM  * the uid/gid aren't changing.
123512178SMark.Shellenbaum@Sun.COM  */
123612178SMark.Shellenbaum@Sun.COM static void *
123712178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
123812178SMark.Shellenbaum@Sun.COM {
123912178SMark.Shellenbaum@Sun.COM 	dbuf_dirty_record_t *dr, **drp;
124012178SMark.Shellenbaum@Sun.COM 	void *data;
124112178SMark.Shellenbaum@Sun.COM 
124212178SMark.Shellenbaum@Sun.COM 	if (db->db_dirtycnt == 0)
124312178SMark.Shellenbaum@Sun.COM 		return (db->db.db_data);  /* Nothing is changing */
124412178SMark.Shellenbaum@Sun.COM 
124512178SMark.Shellenbaum@Sun.COM 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
124612178SMark.Shellenbaum@Sun.COM 		if (dr->dr_txg == tx->tx_txg)
124712178SMark.Shellenbaum@Sun.COM 			break;
124812178SMark.Shellenbaum@Sun.COM 
124912684STom.Erickson@Sun.COM 	if (dr == NULL) {
125012178SMark.Shellenbaum@Sun.COM 		data = NULL;
125112684STom.Erickson@Sun.COM 	} else {
125212684STom.Erickson@Sun.COM 		dnode_t *dn;
125312684STom.Erickson@Sun.COM 
125412684STom.Erickson@Sun.COM 		DB_DNODE_ENTER(dr->dr_dbuf);
125512684STom.Erickson@Sun.COM 		dn = DB_DNODE(dr->dr_dbuf);
125612684STom.Erickson@Sun.COM 
125712684STom.Erickson@Sun.COM 		if (dn->dn_bonuslen == 0 &&
125812684STom.Erickson@Sun.COM 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
125912684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data->b_data;
126012684STom.Erickson@Sun.COM 		else
126112684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data;
126212684STom.Erickson@Sun.COM 
126312684STom.Erickson@Sun.COM 		DB_DNODE_EXIT(dr->dr_dbuf);
126412684STom.Erickson@Sun.COM 	}
126512684STom.Erickson@Sun.COM 
126612178SMark.Shellenbaum@Sun.COM 	return (data);
126712178SMark.Shellenbaum@Sun.COM }
126812178SMark.Shellenbaum@Sun.COM 
126911935SMark.Shellenbaum@Sun.COM void
127012178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
127111935SMark.Shellenbaum@Sun.COM {
127211935SMark.Shellenbaum@Sun.COM 	objset_t *os = dn->dn_objset;
127311935SMark.Shellenbaum@Sun.COM 	void *data = NULL;
127412178SMark.Shellenbaum@Sun.COM 	dmu_buf_impl_t *db = NULL;
127511935SMark.Shellenbaum@Sun.COM 	uint64_t *user, *group;
127611935SMark.Shellenbaum@Sun.COM 	int flags = dn->dn_id_flags;
127711935SMark.Shellenbaum@Sun.COM 	int error;
127812178SMark.Shellenbaum@Sun.COM 	boolean_t have_spill = B_FALSE;
127911935SMark.Shellenbaum@Sun.COM 
128011935SMark.Shellenbaum@Sun.COM 	if (!dmu_objset_userused_enabled(dn->dn_objset))
128111935SMark.Shellenbaum@Sun.COM 		return;
128211935SMark.Shellenbaum@Sun.COM 
128311935SMark.Shellenbaum@Sun.COM 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
128411935SMark.Shellenbaum@Sun.COM 	    DN_ID_CHKED_SPILL)))
128511935SMark.Shellenbaum@Sun.COM 		return;
128611935SMark.Shellenbaum@Sun.COM 
128711935SMark.Shellenbaum@Sun.COM 	if (before && dn->dn_bonuslen != 0)
128811935SMark.Shellenbaum@Sun.COM 		data = DN_BONUS(dn->dn_phys);
128912178SMark.Shellenbaum@Sun.COM 	else if (!before && dn->dn_bonuslen != 0) {
129012178SMark.Shellenbaum@Sun.COM 		if (dn->dn_bonus) {
129112178SMark.Shellenbaum@Sun.COM 			db = dn->dn_bonus;
129212178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
129312178SMark.Shellenbaum@Sun.COM 			data = dmu_objset_userquota_find_data(db, tx);
129412178SMark.Shellenbaum@Sun.COM 		} else {
129512178SMark.Shellenbaum@Sun.COM 			data = DN_BONUS(dn->dn_phys);
129612178SMark.Shellenbaum@Sun.COM 		}
129712178SMark.Shellenbaum@Sun.COM 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
129811935SMark.Shellenbaum@Sun.COM 			int rf = 0;
129911935SMark.Shellenbaum@Sun.COM 
130011935SMark.Shellenbaum@Sun.COM 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
130111935SMark.Shellenbaum@Sun.COM 				rf |= DB_RF_HAVESTRUCT;
130212493SMark.Shellenbaum@Oracle.COM 			error = dmu_spill_hold_by_dnode(dn,
130312493SMark.Shellenbaum@Oracle.COM 			    rf | DB_RF_MUST_SUCCEED,
130412178SMark.Shellenbaum@Sun.COM 			    FTAG, (dmu_buf_t **)&db);
130511935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
130612178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
130712178SMark.Shellenbaum@Sun.COM 			data = (before) ? db->db.db_data :
130812178SMark.Shellenbaum@Sun.COM 			    dmu_objset_userquota_find_data(db, tx);
130912178SMark.Shellenbaum@Sun.COM 			have_spill = B_TRUE;
131011935SMark.Shellenbaum@Sun.COM 	} else {
131111935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
131211935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
131311935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
131411935SMark.Shellenbaum@Sun.COM 		return;
131511935SMark.Shellenbaum@Sun.COM 	}
131611935SMark.Shellenbaum@Sun.COM 
131711935SMark.Shellenbaum@Sun.COM 	if (before) {
131812178SMark.Shellenbaum@Sun.COM 		ASSERT(data);
131911935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_olduid;
132011935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_oldgid;
132112178SMark.Shellenbaum@Sun.COM 	} else if (data) {
132211935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_newuid;
132311935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_newgid;
132411935SMark.Shellenbaum@Sun.COM 	}
132511935SMark.Shellenbaum@Sun.COM 
132612178SMark.Shellenbaum@Sun.COM 	/*
132712178SMark.Shellenbaum@Sun.COM 	 * Must always call the callback in case the object
132812178SMark.Shellenbaum@Sun.COM 	 * type has changed and that type isn't an object type to track
132912178SMark.Shellenbaum@Sun.COM 	 */
133011935SMark.Shellenbaum@Sun.COM 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
133111935SMark.Shellenbaum@Sun.COM 	    user, group);
133211935SMark.Shellenbaum@Sun.COM 
133312178SMark.Shellenbaum@Sun.COM 	/*
133412178SMark.Shellenbaum@Sun.COM 	 * Preserve existing uid/gid when the callback can't determine
133512178SMark.Shellenbaum@Sun.COM 	 * what the new uid/gid are and the callback returned EEXIST.
133612178SMark.Shellenbaum@Sun.COM 	 * The EEXIST error tells us to just use the existing uid/gid.
133712178SMark.Shellenbaum@Sun.COM 	 * If we don't know what the old values are then just assign
133812178SMark.Shellenbaum@Sun.COM 	 * them to 0, since that is a new file  being created.
133912178SMark.Shellenbaum@Sun.COM 	 */
134012178SMark.Shellenbaum@Sun.COM 	if (!before && data == NULL && error == EEXIST) {
134112178SMark.Shellenbaum@Sun.COM 		if (flags & DN_ID_OLD_EXIST) {
134212178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = dn->dn_olduid;
134312178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = dn->dn_oldgid;
134412178SMark.Shellenbaum@Sun.COM 		} else {
134512178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = 0;
134612178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = 0;
134712178SMark.Shellenbaum@Sun.COM 		}
134812178SMark.Shellenbaum@Sun.COM 		error = 0;
134912178SMark.Shellenbaum@Sun.COM 	}
135012178SMark.Shellenbaum@Sun.COM 
135112178SMark.Shellenbaum@Sun.COM 	if (db)
135212178SMark.Shellenbaum@Sun.COM 		mutex_exit(&db->db_mtx);
135312178SMark.Shellenbaum@Sun.COM 
135411935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
135511935SMark.Shellenbaum@Sun.COM 	if (error == 0 && before)
135611935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
135711935SMark.Shellenbaum@Sun.COM 	if (error == 0 && !before)
135811935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
135911935SMark.Shellenbaum@Sun.COM 
136012178SMark.Shellenbaum@Sun.COM 	if (have_spill) {
136111935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
136211935SMark.Shellenbaum@Sun.COM 	} else {
136311935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
136411935SMark.Shellenbaum@Sun.COM 	}
136511935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
136612178SMark.Shellenbaum@Sun.COM 	if (have_spill)
136712178SMark.Shellenbaum@Sun.COM 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
136811935SMark.Shellenbaum@Sun.COM }
136911935SMark.Shellenbaum@Sun.COM 
13709396SMatthew.Ahrens@Sun.COM boolean_t
13719396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
13729396SMatthew.Ahrens@Sun.COM {
137310298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
13749396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
13759396SMatthew.Ahrens@Sun.COM }
13769396SMatthew.Ahrens@Sun.COM 
13779396SMatthew.Ahrens@Sun.COM int
13789396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
13799396SMatthew.Ahrens@Sun.COM {
13809396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
13819396SMatthew.Ahrens@Sun.COM 	int err = 0;
13829396SMatthew.Ahrens@Sun.COM 
13839396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
13849396SMatthew.Ahrens@Sun.COM 		return (0);
138510298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
13869396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
13879396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
13889396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
13899396SMatthew.Ahrens@Sun.COM 
13909396SMatthew.Ahrens@Sun.COM 	/*
13919396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
13929396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
13939396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
13949396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
13959396SMatthew.Ahrens@Sun.COM 	 * independently.
13969396SMatthew.Ahrens@Sun.COM 	 */
13979396SMatthew.Ahrens@Sun.COM 
13989396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
13999951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
14009396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
14019396SMatthew.Ahrens@Sun.COM 		int objerr;
14029396SMatthew.Ahrens@Sun.COM 
14039396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
14049396SMatthew.Ahrens@Sun.COM 			return (EINTR);
14059396SMatthew.Ahrens@Sun.COM 
14069396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14079396SMatthew.Ahrens@Sun.COM 		if (objerr)
14089396SMatthew.Ahrens@Sun.COM 			continue;
14099951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
14109396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
14119396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14129396SMatthew.Ahrens@Sun.COM 		if (objerr) {
14139396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
14149396SMatthew.Ahrens@Sun.COM 			continue;
14159396SMatthew.Ahrens@Sun.COM 		}
14169396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
14179396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
14189396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
14199396SMatthew.Ahrens@Sun.COM 	}
14209396SMatthew.Ahrens@Sun.COM 
142110298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
14229396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
14239396SMatthew.Ahrens@Sun.COM 	return (0);
14249396SMatthew.Ahrens@Sun.COM }
14259396SMatthew.Ahrens@Sun.COM 
1426789Sahrens void
14272885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
14282885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
14292885Sahrens {
143010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
14312885Sahrens 	    usedobjsp, availobjsp);
14322885Sahrens }
14332885Sahrens 
14342885Sahrens uint64_t
14352885Sahrens dmu_objset_fsid_guid(objset_t *os)
14362885Sahrens {
143710298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
14382885Sahrens }
14392885Sahrens 
14402885Sahrens void
14412885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1442789Sahrens {
144310298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
144410298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
144510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
14462885Sahrens }
14472885Sahrens 
14482885Sahrens void
14492885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
14502885Sahrens {
145110298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
145210298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
14532885Sahrens 
145410298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
145510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
14562885Sahrens 
14572885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
145810298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
14599396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
14609396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1461789Sahrens }
1462789Sahrens 
1463789Sahrens int
1464789Sahrens dmu_objset_is_snapshot(objset_t *os)
1465789Sahrens {
146610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
146710298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1468789Sahrens 	else
1469789Sahrens 		return (B_FALSE);
1470789Sahrens }
1471789Sahrens 
1472789Sahrens int
14736492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
14746492Stimh     boolean_t *conflict)
14756492Stimh {
147610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
14776492Stimh 	uint64_t ignored;
14786492Stimh 
14796492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
14806492Stimh 		return (ENOENT);
14816492Stimh 
14826492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
14836492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
14846492Stimh 	    real, maxlen, conflict));
14856492Stimh }
14866492Stimh 
14876492Stimh int
1488789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
14895663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1490789Sahrens {
149110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1492789Sahrens 	zap_cursor_t cursor;
1493789Sahrens 	zap_attribute_t attr;
1494789Sahrens 
1495789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1496789Sahrens 		return (ENOENT);
1497789Sahrens 
1498789Sahrens 	zap_cursor_init_serialized(&cursor,
1499789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1500789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1501789Sahrens 
1502885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1503885Sahrens 		zap_cursor_fini(&cursor);
1504885Sahrens 		return (ENOENT);
1505885Sahrens 	}
1506885Sahrens 
1507885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1508885Sahrens 		zap_cursor_fini(&cursor);
1509885Sahrens 		return (ENAMETOOLONG);
1510885Sahrens 	}
1511885Sahrens 
1512885Sahrens 	(void) strcpy(name, attr.za_name);
1513885Sahrens 	if (idp)
1514885Sahrens 		*idp = attr.za_first_integer;
15155663Sck153898 	if (case_conflict)
15165663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1517885Sahrens 	zap_cursor_advance(&cursor);
1518885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1519885Sahrens 	zap_cursor_fini(&cursor);
1520885Sahrens 
1521885Sahrens 	return (0);
1522885Sahrens }
1523885Sahrens 
1524885Sahrens int
1525885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1526885Sahrens     uint64_t *idp, uint64_t *offp)
1527885Sahrens {
152810298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1529885Sahrens 	zap_cursor_t cursor;
1530885Sahrens 	zap_attribute_t attr;
1531885Sahrens 
1532885Sahrens 	/* there is no next dir on a snapshot! */
153310298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1534885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1535885Sahrens 		return (ENOENT);
1536885Sahrens 
1537885Sahrens 	zap_cursor_init_serialized(&cursor,
1538885Sahrens 	    dd->dd_pool->dp_meta_objset,
1539885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1540885Sahrens 
1541885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1542885Sahrens 		zap_cursor_fini(&cursor);
1543885Sahrens 		return (ENOENT);
1544885Sahrens 	}
1545885Sahrens 
1546885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1547885Sahrens 		zap_cursor_fini(&cursor);
1548789Sahrens 		return (ENAMETOOLONG);
1549885Sahrens 	}
1550789Sahrens 
1551789Sahrens 	(void) strcpy(name, attr.za_name);
1552885Sahrens 	if (idp)
1553885Sahrens 		*idp = attr.za_first_integer;
1554789Sahrens 	zap_cursor_advance(&cursor);
1555789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1556885Sahrens 	zap_cursor_fini(&cursor);
1557789Sahrens 
1558789Sahrens 	return (0);
1559789Sahrens }
1560789Sahrens 
15617046Sahrens struct findarg {
156211209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
15637046Sahrens 	void *arg;
15647046Sahrens };
15657046Sahrens 
15667046Sahrens /* ARGSUSED */
15677046Sahrens static int
15687046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
15697046Sahrens {
15707046Sahrens 	struct findarg *fa = arg;
157111209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
15727046Sahrens }
15737046Sahrens 
1574789Sahrens /*
1575789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
15767046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1577789Sahrens  */
15782199Sahrens int
157911209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
158011209SMatthew.Ahrens@Sun.COM     int flags)
1581789Sahrens {
15827046Sahrens 	struct findarg fa;
15837046Sahrens 	fa.func = func;
15847046Sahrens 	fa.arg = arg;
15857046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
15867046Sahrens }
15877046Sahrens 
15887046Sahrens /*
15897046Sahrens  * Find all objsets under name, call func on each
15907046Sahrens  */
15917046Sahrens int
15927046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
15937046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
15947046Sahrens {
1595789Sahrens 	dsl_dir_t *dd;
15967046Sahrens 	dsl_pool_t *dp;
15977046Sahrens 	dsl_dataset_t *ds;
1598789Sahrens 	zap_cursor_t zc;
15993978Smmusante 	zap_attribute_t *attr;
1600789Sahrens 	char *child;
16017046Sahrens 	uint64_t thisobj;
16027046Sahrens 	int err;
1603789Sahrens 
16047046Sahrens 	if (name == NULL)
16057046Sahrens 		name = spa_name(spa);
16067046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
16071544Seschrock 	if (err)
16082199Sahrens 		return (err);
1609789Sahrens 
16107046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
16117046Sahrens 	if (dd->dd_myname[0] == '$') {
16127046Sahrens 		dsl_dir_close(dd, FTAG);
16137046Sahrens 		return (0);
16147046Sahrens 	}
16157046Sahrens 
16167046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
16173978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16187046Sahrens 	dp = dd->dd_pool;
1619789Sahrens 
1620789Sahrens 	/*
1621789Sahrens 	 * Iterate over all children.
1622789Sahrens 	 */
16232417Sahrens 	if (flags & DS_FIND_CHILDREN) {
16247046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16252417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
16263978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
16272417Sahrens 		    (void) zap_cursor_advance(&zc)) {
16283978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
16293978Smmusante 			ASSERT(attr->za_num_integers == 1);
1630789Sahrens 
163111165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
16327046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
163311165SMatthew.Ahrens@Sun.COM 			strfree(child);
16342417Sahrens 			if (err)
16352417Sahrens 				break;
16362417Sahrens 		}
16372417Sahrens 		zap_cursor_fini(&zc);
16382199Sahrens 
16392417Sahrens 		if (err) {
16402417Sahrens 			dsl_dir_close(dd, FTAG);
16413978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
16422417Sahrens 			return (err);
16432417Sahrens 		}
1644789Sahrens 	}
1645789Sahrens 
1646789Sahrens 	/*
1647789Sahrens 	 * Iterate over all snapshots.
1648789Sahrens 	 */
16497046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
16507046Sahrens 		if (!dsl_pool_sync_context(dp))
16517046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
16527046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16537046Sahrens 		if (!dsl_pool_sync_context(dp))
16547046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1655789Sahrens 
16567046Sahrens 		if (err == 0) {
16577046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
16587046Sahrens 			dsl_dataset_rele(ds, FTAG);
1659789Sahrens 
16607046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16617046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16627046Sahrens 			    (void) zap_cursor_advance(&zc)) {
16637046Sahrens 				ASSERT(attr->za_integer_length ==
16647046Sahrens 				    sizeof (uint64_t));
16657046Sahrens 				ASSERT(attr->za_num_integers == 1);
1666789Sahrens 
166711165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
166811165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
16697046Sahrens 				err = func(spa, attr->za_first_integer,
16707046Sahrens 				    child, arg);
167111165SMatthew.Ahrens@Sun.COM 				strfree(child);
16727046Sahrens 				if (err)
16737046Sahrens 					break;
16747046Sahrens 			}
16757046Sahrens 			zap_cursor_fini(&zc);
1676789Sahrens 		}
1677789Sahrens 	}
1678789Sahrens 
1679789Sahrens 	dsl_dir_close(dd, FTAG);
16803978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1681789Sahrens 
16822199Sahrens 	if (err)
16832199Sahrens 		return (err);
16842199Sahrens 
1685789Sahrens 	/*
1686789Sahrens 	 * Apply to self if appropriate.
1687789Sahrens 	 */
16887046Sahrens 	err = func(spa, thisobj, name, arg);
16892199Sahrens 	return (err);
1690789Sahrens }
16915326Sek110237 
16928415SRichard.Morris@Sun.COM /* ARGSUSED */
16938415SRichard.Morris@Sun.COM int
169411209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
16958415SRichard.Morris@Sun.COM {
16968415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
16978415SRichard.Morris@Sun.COM 
16988415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
16998415SRichard.Morris@Sun.COM 		return (0);
17008415SRichard.Morris@Sun.COM 
17018415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
17028415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
170310298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
17048415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
17058415SRichard.Morris@Sun.COM 			zbookmark_t zb;
17068415SRichard.Morris@Sun.COM 
170710922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
170810922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
17098415SRichard.Morris@Sun.COM 
171012296SLin.Ling@Sun.COM 			(void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
17118415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
17128415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
17138415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
17148415SRichard.Morris@Sun.COM 			    &aflags, &zb);
17158415SRichard.Morris@Sun.COM 		}
17168415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
17178415SRichard.Morris@Sun.COM 	}
17188415SRichard.Morris@Sun.COM 
17198415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
17208415SRichard.Morris@Sun.COM 	return (0);
17218415SRichard.Morris@Sun.COM }
17228415SRichard.Morris@Sun.COM 
17235326Sek110237 void
17245326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
17255326Sek110237 {
172610298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
172710298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
17285326Sek110237 }
17295326Sek110237 
17305326Sek110237 void *
17315326Sek110237 dmu_objset_get_user(objset_t *os)
17325326Sek110237 {
173310298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
173410298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
17355326Sek110237 }
1736