xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 7656:2621e50fdf4a)
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 /*
225977Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens 
27789Sahrens #include <sys/types.h>
28789Sahrens #include <sys/param.h>
29789Sahrens #include <sys/errno.h>
30789Sahrens #include <sys/uio.h>
31789Sahrens #include <sys/buf.h>
32789Sahrens #include <sys/modctl.h>
33789Sahrens #include <sys/open.h>
34789Sahrens #include <sys/file.h>
35789Sahrens #include <sys/kmem.h>
36789Sahrens #include <sys/conf.h>
37789Sahrens #include <sys/cmn_err.h>
38789Sahrens #include <sys/stat.h>
39789Sahrens #include <sys/zfs_ioctl.h>
405331Samw #include <sys/zfs_znode.h>
41789Sahrens #include <sys/zap.h>
42789Sahrens #include <sys/spa.h>
433912Slling #include <sys/spa_impl.h>
44789Sahrens #include <sys/vdev.h>
453912Slling #include <sys/vdev_impl.h>
46789Sahrens #include <sys/dmu.h>
47789Sahrens #include <sys/dsl_dir.h>
48789Sahrens #include <sys/dsl_dataset.h>
49789Sahrens #include <sys/dsl_prop.h>
504543Smarks #include <sys/dsl_deleg.h>
514543Smarks #include <sys/dmu_objset.h>
52789Sahrens #include <sys/ddi.h>
53789Sahrens #include <sys/sunddi.h>
54789Sahrens #include <sys/sunldi.h>
55789Sahrens #include <sys/policy.h>
56789Sahrens #include <sys/zone.h>
57789Sahrens #include <sys/nvpair.h>
58789Sahrens #include <sys/pathname.h>
59789Sahrens #include <sys/mount.h>
60789Sahrens #include <sys/sdt.h>
61789Sahrens #include <sys/fs/zfs.h>
62789Sahrens #include <sys/zfs_ctldir.h>
635331Samw #include <sys/zfs_dir.h>
642885Sahrens #include <sys/zvol.h>
654543Smarks #include <sharefs/share.h>
665326Sek110237 #include <sys/dmu_objset.h>
67789Sahrens 
68789Sahrens #include "zfs_namecheck.h"
692676Seschrock #include "zfs_prop.h"
704543Smarks #include "zfs_deleg.h"
71789Sahrens 
72789Sahrens extern struct modlfs zfs_modlfs;
73789Sahrens 
74789Sahrens extern void zfs_init(void);
75789Sahrens extern void zfs_fini(void);
76789Sahrens 
77789Sahrens ldi_ident_t zfs_li = NULL;
78789Sahrens dev_info_t *zfs_dip;
79789Sahrens 
80789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
814543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
82789Sahrens 
83789Sahrens typedef struct zfs_ioc_vec {
84789Sahrens 	zfs_ioc_func_t		*zvec_func;
85789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
86789Sahrens 	enum {
874577Sahrens 		NO_NAME,
884577Sahrens 		POOL_NAME,
894577Sahrens 		DATASET_NAME
904543Smarks 	} zvec_namecheck;
914543Smarks 	boolean_t		zvec_his_log;
92789Sahrens } zfs_ioc_vec_t;
93789Sahrens 
947265Sahrens static void clear_props(char *dataset, nvlist_t *props);
957184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
967184Stimh     boolean_t *);
977184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
987184Stimh 
99789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
100789Sahrens void
101789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
102789Sahrens {
103789Sahrens 	const char *newfile;
104789Sahrens 	char buf[256];
105789Sahrens 	va_list adx;
106789Sahrens 
107789Sahrens 	/*
108789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
109789Sahrens 	 */
110789Sahrens 	newfile = strrchr(file, '/');
111789Sahrens 	if (newfile != NULL) {
112789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
113789Sahrens 	} else {
114789Sahrens 		newfile = file;
115789Sahrens 	}
116789Sahrens 
117789Sahrens 	va_start(adx, fmt);
118789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
119789Sahrens 	va_end(adx);
120789Sahrens 
121789Sahrens 	/*
122789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
123789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
124789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
125789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
126789Sahrens 	 * arg0 = file name
127789Sahrens 	 * arg1 = function name
128789Sahrens 	 * arg2 = line number
129789Sahrens 	 * arg3 = message
130789Sahrens 	 */
131789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
132789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
133789Sahrens }
134789Sahrens 
1354543Smarks static void
1364715Sek110237 history_str_free(char *buf)
1374715Sek110237 {
1384715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1394715Sek110237 }
1404715Sek110237 
1414715Sek110237 static char *
1424715Sek110237 history_str_get(zfs_cmd_t *zc)
1434715Sek110237 {
1444715Sek110237 	char *buf;
1454715Sek110237 
1464715Sek110237 	if (zc->zc_history == NULL)
1474715Sek110237 		return (NULL);
1484715Sek110237 
1494715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1504715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1514715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1524715Sek110237 		history_str_free(buf);
1534715Sek110237 		return (NULL);
1544715Sek110237 	}
1554715Sek110237 
1564715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1574715Sek110237 
1584715Sek110237 	return (buf);
1594715Sek110237 }
1604715Sek110237 
1615375Stimh /*
1627042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1637042Sgw25295  */
1647042Sgw25295 static boolean_t
1657042Sgw25295 zfs_is_bootfs(const char *name)
1667042Sgw25295 {
1677042Sgw25295 	spa_t *spa;
1687042Sgw25295 	boolean_t ret = B_FALSE;
1697042Sgw25295 
1707042Sgw25295 	if (spa_open(name, &spa, FTAG) == 0) {
1717042Sgw25295 		if (spa->spa_bootfs) {
1727042Sgw25295 			objset_t *os;
1737042Sgw25295 
1747042Sgw25295 			if (dmu_objset_open(name, DMU_OST_ZFS,
1757042Sgw25295 			    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1767042Sgw25295 				ret = (dmu_objset_id(os) == spa->spa_bootfs);
1777042Sgw25295 				dmu_objset_close(os);
1787042Sgw25295 			}
1797042Sgw25295 		}
1807042Sgw25295 		spa_close(spa, FTAG);
1817042Sgw25295 	}
1827042Sgw25295 	return (ret);
1837042Sgw25295 }
1847042Sgw25295 
1857042Sgw25295 /*
1867184Stimh  * zfs_earlier_version
1875375Stimh  *
1885375Stimh  *	Return non-zero if the spa version is less than requested version.
1895375Stimh  */
1905331Samw static int
1917184Stimh zfs_earlier_version(const char *name, int version)
1925331Samw {
1935331Samw 	spa_t *spa;
1945331Samw 
1955331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
1965331Samw 		if (spa_version(spa) < version) {
1975331Samw 			spa_close(spa, FTAG);
1985331Samw 			return (1);
1995331Samw 		}
2005331Samw 		spa_close(spa, FTAG);
2015331Samw 	}
2025331Samw 	return (0);
2035331Samw }
2045331Samw 
2055977Smarks /*
2066689Smaybee  * zpl_earlier_version
2075977Smarks  *
2086689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2095977Smarks  */
2106689Smaybee static boolean_t
2116689Smaybee zpl_earlier_version(const char *name, int version)
2125977Smarks {
2135977Smarks 	objset_t *os;
2146689Smaybee 	boolean_t rc = B_TRUE;
2155977Smarks 
2165977Smarks 	if (dmu_objset_open(name, DMU_OST_ANY,
2176689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2186689Smaybee 		uint64_t zplversion;
2196689Smaybee 
2206689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2216689Smaybee 			rc = zplversion < version;
2225977Smarks 		dmu_objset_close(os);
2235977Smarks 	}
2245977Smarks 	return (rc);
2255977Smarks }
2265977Smarks 
2274715Sek110237 static void
2284543Smarks zfs_log_history(zfs_cmd_t *zc)
2294543Smarks {
2304543Smarks 	spa_t *spa;
2314603Sahrens 	char *buf;
2324543Smarks 
2334715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2344577Sahrens 		return;
2354577Sahrens 
2364715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2374715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2384715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2394715Sek110237 		spa_close(spa, FTAG);
2404543Smarks 	}
2414715Sek110237 	history_str_free(buf);
2424543Smarks }
2434543Smarks 
244789Sahrens /*
245789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
246789Sahrens  * and can be used in the local zone, as there is no associated dataset.
247789Sahrens  */
248789Sahrens /* ARGSUSED */
249789Sahrens static int
2504543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
251789Sahrens {
252789Sahrens 	return (0);
253789Sahrens }
254789Sahrens 
255789Sahrens /*
256789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
257789Sahrens  * no privileges, but must be visible in the local zone.
258789Sahrens  */
259789Sahrens /* ARGSUSED */
260789Sahrens static int
2614543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
262789Sahrens {
263789Sahrens 	if (INGLOBALZONE(curproc) ||
2644543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
265789Sahrens 		return (0);
266789Sahrens 
267789Sahrens 	return (ENOENT);
268789Sahrens }
269789Sahrens 
270789Sahrens static int
271789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
272789Sahrens {
273789Sahrens 	uint64_t zoned;
274789Sahrens 	int writable = 1;
275789Sahrens 
276789Sahrens 	/*
277789Sahrens 	 * The dataset must be visible by this zone -- check this first
278789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
279789Sahrens 	 */
280789Sahrens 	if (!INGLOBALZONE(curproc) &&
281789Sahrens 	    !zone_dataset_visible(dataset, &writable))
282789Sahrens 		return (ENOENT);
283789Sahrens 
284789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
285789Sahrens 		return (ENOENT);
286789Sahrens 
287789Sahrens 	if (INGLOBALZONE(curproc)) {
288789Sahrens 		/*
289789Sahrens 		 * If the fs is zoned, only root can access it from the
290789Sahrens 		 * global zone.
291789Sahrens 		 */
292789Sahrens 		if (secpolicy_zfs(cr) && zoned)
293789Sahrens 			return (EPERM);
294789Sahrens 	} else {
295789Sahrens 		/*
296789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
297789Sahrens 		 */
298789Sahrens 		if (!zoned)
299789Sahrens 			return (EPERM);
300789Sahrens 
301789Sahrens 		/* must be writable by this zone */
302789Sahrens 		if (!writable)
303789Sahrens 			return (EPERM);
304789Sahrens 	}
305789Sahrens 	return (0);
306789Sahrens }
307789Sahrens 
308789Sahrens int
3094543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
310789Sahrens {
311789Sahrens 	int error;
312789Sahrens 
3134543Smarks 	error = zfs_dozonecheck(name, cr);
3144543Smarks 	if (error == 0) {
3154543Smarks 		error = secpolicy_zfs(cr);
3164670Sahrens 		if (error)
3174543Smarks 			error = dsl_deleg_access(name, perm, cr);
3184543Smarks 	}
3194543Smarks 	return (error);
3204543Smarks }
3214543Smarks 
3224543Smarks static int
3234543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3244543Smarks {
3254543Smarks 	/*
3264543Smarks 	 * Check permissions for special properties.
3274543Smarks 	 */
3284543Smarks 	switch (prop) {
3294543Smarks 	case ZFS_PROP_ZONED:
3304543Smarks 		/*
3314543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3324543Smarks 		 */
3334543Smarks 		if (!INGLOBALZONE(curproc))
3344543Smarks 			return (EPERM);
3354543Smarks 		break;
336789Sahrens 
3374543Smarks 	case ZFS_PROP_QUOTA:
3384543Smarks 		if (!INGLOBALZONE(curproc)) {
3394543Smarks 			uint64_t zoned;
3404543Smarks 			char setpoint[MAXNAMELEN];
3414543Smarks 			/*
3424543Smarks 			 * Unprivileged users are allowed to modify the
3434543Smarks 			 * quota on things *under* (ie. contained by)
3444543Smarks 			 * the thing they own.
3454543Smarks 			 */
3464543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3474543Smarks 			    setpoint))
3484543Smarks 				return (EPERM);
3494670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3504543Smarks 				return (EPERM);
3514543Smarks 		}
3524670Sahrens 		break;
3534543Smarks 	}
3544543Smarks 
3554787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
356789Sahrens }
357789Sahrens 
3584543Smarks int
3594543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3604543Smarks {
3614543Smarks 	int error;
3624543Smarks 
3634543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3644543Smarks 	if (error)
3654543Smarks 		return (error);
3664543Smarks 
3674543Smarks 	/*
3684543Smarks 	 * permission to set permissions will be evaluated later in
3694543Smarks 	 * dsl_deleg_can_allow()
3704543Smarks 	 */
3714543Smarks 	return (0);
3724543Smarks }
3734543Smarks 
3744543Smarks int
3754543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3764543Smarks {
3774543Smarks 	int error;
3784543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
3794543Smarks 	    ZFS_DELEG_PERM_ROLLBACK, cr);
3804543Smarks 	if (error == 0)
3814543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
3824543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
3834543Smarks 	return (error);
3844543Smarks }
3854543Smarks 
3864543Smarks int
3874543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3884543Smarks {
3894543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
3904543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
3914543Smarks }
3924543Smarks 
3934543Smarks int
3944543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
3954543Smarks {
3964543Smarks 	if (!INGLOBALZONE(curproc))
3974543Smarks 		return (EPERM);
3984543Smarks 
3995367Sahrens 	if (secpolicy_nfs(cr) == 0) {
4004543Smarks 		return (0);
4014543Smarks 	} else {
4024543Smarks 		vnode_t *vp;
4034543Smarks 		int error;
4044543Smarks 
4054543Smarks 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4064543Smarks 		    NO_FOLLOW, NULL, &vp)) != 0)
4074543Smarks 			return (error);
4084543Smarks 
4094543Smarks 		/* Now make sure mntpnt and dataset are ZFS */
4104543Smarks 
4114543Smarks 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4124543Smarks 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4134543Smarks 		    zc->zc_name) != 0)) {
4144543Smarks 			VN_RELE(vp);
4154543Smarks 			return (EPERM);
4164543Smarks 		}
4174543Smarks 
4184543Smarks 		VN_RELE(vp);
4194543Smarks 		return (dsl_deleg_access(zc->zc_name,
4204543Smarks 		    ZFS_DELEG_PERM_SHARE, cr));
4214543Smarks 	}
4224543Smarks }
4234543Smarks 
424789Sahrens static int
4254543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
426789Sahrens {
427789Sahrens 	char *cp;
428789Sahrens 
429789Sahrens 	/*
430789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
431789Sahrens 	 */
4324543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4334543Smarks 	cp = strrchr(parent, '@');
434789Sahrens 	if (cp != NULL) {
435789Sahrens 		cp[0] = '\0';
436789Sahrens 	} else {
4374543Smarks 		cp = strrchr(parent, '/');
438789Sahrens 		if (cp == NULL)
439789Sahrens 			return (ENOENT);
440789Sahrens 		cp[0] = '\0';
441789Sahrens 	}
442789Sahrens 
4434543Smarks 	return (0);
4444543Smarks }
4454543Smarks 
4464543Smarks int
4474543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4484543Smarks {
4494543Smarks 	int error;
4504543Smarks 
4514543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4524543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4534543Smarks 		return (error);
4544543Smarks 
4554543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4564543Smarks }
4574543Smarks 
4584543Smarks static int
4594543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4604543Smarks {
4614543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4624543Smarks }
4634543Smarks 
4644543Smarks /*
4654543Smarks  * Must have sys_config privilege to check the iscsi permission
4664543Smarks  */
4674543Smarks /* ARGSUSED */
4684543Smarks static int
4694543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
4704543Smarks {
4714543Smarks 	return (secpolicy_zfs(cr));
4724543Smarks }
4734543Smarks 
4744543Smarks int
4754543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
4764543Smarks {
4774543Smarks 	char 	parentname[MAXNAMELEN];
4784543Smarks 	int	error;
4794543Smarks 
4804543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
4814543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
4824543Smarks 		return (error);
4834543Smarks 
4844543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
4854543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4864543Smarks 		return (error);
4874543Smarks 
4884543Smarks 	if ((error = zfs_get_parent(to, parentname,
4894543Smarks 	    sizeof (parentname))) != 0)
4904543Smarks 		return (error);
4914543Smarks 
4924543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
4934543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
4944543Smarks 		return (error);
4954543Smarks 
4964543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
4974543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4984543Smarks 		return (error);
4994543Smarks 
5004543Smarks 	return (error);
5014543Smarks }
5024543Smarks 
5034543Smarks static int
5044543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5054543Smarks {
5064543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5074543Smarks }
5084543Smarks 
5094543Smarks static int
5104543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5114543Smarks {
5124543Smarks 	char 	parentname[MAXNAMELEN];
5134543Smarks 	objset_t *clone;
5144543Smarks 	int error;
5154543Smarks 
5164543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5174543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5184543Smarks 	if (error)
5194543Smarks 		return (error);
5204543Smarks 
5214543Smarks 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
5226689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &clone);
5234543Smarks 
5244543Smarks 	if (error == 0) {
5254543Smarks 		dsl_dataset_t *pclone = NULL;
5264543Smarks 		dsl_dir_t *dd;
5274543Smarks 		dd = clone->os->os_dsl_dataset->ds_dir;
5284543Smarks 
5294543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5306689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5316689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5324543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5334543Smarks 		if (error) {
5344543Smarks 			dmu_objset_close(clone);
5354543Smarks 			return (error);
5364543Smarks 		}
5374543Smarks 
5384543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5394543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5404543Smarks 
5414543Smarks 		dsl_dataset_name(pclone, parentname);
5424543Smarks 		dmu_objset_close(clone);
5436689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5444543Smarks 		if (error == 0)
5454543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5464543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5474543Smarks 	}
5484543Smarks 	return (error);
5494543Smarks }
5504543Smarks 
5514543Smarks static int
5524543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5534543Smarks {
5544543Smarks 	int error;
5554543Smarks 
5564543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5574543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5584543Smarks 		return (error);
5594543Smarks 
5604543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5614543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5624543Smarks 		return (error);
5634543Smarks 
5644543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5654543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5664543Smarks }
5674543Smarks 
5684543Smarks int
5694543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
5704543Smarks {
5714543Smarks 	int error;
5724543Smarks 
5734543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
5744543Smarks 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
5754543Smarks 		return (error);
5764543Smarks 
5774543Smarks 	error = zfs_secpolicy_write_perms(name,
5784543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
5794543Smarks 
5804543Smarks 	return (error);
5814543Smarks }
5824543Smarks 
5834543Smarks static int
5844543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
5854543Smarks {
5864543Smarks 
5874543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
5884543Smarks }
5894543Smarks 
5904543Smarks static int
5914543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
5924543Smarks {
5934543Smarks 	char 	parentname[MAXNAMELEN];
5944543Smarks 	int 	error;
5954543Smarks 
5964543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
5974543Smarks 	    sizeof (parentname))) != 0)
5984543Smarks 		return (error);
5994543Smarks 
6004543Smarks 	if (zc->zc_value[0] != '\0') {
6014543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6024543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6034543Smarks 			return (error);
6044543Smarks 	}
6054543Smarks 
6064543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6074543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6084543Smarks 		return (error);
6094543Smarks 
6104543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6114543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6124543Smarks 
6134543Smarks 	return (error);
6144543Smarks }
6154543Smarks 
6164543Smarks static int
6174543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6184543Smarks {
6194543Smarks 	int error;
6204543Smarks 
6214543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6224543Smarks 	if (error) {
6234543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6244543Smarks 	}
6254543Smarks 	return (error);
626789Sahrens }
627789Sahrens 
628789Sahrens /*
629789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
630789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
631789Sahrens  */
632789Sahrens /* ARGSUSED */
633789Sahrens static int
6344543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
635789Sahrens {
636789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
637789Sahrens 		return (EPERM);
638789Sahrens 
639789Sahrens 	return (0);
640789Sahrens }
641789Sahrens 
642789Sahrens /*
6434543Smarks  * Just like zfs_secpolicy_config, except that we will check for
6444543Smarks  * mount permission on the dataset for permission to create/remove
6454543Smarks  * the minor nodes.
6464543Smarks  */
6474543Smarks static int
6484543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
6494543Smarks {
6504543Smarks 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
6514543Smarks 		return (dsl_deleg_access(zc->zc_name,
6524543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr));
6534543Smarks 	}
6544543Smarks 
6554543Smarks 	return (0);
6564543Smarks }
6574543Smarks 
6584543Smarks /*
6591544Seschrock  * Policy for fault injection.  Requires all privileges.
6601544Seschrock  */
6611544Seschrock /* ARGSUSED */
6621544Seschrock static int
6634543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6641544Seschrock {
6651544Seschrock 	return (secpolicy_zinject(cr));
6661544Seschrock }
6671544Seschrock 
6684849Sahrens static int
6694849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
6704849Sahrens {
6714849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
6724849Sahrens 
6735094Slling 	if (prop == ZPROP_INVAL) {
6744849Sahrens 		if (!zfs_prop_user(zc->zc_value))
6754849Sahrens 			return (EINVAL);
6764849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
6774849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
6784849Sahrens 	} else {
6794849Sahrens 		if (!zfs_prop_inheritable(prop))
6804849Sahrens 			return (EINVAL);
6814849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
6824849Sahrens 	}
6834849Sahrens }
6844849Sahrens 
6851544Seschrock /*
686789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
687789Sahrens  */
688789Sahrens static int
6895094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
690789Sahrens {
691789Sahrens 	char *packed;
692789Sahrens 	int error;
6935094Slling 	nvlist_t *list = NULL;
694789Sahrens 
695789Sahrens 	/*
6962676Seschrock 	 * Read in and unpack the user-supplied nvlist.
697789Sahrens 	 */
6985094Slling 	if (size == 0)
699789Sahrens 		return (EINVAL);
700789Sahrens 
701789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
702789Sahrens 
7035094Slling 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
704789Sahrens 		kmem_free(packed, size);
705789Sahrens 		return (error);
706789Sahrens 	}
707789Sahrens 
7085094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
709789Sahrens 		kmem_free(packed, size);
710789Sahrens 		return (error);
711789Sahrens 	}
712789Sahrens 
713789Sahrens 	kmem_free(packed, size);
714789Sahrens 
7155094Slling 	*nvp = list;
716789Sahrens 	return (0);
717789Sahrens }
718789Sahrens 
719789Sahrens static int
7202676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
7212676Seschrock {
7222676Seschrock 	char *packed = NULL;
7232676Seschrock 	size_t size;
7242676Seschrock 	int error;
7252676Seschrock 
7262676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
7272676Seschrock 
7282676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
7292676Seschrock 		error = ENOMEM;
7302676Seschrock 	} else {
7314611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
7322676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
7332676Seschrock 		    KM_SLEEP) == 0);
7342676Seschrock 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
7352676Seschrock 		    size);
7362676Seschrock 		kmem_free(packed, size);
7372676Seschrock 	}
7382676Seschrock 
7392676Seschrock 	zc->zc_nvlist_dst_size = size;
7402676Seschrock 	return (error);
7412676Seschrock }
7422676Seschrock 
7432676Seschrock static int
744789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
745789Sahrens {
746789Sahrens 	int error;
7475094Slling 	nvlist_t *config, *props = NULL;
7487184Stimh 	nvlist_t *rootprops = NULL;
7497184Stimh 	nvlist_t *zplprops = NULL;
7504715Sek110237 	char *buf;
751789Sahrens 
7525094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
7535094Slling 	    &config))
7544988Sek110237 		return (error);
7554715Sek110237 
7565094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
7575094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
7585094Slling 		nvlist_free(config);
7595094Slling 		return (error);
7605094Slling 	}
7615094Slling 
7627184Stimh 	if (props) {
7637184Stimh 		nvlist_t *nvl = NULL;
7647184Stimh 		uint64_t version = SPA_VERSION;
7657184Stimh 
7667184Stimh 		(void) nvlist_lookup_uint64(props,
7677184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
7687184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
7697184Stimh 			error = EINVAL;
7707184Stimh 			goto pool_props_bad;
7717184Stimh 		}
7727184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
7737184Stimh 		if (nvl) {
7747184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
7757184Stimh 			if (error != 0) {
7767184Stimh 				nvlist_free(config);
7777184Stimh 				nvlist_free(props);
7787184Stimh 				return (error);
7797184Stimh 			}
7807184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
7817184Stimh 		}
7827184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7837184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
7847184Stimh 		    zplprops, NULL);
7857184Stimh 		if (error)
7867184Stimh 			goto pool_props_bad;
7877184Stimh 	}
7887184Stimh 
7894988Sek110237 	buf = history_str_get(zc);
790789Sahrens 
7917184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
7927184Stimh 
7937184Stimh 	/*
7947184Stimh 	 * Set the remaining root properties
7957184Stimh 	 */
7967184Stimh 	if (!error &&
7977184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
7987184Stimh 		(void) spa_destroy(zc->zc_name);
799789Sahrens 
8004988Sek110237 	if (buf != NULL)
8014988Sek110237 		history_str_free(buf);
8025094Slling 
8037184Stimh pool_props_bad:
8047184Stimh 	nvlist_free(rootprops);
8057184Stimh 	nvlist_free(zplprops);
806789Sahrens 	nvlist_free(config);
8077184Stimh 	nvlist_free(props);
8085094Slling 
809789Sahrens 	return (error);
810789Sahrens }
811789Sahrens 
812789Sahrens static int
813789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
814789Sahrens {
8154543Smarks 	int error;
8164543Smarks 	zfs_log_history(zc);
8174543Smarks 	error = spa_destroy(zc->zc_name);
8184543Smarks 	return (error);
819789Sahrens }
820789Sahrens 
821789Sahrens static int
822789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
823789Sahrens {
824789Sahrens 	int error;
8255094Slling 	nvlist_t *config, *props = NULL;
826789Sahrens 	uint64_t guid;
827789Sahrens 
8285094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8295094Slling 	    &config)) != 0)
830789Sahrens 		return (error);
831789Sahrens 
8325094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8335094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
8345094Slling 		nvlist_free(config);
8355094Slling 		return (error);
8365094Slling 	}
8375094Slling 
838789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
8391544Seschrock 	    guid != zc->zc_guid)
840789Sahrens 		error = EINVAL;
8416643Seschrock 	else if (zc->zc_cookie)
8426643Seschrock 		error = spa_import_faulted(zc->zc_name, config,
8436643Seschrock 		    props);
844789Sahrens 	else
8455094Slling 		error = spa_import(zc->zc_name, config, props);
846789Sahrens 
847789Sahrens 	nvlist_free(config);
848789Sahrens 
8495094Slling 	if (props)
8505094Slling 		nvlist_free(props);
8515094Slling 
852789Sahrens 	return (error);
853789Sahrens }
854789Sahrens 
855789Sahrens static int
856789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
857789Sahrens {
8584543Smarks 	int error;
8597214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
8607214Slling 
8614543Smarks 	zfs_log_history(zc);
8627214Slling 	error = spa_export(zc->zc_name, NULL, force);
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 
11661544Seschrock 	error = spa_vdev_detach(spa, zc->zc_guid, 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 
1325789Sahrens 	do {
1326885Sahrens 		error = dmu_dir_list_next(os,
1327885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1328885Sahrens 		    NULL, &zc->zc_cookie);
1329789Sahrens 		if (error == ENOENT)
1330789Sahrens 			error = ESRCH;
1331885Sahrens 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1332789Sahrens 	    !zone_dataset_visible(zc->zc_name, NULL));
13336689Smaybee 	dmu_objset_close(os);
1334789Sahrens 
1335885Sahrens 	/*
1336885Sahrens 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1337885Sahrens 	 * try to get stats for it.  Userland will skip over it.
1338885Sahrens 	 */
1339885Sahrens 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1340885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1341789Sahrens 
1342789Sahrens 	return (error);
1343789Sahrens }
1344789Sahrens 
13455367Sahrens /*
13465367Sahrens  * inputs:
13475367Sahrens  * zc_name		name of filesystem
13485367Sahrens  * zc_cookie		zap cursor
13495367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13505367Sahrens  *
13515367Sahrens  * outputs:
13525367Sahrens  * zc_name		name of next snapshot
13535367Sahrens  * zc_objset_stats	stats
13545367Sahrens  * zc_nvlist_dst	property nvlist
13555367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13565367Sahrens  */
1357789Sahrens static int
1358789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1359789Sahrens {
1360885Sahrens 	objset_t *os;
1361789Sahrens 	int error;
1362789Sahrens 
13636689Smaybee 	error = dmu_objset_open(zc->zc_name,
13646689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
13656689Smaybee 	if (error)
13666689Smaybee 		return (error == ENOENT ? ESRCH : error);
1367789Sahrens 
13681003Slling 	/*
13691003Slling 	 * A dataset name of maximum length cannot have any snapshots,
13701003Slling 	 * so exit immediately.
13711003Slling 	 */
13721003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1373885Sahrens 		dmu_objset_close(os);
13741003Slling 		return (ESRCH);
1375789Sahrens 	}
1376789Sahrens 
1377885Sahrens 	error = dmu_snapshot_list_next(os,
1378885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
13795663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
13806689Smaybee 	dmu_objset_close(os);
1381885Sahrens 	if (error == 0)
1382885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
13836689Smaybee 	else if (error == ENOENT)
13846689Smaybee 		error = ESRCH;
1385789Sahrens 
13865367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
13876689Smaybee 	if (error)
13885367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1389789Sahrens 	return (error);
1390789Sahrens }
1391789Sahrens 
13926423Sgw25295 int
13934787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1394789Sahrens {
13952676Seschrock 	nvpair_t *elem;
13962676Seschrock 	int error;
13972676Seschrock 	uint64_t intval;
13982676Seschrock 	char *strval;
13992676Seschrock 
14004543Smarks 	/*
14014543Smarks 	 * First validate permission to set all of the properties
14024543Smarks 	 */
14032676Seschrock 	elem = NULL;
14042676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
14054670Sahrens 		const char *propname = nvpair_name(elem);
14064670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
14072676Seschrock 
14085094Slling 		if (prop == ZPROP_INVAL) {
14092676Seschrock 			/*
14102676Seschrock 			 * If this is a user-defined property, it must be a
14112676Seschrock 			 * string, and there is no further validation to do.
14122676Seschrock 			 */
14132676Seschrock 			if (!zfs_prop_user(propname) ||
14142676Seschrock 			    nvpair_type(elem) != DATA_TYPE_STRING)
14152676Seschrock 				return (EINVAL);
14162676Seschrock 
14175331Samw 			if (error = zfs_secpolicy_write_perms(name,
14185331Samw 			    ZFS_DELEG_PERM_USERPROP, CRED()))
14194670Sahrens 				return (error);
14204543Smarks 			continue;
14212676Seschrock 		}
14222676Seschrock 
14234787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
14244670Sahrens 			return (error);
14252676Seschrock 
14264670Sahrens 		/*
14274670Sahrens 		 * Check that this value is valid for this pool version
14284670Sahrens 		 */
14294670Sahrens 		switch (prop) {
14303886Sahl 		case ZFS_PROP_COMPRESSION:
14313886Sahl 			/*
14323886Sahl 			 * If the user specified gzip compression, make sure
14333886Sahl 			 * the SPA supports it. We ignore any errors here since
14343886Sahl 			 * we'll catch them later.
14353886Sahl 			 */
14363886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
14377042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
14387042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
14397042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
14407184Stimh 				    zfs_earlier_version(name,
14415331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
14425331Samw 					return (ENOTSUP);
14437042Sgw25295 
14447042Sgw25295 				/*
14457042Sgw25295 				 * If this is a bootable dataset then
14467042Sgw25295 				 * verify that the compression algorithm
14477042Sgw25295 				 * is supported for booting. We must return
14487042Sgw25295 				 * something other than ENOTSUP since it
14497042Sgw25295 				 * implies a downrev pool version.
14507042Sgw25295 				 */
14517042Sgw25295 				if (zfs_is_bootfs(name) &&
14527042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
14537042Sgw25295 					return (ERANGE);
14543886Sahl 			}
14553886Sahl 			break;
14564603Sahrens 
14574603Sahrens 		case ZFS_PROP_COPIES:
14587184Stimh 			if (zfs_earlier_version(name,
14597184Stimh 			    SPA_VERSION_DITTO_BLOCKS))
14605331Samw 				return (ENOTSUP);
14614603Sahrens 			break;
14625977Smarks 
14635977Smarks 		case ZFS_PROP_SHARESMB:
14646689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
14655977Smarks 				return (ENOTSUP);
14665977Smarks 			break;
14674603Sahrens 		}
14684543Smarks 	}
14694543Smarks 
14704543Smarks 	elem = NULL;
14714543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
14724670Sahrens 		const char *propname = nvpair_name(elem);
14734670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
14744543Smarks 
14755094Slling 		if (prop == ZPROP_INVAL) {
14764543Smarks 			VERIFY(nvpair_value_string(elem, &strval) == 0);
14774543Smarks 			error = dsl_prop_set(name, propname, 1,
14784543Smarks 			    strlen(strval) + 1, strval);
14794543Smarks 			if (error == 0)
14804543Smarks 				continue;
14814543Smarks 			else
14824543Smarks 				return (error);
14834543Smarks 		}
14842676Seschrock 
14852676Seschrock 		switch (prop) {
14862676Seschrock 		case ZFS_PROP_QUOTA:
14872676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
14884577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
14892676Seschrock 				return (error);
14902676Seschrock 			break;
14912676Seschrock 
14925378Sck153898 		case ZFS_PROP_REFQUOTA:
14935378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
14945378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
14955378Sck153898 				return (error);
14965378Sck153898 			break;
14975378Sck153898 
14982676Seschrock 		case ZFS_PROP_RESERVATION:
14992676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15002676Seschrock 			    (error = dsl_dir_set_reservation(name,
15012676Seschrock 			    intval)) != 0)
15022676Seschrock 				return (error);
15032676Seschrock 			break;
1504789Sahrens 
15055378Sck153898 		case ZFS_PROP_REFRESERVATION:
15065378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15075378Sck153898 			    (error = dsl_dataset_set_reservation(name,
15085378Sck153898 			    intval)) != 0)
15095378Sck153898 				return (error);
15105378Sck153898 			break;
15115378Sck153898 
15122676Seschrock 		case ZFS_PROP_VOLSIZE:
15132676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15144787Sahrens 			    (error = zvol_set_volsize(name,
15154787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
15162676Seschrock 				return (error);
15172676Seschrock 			break;
15182676Seschrock 
15192676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
15202676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15214577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
15224577Sahrens 				return (error);
15234577Sahrens 			break;
15244577Sahrens 
15254577Sahrens 		case ZFS_PROP_VERSION:
15264577Sahrens 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15274577Sahrens 			    (error = zfs_set_version(name, intval)) != 0)
15282676Seschrock 				return (error);
15292676Seschrock 			break;
15302676Seschrock 
15312676Seschrock 		default:
15322676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
15332676Seschrock 				if (zfs_prop_get_type(prop) !=
15344787Sahrens 				    PROP_TYPE_STRING)
15352676Seschrock 					return (EINVAL);
15362717Seschrock 				VERIFY(nvpair_value_string(elem, &strval) == 0);
15372717Seschrock 				if ((error = dsl_prop_set(name,
15382676Seschrock 				    nvpair_name(elem), 1, strlen(strval) + 1,
15392717Seschrock 				    strval)) != 0)
15402717Seschrock 					return (error);
15412676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
15422885Sahrens 				const char *unused;
15432885Sahrens 
15442717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
15452676Seschrock 
15462676Seschrock 				switch (zfs_prop_get_type(prop)) {
15474787Sahrens 				case PROP_TYPE_NUMBER:
15482676Seschrock 					break;
15494787Sahrens 				case PROP_TYPE_STRING:
15502717Seschrock 					return (EINVAL);
15514787Sahrens 				case PROP_TYPE_INDEX:
15522717Seschrock 					if (zfs_prop_index_to_string(prop,
15532717Seschrock 					    intval, &unused) != 0)
15542717Seschrock 						return (EINVAL);
15552676Seschrock 					break;
15562676Seschrock 				default:
15574577Sahrens 					cmn_err(CE_PANIC,
15584577Sahrens 					    "unknown property type");
15592676Seschrock 					break;
15602676Seschrock 				}
15612676Seschrock 
15622717Seschrock 				if ((error = dsl_prop_set(name, propname,
15632717Seschrock 				    8, 1, &intval)) != 0)
15642717Seschrock 					return (error);
15652676Seschrock 			} else {
15662676Seschrock 				return (EINVAL);
15672676Seschrock 			}
15682676Seschrock 			break;
15692676Seschrock 		}
15702676Seschrock 	}
15712676Seschrock 
15722676Seschrock 	return (0);
1573789Sahrens }
1574789Sahrens 
15755367Sahrens /*
15765367Sahrens  * inputs:
15775367Sahrens  * zc_name		name of filesystem
15785367Sahrens  * zc_value		name of property to inherit
15795367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
15807265Sahrens  * zc_cookie		clear existing local props?
15815367Sahrens  *
15825367Sahrens  * outputs:		none
15835367Sahrens  */
1584789Sahrens static int
15852676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1586789Sahrens {
15872676Seschrock 	nvlist_t *nvl;
15882676Seschrock 	int error;
1589789Sahrens 
15905094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
15915094Slling 	    &nvl)) != 0)
15922676Seschrock 		return (error);
15932676Seschrock 
15947265Sahrens 	if (zc->zc_cookie) {
15957265Sahrens 		nvlist_t *origprops;
15967265Sahrens 		objset_t *os;
15977265Sahrens 
15987265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
15997265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
16007265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
16017265Sahrens 				clear_props(zc->zc_name, origprops);
16027265Sahrens 				nvlist_free(origprops);
16037265Sahrens 			}
16047265Sahrens 			dmu_objset_close(os);
16057265Sahrens 		}
16067265Sahrens 
16077265Sahrens 	}
16087265Sahrens 
16094787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
16104543Smarks 
16112676Seschrock 	nvlist_free(nvl);
16122676Seschrock 	return (error);
1613789Sahrens }
1614789Sahrens 
16155367Sahrens /*
16165367Sahrens  * inputs:
16175367Sahrens  * zc_name		name of filesystem
16185367Sahrens  * zc_value		name of property to inherit
16195367Sahrens  *
16205367Sahrens  * outputs:		none
16215367Sahrens  */
1622789Sahrens static int
16234849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
16244849Sahrens {
16254849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
16264849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
16274849Sahrens }
16284849Sahrens 
16294849Sahrens static int
16304098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
16313912Slling {
16325094Slling 	nvlist_t *props;
16333912Slling 	spa_t *spa;
16345094Slling 	int error;
16353912Slling 
16365094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16375094Slling 	    &props)))
16383912Slling 		return (error);
16393912Slling 
16403912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
16415094Slling 		nvlist_free(props);
16423912Slling 		return (error);
16433912Slling 	}
16443912Slling 
16455094Slling 	error = spa_prop_set(spa, props);
16463912Slling 
16475094Slling 	nvlist_free(props);
16483912Slling 	spa_close(spa, FTAG);
16493912Slling 
16503912Slling 	return (error);
16513912Slling }
16523912Slling 
16533912Slling static int
16544098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
16553912Slling {
16563912Slling 	spa_t *spa;
16573912Slling 	int error;
16583912Slling 	nvlist_t *nvp = NULL;
16593912Slling 
16603912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
16613912Slling 		return (error);
16623912Slling 
16635094Slling 	error = spa_prop_get(spa, &nvp);
16643912Slling 
16653912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
16663912Slling 		error = put_nvlist(zc, nvp);
16673912Slling 	else
16683912Slling 		error = EFAULT;
16693912Slling 
16703912Slling 	spa_close(spa, FTAG);
16713912Slling 
16723912Slling 	if (nvp)
16733912Slling 		nvlist_free(nvp);
16743912Slling 	return (error);
16753912Slling }
16763912Slling 
16773912Slling static int
16784543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
16794543Smarks {
16804543Smarks 	nvlist_t *nvp;
16814543Smarks 	int error;
16824543Smarks 	uint32_t uid;
16834543Smarks 	uint32_t gid;
16844543Smarks 	uint32_t *groups;
16854543Smarks 	uint_t group_cnt;
16864543Smarks 	cred_t	*usercred;
16874543Smarks 
16885094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16895094Slling 	    &nvp)) != 0) {
16904543Smarks 		return (error);
16914543Smarks 	}
16924543Smarks 
16934543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
16944543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
16954543Smarks 		nvlist_free(nvp);
16964543Smarks 		return (EPERM);
16974543Smarks 	}
16984543Smarks 
16994543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
17004543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
17014543Smarks 		nvlist_free(nvp);
17024543Smarks 		return (EPERM);
17034543Smarks 	}
17044543Smarks 
17054543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
17064543Smarks 	    &groups, &group_cnt)) != 0) {
17074543Smarks 		nvlist_free(nvp);
17084543Smarks 		return (EPERM);
17094543Smarks 	}
17104543Smarks 	usercred = cralloc();
17114543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
17124543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
17134543Smarks 		nvlist_free(nvp);
17144543Smarks 		crfree(usercred);
17154543Smarks 		return (EPERM);
17164543Smarks 	}
17174543Smarks 	nvlist_free(nvp);
17184543Smarks 	error = dsl_deleg_access(zc->zc_name,
17194787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
17204543Smarks 	crfree(usercred);
17214543Smarks 	return (error);
17224543Smarks }
17234543Smarks 
17245367Sahrens /*
17255367Sahrens  * inputs:
17265367Sahrens  * zc_name		name of filesystem
17275367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
17285367Sahrens  * zc_perm_action	allow/unallow flag
17295367Sahrens  *
17305367Sahrens  * outputs:		none
17315367Sahrens  */
17324543Smarks static int
17334543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
17344543Smarks {
17354543Smarks 	int error;
17364543Smarks 	nvlist_t *fsaclnv = NULL;
17374543Smarks 
17385094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17395094Slling 	    &fsaclnv)) != 0)
17404543Smarks 		return (error);
17414543Smarks 
17424543Smarks 	/*
17434543Smarks 	 * Verify nvlist is constructed correctly
17444543Smarks 	 */
17454543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
17464543Smarks 		nvlist_free(fsaclnv);
17474543Smarks 		return (EINVAL);
17484543Smarks 	}
17494543Smarks 
17504543Smarks 	/*
17514543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
17524543Smarks 	 * that user is allowed to hand out each permission in
17534543Smarks 	 * the nvlist(s)
17544543Smarks 	 */
17554543Smarks 
17564787Sahrens 	error = secpolicy_zfs(CRED());
17574543Smarks 	if (error) {
17584787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
17594787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
17604787Sahrens 			    fsaclnv, CRED());
17614787Sahrens 		} else {
17624787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
17634787Sahrens 			    fsaclnv, CRED());
17644787Sahrens 		}
17654543Smarks 	}
17664543Smarks 
17674543Smarks 	if (error == 0)
17684543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
17694543Smarks 
17704543Smarks 	nvlist_free(fsaclnv);
17714543Smarks 	return (error);
17724543Smarks }
17734543Smarks 
17745367Sahrens /*
17755367Sahrens  * inputs:
17765367Sahrens  * zc_name		name of filesystem
17775367Sahrens  *
17785367Sahrens  * outputs:
17795367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
17805367Sahrens  */
17814543Smarks static int
17824543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
17834543Smarks {
17844543Smarks 	nvlist_t *nvp;
17854543Smarks 	int error;
17864543Smarks 
17874543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
17884543Smarks 		error = put_nvlist(zc, nvp);
17894543Smarks 		nvlist_free(nvp);
17904543Smarks 	}
17914543Smarks 
17924543Smarks 	return (error);
17934543Smarks }
17944543Smarks 
17955367Sahrens /*
17965367Sahrens  * inputs:
17975367Sahrens  * zc_name		name of volume
17985367Sahrens  *
17995367Sahrens  * outputs:		none
18005367Sahrens  */
18014543Smarks static int
1802789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
1803789Sahrens {
18044787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1805789Sahrens }
1806789Sahrens 
18075367Sahrens /*
18085367Sahrens  * inputs:
18095367Sahrens  * zc_name		name of volume
18105367Sahrens  *
18115367Sahrens  * outputs:		none
18125367Sahrens  */
1813789Sahrens static int
1814789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
1815789Sahrens {
18162676Seschrock 	return (zvol_remove_minor(zc->zc_name));
1817789Sahrens }
1818789Sahrens 
1819789Sahrens /*
1820789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
1821789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
1822789Sahrens  * is responsible for releasing the returned vfs pointer.
1823789Sahrens  */
1824789Sahrens static vfs_t *
1825789Sahrens zfs_get_vfs(const char *resource)
1826789Sahrens {
1827789Sahrens 	struct vfs *vfsp;
1828789Sahrens 	struct vfs *vfs_found = NULL;
1829789Sahrens 
1830789Sahrens 	vfs_list_read_lock();
1831789Sahrens 	vfsp = rootvfs;
1832789Sahrens 	do {
1833789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1834789Sahrens 			VFS_HOLD(vfsp);
1835789Sahrens 			vfs_found = vfsp;
1836789Sahrens 			break;
1837789Sahrens 		}
1838789Sahrens 		vfsp = vfsp->vfs_next;
1839789Sahrens 	} while (vfsp != rootvfs);
1840789Sahrens 	vfs_list_unlock();
1841789Sahrens 	return (vfs_found);
1842789Sahrens }
1843789Sahrens 
18444543Smarks /* ARGSUSED */
1845789Sahrens static void
18464543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1847789Sahrens {
18485331Samw 	zfs_creat_t *zct = arg;
18495498Stimh 
18505498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
18515331Samw }
18525331Samw 
18535498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
18545498Stimh 
18555331Samw /*
18565498Stimh  * inputs:
18577184Stimh  * createprops		list of properties requested by creator
18587184Stimh  * default_zplver	zpl version to use if unspecified in createprops
18597184Stimh  * fuids_ok		fuids allowed in this version of the spa?
18607184Stimh  * os			parent objset pointer (NULL if root fs)
18615331Samw  *
18625498Stimh  * outputs:
18635498Stimh  * zplprops	values for the zplprops we attach to the master node object
18647184Stimh  * is_ci	true if requested file system will be purely case-insensitive
18655331Samw  *
18665498Stimh  * Determine the settings for utf8only, normalization and
18675498Stimh  * casesensitivity.  Specific values may have been requested by the
18685498Stimh  * creator and/or we can inherit values from the parent dataset.  If
18695498Stimh  * the file system is of too early a vintage, a creator can not
18705498Stimh  * request settings for these properties, even if the requested
18715498Stimh  * setting is the default value.  We don't actually want to create dsl
18725498Stimh  * properties for these, so remove them from the source nvlist after
18735498Stimh  * processing.
18745331Samw  */
18755331Samw static int
18767184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
18777184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
18787184Stimh     boolean_t *is_ci)
18795331Samw {
18807184Stimh 	uint64_t zplver = default_zplver;
18815498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
18825498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
18835498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
18845498Stimh 
18855498Stimh 	ASSERT(zplprops != NULL);
18865498Stimh 
18875375Stimh 	/*
18885498Stimh 	 * Pull out creator prop choices, if any.
18895375Stimh 	 */
18905498Stimh 	if (createprops) {
18915498Stimh 		(void) nvlist_lookup_uint64(createprops,
18927184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
18937184Stimh 		(void) nvlist_lookup_uint64(createprops,
18945498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
18955498Stimh 		(void) nvlist_remove_all(createprops,
18965498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
18975498Stimh 		(void) nvlist_lookup_uint64(createprops,
18985498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
18995498Stimh 		(void) nvlist_remove_all(createprops,
19005498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
19015498Stimh 		(void) nvlist_lookup_uint64(createprops,
19025498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
19035498Stimh 		(void) nvlist_remove_all(createprops,
19045498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
19055331Samw 	}
19065331Samw 
19075375Stimh 	/*
19087184Stimh 	 * If the zpl version requested is whacky or the file system
19097184Stimh 	 * or pool is version is too "young" to support normalization
19107184Stimh 	 * and the creator tried to set a value for one of the props,
19117184Stimh 	 * error out.
19125498Stimh 	 */
19137184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
19147184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
19157184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
19165498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
19177184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
19185498Stimh 		return (ENOTSUP);
19195498Stimh 
19205498Stimh 	/*
19215498Stimh 	 * Put the version in the zplprops
19225498Stimh 	 */
19235498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19245498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
19255498Stimh 
19265498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
19275498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
19285498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19295498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
19305498Stimh 
19315498Stimh 	/*
19325498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
19335498Stimh 	 */
19345498Stimh 	if (norm)
19355498Stimh 		u8 = 1;
19365498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
19375498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
19385498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19395498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
19405498Stimh 
19415498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
19425498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
19435498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
19445498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
19455498Stimh 
19466492Stimh 	if (is_ci)
19476492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
19486492Stimh 
19497184Stimh 	return (0);
19507184Stimh }
19517184Stimh 
19527184Stimh static int
19537184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
19547184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
19557184Stimh {
19567184Stimh 	boolean_t fuids_ok = B_TRUE;
19577184Stimh 	uint64_t zplver = ZPL_VERSION;
19587184Stimh 	objset_t *os = NULL;
19597184Stimh 	char parentname[MAXNAMELEN];
19607184Stimh 	char *cp;
19617184Stimh 	int error;
19627184Stimh 
19637184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
19647184Stimh 	cp = strrchr(parentname, '/');
19657184Stimh 	ASSERT(cp != NULL);
19667184Stimh 	cp[0] = '\0';
19677184Stimh 
19687184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
19697184Stimh 		zplver = ZPL_VERSION_FUID - 1;
19707184Stimh 		fuids_ok = B_FALSE;
19717184Stimh 	}
19727184Stimh 
19737184Stimh 	/*
19747184Stimh 	 * Open parent object set so we can inherit zplprop values.
19757184Stimh 	 */
19767184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
19777184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
19787184Stimh 		return (error);
19797184Stimh 
19807184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
19817184Stimh 	    zplprops, is_ci);
19825498Stimh 	dmu_objset_close(os);
19837184Stimh 	return (error);
19847184Stimh }
19857184Stimh 
19867184Stimh static int
19877184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
19887184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
19897184Stimh {
19907184Stimh 	boolean_t fuids_ok = B_TRUE;
19917184Stimh 	uint64_t zplver = ZPL_VERSION;
19927184Stimh 	int error;
19937184Stimh 
19947184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
19957184Stimh 		zplver = ZPL_VERSION_FUID - 1;
19967184Stimh 		fuids_ok = B_FALSE;
19977184Stimh 	}
19987184Stimh 
19997184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
20007184Stimh 	    zplprops, is_ci);
20017184Stimh 	return (error);
2002789Sahrens }
2003789Sahrens 
20045367Sahrens /*
20055367Sahrens  * inputs:
20065367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
20075367Sahrens  * zc_name		name of new objset
20085367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
20095367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
20105367Sahrens  *
20115498Stimh  * outputs: none
20125367Sahrens  */
2013789Sahrens static int
2014789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2015789Sahrens {
2016789Sahrens 	objset_t *clone;
2017789Sahrens 	int error = 0;
20185331Samw 	zfs_creat_t zct;
20194543Smarks 	nvlist_t *nvprops = NULL;
20204543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2021789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2022789Sahrens 
2023789Sahrens 	switch (type) {
2024789Sahrens 
2025789Sahrens 	case DMU_OST_ZFS:
2026789Sahrens 		cbfunc = zfs_create_cb;
2027789Sahrens 		break;
2028789Sahrens 
2029789Sahrens 	case DMU_OST_ZVOL:
2030789Sahrens 		cbfunc = zvol_create_cb;
2031789Sahrens 		break;
2032789Sahrens 
2033789Sahrens 	default:
20342199Sahrens 		cbfunc = NULL;
20356423Sgw25295 		break;
20362199Sahrens 	}
20375326Sek110237 	if (strchr(zc->zc_name, '@') ||
20385326Sek110237 	    strchr(zc->zc_name, '%'))
2039789Sahrens 		return (EINVAL);
2040789Sahrens 
20412676Seschrock 	if (zc->zc_nvlist_src != NULL &&
20425094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20435094Slling 	    &nvprops)) != 0)
20442676Seschrock 		return (error);
20452676Seschrock 
20465498Stimh 	zct.zct_zplprops = NULL;
20475331Samw 	zct.zct_props = nvprops;
20485331Samw 
20492676Seschrock 	if (zc->zc_value[0] != '\0') {
2050789Sahrens 		/*
2051789Sahrens 		 * We're creating a clone of an existing snapshot.
2052789Sahrens 		 */
20532676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
20542676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
20554543Smarks 			nvlist_free(nvprops);
2056789Sahrens 			return (EINVAL);
20572676Seschrock 		}
2058789Sahrens 
20592676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
20606689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
20612676Seschrock 		if (error) {
20624543Smarks 			nvlist_free(nvprops);
2063789Sahrens 			return (error);
20642676Seschrock 		}
20656492Stimh 
20666492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
20676492Stimh 		    NULL, NULL);
20685331Samw 		if (error) {
20695331Samw 			dmu_objset_close(clone);
20705331Samw 			nvlist_free(nvprops);
20715331Samw 			return (error);
20725331Samw 		}
2073789Sahrens 		dmu_objset_close(clone);
2074789Sahrens 	} else {
20756492Stimh 		boolean_t is_insensitive = B_FALSE;
20766492Stimh 
20772676Seschrock 		if (cbfunc == NULL) {
20784543Smarks 			nvlist_free(nvprops);
20792199Sahrens 			return (EINVAL);
20802676Seschrock 		}
20812676Seschrock 
2082789Sahrens 		if (type == DMU_OST_ZVOL) {
20832676Seschrock 			uint64_t volsize, volblocksize;
20842676Seschrock 
20854543Smarks 			if (nvprops == NULL ||
20864543Smarks 			    nvlist_lookup_uint64(nvprops,
20872676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
20882676Seschrock 			    &volsize) != 0) {
20894543Smarks 				nvlist_free(nvprops);
20902676Seschrock 				return (EINVAL);
20912676Seschrock 			}
20922676Seschrock 
20934543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
20942676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
20952676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
20964543Smarks 				nvlist_free(nvprops);
20972676Seschrock 				return (EINVAL);
20982676Seschrock 			}
20991133Seschrock 
21002676Seschrock 			if (error != 0)
21012676Seschrock 				volblocksize = zfs_prop_default_numeric(
21022676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
21032676Seschrock 
21042676Seschrock 			if ((error = zvol_check_volblocksize(
21052676Seschrock 			    volblocksize)) != 0 ||
21062676Seschrock 			    (error = zvol_check_volsize(volsize,
21072676Seschrock 			    volblocksize)) != 0) {
21084543Smarks 				nvlist_free(nvprops);
2109789Sahrens 				return (error);
21102676Seschrock 			}
21114577Sahrens 		} else if (type == DMU_OST_ZFS) {
21125331Samw 			int error;
21135331Samw 
21145498Stimh 			/*
21155331Samw 			 * We have to have normalization and
21165331Samw 			 * case-folding flags correct when we do the
21175331Samw 			 * file system creation, so go figure them out
21185498Stimh 			 * now.
21195331Samw 			 */
21205498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
21215498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21225498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
21237184Stimh 			    zct.zct_zplprops, &is_insensitive);
21245331Samw 			if (error != 0) {
21255331Samw 				nvlist_free(nvprops);
21265498Stimh 				nvlist_free(zct.zct_zplprops);
21275331Samw 				return (error);
21284577Sahrens 			}
21292676Seschrock 		}
21306492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
21316492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
21325498Stimh 		nvlist_free(zct.zct_zplprops);
2133789Sahrens 	}
21342676Seschrock 
21352676Seschrock 	/*
21362676Seschrock 	 * It would be nice to do this atomically.
21372676Seschrock 	 */
21382676Seschrock 	if (error == 0) {
21394787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
21402676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
21412676Seschrock 	}
21424543Smarks 	nvlist_free(nvprops);
2143789Sahrens 	return (error);
2144789Sahrens }
2145789Sahrens 
21467265Sahrens struct snap_prop_arg {
21477265Sahrens 	nvlist_t *nvprops;
21487265Sahrens 	const char *snapname;
21497265Sahrens };
21507265Sahrens 
21517265Sahrens static int
21527265Sahrens set_snap_props(char *name, void *arg)
21537265Sahrens {
21547265Sahrens 	struct snap_prop_arg *snpa = arg;
21557265Sahrens 	int len = strlen(name) + strlen(snpa->snapname) + 2;
21567265Sahrens 	char *buf = kmem_alloc(len, KM_SLEEP);
21577265Sahrens 	int err;
21587265Sahrens 
21597265Sahrens 	(void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
21607265Sahrens 	err = zfs_set_prop_nvlist(buf, snpa->nvprops);
21617265Sahrens 	if (err)
21627265Sahrens 		(void) dmu_objset_destroy(buf);
21637265Sahrens 	kmem_free(buf, len);
21647265Sahrens 	return (err);
21657265Sahrens }
21667265Sahrens 
21675367Sahrens /*
21685367Sahrens  * inputs:
21695367Sahrens  * zc_name	name of filesystem
21705367Sahrens  * zc_value	short name of snapshot
21715367Sahrens  * zc_cookie	recursive flag
21725367Sahrens  *
21735367Sahrens  * outputs:	none
21745367Sahrens  */
2175789Sahrens static int
21762199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
21772199Sahrens {
21787265Sahrens 	nvlist_t *nvprops = NULL;
21797265Sahrens 	int error;
21807265Sahrens 	boolean_t recursive = zc->zc_cookie;
21817265Sahrens 
21822676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
21832199Sahrens 		return (EINVAL);
21847265Sahrens 
21857265Sahrens 	if (zc->zc_nvlist_src != NULL &&
21867265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
21877265Sahrens 	    &nvprops)) != 0)
21887265Sahrens 		return (error);
21897265Sahrens 
21907265Sahrens 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
21917265Sahrens 
21927265Sahrens 	/*
21937265Sahrens 	 * It would be nice to do this atomically.
21947265Sahrens 	 */
21957265Sahrens 	if (error == 0) {
21967265Sahrens 		struct snap_prop_arg snpa;
21977265Sahrens 		snpa.nvprops = nvprops;
21987265Sahrens 		snpa.snapname = zc->zc_value;
21997265Sahrens 		if (recursive) {
22007265Sahrens 			error = dmu_objset_find(zc->zc_name,
22017265Sahrens 			    set_snap_props, &snpa, DS_FIND_CHILDREN);
22027265Sahrens 			if (error) {
22037265Sahrens 				(void) dmu_snapshots_destroy(zc->zc_name,
22047265Sahrens 				    zc->zc_value);
22057265Sahrens 			}
22067265Sahrens 		} else {
22077265Sahrens 			error = set_snap_props(zc->zc_name, &snpa);
22087265Sahrens 		}
22097265Sahrens 	}
22107265Sahrens 	nvlist_free(nvprops);
22117265Sahrens 	return (error);
22122199Sahrens }
22132199Sahrens 
22144007Smmusante int
22152199Sahrens zfs_unmount_snap(char *name, void *arg)
2216789Sahrens {
22172417Sahrens 	vfs_t *vfsp = NULL;
22182199Sahrens 
22196689Smaybee 	if (arg) {
22206689Smaybee 		char *snapname = arg;
22216689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
22226689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
22236689Smaybee 
22246689Smaybee 		(void) strcpy(buf, name);
22256689Smaybee 		(void) strcat(buf, "@");
22266689Smaybee 		(void) strcat(buf, snapname);
22276689Smaybee 		vfsp = zfs_get_vfs(buf);
22286689Smaybee 		kmem_free(buf, len);
22292417Sahrens 	} else if (strchr(name, '@')) {
22302199Sahrens 		vfsp = zfs_get_vfs(name);
22312199Sahrens 	}
22322199Sahrens 
22332199Sahrens 	if (vfsp) {
22342199Sahrens 		/*
22352199Sahrens 		 * Always force the unmount for snapshots.
22362199Sahrens 		 */
22372199Sahrens 		int flag = MS_FORCE;
2238789Sahrens 		int err;
2239789Sahrens 
22402199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
22412199Sahrens 			VFS_RELE(vfsp);
22422199Sahrens 			return (err);
22432199Sahrens 		}
22442199Sahrens 		VFS_RELE(vfsp);
22452199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
22462199Sahrens 			return (err);
22472199Sahrens 	}
22482199Sahrens 	return (0);
22492199Sahrens }
22502199Sahrens 
22515367Sahrens /*
22525367Sahrens  * inputs:
22535367Sahrens  * zc_name	name of filesystem
22545367Sahrens  * zc_value	short name of snapshot
22555367Sahrens  *
22565367Sahrens  * outputs:	none
22575367Sahrens  */
22582199Sahrens static int
22592199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
22602199Sahrens {
22612199Sahrens 	int err;
2262789Sahrens 
22632676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
22642199Sahrens 		return (EINVAL);
22652199Sahrens 	err = dmu_objset_find(zc->zc_name,
22662676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
22672199Sahrens 	if (err)
22682199Sahrens 		return (err);
22692676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
22702199Sahrens }
22712199Sahrens 
22725367Sahrens /*
22735367Sahrens  * inputs:
22745367Sahrens  * zc_name		name of dataset to destroy
22755367Sahrens  * zc_objset_type	type of objset
22765367Sahrens  *
22775367Sahrens  * outputs:		none
22785367Sahrens  */
22792199Sahrens static int
22802199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
22812199Sahrens {
22822199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
22832199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
22842199Sahrens 		if (err)
22852199Sahrens 			return (err);
2286789Sahrens 	}
2287789Sahrens 
2288789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2289789Sahrens }
2290789Sahrens 
22915367Sahrens /*
22925367Sahrens  * inputs:
22935446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
22945367Sahrens  *
22955367Sahrens  * outputs:	none
22965367Sahrens  */
2297789Sahrens static int
2298789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2299789Sahrens {
23005446Sahrens 	objset_t *os;
23015446Sahrens 	int error;
23025446Sahrens 	zfsvfs_t *zfsvfs = NULL;
23035446Sahrens 
23045446Sahrens 	/*
23055446Sahrens 	 * Get the zfsvfs for the receiving objset. There
23065446Sahrens 	 * won't be one if we're operating on a zvol, if the
23075446Sahrens 	 * objset doesn't exist yet, or is not mounted.
23085446Sahrens 	 */
23096689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
23105446Sahrens 	if (error)
23115446Sahrens 		return (error);
23125446Sahrens 
23135446Sahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
23145446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
23155446Sahrens 		zfsvfs = dmu_objset_get_user(os);
23165446Sahrens 		if (zfsvfs != NULL)
23175446Sahrens 			VFS_HOLD(zfsvfs->z_vfs);
23185446Sahrens 		mutex_exit(&os->os->os_user_ptr_lock);
23195446Sahrens 	}
23205446Sahrens 
23215446Sahrens 	if (zfsvfs != NULL) {
23225446Sahrens 		char osname[MAXNAMELEN];
23235446Sahrens 		int mode;
23245446Sahrens 
23256083Sek110237 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
23266083Sek110237 		if (error == 0) {
23276083Sek110237 			int resume_err;
23286083Sek110237 
23296083Sek110237 			ASSERT(strcmp(osname, zc->zc_name) == 0);
23306083Sek110237 			error = dmu_objset_rollback(os);
23316083Sek110237 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
23326083Sek110237 			error = error ? error : resume_err;
23336083Sek110237 		} else {
23346083Sek110237 			dmu_objset_close(os);
23356083Sek110237 		}
23365446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
23375446Sahrens 	} else {
23385446Sahrens 		error = dmu_objset_rollback(os);
23395446Sahrens 	}
23406689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
23415446Sahrens 
23425446Sahrens 	return (error);
2343789Sahrens }
2344789Sahrens 
23455367Sahrens /*
23465367Sahrens  * inputs:
23475367Sahrens  * zc_name	old name of dataset
23485367Sahrens  * zc_value	new name of dataset
23495367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
23505367Sahrens  *
23515367Sahrens  * outputs:	none
23525367Sahrens  */
2353789Sahrens static int
2354789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2355789Sahrens {
23564490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
23574007Smmusante 
23582676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23595326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
23605326Sek110237 	    strchr(zc->zc_value, '%'))
2361789Sahrens 		return (EINVAL);
2362789Sahrens 
23634007Smmusante 	/*
23644007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
23654007Smmusante 	 * in which case the dataset code figures out which snapshots
23664007Smmusante 	 * to unmount.
23674007Smmusante 	 */
23684007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2369789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
23702199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
23712199Sahrens 		if (err)
23722199Sahrens 			return (err);
2373789Sahrens 	}
23744007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2375789Sahrens }
2376789Sahrens 
23776689Smaybee static void
23786689Smaybee clear_props(char *dataset, nvlist_t *props)
23796689Smaybee {
23806689Smaybee 	zfs_cmd_t *zc;
23816689Smaybee 	nvpair_t *prop;
23826689Smaybee 
23836689Smaybee 	if (props == NULL)
23846689Smaybee 		return;
23856689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
23866689Smaybee 	(void) strcpy(zc->zc_name, dataset);
23876689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
23886689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
23896689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
23906689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
23916689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
23926689Smaybee 	}
23936689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
23946689Smaybee }
23956689Smaybee 
23965367Sahrens /*
23975367Sahrens  * inputs:
23985367Sahrens  * zc_name		name of containing filesystem
23995367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
24005367Sahrens  * zc_value		name of snapshot to create
24015367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
24025367Sahrens  * zc_cookie		file descriptor to recv from
24035367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
24045367Sahrens  * zc_guid		force flag
24055367Sahrens  *
24065367Sahrens  * outputs:
24075367Sahrens  * zc_cookie		number of bytes read
24085367Sahrens  */
2409789Sahrens static int
24105367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2411789Sahrens {
2412789Sahrens 	file_t *fp;
24135326Sek110237 	objset_t *os;
24145367Sahrens 	dmu_recv_cookie_t drc;
24155326Sek110237 	zfsvfs_t *zfsvfs = NULL;
24165326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2417789Sahrens 	int error, fd;
24185367Sahrens 	offset_t off;
24195367Sahrens 	nvlist_t *props = NULL;
24206689Smaybee 	nvlist_t *origprops = NULL;
24215367Sahrens 	objset_t *origin = NULL;
24225367Sahrens 	char *tosnap;
24235367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2424789Sahrens 
24253265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
24265326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
24275326Sek110237 	    strchr(zc->zc_value, '%'))
24283265Sahrens 		return (EINVAL);
24293265Sahrens 
24305367Sahrens 	(void) strcpy(tofs, zc->zc_value);
24315367Sahrens 	tosnap = strchr(tofs, '@');
24325367Sahrens 	*tosnap = '\0';
24335367Sahrens 	tosnap++;
24345367Sahrens 
24355367Sahrens 	if (zc->zc_nvlist_src != NULL &&
24365367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24375367Sahrens 	    &props)) != 0)
24385367Sahrens 		return (error);
24395367Sahrens 
2440789Sahrens 	fd = zc->zc_cookie;
2441789Sahrens 	fp = getf(fd);
24425367Sahrens 	if (fp == NULL) {
24435367Sahrens 		nvlist_free(props);
2444789Sahrens 		return (EBADF);
24455367Sahrens 	}
24465326Sek110237 
24476689Smaybee 	if (dmu_objset_open(tofs, DMU_OST_ANY,
24486689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
24496689Smaybee 		/*
24506689Smaybee 		 * Try to get the zfsvfs for the receiving objset.
24516689Smaybee 		 * There won't be one if we're operating on a zvol,
24526689Smaybee 		 * if the objset doesn't exist yet, or is not mounted.
24536689Smaybee 		 */
24545446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
24556689Smaybee 		if (zfsvfs = dmu_objset_get_user(os)) {
24566083Sek110237 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
24576689Smaybee 				mutex_exit(&os->os->os_user_ptr_lock);
24586083Sek110237 				dmu_objset_close(os);
24596689Smaybee 				zfsvfs = NULL;
24606689Smaybee 				error = EBUSY;
24616689Smaybee 				goto out;
24626083Sek110237 			}
24636689Smaybee 			VFS_HOLD(zfsvfs->z_vfs);
24646083Sek110237 		}
24656689Smaybee 		mutex_exit(&os->os->os_user_ptr_lock);
24666689Smaybee 
24676689Smaybee 		/*
24686689Smaybee 		 * If new properties are supplied, they are to completely
24696689Smaybee 		 * replace the existing ones, so stash away the existing ones.
24706689Smaybee 		 */
24716689Smaybee 		if (props)
24726689Smaybee 			(void) dsl_prop_get_all(os, &origprops, TRUE);
24736689Smaybee 
24745326Sek110237 		dmu_objset_close(os);
24755326Sek110237 	}
24765326Sek110237 
24775367Sahrens 	if (zc->zc_string[0]) {
24785367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
24796689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
24806689Smaybee 		if (error)
24816689Smaybee 			goto out;
24825367Sahrens 	}
24835367Sahrens 
24845367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
24855367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
24865367Sahrens 	if (origin)
24875367Sahrens 		dmu_objset_close(origin);
24886689Smaybee 	if (error)
24896689Smaybee 		goto out;
24905326Sek110237 
24915326Sek110237 	/*
24926689Smaybee 	 * Reset properties.  We do this before we receive the stream
24936689Smaybee 	 * so that the properties are applied to the new data.
24945326Sek110237 	 */
24955367Sahrens 	if (props) {
24966689Smaybee 		clear_props(tofs, origprops);
24976689Smaybee 		/*
24986689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
24996689Smaybee 		 */
25006689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
25015367Sahrens 	}
25025367Sahrens 
25035367Sahrens 	off = fp->f_offset;
25045367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
25055367Sahrens 
25066689Smaybee 	if (error == 0 && zfsvfs) {
25076689Smaybee 		char osname[MAXNAMELEN];
25086689Smaybee 		int mode;
25096689Smaybee 
25106689Smaybee 		/* online recv */
25116689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
25126689Smaybee 		if (error == 0) {
25136689Smaybee 			int resume_err;
25146689Smaybee 
25156689Smaybee 			error = dmu_recv_end(&drc);
25166689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
25176689Smaybee 			error = error ? error : resume_err;
25185367Sahrens 		} else {
25196689Smaybee 			dmu_recv_abort_cleanup(&drc);
25205367Sahrens 		}
25216689Smaybee 	} else if (error == 0) {
25226689Smaybee 		error = dmu_recv_end(&drc);
25236083Sek110237 	}
25245367Sahrens 
25255367Sahrens 	zc->zc_cookie = off - fp->f_offset;
25265367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
25275367Sahrens 		fp->f_offset = off;
25282885Sahrens 
25296689Smaybee 	/*
25306689Smaybee 	 * On error, restore the original props.
25316689Smaybee 	 */
25326689Smaybee 	if (error && props) {
25336689Smaybee 		clear_props(tofs, props);
25346689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
25356689Smaybee 	}
25366689Smaybee out:
25376689Smaybee 	if (zfsvfs) {
25386689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
25396689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
25406689Smaybee 	}
25416689Smaybee 	nvlist_free(props);
25426689Smaybee 	nvlist_free(origprops);
2543789Sahrens 	releasef(fd);
2544789Sahrens 	return (error);
2545789Sahrens }
2546789Sahrens 
25475367Sahrens /*
25485367Sahrens  * inputs:
25495367Sahrens  * zc_name	name of snapshot to send
25505367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
25515367Sahrens  * zc_cookie	file descriptor to send stream to
25525367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
25535367Sahrens  *
25545367Sahrens  * outputs: none
25555367Sahrens  */
2556789Sahrens static int
25575367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2558789Sahrens {
2559789Sahrens 	objset_t *fromsnap = NULL;
2560789Sahrens 	objset_t *tosnap;
2561789Sahrens 	file_t *fp;
2562789Sahrens 	int error;
25635367Sahrens 	offset_t off;
2564789Sahrens 
2565789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
25666689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2567789Sahrens 	if (error)
2568789Sahrens 		return (error);
2569789Sahrens 
25702676Seschrock 	if (zc->zc_value[0] != '\0') {
25712885Sahrens 		char buf[MAXPATHLEN];
25722885Sahrens 		char *cp;
25732885Sahrens 
25742885Sahrens 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
25752885Sahrens 		cp = strchr(buf, '@');
25762885Sahrens 		if (cp)
25772885Sahrens 			*(cp+1) = 0;
25782885Sahrens 		(void) strncat(buf, zc->zc_value, sizeof (buf));
25792885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
25806689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2581789Sahrens 		if (error) {
2582789Sahrens 			dmu_objset_close(tosnap);
2583789Sahrens 			return (error);
2584789Sahrens 		}
2585789Sahrens 	}
2586789Sahrens 
2587789Sahrens 	fp = getf(zc->zc_cookie);
2588789Sahrens 	if (fp == NULL) {
2589789Sahrens 		dmu_objset_close(tosnap);
2590789Sahrens 		if (fromsnap)
2591789Sahrens 			dmu_objset_close(fromsnap);
2592789Sahrens 		return (EBADF);
2593789Sahrens 	}
2594789Sahrens 
25955367Sahrens 	off = fp->f_offset;
25965367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
25975367Sahrens 
25985367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
25995367Sahrens 		fp->f_offset = off;
2600789Sahrens 	releasef(zc->zc_cookie);
2601789Sahrens 	if (fromsnap)
2602789Sahrens 		dmu_objset_close(fromsnap);
2603789Sahrens 	dmu_objset_close(tosnap);
2604789Sahrens 	return (error);
2605789Sahrens }
2606789Sahrens 
26071544Seschrock static int
26081544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
26091544Seschrock {
26101544Seschrock 	int id, error;
26111544Seschrock 
26121544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
26131544Seschrock 	    &zc->zc_inject_record);
26141544Seschrock 
26151544Seschrock 	if (error == 0)
26161544Seschrock 		zc->zc_guid = (uint64_t)id;
26171544Seschrock 
26181544Seschrock 	return (error);
26191544Seschrock }
26201544Seschrock 
26211544Seschrock static int
26221544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
26231544Seschrock {
26241544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
26251544Seschrock }
26261544Seschrock 
26271544Seschrock static int
26281544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
26291544Seschrock {
26301544Seschrock 	int id = (int)zc->zc_guid;
26311544Seschrock 	int error;
26321544Seschrock 
26331544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
26341544Seschrock 	    &zc->zc_inject_record);
26351544Seschrock 
26361544Seschrock 	zc->zc_guid = id;
26371544Seschrock 
26381544Seschrock 	return (error);
26391544Seschrock }
26401544Seschrock 
26411544Seschrock static int
26421544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
26431544Seschrock {
26441544Seschrock 	spa_t *spa;
26451544Seschrock 	int error;
26462676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
26471544Seschrock 
26481544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
26491544Seschrock 		return (error);
26501544Seschrock 
26512676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
26521544Seschrock 	    &count);
26531544Seschrock 	if (error == 0)
26542676Seschrock 		zc->zc_nvlist_dst_size = count;
26551544Seschrock 	else
26562676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
26571544Seschrock 
26581544Seschrock 	spa_close(spa, FTAG);
26591544Seschrock 
26601544Seschrock 	return (error);
26611544Seschrock }
26621544Seschrock 
26631544Seschrock static int
26641544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
26651544Seschrock {
26661544Seschrock 	spa_t *spa;
26671544Seschrock 	vdev_t *vd;
26684808Sek110237 	uint64_t txg;
26691544Seschrock 	int error;
26701544Seschrock 
26717294Sperrin 	/*
26727294Sperrin 	 * On zpool clear we also fix up missing slogs
26737294Sperrin 	 */
26747294Sperrin 	mutex_enter(&spa_namespace_lock);
26757294Sperrin 	spa = spa_lookup(zc->zc_name);
26767294Sperrin 	if (spa == NULL) {
26777294Sperrin 		mutex_exit(&spa_namespace_lock);
26787294Sperrin 		return (EIO);
26797294Sperrin 	}
26807294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
26817294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
26827294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
26837294Sperrin 	}
26847294Sperrin 	mutex_exit(&spa_namespace_lock);
26857294Sperrin 
26861544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
26871544Seschrock 		return (error);
26881544Seschrock 
26895329Sgw25295 	/*
26905329Sgw25295 	 * Try to resume any I/Os which may have been suspended
26915329Sgw25295 	 * as a result of a complete pool failure.
26925329Sgw25295 	 */
26935329Sgw25295 	if (!list_is_empty(&spa->spa_zio_list)) {
26945329Sgw25295 		if (zio_vdev_resume_io(spa) != 0) {
26955329Sgw25295 			spa_close(spa, FTAG);
26965329Sgw25295 			return (EIO);
26975329Sgw25295 		}
26985329Sgw25295 	}
26995329Sgw25295 
27004451Seschrock 	txg = spa_vdev_enter(spa);
27011544Seschrock 
27022676Seschrock 	if (zc->zc_guid == 0) {
27031544Seschrock 		vd = NULL;
27046643Seschrock 	} else {
27056643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
27065450Sbrendan 		if (vd == NULL) {
27075450Sbrendan 			(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
27085450Sbrendan 			spa_close(spa, FTAG);
27095450Sbrendan 			return (ENODEV);
27105450Sbrendan 		}
27111544Seschrock 	}
27121544Seschrock 
27135329Sgw25295 	vdev_clear(spa, vd, B_TRUE);
27141544Seschrock 
27154451Seschrock 	(void) spa_vdev_exit(spa, NULL, txg, 0);
27161544Seschrock 
27171544Seschrock 	spa_close(spa, FTAG);
27181544Seschrock 
27191544Seschrock 	return (0);
27201544Seschrock }
27211544Seschrock 
27225367Sahrens /*
27235367Sahrens  * inputs:
27245367Sahrens  * zc_name	name of filesystem
27255367Sahrens  * zc_value	name of origin snapshot
27265367Sahrens  *
27275367Sahrens  * outputs:	none
27285367Sahrens  */
27291544Seschrock static int
27302082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
27312082Seschrock {
27322417Sahrens 	char *cp;
27332417Sahrens 
27342417Sahrens 	/*
27352417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
27362417Sahrens 	 * it's easier.
27372417Sahrens 	 */
27382676Seschrock 	cp = strchr(zc->zc_value, '@');
27392417Sahrens 	if (cp)
27402417Sahrens 		*cp = '\0';
27412676Seschrock 	(void) dmu_objset_find(zc->zc_value,
27422417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
27432082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
27442082Seschrock }
27452082Seschrock 
27464543Smarks /*
27474543Smarks  * We don't want to have a hard dependency
27484543Smarks  * against some special symbols in sharefs
27495331Samw  * nfs, and smbsrv.  Determine them if needed when
27504543Smarks  * the first file system is shared.
27515331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
27524543Smarks  */
27535331Samw int (*znfsexport_fs)(void *arg);
27544543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
27555331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
27565331Samw 
27575331Samw int zfs_nfsshare_inited;
27585331Samw int zfs_smbshare_inited;
27595331Samw 
27604543Smarks ddi_modhandle_t nfs_mod;
27614543Smarks ddi_modhandle_t sharefs_mod;
27625331Samw ddi_modhandle_t smbsrv_mod;
27634543Smarks kmutex_t zfs_share_lock;
27644543Smarks 
27654543Smarks static int
27665331Samw zfs_init_sharefs()
27675331Samw {
27685331Samw 	int error;
27695331Samw 
27705331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
27715331Samw 	/* Both NFS and SMB shares also require sharetab support. */
27725331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
27735331Samw 	    ddi_modopen("fs/sharefs",
27745331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
27755331Samw 		return (ENOSYS);
27765331Samw 	}
27775331Samw 	if (zshare_fs == NULL && ((zshare_fs =
27785331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
27795331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
27805331Samw 		return (ENOSYS);
27815331Samw 	}
27825331Samw 	return (0);
27835331Samw }
27845331Samw 
27855331Samw static int
27864543Smarks zfs_ioc_share(zfs_cmd_t *zc)
27874543Smarks {
27884543Smarks 	int error;
27894543Smarks 	int opcode;
27904543Smarks 
27915331Samw 	switch (zc->zc_share.z_sharetype) {
27925331Samw 	case ZFS_SHARE_NFS:
27935331Samw 	case ZFS_UNSHARE_NFS:
27945331Samw 		if (zfs_nfsshare_inited == 0) {
27955331Samw 			mutex_enter(&zfs_share_lock);
27965331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
27975331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
27985331Samw 				mutex_exit(&zfs_share_lock);
27995331Samw 				return (ENOSYS);
28005331Samw 			}
28015331Samw 			if (znfsexport_fs == NULL &&
28025331Samw 			    ((znfsexport_fs = (int (*)(void *))
28035331Samw 			    ddi_modsym(nfs_mod,
28045331Samw 			    "nfs_export", &error)) == NULL)) {
28055331Samw 				mutex_exit(&zfs_share_lock);
28065331Samw 				return (ENOSYS);
28075331Samw 			}
28085331Samw 			error = zfs_init_sharefs();
28095331Samw 			if (error) {
28105331Samw 				mutex_exit(&zfs_share_lock);
28115331Samw 				return (ENOSYS);
28125331Samw 			}
28135331Samw 			zfs_nfsshare_inited = 1;
28144543Smarks 			mutex_exit(&zfs_share_lock);
28154543Smarks 		}
28165331Samw 		break;
28175331Samw 	case ZFS_SHARE_SMB:
28185331Samw 	case ZFS_UNSHARE_SMB:
28195331Samw 		if (zfs_smbshare_inited == 0) {
28205331Samw 			mutex_enter(&zfs_share_lock);
28215331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
28225331Samw 			    ddi_modopen("drv/smbsrv",
28235331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
28245331Samw 				mutex_exit(&zfs_share_lock);
28255331Samw 				return (ENOSYS);
28265331Samw 			}
28275331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
28285331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
28296139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
28305331Samw 				mutex_exit(&zfs_share_lock);
28315331Samw 				return (ENOSYS);
28325331Samw 			}
28335331Samw 			error = zfs_init_sharefs();
28345331Samw 			if (error) {
28355331Samw 				mutex_exit(&zfs_share_lock);
28365331Samw 				return (ENOSYS);
28375331Samw 			}
28385331Samw 			zfs_smbshare_inited = 1;
28394543Smarks 			mutex_exit(&zfs_share_lock);
28404543Smarks 		}
28415331Samw 		break;
28425331Samw 	default:
28435331Samw 		return (EINVAL);
28444543Smarks 	}
28454543Smarks 
28465331Samw 	switch (zc->zc_share.z_sharetype) {
28475331Samw 	case ZFS_SHARE_NFS:
28485331Samw 	case ZFS_UNSHARE_NFS:
28495331Samw 		if (error =
28505331Samw 		    znfsexport_fs((void *)
28515331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
28525331Samw 			return (error);
28535331Samw 		break;
28545331Samw 	case ZFS_SHARE_SMB:
28555331Samw 	case ZFS_UNSHARE_SMB:
28565331Samw 		if (error = zsmbexport_fs((void *)
28575331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
28585331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
28595331Samw 		    B_TRUE : B_FALSE)) {
28605331Samw 			return (error);
28615331Samw 		}
28625331Samw 		break;
28635331Samw 	}
28645331Samw 
28655331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
28665331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
28674543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
28684543Smarks 
28695331Samw 	/*
28705331Samw 	 * Add or remove share from sharetab
28715331Samw 	 */
28724543Smarks 	error = zshare_fs(opcode,
28734543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
28744543Smarks 	    zc->zc_share.z_sharemax);
28754543Smarks 
28764543Smarks 	return (error);
28774543Smarks 
28784543Smarks }
28794543Smarks 
28804543Smarks /*
28814988Sek110237  * pool create, destroy, and export don't log the history as part of
28824988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
28834988Sek110237  * do the logging of those commands.
28844543Smarks  */
2885789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
28864715Sek110237 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
28874577Sahrens 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
28884577Sahrens 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
28894577Sahrens 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
28904577Sahrens 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
28914577Sahrens 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
28924577Sahrens 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
28934577Sahrens 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
28944577Sahrens 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
28954577Sahrens 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
28964577Sahrens 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
28974577Sahrens 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
28984577Sahrens 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
28994577Sahrens 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
29004577Sahrens 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29014577Sahrens 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29024577Sahrens 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
29034577Sahrens 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29045498Stimh 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29054543Smarks 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
29064577Sahrens 	    DATASET_NAME, B_FALSE },
29074543Smarks 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
29084577Sahrens 	    DATASET_NAME, B_FALSE },
29094577Sahrens 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
29104577Sahrens 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
29114577Sahrens 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
29124577Sahrens 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
29134577Sahrens 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
29144577Sahrens 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
29154577Sahrens 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
29165367Sahrens 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
29175367Sahrens 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
29184577Sahrens 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
29194577Sahrens 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
29204577Sahrens 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
29214577Sahrens 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
29224577Sahrens 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
29234577Sahrens 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
29244577Sahrens 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
29254577Sahrens 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
29264577Sahrens 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
29274577Sahrens 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
29284577Sahrens 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
29294577Sahrens 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
29304577Sahrens 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
29314577Sahrens 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
29324543Smarks 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
29334577Sahrens 	    DATASET_NAME, B_FALSE },
29344849Sahrens 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
29354849Sahrens 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2936789Sahrens };
2937789Sahrens 
2938789Sahrens static int
2939789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2940789Sahrens {
2941789Sahrens 	zfs_cmd_t *zc;
2942789Sahrens 	uint_t vec;
29432199Sahrens 	int error, rc;
2944789Sahrens 
2945789Sahrens 	if (getminor(dev) != 0)
2946789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2947789Sahrens 
2948789Sahrens 	vec = cmd - ZFS_IOC;
29494787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2950789Sahrens 
2951789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2952789Sahrens 		return (EINVAL);
2953789Sahrens 
2954789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2955789Sahrens 
2956789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2957789Sahrens 
29584787Sahrens 	if (error == 0)
29594543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2960789Sahrens 
2961789Sahrens 	/*
2962789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
2963789Sahrens 	 * the lower layers.
2964789Sahrens 	 */
2965789Sahrens 	if (error == 0) {
2966789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2967789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
29684577Sahrens 		case POOL_NAME:
2969789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2970789Sahrens 				error = EINVAL;
2971789Sahrens 			break;
2972789Sahrens 
29734577Sahrens 		case DATASET_NAME:
2974789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2975789Sahrens 				error = EINVAL;
2976789Sahrens 			break;
29772856Snd150628 
29784577Sahrens 		case NO_NAME:
29792856Snd150628 			break;
2980789Sahrens 		}
2981789Sahrens 	}
2982789Sahrens 
2983789Sahrens 	if (error == 0)
2984789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
2985789Sahrens 
29862199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
29874543Smarks 	if (error == 0) {
29882199Sahrens 		error = rc;
29894543Smarks 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
29904543Smarks 			zfs_log_history(zc);
29914543Smarks 	}
2992789Sahrens 
2993789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
2994789Sahrens 	return (error);
2995789Sahrens }
2996789Sahrens 
2997789Sahrens static int
2998789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2999789Sahrens {
3000789Sahrens 	if (cmd != DDI_ATTACH)
3001789Sahrens 		return (DDI_FAILURE);
3002789Sahrens 
3003789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3004789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3005789Sahrens 		return (DDI_FAILURE);
3006789Sahrens 
3007789Sahrens 	zfs_dip = dip;
3008789Sahrens 
3009789Sahrens 	ddi_report_dev(dip);
3010789Sahrens 
3011789Sahrens 	return (DDI_SUCCESS);
3012789Sahrens }
3013789Sahrens 
3014789Sahrens static int
3015789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3016789Sahrens {
3017789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3018789Sahrens 		return (DDI_FAILURE);
3019789Sahrens 
3020789Sahrens 	if (cmd != DDI_DETACH)
3021789Sahrens 		return (DDI_FAILURE);
3022789Sahrens 
3023789Sahrens 	zfs_dip = NULL;
3024789Sahrens 
3025789Sahrens 	ddi_prop_remove_all(dip);
3026789Sahrens 	ddi_remove_minor_node(dip, NULL);
3027789Sahrens 
3028789Sahrens 	return (DDI_SUCCESS);
3029789Sahrens }
3030789Sahrens 
3031789Sahrens /*ARGSUSED*/
3032789Sahrens static int
3033789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3034789Sahrens {
3035789Sahrens 	switch (infocmd) {
3036789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3037789Sahrens 		*result = zfs_dip;
3038789Sahrens 		return (DDI_SUCCESS);
3039789Sahrens 
3040789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3041849Sbonwick 		*result = (void *)0;
3042789Sahrens 		return (DDI_SUCCESS);
3043789Sahrens 	}
3044789Sahrens 
3045789Sahrens 	return (DDI_FAILURE);
3046789Sahrens }
3047789Sahrens 
3048789Sahrens /*
3049789Sahrens  * OK, so this is a little weird.
3050789Sahrens  *
3051789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3052789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3053789Sahrens  *
3054789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3055789Sahrens  * so most of the standard driver entry points are in zvol.c.
3056789Sahrens  */
3057789Sahrens static struct cb_ops zfs_cb_ops = {
3058789Sahrens 	zvol_open,	/* open */
3059789Sahrens 	zvol_close,	/* close */
3060789Sahrens 	zvol_strategy,	/* strategy */
3061789Sahrens 	nodev,		/* print */
30626423Sgw25295 	zvol_dump,	/* dump */
3063789Sahrens 	zvol_read,	/* read */
3064789Sahrens 	zvol_write,	/* write */
3065789Sahrens 	zfsdev_ioctl,	/* ioctl */
3066789Sahrens 	nodev,		/* devmap */
3067789Sahrens 	nodev,		/* mmap */
3068789Sahrens 	nodev,		/* segmap */
3069789Sahrens 	nochpoll,	/* poll */
3070789Sahrens 	ddi_prop_op,	/* prop_op */
3071789Sahrens 	NULL,		/* streamtab */
3072789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3073789Sahrens 	CB_REV,		/* version */
30743638Sbillm 	nodev,		/* async read */
30753638Sbillm 	nodev,		/* async write */
3076789Sahrens };
3077789Sahrens 
3078789Sahrens static struct dev_ops zfs_dev_ops = {
3079789Sahrens 	DEVO_REV,	/* version */
3080789Sahrens 	0,		/* refcnt */
3081789Sahrens 	zfs_info,	/* info */
3082789Sahrens 	nulldev,	/* identify */
3083789Sahrens 	nulldev,	/* probe */
3084789Sahrens 	zfs_attach,	/* attach */
3085789Sahrens 	zfs_detach,	/* detach */
3086789Sahrens 	nodev,		/* reset */
3087789Sahrens 	&zfs_cb_ops,	/* driver operations */
3088*7656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
3089*7656SSherry.Moore@Sun.COM 	NULL,		/* power */
3090*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3091789Sahrens };
3092789Sahrens 
3093789Sahrens static struct modldrv zfs_modldrv = {
3094*7656SSherry.Moore@Sun.COM 	&mod_driverops,
3095*7656SSherry.Moore@Sun.COM 	"ZFS storage pool",
3096*7656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3097789Sahrens };
3098789Sahrens 
3099789Sahrens static struct modlinkage modlinkage = {
3100789Sahrens 	MODREV_1,
3101789Sahrens 	(void *)&zfs_modlfs,
3102789Sahrens 	(void *)&zfs_modldrv,
3103789Sahrens 	NULL
3104789Sahrens };
3105789Sahrens 
31064720Sfr157268 
31074720Sfr157268 uint_t zfs_fsyncer_key;
31085326Sek110237 extern uint_t rrw_tsd_key;
31094720Sfr157268 
3110789Sahrens int
3111789Sahrens _init(void)
3112789Sahrens {
3113789Sahrens 	int error;
3114789Sahrens 
3115849Sbonwick 	spa_init(FREAD | FWRITE);
3116849Sbonwick 	zfs_init();
3117849Sbonwick 	zvol_init();
3118849Sbonwick 
3119849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3120849Sbonwick 		zvol_fini();
3121849Sbonwick 		zfs_fini();
3122849Sbonwick 		spa_fini();
3123789Sahrens 		return (error);
3124849Sbonwick 	}
3125789Sahrens 
31264720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
31275326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
31284720Sfr157268 
3129789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3130789Sahrens 	ASSERT(error == 0);
31314543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3132789Sahrens 
3133789Sahrens 	return (0);
3134789Sahrens }
3135789Sahrens 
3136789Sahrens int
3137789Sahrens _fini(void)
3138789Sahrens {
3139789Sahrens 	int error;
3140789Sahrens 
31411544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3142789Sahrens 		return (EBUSY);
3143789Sahrens 
3144789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3145789Sahrens 		return (error);
3146789Sahrens 
3147789Sahrens 	zvol_fini();
3148789Sahrens 	zfs_fini();
3149789Sahrens 	spa_fini();
31505331Samw 	if (zfs_nfsshare_inited)
31514543Smarks 		(void) ddi_modclose(nfs_mod);
31525331Samw 	if (zfs_smbshare_inited)
31535331Samw 		(void) ddi_modclose(smbsrv_mod);
31545331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
31554543Smarks 		(void) ddi_modclose(sharefs_mod);
3156789Sahrens 
31574720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3158789Sahrens 	ldi_ident_release(zfs_li);
3159789Sahrens 	zfs_li = NULL;
31604543Smarks 	mutex_destroy(&zfs_share_lock);
3161789Sahrens 
3162789Sahrens 	return (error);
3163789Sahrens }
3164789Sahrens 
3165789Sahrens int
3166789Sahrens _info(struct modinfo *modinfop)
3167789Sahrens {
3168789Sahrens 	return (mod_info(&modlinkage, modinfop));
3169789Sahrens }
3170