xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 11546:42ea6be8961b)
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 /*
2211422SMark.Musante@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/errno.h>
29789Sahrens #include <sys/uio.h>
30789Sahrens #include <sys/buf.h>
31789Sahrens #include <sys/modctl.h>
32789Sahrens #include <sys/open.h>
33789Sahrens #include <sys/file.h>
34789Sahrens #include <sys/kmem.h>
35789Sahrens #include <sys/conf.h>
36789Sahrens #include <sys/cmn_err.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/zfs_ioctl.h>
3910972SRic.Aleshire@Sun.COM #include <sys/zfs_vfsops.h>
405331Samw #include <sys/zfs_znode.h>
41789Sahrens #include <sys/zap.h>
42789Sahrens #include <sys/spa.h>
433912Slling #include <sys/spa_impl.h>
44789Sahrens #include <sys/vdev.h>
4510972SRic.Aleshire@Sun.COM #include <sys/priv_impl.h>
46789Sahrens #include <sys/dmu.h>
47789Sahrens #include <sys/dsl_dir.h>
48789Sahrens #include <sys/dsl_dataset.h>
49789Sahrens #include <sys/dsl_prop.h>
504543Smarks #include <sys/dsl_deleg.h>
514543Smarks #include <sys/dmu_objset.h>
52789Sahrens #include <sys/ddi.h>
53789Sahrens #include <sys/sunddi.h>
54789Sahrens #include <sys/sunldi.h>
55789Sahrens #include <sys/policy.h>
56789Sahrens #include <sys/zone.h>
57789Sahrens #include <sys/nvpair.h>
58789Sahrens #include <sys/pathname.h>
59789Sahrens #include <sys/mount.h>
60789Sahrens #include <sys/sdt.h>
61789Sahrens #include <sys/fs/zfs.h>
62789Sahrens #include <sys/zfs_ctldir.h>
635331Samw #include <sys/zfs_dir.h>
642885Sahrens #include <sys/zvol.h>
654543Smarks #include <sharefs/share.h>
665326Sek110237 #include <sys/dmu_objset.h>
67789Sahrens 
68789Sahrens #include "zfs_namecheck.h"
692676Seschrock #include "zfs_prop.h"
704543Smarks #include "zfs_deleg.h"
71789Sahrens 
72789Sahrens extern struct modlfs zfs_modlfs;
73789Sahrens 
74789Sahrens extern void zfs_init(void);
75789Sahrens extern void zfs_fini(void);
76789Sahrens 
77789Sahrens ldi_ident_t zfs_li = NULL;
78789Sahrens dev_info_t *zfs_dip;
79789Sahrens 
80789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
814543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
82789Sahrens 
839234SGeorge.Wilson@Sun.COM typedef enum {
849234SGeorge.Wilson@Sun.COM 	NO_NAME,
859234SGeorge.Wilson@Sun.COM 	POOL_NAME,
869234SGeorge.Wilson@Sun.COM 	DATASET_NAME
879234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
889234SGeorge.Wilson@Sun.COM 
89789Sahrens typedef struct zfs_ioc_vec {
90789Sahrens 	zfs_ioc_func_t		*zvec_func;
91789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
929234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
934543Smarks 	boolean_t		zvec_his_log;
949234SGeorge.Wilson@Sun.COM 	boolean_t		zvec_pool_check;
95789Sahrens } zfs_ioc_vec_t;
96789Sahrens 
979396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
989396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
999396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
1009396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
1019396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
1029396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
1039396SMatthew.Ahrens@Sun.COM };
1049396SMatthew.Ahrens@Sun.COM 
1059396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
10611022STom.Erickson@Sun.COM static int zfs_check_settable(const char *name, nvpair_t *property,
10711022STom.Erickson@Sun.COM     cred_t *cr);
10811022STom.Erickson@Sun.COM static int zfs_check_clearable(char *dataset, nvlist_t *props,
10911022STom.Erickson@Sun.COM     nvlist_t **errors);
1107184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1117184Stimh     boolean_t *);
11211022STom.Erickson@Sun.COM int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
1137184Stimh 
114789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
115789Sahrens void
116789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
117789Sahrens {
118789Sahrens 	const char *newfile;
119789Sahrens 	char buf[256];
120789Sahrens 	va_list adx;
121789Sahrens 
122789Sahrens 	/*
123789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
124789Sahrens 	 */
125789Sahrens 	newfile = strrchr(file, '/');
126789Sahrens 	if (newfile != NULL) {
127789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
128789Sahrens 	} else {
129789Sahrens 		newfile = file;
130789Sahrens 	}
131789Sahrens 
132789Sahrens 	va_start(adx, fmt);
133789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
134789Sahrens 	va_end(adx);
135789Sahrens 
136789Sahrens 	/*
137789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
138789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
139789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
140789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
141789Sahrens 	 * arg0 = file name
142789Sahrens 	 * arg1 = function name
143789Sahrens 	 * arg2 = line number
144789Sahrens 	 * arg3 = message
145789Sahrens 	 */
146789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
147789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
148789Sahrens }
149789Sahrens 
1504543Smarks static void
1514715Sek110237 history_str_free(char *buf)
1524715Sek110237 {
1534715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1544715Sek110237 }
1554715Sek110237 
1564715Sek110237 static char *
1574715Sek110237 history_str_get(zfs_cmd_t *zc)
1584715Sek110237 {
1594715Sek110237 	char *buf;
1604715Sek110237 
1614715Sek110237 	if (zc->zc_history == NULL)
1624715Sek110237 		return (NULL);
1634715Sek110237 
1644715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1654715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1664715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1674715Sek110237 		history_str_free(buf);
1684715Sek110237 		return (NULL);
1694715Sek110237 	}
1704715Sek110237 
1714715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1724715Sek110237 
1734715Sek110237 	return (buf);
1744715Sek110237 }
1754715Sek110237 
1765375Stimh /*
1777042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1787042Sgw25295  */
1797042Sgw25295 static boolean_t
1807042Sgw25295 zfs_is_bootfs(const char *name)
1817042Sgw25295 {
18210298SMatthew.Ahrens@Sun.COM 	objset_t *os;
18310298SMatthew.Ahrens@Sun.COM 
18410298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
18510298SMatthew.Ahrens@Sun.COM 		boolean_t ret;
18610922SJeff.Bonwick@Sun.COM 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
18710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18810298SMatthew.Ahrens@Sun.COM 		return (ret);
1897042Sgw25295 	}
19010298SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1917042Sgw25295 }
1927042Sgw25295 
1937042Sgw25295 /*
1947184Stimh  * zfs_earlier_version
1955375Stimh  *
1965375Stimh  *	Return non-zero if the spa version is less than requested version.
1975375Stimh  */
1985331Samw static int
1997184Stimh zfs_earlier_version(const char *name, int version)
2005331Samw {
2015331Samw 	spa_t *spa;
2025331Samw 
2035331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
2045331Samw 		if (spa_version(spa) < version) {
2055331Samw 			spa_close(spa, FTAG);
2065331Samw 			return (1);
2075331Samw 		}
2085331Samw 		spa_close(spa, FTAG);
2095331Samw 	}
2105331Samw 	return (0);
2115331Samw }
2125331Samw 
2135977Smarks /*
2146689Smaybee  * zpl_earlier_version
2155977Smarks  *
2166689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2175977Smarks  */
2186689Smaybee static boolean_t
2196689Smaybee zpl_earlier_version(const char *name, int version)
2205977Smarks {
2215977Smarks 	objset_t *os;
2226689Smaybee 	boolean_t rc = B_TRUE;
2235977Smarks 
22410298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
2256689Smaybee 		uint64_t zplversion;
2266689Smaybee 
22710298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
22810298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
22910298SMatthew.Ahrens@Sun.COM 			return (B_TRUE);
23010298SMatthew.Ahrens@Sun.COM 		}
23110298SMatthew.Ahrens@Sun.COM 		/* XXX reading from non-owned objset */
2326689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2336689Smaybee 			rc = zplversion < version;
23410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
2355977Smarks 	}
2365977Smarks 	return (rc);
2375977Smarks }
2385977Smarks 
2394715Sek110237 static void
2404543Smarks zfs_log_history(zfs_cmd_t *zc)
2414543Smarks {
2424543Smarks 	spa_t *spa;
2434603Sahrens 	char *buf;
2444543Smarks 
2454715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2464577Sahrens 		return;
2474577Sahrens 
2484715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2494715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2504715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2514715Sek110237 		spa_close(spa, FTAG);
2524543Smarks 	}
2534715Sek110237 	history_str_free(buf);
2544543Smarks }
2554543Smarks 
256789Sahrens /*
257789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
258789Sahrens  * and can be used in the local zone, as there is no associated dataset.
259789Sahrens  */
260789Sahrens /* ARGSUSED */
261789Sahrens static int
2624543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
263789Sahrens {
264789Sahrens 	return (0);
265789Sahrens }
266789Sahrens 
267789Sahrens /*
268789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
269789Sahrens  * no privileges, but must be visible in the local zone.
270789Sahrens  */
271789Sahrens /* ARGSUSED */
272789Sahrens static int
2734543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
274789Sahrens {
275789Sahrens 	if (INGLOBALZONE(curproc) ||
2764543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
277789Sahrens 		return (0);
278789Sahrens 
279789Sahrens 	return (ENOENT);
280789Sahrens }
281789Sahrens 
282789Sahrens static int
283789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
284789Sahrens {
285789Sahrens 	uint64_t zoned;
286789Sahrens 	int writable = 1;
287789Sahrens 
288789Sahrens 	/*
289789Sahrens 	 * The dataset must be visible by this zone -- check this first
290789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
291789Sahrens 	 */
292789Sahrens 	if (!INGLOBALZONE(curproc) &&
293789Sahrens 	    !zone_dataset_visible(dataset, &writable))
294789Sahrens 		return (ENOENT);
295789Sahrens 
296789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
297789Sahrens 		return (ENOENT);
298789Sahrens 
299789Sahrens 	if (INGLOBALZONE(curproc)) {
300789Sahrens 		/*
301789Sahrens 		 * If the fs is zoned, only root can access it from the
302789Sahrens 		 * global zone.
303789Sahrens 		 */
304789Sahrens 		if (secpolicy_zfs(cr) && zoned)
305789Sahrens 			return (EPERM);
306789Sahrens 	} else {
307789Sahrens 		/*
308789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
309789Sahrens 		 */
310789Sahrens 		if (!zoned)
311789Sahrens 			return (EPERM);
312789Sahrens 
313789Sahrens 		/* must be writable by this zone */
314789Sahrens 		if (!writable)
315789Sahrens 			return (EPERM);
316789Sahrens 	}
317789Sahrens 	return (0);
318789Sahrens }
319789Sahrens 
320789Sahrens int
3214543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
322789Sahrens {
323789Sahrens 	int error;
324789Sahrens 
3254543Smarks 	error = zfs_dozonecheck(name, cr);
3264543Smarks 	if (error == 0) {
3274543Smarks 		error = secpolicy_zfs(cr);
3284670Sahrens 		if (error)
3294543Smarks 			error = dsl_deleg_access(name, perm, cr);
3304543Smarks 	}
3314543Smarks 	return (error);
3324543Smarks }
3334543Smarks 
33410972SRic.Aleshire@Sun.COM /*
33510972SRic.Aleshire@Sun.COM  * Policy for setting the security label property.
33610972SRic.Aleshire@Sun.COM  *
33710972SRic.Aleshire@Sun.COM  * Returns 0 for success, non-zero for access and other errors.
33810972SRic.Aleshire@Sun.COM  */
33910972SRic.Aleshire@Sun.COM static int
34011022STom.Erickson@Sun.COM zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
34110972SRic.Aleshire@Sun.COM {
34210972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
34310972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl, new_sl;
34410972SRic.Aleshire@Sun.COM 	boolean_t	new_default = FALSE;
34510972SRic.Aleshire@Sun.COM 	uint64_t	zoned;
34610972SRic.Aleshire@Sun.COM 	int		needed_priv = -1;
34710972SRic.Aleshire@Sun.COM 	int		error;
34810972SRic.Aleshire@Sun.COM 
34910972SRic.Aleshire@Sun.COM 	/* First get the existing dataset label. */
35010972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
35110972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
35210972SRic.Aleshire@Sun.COM 	if (error)
35310972SRic.Aleshire@Sun.COM 		return (EPERM);
35410972SRic.Aleshire@Sun.COM 
35510972SRic.Aleshire@Sun.COM 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
35610972SRic.Aleshire@Sun.COM 		new_default = TRUE;
35710972SRic.Aleshire@Sun.COM 
35810972SRic.Aleshire@Sun.COM 	/* The label must be translatable */
35910972SRic.Aleshire@Sun.COM 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
36010972SRic.Aleshire@Sun.COM 		return (EINVAL);
36110972SRic.Aleshire@Sun.COM 
36210972SRic.Aleshire@Sun.COM 	/*
36310972SRic.Aleshire@Sun.COM 	 * In a non-global zone, disallow attempts to set a label that
36410972SRic.Aleshire@Sun.COM 	 * doesn't match that of the zone; otherwise no other checks
36510972SRic.Aleshire@Sun.COM 	 * are needed.
36610972SRic.Aleshire@Sun.COM 	 */
36710972SRic.Aleshire@Sun.COM 	if (!INGLOBALZONE(curproc)) {
36810972SRic.Aleshire@Sun.COM 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
36910972SRic.Aleshire@Sun.COM 			return (EPERM);
37010972SRic.Aleshire@Sun.COM 		return (0);
37110972SRic.Aleshire@Sun.COM 	}
37210972SRic.Aleshire@Sun.COM 
37310972SRic.Aleshire@Sun.COM 	/*
37410972SRic.Aleshire@Sun.COM 	 * For global-zone datasets (i.e., those whose zoned property is
37510972SRic.Aleshire@Sun.COM 	 * "off", verify that the specified new label is valid for the
37610972SRic.Aleshire@Sun.COM 	 * global zone.
37710972SRic.Aleshire@Sun.COM 	 */
37810972SRic.Aleshire@Sun.COM 	if (dsl_prop_get_integer(name,
37910972SRic.Aleshire@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
38010972SRic.Aleshire@Sun.COM 		return (EPERM);
38110972SRic.Aleshire@Sun.COM 	if (!zoned) {
38210972SRic.Aleshire@Sun.COM 		if (zfs_check_global_label(name, strval) != 0)
38310972SRic.Aleshire@Sun.COM 			return (EPERM);
38410972SRic.Aleshire@Sun.COM 	}
38510972SRic.Aleshire@Sun.COM 
38610972SRic.Aleshire@Sun.COM 	/*
38710972SRic.Aleshire@Sun.COM 	 * If the existing dataset label is nondefault, check if the
38810972SRic.Aleshire@Sun.COM 	 * dataset is mounted (label cannot be changed while mounted).
38910972SRic.Aleshire@Sun.COM 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
39010972SRic.Aleshire@Sun.COM 	 * mounted (or isn't a dataset, doesn't exist, ...).
39110972SRic.Aleshire@Sun.COM 	 */
39210972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
39311022STom.Erickson@Sun.COM 		objset_t *os;
39411022STom.Erickson@Sun.COM 		static char *setsl_tag = "setsl_tag";
39511022STom.Erickson@Sun.COM 
39610972SRic.Aleshire@Sun.COM 		/*
39710972SRic.Aleshire@Sun.COM 		 * Try to own the dataset; abort if there is any error,
39810972SRic.Aleshire@Sun.COM 		 * (e.g., already mounted, in use, or other error).
39910972SRic.Aleshire@Sun.COM 		 */
40010972SRic.Aleshire@Sun.COM 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
40111022STom.Erickson@Sun.COM 		    setsl_tag, &os);
40210972SRic.Aleshire@Sun.COM 		if (error)
40310972SRic.Aleshire@Sun.COM 			return (EPERM);
40410972SRic.Aleshire@Sun.COM 
40511022STom.Erickson@Sun.COM 		dmu_objset_disown(os, setsl_tag);
40611022STom.Erickson@Sun.COM 
40710972SRic.Aleshire@Sun.COM 		if (new_default) {
40810972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
40910972SRic.Aleshire@Sun.COM 			goto out_check;
41010972SRic.Aleshire@Sun.COM 		}
41110972SRic.Aleshire@Sun.COM 
41210972SRic.Aleshire@Sun.COM 		if (hexstr_to_label(strval, &new_sl) != 0)
41310972SRic.Aleshire@Sun.COM 			return (EPERM);
41410972SRic.Aleshire@Sun.COM 
41510972SRic.Aleshire@Sun.COM 		if (blstrictdom(&ds_sl, &new_sl))
41610972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
41710972SRic.Aleshire@Sun.COM 		else if (blstrictdom(&new_sl, &ds_sl))
41810972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
41910972SRic.Aleshire@Sun.COM 	} else {
42010972SRic.Aleshire@Sun.COM 		/* dataset currently has a default label */
42110972SRic.Aleshire@Sun.COM 		if (!new_default)
42210972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
42310972SRic.Aleshire@Sun.COM 	}
42410972SRic.Aleshire@Sun.COM 
42510972SRic.Aleshire@Sun.COM out_check:
42610972SRic.Aleshire@Sun.COM 	if (needed_priv != -1)
42710972SRic.Aleshire@Sun.COM 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
42810972SRic.Aleshire@Sun.COM 	return (0);
42910972SRic.Aleshire@Sun.COM }
43010972SRic.Aleshire@Sun.COM 
4314543Smarks static int
43211022STom.Erickson@Sun.COM zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
43311022STom.Erickson@Sun.COM     cred_t *cr)
4344543Smarks {
43511022STom.Erickson@Sun.COM 	char *strval;
43611022STom.Erickson@Sun.COM 
4374543Smarks 	/*
4384543Smarks 	 * Check permissions for special properties.
4394543Smarks 	 */
4404543Smarks 	switch (prop) {
4414543Smarks 	case ZFS_PROP_ZONED:
4424543Smarks 		/*
4434543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
4444543Smarks 		 */
4454543Smarks 		if (!INGLOBALZONE(curproc))
4464543Smarks 			return (EPERM);
4474543Smarks 		break;
448789Sahrens 
4494543Smarks 	case ZFS_PROP_QUOTA:
4504543Smarks 		if (!INGLOBALZONE(curproc)) {
4514543Smarks 			uint64_t zoned;
4524543Smarks 			char setpoint[MAXNAMELEN];
4534543Smarks 			/*
4544543Smarks 			 * Unprivileged users are allowed to modify the
4554543Smarks 			 * quota on things *under* (ie. contained by)
4564543Smarks 			 * the thing they own.
4574543Smarks 			 */
45811022STom.Erickson@Sun.COM 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
4594543Smarks 			    setpoint))
4604543Smarks 				return (EPERM);
46111022STom.Erickson@Sun.COM 			if (!zoned || strlen(dsname) <= strlen(setpoint))
4624543Smarks 				return (EPERM);
4634543Smarks 		}
4644670Sahrens 		break;
46510972SRic.Aleshire@Sun.COM 
46610972SRic.Aleshire@Sun.COM 	case ZFS_PROP_MLSLABEL:
46710972SRic.Aleshire@Sun.COM 		if (!is_system_labeled())
46810972SRic.Aleshire@Sun.COM 			return (EPERM);
46911022STom.Erickson@Sun.COM 
47011022STom.Erickson@Sun.COM 		if (nvpair_value_string(propval, &strval) == 0) {
47111022STom.Erickson@Sun.COM 			int err;
47211022STom.Erickson@Sun.COM 
47311022STom.Erickson@Sun.COM 			err = zfs_set_slabel_policy(dsname, strval, CRED());
47411022STom.Erickson@Sun.COM 			if (err != 0)
47511022STom.Erickson@Sun.COM 				return (err);
47611022STom.Erickson@Sun.COM 		}
47710972SRic.Aleshire@Sun.COM 		break;
4784543Smarks 	}
4794543Smarks 
48011022STom.Erickson@Sun.COM 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
481789Sahrens }
482789Sahrens 
4834543Smarks int
4844543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
4854543Smarks {
4864543Smarks 	int error;
4874543Smarks 
4884543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
4894543Smarks 	if (error)
4904543Smarks 		return (error);
4914543Smarks 
4924543Smarks 	/*
4934543Smarks 	 * permission to set permissions will be evaluated later in
4944543Smarks 	 * dsl_deleg_can_allow()
4954543Smarks 	 */
4964543Smarks 	return (0);
4974543Smarks }
4984543Smarks 
4994543Smarks int
5004543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
5014543Smarks {
50210588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
50310588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_ROLLBACK, cr));
5044543Smarks }
5054543Smarks 
5064543Smarks int
5074543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
5084543Smarks {
5094543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5104543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
5114543Smarks }
5124543Smarks 
5138845Samw@Sun.COM static int
5148845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
5158845Samw@Sun.COM {
5168845Samw@Sun.COM 	vnode_t *vp;
5178845Samw@Sun.COM 	int error;
5188845Samw@Sun.COM 
5198845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5208845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
5218845Samw@Sun.COM 		return (error);
5228845Samw@Sun.COM 
5238845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
5248845Samw@Sun.COM 
5258845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5268845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5278845Samw@Sun.COM 	    zc->zc_name) != 0)) {
5288845Samw@Sun.COM 		VN_RELE(vp);
5298845Samw@Sun.COM 		return (EPERM);
5308845Samw@Sun.COM 	}
5318845Samw@Sun.COM 
5328845Samw@Sun.COM 	VN_RELE(vp);
5338845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
5348845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
5358845Samw@Sun.COM }
5368845Samw@Sun.COM 
5374543Smarks int
5384543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
5394543Smarks {
5404543Smarks 	if (!INGLOBALZONE(curproc))
5414543Smarks 		return (EPERM);
5424543Smarks 
5435367Sahrens 	if (secpolicy_nfs(cr) == 0) {
5444543Smarks 		return (0);
5454543Smarks 	} else {
5468845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
5478845Samw@Sun.COM 	}
5488845Samw@Sun.COM }
5498845Samw@Sun.COM 
5508845Samw@Sun.COM int
5518845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
5528845Samw@Sun.COM {
5538845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
5548845Samw@Sun.COM 		return (EPERM);
5558845Samw@Sun.COM 
5568845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
5578845Samw@Sun.COM 		return (0);
5588845Samw@Sun.COM 	} else {
5598845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
5604543Smarks 	}
5614543Smarks }
5624543Smarks 
563789Sahrens static int
5644543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
565789Sahrens {
566789Sahrens 	char *cp;
567789Sahrens 
568789Sahrens 	/*
569789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
570789Sahrens 	 */
5714543Smarks 	(void) strncpy(parent, datasetname, parentsize);
5724543Smarks 	cp = strrchr(parent, '@');
573789Sahrens 	if (cp != NULL) {
574789Sahrens 		cp[0] = '\0';
575789Sahrens 	} else {
5764543Smarks 		cp = strrchr(parent, '/');
577789Sahrens 		if (cp == NULL)
578789Sahrens 			return (ENOENT);
579789Sahrens 		cp[0] = '\0';
580789Sahrens 	}
581789Sahrens 
5824543Smarks 	return (0);
5834543Smarks }
5844543Smarks 
5854543Smarks int
5864543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
5874543Smarks {
5884543Smarks 	int error;
5894543Smarks 
5904543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
5914543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5924543Smarks 		return (error);
5934543Smarks 
5944543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
5954543Smarks }
5964543Smarks 
5974543Smarks static int
5984543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
5994543Smarks {
6004543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
6014543Smarks }
6024543Smarks 
6034543Smarks /*
60411314Swilliam.gorrell@sun.com  * Destroying snapshots with delegated permissions requires
60511314Swilliam.gorrell@sun.com  * descendent mount and destroy permissions.
60611314Swilliam.gorrell@sun.com  * Reassemble the full filesystem@snap name so dsl_deleg_access()
60711314Swilliam.gorrell@sun.com  * can do the correct permission check.
60811314Swilliam.gorrell@sun.com  *
60911314Swilliam.gorrell@sun.com  * Since this routine is used when doing a recursive destroy of snapshots
61011314Swilliam.gorrell@sun.com  * and destroying snapshots requires descendent permissions, a successfull
61111314Swilliam.gorrell@sun.com  * check of the top level snapshot applies to snapshots of all descendent
61211314Swilliam.gorrell@sun.com  * datasets as well.
61311314Swilliam.gorrell@sun.com  */
61411314Swilliam.gorrell@sun.com static int
61511314Swilliam.gorrell@sun.com zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
61611314Swilliam.gorrell@sun.com {
61711314Swilliam.gorrell@sun.com 	int error;
61811314Swilliam.gorrell@sun.com 	char *dsname;
61911314Swilliam.gorrell@sun.com 
62011314Swilliam.gorrell@sun.com 	dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
62111314Swilliam.gorrell@sun.com 
62211314Swilliam.gorrell@sun.com 	error = zfs_secpolicy_destroy_perms(dsname, cr);
62311314Swilliam.gorrell@sun.com 
62411314Swilliam.gorrell@sun.com 	strfree(dsname);
62511314Swilliam.gorrell@sun.com 	return (error);
62611314Swilliam.gorrell@sun.com }
62711314Swilliam.gorrell@sun.com 
62811314Swilliam.gorrell@sun.com /*
6294543Smarks  * Must have sys_config privilege to check the iscsi permission
6304543Smarks  */
6314543Smarks /* ARGSUSED */
6324543Smarks static int
6334543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
6344543Smarks {
6354543Smarks 	return (secpolicy_zfs(cr));
6364543Smarks }
6374543Smarks 
6384543Smarks int
6394543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
6404543Smarks {
64111022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
6424543Smarks 	int	error;
6434543Smarks 
6444543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
6454543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
6464543Smarks 		return (error);
6474543Smarks 
6484543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
6494543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
6504543Smarks 		return (error);
6514543Smarks 
6524543Smarks 	if ((error = zfs_get_parent(to, parentname,
6534543Smarks 	    sizeof (parentname))) != 0)
6544543Smarks 		return (error);
6554543Smarks 
6564543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6574543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6584543Smarks 		return (error);
6594543Smarks 
6604543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6614543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
6624543Smarks 		return (error);
6634543Smarks 
6644543Smarks 	return (error);
6654543Smarks }
6664543Smarks 
6674543Smarks static int
6684543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
6694543Smarks {
6704543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
6714543Smarks }
6724543Smarks 
6734543Smarks static int
6744543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
6754543Smarks {
67611022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
6774543Smarks 	objset_t *clone;
6784543Smarks 	int error;
6794543Smarks 
6804543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
6814543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
6824543Smarks 	if (error)
6834543Smarks 		return (error);
6844543Smarks 
68510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
6864543Smarks 
6874543Smarks 	if (error == 0) {
6884543Smarks 		dsl_dataset_t *pclone = NULL;
6894543Smarks 		dsl_dir_t *dd;
69010298SMatthew.Ahrens@Sun.COM 		dd = clone->os_dsl_dataset->ds_dir;
6914543Smarks 
6924543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
6936689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
6946689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
6954543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
6964543Smarks 		if (error) {
69710298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(clone, FTAG);
6984543Smarks 			return (error);
6994543Smarks 		}
7004543Smarks 
7014543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
7024543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
7034543Smarks 
7044543Smarks 		dsl_dataset_name(pclone, parentname);
70510298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
7066689Smaybee 		dsl_dataset_rele(pclone, FTAG);
7074543Smarks 		if (error == 0)
7084543Smarks 			error = zfs_secpolicy_write_perms(parentname,
7094543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
7104543Smarks 	}
7114543Smarks 	return (error);
7124543Smarks }
7134543Smarks 
7144543Smarks static int
7154543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
7164543Smarks {
7174543Smarks 	int error;
7184543Smarks 
7194543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7204543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
7214543Smarks 		return (error);
7224543Smarks 
7234543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7244543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
7254543Smarks 		return (error);
7264543Smarks 
7274543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
7284543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
7294543Smarks }
7304543Smarks 
7314543Smarks int
7324543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
7334543Smarks {
73410588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(name,
73510588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
7364543Smarks }
7374543Smarks 
7384543Smarks static int
7394543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
7404543Smarks {
7414543Smarks 
7424543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
7434543Smarks }
7444543Smarks 
7454543Smarks static int
7464543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
7474543Smarks {
74811022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
74911022STom.Erickson@Sun.COM 	int	error;
7504543Smarks 
7514543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
7524543Smarks 	    sizeof (parentname))) != 0)
7534543Smarks 		return (error);
7544543Smarks 
7554543Smarks 	if (zc->zc_value[0] != '\0') {
7564543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
7574543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
7584543Smarks 			return (error);
7594543Smarks 	}
7604543Smarks 
7614543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
7624543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
7634543Smarks 		return (error);
7644543Smarks 
7654543Smarks 	error = zfs_secpolicy_write_perms(parentname,
7664543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
7674543Smarks 
7684543Smarks 	return (error);
7694543Smarks }
7704543Smarks 
7714543Smarks static int
7724543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
7734543Smarks {
7744543Smarks 	int error;
7754543Smarks 
7764543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
7774543Smarks 	if (error) {
7784543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
7794543Smarks 	}
7804543Smarks 	return (error);
781789Sahrens }
782789Sahrens 
783789Sahrens /*
784789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
785789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
786789Sahrens  */
787789Sahrens /* ARGSUSED */
788789Sahrens static int
7894543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
790789Sahrens {
791789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
792789Sahrens 		return (EPERM);
793789Sahrens 
794789Sahrens 	return (0);
795789Sahrens }
796789Sahrens 
797789Sahrens /*
7981544Seschrock  * Policy for fault injection.  Requires all privileges.
7991544Seschrock  */
8001544Seschrock /* ARGSUSED */
8011544Seschrock static int
8024543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
8031544Seschrock {
8041544Seschrock 	return (secpolicy_zinject(cr));
8051544Seschrock }
8061544Seschrock 
8074849Sahrens static int
8084849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
8094849Sahrens {
8104849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
8114849Sahrens 
8125094Slling 	if (prop == ZPROP_INVAL) {
8134849Sahrens 		if (!zfs_prop_user(zc->zc_value))
8144849Sahrens 			return (EINVAL);
8154849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
8164849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
8174849Sahrens 	} else {
81811022STom.Erickson@Sun.COM 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
81911022STom.Erickson@Sun.COM 		    NULL, cr));
8204849Sahrens 	}
8214849Sahrens }
8224849Sahrens 
8239396SMatthew.Ahrens@Sun.COM static int
8249396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
8259396SMatthew.Ahrens@Sun.COM {
8269396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
8279396SMatthew.Ahrens@Sun.COM 	if (err)
8289396SMatthew.Ahrens@Sun.COM 		return (err);
8299396SMatthew.Ahrens@Sun.COM 
8309396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
8319396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
8329396SMatthew.Ahrens@Sun.COM 
8339396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
8349396SMatthew.Ahrens@Sun.COM 		/*
8359396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
8369396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
8379396SMatthew.Ahrens@Sun.COM 		 */
8389396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
8399396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
8409396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
8419396SMatthew.Ahrens@Sun.COM 				return (0);
8429396SMatthew.Ahrens@Sun.COM 		} else {
8439396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
8449396SMatthew.Ahrens@Sun.COM 				return (0);
8459396SMatthew.Ahrens@Sun.COM 		}
8469396SMatthew.Ahrens@Sun.COM 	}
8479396SMatthew.Ahrens@Sun.COM 
8489396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
8499396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
8509396SMatthew.Ahrens@Sun.COM }
8519396SMatthew.Ahrens@Sun.COM 
8529396SMatthew.Ahrens@Sun.COM static int
8539396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
8549396SMatthew.Ahrens@Sun.COM {
8559396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
8569396SMatthew.Ahrens@Sun.COM 	if (err)
8579396SMatthew.Ahrens@Sun.COM 		return (err);
8589396SMatthew.Ahrens@Sun.COM 
8599396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
8609396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
8619396SMatthew.Ahrens@Sun.COM 
8629396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
8639396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
8649396SMatthew.Ahrens@Sun.COM }
8659396SMatthew.Ahrens@Sun.COM 
8669396SMatthew.Ahrens@Sun.COM static int
8679396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
8689396SMatthew.Ahrens@Sun.COM {
86911022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
87011022STom.Erickson@Sun.COM 	    NULL, cr));
8719396SMatthew.Ahrens@Sun.COM }
8729396SMatthew.Ahrens@Sun.COM 
87310242Schris.kirby@sun.com static int
87410242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
87510242Schris.kirby@sun.com {
87610242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
87710242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
87810242Schris.kirby@sun.com }
87910242Schris.kirby@sun.com 
88010242Schris.kirby@sun.com static int
88110242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
88210242Schris.kirby@sun.com {
88310242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
88410242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
88510242Schris.kirby@sun.com }
88610242Schris.kirby@sun.com 
8871544Seschrock /*
888789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
889789Sahrens  */
890789Sahrens static int
8919643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
892789Sahrens {
893789Sahrens 	char *packed;
894789Sahrens 	int error;
8955094Slling 	nvlist_t *list = NULL;
896789Sahrens 
897789Sahrens 	/*
8982676Seschrock 	 * Read in and unpack the user-supplied nvlist.
899789Sahrens 	 */
9005094Slling 	if (size == 0)
901789Sahrens 		return (EINVAL);
902789Sahrens 
903789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
904789Sahrens 
9059643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
9069643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
907789Sahrens 		kmem_free(packed, size);
908789Sahrens 		return (error);
909789Sahrens 	}
910789Sahrens 
9115094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
912789Sahrens 		kmem_free(packed, size);
913789Sahrens 		return (error);
914789Sahrens 	}
915789Sahrens 
916789Sahrens 	kmem_free(packed, size);
917789Sahrens 
9185094Slling 	*nvp = list;
919789Sahrens 	return (0);
920789Sahrens }
921789Sahrens 
922789Sahrens static int
92311022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
92411022STom.Erickson@Sun.COM {
92511022STom.Erickson@Sun.COM 	size_t size;
92611022STom.Erickson@Sun.COM 
92711022STom.Erickson@Sun.COM 	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
92811022STom.Erickson@Sun.COM 
92911022STom.Erickson@Sun.COM 	if (size > zc->zc_nvlist_dst_size) {
93011022STom.Erickson@Sun.COM 		nvpair_t *more_errors;
93111022STom.Erickson@Sun.COM 		int n = 0;
93211022STom.Erickson@Sun.COM 
93311022STom.Erickson@Sun.COM 		if (zc->zc_nvlist_dst_size < 1024)
93411022STom.Erickson@Sun.COM 			return (ENOMEM);
93511022STom.Erickson@Sun.COM 
93611022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
93711022STom.Erickson@Sun.COM 		more_errors = nvlist_prev_nvpair(*errors, NULL);
93811022STom.Erickson@Sun.COM 
93911022STom.Erickson@Sun.COM 		do {
94011022STom.Erickson@Sun.COM 			nvpair_t *pair = nvlist_prev_nvpair(*errors,
94111022STom.Erickson@Sun.COM 			    more_errors);
94211022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
94311022STom.Erickson@Sun.COM 			n++;
94411022STom.Erickson@Sun.COM 			VERIFY(nvlist_size(*errors, &size,
94511022STom.Erickson@Sun.COM 			    NV_ENCODE_NATIVE) == 0);
94611022STom.Erickson@Sun.COM 		} while (size > zc->zc_nvlist_dst_size);
94711022STom.Erickson@Sun.COM 
94811022STom.Erickson@Sun.COM 		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
94911022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
95011022STom.Erickson@Sun.COM 		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
95111022STom.Erickson@Sun.COM 		ASSERT(size <= zc->zc_nvlist_dst_size);
95211022STom.Erickson@Sun.COM 	}
95311022STom.Erickson@Sun.COM 
95411022STom.Erickson@Sun.COM 	return (0);
95511022STom.Erickson@Sun.COM }
95611022STom.Erickson@Sun.COM 
95711022STom.Erickson@Sun.COM static int
9582676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
9592676Seschrock {
9602676Seschrock 	char *packed = NULL;
9612676Seschrock 	size_t size;
9622676Seschrock 	int error;
9632676Seschrock 
9642676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
9652676Seschrock 
9662676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
9672676Seschrock 		error = ENOMEM;
9682676Seschrock 	} else {
9694611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
9702676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
9712676Seschrock 		    KM_SLEEP) == 0);
9729643SEric.Taylor@Sun.COM 		error = ddi_copyout(packed,
9739643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
9742676Seschrock 		kmem_free(packed, size);
9752676Seschrock 	}
9762676Seschrock 
9772676Seschrock 	zc->zc_nvlist_dst_size = size;
9782676Seschrock 	return (error);
9792676Seschrock }
9802676Seschrock 
9812676Seschrock static int
98211185SSean.McEnroe@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
9839396SMatthew.Ahrens@Sun.COM {
9849396SMatthew.Ahrens@Sun.COM 	objset_t *os;
9859396SMatthew.Ahrens@Sun.COM 	int error;
9869396SMatthew.Ahrens@Sun.COM 
98710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(dsname, FTAG, &os);
9889396SMatthew.Ahrens@Sun.COM 	if (error)
9899396SMatthew.Ahrens@Sun.COM 		return (error);
99010298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
99110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
99210298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
99310298SMatthew.Ahrens@Sun.COM 	}
99410298SMatthew.Ahrens@Sun.COM 
99510298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_user_ptr_lock);
99611185SSean.McEnroe@Sun.COM 	*zfvp = dmu_objset_get_user(os);
99711185SSean.McEnroe@Sun.COM 	if (*zfvp) {
99811185SSean.McEnroe@Sun.COM 		VFS_HOLD((*zfvp)->z_vfs);
9999396SMatthew.Ahrens@Sun.COM 	} else {
10009396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
10019396SMatthew.Ahrens@Sun.COM 	}
100210298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_user_ptr_lock);
100310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
10049396SMatthew.Ahrens@Sun.COM 	return (error);
10059396SMatthew.Ahrens@Sun.COM }
10069396SMatthew.Ahrens@Sun.COM 
10079396SMatthew.Ahrens@Sun.COM /*
10089396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
10099396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
10109396SMatthew.Ahrens@Sun.COM  */
10119396SMatthew.Ahrens@Sun.COM static int
101211185SSean.McEnroe@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp)
10139396SMatthew.Ahrens@Sun.COM {
10149396SMatthew.Ahrens@Sun.COM 	int error = 0;
10159396SMatthew.Ahrens@Sun.COM 
101611185SSean.McEnroe@Sun.COM 	if (getzfsvfs(name, zfvp) != 0)
101711185SSean.McEnroe@Sun.COM 		error = zfsvfs_create(name, zfvp);
10189396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
101911185SSean.McEnroe@Sun.COM 		rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag);
102011185SSean.McEnroe@Sun.COM 		if ((*zfvp)->z_unmounted) {
10219396SMatthew.Ahrens@Sun.COM 			/*
10229396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
10239396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
10249396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
10259396SMatthew.Ahrens@Sun.COM 			 */
102611185SSean.McEnroe@Sun.COM 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
10279396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
10289396SMatthew.Ahrens@Sun.COM 		}
10299396SMatthew.Ahrens@Sun.COM 	}
10309396SMatthew.Ahrens@Sun.COM 	return (error);
10319396SMatthew.Ahrens@Sun.COM }
10329396SMatthew.Ahrens@Sun.COM 
10339396SMatthew.Ahrens@Sun.COM static void
10349396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
10359396SMatthew.Ahrens@Sun.COM {
10369396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
10379396SMatthew.Ahrens@Sun.COM 
10389396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
10399396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
10409396SMatthew.Ahrens@Sun.COM 	} else {
104110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
10429396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
10439396SMatthew.Ahrens@Sun.COM 	}
10449396SMatthew.Ahrens@Sun.COM }
10459396SMatthew.Ahrens@Sun.COM 
10469396SMatthew.Ahrens@Sun.COM static int
1047789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1048789Sahrens {
1049789Sahrens 	int error;
10505094Slling 	nvlist_t *config, *props = NULL;
10517184Stimh 	nvlist_t *rootprops = NULL;
10527184Stimh 	nvlist_t *zplprops = NULL;
10534715Sek110237 	char *buf;
1054789Sahrens 
10555094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10569643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
10574988Sek110237 		return (error);
10584715Sek110237 
10595094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
10609643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
10619643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
10625094Slling 		nvlist_free(config);
10635094Slling 		return (error);
10645094Slling 	}
10655094Slling 
10667184Stimh 	if (props) {
10677184Stimh 		nvlist_t *nvl = NULL;
10687184Stimh 		uint64_t version = SPA_VERSION;
10697184Stimh 
10707184Stimh 		(void) nvlist_lookup_uint64(props,
10717184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
10727184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
10737184Stimh 			error = EINVAL;
10747184Stimh 			goto pool_props_bad;
10757184Stimh 		}
10767184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
10777184Stimh 		if (nvl) {
10787184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
10797184Stimh 			if (error != 0) {
10807184Stimh 				nvlist_free(config);
10817184Stimh 				nvlist_free(props);
10827184Stimh 				return (error);
10837184Stimh 			}
10847184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
10857184Stimh 		}
10867184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10877184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
10887184Stimh 		    zplprops, NULL);
10897184Stimh 		if (error)
10907184Stimh 			goto pool_props_bad;
10917184Stimh 	}
10927184Stimh 
10934988Sek110237 	buf = history_str_get(zc);
1094789Sahrens 
10957184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
10967184Stimh 
10977184Stimh 	/*
10987184Stimh 	 * Set the remaining root properties
10997184Stimh 	 */
110011022STom.Erickson@Sun.COM 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
110111022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
11027184Stimh 		(void) spa_destroy(zc->zc_name);
1103789Sahrens 
11044988Sek110237 	if (buf != NULL)
11054988Sek110237 		history_str_free(buf);
11065094Slling 
11077184Stimh pool_props_bad:
11087184Stimh 	nvlist_free(rootprops);
11097184Stimh 	nvlist_free(zplprops);
1110789Sahrens 	nvlist_free(config);
11117184Stimh 	nvlist_free(props);
11125094Slling 
1113789Sahrens 	return (error);
1114789Sahrens }
1115789Sahrens 
1116789Sahrens static int
1117789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1118789Sahrens {
11194543Smarks 	int error;
11204543Smarks 	zfs_log_history(zc);
11214543Smarks 	error = spa_destroy(zc->zc_name);
112210588SEric.Taylor@Sun.COM 	if (error == 0)
112310588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
11244543Smarks 	return (error);
1125789Sahrens }
1126789Sahrens 
1127789Sahrens static int
1128789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1129789Sahrens {
11305094Slling 	nvlist_t *config, *props = NULL;
1131789Sahrens 	uint64_t guid;
113210921STim.Haley@Sun.COM 	int error;
1133789Sahrens 
11345094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
11359643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
1136789Sahrens 		return (error);
1137789Sahrens 
11385094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
11399643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
11409643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
11415094Slling 		nvlist_free(config);
11425094Slling 		return (error);
11435094Slling 	}
11445094Slling 
1145789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
11461544Seschrock 	    guid != zc->zc_guid)
1147789Sahrens 		error = EINVAL;
11486643Seschrock 	else if (zc->zc_cookie)
114910921STim.Haley@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config, props);
1150789Sahrens 	else
11515094Slling 		error = spa_import(zc->zc_name, config, props);
1152789Sahrens 
115310921STim.Haley@Sun.COM 	if (zc->zc_nvlist_dst != 0)
115410921STim.Haley@Sun.COM 		(void) put_nvlist(zc, config);
115510921STim.Haley@Sun.COM 
1156789Sahrens 	nvlist_free(config);
1157789Sahrens 
11585094Slling 	if (props)
11595094Slling 		nvlist_free(props);
11605094Slling 
1161789Sahrens 	return (error);
1162789Sahrens }
1163789Sahrens 
1164789Sahrens static int
1165789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1166789Sahrens {
11674543Smarks 	int error;
11687214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
11698211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
11707214Slling 
11714543Smarks 	zfs_log_history(zc);
11728211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
117310588SEric.Taylor@Sun.COM 	if (error == 0)
117410588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
11754543Smarks 	return (error);
1176789Sahrens }
1177789Sahrens 
1178789Sahrens static int
1179789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1180789Sahrens {
1181789Sahrens 	nvlist_t *configs;
1182789Sahrens 	int error;
1183789Sahrens 
1184789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1185789Sahrens 		return (EEXIST);
1186789Sahrens 
11872676Seschrock 	error = put_nvlist(zc, configs);
1188789Sahrens 
1189789Sahrens 	nvlist_free(configs);
1190789Sahrens 
1191789Sahrens 	return (error);
1192789Sahrens }
1193789Sahrens 
1194789Sahrens static int
1195789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1196789Sahrens {
1197789Sahrens 	nvlist_t *config;
1198789Sahrens 	int error;
11991544Seschrock 	int ret = 0;
1200789Sahrens 
12012676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
12022676Seschrock 	    sizeof (zc->zc_value));
1203789Sahrens 
1204789Sahrens 	if (config != NULL) {
12052676Seschrock 		ret = put_nvlist(zc, config);
1206789Sahrens 		nvlist_free(config);
12071544Seschrock 
12081544Seschrock 		/*
12091544Seschrock 		 * The config may be present even if 'error' is non-zero.
12101544Seschrock 		 * In this case we return success, and preserve the real errno
12111544Seschrock 		 * in 'zc_cookie'.
12121544Seschrock 		 */
12131544Seschrock 		zc->zc_cookie = error;
1214789Sahrens 	} else {
12151544Seschrock 		ret = error;
1216789Sahrens 	}
1217789Sahrens 
12181544Seschrock 	return (ret);
1219789Sahrens }
1220789Sahrens 
1221789Sahrens /*
1222789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1223789Sahrens  * user land knows which devices are available and overall pool health.
1224789Sahrens  */
1225789Sahrens static int
1226789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1227789Sahrens {
1228789Sahrens 	nvlist_t *tryconfig, *config;
1229789Sahrens 	int error;
1230789Sahrens 
12315094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12329643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1233789Sahrens 		return (error);
1234789Sahrens 
1235789Sahrens 	config = spa_tryimport(tryconfig);
1236789Sahrens 
1237789Sahrens 	nvlist_free(tryconfig);
1238789Sahrens 
1239789Sahrens 	if (config == NULL)
1240789Sahrens 		return (EINVAL);
1241789Sahrens 
12422676Seschrock 	error = put_nvlist(zc, config);
1243789Sahrens 	nvlist_free(config);
1244789Sahrens 
1245789Sahrens 	return (error);
1246789Sahrens }
1247789Sahrens 
1248789Sahrens static int
1249789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1250789Sahrens {
1251789Sahrens 	spa_t *spa;
1252789Sahrens 	int error;
1253789Sahrens 
12542926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
12552926Sek110237 		return (error);
12562926Sek110237 
12577046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
12582926Sek110237 
12592926Sek110237 	spa_close(spa, FTAG);
12602926Sek110237 
1261789Sahrens 	return (error);
1262789Sahrens }
1263789Sahrens 
1264789Sahrens static int
1265789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1266789Sahrens {
1267789Sahrens 	spa_t *spa;
1268789Sahrens 	int error;
1269789Sahrens 
1270789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1271789Sahrens 	if (error == 0) {
1272789Sahrens 		spa_freeze(spa);
1273789Sahrens 		spa_close(spa, FTAG);
1274789Sahrens 	}
1275789Sahrens 	return (error);
1276789Sahrens }
1277789Sahrens 
1278789Sahrens static int
12791760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
12801760Seschrock {
12811760Seschrock 	spa_t *spa;
12821760Seschrock 	int error;
12831760Seschrock 
12842926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
12852926Sek110237 		return (error);
12862926Sek110237 
12875118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
12885118Slling 		spa_close(spa, FTAG);
12895118Slling 		return (EINVAL);
12905118Slling 	}
12915118Slling 
12925094Slling 	spa_upgrade(spa, zc->zc_cookie);
12932926Sek110237 	spa_close(spa, FTAG);
12942926Sek110237 
12952926Sek110237 	return (error);
12962926Sek110237 }
12972926Sek110237 
12982926Sek110237 static int
12992926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
13002926Sek110237 {
13012926Sek110237 	spa_t *spa;
13022926Sek110237 	char *hist_buf;
13032926Sek110237 	uint64_t size;
13042926Sek110237 	int error;
13052926Sek110237 
13062926Sek110237 	if ((size = zc->zc_history_len) == 0)
13072926Sek110237 		return (EINVAL);
13082926Sek110237 
13092926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
13102926Sek110237 		return (error);
13112926Sek110237 
13124577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
13133863Sek110237 		spa_close(spa, FTAG);
13143863Sek110237 		return (ENOTSUP);
13153863Sek110237 	}
13163863Sek110237 
13172926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
13182926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
13192926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
13209643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
13219643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
13229643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
13232926Sek110237 	}
13242926Sek110237 
13252926Sek110237 	spa_close(spa, FTAG);
13262926Sek110237 	kmem_free(hist_buf, size);
13272926Sek110237 	return (error);
13282926Sek110237 }
13292926Sek110237 
13302926Sek110237 static int
13313444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
13323444Sek110237 {
13333444Sek110237 	int error;
13343444Sek110237 
13353912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
13363444Sek110237 		return (error);
13373444Sek110237 
13383444Sek110237 	return (0);
13393444Sek110237 }
13403444Sek110237 
134110298SMatthew.Ahrens@Sun.COM /*
134210298SMatthew.Ahrens@Sun.COM  * inputs:
134310298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
134410298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
134510298SMatthew.Ahrens@Sun.COM  *
134610298SMatthew.Ahrens@Sun.COM  * outputs:
134710298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
134810298SMatthew.Ahrens@Sun.COM  */
13493444Sek110237 static int
13503444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
13513444Sek110237 {
135210298SMatthew.Ahrens@Sun.COM 	objset_t *os;
13533444Sek110237 	int error;
13543444Sek110237 
135510298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
135610298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
13573444Sek110237 		return (error);
135810298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
135910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
136010298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
136110298SMatthew.Ahrens@Sun.COM 	}
136210298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
13633444Sek110237 	    sizeof (zc->zc_value));
136410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
13653444Sek110237 
13663444Sek110237 	return (error);
13673444Sek110237 }
13683444Sek110237 
13693444Sek110237 static int
1370789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1371789Sahrens {
1372789Sahrens 	spa_t *spa;
1373789Sahrens 	int error;
13746423Sgw25295 	nvlist_t *config, **l2cache, **spares;
13756423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1376789Sahrens 
1377789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1378789Sahrens 	if (error != 0)
1379789Sahrens 		return (error);
1380789Sahrens 
13815450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13829643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
13835450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
13845450Sbrendan 	    &l2cache, &nl2cache);
13855450Sbrendan 
13866423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
13876423Sgw25295 	    &spares, &nspares);
13886423Sgw25295 
13893912Slling 	/*
13903912Slling 	 * A root pool with concatenated devices is not supported.
13916423Sgw25295 	 * Thus, can not add a device to a root pool.
13926423Sgw25295 	 *
13936423Sgw25295 	 * Intent log device can not be added to a rootpool because
13946423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
13956423Sgw25295 	 * can not be accessed during the mountroot time.
13966423Sgw25295 	 *
13976423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
13983912Slling 	 */
139910922SJeff.Bonwick@Sun.COM 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
140011422SMark.Musante@Sun.COM 		nvlist_free(config);
14013912Slling 		spa_close(spa, FTAG);
14023912Slling 		return (EDOM);
14033912Slling 	}
14043912Slling 
14055450Sbrendan 	if (error == 0) {
1406789Sahrens 		error = spa_vdev_add(spa, config);
1407789Sahrens 		nvlist_free(config);
1408789Sahrens 	}
1409789Sahrens 	spa_close(spa, FTAG);
1410789Sahrens 	return (error);
1411789Sahrens }
1412789Sahrens 
1413789Sahrens static int
1414789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1415789Sahrens {
14162082Seschrock 	spa_t *spa;
14172082Seschrock 	int error;
14182082Seschrock 
14192082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
14202082Seschrock 	if (error != 0)
14212082Seschrock 		return (error);
14222082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
14232082Seschrock 	spa_close(spa, FTAG);
14242082Seschrock 	return (error);
1425789Sahrens }
1426789Sahrens 
1427789Sahrens static int
14284451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1429789Sahrens {
1430789Sahrens 	spa_t *spa;
1431789Sahrens 	int error;
14324451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1433789Sahrens 
14342926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1435789Sahrens 		return (error);
14364451Seschrock 	switch (zc->zc_cookie) {
14374451Seschrock 	case VDEV_STATE_ONLINE:
14384451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
14394451Seschrock 		break;
14404451Seschrock 
14414451Seschrock 	case VDEV_STATE_OFFLINE:
14424451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
14434451Seschrock 		break;
1444789Sahrens 
14454451Seschrock 	case VDEV_STATE_FAULTED:
144610817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
144710817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
144810817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
144910817SEric.Schrock@Sun.COM 
145010817SEric.Schrock@Sun.COM 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
14514451Seschrock 		break;
1452789Sahrens 
14534451Seschrock 	case VDEV_STATE_DEGRADED:
145410817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
145510817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
145610817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
145710817SEric.Schrock@Sun.COM 
145810817SEric.Schrock@Sun.COM 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
14594451Seschrock 		break;
14604451Seschrock 
14614451Seschrock 	default:
14624451Seschrock 		error = EINVAL;
14634451Seschrock 	}
14644451Seschrock 	zc->zc_cookie = newstate;
1465789Sahrens 	spa_close(spa, FTAG);
1466789Sahrens 	return (error);
1467789Sahrens }
1468789Sahrens 
1469789Sahrens static int
1470789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1471789Sahrens {
1472789Sahrens 	spa_t *spa;
1473789Sahrens 	int replacing = zc->zc_cookie;
1474789Sahrens 	nvlist_t *config;
1475789Sahrens 	int error;
1476789Sahrens 
14772926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1478789Sahrens 		return (error);
1479789Sahrens 
14805094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
14819643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
14821544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1483789Sahrens 		nvlist_free(config);
1484789Sahrens 	}
1485789Sahrens 
1486789Sahrens 	spa_close(spa, FTAG);
1487789Sahrens 	return (error);
1488789Sahrens }
1489789Sahrens 
1490789Sahrens static int
1491789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1492789Sahrens {
1493789Sahrens 	spa_t *spa;
1494789Sahrens 	int error;
1495789Sahrens 
14962926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1497789Sahrens 		return (error);
1498789Sahrens 
14998241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1500789Sahrens 
1501789Sahrens 	spa_close(spa, FTAG);
1502789Sahrens 	return (error);
1503789Sahrens }
1504789Sahrens 
1505789Sahrens static int
150611422SMark.Musante@Sun.COM zfs_ioc_vdev_split(zfs_cmd_t *zc)
150711422SMark.Musante@Sun.COM {
150811422SMark.Musante@Sun.COM 	spa_t *spa;
150911422SMark.Musante@Sun.COM 	nvlist_t *config, *props = NULL;
151011422SMark.Musante@Sun.COM 	int error;
151111422SMark.Musante@Sun.COM 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
151211422SMark.Musante@Sun.COM 
151311422SMark.Musante@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
151411422SMark.Musante@Sun.COM 		return (error);
151511422SMark.Musante@Sun.COM 
151611422SMark.Musante@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
151711422SMark.Musante@Sun.COM 	    zc->zc_iflags, &config)) {
151811422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
151911422SMark.Musante@Sun.COM 		return (error);
152011422SMark.Musante@Sun.COM 	}
152111422SMark.Musante@Sun.COM 
152211422SMark.Musante@Sun.COM 	if (zc->zc_nvlist_src_size != 0 && (error =
152311422SMark.Musante@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
152411422SMark.Musante@Sun.COM 	    zc->zc_iflags, &props))) {
152511422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
152611422SMark.Musante@Sun.COM 		nvlist_free(config);
152711422SMark.Musante@Sun.COM 		return (error);
152811422SMark.Musante@Sun.COM 	}
152911422SMark.Musante@Sun.COM 
153011422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
153111422SMark.Musante@Sun.COM 
153211422SMark.Musante@Sun.COM 	spa_close(spa, FTAG);
153311422SMark.Musante@Sun.COM 
153411422SMark.Musante@Sun.COM 	nvlist_free(config);
153511422SMark.Musante@Sun.COM 	nvlist_free(props);
153611422SMark.Musante@Sun.COM 
153711422SMark.Musante@Sun.COM 	return (error);
153811422SMark.Musante@Sun.COM }
153911422SMark.Musante@Sun.COM 
154011422SMark.Musante@Sun.COM static int
15411354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
15421354Seschrock {
15431354Seschrock 	spa_t *spa;
15442676Seschrock 	char *path = zc->zc_value;
15451544Seschrock 	uint64_t guid = zc->zc_guid;
15461354Seschrock 	int error;
15471354Seschrock 
15481354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
15491354Seschrock 	if (error != 0)
15501354Seschrock 		return (error);
15511354Seschrock 
15521354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
15531354Seschrock 	spa_close(spa, FTAG);
15541354Seschrock 	return (error);
15551354Seschrock }
15561354Seschrock 
15579425SEric.Schrock@Sun.COM static int
15589425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
15599425SEric.Schrock@Sun.COM {
15609425SEric.Schrock@Sun.COM 	spa_t *spa;
15619425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
15629425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
15639425SEric.Schrock@Sun.COM 	int error;
15649425SEric.Schrock@Sun.COM 
15659425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
15669425SEric.Schrock@Sun.COM 	if (error != 0)
15679425SEric.Schrock@Sun.COM 		return (error);
15689425SEric.Schrock@Sun.COM 
15699425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
15709425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
15719425SEric.Schrock@Sun.COM 	return (error);
15729425SEric.Schrock@Sun.COM }
15739425SEric.Schrock@Sun.COM 
15745367Sahrens /*
15755367Sahrens  * inputs:
15765367Sahrens  * zc_name		name of filesystem
15775367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15785367Sahrens  *
15795367Sahrens  * outputs:
15805367Sahrens  * zc_objset_stats	stats
15815367Sahrens  * zc_nvlist_dst	property nvlist
15825367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15835367Sahrens  */
15841354Seschrock static int
1585789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1586789Sahrens {
1587789Sahrens 	objset_t *os = NULL;
1588789Sahrens 	int error;
15891356Seschrock 	nvlist_t *nv;
1590789Sahrens 
159110298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1592789Sahrens 		return (error);
1593789Sahrens 
15942885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1595789Sahrens 
15962856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
159711022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
15982885Sahrens 		dmu_objset_stats(os, nv);
15993087Sahrens 		/*
16005147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
16013087Sahrens 		 * which we aren't supposed to do with a
16026689Smaybee 		 * DS_MODE_USER hold, because it could be
16033087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
160410298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
16053087Sahrens 		 */
16064577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
16074577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
16084577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
16094577Sahrens 		}
16102676Seschrock 		error = put_nvlist(zc, nv);
16111356Seschrock 		nvlist_free(nv);
16121356Seschrock 	}
1613789Sahrens 
161410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1615789Sahrens 	return (error);
1616789Sahrens }
1617789Sahrens 
161811022STom.Erickson@Sun.COM /*
161911022STom.Erickson@Sun.COM  * inputs:
162011022STom.Erickson@Sun.COM  * zc_name		name of filesystem
162111022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of buffer for property nvlist
162211022STom.Erickson@Sun.COM  *
162311022STom.Erickson@Sun.COM  * outputs:
162411022STom.Erickson@Sun.COM  * zc_nvlist_dst	received property nvlist
162511022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of received property nvlist
162611022STom.Erickson@Sun.COM  *
162711022STom.Erickson@Sun.COM  * Gets received properties (distinct from local properties on or after
162811022STom.Erickson@Sun.COM  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
162911022STom.Erickson@Sun.COM  * local property values.
163011022STom.Erickson@Sun.COM  */
163111022STom.Erickson@Sun.COM static int
163211022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
163311022STom.Erickson@Sun.COM {
163411022STom.Erickson@Sun.COM 	objset_t *os = NULL;
163511022STom.Erickson@Sun.COM 	int error;
163611022STom.Erickson@Sun.COM 	nvlist_t *nv;
163711022STom.Erickson@Sun.COM 
163811022STom.Erickson@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
163911022STom.Erickson@Sun.COM 		return (error);
164011022STom.Erickson@Sun.COM 
164111022STom.Erickson@Sun.COM 	/*
164211022STom.Erickson@Sun.COM 	 * Without this check, we would return local property values if the
164311022STom.Erickson@Sun.COM 	 * caller has not already received properties on or after
164411022STom.Erickson@Sun.COM 	 * SPA_VERSION_RECVD_PROPS.
164511022STom.Erickson@Sun.COM 	 */
164611022STom.Erickson@Sun.COM 	if (!dsl_prop_get_hasrecvd(os)) {
164711022STom.Erickson@Sun.COM 		dmu_objset_rele(os, FTAG);
164811022STom.Erickson@Sun.COM 		return (ENOTSUP);
164911022STom.Erickson@Sun.COM 	}
165011022STom.Erickson@Sun.COM 
165111022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != 0 &&
165211022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_received(os, &nv)) == 0) {
165311022STom.Erickson@Sun.COM 		error = put_nvlist(zc, nv);
165411022STom.Erickson@Sun.COM 		nvlist_free(nv);
165511022STom.Erickson@Sun.COM 	}
165611022STom.Erickson@Sun.COM 
165711022STom.Erickson@Sun.COM 	dmu_objset_rele(os, FTAG);
165811022STom.Erickson@Sun.COM 	return (error);
165911022STom.Erickson@Sun.COM }
166011022STom.Erickson@Sun.COM 
16615498Stimh static int
16625498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
16635498Stimh {
16645498Stimh 	uint64_t value;
16655498Stimh 	int error;
16665498Stimh 
16675498Stimh 	/*
16685498Stimh 	 * zfs_get_zplprop() will either find a value or give us
16695498Stimh 	 * the default value (if there is one).
16705498Stimh 	 */
16715498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
16725498Stimh 		return (error);
16735498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
16745498Stimh 	return (0);
16755498Stimh }
16765498Stimh 
16775498Stimh /*
16785498Stimh  * inputs:
16795498Stimh  * zc_name		name of filesystem
16805498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
16815498Stimh  *
16825498Stimh  * outputs:
16835498Stimh  * zc_nvlist_dst	zpl property nvlist
16845498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
16855498Stimh  */
16865498Stimh static int
16875498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
16885498Stimh {
16895498Stimh 	objset_t *os;
16905498Stimh 	int err;
16915498Stimh 
169210298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
169310298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
16945498Stimh 		return (err);
16955498Stimh 
16965498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
16975498Stimh 
16985498Stimh 	/*
16995498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
17006689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
17016689Smaybee 	 * hold, because it could be inconsistent.
17025498Stimh 	 */
17035498Stimh 	if (zc->zc_nvlist_dst != NULL &&
17045498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
17055498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
17065498Stimh 		nvlist_t *nv;
17075498Stimh 
17085498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17095498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
17105498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
17115498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
17125498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
17135498Stimh 			err = put_nvlist(zc, nv);
17145498Stimh 		nvlist_free(nv);
17155498Stimh 	} else {
17165498Stimh 		err = ENOENT;
17175498Stimh 	}
171810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
17195498Stimh 	return (err);
17205498Stimh }
17215498Stimh 
17229396SMatthew.Ahrens@Sun.COM static boolean_t
17239396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
17249396SMatthew.Ahrens@Sun.COM {
17259396SMatthew.Ahrens@Sun.COM 	/*
17269396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
17279396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
17289396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
17299396SMatthew.Ahrens@Sun.COM 	 */
17309396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
17319396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17329396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
17339396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17349396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
17359396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17369396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
17379396SMatthew.Ahrens@Sun.COM }
17389396SMatthew.Ahrens@Sun.COM 
17395367Sahrens /*
17405367Sahrens  * inputs:
17415367Sahrens  * zc_name		name of filesystem
17425367Sahrens  * zc_cookie		zap cursor
17435367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
17445367Sahrens  *
17455367Sahrens  * outputs:
17465367Sahrens  * zc_name		name of next filesystem
17479396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
17485367Sahrens  * zc_objset_stats	stats
17495367Sahrens  * zc_nvlist_dst	property nvlist
17505367Sahrens  * zc_nvlist_dst_size	size of property nvlist
17515367Sahrens  */
1752789Sahrens static int
1753789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1754789Sahrens {
1755885Sahrens 	objset_t *os;
1756789Sahrens 	int error;
1757789Sahrens 	char *p;
1758*11546SChris.Kirby@sun.com 	size_t orig_len = strlen(zc->zc_name);
1759*11546SChris.Kirby@sun.com 
1760*11546SChris.Kirby@sun.com top:
176110298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1762885Sahrens 		if (error == ENOENT)
1763885Sahrens 			error = ESRCH;
1764885Sahrens 		return (error);
1765789Sahrens 	}
1766789Sahrens 
1767789Sahrens 	p = strrchr(zc->zc_name, '/');
1768789Sahrens 	if (p == NULL || p[1] != '\0')
1769789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1770789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1771789Sahrens 
17728697SRichard.Morris@Sun.COM 	/*
17738697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
17748697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
17758697SRichard.Morris@Sun.COM 	 */
17768415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
17778415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
17788415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
17798415SRichard.Morris@Sun.COM 
17808415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
17818697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
17828415SRichard.Morris@Sun.COM 	}
17838415SRichard.Morris@Sun.COM 
1784789Sahrens 	do {
1785885Sahrens 		error = dmu_dir_list_next(os,
1786885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1787885Sahrens 		    NULL, &zc->zc_cookie);
1788789Sahrens 		if (error == ENOENT)
1789789Sahrens 			error = ESRCH;
179010588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
179110588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
179210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1793789Sahrens 
179410588SEric.Taylor@Sun.COM 	/*
179510588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
179610588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
179710588SEric.Taylor@Sun.COM 	 */
1798*11546SChris.Kirby@sun.com 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1799885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1800*11546SChris.Kirby@sun.com 		if (error == ENOENT) {
1801*11546SChris.Kirby@sun.com 			/* We lost a race with destroy, get the next one. */
1802*11546SChris.Kirby@sun.com 			zc->zc_name[orig_len] = '\0';
1803*11546SChris.Kirby@sun.com 			goto top;
1804*11546SChris.Kirby@sun.com 		}
1805*11546SChris.Kirby@sun.com 	}
1806789Sahrens 	return (error);
1807789Sahrens }
1808789Sahrens 
18095367Sahrens /*
18105367Sahrens  * inputs:
18115367Sahrens  * zc_name		name of filesystem
18125367Sahrens  * zc_cookie		zap cursor
18135367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
18145367Sahrens  *
18155367Sahrens  * outputs:
18165367Sahrens  * zc_name		name of next snapshot
18175367Sahrens  * zc_objset_stats	stats
18185367Sahrens  * zc_nvlist_dst	property nvlist
18195367Sahrens  * zc_nvlist_dst_size	size of property nvlist
18205367Sahrens  */
1821789Sahrens static int
1822789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1823789Sahrens {
1824885Sahrens 	objset_t *os;
1825789Sahrens 	int error;
1826789Sahrens 
1827*11546SChris.Kirby@sun.com top:
182810474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
182910474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
183010474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
183110474SRichard.Morris@Sun.COM 
183210298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
18336689Smaybee 	if (error)
18346689Smaybee 		return (error == ENOENT ? ESRCH : error);
1835789Sahrens 
18361003Slling 	/*
18371003Slling 	 * A dataset name of maximum length cannot have any snapshots,
18381003Slling 	 * so exit immediately.
18391003Slling 	 */
18401003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
184110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18421003Slling 		return (ESRCH);
1843789Sahrens 	}
1844789Sahrens 
1845885Sahrens 	error = dmu_snapshot_list_next(os,
1846885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
18475663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
184810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1849*11546SChris.Kirby@sun.com 	if (error == 0) {
1850885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1851*11546SChris.Kirby@sun.com 		if (error == ENOENT)  {
1852*11546SChris.Kirby@sun.com 			/* We lost a race with destroy, get the next one. */
1853*11546SChris.Kirby@sun.com 			*strchr(zc->zc_name, '@') = '\0';
1854*11546SChris.Kirby@sun.com 			goto top;
1855*11546SChris.Kirby@sun.com 		}
1856*11546SChris.Kirby@sun.com 	} else if (error == ENOENT) {
18576689Smaybee 		error = ESRCH;
1858*11546SChris.Kirby@sun.com 	}
1859789Sahrens 
18605367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
18616689Smaybee 	if (error)
18625367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1863789Sahrens 	return (error);
1864789Sahrens }
1865789Sahrens 
186611022STom.Erickson@Sun.COM static int
186711022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
186811022STom.Erickson@Sun.COM {
186911022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
187011022STom.Erickson@Sun.COM 	uint64_t *valary;
187111022STom.Erickson@Sun.COM 	unsigned int vallen;
187211022STom.Erickson@Sun.COM 	const char *domain;
187311022STom.Erickson@Sun.COM 	zfs_userquota_prop_t type;
187411022STom.Erickson@Sun.COM 	uint64_t rid;
187511022STom.Erickson@Sun.COM 	uint64_t quota;
187611022STom.Erickson@Sun.COM 	zfsvfs_t *zfsvfs;
187711022STom.Erickson@Sun.COM 	int err;
187811022STom.Erickson@Sun.COM 
187911022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
188011022STom.Erickson@Sun.COM 		nvlist_t *attrs;
188111022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
188211022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
188311022STom.Erickson@Sun.COM 		    &pair) == 0);
188411022STom.Erickson@Sun.COM 	}
188511022STom.Erickson@Sun.COM 
188611022STom.Erickson@Sun.COM 	VERIFY(nvpair_value_uint64_array(pair, &valary, &vallen) == 0);
188711022STom.Erickson@Sun.COM 	VERIFY(vallen == 3);
188811022STom.Erickson@Sun.COM 	type = valary[0];
188911022STom.Erickson@Sun.COM 	rid = valary[1];
189011022STom.Erickson@Sun.COM 	quota = valary[2];
189111022STom.Erickson@Sun.COM 	/*
189211022STom.Erickson@Sun.COM 	 * The propname is encoded as
189311022STom.Erickson@Sun.COM 	 * userquota@<rid>-<domain>.
189411022STom.Erickson@Sun.COM 	 */
189511022STom.Erickson@Sun.COM 	domain = strchr(propname, '-') + 1;
189611022STom.Erickson@Sun.COM 
189711022STom.Erickson@Sun.COM 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs);
189811022STom.Erickson@Sun.COM 	if (err == 0) {
189911022STom.Erickson@Sun.COM 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
190011022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
190111022STom.Erickson@Sun.COM 	}
190211022STom.Erickson@Sun.COM 
190311022STom.Erickson@Sun.COM 	return (err);
190411022STom.Erickson@Sun.COM }
190511022STom.Erickson@Sun.COM 
190611022STom.Erickson@Sun.COM /*
190711022STom.Erickson@Sun.COM  * If the named property is one that has a special function to set its value,
190811022STom.Erickson@Sun.COM  * return 0 on success and a positive error code on failure; otherwise if it is
190911022STom.Erickson@Sun.COM  * not one of the special properties handled by this function, return -1.
191011022STom.Erickson@Sun.COM  *
191111022STom.Erickson@Sun.COM  * XXX: It would be better for callers of the properety interface if we handled
191211022STom.Erickson@Sun.COM  * these special cases in dsl_prop.c (in the dsl layer).
191311022STom.Erickson@Sun.COM  */
191411022STom.Erickson@Sun.COM static int
191511022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source,
191611022STom.Erickson@Sun.COM     nvpair_t *pair)
1917789Sahrens {
191811022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
191911022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
192011022STom.Erickson@Sun.COM 	uint64_t intval;
192111022STom.Erickson@Sun.COM 	int err;
192211022STom.Erickson@Sun.COM 
192311022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
192411022STom.Erickson@Sun.COM 		if (zfs_prop_userquota(propname))
192511022STom.Erickson@Sun.COM 			return (zfs_prop_set_userquota(dsname, pair));
192611022STom.Erickson@Sun.COM 		return (-1);
192711022STom.Erickson@Sun.COM 	}
192811022STom.Erickson@Sun.COM 
192911022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
193011022STom.Erickson@Sun.COM 		nvlist_t *attrs;
193111022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
193211022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
193311022STom.Erickson@Sun.COM 		    &pair) == 0);
193411022STom.Erickson@Sun.COM 	}
193511022STom.Erickson@Sun.COM 
193611022STom.Erickson@Sun.COM 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
193711022STom.Erickson@Sun.COM 		return (-1);
193811022STom.Erickson@Sun.COM 
193911022STom.Erickson@Sun.COM 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
194011022STom.Erickson@Sun.COM 
194111022STom.Erickson@Sun.COM 	switch (prop) {
194211022STom.Erickson@Sun.COM 	case ZFS_PROP_QUOTA:
194311022STom.Erickson@Sun.COM 		err = dsl_dir_set_quota(dsname, source, intval);
194411022STom.Erickson@Sun.COM 		break;
194511022STom.Erickson@Sun.COM 	case ZFS_PROP_REFQUOTA:
194611022STom.Erickson@Sun.COM 		err = dsl_dataset_set_quota(dsname, source, intval);
194711022STom.Erickson@Sun.COM 		break;
194811022STom.Erickson@Sun.COM 	case ZFS_PROP_RESERVATION:
194911022STom.Erickson@Sun.COM 		err = dsl_dir_set_reservation(dsname, source, intval);
195011022STom.Erickson@Sun.COM 		break;
195111022STom.Erickson@Sun.COM 	case ZFS_PROP_REFRESERVATION:
195211022STom.Erickson@Sun.COM 		err = dsl_dataset_set_reservation(dsname, source, intval);
195311022STom.Erickson@Sun.COM 		break;
195411022STom.Erickson@Sun.COM 	case ZFS_PROP_VOLSIZE:
195511022STom.Erickson@Sun.COM 		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
195611022STom.Erickson@Sun.COM 		    intval);
195711022STom.Erickson@Sun.COM 		break;
195811022STom.Erickson@Sun.COM 	case ZFS_PROP_VERSION:
195911022STom.Erickson@Sun.COM 	{
196011022STom.Erickson@Sun.COM 		zfsvfs_t *zfsvfs;
196111022STom.Erickson@Sun.COM 
196211022STom.Erickson@Sun.COM 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0)
196311022STom.Erickson@Sun.COM 			break;
196411022STom.Erickson@Sun.COM 
196511022STom.Erickson@Sun.COM 		err = zfs_set_version(zfsvfs, intval);
196611022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
196711022STom.Erickson@Sun.COM 
196811022STom.Erickson@Sun.COM 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
196911147SGeorge.Wilson@Sun.COM 			zfs_cmd_t *zc;
197011147SGeorge.Wilson@Sun.COM 
197111147SGeorge.Wilson@Sun.COM 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
197211147SGeorge.Wilson@Sun.COM 			(void) strcpy(zc->zc_name, dsname);
197311147SGeorge.Wilson@Sun.COM 			(void) zfs_ioc_userspace_upgrade(zc);
197411147SGeorge.Wilson@Sun.COM 			kmem_free(zc, sizeof (zfs_cmd_t));
197511022STom.Erickson@Sun.COM 		}
197611022STom.Erickson@Sun.COM 		break;
197711022STom.Erickson@Sun.COM 	}
197811022STom.Erickson@Sun.COM 
197911022STom.Erickson@Sun.COM 	default:
198011022STom.Erickson@Sun.COM 		err = -1;
198111022STom.Erickson@Sun.COM 	}
198211022STom.Erickson@Sun.COM 
198311022STom.Erickson@Sun.COM 	return (err);
198411022STom.Erickson@Sun.COM }
198511022STom.Erickson@Sun.COM 
198611022STom.Erickson@Sun.COM /*
198711022STom.Erickson@Sun.COM  * This function is best effort. If it fails to set any of the given properties,
198811022STom.Erickson@Sun.COM  * it continues to set as many as it can and returns the first error
198911022STom.Erickson@Sun.COM  * encountered. If the caller provides a non-NULL errlist, it also gives the
199011022STom.Erickson@Sun.COM  * complete list of names of all the properties it failed to set along with the
199111022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
199211022STom.Erickson@Sun.COM  * returned errlist.
199311022STom.Erickson@Sun.COM  *
199411022STom.Erickson@Sun.COM  * If every property is set successfully, zero is returned and the list pointed
199511022STom.Erickson@Sun.COM  * at by errlist is NULL.
199611022STom.Erickson@Sun.COM  */
199711022STom.Erickson@Sun.COM int
199811022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
199911022STom.Erickson@Sun.COM     nvlist_t **errlist)
200011022STom.Erickson@Sun.COM {
200111022STom.Erickson@Sun.COM 	nvpair_t *pair;
200211022STom.Erickson@Sun.COM 	nvpair_t *propval;
200311045STom.Erickson@Sun.COM 	int rv = 0;
20042676Seschrock 	uint64_t intval;
20052676Seschrock 	char *strval;
20068697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
200711022STom.Erickson@Sun.COM 	nvlist_t *errors;
200811022STom.Erickson@Sun.COM 	nvlist_t *retrynvl;
20094543Smarks 
20108697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
201111022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
201211022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
201311022STom.Erickson@Sun.COM 
201411022STom.Erickson@Sun.COM retry:
201511022STom.Erickson@Sun.COM 	pair = NULL;
201611022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
201711022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
20184670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
201911181STom.Erickson@Sun.COM 		int err = 0;
20204543Smarks 
202111022STom.Erickson@Sun.COM 		/* decode the property value */
202211022STom.Erickson@Sun.COM 		propval = pair;
202311022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
202411022STom.Erickson@Sun.COM 			nvlist_t *attrs;
202511022STom.Erickson@Sun.COM 			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
202611022STom.Erickson@Sun.COM 			VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
202711022STom.Erickson@Sun.COM 			    &propval) == 0);
20284543Smarks 		}
20292676Seschrock 
203011022STom.Erickson@Sun.COM 		/* Validate value type */
203111022STom.Erickson@Sun.COM 		if (prop == ZPROP_INVAL) {
203211022STom.Erickson@Sun.COM 			if (zfs_prop_user(propname)) {
203311022STom.Erickson@Sun.COM 				if (nvpair_type(propval) != DATA_TYPE_STRING)
203411022STom.Erickson@Sun.COM 					err = EINVAL;
203511022STom.Erickson@Sun.COM 			} else if (zfs_prop_userquota(propname)) {
203611022STom.Erickson@Sun.COM 				if (nvpair_type(propval) !=
203711022STom.Erickson@Sun.COM 				    DATA_TYPE_UINT64_ARRAY)
203811022STom.Erickson@Sun.COM 					err = EINVAL;
20399396SMatthew.Ahrens@Sun.COM 			}
204011022STom.Erickson@Sun.COM 		} else {
204111022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
204211022STom.Erickson@Sun.COM 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
204311022STom.Erickson@Sun.COM 					err = EINVAL;
204411022STom.Erickson@Sun.COM 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
20452885Sahrens 				const char *unused;
20462885Sahrens 
204711022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
204811022STom.Erickson@Sun.COM 				    &intval) == 0);
20492676Seschrock 
20502676Seschrock 				switch (zfs_prop_get_type(prop)) {
20514787Sahrens 				case PROP_TYPE_NUMBER:
20522676Seschrock 					break;
20534787Sahrens 				case PROP_TYPE_STRING:
205411022STom.Erickson@Sun.COM 					err = EINVAL;
205511022STom.Erickson@Sun.COM 					break;
20564787Sahrens 				case PROP_TYPE_INDEX:
20572717Seschrock 					if (zfs_prop_index_to_string(prop,
205811022STom.Erickson@Sun.COM 					    intval, &unused) != 0)
205911022STom.Erickson@Sun.COM 						err = EINVAL;
20602676Seschrock 					break;
20612676Seschrock 				default:
20624577Sahrens 					cmn_err(CE_PANIC,
20634577Sahrens 					    "unknown property type");
20642676Seschrock 				}
20652676Seschrock 			} else {
206611022STom.Erickson@Sun.COM 				err = EINVAL;
206711022STom.Erickson@Sun.COM 			}
206811022STom.Erickson@Sun.COM 		}
206911022STom.Erickson@Sun.COM 
207011022STom.Erickson@Sun.COM 		/* Validate permissions */
207111022STom.Erickson@Sun.COM 		if (err == 0)
207211022STom.Erickson@Sun.COM 			err = zfs_check_settable(dsname, pair, CRED());
207311022STom.Erickson@Sun.COM 
207411022STom.Erickson@Sun.COM 		if (err == 0) {
207511022STom.Erickson@Sun.COM 			err = zfs_prop_set_special(dsname, source, pair);
207611022STom.Erickson@Sun.COM 			if (err == -1) {
207711022STom.Erickson@Sun.COM 				/*
207811022STom.Erickson@Sun.COM 				 * For better performance we build up a list of
207911022STom.Erickson@Sun.COM 				 * properties to set in a single transaction.
208011022STom.Erickson@Sun.COM 				 */
208111022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(genericnvl, pair);
208211022STom.Erickson@Sun.COM 			} else if (err != 0 && nvl != retrynvl) {
208311022STom.Erickson@Sun.COM 				/*
208411022STom.Erickson@Sun.COM 				 * This may be a spurious error caused by
208511022STom.Erickson@Sun.COM 				 * receiving quota and reservation out of order.
208611022STom.Erickson@Sun.COM 				 * Try again in a second pass.
208711022STom.Erickson@Sun.COM 				 */
208811022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(retrynvl, pair);
20892676Seschrock 			}
209011022STom.Erickson@Sun.COM 		}
209111022STom.Erickson@Sun.COM 
209211022STom.Erickson@Sun.COM 		if (err != 0)
209311022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
209411022STom.Erickson@Sun.COM 	}
209511022STom.Erickson@Sun.COM 
209611022STom.Erickson@Sun.COM 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
209711022STom.Erickson@Sun.COM 		nvl = retrynvl;
209811022STom.Erickson@Sun.COM 		goto retry;
209911022STom.Erickson@Sun.COM 	}
210011022STom.Erickson@Sun.COM 
210111022STom.Erickson@Sun.COM 	if (!nvlist_empty(genericnvl) &&
210211022STom.Erickson@Sun.COM 	    dsl_props_set(dsname, source, genericnvl) != 0) {
210311022STom.Erickson@Sun.COM 		/*
210411022STom.Erickson@Sun.COM 		 * If this fails, we still want to set as many properties as we
210511022STom.Erickson@Sun.COM 		 * can, so try setting them individually.
210611022STom.Erickson@Sun.COM 		 */
210711022STom.Erickson@Sun.COM 		pair = NULL;
210811022STom.Erickson@Sun.COM 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
210911022STom.Erickson@Sun.COM 			const char *propname = nvpair_name(pair);
211011181STom.Erickson@Sun.COM 			int err = 0;
211111022STom.Erickson@Sun.COM 
211211022STom.Erickson@Sun.COM 			propval = pair;
211311022STom.Erickson@Sun.COM 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
211411022STom.Erickson@Sun.COM 				nvlist_t *attrs;
211511022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
211611022STom.Erickson@Sun.COM 				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
211711022STom.Erickson@Sun.COM 				    &propval) == 0);
211811022STom.Erickson@Sun.COM 			}
211911022STom.Erickson@Sun.COM 
212011022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
212111022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_string(propval,
212211022STom.Erickson@Sun.COM 				    &strval) == 0);
212311022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 1,
212411022STom.Erickson@Sun.COM 				    strlen(strval) + 1, strval);
212511022STom.Erickson@Sun.COM 			} else {
212611022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
212711022STom.Erickson@Sun.COM 				    &intval) == 0);
212811022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 8,
212911022STom.Erickson@Sun.COM 				    1, &intval);
213011022STom.Erickson@Sun.COM 			}
213111022STom.Erickson@Sun.COM 
213211022STom.Erickson@Sun.COM 			if (err != 0) {
213311022STom.Erickson@Sun.COM 				VERIFY(nvlist_add_int32(errors, propname,
213411022STom.Erickson@Sun.COM 				    err) == 0);
213511022STom.Erickson@Sun.COM 			}
21362676Seschrock 		}
21372676Seschrock 	}
213811022STom.Erickson@Sun.COM 	nvlist_free(genericnvl);
213911022STom.Erickson@Sun.COM 	nvlist_free(retrynvl);
214011022STom.Erickson@Sun.COM 
214111022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
214211022STom.Erickson@Sun.COM 		nvlist_free(errors);
214311022STom.Erickson@Sun.COM 		errors = NULL;
214411022STom.Erickson@Sun.COM 	} else {
214511022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
21468697SRichard.Morris@Sun.COM 	}
214711022STom.Erickson@Sun.COM 
214811022STom.Erickson@Sun.COM 	if (errlist == NULL)
214911022STom.Erickson@Sun.COM 		nvlist_free(errors);
215011022STom.Erickson@Sun.COM 	else
215111022STom.Erickson@Sun.COM 		*errlist = errors;
215211022STom.Erickson@Sun.COM 
215311022STom.Erickson@Sun.COM 	return (rv);
2154789Sahrens }
2155789Sahrens 
21565367Sahrens /*
21579355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
21589355SMatthew.Ahrens@Sun.COM  */
21599355SMatthew.Ahrens@Sun.COM static int
21609355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
21619355SMatthew.Ahrens@Sun.COM {
216211022STom.Erickson@Sun.COM 	nvpair_t *pair = NULL;
21639355SMatthew.Ahrens@Sun.COM 	int error = 0;
21649355SMatthew.Ahrens@Sun.COM 
216511022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
216611022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
21679355SMatthew.Ahrens@Sun.COM 		char *valstr;
21689355SMatthew.Ahrens@Sun.COM 
21699355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
217011022STom.Erickson@Sun.COM 		    nvpair_type(pair) != DATA_TYPE_STRING)
21719355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
21729355SMatthew.Ahrens@Sun.COM 
21739355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
21749355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
21759355SMatthew.Ahrens@Sun.COM 			return (error);
21769355SMatthew.Ahrens@Sun.COM 
21779355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
21789355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
21799355SMatthew.Ahrens@Sun.COM 
218011022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
21819355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
21829355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
21839355SMatthew.Ahrens@Sun.COM 	}
21849355SMatthew.Ahrens@Sun.COM 	return (0);
21859355SMatthew.Ahrens@Sun.COM }
21869355SMatthew.Ahrens@Sun.COM 
218711022STom.Erickson@Sun.COM static void
218811022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
218911022STom.Erickson@Sun.COM {
219011022STom.Erickson@Sun.COM 	nvpair_t *pair;
219111022STom.Erickson@Sun.COM 
219211022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
219311022STom.Erickson@Sun.COM 
219411022STom.Erickson@Sun.COM 	pair = NULL;
219511022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
219611022STom.Erickson@Sun.COM 		if (nvlist_exists(skipped, nvpair_name(pair)))
219711022STom.Erickson@Sun.COM 			continue;
219811022STom.Erickson@Sun.COM 
219911022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
220011022STom.Erickson@Sun.COM 	}
220111022STom.Erickson@Sun.COM }
220211022STom.Erickson@Sun.COM 
220311022STom.Erickson@Sun.COM static int
220411022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
220511022STom.Erickson@Sun.COM     nvlist_t *skipped)
220611022STom.Erickson@Sun.COM {
220711022STom.Erickson@Sun.COM 	int err = 0;
220811022STom.Erickson@Sun.COM 	nvlist_t *cleared_props = NULL;
220911022STom.Erickson@Sun.COM 	props_skip(props, skipped, &cleared_props);
221011022STom.Erickson@Sun.COM 	if (!nvlist_empty(cleared_props)) {
221111022STom.Erickson@Sun.COM 		/*
221211022STom.Erickson@Sun.COM 		 * Acts on local properties until the dataset has received
221311022STom.Erickson@Sun.COM 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
221411022STom.Erickson@Sun.COM 		 */
221511022STom.Erickson@Sun.COM 		zprop_source_t flags = (ZPROP_SRC_NONE |
221611022STom.Erickson@Sun.COM 		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
221711022STom.Erickson@Sun.COM 		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
221811022STom.Erickson@Sun.COM 	}
221911022STom.Erickson@Sun.COM 	nvlist_free(cleared_props);
222011022STom.Erickson@Sun.COM 	return (err);
222111022STom.Erickson@Sun.COM }
222211022STom.Erickson@Sun.COM 
22239355SMatthew.Ahrens@Sun.COM /*
22245367Sahrens  * inputs:
22255367Sahrens  * zc_name		name of filesystem
22268697SRichard.Morris@Sun.COM  * zc_value		name of property to set
22275367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
222811022STom.Erickson@Sun.COM  * zc_cookie		received properties flag
22295367Sahrens  *
223011022STom.Erickson@Sun.COM  * outputs:
223111022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
22325367Sahrens  */
2233789Sahrens static int
22342676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2235789Sahrens {
22362676Seschrock 	nvlist_t *nvl;
223711022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
223811022STom.Erickson@Sun.COM 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
223911022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL);
224011022STom.Erickson@Sun.COM 	nvlist_t *errors = NULL;
22412676Seschrock 	int error;
2242789Sahrens 
22435094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
22449643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
22452676Seschrock 		return (error);
22462676Seschrock 
224711022STom.Erickson@Sun.COM 	if (received) {
22487265Sahrens 		nvlist_t *origprops;
22497265Sahrens 		objset_t *os;
22507265Sahrens 
225110298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
225211022STom.Erickson@Sun.COM 			if (dsl_prop_get_received(os, &origprops) == 0) {
225311022STom.Erickson@Sun.COM 				(void) clear_received_props(os,
225411022STom.Erickson@Sun.COM 				    zc->zc_name, origprops, nvl);
22557265Sahrens 				nvlist_free(origprops);
22567265Sahrens 			}
225711022STom.Erickson@Sun.COM 
225811022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
225910298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
22607265Sahrens 		}
22617265Sahrens 	}
22627265Sahrens 
226311022STom.Erickson@Sun.COM 	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
226411022STom.Erickson@Sun.COM 
226511022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
226611022STom.Erickson@Sun.COM 		(void) put_nvlist(zc, errors);
226711022STom.Erickson@Sun.COM 	}
226811022STom.Erickson@Sun.COM 
226911022STom.Erickson@Sun.COM 	nvlist_free(errors);
22702676Seschrock 	nvlist_free(nvl);
22712676Seschrock 	return (error);
2272789Sahrens }
2273789Sahrens 
22745367Sahrens /*
22755367Sahrens  * inputs:
22765367Sahrens  * zc_name		name of filesystem
22775367Sahrens  * zc_value		name of property to inherit
227811022STom.Erickson@Sun.COM  * zc_cookie		revert to received value if TRUE
22795367Sahrens  *
22805367Sahrens  * outputs:		none
22815367Sahrens  */
2282789Sahrens static int
22834849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
22844849Sahrens {
228511022STom.Erickson@Sun.COM 	const char *propname = zc->zc_value;
228611022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
228711022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
228811022STom.Erickson@Sun.COM 	zprop_source_t source = (received
228911022STom.Erickson@Sun.COM 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
229011022STom.Erickson@Sun.COM 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
229111022STom.Erickson@Sun.COM 
229211022STom.Erickson@Sun.COM 	if (received) {
229311022STom.Erickson@Sun.COM 		nvlist_t *dummy;
229411022STom.Erickson@Sun.COM 		nvpair_t *pair;
229511022STom.Erickson@Sun.COM 		zprop_type_t type;
229611022STom.Erickson@Sun.COM 		int err;
229711022STom.Erickson@Sun.COM 
229811022STom.Erickson@Sun.COM 		/*
229911022STom.Erickson@Sun.COM 		 * zfs_prop_set_special() expects properties in the form of an
230011022STom.Erickson@Sun.COM 		 * nvpair with type info.
230111022STom.Erickson@Sun.COM 		 */
230211022STom.Erickson@Sun.COM 		if (prop == ZPROP_INVAL) {
230311022STom.Erickson@Sun.COM 			if (!zfs_prop_user(propname))
230411022STom.Erickson@Sun.COM 				return (EINVAL);
230511022STom.Erickson@Sun.COM 
230611022STom.Erickson@Sun.COM 			type = PROP_TYPE_STRING;
230711515STom.Erickson@Sun.COM 		} else if (prop == ZFS_PROP_VOLSIZE ||
230811515STom.Erickson@Sun.COM 		    prop == ZFS_PROP_VERSION) {
230911515STom.Erickson@Sun.COM 			return (EINVAL);
231011022STom.Erickson@Sun.COM 		} else {
231111022STom.Erickson@Sun.COM 			type = zfs_prop_get_type(prop);
231211022STom.Erickson@Sun.COM 		}
231311022STom.Erickson@Sun.COM 
231411022STom.Erickson@Sun.COM 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
231511022STom.Erickson@Sun.COM 
231611022STom.Erickson@Sun.COM 		switch (type) {
231711022STom.Erickson@Sun.COM 		case PROP_TYPE_STRING:
231811022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
231911022STom.Erickson@Sun.COM 			break;
232011022STom.Erickson@Sun.COM 		case PROP_TYPE_NUMBER:
232111022STom.Erickson@Sun.COM 		case PROP_TYPE_INDEX:
232211022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
232311022STom.Erickson@Sun.COM 			break;
232411022STom.Erickson@Sun.COM 		default:
232511022STom.Erickson@Sun.COM 			nvlist_free(dummy);
232611022STom.Erickson@Sun.COM 			return (EINVAL);
232711022STom.Erickson@Sun.COM 		}
232811022STom.Erickson@Sun.COM 
232911022STom.Erickson@Sun.COM 		pair = nvlist_next_nvpair(dummy, NULL);
233011022STom.Erickson@Sun.COM 		err = zfs_prop_set_special(zc->zc_name, source, pair);
233111022STom.Erickson@Sun.COM 		nvlist_free(dummy);
233211022STom.Erickson@Sun.COM 		if (err != -1)
233311022STom.Erickson@Sun.COM 			return (err); /* special property already handled */
233411022STom.Erickson@Sun.COM 	} else {
233511022STom.Erickson@Sun.COM 		/*
233611022STom.Erickson@Sun.COM 		 * Only check this in the non-received case. We want to allow
233711022STom.Erickson@Sun.COM 		 * 'inherit -S' to revert non-inheritable properties like quota
233811022STom.Erickson@Sun.COM 		 * and reservation to the received or default values even though
233911022STom.Erickson@Sun.COM 		 * they are not considered inheritable.
234011022STom.Erickson@Sun.COM 		 */
234111022STom.Erickson@Sun.COM 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
234211022STom.Erickson@Sun.COM 			return (EINVAL);
234311022STom.Erickson@Sun.COM 	}
234411022STom.Erickson@Sun.COM 
23454849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
234611022STom.Erickson@Sun.COM 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
23474849Sahrens }
23484849Sahrens 
23494849Sahrens static int
23504098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
23513912Slling {
23525094Slling 	nvlist_t *props;
23533912Slling 	spa_t *spa;
23545094Slling 	int error;
235511022STom.Erickson@Sun.COM 	nvpair_t *pair;
235611022STom.Erickson@Sun.COM 
235711022STom.Erickson@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
235811022STom.Erickson@Sun.COM 	    zc->zc_iflags, &props))
23593912Slling 		return (error);
23603912Slling 
23618525SEric.Schrock@Sun.COM 	/*
23628525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
23638525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
23648525SEric.Schrock@Sun.COM 	 */
236511022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
236611022STom.Erickson@Sun.COM 	if (pair != NULL && strcmp(nvpair_name(pair),
23678525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
236811022STom.Erickson@Sun.COM 	    nvlist_next_nvpair(props, pair) == NULL) {
23698525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
23708525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
23718525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
23728525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
23738525SEric.Schrock@Sun.COM 		}
23748525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
237510672SEric.Schrock@Sun.COM 		if (spa != NULL) {
237610672SEric.Schrock@Sun.COM 			nvlist_free(props);
23778525SEric.Schrock@Sun.COM 			return (0);
237810672SEric.Schrock@Sun.COM 		}
23798525SEric.Schrock@Sun.COM 	}
23808525SEric.Schrock@Sun.COM 
23813912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
23825094Slling 		nvlist_free(props);
23833912Slling 		return (error);
23843912Slling 	}
23853912Slling 
23865094Slling 	error = spa_prop_set(spa, props);
23873912Slling 
23885094Slling 	nvlist_free(props);
23893912Slling 	spa_close(spa, FTAG);
23903912Slling 
23913912Slling 	return (error);
23923912Slling }
23933912Slling 
23943912Slling static int
23954098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
23963912Slling {
23973912Slling 	spa_t *spa;
23983912Slling 	int error;
23993912Slling 	nvlist_t *nvp = NULL;
24003912Slling 
24018525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
24028525SEric.Schrock@Sun.COM 		/*
24038525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
24048525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
24058525SEric.Schrock@Sun.COM 		 * anyway.
24068525SEric.Schrock@Sun.COM 		 */
24078525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
24088525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
24098525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
24108525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
24118525SEric.Schrock@Sun.COM 	} else {
24128525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
24138525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
24148525SEric.Schrock@Sun.COM 	}
24153912Slling 
24163912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
24173912Slling 		error = put_nvlist(zc, nvp);
24183912Slling 	else
24193912Slling 		error = EFAULT;
24203912Slling 
24218525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
24223912Slling 	return (error);
24233912Slling }
24243912Slling 
24253912Slling static int
24264543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
24274543Smarks {
24284543Smarks 	nvlist_t *nvp;
24294543Smarks 	int error;
24304543Smarks 	uint32_t uid;
24314543Smarks 	uint32_t gid;
24324543Smarks 	uint32_t *groups;
24334543Smarks 	uint_t group_cnt;
24344543Smarks 	cred_t	*usercred;
24354543Smarks 
24365094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24379643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
24384543Smarks 		return (error);
24394543Smarks 	}
24404543Smarks 
24414543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
24424543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
24434543Smarks 		nvlist_free(nvp);
24444543Smarks 		return (EPERM);
24454543Smarks 	}
24464543Smarks 
24474543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
24484543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
24494543Smarks 		nvlist_free(nvp);
24504543Smarks 		return (EPERM);
24514543Smarks 	}
24524543Smarks 
24534543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
24544543Smarks 	    &groups, &group_cnt)) != 0) {
24554543Smarks 		nvlist_free(nvp);
24564543Smarks 		return (EPERM);
24574543Smarks 	}
24584543Smarks 	usercred = cralloc();
24594543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
24604543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
24614543Smarks 		nvlist_free(nvp);
24624543Smarks 		crfree(usercred);
24634543Smarks 		return (EPERM);
24644543Smarks 	}
24654543Smarks 	nvlist_free(nvp);
24664543Smarks 	error = dsl_deleg_access(zc->zc_name,
24674787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
24684543Smarks 	crfree(usercred);
24694543Smarks 	return (error);
24704543Smarks }
24714543Smarks 
24725367Sahrens /*
24735367Sahrens  * inputs:
24745367Sahrens  * zc_name		name of filesystem
24755367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
24765367Sahrens  * zc_perm_action	allow/unallow flag
24775367Sahrens  *
24785367Sahrens  * outputs:		none
24795367Sahrens  */
24804543Smarks static int
24814543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
24824543Smarks {
24834543Smarks 	int error;
24844543Smarks 	nvlist_t *fsaclnv = NULL;
24854543Smarks 
24865094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24879643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
24884543Smarks 		return (error);
24894543Smarks 
24904543Smarks 	/*
24914543Smarks 	 * Verify nvlist is constructed correctly
24924543Smarks 	 */
24934543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
24944543Smarks 		nvlist_free(fsaclnv);
24954543Smarks 		return (EINVAL);
24964543Smarks 	}
24974543Smarks 
24984543Smarks 	/*
24994543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
25004543Smarks 	 * that user is allowed to hand out each permission in
25014543Smarks 	 * the nvlist(s)
25024543Smarks 	 */
25034543Smarks 
25044787Sahrens 	error = secpolicy_zfs(CRED());
25054543Smarks 	if (error) {
25064787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
25074787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
25084787Sahrens 			    fsaclnv, CRED());
25094787Sahrens 		} else {
25104787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
25114787Sahrens 			    fsaclnv, CRED());
25124787Sahrens 		}
25134543Smarks 	}
25144543Smarks 
25154543Smarks 	if (error == 0)
25164543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
25174543Smarks 
25184543Smarks 	nvlist_free(fsaclnv);
25194543Smarks 	return (error);
25204543Smarks }
25214543Smarks 
25225367Sahrens /*
25235367Sahrens  * inputs:
25245367Sahrens  * zc_name		name of filesystem
25255367Sahrens  *
25265367Sahrens  * outputs:
25275367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
25285367Sahrens  */
25294543Smarks static int
25304543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
25314543Smarks {
25324543Smarks 	nvlist_t *nvp;
25334543Smarks 	int error;
25344543Smarks 
25354543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
25364543Smarks 		error = put_nvlist(zc, nvp);
25374543Smarks 		nvlist_free(nvp);
25384543Smarks 	}
25394543Smarks 
25404543Smarks 	return (error);
25414543Smarks }
25424543Smarks 
25435367Sahrens /*
2544789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2545789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2546789Sahrens  * is responsible for releasing the returned vfs pointer.
2547789Sahrens  */
2548789Sahrens static vfs_t *
2549789Sahrens zfs_get_vfs(const char *resource)
2550789Sahrens {
2551789Sahrens 	struct vfs *vfsp;
2552789Sahrens 	struct vfs *vfs_found = NULL;
2553789Sahrens 
2554789Sahrens 	vfs_list_read_lock();
2555789Sahrens 	vfsp = rootvfs;
2556789Sahrens 	do {
2557789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2558789Sahrens 			VFS_HOLD(vfsp);
2559789Sahrens 			vfs_found = vfsp;
2560789Sahrens 			break;
2561789Sahrens 		}
2562789Sahrens 		vfsp = vfsp->vfs_next;
2563789Sahrens 	} while (vfsp != rootvfs);
2564789Sahrens 	vfs_list_unlock();
2565789Sahrens 	return (vfs_found);
2566789Sahrens }
2567789Sahrens 
25684543Smarks /* ARGSUSED */
2569789Sahrens static void
25704543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2571789Sahrens {
25725331Samw 	zfs_creat_t *zct = arg;
25735498Stimh 
25745498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
25755331Samw }
25765331Samw 
25775498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
25785498Stimh 
25795331Samw /*
25805498Stimh  * inputs:
25817184Stimh  * createprops		list of properties requested by creator
25827184Stimh  * default_zplver	zpl version to use if unspecified in createprops
25837184Stimh  * fuids_ok		fuids allowed in this version of the spa?
25847184Stimh  * os			parent objset pointer (NULL if root fs)
25855331Samw  *
25865498Stimh  * outputs:
25875498Stimh  * zplprops	values for the zplprops we attach to the master node object
25887184Stimh  * is_ci	true if requested file system will be purely case-insensitive
25895331Samw  *
25905498Stimh  * Determine the settings for utf8only, normalization and
25915498Stimh  * casesensitivity.  Specific values may have been requested by the
25925498Stimh  * creator and/or we can inherit values from the parent dataset.  If
25935498Stimh  * the file system is of too early a vintage, a creator can not
25945498Stimh  * request settings for these properties, even if the requested
25955498Stimh  * setting is the default value.  We don't actually want to create dsl
25965498Stimh  * properties for these, so remove them from the source nvlist after
25975498Stimh  * processing.
25985331Samw  */
25995331Samw static int
26009396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
26017184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
26027184Stimh     boolean_t *is_ci)
26035331Samw {
26045498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
26055498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
26065498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
26075498Stimh 
26085498Stimh 	ASSERT(zplprops != NULL);
26095498Stimh 
26105375Stimh 	/*
26115498Stimh 	 * Pull out creator prop choices, if any.
26125375Stimh 	 */
26135498Stimh 	if (createprops) {
26145498Stimh 		(void) nvlist_lookup_uint64(createprops,
26157184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
26167184Stimh 		(void) nvlist_lookup_uint64(createprops,
26175498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
26185498Stimh 		(void) nvlist_remove_all(createprops,
26195498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
26205498Stimh 		(void) nvlist_lookup_uint64(createprops,
26215498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
26225498Stimh 		(void) nvlist_remove_all(createprops,
26235498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
26245498Stimh 		(void) nvlist_lookup_uint64(createprops,
26255498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
26265498Stimh 		(void) nvlist_remove_all(createprops,
26275498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
26285331Samw 	}
26295331Samw 
26305375Stimh 	/*
26317184Stimh 	 * If the zpl version requested is whacky or the file system
26327184Stimh 	 * or pool is version is too "young" to support normalization
26337184Stimh 	 * and the creator tried to set a value for one of the props,
26347184Stimh 	 * error out.
26355498Stimh 	 */
26367184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
26377184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
26387184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
26395498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
26407184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
26415498Stimh 		return (ENOTSUP);
26425498Stimh 
26435498Stimh 	/*
26445498Stimh 	 * Put the version in the zplprops
26455498Stimh 	 */
26465498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26475498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
26485498Stimh 
26495498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
26505498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
26515498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26525498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
26535498Stimh 
26545498Stimh 	/*
26555498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
26565498Stimh 	 */
26575498Stimh 	if (norm)
26585498Stimh 		u8 = 1;
26595498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
26605498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
26615498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26625498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
26635498Stimh 
26645498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
26655498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
26665498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26675498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
26685498Stimh 
26696492Stimh 	if (is_ci)
26706492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
26716492Stimh 
26727184Stimh 	return (0);
26737184Stimh }
26747184Stimh 
26757184Stimh static int
26767184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
26777184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
26787184Stimh {
26797184Stimh 	boolean_t fuids_ok = B_TRUE;
26807184Stimh 	uint64_t zplver = ZPL_VERSION;
26817184Stimh 	objset_t *os = NULL;
26827184Stimh 	char parentname[MAXNAMELEN];
26837184Stimh 	char *cp;
26847184Stimh 	int error;
26857184Stimh 
26867184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
26877184Stimh 	cp = strrchr(parentname, '/');
26887184Stimh 	ASSERT(cp != NULL);
26897184Stimh 	cp[0] = '\0';
26907184Stimh 
26919396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
26929396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
26937184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
26947184Stimh 		zplver = ZPL_VERSION_FUID - 1;
26957184Stimh 		fuids_ok = B_FALSE;
26967184Stimh 	}
26977184Stimh 
26987184Stimh 	/*
26997184Stimh 	 * Open parent object set so we can inherit zplprop values.
27007184Stimh 	 */
270110298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
27027184Stimh 		return (error);
27037184Stimh 
27047184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
27057184Stimh 	    zplprops, is_ci);
270610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
27077184Stimh 	return (error);
27087184Stimh }
27097184Stimh 
27107184Stimh static int
27117184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
27127184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
27137184Stimh {
27147184Stimh 	boolean_t fuids_ok = B_TRUE;
27157184Stimh 	uint64_t zplver = ZPL_VERSION;
27167184Stimh 	int error;
27177184Stimh 
27187184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
27197184Stimh 		zplver = ZPL_VERSION_FUID - 1;
27207184Stimh 		fuids_ok = B_FALSE;
27217184Stimh 	}
27227184Stimh 
27237184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
27247184Stimh 	    zplprops, is_ci);
27257184Stimh 	return (error);
2726789Sahrens }
2727789Sahrens 
27285367Sahrens /*
27295367Sahrens  * inputs:
27305367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
27315367Sahrens  * zc_name		name of new objset
27325367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
27335367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27345367Sahrens  *
27355498Stimh  * outputs: none
27365367Sahrens  */
2737789Sahrens static int
2738789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2739789Sahrens {
2740789Sahrens 	objset_t *clone;
2741789Sahrens 	int error = 0;
27425331Samw 	zfs_creat_t zct;
27434543Smarks 	nvlist_t *nvprops = NULL;
27444543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2745789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2746789Sahrens 
2747789Sahrens 	switch (type) {
2748789Sahrens 
2749789Sahrens 	case DMU_OST_ZFS:
2750789Sahrens 		cbfunc = zfs_create_cb;
2751789Sahrens 		break;
2752789Sahrens 
2753789Sahrens 	case DMU_OST_ZVOL:
2754789Sahrens 		cbfunc = zvol_create_cb;
2755789Sahrens 		break;
2756789Sahrens 
2757789Sahrens 	default:
27582199Sahrens 		cbfunc = NULL;
27596423Sgw25295 		break;
27602199Sahrens 	}
27615326Sek110237 	if (strchr(zc->zc_name, '@') ||
27625326Sek110237 	    strchr(zc->zc_name, '%'))
2763789Sahrens 		return (EINVAL);
2764789Sahrens 
27652676Seschrock 	if (zc->zc_nvlist_src != NULL &&
27665094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27679643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
27682676Seschrock 		return (error);
27692676Seschrock 
27705498Stimh 	zct.zct_zplprops = NULL;
27715331Samw 	zct.zct_props = nvprops;
27725331Samw 
27732676Seschrock 	if (zc->zc_value[0] != '\0') {
2774789Sahrens 		/*
2775789Sahrens 		 * We're creating a clone of an existing snapshot.
2776789Sahrens 		 */
27772676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
27782676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
27794543Smarks 			nvlist_free(nvprops);
2780789Sahrens 			return (EINVAL);
27812676Seschrock 		}
2782789Sahrens 
278310298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
27842676Seschrock 		if (error) {
27854543Smarks 			nvlist_free(nvprops);
2786789Sahrens 			return (error);
27872676Seschrock 		}
27886492Stimh 
278910272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
279010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
27915331Samw 		if (error) {
27925331Samw 			nvlist_free(nvprops);
27935331Samw 			return (error);
27945331Samw 		}
2795789Sahrens 	} else {
27966492Stimh 		boolean_t is_insensitive = B_FALSE;
27976492Stimh 
27982676Seschrock 		if (cbfunc == NULL) {
27994543Smarks 			nvlist_free(nvprops);
28002199Sahrens 			return (EINVAL);
28012676Seschrock 		}
28022676Seschrock 
2803789Sahrens 		if (type == DMU_OST_ZVOL) {
28042676Seschrock 			uint64_t volsize, volblocksize;
28052676Seschrock 
28064543Smarks 			if (nvprops == NULL ||
28074543Smarks 			    nvlist_lookup_uint64(nvprops,
28082676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
28092676Seschrock 			    &volsize) != 0) {
28104543Smarks 				nvlist_free(nvprops);
28112676Seschrock 				return (EINVAL);
28122676Seschrock 			}
28132676Seschrock 
28144543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
28152676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
28162676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
28174543Smarks 				nvlist_free(nvprops);
28182676Seschrock 				return (EINVAL);
28192676Seschrock 			}
28201133Seschrock 
28212676Seschrock 			if (error != 0)
28222676Seschrock 				volblocksize = zfs_prop_default_numeric(
28232676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
28242676Seschrock 
28252676Seschrock 			if ((error = zvol_check_volblocksize(
28262676Seschrock 			    volblocksize)) != 0 ||
28272676Seschrock 			    (error = zvol_check_volsize(volsize,
28282676Seschrock 			    volblocksize)) != 0) {
28294543Smarks 				nvlist_free(nvprops);
2830789Sahrens 				return (error);
28312676Seschrock 			}
28324577Sahrens 		} else if (type == DMU_OST_ZFS) {
28335331Samw 			int error;
28345331Samw 
28355498Stimh 			/*
28365331Samw 			 * We have to have normalization and
28375331Samw 			 * case-folding flags correct when we do the
28385331Samw 			 * file system creation, so go figure them out
28395498Stimh 			 * now.
28405331Samw 			 */
28415498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
28425498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
28435498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
28447184Stimh 			    zct.zct_zplprops, &is_insensitive);
28455331Samw 			if (error != 0) {
28465331Samw 				nvlist_free(nvprops);
28475498Stimh 				nvlist_free(zct.zct_zplprops);
28485331Samw 				return (error);
28494577Sahrens 			}
28502676Seschrock 		}
285110272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
28526492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
28535498Stimh 		nvlist_free(zct.zct_zplprops);
2854789Sahrens 	}
28552676Seschrock 
28562676Seschrock 	/*
28572676Seschrock 	 * It would be nice to do this atomically.
28582676Seschrock 	 */
28592676Seschrock 	if (error == 0) {
286011022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
286111022STom.Erickson@Sun.COM 		    nvprops, NULL);
286211022STom.Erickson@Sun.COM 		if (error != 0)
286310242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
28642676Seschrock 	}
28654543Smarks 	nvlist_free(nvprops);
2866789Sahrens 	return (error);
2867789Sahrens }
2868789Sahrens 
28695367Sahrens /*
28705367Sahrens  * inputs:
28715367Sahrens  * zc_name	name of filesystem
28725367Sahrens  * zc_value	short name of snapshot
28735367Sahrens  * zc_cookie	recursive flag
28749396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
28755367Sahrens  *
287610588SEric.Taylor@Sun.COM  * outputs:
287710588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
28785367Sahrens  */
2879789Sahrens static int
28802199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
28812199Sahrens {
28827265Sahrens 	nvlist_t *nvprops = NULL;
28837265Sahrens 	int error;
28847265Sahrens 	boolean_t recursive = zc->zc_cookie;
28857265Sahrens 
28862676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
28872199Sahrens 		return (EINVAL);
28887265Sahrens 
28897265Sahrens 	if (zc->zc_nvlist_src != NULL &&
28907265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
28919643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
28927265Sahrens 		return (error);
28937265Sahrens 
28949355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
28959355SMatthew.Ahrens@Sun.COM 	if (error)
28969355SMatthew.Ahrens@Sun.COM 		goto out;
28979355SMatthew.Ahrens@Sun.COM 
289811022STom.Erickson@Sun.COM 	if (!nvlist_empty(nvprops) &&
28999355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
29009355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
29019355SMatthew.Ahrens@Sun.COM 		goto out;
29027265Sahrens 	}
29039355SMatthew.Ahrens@Sun.COM 
29049355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
29059355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
29069355SMatthew.Ahrens@Sun.COM 
29079355SMatthew.Ahrens@Sun.COM out:
29087265Sahrens 	nvlist_free(nvprops);
29097265Sahrens 	return (error);
29102199Sahrens }
29112199Sahrens 
29124007Smmusante int
291311209SMatthew.Ahrens@Sun.COM zfs_unmount_snap(const char *name, void *arg)
2914789Sahrens {
29152417Sahrens 	vfs_t *vfsp = NULL;
29162199Sahrens 
29176689Smaybee 	if (arg) {
29186689Smaybee 		char *snapname = arg;
291911209SMatthew.Ahrens@Sun.COM 		char *fullname = kmem_asprintf("%s@%s", name, snapname);
292011209SMatthew.Ahrens@Sun.COM 		vfsp = zfs_get_vfs(fullname);
292111209SMatthew.Ahrens@Sun.COM 		strfree(fullname);
29222417Sahrens 	} else if (strchr(name, '@')) {
29232199Sahrens 		vfsp = zfs_get_vfs(name);
29242199Sahrens 	}
29252199Sahrens 
29262199Sahrens 	if (vfsp) {
29272199Sahrens 		/*
29282199Sahrens 		 * Always force the unmount for snapshots.
29292199Sahrens 		 */
29302199Sahrens 		int flag = MS_FORCE;
2931789Sahrens 		int err;
2932789Sahrens 
29332199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
29342199Sahrens 			VFS_RELE(vfsp);
29352199Sahrens 			return (err);
29362199Sahrens 		}
29372199Sahrens 		VFS_RELE(vfsp);
29382199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
29392199Sahrens 			return (err);
29402199Sahrens 	}
29412199Sahrens 	return (0);
29422199Sahrens }
29432199Sahrens 
29445367Sahrens /*
29455367Sahrens  * inputs:
294610242Schris.kirby@sun.com  * zc_name		name of filesystem
294710242Schris.kirby@sun.com  * zc_value		short name of snapshot
294810242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
29495367Sahrens  *
29505367Sahrens  * outputs:	none
29515367Sahrens  */
29522199Sahrens static int
29532199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
29542199Sahrens {
29552199Sahrens 	int err;
2956789Sahrens 
29572676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
29582199Sahrens 		return (EINVAL);
29592199Sahrens 	err = dmu_objset_find(zc->zc_name,
29602676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
29612199Sahrens 	if (err)
29622199Sahrens 		return (err);
296310242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
296410242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
29652199Sahrens }
29662199Sahrens 
29675367Sahrens /*
29685367Sahrens  * inputs:
29695367Sahrens  * zc_name		name of dataset to destroy
29705367Sahrens  * zc_objset_type	type of objset
297110242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
29725367Sahrens  *
29735367Sahrens  * outputs:		none
29745367Sahrens  */
29752199Sahrens static int
29762199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
29772199Sahrens {
297810588SEric.Taylor@Sun.COM 	int err;
29792199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
298010588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
29812199Sahrens 		if (err)
29822199Sahrens 			return (err);
2983789Sahrens 	}
2984789Sahrens 
298510588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
298610588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
298710693Schris.kirby@sun.com 		(void) zvol_remove_minor(zc->zc_name);
298810588SEric.Taylor@Sun.COM 	return (err);
2989789Sahrens }
2990789Sahrens 
29915367Sahrens /*
29925367Sahrens  * inputs:
29935446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
29945367Sahrens  *
29955367Sahrens  * outputs:	none
29965367Sahrens  */
2997789Sahrens static int
2998789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2999789Sahrens {
300010272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
30015446Sahrens 	int error;
300210272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
300310272SMatthew.Ahrens@Sun.COM 	char *clone_name;
300410272SMatthew.Ahrens@Sun.COM 
300510272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
300610272SMatthew.Ahrens@Sun.COM 	if (error)
300710272SMatthew.Ahrens@Sun.COM 		return (error);
300810272SMatthew.Ahrens@Sun.COM 
300910272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
301010272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
301110272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
301210272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
301310272SMatthew.Ahrens@Sun.COM 	}
301410272SMatthew.Ahrens@Sun.COM 
301510272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
301610272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
301710272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
301810272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
301910272SMatthew.Ahrens@Sun.COM 	}
30205446Sahrens 
30215446Sahrens 	/*
302210272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
30235446Sahrens 	 */
302410272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
302510272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
30265446Sahrens 	if (error)
302710272SMatthew.Ahrens@Sun.COM 		goto out;
302810272SMatthew.Ahrens@Sun.COM 
302910298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
303010272SMatthew.Ahrens@Sun.COM 	if (error)
303110272SMatthew.Ahrens@Sun.COM 		goto out;
303210272SMatthew.Ahrens@Sun.COM 
303310272SMatthew.Ahrens@Sun.COM 	/*
303410272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
303510272SMatthew.Ahrens@Sun.COM 	 */
30369396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
303710298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
30386083Sek110237 		if (error == 0) {
30396083Sek110237 			int resume_err;
30406083Sek110237 
304110272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
304210272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
304310272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
304410272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
304510272SMatthew.Ahrens@Sun.COM 				ds = NULL;
304610272SMatthew.Ahrens@Sun.COM 			} else {
304710272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
304810272SMatthew.Ahrens@Sun.COM 			}
304910298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
30506083Sek110237 			error = error ? error : resume_err;
30516083Sek110237 		}
30525446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
30535446Sahrens 	} else {
305410272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
305510272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
305610272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
305710272SMatthew.Ahrens@Sun.COM 			ds = NULL;
305810272SMatthew.Ahrens@Sun.COM 		} else {
305910272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
306010272SMatthew.Ahrens@Sun.COM 		}
30615446Sahrens 	}
306210272SMatthew.Ahrens@Sun.COM 
306310272SMatthew.Ahrens@Sun.COM 	/*
306410272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
306510272SMatthew.Ahrens@Sun.COM 	 */
306610272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
306710272SMatthew.Ahrens@Sun.COM 
306810272SMatthew.Ahrens@Sun.COM out:
306910272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
307010272SMatthew.Ahrens@Sun.COM 	if (ds)
307110272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
30725446Sahrens 	return (error);
3073789Sahrens }
3074789Sahrens 
30755367Sahrens /*
30765367Sahrens  * inputs:
30775367Sahrens  * zc_name	old name of dataset
30785367Sahrens  * zc_value	new name of dataset
30795367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
30805367Sahrens  *
30815367Sahrens  * outputs:	none
30825367Sahrens  */
3083789Sahrens static int
3084789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3085789Sahrens {
30864490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
30874007Smmusante 
30882676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
30895326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
30905326Sek110237 	    strchr(zc->zc_value, '%'))
3091789Sahrens 		return (EINVAL);
3092789Sahrens 
30934007Smmusante 	/*
30944007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
30954007Smmusante 	 * in which case the dataset code figures out which snapshots
30964007Smmusante 	 * to unmount.
30974007Smmusante 	 */
30984007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3099789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
31002199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
31012199Sahrens 		if (err)
31022199Sahrens 			return (err);
3103789Sahrens 	}
310410588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
310510588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
31064007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3107789Sahrens }
3108789Sahrens 
310911022STom.Erickson@Sun.COM static int
311011022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
311111022STom.Erickson@Sun.COM {
311211022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
311311022STom.Erickson@Sun.COM 	boolean_t issnap = (strchr(dsname, '@') != NULL);
311411022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
311511022STom.Erickson@Sun.COM 	uint64_t intval;
311611022STom.Erickson@Sun.COM 	int err;
311711022STom.Erickson@Sun.COM 
311811022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
311911022STom.Erickson@Sun.COM 		if (zfs_prop_user(propname)) {
312011022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname,
312111022STom.Erickson@Sun.COM 			    ZFS_DELEG_PERM_USERPROP, cr))
312211022STom.Erickson@Sun.COM 				return (err);
312311022STom.Erickson@Sun.COM 			return (0);
312411022STom.Erickson@Sun.COM 		}
312511022STom.Erickson@Sun.COM 
312611022STom.Erickson@Sun.COM 		if (!issnap && zfs_prop_userquota(propname)) {
312711022STom.Erickson@Sun.COM 			const char *perm = NULL;
312811022STom.Erickson@Sun.COM 			const char *uq_prefix =
312911022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
313011022STom.Erickson@Sun.COM 			const char *gq_prefix =
313111022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
313211022STom.Erickson@Sun.COM 
313311022STom.Erickson@Sun.COM 			if (strncmp(propname, uq_prefix,
313411022STom.Erickson@Sun.COM 			    strlen(uq_prefix)) == 0) {
313511022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_USERQUOTA;
313611022STom.Erickson@Sun.COM 			} else if (strncmp(propname, gq_prefix,
313711022STom.Erickson@Sun.COM 			    strlen(gq_prefix)) == 0) {
313811022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
313911022STom.Erickson@Sun.COM 			} else {
314011022STom.Erickson@Sun.COM 				/* USERUSED and GROUPUSED are read-only */
314111022STom.Erickson@Sun.COM 				return (EINVAL);
314211022STom.Erickson@Sun.COM 			}
314311022STom.Erickson@Sun.COM 
314411022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
314511022STom.Erickson@Sun.COM 				return (err);
314611022STom.Erickson@Sun.COM 			return (0);
314711022STom.Erickson@Sun.COM 		}
314811022STom.Erickson@Sun.COM 
314911022STom.Erickson@Sun.COM 		return (EINVAL);
315011022STom.Erickson@Sun.COM 	}
315111022STom.Erickson@Sun.COM 
315211022STom.Erickson@Sun.COM 	if (issnap)
315311022STom.Erickson@Sun.COM 		return (EINVAL);
315411022STom.Erickson@Sun.COM 
315511022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
315611022STom.Erickson@Sun.COM 		/*
315711022STom.Erickson@Sun.COM 		 * dsl_prop_get_all_impl() returns properties in this
315811022STom.Erickson@Sun.COM 		 * format.
315911022STom.Erickson@Sun.COM 		 */
316011022STom.Erickson@Sun.COM 		nvlist_t *attrs;
316111022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
316211022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
316311022STom.Erickson@Sun.COM 		    &pair) == 0);
316411022STom.Erickson@Sun.COM 	}
316511022STom.Erickson@Sun.COM 
316611022STom.Erickson@Sun.COM 	/*
316711022STom.Erickson@Sun.COM 	 * Check that this value is valid for this pool version
316811022STom.Erickson@Sun.COM 	 */
316911022STom.Erickson@Sun.COM 	switch (prop) {
317011022STom.Erickson@Sun.COM 	case ZFS_PROP_COMPRESSION:
317111022STom.Erickson@Sun.COM 		/*
317211022STom.Erickson@Sun.COM 		 * If the user specified gzip compression, make sure
317311022STom.Erickson@Sun.COM 		 * the SPA supports it. We ignore any errors here since
317411022STom.Erickson@Sun.COM 		 * we'll catch them later.
317511022STom.Erickson@Sun.COM 		 */
317611022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
317711022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
317811022STom.Erickson@Sun.COM 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
317911022STom.Erickson@Sun.COM 			    intval <= ZIO_COMPRESS_GZIP_9 &&
318011022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
318111022STom.Erickson@Sun.COM 			    SPA_VERSION_GZIP_COMPRESSION)) {
318211022STom.Erickson@Sun.COM 				return (ENOTSUP);
318311022STom.Erickson@Sun.COM 			}
318411022STom.Erickson@Sun.COM 
318511022STom.Erickson@Sun.COM 			if (intval == ZIO_COMPRESS_ZLE &&
318611022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
318711022STom.Erickson@Sun.COM 			    SPA_VERSION_ZLE_COMPRESSION))
318811022STom.Erickson@Sun.COM 				return (ENOTSUP);
318911022STom.Erickson@Sun.COM 
319011022STom.Erickson@Sun.COM 			/*
319111022STom.Erickson@Sun.COM 			 * If this is a bootable dataset then
319211022STom.Erickson@Sun.COM 			 * verify that the compression algorithm
319311022STom.Erickson@Sun.COM 			 * is supported for booting. We must return
319411022STom.Erickson@Sun.COM 			 * something other than ENOTSUP since it
319511022STom.Erickson@Sun.COM 			 * implies a downrev pool version.
319611022STom.Erickson@Sun.COM 			 */
319711022STom.Erickson@Sun.COM 			if (zfs_is_bootfs(dsname) &&
319811022STom.Erickson@Sun.COM 			    !BOOTFS_COMPRESS_VALID(intval)) {
319911022STom.Erickson@Sun.COM 				return (ERANGE);
320011022STom.Erickson@Sun.COM 			}
320111022STom.Erickson@Sun.COM 		}
320211022STom.Erickson@Sun.COM 		break;
320311022STom.Erickson@Sun.COM 
320411022STom.Erickson@Sun.COM 	case ZFS_PROP_COPIES:
320511022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
320611022STom.Erickson@Sun.COM 			return (ENOTSUP);
320711022STom.Erickson@Sun.COM 		break;
320811022STom.Erickson@Sun.COM 
320911022STom.Erickson@Sun.COM 	case ZFS_PROP_DEDUP:
321011022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
321111022STom.Erickson@Sun.COM 			return (ENOTSUP);
321211022STom.Erickson@Sun.COM 		break;
321311022STom.Erickson@Sun.COM 
321411022STom.Erickson@Sun.COM 	case ZFS_PROP_SHARESMB:
321511022STom.Erickson@Sun.COM 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
321611022STom.Erickson@Sun.COM 			return (ENOTSUP);
321711022STom.Erickson@Sun.COM 		break;
321811022STom.Erickson@Sun.COM 
321911022STom.Erickson@Sun.COM 	case ZFS_PROP_ACLINHERIT:
322011022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
322111022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
322211022STom.Erickson@Sun.COM 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
322311022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
322411022STom.Erickson@Sun.COM 			    SPA_VERSION_PASSTHROUGH_X))
322511022STom.Erickson@Sun.COM 				return (ENOTSUP);
322611022STom.Erickson@Sun.COM 		}
322711022STom.Erickson@Sun.COM 		break;
322811022STom.Erickson@Sun.COM 	}
322911022STom.Erickson@Sun.COM 
323011022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
323111022STom.Erickson@Sun.COM }
323211022STom.Erickson@Sun.COM 
323311022STom.Erickson@Sun.COM /*
323411022STom.Erickson@Sun.COM  * Removes properties from the given props list that fail permission checks
323511022STom.Erickson@Sun.COM  * needed to clear them and to restore them in case of a receive error. For each
323611022STom.Erickson@Sun.COM  * property, make sure we have both set and inherit permissions.
323711022STom.Erickson@Sun.COM  *
323811022STom.Erickson@Sun.COM  * Returns the first error encountered if any permission checks fail. If the
323911022STom.Erickson@Sun.COM  * caller provides a non-NULL errlist, it also gives the complete list of names
324011022STom.Erickson@Sun.COM  * of all the properties that failed a permission check along with the
324111022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
324211022STom.Erickson@Sun.COM  * returned errlist.
324311022STom.Erickson@Sun.COM  *
324411022STom.Erickson@Sun.COM  * If every property checks out successfully, zero is returned and the list
324511022STom.Erickson@Sun.COM  * pointed at by errlist is NULL.
324611022STom.Erickson@Sun.COM  */
324711022STom.Erickson@Sun.COM static int
324811022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
32496689Smaybee {
32506689Smaybee 	zfs_cmd_t *zc;
325111022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
325211022STom.Erickson@Sun.COM 	nvlist_t *errors;
325311022STom.Erickson@Sun.COM 	int err, rv = 0;
32546689Smaybee 
32556689Smaybee 	if (props == NULL)
325611022STom.Erickson@Sun.COM 		return (0);
325711022STom.Erickson@Sun.COM 
325811022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
325911022STom.Erickson@Sun.COM 
32606689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
32616689Smaybee 	(void) strcpy(zc->zc_name, dataset);
326211022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
326311022STom.Erickson@Sun.COM 	while (pair != NULL) {
326411022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
326511022STom.Erickson@Sun.COM 
326611022STom.Erickson@Sun.COM 		(void) strcpy(zc->zc_value, nvpair_name(pair));
326711022STom.Erickson@Sun.COM 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
326811022STom.Erickson@Sun.COM 		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
326911022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
327011022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors,
327111022STom.Erickson@Sun.COM 			    zc->zc_value, err) == 0);
327211022STom.Erickson@Sun.COM 		}
327311022STom.Erickson@Sun.COM 		pair = next_pair;
32746689Smaybee 	}
32756689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
327611022STom.Erickson@Sun.COM 
327711022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
327811022STom.Erickson@Sun.COM 		nvlist_free(errors);
327911022STom.Erickson@Sun.COM 		errors = NULL;
328011022STom.Erickson@Sun.COM 	} else {
328111022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
328211022STom.Erickson@Sun.COM 	}
328311022STom.Erickson@Sun.COM 
328411022STom.Erickson@Sun.COM 	if (errlist == NULL)
328511022STom.Erickson@Sun.COM 		nvlist_free(errors);
328611022STom.Erickson@Sun.COM 	else
328711022STom.Erickson@Sun.COM 		*errlist = errors;
328811022STom.Erickson@Sun.COM 
328911022STom.Erickson@Sun.COM 	return (rv);
32906689Smaybee }
32916689Smaybee 
329211022STom.Erickson@Sun.COM static boolean_t
329311022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2)
329411022STom.Erickson@Sun.COM {
329511022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
329611022STom.Erickson@Sun.COM 		/* dsl_prop_get_all_impl() format */
329711022STom.Erickson@Sun.COM 		nvlist_t *attrs;
329811022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
329911022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
330011022STom.Erickson@Sun.COM 		    &p1) == 0);
330111022STom.Erickson@Sun.COM 	}
330211022STom.Erickson@Sun.COM 
330311022STom.Erickson@Sun.COM 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
330411022STom.Erickson@Sun.COM 		nvlist_t *attrs;
330511022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
330611022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
330711022STom.Erickson@Sun.COM 		    &p2) == 0);
330811022STom.Erickson@Sun.COM 	}
330911022STom.Erickson@Sun.COM 
331011022STom.Erickson@Sun.COM 	if (nvpair_type(p1) != nvpair_type(p2))
331111022STom.Erickson@Sun.COM 		return (B_FALSE);
331211022STom.Erickson@Sun.COM 
331311022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
331411022STom.Erickson@Sun.COM 		char *valstr1, *valstr2;
331511022STom.Erickson@Sun.COM 
331611022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
331711022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
331811022STom.Erickson@Sun.COM 		return (strcmp(valstr1, valstr2) == 0);
331911022STom.Erickson@Sun.COM 	} else {
332011022STom.Erickson@Sun.COM 		uint64_t intval1, intval2;
332111022STom.Erickson@Sun.COM 
332211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
332311022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
332411022STom.Erickson@Sun.COM 		return (intval1 == intval2);
332511022STom.Erickson@Sun.COM 	}
332611022STom.Erickson@Sun.COM }
332711022STom.Erickson@Sun.COM 
332811022STom.Erickson@Sun.COM /*
332911022STom.Erickson@Sun.COM  * Remove properties from props if they are not going to change (as determined
333011022STom.Erickson@Sun.COM  * by comparison with origprops). Remove them from origprops as well, since we
333111022STom.Erickson@Sun.COM  * do not need to clear or restore properties that won't change.
333211022STom.Erickson@Sun.COM  */
333311022STom.Erickson@Sun.COM static void
333411022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops)
333511022STom.Erickson@Sun.COM {
333611022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
333711022STom.Erickson@Sun.COM 
333811022STom.Erickson@Sun.COM 	if (origprops == NULL)
333911022STom.Erickson@Sun.COM 		return; /* all props need to be received */
334011022STom.Erickson@Sun.COM 
334111022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
334211022STom.Erickson@Sun.COM 	while (pair != NULL) {
334311022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
334411022STom.Erickson@Sun.COM 		nvpair_t *match;
334511022STom.Erickson@Sun.COM 
334611022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
334711022STom.Erickson@Sun.COM 
334811022STom.Erickson@Sun.COM 		if ((nvlist_lookup_nvpair(origprops, propname,
334911022STom.Erickson@Sun.COM 		    &match) != 0) || !propval_equals(pair, match))
335011022STom.Erickson@Sun.COM 			goto next; /* need to set received value */
335111022STom.Erickson@Sun.COM 
335211022STom.Erickson@Sun.COM 		/* don't clear the existing received value */
335311022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(origprops, match);
335411022STom.Erickson@Sun.COM 		/* don't bother receiving the property */
335511022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(props, pair);
335611022STom.Erickson@Sun.COM next:
335711022STom.Erickson@Sun.COM 		pair = next_pair;
335811022STom.Erickson@Sun.COM 	}
335911022STom.Erickson@Sun.COM }
336011022STom.Erickson@Sun.COM 
336111022STom.Erickson@Sun.COM #ifdef	DEBUG
336211022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err;
336311022STom.Erickson@Sun.COM #endif
336411022STom.Erickson@Sun.COM 
33655367Sahrens /*
33665367Sahrens  * inputs:
33675367Sahrens  * zc_name		name of containing filesystem
33685367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
33695367Sahrens  * zc_value		name of snapshot to create
33705367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
33715367Sahrens  * zc_cookie		file descriptor to recv from
33725367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
33735367Sahrens  * zc_guid		force flag
33745367Sahrens  *
33755367Sahrens  * outputs:
33765367Sahrens  * zc_cookie		number of bytes read
337711022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
337811022STom.Erickson@Sun.COM  * zc_obj		zprop_errflags_t
33795367Sahrens  */
3380789Sahrens static int
33815367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3382789Sahrens {
3383789Sahrens 	file_t *fp;
33845326Sek110237 	objset_t *os;
33855367Sahrens 	dmu_recv_cookie_t drc;
33865326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
338711022STom.Erickson@Sun.COM 	int fd;
338811022STom.Erickson@Sun.COM 	int error = 0;
338911022STom.Erickson@Sun.COM 	int props_error = 0;
339011022STom.Erickson@Sun.COM 	nvlist_t *errors;
33915367Sahrens 	offset_t off;
339211022STom.Erickson@Sun.COM 	nvlist_t *props = NULL; /* sent properties */
339311022STom.Erickson@Sun.COM 	nvlist_t *origprops = NULL; /* existing properties */
33945367Sahrens 	objset_t *origin = NULL;
33955367Sahrens 	char *tosnap;
33965367Sahrens 	char tofs[ZFS_MAXNAMELEN];
339711022STom.Erickson@Sun.COM 	boolean_t first_recvd_props = B_FALSE;
3398789Sahrens 
33993265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
34005326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
34015326Sek110237 	    strchr(zc->zc_value, '%'))
34023265Sahrens 		return (EINVAL);
34033265Sahrens 
34045367Sahrens 	(void) strcpy(tofs, zc->zc_value);
34055367Sahrens 	tosnap = strchr(tofs, '@');
340611022STom.Erickson@Sun.COM 	*tosnap++ = '\0';
34075367Sahrens 
34085367Sahrens 	if (zc->zc_nvlist_src != NULL &&
34095367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
34109643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
34115367Sahrens 		return (error);
34125367Sahrens 
3413789Sahrens 	fd = zc->zc_cookie;
3414789Sahrens 	fp = getf(fd);
34155367Sahrens 	if (fp == NULL) {
34165367Sahrens 		nvlist_free(props);
3417789Sahrens 		return (EBADF);
34185367Sahrens 	}
34195326Sek110237 
342011022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
342111022STom.Erickson@Sun.COM 
342210298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
342311022STom.Erickson@Sun.COM 		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
342411022STom.Erickson@Sun.COM 		    !dsl_prop_get_hasrecvd(os)) {
342511022STom.Erickson@Sun.COM 			first_recvd_props = B_TRUE;
342611022STom.Erickson@Sun.COM 		}
342711022STom.Erickson@Sun.COM 
34286689Smaybee 		/*
342911022STom.Erickson@Sun.COM 		 * If new received properties are supplied, they are to
343011022STom.Erickson@Sun.COM 		 * completely replace the existing received properties, so stash
343111022STom.Erickson@Sun.COM 		 * away the existing ones.
34326689Smaybee 		 */
343311022STom.Erickson@Sun.COM 		if (dsl_prop_get_received(os, &origprops) == 0) {
343411022STom.Erickson@Sun.COM 			nvlist_t *errlist = NULL;
343511022STom.Erickson@Sun.COM 			/*
343611022STom.Erickson@Sun.COM 			 * Don't bother writing a property if its value won't
343711022STom.Erickson@Sun.COM 			 * change (and avoid the unnecessary security checks).
343811022STom.Erickson@Sun.COM 			 *
343911022STom.Erickson@Sun.COM 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
344011022STom.Erickson@Sun.COM 			 * special case where we blow away all local properties
344111022STom.Erickson@Sun.COM 			 * regardless.
344211022STom.Erickson@Sun.COM 			 */
344311022STom.Erickson@Sun.COM 			if (!first_recvd_props)
344411022STom.Erickson@Sun.COM 				props_reduce(props, origprops);
344511022STom.Erickson@Sun.COM 			if (zfs_check_clearable(tofs, origprops,
344611022STom.Erickson@Sun.COM 			    &errlist) != 0)
344711022STom.Erickson@Sun.COM 				(void) nvlist_merge(errors, errlist, 0);
344811022STom.Erickson@Sun.COM 			nvlist_free(errlist);
344911022STom.Erickson@Sun.COM 		}
345010298SMatthew.Ahrens@Sun.COM 
345110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
34525326Sek110237 	}
34535326Sek110237 
34545367Sahrens 	if (zc->zc_string[0]) {
345510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
34566689Smaybee 		if (error)
34576689Smaybee 			goto out;
34585367Sahrens 	}
34595367Sahrens 
346011007SLori.Alt@Sun.COM 	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
346111007SLori.Alt@Sun.COM 	    &zc->zc_begin_record, force, origin, &drc);
34625367Sahrens 	if (origin)
346310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
34646689Smaybee 	if (error)
34656689Smaybee 		goto out;
34665326Sek110237 
34675326Sek110237 	/*
346811022STom.Erickson@Sun.COM 	 * Set properties before we receive the stream so that they are applied
346911022STom.Erickson@Sun.COM 	 * to the new data. Note that we must call dmu_recv_stream() if
347011022STom.Erickson@Sun.COM 	 * dmu_recv_begin() succeeds.
34715326Sek110237 	 */
34725367Sahrens 	if (props) {
347311022STom.Erickson@Sun.COM 		nvlist_t *errlist;
347411022STom.Erickson@Sun.COM 
347511022STom.Erickson@Sun.COM 		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
347611022STom.Erickson@Sun.COM 			if (drc.drc_newfs) {
347711022STom.Erickson@Sun.COM 				if (spa_version(os->os_spa) >=
347811022STom.Erickson@Sun.COM 				    SPA_VERSION_RECVD_PROPS)
347911022STom.Erickson@Sun.COM 					first_recvd_props = B_TRUE;
348011022STom.Erickson@Sun.COM 			} else if (origprops != NULL) {
348111022STom.Erickson@Sun.COM 				if (clear_received_props(os, tofs, origprops,
348211022STom.Erickson@Sun.COM 				    first_recvd_props ? NULL : props) != 0)
348311022STom.Erickson@Sun.COM 					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
348411022STom.Erickson@Sun.COM 			} else {
348511022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
348611022STom.Erickson@Sun.COM 			}
348711022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
348811022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
348911022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
349011022STom.Erickson@Sun.COM 		}
349111022STom.Erickson@Sun.COM 
349211022STom.Erickson@Sun.COM 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
349311022STom.Erickson@Sun.COM 		    props, &errlist);
349411022STom.Erickson@Sun.COM 		(void) nvlist_merge(errors, errlist, 0);
349511022STom.Erickson@Sun.COM 		nvlist_free(errlist);
349611022STom.Erickson@Sun.COM 	}
349711022STom.Erickson@Sun.COM 
349811022STom.Erickson@Sun.COM 	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
34996689Smaybee 		/*
350011022STom.Erickson@Sun.COM 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
350111022STom.Erickson@Sun.COM 		 * size or supplied an invalid address.
35026689Smaybee 		 */
350311022STom.Erickson@Sun.COM 		props_error = EINVAL;
35045367Sahrens 	}
35055367Sahrens 
35065367Sahrens 	off = fp->f_offset;
35075367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
35085367Sahrens 
350910204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
351010204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
351110204SMatthew.Ahrens@Sun.COM 
351210204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
351310204SMatthew.Ahrens@Sun.COM 			/* online recv */
351410204SMatthew.Ahrens@Sun.COM 			int end_err;
351510298SMatthew.Ahrens@Sun.COM 
351610298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
351710204SMatthew.Ahrens@Sun.COM 			/*
351810204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
351910204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
352010204SMatthew.Ahrens@Sun.COM 			 */
352110204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
352210204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
352310204SMatthew.Ahrens@Sun.COM 				int resume_err =
352410298SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, tofs);
352510204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
352610204SMatthew.Ahrens@Sun.COM 			}
352710204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
352810204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
352910204SMatthew.Ahrens@Sun.COM 		} else {
35306689Smaybee 			error = dmu_recv_end(&drc);
35315367Sahrens 		}
35326083Sek110237 	}
35335367Sahrens 
35345367Sahrens 	zc->zc_cookie = off - fp->f_offset;
35355367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
35365367Sahrens 		fp->f_offset = off;
35372885Sahrens 
353811022STom.Erickson@Sun.COM #ifdef	DEBUG
353911022STom.Erickson@Sun.COM 	if (zfs_ioc_recv_inject_err) {
354011022STom.Erickson@Sun.COM 		zfs_ioc_recv_inject_err = B_FALSE;
354111022STom.Erickson@Sun.COM 		error = 1;
354211022STom.Erickson@Sun.COM 	}
354311022STom.Erickson@Sun.COM #endif
35446689Smaybee 	/*
35456689Smaybee 	 * On error, restore the original props.
35466689Smaybee 	 */
35476689Smaybee 	if (error && props) {
354811022STom.Erickson@Sun.COM 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
354911022STom.Erickson@Sun.COM 			if (clear_received_props(os, tofs, props, NULL) != 0) {
355011022STom.Erickson@Sun.COM 				/*
355111022STom.Erickson@Sun.COM 				 * We failed to clear the received properties.
355211022STom.Erickson@Sun.COM 				 * Since we may have left a $recvd value on the
355311022STom.Erickson@Sun.COM 				 * system, we can't clear the $hasrecvd flag.
355411022STom.Erickson@Sun.COM 				 */
355511022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NORESTORE;
355611022STom.Erickson@Sun.COM 			} else if (first_recvd_props) {
355711022STom.Erickson@Sun.COM 				dsl_prop_unset_hasrecvd(os);
355811022STom.Erickson@Sun.COM 			}
355911022STom.Erickson@Sun.COM 			dmu_objset_rele(os, FTAG);
356011022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
356111022STom.Erickson@Sun.COM 			/* We failed to clear the received properties. */
356211022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
356311022STom.Erickson@Sun.COM 		}
356411022STom.Erickson@Sun.COM 
356511022STom.Erickson@Sun.COM 		if (origprops == NULL && !drc.drc_newfs) {
356611022STom.Erickson@Sun.COM 			/* We failed to stash the original properties. */
356711022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
356811022STom.Erickson@Sun.COM 		}
356911022STom.Erickson@Sun.COM 
357011022STom.Erickson@Sun.COM 		/*
357111022STom.Erickson@Sun.COM 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
357211022STom.Erickson@Sun.COM 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
357311022STom.Erickson@Sun.COM 		 * explictly if we're restoring local properties cleared in the
357411022STom.Erickson@Sun.COM 		 * first new-style receive.
357511022STom.Erickson@Sun.COM 		 */
357611022STom.Erickson@Sun.COM 		if (origprops != NULL &&
357711022STom.Erickson@Sun.COM 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
357811022STom.Erickson@Sun.COM 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
357911022STom.Erickson@Sun.COM 		    origprops, NULL) != 0) {
358011022STom.Erickson@Sun.COM 			/*
358111022STom.Erickson@Sun.COM 			 * We stashed the original properties but failed to
358211022STom.Erickson@Sun.COM 			 * restore them.
358311022STom.Erickson@Sun.COM 			 */
358411022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
358511022STom.Erickson@Sun.COM 		}
35866689Smaybee 	}
35876689Smaybee out:
35886689Smaybee 	nvlist_free(props);
35896689Smaybee 	nvlist_free(origprops);
359011022STom.Erickson@Sun.COM 	nvlist_free(errors);
3591789Sahrens 	releasef(fd);
359211022STom.Erickson@Sun.COM 
359311022STom.Erickson@Sun.COM 	if (error == 0)
359411022STom.Erickson@Sun.COM 		error = props_error;
359511022STom.Erickson@Sun.COM 
3596789Sahrens 	return (error);
3597789Sahrens }
3598789Sahrens 
35995367Sahrens /*
36005367Sahrens  * inputs:
36015367Sahrens  * zc_name	name of snapshot to send
36025367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
36035367Sahrens  * zc_cookie	file descriptor to send stream to
36045367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
36055367Sahrens  *
36065367Sahrens  * outputs: none
36075367Sahrens  */
3608789Sahrens static int
36095367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
3610789Sahrens {
3611789Sahrens 	objset_t *fromsnap = NULL;
3612789Sahrens 	objset_t *tosnap;
3613789Sahrens 	file_t *fp;
3614789Sahrens 	int error;
36155367Sahrens 	offset_t off;
3616789Sahrens 
361710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
3618789Sahrens 	if (error)
3619789Sahrens 		return (error);
3620789Sahrens 
36212676Seschrock 	if (zc->zc_value[0] != '\0') {
36228012SEric.Taylor@Sun.COM 		char *buf;
36232885Sahrens 		char *cp;
36242885Sahrens 
36258012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
36268012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
36272885Sahrens 		cp = strchr(buf, '@');
36282885Sahrens 		if (cp)
36292885Sahrens 			*(cp+1) = 0;
36308012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
363110298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
36328012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
3633789Sahrens 		if (error) {
363410298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(tosnap, FTAG);
3635789Sahrens 			return (error);
3636789Sahrens 		}
3637789Sahrens 	}
3638789Sahrens 
3639789Sahrens 	fp = getf(zc->zc_cookie);
3640789Sahrens 	if (fp == NULL) {
364110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
3642789Sahrens 		if (fromsnap)
364310298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(fromsnap, FTAG);
3644789Sahrens 		return (EBADF);
3645789Sahrens 	}
3646789Sahrens 
36475367Sahrens 	off = fp->f_offset;
36485367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
36495367Sahrens 
36505367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
36515367Sahrens 		fp->f_offset = off;
3652789Sahrens 	releasef(zc->zc_cookie);
3653789Sahrens 	if (fromsnap)
365410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
365510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
3656789Sahrens 	return (error);
3657789Sahrens }
3658789Sahrens 
36591544Seschrock static int
36601544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
36611544Seschrock {
36621544Seschrock 	int id, error;
36631544Seschrock 
36641544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
36651544Seschrock 	    &zc->zc_inject_record);
36661544Seschrock 
36671544Seschrock 	if (error == 0)
36681544Seschrock 		zc->zc_guid = (uint64_t)id;
36691544Seschrock 
36701544Seschrock 	return (error);
36711544Seschrock }
36721544Seschrock 
36731544Seschrock static int
36741544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
36751544Seschrock {
36761544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
36771544Seschrock }
36781544Seschrock 
36791544Seschrock static int
36801544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
36811544Seschrock {
36821544Seschrock 	int id = (int)zc->zc_guid;
36831544Seschrock 	int error;
36841544Seschrock 
36851544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
36861544Seschrock 	    &zc->zc_inject_record);
36871544Seschrock 
36881544Seschrock 	zc->zc_guid = id;
36891544Seschrock 
36901544Seschrock 	return (error);
36911544Seschrock }
36921544Seschrock 
36931544Seschrock static int
36941544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
36951544Seschrock {
36961544Seschrock 	spa_t *spa;
36971544Seschrock 	int error;
36982676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
36991544Seschrock 
37001544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
37011544Seschrock 		return (error);
37021544Seschrock 
37032676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
37041544Seschrock 	    &count);
37051544Seschrock 	if (error == 0)
37062676Seschrock 		zc->zc_nvlist_dst_size = count;
37071544Seschrock 	else
37082676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
37091544Seschrock 
37101544Seschrock 	spa_close(spa, FTAG);
37111544Seschrock 
37121544Seschrock 	return (error);
37131544Seschrock }
37141544Seschrock 
37151544Seschrock static int
37161544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
37171544Seschrock {
37181544Seschrock 	spa_t *spa;
37191544Seschrock 	vdev_t *vd;
37201544Seschrock 	int error;
37211544Seschrock 
37227294Sperrin 	/*
37237294Sperrin 	 * On zpool clear we also fix up missing slogs
37247294Sperrin 	 */
37257294Sperrin 	mutex_enter(&spa_namespace_lock);
37267294Sperrin 	spa = spa_lookup(zc->zc_name);
37277294Sperrin 	if (spa == NULL) {
37287294Sperrin 		mutex_exit(&spa_namespace_lock);
37297294Sperrin 		return (EIO);
37307294Sperrin 	}
373110922SJeff.Bonwick@Sun.COM 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
37327294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
373310922SJeff.Bonwick@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
37347294Sperrin 	}
373510921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
37367294Sperrin 	mutex_exit(&spa_namespace_lock);
37377294Sperrin 
373810921STim.Haley@Sun.COM 	if (zc->zc_cookie == ZPOOL_NO_REWIND) {
373910921STim.Haley@Sun.COM 		error = spa_open(zc->zc_name, &spa, FTAG);
374010921STim.Haley@Sun.COM 	} else {
374110921STim.Haley@Sun.COM 		nvlist_t *policy;
374210921STim.Haley@Sun.COM 		nvlist_t *config = NULL;
374310921STim.Haley@Sun.COM 
374410921STim.Haley@Sun.COM 		if (zc->zc_nvlist_src == NULL)
374510921STim.Haley@Sun.COM 			return (EINVAL);
374610921STim.Haley@Sun.COM 
374710921STim.Haley@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
374810921STim.Haley@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
374910921STim.Haley@Sun.COM 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
375010921STim.Haley@Sun.COM 			    policy, &config);
375110921STim.Haley@Sun.COM 			if (config != NULL) {
375210921STim.Haley@Sun.COM 				(void) put_nvlist(zc, config);
375310921STim.Haley@Sun.COM 				nvlist_free(config);
375410921STim.Haley@Sun.COM 			}
375510921STim.Haley@Sun.COM 			nvlist_free(policy);
375610921STim.Haley@Sun.COM 		}
375710921STim.Haley@Sun.COM 	}
375810921STim.Haley@Sun.COM 
375910921STim.Haley@Sun.COM 	if (error)
37601544Seschrock 		return (error);
37611544Seschrock 
376210685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
37631544Seschrock 
37642676Seschrock 	if (zc->zc_guid == 0) {
37651544Seschrock 		vd = NULL;
37666643Seschrock 	} else {
37676643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
37685450Sbrendan 		if (vd == NULL) {
37697754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
37705450Sbrendan 			spa_close(spa, FTAG);
37715450Sbrendan 			return (ENODEV);
37725450Sbrendan 		}
37731544Seschrock 	}
37741544Seschrock 
37757754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
37767754SJeff.Bonwick@Sun.COM 
37777754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
37787754SJeff.Bonwick@Sun.COM 
37797754SJeff.Bonwick@Sun.COM 	/*
37807754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
37817754SJeff.Bonwick@Sun.COM 	 */
37829234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
37839234SGeorge.Wilson@Sun.COM 		error = EIO;
37841544Seschrock 
37851544Seschrock 	spa_close(spa, FTAG);
37861544Seschrock 
37879234SGeorge.Wilson@Sun.COM 	return (error);
37881544Seschrock }
37891544Seschrock 
37905367Sahrens /*
37915367Sahrens  * inputs:
37925367Sahrens  * zc_name	name of filesystem
37935367Sahrens  * zc_value	name of origin snapshot
37945367Sahrens  *
379510588SEric.Taylor@Sun.COM  * outputs:
379610588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
37975367Sahrens  */
37981544Seschrock static int
37992082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
38002082Seschrock {
38012417Sahrens 	char *cp;
38022417Sahrens 
38032417Sahrens 	/*
38042417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
38052417Sahrens 	 * it's easier.
38062417Sahrens 	 */
38072676Seschrock 	cp = strchr(zc->zc_value, '@');
38082417Sahrens 	if (cp)
38092417Sahrens 		*cp = '\0';
38102676Seschrock 	(void) dmu_objset_find(zc->zc_value,
38112417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
381210588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
38132082Seschrock }
38142082Seschrock 
38154543Smarks /*
38169396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
38179396SMatthew.Ahrens@Sun.COM  *
38189396SMatthew.Ahrens@Sun.COM  * inputs:
38199396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
38209396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
38219396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
38229396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
38239396SMatthew.Ahrens@Sun.COM  *
38249396SMatthew.Ahrens@Sun.COM  * outputs:
38259396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
38269396SMatthew.Ahrens@Sun.COM  */
38279396SMatthew.Ahrens@Sun.COM static int
38289396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
38299396SMatthew.Ahrens@Sun.COM {
38309396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
38319396SMatthew.Ahrens@Sun.COM 	int error;
38329396SMatthew.Ahrens@Sun.COM 
38339396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
38349396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
38359396SMatthew.Ahrens@Sun.COM 
383610298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
38379396SMatthew.Ahrens@Sun.COM 	if (error)
38389396SMatthew.Ahrens@Sun.COM 		return (error);
38399396SMatthew.Ahrens@Sun.COM 
38409396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
38419396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
38429396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
38439396SMatthew.Ahrens@Sun.COM 
38449396SMatthew.Ahrens@Sun.COM 	return (error);
38459396SMatthew.Ahrens@Sun.COM }
38469396SMatthew.Ahrens@Sun.COM 
38479396SMatthew.Ahrens@Sun.COM /*
38489396SMatthew.Ahrens@Sun.COM  * inputs:
38499396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
38509396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
38519396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
38529396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
38539396SMatthew.Ahrens@Sun.COM  *
38549396SMatthew.Ahrens@Sun.COM  * outputs:
38559396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
38569396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
38579396SMatthew.Ahrens@Sun.COM  */
38589396SMatthew.Ahrens@Sun.COM static int
38599396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
38609396SMatthew.Ahrens@Sun.COM {
38619396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
38629396SMatthew.Ahrens@Sun.COM 	int error;
38639396SMatthew.Ahrens@Sun.COM 
386410298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
38659396SMatthew.Ahrens@Sun.COM 	if (error)
38669396SMatthew.Ahrens@Sun.COM 		return (error);
38679396SMatthew.Ahrens@Sun.COM 
38689396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
38699396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
38709396SMatthew.Ahrens@Sun.COM 
38719396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
38729396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
38739396SMatthew.Ahrens@Sun.COM 
38749396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
38759396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
38769396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
38779396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
38789396SMatthew.Ahrens@Sun.COM 	}
38799396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
38809396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
38819396SMatthew.Ahrens@Sun.COM 
38829396SMatthew.Ahrens@Sun.COM 	return (error);
38839396SMatthew.Ahrens@Sun.COM }
38849396SMatthew.Ahrens@Sun.COM 
38859396SMatthew.Ahrens@Sun.COM /*
38869396SMatthew.Ahrens@Sun.COM  * inputs:
38879396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
38889396SMatthew.Ahrens@Sun.COM  *
38899396SMatthew.Ahrens@Sun.COM  * outputs:
38909396SMatthew.Ahrens@Sun.COM  * none
38919396SMatthew.Ahrens@Sun.COM  */
38929396SMatthew.Ahrens@Sun.COM static int
38939396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
38949396SMatthew.Ahrens@Sun.COM {
38959396SMatthew.Ahrens@Sun.COM 	objset_t *os;
389611422SMark.Musante@Sun.COM 	int error = 0;
38979396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
38989396SMatthew.Ahrens@Sun.COM 
38999396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
390010298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
39019396SMatthew.Ahrens@Sun.COM 			/*
39029396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
39039396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
39049396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
39059396SMatthew.Ahrens@Sun.COM 			 */
390610298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
390710298SMatthew.Ahrens@Sun.COM 			if (error == 0)
390810298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
39099396SMatthew.Ahrens@Sun.COM 		}
39109396SMatthew.Ahrens@Sun.COM 		if (error == 0)
39119396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
39129396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
39139396SMatthew.Ahrens@Sun.COM 	} else {
391410298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
391510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
39169396SMatthew.Ahrens@Sun.COM 		if (error)
39179396SMatthew.Ahrens@Sun.COM 			return (error);
39189396SMatthew.Ahrens@Sun.COM 
39199396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
392010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
39219396SMatthew.Ahrens@Sun.COM 	}
39229396SMatthew.Ahrens@Sun.COM 
39239396SMatthew.Ahrens@Sun.COM 	return (error);
39249396SMatthew.Ahrens@Sun.COM }
39259396SMatthew.Ahrens@Sun.COM 
39269396SMatthew.Ahrens@Sun.COM /*
39274543Smarks  * We don't want to have a hard dependency
39284543Smarks  * against some special symbols in sharefs
39295331Samw  * nfs, and smbsrv.  Determine them if needed when
39304543Smarks  * the first file system is shared.
39315331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
39324543Smarks  */
39335331Samw int (*znfsexport_fs)(void *arg);
39344543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
39355331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
39365331Samw 
39375331Samw int zfs_nfsshare_inited;
39385331Samw int zfs_smbshare_inited;
39395331Samw 
39404543Smarks ddi_modhandle_t nfs_mod;
39414543Smarks ddi_modhandle_t sharefs_mod;
39425331Samw ddi_modhandle_t smbsrv_mod;
39434543Smarks kmutex_t zfs_share_lock;
39444543Smarks 
39454543Smarks static int
39465331Samw zfs_init_sharefs()
39475331Samw {
39485331Samw 	int error;
39495331Samw 
39505331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
39515331Samw 	/* Both NFS and SMB shares also require sharetab support. */
39525331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
39535331Samw 	    ddi_modopen("fs/sharefs",
39545331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
39555331Samw 		return (ENOSYS);
39565331Samw 	}
39575331Samw 	if (zshare_fs == NULL && ((zshare_fs =
39585331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
39595331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
39605331Samw 		return (ENOSYS);
39615331Samw 	}
39625331Samw 	return (0);
39635331Samw }
39645331Samw 
39655331Samw static int
39664543Smarks zfs_ioc_share(zfs_cmd_t *zc)
39674543Smarks {
39684543Smarks 	int error;
39694543Smarks 	int opcode;
39704543Smarks 
39715331Samw 	switch (zc->zc_share.z_sharetype) {
39725331Samw 	case ZFS_SHARE_NFS:
39735331Samw 	case ZFS_UNSHARE_NFS:
39745331Samw 		if (zfs_nfsshare_inited == 0) {
39755331Samw 			mutex_enter(&zfs_share_lock);
39765331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
39775331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
39785331Samw 				mutex_exit(&zfs_share_lock);
39795331Samw 				return (ENOSYS);
39805331Samw 			}
39815331Samw 			if (znfsexport_fs == NULL &&
39825331Samw 			    ((znfsexport_fs = (int (*)(void *))
39835331Samw 			    ddi_modsym(nfs_mod,
39845331Samw 			    "nfs_export", &error)) == NULL)) {
39855331Samw 				mutex_exit(&zfs_share_lock);
39865331Samw 				return (ENOSYS);
39875331Samw 			}
39885331Samw 			error = zfs_init_sharefs();
39895331Samw 			if (error) {
39905331Samw 				mutex_exit(&zfs_share_lock);
39915331Samw 				return (ENOSYS);
39925331Samw 			}
39935331Samw 			zfs_nfsshare_inited = 1;
39944543Smarks 			mutex_exit(&zfs_share_lock);
39954543Smarks 		}
39965331Samw 		break;
39975331Samw 	case ZFS_SHARE_SMB:
39985331Samw 	case ZFS_UNSHARE_SMB:
39995331Samw 		if (zfs_smbshare_inited == 0) {
40005331Samw 			mutex_enter(&zfs_share_lock);
40015331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
40025331Samw 			    ddi_modopen("drv/smbsrv",
40035331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
40045331Samw 				mutex_exit(&zfs_share_lock);
40055331Samw 				return (ENOSYS);
40065331Samw 			}
40075331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
40085331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
40096139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
40105331Samw 				mutex_exit(&zfs_share_lock);
40115331Samw 				return (ENOSYS);
40125331Samw 			}
40135331Samw 			error = zfs_init_sharefs();
40145331Samw 			if (error) {
40155331Samw 				mutex_exit(&zfs_share_lock);
40165331Samw 				return (ENOSYS);
40175331Samw 			}
40185331Samw 			zfs_smbshare_inited = 1;
40194543Smarks 			mutex_exit(&zfs_share_lock);
40204543Smarks 		}
40215331Samw 		break;
40225331Samw 	default:
40235331Samw 		return (EINVAL);
40244543Smarks 	}
40254543Smarks 
40265331Samw 	switch (zc->zc_share.z_sharetype) {
40275331Samw 	case ZFS_SHARE_NFS:
40285331Samw 	case ZFS_UNSHARE_NFS:
40295331Samw 		if (error =
40305331Samw 		    znfsexport_fs((void *)
40315331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
40325331Samw 			return (error);
40335331Samw 		break;
40345331Samw 	case ZFS_SHARE_SMB:
40355331Samw 	case ZFS_UNSHARE_SMB:
40365331Samw 		if (error = zsmbexport_fs((void *)
40375331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
40385331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
40398845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
40405331Samw 			return (error);
40415331Samw 		}
40425331Samw 		break;
40435331Samw 	}
40445331Samw 
40455331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
40465331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
40474543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
40484543Smarks 
40495331Samw 	/*
40505331Samw 	 * Add or remove share from sharetab
40515331Samw 	 */
40524543Smarks 	error = zshare_fs(opcode,
40534543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
40544543Smarks 	    zc->zc_share.z_sharemax);
40554543Smarks 
40564543Smarks 	return (error);
40574543Smarks 
40584543Smarks }
40594543Smarks 
40608845Samw@Sun.COM ace_t full_access[] = {
40618845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
40628845Samw@Sun.COM };
40638845Samw@Sun.COM 
40648845Samw@Sun.COM /*
40658845Samw@Sun.COM  * Remove all ACL files in shares dir
40668845Samw@Sun.COM  */
40678845Samw@Sun.COM static int
40688845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
40698845Samw@Sun.COM {
40708845Samw@Sun.COM 	zap_cursor_t	zc;
40718845Samw@Sun.COM 	zap_attribute_t	zap;
40728845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
40738845Samw@Sun.COM 	int error;
40748845Samw@Sun.COM 
40758845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
40768845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
40778845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
40788845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
40798845Samw@Sun.COM 		    NULL, 0)) != 0)
40808845Samw@Sun.COM 			break;
40818845Samw@Sun.COM 	}
40828845Samw@Sun.COM 	zap_cursor_fini(&zc);
40838845Samw@Sun.COM 	return (error);
40848845Samw@Sun.COM }
40858845Samw@Sun.COM 
40868845Samw@Sun.COM static int
40878845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
40888845Samw@Sun.COM {
40898845Samw@Sun.COM 	vnode_t *vp;
40908845Samw@Sun.COM 	znode_t *dzp;
40918845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
40928845Samw@Sun.COM 	znode_t *sharedir;
40938845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
40948845Samw@Sun.COM 	nvlist_t *nvlist;
40958845Samw@Sun.COM 	char *src, *target;
40968845Samw@Sun.COM 	vattr_t vattr;
40978845Samw@Sun.COM 	vsecattr_t vsec;
40988845Samw@Sun.COM 	int error = 0;
40998845Samw@Sun.COM 
41008845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
41018845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
41028845Samw@Sun.COM 		return (error);
41038845Samw@Sun.COM 
41048845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
41058845Samw@Sun.COM 
41068845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
41078845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
41088845Samw@Sun.COM 	    zc->zc_name) != 0)) {
41098845Samw@Sun.COM 		VN_RELE(vp);
41108845Samw@Sun.COM 		return (EINVAL);
41118845Samw@Sun.COM 	}
41128845Samw@Sun.COM 
41138845Samw@Sun.COM 	dzp = VTOZ(vp);
41148845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
41158845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
41168845Samw@Sun.COM 
41179030SMark.Shellenbaum@Sun.COM 	/*
41189030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
41199030SMark.Shellenbaum@Sun.COM 	 */
41209030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
41219030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
41229030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
41239030SMark.Shellenbaum@Sun.COM 
41249030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
41259030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
41269030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
41279030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
41289030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
41299030SMark.Shellenbaum@Sun.COM 		if (error) {
41309030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
41319030SMark.Shellenbaum@Sun.COM 		} else {
41329030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
41339030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
41349030SMark.Shellenbaum@Sun.COM 		}
41359030SMark.Shellenbaum@Sun.COM 		if (error) {
41369030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
41379030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
41389030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
41399030SMark.Shellenbaum@Sun.COM 			return (error);
41409030SMark.Shellenbaum@Sun.COM 		}
41419030SMark.Shellenbaum@Sun.COM 	}
41429030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
41439030SMark.Shellenbaum@Sun.COM 
41449030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
41458845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
41469030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
41478845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
41488845Samw@Sun.COM 		return (error);
41498845Samw@Sun.COM 	}
41508845Samw@Sun.COM 
41518845Samw@Sun.COM 	switch (zc->zc_cookie) {
41528845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
41538845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
41548845Samw@Sun.COM 		vattr.va_type = VREG;
41558845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
41568845Samw@Sun.COM 		vattr.va_uid = 0;
41578845Samw@Sun.COM 		vattr.va_gid = 0;
41588845Samw@Sun.COM 
41598845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
41608845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
41618845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
41628845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
41638845Samw@Sun.COM 
41648845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
41658845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
41668845Samw@Sun.COM 		if (resourcevp)
41678845Samw@Sun.COM 			VN_RELE(resourcevp);
41688845Samw@Sun.COM 		break;
41698845Samw@Sun.COM 
41708845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
41718845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
41728845Samw@Sun.COM 		    NULL, 0);
41738845Samw@Sun.COM 		break;
41748845Samw@Sun.COM 
41758845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
41768845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
41779643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
41788845Samw@Sun.COM 			VN_RELE(vp);
41798845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
41808845Samw@Sun.COM 			return (error);
41818845Samw@Sun.COM 		}
41828845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
41838845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
41848845Samw@Sun.COM 		    &target)) {
41858845Samw@Sun.COM 			VN_RELE(vp);
41869179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
41878845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
418811422SMark.Musante@Sun.COM 			nvlist_free(nvlist);
41898845Samw@Sun.COM 			return (error);
41908845Samw@Sun.COM 		}
41918845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
41928845Samw@Sun.COM 		    kcred, NULL, 0);
41938845Samw@Sun.COM 		nvlist_free(nvlist);
41948845Samw@Sun.COM 		break;
41958845Samw@Sun.COM 
41968845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
41978845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
41988845Samw@Sun.COM 		break;
41998845Samw@Sun.COM 
42008845Samw@Sun.COM 	default:
42018845Samw@Sun.COM 		error = EINVAL;
42028845Samw@Sun.COM 		break;
42038845Samw@Sun.COM 	}
42048845Samw@Sun.COM 
42058845Samw@Sun.COM 	VN_RELE(vp);
42068845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
42078845Samw@Sun.COM 
42088845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
42098845Samw@Sun.COM 
42108845Samw@Sun.COM 	return (error);
42118845Samw@Sun.COM }
42128845Samw@Sun.COM 
42134543Smarks /*
421410242Schris.kirby@sun.com  * inputs:
421510242Schris.kirby@sun.com  * zc_name	name of filesystem
421610242Schris.kirby@sun.com  * zc_value	short name of snap
421710242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
421810242Schris.kirby@sun.com  * zc_cookie	recursive flag
421910342Schris.kirby@sun.com  * zc_temphold	set if hold is temporary
422010242Schris.kirby@sun.com  *
422110242Schris.kirby@sun.com  * outputs:		none
422210242Schris.kirby@sun.com  */
422310242Schris.kirby@sun.com static int
422410242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
422510242Schris.kirby@sun.com {
422610242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
422710242Schris.kirby@sun.com 
422810242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
422910242Schris.kirby@sun.com 		return (EINVAL);
423010242Schris.kirby@sun.com 
423110242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
423210342Schris.kirby@sun.com 	    zc->zc_string, recursive, zc->zc_temphold));
423310242Schris.kirby@sun.com }
423410242Schris.kirby@sun.com 
423510242Schris.kirby@sun.com /*
423610242Schris.kirby@sun.com  * inputs:
423710242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
423810242Schris.kirby@sun.com  * zc_value	short name of snap
423910242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
424010242Schris.kirby@sun.com  * zc_cookie	recursive flag
424110242Schris.kirby@sun.com  *
424210242Schris.kirby@sun.com  * outputs:		none
424310242Schris.kirby@sun.com  */
424410242Schris.kirby@sun.com static int
424510242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
424610242Schris.kirby@sun.com {
424710242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
424810242Schris.kirby@sun.com 
424910242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
425010242Schris.kirby@sun.com 		return (EINVAL);
425110242Schris.kirby@sun.com 
425210242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
425310242Schris.kirby@sun.com 	    zc->zc_string, recursive));
425410242Schris.kirby@sun.com }
425510242Schris.kirby@sun.com 
425610242Schris.kirby@sun.com /*
425710242Schris.kirby@sun.com  * inputs:
425810242Schris.kirby@sun.com  * zc_name		name of filesystem
425910242Schris.kirby@sun.com  *
426010242Schris.kirby@sun.com  * outputs:
426110242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
426210242Schris.kirby@sun.com  */
426310242Schris.kirby@sun.com static int
426410242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
426510242Schris.kirby@sun.com {
426610242Schris.kirby@sun.com 	nvlist_t *nvp;
426710242Schris.kirby@sun.com 	int error;
426810242Schris.kirby@sun.com 
426910242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
427010242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
427110242Schris.kirby@sun.com 		nvlist_free(nvp);
427210242Schris.kirby@sun.com 	}
427310242Schris.kirby@sun.com 
427410242Schris.kirby@sun.com 	return (error);
427510242Schris.kirby@sun.com }
427610242Schris.kirby@sun.com 
427710242Schris.kirby@sun.com /*
42784988Sek110237  * pool create, destroy, and export don't log the history as part of
42794988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
42804988Sek110237  * do the logging of those commands.
42814543Smarks  */
4282789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
42839234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
42849234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
42869234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42879234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42889234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
42909234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
42929234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
42949234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
42969234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42989234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
43009234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43019234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
43029234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43039234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
43049234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
43069234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43079234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
43089234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43099234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
43109234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43119234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
43129234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43139234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
43149234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43159234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
43169234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43179425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
43189425SEric.Schrock@Sun.COM 	    B_TRUE },
43199234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
432011454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
43219234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
43229234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43239234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
432411454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
43259234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
432611454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
43279234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
43289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
43299234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
43309234SGeorge.Wilson@Sun.COM 	    B_TRUE},
43319234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
43329234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43339234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
43349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
43359234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
43369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
43379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
43399234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
43419234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
43439234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
43459234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
43469234SGeorge.Wilson@Sun.COM 	    B_TRUE },
434711314Swilliam.gorrell@sun.com 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
434811314Swilliam.gorrell@sun.com 	    B_TRUE, B_TRUE },
43499234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
43509234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43519234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
43529234SGeorge.Wilson@Sun.COM 	    B_FALSE },
435310233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
435410233SGeorge.Wilson@Sun.COM 	    B_TRUE },
43559234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
43569234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
43589234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
43609234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43619234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
43629234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
43649234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
43669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
43679234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
43699396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
43709396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
43719396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
43729396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
43739396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
43749396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
43759396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
437610242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
437710242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
437810242Schris.kirby@sun.com 	    B_TRUE },
437910242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
438011022STom.Erickson@Sun.COM 	    B_TRUE },
438111022STom.Erickson@Sun.COM 	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
438211422SMark.Musante@Sun.COM 	    B_FALSE },
438311422SMark.Musante@Sun.COM 	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
438411422SMark.Musante@Sun.COM 	    B_TRUE }
4385789Sahrens };
4386789Sahrens 
43879234SGeorge.Wilson@Sun.COM int
43889234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
43899234SGeorge.Wilson@Sun.COM {
43909234SGeorge.Wilson@Sun.COM 	spa_t *spa;
43919234SGeorge.Wilson@Sun.COM 	int error;
43929234SGeorge.Wilson@Sun.COM 
43939234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
43949234SGeorge.Wilson@Sun.COM 
43959396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
43969234SGeorge.Wilson@Sun.COM 	if (error == 0) {
43979234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
43989234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
43999234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
44009234SGeorge.Wilson@Sun.COM 	}
44019234SGeorge.Wilson@Sun.COM 	return (error);
44029234SGeorge.Wilson@Sun.COM }
44039234SGeorge.Wilson@Sun.COM 
4404789Sahrens static int
4405789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4406789Sahrens {
4407789Sahrens 	zfs_cmd_t *zc;
4408789Sahrens 	uint_t vec;
44092199Sahrens 	int error, rc;
4410789Sahrens 
4411789Sahrens 	if (getminor(dev) != 0)
4412789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4413789Sahrens 
4414789Sahrens 	vec = cmd - ZFS_IOC;
44154787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4416789Sahrens 
4417789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4418789Sahrens 		return (EINVAL);
4419789Sahrens 
4420789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4421789Sahrens 
44229643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4423789Sahrens 
442410588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
44254543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4426789Sahrens 
4427789Sahrens 	/*
4428789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
4429789Sahrens 	 * the lower layers.
4430789Sahrens 	 */
4431789Sahrens 	if (error == 0) {
4432789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
44339643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
4434789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
44354577Sahrens 		case POOL_NAME:
4436789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4437789Sahrens 				error = EINVAL;
44389234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
44399234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
44409234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4441789Sahrens 			break;
4442789Sahrens 
44434577Sahrens 		case DATASET_NAME:
4444789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4445789Sahrens 				error = EINVAL;
44469234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
44479234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
44489234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4449789Sahrens 			break;
44502856Snd150628 
44514577Sahrens 		case NO_NAME:
44522856Snd150628 			break;
4453789Sahrens 		}
4454789Sahrens 	}
4455789Sahrens 
4456789Sahrens 	if (error == 0)
4457789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
4458789Sahrens 
44599643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
44604543Smarks 	if (error == 0) {
44612199Sahrens 		error = rc;
44629396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
44634543Smarks 			zfs_log_history(zc);
44644543Smarks 	}
4465789Sahrens 
4466789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
4467789Sahrens 	return (error);
4468789Sahrens }
4469789Sahrens 
4470789Sahrens static int
4471789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4472789Sahrens {
4473789Sahrens 	if (cmd != DDI_ATTACH)
4474789Sahrens 		return (DDI_FAILURE);
4475789Sahrens 
4476789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4477789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
4478789Sahrens 		return (DDI_FAILURE);
4479789Sahrens 
4480789Sahrens 	zfs_dip = dip;
4481789Sahrens 
4482789Sahrens 	ddi_report_dev(dip);
4483789Sahrens 
4484789Sahrens 	return (DDI_SUCCESS);
4485789Sahrens }
4486789Sahrens 
4487789Sahrens static int
4488789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4489789Sahrens {
4490789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
4491789Sahrens 		return (DDI_FAILURE);
4492789Sahrens 
4493789Sahrens 	if (cmd != DDI_DETACH)
4494789Sahrens 		return (DDI_FAILURE);
4495789Sahrens 
4496789Sahrens 	zfs_dip = NULL;
4497789Sahrens 
4498789Sahrens 	ddi_prop_remove_all(dip);
4499789Sahrens 	ddi_remove_minor_node(dip, NULL);
4500789Sahrens 
4501789Sahrens 	return (DDI_SUCCESS);
4502789Sahrens }
4503789Sahrens 
4504789Sahrens /*ARGSUSED*/
4505789Sahrens static int
4506789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4507789Sahrens {
4508789Sahrens 	switch (infocmd) {
4509789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
4510789Sahrens 		*result = zfs_dip;
4511789Sahrens 		return (DDI_SUCCESS);
4512789Sahrens 
4513789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
4514849Sbonwick 		*result = (void *)0;
4515789Sahrens 		return (DDI_SUCCESS);
4516789Sahrens 	}
4517789Sahrens 
4518789Sahrens 	return (DDI_FAILURE);
4519789Sahrens }
4520789Sahrens 
4521789Sahrens /*
4522789Sahrens  * OK, so this is a little weird.
4523789Sahrens  *
4524789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
4525789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
4526789Sahrens  *
4527789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
4528789Sahrens  * so most of the standard driver entry points are in zvol.c.
4529789Sahrens  */
4530789Sahrens static struct cb_ops zfs_cb_ops = {
4531789Sahrens 	zvol_open,	/* open */
4532789Sahrens 	zvol_close,	/* close */
4533789Sahrens 	zvol_strategy,	/* strategy */
4534789Sahrens 	nodev,		/* print */
45356423Sgw25295 	zvol_dump,	/* dump */
4536789Sahrens 	zvol_read,	/* read */
4537789Sahrens 	zvol_write,	/* write */
4538789Sahrens 	zfsdev_ioctl,	/* ioctl */
4539789Sahrens 	nodev,		/* devmap */
4540789Sahrens 	nodev,		/* mmap */
4541789Sahrens 	nodev,		/* segmap */
4542789Sahrens 	nochpoll,	/* poll */
4543789Sahrens 	ddi_prop_op,	/* prop_op */
4544789Sahrens 	NULL,		/* streamtab */
4545789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
4546789Sahrens 	CB_REV,		/* version */
45473638Sbillm 	nodev,		/* async read */
45483638Sbillm 	nodev,		/* async write */
4549789Sahrens };
4550789Sahrens 
4551789Sahrens static struct dev_ops zfs_dev_ops = {
4552789Sahrens 	DEVO_REV,	/* version */
4553789Sahrens 	0,		/* refcnt */
4554789Sahrens 	zfs_info,	/* info */
4555789Sahrens 	nulldev,	/* identify */
4556789Sahrens 	nulldev,	/* probe */
4557789Sahrens 	zfs_attach,	/* attach */
4558789Sahrens 	zfs_detach,	/* detach */
4559789Sahrens 	nodev,		/* reset */
4560789Sahrens 	&zfs_cb_ops,	/* driver operations */
45617656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
45627656SSherry.Moore@Sun.COM 	NULL,		/* power */
45637656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
4564789Sahrens };
4565789Sahrens 
4566789Sahrens static struct modldrv zfs_modldrv = {
45677656SSherry.Moore@Sun.COM 	&mod_driverops,
45687656SSherry.Moore@Sun.COM 	"ZFS storage pool",
45697656SSherry.Moore@Sun.COM 	&zfs_dev_ops
4570789Sahrens };
4571789Sahrens 
4572789Sahrens static struct modlinkage modlinkage = {
4573789Sahrens 	MODREV_1,
4574789Sahrens 	(void *)&zfs_modlfs,
4575789Sahrens 	(void *)&zfs_modldrv,
4576789Sahrens 	NULL
4577789Sahrens };
4578789Sahrens 
45794720Sfr157268 
45804720Sfr157268 uint_t zfs_fsyncer_key;
45815326Sek110237 extern uint_t rrw_tsd_key;
45824720Sfr157268 
4583789Sahrens int
4584789Sahrens _init(void)
4585789Sahrens {
4586789Sahrens 	int error;
4587789Sahrens 
4588849Sbonwick 	spa_init(FREAD | FWRITE);
4589849Sbonwick 	zfs_init();
4590849Sbonwick 	zvol_init();
4591849Sbonwick 
4592849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
4593849Sbonwick 		zvol_fini();
4594849Sbonwick 		zfs_fini();
4595849Sbonwick 		spa_fini();
4596789Sahrens 		return (error);
4597849Sbonwick 	}
4598789Sahrens 
45994720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
46005326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
46014720Sfr157268 
4602789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
4603789Sahrens 	ASSERT(error == 0);
46044543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
4605789Sahrens 
4606789Sahrens 	return (0);
4607789Sahrens }
4608789Sahrens 
4609789Sahrens int
4610789Sahrens _fini(void)
4611789Sahrens {
4612789Sahrens 	int error;
4613789Sahrens 
46141544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
4615789Sahrens 		return (EBUSY);
4616789Sahrens 
4617789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
4618789Sahrens 		return (error);
4619789Sahrens 
4620789Sahrens 	zvol_fini();
4621789Sahrens 	zfs_fini();
4622789Sahrens 	spa_fini();
46235331Samw 	if (zfs_nfsshare_inited)
46244543Smarks 		(void) ddi_modclose(nfs_mod);
46255331Samw 	if (zfs_smbshare_inited)
46265331Samw 		(void) ddi_modclose(smbsrv_mod);
46275331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
46284543Smarks 		(void) ddi_modclose(sharefs_mod);
4629789Sahrens 
46304720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
4631789Sahrens 	ldi_ident_release(zfs_li);
4632789Sahrens 	zfs_li = NULL;
46334543Smarks 	mutex_destroy(&zfs_share_lock);
4634789Sahrens 
4635789Sahrens 	return (error);
4636789Sahrens }
4637789Sahrens 
4638789Sahrens int
4639789Sahrens _info(struct modinfo *modinfop)
4640789Sahrens {
4641789Sahrens 	return (mod_info(&modlinkage, modinfop));
4642789Sahrens }
4643