xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 13043:8c712bbb18ea)
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>
45*13043STim.Haley@Sun.COM #include <sys/zfs_onexit.h>
46789Sahrens 
4712684STom.Erickson@Sun.COM /*
4812684STom.Erickson@Sun.COM  * Needed to close a window in dnode_move() that allows the objset to be freed
4912684STom.Erickson@Sun.COM  * before it can be safely accessed.
5012684STom.Erickson@Sun.COM  */
5112684STom.Erickson@Sun.COM krwlock_t os_lock;
5212684STom.Erickson@Sun.COM 
5312684STom.Erickson@Sun.COM void
dmu_objset_init(void)5412684STom.Erickson@Sun.COM dmu_objset_init(void)
5512684STom.Erickson@Sun.COM {
5612684STom.Erickson@Sun.COM 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
5712684STom.Erickson@Sun.COM }
5812684STom.Erickson@Sun.COM 
5912684STom.Erickson@Sun.COM void
dmu_objset_fini(void)6012684STom.Erickson@Sun.COM dmu_objset_fini(void)
6112684STom.Erickson@Sun.COM {
6212684STom.Erickson@Sun.COM 	rw_destroy(&os_lock);
6312684STom.Erickson@Sun.COM }
6412684STom.Erickson@Sun.COM 
65789Sahrens spa_t *
dmu_objset_spa(objset_t * os)66789Sahrens dmu_objset_spa(objset_t *os)
67789Sahrens {
6810298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
69789Sahrens }
70789Sahrens 
71789Sahrens zilog_t *
dmu_objset_zil(objset_t * os)72789Sahrens dmu_objset_zil(objset_t *os)
73789Sahrens {
7410298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
75789Sahrens }
76789Sahrens 
77789Sahrens dsl_pool_t *
dmu_objset_pool(objset_t * os)78789Sahrens dmu_objset_pool(objset_t *os)
79789Sahrens {
80789Sahrens 	dsl_dataset_t *ds;
81789Sahrens 
8210298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
83789Sahrens 		return (ds->ds_dir->dd_pool);
84789Sahrens 	else
8510298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
86789Sahrens }
87789Sahrens 
88789Sahrens dsl_dataset_t *
dmu_objset_ds(objset_t * os)89789Sahrens dmu_objset_ds(objset_t *os)
90789Sahrens {
9110298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
92789Sahrens }
93789Sahrens 
94789Sahrens dmu_objset_type_t
dmu_objset_type(objset_t * os)95789Sahrens dmu_objset_type(objset_t *os)
96789Sahrens {
9710298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
98789Sahrens }
99789Sahrens 
100789Sahrens void
dmu_objset_name(objset_t * os,char * buf)101789Sahrens dmu_objset_name(objset_t *os, char *buf)
102789Sahrens {
10310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
104789Sahrens }
105789Sahrens 
106789Sahrens uint64_t
dmu_objset_id(objset_t * os)107789Sahrens dmu_objset_id(objset_t *os)
108789Sahrens {
10910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
110789Sahrens 
111789Sahrens 	return (ds ? ds->ds_object : 0);
112789Sahrens }
113789Sahrens 
11410310SNeil.Perrin@Sun.COM uint64_t
dmu_objset_syncprop(objset_t * os)11512294SMark.Musante@Sun.COM dmu_objset_syncprop(objset_t *os)
11612294SMark.Musante@Sun.COM {
11712294SMark.Musante@Sun.COM 	return (os->os_sync);
11812294SMark.Musante@Sun.COM }
11912294SMark.Musante@Sun.COM 
12012294SMark.Musante@Sun.COM uint64_t
dmu_objset_logbias(objset_t * os)12110310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
12210310SNeil.Perrin@Sun.COM {
12310310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
12410310SNeil.Perrin@Sun.COM }
12510310SNeil.Perrin@Sun.COM 
126789Sahrens static void
checksum_changed_cb(void * arg,uint64_t newval)127789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
128789Sahrens {
12910298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
130789Sahrens 
131789Sahrens 	/*
132789Sahrens 	 * Inheritance should have been done by now.
133789Sahrens 	 */
134789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
135789Sahrens 
13610298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
137789Sahrens }
138789Sahrens 
139789Sahrens static void
compression_changed_cb(void * arg,uint64_t newval)140789Sahrens compression_changed_cb(void *arg, uint64_t newval)
141789Sahrens {
14210298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
143789Sahrens 
144789Sahrens 	/*
145789Sahrens 	 * Inheritance and range checking should have been done by now.
146789Sahrens 	 */
147789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
148789Sahrens 
14910298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
150789Sahrens }
151789Sahrens 
1523835Sahrens static void
copies_changed_cb(void * arg,uint64_t newval)1533835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1543835Sahrens {
15510298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1563835Sahrens 
1573835Sahrens 	/*
1583835Sahrens 	 * Inheritance and range checking should have been done by now.
1593835Sahrens 	 */
1603835Sahrens 	ASSERT(newval > 0);
16110298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1623835Sahrens 
16310298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1643835Sahrens }
1653835Sahrens 
1667237Sek110237 static void
dedup_changed_cb(void * arg,uint64_t newval)16710922SJeff.Bonwick@Sun.COM dedup_changed_cb(void *arg, uint64_t newval)
16810922SJeff.Bonwick@Sun.COM {
16910922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
17010922SJeff.Bonwick@Sun.COM 	spa_t *spa = os->os_spa;
17110922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum;
17210922SJeff.Bonwick@Sun.COM 
17310922SJeff.Bonwick@Sun.COM 	/*
17410922SJeff.Bonwick@Sun.COM 	 * Inheritance should have been done by now.
17510922SJeff.Bonwick@Sun.COM 	 */
17610922SJeff.Bonwick@Sun.COM 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
17710922SJeff.Bonwick@Sun.COM 
17810922SJeff.Bonwick@Sun.COM 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
17910922SJeff.Bonwick@Sun.COM 
18010922SJeff.Bonwick@Sun.COM 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
18110922SJeff.Bonwick@Sun.COM 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
18210922SJeff.Bonwick@Sun.COM }
18310922SJeff.Bonwick@Sun.COM 
18410922SJeff.Bonwick@Sun.COM static void
primary_cache_changed_cb(void * arg,uint64_t newval)1857237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1867237Sek110237 {
18710298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1887237Sek110237 
1897237Sek110237 	/*
1907237Sek110237 	 * Inheritance and range checking should have been done by now.
1917237Sek110237 	 */
1927237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1937237Sek110237 	    newval == ZFS_CACHE_METADATA);
1947237Sek110237 
19510298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1967237Sek110237 }
1977237Sek110237 
1987237Sek110237 static void
secondary_cache_changed_cb(void * arg,uint64_t newval)1997237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
2007237Sek110237 {
20110298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
2027237Sek110237 
2037237Sek110237 	/*
2047237Sek110237 	 * Inheritance and range checking should have been done by now.
2057237Sek110237 	 */
2067237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2077237Sek110237 	    newval == ZFS_CACHE_METADATA);
2087237Sek110237 
20910298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
2107237Sek110237 }
2117237Sek110237 
21210310SNeil.Perrin@Sun.COM static void
sync_changed_cb(void * arg,uint64_t newval)21312294SMark.Musante@Sun.COM sync_changed_cb(void *arg, uint64_t newval)
21412294SMark.Musante@Sun.COM {
21512294SMark.Musante@Sun.COM 	objset_t *os = arg;
21612294SMark.Musante@Sun.COM 
21712294SMark.Musante@Sun.COM 	/*
21812294SMark.Musante@Sun.COM 	 * Inheritance and range checking should have been done by now.
21912294SMark.Musante@Sun.COM 	 */
22012294SMark.Musante@Sun.COM 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22112294SMark.Musante@Sun.COM 	    newval == ZFS_SYNC_DISABLED);
22212294SMark.Musante@Sun.COM 
22312294SMark.Musante@Sun.COM 	os->os_sync = newval;
22412294SMark.Musante@Sun.COM 	if (os->os_zil)
22512294SMark.Musante@Sun.COM 		zil_set_sync(os->os_zil, newval);
22612294SMark.Musante@Sun.COM }
22712294SMark.Musante@Sun.COM 
22812294SMark.Musante@Sun.COM static void
logbias_changed_cb(void * arg,uint64_t newval)22910310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
23010310SNeil.Perrin@Sun.COM {
23110310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
23210310SNeil.Perrin@Sun.COM 
23310310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
23410310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
23510310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
23610310SNeil.Perrin@Sun.COM 	if (os->os_zil)
23710310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
23810310SNeil.Perrin@Sun.COM }
23910310SNeil.Perrin@Sun.COM 
240789Sahrens void
dmu_objset_byteswap(void * buf,size_t size)241789Sahrens dmu_objset_byteswap(void *buf, size_t size)
242789Sahrens {
243789Sahrens 	objset_phys_t *osp = buf;
244789Sahrens 
2459396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
246789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
247789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
248789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
2499396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
2509396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
2519396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
2529396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
2539396SMatthew.Ahrens@Sun.COM 	}
254789Sahrens }
255789Sahrens 
2561544Seschrock int
dmu_objset_open_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,objset_t ** osp)2571544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
25810298SMatthew.Ahrens@Sun.COM     objset_t **osp)
259789Sahrens {
26010298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2617046Sahrens 	int i, err;
262789Sahrens 
2634787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2644787Sahrens 
26510298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
26610298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
26710298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
26810298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
26910298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2702391Smaybee 		uint32_t aflags = ARC_WAIT;
2711544Seschrock 		zbookmark_t zb;
27210922SJeff.Bonwick@Sun.COM 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
27310922SJeff.Bonwick@Sun.COM 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
27410922SJeff.Bonwick@Sun.COM 
27510298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2767237Sek110237 			aflags |= ARC_L2CACHE;
2771544Seschrock 
27810298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2797046Sahrens 		/*
28012296SLin.Ling@Sun.COM 		 * XXX when bprewrite scrub can change the bp,
2817046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2827046Sahrens 		 * could change, and we'll need a lock.
2837046Sahrens 		 */
28412296SLin.Ling@Sun.COM 		err = dsl_read_nolock(NULL, spa, os->os_rootbp,
28510298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2862391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2871544Seschrock 		if (err) {
28810298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2897294Sperrin 			/* convert checksum errors into IO errors */
2907294Sperrin 			if (err == ECKSUM)
2917294Sperrin 				err = EIO;
2921544Seschrock 			return (err);
2931544Seschrock 		}
2949396SMatthew.Ahrens@Sun.COM 
2959396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2969396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
29710298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2989396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
29910298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
3009396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
3019396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
30210298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
30310298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
30410298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
30510298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
30610298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
3079396SMatthew.Ahrens@Sun.COM 		}
3089396SMatthew.Ahrens@Sun.COM 
30910298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
31010298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
311789Sahrens 	} else {
3129396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
3139396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
31410298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
31510298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
31610298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
31710298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
318789Sahrens 	}
319789Sahrens 
320789Sahrens 	/*
321789Sahrens 	 * Note: the changed_cb will be called once before the register
322789Sahrens 	 * func returns, thus changing the checksum/compression from the
3237237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
3247237Sek110237 	 * checksum/compression/copies.
325789Sahrens 	 */
3267237Sek110237 	if (ds) {
3277237Sek110237 		err = dsl_prop_register(ds, "primarycache",
32810298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
3291544Seschrock 		if (err == 0)
3307237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
33110298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
3327237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
3337237Sek110237 			if (err == 0)
3347237Sek110237 				err = dsl_prop_register(ds, "checksum",
33510298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
3367237Sek110237 			if (err == 0)
3377237Sek110237 				err = dsl_prop_register(ds, "compression",
33810298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
3397237Sek110237 			if (err == 0)
3407237Sek110237 				err = dsl_prop_register(ds, "copies",
34110298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
34210310SNeil.Perrin@Sun.COM 			if (err == 0)
34310922SJeff.Bonwick@Sun.COM 				err = dsl_prop_register(ds, "dedup",
34410922SJeff.Bonwick@Sun.COM 				    dedup_changed_cb, os);
34510922SJeff.Bonwick@Sun.COM 			if (err == 0)
34610310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
34710310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
34812294SMark.Musante@Sun.COM 			if (err == 0)
34912294SMark.Musante@Sun.COM 				err = dsl_prop_register(ds, "sync",
35012294SMark.Musante@Sun.COM 				    sync_changed_cb, os);
3517237Sek110237 		}
3521544Seschrock 		if (err) {
35310298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
35410298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
35510298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
3561544Seschrock 			return (err);
3571544Seschrock 		}
3582082Seschrock 	} else if (ds == NULL) {
359789Sahrens 		/* It's the meta-objset. */
36010298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
36110298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
36210298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
36310922SJeff.Bonwick@Sun.COM 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
36410922SJeff.Bonwick@Sun.COM 		os->os_dedup_verify = 0;
36510922SJeff.Bonwick@Sun.COM 		os->os_logbias = 0;
36612294SMark.Musante@Sun.COM 		os->os_sync = 0;
36710298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
36810298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
369789Sahrens 	}
370789Sahrens 
37112827SMatthew.Ahrens@Sun.COM 	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
37212827SMatthew.Ahrens@Sun.COM 		os->os_zil_header = os->os_phys->os_zil_header;
37310298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
374789Sahrens 
375789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
37610298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
377789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
37810298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
379789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
380789Sahrens 	}
38110298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
382789Sahrens 	    offsetof(dnode_t, dn_link));
38310298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
384789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
385789Sahrens 
38610298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
38710298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
38810298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3892856Snd150628 
39012684STom.Erickson@Sun.COM 	DMU_META_DNODE(os) = dnode_special_open(os,
39112684STom.Erickson@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
39212684STom.Erickson@Sun.COM 	    &os->os_meta_dnode);
39310298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
39412684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
39512684STom.Erickson@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
39612684STom.Erickson@Sun.COM 		    &os->os_userused_dnode);
39712684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
39812684STom.Erickson@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
39912684STom.Erickson@Sun.COM 		    &os->os_groupused_dnode);
4009396SMatthew.Ahrens@Sun.COM 	}
401789Sahrens 
4024787Sahrens 	/*
4034787Sahrens 	 * We should be the only thread trying to do this because we
4044787Sahrens 	 * have ds_opening_lock
4054787Sahrens 	 */
4064787Sahrens 	if (ds) {
40710298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
40810298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
40910298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
41010298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
411789Sahrens 	}
412789Sahrens 
41310298SMatthew.Ahrens@Sun.COM 	*osp = os;
4145367Sahrens 	return (0);
4155367Sahrens }
4165367Sahrens 
4175367Sahrens int
dmu_objset_from_ds(dsl_dataset_t * ds,objset_t ** osp)41810298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4195367Sahrens {
42010298SMatthew.Ahrens@Sun.COM 	int err = 0;
42110298SMatthew.Ahrens@Sun.COM 
42210298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
42310298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
42410298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
42510298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
42612970SMark.Maybee@Sun.COM 		    ds, dsl_dataset_get_blkptr(ds), osp);
42710298SMatthew.Ahrens@Sun.COM 	}
42810298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
42910298SMatthew.Ahrens@Sun.COM 	return (err);
43010298SMatthew.Ahrens@Sun.COM }
43110298SMatthew.Ahrens@Sun.COM 
43210298SMatthew.Ahrens@Sun.COM /* called from zpl */
43310298SMatthew.Ahrens@Sun.COM int
dmu_objset_hold(const char * name,void * tag,objset_t ** osp)43410298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
43510298SMatthew.Ahrens@Sun.COM {
43610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
4375367Sahrens 	int err;
4385367Sahrens 
43910298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
4405367Sahrens 	if (err)
44110298SMatthew.Ahrens@Sun.COM 		return (err);
44210298SMatthew.Ahrens@Sun.COM 
44310298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
44410298SMatthew.Ahrens@Sun.COM 	if (err)
44510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
44610298SMatthew.Ahrens@Sun.COM 
4475367Sahrens 	return (err);
4485367Sahrens }
4495367Sahrens 
450789Sahrens /* called from zpl */
451789Sahrens int
dmu_objset_own(const char * name,dmu_objset_type_t type,boolean_t readonly,void * tag,objset_t ** osp)45210298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
45310298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
454789Sahrens {
455789Sahrens 	dsl_dataset_t *ds;
456789Sahrens 	int err;
457789Sahrens 
45810298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
45910298SMatthew.Ahrens@Sun.COM 	if (err)
46010298SMatthew.Ahrens@Sun.COM 		return (err);
4615367Sahrens 
46210298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
463789Sahrens 	if (err) {
46410298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
46510588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
46610298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
46710298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
46810588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
46910588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
47010588SEric.Taylor@Sun.COM 		return (EROFS);
471789Sahrens 	}
4725367Sahrens 	return (err);
473789Sahrens }
474789Sahrens 
475789Sahrens void
dmu_objset_rele(objset_t * os,void * tag)47610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
477789Sahrens {
47810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
47910298SMatthew.Ahrens@Sun.COM }
4806689Smaybee 
48110298SMatthew.Ahrens@Sun.COM void
dmu_objset_disown(objset_t * os,void * tag)48210298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
48310298SMatthew.Ahrens@Sun.COM {
48410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
485789Sahrens }
486789Sahrens 
4871646Sperrin int
dmu_objset_evict_dbufs(objset_t * os)4884944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4891544Seschrock {
4901544Seschrock 	dnode_t *dn;
4911596Sahrens 
49210298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4931596Sahrens 
4941596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
49512684STom.Erickson@Sun.COM 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
49612684STom.Erickson@Sun.COM 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
4971544Seschrock 
4981544Seschrock 	/*
4991596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
5001596Sahrens 	 * because dnode_add_ref() only works if you already have a
5011596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
5021596Sahrens 	 * skip.
5031544Seschrock 	 */
50410298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
5054944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
50610298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
5071596Sahrens 		continue;
5081596Sahrens 
5091596Sahrens 	while (dn) {
5101596Sahrens 		dnode_t *next_dn = dn;
5111596Sahrens 
5121596Sahrens 		do {
51310298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
5144944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
5151596Sahrens 
51610298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
5174944Smaybee 		dnode_evict_dbufs(dn);
5181596Sahrens 		dnode_rele(dn, FTAG);
51910298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
5201596Sahrens 		dn = next_dn;
5211544Seschrock 	}
52212684STom.Erickson@Sun.COM 	dn = list_head(&os->os_dnodes);
52310298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
52412684STom.Erickson@Sun.COM 	return (dn != DMU_META_DNODE(os));
5251544Seschrock }
5261544Seschrock 
5271544Seschrock void
dmu_objset_evict(objset_t * os)52810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
529789Sahrens {
53010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
531789Sahrens 
53210922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
53310922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
534789Sahrens 
5357237Sek110237 	if (ds) {
5367237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
5377237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
53810298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
5397237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
54010298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
5417237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
54210298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
54310922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
54410922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
54510310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
54610310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
54712294SMark.Musante@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "sync",
54812294SMark.Musante@Sun.COM 			    sync_changed_cb, os));
5497237Sek110237 		}
5507237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
55110298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
5527237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
55310298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
554789Sahrens 	}
555789Sahrens 
55611935SMark.Shellenbaum@Sun.COM 	if (os->os_sa)
55711935SMark.Shellenbaum@Sun.COM 		sa_tear_down(os);
55811935SMark.Shellenbaum@Sun.COM 
5591544Seschrock 	/*
5601544Seschrock 	 * We should need only a single pass over the dnode list, since
5611544Seschrock 	 * nothing can be added to the list at this point.
5621544Seschrock 	 */
56310298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5641544Seschrock 
56512684STom.Erickson@Sun.COM 	dnode_special_close(&os->os_meta_dnode);
56612684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os)) {
56712684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_userused_dnode);
56812684STom.Erickson@Sun.COM 		dnode_special_close(&os->os_groupused_dnode);
5699396SMatthew.Ahrens@Sun.COM 	}
57010298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
571789Sahrens 
57210298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
573789Sahrens 
57410298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
57512684STom.Erickson@Sun.COM 
57612684STom.Erickson@Sun.COM 	/*
57712684STom.Erickson@Sun.COM 	 * This is a barrier to prevent the objset from going away in
57812684STom.Erickson@Sun.COM 	 * dnode_move() until we can safely ensure that the objset is still in
57912684STom.Erickson@Sun.COM 	 * use. We consider the objset valid before the barrier and invalid
58012684STom.Erickson@Sun.COM 	 * after the barrier.
58112684STom.Erickson@Sun.COM 	 */
58212684STom.Erickson@Sun.COM 	rw_enter(&os_lock, RW_READER);
58312684STom.Erickson@Sun.COM 	rw_exit(&os_lock);
58412684STom.Erickson@Sun.COM 
58510298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
58610298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
58710298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
58810298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
589789Sahrens }
590789Sahrens 
59110373Schris.kirby@sun.com timestruc_t
dmu_objset_snap_cmtime(objset_t * os)59210373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
59310373Schris.kirby@sun.com {
59410373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
59510373Schris.kirby@sun.com }
59610373Schris.kirby@sun.com 
597789Sahrens /* called from dsl for meta-objset */
59810298SMatthew.Ahrens@Sun.COM objset_t *
dmu_objset_create_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,dmu_objset_type_t type,dmu_tx_t * tx)5993547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
6003547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
601789Sahrens {
60210298SMatthew.Ahrens@Sun.COM 	objset_t *os;
603789Sahrens 	dnode_t *mdn;
604789Sahrens 
605789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
60612970SMark.Maybee@Sun.COM 	if (ds != NULL)
60712970SMark.Maybee@Sun.COM 		VERIFY(0 == dmu_objset_from_ds(ds, &os));
60812970SMark.Maybee@Sun.COM 	else
60912970SMark.Maybee@Sun.COM 		VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os));
61012970SMark.Maybee@Sun.COM 
61112684STom.Erickson@Sun.COM 	mdn = DMU_META_DNODE(os);
612789Sahrens 
613789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
614789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
615789Sahrens 
616789Sahrens 	/*
617789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
618789Sahrens 	 * later, because then we could do it in quescing context while
619789Sahrens 	 * we are also accessing it in open context.
620789Sahrens 	 *
621789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
622789Sahrens 	 * because the MOS is only updated in syncing context.
623789Sahrens 	 * This is most fortunate: the MOS is the only objset that
624789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
625789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
626789Sahrens 	 */
6271544Seschrock 	if (ds != NULL) {
6281544Seschrock 		int levels = 1;
6291544Seschrock 
6301544Seschrock 		/*
6311544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
6321544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
6331544Seschrock 		 */
6341544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
6351544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
6361544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
6371544Seschrock 			levels++;
6381544Seschrock 
639789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
6401544Seschrock 		    mdn->dn_nlevels = levels;
6411544Seschrock 	}
642789Sahrens 
643789Sahrens 	ASSERT(type != DMU_OST_NONE);
644789Sahrens 	ASSERT(type != DMU_OST_ANY);
645789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
64610298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
64710298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
64810298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
64910298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
6509396SMatthew.Ahrens@Sun.COM 	}
651789Sahrens 
652789Sahrens 	dsl_dataset_dirty(ds, tx);
653789Sahrens 
65410298SMatthew.Ahrens@Sun.COM 	return (os);
655789Sahrens }
656789Sahrens 
657789Sahrens struct oscarg {
6584543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
659789Sahrens 	void *userarg;
66010272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
661789Sahrens 	const char *lastname;
662789Sahrens 	dmu_objset_type_t type;
6636492Stimh 	uint64_t flags;
66412296SLin.Ling@Sun.COM 	cred_t *cr;
665789Sahrens };
666789Sahrens 
6674543Smarks /*ARGSUSED*/
668789Sahrens static int
dmu_objset_create_check(void * arg1,void * arg2,dmu_tx_t * tx)6692199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
670789Sahrens {
6712199Sahrens 	dsl_dir_t *dd = arg1;
6722199Sahrens 	struct oscarg *oa = arg2;
6732199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6742199Sahrens 	int err;
6752199Sahrens 	uint64_t ddobj;
6762199Sahrens 
6772199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6782199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6792199Sahrens 	if (err != ENOENT)
6802199Sahrens 		return (err ? err : EEXIST);
6812199Sahrens 
68210272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
68310272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
68410272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6852199Sahrens 			return (EXDEV);
6862199Sahrens 
68710272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
68810272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6892199Sahrens 			return (EINVAL);
6902199Sahrens 	}
6914543Smarks 
6922199Sahrens 	return (0);
6932199Sahrens }
6942199Sahrens 
6952199Sahrens static void
dmu_objset_create_sync(void * arg1,void * arg2,dmu_tx_t * tx)69612296SLin.Ling@Sun.COM dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
6972199Sahrens {
6982199Sahrens 	dsl_dir_t *dd = arg1;
69912970SMark.Maybee@Sun.COM 	spa_t *spa = dd->dd_pool->dp_spa;
7002199Sahrens 	struct oscarg *oa = arg2;
70112970SMark.Maybee@Sun.COM 	uint64_t obj;
702789Sahrens 
703789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
704789Sahrens 
70512970SMark.Maybee@Sun.COM 	obj = dsl_dataset_create_sync(dd, oa->lastname,
70612296SLin.Ling@Sun.COM 	    oa->clone_origin, oa->flags, oa->cr, tx);
707789Sahrens 
70812970SMark.Maybee@Sun.COM 	if (oa->clone_origin == NULL) {
70912970SMark.Maybee@Sun.COM 		dsl_pool_t *dp = dd->dd_pool;
71012970SMark.Maybee@Sun.COM 		dsl_dataset_t *ds;
71112970SMark.Maybee@Sun.COM 		blkptr_t *bp;
71212970SMark.Maybee@Sun.COM 		objset_t *os;
71312827SMatthew.Ahrens@Sun.COM 
71412970SMark.Maybee@Sun.COM 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
71510272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
71610272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
71710272SMatthew.Ahrens@Sun.COM 
71812970SMark.Maybee@Sun.COM 		os = dmu_objset_create_impl(spa, ds, bp, oa->type, tx);
719789Sahrens 
720789Sahrens 		if (oa->userfunc)
72112296SLin.Ling@Sun.COM 			oa->userfunc(os, oa->userarg, oa->cr, tx);
72212970SMark.Maybee@Sun.COM 		dsl_dataset_rele(ds, FTAG);
723789Sahrens 	}
7244543Smarks 
72512970SMark.Maybee@Sun.COM 	spa_history_log_internal(LOG_DS_CREATE, spa, tx, "dataset = %llu", obj);
726789Sahrens }
727789Sahrens 
728789Sahrens int
dmu_objset_create(const char * name,dmu_objset_type_t type,uint64_t flags,void (* func)(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx),void * arg)72910272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
7304543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
731789Sahrens {
7322199Sahrens 	dsl_dir_t *pdd;
733789Sahrens 	const char *tail;
734789Sahrens 	int err = 0;
7352199Sahrens 	struct oscarg oa = { 0 };
736789Sahrens 
7372199Sahrens 	ASSERT(strchr(name, '@') == NULL);
7382199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
7391544Seschrock 	if (err)
7401544Seschrock 		return (err);
741789Sahrens 	if (tail == NULL) {
7422199Sahrens 		dsl_dir_close(pdd, FTAG);
743789Sahrens 		return (EEXIST);
744789Sahrens 	}
745789Sahrens 
7462199Sahrens 	oa.userfunc = func;
7472199Sahrens 	oa.userarg = arg;
7482199Sahrens 	oa.lastname = tail;
7492199Sahrens 	oa.type = type;
7506492Stimh 	oa.flags = flags;
75112296SLin.Ling@Sun.COM 	oa.cr = CRED();
7524543Smarks 
75310272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
75410272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
75510272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
75610272SMatthew.Ahrens@Sun.COM 	return (err);
75710272SMatthew.Ahrens@Sun.COM }
75810272SMatthew.Ahrens@Sun.COM 
75910272SMatthew.Ahrens@Sun.COM int
dmu_objset_clone(const char * name,dsl_dataset_t * clone_origin,uint64_t flags)76010272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
76110272SMatthew.Ahrens@Sun.COM {
76210272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
76310272SMatthew.Ahrens@Sun.COM 	const char *tail;
76410272SMatthew.Ahrens@Sun.COM 	int err = 0;
76510272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
76610272SMatthew.Ahrens@Sun.COM 
76710272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
76810272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
76910272SMatthew.Ahrens@Sun.COM 	if (err)
77010272SMatthew.Ahrens@Sun.COM 		return (err);
77110272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
77210272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
77310272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
774789Sahrens 	}
77510272SMatthew.Ahrens@Sun.COM 
77610272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
77710272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
77810272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
77912296SLin.Ling@Sun.COM 	oa.cr = CRED();
78010272SMatthew.Ahrens@Sun.COM 
7812199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7822199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7832199Sahrens 	dsl_dir_close(pdd, FTAG);
784789Sahrens 	return (err);
785789Sahrens }
786789Sahrens 
787789Sahrens int
dmu_objset_destroy(const char * name,boolean_t defer)78810242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
789789Sahrens {
79010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
791789Sahrens 	int error;
792789Sahrens 
79310298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
794789Sahrens 	if (error == 0) {
79510298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
79610272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
797789Sahrens 	}
798789Sahrens 
7995367Sahrens 	return (error);
800789Sahrens }
801789Sahrens 
8022199Sahrens struct snaparg {
8032199Sahrens 	dsl_sync_task_group_t *dstg;
8042199Sahrens 	char *snapname;
805*13043STim.Haley@Sun.COM 	char *htag;
8062199Sahrens 	char failed[MAXPATHLEN];
80711620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
80812827SMatthew.Ahrens@Sun.COM 	boolean_t needsuspend;
809*13043STim.Haley@Sun.COM 	boolean_t temporary;
8109355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
811*13043STim.Haley@Sun.COM 	struct dsl_ds_holdarg *ha;	/* only needed in the temporary case */
812*13043STim.Haley@Sun.COM 	dsl_dataset_t *newds;
8135367Sahrens };
8145367Sahrens 
8159355SMatthew.Ahrens@Sun.COM static int
snapshot_check(void * arg1,void * arg2,dmu_tx_t * tx)8169355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
8179355SMatthew.Ahrens@Sun.COM {
8189355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
8199355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
820*13043STim.Haley@Sun.COM 	int error;
8219355SMatthew.Ahrens@Sun.COM 
8229355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
8239355SMatthew.Ahrens@Sun.COM 
824*13043STim.Haley@Sun.COM 	error = dsl_dataset_snapshot_check(os->os_dsl_dataset,
825*13043STim.Haley@Sun.COM 	    sn->snapname, tx);
826*13043STim.Haley@Sun.COM 	if (error)
827*13043STim.Haley@Sun.COM 		return (error);
828*13043STim.Haley@Sun.COM 
829*13043STim.Haley@Sun.COM 	if (sn->temporary) {
830*13043STim.Haley@Sun.COM 		/*
831*13043STim.Haley@Sun.COM 		 * Ideally we would just call
832*13043STim.Haley@Sun.COM 		 * dsl_dataset_user_hold_check() and
833*13043STim.Haley@Sun.COM 		 * dsl_dataset_destroy_check() here.  However the
834*13043STim.Haley@Sun.COM 		 * dataset we want to hold and destroy is the snapshot
835*13043STim.Haley@Sun.COM 		 * that we just confirmed we can create, but it won't
836*13043STim.Haley@Sun.COM 		 * exist until after these checks are run.  Do any
837*13043STim.Haley@Sun.COM 		 * checks we can here and if more checks are added to
838*13043STim.Haley@Sun.COM 		 * those routines in the future, similar checks may be
839*13043STim.Haley@Sun.COM 		 * necessary here.
840*13043STim.Haley@Sun.COM 		 */
841*13043STim.Haley@Sun.COM 		if (spa_version(os->os_spa) < SPA_VERSION_USERREFS)
842*13043STim.Haley@Sun.COM 			return (ENOTSUP);
843*13043STim.Haley@Sun.COM 		/*
844*13043STim.Haley@Sun.COM 		 * Not checking number of tags because the tag will be
845*13043STim.Haley@Sun.COM 		 * unique, as it will be the only tag.
846*13043STim.Haley@Sun.COM 		 */
847*13043STim.Haley@Sun.COM 		if (strlen(sn->htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
848*13043STim.Haley@Sun.COM 			return (E2BIG);
849*13043STim.Haley@Sun.COM 
850*13043STim.Haley@Sun.COM 		sn->ha = kmem_alloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
851*13043STim.Haley@Sun.COM 		sn->ha->temphold = B_TRUE;
852*13043STim.Haley@Sun.COM 		sn->ha->htag = sn->htag;
853*13043STim.Haley@Sun.COM 	}
854*13043STim.Haley@Sun.COM 	return (error);
8559355SMatthew.Ahrens@Sun.COM }
8569355SMatthew.Ahrens@Sun.COM 
8579355SMatthew.Ahrens@Sun.COM static void
snapshot_sync(void * arg1,void * arg2,dmu_tx_t * tx)85812296SLin.Ling@Sun.COM snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
8599355SMatthew.Ahrens@Sun.COM {
8609355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
86110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
8629355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
8639355SMatthew.Ahrens@Sun.COM 
86412296SLin.Ling@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, tx);
8659355SMatthew.Ahrens@Sun.COM 
86611022STom.Erickson@Sun.COM 	if (sn->props) {
86711022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
86811022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
86911022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
87012296SLin.Ling@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, tx);
87111022STom.Erickson@Sun.COM 	}
872*13043STim.Haley@Sun.COM 
873*13043STim.Haley@Sun.COM 	if (sn->temporary) {
874*13043STim.Haley@Sun.COM 		struct dsl_ds_destroyarg da;
875*13043STim.Haley@Sun.COM 
876*13043STim.Haley@Sun.COM 		dsl_dataset_user_hold_sync(ds->ds_prev, sn->ha, tx);
877*13043STim.Haley@Sun.COM 		kmem_free(sn->ha, sizeof (struct dsl_ds_holdarg));
878*13043STim.Haley@Sun.COM 		sn->ha = NULL;
879*13043STim.Haley@Sun.COM 		sn->newds = ds->ds_prev;
880*13043STim.Haley@Sun.COM 
881*13043STim.Haley@Sun.COM 		da.ds = ds->ds_prev;
882*13043STim.Haley@Sun.COM 		da.defer = B_TRUE;
883*13043STim.Haley@Sun.COM 		dsl_dataset_destroy_sync(&da, FTAG, tx);
884*13043STim.Haley@Sun.COM 	}
8859355SMatthew.Ahrens@Sun.COM }
8862199Sahrens 
8872199Sahrens static int
dmu_objset_snapshot_one(const char * name,void * arg)88811209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
8892199Sahrens {
8902199Sahrens 	struct snaparg *sn = arg;
8912199Sahrens 	objset_t *os;
8922199Sahrens 	int err;
89311620SMatthew.Ahrens@Sun.COM 	char *cp;
89411620SMatthew.Ahrens@Sun.COM 
89511620SMatthew.Ahrens@Sun.COM 	/*
89611620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
89711620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
89811620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
89911620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
90011620SMatthew.Ahrens@Sun.COM 	 */
90111620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
90211620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
90311620SMatthew.Ahrens@Sun.COM 		return (0);
9042199Sahrens 
9052199Sahrens 	(void) strcpy(sn->failed, name);
9062199Sahrens 
9074543Smarks 	/*
90811620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
90911620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
91011620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
9114543Smarks 	 */
91211620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
9134543Smarks 		return (err);
9144543Smarks 
91510298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
9162199Sahrens 	if (err != 0)
9172199Sahrens 		return (err);
9182199Sahrens 
91911620SMatthew.Ahrens@Sun.COM 	/*
92011620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
92111620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
92211620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
92311620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
92411620SMatthew.Ahrens@Sun.COM 	 */
92510298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
92610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
92711620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
9283637Srm160521 	}
9293637Srm160521 
93012827SMatthew.Ahrens@Sun.COM 	if (sn->needsuspend) {
93112827SMatthew.Ahrens@Sun.COM 		err = zil_suspend(dmu_objset_zil(os));
93212827SMatthew.Ahrens@Sun.COM 		if (err) {
93312827SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, sn);
93412827SMatthew.Ahrens@Sun.COM 			return (err);
93512827SMatthew.Ahrens@Sun.COM 		}
9362199Sahrens 	}
93712827SMatthew.Ahrens@Sun.COM 	dsl_sync_task_create(sn->dstg, snapshot_check, snapshot_sync,
93812827SMatthew.Ahrens@Sun.COM 	    os, sn, 3);
9393637Srm160521 
94012827SMatthew.Ahrens@Sun.COM 	return (0);
9412199Sahrens }
9422199Sahrens 
9432199Sahrens int
dmu_objset_snapshot(char * fsname,char * snapname,char * tag,nvlist_t * props,boolean_t recursive,boolean_t temporary,int cleanup_fd)944*13043STim.Haley@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname, char *tag,
945*13043STim.Haley@Sun.COM     nvlist_t *props, boolean_t recursive, boolean_t temporary, int cleanup_fd)
9462199Sahrens {
9472199Sahrens 	dsl_sync_task_t *dst;
9489355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
9492199Sahrens 	spa_t *spa;
950*13043STim.Haley@Sun.COM 	minor_t minor;
9512199Sahrens 	int err;
9522199Sahrens 
9532199Sahrens 	(void) strcpy(sn.failed, fsname);
9542199Sahrens 
9554603Sahrens 	err = spa_open(fsname, &spa, FTAG);
9562199Sahrens 	if (err)
9572199Sahrens 		return (err);
9582199Sahrens 
959*13043STim.Haley@Sun.COM 	if (temporary) {
960*13043STim.Haley@Sun.COM 		if (cleanup_fd < 0) {
961*13043STim.Haley@Sun.COM 			spa_close(spa, FTAG);
962*13043STim.Haley@Sun.COM 			return (EINVAL);
963*13043STim.Haley@Sun.COM 		}
964*13043STim.Haley@Sun.COM 		if ((err = zfs_onexit_fd_hold(cleanup_fd, &minor)) != 0) {
965*13043STim.Haley@Sun.COM 			spa_close(spa, FTAG);
966*13043STim.Haley@Sun.COM 			return (err);
967*13043STim.Haley@Sun.COM 		}
968*13043STim.Haley@Sun.COM 	}
969*13043STim.Haley@Sun.COM 
9702199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9712199Sahrens 	sn.snapname = snapname;
972*13043STim.Haley@Sun.COM 	sn.htag = tag;
9739355SMatthew.Ahrens@Sun.COM 	sn.props = props;
97411620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
97512827SMatthew.Ahrens@Sun.COM 	sn.needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
976*13043STim.Haley@Sun.COM 	sn.temporary = temporary;
977*13043STim.Haley@Sun.COM 	sn.ha = NULL;
978*13043STim.Haley@Sun.COM 	sn.newds = NULL;
9792199Sahrens 
9802417Sahrens 	if (recursive) {
9812417Sahrens 		err = dmu_objset_find(fsname,
9822417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
9832417Sahrens 	} else {
9842199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
9852417Sahrens 	}
9862199Sahrens 
9879355SMatthew.Ahrens@Sun.COM 	if (err == 0)
9889355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
9892199Sahrens 
9902199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
9912199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
9929355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
99310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
994*13043STim.Haley@Sun.COM 		if (dst->dst_err) {
9955367Sahrens 			dsl_dataset_name(ds, sn.failed);
996*13043STim.Haley@Sun.COM 		} else if (temporary) {
997*13043STim.Haley@Sun.COM 			dsl_register_onexit_hold_cleanup(sn.newds, tag, minor);
998*13043STim.Haley@Sun.COM 		}
99912827SMatthew.Ahrens@Sun.COM 		if (sn.needsuspend)
100012827SMatthew.Ahrens@Sun.COM 			zil_resume(dmu_objset_zil(os));
100110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
10022199Sahrens 	}
10035367Sahrens 
10042199Sahrens 	if (err)
10052199Sahrens 		(void) strcpy(fsname, sn.failed);
1006*13043STim.Haley@Sun.COM 	if (temporary)
1007*13043STim.Haley@Sun.COM 		zfs_onexit_fd_rele(cleanup_fd);
10082199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
10092199Sahrens 	spa_close(spa, FTAG);
10102199Sahrens 	return (err);
10112199Sahrens }
10122199Sahrens 
1013789Sahrens static void
dmu_objset_sync_dnodes(list_t * list,list_t * newlist,dmu_tx_t * tx)10149396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1015789Sahrens {
10163547Smaybee 	dnode_t *dn;
1017789Sahrens 
10183547Smaybee 	while (dn = list_head(list)) {
10193547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
10203547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
10213547Smaybee 		/*
10229396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
10239396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
10243547Smaybee 		 */
10253547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
10263547Smaybee 		ASSERT(dn->dn_zio);
1027789Sahrens 
10283547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
10293547Smaybee 		list_remove(list, dn);
10309396SMatthew.Ahrens@Sun.COM 
10319396SMatthew.Ahrens@Sun.COM 		if (newlist) {
10329396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
10339396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
10349396SMatthew.Ahrens@Sun.COM 		}
10359396SMatthew.Ahrens@Sun.COM 
10363547Smaybee 		dnode_sync(dn, tx);
10373547Smaybee 	}
10383547Smaybee }
10392981Sahrens 
10403547Smaybee /* ARGSUSED */
10413547Smaybee static void
dmu_objset_write_ready(zio_t * zio,arc_buf_t * abuf,void * arg)104210922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
10433547Smaybee {
10447754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
104510298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
10463547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
10472981Sahrens 
10487754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
10497754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
10507754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
10515329Sgw25295 
10523547Smaybee 	/*
10539396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
10549396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
10559396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
10569396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
10573547Smaybee 	 */
10589396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
10597754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10603547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
106110922SJeff.Bonwick@Sun.COM }
106210922SJeff.Bonwick@Sun.COM 
106310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
106410922SJeff.Bonwick@Sun.COM static void
dmu_objset_write_done(zio_t * zio,arc_buf_t * abuf,void * arg)106510922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
106610922SJeff.Bonwick@Sun.COM {
106710922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
106810922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
106910922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
10705329Sgw25295 
10717754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
107210922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
10737754SJeff.Bonwick@Sun.COM 	} else {
107410922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
107510922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
107610922SJeff.Bonwick@Sun.COM 
107710922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
107810922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
10795329Sgw25295 	}
1080789Sahrens }
1081789Sahrens 
1082789Sahrens /* called from dsl */
1083789Sahrens void
dmu_objset_sync(objset_t * os,zio_t * pio,dmu_tx_t * tx)108410298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1085789Sahrens {
1086789Sahrens 	int txgoff;
10871544Seschrock 	zbookmark_t zb;
108810922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
10893547Smaybee 	zio_t *zio;
10903547Smaybee 	list_t *list;
10919396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
10923547Smaybee 	dbuf_dirty_record_t *dr;
10933547Smaybee 
10943547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1095789Sahrens 
1096789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1097789Sahrens 	/* XXX the write_done callback should really give us the tx... */
1098789Sahrens 	os->os_synctx = tx;
1099789Sahrens 
11003882Sahrens 	if (os->os_dsl_dataset == NULL) {
11013882Sahrens 		/*
11023882Sahrens 		 * This is the MOS.  If we have upgraded,
11033882Sahrens 		 * spa_max_replication() could change, so reset
11043882Sahrens 		 * os_copies here.
11053882Sahrens 		 */
11063882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
11073882Sahrens 	}
11083882Sahrens 
11093547Smaybee 	/*
11103547Smaybee 	 * Create the root block IO
11113547Smaybee 	 */
111210922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
111310922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
111410922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
111512296SLin.Ling@Sun.COM 	VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
111612296SLin.Ling@Sun.COM 	    os->os_rootbp, os->os_spa, &zb));
111710922SJeff.Bonwick@Sun.COM 
111810922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
111910922SJeff.Bonwick@Sun.COM 
112010922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
112110922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
112210922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
11237754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
11243547Smaybee 
11253547Smaybee 	/*
11269396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
11273547Smaybee 	 */
112812684STom.Erickson@Sun.COM 	DMU_META_DNODE(os)->dn_zio = zio;
112912684STom.Erickson@Sun.COM 	dnode_sync(DMU_META_DNODE(os), tx);
1130789Sahrens 
11319396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
11329396SMatthew.Ahrens@Sun.COM 
113312684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os) &&
113412684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
113512684STom.Erickson@Sun.COM 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
113612684STom.Erickson@Sun.COM 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
113712684STom.Erickson@Sun.COM 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
113812684STom.Erickson@Sun.COM 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
11399396SMatthew.Ahrens@Sun.COM 	}
11409396SMatthew.Ahrens@Sun.COM 
1141789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1142789Sahrens 
11439396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
11449396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
11459396SMatthew.Ahrens@Sun.COM 		/*
11469396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
11479396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
11489396SMatthew.Ahrens@Sun.COM 		 */
11499396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
11509396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
11519396SMatthew.Ahrens@Sun.COM 	}
11529396SMatthew.Ahrens@Sun.COM 
11539396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
11549396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1155789Sahrens 
115612684STom.Erickson@Sun.COM 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
11573547Smaybee 	while (dr = list_head(list)) {
11583547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
11593547Smaybee 		list_remove(list, dr);
11603547Smaybee 		if (dr->dr_zio)
11613547Smaybee 			zio_nowait(dr->dr_zio);
11623547Smaybee 	}
1163789Sahrens 	/*
1164789Sahrens 	 * Free intent log blocks up to this tx.
1165789Sahrens 	 */
1166789Sahrens 	zil_sync(os->os_zil, tx);
11677046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
11683547Smaybee 	zio_nowait(zio);
1169789Sahrens }
1170789Sahrens 
117110922SJeff.Bonwick@Sun.COM boolean_t
dmu_objset_is_dirty(objset_t * os,uint64_t txg)117210922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
117310922SJeff.Bonwick@Sun.COM {
117410922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
117510922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
117610922SJeff.Bonwick@Sun.COM }
117710922SJeff.Bonwick@Sun.COM 
117812722SSanjeev.Bagewadi@Sun.COM boolean_t
dmu_objset_is_dirty_anywhere(objset_t * os)117912722SSanjeev.Bagewadi@Sun.COM dmu_objset_is_dirty_anywhere(objset_t *os)
118012722SSanjeev.Bagewadi@Sun.COM {
118112722SSanjeev.Bagewadi@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
118212722SSanjeev.Bagewadi@Sun.COM 		if (dmu_objset_is_dirty(os, t))
118312722SSanjeev.Bagewadi@Sun.COM 			return (B_TRUE);
118412722SSanjeev.Bagewadi@Sun.COM 	return (B_FALSE);
118512722SSanjeev.Bagewadi@Sun.COM }
118612722SSanjeev.Bagewadi@Sun.COM 
118712684STom.Erickson@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
11889396SMatthew.Ahrens@Sun.COM 
11899396SMatthew.Ahrens@Sun.COM void
dmu_objset_register_type(dmu_objset_type_t ost,objset_used_cb_t * cb)11909396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
11919396SMatthew.Ahrens@Sun.COM {
11929396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
11939396SMatthew.Ahrens@Sun.COM }
11949396SMatthew.Ahrens@Sun.COM 
11959396SMatthew.Ahrens@Sun.COM boolean_t
dmu_objset_userused_enabled(objset_t * os)119610298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
11979396SMatthew.Ahrens@Sun.COM {
11989396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
119912684STom.Erickson@Sun.COM 	    used_cbs[os->os_phys->os_type] != NULL &&
120012684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os) != NULL);
12019396SMatthew.Ahrens@Sun.COM }
12029396SMatthew.Ahrens@Sun.COM 
120310407SMatthew.Ahrens@Sun.COM static void
do_userquota_update(objset_t * os,uint64_t used,uint64_t flags,uint64_t user,uint64_t group,boolean_t subtract,dmu_tx_t * tx)120411935SMark.Shellenbaum@Sun.COM do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
120511935SMark.Shellenbaum@Sun.COM     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
120610407SMatthew.Ahrens@Sun.COM {
120711935SMark.Shellenbaum@Sun.COM 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
120811935SMark.Shellenbaum@Sun.COM 		int64_t delta = DNODE_SIZE + used;
120910407SMatthew.Ahrens@Sun.COM 		if (subtract)
121010407SMatthew.Ahrens@Sun.COM 			delta = -delta;
121110801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
121210407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
121310801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
121410407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
121510407SMatthew.Ahrens@Sun.COM 	}
121610407SMatthew.Ahrens@Sun.COM }
121710407SMatthew.Ahrens@Sun.COM 
12189396SMatthew.Ahrens@Sun.COM void
dmu_objset_do_userquota_updates(objset_t * os,dmu_tx_t * tx)121911935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
12209396SMatthew.Ahrens@Sun.COM {
12219396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
12229396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
12239396SMatthew.Ahrens@Sun.COM 
12249396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
12259396SMatthew.Ahrens@Sun.COM 
12269396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
122712493SMark.Shellenbaum@Oracle.COM 		int flags;
12289396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
12299396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
12309396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
12319396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
12329396SMatthew.Ahrens@Sun.COM 
12339396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
123412684STom.Erickson@Sun.COM 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
123510298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
12369396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
12379396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
123810298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
12399396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
12409396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
12419396SMatthew.Ahrens@Sun.COM 		}
12429396SMatthew.Ahrens@Sun.COM 
12439396SMatthew.Ahrens@Sun.COM 		/*
124410407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
124511935SMark.Shellenbaum@Sun.COM 		 * net delta is zero.  Otherwise
124610407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
124710407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
124810407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
12499396SMatthew.Ahrens@Sun.COM 		 */
12509396SMatthew.Ahrens@Sun.COM 
125112493SMark.Shellenbaum@Oracle.COM 		flags = dn->dn_id_flags;
125212493SMark.Shellenbaum@Oracle.COM 		ASSERT(flags);
125312493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_OLD_EXIST)  {
125411935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
125511935SMark.Shellenbaum@Sun.COM 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
125611935SMark.Shellenbaum@Sun.COM 		}
125712493SMark.Shellenbaum@Oracle.COM 		if (flags & DN_ID_NEW_EXIST) {
125811935SMark.Shellenbaum@Sun.COM 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
125911935SMark.Shellenbaum@Sun.COM 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
126011935SMark.Shellenbaum@Sun.COM 			    dn->dn_newgid, B_FALSE, tx);
126111935SMark.Shellenbaum@Sun.COM 		}
126211935SMark.Shellenbaum@Sun.COM 
126312493SMark.Shellenbaum@Oracle.COM 		mutex_enter(&dn->dn_mtx);
126411935SMark.Shellenbaum@Sun.COM 		dn->dn_oldused = 0;
126511935SMark.Shellenbaum@Sun.COM 		dn->dn_oldflags = 0;
126611935SMark.Shellenbaum@Sun.COM 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
126711935SMark.Shellenbaum@Sun.COM 			dn->dn_olduid = dn->dn_newuid;
126811935SMark.Shellenbaum@Sun.COM 			dn->dn_oldgid = dn->dn_newgid;
126911935SMark.Shellenbaum@Sun.COM 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
127011935SMark.Shellenbaum@Sun.COM 			if (dn->dn_bonuslen == 0)
127111935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
127211935SMark.Shellenbaum@Sun.COM 			else
127311935SMark.Shellenbaum@Sun.COM 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
127411935SMark.Shellenbaum@Sun.COM 		}
127512432SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
12769396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
12779396SMatthew.Ahrens@Sun.COM 
12789396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
12799396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
12809396SMatthew.Ahrens@Sun.COM 	}
12819396SMatthew.Ahrens@Sun.COM }
12829396SMatthew.Ahrens@Sun.COM 
128312178SMark.Shellenbaum@Sun.COM /*
128412178SMark.Shellenbaum@Sun.COM  * Returns a pointer to data to find uid/gid from
128512178SMark.Shellenbaum@Sun.COM  *
128612178SMark.Shellenbaum@Sun.COM  * If a dirty record for transaction group that is syncing can't
128712178SMark.Shellenbaum@Sun.COM  * be found then NULL is returned.  In the NULL case it is assumed
128812178SMark.Shellenbaum@Sun.COM  * the uid/gid aren't changing.
128912178SMark.Shellenbaum@Sun.COM  */
129012178SMark.Shellenbaum@Sun.COM static void *
dmu_objset_userquota_find_data(dmu_buf_impl_t * db,dmu_tx_t * tx)129112178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
129212178SMark.Shellenbaum@Sun.COM {
129312178SMark.Shellenbaum@Sun.COM 	dbuf_dirty_record_t *dr, **drp;
129412178SMark.Shellenbaum@Sun.COM 	void *data;
129512178SMark.Shellenbaum@Sun.COM 
129612178SMark.Shellenbaum@Sun.COM 	if (db->db_dirtycnt == 0)
129712178SMark.Shellenbaum@Sun.COM 		return (db->db.db_data);  /* Nothing is changing */
129812178SMark.Shellenbaum@Sun.COM 
129912178SMark.Shellenbaum@Sun.COM 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
130012178SMark.Shellenbaum@Sun.COM 		if (dr->dr_txg == tx->tx_txg)
130112178SMark.Shellenbaum@Sun.COM 			break;
130212178SMark.Shellenbaum@Sun.COM 
130312684STom.Erickson@Sun.COM 	if (dr == NULL) {
130412178SMark.Shellenbaum@Sun.COM 		data = NULL;
130512684STom.Erickson@Sun.COM 	} else {
130612684STom.Erickson@Sun.COM 		dnode_t *dn;
130712684STom.Erickson@Sun.COM 
130812684STom.Erickson@Sun.COM 		DB_DNODE_ENTER(dr->dr_dbuf);
130912684STom.Erickson@Sun.COM 		dn = DB_DNODE(dr->dr_dbuf);
131012684STom.Erickson@Sun.COM 
131112684STom.Erickson@Sun.COM 		if (dn->dn_bonuslen == 0 &&
131212684STom.Erickson@Sun.COM 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
131312684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data->b_data;
131412684STom.Erickson@Sun.COM 		else
131512684STom.Erickson@Sun.COM 			data = dr->dt.dl.dr_data;
131612684STom.Erickson@Sun.COM 
131712684STom.Erickson@Sun.COM 		DB_DNODE_EXIT(dr->dr_dbuf);
131812684STom.Erickson@Sun.COM 	}
131912684STom.Erickson@Sun.COM 
132012178SMark.Shellenbaum@Sun.COM 	return (data);
132112178SMark.Shellenbaum@Sun.COM }
132212178SMark.Shellenbaum@Sun.COM 
132311935SMark.Shellenbaum@Sun.COM void
dmu_objset_userquota_get_ids(dnode_t * dn,boolean_t before,dmu_tx_t * tx)132412178SMark.Shellenbaum@Sun.COM dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
132511935SMark.Shellenbaum@Sun.COM {
132611935SMark.Shellenbaum@Sun.COM 	objset_t *os = dn->dn_objset;
132711935SMark.Shellenbaum@Sun.COM 	void *data = NULL;
132812178SMark.Shellenbaum@Sun.COM 	dmu_buf_impl_t *db = NULL;
132911935SMark.Shellenbaum@Sun.COM 	uint64_t *user, *group;
133011935SMark.Shellenbaum@Sun.COM 	int flags = dn->dn_id_flags;
133111935SMark.Shellenbaum@Sun.COM 	int error;
133212178SMark.Shellenbaum@Sun.COM 	boolean_t have_spill = B_FALSE;
133311935SMark.Shellenbaum@Sun.COM 
133411935SMark.Shellenbaum@Sun.COM 	if (!dmu_objset_userused_enabled(dn->dn_objset))
133511935SMark.Shellenbaum@Sun.COM 		return;
133611935SMark.Shellenbaum@Sun.COM 
133711935SMark.Shellenbaum@Sun.COM 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
133811935SMark.Shellenbaum@Sun.COM 	    DN_ID_CHKED_SPILL)))
133911935SMark.Shellenbaum@Sun.COM 		return;
134011935SMark.Shellenbaum@Sun.COM 
134111935SMark.Shellenbaum@Sun.COM 	if (before && dn->dn_bonuslen != 0)
134211935SMark.Shellenbaum@Sun.COM 		data = DN_BONUS(dn->dn_phys);
134312178SMark.Shellenbaum@Sun.COM 	else if (!before && dn->dn_bonuslen != 0) {
134412178SMark.Shellenbaum@Sun.COM 		if (dn->dn_bonus) {
134512178SMark.Shellenbaum@Sun.COM 			db = dn->dn_bonus;
134612178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
134712178SMark.Shellenbaum@Sun.COM 			data = dmu_objset_userquota_find_data(db, tx);
134812178SMark.Shellenbaum@Sun.COM 		} else {
134912178SMark.Shellenbaum@Sun.COM 			data = DN_BONUS(dn->dn_phys);
135012178SMark.Shellenbaum@Sun.COM 		}
135112178SMark.Shellenbaum@Sun.COM 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
135211935SMark.Shellenbaum@Sun.COM 			int rf = 0;
135311935SMark.Shellenbaum@Sun.COM 
135411935SMark.Shellenbaum@Sun.COM 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
135511935SMark.Shellenbaum@Sun.COM 				rf |= DB_RF_HAVESTRUCT;
135612493SMark.Shellenbaum@Oracle.COM 			error = dmu_spill_hold_by_dnode(dn,
135712493SMark.Shellenbaum@Oracle.COM 			    rf | DB_RF_MUST_SUCCEED,
135812178SMark.Shellenbaum@Sun.COM 			    FTAG, (dmu_buf_t **)&db);
135911935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
136012178SMark.Shellenbaum@Sun.COM 			mutex_enter(&db->db_mtx);
136112178SMark.Shellenbaum@Sun.COM 			data = (before) ? db->db.db_data :
136212178SMark.Shellenbaum@Sun.COM 			    dmu_objset_userquota_find_data(db, tx);
136312178SMark.Shellenbaum@Sun.COM 			have_spill = B_TRUE;
136411935SMark.Shellenbaum@Sun.COM 	} else {
136511935SMark.Shellenbaum@Sun.COM 		mutex_enter(&dn->dn_mtx);
136611935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
136711935SMark.Shellenbaum@Sun.COM 		mutex_exit(&dn->dn_mtx);
136811935SMark.Shellenbaum@Sun.COM 		return;
136911935SMark.Shellenbaum@Sun.COM 	}
137011935SMark.Shellenbaum@Sun.COM 
137111935SMark.Shellenbaum@Sun.COM 	if (before) {
137212178SMark.Shellenbaum@Sun.COM 		ASSERT(data);
137311935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_olduid;
137411935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_oldgid;
137512178SMark.Shellenbaum@Sun.COM 	} else if (data) {
137611935SMark.Shellenbaum@Sun.COM 		user = &dn->dn_newuid;
137711935SMark.Shellenbaum@Sun.COM 		group = &dn->dn_newgid;
137811935SMark.Shellenbaum@Sun.COM 	}
137911935SMark.Shellenbaum@Sun.COM 
138012178SMark.Shellenbaum@Sun.COM 	/*
138112178SMark.Shellenbaum@Sun.COM 	 * Must always call the callback in case the object
138212178SMark.Shellenbaum@Sun.COM 	 * type has changed and that type isn't an object type to track
138312178SMark.Shellenbaum@Sun.COM 	 */
138411935SMark.Shellenbaum@Sun.COM 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
138511935SMark.Shellenbaum@Sun.COM 	    user, group);
138611935SMark.Shellenbaum@Sun.COM 
138712178SMark.Shellenbaum@Sun.COM 	/*
138812178SMark.Shellenbaum@Sun.COM 	 * Preserve existing uid/gid when the callback can't determine
138912178SMark.Shellenbaum@Sun.COM 	 * what the new uid/gid are and the callback returned EEXIST.
139012178SMark.Shellenbaum@Sun.COM 	 * The EEXIST error tells us to just use the existing uid/gid.
139112178SMark.Shellenbaum@Sun.COM 	 * If we don't know what the old values are then just assign
139212178SMark.Shellenbaum@Sun.COM 	 * them to 0, since that is a new file  being created.
139312178SMark.Shellenbaum@Sun.COM 	 */
139412178SMark.Shellenbaum@Sun.COM 	if (!before && data == NULL && error == EEXIST) {
139512178SMark.Shellenbaum@Sun.COM 		if (flags & DN_ID_OLD_EXIST) {
139612178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = dn->dn_olduid;
139712178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = dn->dn_oldgid;
139812178SMark.Shellenbaum@Sun.COM 		} else {
139912178SMark.Shellenbaum@Sun.COM 			dn->dn_newuid = 0;
140012178SMark.Shellenbaum@Sun.COM 			dn->dn_newgid = 0;
140112178SMark.Shellenbaum@Sun.COM 		}
140212178SMark.Shellenbaum@Sun.COM 		error = 0;
140312178SMark.Shellenbaum@Sun.COM 	}
140412178SMark.Shellenbaum@Sun.COM 
140512178SMark.Shellenbaum@Sun.COM 	if (db)
140612178SMark.Shellenbaum@Sun.COM 		mutex_exit(&db->db_mtx);
140712178SMark.Shellenbaum@Sun.COM 
140811935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
140911935SMark.Shellenbaum@Sun.COM 	if (error == 0 && before)
141011935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
141111935SMark.Shellenbaum@Sun.COM 	if (error == 0 && !before)
141211935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
141311935SMark.Shellenbaum@Sun.COM 
141412178SMark.Shellenbaum@Sun.COM 	if (have_spill) {
141511935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
141611935SMark.Shellenbaum@Sun.COM 	} else {
141711935SMark.Shellenbaum@Sun.COM 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
141811935SMark.Shellenbaum@Sun.COM 	}
141911935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
142012178SMark.Shellenbaum@Sun.COM 	if (have_spill)
142112178SMark.Shellenbaum@Sun.COM 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
142211935SMark.Shellenbaum@Sun.COM }
142311935SMark.Shellenbaum@Sun.COM 
14249396SMatthew.Ahrens@Sun.COM boolean_t
dmu_objset_userspace_present(objset_t * os)14259396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
14269396SMatthew.Ahrens@Sun.COM {
142710298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
14289396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
14299396SMatthew.Ahrens@Sun.COM }
14309396SMatthew.Ahrens@Sun.COM 
14319396SMatthew.Ahrens@Sun.COM int
dmu_objset_userspace_upgrade(objset_t * os)14329396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
14339396SMatthew.Ahrens@Sun.COM {
14349396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
14359396SMatthew.Ahrens@Sun.COM 	int err = 0;
14369396SMatthew.Ahrens@Sun.COM 
14379396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
14389396SMatthew.Ahrens@Sun.COM 		return (0);
143910298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
14409396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
14419396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
14429396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
14439396SMatthew.Ahrens@Sun.COM 
14449396SMatthew.Ahrens@Sun.COM 	/*
14459396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
14469396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
14479396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
14489396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
14499396SMatthew.Ahrens@Sun.COM 	 * independently.
14509396SMatthew.Ahrens@Sun.COM 	 */
14519396SMatthew.Ahrens@Sun.COM 
14529396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
14539951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
14549396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
14559396SMatthew.Ahrens@Sun.COM 		int objerr;
14569396SMatthew.Ahrens@Sun.COM 
14579396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
14589396SMatthew.Ahrens@Sun.COM 			return (EINTR);
14599396SMatthew.Ahrens@Sun.COM 
14609396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14619396SMatthew.Ahrens@Sun.COM 		if (objerr)
14629396SMatthew.Ahrens@Sun.COM 			continue;
14639951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
14649396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
14659396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14669396SMatthew.Ahrens@Sun.COM 		if (objerr) {
14679396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
14689396SMatthew.Ahrens@Sun.COM 			continue;
14699396SMatthew.Ahrens@Sun.COM 		}
14709396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
14719396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
14729396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
14739396SMatthew.Ahrens@Sun.COM 	}
14749396SMatthew.Ahrens@Sun.COM 
147510298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
14769396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
14779396SMatthew.Ahrens@Sun.COM 	return (0);
14789396SMatthew.Ahrens@Sun.COM }
14799396SMatthew.Ahrens@Sun.COM 
1480789Sahrens void
dmu_objset_space(objset_t * os,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)14812885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
14822885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
14832885Sahrens {
148410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
14852885Sahrens 	    usedobjsp, availobjsp);
14862885Sahrens }
14872885Sahrens 
14882885Sahrens uint64_t
dmu_objset_fsid_guid(objset_t * os)14892885Sahrens dmu_objset_fsid_guid(objset_t *os)
14902885Sahrens {
149110298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
14922885Sahrens }
14932885Sahrens 
14942885Sahrens void
dmu_objset_fast_stat(objset_t * os,dmu_objset_stats_t * stat)14952885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1496789Sahrens {
149710298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
149810298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
149910298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
15002885Sahrens }
15012885Sahrens 
15022885Sahrens void
dmu_objset_stats(objset_t * os,nvlist_t * nv)15032885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
15042885Sahrens {
150510298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
150610298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
15072885Sahrens 
150810298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
150910298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
15102885Sahrens 
15112885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
151210298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
15139396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
15149396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1515789Sahrens }
1516789Sahrens 
1517789Sahrens int
dmu_objset_is_snapshot(objset_t * os)1518789Sahrens dmu_objset_is_snapshot(objset_t *os)
1519789Sahrens {
152010298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
152110298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1522789Sahrens 	else
1523789Sahrens 		return (B_FALSE);
1524789Sahrens }
1525789Sahrens 
1526789Sahrens int
dmu_snapshot_realname(objset_t * os,char * name,char * real,int maxlen,boolean_t * conflict)15276492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
15286492Stimh     boolean_t *conflict)
15296492Stimh {
153010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
15316492Stimh 	uint64_t ignored;
15326492Stimh 
15336492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
15346492Stimh 		return (ENOENT);
15356492Stimh 
15366492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
15376492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
15386492Stimh 	    real, maxlen, conflict));
15396492Stimh }
15406492Stimh 
15416492Stimh int
dmu_snapshot_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp,boolean_t * case_conflict)1542789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
15435663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1544789Sahrens {
154510298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1546789Sahrens 	zap_cursor_t cursor;
1547789Sahrens 	zap_attribute_t attr;
1548789Sahrens 
1549789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1550789Sahrens 		return (ENOENT);
1551789Sahrens 
1552789Sahrens 	zap_cursor_init_serialized(&cursor,
1553789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1554789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1555789Sahrens 
1556885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1557885Sahrens 		zap_cursor_fini(&cursor);
1558885Sahrens 		return (ENOENT);
1559885Sahrens 	}
1560885Sahrens 
1561885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1562885Sahrens 		zap_cursor_fini(&cursor);
1563885Sahrens 		return (ENAMETOOLONG);
1564885Sahrens 	}
1565885Sahrens 
1566885Sahrens 	(void) strcpy(name, attr.za_name);
1567885Sahrens 	if (idp)
1568885Sahrens 		*idp = attr.za_first_integer;
15695663Sck153898 	if (case_conflict)
15705663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1571885Sahrens 	zap_cursor_advance(&cursor);
1572885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1573885Sahrens 	zap_cursor_fini(&cursor);
1574885Sahrens 
1575885Sahrens 	return (0);
1576885Sahrens }
1577885Sahrens 
1578885Sahrens int
dmu_dir_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp)1579885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1580885Sahrens     uint64_t *idp, uint64_t *offp)
1581885Sahrens {
158210298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1583885Sahrens 	zap_cursor_t cursor;
1584885Sahrens 	zap_attribute_t attr;
1585885Sahrens 
1586885Sahrens 	/* there is no next dir on a snapshot! */
158710298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1588885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1589885Sahrens 		return (ENOENT);
1590885Sahrens 
1591885Sahrens 	zap_cursor_init_serialized(&cursor,
1592885Sahrens 	    dd->dd_pool->dp_meta_objset,
1593885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1594885Sahrens 
1595885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1596885Sahrens 		zap_cursor_fini(&cursor);
1597885Sahrens 		return (ENOENT);
1598885Sahrens 	}
1599885Sahrens 
1600885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1601885Sahrens 		zap_cursor_fini(&cursor);
1602789Sahrens 		return (ENAMETOOLONG);
1603885Sahrens 	}
1604789Sahrens 
1605789Sahrens 	(void) strcpy(name, attr.za_name);
1606885Sahrens 	if (idp)
1607885Sahrens 		*idp = attr.za_first_integer;
1608789Sahrens 	zap_cursor_advance(&cursor);
1609789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1610885Sahrens 	zap_cursor_fini(&cursor);
1611789Sahrens 
1612789Sahrens 	return (0);
1613789Sahrens }
1614789Sahrens 
16157046Sahrens struct findarg {
161611209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
16177046Sahrens 	void *arg;
16187046Sahrens };
16197046Sahrens 
16207046Sahrens /* ARGSUSED */
16217046Sahrens static int
findfunc(spa_t * spa,uint64_t dsobj,const char * dsname,void * arg)16227046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
16237046Sahrens {
16247046Sahrens 	struct findarg *fa = arg;
162511209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
16267046Sahrens }
16277046Sahrens 
1628789Sahrens /*
1629789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
16307046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1631789Sahrens  */
16322199Sahrens int
dmu_objset_find(char * name,int func (const char *,void *),void * arg,int flags)163311209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
163411209SMatthew.Ahrens@Sun.COM     int flags)
1635789Sahrens {
16367046Sahrens 	struct findarg fa;
16377046Sahrens 	fa.func = func;
16387046Sahrens 	fa.arg = arg;
16397046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
16407046Sahrens }
16417046Sahrens 
16427046Sahrens /*
16437046Sahrens  * Find all objsets under name, call func on each
16447046Sahrens  */
16457046Sahrens int
dmu_objset_find_spa(spa_t * spa,const char * name,int func (spa_t *,uint64_t,const char *,void *),void * arg,int flags)16467046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
16477046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
16487046Sahrens {
1649789Sahrens 	dsl_dir_t *dd;
16507046Sahrens 	dsl_pool_t *dp;
16517046Sahrens 	dsl_dataset_t *ds;
1652789Sahrens 	zap_cursor_t zc;
16533978Smmusante 	zap_attribute_t *attr;
1654789Sahrens 	char *child;
16557046Sahrens 	uint64_t thisobj;
16567046Sahrens 	int err;
1657789Sahrens 
16587046Sahrens 	if (name == NULL)
16597046Sahrens 		name = spa_name(spa);
16607046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
16611544Seschrock 	if (err)
16622199Sahrens 		return (err);
1663789Sahrens 
16647046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
16657046Sahrens 	if (dd->dd_myname[0] == '$') {
16667046Sahrens 		dsl_dir_close(dd, FTAG);
16677046Sahrens 		return (0);
16687046Sahrens 	}
16697046Sahrens 
16707046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
16713978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16727046Sahrens 	dp = dd->dd_pool;
1673789Sahrens 
1674789Sahrens 	/*
1675789Sahrens 	 * Iterate over all children.
1676789Sahrens 	 */
16772417Sahrens 	if (flags & DS_FIND_CHILDREN) {
16787046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16792417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
16803978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
16812417Sahrens 		    (void) zap_cursor_advance(&zc)) {
16823978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
16833978Smmusante 			ASSERT(attr->za_num_integers == 1);
1684789Sahrens 
168511165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
16867046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
168711165SMatthew.Ahrens@Sun.COM 			strfree(child);
16882417Sahrens 			if (err)
16892417Sahrens 				break;
16902417Sahrens 		}
16912417Sahrens 		zap_cursor_fini(&zc);
16922199Sahrens 
16932417Sahrens 		if (err) {
16942417Sahrens 			dsl_dir_close(dd, FTAG);
16953978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
16962417Sahrens 			return (err);
16972417Sahrens 		}
1698789Sahrens 	}
1699789Sahrens 
1700789Sahrens 	/*
1701789Sahrens 	 * Iterate over all snapshots.
1702789Sahrens 	 */
17037046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
17047046Sahrens 		if (!dsl_pool_sync_context(dp))
17057046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
17067046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17077046Sahrens 		if (!dsl_pool_sync_context(dp))
17087046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1709789Sahrens 
17107046Sahrens 		if (err == 0) {
17117046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
17127046Sahrens 			dsl_dataset_rele(ds, FTAG);
1713789Sahrens 
17147046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
17157046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
17167046Sahrens 			    (void) zap_cursor_advance(&zc)) {
17177046Sahrens 				ASSERT(attr->za_integer_length ==
17187046Sahrens 				    sizeof (uint64_t));
17197046Sahrens 				ASSERT(attr->za_num_integers == 1);
1720789Sahrens 
172111165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
172211165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
17237046Sahrens 				err = func(spa, attr->za_first_integer,
17247046Sahrens 				    child, arg);
172511165SMatthew.Ahrens@Sun.COM 				strfree(child);
17267046Sahrens 				if (err)
17277046Sahrens 					break;
17287046Sahrens 			}
17297046Sahrens 			zap_cursor_fini(&zc);
1730789Sahrens 		}
1731789Sahrens 	}
1732789Sahrens 
1733789Sahrens 	dsl_dir_close(dd, FTAG);
17343978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1735789Sahrens 
17362199Sahrens 	if (err)
17372199Sahrens 		return (err);
17382199Sahrens 
1739789Sahrens 	/*
1740789Sahrens 	 * Apply to self if appropriate.
1741789Sahrens 	 */
17427046Sahrens 	err = func(spa, thisobj, name, arg);
17432199Sahrens 	return (err);
1744789Sahrens }
17455326Sek110237 
17468415SRichard.Morris@Sun.COM /* ARGSUSED */
17478415SRichard.Morris@Sun.COM int
dmu_objset_prefetch(const char * name,void * arg)174811209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
17498415SRichard.Morris@Sun.COM {
17508415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
17518415SRichard.Morris@Sun.COM 
17528415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
17538415SRichard.Morris@Sun.COM 		return (0);
17548415SRichard.Morris@Sun.COM 
17558415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
17568415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
175710298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
17588415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
17598415SRichard.Morris@Sun.COM 			zbookmark_t zb;
17608415SRichard.Morris@Sun.COM 
176110922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
176210922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
17638415SRichard.Morris@Sun.COM 
176412296SLin.Ling@Sun.COM 			(void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
17658415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
17668415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
17678415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
17688415SRichard.Morris@Sun.COM 			    &aflags, &zb);
17698415SRichard.Morris@Sun.COM 		}
17708415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
17718415SRichard.Morris@Sun.COM 	}
17728415SRichard.Morris@Sun.COM 
17738415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
17748415SRichard.Morris@Sun.COM 	return (0);
17758415SRichard.Morris@Sun.COM }
17768415SRichard.Morris@Sun.COM 
17775326Sek110237 void
dmu_objset_set_user(objset_t * os,void * user_ptr)17785326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
17795326Sek110237 {
178010298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
178110298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
17825326Sek110237 }
17835326Sek110237 
17845326Sek110237 void *
dmu_objset_get_user(objset_t * os)17855326Sek110237 dmu_objset_get_user(objset_t *os)
17865326Sek110237 {
178710298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
178810298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
17895326Sek110237 }
1790