xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 13043:8c712bbb18ea)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * 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 /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/types.h>
26789Sahrens #include <sys/param.h>
27789Sahrens #include <sys/errno.h>
28789Sahrens #include <sys/uio.h>
29789Sahrens #include <sys/buf.h>
30789Sahrens #include <sys/modctl.h>
31789Sahrens #include <sys/open.h>
32789Sahrens #include <sys/file.h>
33789Sahrens #include <sys/kmem.h>
34789Sahrens #include <sys/conf.h>
35789Sahrens #include <sys/cmn_err.h>
36789Sahrens #include <sys/stat.h>
37789Sahrens #include <sys/zfs_ioctl.h>
3810972SRic.Aleshire@Sun.COM #include <sys/zfs_vfsops.h>
395331Samw #include <sys/zfs_znode.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/spa.h>
423912Slling #include <sys/spa_impl.h>
43789Sahrens #include <sys/vdev.h>
4410972SRic.Aleshire@Sun.COM #include <sys/priv_impl.h>
45789Sahrens #include <sys/dmu.h>
46789Sahrens #include <sys/dsl_dir.h>
47789Sahrens #include <sys/dsl_dataset.h>
48789Sahrens #include <sys/dsl_prop.h>
494543Smarks #include <sys/dsl_deleg.h>
504543Smarks #include <sys/dmu_objset.h>
51789Sahrens #include <sys/ddi.h>
52789Sahrens #include <sys/sunddi.h>
53789Sahrens #include <sys/sunldi.h>
54789Sahrens #include <sys/policy.h>
55789Sahrens #include <sys/zone.h>
56789Sahrens #include <sys/nvpair.h>
57789Sahrens #include <sys/pathname.h>
58789Sahrens #include <sys/mount.h>
59789Sahrens #include <sys/sdt.h>
60789Sahrens #include <sys/fs/zfs.h>
61789Sahrens #include <sys/zfs_ctldir.h>
625331Samw #include <sys/zfs_dir.h>
6312527SChris.Kirby@oracle.com #include <sys/zfs_onexit.h>
642885Sahrens #include <sys/zvol.h>
6512296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
664543Smarks #include <sharefs/share.h>
675326Sek110237 #include <sys/dmu_objset.h>
68789Sahrens 
69789Sahrens #include "zfs_namecheck.h"
702676Seschrock #include "zfs_prop.h"
714543Smarks #include "zfs_deleg.h"
7211935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
73789Sahrens 
74789Sahrens extern struct modlfs zfs_modlfs;
75789Sahrens 
76789Sahrens extern void zfs_init(void);
77789Sahrens extern void zfs_fini(void);
78789Sahrens 
79789Sahrens ldi_ident_t zfs_li = NULL;
80789Sahrens dev_info_t *zfs_dip;
81789Sahrens 
82789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
834543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
84789Sahrens 
859234SGeorge.Wilson@Sun.COM typedef enum {
869234SGeorge.Wilson@Sun.COM 	NO_NAME,
879234SGeorge.Wilson@Sun.COM 	POOL_NAME,
889234SGeorge.Wilson@Sun.COM 	DATASET_NAME
899234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
909234SGeorge.Wilson@Sun.COM 
91789Sahrens typedef struct zfs_ioc_vec {
92789Sahrens 	zfs_ioc_func_t		*zvec_func;
93789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
949234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
954543Smarks 	boolean_t		zvec_his_log;
969234SGeorge.Wilson@Sun.COM 	boolean_t		zvec_pool_check;
97789Sahrens } zfs_ioc_vec_t;
98789Sahrens 
999396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
1009396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
1019396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
1029396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
1039396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
1049396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
1059396SMatthew.Ahrens@Sun.COM };
1069396SMatthew.Ahrens@Sun.COM 
1079396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
10811022STom.Erickson@Sun.COM static int zfs_check_settable(const char *name, nvpair_t *property,
10911022STom.Erickson@Sun.COM     cred_t *cr);
11011022STom.Erickson@Sun.COM static int zfs_check_clearable(char *dataset, nvlist_t *props,
11111022STom.Erickson@Sun.COM     nvlist_t **errors);
1127184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1137184Stimh     boolean_t *);
11411022STom.Erickson@Sun.COM int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
1157184Stimh 
116789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
117789Sahrens void
118789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
119789Sahrens {
120789Sahrens 	const char *newfile;
12112296SLin.Ling@Sun.COM 	char buf[512];
122789Sahrens 	va_list adx;
123789Sahrens 
124789Sahrens 	/*
125789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
126789Sahrens 	 */
127789Sahrens 	newfile = strrchr(file, '/');
128789Sahrens 	if (newfile != NULL) {
129789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
130789Sahrens 	} else {
131789Sahrens 		newfile = file;
132789Sahrens 	}
133789Sahrens 
134789Sahrens 	va_start(adx, fmt);
135789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
136789Sahrens 	va_end(adx);
137789Sahrens 
138789Sahrens 	/*
139789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
140789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
141789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
142789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
143789Sahrens 	 * arg0 = file name
144789Sahrens 	 * arg1 = function name
145789Sahrens 	 * arg2 = line number
146789Sahrens 	 * arg3 = message
147789Sahrens 	 */
148789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
149789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
150789Sahrens }
151789Sahrens 
1524543Smarks static void
1534715Sek110237 history_str_free(char *buf)
1544715Sek110237 {
1554715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1564715Sek110237 }
1574715Sek110237 
1584715Sek110237 static char *
1594715Sek110237 history_str_get(zfs_cmd_t *zc)
1604715Sek110237 {
1614715Sek110237 	char *buf;
1624715Sek110237 
1634715Sek110237 	if (zc->zc_history == NULL)
1644715Sek110237 		return (NULL);
1654715Sek110237 
1664715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1674715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1684715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1694715Sek110237 		history_str_free(buf);
1704715Sek110237 		return (NULL);
1714715Sek110237 	}
1724715Sek110237 
1734715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1744715Sek110237 
1754715Sek110237 	return (buf);
1764715Sek110237 }
1774715Sek110237 
1785375Stimh /*
1797042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1807042Sgw25295  */
1817042Sgw25295 static boolean_t
1827042Sgw25295 zfs_is_bootfs(const char *name)
1837042Sgw25295 {
18410298SMatthew.Ahrens@Sun.COM 	objset_t *os;
18510298SMatthew.Ahrens@Sun.COM 
18610298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
18710298SMatthew.Ahrens@Sun.COM 		boolean_t ret;
18810922SJeff.Bonwick@Sun.COM 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
18910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
19010298SMatthew.Ahrens@Sun.COM 		return (ret);
1917042Sgw25295 	}
19210298SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1937042Sgw25295 }
1947042Sgw25295 
1957042Sgw25295 /*
1967184Stimh  * zfs_earlier_version
1975375Stimh  *
1985375Stimh  *	Return non-zero if the spa version is less than requested version.
1995375Stimh  */
2005331Samw static int
2017184Stimh zfs_earlier_version(const char *name, int version)
2025331Samw {
2035331Samw 	spa_t *spa;
2045331Samw 
2055331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
2065331Samw 		if (spa_version(spa) < version) {
2075331Samw 			spa_close(spa, FTAG);
2085331Samw 			return (1);
2095331Samw 		}
2105331Samw 		spa_close(spa, FTAG);
2115331Samw 	}
2125331Samw 	return (0);
2135331Samw }
2145331Samw 
2155977Smarks /*
2166689Smaybee  * zpl_earlier_version
2175977Smarks  *
2186689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2195977Smarks  */
2206689Smaybee static boolean_t
2216689Smaybee zpl_earlier_version(const char *name, int version)
2225977Smarks {
2235977Smarks 	objset_t *os;
2246689Smaybee 	boolean_t rc = B_TRUE;
2255977Smarks 
22610298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
2276689Smaybee 		uint64_t zplversion;
2286689Smaybee 
22910298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
23010298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
23110298SMatthew.Ahrens@Sun.COM 			return (B_TRUE);
23210298SMatthew.Ahrens@Sun.COM 		}
23310298SMatthew.Ahrens@Sun.COM 		/* XXX reading from non-owned objset */
2346689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2356689Smaybee 			rc = zplversion < version;
23610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
2375977Smarks 	}
2385977Smarks 	return (rc);
2395977Smarks }
2405977Smarks 
2414715Sek110237 static void
2424543Smarks zfs_log_history(zfs_cmd_t *zc)
2434543Smarks {
2444543Smarks 	spa_t *spa;
2454603Sahrens 	char *buf;
2464543Smarks 
2474715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2484577Sahrens 		return;
2494577Sahrens 
2504715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2514715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2524715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2534715Sek110237 		spa_close(spa, FTAG);
2544543Smarks 	}
2554715Sek110237 	history_str_free(buf);
2564543Smarks }
2574543Smarks 
258789Sahrens /*
259789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
260789Sahrens  * and can be used in the local zone, as there is no associated dataset.
261789Sahrens  */
262789Sahrens /* ARGSUSED */
263789Sahrens static int
2644543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
265789Sahrens {
266789Sahrens 	return (0);
267789Sahrens }
268789Sahrens 
269789Sahrens /*
270789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
271789Sahrens  * no privileges, but must be visible in the local zone.
272789Sahrens  */
273789Sahrens /* ARGSUSED */
274789Sahrens static int
2754543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
276789Sahrens {
277789Sahrens 	if (INGLOBALZONE(curproc) ||
2784543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
279789Sahrens 		return (0);
280789Sahrens 
281789Sahrens 	return (ENOENT);
282789Sahrens }
283789Sahrens 
284789Sahrens static int
28512786SChris.Kirby@oracle.com zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
286789Sahrens {
287789Sahrens 	int writable = 1;
288789Sahrens 
289789Sahrens 	/*
290789Sahrens 	 * The dataset must be visible by this zone -- check this first
291789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
292789Sahrens 	 */
293789Sahrens 	if (!INGLOBALZONE(curproc) &&
294789Sahrens 	    !zone_dataset_visible(dataset, &writable))
295789Sahrens 		return (ENOENT);
296789Sahrens 
297789Sahrens 	if (INGLOBALZONE(curproc)) {
298789Sahrens 		/*
299789Sahrens 		 * If the fs is zoned, only root can access it from the
300789Sahrens 		 * global zone.
301789Sahrens 		 */
302789Sahrens 		if (secpolicy_zfs(cr) && zoned)
303789Sahrens 			return (EPERM);
304789Sahrens 	} else {
305789Sahrens 		/*
306789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
307789Sahrens 		 */
308789Sahrens 		if (!zoned)
309789Sahrens 			return (EPERM);
310789Sahrens 
311789Sahrens 		/* must be writable by this zone */
312789Sahrens 		if (!writable)
313789Sahrens 			return (EPERM);
314789Sahrens 	}
315789Sahrens 	return (0);
316789Sahrens }
317789Sahrens 
31812786SChris.Kirby@oracle.com static int
31912786SChris.Kirby@oracle.com zfs_dozonecheck(const char *dataset, cred_t *cr)
32012786SChris.Kirby@oracle.com {
32112786SChris.Kirby@oracle.com 	uint64_t zoned;
32212786SChris.Kirby@oracle.com 
32312786SChris.Kirby@oracle.com 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
32412786SChris.Kirby@oracle.com 		return (ENOENT);
32512786SChris.Kirby@oracle.com 
32612786SChris.Kirby@oracle.com 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
32712786SChris.Kirby@oracle.com }
32812786SChris.Kirby@oracle.com 
32912786SChris.Kirby@oracle.com static int
33012786SChris.Kirby@oracle.com zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
33112786SChris.Kirby@oracle.com {
33212786SChris.Kirby@oracle.com 	uint64_t zoned;
33312786SChris.Kirby@oracle.com 
33412786SChris.Kirby@oracle.com 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
33512786SChris.Kirby@oracle.com 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
33612786SChris.Kirby@oracle.com 		rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
33712786SChris.Kirby@oracle.com 		return (ENOENT);
33812786SChris.Kirby@oracle.com 	}
33912786SChris.Kirby@oracle.com 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
34012786SChris.Kirby@oracle.com 
34112786SChris.Kirby@oracle.com 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
34212786SChris.Kirby@oracle.com }
34312786SChris.Kirby@oracle.com 
344789Sahrens int
3454543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
346789Sahrens {
347789Sahrens 	int error;
348789Sahrens 
3494543Smarks 	error = zfs_dozonecheck(name, cr);
3504543Smarks 	if (error == 0) {
3514543Smarks 		error = secpolicy_zfs(cr);
3524670Sahrens 		if (error)
3534543Smarks 			error = dsl_deleg_access(name, perm, cr);
3544543Smarks 	}
3554543Smarks 	return (error);
3564543Smarks }
3574543Smarks 
35812786SChris.Kirby@oracle.com int
35912786SChris.Kirby@oracle.com zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
36012786SChris.Kirby@oracle.com     const char *perm, cred_t *cr)
36112786SChris.Kirby@oracle.com {
36212786SChris.Kirby@oracle.com 	int error;
36312786SChris.Kirby@oracle.com 
36412786SChris.Kirby@oracle.com 	error = zfs_dozonecheck_ds(name, ds, cr);
36512786SChris.Kirby@oracle.com 	if (error == 0) {
36612786SChris.Kirby@oracle.com 		error = secpolicy_zfs(cr);
36712786SChris.Kirby@oracle.com 		if (error)
36812786SChris.Kirby@oracle.com 			error = dsl_deleg_access_impl(ds, perm, cr);
36912786SChris.Kirby@oracle.com 	}
37012786SChris.Kirby@oracle.com 	return (error);
37112786SChris.Kirby@oracle.com }
37212786SChris.Kirby@oracle.com 
37310972SRic.Aleshire@Sun.COM /*
37410972SRic.Aleshire@Sun.COM  * Policy for setting the security label property.
37510972SRic.Aleshire@Sun.COM  *
37610972SRic.Aleshire@Sun.COM  * Returns 0 for success, non-zero for access and other errors.
37710972SRic.Aleshire@Sun.COM  */
37810972SRic.Aleshire@Sun.COM static int
37911022STom.Erickson@Sun.COM zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
38010972SRic.Aleshire@Sun.COM {
38110972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
38210972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl, new_sl;
38310972SRic.Aleshire@Sun.COM 	boolean_t	new_default = FALSE;
38410972SRic.Aleshire@Sun.COM 	uint64_t	zoned;
38510972SRic.Aleshire@Sun.COM 	int		needed_priv = -1;
38610972SRic.Aleshire@Sun.COM 	int		error;
38710972SRic.Aleshire@Sun.COM 
38810972SRic.Aleshire@Sun.COM 	/* First get the existing dataset label. */
38910972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
39010972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
39110972SRic.Aleshire@Sun.COM 	if (error)
39210972SRic.Aleshire@Sun.COM 		return (EPERM);
39310972SRic.Aleshire@Sun.COM 
39410972SRic.Aleshire@Sun.COM 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
39510972SRic.Aleshire@Sun.COM 		new_default = TRUE;
39610972SRic.Aleshire@Sun.COM 
39710972SRic.Aleshire@Sun.COM 	/* The label must be translatable */
39810972SRic.Aleshire@Sun.COM 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
39910972SRic.Aleshire@Sun.COM 		return (EINVAL);
40010972SRic.Aleshire@Sun.COM 
40110972SRic.Aleshire@Sun.COM 	/*
40210972SRic.Aleshire@Sun.COM 	 * In a non-global zone, disallow attempts to set a label that
40310972SRic.Aleshire@Sun.COM 	 * doesn't match that of the zone; otherwise no other checks
40410972SRic.Aleshire@Sun.COM 	 * are needed.
40510972SRic.Aleshire@Sun.COM 	 */
40610972SRic.Aleshire@Sun.COM 	if (!INGLOBALZONE(curproc)) {
40710972SRic.Aleshire@Sun.COM 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
40810972SRic.Aleshire@Sun.COM 			return (EPERM);
40910972SRic.Aleshire@Sun.COM 		return (0);
41010972SRic.Aleshire@Sun.COM 	}
41110972SRic.Aleshire@Sun.COM 
41210972SRic.Aleshire@Sun.COM 	/*
41310972SRic.Aleshire@Sun.COM 	 * For global-zone datasets (i.e., those whose zoned property is
41410972SRic.Aleshire@Sun.COM 	 * "off", verify that the specified new label is valid for the
41510972SRic.Aleshire@Sun.COM 	 * global zone.
41610972SRic.Aleshire@Sun.COM 	 */
41710972SRic.Aleshire@Sun.COM 	if (dsl_prop_get_integer(name,
41810972SRic.Aleshire@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
41910972SRic.Aleshire@Sun.COM 		return (EPERM);
42010972SRic.Aleshire@Sun.COM 	if (!zoned) {
42110972SRic.Aleshire@Sun.COM 		if (zfs_check_global_label(name, strval) != 0)
42210972SRic.Aleshire@Sun.COM 			return (EPERM);
42310972SRic.Aleshire@Sun.COM 	}
42410972SRic.Aleshire@Sun.COM 
42510972SRic.Aleshire@Sun.COM 	/*
42610972SRic.Aleshire@Sun.COM 	 * If the existing dataset label is nondefault, check if the
42710972SRic.Aleshire@Sun.COM 	 * dataset is mounted (label cannot be changed while mounted).
42810972SRic.Aleshire@Sun.COM 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
42910972SRic.Aleshire@Sun.COM 	 * mounted (or isn't a dataset, doesn't exist, ...).
43010972SRic.Aleshire@Sun.COM 	 */
43110972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
43211022STom.Erickson@Sun.COM 		objset_t *os;
43311022STom.Erickson@Sun.COM 		static char *setsl_tag = "setsl_tag";
43411022STom.Erickson@Sun.COM 
43510972SRic.Aleshire@Sun.COM 		/*
43610972SRic.Aleshire@Sun.COM 		 * Try to own the dataset; abort if there is any error,
43710972SRic.Aleshire@Sun.COM 		 * (e.g., already mounted, in use, or other error).
43810972SRic.Aleshire@Sun.COM 		 */
43910972SRic.Aleshire@Sun.COM 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
44011022STom.Erickson@Sun.COM 		    setsl_tag, &os);
44110972SRic.Aleshire@Sun.COM 		if (error)
44210972SRic.Aleshire@Sun.COM 			return (EPERM);
44310972SRic.Aleshire@Sun.COM 
44411022STom.Erickson@Sun.COM 		dmu_objset_disown(os, setsl_tag);
44511022STom.Erickson@Sun.COM 
44610972SRic.Aleshire@Sun.COM 		if (new_default) {
44710972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
44810972SRic.Aleshire@Sun.COM 			goto out_check;
44910972SRic.Aleshire@Sun.COM 		}
45010972SRic.Aleshire@Sun.COM 
45110972SRic.Aleshire@Sun.COM 		if (hexstr_to_label(strval, &new_sl) != 0)
45210972SRic.Aleshire@Sun.COM 			return (EPERM);
45310972SRic.Aleshire@Sun.COM 
45410972SRic.Aleshire@Sun.COM 		if (blstrictdom(&ds_sl, &new_sl))
45510972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
45610972SRic.Aleshire@Sun.COM 		else if (blstrictdom(&new_sl, &ds_sl))
45710972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
45810972SRic.Aleshire@Sun.COM 	} else {
45910972SRic.Aleshire@Sun.COM 		/* dataset currently has a default label */
46010972SRic.Aleshire@Sun.COM 		if (!new_default)
46110972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
46210972SRic.Aleshire@Sun.COM 	}
46310972SRic.Aleshire@Sun.COM 
46410972SRic.Aleshire@Sun.COM out_check:
46510972SRic.Aleshire@Sun.COM 	if (needed_priv != -1)
46610972SRic.Aleshire@Sun.COM 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
46710972SRic.Aleshire@Sun.COM 	return (0);
46810972SRic.Aleshire@Sun.COM }
46910972SRic.Aleshire@Sun.COM 
4704543Smarks static int
47111022STom.Erickson@Sun.COM zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
47211022STom.Erickson@Sun.COM     cred_t *cr)
4734543Smarks {
47411022STom.Erickson@Sun.COM 	char *strval;
47511022STom.Erickson@Sun.COM 
4764543Smarks 	/*
4774543Smarks 	 * Check permissions for special properties.
4784543Smarks 	 */
4794543Smarks 	switch (prop) {
4804543Smarks 	case ZFS_PROP_ZONED:
4814543Smarks 		/*
4824543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
4834543Smarks 		 */
4844543Smarks 		if (!INGLOBALZONE(curproc))
4854543Smarks 			return (EPERM);
4864543Smarks 		break;
487789Sahrens 
4884543Smarks 	case ZFS_PROP_QUOTA:
4894543Smarks 		if (!INGLOBALZONE(curproc)) {
4904543Smarks 			uint64_t zoned;
4914543Smarks 			char setpoint[MAXNAMELEN];
4924543Smarks 			/*
4934543Smarks 			 * Unprivileged users are allowed to modify the
4944543Smarks 			 * quota on things *under* (ie. contained by)
4954543Smarks 			 * the thing they own.
4964543Smarks 			 */
49711022STom.Erickson@Sun.COM 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
4984543Smarks 			    setpoint))
4994543Smarks 				return (EPERM);
50011022STom.Erickson@Sun.COM 			if (!zoned || strlen(dsname) <= strlen(setpoint))
5014543Smarks 				return (EPERM);
5024543Smarks 		}
5034670Sahrens 		break;
50410972SRic.Aleshire@Sun.COM 
50510972SRic.Aleshire@Sun.COM 	case ZFS_PROP_MLSLABEL:
50610972SRic.Aleshire@Sun.COM 		if (!is_system_labeled())
50710972SRic.Aleshire@Sun.COM 			return (EPERM);
50811022STom.Erickson@Sun.COM 
50911022STom.Erickson@Sun.COM 		if (nvpair_value_string(propval, &strval) == 0) {
51011022STom.Erickson@Sun.COM 			int err;
51111022STom.Erickson@Sun.COM 
51211022STom.Erickson@Sun.COM 			err = zfs_set_slabel_policy(dsname, strval, CRED());
51311022STom.Erickson@Sun.COM 			if (err != 0)
51411022STom.Erickson@Sun.COM 				return (err);
51511022STom.Erickson@Sun.COM 		}
51610972SRic.Aleshire@Sun.COM 		break;
5174543Smarks 	}
5184543Smarks 
51911022STom.Erickson@Sun.COM 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
520789Sahrens }
521789Sahrens 
5224543Smarks int
5234543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
5244543Smarks {
5254543Smarks 	int error;
5264543Smarks 
5274543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
5284543Smarks 	if (error)
5294543Smarks 		return (error);
5304543Smarks 
5314543Smarks 	/*
5324543Smarks 	 * permission to set permissions will be evaluated later in
5334543Smarks 	 * dsl_deleg_can_allow()
5344543Smarks 	 */
5354543Smarks 	return (0);
5364543Smarks }
5374543Smarks 
5384543Smarks int
5394543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
5404543Smarks {
54110588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
54210588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_ROLLBACK, cr));
5434543Smarks }
5444543Smarks 
5454543Smarks int
5464543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
5474543Smarks {
54812786SChris.Kirby@oracle.com 	spa_t *spa;
54912786SChris.Kirby@oracle.com 	dsl_pool_t *dp;
55012786SChris.Kirby@oracle.com 	dsl_dataset_t *ds;
55112786SChris.Kirby@oracle.com 	char *cp;
55212786SChris.Kirby@oracle.com 	int error;
55312786SChris.Kirby@oracle.com 
55412786SChris.Kirby@oracle.com 	/*
55512786SChris.Kirby@oracle.com 	 * Generate the current snapshot name from the given objsetid, then
55612786SChris.Kirby@oracle.com 	 * use that name for the secpolicy/zone checks.
55712786SChris.Kirby@oracle.com 	 */
55812786SChris.Kirby@oracle.com 	cp = strchr(zc->zc_name, '@');
55912786SChris.Kirby@oracle.com 	if (cp == NULL)
56012786SChris.Kirby@oracle.com 		return (EINVAL);
56112786SChris.Kirby@oracle.com 	error = spa_open(zc->zc_name, &spa, FTAG);
56212786SChris.Kirby@oracle.com 	if (error)
56312786SChris.Kirby@oracle.com 		return (error);
56412786SChris.Kirby@oracle.com 
56512786SChris.Kirby@oracle.com 	dp = spa_get_dsl(spa);
56612786SChris.Kirby@oracle.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
56712786SChris.Kirby@oracle.com 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
56812786SChris.Kirby@oracle.com 	rw_exit(&dp->dp_config_rwlock);
56912786SChris.Kirby@oracle.com 	spa_close(spa, FTAG);
57012786SChris.Kirby@oracle.com 	if (error)
57112786SChris.Kirby@oracle.com 		return (error);
57212786SChris.Kirby@oracle.com 
57312786SChris.Kirby@oracle.com 	dsl_dataset_name(ds, zc->zc_name);
57412786SChris.Kirby@oracle.com 
57512786SChris.Kirby@oracle.com 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
57612786SChris.Kirby@oracle.com 	    ZFS_DELEG_PERM_SEND, cr);
57712786SChris.Kirby@oracle.com 	dsl_dataset_rele(ds, FTAG);
57812786SChris.Kirby@oracle.com 
57912786SChris.Kirby@oracle.com 	return (error);
5804543Smarks }
5814543Smarks 
5828845Samw@Sun.COM static int
5838845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
5848845Samw@Sun.COM {
5858845Samw@Sun.COM 	vnode_t *vp;
5868845Samw@Sun.COM 	int error;
5878845Samw@Sun.COM 
5888845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5898845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
5908845Samw@Sun.COM 		return (error);
5918845Samw@Sun.COM 
5928845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
5938845Samw@Sun.COM 
5948845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5958845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5968845Samw@Sun.COM 	    zc->zc_name) != 0)) {
5978845Samw@Sun.COM 		VN_RELE(vp);
5988845Samw@Sun.COM 		return (EPERM);
5998845Samw@Sun.COM 	}
6008845Samw@Sun.COM 
6018845Samw@Sun.COM 	VN_RELE(vp);
6028845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
6038845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
6048845Samw@Sun.COM }
6058845Samw@Sun.COM 
6064543Smarks int
6074543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
6084543Smarks {
6094543Smarks 	if (!INGLOBALZONE(curproc))
6104543Smarks 		return (EPERM);
6114543Smarks 
6125367Sahrens 	if (secpolicy_nfs(cr) == 0) {
6134543Smarks 		return (0);
6144543Smarks 	} else {
6158845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
6168845Samw@Sun.COM 	}
6178845Samw@Sun.COM }
6188845Samw@Sun.COM 
6198845Samw@Sun.COM int
6208845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
6218845Samw@Sun.COM {
6228845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
6238845Samw@Sun.COM 		return (EPERM);
6248845Samw@Sun.COM 
6258845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
6268845Samw@Sun.COM 		return (0);
6278845Samw@Sun.COM 	} else {
6288845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
6294543Smarks 	}
6304543Smarks }
6314543Smarks 
632789Sahrens static int
6334543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
634789Sahrens {
635789Sahrens 	char *cp;
636789Sahrens 
637789Sahrens 	/*
638789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
639789Sahrens 	 */
6404543Smarks 	(void) strncpy(parent, datasetname, parentsize);
6414543Smarks 	cp = strrchr(parent, '@');
642789Sahrens 	if (cp != NULL) {
643789Sahrens 		cp[0] = '\0';
644789Sahrens 	} else {
6454543Smarks 		cp = strrchr(parent, '/');
646789Sahrens 		if (cp == NULL)
647789Sahrens 			return (ENOENT);
648789Sahrens 		cp[0] = '\0';
649789Sahrens 	}
650789Sahrens 
6514543Smarks 	return (0);
6524543Smarks }
6534543Smarks 
6544543Smarks int
6554543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
6564543Smarks {
6574543Smarks 	int error;
6584543Smarks 
6594543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
6604543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
6614543Smarks 		return (error);
6624543Smarks 
6634543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
6644543Smarks }
6654543Smarks 
6664543Smarks static int
6674543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
6684543Smarks {
6694543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
6704543Smarks }
6714543Smarks 
6724543Smarks /*
67311314Swilliam.gorrell@sun.com  * Destroying snapshots with delegated permissions requires
67411314Swilliam.gorrell@sun.com  * descendent mount and destroy permissions.
67511314Swilliam.gorrell@sun.com  * Reassemble the full filesystem@snap name so dsl_deleg_access()
67611314Swilliam.gorrell@sun.com  * can do the correct permission check.
67711314Swilliam.gorrell@sun.com  *
67811314Swilliam.gorrell@sun.com  * Since this routine is used when doing a recursive destroy of snapshots
67911314Swilliam.gorrell@sun.com  * and destroying snapshots requires descendent permissions, a successfull
68011314Swilliam.gorrell@sun.com  * check of the top level snapshot applies to snapshots of all descendent
68111314Swilliam.gorrell@sun.com  * datasets as well.
68211314Swilliam.gorrell@sun.com  */
68311314Swilliam.gorrell@sun.com static int
68411314Swilliam.gorrell@sun.com zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
68511314Swilliam.gorrell@sun.com {
68611314Swilliam.gorrell@sun.com 	int error;
68711314Swilliam.gorrell@sun.com 	char *dsname;
68811314Swilliam.gorrell@sun.com 
68911314Swilliam.gorrell@sun.com 	dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
69011314Swilliam.gorrell@sun.com 
69111314Swilliam.gorrell@sun.com 	error = zfs_secpolicy_destroy_perms(dsname, cr);
69211314Swilliam.gorrell@sun.com 
69311314Swilliam.gorrell@sun.com 	strfree(dsname);
69411314Swilliam.gorrell@sun.com 	return (error);
69511314Swilliam.gorrell@sun.com }
69611314Swilliam.gorrell@sun.com 
6974543Smarks int
6984543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
6994543Smarks {
70011022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
7014543Smarks 	int	error;
7024543Smarks 
7034543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
7044543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
7054543Smarks 		return (error);
7064543Smarks 
7074543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
7084543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
7094543Smarks 		return (error);
7104543Smarks 
7114543Smarks 	if ((error = zfs_get_parent(to, parentname,
7124543Smarks 	    sizeof (parentname))) != 0)
7134543Smarks 		return (error);
7144543Smarks 
7154543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
7164543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
7174543Smarks 		return (error);
7184543Smarks 
7194543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
7204543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
7214543Smarks 		return (error);
7224543Smarks 
7234543Smarks 	return (error);
7244543Smarks }
7254543Smarks 
7264543Smarks static int
7274543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
7284543Smarks {
7294543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
7304543Smarks }
7314543Smarks 
7324543Smarks static int
7334543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
7344543Smarks {
73511022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
7364543Smarks 	objset_t *clone;
7374543Smarks 	int error;
7384543Smarks 
7394543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
7404543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
7414543Smarks 	if (error)
7424543Smarks 		return (error);
7434543Smarks 
74410298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
7454543Smarks 
7464543Smarks 	if (error == 0) {
7474543Smarks 		dsl_dataset_t *pclone = NULL;
7484543Smarks 		dsl_dir_t *dd;
74910298SMatthew.Ahrens@Sun.COM 		dd = clone->os_dsl_dataset->ds_dir;
7504543Smarks 
7514543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
7526689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
7536689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
7544543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
7554543Smarks 		if (error) {
75610298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(clone, FTAG);
7574543Smarks 			return (error);
7584543Smarks 		}
7594543Smarks 
7604543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
7614543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
7624543Smarks 
7634543Smarks 		dsl_dataset_name(pclone, parentname);
76410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
7656689Smaybee 		dsl_dataset_rele(pclone, FTAG);
7664543Smarks 		if (error == 0)
7674543Smarks 			error = zfs_secpolicy_write_perms(parentname,
7684543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
7694543Smarks 	}
7704543Smarks 	return (error);
7714543Smarks }
7724543Smarks 
7734543Smarks static int
7744543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
7754543Smarks {
7764543Smarks 	int error;
7774543Smarks 
7784543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7794543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
7804543Smarks 		return (error);
7814543Smarks 
7824543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7834543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
7844543Smarks 		return (error);
7854543Smarks 
7864543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
7874543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
7884543Smarks }
7894543Smarks 
7904543Smarks int
7914543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
7924543Smarks {
79310588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(name,
79410588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
7954543Smarks }
7964543Smarks 
7974543Smarks static int
7984543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
7994543Smarks {
8004543Smarks 
8014543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
8024543Smarks }
8034543Smarks 
8044543Smarks static int
8054543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
8064543Smarks {
80711022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
80811022STom.Erickson@Sun.COM 	int	error;
8094543Smarks 
8104543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
8114543Smarks 	    sizeof (parentname))) != 0)
8124543Smarks 		return (error);
8134543Smarks 
8144543Smarks 	if (zc->zc_value[0] != '\0') {
8154543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
8164543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
8174543Smarks 			return (error);
8184543Smarks 	}
8194543Smarks 
8204543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
8214543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
8224543Smarks 		return (error);
8234543Smarks 
8244543Smarks 	error = zfs_secpolicy_write_perms(parentname,
8254543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
8264543Smarks 
8274543Smarks 	return (error);
8284543Smarks }
8294543Smarks 
8304543Smarks static int
8314543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
8324543Smarks {
8334543Smarks 	int error;
8344543Smarks 
8354543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
8364543Smarks 	if (error) {
8374543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
8384543Smarks 	}
8394543Smarks 	return (error);
840789Sahrens }
841789Sahrens 
842789Sahrens /*
843789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
844789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
845789Sahrens  */
846789Sahrens /* ARGSUSED */
847789Sahrens static int
8484543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
849789Sahrens {
850789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
851789Sahrens 		return (EPERM);
852789Sahrens 
853789Sahrens 	return (0);
854789Sahrens }
855789Sahrens 
856789Sahrens /*
857*13043STim.Haley@Sun.COM  * Policy for object to name lookups.
858*13043STim.Haley@Sun.COM  */
859*13043STim.Haley@Sun.COM /* ARGSUSED */
860*13043STim.Haley@Sun.COM static int
861*13043STim.Haley@Sun.COM zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
862*13043STim.Haley@Sun.COM {
863*13043STim.Haley@Sun.COM 	int error;
864*13043STim.Haley@Sun.COM 
865*13043STim.Haley@Sun.COM 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
866*13043STim.Haley@Sun.COM 		return (0);
867*13043STim.Haley@Sun.COM 
868*13043STim.Haley@Sun.COM 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
869*13043STim.Haley@Sun.COM 	return (error);
870*13043STim.Haley@Sun.COM }
871*13043STim.Haley@Sun.COM 
872*13043STim.Haley@Sun.COM /*
8731544Seschrock  * Policy for fault injection.  Requires all privileges.
8741544Seschrock  */
8751544Seschrock /* ARGSUSED */
8761544Seschrock static int
8774543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
8781544Seschrock {
8791544Seschrock 	return (secpolicy_zinject(cr));
8801544Seschrock }
8811544Seschrock 
8824849Sahrens static int
8834849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
8844849Sahrens {
8854849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
8864849Sahrens 
8875094Slling 	if (prop == ZPROP_INVAL) {
8884849Sahrens 		if (!zfs_prop_user(zc->zc_value))
8894849Sahrens 			return (EINVAL);
8904849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
8914849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
8924849Sahrens 	} else {
89311022STom.Erickson@Sun.COM 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
89411022STom.Erickson@Sun.COM 		    NULL, cr));
8954849Sahrens 	}
8964849Sahrens }
8974849Sahrens 
8989396SMatthew.Ahrens@Sun.COM static int
8999396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
9009396SMatthew.Ahrens@Sun.COM {
9019396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
9029396SMatthew.Ahrens@Sun.COM 	if (err)
9039396SMatthew.Ahrens@Sun.COM 		return (err);
9049396SMatthew.Ahrens@Sun.COM 
9059396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
9069396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
9079396SMatthew.Ahrens@Sun.COM 
9089396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
9099396SMatthew.Ahrens@Sun.COM 		/*
9109396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
9119396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
9129396SMatthew.Ahrens@Sun.COM 		 */
9139396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
9149396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
9159396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
9169396SMatthew.Ahrens@Sun.COM 				return (0);
9179396SMatthew.Ahrens@Sun.COM 		} else {
9189396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
9199396SMatthew.Ahrens@Sun.COM 				return (0);
9209396SMatthew.Ahrens@Sun.COM 		}
9219396SMatthew.Ahrens@Sun.COM 	}
9229396SMatthew.Ahrens@Sun.COM 
9239396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
9249396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
9259396SMatthew.Ahrens@Sun.COM }
9269396SMatthew.Ahrens@Sun.COM 
9279396SMatthew.Ahrens@Sun.COM static int
9289396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
9299396SMatthew.Ahrens@Sun.COM {
9309396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
9319396SMatthew.Ahrens@Sun.COM 	if (err)
9329396SMatthew.Ahrens@Sun.COM 		return (err);
9339396SMatthew.Ahrens@Sun.COM 
9349396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
9359396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
9369396SMatthew.Ahrens@Sun.COM 
9379396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
9389396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
9399396SMatthew.Ahrens@Sun.COM }
9409396SMatthew.Ahrens@Sun.COM 
9419396SMatthew.Ahrens@Sun.COM static int
9429396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
9439396SMatthew.Ahrens@Sun.COM {
94411022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
94511022STom.Erickson@Sun.COM 	    NULL, cr));
9469396SMatthew.Ahrens@Sun.COM }
9479396SMatthew.Ahrens@Sun.COM 
94810242Schris.kirby@sun.com static int
94910242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
95010242Schris.kirby@sun.com {
95110242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
95210242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
95310242Schris.kirby@sun.com }
95410242Schris.kirby@sun.com 
95510242Schris.kirby@sun.com static int
95610242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
95710242Schris.kirby@sun.com {
95810242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
95910242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
96010242Schris.kirby@sun.com }
96110242Schris.kirby@sun.com 
9621544Seschrock /*
963*13043STim.Haley@Sun.COM  * Policy for allowing temporary snapshots to be taken or released
964*13043STim.Haley@Sun.COM  */
965*13043STim.Haley@Sun.COM static int
966*13043STim.Haley@Sun.COM zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
967*13043STim.Haley@Sun.COM {
968*13043STim.Haley@Sun.COM 	/*
969*13043STim.Haley@Sun.COM 	 * A temporary snapshot is the same as a snapshot,
970*13043STim.Haley@Sun.COM 	 * hold, destroy and release all rolled into one.
971*13043STim.Haley@Sun.COM 	 * Delegated diff alone is sufficient that we allow this.
972*13043STim.Haley@Sun.COM 	 */
973*13043STim.Haley@Sun.COM 	int error;
974*13043STim.Haley@Sun.COM 
975*13043STim.Haley@Sun.COM 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
976*13043STim.Haley@Sun.COM 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
977*13043STim.Haley@Sun.COM 		return (0);
978*13043STim.Haley@Sun.COM 
979*13043STim.Haley@Sun.COM 	error = zfs_secpolicy_snapshot(zc, cr);
980*13043STim.Haley@Sun.COM 	if (!error)
981*13043STim.Haley@Sun.COM 		error = zfs_secpolicy_hold(zc, cr);
982*13043STim.Haley@Sun.COM 	if (!error)
983*13043STim.Haley@Sun.COM 		error = zfs_secpolicy_release(zc, cr);
984*13043STim.Haley@Sun.COM 	if (!error)
985*13043STim.Haley@Sun.COM 		error = zfs_secpolicy_destroy(zc, cr);
986*13043STim.Haley@Sun.COM 	return (error);
987*13043STim.Haley@Sun.COM }
988*13043STim.Haley@Sun.COM 
989*13043STim.Haley@Sun.COM /*
990789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
991789Sahrens  */
992789Sahrens static int
9939643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
994789Sahrens {
995789Sahrens 	char *packed;
996789Sahrens 	int error;
9975094Slling 	nvlist_t *list = NULL;
998789Sahrens 
999789Sahrens 	/*
10002676Seschrock 	 * Read in and unpack the user-supplied nvlist.
1001789Sahrens 	 */
10025094Slling 	if (size == 0)
1003789Sahrens 		return (EINVAL);
1004789Sahrens 
1005789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1006789Sahrens 
10079643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
10089643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
1009789Sahrens 		kmem_free(packed, size);
1010789Sahrens 		return (error);
1011789Sahrens 	}
1012789Sahrens 
10135094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1014789Sahrens 		kmem_free(packed, size);
1015789Sahrens 		return (error);
1016789Sahrens 	}
1017789Sahrens 
1018789Sahrens 	kmem_free(packed, size);
1019789Sahrens 
10205094Slling 	*nvp = list;
1021789Sahrens 	return (0);
1022789Sahrens }
1023789Sahrens 
1024789Sahrens static int
102511022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
102611022STom.Erickson@Sun.COM {
102711022STom.Erickson@Sun.COM 	size_t size;
102811022STom.Erickson@Sun.COM 
102911022STom.Erickson@Sun.COM 	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
103011022STom.Erickson@Sun.COM 
103111022STom.Erickson@Sun.COM 	if (size > zc->zc_nvlist_dst_size) {
103211022STom.Erickson@Sun.COM 		nvpair_t *more_errors;
103311022STom.Erickson@Sun.COM 		int n = 0;
103411022STom.Erickson@Sun.COM 
103511022STom.Erickson@Sun.COM 		if (zc->zc_nvlist_dst_size < 1024)
103611022STom.Erickson@Sun.COM 			return (ENOMEM);
103711022STom.Erickson@Sun.COM 
103811022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
103911022STom.Erickson@Sun.COM 		more_errors = nvlist_prev_nvpair(*errors, NULL);
104011022STom.Erickson@Sun.COM 
104111022STom.Erickson@Sun.COM 		do {
104211022STom.Erickson@Sun.COM 			nvpair_t *pair = nvlist_prev_nvpair(*errors,
104311022STom.Erickson@Sun.COM 			    more_errors);
104411022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
104511022STom.Erickson@Sun.COM 			n++;
104611022STom.Erickson@Sun.COM 			VERIFY(nvlist_size(*errors, &size,
104711022STom.Erickson@Sun.COM 			    NV_ENCODE_NATIVE) == 0);
104811022STom.Erickson@Sun.COM 		} while (size > zc->zc_nvlist_dst_size);
104911022STom.Erickson@Sun.COM 
105011022STom.Erickson@Sun.COM 		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
105111022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
105211022STom.Erickson@Sun.COM 		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
105311022STom.Erickson@Sun.COM 		ASSERT(size <= zc->zc_nvlist_dst_size);
105411022STom.Erickson@Sun.COM 	}
105511022STom.Erickson@Sun.COM 
105611022STom.Erickson@Sun.COM 	return (0);
105711022STom.Erickson@Sun.COM }
105811022STom.Erickson@Sun.COM 
105911022STom.Erickson@Sun.COM static int
10602676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
10612676Seschrock {
10622676Seschrock 	char *packed = NULL;
106311807SSam.Falkner@Sun.COM 	int error = 0;
10642676Seschrock 	size_t size;
10652676Seschrock 
10662676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
10672676Seschrock 
10682676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
10692676Seschrock 		error = ENOMEM;
10702676Seschrock 	} else {
10714611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
10722676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
10732676Seschrock 		    KM_SLEEP) == 0);
107411807SSam.Falkner@Sun.COM 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
107511807SSam.Falkner@Sun.COM 		    size, zc->zc_iflags) != 0)
107611807SSam.Falkner@Sun.COM 			error = EFAULT;
10772676Seschrock 		kmem_free(packed, size);
10782676Seschrock 	}
10792676Seschrock 
10802676Seschrock 	zc->zc_nvlist_dst_size = size;
10812676Seschrock 	return (error);
10822676Seschrock }
10832676Seschrock 
10842676Seschrock static int
108511185SSean.McEnroe@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
10869396SMatthew.Ahrens@Sun.COM {
10879396SMatthew.Ahrens@Sun.COM 	objset_t *os;
10889396SMatthew.Ahrens@Sun.COM 	int error;
10899396SMatthew.Ahrens@Sun.COM 
109010298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(dsname, FTAG, &os);
10919396SMatthew.Ahrens@Sun.COM 	if (error)
10929396SMatthew.Ahrens@Sun.COM 		return (error);
109310298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
109410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
109510298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
109610298SMatthew.Ahrens@Sun.COM 	}
109710298SMatthew.Ahrens@Sun.COM 
109810298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_user_ptr_lock);
109911185SSean.McEnroe@Sun.COM 	*zfvp = dmu_objset_get_user(os);
110011185SSean.McEnroe@Sun.COM 	if (*zfvp) {
110111185SSean.McEnroe@Sun.COM 		VFS_HOLD((*zfvp)->z_vfs);
11029396SMatthew.Ahrens@Sun.COM 	} else {
11039396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
11049396SMatthew.Ahrens@Sun.COM 	}
110510298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_user_ptr_lock);
110610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
11079396SMatthew.Ahrens@Sun.COM 	return (error);
11089396SMatthew.Ahrens@Sun.COM }
11099396SMatthew.Ahrens@Sun.COM 
11109396SMatthew.Ahrens@Sun.COM /*
11119396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
11129396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
11139396SMatthew.Ahrens@Sun.COM  */
11149396SMatthew.Ahrens@Sun.COM static int
111512620SMark.Shellenbaum@Oracle.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
11169396SMatthew.Ahrens@Sun.COM {
11179396SMatthew.Ahrens@Sun.COM 	int error = 0;
11189396SMatthew.Ahrens@Sun.COM 
111911185SSean.McEnroe@Sun.COM 	if (getzfsvfs(name, zfvp) != 0)
112011185SSean.McEnroe@Sun.COM 		error = zfsvfs_create(name, zfvp);
11219396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
112212620SMark.Shellenbaum@Oracle.COM 		rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
112312620SMark.Shellenbaum@Oracle.COM 		    RW_READER, tag);
112411185SSean.McEnroe@Sun.COM 		if ((*zfvp)->z_unmounted) {
11259396SMatthew.Ahrens@Sun.COM 			/*
11269396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
11279396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
11289396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
11299396SMatthew.Ahrens@Sun.COM 			 */
113011185SSean.McEnroe@Sun.COM 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
11319396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
11329396SMatthew.Ahrens@Sun.COM 		}
11339396SMatthew.Ahrens@Sun.COM 	}
11349396SMatthew.Ahrens@Sun.COM 	return (error);
11359396SMatthew.Ahrens@Sun.COM }
11369396SMatthew.Ahrens@Sun.COM 
11379396SMatthew.Ahrens@Sun.COM static void
11389396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
11399396SMatthew.Ahrens@Sun.COM {
11409396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
11419396SMatthew.Ahrens@Sun.COM 
11429396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
11439396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
11449396SMatthew.Ahrens@Sun.COM 	} else {
114510298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
11469396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
11479396SMatthew.Ahrens@Sun.COM 	}
11489396SMatthew.Ahrens@Sun.COM }
11499396SMatthew.Ahrens@Sun.COM 
11509396SMatthew.Ahrens@Sun.COM static int
1151789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1152789Sahrens {
1153789Sahrens 	int error;
11545094Slling 	nvlist_t *config, *props = NULL;
11557184Stimh 	nvlist_t *rootprops = NULL;
11567184Stimh 	nvlist_t *zplprops = NULL;
11574715Sek110237 	char *buf;
1158789Sahrens 
11595094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
11609643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
11614988Sek110237 		return (error);
11624715Sek110237 
11635094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
11649643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
11659643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
11665094Slling 		nvlist_free(config);
11675094Slling 		return (error);
11685094Slling 	}
11695094Slling 
11707184Stimh 	if (props) {
11717184Stimh 		nvlist_t *nvl = NULL;
11727184Stimh 		uint64_t version = SPA_VERSION;
11737184Stimh 
11747184Stimh 		(void) nvlist_lookup_uint64(props,
11757184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
11767184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
11777184Stimh 			error = EINVAL;
11787184Stimh 			goto pool_props_bad;
11797184Stimh 		}
11807184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
11817184Stimh 		if (nvl) {
11827184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
11837184Stimh 			if (error != 0) {
11847184Stimh 				nvlist_free(config);
11857184Stimh 				nvlist_free(props);
11867184Stimh 				return (error);
11877184Stimh 			}
11887184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
11897184Stimh 		}
11907184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11917184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
11927184Stimh 		    zplprops, NULL);
11937184Stimh 		if (error)
11947184Stimh 			goto pool_props_bad;
11957184Stimh 	}
11967184Stimh 
11974988Sek110237 	buf = history_str_get(zc);
1198789Sahrens 
11997184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
12007184Stimh 
12017184Stimh 	/*
12027184Stimh 	 * Set the remaining root properties
12037184Stimh 	 */
120411022STom.Erickson@Sun.COM 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
120511022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
12067184Stimh 		(void) spa_destroy(zc->zc_name);
1207789Sahrens 
12084988Sek110237 	if (buf != NULL)
12094988Sek110237 		history_str_free(buf);
12105094Slling 
12117184Stimh pool_props_bad:
12127184Stimh 	nvlist_free(rootprops);
12137184Stimh 	nvlist_free(zplprops);
1214789Sahrens 	nvlist_free(config);
12157184Stimh 	nvlist_free(props);
12165094Slling 
1217789Sahrens 	return (error);
1218789Sahrens }
1219789Sahrens 
1220789Sahrens static int
1221789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1222789Sahrens {
12234543Smarks 	int error;
12244543Smarks 	zfs_log_history(zc);
12254543Smarks 	error = spa_destroy(zc->zc_name);
122610588SEric.Taylor@Sun.COM 	if (error == 0)
122710588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
12284543Smarks 	return (error);
1229789Sahrens }
1230789Sahrens 
1231789Sahrens static int
1232789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1233789Sahrens {
12345094Slling 	nvlist_t *config, *props = NULL;
1235789Sahrens 	uint64_t guid;
123610921STim.Haley@Sun.COM 	int error;
1237789Sahrens 
12385094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12399643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
1240789Sahrens 		return (error);
1241789Sahrens 
12425094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
12439643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
12449643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
12455094Slling 		nvlist_free(config);
12465094Slling 		return (error);
12475094Slling 	}
12485094Slling 
1249789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
12501544Seschrock 	    guid != zc->zc_guid)
1251789Sahrens 		error = EINVAL;
1252789Sahrens 	else
125312949SGeorge.Wilson@Sun.COM 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
125412949SGeorge.Wilson@Sun.COM 
125512949SGeorge.Wilson@Sun.COM 	if (zc->zc_nvlist_dst != 0) {
125612949SGeorge.Wilson@Sun.COM 		int err;
125712949SGeorge.Wilson@Sun.COM 
125812949SGeorge.Wilson@Sun.COM 		if ((err = put_nvlist(zc, config)) != 0)
125912949SGeorge.Wilson@Sun.COM 			error = err;
126012949SGeorge.Wilson@Sun.COM 	}
126110921STim.Haley@Sun.COM 
1262789Sahrens 	nvlist_free(config);
1263789Sahrens 
12645094Slling 	if (props)
12655094Slling 		nvlist_free(props);
12665094Slling 
1267789Sahrens 	return (error);
1268789Sahrens }
1269789Sahrens 
1270789Sahrens static int
1271789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1272789Sahrens {
12734543Smarks 	int error;
12747214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
12758211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
12767214Slling 
12774543Smarks 	zfs_log_history(zc);
12788211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
127910588SEric.Taylor@Sun.COM 	if (error == 0)
128010588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
12814543Smarks 	return (error);
1282789Sahrens }
1283789Sahrens 
1284789Sahrens static int
1285789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1286789Sahrens {
1287789Sahrens 	nvlist_t *configs;
1288789Sahrens 	int error;
1289789Sahrens 
1290789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1291789Sahrens 		return (EEXIST);
1292789Sahrens 
12932676Seschrock 	error = put_nvlist(zc, configs);
1294789Sahrens 
1295789Sahrens 	nvlist_free(configs);
1296789Sahrens 
1297789Sahrens 	return (error);
1298789Sahrens }
1299789Sahrens 
1300789Sahrens static int
1301789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1302789Sahrens {
1303789Sahrens 	nvlist_t *config;
1304789Sahrens 	int error;
13051544Seschrock 	int ret = 0;
1306789Sahrens 
13072676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
13082676Seschrock 	    sizeof (zc->zc_value));
1309789Sahrens 
1310789Sahrens 	if (config != NULL) {
13112676Seschrock 		ret = put_nvlist(zc, config);
1312789Sahrens 		nvlist_free(config);
13131544Seschrock 
13141544Seschrock 		/*
13151544Seschrock 		 * The config may be present even if 'error' is non-zero.
13161544Seschrock 		 * In this case we return success, and preserve the real errno
13171544Seschrock 		 * in 'zc_cookie'.
13181544Seschrock 		 */
13191544Seschrock 		zc->zc_cookie = error;
1320789Sahrens 	} else {
13211544Seschrock 		ret = error;
1322789Sahrens 	}
1323789Sahrens 
13241544Seschrock 	return (ret);
1325789Sahrens }
1326789Sahrens 
1327789Sahrens /*
1328789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1329789Sahrens  * user land knows which devices are available and overall pool health.
1330789Sahrens  */
1331789Sahrens static int
1332789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1333789Sahrens {
1334789Sahrens 	nvlist_t *tryconfig, *config;
1335789Sahrens 	int error;
1336789Sahrens 
13375094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13389643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1339789Sahrens 		return (error);
1340789Sahrens 
1341789Sahrens 	config = spa_tryimport(tryconfig);
1342789Sahrens 
1343789Sahrens 	nvlist_free(tryconfig);
1344789Sahrens 
1345789Sahrens 	if (config == NULL)
1346789Sahrens 		return (EINVAL);
1347789Sahrens 
13482676Seschrock 	error = put_nvlist(zc, config);
1349789Sahrens 	nvlist_free(config);
1350789Sahrens 
1351789Sahrens 	return (error);
1352789Sahrens }
1353789Sahrens 
135412296SLin.Ling@Sun.COM /*
135512296SLin.Ling@Sun.COM  * inputs:
135612296SLin.Ling@Sun.COM  * zc_name              name of the pool
135712296SLin.Ling@Sun.COM  * zc_cookie            scan func (pool_scan_func_t)
135812296SLin.Ling@Sun.COM  */
1359789Sahrens static int
136012296SLin.Ling@Sun.COM zfs_ioc_pool_scan(zfs_cmd_t *zc)
1361789Sahrens {
1362789Sahrens 	spa_t *spa;
1363789Sahrens 	int error;
1364789Sahrens 
13652926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
13662926Sek110237 		return (error);
13672926Sek110237 
136812296SLin.Ling@Sun.COM 	if (zc->zc_cookie == POOL_SCAN_NONE)
136912296SLin.Ling@Sun.COM 		error = spa_scan_stop(spa);
137012296SLin.Ling@Sun.COM 	else
137112296SLin.Ling@Sun.COM 		error = spa_scan(spa, zc->zc_cookie);
13722926Sek110237 
13732926Sek110237 	spa_close(spa, FTAG);
13742926Sek110237 
1375789Sahrens 	return (error);
1376789Sahrens }
1377789Sahrens 
1378789Sahrens static int
1379789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1380789Sahrens {
1381789Sahrens 	spa_t *spa;
1382789Sahrens 	int error;
1383789Sahrens 
1384789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1385789Sahrens 	if (error == 0) {
1386789Sahrens 		spa_freeze(spa);
1387789Sahrens 		spa_close(spa, FTAG);
1388789Sahrens 	}
1389789Sahrens 	return (error);
1390789Sahrens }
1391789Sahrens 
1392789Sahrens static int
13931760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
13941760Seschrock {
13951760Seschrock 	spa_t *spa;
13961760Seschrock 	int error;
13971760Seschrock 
13982926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
13992926Sek110237 		return (error);
14002926Sek110237 
14015118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
14025118Slling 		spa_close(spa, FTAG);
14035118Slling 		return (EINVAL);
14045118Slling 	}
14055118Slling 
14065094Slling 	spa_upgrade(spa, zc->zc_cookie);
14072926Sek110237 	spa_close(spa, FTAG);
14082926Sek110237 
14092926Sek110237 	return (error);
14102926Sek110237 }
14112926Sek110237 
14122926Sek110237 static int
14132926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
14142926Sek110237 {
14152926Sek110237 	spa_t *spa;
14162926Sek110237 	char *hist_buf;
14172926Sek110237 	uint64_t size;
14182926Sek110237 	int error;
14192926Sek110237 
14202926Sek110237 	if ((size = zc->zc_history_len) == 0)
14212926Sek110237 		return (EINVAL);
14222926Sek110237 
14232926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
14242926Sek110237 		return (error);
14252926Sek110237 
14264577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
14273863Sek110237 		spa_close(spa, FTAG);
14283863Sek110237 		return (ENOTSUP);
14293863Sek110237 	}
14303863Sek110237 
14312926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
14322926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
14332926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
14349643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
14359643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
14369643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
14372926Sek110237 	}
14382926Sek110237 
14392926Sek110237 	spa_close(spa, FTAG);
14402926Sek110237 	kmem_free(hist_buf, size);
14412926Sek110237 	return (error);
14422926Sek110237 }
14432926Sek110237 
14442926Sek110237 static int
14453444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
14463444Sek110237 {
14473444Sek110237 	int error;
14483444Sek110237 
14493912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
14503444Sek110237 		return (error);
14513444Sek110237 
14523444Sek110237 	return (0);
14533444Sek110237 }
14543444Sek110237 
145510298SMatthew.Ahrens@Sun.COM /*
145610298SMatthew.Ahrens@Sun.COM  * inputs:
145710298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
145810298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
145910298SMatthew.Ahrens@Sun.COM  *
146010298SMatthew.Ahrens@Sun.COM  * outputs:
146110298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
146210298SMatthew.Ahrens@Sun.COM  */
14633444Sek110237 static int
14643444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
14653444Sek110237 {
146610298SMatthew.Ahrens@Sun.COM 	objset_t *os;
14673444Sek110237 	int error;
14683444Sek110237 
146910298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
147010298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
14713444Sek110237 		return (error);
147210298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
147310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
147410298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
147510298SMatthew.Ahrens@Sun.COM 	}
147610298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
14773444Sek110237 	    sizeof (zc->zc_value));
147810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
14793444Sek110237 
14803444Sek110237 	return (error);
14813444Sek110237 }
14823444Sek110237 
1483*13043STim.Haley@Sun.COM /*
1484*13043STim.Haley@Sun.COM  * inputs:
1485*13043STim.Haley@Sun.COM  * zc_name		name of filesystem
1486*13043STim.Haley@Sun.COM  * zc_obj		object to find
1487*13043STim.Haley@Sun.COM  *
1488*13043STim.Haley@Sun.COM  * outputs:
1489*13043STim.Haley@Sun.COM  * zc_stat		stats on object
1490*13043STim.Haley@Sun.COM  * zc_value		path to object
1491*13043STim.Haley@Sun.COM  */
1492*13043STim.Haley@Sun.COM static int
1493*13043STim.Haley@Sun.COM zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1494*13043STim.Haley@Sun.COM {
1495*13043STim.Haley@Sun.COM 	objset_t *os;
1496*13043STim.Haley@Sun.COM 	int error;
1497*13043STim.Haley@Sun.COM 
1498*13043STim.Haley@Sun.COM 	/* XXX reading from objset not owned */
1499*13043STim.Haley@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1500*13043STim.Haley@Sun.COM 		return (error);
1501*13043STim.Haley@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1502*13043STim.Haley@Sun.COM 		dmu_objset_rele(os, FTAG);
1503*13043STim.Haley@Sun.COM 		return (EINVAL);
1504*13043STim.Haley@Sun.COM 	}
1505*13043STim.Haley@Sun.COM 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1506*13043STim.Haley@Sun.COM 	    sizeof (zc->zc_value));
1507*13043STim.Haley@Sun.COM 	dmu_objset_rele(os, FTAG);
1508*13043STim.Haley@Sun.COM 
1509*13043STim.Haley@Sun.COM 	return (error);
1510*13043STim.Haley@Sun.COM }
1511*13043STim.Haley@Sun.COM 
15123444Sek110237 static int
1513789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1514789Sahrens {
1515789Sahrens 	spa_t *spa;
1516789Sahrens 	int error;
15176423Sgw25295 	nvlist_t *config, **l2cache, **spares;
15186423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1519789Sahrens 
1520789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1521789Sahrens 	if (error != 0)
1522789Sahrens 		return (error);
1523789Sahrens 
15245450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
15259643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
15265450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
15275450Sbrendan 	    &l2cache, &nl2cache);
15285450Sbrendan 
15296423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
15306423Sgw25295 	    &spares, &nspares);
15316423Sgw25295 
15323912Slling 	/*
15333912Slling 	 * A root pool with concatenated devices is not supported.
15346423Sgw25295 	 * Thus, can not add a device to a root pool.
15356423Sgw25295 	 *
15366423Sgw25295 	 * Intent log device can not be added to a rootpool because
15376423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
15386423Sgw25295 	 * can not be accessed during the mountroot time.
15396423Sgw25295 	 *
15406423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
15413912Slling 	 */
154210922SJeff.Bonwick@Sun.COM 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
154311422SMark.Musante@Sun.COM 		nvlist_free(config);
15443912Slling 		spa_close(spa, FTAG);
15453912Slling 		return (EDOM);
15463912Slling 	}
15473912Slling 
15485450Sbrendan 	if (error == 0) {
1549789Sahrens 		error = spa_vdev_add(spa, config);
1550789Sahrens 		nvlist_free(config);
1551789Sahrens 	}
1552789Sahrens 	spa_close(spa, FTAG);
1553789Sahrens 	return (error);
1554789Sahrens }
1555789Sahrens 
155612296SLin.Ling@Sun.COM /*
155712296SLin.Ling@Sun.COM  * inputs:
155812296SLin.Ling@Sun.COM  * zc_name		name of the pool
155912296SLin.Ling@Sun.COM  * zc_nvlist_conf	nvlist of devices to remove
156012296SLin.Ling@Sun.COM  * zc_cookie		to stop the remove?
156112296SLin.Ling@Sun.COM  */
1562789Sahrens static int
1563789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1564789Sahrens {
15652082Seschrock 	spa_t *spa;
15662082Seschrock 	int error;
15672082Seschrock 
15682082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
15692082Seschrock 	if (error != 0)
15702082Seschrock 		return (error);
15712082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
15722082Seschrock 	spa_close(spa, FTAG);
15732082Seschrock 	return (error);
1574789Sahrens }
1575789Sahrens 
1576789Sahrens static int
15774451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1578789Sahrens {
1579789Sahrens 	spa_t *spa;
1580789Sahrens 	int error;
15814451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1582789Sahrens 
15832926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1584789Sahrens 		return (error);
15854451Seschrock 	switch (zc->zc_cookie) {
15864451Seschrock 	case VDEV_STATE_ONLINE:
15874451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
15884451Seschrock 		break;
15894451Seschrock 
15904451Seschrock 	case VDEV_STATE_OFFLINE:
15914451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
15924451Seschrock 		break;
1593789Sahrens 
15944451Seschrock 	case VDEV_STATE_FAULTED:
159510817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
159610817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
159710817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
159810817SEric.Schrock@Sun.COM 
159910817SEric.Schrock@Sun.COM 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
16004451Seschrock 		break;
1601789Sahrens 
16024451Seschrock 	case VDEV_STATE_DEGRADED:
160310817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
160410817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
160510817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
160610817SEric.Schrock@Sun.COM 
160710817SEric.Schrock@Sun.COM 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
16084451Seschrock 		break;
16094451Seschrock 
16104451Seschrock 	default:
16114451Seschrock 		error = EINVAL;
16124451Seschrock 	}
16134451Seschrock 	zc->zc_cookie = newstate;
1614789Sahrens 	spa_close(spa, FTAG);
1615789Sahrens 	return (error);
1616789Sahrens }
1617789Sahrens 
1618789Sahrens static int
1619789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1620789Sahrens {
1621789Sahrens 	spa_t *spa;
1622789Sahrens 	int replacing = zc->zc_cookie;
1623789Sahrens 	nvlist_t *config;
1624789Sahrens 	int error;
1625789Sahrens 
16262926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1627789Sahrens 		return (error);
1628789Sahrens 
16295094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
16309643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
16311544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1632789Sahrens 		nvlist_free(config);
1633789Sahrens 	}
1634789Sahrens 
1635789Sahrens 	spa_close(spa, FTAG);
1636789Sahrens 	return (error);
1637789Sahrens }
1638789Sahrens 
1639789Sahrens static int
1640789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1641789Sahrens {
1642789Sahrens 	spa_t *spa;
1643789Sahrens 	int error;
1644789Sahrens 
16452926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1646789Sahrens 		return (error);
1647789Sahrens 
16488241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1649789Sahrens 
1650789Sahrens 	spa_close(spa, FTAG);
1651789Sahrens 	return (error);
1652789Sahrens }
1653789Sahrens 
1654789Sahrens static int
165511422SMark.Musante@Sun.COM zfs_ioc_vdev_split(zfs_cmd_t *zc)
165611422SMark.Musante@Sun.COM {
165711422SMark.Musante@Sun.COM 	spa_t *spa;
165811422SMark.Musante@Sun.COM 	nvlist_t *config, *props = NULL;
165911422SMark.Musante@Sun.COM 	int error;
166011422SMark.Musante@Sun.COM 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
166111422SMark.Musante@Sun.COM 
166211422SMark.Musante@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
166311422SMark.Musante@Sun.COM 		return (error);
166411422SMark.Musante@Sun.COM 
166511422SMark.Musante@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
166611422SMark.Musante@Sun.COM 	    zc->zc_iflags, &config)) {
166711422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
166811422SMark.Musante@Sun.COM 		return (error);
166911422SMark.Musante@Sun.COM 	}
167011422SMark.Musante@Sun.COM 
167111422SMark.Musante@Sun.COM 	if (zc->zc_nvlist_src_size != 0 && (error =
167211422SMark.Musante@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
167311422SMark.Musante@Sun.COM 	    zc->zc_iflags, &props))) {
167411422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
167511422SMark.Musante@Sun.COM 		nvlist_free(config);
167611422SMark.Musante@Sun.COM 		return (error);
167711422SMark.Musante@Sun.COM 	}
167811422SMark.Musante@Sun.COM 
167911422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
168011422SMark.Musante@Sun.COM 
168111422SMark.Musante@Sun.COM 	spa_close(spa, FTAG);
168211422SMark.Musante@Sun.COM 
168311422SMark.Musante@Sun.COM 	nvlist_free(config);
168411422SMark.Musante@Sun.COM 	nvlist_free(props);
168511422SMark.Musante@Sun.COM 
168611422SMark.Musante@Sun.COM 	return (error);
168711422SMark.Musante@Sun.COM }
168811422SMark.Musante@Sun.COM 
168911422SMark.Musante@Sun.COM static int
16901354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
16911354Seschrock {
16921354Seschrock 	spa_t *spa;
16932676Seschrock 	char *path = zc->zc_value;
16941544Seschrock 	uint64_t guid = zc->zc_guid;
16951354Seschrock 	int error;
16961354Seschrock 
16971354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
16981354Seschrock 	if (error != 0)
16991354Seschrock 		return (error);
17001354Seschrock 
17011354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
17021354Seschrock 	spa_close(spa, FTAG);
17031354Seschrock 	return (error);
17041354Seschrock }
17051354Seschrock 
17069425SEric.Schrock@Sun.COM static int
17079425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
17089425SEric.Schrock@Sun.COM {
17099425SEric.Schrock@Sun.COM 	spa_t *spa;
17109425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
17119425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
17129425SEric.Schrock@Sun.COM 	int error;
17139425SEric.Schrock@Sun.COM 
17149425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
17159425SEric.Schrock@Sun.COM 	if (error != 0)
17169425SEric.Schrock@Sun.COM 		return (error);
17179425SEric.Schrock@Sun.COM 
17189425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
17199425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
17209425SEric.Schrock@Sun.COM 	return (error);
17219425SEric.Schrock@Sun.COM }
17229425SEric.Schrock@Sun.COM 
17231354Seschrock static int
172412786SChris.Kirby@oracle.com zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1725789Sahrens {
172612786SChris.Kirby@oracle.com 	int error = 0;
17271356Seschrock 	nvlist_t *nv;
1728789Sahrens 
17292885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1730789Sahrens 
17312856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
173211022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
17332885Sahrens 		dmu_objset_stats(os, nv);
17343087Sahrens 		/*
17355147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
17363087Sahrens 		 * which we aren't supposed to do with a
17376689Smaybee 		 * DS_MODE_USER hold, because it could be
17383087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
173910298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
17403087Sahrens 		 */
17414577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
17424577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
17434577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
17444577Sahrens 		}
17452676Seschrock 		error = put_nvlist(zc, nv);
17461356Seschrock 		nvlist_free(nv);
17471356Seschrock 	}
1748789Sahrens 
174912786SChris.Kirby@oracle.com 	return (error);
175012786SChris.Kirby@oracle.com }
175112786SChris.Kirby@oracle.com 
175212786SChris.Kirby@oracle.com /*
175312786SChris.Kirby@oracle.com  * inputs:
175412786SChris.Kirby@oracle.com  * zc_name		name of filesystem
175512786SChris.Kirby@oracle.com  * zc_nvlist_dst_size	size of buffer for property nvlist
175612786SChris.Kirby@oracle.com  *
175712786SChris.Kirby@oracle.com  * outputs:
175812786SChris.Kirby@oracle.com  * zc_objset_stats	stats
175912786SChris.Kirby@oracle.com  * zc_nvlist_dst	property nvlist
176012786SChris.Kirby@oracle.com  * zc_nvlist_dst_size	size of property nvlist
176112786SChris.Kirby@oracle.com  */
176212786SChris.Kirby@oracle.com static int
176312786SChris.Kirby@oracle.com zfs_ioc_objset_stats(zfs_cmd_t *zc)
176412786SChris.Kirby@oracle.com {
176512786SChris.Kirby@oracle.com 	objset_t *os = NULL;
176612786SChris.Kirby@oracle.com 	int error;
176712786SChris.Kirby@oracle.com 
176812786SChris.Kirby@oracle.com 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
176912786SChris.Kirby@oracle.com 		return (error);
177012786SChris.Kirby@oracle.com 
177112786SChris.Kirby@oracle.com 	error = zfs_ioc_objset_stats_impl(zc, os);
177212786SChris.Kirby@oracle.com 
177310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
177412786SChris.Kirby@oracle.com 
1775789Sahrens 	return (error);
1776789Sahrens }
1777789Sahrens 
177811022STom.Erickson@Sun.COM /*
177911022STom.Erickson@Sun.COM  * inputs:
178011022STom.Erickson@Sun.COM  * zc_name		name of filesystem
178111022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of buffer for property nvlist
178211022STom.Erickson@Sun.COM  *
178311022STom.Erickson@Sun.COM  * outputs:
178411022STom.Erickson@Sun.COM  * zc_nvlist_dst	received property nvlist
178511022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of received property nvlist
178611022STom.Erickson@Sun.COM  *
178711022STom.Erickson@Sun.COM  * Gets received properties (distinct from local properties on or after
178811022STom.Erickson@Sun.COM  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
178911022STom.Erickson@Sun.COM  * local property values.
179011022STom.Erickson@Sun.COM  */
179111022STom.Erickson@Sun.COM static int
179211022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
179311022STom.Erickson@Sun.COM {
179411022STom.Erickson@Sun.COM 	objset_t *os = NULL;
179511022STom.Erickson@Sun.COM 	int error;
179611022STom.Erickson@Sun.COM 	nvlist_t *nv;
179711022STom.Erickson@Sun.COM 
179811022STom.Erickson@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
179911022STom.Erickson@Sun.COM 		return (error);
180011022STom.Erickson@Sun.COM 
180111022STom.Erickson@Sun.COM 	/*
180211022STom.Erickson@Sun.COM 	 * Without this check, we would return local property values if the
180311022STom.Erickson@Sun.COM 	 * caller has not already received properties on or after
180411022STom.Erickson@Sun.COM 	 * SPA_VERSION_RECVD_PROPS.
180511022STom.Erickson@Sun.COM 	 */
180611022STom.Erickson@Sun.COM 	if (!dsl_prop_get_hasrecvd(os)) {
180711022STom.Erickson@Sun.COM 		dmu_objset_rele(os, FTAG);
180811022STom.Erickson@Sun.COM 		return (ENOTSUP);
180911022STom.Erickson@Sun.COM 	}
181011022STom.Erickson@Sun.COM 
181111022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != 0 &&
181211022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_received(os, &nv)) == 0) {
181311022STom.Erickson@Sun.COM 		error = put_nvlist(zc, nv);
181411022STom.Erickson@Sun.COM 		nvlist_free(nv);
181511022STom.Erickson@Sun.COM 	}
181611022STom.Erickson@Sun.COM 
181711022STom.Erickson@Sun.COM 	dmu_objset_rele(os, FTAG);
181811022STom.Erickson@Sun.COM 	return (error);
181911022STom.Erickson@Sun.COM }
182011022STom.Erickson@Sun.COM 
18215498Stimh static int
18225498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
18235498Stimh {
18245498Stimh 	uint64_t value;
18255498Stimh 	int error;
18265498Stimh 
18275498Stimh 	/*
18285498Stimh 	 * zfs_get_zplprop() will either find a value or give us
18295498Stimh 	 * the default value (if there is one).
18305498Stimh 	 */
18315498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
18325498Stimh 		return (error);
18335498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
18345498Stimh 	return (0);
18355498Stimh }
18365498Stimh 
18375498Stimh /*
18385498Stimh  * inputs:
18395498Stimh  * zc_name		name of filesystem
18405498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
18415498Stimh  *
18425498Stimh  * outputs:
18435498Stimh  * zc_nvlist_dst	zpl property nvlist
18445498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
18455498Stimh  */
18465498Stimh static int
18475498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
18485498Stimh {
18495498Stimh 	objset_t *os;
18505498Stimh 	int err;
18515498Stimh 
185210298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
185310298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
18545498Stimh 		return (err);
18555498Stimh 
18565498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
18575498Stimh 
18585498Stimh 	/*
18595498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
18606689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
18616689Smaybee 	 * hold, because it could be inconsistent.
18625498Stimh 	 */
18635498Stimh 	if (zc->zc_nvlist_dst != NULL &&
18645498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
18655498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
18665498Stimh 		nvlist_t *nv;
18675498Stimh 
18685498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18695498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
18705498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
18715498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
18725498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
18735498Stimh 			err = put_nvlist(zc, nv);
18745498Stimh 		nvlist_free(nv);
18755498Stimh 	} else {
18765498Stimh 		err = ENOENT;
18775498Stimh 	}
187810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
18795498Stimh 	return (err);
18805498Stimh }
18815498Stimh 
18829396SMatthew.Ahrens@Sun.COM static boolean_t
18839396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
18849396SMatthew.Ahrens@Sun.COM {
18859396SMatthew.Ahrens@Sun.COM 	/*
18869396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
18879396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
18889396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
18899396SMatthew.Ahrens@Sun.COM 	 */
18909396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
18919396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
18929396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
18939396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
18949396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
18959396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
18969396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
18979396SMatthew.Ahrens@Sun.COM }
18989396SMatthew.Ahrens@Sun.COM 
18995367Sahrens /*
19005367Sahrens  * inputs:
19015367Sahrens  * zc_name		name of filesystem
19025367Sahrens  * zc_cookie		zap cursor
19035367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
19045367Sahrens  *
19055367Sahrens  * outputs:
19065367Sahrens  * zc_name		name of next filesystem
19079396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
19085367Sahrens  * zc_objset_stats	stats
19095367Sahrens  * zc_nvlist_dst	property nvlist
19105367Sahrens  * zc_nvlist_dst_size	size of property nvlist
19115367Sahrens  */
1912789Sahrens static int
1913789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1914789Sahrens {
1915885Sahrens 	objset_t *os;
1916789Sahrens 	int error;
1917789Sahrens 	char *p;
191811546SChris.Kirby@sun.com 	size_t orig_len = strlen(zc->zc_name);
191911546SChris.Kirby@sun.com 
192011546SChris.Kirby@sun.com top:
192110298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1922885Sahrens 		if (error == ENOENT)
1923885Sahrens 			error = ESRCH;
1924885Sahrens 		return (error);
1925789Sahrens 	}
1926789Sahrens 
1927789Sahrens 	p = strrchr(zc->zc_name, '/');
1928789Sahrens 	if (p == NULL || p[1] != '\0')
1929789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1930789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1931789Sahrens 
19328697SRichard.Morris@Sun.COM 	/*
19338697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
19348697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
19358697SRichard.Morris@Sun.COM 	 */
19368415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
19378415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
19388415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
19398415SRichard.Morris@Sun.COM 
19408415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
19418697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
19428415SRichard.Morris@Sun.COM 	}
19438415SRichard.Morris@Sun.COM 
1944789Sahrens 	do {
1945885Sahrens 		error = dmu_dir_list_next(os,
1946885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1947885Sahrens 		    NULL, &zc->zc_cookie);
1948789Sahrens 		if (error == ENOENT)
1949789Sahrens 			error = ESRCH;
195010588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
195110588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
195210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1953789Sahrens 
195410588SEric.Taylor@Sun.COM 	/*
195510588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
195610588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
195710588SEric.Taylor@Sun.COM 	 */
195811546SChris.Kirby@sun.com 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1959885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
196011546SChris.Kirby@sun.com 		if (error == ENOENT) {
196111546SChris.Kirby@sun.com 			/* We lost a race with destroy, get the next one. */
196211546SChris.Kirby@sun.com 			zc->zc_name[orig_len] = '\0';
196311546SChris.Kirby@sun.com 			goto top;
196411546SChris.Kirby@sun.com 		}
196511546SChris.Kirby@sun.com 	}
1966789Sahrens 	return (error);
1967789Sahrens }
1968789Sahrens 
19695367Sahrens /*
19705367Sahrens  * inputs:
19715367Sahrens  * zc_name		name of filesystem
19725367Sahrens  * zc_cookie		zap cursor
19735367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
19745367Sahrens  *
19755367Sahrens  * outputs:
19765367Sahrens  * zc_name		name of next snapshot
19775367Sahrens  * zc_objset_stats	stats
19785367Sahrens  * zc_nvlist_dst	property nvlist
19795367Sahrens  * zc_nvlist_dst_size	size of property nvlist
19805367Sahrens  */
1981789Sahrens static int
1982789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1983789Sahrens {
1984885Sahrens 	objset_t *os;
1985789Sahrens 	int error;
1986789Sahrens 
198711546SChris.Kirby@sun.com top:
198810474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
198910474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
199010474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
199110474SRichard.Morris@Sun.COM 
199210298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
19936689Smaybee 	if (error)
19946689Smaybee 		return (error == ENOENT ? ESRCH : error);
1995789Sahrens 
19961003Slling 	/*
19971003Slling 	 * A dataset name of maximum length cannot have any snapshots,
19981003Slling 	 * so exit immediately.
19991003Slling 	 */
20001003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
200110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
20021003Slling 		return (ESRCH);
2003789Sahrens 	}
2004789Sahrens 
2005885Sahrens 	error = dmu_snapshot_list_next(os,
2006885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
200712786SChris.Kirby@oracle.com 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
200812786SChris.Kirby@oracle.com 	    NULL);
200912786SChris.Kirby@oracle.com 
201011546SChris.Kirby@sun.com 	if (error == 0) {
201112786SChris.Kirby@oracle.com 		dsl_dataset_t *ds;
201212786SChris.Kirby@oracle.com 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
201312786SChris.Kirby@oracle.com 
201412786SChris.Kirby@oracle.com 		/*
201512786SChris.Kirby@oracle.com 		 * Since we probably don't have a hold on this snapshot,
201612786SChris.Kirby@oracle.com 		 * it's possible that the objsetid could have been destroyed
201712786SChris.Kirby@oracle.com 		 * and reused for a new objset. It's OK if this happens during
201812786SChris.Kirby@oracle.com 		 * a zfs send operation, since the new createtxg will be
201912786SChris.Kirby@oracle.com 		 * beyond the range we're interested in.
202012786SChris.Kirby@oracle.com 		 */
202112786SChris.Kirby@oracle.com 		rw_enter(&dp->dp_config_rwlock, RW_READER);
202212786SChris.Kirby@oracle.com 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
202312786SChris.Kirby@oracle.com 		rw_exit(&dp->dp_config_rwlock);
202412786SChris.Kirby@oracle.com 		if (error) {
202512786SChris.Kirby@oracle.com 			if (error == ENOENT) {
202612786SChris.Kirby@oracle.com 				/* Racing with destroy, get the next one. */
202712786SChris.Kirby@oracle.com 				*strchr(zc->zc_name, '@') = '\0';
202812786SChris.Kirby@oracle.com 				dmu_objset_rele(os, FTAG);
202912786SChris.Kirby@oracle.com 				goto top;
203012786SChris.Kirby@oracle.com 			}
203112786SChris.Kirby@oracle.com 		} else {
203212786SChris.Kirby@oracle.com 			objset_t *ossnap;
203312786SChris.Kirby@oracle.com 
203412786SChris.Kirby@oracle.com 			error = dmu_objset_from_ds(ds, &ossnap);
203512786SChris.Kirby@oracle.com 			if (error == 0)
203612786SChris.Kirby@oracle.com 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
203712786SChris.Kirby@oracle.com 			dsl_dataset_rele(ds, FTAG);
203811546SChris.Kirby@sun.com 		}
203911546SChris.Kirby@sun.com 	} else if (error == ENOENT) {
20406689Smaybee 		error = ESRCH;
204111546SChris.Kirby@sun.com 	}
2042789Sahrens 
204312786SChris.Kirby@oracle.com 	dmu_objset_rele(os, FTAG);
20445367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
20456689Smaybee 	if (error)
20465367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2047789Sahrens 	return (error);
2048789Sahrens }
2049789Sahrens 
205011022STom.Erickson@Sun.COM static int
205111022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
205211022STom.Erickson@Sun.COM {
205311022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
205411022STom.Erickson@Sun.COM 	uint64_t *valary;
205511022STom.Erickson@Sun.COM 	unsigned int vallen;
205611022STom.Erickson@Sun.COM 	const char *domain;
205711933STim.Haley@Sun.COM 	char *dash;
205811022STom.Erickson@Sun.COM 	zfs_userquota_prop_t type;
205911022STom.Erickson@Sun.COM 	uint64_t rid;
206011022STom.Erickson@Sun.COM 	uint64_t quota;
206111022STom.Erickson@Sun.COM 	zfsvfs_t *zfsvfs;
206211022STom.Erickson@Sun.COM 	int err;
206311022STom.Erickson@Sun.COM 
206411022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
206511022STom.Erickson@Sun.COM 		nvlist_t *attrs;
206611022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
206711933STim.Haley@Sun.COM 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
206811933STim.Haley@Sun.COM 		    &pair) != 0)
206911933STim.Haley@Sun.COM 			return (EINVAL);
207011022STom.Erickson@Sun.COM 	}
207111022STom.Erickson@Sun.COM 
207211933STim.Haley@Sun.COM 	/*
207311933STim.Haley@Sun.COM 	 * A correctly constructed propname is encoded as
207411933STim.Haley@Sun.COM 	 * userquota@<rid>-<domain>.
207511933STim.Haley@Sun.COM 	 */
207611933STim.Haley@Sun.COM 	if ((dash = strchr(propname, '-')) == NULL ||
207711933STim.Haley@Sun.COM 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
207811933STim.Haley@Sun.COM 	    vallen != 3)
207911933STim.Haley@Sun.COM 		return (EINVAL);
208011933STim.Haley@Sun.COM 
208111933STim.Haley@Sun.COM 	domain = dash + 1;
208211022STom.Erickson@Sun.COM 	type = valary[0];
208311022STom.Erickson@Sun.COM 	rid = valary[1];
208411022STom.Erickson@Sun.COM 	quota = valary[2];
208511022STom.Erickson@Sun.COM 
208612620SMark.Shellenbaum@Oracle.COM 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
208711022STom.Erickson@Sun.COM 	if (err == 0) {
208811022STom.Erickson@Sun.COM 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
208911022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
209011022STom.Erickson@Sun.COM 	}
209111022STom.Erickson@Sun.COM 
209211022STom.Erickson@Sun.COM 	return (err);
209311022STom.Erickson@Sun.COM }
209411022STom.Erickson@Sun.COM 
209511022STom.Erickson@Sun.COM /*
209611022STom.Erickson@Sun.COM  * If the named property is one that has a special function to set its value,
209711022STom.Erickson@Sun.COM  * return 0 on success and a positive error code on failure; otherwise if it is
209811022STom.Erickson@Sun.COM  * not one of the special properties handled by this function, return -1.
209911022STom.Erickson@Sun.COM  *
210011933STim.Haley@Sun.COM  * XXX: It would be better for callers of the property interface if we handled
210111022STom.Erickson@Sun.COM  * these special cases in dsl_prop.c (in the dsl layer).
210211022STom.Erickson@Sun.COM  */
210311022STom.Erickson@Sun.COM static int
210411022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source,
210511022STom.Erickson@Sun.COM     nvpair_t *pair)
2106789Sahrens {
210711022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
210811022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
210911022STom.Erickson@Sun.COM 	uint64_t intval;
211011022STom.Erickson@Sun.COM 	int err;
211111022STom.Erickson@Sun.COM 
211211022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
211311022STom.Erickson@Sun.COM 		if (zfs_prop_userquota(propname))
211411022STom.Erickson@Sun.COM 			return (zfs_prop_set_userquota(dsname, pair));
211511022STom.Erickson@Sun.COM 		return (-1);
211611022STom.Erickson@Sun.COM 	}
211711022STom.Erickson@Sun.COM 
211811022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
211911022STom.Erickson@Sun.COM 		nvlist_t *attrs;
212011022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
212111022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
212211022STom.Erickson@Sun.COM 		    &pair) == 0);
212311022STom.Erickson@Sun.COM 	}
212411022STom.Erickson@Sun.COM 
212511022STom.Erickson@Sun.COM 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
212611022STom.Erickson@Sun.COM 		return (-1);
212711022STom.Erickson@Sun.COM 
212811022STom.Erickson@Sun.COM 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
212911022STom.Erickson@Sun.COM 
213011022STom.Erickson@Sun.COM 	switch (prop) {
213111022STom.Erickson@Sun.COM 	case ZFS_PROP_QUOTA:
213211022STom.Erickson@Sun.COM 		err = dsl_dir_set_quota(dsname, source, intval);
213311022STom.Erickson@Sun.COM 		break;
213411022STom.Erickson@Sun.COM 	case ZFS_PROP_REFQUOTA:
213511022STom.Erickson@Sun.COM 		err = dsl_dataset_set_quota(dsname, source, intval);
213611022STom.Erickson@Sun.COM 		break;
213711022STom.Erickson@Sun.COM 	case ZFS_PROP_RESERVATION:
213811022STom.Erickson@Sun.COM 		err = dsl_dir_set_reservation(dsname, source, intval);
213911022STom.Erickson@Sun.COM 		break;
214011022STom.Erickson@Sun.COM 	case ZFS_PROP_REFRESERVATION:
214111022STom.Erickson@Sun.COM 		err = dsl_dataset_set_reservation(dsname, source, intval);
214211022STom.Erickson@Sun.COM 		break;
214311022STom.Erickson@Sun.COM 	case ZFS_PROP_VOLSIZE:
214411022STom.Erickson@Sun.COM 		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
214511022STom.Erickson@Sun.COM 		    intval);
214611022STom.Erickson@Sun.COM 		break;
214711022STom.Erickson@Sun.COM 	case ZFS_PROP_VERSION:
214811022STom.Erickson@Sun.COM 	{
214911022STom.Erickson@Sun.COM 		zfsvfs_t *zfsvfs;
215011022STom.Erickson@Sun.COM 
215112620SMark.Shellenbaum@Oracle.COM 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
215211022STom.Erickson@Sun.COM 			break;
215311022STom.Erickson@Sun.COM 
215411022STom.Erickson@Sun.COM 		err = zfs_set_version(zfsvfs, intval);
215511022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
215611022STom.Erickson@Sun.COM 
215711022STom.Erickson@Sun.COM 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
215811147SGeorge.Wilson@Sun.COM 			zfs_cmd_t *zc;
215911147SGeorge.Wilson@Sun.COM 
216011147SGeorge.Wilson@Sun.COM 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
216111147SGeorge.Wilson@Sun.COM 			(void) strcpy(zc->zc_name, dsname);
216211147SGeorge.Wilson@Sun.COM 			(void) zfs_ioc_userspace_upgrade(zc);
216311147SGeorge.Wilson@Sun.COM 			kmem_free(zc, sizeof (zfs_cmd_t));
216411022STom.Erickson@Sun.COM 		}
216511022STom.Erickson@Sun.COM 		break;
216611022STom.Erickson@Sun.COM 	}
216711022STom.Erickson@Sun.COM 
216811022STom.Erickson@Sun.COM 	default:
216911022STom.Erickson@Sun.COM 		err = -1;
217011022STom.Erickson@Sun.COM 	}
217111022STom.Erickson@Sun.COM 
217211022STom.Erickson@Sun.COM 	return (err);
217311022STom.Erickson@Sun.COM }
217411022STom.Erickson@Sun.COM 
217511022STom.Erickson@Sun.COM /*
217611022STom.Erickson@Sun.COM  * This function is best effort. If it fails to set any of the given properties,
217711022STom.Erickson@Sun.COM  * it continues to set as many as it can and returns the first error
217811022STom.Erickson@Sun.COM  * encountered. If the caller provides a non-NULL errlist, it also gives the
217911022STom.Erickson@Sun.COM  * complete list of names of all the properties it failed to set along with the
218011022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
218111022STom.Erickson@Sun.COM  * returned errlist.
218211022STom.Erickson@Sun.COM  *
218311022STom.Erickson@Sun.COM  * If every property is set successfully, zero is returned and the list pointed
218411022STom.Erickson@Sun.COM  * at by errlist is NULL.
218511022STom.Erickson@Sun.COM  */
218611022STom.Erickson@Sun.COM int
218711022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
218811022STom.Erickson@Sun.COM     nvlist_t **errlist)
218911022STom.Erickson@Sun.COM {
219011022STom.Erickson@Sun.COM 	nvpair_t *pair;
219111022STom.Erickson@Sun.COM 	nvpair_t *propval;
219211045STom.Erickson@Sun.COM 	int rv = 0;
21932676Seschrock 	uint64_t intval;
21942676Seschrock 	char *strval;
21958697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
219611022STom.Erickson@Sun.COM 	nvlist_t *errors;
219711022STom.Erickson@Sun.COM 	nvlist_t *retrynvl;
21984543Smarks 
21998697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
220011022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
220111022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
220211022STom.Erickson@Sun.COM 
220311022STom.Erickson@Sun.COM retry:
220411022STom.Erickson@Sun.COM 	pair = NULL;
220511022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
220611022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
22074670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
220811181STom.Erickson@Sun.COM 		int err = 0;
22094543Smarks 
221011022STom.Erickson@Sun.COM 		/* decode the property value */
221111022STom.Erickson@Sun.COM 		propval = pair;
221211022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
221311022STom.Erickson@Sun.COM 			nvlist_t *attrs;
221411022STom.Erickson@Sun.COM 			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
221511933STim.Haley@Sun.COM 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
221611933STim.Haley@Sun.COM 			    &propval) != 0)
221711933STim.Haley@Sun.COM 				err = EINVAL;
22184543Smarks 		}
22192676Seschrock 
222011022STom.Erickson@Sun.COM 		/* Validate value type */
222111933STim.Haley@Sun.COM 		if (err == 0 && prop == ZPROP_INVAL) {
222211022STom.Erickson@Sun.COM 			if (zfs_prop_user(propname)) {
222311022STom.Erickson@Sun.COM 				if (nvpair_type(propval) != DATA_TYPE_STRING)
222411022STom.Erickson@Sun.COM 					err = EINVAL;
222511022STom.Erickson@Sun.COM 			} else if (zfs_prop_userquota(propname)) {
222611022STom.Erickson@Sun.COM 				if (nvpair_type(propval) !=
222711022STom.Erickson@Sun.COM 				    DATA_TYPE_UINT64_ARRAY)
222811022STom.Erickson@Sun.COM 					err = EINVAL;
22299396SMatthew.Ahrens@Sun.COM 			}
223011933STim.Haley@Sun.COM 		} else if (err == 0) {
223111022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
223211022STom.Erickson@Sun.COM 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
223311022STom.Erickson@Sun.COM 					err = EINVAL;
223411022STom.Erickson@Sun.COM 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
22352885Sahrens 				const char *unused;
22362885Sahrens 
223711022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
223811022STom.Erickson@Sun.COM 				    &intval) == 0);
22392676Seschrock 
22402676Seschrock 				switch (zfs_prop_get_type(prop)) {
22414787Sahrens 				case PROP_TYPE_NUMBER:
22422676Seschrock 					break;
22434787Sahrens 				case PROP_TYPE_STRING:
224411022STom.Erickson@Sun.COM 					err = EINVAL;
224511022STom.Erickson@Sun.COM 					break;
22464787Sahrens 				case PROP_TYPE_INDEX:
22472717Seschrock 					if (zfs_prop_index_to_string(prop,
224811022STom.Erickson@Sun.COM 					    intval, &unused) != 0)
224911022STom.Erickson@Sun.COM 						err = EINVAL;
22502676Seschrock 					break;
22512676Seschrock 				default:
22524577Sahrens 					cmn_err(CE_PANIC,
22534577Sahrens 					    "unknown property type");
22542676Seschrock 				}
22552676Seschrock 			} else {
225611022STom.Erickson@Sun.COM 				err = EINVAL;
225711022STom.Erickson@Sun.COM 			}
225811022STom.Erickson@Sun.COM 		}
225911022STom.Erickson@Sun.COM 
226011022STom.Erickson@Sun.COM 		/* Validate permissions */
226111022STom.Erickson@Sun.COM 		if (err == 0)
226211022STom.Erickson@Sun.COM 			err = zfs_check_settable(dsname, pair, CRED());
226311022STom.Erickson@Sun.COM 
226411022STom.Erickson@Sun.COM 		if (err == 0) {
226511022STom.Erickson@Sun.COM 			err = zfs_prop_set_special(dsname, source, pair);
226611022STom.Erickson@Sun.COM 			if (err == -1) {
226711022STom.Erickson@Sun.COM 				/*
226811022STom.Erickson@Sun.COM 				 * For better performance we build up a list of
226911022STom.Erickson@Sun.COM 				 * properties to set in a single transaction.
227011022STom.Erickson@Sun.COM 				 */
227111022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(genericnvl, pair);
227211022STom.Erickson@Sun.COM 			} else if (err != 0 && nvl != retrynvl) {
227311022STom.Erickson@Sun.COM 				/*
227411022STom.Erickson@Sun.COM 				 * This may be a spurious error caused by
227511022STom.Erickson@Sun.COM 				 * receiving quota and reservation out of order.
227611022STom.Erickson@Sun.COM 				 * Try again in a second pass.
227711022STom.Erickson@Sun.COM 				 */
227811022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(retrynvl, pair);
22792676Seschrock 			}
228011022STom.Erickson@Sun.COM 		}
228111022STom.Erickson@Sun.COM 
228211022STom.Erickson@Sun.COM 		if (err != 0)
228311022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
228411022STom.Erickson@Sun.COM 	}
228511022STom.Erickson@Sun.COM 
228611022STom.Erickson@Sun.COM 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
228711022STom.Erickson@Sun.COM 		nvl = retrynvl;
228811022STom.Erickson@Sun.COM 		goto retry;
228911022STom.Erickson@Sun.COM 	}
229011022STom.Erickson@Sun.COM 
229111022STom.Erickson@Sun.COM 	if (!nvlist_empty(genericnvl) &&
229211022STom.Erickson@Sun.COM 	    dsl_props_set(dsname, source, genericnvl) != 0) {
229311022STom.Erickson@Sun.COM 		/*
229411022STom.Erickson@Sun.COM 		 * If this fails, we still want to set as many properties as we
229511022STom.Erickson@Sun.COM 		 * can, so try setting them individually.
229611022STom.Erickson@Sun.COM 		 */
229711022STom.Erickson@Sun.COM 		pair = NULL;
229811022STom.Erickson@Sun.COM 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
229911022STom.Erickson@Sun.COM 			const char *propname = nvpair_name(pair);
230011181STom.Erickson@Sun.COM 			int err = 0;
230111022STom.Erickson@Sun.COM 
230211022STom.Erickson@Sun.COM 			propval = pair;
230311022STom.Erickson@Sun.COM 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
230411022STom.Erickson@Sun.COM 				nvlist_t *attrs;
230511022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
230611022STom.Erickson@Sun.COM 				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
230711022STom.Erickson@Sun.COM 				    &propval) == 0);
230811022STom.Erickson@Sun.COM 			}
230911022STom.Erickson@Sun.COM 
231011022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
231111022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_string(propval,
231211022STom.Erickson@Sun.COM 				    &strval) == 0);
231311022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 1,
231411022STom.Erickson@Sun.COM 				    strlen(strval) + 1, strval);
231511022STom.Erickson@Sun.COM 			} else {
231611022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
231711022STom.Erickson@Sun.COM 				    &intval) == 0);
231811022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 8,
231911022STom.Erickson@Sun.COM 				    1, &intval);
232011022STom.Erickson@Sun.COM 			}
232111022STom.Erickson@Sun.COM 
232211022STom.Erickson@Sun.COM 			if (err != 0) {
232311022STom.Erickson@Sun.COM 				VERIFY(nvlist_add_int32(errors, propname,
232411022STom.Erickson@Sun.COM 				    err) == 0);
232511022STom.Erickson@Sun.COM 			}
23262676Seschrock 		}
23272676Seschrock 	}
232811022STom.Erickson@Sun.COM 	nvlist_free(genericnvl);
232911022STom.Erickson@Sun.COM 	nvlist_free(retrynvl);
233011022STom.Erickson@Sun.COM 
233111022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
233211022STom.Erickson@Sun.COM 		nvlist_free(errors);
233311022STom.Erickson@Sun.COM 		errors = NULL;
233411022STom.Erickson@Sun.COM 	} else {
233511022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
23368697SRichard.Morris@Sun.COM 	}
233711022STom.Erickson@Sun.COM 
233811022STom.Erickson@Sun.COM 	if (errlist == NULL)
233911022STom.Erickson@Sun.COM 		nvlist_free(errors);
234011022STom.Erickson@Sun.COM 	else
234111022STom.Erickson@Sun.COM 		*errlist = errors;
234211022STom.Erickson@Sun.COM 
234311022STom.Erickson@Sun.COM 	return (rv);
2344789Sahrens }
2345789Sahrens 
23465367Sahrens /*
23479355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
23489355SMatthew.Ahrens@Sun.COM  */
23499355SMatthew.Ahrens@Sun.COM static int
23509355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
23519355SMatthew.Ahrens@Sun.COM {
235211022STom.Erickson@Sun.COM 	nvpair_t *pair = NULL;
23539355SMatthew.Ahrens@Sun.COM 	int error = 0;
23549355SMatthew.Ahrens@Sun.COM 
235511022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
235611022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
23579355SMatthew.Ahrens@Sun.COM 		char *valstr;
23589355SMatthew.Ahrens@Sun.COM 
23599355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
236011022STom.Erickson@Sun.COM 		    nvpair_type(pair) != DATA_TYPE_STRING)
23619355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
23629355SMatthew.Ahrens@Sun.COM 
23639355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
23649355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
23659355SMatthew.Ahrens@Sun.COM 			return (error);
23669355SMatthew.Ahrens@Sun.COM 
23679355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
23689355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
23699355SMatthew.Ahrens@Sun.COM 
237011022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
23719355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
23729355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
23739355SMatthew.Ahrens@Sun.COM 	}
23749355SMatthew.Ahrens@Sun.COM 	return (0);
23759355SMatthew.Ahrens@Sun.COM }
23769355SMatthew.Ahrens@Sun.COM 
237711022STom.Erickson@Sun.COM static void
237811022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
237911022STom.Erickson@Sun.COM {
238011022STom.Erickson@Sun.COM 	nvpair_t *pair;
238111022STom.Erickson@Sun.COM 
238211022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
238311022STom.Erickson@Sun.COM 
238411022STom.Erickson@Sun.COM 	pair = NULL;
238511022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
238611022STom.Erickson@Sun.COM 		if (nvlist_exists(skipped, nvpair_name(pair)))
238711022STom.Erickson@Sun.COM 			continue;
238811022STom.Erickson@Sun.COM 
238911022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
239011022STom.Erickson@Sun.COM 	}
239111022STom.Erickson@Sun.COM }
239211022STom.Erickson@Sun.COM 
239311022STom.Erickson@Sun.COM static int
239411022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
239511022STom.Erickson@Sun.COM     nvlist_t *skipped)
239611022STom.Erickson@Sun.COM {
239711022STom.Erickson@Sun.COM 	int err = 0;
239811022STom.Erickson@Sun.COM 	nvlist_t *cleared_props = NULL;
239911022STom.Erickson@Sun.COM 	props_skip(props, skipped, &cleared_props);
240011022STom.Erickson@Sun.COM 	if (!nvlist_empty(cleared_props)) {
240111022STom.Erickson@Sun.COM 		/*
240211022STom.Erickson@Sun.COM 		 * Acts on local properties until the dataset has received
240311022STom.Erickson@Sun.COM 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
240411022STom.Erickson@Sun.COM 		 */
240511022STom.Erickson@Sun.COM 		zprop_source_t flags = (ZPROP_SRC_NONE |
240611022STom.Erickson@Sun.COM 		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
240711022STom.Erickson@Sun.COM 		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
240811022STom.Erickson@Sun.COM 	}
240911022STom.Erickson@Sun.COM 	nvlist_free(cleared_props);
241011022STom.Erickson@Sun.COM 	return (err);
241111022STom.Erickson@Sun.COM }
241211022STom.Erickson@Sun.COM 
24139355SMatthew.Ahrens@Sun.COM /*
24145367Sahrens  * inputs:
24155367Sahrens  * zc_name		name of filesystem
24168697SRichard.Morris@Sun.COM  * zc_value		name of property to set
24175367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
241811022STom.Erickson@Sun.COM  * zc_cookie		received properties flag
24195367Sahrens  *
242011022STom.Erickson@Sun.COM  * outputs:
242111022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
24225367Sahrens  */
2423789Sahrens static int
24242676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2425789Sahrens {
24262676Seschrock 	nvlist_t *nvl;
242711022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
242811022STom.Erickson@Sun.COM 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
242911022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL);
243011022STom.Erickson@Sun.COM 	nvlist_t *errors = NULL;
24312676Seschrock 	int error;
2432789Sahrens 
24335094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24349643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
24352676Seschrock 		return (error);
24362676Seschrock 
243711022STom.Erickson@Sun.COM 	if (received) {
24387265Sahrens 		nvlist_t *origprops;
24397265Sahrens 		objset_t *os;
24407265Sahrens 
244110298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
244211022STom.Erickson@Sun.COM 			if (dsl_prop_get_received(os, &origprops) == 0) {
244311022STom.Erickson@Sun.COM 				(void) clear_received_props(os,
244411022STom.Erickson@Sun.COM 				    zc->zc_name, origprops, nvl);
24457265Sahrens 				nvlist_free(origprops);
24467265Sahrens 			}
244711022STom.Erickson@Sun.COM 
244811022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
244910298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
24507265Sahrens 		}
24517265Sahrens 	}
24527265Sahrens 
245311022STom.Erickson@Sun.COM 	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
245411022STom.Erickson@Sun.COM 
245511022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
245611022STom.Erickson@Sun.COM 		(void) put_nvlist(zc, errors);
245711022STom.Erickson@Sun.COM 	}
245811022STom.Erickson@Sun.COM 
245911022STom.Erickson@Sun.COM 	nvlist_free(errors);
24602676Seschrock 	nvlist_free(nvl);
24612676Seschrock 	return (error);
2462789Sahrens }
2463789Sahrens 
24645367Sahrens /*
24655367Sahrens  * inputs:
24665367Sahrens  * zc_name		name of filesystem
24675367Sahrens  * zc_value		name of property to inherit
246811022STom.Erickson@Sun.COM  * zc_cookie		revert to received value if TRUE
24695367Sahrens  *
24705367Sahrens  * outputs:		none
24715367Sahrens  */
2472789Sahrens static int
24734849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
24744849Sahrens {
247511022STom.Erickson@Sun.COM 	const char *propname = zc->zc_value;
247611022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
247711022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
247811022STom.Erickson@Sun.COM 	zprop_source_t source = (received
247911022STom.Erickson@Sun.COM 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
248011022STom.Erickson@Sun.COM 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
248111022STom.Erickson@Sun.COM 
248211022STom.Erickson@Sun.COM 	if (received) {
248311022STom.Erickson@Sun.COM 		nvlist_t *dummy;
248411022STom.Erickson@Sun.COM 		nvpair_t *pair;
248511022STom.Erickson@Sun.COM 		zprop_type_t type;
248611022STom.Erickson@Sun.COM 		int err;
248711022STom.Erickson@Sun.COM 
248811022STom.Erickson@Sun.COM 		/*
248911022STom.Erickson@Sun.COM 		 * zfs_prop_set_special() expects properties in the form of an
249011022STom.Erickson@Sun.COM 		 * nvpair with type info.
249111022STom.Erickson@Sun.COM 		 */
249211022STom.Erickson@Sun.COM 		if (prop == ZPROP_INVAL) {
249311022STom.Erickson@Sun.COM 			if (!zfs_prop_user(propname))
249411022STom.Erickson@Sun.COM 				return (EINVAL);
249511022STom.Erickson@Sun.COM 
249611022STom.Erickson@Sun.COM 			type = PROP_TYPE_STRING;
249711515STom.Erickson@Sun.COM 		} else if (prop == ZFS_PROP_VOLSIZE ||
249811515STom.Erickson@Sun.COM 		    prop == ZFS_PROP_VERSION) {
249911515STom.Erickson@Sun.COM 			return (EINVAL);
250011022STom.Erickson@Sun.COM 		} else {
250111022STom.Erickson@Sun.COM 			type = zfs_prop_get_type(prop);
250211022STom.Erickson@Sun.COM 		}
250311022STom.Erickson@Sun.COM 
250411022STom.Erickson@Sun.COM 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
250511022STom.Erickson@Sun.COM 
250611022STom.Erickson@Sun.COM 		switch (type) {
250711022STom.Erickson@Sun.COM 		case PROP_TYPE_STRING:
250811022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
250911022STom.Erickson@Sun.COM 			break;
251011022STom.Erickson@Sun.COM 		case PROP_TYPE_NUMBER:
251111022STom.Erickson@Sun.COM 		case PROP_TYPE_INDEX:
251211022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
251311022STom.Erickson@Sun.COM 			break;
251411022STom.Erickson@Sun.COM 		default:
251511022STom.Erickson@Sun.COM 			nvlist_free(dummy);
251611022STom.Erickson@Sun.COM 			return (EINVAL);
251711022STom.Erickson@Sun.COM 		}
251811022STom.Erickson@Sun.COM 
251911022STom.Erickson@Sun.COM 		pair = nvlist_next_nvpair(dummy, NULL);
252011022STom.Erickson@Sun.COM 		err = zfs_prop_set_special(zc->zc_name, source, pair);
252111022STom.Erickson@Sun.COM 		nvlist_free(dummy);
252211022STom.Erickson@Sun.COM 		if (err != -1)
252311022STom.Erickson@Sun.COM 			return (err); /* special property already handled */
252411022STom.Erickson@Sun.COM 	} else {
252511022STom.Erickson@Sun.COM 		/*
252611022STom.Erickson@Sun.COM 		 * Only check this in the non-received case. We want to allow
252711022STom.Erickson@Sun.COM 		 * 'inherit -S' to revert non-inheritable properties like quota
252811022STom.Erickson@Sun.COM 		 * and reservation to the received or default values even though
252911022STom.Erickson@Sun.COM 		 * they are not considered inheritable.
253011022STom.Erickson@Sun.COM 		 */
253111022STom.Erickson@Sun.COM 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
253211022STom.Erickson@Sun.COM 			return (EINVAL);
253311022STom.Erickson@Sun.COM 	}
253411022STom.Erickson@Sun.COM 
25354849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
253611022STom.Erickson@Sun.COM 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
25374849Sahrens }
25384849Sahrens 
25394849Sahrens static int
25404098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
25413912Slling {
25425094Slling 	nvlist_t *props;
25433912Slling 	spa_t *spa;
25445094Slling 	int error;
254511022STom.Erickson@Sun.COM 	nvpair_t *pair;
254611022STom.Erickson@Sun.COM 
254711022STom.Erickson@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
254811022STom.Erickson@Sun.COM 	    zc->zc_iflags, &props))
25493912Slling 		return (error);
25503912Slling 
25518525SEric.Schrock@Sun.COM 	/*
25528525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
25538525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
25548525SEric.Schrock@Sun.COM 	 */
255511022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
255611022STom.Erickson@Sun.COM 	if (pair != NULL && strcmp(nvpair_name(pair),
25578525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
255811022STom.Erickson@Sun.COM 	    nvlist_next_nvpair(props, pair) == NULL) {
25598525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
25608525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
25618525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
25628525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
25638525SEric.Schrock@Sun.COM 		}
25648525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
256510672SEric.Schrock@Sun.COM 		if (spa != NULL) {
256610672SEric.Schrock@Sun.COM 			nvlist_free(props);
25678525SEric.Schrock@Sun.COM 			return (0);
256810672SEric.Schrock@Sun.COM 		}
25698525SEric.Schrock@Sun.COM 	}
25708525SEric.Schrock@Sun.COM 
25713912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
25725094Slling 		nvlist_free(props);
25733912Slling 		return (error);
25743912Slling 	}
25753912Slling 
25765094Slling 	error = spa_prop_set(spa, props);
25773912Slling 
25785094Slling 	nvlist_free(props);
25793912Slling 	spa_close(spa, FTAG);
25803912Slling 
25813912Slling 	return (error);
25823912Slling }
25833912Slling 
25843912Slling static int
25854098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
25863912Slling {
25873912Slling 	spa_t *spa;
25883912Slling 	int error;
25893912Slling 	nvlist_t *nvp = NULL;
25903912Slling 
25918525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
25928525SEric.Schrock@Sun.COM 		/*
25938525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
25948525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
25958525SEric.Schrock@Sun.COM 		 * anyway.
25968525SEric.Schrock@Sun.COM 		 */
25978525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
25988525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
25998525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
26008525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
26018525SEric.Schrock@Sun.COM 	} else {
26028525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
26038525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
26048525SEric.Schrock@Sun.COM 	}
26053912Slling 
26063912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
26073912Slling 		error = put_nvlist(zc, nvp);
26083912Slling 	else
26093912Slling 		error = EFAULT;
26103912Slling 
26118525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
26123912Slling 	return (error);
26133912Slling }
26143912Slling 
26155367Sahrens /*
26165367Sahrens  * inputs:
26175367Sahrens  * zc_name		name of filesystem
26185367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
26195367Sahrens  * zc_perm_action	allow/unallow flag
26205367Sahrens  *
26215367Sahrens  * outputs:		none
26225367Sahrens  */
26234543Smarks static int
26244543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
26254543Smarks {
26264543Smarks 	int error;
26274543Smarks 	nvlist_t *fsaclnv = NULL;
26284543Smarks 
26295094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
26309643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
26314543Smarks 		return (error);
26324543Smarks 
26334543Smarks 	/*
26344543Smarks 	 * Verify nvlist is constructed correctly
26354543Smarks 	 */
26364543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
26374543Smarks 		nvlist_free(fsaclnv);
26384543Smarks 		return (EINVAL);
26394543Smarks 	}
26404543Smarks 
26414543Smarks 	/*
26424543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
26434543Smarks 	 * that user is allowed to hand out each permission in
26444543Smarks 	 * the nvlist(s)
26454543Smarks 	 */
26464543Smarks 
26474787Sahrens 	error = secpolicy_zfs(CRED());
26484543Smarks 	if (error) {
26494787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
26504787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
26514787Sahrens 			    fsaclnv, CRED());
26524787Sahrens 		} else {
26534787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
26544787Sahrens 			    fsaclnv, CRED());
26554787Sahrens 		}
26564543Smarks 	}
26574543Smarks 
26584543Smarks 	if (error == 0)
26594543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
26604543Smarks 
26614543Smarks 	nvlist_free(fsaclnv);
26624543Smarks 	return (error);
26634543Smarks }
26644543Smarks 
26655367Sahrens /*
26665367Sahrens  * inputs:
26675367Sahrens  * zc_name		name of filesystem
26685367Sahrens  *
26695367Sahrens  * outputs:
26705367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
26715367Sahrens  */
26724543Smarks static int
26734543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
26744543Smarks {
26754543Smarks 	nvlist_t *nvp;
26764543Smarks 	int error;
26774543Smarks 
26784543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
26794543Smarks 		error = put_nvlist(zc, nvp);
26804543Smarks 		nvlist_free(nvp);
26814543Smarks 	}
26824543Smarks 
26834543Smarks 	return (error);
26844543Smarks }
26854543Smarks 
26865367Sahrens /*
2687789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2688789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2689789Sahrens  * is responsible for releasing the returned vfs pointer.
2690789Sahrens  */
2691789Sahrens static vfs_t *
2692789Sahrens zfs_get_vfs(const char *resource)
2693789Sahrens {
2694789Sahrens 	struct vfs *vfsp;
2695789Sahrens 	struct vfs *vfs_found = NULL;
2696789Sahrens 
2697789Sahrens 	vfs_list_read_lock();
2698789Sahrens 	vfsp = rootvfs;
2699789Sahrens 	do {
2700789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2701789Sahrens 			VFS_HOLD(vfsp);
2702789Sahrens 			vfs_found = vfsp;
2703789Sahrens 			break;
2704789Sahrens 		}
2705789Sahrens 		vfsp = vfsp->vfs_next;
2706789Sahrens 	} while (vfsp != rootvfs);
2707789Sahrens 	vfs_list_unlock();
2708789Sahrens 	return (vfs_found);
2709789Sahrens }
2710789Sahrens 
27114543Smarks /* ARGSUSED */
2712789Sahrens static void
27134543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2714789Sahrens {
27155331Samw 	zfs_creat_t *zct = arg;
27165498Stimh 
27175498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
27185331Samw }
27195331Samw 
27205498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
27215498Stimh 
27225331Samw /*
27235498Stimh  * inputs:
27247184Stimh  * createprops		list of properties requested by creator
27257184Stimh  * default_zplver	zpl version to use if unspecified in createprops
27267184Stimh  * fuids_ok		fuids allowed in this version of the spa?
27277184Stimh  * os			parent objset pointer (NULL if root fs)
27285331Samw  *
27295498Stimh  * outputs:
27305498Stimh  * zplprops	values for the zplprops we attach to the master node object
27317184Stimh  * is_ci	true if requested file system will be purely case-insensitive
27325331Samw  *
27335498Stimh  * Determine the settings for utf8only, normalization and
27345498Stimh  * casesensitivity.  Specific values may have been requested by the
27355498Stimh  * creator and/or we can inherit values from the parent dataset.  If
27365498Stimh  * the file system is of too early a vintage, a creator can not
27375498Stimh  * request settings for these properties, even if the requested
27385498Stimh  * setting is the default value.  We don't actually want to create dsl
27395498Stimh  * properties for these, so remove them from the source nvlist after
27405498Stimh  * processing.
27415331Samw  */
27425331Samw static int
27439396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
274411935SMark.Shellenbaum@Sun.COM     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
274511935SMark.Shellenbaum@Sun.COM     nvlist_t *zplprops, boolean_t *is_ci)
27465331Samw {
27475498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
27485498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
27495498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
27505498Stimh 
27515498Stimh 	ASSERT(zplprops != NULL);
27525498Stimh 
27535375Stimh 	/*
27545498Stimh 	 * Pull out creator prop choices, if any.
27555375Stimh 	 */
27565498Stimh 	if (createprops) {
27575498Stimh 		(void) nvlist_lookup_uint64(createprops,
27587184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
27597184Stimh 		(void) nvlist_lookup_uint64(createprops,
27605498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
27615498Stimh 		(void) nvlist_remove_all(createprops,
27625498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
27635498Stimh 		(void) nvlist_lookup_uint64(createprops,
27645498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
27655498Stimh 		(void) nvlist_remove_all(createprops,
27665498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
27675498Stimh 		(void) nvlist_lookup_uint64(createprops,
27685498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
27695498Stimh 		(void) nvlist_remove_all(createprops,
27705498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
27715331Samw 	}
27725331Samw 
27735375Stimh 	/*
27747184Stimh 	 * If the zpl version requested is whacky or the file system
27757184Stimh 	 * or pool is version is too "young" to support normalization
27767184Stimh 	 * and the creator tried to set a value for one of the props,
27777184Stimh 	 * error out.
27785498Stimh 	 */
27797184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
27807184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
278111935SMark.Shellenbaum@Sun.COM 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
27827184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
27835498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
27847184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
27855498Stimh 		return (ENOTSUP);
27865498Stimh 
27875498Stimh 	/*
27885498Stimh 	 * Put the version in the zplprops
27895498Stimh 	 */
27905498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
27915498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
27925498Stimh 
27935498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
27945498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
27955498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
27965498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
27975498Stimh 
27985498Stimh 	/*
27995498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
28005498Stimh 	 */
28015498Stimh 	if (norm)
28025498Stimh 		u8 = 1;
28035498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
28045498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
28055498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
28065498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
28075498Stimh 
28085498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
28095498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
28105498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
28115498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
28125498Stimh 
28136492Stimh 	if (is_ci)
28146492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
28156492Stimh 
28167184Stimh 	return (0);
28177184Stimh }
28187184Stimh 
28197184Stimh static int
28207184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
28217184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
28227184Stimh {
282311935SMark.Shellenbaum@Sun.COM 	boolean_t fuids_ok, sa_ok;
28247184Stimh 	uint64_t zplver = ZPL_VERSION;
28257184Stimh 	objset_t *os = NULL;
28267184Stimh 	char parentname[MAXNAMELEN];
28277184Stimh 	char *cp;
282811935SMark.Shellenbaum@Sun.COM 	spa_t *spa;
282911935SMark.Shellenbaum@Sun.COM 	uint64_t spa_vers;
28307184Stimh 	int error;
28317184Stimh 
28327184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
28337184Stimh 	cp = strrchr(parentname, '/');
28347184Stimh 	ASSERT(cp != NULL);
28357184Stimh 	cp[0] = '\0';
28367184Stimh 
283711935SMark.Shellenbaum@Sun.COM 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
283811935SMark.Shellenbaum@Sun.COM 		return (error);
283911935SMark.Shellenbaum@Sun.COM 
284011935SMark.Shellenbaum@Sun.COM 	spa_vers = spa_version(spa);
284111935SMark.Shellenbaum@Sun.COM 	spa_close(spa, FTAG);
284211935SMark.Shellenbaum@Sun.COM 
284311935SMark.Shellenbaum@Sun.COM 	zplver = zfs_zpl_version_map(spa_vers);
284411935SMark.Shellenbaum@Sun.COM 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
284511935SMark.Shellenbaum@Sun.COM 	sa_ok = (zplver >= ZPL_VERSION_SA);
28467184Stimh 
28477184Stimh 	/*
28487184Stimh 	 * Open parent object set so we can inherit zplprop values.
28497184Stimh 	 */
285010298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
28517184Stimh 		return (error);
28527184Stimh 
285311935SMark.Shellenbaum@Sun.COM 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
28547184Stimh 	    zplprops, is_ci);
285510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
28567184Stimh 	return (error);
28577184Stimh }
28587184Stimh 
28597184Stimh static int
28607184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
28617184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
28627184Stimh {
286311935SMark.Shellenbaum@Sun.COM 	boolean_t fuids_ok;
286411935SMark.Shellenbaum@Sun.COM 	boolean_t sa_ok;
28657184Stimh 	uint64_t zplver = ZPL_VERSION;
28667184Stimh 	int error;
28677184Stimh 
286811935SMark.Shellenbaum@Sun.COM 	zplver = zfs_zpl_version_map(spa_vers);
286911935SMark.Shellenbaum@Sun.COM 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
287011935SMark.Shellenbaum@Sun.COM 	sa_ok = (zplver >= ZPL_VERSION_SA);
287111935SMark.Shellenbaum@Sun.COM 
287211935SMark.Shellenbaum@Sun.COM 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
287311935SMark.Shellenbaum@Sun.COM 	    createprops, zplprops, is_ci);
28747184Stimh 	return (error);
2875789Sahrens }
2876789Sahrens 
28775367Sahrens /*
28785367Sahrens  * inputs:
28795367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
28805367Sahrens  * zc_name		name of new objset
28815367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
28825367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
28835367Sahrens  *
28845498Stimh  * outputs: none
28855367Sahrens  */
2886789Sahrens static int
2887789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2888789Sahrens {
2889789Sahrens 	objset_t *clone;
2890789Sahrens 	int error = 0;
28915331Samw 	zfs_creat_t zct;
28924543Smarks 	nvlist_t *nvprops = NULL;
28934543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2894789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2895789Sahrens 
2896789Sahrens 	switch (type) {
2897789Sahrens 
2898789Sahrens 	case DMU_OST_ZFS:
2899789Sahrens 		cbfunc = zfs_create_cb;
2900789Sahrens 		break;
2901789Sahrens 
2902789Sahrens 	case DMU_OST_ZVOL:
2903789Sahrens 		cbfunc = zvol_create_cb;
2904789Sahrens 		break;
2905789Sahrens 
2906789Sahrens 	default:
29072199Sahrens 		cbfunc = NULL;
29086423Sgw25295 		break;
29092199Sahrens 	}
29105326Sek110237 	if (strchr(zc->zc_name, '@') ||
29115326Sek110237 	    strchr(zc->zc_name, '%'))
2912789Sahrens 		return (EINVAL);
2913789Sahrens 
29142676Seschrock 	if (zc->zc_nvlist_src != NULL &&
29155094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
29169643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
29172676Seschrock 		return (error);
29182676Seschrock 
29195498Stimh 	zct.zct_zplprops = NULL;
29205331Samw 	zct.zct_props = nvprops;
29215331Samw 
29222676Seschrock 	if (zc->zc_value[0] != '\0') {
2923789Sahrens 		/*
2924789Sahrens 		 * We're creating a clone of an existing snapshot.
2925789Sahrens 		 */
29262676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
29272676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
29284543Smarks 			nvlist_free(nvprops);
2929789Sahrens 			return (EINVAL);
29302676Seschrock 		}
2931789Sahrens 
293210298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
29332676Seschrock 		if (error) {
29344543Smarks 			nvlist_free(nvprops);
2935789Sahrens 			return (error);
29362676Seschrock 		}
29376492Stimh 
293810272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
293910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
29405331Samw 		if (error) {
29415331Samw 			nvlist_free(nvprops);
29425331Samw 			return (error);
29435331Samw 		}
2944789Sahrens 	} else {
29456492Stimh 		boolean_t is_insensitive = B_FALSE;
29466492Stimh 
29472676Seschrock 		if (cbfunc == NULL) {
29484543Smarks 			nvlist_free(nvprops);
29492199Sahrens 			return (EINVAL);
29502676Seschrock 		}
29512676Seschrock 
2952789Sahrens 		if (type == DMU_OST_ZVOL) {
29532676Seschrock 			uint64_t volsize, volblocksize;
29542676Seschrock 
29554543Smarks 			if (nvprops == NULL ||
29564543Smarks 			    nvlist_lookup_uint64(nvprops,
29572676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
29582676Seschrock 			    &volsize) != 0) {
29594543Smarks 				nvlist_free(nvprops);
29602676Seschrock 				return (EINVAL);
29612676Seschrock 			}
29622676Seschrock 
29634543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
29642676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29652676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
29664543Smarks 				nvlist_free(nvprops);
29672676Seschrock 				return (EINVAL);
29682676Seschrock 			}
29691133Seschrock 
29702676Seschrock 			if (error != 0)
29712676Seschrock 				volblocksize = zfs_prop_default_numeric(
29722676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29732676Seschrock 
29742676Seschrock 			if ((error = zvol_check_volblocksize(
29752676Seschrock 			    volblocksize)) != 0 ||
29762676Seschrock 			    (error = zvol_check_volsize(volsize,
29772676Seschrock 			    volblocksize)) != 0) {
29784543Smarks 				nvlist_free(nvprops);
2979789Sahrens 				return (error);
29802676Seschrock 			}
29814577Sahrens 		} else if (type == DMU_OST_ZFS) {
29825331Samw 			int error;
29835331Samw 
29845498Stimh 			/*
29855331Samw 			 * We have to have normalization and
29865331Samw 			 * case-folding flags correct when we do the
29875331Samw 			 * file system creation, so go figure them out
29885498Stimh 			 * now.
29895331Samw 			 */
29905498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
29915498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
29925498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
29937184Stimh 			    zct.zct_zplprops, &is_insensitive);
29945331Samw 			if (error != 0) {
29955331Samw 				nvlist_free(nvprops);
29965498Stimh 				nvlist_free(zct.zct_zplprops);
29975331Samw 				return (error);
29984577Sahrens 			}
29992676Seschrock 		}
300010272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
30016492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
30025498Stimh 		nvlist_free(zct.zct_zplprops);
3003789Sahrens 	}
30042676Seschrock 
30052676Seschrock 	/*
30062676Seschrock 	 * It would be nice to do this atomically.
30072676Seschrock 	 */
30082676Seschrock 	if (error == 0) {
300911022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
301011022STom.Erickson@Sun.COM 		    nvprops, NULL);
301111022STom.Erickson@Sun.COM 		if (error != 0)
301210242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
30132676Seschrock 	}
30144543Smarks 	nvlist_free(nvprops);
3015789Sahrens 	return (error);
3016789Sahrens }
3017789Sahrens 
30185367Sahrens /*
30195367Sahrens  * inputs:
30205367Sahrens  * zc_name	name of filesystem
30215367Sahrens  * zc_value	short name of snapshot
30225367Sahrens  * zc_cookie	recursive flag
30239396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
30245367Sahrens  *
302510588SEric.Taylor@Sun.COM  * outputs:
302610588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
30275367Sahrens  */
3028789Sahrens static int
30292199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
30302199Sahrens {
30317265Sahrens 	nvlist_t *nvprops = NULL;
30327265Sahrens 	int error;
30337265Sahrens 	boolean_t recursive = zc->zc_cookie;
30347265Sahrens 
30352676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
30362199Sahrens 		return (EINVAL);
30377265Sahrens 
30387265Sahrens 	if (zc->zc_nvlist_src != NULL &&
30397265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
30409643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
30417265Sahrens 		return (error);
30427265Sahrens 
30439355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
30449355SMatthew.Ahrens@Sun.COM 	if (error)
30459355SMatthew.Ahrens@Sun.COM 		goto out;
30469355SMatthew.Ahrens@Sun.COM 
304711022STom.Erickson@Sun.COM 	if (!nvlist_empty(nvprops) &&
30489355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
30499355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
30509355SMatthew.Ahrens@Sun.COM 		goto out;
30517265Sahrens 	}
30529355SMatthew.Ahrens@Sun.COM 
3053*13043STim.Haley@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3054*13043STim.Haley@Sun.COM 	    nvprops, recursive, B_FALSE, -1);
30559355SMatthew.Ahrens@Sun.COM 
30569355SMatthew.Ahrens@Sun.COM out:
30577265Sahrens 	nvlist_free(nvprops);
30587265Sahrens 	return (error);
30592199Sahrens }
30602199Sahrens 
30614007Smmusante int
306211209SMatthew.Ahrens@Sun.COM zfs_unmount_snap(const char *name, void *arg)
3063789Sahrens {
30642417Sahrens 	vfs_t *vfsp = NULL;
30652199Sahrens 
30666689Smaybee 	if (arg) {
30676689Smaybee 		char *snapname = arg;
306811209SMatthew.Ahrens@Sun.COM 		char *fullname = kmem_asprintf("%s@%s", name, snapname);
306911209SMatthew.Ahrens@Sun.COM 		vfsp = zfs_get_vfs(fullname);
307011209SMatthew.Ahrens@Sun.COM 		strfree(fullname);
30712417Sahrens 	} else if (strchr(name, '@')) {
30722199Sahrens 		vfsp = zfs_get_vfs(name);
30732199Sahrens 	}
30742199Sahrens 
30752199Sahrens 	if (vfsp) {
30762199Sahrens 		/*
30772199Sahrens 		 * Always force the unmount for snapshots.
30782199Sahrens 		 */
30792199Sahrens 		int flag = MS_FORCE;
3080789Sahrens 		int err;
3081789Sahrens 
30822199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
30832199Sahrens 			VFS_RELE(vfsp);
30842199Sahrens 			return (err);
30852199Sahrens 		}
30862199Sahrens 		VFS_RELE(vfsp);
30872199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
30882199Sahrens 			return (err);
30892199Sahrens 	}
30902199Sahrens 	return (0);
30912199Sahrens }
30922199Sahrens 
30935367Sahrens /*
30945367Sahrens  * inputs:
309510242Schris.kirby@sun.com  * zc_name		name of filesystem
309610242Schris.kirby@sun.com  * zc_value		short name of snapshot
309710242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
30985367Sahrens  *
30995367Sahrens  * outputs:	none
31005367Sahrens  */
31012199Sahrens static int
31022199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
31032199Sahrens {
31042199Sahrens 	int err;
3105789Sahrens 
31062676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
31072199Sahrens 		return (EINVAL);
31082199Sahrens 	err = dmu_objset_find(zc->zc_name,
31092676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
31102199Sahrens 	if (err)
31112199Sahrens 		return (err);
311210242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
311310242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
31142199Sahrens }
31152199Sahrens 
31165367Sahrens /*
31175367Sahrens  * inputs:
31185367Sahrens  * zc_name		name of dataset to destroy
31195367Sahrens  * zc_objset_type	type of objset
312010242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
31215367Sahrens  *
31225367Sahrens  * outputs:		none
31235367Sahrens  */
31242199Sahrens static int
31252199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
31262199Sahrens {
312710588SEric.Taylor@Sun.COM 	int err;
31282199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
312910588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
31302199Sahrens 		if (err)
31312199Sahrens 			return (err);
3132789Sahrens 	}
3133789Sahrens 
313410588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
313510588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
313610693Schris.kirby@sun.com 		(void) zvol_remove_minor(zc->zc_name);
313710588SEric.Taylor@Sun.COM 	return (err);
3138789Sahrens }
3139789Sahrens 
31405367Sahrens /*
31415367Sahrens  * inputs:
31425446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
31435367Sahrens  *
31445367Sahrens  * outputs:	none
31455367Sahrens  */
3146789Sahrens static int
3147789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
3148789Sahrens {
314910272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
31505446Sahrens 	int error;
315110272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
315210272SMatthew.Ahrens@Sun.COM 	char *clone_name;
315310272SMatthew.Ahrens@Sun.COM 
315410272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
315510272SMatthew.Ahrens@Sun.COM 	if (error)
315610272SMatthew.Ahrens@Sun.COM 		return (error);
315710272SMatthew.Ahrens@Sun.COM 
315810272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
315910272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
316010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
316110272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
316210272SMatthew.Ahrens@Sun.COM 	}
316310272SMatthew.Ahrens@Sun.COM 
316410272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
316510272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
316610272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
316710272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
316810272SMatthew.Ahrens@Sun.COM 	}
31695446Sahrens 
31705446Sahrens 	/*
317110272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
31725446Sahrens 	 */
317310272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
317410272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
31755446Sahrens 	if (error)
317610272SMatthew.Ahrens@Sun.COM 		goto out;
317710272SMatthew.Ahrens@Sun.COM 
317810298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
317910272SMatthew.Ahrens@Sun.COM 	if (error)
318010272SMatthew.Ahrens@Sun.COM 		goto out;
318110272SMatthew.Ahrens@Sun.COM 
318210272SMatthew.Ahrens@Sun.COM 	/*
318310272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
318410272SMatthew.Ahrens@Sun.COM 	 */
31859396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
318610298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
31876083Sek110237 		if (error == 0) {
31886083Sek110237 			int resume_err;
31896083Sek110237 
319010272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
319110272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
319210272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
319310272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
319410272SMatthew.Ahrens@Sun.COM 				ds = NULL;
319510272SMatthew.Ahrens@Sun.COM 			} else {
319610272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
319710272SMatthew.Ahrens@Sun.COM 			}
319810298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
31996083Sek110237 			error = error ? error : resume_err;
32006083Sek110237 		}
32015446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
32025446Sahrens 	} else {
320310272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
320410272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
320510272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
320610272SMatthew.Ahrens@Sun.COM 			ds = NULL;
320710272SMatthew.Ahrens@Sun.COM 		} else {
320810272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
320910272SMatthew.Ahrens@Sun.COM 		}
32105446Sahrens 	}
321110272SMatthew.Ahrens@Sun.COM 
321210272SMatthew.Ahrens@Sun.COM 	/*
321310272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
321410272SMatthew.Ahrens@Sun.COM 	 */
321510272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
321610272SMatthew.Ahrens@Sun.COM 
321710272SMatthew.Ahrens@Sun.COM out:
321810272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
321910272SMatthew.Ahrens@Sun.COM 	if (ds)
322010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
32215446Sahrens 	return (error);
3222789Sahrens }
3223789Sahrens 
32245367Sahrens /*
32255367Sahrens  * inputs:
32265367Sahrens  * zc_name	old name of dataset
32275367Sahrens  * zc_value	new name of dataset
32285367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
32295367Sahrens  *
32305367Sahrens  * outputs:	none
32315367Sahrens  */
3232789Sahrens static int
3233789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3234789Sahrens {
32354490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
32364007Smmusante 
32372676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
32385326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
32395326Sek110237 	    strchr(zc->zc_value, '%'))
3240789Sahrens 		return (EINVAL);
3241789Sahrens 
32424007Smmusante 	/*
32434007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
32444007Smmusante 	 * in which case the dataset code figures out which snapshots
32454007Smmusante 	 * to unmount.
32464007Smmusante 	 */
32474007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3248789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
32492199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
32502199Sahrens 		if (err)
32512199Sahrens 			return (err);
3252789Sahrens 	}
325310588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
325410588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
32554007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3256789Sahrens }
3257789Sahrens 
325811022STom.Erickson@Sun.COM static int
325911022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
326011022STom.Erickson@Sun.COM {
326111022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
326211022STom.Erickson@Sun.COM 	boolean_t issnap = (strchr(dsname, '@') != NULL);
326311022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
326411022STom.Erickson@Sun.COM 	uint64_t intval;
326511022STom.Erickson@Sun.COM 	int err;
326611022STom.Erickson@Sun.COM 
326711022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
326811022STom.Erickson@Sun.COM 		if (zfs_prop_user(propname)) {
326911022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname,
327011022STom.Erickson@Sun.COM 			    ZFS_DELEG_PERM_USERPROP, cr))
327111022STom.Erickson@Sun.COM 				return (err);
327211022STom.Erickson@Sun.COM 			return (0);
327311022STom.Erickson@Sun.COM 		}
327411022STom.Erickson@Sun.COM 
327511022STom.Erickson@Sun.COM 		if (!issnap && zfs_prop_userquota(propname)) {
327611022STom.Erickson@Sun.COM 			const char *perm = NULL;
327711022STom.Erickson@Sun.COM 			const char *uq_prefix =
327811022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
327911022STom.Erickson@Sun.COM 			const char *gq_prefix =
328011022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
328111022STom.Erickson@Sun.COM 
328211022STom.Erickson@Sun.COM 			if (strncmp(propname, uq_prefix,
328311022STom.Erickson@Sun.COM 			    strlen(uq_prefix)) == 0) {
328411022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_USERQUOTA;
328511022STom.Erickson@Sun.COM 			} else if (strncmp(propname, gq_prefix,
328611022STom.Erickson@Sun.COM 			    strlen(gq_prefix)) == 0) {
328711022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
328811022STom.Erickson@Sun.COM 			} else {
328911022STom.Erickson@Sun.COM 				/* USERUSED and GROUPUSED are read-only */
329011022STom.Erickson@Sun.COM 				return (EINVAL);
329111022STom.Erickson@Sun.COM 			}
329211022STom.Erickson@Sun.COM 
329311022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
329411022STom.Erickson@Sun.COM 				return (err);
329511022STom.Erickson@Sun.COM 			return (0);
329611022STom.Erickson@Sun.COM 		}
329711022STom.Erickson@Sun.COM 
329811022STom.Erickson@Sun.COM 		return (EINVAL);
329911022STom.Erickson@Sun.COM 	}
330011022STom.Erickson@Sun.COM 
330111022STom.Erickson@Sun.COM 	if (issnap)
330211022STom.Erickson@Sun.COM 		return (EINVAL);
330311022STom.Erickson@Sun.COM 
330411022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
330511022STom.Erickson@Sun.COM 		/*
330611022STom.Erickson@Sun.COM 		 * dsl_prop_get_all_impl() returns properties in this
330711022STom.Erickson@Sun.COM 		 * format.
330811022STom.Erickson@Sun.COM 		 */
330911022STom.Erickson@Sun.COM 		nvlist_t *attrs;
331011022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
331111022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
331211022STom.Erickson@Sun.COM 		    &pair) == 0);
331311022STom.Erickson@Sun.COM 	}
331411022STom.Erickson@Sun.COM 
331511022STom.Erickson@Sun.COM 	/*
331611022STom.Erickson@Sun.COM 	 * Check that this value is valid for this pool version
331711022STom.Erickson@Sun.COM 	 */
331811022STom.Erickson@Sun.COM 	switch (prop) {
331911022STom.Erickson@Sun.COM 	case ZFS_PROP_COMPRESSION:
332011022STom.Erickson@Sun.COM 		/*
332111022STom.Erickson@Sun.COM 		 * If the user specified gzip compression, make sure
332211022STom.Erickson@Sun.COM 		 * the SPA supports it. We ignore any errors here since
332311022STom.Erickson@Sun.COM 		 * we'll catch them later.
332411022STom.Erickson@Sun.COM 		 */
332511022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
332611022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
332711022STom.Erickson@Sun.COM 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
332811022STom.Erickson@Sun.COM 			    intval <= ZIO_COMPRESS_GZIP_9 &&
332911022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
333011022STom.Erickson@Sun.COM 			    SPA_VERSION_GZIP_COMPRESSION)) {
333111022STom.Erickson@Sun.COM 				return (ENOTSUP);
333211022STom.Erickson@Sun.COM 			}
333311022STom.Erickson@Sun.COM 
333411022STom.Erickson@Sun.COM 			if (intval == ZIO_COMPRESS_ZLE &&
333511022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
333611022STom.Erickson@Sun.COM 			    SPA_VERSION_ZLE_COMPRESSION))
333711022STom.Erickson@Sun.COM 				return (ENOTSUP);
333811022STom.Erickson@Sun.COM 
333911022STom.Erickson@Sun.COM 			/*
334011022STom.Erickson@Sun.COM 			 * If this is a bootable dataset then
334111022STom.Erickson@Sun.COM 			 * verify that the compression algorithm
334211022STom.Erickson@Sun.COM 			 * is supported for booting. We must return
334311022STom.Erickson@Sun.COM 			 * something other than ENOTSUP since it
334411022STom.Erickson@Sun.COM 			 * implies a downrev pool version.
334511022STom.Erickson@Sun.COM 			 */
334611022STom.Erickson@Sun.COM 			if (zfs_is_bootfs(dsname) &&
334711022STom.Erickson@Sun.COM 			    !BOOTFS_COMPRESS_VALID(intval)) {
334811022STom.Erickson@Sun.COM 				return (ERANGE);
334911022STom.Erickson@Sun.COM 			}
335011022STom.Erickson@Sun.COM 		}
335111022STom.Erickson@Sun.COM 		break;
335211022STom.Erickson@Sun.COM 
335311022STom.Erickson@Sun.COM 	case ZFS_PROP_COPIES:
335411022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
335511022STom.Erickson@Sun.COM 			return (ENOTSUP);
335611022STom.Erickson@Sun.COM 		break;
335711022STom.Erickson@Sun.COM 
335811022STom.Erickson@Sun.COM 	case ZFS_PROP_DEDUP:
335911022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
336011022STom.Erickson@Sun.COM 			return (ENOTSUP);
336111022STom.Erickson@Sun.COM 		break;
336211022STom.Erickson@Sun.COM 
336311022STom.Erickson@Sun.COM 	case ZFS_PROP_SHARESMB:
336411022STom.Erickson@Sun.COM 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
336511022STom.Erickson@Sun.COM 			return (ENOTSUP);
336611022STom.Erickson@Sun.COM 		break;
336711022STom.Erickson@Sun.COM 
336811022STom.Erickson@Sun.COM 	case ZFS_PROP_ACLINHERIT:
336911022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
337011022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
337111022STom.Erickson@Sun.COM 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
337211022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
337311022STom.Erickson@Sun.COM 			    SPA_VERSION_PASSTHROUGH_X))
337411022STom.Erickson@Sun.COM 				return (ENOTSUP);
337511022STom.Erickson@Sun.COM 		}
337611022STom.Erickson@Sun.COM 		break;
337711022STom.Erickson@Sun.COM 	}
337811022STom.Erickson@Sun.COM 
337911022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
338011022STom.Erickson@Sun.COM }
338111022STom.Erickson@Sun.COM 
338211022STom.Erickson@Sun.COM /*
338311022STom.Erickson@Sun.COM  * Removes properties from the given props list that fail permission checks
338411022STom.Erickson@Sun.COM  * needed to clear them and to restore them in case of a receive error. For each
338511022STom.Erickson@Sun.COM  * property, make sure we have both set and inherit permissions.
338611022STom.Erickson@Sun.COM  *
338711022STom.Erickson@Sun.COM  * Returns the first error encountered if any permission checks fail. If the
338811022STom.Erickson@Sun.COM  * caller provides a non-NULL errlist, it also gives the complete list of names
338911022STom.Erickson@Sun.COM  * of all the properties that failed a permission check along with the
339011022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
339111022STom.Erickson@Sun.COM  * returned errlist.
339211022STom.Erickson@Sun.COM  *
339311022STom.Erickson@Sun.COM  * If every property checks out successfully, zero is returned and the list
339411022STom.Erickson@Sun.COM  * pointed at by errlist is NULL.
339511022STom.Erickson@Sun.COM  */
339611022STom.Erickson@Sun.COM static int
339711022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
33986689Smaybee {
33996689Smaybee 	zfs_cmd_t *zc;
340011022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
340111022STom.Erickson@Sun.COM 	nvlist_t *errors;
340211022STom.Erickson@Sun.COM 	int err, rv = 0;
34036689Smaybee 
34046689Smaybee 	if (props == NULL)
340511022STom.Erickson@Sun.COM 		return (0);
340611022STom.Erickson@Sun.COM 
340711022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
340811022STom.Erickson@Sun.COM 
34096689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
34106689Smaybee 	(void) strcpy(zc->zc_name, dataset);
341111022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
341211022STom.Erickson@Sun.COM 	while (pair != NULL) {
341311022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
341411022STom.Erickson@Sun.COM 
341511022STom.Erickson@Sun.COM 		(void) strcpy(zc->zc_value, nvpair_name(pair));
341611022STom.Erickson@Sun.COM 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
341711022STom.Erickson@Sun.COM 		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
341811022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
341911022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors,
342011022STom.Erickson@Sun.COM 			    zc->zc_value, err) == 0);
342111022STom.Erickson@Sun.COM 		}
342211022STom.Erickson@Sun.COM 		pair = next_pair;
34236689Smaybee 	}
34246689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
342511022STom.Erickson@Sun.COM 
342611022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
342711022STom.Erickson@Sun.COM 		nvlist_free(errors);
342811022STom.Erickson@Sun.COM 		errors = NULL;
342911022STom.Erickson@Sun.COM 	} else {
343011022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
343111022STom.Erickson@Sun.COM 	}
343211022STom.Erickson@Sun.COM 
343311022STom.Erickson@Sun.COM 	if (errlist == NULL)
343411022STom.Erickson@Sun.COM 		nvlist_free(errors);
343511022STom.Erickson@Sun.COM 	else
343611022STom.Erickson@Sun.COM 		*errlist = errors;
343711022STom.Erickson@Sun.COM 
343811022STom.Erickson@Sun.COM 	return (rv);
34396689Smaybee }
34406689Smaybee 
344111022STom.Erickson@Sun.COM static boolean_t
344211022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2)
344311022STom.Erickson@Sun.COM {
344411022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
344511022STom.Erickson@Sun.COM 		/* dsl_prop_get_all_impl() format */
344611022STom.Erickson@Sun.COM 		nvlist_t *attrs;
344711022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
344811022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
344911022STom.Erickson@Sun.COM 		    &p1) == 0);
345011022STom.Erickson@Sun.COM 	}
345111022STom.Erickson@Sun.COM 
345211022STom.Erickson@Sun.COM 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
345311022STom.Erickson@Sun.COM 		nvlist_t *attrs;
345411022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
345511022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
345611022STom.Erickson@Sun.COM 		    &p2) == 0);
345711022STom.Erickson@Sun.COM 	}
345811022STom.Erickson@Sun.COM 
345911022STom.Erickson@Sun.COM 	if (nvpair_type(p1) != nvpair_type(p2))
346011022STom.Erickson@Sun.COM 		return (B_FALSE);
346111022STom.Erickson@Sun.COM 
346211022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
346311022STom.Erickson@Sun.COM 		char *valstr1, *valstr2;
346411022STom.Erickson@Sun.COM 
346511022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
346611022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
346711022STom.Erickson@Sun.COM 		return (strcmp(valstr1, valstr2) == 0);
346811022STom.Erickson@Sun.COM 	} else {
346911022STom.Erickson@Sun.COM 		uint64_t intval1, intval2;
347011022STom.Erickson@Sun.COM 
347111022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
347211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
347311022STom.Erickson@Sun.COM 		return (intval1 == intval2);
347411022STom.Erickson@Sun.COM 	}
347511022STom.Erickson@Sun.COM }
347611022STom.Erickson@Sun.COM 
347711022STom.Erickson@Sun.COM /*
347811022STom.Erickson@Sun.COM  * Remove properties from props if they are not going to change (as determined
347911022STom.Erickson@Sun.COM  * by comparison with origprops). Remove them from origprops as well, since we
348011022STom.Erickson@Sun.COM  * do not need to clear or restore properties that won't change.
348111022STom.Erickson@Sun.COM  */
348211022STom.Erickson@Sun.COM static void
348311022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops)
348411022STom.Erickson@Sun.COM {
348511022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
348611022STom.Erickson@Sun.COM 
348711022STom.Erickson@Sun.COM 	if (origprops == NULL)
348811022STom.Erickson@Sun.COM 		return; /* all props need to be received */
348911022STom.Erickson@Sun.COM 
349011022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
349111022STom.Erickson@Sun.COM 	while (pair != NULL) {
349211022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
349311022STom.Erickson@Sun.COM 		nvpair_t *match;
349411022STom.Erickson@Sun.COM 
349511022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
349611022STom.Erickson@Sun.COM 
349711022STom.Erickson@Sun.COM 		if ((nvlist_lookup_nvpair(origprops, propname,
349811022STom.Erickson@Sun.COM 		    &match) != 0) || !propval_equals(pair, match))
349911022STom.Erickson@Sun.COM 			goto next; /* need to set received value */
350011022STom.Erickson@Sun.COM 
350111022STom.Erickson@Sun.COM 		/* don't clear the existing received value */
350211022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(origprops, match);
350311022STom.Erickson@Sun.COM 		/* don't bother receiving the property */
350411022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(props, pair);
350511022STom.Erickson@Sun.COM next:
350611022STom.Erickson@Sun.COM 		pair = next_pair;
350711022STom.Erickson@Sun.COM 	}
350811022STom.Erickson@Sun.COM }
350911022STom.Erickson@Sun.COM 
351011022STom.Erickson@Sun.COM #ifdef	DEBUG
351111022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err;
351211022STom.Erickson@Sun.COM #endif
351311022STom.Erickson@Sun.COM 
35145367Sahrens /*
35155367Sahrens  * inputs:
35165367Sahrens  * zc_name		name of containing filesystem
35175367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
35185367Sahrens  * zc_value		name of snapshot to create
35195367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
35205367Sahrens  * zc_cookie		file descriptor to recv from
35215367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
35225367Sahrens  * zc_guid		force flag
352312527SChris.Kirby@oracle.com  * zc_cleanup_fd	cleanup-on-exit file descriptor
352412527SChris.Kirby@oracle.com  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
35255367Sahrens  *
35265367Sahrens  * outputs:
35275367Sahrens  * zc_cookie		number of bytes read
352811022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
352911022STom.Erickson@Sun.COM  * zc_obj		zprop_errflags_t
353012527SChris.Kirby@oracle.com  * zc_action_handle	handle for this guid/ds mapping
35315367Sahrens  */
3532789Sahrens static int
35335367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3534789Sahrens {
3535789Sahrens 	file_t *fp;
35365326Sek110237 	objset_t *os;
35375367Sahrens 	dmu_recv_cookie_t drc;
35385326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
353911022STom.Erickson@Sun.COM 	int fd;
354011022STom.Erickson@Sun.COM 	int error = 0;
354111022STom.Erickson@Sun.COM 	int props_error = 0;
354211022STom.Erickson@Sun.COM 	nvlist_t *errors;
35435367Sahrens 	offset_t off;
354411022STom.Erickson@Sun.COM 	nvlist_t *props = NULL; /* sent properties */
354511022STom.Erickson@Sun.COM 	nvlist_t *origprops = NULL; /* existing properties */
35465367Sahrens 	objset_t *origin = NULL;
35475367Sahrens 	char *tosnap;
35485367Sahrens 	char tofs[ZFS_MAXNAMELEN];
354911022STom.Erickson@Sun.COM 	boolean_t first_recvd_props = B_FALSE;
3550789Sahrens 
35513265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
35525326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
35535326Sek110237 	    strchr(zc->zc_value, '%'))
35543265Sahrens 		return (EINVAL);
35553265Sahrens 
35565367Sahrens 	(void) strcpy(tofs, zc->zc_value);
35575367Sahrens 	tosnap = strchr(tofs, '@');
355811022STom.Erickson@Sun.COM 	*tosnap++ = '\0';
35595367Sahrens 
35605367Sahrens 	if (zc->zc_nvlist_src != NULL &&
35615367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
35629643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
35635367Sahrens 		return (error);
35645367Sahrens 
3565789Sahrens 	fd = zc->zc_cookie;
3566789Sahrens 	fp = getf(fd);
35675367Sahrens 	if (fp == NULL) {
35685367Sahrens 		nvlist_free(props);
3569789Sahrens 		return (EBADF);
35705367Sahrens 	}
35715326Sek110237 
357211022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
357311022STom.Erickson@Sun.COM 
357410298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
357511022STom.Erickson@Sun.COM 		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
357611022STom.Erickson@Sun.COM 		    !dsl_prop_get_hasrecvd(os)) {
357711022STom.Erickson@Sun.COM 			first_recvd_props = B_TRUE;
357811022STom.Erickson@Sun.COM 		}
357911022STom.Erickson@Sun.COM 
35806689Smaybee 		/*
358111022STom.Erickson@Sun.COM 		 * If new received properties are supplied, they are to
358211022STom.Erickson@Sun.COM 		 * completely replace the existing received properties, so stash
358311022STom.Erickson@Sun.COM 		 * away the existing ones.
35846689Smaybee 		 */
358511022STom.Erickson@Sun.COM 		if (dsl_prop_get_received(os, &origprops) == 0) {
358611022STom.Erickson@Sun.COM 			nvlist_t *errlist = NULL;
358711022STom.Erickson@Sun.COM 			/*
358811022STom.Erickson@Sun.COM 			 * Don't bother writing a property if its value won't
358911022STom.Erickson@Sun.COM 			 * change (and avoid the unnecessary security checks).
359011022STom.Erickson@Sun.COM 			 *
359111022STom.Erickson@Sun.COM 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
359211022STom.Erickson@Sun.COM 			 * special case where we blow away all local properties
359311022STom.Erickson@Sun.COM 			 * regardless.
359411022STom.Erickson@Sun.COM 			 */
359511022STom.Erickson@Sun.COM 			if (!first_recvd_props)
359611022STom.Erickson@Sun.COM 				props_reduce(props, origprops);
359711022STom.Erickson@Sun.COM 			if (zfs_check_clearable(tofs, origprops,
359811022STom.Erickson@Sun.COM 			    &errlist) != 0)
359911022STom.Erickson@Sun.COM 				(void) nvlist_merge(errors, errlist, 0);
360011022STom.Erickson@Sun.COM 			nvlist_free(errlist);
360111022STom.Erickson@Sun.COM 		}
360210298SMatthew.Ahrens@Sun.COM 
360310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
36045326Sek110237 	}
36055326Sek110237 
36065367Sahrens 	if (zc->zc_string[0]) {
360710298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
36086689Smaybee 		if (error)
36096689Smaybee 			goto out;
36105367Sahrens 	}
36115367Sahrens 
361211007SLori.Alt@Sun.COM 	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
361311007SLori.Alt@Sun.COM 	    &zc->zc_begin_record, force, origin, &drc);
36145367Sahrens 	if (origin)
361510298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
36166689Smaybee 	if (error)
36176689Smaybee 		goto out;
36185326Sek110237 
36195326Sek110237 	/*
362011022STom.Erickson@Sun.COM 	 * Set properties before we receive the stream so that they are applied
362111022STom.Erickson@Sun.COM 	 * to the new data. Note that we must call dmu_recv_stream() if
362211022STom.Erickson@Sun.COM 	 * dmu_recv_begin() succeeds.
36235326Sek110237 	 */
36245367Sahrens 	if (props) {
362511022STom.Erickson@Sun.COM 		nvlist_t *errlist;
362611022STom.Erickson@Sun.COM 
362711022STom.Erickson@Sun.COM 		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
362811022STom.Erickson@Sun.COM 			if (drc.drc_newfs) {
362911022STom.Erickson@Sun.COM 				if (spa_version(os->os_spa) >=
363011022STom.Erickson@Sun.COM 				    SPA_VERSION_RECVD_PROPS)
363111022STom.Erickson@Sun.COM 					first_recvd_props = B_TRUE;
363211022STom.Erickson@Sun.COM 			} else if (origprops != NULL) {
363311022STom.Erickson@Sun.COM 				if (clear_received_props(os, tofs, origprops,
363411022STom.Erickson@Sun.COM 				    first_recvd_props ? NULL : props) != 0)
363511022STom.Erickson@Sun.COM 					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
363611022STom.Erickson@Sun.COM 			} else {
363711022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
363811022STom.Erickson@Sun.COM 			}
363911022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
364011022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
364111022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
364211022STom.Erickson@Sun.COM 		}
364311022STom.Erickson@Sun.COM 
364411022STom.Erickson@Sun.COM 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
364511022STom.Erickson@Sun.COM 		    props, &errlist);
364611022STom.Erickson@Sun.COM 		(void) nvlist_merge(errors, errlist, 0);
364711022STom.Erickson@Sun.COM 		nvlist_free(errlist);
364811022STom.Erickson@Sun.COM 	}
364911022STom.Erickson@Sun.COM 
365011022STom.Erickson@Sun.COM 	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
36516689Smaybee 		/*
365211022STom.Erickson@Sun.COM 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
365311022STom.Erickson@Sun.COM 		 * size or supplied an invalid address.
36546689Smaybee 		 */
365511022STom.Erickson@Sun.COM 		props_error = EINVAL;
36565367Sahrens 	}
36575367Sahrens 
36585367Sahrens 	off = fp->f_offset;
365912527SChris.Kirby@oracle.com 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
366012527SChris.Kirby@oracle.com 	    &zc->zc_action_handle);
36615367Sahrens 
366210204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
366310204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
366410204SMatthew.Ahrens@Sun.COM 
366510204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
366610204SMatthew.Ahrens@Sun.COM 			/* online recv */
366710204SMatthew.Ahrens@Sun.COM 			int end_err;
366810298SMatthew.Ahrens@Sun.COM 
366910298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
367010204SMatthew.Ahrens@Sun.COM 			/*
367110204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
367210204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
367310204SMatthew.Ahrens@Sun.COM 			 */
367410204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
367511812SGeorge.Wilson@Sun.COM 			if (error == 0)
367611812SGeorge.Wilson@Sun.COM 				error = zfs_resume_fs(zfsvfs, tofs);
367710204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
367810204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
367910204SMatthew.Ahrens@Sun.COM 		} else {
36806689Smaybee 			error = dmu_recv_end(&drc);
36815367Sahrens 		}
36826083Sek110237 	}
36835367Sahrens 
36845367Sahrens 	zc->zc_cookie = off - fp->f_offset;
36855367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
36865367Sahrens 		fp->f_offset = off;
36872885Sahrens 
368811022STom.Erickson@Sun.COM #ifdef	DEBUG
368911022STom.Erickson@Sun.COM 	if (zfs_ioc_recv_inject_err) {
369011022STom.Erickson@Sun.COM 		zfs_ioc_recv_inject_err = B_FALSE;
369111022STom.Erickson@Sun.COM 		error = 1;
369211022STom.Erickson@Sun.COM 	}
369311022STom.Erickson@Sun.COM #endif
36946689Smaybee 	/*
36956689Smaybee 	 * On error, restore the original props.
36966689Smaybee 	 */
36976689Smaybee 	if (error && props) {
369811022STom.Erickson@Sun.COM 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
369911022STom.Erickson@Sun.COM 			if (clear_received_props(os, tofs, props, NULL) != 0) {
370011022STom.Erickson@Sun.COM 				/*
370111022STom.Erickson@Sun.COM 				 * We failed to clear the received properties.
370211022STom.Erickson@Sun.COM 				 * Since we may have left a $recvd value on the
370311022STom.Erickson@Sun.COM 				 * system, we can't clear the $hasrecvd flag.
370411022STom.Erickson@Sun.COM 				 */
370511022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NORESTORE;
370611022STom.Erickson@Sun.COM 			} else if (first_recvd_props) {
370711022STom.Erickson@Sun.COM 				dsl_prop_unset_hasrecvd(os);
370811022STom.Erickson@Sun.COM 			}
370911022STom.Erickson@Sun.COM 			dmu_objset_rele(os, FTAG);
371011022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
371111022STom.Erickson@Sun.COM 			/* We failed to clear the received properties. */
371211022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
371311022STom.Erickson@Sun.COM 		}
371411022STom.Erickson@Sun.COM 
371511022STom.Erickson@Sun.COM 		if (origprops == NULL && !drc.drc_newfs) {
371611022STom.Erickson@Sun.COM 			/* We failed to stash the original properties. */
371711022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
371811022STom.Erickson@Sun.COM 		}
371911022STom.Erickson@Sun.COM 
372011022STom.Erickson@Sun.COM 		/*
372111022STom.Erickson@Sun.COM 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
372211022STom.Erickson@Sun.COM 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
372311022STom.Erickson@Sun.COM 		 * explictly if we're restoring local properties cleared in the
372411022STom.Erickson@Sun.COM 		 * first new-style receive.
372511022STom.Erickson@Sun.COM 		 */
372611022STom.Erickson@Sun.COM 		if (origprops != NULL &&
372711022STom.Erickson@Sun.COM 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
372811022STom.Erickson@Sun.COM 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
372911022STom.Erickson@Sun.COM 		    origprops, NULL) != 0) {
373011022STom.Erickson@Sun.COM 			/*
373111022STom.Erickson@Sun.COM 			 * We stashed the original properties but failed to
373211022STom.Erickson@Sun.COM 			 * restore them.
373311022STom.Erickson@Sun.COM 			 */
373411022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
373511022STom.Erickson@Sun.COM 		}
37366689Smaybee 	}
37376689Smaybee out:
37386689Smaybee 	nvlist_free(props);
37396689Smaybee 	nvlist_free(origprops);
374011022STom.Erickson@Sun.COM 	nvlist_free(errors);
3741789Sahrens 	releasef(fd);
374211022STom.Erickson@Sun.COM 
374311022STom.Erickson@Sun.COM 	if (error == 0)
374411022STom.Erickson@Sun.COM 		error = props_error;
374511022STom.Erickson@Sun.COM 
3746789Sahrens 	return (error);
3747789Sahrens }
3748789Sahrens 
37495367Sahrens /*
37505367Sahrens  * inputs:
37515367Sahrens  * zc_name	name of snapshot to send
37525367Sahrens  * zc_cookie	file descriptor to send stream to
375312786SChris.Kirby@oracle.com  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
375412786SChris.Kirby@oracle.com  * zc_sendobj	objsetid of snapshot to send
375512786SChris.Kirby@oracle.com  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
37565367Sahrens  *
37575367Sahrens  * outputs: none
37585367Sahrens  */
3759789Sahrens static int
37605367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
3761789Sahrens {
3762789Sahrens 	objset_t *fromsnap = NULL;
3763789Sahrens 	objset_t *tosnap;
3764789Sahrens 	file_t *fp;
3765789Sahrens 	int error;
37665367Sahrens 	offset_t off;
376712786SChris.Kirby@oracle.com 	dsl_dataset_t *ds;
376812786SChris.Kirby@oracle.com 	dsl_dataset_t *dsfrom = NULL;
376912786SChris.Kirby@oracle.com 	spa_t *spa;
377012786SChris.Kirby@oracle.com 	dsl_pool_t *dp;
377112786SChris.Kirby@oracle.com 
377212786SChris.Kirby@oracle.com 	error = spa_open(zc->zc_name, &spa, FTAG);
3773789Sahrens 	if (error)
3774789Sahrens 		return (error);
3775789Sahrens 
377612786SChris.Kirby@oracle.com 	dp = spa_get_dsl(spa);
377712786SChris.Kirby@oracle.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
377812786SChris.Kirby@oracle.com 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
377912786SChris.Kirby@oracle.com 	rw_exit(&dp->dp_config_rwlock);
378012786SChris.Kirby@oracle.com 	if (error) {
378112786SChris.Kirby@oracle.com 		spa_close(spa, FTAG);
378212786SChris.Kirby@oracle.com 		return (error);
378312786SChris.Kirby@oracle.com 	}
378412786SChris.Kirby@oracle.com 
378512786SChris.Kirby@oracle.com 	error = dmu_objset_from_ds(ds, &tosnap);
378612786SChris.Kirby@oracle.com 	if (error) {
378712786SChris.Kirby@oracle.com 		dsl_dataset_rele(ds, FTAG);
378812786SChris.Kirby@oracle.com 		spa_close(spa, FTAG);
378912786SChris.Kirby@oracle.com 		return (error);
379012786SChris.Kirby@oracle.com 	}
379112786SChris.Kirby@oracle.com 
379212786SChris.Kirby@oracle.com 	if (zc->zc_fromobj != 0) {
379312786SChris.Kirby@oracle.com 		rw_enter(&dp->dp_config_rwlock, RW_READER);
379412786SChris.Kirby@oracle.com 		error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
379512786SChris.Kirby@oracle.com 		rw_exit(&dp->dp_config_rwlock);
379612786SChris.Kirby@oracle.com 		spa_close(spa, FTAG);
3797789Sahrens 		if (error) {
379812786SChris.Kirby@oracle.com 			dsl_dataset_rele(ds, FTAG);
3799789Sahrens 			return (error);
3800789Sahrens 		}
380112786SChris.Kirby@oracle.com 		error = dmu_objset_from_ds(dsfrom, &fromsnap);
380212786SChris.Kirby@oracle.com 		if (error) {
380312786SChris.Kirby@oracle.com 			dsl_dataset_rele(dsfrom, FTAG);
380412786SChris.Kirby@oracle.com 			dsl_dataset_rele(ds, FTAG);
380512786SChris.Kirby@oracle.com 			return (error);
380612786SChris.Kirby@oracle.com 		}
380712786SChris.Kirby@oracle.com 	} else {
380812786SChris.Kirby@oracle.com 		spa_close(spa, FTAG);
3809789Sahrens 	}
3810789Sahrens 
3811789Sahrens 	fp = getf(zc->zc_cookie);
3812789Sahrens 	if (fp == NULL) {
381312786SChris.Kirby@oracle.com 		dsl_dataset_rele(ds, FTAG);
381412786SChris.Kirby@oracle.com 		if (dsfrom)
381512786SChris.Kirby@oracle.com 			dsl_dataset_rele(dsfrom, FTAG);
3816789Sahrens 		return (EBADF);
3817789Sahrens 	}
3818789Sahrens 
38195367Sahrens 	off = fp->f_offset;
38205367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
38215367Sahrens 
38225367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
38235367Sahrens 		fp->f_offset = off;
3824789Sahrens 	releasef(zc->zc_cookie);
382512786SChris.Kirby@oracle.com 	if (dsfrom)
382612786SChris.Kirby@oracle.com 		dsl_dataset_rele(dsfrom, FTAG);
382712786SChris.Kirby@oracle.com 	dsl_dataset_rele(ds, FTAG);
3828789Sahrens 	return (error);
3829789Sahrens }
3830789Sahrens 
38311544Seschrock static int
38321544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
38331544Seschrock {
38341544Seschrock 	int id, error;
38351544Seschrock 
38361544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
38371544Seschrock 	    &zc->zc_inject_record);
38381544Seschrock 
38391544Seschrock 	if (error == 0)
38401544Seschrock 		zc->zc_guid = (uint64_t)id;
38411544Seschrock 
38421544Seschrock 	return (error);
38431544Seschrock }
38441544Seschrock 
38451544Seschrock static int
38461544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
38471544Seschrock {
38481544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
38491544Seschrock }
38501544Seschrock 
38511544Seschrock static int
38521544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
38531544Seschrock {
38541544Seschrock 	int id = (int)zc->zc_guid;
38551544Seschrock 	int error;
38561544Seschrock 
38571544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
38581544Seschrock 	    &zc->zc_inject_record);
38591544Seschrock 
38601544Seschrock 	zc->zc_guid = id;
38611544Seschrock 
38621544Seschrock 	return (error);
38631544Seschrock }
38641544Seschrock 
38651544Seschrock static int
38661544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
38671544Seschrock {
38681544Seschrock 	spa_t *spa;
38691544Seschrock 	int error;
38702676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
38711544Seschrock 
38721544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
38731544Seschrock 		return (error);
38741544Seschrock 
38752676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
38761544Seschrock 	    &count);
38771544Seschrock 	if (error == 0)
38782676Seschrock 		zc->zc_nvlist_dst_size = count;
38791544Seschrock 	else
38802676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
38811544Seschrock 
38821544Seschrock 	spa_close(spa, FTAG);
38831544Seschrock 
38841544Seschrock 	return (error);
38851544Seschrock }
38861544Seschrock 
38871544Seschrock static int
38881544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
38891544Seschrock {
38901544Seschrock 	spa_t *spa;
38911544Seschrock 	vdev_t *vd;
38921544Seschrock 	int error;
38931544Seschrock 
38947294Sperrin 	/*
38957294Sperrin 	 * On zpool clear we also fix up missing slogs
38967294Sperrin 	 */
38977294Sperrin 	mutex_enter(&spa_namespace_lock);
38987294Sperrin 	spa = spa_lookup(zc->zc_name);
38997294Sperrin 	if (spa == NULL) {
39007294Sperrin 		mutex_exit(&spa_namespace_lock);
39017294Sperrin 		return (EIO);
39027294Sperrin 	}
390310922SJeff.Bonwick@Sun.COM 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
39047294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
390510922SJeff.Bonwick@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
39067294Sperrin 	}
390710921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
39087294Sperrin 	mutex_exit(&spa_namespace_lock);
39097294Sperrin 
391011727SVictor.Latushkin@Sun.COM 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
391110921STim.Haley@Sun.COM 		error = spa_open(zc->zc_name, &spa, FTAG);
391210921STim.Haley@Sun.COM 	} else {
391310921STim.Haley@Sun.COM 		nvlist_t *policy;
391410921STim.Haley@Sun.COM 		nvlist_t *config = NULL;
391510921STim.Haley@Sun.COM 
391610921STim.Haley@Sun.COM 		if (zc->zc_nvlist_src == NULL)
391710921STim.Haley@Sun.COM 			return (EINVAL);
391810921STim.Haley@Sun.COM 
391910921STim.Haley@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
392010921STim.Haley@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
392110921STim.Haley@Sun.COM 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
392210921STim.Haley@Sun.COM 			    policy, &config);
392310921STim.Haley@Sun.COM 			if (config != NULL) {
392412949SGeorge.Wilson@Sun.COM 				int err;
392512949SGeorge.Wilson@Sun.COM 
392612949SGeorge.Wilson@Sun.COM 				if ((err = put_nvlist(zc, config)) != 0)
392712949SGeorge.Wilson@Sun.COM 					error = err;
392810921STim.Haley@Sun.COM 				nvlist_free(config);
392910921STim.Haley@Sun.COM 			}
393010921STim.Haley@Sun.COM 			nvlist_free(policy);
393110921STim.Haley@Sun.COM 		}
393210921STim.Haley@Sun.COM 	}
393310921STim.Haley@Sun.COM 
393410921STim.Haley@Sun.COM 	if (error)
39351544Seschrock 		return (error);
39361544Seschrock 
393710685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
39381544Seschrock 
39392676Seschrock 	if (zc->zc_guid == 0) {
39401544Seschrock 		vd = NULL;
39416643Seschrock 	} else {
39426643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
39435450Sbrendan 		if (vd == NULL) {
39447754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
39455450Sbrendan 			spa_close(spa, FTAG);
39465450Sbrendan 			return (ENODEV);
39475450Sbrendan 		}
39481544Seschrock 	}
39491544Seschrock 
39507754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
39517754SJeff.Bonwick@Sun.COM 
39527754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
39537754SJeff.Bonwick@Sun.COM 
39547754SJeff.Bonwick@Sun.COM 	/*
39557754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
39567754SJeff.Bonwick@Sun.COM 	 */
39579234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
39589234SGeorge.Wilson@Sun.COM 		error = EIO;
39591544Seschrock 
39601544Seschrock 	spa_close(spa, FTAG);
39611544Seschrock 
39629234SGeorge.Wilson@Sun.COM 	return (error);
39631544Seschrock }
39641544Seschrock 
39655367Sahrens /*
39665367Sahrens  * inputs:
39675367Sahrens  * zc_name	name of filesystem
39685367Sahrens  * zc_value	name of origin snapshot
39695367Sahrens  *
397010588SEric.Taylor@Sun.COM  * outputs:
397110588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
39725367Sahrens  */
39731544Seschrock static int
39742082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
39752082Seschrock {
39762417Sahrens 	char *cp;
39772417Sahrens 
39782417Sahrens 	/*
39792417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
39802417Sahrens 	 * it's easier.
39812417Sahrens 	 */
39822676Seschrock 	cp = strchr(zc->zc_value, '@');
39832417Sahrens 	if (cp)
39842417Sahrens 		*cp = '\0';
39852676Seschrock 	(void) dmu_objset_find(zc->zc_value,
39862417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
398710588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
39882082Seschrock }
39892082Seschrock 
39904543Smarks /*
39919396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
39929396SMatthew.Ahrens@Sun.COM  *
39939396SMatthew.Ahrens@Sun.COM  * inputs:
39949396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
39959396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
39969396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
39979396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
39989396SMatthew.Ahrens@Sun.COM  *
39999396SMatthew.Ahrens@Sun.COM  * outputs:
40009396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
40019396SMatthew.Ahrens@Sun.COM  */
40029396SMatthew.Ahrens@Sun.COM static int
40039396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
40049396SMatthew.Ahrens@Sun.COM {
40059396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
40069396SMatthew.Ahrens@Sun.COM 	int error;
40079396SMatthew.Ahrens@Sun.COM 
40089396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
40099396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
40109396SMatthew.Ahrens@Sun.COM 
401112620SMark.Shellenbaum@Oracle.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
40129396SMatthew.Ahrens@Sun.COM 	if (error)
40139396SMatthew.Ahrens@Sun.COM 		return (error);
40149396SMatthew.Ahrens@Sun.COM 
40159396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
40169396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
40179396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
40189396SMatthew.Ahrens@Sun.COM 
40199396SMatthew.Ahrens@Sun.COM 	return (error);
40209396SMatthew.Ahrens@Sun.COM }
40219396SMatthew.Ahrens@Sun.COM 
40229396SMatthew.Ahrens@Sun.COM /*
40239396SMatthew.Ahrens@Sun.COM  * inputs:
40249396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
40259396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
40269396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
40279396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
40289396SMatthew.Ahrens@Sun.COM  *
40299396SMatthew.Ahrens@Sun.COM  * outputs:
40309396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
40319396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
40329396SMatthew.Ahrens@Sun.COM  */
40339396SMatthew.Ahrens@Sun.COM static int
40349396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
40359396SMatthew.Ahrens@Sun.COM {
40369396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
403711933STim.Haley@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
403811933STim.Haley@Sun.COM 
403911933STim.Haley@Sun.COM 	if (bufsize <= 0)
404011933STim.Haley@Sun.COM 		return (ENOMEM);
404111933STim.Haley@Sun.COM 
404212620SMark.Shellenbaum@Oracle.COM 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
40439396SMatthew.Ahrens@Sun.COM 	if (error)
40449396SMatthew.Ahrens@Sun.COM 		return (error);
40459396SMatthew.Ahrens@Sun.COM 
40469396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
40479396SMatthew.Ahrens@Sun.COM 
40489396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
40499396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
40509396SMatthew.Ahrens@Sun.COM 
40519396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
40529396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
40539396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
40549396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
40559396SMatthew.Ahrens@Sun.COM 	}
40569396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
40579396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
40589396SMatthew.Ahrens@Sun.COM 
40599396SMatthew.Ahrens@Sun.COM 	return (error);
40609396SMatthew.Ahrens@Sun.COM }
40619396SMatthew.Ahrens@Sun.COM 
40629396SMatthew.Ahrens@Sun.COM /*
40639396SMatthew.Ahrens@Sun.COM  * inputs:
40649396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
40659396SMatthew.Ahrens@Sun.COM  *
40669396SMatthew.Ahrens@Sun.COM  * outputs:
40679396SMatthew.Ahrens@Sun.COM  * none
40689396SMatthew.Ahrens@Sun.COM  */
40699396SMatthew.Ahrens@Sun.COM static int
40709396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
40719396SMatthew.Ahrens@Sun.COM {
40729396SMatthew.Ahrens@Sun.COM 	objset_t *os;
407311422SMark.Musante@Sun.COM 	int error = 0;
40749396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
40759396SMatthew.Ahrens@Sun.COM 
40769396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
407710298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
40789396SMatthew.Ahrens@Sun.COM 			/*
40799396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
40809396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
40819396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
40829396SMatthew.Ahrens@Sun.COM 			 */
408310298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
408410298SMatthew.Ahrens@Sun.COM 			if (error == 0)
408510298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
40869396SMatthew.Ahrens@Sun.COM 		}
40879396SMatthew.Ahrens@Sun.COM 		if (error == 0)
40889396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
40899396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
40909396SMatthew.Ahrens@Sun.COM 	} else {
409110298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
409210298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
40939396SMatthew.Ahrens@Sun.COM 		if (error)
40949396SMatthew.Ahrens@Sun.COM 			return (error);
40959396SMatthew.Ahrens@Sun.COM 
40969396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
409710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
40989396SMatthew.Ahrens@Sun.COM 	}
40999396SMatthew.Ahrens@Sun.COM 
41009396SMatthew.Ahrens@Sun.COM 	return (error);
41019396SMatthew.Ahrens@Sun.COM }
41029396SMatthew.Ahrens@Sun.COM 
41039396SMatthew.Ahrens@Sun.COM /*
41044543Smarks  * We don't want to have a hard dependency
41054543Smarks  * against some special symbols in sharefs
41065331Samw  * nfs, and smbsrv.  Determine them if needed when
41074543Smarks  * the first file system is shared.
41085331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
41094543Smarks  */
41105331Samw int (*znfsexport_fs)(void *arg);
41114543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
41125331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
41135331Samw 
41145331Samw int zfs_nfsshare_inited;
41155331Samw int zfs_smbshare_inited;
41165331Samw 
41174543Smarks ddi_modhandle_t nfs_mod;
41184543Smarks ddi_modhandle_t sharefs_mod;
41195331Samw ddi_modhandle_t smbsrv_mod;
41204543Smarks kmutex_t zfs_share_lock;
41214543Smarks 
41224543Smarks static int
41235331Samw zfs_init_sharefs()
41245331Samw {
41255331Samw 	int error;
41265331Samw 
41275331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
41285331Samw 	/* Both NFS and SMB shares also require sharetab support. */
41295331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
41305331Samw 	    ddi_modopen("fs/sharefs",
41315331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
41325331Samw 		return (ENOSYS);
41335331Samw 	}
41345331Samw 	if (zshare_fs == NULL && ((zshare_fs =
41355331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
41365331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
41375331Samw 		return (ENOSYS);
41385331Samw 	}
41395331Samw 	return (0);
41405331Samw }
41415331Samw 
41425331Samw static int
41434543Smarks zfs_ioc_share(zfs_cmd_t *zc)
41444543Smarks {
41454543Smarks 	int error;
41464543Smarks 	int opcode;
41474543Smarks 
41485331Samw 	switch (zc->zc_share.z_sharetype) {
41495331Samw 	case ZFS_SHARE_NFS:
41505331Samw 	case ZFS_UNSHARE_NFS:
41515331Samw 		if (zfs_nfsshare_inited == 0) {
41525331Samw 			mutex_enter(&zfs_share_lock);
41535331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
41545331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
41555331Samw 				mutex_exit(&zfs_share_lock);
41565331Samw 				return (ENOSYS);
41575331Samw 			}
41585331Samw 			if (znfsexport_fs == NULL &&
41595331Samw 			    ((znfsexport_fs = (int (*)(void *))
41605331Samw 			    ddi_modsym(nfs_mod,
41615331Samw 			    "nfs_export", &error)) == NULL)) {
41625331Samw 				mutex_exit(&zfs_share_lock);
41635331Samw 				return (ENOSYS);
41645331Samw 			}
41655331Samw 			error = zfs_init_sharefs();
41665331Samw 			if (error) {
41675331Samw 				mutex_exit(&zfs_share_lock);
41685331Samw 				return (ENOSYS);
41695331Samw 			}
41705331Samw 			zfs_nfsshare_inited = 1;
41714543Smarks 			mutex_exit(&zfs_share_lock);
41724543Smarks 		}
41735331Samw 		break;
41745331Samw 	case ZFS_SHARE_SMB:
41755331Samw 	case ZFS_UNSHARE_SMB:
41765331Samw 		if (zfs_smbshare_inited == 0) {
41775331Samw 			mutex_enter(&zfs_share_lock);
41785331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
41795331Samw 			    ddi_modopen("drv/smbsrv",
41805331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
41815331Samw 				mutex_exit(&zfs_share_lock);
41825331Samw 				return (ENOSYS);
41835331Samw 			}
41845331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
41855331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
41866139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
41875331Samw 				mutex_exit(&zfs_share_lock);
41885331Samw 				return (ENOSYS);
41895331Samw 			}
41905331Samw 			error = zfs_init_sharefs();
41915331Samw 			if (error) {
41925331Samw 				mutex_exit(&zfs_share_lock);
41935331Samw 				return (ENOSYS);
41945331Samw 			}
41955331Samw 			zfs_smbshare_inited = 1;
41964543Smarks 			mutex_exit(&zfs_share_lock);
41974543Smarks 		}
41985331Samw 		break;
41995331Samw 	default:
42005331Samw 		return (EINVAL);
42014543Smarks 	}
42024543Smarks 
42035331Samw 	switch (zc->zc_share.z_sharetype) {
42045331Samw 	case ZFS_SHARE_NFS:
42055331Samw 	case ZFS_UNSHARE_NFS:
42065331Samw 		if (error =
42075331Samw 		    znfsexport_fs((void *)
42085331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
42095331Samw 			return (error);
42105331Samw 		break;
42115331Samw 	case ZFS_SHARE_SMB:
42125331Samw 	case ZFS_UNSHARE_SMB:
42135331Samw 		if (error = zsmbexport_fs((void *)
42145331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
42155331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
42168845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
42175331Samw 			return (error);
42185331Samw 		}
42195331Samw 		break;
42205331Samw 	}
42215331Samw 
42225331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
42235331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
42244543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
42254543Smarks 
42265331Samw 	/*
42275331Samw 	 * Add or remove share from sharetab
42285331Samw 	 */
42294543Smarks 	error = zshare_fs(opcode,
42304543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
42314543Smarks 	    zc->zc_share.z_sharemax);
42324543Smarks 
42334543Smarks 	return (error);
42344543Smarks 
42354543Smarks }
42364543Smarks 
42378845Samw@Sun.COM ace_t full_access[] = {
42388845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
42398845Samw@Sun.COM };
42408845Samw@Sun.COM 
42418845Samw@Sun.COM /*
4242*13043STim.Haley@Sun.COM  * inputs:
4243*13043STim.Haley@Sun.COM  * zc_name		name of containing filesystem
4244*13043STim.Haley@Sun.COM  * zc_obj		object # beyond which we want next in-use object #
4245*13043STim.Haley@Sun.COM  *
4246*13043STim.Haley@Sun.COM  * outputs:
4247*13043STim.Haley@Sun.COM  * zc_obj		next in-use object #
4248*13043STim.Haley@Sun.COM  */
4249*13043STim.Haley@Sun.COM static int
4250*13043STim.Haley@Sun.COM zfs_ioc_next_obj(zfs_cmd_t *zc)
4251*13043STim.Haley@Sun.COM {
4252*13043STim.Haley@Sun.COM 	objset_t *os = NULL;
4253*13043STim.Haley@Sun.COM 	int error;
4254*13043STim.Haley@Sun.COM 
4255*13043STim.Haley@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4256*13043STim.Haley@Sun.COM 	if (error)
4257*13043STim.Haley@Sun.COM 		return (error);
4258*13043STim.Haley@Sun.COM 
4259*13043STim.Haley@Sun.COM 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4260*13043STim.Haley@Sun.COM 	    os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4261*13043STim.Haley@Sun.COM 
4262*13043STim.Haley@Sun.COM 	dmu_objset_rele(os, FTAG);
4263*13043STim.Haley@Sun.COM 	return (error);
4264*13043STim.Haley@Sun.COM }
4265*13043STim.Haley@Sun.COM 
4266*13043STim.Haley@Sun.COM /*
4267*13043STim.Haley@Sun.COM  * inputs:
4268*13043STim.Haley@Sun.COM  * zc_name		name of filesystem
4269*13043STim.Haley@Sun.COM  * zc_value		prefix name for snapshot
4270*13043STim.Haley@Sun.COM  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
4271*13043STim.Haley@Sun.COM  *
4272*13043STim.Haley@Sun.COM  * outputs:
4273*13043STim.Haley@Sun.COM  */
4274*13043STim.Haley@Sun.COM static int
4275*13043STim.Haley@Sun.COM zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4276*13043STim.Haley@Sun.COM {
4277*13043STim.Haley@Sun.COM 	char *snap_name;
4278*13043STim.Haley@Sun.COM 	int error;
4279*13043STim.Haley@Sun.COM 
4280*13043STim.Haley@Sun.COM 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4281*13043STim.Haley@Sun.COM 	    (u_longlong_t)ddi_get_lbolt64());
4282*13043STim.Haley@Sun.COM 
4283*13043STim.Haley@Sun.COM 	if (strlen(snap_name) >= MAXNAMELEN) {
4284*13043STim.Haley@Sun.COM 		strfree(snap_name);
4285*13043STim.Haley@Sun.COM 		return (E2BIG);
4286*13043STim.Haley@Sun.COM 	}
4287*13043STim.Haley@Sun.COM 
4288*13043STim.Haley@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4289*13043STim.Haley@Sun.COM 	    NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4290*13043STim.Haley@Sun.COM 	if (error != 0) {
4291*13043STim.Haley@Sun.COM 		strfree(snap_name);
4292*13043STim.Haley@Sun.COM 		return (error);
4293*13043STim.Haley@Sun.COM 	}
4294*13043STim.Haley@Sun.COM 
4295*13043STim.Haley@Sun.COM 	(void) strcpy(zc->zc_value, snap_name);
4296*13043STim.Haley@Sun.COM 	strfree(snap_name);
4297*13043STim.Haley@Sun.COM 	return (0);
4298*13043STim.Haley@Sun.COM }
4299*13043STim.Haley@Sun.COM 
4300*13043STim.Haley@Sun.COM /*
4301*13043STim.Haley@Sun.COM  * inputs:
4302*13043STim.Haley@Sun.COM  * zc_name		name of "to" snapshot
4303*13043STim.Haley@Sun.COM  * zc_value		name of "from" snapshot
4304*13043STim.Haley@Sun.COM  * zc_cookie		file descriptor to write diff data on
4305*13043STim.Haley@Sun.COM  *
4306*13043STim.Haley@Sun.COM  * outputs:
4307*13043STim.Haley@Sun.COM  * dmu_diff_record_t's to the file descriptor
4308*13043STim.Haley@Sun.COM  */
4309*13043STim.Haley@Sun.COM static int
4310*13043STim.Haley@Sun.COM zfs_ioc_diff(zfs_cmd_t *zc)
4311*13043STim.Haley@Sun.COM {
4312*13043STim.Haley@Sun.COM 	objset_t *fromsnap;
4313*13043STim.Haley@Sun.COM 	objset_t *tosnap;
4314*13043STim.Haley@Sun.COM 	file_t *fp;
4315*13043STim.Haley@Sun.COM 	offset_t off;
4316*13043STim.Haley@Sun.COM 	int error;
4317*13043STim.Haley@Sun.COM 
4318*13043STim.Haley@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4319*13043STim.Haley@Sun.COM 	if (error)
4320*13043STim.Haley@Sun.COM 		return (error);
4321*13043STim.Haley@Sun.COM 
4322*13043STim.Haley@Sun.COM 	error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4323*13043STim.Haley@Sun.COM 	if (error) {
4324*13043STim.Haley@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
4325*13043STim.Haley@Sun.COM 		return (error);
4326*13043STim.Haley@Sun.COM 	}
4327*13043STim.Haley@Sun.COM 
4328*13043STim.Haley@Sun.COM 	fp = getf(zc->zc_cookie);
4329*13043STim.Haley@Sun.COM 	if (fp == NULL) {
4330*13043STim.Haley@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
4331*13043STim.Haley@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
4332*13043STim.Haley@Sun.COM 		return (EBADF);
4333*13043STim.Haley@Sun.COM 	}
4334*13043STim.Haley@Sun.COM 
4335*13043STim.Haley@Sun.COM 	off = fp->f_offset;
4336*13043STim.Haley@Sun.COM 
4337*13043STim.Haley@Sun.COM 	error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4338*13043STim.Haley@Sun.COM 
4339*13043STim.Haley@Sun.COM 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4340*13043STim.Haley@Sun.COM 		fp->f_offset = off;
4341*13043STim.Haley@Sun.COM 	releasef(zc->zc_cookie);
4342*13043STim.Haley@Sun.COM 
4343*13043STim.Haley@Sun.COM 	dmu_objset_rele(fromsnap, FTAG);
4344*13043STim.Haley@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
4345*13043STim.Haley@Sun.COM 	return (error);
4346*13043STim.Haley@Sun.COM }
4347*13043STim.Haley@Sun.COM 
4348*13043STim.Haley@Sun.COM /*
43498845Samw@Sun.COM  * Remove all ACL files in shares dir
43508845Samw@Sun.COM  */
43518845Samw@Sun.COM static int
43528845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
43538845Samw@Sun.COM {
43548845Samw@Sun.COM 	zap_cursor_t	zc;
43558845Samw@Sun.COM 	zap_attribute_t	zap;
43568845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
43578845Samw@Sun.COM 	int error;
43588845Samw@Sun.COM 
43598845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
43608845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
43618845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
43628845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
43638845Samw@Sun.COM 		    NULL, 0)) != 0)
43648845Samw@Sun.COM 			break;
43658845Samw@Sun.COM 	}
43668845Samw@Sun.COM 	zap_cursor_fini(&zc);
43678845Samw@Sun.COM 	return (error);
43688845Samw@Sun.COM }
43698845Samw@Sun.COM 
43708845Samw@Sun.COM static int
43718845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
43728845Samw@Sun.COM {
43738845Samw@Sun.COM 	vnode_t *vp;
43748845Samw@Sun.COM 	znode_t *dzp;
43758845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
43768845Samw@Sun.COM 	znode_t *sharedir;
43778845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
43788845Samw@Sun.COM 	nvlist_t *nvlist;
43798845Samw@Sun.COM 	char *src, *target;
43808845Samw@Sun.COM 	vattr_t vattr;
43818845Samw@Sun.COM 	vsecattr_t vsec;
43828845Samw@Sun.COM 	int error = 0;
43838845Samw@Sun.COM 
43848845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
43858845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
43868845Samw@Sun.COM 		return (error);
43878845Samw@Sun.COM 
43888845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
43898845Samw@Sun.COM 
43908845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
43918845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
43928845Samw@Sun.COM 	    zc->zc_name) != 0)) {
43938845Samw@Sun.COM 		VN_RELE(vp);
43948845Samw@Sun.COM 		return (EINVAL);
43958845Samw@Sun.COM 	}
43968845Samw@Sun.COM 
43978845Samw@Sun.COM 	dzp = VTOZ(vp);
43988845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
43998845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
44008845Samw@Sun.COM 
44019030SMark.Shellenbaum@Sun.COM 	/*
44029030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
44039030SMark.Shellenbaum@Sun.COM 	 */
44049030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
44059030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
44069030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
44079030SMark.Shellenbaum@Sun.COM 
44089030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
44099030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
44109030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
44119030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
44129030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
44139030SMark.Shellenbaum@Sun.COM 		if (error) {
44149030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
44159030SMark.Shellenbaum@Sun.COM 		} else {
44169030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
44179030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
44189030SMark.Shellenbaum@Sun.COM 		}
44199030SMark.Shellenbaum@Sun.COM 		if (error) {
44209030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
44219030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
44229030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
44239030SMark.Shellenbaum@Sun.COM 			return (error);
44249030SMark.Shellenbaum@Sun.COM 		}
44259030SMark.Shellenbaum@Sun.COM 	}
44269030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
44279030SMark.Shellenbaum@Sun.COM 
44289030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
44298845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
44309030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
44318845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
44328845Samw@Sun.COM 		return (error);
44338845Samw@Sun.COM 	}
44348845Samw@Sun.COM 
44358845Samw@Sun.COM 	switch (zc->zc_cookie) {
44368845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
44378845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
44388845Samw@Sun.COM 		vattr.va_type = VREG;
44398845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
44408845Samw@Sun.COM 		vattr.va_uid = 0;
44418845Samw@Sun.COM 		vattr.va_gid = 0;
44428845Samw@Sun.COM 
44438845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
44448845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
44458845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
44468845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
44478845Samw@Sun.COM 
44488845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
44498845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
44508845Samw@Sun.COM 		if (resourcevp)
44518845Samw@Sun.COM 			VN_RELE(resourcevp);
44528845Samw@Sun.COM 		break;
44538845Samw@Sun.COM 
44548845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
44558845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
44568845Samw@Sun.COM 		    NULL, 0);
44578845Samw@Sun.COM 		break;
44588845Samw@Sun.COM 
44598845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
44608845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
44619643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
44628845Samw@Sun.COM 			VN_RELE(vp);
44638845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
44648845Samw@Sun.COM 			return (error);
44658845Samw@Sun.COM 		}
44668845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
44678845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
44688845Samw@Sun.COM 		    &target)) {
44698845Samw@Sun.COM 			VN_RELE(vp);
44709179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
44718845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
447211422SMark.Musante@Sun.COM 			nvlist_free(nvlist);
44738845Samw@Sun.COM 			return (error);
44748845Samw@Sun.COM 		}
44758845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
44768845Samw@Sun.COM 		    kcred, NULL, 0);
44778845Samw@Sun.COM 		nvlist_free(nvlist);
44788845Samw@Sun.COM 		break;
44798845Samw@Sun.COM 
44808845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
44818845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
44828845Samw@Sun.COM 		break;
44838845Samw@Sun.COM 
44848845Samw@Sun.COM 	default:
44858845Samw@Sun.COM 		error = EINVAL;
44868845Samw@Sun.COM 		break;
44878845Samw@Sun.COM 	}
44888845Samw@Sun.COM 
44898845Samw@Sun.COM 	VN_RELE(vp);
44908845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
44918845Samw@Sun.COM 
44928845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
44938845Samw@Sun.COM 
44948845Samw@Sun.COM 	return (error);
44958845Samw@Sun.COM }
44968845Samw@Sun.COM 
44974543Smarks /*
449810242Schris.kirby@sun.com  * inputs:
449912527SChris.Kirby@oracle.com  * zc_name		name of filesystem
450012527SChris.Kirby@oracle.com  * zc_value		short name of snap
450112527SChris.Kirby@oracle.com  * zc_string		user-supplied tag for this hold
450212527SChris.Kirby@oracle.com  * zc_cookie		recursive flag
450312527SChris.Kirby@oracle.com  * zc_temphold		set if hold is temporary
450412527SChris.Kirby@oracle.com  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
450512786SChris.Kirby@oracle.com  * zc_sendobj		if non-zero, the objid for zc_name@zc_value
450612786SChris.Kirby@oracle.com  * zc_createtxg		if zc_sendobj is non-zero, snap must have zc_createtxg
450710242Schris.kirby@sun.com  *
450810242Schris.kirby@sun.com  * outputs:		none
450910242Schris.kirby@sun.com  */
451010242Schris.kirby@sun.com static int
451110242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
451210242Schris.kirby@sun.com {
451310242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
451412786SChris.Kirby@oracle.com 	spa_t *spa;
451512786SChris.Kirby@oracle.com 	dsl_pool_t *dp;
451612786SChris.Kirby@oracle.com 	dsl_dataset_t *ds;
451712786SChris.Kirby@oracle.com 	int error;
451812786SChris.Kirby@oracle.com 	minor_t minor = 0;
451910242Schris.kirby@sun.com 
452010242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
452110242Schris.kirby@sun.com 		return (EINVAL);
452210242Schris.kirby@sun.com 
452312786SChris.Kirby@oracle.com 	if (zc->zc_sendobj == 0) {
452412786SChris.Kirby@oracle.com 		return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
452512786SChris.Kirby@oracle.com 		    zc->zc_string, recursive, zc->zc_temphold,
452612786SChris.Kirby@oracle.com 		    zc->zc_cleanup_fd));
452712786SChris.Kirby@oracle.com 	}
452812786SChris.Kirby@oracle.com 
452912786SChris.Kirby@oracle.com 	if (recursive)
453012786SChris.Kirby@oracle.com 		return (EINVAL);
453112786SChris.Kirby@oracle.com 
453212786SChris.Kirby@oracle.com 	error = spa_open(zc->zc_name, &spa, FTAG);
453312786SChris.Kirby@oracle.com 	if (error)
453412786SChris.Kirby@oracle.com 		return (error);
453512786SChris.Kirby@oracle.com 
453612786SChris.Kirby@oracle.com 	dp = spa_get_dsl(spa);
453712786SChris.Kirby@oracle.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
453812786SChris.Kirby@oracle.com 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
453912786SChris.Kirby@oracle.com 	rw_exit(&dp->dp_config_rwlock);
454012786SChris.Kirby@oracle.com 	spa_close(spa, FTAG);
454112786SChris.Kirby@oracle.com 	if (error)
454212786SChris.Kirby@oracle.com 		return (error);
454312786SChris.Kirby@oracle.com 
454412786SChris.Kirby@oracle.com 	/*
454512786SChris.Kirby@oracle.com 	 * Until we have a hold on this snapshot, it's possible that
454612786SChris.Kirby@oracle.com 	 * zc_sendobj could've been destroyed and reused as part
454712786SChris.Kirby@oracle.com 	 * of a later txg.  Make sure we're looking at the right object.
454812786SChris.Kirby@oracle.com 	 */
454912786SChris.Kirby@oracle.com 	if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
455012786SChris.Kirby@oracle.com 		dsl_dataset_rele(ds, FTAG);
455112786SChris.Kirby@oracle.com 		return (ENOENT);
455212786SChris.Kirby@oracle.com 	}
455312786SChris.Kirby@oracle.com 
455412786SChris.Kirby@oracle.com 	if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
455512786SChris.Kirby@oracle.com 		error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
455612786SChris.Kirby@oracle.com 		if (error) {
455712786SChris.Kirby@oracle.com 			dsl_dataset_rele(ds, FTAG);
455812786SChris.Kirby@oracle.com 			return (error);
455912786SChris.Kirby@oracle.com 		}
456012786SChris.Kirby@oracle.com 	}
456112786SChris.Kirby@oracle.com 
456212786SChris.Kirby@oracle.com 	error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
456312786SChris.Kirby@oracle.com 	    zc->zc_temphold);
456412786SChris.Kirby@oracle.com 	if (minor != 0) {
456512786SChris.Kirby@oracle.com 		if (error == 0) {
456612786SChris.Kirby@oracle.com 			dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
456712786SChris.Kirby@oracle.com 			    minor);
456812786SChris.Kirby@oracle.com 		}
456912786SChris.Kirby@oracle.com 		zfs_onexit_fd_rele(zc->zc_cleanup_fd);
457012786SChris.Kirby@oracle.com 	}
457112786SChris.Kirby@oracle.com 	dsl_dataset_rele(ds, FTAG);
457212786SChris.Kirby@oracle.com 
457312786SChris.Kirby@oracle.com 	return (error);
457410242Schris.kirby@sun.com }
457510242Schris.kirby@sun.com 
457610242Schris.kirby@sun.com /*
457710242Schris.kirby@sun.com  * inputs:
457812527SChris.Kirby@oracle.com  * zc_name	name of dataset from which we're releasing a user hold
457910242Schris.kirby@sun.com  * zc_value	short name of snap
458012527SChris.Kirby@oracle.com  * zc_string	user-supplied tag for this hold
458110242Schris.kirby@sun.com  * zc_cookie	recursive flag
458210242Schris.kirby@sun.com  *
458312527SChris.Kirby@oracle.com  * outputs:	none
458410242Schris.kirby@sun.com  */
458510242Schris.kirby@sun.com static int
458610242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
458710242Schris.kirby@sun.com {
458810242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
458910242Schris.kirby@sun.com 
459010242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
459110242Schris.kirby@sun.com 		return (EINVAL);
459210242Schris.kirby@sun.com 
459310242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
459410242Schris.kirby@sun.com 	    zc->zc_string, recursive));
459510242Schris.kirby@sun.com }
459610242Schris.kirby@sun.com 
459710242Schris.kirby@sun.com /*
459810242Schris.kirby@sun.com  * inputs:
459910242Schris.kirby@sun.com  * zc_name		name of filesystem
460010242Schris.kirby@sun.com  *
460110242Schris.kirby@sun.com  * outputs:
460210242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
460310242Schris.kirby@sun.com  */
460410242Schris.kirby@sun.com static int
460510242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
460610242Schris.kirby@sun.com {
460710242Schris.kirby@sun.com 	nvlist_t *nvp;
460810242Schris.kirby@sun.com 	int error;
460910242Schris.kirby@sun.com 
461010242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
461110242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
461210242Schris.kirby@sun.com 		nvlist_free(nvp);
461310242Schris.kirby@sun.com 	}
461410242Schris.kirby@sun.com 
461510242Schris.kirby@sun.com 	return (error);
461610242Schris.kirby@sun.com }
461710242Schris.kirby@sun.com 
461810242Schris.kirby@sun.com /*
46194988Sek110237  * pool create, destroy, and export don't log the history as part of
46204988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
46214988Sek110237  * do the logging of those commands.
46224543Smarks  */
4623789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
46249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
46259234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
46279234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46299234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
46319234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
46339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
46359234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
46379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
463812296SLin.Ling@Sun.COM 	{ zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46399234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
46419234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
46439234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
46459234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46479234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46499234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
46519234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46539234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
46559234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
46579234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46589425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
46599425SEric.Schrock@Sun.COM 	    B_TRUE },
46609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
466111454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
46629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
46639234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
466511454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
46669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
466711454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
46689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
46699234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
46709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
46719234SGeorge.Wilson@Sun.COM 	    B_TRUE},
46729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
46739234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
46759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
46769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
46779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
46789234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46799234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
46809234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46819234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
46829234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46839234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
46849234SGeorge.Wilson@Sun.COM 	    B_FALSE },
46859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
46869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
46879234SGeorge.Wilson@Sun.COM 	    B_TRUE },
468811314Swilliam.gorrell@sun.com 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
468911314Swilliam.gorrell@sun.com 	    B_TRUE, B_TRUE },
46909234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
46919234SGeorge.Wilson@Sun.COM 	    B_TRUE },
4692*13043STim.Haley@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
46939234SGeorge.Wilson@Sun.COM 	    B_FALSE },
4694*13043STim.Haley@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
469510233SGeorge.Wilson@Sun.COM 	    B_TRUE },
46969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
46979234SGeorge.Wilson@Sun.COM 	    B_TRUE },
46989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
46999234SGeorge.Wilson@Sun.COM 	    B_FALSE },
47009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
47019234SGeorge.Wilson@Sun.COM 	    B_TRUE },
47029234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
47039234SGeorge.Wilson@Sun.COM 	    B_FALSE },
47049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
47059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
47069234SGeorge.Wilson@Sun.COM 	    B_TRUE },
47079234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
47089396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
47099396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
47109396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
47119396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
47129396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
47139396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
47149396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
471510242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
471610242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
471710242Schris.kirby@sun.com 	    B_TRUE },
471810242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
471911022STom.Erickson@Sun.COM 	    B_TRUE },
472011022STom.Erickson@Sun.COM 	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
472111422SMark.Musante@Sun.COM 	    B_FALSE },
472211422SMark.Musante@Sun.COM 	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4723*13043STim.Haley@Sun.COM 	    B_TRUE },
4724*13043STim.Haley@Sun.COM 	{ zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4725*13043STim.Haley@Sun.COM 	    B_FALSE },
4726*13043STim.Haley@Sun.COM 	{ zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, B_FALSE },
4727*13043STim.Haley@Sun.COM 	{ zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4728*13043STim.Haley@Sun.COM 	    B_FALSE, B_FALSE },
4729*13043STim.Haley@Sun.COM 	{ zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
473011422SMark.Musante@Sun.COM 	    B_TRUE }
4731789Sahrens };
4732789Sahrens 
47339234SGeorge.Wilson@Sun.COM int
47349234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
47359234SGeorge.Wilson@Sun.COM {
47369234SGeorge.Wilson@Sun.COM 	spa_t *spa;
47379234SGeorge.Wilson@Sun.COM 	int error;
47389234SGeorge.Wilson@Sun.COM 
47399234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
47409234SGeorge.Wilson@Sun.COM 
47419396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
47429234SGeorge.Wilson@Sun.COM 	if (error == 0) {
47439234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
47449234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
47459234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
47469234SGeorge.Wilson@Sun.COM 	}
47479234SGeorge.Wilson@Sun.COM 	return (error);
47489234SGeorge.Wilson@Sun.COM }
47499234SGeorge.Wilson@Sun.COM 
475012527SChris.Kirby@oracle.com /*
475112527SChris.Kirby@oracle.com  * Find a free minor number.
475212527SChris.Kirby@oracle.com  */
475312527SChris.Kirby@oracle.com minor_t
475412527SChris.Kirby@oracle.com zfsdev_minor_alloc(void)
475512527SChris.Kirby@oracle.com {
475612527SChris.Kirby@oracle.com 	static minor_t last_minor;
475712527SChris.Kirby@oracle.com 	minor_t m;
475812527SChris.Kirby@oracle.com 
475912527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
476012527SChris.Kirby@oracle.com 
476112527SChris.Kirby@oracle.com 	for (m = last_minor + 1; m != last_minor; m++) {
476212527SChris.Kirby@oracle.com 		if (m > ZFSDEV_MAX_MINOR)
476312527SChris.Kirby@oracle.com 			m = 1;
476412527SChris.Kirby@oracle.com 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
476512527SChris.Kirby@oracle.com 			last_minor = m;
476612527SChris.Kirby@oracle.com 			return (m);
476712527SChris.Kirby@oracle.com 		}
476812527SChris.Kirby@oracle.com 	}
476912527SChris.Kirby@oracle.com 
477012527SChris.Kirby@oracle.com 	return (0);
477112527SChris.Kirby@oracle.com }
477212527SChris.Kirby@oracle.com 
477312527SChris.Kirby@oracle.com static int
477412527SChris.Kirby@oracle.com zfs_ctldev_init(dev_t *devp)
477512527SChris.Kirby@oracle.com {
477612527SChris.Kirby@oracle.com 	minor_t minor;
477712527SChris.Kirby@oracle.com 	zfs_soft_state_t *zs;
477812527SChris.Kirby@oracle.com 
477912527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
478012527SChris.Kirby@oracle.com 	ASSERT(getminor(*devp) == 0);
478112527SChris.Kirby@oracle.com 
478212527SChris.Kirby@oracle.com 	minor = zfsdev_minor_alloc();
478312527SChris.Kirby@oracle.com 	if (minor == 0)
478412527SChris.Kirby@oracle.com 		return (ENXIO);
478512527SChris.Kirby@oracle.com 
478612527SChris.Kirby@oracle.com 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
478712527SChris.Kirby@oracle.com 		return (EAGAIN);
478812527SChris.Kirby@oracle.com 
478912527SChris.Kirby@oracle.com 	*devp = makedevice(getemajor(*devp), minor);
479012527SChris.Kirby@oracle.com 
479112527SChris.Kirby@oracle.com 	zs = ddi_get_soft_state(zfsdev_state, minor);
479212527SChris.Kirby@oracle.com 	zs->zss_type = ZSST_CTLDEV;
479312527SChris.Kirby@oracle.com 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
479412527SChris.Kirby@oracle.com 
479512527SChris.Kirby@oracle.com 	return (0);
479612527SChris.Kirby@oracle.com }
479712527SChris.Kirby@oracle.com 
479812527SChris.Kirby@oracle.com static void
479912527SChris.Kirby@oracle.com zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
480012527SChris.Kirby@oracle.com {
480112527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
480212527SChris.Kirby@oracle.com 
480312527SChris.Kirby@oracle.com 	zfs_onexit_destroy(zo);
480412527SChris.Kirby@oracle.com 	ddi_soft_state_free(zfsdev_state, minor);
480512527SChris.Kirby@oracle.com }
480612527SChris.Kirby@oracle.com 
480712527SChris.Kirby@oracle.com void *
480812527SChris.Kirby@oracle.com zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
480912527SChris.Kirby@oracle.com {
481012527SChris.Kirby@oracle.com 	zfs_soft_state_t *zp;
481112527SChris.Kirby@oracle.com 
481212527SChris.Kirby@oracle.com 	zp = ddi_get_soft_state(zfsdev_state, minor);
481312527SChris.Kirby@oracle.com 	if (zp == NULL || zp->zss_type != which)
481412527SChris.Kirby@oracle.com 		return (NULL);
481512527SChris.Kirby@oracle.com 
481612527SChris.Kirby@oracle.com 	return (zp->zss_data);
481712527SChris.Kirby@oracle.com }
481812527SChris.Kirby@oracle.com 
481912527SChris.Kirby@oracle.com static int
482012527SChris.Kirby@oracle.com zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
482112527SChris.Kirby@oracle.com {
482212527SChris.Kirby@oracle.com 	int error = 0;
482312527SChris.Kirby@oracle.com 
482412527SChris.Kirby@oracle.com 	if (getminor(*devp) != 0)
482512527SChris.Kirby@oracle.com 		return (zvol_open(devp, flag, otyp, cr));
482612527SChris.Kirby@oracle.com 
482712527SChris.Kirby@oracle.com 	/* This is the control device. Allocate a new minor if requested. */
482812527SChris.Kirby@oracle.com 	if (flag & FEXCL) {
482912527SChris.Kirby@oracle.com 		mutex_enter(&zfsdev_state_lock);
483012527SChris.Kirby@oracle.com 		error = zfs_ctldev_init(devp);
483112527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
483212527SChris.Kirby@oracle.com 	}
483312527SChris.Kirby@oracle.com 
483412527SChris.Kirby@oracle.com 	return (error);
483512527SChris.Kirby@oracle.com }
483612527SChris.Kirby@oracle.com 
483712527SChris.Kirby@oracle.com static int
483812527SChris.Kirby@oracle.com zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
483912527SChris.Kirby@oracle.com {
484012527SChris.Kirby@oracle.com 	zfs_onexit_t *zo;
484112527SChris.Kirby@oracle.com 	minor_t minor = getminor(dev);
484212527SChris.Kirby@oracle.com 
484312527SChris.Kirby@oracle.com 	if (minor == 0)
484412527SChris.Kirby@oracle.com 		return (0);
484512527SChris.Kirby@oracle.com 
484612527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
484712527SChris.Kirby@oracle.com 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
484812527SChris.Kirby@oracle.com 	if (zo == NULL) {
484912527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
485012527SChris.Kirby@oracle.com 		return (zvol_close(dev, flag, otyp, cr));
485112527SChris.Kirby@oracle.com 	}
485212527SChris.Kirby@oracle.com 	zfs_ctldev_destroy(zo, minor);
485312527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
485412527SChris.Kirby@oracle.com 
485512527SChris.Kirby@oracle.com 	return (0);
485612527SChris.Kirby@oracle.com }
485712527SChris.Kirby@oracle.com 
4858789Sahrens static int
4859789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4860789Sahrens {
4861789Sahrens 	zfs_cmd_t *zc;
4862789Sahrens 	uint_t vec;
48632199Sahrens 	int error, rc;
486412527SChris.Kirby@oracle.com 	minor_t minor = getminor(dev);
486512527SChris.Kirby@oracle.com 
486612527SChris.Kirby@oracle.com 	if (minor != 0 &&
486712527SChris.Kirby@oracle.com 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
4868789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4869789Sahrens 
4870789Sahrens 	vec = cmd - ZFS_IOC;
48714787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4872789Sahrens 
4873789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4874789Sahrens 		return (EINVAL);
4875789Sahrens 
4876789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4877789Sahrens 
48789643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
487911807SSam.Falkner@Sun.COM 	if (error != 0)
488011807SSam.Falkner@Sun.COM 		error = EFAULT;
4881789Sahrens 
488210588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
48834543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4884789Sahrens 
4885789Sahrens 	/*
4886789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
4887789Sahrens 	 * the lower layers.
4888789Sahrens 	 */
4889789Sahrens 	if (error == 0) {
4890789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
48919643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
4892789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
48934577Sahrens 		case POOL_NAME:
4894789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4895789Sahrens 				error = EINVAL;
48969234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
48979234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
48989234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4899789Sahrens 			break;
4900789Sahrens 
49014577Sahrens 		case DATASET_NAME:
4902789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4903789Sahrens 				error = EINVAL;
49049234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
49059234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
49069234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4907789Sahrens 			break;
49082856Snd150628 
49094577Sahrens 		case NO_NAME:
49102856Snd150628 			break;
4911789Sahrens 		}
4912789Sahrens 	}
4913789Sahrens 
4914789Sahrens 	if (error == 0)
4915789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
4916789Sahrens 
49179643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
49184543Smarks 	if (error == 0) {
491911807SSam.Falkner@Sun.COM 		if (rc != 0)
492011807SSam.Falkner@Sun.COM 			error = EFAULT;
49219396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
49224543Smarks 			zfs_log_history(zc);
49234543Smarks 	}
4924789Sahrens 
4925789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
4926789Sahrens 	return (error);
4927789Sahrens }
4928789Sahrens 
4929789Sahrens static int
4930789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4931789Sahrens {
4932789Sahrens 	if (cmd != DDI_ATTACH)
4933789Sahrens 		return (DDI_FAILURE);
4934789Sahrens 
4935789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4936789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
4937789Sahrens 		return (DDI_FAILURE);
4938789Sahrens 
4939789Sahrens 	zfs_dip = dip;
4940789Sahrens 
4941789Sahrens 	ddi_report_dev(dip);
4942789Sahrens 
4943789Sahrens 	return (DDI_SUCCESS);
4944789Sahrens }
4945789Sahrens 
4946789Sahrens static int
4947789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4948789Sahrens {
4949789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
4950789Sahrens 		return (DDI_FAILURE);
4951789Sahrens 
4952789Sahrens 	if (cmd != DDI_DETACH)
4953789Sahrens 		return (DDI_FAILURE);
4954789Sahrens 
4955789Sahrens 	zfs_dip = NULL;
4956789Sahrens 
4957789Sahrens 	ddi_prop_remove_all(dip);
4958789Sahrens 	ddi_remove_minor_node(dip, NULL);
4959789Sahrens 
4960789Sahrens 	return (DDI_SUCCESS);
4961789Sahrens }
4962789Sahrens 
4963789Sahrens /*ARGSUSED*/
4964789Sahrens static int
4965789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4966789Sahrens {
4967789Sahrens 	switch (infocmd) {
4968789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
4969789Sahrens 		*result = zfs_dip;
4970789Sahrens 		return (DDI_SUCCESS);
4971789Sahrens 
4972789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
4973849Sbonwick 		*result = (void *)0;
4974789Sahrens 		return (DDI_SUCCESS);
4975789Sahrens 	}
4976789Sahrens 
4977789Sahrens 	return (DDI_FAILURE);
4978789Sahrens }
4979789Sahrens 
4980789Sahrens /*
4981789Sahrens  * OK, so this is a little weird.
4982789Sahrens  *
4983789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
4984789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
4985789Sahrens  *
4986789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
4987789Sahrens  * so most of the standard driver entry points are in zvol.c.
4988789Sahrens  */
4989789Sahrens static struct cb_ops zfs_cb_ops = {
499012527SChris.Kirby@oracle.com 	zfsdev_open,	/* open */
499112527SChris.Kirby@oracle.com 	zfsdev_close,	/* close */
4992789Sahrens 	zvol_strategy,	/* strategy */
4993789Sahrens 	nodev,		/* print */
49946423Sgw25295 	zvol_dump,	/* dump */
4995789Sahrens 	zvol_read,	/* read */
4996789Sahrens 	zvol_write,	/* write */
4997789Sahrens 	zfsdev_ioctl,	/* ioctl */
4998789Sahrens 	nodev,		/* devmap */
4999789Sahrens 	nodev,		/* mmap */
5000789Sahrens 	nodev,		/* segmap */
5001789Sahrens 	nochpoll,	/* poll */
5002789Sahrens 	ddi_prop_op,	/* prop_op */
5003789Sahrens 	NULL,		/* streamtab */
5004789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
5005789Sahrens 	CB_REV,		/* version */
50063638Sbillm 	nodev,		/* async read */
50073638Sbillm 	nodev,		/* async write */
5008789Sahrens };
5009789Sahrens 
5010789Sahrens static struct dev_ops zfs_dev_ops = {
5011789Sahrens 	DEVO_REV,	/* version */
5012789Sahrens 	0,		/* refcnt */
5013789Sahrens 	zfs_info,	/* info */
5014789Sahrens 	nulldev,	/* identify */
5015789Sahrens 	nulldev,	/* probe */
5016789Sahrens 	zfs_attach,	/* attach */
5017789Sahrens 	zfs_detach,	/* detach */
5018789Sahrens 	nodev,		/* reset */
5019789Sahrens 	&zfs_cb_ops,	/* driver operations */
50207656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
50217656SSherry.Moore@Sun.COM 	NULL,		/* power */
50227656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
5023789Sahrens };
5024789Sahrens 
5025789Sahrens static struct modldrv zfs_modldrv = {
50267656SSherry.Moore@Sun.COM 	&mod_driverops,
50277656SSherry.Moore@Sun.COM 	"ZFS storage pool",
50287656SSherry.Moore@Sun.COM 	&zfs_dev_ops
5029789Sahrens };
5030789Sahrens 
5031789Sahrens static struct modlinkage modlinkage = {
5032789Sahrens 	MODREV_1,
5033789Sahrens 	(void *)&zfs_modlfs,
5034789Sahrens 	(void *)&zfs_modldrv,
5035789Sahrens 	NULL
5036789Sahrens };
5037789Sahrens 
50384720Sfr157268 
50394720Sfr157268 uint_t zfs_fsyncer_key;
50405326Sek110237 extern uint_t rrw_tsd_key;
50414720Sfr157268 
5042789Sahrens int
5043789Sahrens _init(void)
5044789Sahrens {
5045789Sahrens 	int error;
5046789Sahrens 
5047849Sbonwick 	spa_init(FREAD | FWRITE);
5048849Sbonwick 	zfs_init();
5049849Sbonwick 	zvol_init();
5050849Sbonwick 
5051849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
5052849Sbonwick 		zvol_fini();
5053849Sbonwick 		zfs_fini();
5054849Sbonwick 		spa_fini();
5055789Sahrens 		return (error);
5056849Sbonwick 	}
5057789Sahrens 
50584720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
50595326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
50604720Sfr157268 
5061789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5062789Sahrens 	ASSERT(error == 0);
50634543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5064789Sahrens 
5065789Sahrens 	return (0);
5066789Sahrens }
5067789Sahrens 
5068789Sahrens int
5069789Sahrens _fini(void)
5070789Sahrens {
5071789Sahrens 	int error;
5072789Sahrens 
50731544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5074789Sahrens 		return (EBUSY);
5075789Sahrens 
5076789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
5077789Sahrens 		return (error);
5078789Sahrens 
5079789Sahrens 	zvol_fini();
5080789Sahrens 	zfs_fini();
5081789Sahrens 	spa_fini();
50825331Samw 	if (zfs_nfsshare_inited)
50834543Smarks 		(void) ddi_modclose(nfs_mod);
50845331Samw 	if (zfs_smbshare_inited)
50855331Samw 		(void) ddi_modclose(smbsrv_mod);
50865331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
50874543Smarks 		(void) ddi_modclose(sharefs_mod);
5088789Sahrens 
50894720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
5090789Sahrens 	ldi_ident_release(zfs_li);
5091789Sahrens 	zfs_li = NULL;
50924543Smarks 	mutex_destroy(&zfs_share_lock);
5093789Sahrens 
5094789Sahrens 	return (error);
5095789Sahrens }
5096789Sahrens 
5097789Sahrens int
5098789Sahrens _info(struct modinfo *modinfop)
5099789Sahrens {
5100789Sahrens 	return (mod_info(&modlinkage, modinfop));
5101789Sahrens }
5102