xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 12684:397e44ebb8a9)
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 
46*12684STom.Erickson@Sun.COM /*
47*12684STom.Erickson@Sun.COM  * Needed to close a window in dnode_move() that allows the objset to be freed
48*12684STom.Erickson@Sun.COM  * before it can be safely accessed.
49*12684STom.Erickson@Sun.COM  */
50*12684STom.Erickson@Sun.COM krwlock_t os_lock;
51*12684STom.Erickson@Sun.COM 
52*12684STom.Erickson@Sun.COM void
53*12684STom.Erickson@Sun.COM dmu_objset_init(void)
54*12684STom.Erickson@Sun.COM {
55*12684STom.Erickson@Sun.COM 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
56*12684STom.Erickson@Sun.COM }
57*12684STom.Erickson@Sun.COM 
58*12684STom.Erickson@Sun.COM void
59*12684STom.Erickson@Sun.COM dmu_objset_fini(void)
60*12684STom.Erickson@Sun.COM {
61*12684STom.Erickson@Sun.COM 	rw_destroy(&os_lock);
62*12684STom.Erickson@Sun.COM }
63*12684STom.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 
37010298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
37110298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
372789Sahrens 
373789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
37410298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
375789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
37610298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
377789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
378789Sahrens 	}
37910298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
380789Sahrens 	    offsetof(dnode_t, dn_link));
38110298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
382789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
383789Sahrens 
38410298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
38510298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
38610298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3872856Snd150628 
388*12684STom.Erickson@Sun.COM 	DMU_META_DNODE(os) = dnode_special_open(os,
389*12684STom.Erickson@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
390*12684STom.Erickson@Sun.COM 	    &os->os_meta_dnode);
39110298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
392*12684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
393*12684STom.Erickson@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
394*12684STom.Erickson@Sun.COM 		    &os->os_userused_dnode);
395*12684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
396*12684STom.Erickson@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
397*12684STom.Erickson@Sun.COM 		    &os->os_groupused_dnode);
3989396SMatthew.Ahrens@Sun.COM 	}
399789Sahrens 
4004787Sahrens 	/*
4014787Sahrens 	 * We should be the only thread trying to do this because we
4024787Sahrens 	 * have ds_opening_lock
4034787Sahrens 	 */
4044787Sahrens 	if (ds) {
40510298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
40610298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
40710298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
40810298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
409789Sahrens 	}
410789Sahrens 
41110298SMatthew.Ahrens@Sun.COM 	*osp = os;
4125367Sahrens 	return (0);
4135367Sahrens }
4145367Sahrens 
4155367Sahrens int
41610298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4175367Sahrens {
41810298SMatthew.Ahrens@Sun.COM 	int err = 0;
41910298SMatthew.Ahrens@Sun.COM 
42010298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
42110298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
42210298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
42310298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
42410298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
42510298SMatthew.Ahrens@Sun.COM 	}
42610298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
42710298SMatthew.Ahrens@Sun.COM 	return (err);
42810298SMatthew.Ahrens@Sun.COM }
42910298SMatthew.Ahrens@Sun.COM 
43010298SMatthew.Ahrens@Sun.COM /* called from zpl */
43110298SMatthew.Ahrens@Sun.COM int
43210298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
43310298SMatthew.Ahrens@Sun.COM {
43410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
4355367Sahrens 	int err;
4365367Sahrens 
43710298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
4385367Sahrens 	if (err)
43910298SMatthew.Ahrens@Sun.COM 		return (err);
44010298SMatthew.Ahrens@Sun.COM 
44110298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
44210298SMatthew.Ahrens@Sun.COM 	if (err)
44310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
44410298SMatthew.Ahrens@Sun.COM 
4455367Sahrens 	return (err);
4465367Sahrens }
4475367Sahrens 
448789Sahrens /* called from zpl */
449789Sahrens int
45010298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
45110298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
452789Sahrens {
453789Sahrens 	dsl_dataset_t *ds;
454789Sahrens 	int err;
455789Sahrens 
45610298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
45710298SMatthew.Ahrens@Sun.COM 	if (err)
45810298SMatthew.Ahrens@Sun.COM 		return (err);
4595367Sahrens 
46010298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
461789Sahrens 	if (err) {
46210298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
46310588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
46410298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
46510298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
46610588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
46710588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
46810588SEric.Taylor@Sun.COM 		return (EROFS);
469789Sahrens 	}
4705367Sahrens 	return (err);
471789Sahrens }
472789Sahrens 
473789Sahrens void
47410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
475789Sahrens {
47610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
47710298SMatthew.Ahrens@Sun.COM }
4786689Smaybee 
47910298SMatthew.Ahrens@Sun.COM void
48010298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
48110298SMatthew.Ahrens@Sun.COM {
48210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
483789Sahrens }
484789Sahrens 
4851646Sperrin int
4864944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4871544Seschrock {
4881544Seschrock 	dnode_t *dn;
4891596Sahrens 
49010298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4911596Sahrens 
4921596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
493*12684STom.Erickson@Sun.COM 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
494*12684STom.Erickson@Sun.COM 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
4951544Seschrock 
4961544Seschrock 	/*
4971596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4981596Sahrens 	 * because dnode_add_ref() only works if you already have a
4991596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
5001596Sahrens 	 * skip.
5011544Seschrock 	 */
50210298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
5034944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
50410298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
5051596Sahrens 		continue;
5061596Sahrens 
5071596Sahrens 	while (dn) {
5081596Sahrens 		dnode_t *next_dn = dn;
5091596Sahrens 
5101596Sahrens 		do {
51110298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
5124944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
5131596Sahrens 
51410298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
5154944Smaybee 		dnode_evict_dbufs(dn);
5161596Sahrens 		dnode_rele(dn, FTAG);
51710298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
5181596Sahrens 		dn = next_dn;
5191544Seschrock 	}
520*12684STom.Erickson@Sun.COM 	dn = list_head(&os->os_dnodes);
52110298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
522*12684STom.Erickson@Sun.COM 	return (dn != DMU_META_DNODE(os));
5231544Seschrock }
5241544Seschrock 
5251544Seschrock void
52610298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
527789Sahrens {
52810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
529789Sahrens 
53010922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
53110922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
532789Sahrens 
5337237Sek110237 	if (ds) {
5347237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
5357237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
53610298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
5377237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
53810298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
5397237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
54010298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
54110922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
54210922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
54310310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
54410310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
54512294SMark.Musante@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "sync",
54612294SMark.Musante@Sun.COM 			    sync_changed_cb, os));
5477237Sek110237 		}
5487237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
54910298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
5507237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
55110298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
552789Sahrens 	}
553789Sahrens 
55411935SMark.Shellenbaum@Sun.COM 	if (os->os_sa)
55511935SMark.Shellenbaum@Sun.COM 		sa_tear_down(os);
55611935SMark.Shellenbaum@Sun.COM 
5571544Seschrock 	/*
5581544Seschrock 	 * We should need only a single pass over the dnode list, since
5591544Seschrock 	 * nothing can be added to the list at this point.
5601544Seschrock 	 */
56110298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5621544Seschrock 
563*12684STom.Erickson@Sun.COM 	dnode_special_close(&os->os_meta_dnode);
564*12684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os)) {
565*12684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_userused_dnode);
566*12684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_groupused_dnode);
5679396SMatthew.Ahrens@Sun.COM 	}
56810298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
569789Sahrens 
57010298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
571789Sahrens 
57210298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
573*12684STom.Erickson@Sun.COM 
574*12684STom.Erickson@Sun.COM 	/*
575*12684STom.Erickson@Sun.COM 	 * This is a barrier to prevent the objset from going away in
576*12684STom.Erickson@Sun.COM 	 * dnode_move() until we can safely ensure that the objset is still in
577*12684STom.Erickson@Sun.COM 	 * use. We consider the objset valid before the barrier and invalid
578*12684STom.Erickson@Sun.COM 	 * after the barrier.
579*12684STom.Erickson@Sun.COM 	 */
580*12684STom.Erickson@Sun.COM 	rw_enter(&os_lock, RW_READER);
581*12684STom.Erickson@Sun.COM 	rw_exit(&os_lock);
582*12684STom.Erickson@Sun.COM 
58310298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
58410298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
58510298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
58610298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
587789Sahrens }
588789Sahrens 
58910373Schris.kirby@sun.com timestruc_t
59010373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
59110373Schris.kirby@sun.com {
59210373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
59310373Schris.kirby@sun.com }
59410373Schris.kirby@sun.com 
595789Sahrens /* called from dsl for meta-objset */
59610298SMatthew.Ahrens@Sun.COM objset_t *
5973547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5983547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
599789Sahrens {
60010298SMatthew.Ahrens@Sun.COM 	objset_t *os;
601789Sahrens 	dnode_t *mdn;
602789Sahrens 
603789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6044787Sahrens 	if (ds)
6054787Sahrens 		mutex_enter(&ds->ds_opening_lock);
60610298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
6074787Sahrens 	if (ds)
6084787Sahrens 		mutex_exit(&ds->ds_opening_lock);
609*12684STom.Erickson@Sun.COM 	mdn = DMU_META_DNODE(os);
610789Sahrens 
611789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
612789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
613789Sahrens 
614789Sahrens 	/*
615789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
616789Sahrens 	 * later, because then we could do it in quescing context while
617789Sahrens 	 * we are also accessing it in open context.
618789Sahrens 	 *
619789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
620789Sahrens 	 * because the MOS is only updated in syncing context.
621789Sahrens 	 * This is most fortunate: the MOS is the only objset that
622789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
623789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
624789Sahrens 	 */
6251544Seschrock 	if (ds != NULL) {
6261544Seschrock 		int levels = 1;
6271544Seschrock 
6281544Seschrock 		/*
6291544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
6301544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
6311544Seschrock 		 */
6321544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
6331544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
6341544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
6351544Seschrock 			levels++;
6361544Seschrock 
637789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
6381544Seschrock 		    mdn->dn_nlevels = levels;
6391544Seschrock 	}
640789Sahrens 
641789Sahrens 	ASSERT(type != DMU_OST_NONE);
642789Sahrens 	ASSERT(type != DMU_OST_ANY);
643789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
64410298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
64510298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
64610298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
64710298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
6489396SMatthew.Ahrens@Sun.COM 	}
649789Sahrens 
650789Sahrens 	dsl_dataset_dirty(ds, tx);
651789Sahrens 
65210298SMatthew.Ahrens@Sun.COM 	return (os);
653789Sahrens }
654789Sahrens 
655789Sahrens struct oscarg {
6564543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
657789Sahrens 	void *userarg;
65810272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
659789Sahrens 	const char *lastname;
660789Sahrens 	dmu_objset_type_t type;
6616492Stimh 	uint64_t flags;
66212296SLin.Ling@Sun.COM 	cred_t *cr;
663789Sahrens };
664789Sahrens 
6654543Smarks /*ARGSUSED*/
666789Sahrens static int
6672199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
668789Sahrens {
6692199Sahrens 	dsl_dir_t *dd = arg1;
6702199Sahrens 	struct oscarg *oa = arg2;
6712199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6722199Sahrens 	int err;
6732199Sahrens 	uint64_t ddobj;
6742199Sahrens 
6752199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6762199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6772199Sahrens 	if (err != ENOENT)
6782199Sahrens 		return (err ? err : EEXIST);
6792199Sahrens 
68010272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
68110272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
68210272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6832199Sahrens 			return (EXDEV);
6842199Sahrens 
68510272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
68610272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6872199Sahrens 			return (EINVAL);
6882199Sahrens 	}
6894543Smarks 
6902199Sahrens 	return (0);
6912199Sahrens }
6922199Sahrens 
6932199Sahrens static void
69412296SLin.Ling@Sun.COM dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
6952199Sahrens {
6962199Sahrens 	dsl_dir_t *dd = arg1;
6972199Sahrens 	struct oscarg *oa = arg2;
6982199Sahrens 	uint64_t dsobj;
699789Sahrens 
700789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
701789Sahrens 
7022199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
70312296SLin.Ling@Sun.COM 	    oa->clone_origin, oa->flags, oa->cr, tx);
704789Sahrens 
70510272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
70610272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
70710272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
70810298SMatthew.Ahrens@Sun.COM 		objset_t *os;
709789Sahrens 
71010272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
71110272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
71210272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
71310272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
71410272SMatthew.Ahrens@Sun.COM 
71510298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
7163547Smaybee 		    ds, bp, oa->type, tx);
717789Sahrens 
718789Sahrens 		if (oa->userfunc)
71912296SLin.Ling@Sun.COM 			oa->userfunc(os, oa->userarg, oa->cr, tx);
72010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
721789Sahrens 	}
7224543Smarks 
72312296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_CREATE, dd->dd_pool->dp_spa,
72412296SLin.Ling@Sun.COM 	    tx, "dataset = %llu", dsobj);
725789Sahrens }
726789Sahrens 
727789Sahrens int
72810272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
7294543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
730789Sahrens {
7312199Sahrens 	dsl_dir_t *pdd;
732789Sahrens 	const char *tail;
733789Sahrens 	int err = 0;
7342199Sahrens 	struct oscarg oa = { 0 };
735789Sahrens 
7362199Sahrens 	ASSERT(strchr(name, '@') == NULL);
7372199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
7381544Seschrock 	if (err)
7391544Seschrock 		return (err);
740789Sahrens 	if (tail == NULL) {
7412199Sahrens 		dsl_dir_close(pdd, FTAG);
742789Sahrens 		return (EEXIST);
743789Sahrens 	}
744789Sahrens 
7452199Sahrens 	oa.userfunc = func;
7462199Sahrens 	oa.userarg = arg;
7472199Sahrens 	oa.lastname = tail;
7482199Sahrens 	oa.type = type;
7496492Stimh 	oa.flags = flags;
75012296SLin.Ling@Sun.COM 	oa.cr = CRED();
7514543Smarks 
75210272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
75310272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
75410272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
75510272SMatthew.Ahrens@Sun.COM 	return (err);
75610272SMatthew.Ahrens@Sun.COM }
75710272SMatthew.Ahrens@Sun.COM 
75810272SMatthew.Ahrens@Sun.COM int
75910272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
76010272SMatthew.Ahrens@Sun.COM {
76110272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
76210272SMatthew.Ahrens@Sun.COM 	const char *tail;
76310272SMatthew.Ahrens@Sun.COM 	int err = 0;
76410272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
76510272SMatthew.Ahrens@Sun.COM 
76610272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
76710272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
76810272SMatthew.Ahrens@Sun.COM 	if (err)
76910272SMatthew.Ahrens@Sun.COM 		return (err);
77010272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
77110272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
77210272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
773789Sahrens 	}
77410272SMatthew.Ahrens@Sun.COM 
77510272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
77610272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
77710272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
77812296SLin.Ling@Sun.COM 	oa.cr = CRED();
77910272SMatthew.Ahrens@Sun.COM 
7802199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7812199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7822199Sahrens 	dsl_dir_close(pdd, FTAG);
783789Sahrens 	return (err);
784789Sahrens }
785789Sahrens 
786789Sahrens int
78710242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
788789Sahrens {
78910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
790789Sahrens 	int error;
791789Sahrens 
792789Sahrens 	/*
79310272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
79410272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
79510272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
79610272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
79710272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
798789Sahrens 	 */
79910298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
800789Sahrens 	if (error == 0) {
80110298SMatthew.Ahrens@Sun.COM 		objset_t *os;
80210298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
80310298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
80410298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
80510272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
806789Sahrens 	}
807789Sahrens 
8085367Sahrens 	return (error);
809789Sahrens }
810789Sahrens 
8112199Sahrens struct snaparg {
8122199Sahrens 	dsl_sync_task_group_t *dstg;
8132199Sahrens 	char *snapname;
8142199Sahrens 	char failed[MAXPATHLEN];
81511620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
8169355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
8175367Sahrens };
8185367Sahrens 
8199355SMatthew.Ahrens@Sun.COM static int
8209355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
8219355SMatthew.Ahrens@Sun.COM {
8229355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
8239355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8249355SMatthew.Ahrens@Sun.COM 
8259355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
8269355SMatthew.Ahrens@Sun.COM 
82710298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
8289355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
8299355SMatthew.Ahrens@Sun.COM }
8309355SMatthew.Ahrens@Sun.COM 
8319355SMatthew.Ahrens@Sun.COM static void
83212296SLin.Ling@Sun.COM snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
8339355SMatthew.Ahrens@Sun.COM {
8349355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
83510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
8369355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8379355SMatthew.Ahrens@Sun.COM 
83812296SLin.Ling@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, tx);
8399355SMatthew.Ahrens@Sun.COM 
84011022STom.Erickson@Sun.COM 	if (sn->props) {
84111022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
84211022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
84311022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
84412296SLin.Ling@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, tx);
84511022STom.Erickson@Sun.COM 	}
8469355SMatthew.Ahrens@Sun.COM }
8472199Sahrens 
8482199Sahrens static int
84911209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
8502199Sahrens {
8512199Sahrens 	struct snaparg *sn = arg;
8522199Sahrens 	objset_t *os;
8532199Sahrens 	int err;
85411620SMatthew.Ahrens@Sun.COM 	char *cp;
85511620SMatthew.Ahrens@Sun.COM 
85611620SMatthew.Ahrens@Sun.COM 	/*
85711620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
85811620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
85911620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
86011620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
86111620SMatthew.Ahrens@Sun.COM 	 */
86211620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
86311620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
86411620SMatthew.Ahrens@Sun.COM 		return (0);
8652199Sahrens 
8662199Sahrens 	(void) strcpy(sn->failed, name);
8672199Sahrens 
8684543Smarks 	/*
86911620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
87011620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
87111620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
8724543Smarks 	 */
87311620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
8744543Smarks 		return (err);
8754543Smarks 
87610298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
8772199Sahrens 	if (err != 0)
8782199Sahrens 		return (err);
8792199Sahrens 
88011620SMatthew.Ahrens@Sun.COM 	/*
88111620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
88211620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
88311620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
88411620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
88511620SMatthew.Ahrens@Sun.COM 	 */
88610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
88710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
88811620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
8893637Srm160521 	}
8903637Srm160521 
8913637Srm160521 	/*
8922199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8932199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8942199Sahrens 	 * a side effect.
8952199Sahrens 	 */
8962199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8972199Sahrens 	if (err == 0) {
8989355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
8999355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
9003637Srm160521 	} else {
90110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
9022199Sahrens 	}
9033637Srm160521 
9042199Sahrens 	return (err);
9052199Sahrens }
9062199Sahrens 
9072199Sahrens int
9089355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
9099355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
9102199Sahrens {
9112199Sahrens 	dsl_sync_task_t *dst;
9129355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
9132199Sahrens 	spa_t *spa;
9142199Sahrens 	int err;
9152199Sahrens 
9162199Sahrens 	(void) strcpy(sn.failed, fsname);
9172199Sahrens 
9184603Sahrens 	err = spa_open(fsname, &spa, FTAG);
9192199Sahrens 	if (err)
9202199Sahrens 		return (err);
9212199Sahrens 
9222199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9232199Sahrens 	sn.snapname = snapname;
9249355SMatthew.Ahrens@Sun.COM 	sn.props = props;
92511620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
9262199Sahrens 
9272417Sahrens 	if (recursive) {
9282417Sahrens 		err = dmu_objset_find(fsname,
9292417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
9302417Sahrens 	} else {
9312199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
9322417Sahrens 	}
9332199Sahrens 
9349355SMatthew.Ahrens@Sun.COM 	if (err == 0)
9359355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
9362199Sahrens 
9372199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
9382199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
9399355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
94010298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
9412199Sahrens 		if (dst->dst_err)
9425367Sahrens 			dsl_dataset_name(ds, sn.failed);
9439355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
94410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
9452199Sahrens 	}
9465367Sahrens 
9472199Sahrens 	if (err)
9482199Sahrens 		(void) strcpy(fsname, sn.failed);
9492199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
9502199Sahrens 	spa_close(spa, FTAG);
9512199Sahrens 	return (err);
9522199Sahrens }
9532199Sahrens 
954789Sahrens static void
9559396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
956789Sahrens {
9573547Smaybee 	dnode_t *dn;
958789Sahrens 
9593547Smaybee 	while (dn = list_head(list)) {
9603547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
9613547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
9623547Smaybee 		/*
9639396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
9649396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
9653547Smaybee 		 */
9663547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
9673547Smaybee 		ASSERT(dn->dn_zio);
968789Sahrens 
9693547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
9703547Smaybee 		list_remove(list, dn);
9719396SMatthew.Ahrens@Sun.COM 
9729396SMatthew.Ahrens@Sun.COM 		if (newlist) {
9739396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
9749396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
9759396SMatthew.Ahrens@Sun.COM 		}
9769396SMatthew.Ahrens@Sun.COM 
9773547Smaybee 		dnode_sync(dn, tx);
9783547Smaybee 	}
9793547Smaybee }
9802981Sahrens 
9813547Smaybee /* ARGSUSED */
9823547Smaybee static void
98310922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
9843547Smaybee {
9857754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
98610298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
9873547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
9882981Sahrens 
9897754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
9907754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
9917754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9925329Sgw25295 
9933547Smaybee 	/*
9949396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
9959396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
9969396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
9979396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
9983547Smaybee 	 */
9999396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
10007754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10013547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
100210922SJeff.Bonwick@Sun.COM }
100310922SJeff.Bonwick@Sun.COM 
100410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
100510922SJeff.Bonwick@Sun.COM static void
100610922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
100710922SJeff.Bonwick@Sun.COM {
100810922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
100910922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
101010922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
10115329Sgw25295 
10127754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
101310922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
10147754SJeff.Bonwick@Sun.COM 	} else {
101510922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
101610922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
101710922SJeff.Bonwick@Sun.COM 
101810922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
101910922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
10205329Sgw25295 	}
1021789Sahrens }
1022789Sahrens 
1023789Sahrens /* called from dsl */
1024789Sahrens void
102510298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1026789Sahrens {
1027789Sahrens 	int txgoff;
10281544Seschrock 	zbookmark_t zb;
102910922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
10303547Smaybee 	zio_t *zio;
10313547Smaybee 	list_t *list;
10329396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
10333547Smaybee 	dbuf_dirty_record_t *dr;
10343547Smaybee 
10353547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1036789Sahrens 
1037789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1038789Sahrens 	/* XXX the write_done callback should really give us the tx... */
1039789Sahrens 	os->os_synctx = tx;
1040789Sahrens 
10413882Sahrens 	if (os->os_dsl_dataset == NULL) {
10423882Sahrens 		/*
10433882Sahrens 		 * This is the MOS.  If we have upgraded,
10443882Sahrens 		 * spa_max_replication() could change, so reset
10453882Sahrens 		 * os_copies here.
10463882Sahrens 		 */
10473882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
10483882Sahrens 	}
10493882Sahrens 
10503547Smaybee 	/*
10513547Smaybee 	 * Create the root block IO
10523547Smaybee 	 */
105310922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
105410922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
105510922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
105612296SLin.Ling@Sun.COM 	VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
105712296SLin.Ling@Sun.COM 	    os->os_rootbp, os->os_spa, &zb));
105810922SJeff.Bonwick@Sun.COM 
105910922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
106010922SJeff.Bonwick@Sun.COM 
106110922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
106210922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
106310922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
10647754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
10653547Smaybee 
10663547Smaybee 	/*
10679396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
10683547Smaybee 	 */
1069*12684STom.Erickson@Sun.COM 	DMU_META_DNODE(os)->dn_zio = zio;
1070*12684STom.Erickson@Sun.COM 	dnode_sync(DMU_META_DNODE(os), tx);
1071789Sahrens 
10729396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
10739396SMatthew.Ahrens@Sun.COM 
1074*12684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os) &&
1075*12684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1076*12684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1077*12684STom.Erickson@Sun.COM 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1078*12684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1079*12684STom.Erickson@Sun.COM 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
10809396SMatthew.Ahrens@Sun.COM 	}
10819396SMatthew.Ahrens@Sun.COM 
1082789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1083789Sahrens 
10849396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
10859396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
10869396SMatthew.Ahrens@Sun.COM 		/*
10879396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
10889396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
10899396SMatthew.Ahrens@Sun.COM 		 */
10909396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
10919396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
10929396SMatthew.Ahrens@Sun.COM 	}
10939396SMatthew.Ahrens@Sun.COM 
10949396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
10959396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1096789Sahrens 
1097*12684STom.Erickson@Sun.COM 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
10983547Smaybee 	while (dr = list_head(list)) {
10993547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
11003547Smaybee 		list_remove(list, dr);
11013547Smaybee 		if (dr->dr_zio)
11023547Smaybee 			zio_nowait(dr->dr_zio);
11033547Smaybee 	}
1104789Sahrens 	/*
1105789Sahrens 	 * Free intent log blocks up to this tx.
1106789Sahrens 	 */
1107789Sahrens 	zil_sync(os->os_zil, tx);
11087046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
11093547Smaybee 	zio_nowait(zio);
1110789Sahrens }
1111789Sahrens 
111210922SJeff.Bonwick@Sun.COM boolean_t
111310922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
111410922SJeff.Bonwick@Sun.COM {
111510922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
111610922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
111710922SJeff.Bonwick@Sun.COM }
111810922SJeff.Bonwick@Sun.COM 
1119*12684STom.Erickson@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
11209396SMatthew.Ahrens@Sun.COM 
11219396SMatthew.Ahrens@Sun.COM void
11229396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
11239396SMatthew.Ahrens@Sun.COM {
11249396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
11259396SMatthew.Ahrens@Sun.COM }
11269396SMatthew.Ahrens@Sun.COM 
11279396SMatthew.Ahrens@Sun.COM boolean_t
112810298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
11299396SMatthew.Ahrens@Sun.COM {
11309396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1131*12684STom.Erickson@Sun.COM 	    used_cbs[os->os_phys->os_type] != NULL &&
1132*12684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os) != NULL);
11339396SMatthew.Ahrens@Sun.COM }
11349396SMatthew.Ahrens@Sun.COM 
113510407SMatthew.Ahrens@Sun.COM static void
113611935SMark.Shellenbaum@Sun.COM do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
113711935SMark.Shellenbaum@Sun.COM     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
113810407SMatthew.Ahrens@Sun.COM {
113911935SMark.Shellenbaum@Sun.COM 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
114011935SMark.Shellenbaum@Sun.COM 		int64_t delta = DNODE_SIZE + used;
114110407SMatthew.Ahrens@Sun.COM 		if (subtract)
114210407SMatthew.Ahrens@Sun.COM 			delta = -delta;
114310801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
114410407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
114510801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
114610407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
114710407SMatthew.Ahrens@Sun.COM 	}
114810407SMatthew.Ahrens@Sun.COM }
114910407SMatthew.Ahrens@Sun.COM 
11509396SMatthew.Ahrens@Sun.COM void
115111935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
11529396SMatthew.Ahrens@Sun.COM {
11539396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
11549396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
11559396SMatthew.Ahrens@Sun.COM 
11569396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
11579396SMatthew.Ahrens@Sun.COM 
11589396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
115912493SMark.Shellenbaum@Oracle.COM 		int flags;
11609396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
11619396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
11629396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
11639396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
11649396SMatthew.Ahrens@Sun.COM 
11659396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
1166*12684STom.Erickson@Sun.COM 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
116710298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11689396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
11699396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
117010298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11719396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
11729396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
11739396SMatthew.Ahrens@Sun.COM 		}
11749396SMatthew.Ahrens@Sun.COM 
11759396SMatthew.Ahrens@Sun.COM 		/*
117610407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
117711935SMark.Shellenbaum@Sun.COM 		 * net delta is zero.  Otherwise
117810407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
117910407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
118010407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
11819396SMatthew.Ahrens@Sun.COM 		 */
11829396SMatthew.Ahrens@Sun.COM 
118312493SMark.Shellenbaum@Oracle.COM 		flags = dn->dn_id_flags;
118412493SMark.Shellenbaum@Oracle.COM 		ASSERT(flags);
118512493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_OLD_EXIST)  {
118611935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
118711935SMark.Shellenbaum@Sun.COM 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
118811935SMark.Shellenbaum@Sun.COM 		}
118912493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_NEW_EXIST) {
119011935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
119111935SMark.Shellenbaum@Sun.COM 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
119211935SMark.Shellenbaum@Sun.COM 			    dn->dn_newgid, B_FALSE, tx);
119311935SMark.Shellenbaum@Sun.COM 		}
119411935SMark.Shellenbaum@Sun.COM 
119512493SMark.Shellenbaum@Oracle.COM 		mutex_enter(&dn->dn_mtx);
119611935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = 0;
119711935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = 0;
119811935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
119911935SMark.Shellenbaum@Sun.COM 			dn->dn_olduid = dn->dn_newuid;
120011935SMark.Shellenbaum@Sun.COM 			dn->dn_oldgid = dn->dn_newgid;
120111935SMark.Shellenbaum@Sun.COM 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
120211935SMark.Shellenbaum@Sun.COM 			if (dn->dn_bonuslen == 0)
120311935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
120411935SMark.Shellenbaum@Sun.COM 			else
120511935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
120611935SMark.Shellenbaum@Sun.COM 		}
120712432SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
12089396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
12099396SMatthew.Ahrens@Sun.COM 
12109396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
12119396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
12129396SMatthew.Ahrens@Sun.COM 	}
12139396SMatthew.Ahrens@Sun.COM }
12149396SMatthew.Ahrens@Sun.COM 
121512178SMark.Shellenbaum@Sun.COM /*
121612178SMark.Shellenbaum@Sun.COM  * Returns a pointer to data to find uid/gid from
121712178SMark.Shellenbaum@Sun.COM  *
121812178SMark.Shellenbaum@Sun.COM  * If a dirty record for transaction group that is syncing can't
121912178SMark.Shellenbaum@Sun.COM  * be found then NULL is returned.  In the NULL case it is assumed
122012178SMark.Shellenbaum@Sun.COM  * the uid/gid aren't changing.
122112178SMark.Shellenbaum@Sun.COM  */
122212178SMark.Shellenbaum@Sun.COM static void *
122312178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
122412178SMark.Shellenbaum@Sun.COM {
122512178SMark.Shellenbaum@Sun.COM 	dbuf_dirty_record_t *dr, **drp;
122612178SMark.Shellenbaum@Sun.COM 	void *data;
122712178SMark.Shellenbaum@Sun.COM 
122812178SMark.Shellenbaum@Sun.COM 	if (db->db_dirtycnt == 0)
122912178SMark.Shellenbaum@Sun.COM 		return (db->db.db_data);  /* Nothing is changing */
123012178SMark.Shellenbaum@Sun.COM 
123112178SMark.Shellenbaum@Sun.COM 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
123212178SMark.Shellenbaum@Sun.COM 		if (dr->dr_txg == tx->tx_txg)
123312178SMark.Shellenbaum@Sun.COM 			break;
123412178SMark.Shellenbaum@Sun.COM 
1235*12684STom.Erickson@Sun.COM 	if (dr == NULL) {
123612178SMark.Shellenbaum@Sun.COM 		data = NULL;
1237*12684STom.Erickson@Sun.COM 	} else {
1238*12684STom.Erickson@Sun.COM 		dnode_t *dn;
1239*12684STom.Erickson@Sun.COM 
1240*12684STom.Erickson@Sun.COM 		DB_DNODE_ENTER(dr->dr_dbuf);
1241*12684STom.Erickson@Sun.COM 		dn = DB_DNODE(dr->dr_dbuf);
1242*12684STom.Erickson@Sun.COM 
1243*12684STom.Erickson@Sun.COM 		if (dn->dn_bonuslen == 0 &&
1244*12684STom.Erickson@Sun.COM 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1245*12684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data->b_data;
1246*12684STom.Erickson@Sun.COM 		else
1247*12684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data;
1248*12684STom.Erickson@Sun.COM 
1249*12684STom.Erickson@Sun.COM 		DB_DNODE_EXIT(dr->dr_dbuf);
1250*12684STom.Erickson@Sun.COM 	}
1251*12684STom.Erickson@Sun.COM 
125212178SMark.Shellenbaum@Sun.COM 	return (data);
125312178SMark.Shellenbaum@Sun.COM }
125412178SMark.Shellenbaum@Sun.COM 
125511935SMark.Shellenbaum@Sun.COM void
125612178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
125711935SMark.Shellenbaum@Sun.COM {
125811935SMark.Shellenbaum@Sun.COM 	objset_t *os = dn->dn_objset;
125911935SMark.Shellenbaum@Sun.COM 	void *data = NULL;
126012178SMark.Shellenbaum@Sun.COM 	dmu_buf_impl_t *db = NULL;
126111935SMark.Shellenbaum@Sun.COM 	uint64_t *user, *group;
126211935SMark.Shellenbaum@Sun.COM 	int flags = dn->dn_id_flags;
126311935SMark.Shellenbaum@Sun.COM 	int error;
126412178SMark.Shellenbaum@Sun.COM 	boolean_t have_spill = B_FALSE;
126511935SMark.Shellenbaum@Sun.COM 
126611935SMark.Shellenbaum@Sun.COM 	if (!dmu_objset_userused_enabled(dn->dn_objset))
126711935SMark.Shellenbaum@Sun.COM 		return;
126811935SMark.Shellenbaum@Sun.COM 
126911935SMark.Shellenbaum@Sun.COM 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
127011935SMark.Shellenbaum@Sun.COM 	    DN_ID_CHKED_SPILL)))
127111935SMark.Shellenbaum@Sun.COM 		return;
127211935SMark.Shellenbaum@Sun.COM 
127311935SMark.Shellenbaum@Sun.COM 	if (before && dn->dn_bonuslen != 0)
127411935SMark.Shellenbaum@Sun.COM 		data = DN_BONUS(dn->dn_phys);
127512178SMark.Shellenbaum@Sun.COM 	else if (!before && dn->dn_bonuslen != 0) {
127612178SMark.Shellenbaum@Sun.COM 		if (dn->dn_bonus) {
127712178SMark.Shellenbaum@Sun.COM 			db = dn->dn_bonus;
127812178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
127912178SMark.Shellenbaum@Sun.COM 			data = dmu_objset_userquota_find_data(db, tx);
128012178SMark.Shellenbaum@Sun.COM 		} else {
128112178SMark.Shellenbaum@Sun.COM 			data = DN_BONUS(dn->dn_phys);
128212178SMark.Shellenbaum@Sun.COM 		}
128312178SMark.Shellenbaum@Sun.COM 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
128411935SMark.Shellenbaum@Sun.COM 			int rf = 0;
128511935SMark.Shellenbaum@Sun.COM 
128611935SMark.Shellenbaum@Sun.COM 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
128711935SMark.Shellenbaum@Sun.COM 				rf |= DB_RF_HAVESTRUCT;
128812493SMark.Shellenbaum@Oracle.COM 			error = dmu_spill_hold_by_dnode(dn,
128912493SMark.Shellenbaum@Oracle.COM 			    rf | DB_RF_MUST_SUCCEED,
129012178SMark.Shellenbaum@Sun.COM 			    FTAG, (dmu_buf_t **)&db);
129111935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
129212178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
129312178SMark.Shellenbaum@Sun.COM 			data = (before) ? db->db.db_data :
129412178SMark.Shellenbaum@Sun.COM 			    dmu_objset_userquota_find_data(db, tx);
129512178SMark.Shellenbaum@Sun.COM 			have_spill = B_TRUE;
129611935SMark.Shellenbaum@Sun.COM 	} else {
129711935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
129811935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
129911935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
130011935SMark.Shellenbaum@Sun.COM 		return;
130111935SMark.Shellenbaum@Sun.COM 	}
130211935SMark.Shellenbaum@Sun.COM 
130311935SMark.Shellenbaum@Sun.COM 	if (before) {
130412178SMark.Shellenbaum@Sun.COM 		ASSERT(data);
130511935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_olduid;
130611935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_oldgid;
130712178SMark.Shellenbaum@Sun.COM 	} else if (data) {
130811935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_newuid;
130911935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_newgid;
131011935SMark.Shellenbaum@Sun.COM 	}
131111935SMark.Shellenbaum@Sun.COM 
131212178SMark.Shellenbaum@Sun.COM 	/*
131312178SMark.Shellenbaum@Sun.COM 	 * Must always call the callback in case the object
131412178SMark.Shellenbaum@Sun.COM 	 * type has changed and that type isn't an object type to track
131512178SMark.Shellenbaum@Sun.COM 	 */
131611935SMark.Shellenbaum@Sun.COM 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
131711935SMark.Shellenbaum@Sun.COM 	    user, group);
131811935SMark.Shellenbaum@Sun.COM 
131912178SMark.Shellenbaum@Sun.COM 	/*
132012178SMark.Shellenbaum@Sun.COM 	 * Preserve existing uid/gid when the callback can't determine
132112178SMark.Shellenbaum@Sun.COM 	 * what the new uid/gid are and the callback returned EEXIST.
132212178SMark.Shellenbaum@Sun.COM 	 * The EEXIST error tells us to just use the existing uid/gid.
132312178SMark.Shellenbaum@Sun.COM 	 * If we don't know what the old values are then just assign
132412178SMark.Shellenbaum@Sun.COM 	 * them to 0, since that is a new file  being created.
132512178SMark.Shellenbaum@Sun.COM 	 */
132612178SMark.Shellenbaum@Sun.COM 	if (!before && data == NULL && error == EEXIST) {
132712178SMark.Shellenbaum@Sun.COM 		if (flags & DN_ID_OLD_EXIST) {
132812178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = dn->dn_olduid;
132912178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = dn->dn_oldgid;
133012178SMark.Shellenbaum@Sun.COM 		} else {
133112178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = 0;
133212178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = 0;
133312178SMark.Shellenbaum@Sun.COM 		}
133412178SMark.Shellenbaum@Sun.COM 		error = 0;
133512178SMark.Shellenbaum@Sun.COM 	}
133612178SMark.Shellenbaum@Sun.COM 
133712178SMark.Shellenbaum@Sun.COM 	if (db)
133812178SMark.Shellenbaum@Sun.COM 		mutex_exit(&db->db_mtx);
133912178SMark.Shellenbaum@Sun.COM 
134011935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
134111935SMark.Shellenbaum@Sun.COM 	if (error == 0 && before)
134211935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
134311935SMark.Shellenbaum@Sun.COM 	if (error == 0 && !before)
134411935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
134511935SMark.Shellenbaum@Sun.COM 
134612178SMark.Shellenbaum@Sun.COM 	if (have_spill) {
134711935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
134811935SMark.Shellenbaum@Sun.COM 	} else {
134911935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
135011935SMark.Shellenbaum@Sun.COM 	}
135111935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
135212178SMark.Shellenbaum@Sun.COM 	if (have_spill)
135312178SMark.Shellenbaum@Sun.COM 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
135411935SMark.Shellenbaum@Sun.COM }
135511935SMark.Shellenbaum@Sun.COM 
13569396SMatthew.Ahrens@Sun.COM boolean_t
13579396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
13589396SMatthew.Ahrens@Sun.COM {
135910298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
13609396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
13619396SMatthew.Ahrens@Sun.COM }
13629396SMatthew.Ahrens@Sun.COM 
13639396SMatthew.Ahrens@Sun.COM int
13649396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
13659396SMatthew.Ahrens@Sun.COM {
13669396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
13679396SMatthew.Ahrens@Sun.COM 	int err = 0;
13689396SMatthew.Ahrens@Sun.COM 
13699396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
13709396SMatthew.Ahrens@Sun.COM 		return (0);
137110298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
13729396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
13739396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
13749396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
13759396SMatthew.Ahrens@Sun.COM 
13769396SMatthew.Ahrens@Sun.COM 	/*
13779396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
13789396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
13799396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
13809396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
13819396SMatthew.Ahrens@Sun.COM 	 * independently.
13829396SMatthew.Ahrens@Sun.COM 	 */
13839396SMatthew.Ahrens@Sun.COM 
13849396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
13859951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
13869396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
13879396SMatthew.Ahrens@Sun.COM 		int objerr;
13889396SMatthew.Ahrens@Sun.COM 
13899396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
13909396SMatthew.Ahrens@Sun.COM 			return (EINTR);
13919396SMatthew.Ahrens@Sun.COM 
13929396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13939396SMatthew.Ahrens@Sun.COM 		if (objerr)
13949396SMatthew.Ahrens@Sun.COM 			continue;
13959951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
13969396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
13979396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
13989396SMatthew.Ahrens@Sun.COM 		if (objerr) {
13999396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
14009396SMatthew.Ahrens@Sun.COM 			continue;
14019396SMatthew.Ahrens@Sun.COM 		}
14029396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
14039396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
14049396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
14059396SMatthew.Ahrens@Sun.COM 	}
14069396SMatthew.Ahrens@Sun.COM 
140710298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
14089396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
14099396SMatthew.Ahrens@Sun.COM 	return (0);
14109396SMatthew.Ahrens@Sun.COM }
14119396SMatthew.Ahrens@Sun.COM 
1412789Sahrens void
14132885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
14142885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
14152885Sahrens {
141610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
14172885Sahrens 	    usedobjsp, availobjsp);
14182885Sahrens }
14192885Sahrens 
14202885Sahrens uint64_t
14212885Sahrens dmu_objset_fsid_guid(objset_t *os)
14222885Sahrens {
142310298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
14242885Sahrens }
14252885Sahrens 
14262885Sahrens void
14272885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1428789Sahrens {
142910298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
143010298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
143110298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
14322885Sahrens }
14332885Sahrens 
14342885Sahrens void
14352885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
14362885Sahrens {
143710298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
143810298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
14392885Sahrens 
144010298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
144110298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
14422885Sahrens 
14432885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
144410298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
14459396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
14469396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1447789Sahrens }
1448789Sahrens 
1449789Sahrens int
1450789Sahrens dmu_objset_is_snapshot(objset_t *os)
1451789Sahrens {
145210298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
145310298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1454789Sahrens 	else
1455789Sahrens 		return (B_FALSE);
1456789Sahrens }
1457789Sahrens 
1458789Sahrens int
14596492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
14606492Stimh     boolean_t *conflict)
14616492Stimh {
146210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
14636492Stimh 	uint64_t ignored;
14646492Stimh 
14656492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
14666492Stimh 		return (ENOENT);
14676492Stimh 
14686492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
14696492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
14706492Stimh 	    real, maxlen, conflict));
14716492Stimh }
14726492Stimh 
14736492Stimh int
1474789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
14755663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1476789Sahrens {
147710298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1478789Sahrens 	zap_cursor_t cursor;
1479789Sahrens 	zap_attribute_t attr;
1480789Sahrens 
1481789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1482789Sahrens 		return (ENOENT);
1483789Sahrens 
1484789Sahrens 	zap_cursor_init_serialized(&cursor,
1485789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1486789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1487789Sahrens 
1488885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1489885Sahrens 		zap_cursor_fini(&cursor);
1490885Sahrens 		return (ENOENT);
1491885Sahrens 	}
1492885Sahrens 
1493885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1494885Sahrens 		zap_cursor_fini(&cursor);
1495885Sahrens 		return (ENAMETOOLONG);
1496885Sahrens 	}
1497885Sahrens 
1498885Sahrens 	(void) strcpy(name, attr.za_name);
1499885Sahrens 	if (idp)
1500885Sahrens 		*idp = attr.za_first_integer;
15015663Sck153898 	if (case_conflict)
15025663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1503885Sahrens 	zap_cursor_advance(&cursor);
1504885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1505885Sahrens 	zap_cursor_fini(&cursor);
1506885Sahrens 
1507885Sahrens 	return (0);
1508885Sahrens }
1509885Sahrens 
1510885Sahrens int
1511885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1512885Sahrens     uint64_t *idp, uint64_t *offp)
1513885Sahrens {
151410298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1515885Sahrens 	zap_cursor_t cursor;
1516885Sahrens 	zap_attribute_t attr;
1517885Sahrens 
1518885Sahrens 	/* there is no next dir on a snapshot! */
151910298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1520885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1521885Sahrens 		return (ENOENT);
1522885Sahrens 
1523885Sahrens 	zap_cursor_init_serialized(&cursor,
1524885Sahrens 	    dd->dd_pool->dp_meta_objset,
1525885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1526885Sahrens 
1527885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1528885Sahrens 		zap_cursor_fini(&cursor);
1529885Sahrens 		return (ENOENT);
1530885Sahrens 	}
1531885Sahrens 
1532885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1533885Sahrens 		zap_cursor_fini(&cursor);
1534789Sahrens 		return (ENAMETOOLONG);
1535885Sahrens 	}
1536789Sahrens 
1537789Sahrens 	(void) strcpy(name, attr.za_name);
1538885Sahrens 	if (idp)
1539885Sahrens 		*idp = attr.za_first_integer;
1540789Sahrens 	zap_cursor_advance(&cursor);
1541789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1542885Sahrens 	zap_cursor_fini(&cursor);
1543789Sahrens 
1544789Sahrens 	return (0);
1545789Sahrens }
1546789Sahrens 
15477046Sahrens struct findarg {
154811209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
15497046Sahrens 	void *arg;
15507046Sahrens };
15517046Sahrens 
15527046Sahrens /* ARGSUSED */
15537046Sahrens static int
15547046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
15557046Sahrens {
15567046Sahrens 	struct findarg *fa = arg;
155711209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
15587046Sahrens }
15597046Sahrens 
1560789Sahrens /*
1561789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
15627046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1563789Sahrens  */
15642199Sahrens int
156511209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
156611209SMatthew.Ahrens@Sun.COM     int flags)
1567789Sahrens {
15687046Sahrens 	struct findarg fa;
15697046Sahrens 	fa.func = func;
15707046Sahrens 	fa.arg = arg;
15717046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
15727046Sahrens }
15737046Sahrens 
15747046Sahrens /*
15757046Sahrens  * Find all objsets under name, call func on each
15767046Sahrens  */
15777046Sahrens int
15787046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
15797046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
15807046Sahrens {
1581789Sahrens 	dsl_dir_t *dd;
15827046Sahrens 	dsl_pool_t *dp;
15837046Sahrens 	dsl_dataset_t *ds;
1584789Sahrens 	zap_cursor_t zc;
15853978Smmusante 	zap_attribute_t *attr;
1586789Sahrens 	char *child;
15877046Sahrens 	uint64_t thisobj;
15887046Sahrens 	int err;
1589789Sahrens 
15907046Sahrens 	if (name == NULL)
15917046Sahrens 		name = spa_name(spa);
15927046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
15931544Seschrock 	if (err)
15942199Sahrens 		return (err);
1595789Sahrens 
15967046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
15977046Sahrens 	if (dd->dd_myname[0] == '$') {
15987046Sahrens 		dsl_dir_close(dd, FTAG);
15997046Sahrens 		return (0);
16007046Sahrens 	}
16017046Sahrens 
16027046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
16033978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16047046Sahrens 	dp = dd->dd_pool;
1605789Sahrens 
1606789Sahrens 	/*
1607789Sahrens 	 * Iterate over all children.
1608789Sahrens 	 */
16092417Sahrens 	if (flags & DS_FIND_CHILDREN) {
16107046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16112417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
16123978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
16132417Sahrens 		    (void) zap_cursor_advance(&zc)) {
16143978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
16153978Smmusante 			ASSERT(attr->za_num_integers == 1);
1616789Sahrens 
161711165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
16187046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
161911165SMatthew.Ahrens@Sun.COM 			strfree(child);
16202417Sahrens 			if (err)
16212417Sahrens 				break;
16222417Sahrens 		}
16232417Sahrens 		zap_cursor_fini(&zc);
16242199Sahrens 
16252417Sahrens 		if (err) {
16262417Sahrens 			dsl_dir_close(dd, FTAG);
16273978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
16282417Sahrens 			return (err);
16292417Sahrens 		}
1630789Sahrens 	}
1631789Sahrens 
1632789Sahrens 	/*
1633789Sahrens 	 * Iterate over all snapshots.
1634789Sahrens 	 */
16357046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
16367046Sahrens 		if (!dsl_pool_sync_context(dp))
16377046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
16387046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16397046Sahrens 		if (!dsl_pool_sync_context(dp))
16407046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1641789Sahrens 
16427046Sahrens 		if (err == 0) {
16437046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
16447046Sahrens 			dsl_dataset_rele(ds, FTAG);
1645789Sahrens 
16467046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16477046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16487046Sahrens 			    (void) zap_cursor_advance(&zc)) {
16497046Sahrens 				ASSERT(attr->za_integer_length ==
16507046Sahrens 				    sizeof (uint64_t));
16517046Sahrens 				ASSERT(attr->za_num_integers == 1);
1652789Sahrens 
165311165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
165411165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
16557046Sahrens 				err = func(spa, attr->za_first_integer,
16567046Sahrens 				    child, arg);
165711165SMatthew.Ahrens@Sun.COM 				strfree(child);
16587046Sahrens 				if (err)
16597046Sahrens 					break;
16607046Sahrens 			}
16617046Sahrens 			zap_cursor_fini(&zc);
1662789Sahrens 		}
1663789Sahrens 	}
1664789Sahrens 
1665789Sahrens 	dsl_dir_close(dd, FTAG);
16663978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1667789Sahrens 
16682199Sahrens 	if (err)
16692199Sahrens 		return (err);
16702199Sahrens 
1671789Sahrens 	/*
1672789Sahrens 	 * Apply to self if appropriate.
1673789Sahrens 	 */
16747046Sahrens 	err = func(spa, thisobj, name, arg);
16752199Sahrens 	return (err);
1676789Sahrens }
16775326Sek110237 
16788415SRichard.Morris@Sun.COM /* ARGSUSED */
16798415SRichard.Morris@Sun.COM int
168011209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
16818415SRichard.Morris@Sun.COM {
16828415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
16838415SRichard.Morris@Sun.COM 
16848415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
16858415SRichard.Morris@Sun.COM 		return (0);
16868415SRichard.Morris@Sun.COM 
16878415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
16888415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
168910298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
16908415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
16918415SRichard.Morris@Sun.COM 			zbookmark_t zb;
16928415SRichard.Morris@Sun.COM 
169310922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
169410922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
16958415SRichard.Morris@Sun.COM 
169612296SLin.Ling@Sun.COM 			(void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
16978415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
16988415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
16998415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
17008415SRichard.Morris@Sun.COM 			    &aflags, &zb);
17018415SRichard.Morris@Sun.COM 		}
17028415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
17038415SRichard.Morris@Sun.COM 	}
17048415SRichard.Morris@Sun.COM 
17058415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
17068415SRichard.Morris@Sun.COM 	return (0);
17078415SRichard.Morris@Sun.COM }
17088415SRichard.Morris@Sun.COM 
17095326Sek110237 void
17105326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
17115326Sek110237 {
171210298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
171310298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
17145326Sek110237 }
17155326Sek110237 
17165326Sek110237 void *
17175326Sek110237 dmu_objset_get_user(objset_t *os)
17185326Sek110237 {
171910298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
172010298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
17215326Sek110237 }
1722