xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 8697:e62d2612c14d)
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 /*
228525SEric.Schrock@Sun.COM  * Copyright 2009 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>
395331Samw #include <sys/zfs_znode.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/spa.h>
423912Slling #include <sys/spa_impl.h>
43789Sahrens #include <sys/vdev.h>
443912Slling #include <sys/vdev_impl.h>
45789Sahrens #include <sys/dmu.h>
46789Sahrens #include <sys/dsl_dir.h>
47789Sahrens #include <sys/dsl_dataset.h>
48789Sahrens #include <sys/dsl_prop.h>
494543Smarks #include <sys/dsl_deleg.h>
504543Smarks #include <sys/dmu_objset.h>
51789Sahrens #include <sys/ddi.h>
52789Sahrens #include <sys/sunddi.h>
53789Sahrens #include <sys/sunldi.h>
54789Sahrens #include <sys/policy.h>
55789Sahrens #include <sys/zone.h>
56789Sahrens #include <sys/nvpair.h>
57789Sahrens #include <sys/pathname.h>
58789Sahrens #include <sys/mount.h>
59789Sahrens #include <sys/sdt.h>
60789Sahrens #include <sys/fs/zfs.h>
61789Sahrens #include <sys/zfs_ctldir.h>
625331Samw #include <sys/zfs_dir.h>
632885Sahrens #include <sys/zvol.h>
644543Smarks #include <sharefs/share.h>
655326Sek110237 #include <sys/dmu_objset.h>
66789Sahrens 
67789Sahrens #include "zfs_namecheck.h"
682676Seschrock #include "zfs_prop.h"
694543Smarks #include "zfs_deleg.h"
70789Sahrens 
71789Sahrens extern struct modlfs zfs_modlfs;
72789Sahrens 
73789Sahrens extern void zfs_init(void);
74789Sahrens extern void zfs_fini(void);
75789Sahrens 
76789Sahrens ldi_ident_t zfs_li = NULL;
77789Sahrens dev_info_t *zfs_dip;
78789Sahrens 
79789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
804543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
81789Sahrens 
82789Sahrens typedef struct zfs_ioc_vec {
83789Sahrens 	zfs_ioc_func_t		*zvec_func;
84789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
85789Sahrens 	enum {
864577Sahrens 		NO_NAME,
874577Sahrens 		POOL_NAME,
884577Sahrens 		DATASET_NAME
894543Smarks 	} zvec_namecheck;
904543Smarks 	boolean_t		zvec_his_log;
91789Sahrens } zfs_ioc_vec_t;
92789Sahrens 
938536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
947184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
957184Stimh     boolean_t *);
967184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
977184Stimh 
98789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
99789Sahrens void
100789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
101789Sahrens {
102789Sahrens 	const char *newfile;
103789Sahrens 	char buf[256];
104789Sahrens 	va_list adx;
105789Sahrens 
106789Sahrens 	/*
107789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
108789Sahrens 	 */
109789Sahrens 	newfile = strrchr(file, '/');
110789Sahrens 	if (newfile != NULL) {
111789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
112789Sahrens 	} else {
113789Sahrens 		newfile = file;
114789Sahrens 	}
115789Sahrens 
116789Sahrens 	va_start(adx, fmt);
117789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
118789Sahrens 	va_end(adx);
119789Sahrens 
120789Sahrens 	/*
121789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
122789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
123789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
124789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
125789Sahrens 	 * arg0 = file name
126789Sahrens 	 * arg1 = function name
127789Sahrens 	 * arg2 = line number
128789Sahrens 	 * arg3 = message
129789Sahrens 	 */
130789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
131789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
132789Sahrens }
133789Sahrens 
1344543Smarks static void
1354715Sek110237 history_str_free(char *buf)
1364715Sek110237 {
1374715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1384715Sek110237 }
1394715Sek110237 
1404715Sek110237 static char *
1414715Sek110237 history_str_get(zfs_cmd_t *zc)
1424715Sek110237 {
1434715Sek110237 	char *buf;
1444715Sek110237 
1454715Sek110237 	if (zc->zc_history == NULL)
1464715Sek110237 		return (NULL);
1474715Sek110237 
1484715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1494715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1504715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1514715Sek110237 		history_str_free(buf);
1524715Sek110237 		return (NULL);
1534715Sek110237 	}
1544715Sek110237 
1554715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1564715Sek110237 
1574715Sek110237 	return (buf);
1584715Sek110237 }
1594715Sek110237 
1605375Stimh /*
1617042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1627042Sgw25295  */
1637042Sgw25295 static boolean_t
1647042Sgw25295 zfs_is_bootfs(const char *name)
1657042Sgw25295 {
1667042Sgw25295 	spa_t *spa;
1677042Sgw25295 	boolean_t ret = B_FALSE;
1687042Sgw25295 
1697042Sgw25295 	if (spa_open(name, &spa, FTAG) == 0) {
1707042Sgw25295 		if (spa->spa_bootfs) {
1717042Sgw25295 			objset_t *os;
1727042Sgw25295 
1737042Sgw25295 			if (dmu_objset_open(name, DMU_OST_ZFS,
1747042Sgw25295 			    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1757042Sgw25295 				ret = (dmu_objset_id(os) == spa->spa_bootfs);
1767042Sgw25295 				dmu_objset_close(os);
1777042Sgw25295 			}
1787042Sgw25295 		}
1797042Sgw25295 		spa_close(spa, FTAG);
1807042Sgw25295 	}
1817042Sgw25295 	return (ret);
1827042Sgw25295 }
1837042Sgw25295 
1847042Sgw25295 /*
1857184Stimh  * zfs_earlier_version
1865375Stimh  *
1875375Stimh  *	Return non-zero if the spa version is less than requested version.
1885375Stimh  */
1895331Samw static int
1907184Stimh zfs_earlier_version(const char *name, int version)
1915331Samw {
1925331Samw 	spa_t *spa;
1935331Samw 
1945331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
1955331Samw 		if (spa_version(spa) < version) {
1965331Samw 			spa_close(spa, FTAG);
1975331Samw 			return (1);
1985331Samw 		}
1995331Samw 		spa_close(spa, FTAG);
2005331Samw 	}
2015331Samw 	return (0);
2025331Samw }
2035331Samw 
2045977Smarks /*
2056689Smaybee  * zpl_earlier_version
2065977Smarks  *
2076689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2085977Smarks  */
2096689Smaybee static boolean_t
2106689Smaybee zpl_earlier_version(const char *name, int version)
2115977Smarks {
2125977Smarks 	objset_t *os;
2136689Smaybee 	boolean_t rc = B_TRUE;
2145977Smarks 
2155977Smarks 	if (dmu_objset_open(name, DMU_OST_ANY,
2166689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2176689Smaybee 		uint64_t zplversion;
2186689Smaybee 
2196689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2206689Smaybee 			rc = zplversion < version;
2215977Smarks 		dmu_objset_close(os);
2225977Smarks 	}
2235977Smarks 	return (rc);
2245977Smarks }
2255977Smarks 
2264715Sek110237 static void
2274543Smarks zfs_log_history(zfs_cmd_t *zc)
2284543Smarks {
2294543Smarks 	spa_t *spa;
2304603Sahrens 	char *buf;
2314543Smarks 
2324715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2334577Sahrens 		return;
2344577Sahrens 
2354715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2364715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2374715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2384715Sek110237 		spa_close(spa, FTAG);
2394543Smarks 	}
2404715Sek110237 	history_str_free(buf);
2414543Smarks }
2424543Smarks 
243789Sahrens /*
244789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
245789Sahrens  * and can be used in the local zone, as there is no associated dataset.
246789Sahrens  */
247789Sahrens /* ARGSUSED */
248789Sahrens static int
2494543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
250789Sahrens {
251789Sahrens 	return (0);
252789Sahrens }
253789Sahrens 
254789Sahrens /*
255789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
256789Sahrens  * no privileges, but must be visible in the local zone.
257789Sahrens  */
258789Sahrens /* ARGSUSED */
259789Sahrens static int
2604543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
261789Sahrens {
262789Sahrens 	if (INGLOBALZONE(curproc) ||
2634543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
264789Sahrens 		return (0);
265789Sahrens 
266789Sahrens 	return (ENOENT);
267789Sahrens }
268789Sahrens 
269789Sahrens static int
270789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
271789Sahrens {
272789Sahrens 	uint64_t zoned;
273789Sahrens 	int writable = 1;
274789Sahrens 
275789Sahrens 	/*
276789Sahrens 	 * The dataset must be visible by this zone -- check this first
277789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
278789Sahrens 	 */
279789Sahrens 	if (!INGLOBALZONE(curproc) &&
280789Sahrens 	    !zone_dataset_visible(dataset, &writable))
281789Sahrens 		return (ENOENT);
282789Sahrens 
283789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
284789Sahrens 		return (ENOENT);
285789Sahrens 
286789Sahrens 	if (INGLOBALZONE(curproc)) {
287789Sahrens 		/*
288789Sahrens 		 * If the fs is zoned, only root can access it from the
289789Sahrens 		 * global zone.
290789Sahrens 		 */
291789Sahrens 		if (secpolicy_zfs(cr) && zoned)
292789Sahrens 			return (EPERM);
293789Sahrens 	} else {
294789Sahrens 		/*
295789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
296789Sahrens 		 */
297789Sahrens 		if (!zoned)
298789Sahrens 			return (EPERM);
299789Sahrens 
300789Sahrens 		/* must be writable by this zone */
301789Sahrens 		if (!writable)
302789Sahrens 			return (EPERM);
303789Sahrens 	}
304789Sahrens 	return (0);
305789Sahrens }
306789Sahrens 
307789Sahrens int
3084543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
309789Sahrens {
310789Sahrens 	int error;
311789Sahrens 
3124543Smarks 	error = zfs_dozonecheck(name, cr);
3134543Smarks 	if (error == 0) {
3144543Smarks 		error = secpolicy_zfs(cr);
3154670Sahrens 		if (error)
3164543Smarks 			error = dsl_deleg_access(name, perm, cr);
3174543Smarks 	}
3184543Smarks 	return (error);
3194543Smarks }
3204543Smarks 
3214543Smarks static int
3224543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3234543Smarks {
3244543Smarks 	/*
3254543Smarks 	 * Check permissions for special properties.
3264543Smarks 	 */
3274543Smarks 	switch (prop) {
3284543Smarks 	case ZFS_PROP_ZONED:
3294543Smarks 		/*
3304543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3314543Smarks 		 */
3324543Smarks 		if (!INGLOBALZONE(curproc))
3334543Smarks 			return (EPERM);
3344543Smarks 		break;
335789Sahrens 
3364543Smarks 	case ZFS_PROP_QUOTA:
3374543Smarks 		if (!INGLOBALZONE(curproc)) {
3384543Smarks 			uint64_t zoned;
3394543Smarks 			char setpoint[MAXNAMELEN];
3404543Smarks 			/*
3414543Smarks 			 * Unprivileged users are allowed to modify the
3424543Smarks 			 * quota on things *under* (ie. contained by)
3434543Smarks 			 * the thing they own.
3444543Smarks 			 */
3454543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3464543Smarks 			    setpoint))
3474543Smarks 				return (EPERM);
3484670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3494543Smarks 				return (EPERM);
3504543Smarks 		}
3514670Sahrens 		break;
3524543Smarks 	}
3534543Smarks 
3544787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
355789Sahrens }
356789Sahrens 
3574543Smarks int
3584543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3594543Smarks {
3604543Smarks 	int error;
3614543Smarks 
3624543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3634543Smarks 	if (error)
3644543Smarks 		return (error);
3654543Smarks 
3664543Smarks 	/*
3674543Smarks 	 * permission to set permissions will be evaluated later in
3684543Smarks 	 * dsl_deleg_can_allow()
3694543Smarks 	 */
3704543Smarks 	return (0);
3714543Smarks }
3724543Smarks 
3734543Smarks int
3744543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3754543Smarks {
3764543Smarks 	int error;
3774543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
3784543Smarks 	    ZFS_DELEG_PERM_ROLLBACK, cr);
3794543Smarks 	if (error == 0)
3804543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
3814543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
3824543Smarks 	return (error);
3834543Smarks }
3844543Smarks 
3854543Smarks int
3864543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3874543Smarks {
3884543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
3894543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
3904543Smarks }
3914543Smarks 
3924543Smarks int
3934543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
3944543Smarks {
3954543Smarks 	if (!INGLOBALZONE(curproc))
3964543Smarks 		return (EPERM);
3974543Smarks 
3985367Sahrens 	if (secpolicy_nfs(cr) == 0) {
3994543Smarks 		return (0);
4004543Smarks 	} else {
4014543Smarks 		vnode_t *vp;
4024543Smarks 		int error;
4034543Smarks 
4044543Smarks 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4054543Smarks 		    NO_FOLLOW, NULL, &vp)) != 0)
4064543Smarks 			return (error);
4074543Smarks 
4084543Smarks 		/* Now make sure mntpnt and dataset are ZFS */
4094543Smarks 
4104543Smarks 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4114543Smarks 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4124543Smarks 		    zc->zc_name) != 0)) {
4134543Smarks 			VN_RELE(vp);
4144543Smarks 			return (EPERM);
4154543Smarks 		}
4164543Smarks 
4174543Smarks 		VN_RELE(vp);
4184543Smarks 		return (dsl_deleg_access(zc->zc_name,
4194543Smarks 		    ZFS_DELEG_PERM_SHARE, cr));
4204543Smarks 	}
4214543Smarks }
4224543Smarks 
423789Sahrens static int
4244543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
425789Sahrens {
426789Sahrens 	char *cp;
427789Sahrens 
428789Sahrens 	/*
429789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
430789Sahrens 	 */
4314543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4324543Smarks 	cp = strrchr(parent, '@');
433789Sahrens 	if (cp != NULL) {
434789Sahrens 		cp[0] = '\0';
435789Sahrens 	} else {
4364543Smarks 		cp = strrchr(parent, '/');
437789Sahrens 		if (cp == NULL)
438789Sahrens 			return (ENOENT);
439789Sahrens 		cp[0] = '\0';
440789Sahrens 	}
441789Sahrens 
4424543Smarks 	return (0);
4434543Smarks }
4444543Smarks 
4454543Smarks int
4464543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4474543Smarks {
4484543Smarks 	int error;
4494543Smarks 
4504543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4514543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4524543Smarks 		return (error);
4534543Smarks 
4544543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4554543Smarks }
4564543Smarks 
4574543Smarks static int
4584543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4594543Smarks {
4604543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4614543Smarks }
4624543Smarks 
4634543Smarks /*
4644543Smarks  * Must have sys_config privilege to check the iscsi permission
4654543Smarks  */
4664543Smarks /* ARGSUSED */
4674543Smarks static int
4684543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
4694543Smarks {
4704543Smarks 	return (secpolicy_zfs(cr));
4714543Smarks }
4724543Smarks 
4734543Smarks int
4744543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
4754543Smarks {
4764543Smarks 	char 	parentname[MAXNAMELEN];
4774543Smarks 	int	error;
4784543Smarks 
4794543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
4804543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
4814543Smarks 		return (error);
4824543Smarks 
4834543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
4844543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4854543Smarks 		return (error);
4864543Smarks 
4874543Smarks 	if ((error = zfs_get_parent(to, parentname,
4884543Smarks 	    sizeof (parentname))) != 0)
4894543Smarks 		return (error);
4904543Smarks 
4914543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
4924543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
4934543Smarks 		return (error);
4944543Smarks 
4954543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
4964543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4974543Smarks 		return (error);
4984543Smarks 
4994543Smarks 	return (error);
5004543Smarks }
5014543Smarks 
5024543Smarks static int
5034543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5044543Smarks {
5054543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5064543Smarks }
5074543Smarks 
5084543Smarks static int
5094543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5104543Smarks {
5114543Smarks 	char 	parentname[MAXNAMELEN];
5124543Smarks 	objset_t *clone;
5134543Smarks 	int error;
5144543Smarks 
5154543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5164543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5174543Smarks 	if (error)
5184543Smarks 		return (error);
5194543Smarks 
5204543Smarks 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
5216689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &clone);
5224543Smarks 
5234543Smarks 	if (error == 0) {
5244543Smarks 		dsl_dataset_t *pclone = NULL;
5254543Smarks 		dsl_dir_t *dd;
5264543Smarks 		dd = clone->os->os_dsl_dataset->ds_dir;
5274543Smarks 
5284543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5296689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5306689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5314543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5324543Smarks 		if (error) {
5334543Smarks 			dmu_objset_close(clone);
5344543Smarks 			return (error);
5354543Smarks 		}
5364543Smarks 
5374543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5384543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5394543Smarks 
5404543Smarks 		dsl_dataset_name(pclone, parentname);
5414543Smarks 		dmu_objset_close(clone);
5426689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5434543Smarks 		if (error == 0)
5444543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5454543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5464543Smarks 	}
5474543Smarks 	return (error);
5484543Smarks }
5494543Smarks 
5504543Smarks static int
5514543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5524543Smarks {
5534543Smarks 	int error;
5544543Smarks 
5554543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5564543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5574543Smarks 		return (error);
5584543Smarks 
5594543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5604543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5614543Smarks 		return (error);
5624543Smarks 
5634543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5644543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5654543Smarks }
5664543Smarks 
5674543Smarks int
5684543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
5694543Smarks {
5704543Smarks 	int error;
5714543Smarks 
5724543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
5734543Smarks 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
5744543Smarks 		return (error);
5754543Smarks 
5764543Smarks 	error = zfs_secpolicy_write_perms(name,
5774543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
5784543Smarks 
5794543Smarks 	return (error);
5804543Smarks }
5814543Smarks 
5824543Smarks static int
5834543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
5844543Smarks {
5854543Smarks 
5864543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
5874543Smarks }
5884543Smarks 
5894543Smarks static int
5904543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
5914543Smarks {
5924543Smarks 	char 	parentname[MAXNAMELEN];
5934543Smarks 	int 	error;
5944543Smarks 
5954543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
5964543Smarks 	    sizeof (parentname))) != 0)
5974543Smarks 		return (error);
5984543Smarks 
5994543Smarks 	if (zc->zc_value[0] != '\0') {
6004543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6014543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6024543Smarks 			return (error);
6034543Smarks 	}
6044543Smarks 
6054543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6064543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6074543Smarks 		return (error);
6084543Smarks 
6094543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6104543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6114543Smarks 
6124543Smarks 	return (error);
6134543Smarks }
6144543Smarks 
6154543Smarks static int
6164543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6174543Smarks {
6184543Smarks 	int error;
6194543Smarks 
6204543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6214543Smarks 	if (error) {
6224543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6234543Smarks 	}
6244543Smarks 	return (error);
625789Sahrens }
626789Sahrens 
627789Sahrens /*
628789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
629789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
630789Sahrens  */
631789Sahrens /* ARGSUSED */
632789Sahrens static int
6334543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
634789Sahrens {
635789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
636789Sahrens 		return (EPERM);
637789Sahrens 
638789Sahrens 	return (0);
639789Sahrens }
640789Sahrens 
641789Sahrens /*
6424543Smarks  * Just like zfs_secpolicy_config, except that we will check for
6434543Smarks  * mount permission on the dataset for permission to create/remove
6444543Smarks  * the minor nodes.
6454543Smarks  */
6464543Smarks static int
6474543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
6484543Smarks {
6494543Smarks 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
6504543Smarks 		return (dsl_deleg_access(zc->zc_name,
6514543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr));
6524543Smarks 	}
6534543Smarks 
6544543Smarks 	return (0);
6554543Smarks }
6564543Smarks 
6574543Smarks /*
6581544Seschrock  * Policy for fault injection.  Requires all privileges.
6591544Seschrock  */
6601544Seschrock /* ARGSUSED */
6611544Seschrock static int
6624543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6631544Seschrock {
6641544Seschrock 	return (secpolicy_zinject(cr));
6651544Seschrock }
6661544Seschrock 
6674849Sahrens static int
6684849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
6694849Sahrens {
6704849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
6714849Sahrens 
6725094Slling 	if (prop == ZPROP_INVAL) {
6734849Sahrens 		if (!zfs_prop_user(zc->zc_value))
6744849Sahrens 			return (EINVAL);
6754849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
6764849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
6774849Sahrens 	} else {
6784849Sahrens 		if (!zfs_prop_inheritable(prop))
6794849Sahrens 			return (EINVAL);
6804849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
6814849Sahrens 	}
6824849Sahrens }
6834849Sahrens 
6841544Seschrock /*
685789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
686789Sahrens  */
687789Sahrens static int
6885094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
689789Sahrens {
690789Sahrens 	char *packed;
691789Sahrens 	int error;
6925094Slling 	nvlist_t *list = NULL;
693789Sahrens 
694789Sahrens 	/*
6952676Seschrock 	 * Read in and unpack the user-supplied nvlist.
696789Sahrens 	 */
6975094Slling 	if (size == 0)
698789Sahrens 		return (EINVAL);
699789Sahrens 
700789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
701789Sahrens 
7025094Slling 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
703789Sahrens 		kmem_free(packed, size);
704789Sahrens 		return (error);
705789Sahrens 	}
706789Sahrens 
7075094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
708789Sahrens 		kmem_free(packed, size);
709789Sahrens 		return (error);
710789Sahrens 	}
711789Sahrens 
712789Sahrens 	kmem_free(packed, size);
713789Sahrens 
7145094Slling 	*nvp = list;
715789Sahrens 	return (0);
716789Sahrens }
717789Sahrens 
718789Sahrens static int
7192676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
7202676Seschrock {
7212676Seschrock 	char *packed = NULL;
7222676Seschrock 	size_t size;
7232676Seschrock 	int error;
7242676Seschrock 
7252676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
7262676Seschrock 
7272676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
7282676Seschrock 		error = ENOMEM;
7292676Seschrock 	} else {
7304611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
7312676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
7322676Seschrock 		    KM_SLEEP) == 0);
7332676Seschrock 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
7342676Seschrock 		    size);
7352676Seschrock 		kmem_free(packed, size);
7362676Seschrock 	}
7372676Seschrock 
7382676Seschrock 	zc->zc_nvlist_dst_size = size;
7392676Seschrock 	return (error);
7402676Seschrock }
7412676Seschrock 
7422676Seschrock static int
743789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
744789Sahrens {
745789Sahrens 	int error;
7465094Slling 	nvlist_t *config, *props = NULL;
7477184Stimh 	nvlist_t *rootprops = NULL;
7487184Stimh 	nvlist_t *zplprops = NULL;
7494715Sek110237 	char *buf;
750789Sahrens 
7515094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
7525094Slling 	    &config))
7534988Sek110237 		return (error);
7544715Sek110237 
7555094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
7565094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
7575094Slling 		nvlist_free(config);
7585094Slling 		return (error);
7595094Slling 	}
7605094Slling 
7617184Stimh 	if (props) {
7627184Stimh 		nvlist_t *nvl = NULL;
7637184Stimh 		uint64_t version = SPA_VERSION;
7647184Stimh 
7657184Stimh 		(void) nvlist_lookup_uint64(props,
7667184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
7677184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
7687184Stimh 			error = EINVAL;
7697184Stimh 			goto pool_props_bad;
7707184Stimh 		}
7717184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
7727184Stimh 		if (nvl) {
7737184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
7747184Stimh 			if (error != 0) {
7757184Stimh 				nvlist_free(config);
7767184Stimh 				nvlist_free(props);
7777184Stimh 				return (error);
7787184Stimh 			}
7797184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
7807184Stimh 		}
7817184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7827184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
7837184Stimh 		    zplprops, NULL);
7847184Stimh 		if (error)
7857184Stimh 			goto pool_props_bad;
7867184Stimh 	}
7877184Stimh 
7884988Sek110237 	buf = history_str_get(zc);
789789Sahrens 
7907184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
7917184Stimh 
7927184Stimh 	/*
7937184Stimh 	 * Set the remaining root properties
7947184Stimh 	 */
7957184Stimh 	if (!error &&
7967184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
7977184Stimh 		(void) spa_destroy(zc->zc_name);
798789Sahrens 
7994988Sek110237 	if (buf != NULL)
8004988Sek110237 		history_str_free(buf);
8015094Slling 
8027184Stimh pool_props_bad:
8037184Stimh 	nvlist_free(rootprops);
8047184Stimh 	nvlist_free(zplprops);
805789Sahrens 	nvlist_free(config);
8067184Stimh 	nvlist_free(props);
8075094Slling 
808789Sahrens 	return (error);
809789Sahrens }
810789Sahrens 
811789Sahrens static int
812789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
813789Sahrens {
8144543Smarks 	int error;
8154543Smarks 	zfs_log_history(zc);
8164543Smarks 	error = spa_destroy(zc->zc_name);
8174543Smarks 	return (error);
818789Sahrens }
819789Sahrens 
820789Sahrens static int
821789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
822789Sahrens {
823789Sahrens 	int error;
8245094Slling 	nvlist_t *config, *props = NULL;
825789Sahrens 	uint64_t guid;
826789Sahrens 
8275094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8285094Slling 	    &config)) != 0)
829789Sahrens 		return (error);
830789Sahrens 
8315094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8325094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
8335094Slling 		nvlist_free(config);
8345094Slling 		return (error);
8355094Slling 	}
8365094Slling 
837789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
8381544Seschrock 	    guid != zc->zc_guid)
839789Sahrens 		error = EINVAL;
8406643Seschrock 	else if (zc->zc_cookie)
8416643Seschrock 		error = spa_import_faulted(zc->zc_name, config,
8426643Seschrock 		    props);
843789Sahrens 	else
8445094Slling 		error = spa_import(zc->zc_name, config, props);
845789Sahrens 
846789Sahrens 	nvlist_free(config);
847789Sahrens 
8485094Slling 	if (props)
8495094Slling 		nvlist_free(props);
8505094Slling 
851789Sahrens 	return (error);
852789Sahrens }
853789Sahrens 
854789Sahrens static int
855789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
856789Sahrens {
8574543Smarks 	int error;
8587214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
8598211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
8607214Slling 
8614543Smarks 	zfs_log_history(zc);
8628211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
8634543Smarks 	return (error);
864789Sahrens }
865789Sahrens 
866789Sahrens static int
867789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
868789Sahrens {
869789Sahrens 	nvlist_t *configs;
870789Sahrens 	int error;
871789Sahrens 
872789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
873789Sahrens 		return (EEXIST);
874789Sahrens 
8752676Seschrock 	error = put_nvlist(zc, configs);
876789Sahrens 
877789Sahrens 	nvlist_free(configs);
878789Sahrens 
879789Sahrens 	return (error);
880789Sahrens }
881789Sahrens 
882789Sahrens static int
883789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
884789Sahrens {
885789Sahrens 	nvlist_t *config;
886789Sahrens 	int error;
8871544Seschrock 	int ret = 0;
888789Sahrens 
8892676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
8902676Seschrock 	    sizeof (zc->zc_value));
891789Sahrens 
892789Sahrens 	if (config != NULL) {
8932676Seschrock 		ret = put_nvlist(zc, config);
894789Sahrens 		nvlist_free(config);
8951544Seschrock 
8961544Seschrock 		/*
8971544Seschrock 		 * The config may be present even if 'error' is non-zero.
8981544Seschrock 		 * In this case we return success, and preserve the real errno
8991544Seschrock 		 * in 'zc_cookie'.
9001544Seschrock 		 */
9011544Seschrock 		zc->zc_cookie = error;
902789Sahrens 	} else {
9031544Seschrock 		ret = error;
904789Sahrens 	}
905789Sahrens 
9061544Seschrock 	return (ret);
907789Sahrens }
908789Sahrens 
909789Sahrens /*
910789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
911789Sahrens  * user land knows which devices are available and overall pool health.
912789Sahrens  */
913789Sahrens static int
914789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
915789Sahrens {
916789Sahrens 	nvlist_t *tryconfig, *config;
917789Sahrens 	int error;
918789Sahrens 
9195094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9205094Slling 	    &tryconfig)) != 0)
921789Sahrens 		return (error);
922789Sahrens 
923789Sahrens 	config = spa_tryimport(tryconfig);
924789Sahrens 
925789Sahrens 	nvlist_free(tryconfig);
926789Sahrens 
927789Sahrens 	if (config == NULL)
928789Sahrens 		return (EINVAL);
929789Sahrens 
9302676Seschrock 	error = put_nvlist(zc, config);
931789Sahrens 	nvlist_free(config);
932789Sahrens 
933789Sahrens 	return (error);
934789Sahrens }
935789Sahrens 
936789Sahrens static int
937789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
938789Sahrens {
939789Sahrens 	spa_t *spa;
940789Sahrens 	int error;
941789Sahrens 
9422926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
9432926Sek110237 		return (error);
9442926Sek110237 
9457046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
9462926Sek110237 
9472926Sek110237 	spa_close(spa, FTAG);
9482926Sek110237 
949789Sahrens 	return (error);
950789Sahrens }
951789Sahrens 
952789Sahrens static int
953789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
954789Sahrens {
955789Sahrens 	spa_t *spa;
956789Sahrens 	int error;
957789Sahrens 
958789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
959789Sahrens 	if (error == 0) {
960789Sahrens 		spa_freeze(spa);
961789Sahrens 		spa_close(spa, FTAG);
962789Sahrens 	}
963789Sahrens 	return (error);
964789Sahrens }
965789Sahrens 
966789Sahrens static int
9671760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
9681760Seschrock {
9691760Seschrock 	spa_t *spa;
9701760Seschrock 	int error;
9711760Seschrock 
9722926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
9732926Sek110237 		return (error);
9742926Sek110237 
9755118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
9765118Slling 		spa_close(spa, FTAG);
9775118Slling 		return (EINVAL);
9785118Slling 	}
9795118Slling 
9805094Slling 	spa_upgrade(spa, zc->zc_cookie);
9812926Sek110237 	spa_close(spa, FTAG);
9822926Sek110237 
9832926Sek110237 	return (error);
9842926Sek110237 }
9852926Sek110237 
9862926Sek110237 static int
9872926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
9882926Sek110237 {
9892926Sek110237 	spa_t *spa;
9902926Sek110237 	char *hist_buf;
9912926Sek110237 	uint64_t size;
9922926Sek110237 	int error;
9932926Sek110237 
9942926Sek110237 	if ((size = zc->zc_history_len) == 0)
9952926Sek110237 		return (EINVAL);
9962926Sek110237 
9972926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
9982926Sek110237 		return (error);
9992926Sek110237 
10004577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
10013863Sek110237 		spa_close(spa, FTAG);
10023863Sek110237 		return (ENOTSUP);
10033863Sek110237 	}
10043863Sek110237 
10052926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
10062926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
10072926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
10084543Smarks 		error = xcopyout(hist_buf,
10094543Smarks 		    (char *)(uintptr_t)zc->zc_history,
10102926Sek110237 		    zc->zc_history_len);
10112926Sek110237 	}
10122926Sek110237 
10132926Sek110237 	spa_close(spa, FTAG);
10142926Sek110237 	kmem_free(hist_buf, size);
10152926Sek110237 	return (error);
10162926Sek110237 }
10172926Sek110237 
10182926Sek110237 static int
10193444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
10203444Sek110237 {
10213444Sek110237 	int error;
10223444Sek110237 
10233912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
10243444Sek110237 		return (error);
10253444Sek110237 
10263444Sek110237 	return (0);
10273444Sek110237 }
10283444Sek110237 
10293444Sek110237 static int
10303444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
10313444Sek110237 {
10323444Sek110237 	objset_t *osp;
10333444Sek110237 	int error;
10343444Sek110237 
10353444Sek110237 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
10366689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
10373444Sek110237 		return (error);
10383444Sek110237 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
10393444Sek110237 	    sizeof (zc->zc_value));
10403444Sek110237 	dmu_objset_close(osp);
10413444Sek110237 
10423444Sek110237 	return (error);
10433444Sek110237 }
10443444Sek110237 
10453444Sek110237 static int
1046789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1047789Sahrens {
1048789Sahrens 	spa_t *spa;
1049789Sahrens 	int error;
10506423Sgw25295 	nvlist_t *config, **l2cache, **spares;
10516423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1052789Sahrens 
1053789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1054789Sahrens 	if (error != 0)
1055789Sahrens 		return (error);
1056789Sahrens 
10575450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10585450Sbrendan 	    &config);
10595450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
10605450Sbrendan 	    &l2cache, &nl2cache);
10615450Sbrendan 
10626423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
10636423Sgw25295 	    &spares, &nspares);
10646423Sgw25295 
10653912Slling 	/*
10663912Slling 	 * A root pool with concatenated devices is not supported.
10676423Sgw25295 	 * Thus, can not add a device to a root pool.
10686423Sgw25295 	 *
10696423Sgw25295 	 * Intent log device can not be added to a rootpool because
10706423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
10716423Sgw25295 	 * can not be accessed during the mountroot time.
10726423Sgw25295 	 *
10736423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
10743912Slling 	 */
10756423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
10763912Slling 		spa_close(spa, FTAG);
10773912Slling 		return (EDOM);
10783912Slling 	}
10793912Slling 
10805450Sbrendan 	if (error == 0) {
1081789Sahrens 		error = spa_vdev_add(spa, config);
1082789Sahrens 		nvlist_free(config);
1083789Sahrens 	}
1084789Sahrens 	spa_close(spa, FTAG);
1085789Sahrens 	return (error);
1086789Sahrens }
1087789Sahrens 
1088789Sahrens static int
1089789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1090789Sahrens {
10912082Seschrock 	spa_t *spa;
10922082Seschrock 	int error;
10932082Seschrock 
10942082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
10952082Seschrock 	if (error != 0)
10962082Seschrock 		return (error);
10972082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
10982082Seschrock 	spa_close(spa, FTAG);
10992082Seschrock 	return (error);
1100789Sahrens }
1101789Sahrens 
1102789Sahrens static int
11034451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1104789Sahrens {
1105789Sahrens 	spa_t *spa;
1106789Sahrens 	int error;
11074451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1108789Sahrens 
11092926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1110789Sahrens 		return (error);
11114451Seschrock 	switch (zc->zc_cookie) {
11124451Seschrock 	case VDEV_STATE_ONLINE:
11134451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
11144451Seschrock 		break;
11154451Seschrock 
11164451Seschrock 	case VDEV_STATE_OFFLINE:
11174451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
11184451Seschrock 		break;
1119789Sahrens 
11204451Seschrock 	case VDEV_STATE_FAULTED:
11214451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
11224451Seschrock 		break;
1123789Sahrens 
11244451Seschrock 	case VDEV_STATE_DEGRADED:
11254451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
11264451Seschrock 		break;
11274451Seschrock 
11284451Seschrock 	default:
11294451Seschrock 		error = EINVAL;
11304451Seschrock 	}
11314451Seschrock 	zc->zc_cookie = newstate;
1132789Sahrens 	spa_close(spa, FTAG);
1133789Sahrens 	return (error);
1134789Sahrens }
1135789Sahrens 
1136789Sahrens static int
1137789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1138789Sahrens {
1139789Sahrens 	spa_t *spa;
1140789Sahrens 	int replacing = zc->zc_cookie;
1141789Sahrens 	nvlist_t *config;
1142789Sahrens 	int error;
1143789Sahrens 
11442926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1145789Sahrens 		return (error);
1146789Sahrens 
11475094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
11485094Slling 	    &config)) == 0) {
11491544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1150789Sahrens 		nvlist_free(config);
1151789Sahrens 	}
1152789Sahrens 
1153789Sahrens 	spa_close(spa, FTAG);
1154789Sahrens 	return (error);
1155789Sahrens }
1156789Sahrens 
1157789Sahrens static int
1158789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1159789Sahrens {
1160789Sahrens 	spa_t *spa;
1161789Sahrens 	int error;
1162789Sahrens 
11632926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1164789Sahrens 		return (error);
1165789Sahrens 
11668241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1167789Sahrens 
1168789Sahrens 	spa_close(spa, FTAG);
1169789Sahrens 	return (error);
1170789Sahrens }
1171789Sahrens 
1172789Sahrens static int
11731354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
11741354Seschrock {
11751354Seschrock 	spa_t *spa;
11762676Seschrock 	char *path = zc->zc_value;
11771544Seschrock 	uint64_t guid = zc->zc_guid;
11781354Seschrock 	int error;
11791354Seschrock 
11801354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
11811354Seschrock 	if (error != 0)
11821354Seschrock 		return (error);
11831354Seschrock 
11841354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
11851354Seschrock 	spa_close(spa, FTAG);
11861354Seschrock 	return (error);
11871354Seschrock }
11881354Seschrock 
11895367Sahrens /*
11905367Sahrens  * inputs:
11915367Sahrens  * zc_name		name of filesystem
11925367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
11935367Sahrens  *
11945367Sahrens  * outputs:
11955367Sahrens  * zc_objset_stats	stats
11965367Sahrens  * zc_nvlist_dst	property nvlist
11975367Sahrens  * zc_nvlist_dst_size	size of property nvlist
11985367Sahrens  */
11991354Seschrock static int
1200789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1201789Sahrens {
1202789Sahrens 	objset_t *os = NULL;
1203789Sahrens 	int error;
12041356Seschrock 	nvlist_t *nv;
1205789Sahrens 
12066689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
12076689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1208789Sahrens 		return (error);
1209789Sahrens 
12102885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1211789Sahrens 
12122856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
12136689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
12142885Sahrens 		dmu_objset_stats(os, nv);
12153087Sahrens 		/*
12165147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
12173087Sahrens 		 * which we aren't supposed to do with a
12186689Smaybee 		 * DS_MODE_USER hold, because it could be
12193087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
12203087Sahrens 		 */
12214577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
12224577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
12234577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
12244577Sahrens 		}
12252676Seschrock 		error = put_nvlist(zc, nv);
12261356Seschrock 		nvlist_free(nv);
12271356Seschrock 	}
1228789Sahrens 
1229789Sahrens 	dmu_objset_close(os);
1230789Sahrens 	return (error);
1231789Sahrens }
1232789Sahrens 
12335498Stimh static int
12345498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
12355498Stimh {
12365498Stimh 	uint64_t value;
12375498Stimh 	int error;
12385498Stimh 
12395498Stimh 	/*
12405498Stimh 	 * zfs_get_zplprop() will either find a value or give us
12415498Stimh 	 * the default value (if there is one).
12425498Stimh 	 */
12435498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
12445498Stimh 		return (error);
12455498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
12465498Stimh 	return (0);
12475498Stimh }
12485498Stimh 
12495498Stimh /*
12505498Stimh  * inputs:
12515498Stimh  * zc_name		name of filesystem
12525498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
12535498Stimh  *
12545498Stimh  * outputs:
12555498Stimh  * zc_nvlist_dst	zpl property nvlist
12565498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
12575498Stimh  */
12585498Stimh static int
12595498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
12605498Stimh {
12615498Stimh 	objset_t *os;
12625498Stimh 	int err;
12635498Stimh 
12646689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
12656689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
12665498Stimh 		return (err);
12675498Stimh 
12685498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
12695498Stimh 
12705498Stimh 	/*
12715498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
12726689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
12736689Smaybee 	 * hold, because it could be inconsistent.
12745498Stimh 	 */
12755498Stimh 	if (zc->zc_nvlist_dst != NULL &&
12765498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
12775498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
12785498Stimh 		nvlist_t *nv;
12795498Stimh 
12805498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
12815498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
12825498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
12835498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
12845498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
12855498Stimh 			err = put_nvlist(zc, nv);
12865498Stimh 		nvlist_free(nv);
12875498Stimh 	} else {
12885498Stimh 		err = ENOENT;
12895498Stimh 	}
12905498Stimh 	dmu_objset_close(os);
12915498Stimh 	return (err);
12925498Stimh }
12935498Stimh 
12945367Sahrens /*
12955367Sahrens  * inputs:
12965367Sahrens  * zc_name		name of filesystem
12975367Sahrens  * zc_cookie		zap cursor
12985367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
12995367Sahrens  *
13005367Sahrens  * outputs:
13015367Sahrens  * zc_name		name of next filesystem
13025367Sahrens  * zc_objset_stats	stats
13035367Sahrens  * zc_nvlist_dst	property nvlist
13045367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13055367Sahrens  */
1306789Sahrens static int
1307789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1308789Sahrens {
1309885Sahrens 	objset_t *os;
1310789Sahrens 	int error;
1311789Sahrens 	char *p;
1312789Sahrens 
13136689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13146689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1315885Sahrens 		if (error == ENOENT)
1316885Sahrens 			error = ESRCH;
1317885Sahrens 		return (error);
1318789Sahrens 	}
1319789Sahrens 
1320789Sahrens 	p = strrchr(zc->zc_name, '/');
1321789Sahrens 	if (p == NULL || p[1] != '\0')
1322789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1323789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1324789Sahrens 
1325*8697SRichard.Morris@Sun.COM 	/*
1326*8697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
1327*8697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
1328*8697SRichard.Morris@Sun.COM 	 */
13298415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
13308415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
13318415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
13328415SRichard.Morris@Sun.COM 
13338415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1334*8697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
13358415SRichard.Morris@Sun.COM 	}
13368415SRichard.Morris@Sun.COM 
1337789Sahrens 	do {
1338885Sahrens 		error = dmu_dir_list_next(os,
1339885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1340885Sahrens 		    NULL, &zc->zc_cookie);
1341789Sahrens 		if (error == ENOENT)
1342789Sahrens 			error = ESRCH;
1343885Sahrens 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1344789Sahrens 	    !zone_dataset_visible(zc->zc_name, NULL));
13456689Smaybee 	dmu_objset_close(os);
1346789Sahrens 
1347885Sahrens 	/*
1348885Sahrens 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1349885Sahrens 	 * try to get stats for it.  Userland will skip over it.
1350885Sahrens 	 */
1351885Sahrens 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1352885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1353789Sahrens 
1354789Sahrens 	return (error);
1355789Sahrens }
1356789Sahrens 
13575367Sahrens /*
13585367Sahrens  * inputs:
13595367Sahrens  * zc_name		name of filesystem
13605367Sahrens  * zc_cookie		zap cursor
13615367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13625367Sahrens  *
13635367Sahrens  * outputs:
13645367Sahrens  * zc_name		name of next snapshot
13655367Sahrens  * zc_objset_stats	stats
13665367Sahrens  * zc_nvlist_dst	property nvlist
13675367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13685367Sahrens  */
1369789Sahrens static int
1370789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1371789Sahrens {
1372885Sahrens 	objset_t *os;
1373789Sahrens 	int error;
1374789Sahrens 
13756689Smaybee 	error = dmu_objset_open(zc->zc_name,
13766689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
13776689Smaybee 	if (error)
13786689Smaybee 		return (error == ENOENT ? ESRCH : error);
1379789Sahrens 
13808415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
1381*8697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
13828415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
13831003Slling 	/*
13841003Slling 	 * A dataset name of maximum length cannot have any snapshots,
13851003Slling 	 * so exit immediately.
13861003Slling 	 */
13871003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1388885Sahrens 		dmu_objset_close(os);
13891003Slling 		return (ESRCH);
1390789Sahrens 	}
1391789Sahrens 
1392885Sahrens 	error = dmu_snapshot_list_next(os,
1393885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
13945663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
13956689Smaybee 	dmu_objset_close(os);
1396885Sahrens 	if (error == 0)
1397885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
13986689Smaybee 	else if (error == ENOENT)
13996689Smaybee 		error = ESRCH;
1400789Sahrens 
14015367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
14026689Smaybee 	if (error)
14035367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1404789Sahrens 	return (error);
1405789Sahrens }
1406789Sahrens 
14076423Sgw25295 int
14084787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1409789Sahrens {
14102676Seschrock 	nvpair_t *elem;
14112676Seschrock 	int error;
14122676Seschrock 	uint64_t intval;
14132676Seschrock 	char *strval;
1414*8697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
14152676Seschrock 
14164543Smarks 	/*
14174543Smarks 	 * First validate permission to set all of the properties
14184543Smarks 	 */
14192676Seschrock 	elem = NULL;
14202676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
14214670Sahrens 		const char *propname = nvpair_name(elem);
14224670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
14232676Seschrock 
14245094Slling 		if (prop == ZPROP_INVAL) {
14252676Seschrock 			/*
14262676Seschrock 			 * If this is a user-defined property, it must be a
14272676Seschrock 			 * string, and there is no further validation to do.
14282676Seschrock 			 */
14292676Seschrock 			if (!zfs_prop_user(propname) ||
14302676Seschrock 			    nvpair_type(elem) != DATA_TYPE_STRING)
14312676Seschrock 				return (EINVAL);
14322676Seschrock 
14335331Samw 			if (error = zfs_secpolicy_write_perms(name,
14345331Samw 			    ZFS_DELEG_PERM_USERPROP, CRED()))
14354670Sahrens 				return (error);
14364543Smarks 			continue;
14372676Seschrock 		}
14382676Seschrock 
14394787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
14404670Sahrens 			return (error);
14412676Seschrock 
14424670Sahrens 		/*
14434670Sahrens 		 * Check that this value is valid for this pool version
14444670Sahrens 		 */
14454670Sahrens 		switch (prop) {
14463886Sahl 		case ZFS_PROP_COMPRESSION:
14473886Sahl 			/*
14483886Sahl 			 * If the user specified gzip compression, make sure
14493886Sahl 			 * the SPA supports it. We ignore any errors here since
14503886Sahl 			 * we'll catch them later.
14513886Sahl 			 */
14523886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
14537042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
14547042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
14557042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
14567184Stimh 				    zfs_earlier_version(name,
14575331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
14585331Samw 					return (ENOTSUP);
14597042Sgw25295 
14607042Sgw25295 				/*
14617042Sgw25295 				 * If this is a bootable dataset then
14627042Sgw25295 				 * verify that the compression algorithm
14637042Sgw25295 				 * is supported for booting. We must return
14647042Sgw25295 				 * something other than ENOTSUP since it
14657042Sgw25295 				 * implies a downrev pool version.
14667042Sgw25295 				 */
14677042Sgw25295 				if (zfs_is_bootfs(name) &&
14687042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
14697042Sgw25295 					return (ERANGE);
14703886Sahl 			}
14713886Sahl 			break;
14724603Sahrens 
14734603Sahrens 		case ZFS_PROP_COPIES:
14747184Stimh 			if (zfs_earlier_version(name,
14757184Stimh 			    SPA_VERSION_DITTO_BLOCKS))
14765331Samw 				return (ENOTSUP);
14774603Sahrens 			break;
14785977Smarks 
14795977Smarks 		case ZFS_PROP_SHARESMB:
14806689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
14815977Smarks 				return (ENOTSUP);
14825977Smarks 			break;
14838053SMark.Shellenbaum@Sun.COM 
14848053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
14858053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
14868053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
14878053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
14888053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
14898053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
14908053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
14914603Sahrens 		}
14924543Smarks 	}
14934543Smarks 
1494*8697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14954543Smarks 	elem = NULL;
14964543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
14974670Sahrens 		const char *propname = nvpair_name(elem);
14984670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
14994543Smarks 
15005094Slling 		if (prop == ZPROP_INVAL) {
15014543Smarks 			VERIFY(nvpair_value_string(elem, &strval) == 0);
15024543Smarks 			error = dsl_prop_set(name, propname, 1,
15034543Smarks 			    strlen(strval) + 1, strval);
15044543Smarks 			if (error == 0)
15054543Smarks 				continue;
15064543Smarks 			else
1507*8697SRichard.Morris@Sun.COM 				goto out;
15084543Smarks 		}
15092676Seschrock 
15102676Seschrock 		switch (prop) {
15112676Seschrock 		case ZFS_PROP_QUOTA:
15122676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15134577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1514*8697SRichard.Morris@Sun.COM 				goto out;
15152676Seschrock 			break;
15162676Seschrock 
15175378Sck153898 		case ZFS_PROP_REFQUOTA:
15185378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15195378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
1520*8697SRichard.Morris@Sun.COM 				goto out;
15215378Sck153898 			break;
15225378Sck153898 
15232676Seschrock 		case ZFS_PROP_RESERVATION:
15242676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15252676Seschrock 			    (error = dsl_dir_set_reservation(name,
15262676Seschrock 			    intval)) != 0)
1527*8697SRichard.Morris@Sun.COM 				goto out;
15282676Seschrock 			break;
1529789Sahrens 
15305378Sck153898 		case ZFS_PROP_REFRESERVATION:
15315378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15325378Sck153898 			    (error = dsl_dataset_set_reservation(name,
15335378Sck153898 			    intval)) != 0)
1534*8697SRichard.Morris@Sun.COM 				goto out;
15355378Sck153898 			break;
15365378Sck153898 
15372676Seschrock 		case ZFS_PROP_VOLSIZE:
15382676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15394787Sahrens 			    (error = zvol_set_volsize(name,
15404787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
1541*8697SRichard.Morris@Sun.COM 				goto out;
15422676Seschrock 			break;
15432676Seschrock 
15442676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
15452676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15464577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1547*8697SRichard.Morris@Sun.COM 				goto out;
15484577Sahrens 			break;
15494577Sahrens 
15504577Sahrens 		case ZFS_PROP_VERSION:
15514577Sahrens 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15524577Sahrens 			    (error = zfs_set_version(name, intval)) != 0)
1553*8697SRichard.Morris@Sun.COM 				goto out;
15542676Seschrock 			break;
15552676Seschrock 
15562676Seschrock 		default:
15572676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
15582676Seschrock 				if (zfs_prop_get_type(prop) !=
1559*8697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
1560*8697SRichard.Morris@Sun.COM 					error = EINVAL;
1561*8697SRichard.Morris@Sun.COM 					goto out;
1562*8697SRichard.Morris@Sun.COM 				}
15632676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
15642885Sahrens 				const char *unused;
15652885Sahrens 
15662717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
15672676Seschrock 
15682676Seschrock 				switch (zfs_prop_get_type(prop)) {
15694787Sahrens 				case PROP_TYPE_NUMBER:
15702676Seschrock 					break;
15714787Sahrens 				case PROP_TYPE_STRING:
1572*8697SRichard.Morris@Sun.COM 					error = EINVAL;
1573*8697SRichard.Morris@Sun.COM 					goto out;
15744787Sahrens 				case PROP_TYPE_INDEX:
15752717Seschrock 					if (zfs_prop_index_to_string(prop,
1576*8697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
1577*8697SRichard.Morris@Sun.COM 						error = EINVAL;
1578*8697SRichard.Morris@Sun.COM 						goto out;
1579*8697SRichard.Morris@Sun.COM 					}
15802676Seschrock 					break;
15812676Seschrock 				default:
15824577Sahrens 					cmn_err(CE_PANIC,
15834577Sahrens 					    "unknown property type");
15842676Seschrock 					break;
15852676Seschrock 				}
15862676Seschrock 			} else {
1587*8697SRichard.Morris@Sun.COM 				error = EINVAL;
1588*8697SRichard.Morris@Sun.COM 				goto out;
15892676Seschrock 			}
1590*8697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
1591*8697SRichard.Morris@Sun.COM 				goto out;
15922676Seschrock 		}
15932676Seschrock 	}
15942676Seschrock 
1595*8697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
1596*8697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
1597*8697SRichard.Morris@Sun.COM 	}
1598*8697SRichard.Morris@Sun.COM out:
1599*8697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
1600*8697SRichard.Morris@Sun.COM 	return (error);
1601789Sahrens }
1602789Sahrens 
16035367Sahrens /*
16045367Sahrens  * inputs:
16055367Sahrens  * zc_name		name of filesystem
1606*8697SRichard.Morris@Sun.COM  * zc_value		name of property to set
16075367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
16087265Sahrens  * zc_cookie		clear existing local props?
16095367Sahrens  *
16105367Sahrens  * outputs:		none
16115367Sahrens  */
1612789Sahrens static int
16132676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1614789Sahrens {
16152676Seschrock 	nvlist_t *nvl;
16162676Seschrock 	int error;
1617789Sahrens 
16185094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16195094Slling 	    &nvl)) != 0)
16202676Seschrock 		return (error);
16212676Seschrock 
16227265Sahrens 	if (zc->zc_cookie) {
16237265Sahrens 		nvlist_t *origprops;
16247265Sahrens 		objset_t *os;
16257265Sahrens 
16267265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
16277265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
16287265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
16298536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
16307265Sahrens 				nvlist_free(origprops);
16317265Sahrens 			}
16327265Sahrens 			dmu_objset_close(os);
16337265Sahrens 		}
16347265Sahrens 
16357265Sahrens 	}
16367265Sahrens 
16374787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
16384543Smarks 
16392676Seschrock 	nvlist_free(nvl);
16402676Seschrock 	return (error);
1641789Sahrens }
1642789Sahrens 
16435367Sahrens /*
16445367Sahrens  * inputs:
16455367Sahrens  * zc_name		name of filesystem
16465367Sahrens  * zc_value		name of property to inherit
16475367Sahrens  *
16485367Sahrens  * outputs:		none
16495367Sahrens  */
1650789Sahrens static int
16514849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
16524849Sahrens {
16534849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
16544849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
16554849Sahrens }
16564849Sahrens 
16574849Sahrens static int
16584098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
16593912Slling {
16605094Slling 	nvlist_t *props;
16613912Slling 	spa_t *spa;
16625094Slling 	int error;
16638525SEric.Schrock@Sun.COM 	nvpair_t *elem;
16643912Slling 
16655094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16665094Slling 	    &props)))
16673912Slling 		return (error);
16683912Slling 
16698525SEric.Schrock@Sun.COM 	/*
16708525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
16718525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
16728525SEric.Schrock@Sun.COM 	 */
16738525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
16748525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
16758525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
16768525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
16778525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
16788525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
16798525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
16808525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
16818525SEric.Schrock@Sun.COM 		}
16828525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
16838525SEric.Schrock@Sun.COM 		if (spa != NULL)
16848525SEric.Schrock@Sun.COM 			return (0);
16858525SEric.Schrock@Sun.COM 	}
16868525SEric.Schrock@Sun.COM 
16873912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
16885094Slling 		nvlist_free(props);
16893912Slling 		return (error);
16903912Slling 	}
16913912Slling 
16925094Slling 	error = spa_prop_set(spa, props);
16933912Slling 
16945094Slling 	nvlist_free(props);
16953912Slling 	spa_close(spa, FTAG);
16963912Slling 
16973912Slling 	return (error);
16983912Slling }
16993912Slling 
17003912Slling static int
17014098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
17023912Slling {
17033912Slling 	spa_t *spa;
17043912Slling 	int error;
17053912Slling 	nvlist_t *nvp = NULL;
17063912Slling 
17078525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
17088525SEric.Schrock@Sun.COM 		/*
17098525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
17108525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
17118525SEric.Schrock@Sun.COM 		 * anyway.
17128525SEric.Schrock@Sun.COM 		 */
17138525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
17148525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
17158525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
17168525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
17178525SEric.Schrock@Sun.COM 	} else {
17188525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
17198525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
17208525SEric.Schrock@Sun.COM 	}
17213912Slling 
17223912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
17233912Slling 		error = put_nvlist(zc, nvp);
17243912Slling 	else
17253912Slling 		error = EFAULT;
17263912Slling 
17278525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
17283912Slling 	return (error);
17293912Slling }
17303912Slling 
17313912Slling static int
17324543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
17334543Smarks {
17344543Smarks 	nvlist_t *nvp;
17354543Smarks 	int error;
17364543Smarks 	uint32_t uid;
17374543Smarks 	uint32_t gid;
17384543Smarks 	uint32_t *groups;
17394543Smarks 	uint_t group_cnt;
17404543Smarks 	cred_t	*usercred;
17414543Smarks 
17425094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17435094Slling 	    &nvp)) != 0) {
17444543Smarks 		return (error);
17454543Smarks 	}
17464543Smarks 
17474543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
17484543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
17494543Smarks 		nvlist_free(nvp);
17504543Smarks 		return (EPERM);
17514543Smarks 	}
17524543Smarks 
17534543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
17544543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
17554543Smarks 		nvlist_free(nvp);
17564543Smarks 		return (EPERM);
17574543Smarks 	}
17584543Smarks 
17594543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
17604543Smarks 	    &groups, &group_cnt)) != 0) {
17614543Smarks 		nvlist_free(nvp);
17624543Smarks 		return (EPERM);
17634543Smarks 	}
17644543Smarks 	usercred = cralloc();
17654543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
17664543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
17674543Smarks 		nvlist_free(nvp);
17684543Smarks 		crfree(usercred);
17694543Smarks 		return (EPERM);
17704543Smarks 	}
17714543Smarks 	nvlist_free(nvp);
17724543Smarks 	error = dsl_deleg_access(zc->zc_name,
17734787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
17744543Smarks 	crfree(usercred);
17754543Smarks 	return (error);
17764543Smarks }
17774543Smarks 
17785367Sahrens /*
17795367Sahrens  * inputs:
17805367Sahrens  * zc_name		name of filesystem
17815367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
17825367Sahrens  * zc_perm_action	allow/unallow flag
17835367Sahrens  *
17845367Sahrens  * outputs:		none
17855367Sahrens  */
17864543Smarks static int
17874543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
17884543Smarks {
17894543Smarks 	int error;
17904543Smarks 	nvlist_t *fsaclnv = NULL;
17914543Smarks 
17925094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17935094Slling 	    &fsaclnv)) != 0)
17944543Smarks 		return (error);
17954543Smarks 
17964543Smarks 	/*
17974543Smarks 	 * Verify nvlist is constructed correctly
17984543Smarks 	 */
17994543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
18004543Smarks 		nvlist_free(fsaclnv);
18014543Smarks 		return (EINVAL);
18024543Smarks 	}
18034543Smarks 
18044543Smarks 	/*
18054543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
18064543Smarks 	 * that user is allowed to hand out each permission in
18074543Smarks 	 * the nvlist(s)
18084543Smarks 	 */
18094543Smarks 
18104787Sahrens 	error = secpolicy_zfs(CRED());
18114543Smarks 	if (error) {
18124787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
18134787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
18144787Sahrens 			    fsaclnv, CRED());
18154787Sahrens 		} else {
18164787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
18174787Sahrens 			    fsaclnv, CRED());
18184787Sahrens 		}
18194543Smarks 	}
18204543Smarks 
18214543Smarks 	if (error == 0)
18224543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
18234543Smarks 
18244543Smarks 	nvlist_free(fsaclnv);
18254543Smarks 	return (error);
18264543Smarks }
18274543Smarks 
18285367Sahrens /*
18295367Sahrens  * inputs:
18305367Sahrens  * zc_name		name of filesystem
18315367Sahrens  *
18325367Sahrens  * outputs:
18335367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
18345367Sahrens  */
18354543Smarks static int
18364543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
18374543Smarks {
18384543Smarks 	nvlist_t *nvp;
18394543Smarks 	int error;
18404543Smarks 
18414543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
18424543Smarks 		error = put_nvlist(zc, nvp);
18434543Smarks 		nvlist_free(nvp);
18444543Smarks 	}
18454543Smarks 
18464543Smarks 	return (error);
18474543Smarks }
18484543Smarks 
18495367Sahrens /*
18505367Sahrens  * inputs:
18515367Sahrens  * zc_name		name of volume
18525367Sahrens  *
18535367Sahrens  * outputs:		none
18545367Sahrens  */
18554543Smarks static int
1856789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
1857789Sahrens {
18584787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1859789Sahrens }
1860789Sahrens 
18615367Sahrens /*
18625367Sahrens  * inputs:
18635367Sahrens  * zc_name		name of volume
18645367Sahrens  *
18655367Sahrens  * outputs:		none
18665367Sahrens  */
1867789Sahrens static int
1868789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
1869789Sahrens {
18702676Seschrock 	return (zvol_remove_minor(zc->zc_name));
1871789Sahrens }
1872789Sahrens 
1873789Sahrens /*
1874789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
1875789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
1876789Sahrens  * is responsible for releasing the returned vfs pointer.
1877789Sahrens  */
1878789Sahrens static vfs_t *
1879789Sahrens zfs_get_vfs(const char *resource)
1880789Sahrens {
1881789Sahrens 	struct vfs *vfsp;
1882789Sahrens 	struct vfs *vfs_found = NULL;
1883789Sahrens 
1884789Sahrens 	vfs_list_read_lock();
1885789Sahrens 	vfsp = rootvfs;
1886789Sahrens 	do {
1887789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1888789Sahrens 			VFS_HOLD(vfsp);
1889789Sahrens 			vfs_found = vfsp;
1890789Sahrens 			break;
1891789Sahrens 		}
1892789Sahrens 		vfsp = vfsp->vfs_next;
1893789Sahrens 	} while (vfsp != rootvfs);
1894789Sahrens 	vfs_list_unlock();
1895789Sahrens 	return (vfs_found);
1896789Sahrens }
1897789Sahrens 
18984543Smarks /* ARGSUSED */
1899789Sahrens static void
19004543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1901789Sahrens {
19025331Samw 	zfs_creat_t *zct = arg;
19035498Stimh 
19045498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
19055331Samw }
19065331Samw 
19075498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
19085498Stimh 
19095331Samw /*
19105498Stimh  * inputs:
19117184Stimh  * createprops		list of properties requested by creator
19127184Stimh  * default_zplver	zpl version to use if unspecified in createprops
19137184Stimh  * fuids_ok		fuids allowed in this version of the spa?
19147184Stimh  * os			parent objset pointer (NULL if root fs)
19155331Samw  *
19165498Stimh  * outputs:
19175498Stimh  * zplprops	values for the zplprops we attach to the master node object
19187184Stimh  * is_ci	true if requested file system will be purely case-insensitive
19195331Samw  *
19205498Stimh  * Determine the settings for utf8only, normalization and
19215498Stimh  * casesensitivity.  Specific values may have been requested by the
19225498Stimh  * creator and/or we can inherit values from the parent dataset.  If
19235498Stimh  * the file system is of too early a vintage, a creator can not
19245498Stimh  * request settings for these properties, even if the requested
19255498Stimh  * setting is the default value.  We don't actually want to create dsl
19265498Stimh  * properties for these, so remove them from the source nvlist after
19275498Stimh  * processing.
19285331Samw  */
19295331Samw static int
19307184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
19317184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
19327184Stimh     boolean_t *is_ci)
19335331Samw {
19347184Stimh 	uint64_t zplver = default_zplver;
19355498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
19365498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
19375498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
19385498Stimh 
19395498Stimh 	ASSERT(zplprops != NULL);
19405498Stimh 
19415375Stimh 	/*
19425498Stimh 	 * Pull out creator prop choices, if any.
19435375Stimh 	 */
19445498Stimh 	if (createprops) {
19455498Stimh 		(void) nvlist_lookup_uint64(createprops,
19467184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
19477184Stimh 		(void) nvlist_lookup_uint64(createprops,
19485498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
19495498Stimh 		(void) nvlist_remove_all(createprops,
19505498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
19515498Stimh 		(void) nvlist_lookup_uint64(createprops,
19525498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
19535498Stimh 		(void) nvlist_remove_all(createprops,
19545498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
19555498Stimh 		(void) nvlist_lookup_uint64(createprops,
19565498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
19575498Stimh 		(void) nvlist_remove_all(createprops,
19585498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
19595331Samw 	}
19605331Samw 
19615375Stimh 	/*
19627184Stimh 	 * If the zpl version requested is whacky or the file system
19637184Stimh 	 * or pool is version is too "young" to support normalization
19647184Stimh 	 * and the creator tried to set a value for one of the props,
19657184Stimh 	 * error out.
19665498Stimh 	 */
19677184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
19687184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
19697184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
19705498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
19717184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
19725498Stimh 		return (ENOTSUP);
19735498Stimh 
19745498Stimh 	/*
19755498Stimh 	 * Put the version in the zplprops
19765498Stimh 	 */
19775498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19785498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
19795498Stimh 
19805498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
19815498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
19825498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19835498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
19845498Stimh 
19855498Stimh 	/*
19865498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
19875498Stimh 	 */
19885498Stimh 	if (norm)
19895498Stimh 		u8 = 1;
19905498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
19915498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
19925498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19935498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
19945498Stimh 
19955498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
19965498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
19975498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19985498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
19995498Stimh 
20006492Stimh 	if (is_ci)
20016492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
20026492Stimh 
20037184Stimh 	return (0);
20047184Stimh }
20057184Stimh 
20067184Stimh static int
20077184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
20087184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20097184Stimh {
20107184Stimh 	boolean_t fuids_ok = B_TRUE;
20117184Stimh 	uint64_t zplver = ZPL_VERSION;
20127184Stimh 	objset_t *os = NULL;
20137184Stimh 	char parentname[MAXNAMELEN];
20147184Stimh 	char *cp;
20157184Stimh 	int error;
20167184Stimh 
20177184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
20187184Stimh 	cp = strrchr(parentname, '/');
20197184Stimh 	ASSERT(cp != NULL);
20207184Stimh 	cp[0] = '\0';
20217184Stimh 
20227184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
20237184Stimh 		zplver = ZPL_VERSION_FUID - 1;
20247184Stimh 		fuids_ok = B_FALSE;
20257184Stimh 	}
20267184Stimh 
20277184Stimh 	/*
20287184Stimh 	 * Open parent object set so we can inherit zplprop values.
20297184Stimh 	 */
20307184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
20317184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
20327184Stimh 		return (error);
20337184Stimh 
20347184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
20357184Stimh 	    zplprops, is_ci);
20365498Stimh 	dmu_objset_close(os);
20377184Stimh 	return (error);
20387184Stimh }
20397184Stimh 
20407184Stimh static int
20417184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
20427184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20437184Stimh {
20447184Stimh 	boolean_t fuids_ok = B_TRUE;
20457184Stimh 	uint64_t zplver = ZPL_VERSION;
20467184Stimh 	int error;
20477184Stimh 
20487184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
20497184Stimh 		zplver = ZPL_VERSION_FUID - 1;
20507184Stimh 		fuids_ok = B_FALSE;
20517184Stimh 	}
20527184Stimh 
20537184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
20547184Stimh 	    zplprops, is_ci);
20557184Stimh 	return (error);
2056789Sahrens }
2057789Sahrens 
20585367Sahrens /*
20595367Sahrens  * inputs:
20605367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
20615367Sahrens  * zc_name		name of new objset
20625367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
20635367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
20645367Sahrens  *
20655498Stimh  * outputs: none
20665367Sahrens  */
2067789Sahrens static int
2068789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2069789Sahrens {
2070789Sahrens 	objset_t *clone;
2071789Sahrens 	int error = 0;
20725331Samw 	zfs_creat_t zct;
20734543Smarks 	nvlist_t *nvprops = NULL;
20744543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2075789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2076789Sahrens 
2077789Sahrens 	switch (type) {
2078789Sahrens 
2079789Sahrens 	case DMU_OST_ZFS:
2080789Sahrens 		cbfunc = zfs_create_cb;
2081789Sahrens 		break;
2082789Sahrens 
2083789Sahrens 	case DMU_OST_ZVOL:
2084789Sahrens 		cbfunc = zvol_create_cb;
2085789Sahrens 		break;
2086789Sahrens 
2087789Sahrens 	default:
20882199Sahrens 		cbfunc = NULL;
20896423Sgw25295 		break;
20902199Sahrens 	}
20915326Sek110237 	if (strchr(zc->zc_name, '@') ||
20925326Sek110237 	    strchr(zc->zc_name, '%'))
2093789Sahrens 		return (EINVAL);
2094789Sahrens 
20952676Seschrock 	if (zc->zc_nvlist_src != NULL &&
20965094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20975094Slling 	    &nvprops)) != 0)
20982676Seschrock 		return (error);
20992676Seschrock 
21005498Stimh 	zct.zct_zplprops = NULL;
21015331Samw 	zct.zct_props = nvprops;
21025331Samw 
21032676Seschrock 	if (zc->zc_value[0] != '\0') {
2104789Sahrens 		/*
2105789Sahrens 		 * We're creating a clone of an existing snapshot.
2106789Sahrens 		 */
21072676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
21082676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
21094543Smarks 			nvlist_free(nvprops);
2110789Sahrens 			return (EINVAL);
21112676Seschrock 		}
2112789Sahrens 
21132676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
21146689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
21152676Seschrock 		if (error) {
21164543Smarks 			nvlist_free(nvprops);
2117789Sahrens 			return (error);
21182676Seschrock 		}
21196492Stimh 
21206492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
21216492Stimh 		    NULL, NULL);
21225331Samw 		if (error) {
21235331Samw 			dmu_objset_close(clone);
21245331Samw 			nvlist_free(nvprops);
21255331Samw 			return (error);
21265331Samw 		}
2127789Sahrens 		dmu_objset_close(clone);
2128789Sahrens 	} else {
21296492Stimh 		boolean_t is_insensitive = B_FALSE;
21306492Stimh 
21312676Seschrock 		if (cbfunc == NULL) {
21324543Smarks 			nvlist_free(nvprops);
21332199Sahrens 			return (EINVAL);
21342676Seschrock 		}
21352676Seschrock 
2136789Sahrens 		if (type == DMU_OST_ZVOL) {
21372676Seschrock 			uint64_t volsize, volblocksize;
21382676Seschrock 
21394543Smarks 			if (nvprops == NULL ||
21404543Smarks 			    nvlist_lookup_uint64(nvprops,
21412676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
21422676Seschrock 			    &volsize) != 0) {
21434543Smarks 				nvlist_free(nvprops);
21442676Seschrock 				return (EINVAL);
21452676Seschrock 			}
21462676Seschrock 
21474543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
21482676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
21492676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
21504543Smarks 				nvlist_free(nvprops);
21512676Seschrock 				return (EINVAL);
21522676Seschrock 			}
21531133Seschrock 
21542676Seschrock 			if (error != 0)
21552676Seschrock 				volblocksize = zfs_prop_default_numeric(
21562676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
21572676Seschrock 
21582676Seschrock 			if ((error = zvol_check_volblocksize(
21592676Seschrock 			    volblocksize)) != 0 ||
21602676Seschrock 			    (error = zvol_check_volsize(volsize,
21612676Seschrock 			    volblocksize)) != 0) {
21624543Smarks 				nvlist_free(nvprops);
2163789Sahrens 				return (error);
21642676Seschrock 			}
21654577Sahrens 		} else if (type == DMU_OST_ZFS) {
21665331Samw 			int error;
21675331Samw 
21685498Stimh 			/*
21695331Samw 			 * We have to have normalization and
21705331Samw 			 * case-folding flags correct when we do the
21715331Samw 			 * file system creation, so go figure them out
21725498Stimh 			 * now.
21735331Samw 			 */
21745498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
21755498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21765498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
21777184Stimh 			    zct.zct_zplprops, &is_insensitive);
21785331Samw 			if (error != 0) {
21795331Samw 				nvlist_free(nvprops);
21805498Stimh 				nvlist_free(zct.zct_zplprops);
21815331Samw 				return (error);
21824577Sahrens 			}
21832676Seschrock 		}
21846492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
21856492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
21865498Stimh 		nvlist_free(zct.zct_zplprops);
2187789Sahrens 	}
21882676Seschrock 
21892676Seschrock 	/*
21902676Seschrock 	 * It would be nice to do this atomically.
21912676Seschrock 	 */
21922676Seschrock 	if (error == 0) {
21934787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
21942676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
21952676Seschrock 	}
21964543Smarks 	nvlist_free(nvprops);
2197789Sahrens 	return (error);
2198789Sahrens }
2199789Sahrens 
22007265Sahrens struct snap_prop_arg {
22017265Sahrens 	nvlist_t *nvprops;
22027265Sahrens 	const char *snapname;
22037265Sahrens };
22047265Sahrens 
22057265Sahrens static int
22067265Sahrens set_snap_props(char *name, void *arg)
22077265Sahrens {
22087265Sahrens 	struct snap_prop_arg *snpa = arg;
22097265Sahrens 	int len = strlen(name) + strlen(snpa->snapname) + 2;
22107265Sahrens 	char *buf = kmem_alloc(len, KM_SLEEP);
22117265Sahrens 	int err;
22127265Sahrens 
22137265Sahrens 	(void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
22147265Sahrens 	err = zfs_set_prop_nvlist(buf, snpa->nvprops);
22157265Sahrens 	if (err)
22167265Sahrens 		(void) dmu_objset_destroy(buf);
22177265Sahrens 	kmem_free(buf, len);
22187265Sahrens 	return (err);
22197265Sahrens }
22207265Sahrens 
22215367Sahrens /*
22225367Sahrens  * inputs:
22235367Sahrens  * zc_name	name of filesystem
22245367Sahrens  * zc_value	short name of snapshot
22255367Sahrens  * zc_cookie	recursive flag
22265367Sahrens  *
22275367Sahrens  * outputs:	none
22285367Sahrens  */
2229789Sahrens static int
22302199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
22312199Sahrens {
22327265Sahrens 	nvlist_t *nvprops = NULL;
22337265Sahrens 	int error;
22347265Sahrens 	boolean_t recursive = zc->zc_cookie;
22357265Sahrens 
22362676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
22372199Sahrens 		return (EINVAL);
22387265Sahrens 
22397265Sahrens 	if (zc->zc_nvlist_src != NULL &&
22407265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
22417265Sahrens 	    &nvprops)) != 0)
22427265Sahrens 		return (error);
22437265Sahrens 
22447265Sahrens 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
22457265Sahrens 
22467265Sahrens 	/*
22477265Sahrens 	 * It would be nice to do this atomically.
22487265Sahrens 	 */
22497265Sahrens 	if (error == 0) {
22507265Sahrens 		struct snap_prop_arg snpa;
22517265Sahrens 		snpa.nvprops = nvprops;
22527265Sahrens 		snpa.snapname = zc->zc_value;
22537265Sahrens 		if (recursive) {
22547265Sahrens 			error = dmu_objset_find(zc->zc_name,
22557265Sahrens 			    set_snap_props, &snpa, DS_FIND_CHILDREN);
22567265Sahrens 			if (error) {
22577265Sahrens 				(void) dmu_snapshots_destroy(zc->zc_name,
22587265Sahrens 				    zc->zc_value);
22597265Sahrens 			}
22607265Sahrens 		} else {
22617265Sahrens 			error = set_snap_props(zc->zc_name, &snpa);
22627265Sahrens 		}
22637265Sahrens 	}
22647265Sahrens 	nvlist_free(nvprops);
22657265Sahrens 	return (error);
22662199Sahrens }
22672199Sahrens 
22684007Smmusante int
22692199Sahrens zfs_unmount_snap(char *name, void *arg)
2270789Sahrens {
22712417Sahrens 	vfs_t *vfsp = NULL;
22722199Sahrens 
22736689Smaybee 	if (arg) {
22746689Smaybee 		char *snapname = arg;
22756689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
22766689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
22776689Smaybee 
22786689Smaybee 		(void) strcpy(buf, name);
22796689Smaybee 		(void) strcat(buf, "@");
22806689Smaybee 		(void) strcat(buf, snapname);
22816689Smaybee 		vfsp = zfs_get_vfs(buf);
22826689Smaybee 		kmem_free(buf, len);
22832417Sahrens 	} else if (strchr(name, '@')) {
22842199Sahrens 		vfsp = zfs_get_vfs(name);
22852199Sahrens 	}
22862199Sahrens 
22872199Sahrens 	if (vfsp) {
22882199Sahrens 		/*
22892199Sahrens 		 * Always force the unmount for snapshots.
22902199Sahrens 		 */
22912199Sahrens 		int flag = MS_FORCE;
2292789Sahrens 		int err;
2293789Sahrens 
22942199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
22952199Sahrens 			VFS_RELE(vfsp);
22962199Sahrens 			return (err);
22972199Sahrens 		}
22982199Sahrens 		VFS_RELE(vfsp);
22992199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
23002199Sahrens 			return (err);
23012199Sahrens 	}
23022199Sahrens 	return (0);
23032199Sahrens }
23042199Sahrens 
23055367Sahrens /*
23065367Sahrens  * inputs:
23075367Sahrens  * zc_name	name of filesystem
23085367Sahrens  * zc_value	short name of snapshot
23095367Sahrens  *
23105367Sahrens  * outputs:	none
23115367Sahrens  */
23122199Sahrens static int
23132199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
23142199Sahrens {
23152199Sahrens 	int err;
2316789Sahrens 
23172676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
23182199Sahrens 		return (EINVAL);
23192199Sahrens 	err = dmu_objset_find(zc->zc_name,
23202676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
23212199Sahrens 	if (err)
23222199Sahrens 		return (err);
23232676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
23242199Sahrens }
23252199Sahrens 
23265367Sahrens /*
23275367Sahrens  * inputs:
23285367Sahrens  * zc_name		name of dataset to destroy
23295367Sahrens  * zc_objset_type	type of objset
23305367Sahrens  *
23315367Sahrens  * outputs:		none
23325367Sahrens  */
23332199Sahrens static int
23342199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
23352199Sahrens {
23362199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
23372199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
23382199Sahrens 		if (err)
23392199Sahrens 			return (err);
2340789Sahrens 	}
2341789Sahrens 
2342789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2343789Sahrens }
2344789Sahrens 
23455367Sahrens /*
23465367Sahrens  * inputs:
23475446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
23485367Sahrens  *
23495367Sahrens  * outputs:	none
23505367Sahrens  */
2351789Sahrens static int
2352789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2353789Sahrens {
23545446Sahrens 	objset_t *os;
23555446Sahrens 	int error;
23565446Sahrens 	zfsvfs_t *zfsvfs = NULL;
23575446Sahrens 
23585446Sahrens 	/*
23595446Sahrens 	 * Get the zfsvfs for the receiving objset. There
23605446Sahrens 	 * won't be one if we're operating on a zvol, if the
23615446Sahrens 	 * objset doesn't exist yet, or is not mounted.
23625446Sahrens 	 */
23636689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
23645446Sahrens 	if (error)
23655446Sahrens 		return (error);
23665446Sahrens 
23675446Sahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
23685446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
23695446Sahrens 		zfsvfs = dmu_objset_get_user(os);
23705446Sahrens 		if (zfsvfs != NULL)
23715446Sahrens 			VFS_HOLD(zfsvfs->z_vfs);
23725446Sahrens 		mutex_exit(&os->os->os_user_ptr_lock);
23735446Sahrens 	}
23745446Sahrens 
23755446Sahrens 	if (zfsvfs != NULL) {
23768012SEric.Taylor@Sun.COM 		char *osname;
23775446Sahrens 		int mode;
23785446Sahrens 
23798012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
23806083Sek110237 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
23816083Sek110237 		if (error == 0) {
23826083Sek110237 			int resume_err;
23836083Sek110237 
23846083Sek110237 			ASSERT(strcmp(osname, zc->zc_name) == 0);
23856083Sek110237 			error = dmu_objset_rollback(os);
23866083Sek110237 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
23876083Sek110237 			error = error ? error : resume_err;
23886083Sek110237 		} else {
23896083Sek110237 			dmu_objset_close(os);
23906083Sek110237 		}
23918012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
23925446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
23935446Sahrens 	} else {
23945446Sahrens 		error = dmu_objset_rollback(os);
23955446Sahrens 	}
23966689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
23975446Sahrens 
23985446Sahrens 	return (error);
2399789Sahrens }
2400789Sahrens 
24015367Sahrens /*
24025367Sahrens  * inputs:
24035367Sahrens  * zc_name	old name of dataset
24045367Sahrens  * zc_value	new name of dataset
24055367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
24065367Sahrens  *
24075367Sahrens  * outputs:	none
24085367Sahrens  */
2409789Sahrens static int
2410789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2411789Sahrens {
24124490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
24134007Smmusante 
24142676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
24155326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
24165326Sek110237 	    strchr(zc->zc_value, '%'))
2417789Sahrens 		return (EINVAL);
2418789Sahrens 
24194007Smmusante 	/*
24204007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
24214007Smmusante 	 * in which case the dataset code figures out which snapshots
24224007Smmusante 	 * to unmount.
24234007Smmusante 	 */
24244007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2425789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
24262199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
24272199Sahrens 		if (err)
24282199Sahrens 			return (err);
2429789Sahrens 	}
24304007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2431789Sahrens }
2432789Sahrens 
24336689Smaybee static void
24348536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
24356689Smaybee {
24366689Smaybee 	zfs_cmd_t *zc;
24376689Smaybee 	nvpair_t *prop;
24386689Smaybee 
24396689Smaybee 	if (props == NULL)
24406689Smaybee 		return;
24416689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
24426689Smaybee 	(void) strcpy(zc->zc_name, dataset);
24436689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
24446689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
24458536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
24468536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
24478536SDavid.Pacheco@Sun.COM 			continue;
24486689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
24496689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
24506689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
24516689Smaybee 	}
24526689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
24536689Smaybee }
24546689Smaybee 
24555367Sahrens /*
24565367Sahrens  * inputs:
24575367Sahrens  * zc_name		name of containing filesystem
24585367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
24595367Sahrens  * zc_value		name of snapshot to create
24605367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
24615367Sahrens  * zc_cookie		file descriptor to recv from
24625367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
24635367Sahrens  * zc_guid		force flag
24645367Sahrens  *
24655367Sahrens  * outputs:
24665367Sahrens  * zc_cookie		number of bytes read
24675367Sahrens  */
2468789Sahrens static int
24695367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2470789Sahrens {
2471789Sahrens 	file_t *fp;
24725326Sek110237 	objset_t *os;
24735367Sahrens 	dmu_recv_cookie_t drc;
24745326Sek110237 	zfsvfs_t *zfsvfs = NULL;
24755326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2476789Sahrens 	int error, fd;
24775367Sahrens 	offset_t off;
24785367Sahrens 	nvlist_t *props = NULL;
24796689Smaybee 	nvlist_t *origprops = NULL;
24805367Sahrens 	objset_t *origin = NULL;
24815367Sahrens 	char *tosnap;
24825367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2483789Sahrens 
24843265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
24855326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
24865326Sek110237 	    strchr(zc->zc_value, '%'))
24873265Sahrens 		return (EINVAL);
24883265Sahrens 
24895367Sahrens 	(void) strcpy(tofs, zc->zc_value);
24905367Sahrens 	tosnap = strchr(tofs, '@');
24915367Sahrens 	*tosnap = '\0';
24925367Sahrens 	tosnap++;
24935367Sahrens 
24945367Sahrens 	if (zc->zc_nvlist_src != NULL &&
24955367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24965367Sahrens 	    &props)) != 0)
24975367Sahrens 		return (error);
24985367Sahrens 
2499789Sahrens 	fd = zc->zc_cookie;
2500789Sahrens 	fp = getf(fd);
25015367Sahrens 	if (fp == NULL) {
25025367Sahrens 		nvlist_free(props);
2503789Sahrens 		return (EBADF);
25045367Sahrens 	}
25055326Sek110237 
25066689Smaybee 	if (dmu_objset_open(tofs, DMU_OST_ANY,
25076689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
25086689Smaybee 		/*
25096689Smaybee 		 * Try to get the zfsvfs for the receiving objset.
25106689Smaybee 		 * There won't be one if we're operating on a zvol,
25116689Smaybee 		 * if the objset doesn't exist yet, or is not mounted.
25126689Smaybee 		 */
25135446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
25146689Smaybee 		if (zfsvfs = dmu_objset_get_user(os)) {
25156083Sek110237 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
25166689Smaybee 				mutex_exit(&os->os->os_user_ptr_lock);
25176083Sek110237 				dmu_objset_close(os);
25186689Smaybee 				zfsvfs = NULL;
25196689Smaybee 				error = EBUSY;
25206689Smaybee 				goto out;
25216083Sek110237 			}
25226689Smaybee 			VFS_HOLD(zfsvfs->z_vfs);
25236083Sek110237 		}
25246689Smaybee 		mutex_exit(&os->os->os_user_ptr_lock);
25256689Smaybee 
25266689Smaybee 		/*
25276689Smaybee 		 * If new properties are supplied, they are to completely
25286689Smaybee 		 * replace the existing ones, so stash away the existing ones.
25296689Smaybee 		 */
25306689Smaybee 		if (props)
25316689Smaybee 			(void) dsl_prop_get_all(os, &origprops, TRUE);
25326689Smaybee 
25335326Sek110237 		dmu_objset_close(os);
25345326Sek110237 	}
25355326Sek110237 
25365367Sahrens 	if (zc->zc_string[0]) {
25375367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
25386689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
25396689Smaybee 		if (error)
25406689Smaybee 			goto out;
25415367Sahrens 	}
25425367Sahrens 
25435367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
25445367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
25455367Sahrens 	if (origin)
25465367Sahrens 		dmu_objset_close(origin);
25476689Smaybee 	if (error)
25486689Smaybee 		goto out;
25495326Sek110237 
25505326Sek110237 	/*
25516689Smaybee 	 * Reset properties.  We do this before we receive the stream
25526689Smaybee 	 * so that the properties are applied to the new data.
25535326Sek110237 	 */
25545367Sahrens 	if (props) {
25558536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
25566689Smaybee 		/*
25576689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
25586689Smaybee 		 */
25596689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
25605367Sahrens 	}
25615367Sahrens 
25625367Sahrens 	off = fp->f_offset;
25635367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
25645367Sahrens 
25656689Smaybee 	if (error == 0 && zfsvfs) {
25668012SEric.Taylor@Sun.COM 		char *osname;
25676689Smaybee 		int mode;
25686689Smaybee 
25696689Smaybee 		/* online recv */
25708012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25716689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
25726689Smaybee 		if (error == 0) {
25736689Smaybee 			int resume_err;
25746689Smaybee 
25756689Smaybee 			error = dmu_recv_end(&drc);
25766689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
25776689Smaybee 			error = error ? error : resume_err;
25785367Sahrens 		} else {
25796689Smaybee 			dmu_recv_abort_cleanup(&drc);
25805367Sahrens 		}
25818012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
25826689Smaybee 	} else if (error == 0) {
25836689Smaybee 		error = dmu_recv_end(&drc);
25846083Sek110237 	}
25855367Sahrens 
25865367Sahrens 	zc->zc_cookie = off - fp->f_offset;
25875367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
25885367Sahrens 		fp->f_offset = off;
25892885Sahrens 
25906689Smaybee 	/*
25916689Smaybee 	 * On error, restore the original props.
25926689Smaybee 	 */
25936689Smaybee 	if (error && props) {
25948536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
25956689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
25966689Smaybee 	}
25976689Smaybee out:
25986689Smaybee 	if (zfsvfs) {
25996689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
26006689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
26016689Smaybee 	}
26026689Smaybee 	nvlist_free(props);
26036689Smaybee 	nvlist_free(origprops);
2604789Sahrens 	releasef(fd);
2605789Sahrens 	return (error);
2606789Sahrens }
2607789Sahrens 
26085367Sahrens /*
26095367Sahrens  * inputs:
26105367Sahrens  * zc_name	name of snapshot to send
26115367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
26125367Sahrens  * zc_cookie	file descriptor to send stream to
26135367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
26145367Sahrens  *
26155367Sahrens  * outputs: none
26165367Sahrens  */
2617789Sahrens static int
26185367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2619789Sahrens {
2620789Sahrens 	objset_t *fromsnap = NULL;
2621789Sahrens 	objset_t *tosnap;
2622789Sahrens 	file_t *fp;
2623789Sahrens 	int error;
26245367Sahrens 	offset_t off;
2625789Sahrens 
2626789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
26276689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2628789Sahrens 	if (error)
2629789Sahrens 		return (error);
2630789Sahrens 
26312676Seschrock 	if (zc->zc_value[0] != '\0') {
26328012SEric.Taylor@Sun.COM 		char *buf;
26332885Sahrens 		char *cp;
26342885Sahrens 
26358012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26368012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
26372885Sahrens 		cp = strchr(buf, '@');
26382885Sahrens 		if (cp)
26392885Sahrens 			*(cp+1) = 0;
26408012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
26412885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
26426689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
26438012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2644789Sahrens 		if (error) {
2645789Sahrens 			dmu_objset_close(tosnap);
2646789Sahrens 			return (error);
2647789Sahrens 		}
2648789Sahrens 	}
2649789Sahrens 
2650789Sahrens 	fp = getf(zc->zc_cookie);
2651789Sahrens 	if (fp == NULL) {
2652789Sahrens 		dmu_objset_close(tosnap);
2653789Sahrens 		if (fromsnap)
2654789Sahrens 			dmu_objset_close(fromsnap);
2655789Sahrens 		return (EBADF);
2656789Sahrens 	}
2657789Sahrens 
26585367Sahrens 	off = fp->f_offset;
26595367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
26605367Sahrens 
26615367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
26625367Sahrens 		fp->f_offset = off;
2663789Sahrens 	releasef(zc->zc_cookie);
2664789Sahrens 	if (fromsnap)
2665789Sahrens 		dmu_objset_close(fromsnap);
2666789Sahrens 	dmu_objset_close(tosnap);
2667789Sahrens 	return (error);
2668789Sahrens }
2669789Sahrens 
26701544Seschrock static int
26711544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
26721544Seschrock {
26731544Seschrock 	int id, error;
26741544Seschrock 
26751544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
26761544Seschrock 	    &zc->zc_inject_record);
26771544Seschrock 
26781544Seschrock 	if (error == 0)
26791544Seschrock 		zc->zc_guid = (uint64_t)id;
26801544Seschrock 
26811544Seschrock 	return (error);
26821544Seschrock }
26831544Seschrock 
26841544Seschrock static int
26851544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
26861544Seschrock {
26871544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
26881544Seschrock }
26891544Seschrock 
26901544Seschrock static int
26911544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
26921544Seschrock {
26931544Seschrock 	int id = (int)zc->zc_guid;
26941544Seschrock 	int error;
26951544Seschrock 
26961544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
26971544Seschrock 	    &zc->zc_inject_record);
26981544Seschrock 
26991544Seschrock 	zc->zc_guid = id;
27001544Seschrock 
27011544Seschrock 	return (error);
27021544Seschrock }
27031544Seschrock 
27041544Seschrock static int
27051544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
27061544Seschrock {
27071544Seschrock 	spa_t *spa;
27081544Seschrock 	int error;
27092676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
27101544Seschrock 
27111544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27121544Seschrock 		return (error);
27131544Seschrock 
27142676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
27151544Seschrock 	    &count);
27161544Seschrock 	if (error == 0)
27172676Seschrock 		zc->zc_nvlist_dst_size = count;
27181544Seschrock 	else
27192676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
27201544Seschrock 
27211544Seschrock 	spa_close(spa, FTAG);
27221544Seschrock 
27231544Seschrock 	return (error);
27241544Seschrock }
27251544Seschrock 
27261544Seschrock static int
27271544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
27281544Seschrock {
27291544Seschrock 	spa_t *spa;
27301544Seschrock 	vdev_t *vd;
27311544Seschrock 	int error;
27321544Seschrock 
27337294Sperrin 	/*
27347294Sperrin 	 * On zpool clear we also fix up missing slogs
27357294Sperrin 	 */
27367294Sperrin 	mutex_enter(&spa_namespace_lock);
27377294Sperrin 	spa = spa_lookup(zc->zc_name);
27387294Sperrin 	if (spa == NULL) {
27397294Sperrin 		mutex_exit(&spa_namespace_lock);
27407294Sperrin 		return (EIO);
27417294Sperrin 	}
27427294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
27437294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
27447294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
27457294Sperrin 	}
27467294Sperrin 	mutex_exit(&spa_namespace_lock);
27477294Sperrin 
27481544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27491544Seschrock 		return (error);
27501544Seschrock 
27517754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
27521544Seschrock 
27532676Seschrock 	if (zc->zc_guid == 0) {
27541544Seschrock 		vd = NULL;
27556643Seschrock 	} else {
27566643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
27575450Sbrendan 		if (vd == NULL) {
27587754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
27595450Sbrendan 			spa_close(spa, FTAG);
27605450Sbrendan 			return (ENODEV);
27615450Sbrendan 		}
27621544Seschrock 	}
27631544Seschrock 
27647754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
27657754SJeff.Bonwick@Sun.COM 
27667754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
27677754SJeff.Bonwick@Sun.COM 
27687754SJeff.Bonwick@Sun.COM 	/*
27697754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
27707754SJeff.Bonwick@Sun.COM 	 */
27717754SJeff.Bonwick@Sun.COM 	zio_resume(spa);
27721544Seschrock 
27731544Seschrock 	spa_close(spa, FTAG);
27741544Seschrock 
27751544Seschrock 	return (0);
27761544Seschrock }
27771544Seschrock 
27785367Sahrens /*
27795367Sahrens  * inputs:
27805367Sahrens  * zc_name	name of filesystem
27815367Sahrens  * zc_value	name of origin snapshot
27825367Sahrens  *
27835367Sahrens  * outputs:	none
27845367Sahrens  */
27851544Seschrock static int
27862082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
27872082Seschrock {
27882417Sahrens 	char *cp;
27892417Sahrens 
27902417Sahrens 	/*
27912417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
27922417Sahrens 	 * it's easier.
27932417Sahrens 	 */
27942676Seschrock 	cp = strchr(zc->zc_value, '@');
27952417Sahrens 	if (cp)
27962417Sahrens 		*cp = '\0';
27972676Seschrock 	(void) dmu_objset_find(zc->zc_value,
27982417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
27992082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
28002082Seschrock }
28012082Seschrock 
28024543Smarks /*
28034543Smarks  * We don't want to have a hard dependency
28044543Smarks  * against some special symbols in sharefs
28055331Samw  * nfs, and smbsrv.  Determine them if needed when
28064543Smarks  * the first file system is shared.
28075331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
28084543Smarks  */
28095331Samw int (*znfsexport_fs)(void *arg);
28104543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
28115331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
28125331Samw 
28135331Samw int zfs_nfsshare_inited;
28145331Samw int zfs_smbshare_inited;
28155331Samw 
28164543Smarks ddi_modhandle_t nfs_mod;
28174543Smarks ddi_modhandle_t sharefs_mod;
28185331Samw ddi_modhandle_t smbsrv_mod;
28194543Smarks kmutex_t zfs_share_lock;
28204543Smarks 
28214543Smarks static int
28225331Samw zfs_init_sharefs()
28235331Samw {
28245331Samw 	int error;
28255331Samw 
28265331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
28275331Samw 	/* Both NFS and SMB shares also require sharetab support. */
28285331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
28295331Samw 	    ddi_modopen("fs/sharefs",
28305331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
28315331Samw 		return (ENOSYS);
28325331Samw 	}
28335331Samw 	if (zshare_fs == NULL && ((zshare_fs =
28345331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
28355331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
28365331Samw 		return (ENOSYS);
28375331Samw 	}
28385331Samw 	return (0);
28395331Samw }
28405331Samw 
28415331Samw static int
28424543Smarks zfs_ioc_share(zfs_cmd_t *zc)
28434543Smarks {
28444543Smarks 	int error;
28454543Smarks 	int opcode;
28464543Smarks 
28475331Samw 	switch (zc->zc_share.z_sharetype) {
28485331Samw 	case ZFS_SHARE_NFS:
28495331Samw 	case ZFS_UNSHARE_NFS:
28505331Samw 		if (zfs_nfsshare_inited == 0) {
28515331Samw 			mutex_enter(&zfs_share_lock);
28525331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
28535331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
28545331Samw 				mutex_exit(&zfs_share_lock);
28555331Samw 				return (ENOSYS);
28565331Samw 			}
28575331Samw 			if (znfsexport_fs == NULL &&
28585331Samw 			    ((znfsexport_fs = (int (*)(void *))
28595331Samw 			    ddi_modsym(nfs_mod,
28605331Samw 			    "nfs_export", &error)) == NULL)) {
28615331Samw 				mutex_exit(&zfs_share_lock);
28625331Samw 				return (ENOSYS);
28635331Samw 			}
28645331Samw 			error = zfs_init_sharefs();
28655331Samw 			if (error) {
28665331Samw 				mutex_exit(&zfs_share_lock);
28675331Samw 				return (ENOSYS);
28685331Samw 			}
28695331Samw 			zfs_nfsshare_inited = 1;
28704543Smarks 			mutex_exit(&zfs_share_lock);
28714543Smarks 		}
28725331Samw 		break;
28735331Samw 	case ZFS_SHARE_SMB:
28745331Samw 	case ZFS_UNSHARE_SMB:
28755331Samw 		if (zfs_smbshare_inited == 0) {
28765331Samw 			mutex_enter(&zfs_share_lock);
28775331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
28785331Samw 			    ddi_modopen("drv/smbsrv",
28795331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
28805331Samw 				mutex_exit(&zfs_share_lock);
28815331Samw 				return (ENOSYS);
28825331Samw 			}
28835331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
28845331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
28856139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
28865331Samw 				mutex_exit(&zfs_share_lock);
28875331Samw 				return (ENOSYS);
28885331Samw 			}
28895331Samw 			error = zfs_init_sharefs();
28905331Samw 			if (error) {
28915331Samw 				mutex_exit(&zfs_share_lock);
28925331Samw 				return (ENOSYS);
28935331Samw 			}
28945331Samw 			zfs_smbshare_inited = 1;
28954543Smarks 			mutex_exit(&zfs_share_lock);
28964543Smarks 		}
28975331Samw 		break;
28985331Samw 	default:
28995331Samw 		return (EINVAL);
29004543Smarks 	}
29014543Smarks 
29025331Samw 	switch (zc->zc_share.z_sharetype) {
29035331Samw 	case ZFS_SHARE_NFS:
29045331Samw 	case ZFS_UNSHARE_NFS:
29055331Samw 		if (error =
29065331Samw 		    znfsexport_fs((void *)
29075331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
29085331Samw 			return (error);
29095331Samw 		break;
29105331Samw 	case ZFS_SHARE_SMB:
29115331Samw 	case ZFS_UNSHARE_SMB:
29125331Samw 		if (error = zsmbexport_fs((void *)
29135331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
29145331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
29155331Samw 		    B_TRUE : B_FALSE)) {
29165331Samw 			return (error);
29175331Samw 		}
29185331Samw 		break;
29195331Samw 	}
29205331Samw 
29215331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
29225331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
29234543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
29244543Smarks 
29255331Samw 	/*
29265331Samw 	 * Add or remove share from sharetab
29275331Samw 	 */
29284543Smarks 	error = zshare_fs(opcode,
29294543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
29304543Smarks 	    zc->zc_share.z_sharemax);
29314543Smarks 
29324543Smarks 	return (error);
29334543Smarks 
29344543Smarks }
29354543Smarks 
29364543Smarks /*
29374988Sek110237  * pool create, destroy, and export don't log the history as part of
29384988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
29394988Sek110237  * do the logging of those commands.
29404543Smarks  */
2941789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
29424715Sek110237 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
29434577Sahrens 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
29444577Sahrens 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29454577Sahrens 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
29464577Sahrens 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
29474577Sahrens 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
29484577Sahrens 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
29494577Sahrens 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29504577Sahrens 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
29514577Sahrens 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
29524577Sahrens 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
29534577Sahrens 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29544577Sahrens 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29554577Sahrens 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
29564577Sahrens 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29574577Sahrens 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29584577Sahrens 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
29594577Sahrens 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29605498Stimh 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29614543Smarks 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
29624577Sahrens 	    DATASET_NAME, B_FALSE },
29634543Smarks 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
29644577Sahrens 	    DATASET_NAME, B_FALSE },
29654577Sahrens 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
29664577Sahrens 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
29674577Sahrens 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
29684577Sahrens 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
29694577Sahrens 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
29704577Sahrens 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
29714577Sahrens 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
29725367Sahrens 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
29735367Sahrens 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
29744577Sahrens 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
29754577Sahrens 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
29764577Sahrens 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
29774577Sahrens 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
29784577Sahrens 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29794577Sahrens 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
29804577Sahrens 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
29814577Sahrens 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
29824577Sahrens 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
29834577Sahrens 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
29844577Sahrens 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
29854577Sahrens 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
29864577Sahrens 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
29874577Sahrens 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29884543Smarks 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
29894577Sahrens 	    DATASET_NAME, B_FALSE },
29904849Sahrens 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
29914849Sahrens 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2992789Sahrens };
2993789Sahrens 
2994789Sahrens static int
2995789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2996789Sahrens {
2997789Sahrens 	zfs_cmd_t *zc;
2998789Sahrens 	uint_t vec;
29992199Sahrens 	int error, rc;
3000789Sahrens 
3001789Sahrens 	if (getminor(dev) != 0)
3002789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3003789Sahrens 
3004789Sahrens 	vec = cmd - ZFS_IOC;
30054787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3006789Sahrens 
3007789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3008789Sahrens 		return (EINVAL);
3009789Sahrens 
3010789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3011789Sahrens 
3012789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3013789Sahrens 
30144787Sahrens 	if (error == 0)
30154543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3016789Sahrens 
3017789Sahrens 	/*
3018789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3019789Sahrens 	 * the lower layers.
3020789Sahrens 	 */
3021789Sahrens 	if (error == 0) {
3022789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3023789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
30244577Sahrens 		case POOL_NAME:
3025789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3026789Sahrens 				error = EINVAL;
3027789Sahrens 			break;
3028789Sahrens 
30294577Sahrens 		case DATASET_NAME:
3030789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3031789Sahrens 				error = EINVAL;
3032789Sahrens 			break;
30332856Snd150628 
30344577Sahrens 		case NO_NAME:
30352856Snd150628 			break;
3036789Sahrens 		}
3037789Sahrens 	}
3038789Sahrens 
3039789Sahrens 	if (error == 0)
3040789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3041789Sahrens 
30422199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
30434543Smarks 	if (error == 0) {
30442199Sahrens 		error = rc;
30454543Smarks 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
30464543Smarks 			zfs_log_history(zc);
30474543Smarks 	}
3048789Sahrens 
3049789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3050789Sahrens 	return (error);
3051789Sahrens }
3052789Sahrens 
3053789Sahrens static int
3054789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3055789Sahrens {
3056789Sahrens 	if (cmd != DDI_ATTACH)
3057789Sahrens 		return (DDI_FAILURE);
3058789Sahrens 
3059789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3060789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3061789Sahrens 		return (DDI_FAILURE);
3062789Sahrens 
3063789Sahrens 	zfs_dip = dip;
3064789Sahrens 
3065789Sahrens 	ddi_report_dev(dip);
3066789Sahrens 
3067789Sahrens 	return (DDI_SUCCESS);
3068789Sahrens }
3069789Sahrens 
3070789Sahrens static int
3071789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3072789Sahrens {
3073789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3074789Sahrens 		return (DDI_FAILURE);
3075789Sahrens 
3076789Sahrens 	if (cmd != DDI_DETACH)
3077789Sahrens 		return (DDI_FAILURE);
3078789Sahrens 
3079789Sahrens 	zfs_dip = NULL;
3080789Sahrens 
3081789Sahrens 	ddi_prop_remove_all(dip);
3082789Sahrens 	ddi_remove_minor_node(dip, NULL);
3083789Sahrens 
3084789Sahrens 	return (DDI_SUCCESS);
3085789Sahrens }
3086789Sahrens 
3087789Sahrens /*ARGSUSED*/
3088789Sahrens static int
3089789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3090789Sahrens {
3091789Sahrens 	switch (infocmd) {
3092789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3093789Sahrens 		*result = zfs_dip;
3094789Sahrens 		return (DDI_SUCCESS);
3095789Sahrens 
3096789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3097849Sbonwick 		*result = (void *)0;
3098789Sahrens 		return (DDI_SUCCESS);
3099789Sahrens 	}
3100789Sahrens 
3101789Sahrens 	return (DDI_FAILURE);
3102789Sahrens }
3103789Sahrens 
3104789Sahrens /*
3105789Sahrens  * OK, so this is a little weird.
3106789Sahrens  *
3107789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3108789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3109789Sahrens  *
3110789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3111789Sahrens  * so most of the standard driver entry points are in zvol.c.
3112789Sahrens  */
3113789Sahrens static struct cb_ops zfs_cb_ops = {
3114789Sahrens 	zvol_open,	/* open */
3115789Sahrens 	zvol_close,	/* close */
3116789Sahrens 	zvol_strategy,	/* strategy */
3117789Sahrens 	nodev,		/* print */
31186423Sgw25295 	zvol_dump,	/* dump */
3119789Sahrens 	zvol_read,	/* read */
3120789Sahrens 	zvol_write,	/* write */
3121789Sahrens 	zfsdev_ioctl,	/* ioctl */
3122789Sahrens 	nodev,		/* devmap */
3123789Sahrens 	nodev,		/* mmap */
3124789Sahrens 	nodev,		/* segmap */
3125789Sahrens 	nochpoll,	/* poll */
3126789Sahrens 	ddi_prop_op,	/* prop_op */
3127789Sahrens 	NULL,		/* streamtab */
3128789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3129789Sahrens 	CB_REV,		/* version */
31303638Sbillm 	nodev,		/* async read */
31313638Sbillm 	nodev,		/* async write */
3132789Sahrens };
3133789Sahrens 
3134789Sahrens static struct dev_ops zfs_dev_ops = {
3135789Sahrens 	DEVO_REV,	/* version */
3136789Sahrens 	0,		/* refcnt */
3137789Sahrens 	zfs_info,	/* info */
3138789Sahrens 	nulldev,	/* identify */
3139789Sahrens 	nulldev,	/* probe */
3140789Sahrens 	zfs_attach,	/* attach */
3141789Sahrens 	zfs_detach,	/* detach */
3142789Sahrens 	nodev,		/* reset */
3143789Sahrens 	&zfs_cb_ops,	/* driver operations */
31447656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
31457656SSherry.Moore@Sun.COM 	NULL,		/* power */
31467656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3147789Sahrens };
3148789Sahrens 
3149789Sahrens static struct modldrv zfs_modldrv = {
31507656SSherry.Moore@Sun.COM 	&mod_driverops,
31517656SSherry.Moore@Sun.COM 	"ZFS storage pool",
31527656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3153789Sahrens };
3154789Sahrens 
3155789Sahrens static struct modlinkage modlinkage = {
3156789Sahrens 	MODREV_1,
3157789Sahrens 	(void *)&zfs_modlfs,
3158789Sahrens 	(void *)&zfs_modldrv,
3159789Sahrens 	NULL
3160789Sahrens };
3161789Sahrens 
31624720Sfr157268 
31634720Sfr157268 uint_t zfs_fsyncer_key;
31645326Sek110237 extern uint_t rrw_tsd_key;
31654720Sfr157268 
3166789Sahrens int
3167789Sahrens _init(void)
3168789Sahrens {
3169789Sahrens 	int error;
3170789Sahrens 
3171849Sbonwick 	spa_init(FREAD | FWRITE);
3172849Sbonwick 	zfs_init();
3173849Sbonwick 	zvol_init();
3174849Sbonwick 
3175849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3176849Sbonwick 		zvol_fini();
3177849Sbonwick 		zfs_fini();
3178849Sbonwick 		spa_fini();
3179789Sahrens 		return (error);
3180849Sbonwick 	}
3181789Sahrens 
31824720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
31835326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
31844720Sfr157268 
3185789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3186789Sahrens 	ASSERT(error == 0);
31874543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3188789Sahrens 
3189789Sahrens 	return (0);
3190789Sahrens }
3191789Sahrens 
3192789Sahrens int
3193789Sahrens _fini(void)
3194789Sahrens {
3195789Sahrens 	int error;
3196789Sahrens 
31971544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3198789Sahrens 		return (EBUSY);
3199789Sahrens 
3200789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3201789Sahrens 		return (error);
3202789Sahrens 
3203789Sahrens 	zvol_fini();
3204789Sahrens 	zfs_fini();
3205789Sahrens 	spa_fini();
32065331Samw 	if (zfs_nfsshare_inited)
32074543Smarks 		(void) ddi_modclose(nfs_mod);
32085331Samw 	if (zfs_smbshare_inited)
32095331Samw 		(void) ddi_modclose(smbsrv_mod);
32105331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
32114543Smarks 		(void) ddi_modclose(sharefs_mod);
3212789Sahrens 
32134720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3214789Sahrens 	ldi_ident_release(zfs_li);
3215789Sahrens 	zfs_li = NULL;
32164543Smarks 	mutex_destroy(&zfs_share_lock);
3217789Sahrens 
3218789Sahrens 	return (error);
3219789Sahrens }
3220789Sahrens 
3221789Sahrens int
3222789Sahrens _info(struct modinfo *modinfop)
3223789Sahrens {
3224789Sahrens 	return (mod_info(&modlinkage, modinfop));
3225789Sahrens }
3226