xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 9234:bffdc4fc05c4)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
228525SEric.Schrock@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/errno.h>
29789Sahrens #include <sys/uio.h>
30789Sahrens #include <sys/buf.h>
31789Sahrens #include <sys/modctl.h>
32789Sahrens #include <sys/open.h>
33789Sahrens #include <sys/file.h>
34789Sahrens #include <sys/kmem.h>
35789Sahrens #include <sys/conf.h>
36789Sahrens #include <sys/cmn_err.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/zfs_ioctl.h>
395331Samw #include <sys/zfs_znode.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/spa.h>
423912Slling #include <sys/spa_impl.h>
43789Sahrens #include <sys/vdev.h>
443912Slling #include <sys/vdev_impl.h>
45789Sahrens #include <sys/dmu.h>
46789Sahrens #include <sys/dsl_dir.h>
47789Sahrens #include <sys/dsl_dataset.h>
48789Sahrens #include <sys/dsl_prop.h>
494543Smarks #include <sys/dsl_deleg.h>
504543Smarks #include <sys/dmu_objset.h>
51789Sahrens #include <sys/ddi.h>
52789Sahrens #include <sys/sunddi.h>
53789Sahrens #include <sys/sunldi.h>
54789Sahrens #include <sys/policy.h>
55789Sahrens #include <sys/zone.h>
56789Sahrens #include <sys/nvpair.h>
57789Sahrens #include <sys/pathname.h>
58789Sahrens #include <sys/mount.h>
59789Sahrens #include <sys/sdt.h>
60789Sahrens #include <sys/fs/zfs.h>
61789Sahrens #include <sys/zfs_ctldir.h>
625331Samw #include <sys/zfs_dir.h>
632885Sahrens #include <sys/zvol.h>
644543Smarks #include <sharefs/share.h>
655326Sek110237 #include <sys/dmu_objset.h>
66789Sahrens 
67789Sahrens #include "zfs_namecheck.h"
682676Seschrock #include "zfs_prop.h"
694543Smarks #include "zfs_deleg.h"
70789Sahrens 
71789Sahrens extern struct modlfs zfs_modlfs;
72789Sahrens 
73789Sahrens extern void zfs_init(void);
74789Sahrens extern void zfs_fini(void);
75789Sahrens 
76789Sahrens ldi_ident_t zfs_li = NULL;
77789Sahrens dev_info_t *zfs_dip;
78789Sahrens 
79789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
804543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
81789Sahrens 
82*9234SGeorge.Wilson@Sun.COM typedef enum {
83*9234SGeorge.Wilson@Sun.COM 	NO_NAME,
84*9234SGeorge.Wilson@Sun.COM 	POOL_NAME,
85*9234SGeorge.Wilson@Sun.COM 	DATASET_NAME
86*9234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
87*9234SGeorge.Wilson@Sun.COM 
88789Sahrens typedef struct zfs_ioc_vec {
89789Sahrens 	zfs_ioc_func_t		*zvec_func;
90789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
91*9234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
924543Smarks 	boolean_t		zvec_his_log;
93*9234SGeorge.Wilson@Sun.COM 	boolean_t		zvec_pool_check;
94789Sahrens } zfs_ioc_vec_t;
95789Sahrens 
968536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
977184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
987184Stimh     boolean_t *);
997184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
1007184Stimh 
101789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
102789Sahrens void
103789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
104789Sahrens {
105789Sahrens 	const char *newfile;
106789Sahrens 	char buf[256];
107789Sahrens 	va_list adx;
108789Sahrens 
109789Sahrens 	/*
110789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
111789Sahrens 	 */
112789Sahrens 	newfile = strrchr(file, '/');
113789Sahrens 	if (newfile != NULL) {
114789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
115789Sahrens 	} else {
116789Sahrens 		newfile = file;
117789Sahrens 	}
118789Sahrens 
119789Sahrens 	va_start(adx, fmt);
120789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
121789Sahrens 	va_end(adx);
122789Sahrens 
123789Sahrens 	/*
124789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
125789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
126789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
127789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
128789Sahrens 	 * arg0 = file name
129789Sahrens 	 * arg1 = function name
130789Sahrens 	 * arg2 = line number
131789Sahrens 	 * arg3 = message
132789Sahrens 	 */
133789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
134789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
135789Sahrens }
136789Sahrens 
1374543Smarks static void
1384715Sek110237 history_str_free(char *buf)
1394715Sek110237 {
1404715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1414715Sek110237 }
1424715Sek110237 
1434715Sek110237 static char *
1444715Sek110237 history_str_get(zfs_cmd_t *zc)
1454715Sek110237 {
1464715Sek110237 	char *buf;
1474715Sek110237 
1484715Sek110237 	if (zc->zc_history == NULL)
1494715Sek110237 		return (NULL);
1504715Sek110237 
1514715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1524715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1534715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1544715Sek110237 		history_str_free(buf);
1554715Sek110237 		return (NULL);
1564715Sek110237 	}
1574715Sek110237 
1584715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1594715Sek110237 
1604715Sek110237 	return (buf);
1614715Sek110237 }
1624715Sek110237 
1635375Stimh /*
1647042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1657042Sgw25295  */
1667042Sgw25295 static boolean_t
1677042Sgw25295 zfs_is_bootfs(const char *name)
1687042Sgw25295 {
1697042Sgw25295 	spa_t *spa;
1707042Sgw25295 	boolean_t ret = B_FALSE;
1717042Sgw25295 
1727042Sgw25295 	if (spa_open(name, &spa, FTAG) == 0) {
1737042Sgw25295 		if (spa->spa_bootfs) {
1747042Sgw25295 			objset_t *os;
1757042Sgw25295 
1767042Sgw25295 			if (dmu_objset_open(name, DMU_OST_ZFS,
1777042Sgw25295 			    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1787042Sgw25295 				ret = (dmu_objset_id(os) == spa->spa_bootfs);
1797042Sgw25295 				dmu_objset_close(os);
1807042Sgw25295 			}
1817042Sgw25295 		}
1827042Sgw25295 		spa_close(spa, FTAG);
1837042Sgw25295 	}
1847042Sgw25295 	return (ret);
1857042Sgw25295 }
1867042Sgw25295 
1877042Sgw25295 /*
1887184Stimh  * zfs_earlier_version
1895375Stimh  *
1905375Stimh  *	Return non-zero if the spa version is less than requested version.
1915375Stimh  */
1925331Samw static int
1937184Stimh zfs_earlier_version(const char *name, int version)
1945331Samw {
1955331Samw 	spa_t *spa;
1965331Samw 
1975331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
1985331Samw 		if (spa_version(spa) < version) {
1995331Samw 			spa_close(spa, FTAG);
2005331Samw 			return (1);
2015331Samw 		}
2025331Samw 		spa_close(spa, FTAG);
2035331Samw 	}
2045331Samw 	return (0);
2055331Samw }
2065331Samw 
2075977Smarks /*
2086689Smaybee  * zpl_earlier_version
2095977Smarks  *
2106689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2115977Smarks  */
2126689Smaybee static boolean_t
2136689Smaybee zpl_earlier_version(const char *name, int version)
2145977Smarks {
2155977Smarks 	objset_t *os;
2166689Smaybee 	boolean_t rc = B_TRUE;
2175977Smarks 
2185977Smarks 	if (dmu_objset_open(name, DMU_OST_ANY,
2196689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2206689Smaybee 		uint64_t zplversion;
2216689Smaybee 
2226689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2236689Smaybee 			rc = zplversion < version;
2245977Smarks 		dmu_objset_close(os);
2255977Smarks 	}
2265977Smarks 	return (rc);
2275977Smarks }
2285977Smarks 
2294715Sek110237 static void
2304543Smarks zfs_log_history(zfs_cmd_t *zc)
2314543Smarks {
2324543Smarks 	spa_t *spa;
2334603Sahrens 	char *buf;
2344543Smarks 
2354715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2364577Sahrens 		return;
2374577Sahrens 
2384715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2394715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2404715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2414715Sek110237 		spa_close(spa, FTAG);
2424543Smarks 	}
2434715Sek110237 	history_str_free(buf);
2444543Smarks }
2454543Smarks 
246789Sahrens /*
247789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
248789Sahrens  * and can be used in the local zone, as there is no associated dataset.
249789Sahrens  */
250789Sahrens /* ARGSUSED */
251789Sahrens static int
2524543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
253789Sahrens {
254789Sahrens 	return (0);
255789Sahrens }
256789Sahrens 
257789Sahrens /*
258789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
259789Sahrens  * no privileges, but must be visible in the local zone.
260789Sahrens  */
261789Sahrens /* ARGSUSED */
262789Sahrens static int
2634543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
264789Sahrens {
265789Sahrens 	if (INGLOBALZONE(curproc) ||
2664543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
267789Sahrens 		return (0);
268789Sahrens 
269789Sahrens 	return (ENOENT);
270789Sahrens }
271789Sahrens 
272789Sahrens static int
273789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
274789Sahrens {
275789Sahrens 	uint64_t zoned;
276789Sahrens 	int writable = 1;
277789Sahrens 
278789Sahrens 	/*
279789Sahrens 	 * The dataset must be visible by this zone -- check this first
280789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
281789Sahrens 	 */
282789Sahrens 	if (!INGLOBALZONE(curproc) &&
283789Sahrens 	    !zone_dataset_visible(dataset, &writable))
284789Sahrens 		return (ENOENT);
285789Sahrens 
286789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
287789Sahrens 		return (ENOENT);
288789Sahrens 
289789Sahrens 	if (INGLOBALZONE(curproc)) {
290789Sahrens 		/*
291789Sahrens 		 * If the fs is zoned, only root can access it from the
292789Sahrens 		 * global zone.
293789Sahrens 		 */
294789Sahrens 		if (secpolicy_zfs(cr) && zoned)
295789Sahrens 			return (EPERM);
296789Sahrens 	} else {
297789Sahrens 		/*
298789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
299789Sahrens 		 */
300789Sahrens 		if (!zoned)
301789Sahrens 			return (EPERM);
302789Sahrens 
303789Sahrens 		/* must be writable by this zone */
304789Sahrens 		if (!writable)
305789Sahrens 			return (EPERM);
306789Sahrens 	}
307789Sahrens 	return (0);
308789Sahrens }
309789Sahrens 
310789Sahrens int
3114543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
312789Sahrens {
313789Sahrens 	int error;
314789Sahrens 
3154543Smarks 	error = zfs_dozonecheck(name, cr);
3164543Smarks 	if (error == 0) {
3174543Smarks 		error = secpolicy_zfs(cr);
3184670Sahrens 		if (error)
3194543Smarks 			error = dsl_deleg_access(name, perm, cr);
3204543Smarks 	}
3214543Smarks 	return (error);
3224543Smarks }
3234543Smarks 
3244543Smarks static int
3254543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3264543Smarks {
3274543Smarks 	/*
3284543Smarks 	 * Check permissions for special properties.
3294543Smarks 	 */
3304543Smarks 	switch (prop) {
3314543Smarks 	case ZFS_PROP_ZONED:
3324543Smarks 		/*
3334543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3344543Smarks 		 */
3354543Smarks 		if (!INGLOBALZONE(curproc))
3364543Smarks 			return (EPERM);
3374543Smarks 		break;
338789Sahrens 
3394543Smarks 	case ZFS_PROP_QUOTA:
3404543Smarks 		if (!INGLOBALZONE(curproc)) {
3414543Smarks 			uint64_t zoned;
3424543Smarks 			char setpoint[MAXNAMELEN];
3434543Smarks 			/*
3444543Smarks 			 * Unprivileged users are allowed to modify the
3454543Smarks 			 * quota on things *under* (ie. contained by)
3464543Smarks 			 * the thing they own.
3474543Smarks 			 */
3484543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3494543Smarks 			    setpoint))
3504543Smarks 				return (EPERM);
3514670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3524543Smarks 				return (EPERM);
3534543Smarks 		}
3544670Sahrens 		break;
3554543Smarks 	}
3564543Smarks 
3574787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
358789Sahrens }
359789Sahrens 
3604543Smarks int
3614543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3624543Smarks {
3634543Smarks 	int error;
3644543Smarks 
3654543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3664543Smarks 	if (error)
3674543Smarks 		return (error);
3684543Smarks 
3694543Smarks 	/*
3704543Smarks 	 * permission to set permissions will be evaluated later in
3714543Smarks 	 * dsl_deleg_can_allow()
3724543Smarks 	 */
3734543Smarks 	return (0);
3744543Smarks }
3754543Smarks 
3764543Smarks int
3774543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3784543Smarks {
3794543Smarks 	int error;
3804543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
3814543Smarks 	    ZFS_DELEG_PERM_ROLLBACK, cr);
3824543Smarks 	if (error == 0)
3834543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
3844543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
3854543Smarks 	return (error);
3864543Smarks }
3874543Smarks 
3884543Smarks int
3894543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3904543Smarks {
3914543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
3924543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
3934543Smarks }
3944543Smarks 
3958845Samw@Sun.COM static int
3968845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
3978845Samw@Sun.COM {
3988845Samw@Sun.COM 	vnode_t *vp;
3998845Samw@Sun.COM 	int error;
4008845Samw@Sun.COM 
4018845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4028845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
4038845Samw@Sun.COM 		return (error);
4048845Samw@Sun.COM 
4058845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
4068845Samw@Sun.COM 
4078845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4088845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4098845Samw@Sun.COM 	    zc->zc_name) != 0)) {
4108845Samw@Sun.COM 		VN_RELE(vp);
4118845Samw@Sun.COM 		return (EPERM);
4128845Samw@Sun.COM 	}
4138845Samw@Sun.COM 
4148845Samw@Sun.COM 	VN_RELE(vp);
4158845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
4168845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
4178845Samw@Sun.COM }
4188845Samw@Sun.COM 
4194543Smarks int
4204543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
4214543Smarks {
4224543Smarks 	if (!INGLOBALZONE(curproc))
4234543Smarks 		return (EPERM);
4244543Smarks 
4255367Sahrens 	if (secpolicy_nfs(cr) == 0) {
4264543Smarks 		return (0);
4274543Smarks 	} else {
4288845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4298845Samw@Sun.COM 	}
4308845Samw@Sun.COM }
4318845Samw@Sun.COM 
4328845Samw@Sun.COM int
4338845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
4348845Samw@Sun.COM {
4358845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
4368845Samw@Sun.COM 		return (EPERM);
4378845Samw@Sun.COM 
4388845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
4398845Samw@Sun.COM 		return (0);
4408845Samw@Sun.COM 	} else {
4418845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4424543Smarks 	}
4434543Smarks }
4444543Smarks 
445789Sahrens static int
4464543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
447789Sahrens {
448789Sahrens 	char *cp;
449789Sahrens 
450789Sahrens 	/*
451789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
452789Sahrens 	 */
4534543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4544543Smarks 	cp = strrchr(parent, '@');
455789Sahrens 	if (cp != NULL) {
456789Sahrens 		cp[0] = '\0';
457789Sahrens 	} else {
4584543Smarks 		cp = strrchr(parent, '/');
459789Sahrens 		if (cp == NULL)
460789Sahrens 			return (ENOENT);
461789Sahrens 		cp[0] = '\0';
462789Sahrens 	}
463789Sahrens 
4644543Smarks 	return (0);
4654543Smarks }
4664543Smarks 
4674543Smarks int
4684543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4694543Smarks {
4704543Smarks 	int error;
4714543Smarks 
4724543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4734543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4744543Smarks 		return (error);
4754543Smarks 
4764543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4774543Smarks }
4784543Smarks 
4794543Smarks static int
4804543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4814543Smarks {
4824543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4834543Smarks }
4844543Smarks 
4854543Smarks /*
4864543Smarks  * Must have sys_config privilege to check the iscsi permission
4874543Smarks  */
4884543Smarks /* ARGSUSED */
4894543Smarks static int
4904543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
4914543Smarks {
4924543Smarks 	return (secpolicy_zfs(cr));
4934543Smarks }
4944543Smarks 
4954543Smarks int
4964543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
4974543Smarks {
4984543Smarks 	char 	parentname[MAXNAMELEN];
4994543Smarks 	int	error;
5004543Smarks 
5014543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5024543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
5034543Smarks 		return (error);
5044543Smarks 
5054543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5064543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5074543Smarks 		return (error);
5084543Smarks 
5094543Smarks 	if ((error = zfs_get_parent(to, parentname,
5104543Smarks 	    sizeof (parentname))) != 0)
5114543Smarks 		return (error);
5124543Smarks 
5134543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5144543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
5154543Smarks 		return (error);
5164543Smarks 
5174543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5184543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5194543Smarks 		return (error);
5204543Smarks 
5214543Smarks 	return (error);
5224543Smarks }
5234543Smarks 
5244543Smarks static int
5254543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5264543Smarks {
5274543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5284543Smarks }
5294543Smarks 
5304543Smarks static int
5314543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5324543Smarks {
5334543Smarks 	char 	parentname[MAXNAMELEN];
5344543Smarks 	objset_t *clone;
5354543Smarks 	int error;
5364543Smarks 
5374543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5384543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5394543Smarks 	if (error)
5404543Smarks 		return (error);
5414543Smarks 
5424543Smarks 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
5436689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &clone);
5444543Smarks 
5454543Smarks 	if (error == 0) {
5464543Smarks 		dsl_dataset_t *pclone = NULL;
5474543Smarks 		dsl_dir_t *dd;
5484543Smarks 		dd = clone->os->os_dsl_dataset->ds_dir;
5494543Smarks 
5504543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5516689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5526689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5534543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5544543Smarks 		if (error) {
5554543Smarks 			dmu_objset_close(clone);
5564543Smarks 			return (error);
5574543Smarks 		}
5584543Smarks 
5594543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5604543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5614543Smarks 
5624543Smarks 		dsl_dataset_name(pclone, parentname);
5634543Smarks 		dmu_objset_close(clone);
5646689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5654543Smarks 		if (error == 0)
5664543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5674543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5684543Smarks 	}
5694543Smarks 	return (error);
5704543Smarks }
5714543Smarks 
5724543Smarks static int
5734543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5744543Smarks {
5754543Smarks 	int error;
5764543Smarks 
5774543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5784543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5794543Smarks 		return (error);
5804543Smarks 
5814543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5824543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5834543Smarks 		return (error);
5844543Smarks 
5854543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5864543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5874543Smarks }
5884543Smarks 
5894543Smarks int
5904543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
5914543Smarks {
5924543Smarks 	int error;
5934543Smarks 
5944543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
5954543Smarks 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
5964543Smarks 		return (error);
5974543Smarks 
5984543Smarks 	error = zfs_secpolicy_write_perms(name,
5994543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6004543Smarks 
6014543Smarks 	return (error);
6024543Smarks }
6034543Smarks 
6044543Smarks static int
6054543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
6064543Smarks {
6074543Smarks 
6084543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
6094543Smarks }
6104543Smarks 
6114543Smarks static int
6124543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
6134543Smarks {
6144543Smarks 	char 	parentname[MAXNAMELEN];
6154543Smarks 	int 	error;
6164543Smarks 
6174543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
6184543Smarks 	    sizeof (parentname))) != 0)
6194543Smarks 		return (error);
6204543Smarks 
6214543Smarks 	if (zc->zc_value[0] != '\0') {
6224543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6234543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6244543Smarks 			return (error);
6254543Smarks 	}
6264543Smarks 
6274543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6284543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6294543Smarks 		return (error);
6304543Smarks 
6314543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6324543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6334543Smarks 
6344543Smarks 	return (error);
6354543Smarks }
6364543Smarks 
6374543Smarks static int
6384543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6394543Smarks {
6404543Smarks 	int error;
6414543Smarks 
6424543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6434543Smarks 	if (error) {
6444543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6454543Smarks 	}
6464543Smarks 	return (error);
647789Sahrens }
648789Sahrens 
649789Sahrens /*
650789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
651789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
652789Sahrens  */
653789Sahrens /* ARGSUSED */
654789Sahrens static int
6554543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
656789Sahrens {
657789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
658789Sahrens 		return (EPERM);
659789Sahrens 
660789Sahrens 	return (0);
661789Sahrens }
662789Sahrens 
663789Sahrens /*
6644543Smarks  * Just like zfs_secpolicy_config, except that we will check for
6654543Smarks  * mount permission on the dataset for permission to create/remove
6664543Smarks  * the minor nodes.
6674543Smarks  */
6684543Smarks static int
6694543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
6704543Smarks {
6714543Smarks 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
6724543Smarks 		return (dsl_deleg_access(zc->zc_name,
6734543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr));
6744543Smarks 	}
6754543Smarks 
6764543Smarks 	return (0);
6774543Smarks }
6784543Smarks 
6794543Smarks /*
6801544Seschrock  * Policy for fault injection.  Requires all privileges.
6811544Seschrock  */
6821544Seschrock /* ARGSUSED */
6831544Seschrock static int
6844543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6851544Seschrock {
6861544Seschrock 	return (secpolicy_zinject(cr));
6871544Seschrock }
6881544Seschrock 
6894849Sahrens static int
6904849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
6914849Sahrens {
6924849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
6934849Sahrens 
6945094Slling 	if (prop == ZPROP_INVAL) {
6954849Sahrens 		if (!zfs_prop_user(zc->zc_value))
6964849Sahrens 			return (EINVAL);
6974849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
6984849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
6994849Sahrens 	} else {
7004849Sahrens 		if (!zfs_prop_inheritable(prop))
7014849Sahrens 			return (EINVAL);
7024849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
7034849Sahrens 	}
7044849Sahrens }
7054849Sahrens 
7061544Seschrock /*
707789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
708789Sahrens  */
709789Sahrens static int
7105094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
711789Sahrens {
712789Sahrens 	char *packed;
713789Sahrens 	int error;
7145094Slling 	nvlist_t *list = NULL;
715789Sahrens 
716789Sahrens 	/*
7172676Seschrock 	 * Read in and unpack the user-supplied nvlist.
718789Sahrens 	 */
7195094Slling 	if (size == 0)
720789Sahrens 		return (EINVAL);
721789Sahrens 
722789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
723789Sahrens 
7245094Slling 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
725789Sahrens 		kmem_free(packed, size);
726789Sahrens 		return (error);
727789Sahrens 	}
728789Sahrens 
7295094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
730789Sahrens 		kmem_free(packed, size);
731789Sahrens 		return (error);
732789Sahrens 	}
733789Sahrens 
734789Sahrens 	kmem_free(packed, size);
735789Sahrens 
7365094Slling 	*nvp = list;
737789Sahrens 	return (0);
738789Sahrens }
739789Sahrens 
740789Sahrens static int
7412676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
7422676Seschrock {
7432676Seschrock 	char *packed = NULL;
7442676Seschrock 	size_t size;
7452676Seschrock 	int error;
7462676Seschrock 
7472676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
7482676Seschrock 
7492676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
7502676Seschrock 		error = ENOMEM;
7512676Seschrock 	} else {
7524611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
7532676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
7542676Seschrock 		    KM_SLEEP) == 0);
7552676Seschrock 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
7562676Seschrock 		    size);
7572676Seschrock 		kmem_free(packed, size);
7582676Seschrock 	}
7592676Seschrock 
7602676Seschrock 	zc->zc_nvlist_dst_size = size;
7612676Seschrock 	return (error);
7622676Seschrock }
7632676Seschrock 
7642676Seschrock static int
765789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
766789Sahrens {
767789Sahrens 	int error;
7685094Slling 	nvlist_t *config, *props = NULL;
7697184Stimh 	nvlist_t *rootprops = NULL;
7707184Stimh 	nvlist_t *zplprops = NULL;
7714715Sek110237 	char *buf;
772789Sahrens 
7735094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
7745094Slling 	    &config))
7754988Sek110237 		return (error);
7764715Sek110237 
7775094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
7785094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
7795094Slling 		nvlist_free(config);
7805094Slling 		return (error);
7815094Slling 	}
7825094Slling 
7837184Stimh 	if (props) {
7847184Stimh 		nvlist_t *nvl = NULL;
7857184Stimh 		uint64_t version = SPA_VERSION;
7867184Stimh 
7877184Stimh 		(void) nvlist_lookup_uint64(props,
7887184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
7897184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
7907184Stimh 			error = EINVAL;
7917184Stimh 			goto pool_props_bad;
7927184Stimh 		}
7937184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
7947184Stimh 		if (nvl) {
7957184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
7967184Stimh 			if (error != 0) {
7977184Stimh 				nvlist_free(config);
7987184Stimh 				nvlist_free(props);
7997184Stimh 				return (error);
8007184Stimh 			}
8017184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
8027184Stimh 		}
8037184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
8047184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
8057184Stimh 		    zplprops, NULL);
8067184Stimh 		if (error)
8077184Stimh 			goto pool_props_bad;
8087184Stimh 	}
8097184Stimh 
8104988Sek110237 	buf = history_str_get(zc);
811789Sahrens 
8127184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
8137184Stimh 
8147184Stimh 	/*
8157184Stimh 	 * Set the remaining root properties
8167184Stimh 	 */
8177184Stimh 	if (!error &&
8187184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
8197184Stimh 		(void) spa_destroy(zc->zc_name);
820789Sahrens 
8214988Sek110237 	if (buf != NULL)
8224988Sek110237 		history_str_free(buf);
8235094Slling 
8247184Stimh pool_props_bad:
8257184Stimh 	nvlist_free(rootprops);
8267184Stimh 	nvlist_free(zplprops);
827789Sahrens 	nvlist_free(config);
8287184Stimh 	nvlist_free(props);
8295094Slling 
830789Sahrens 	return (error);
831789Sahrens }
832789Sahrens 
833789Sahrens static int
834789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
835789Sahrens {
8364543Smarks 	int error;
8374543Smarks 	zfs_log_history(zc);
8384543Smarks 	error = spa_destroy(zc->zc_name);
8394543Smarks 	return (error);
840789Sahrens }
841789Sahrens 
842789Sahrens static int
843789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
844789Sahrens {
845789Sahrens 	int error;
8465094Slling 	nvlist_t *config, *props = NULL;
847789Sahrens 	uint64_t guid;
848789Sahrens 
8495094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8505094Slling 	    &config)) != 0)
851789Sahrens 		return (error);
852789Sahrens 
8535094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8545094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
8555094Slling 		nvlist_free(config);
8565094Slling 		return (error);
8575094Slling 	}
8585094Slling 
859789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
8601544Seschrock 	    guid != zc->zc_guid)
861789Sahrens 		error = EINVAL;
8626643Seschrock 	else if (zc->zc_cookie)
8636643Seschrock 		error = spa_import_faulted(zc->zc_name, config,
8646643Seschrock 		    props);
865789Sahrens 	else
8665094Slling 		error = spa_import(zc->zc_name, config, props);
867789Sahrens 
868789Sahrens 	nvlist_free(config);
869789Sahrens 
8705094Slling 	if (props)
8715094Slling 		nvlist_free(props);
8725094Slling 
873789Sahrens 	return (error);
874789Sahrens }
875789Sahrens 
876789Sahrens static int
877789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
878789Sahrens {
8794543Smarks 	int error;
8807214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
8818211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
8827214Slling 
8834543Smarks 	zfs_log_history(zc);
8848211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
8854543Smarks 	return (error);
886789Sahrens }
887789Sahrens 
888789Sahrens static int
889789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
890789Sahrens {
891789Sahrens 	nvlist_t *configs;
892789Sahrens 	int error;
893789Sahrens 
894789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
895789Sahrens 		return (EEXIST);
896789Sahrens 
8972676Seschrock 	error = put_nvlist(zc, configs);
898789Sahrens 
899789Sahrens 	nvlist_free(configs);
900789Sahrens 
901789Sahrens 	return (error);
902789Sahrens }
903789Sahrens 
904789Sahrens static int
905789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
906789Sahrens {
907789Sahrens 	nvlist_t *config;
908789Sahrens 	int error;
9091544Seschrock 	int ret = 0;
910789Sahrens 
9112676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
9122676Seschrock 	    sizeof (zc->zc_value));
913789Sahrens 
914789Sahrens 	if (config != NULL) {
9152676Seschrock 		ret = put_nvlist(zc, config);
916789Sahrens 		nvlist_free(config);
9171544Seschrock 
9181544Seschrock 		/*
9191544Seschrock 		 * The config may be present even if 'error' is non-zero.
9201544Seschrock 		 * In this case we return success, and preserve the real errno
9211544Seschrock 		 * in 'zc_cookie'.
9221544Seschrock 		 */
9231544Seschrock 		zc->zc_cookie = error;
924789Sahrens 	} else {
9251544Seschrock 		ret = error;
926789Sahrens 	}
927789Sahrens 
9281544Seschrock 	return (ret);
929789Sahrens }
930789Sahrens 
931789Sahrens /*
932789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
933789Sahrens  * user land knows which devices are available and overall pool health.
934789Sahrens  */
935789Sahrens static int
936789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
937789Sahrens {
938789Sahrens 	nvlist_t *tryconfig, *config;
939789Sahrens 	int error;
940789Sahrens 
9415094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9425094Slling 	    &tryconfig)) != 0)
943789Sahrens 		return (error);
944789Sahrens 
945789Sahrens 	config = spa_tryimport(tryconfig);
946789Sahrens 
947789Sahrens 	nvlist_free(tryconfig);
948789Sahrens 
949789Sahrens 	if (config == NULL)
950789Sahrens 		return (EINVAL);
951789Sahrens 
9522676Seschrock 	error = put_nvlist(zc, config);
953789Sahrens 	nvlist_free(config);
954789Sahrens 
955789Sahrens 	return (error);
956789Sahrens }
957789Sahrens 
958789Sahrens static int
959789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
960789Sahrens {
961789Sahrens 	spa_t *spa;
962789Sahrens 	int error;
963789Sahrens 
9642926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
9652926Sek110237 		return (error);
9662926Sek110237 
9677046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
9682926Sek110237 
9692926Sek110237 	spa_close(spa, FTAG);
9702926Sek110237 
971789Sahrens 	return (error);
972789Sahrens }
973789Sahrens 
974789Sahrens static int
975789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
976789Sahrens {
977789Sahrens 	spa_t *spa;
978789Sahrens 	int error;
979789Sahrens 
980789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
981789Sahrens 	if (error == 0) {
982789Sahrens 		spa_freeze(spa);
983789Sahrens 		spa_close(spa, FTAG);
984789Sahrens 	}
985789Sahrens 	return (error);
986789Sahrens }
987789Sahrens 
988789Sahrens static int
9891760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
9901760Seschrock {
9911760Seschrock 	spa_t *spa;
9921760Seschrock 	int error;
9931760Seschrock 
9942926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
9952926Sek110237 		return (error);
9962926Sek110237 
9975118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
9985118Slling 		spa_close(spa, FTAG);
9995118Slling 		return (EINVAL);
10005118Slling 	}
10015118Slling 
10025094Slling 	spa_upgrade(spa, zc->zc_cookie);
10032926Sek110237 	spa_close(spa, FTAG);
10042926Sek110237 
10052926Sek110237 	return (error);
10062926Sek110237 }
10072926Sek110237 
10082926Sek110237 static int
10092926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
10102926Sek110237 {
10112926Sek110237 	spa_t *spa;
10122926Sek110237 	char *hist_buf;
10132926Sek110237 	uint64_t size;
10142926Sek110237 	int error;
10152926Sek110237 
10162926Sek110237 	if ((size = zc->zc_history_len) == 0)
10172926Sek110237 		return (EINVAL);
10182926Sek110237 
10192926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10202926Sek110237 		return (error);
10212926Sek110237 
10224577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
10233863Sek110237 		spa_close(spa, FTAG);
10243863Sek110237 		return (ENOTSUP);
10253863Sek110237 	}
10263863Sek110237 
10272926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
10282926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
10292926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
10304543Smarks 		error = xcopyout(hist_buf,
10314543Smarks 		    (char *)(uintptr_t)zc->zc_history,
10322926Sek110237 		    zc->zc_history_len);
10332926Sek110237 	}
10342926Sek110237 
10352926Sek110237 	spa_close(spa, FTAG);
10362926Sek110237 	kmem_free(hist_buf, size);
10372926Sek110237 	return (error);
10382926Sek110237 }
10392926Sek110237 
10402926Sek110237 static int
10413444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
10423444Sek110237 {
10433444Sek110237 	int error;
10443444Sek110237 
10453912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
10463444Sek110237 		return (error);
10473444Sek110237 
10483444Sek110237 	return (0);
10493444Sek110237 }
10503444Sek110237 
10513444Sek110237 static int
10523444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
10533444Sek110237 {
10543444Sek110237 	objset_t *osp;
10553444Sek110237 	int error;
10563444Sek110237 
10573444Sek110237 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
10586689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
10593444Sek110237 		return (error);
10603444Sek110237 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
10613444Sek110237 	    sizeof (zc->zc_value));
10623444Sek110237 	dmu_objset_close(osp);
10633444Sek110237 
10643444Sek110237 	return (error);
10653444Sek110237 }
10663444Sek110237 
10673444Sek110237 static int
1068789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1069789Sahrens {
1070789Sahrens 	spa_t *spa;
1071789Sahrens 	int error;
10726423Sgw25295 	nvlist_t *config, **l2cache, **spares;
10736423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1074789Sahrens 
1075789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1076789Sahrens 	if (error != 0)
1077789Sahrens 		return (error);
1078789Sahrens 
10795450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10805450Sbrendan 	    &config);
10815450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
10825450Sbrendan 	    &l2cache, &nl2cache);
10835450Sbrendan 
10846423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
10856423Sgw25295 	    &spares, &nspares);
10866423Sgw25295 
10873912Slling 	/*
10883912Slling 	 * A root pool with concatenated devices is not supported.
10896423Sgw25295 	 * Thus, can not add a device to a root pool.
10906423Sgw25295 	 *
10916423Sgw25295 	 * Intent log device can not be added to a rootpool because
10926423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
10936423Sgw25295 	 * can not be accessed during the mountroot time.
10946423Sgw25295 	 *
10956423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
10963912Slling 	 */
10976423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
10983912Slling 		spa_close(spa, FTAG);
10993912Slling 		return (EDOM);
11003912Slling 	}
11013912Slling 
11025450Sbrendan 	if (error == 0) {
1103789Sahrens 		error = spa_vdev_add(spa, config);
1104789Sahrens 		nvlist_free(config);
1105789Sahrens 	}
1106789Sahrens 	spa_close(spa, FTAG);
1107789Sahrens 	return (error);
1108789Sahrens }
1109789Sahrens 
1110789Sahrens static int
1111789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1112789Sahrens {
11132082Seschrock 	spa_t *spa;
11142082Seschrock 	int error;
11152082Seschrock 
11162082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
11172082Seschrock 	if (error != 0)
11182082Seschrock 		return (error);
11192082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
11202082Seschrock 	spa_close(spa, FTAG);
11212082Seschrock 	return (error);
1122789Sahrens }
1123789Sahrens 
1124789Sahrens static int
11254451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1126789Sahrens {
1127789Sahrens 	spa_t *spa;
1128789Sahrens 	int error;
11294451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1130789Sahrens 
11312926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1132789Sahrens 		return (error);
11334451Seschrock 	switch (zc->zc_cookie) {
11344451Seschrock 	case VDEV_STATE_ONLINE:
11354451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
11364451Seschrock 		break;
11374451Seschrock 
11384451Seschrock 	case VDEV_STATE_OFFLINE:
11394451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
11404451Seschrock 		break;
1141789Sahrens 
11424451Seschrock 	case VDEV_STATE_FAULTED:
11434451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
11444451Seschrock 		break;
1145789Sahrens 
11464451Seschrock 	case VDEV_STATE_DEGRADED:
11474451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
11484451Seschrock 		break;
11494451Seschrock 
11504451Seschrock 	default:
11514451Seschrock 		error = EINVAL;
11524451Seschrock 	}
11534451Seschrock 	zc->zc_cookie = newstate;
1154789Sahrens 	spa_close(spa, FTAG);
1155789Sahrens 	return (error);
1156789Sahrens }
1157789Sahrens 
1158789Sahrens static int
1159789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1160789Sahrens {
1161789Sahrens 	spa_t *spa;
1162789Sahrens 	int replacing = zc->zc_cookie;
1163789Sahrens 	nvlist_t *config;
1164789Sahrens 	int error;
1165789Sahrens 
11662926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1167789Sahrens 		return (error);
1168789Sahrens 
11695094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
11705094Slling 	    &config)) == 0) {
11711544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1172789Sahrens 		nvlist_free(config);
1173789Sahrens 	}
1174789Sahrens 
1175789Sahrens 	spa_close(spa, FTAG);
1176789Sahrens 	return (error);
1177789Sahrens }
1178789Sahrens 
1179789Sahrens static int
1180789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1181789Sahrens {
1182789Sahrens 	spa_t *spa;
1183789Sahrens 	int error;
1184789Sahrens 
11852926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1186789Sahrens 		return (error);
1187789Sahrens 
11888241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1189789Sahrens 
1190789Sahrens 	spa_close(spa, FTAG);
1191789Sahrens 	return (error);
1192789Sahrens }
1193789Sahrens 
1194789Sahrens static int
11951354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
11961354Seschrock {
11971354Seschrock 	spa_t *spa;
11982676Seschrock 	char *path = zc->zc_value;
11991544Seschrock 	uint64_t guid = zc->zc_guid;
12001354Seschrock 	int error;
12011354Seschrock 
12021354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12031354Seschrock 	if (error != 0)
12041354Seschrock 		return (error);
12051354Seschrock 
12061354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
12071354Seschrock 	spa_close(spa, FTAG);
12081354Seschrock 	return (error);
12091354Seschrock }
12101354Seschrock 
12115367Sahrens /*
12125367Sahrens  * inputs:
12135367Sahrens  * zc_name		name of filesystem
12145367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
12155367Sahrens  *
12165367Sahrens  * outputs:
12175367Sahrens  * zc_objset_stats	stats
12185367Sahrens  * zc_nvlist_dst	property nvlist
12195367Sahrens  * zc_nvlist_dst_size	size of property nvlist
12205367Sahrens  */
12211354Seschrock static int
1222789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1223789Sahrens {
1224789Sahrens 	objset_t *os = NULL;
1225789Sahrens 	int error;
12261356Seschrock 	nvlist_t *nv;
1227789Sahrens 
12286689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
12296689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1230789Sahrens 		return (error);
1231789Sahrens 
12322885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1233789Sahrens 
12342856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
12356689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
12362885Sahrens 		dmu_objset_stats(os, nv);
12373087Sahrens 		/*
12385147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
12393087Sahrens 		 * which we aren't supposed to do with a
12406689Smaybee 		 * DS_MODE_USER hold, because it could be
12413087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
12423087Sahrens 		 */
12434577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
12444577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
12454577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
12464577Sahrens 		}
12472676Seschrock 		error = put_nvlist(zc, nv);
12481356Seschrock 		nvlist_free(nv);
12491356Seschrock 	}
1250789Sahrens 
1251789Sahrens 	dmu_objset_close(os);
1252789Sahrens 	return (error);
1253789Sahrens }
1254789Sahrens 
12555498Stimh static int
12565498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
12575498Stimh {
12585498Stimh 	uint64_t value;
12595498Stimh 	int error;
12605498Stimh 
12615498Stimh 	/*
12625498Stimh 	 * zfs_get_zplprop() will either find a value or give us
12635498Stimh 	 * the default value (if there is one).
12645498Stimh 	 */
12655498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
12665498Stimh 		return (error);
12675498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
12685498Stimh 	return (0);
12695498Stimh }
12705498Stimh 
12715498Stimh /*
12725498Stimh  * inputs:
12735498Stimh  * zc_name		name of filesystem
12745498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
12755498Stimh  *
12765498Stimh  * outputs:
12775498Stimh  * zc_nvlist_dst	zpl property nvlist
12785498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
12795498Stimh  */
12805498Stimh static int
12815498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
12825498Stimh {
12835498Stimh 	objset_t *os;
12845498Stimh 	int err;
12855498Stimh 
12866689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
12876689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
12885498Stimh 		return (err);
12895498Stimh 
12905498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
12915498Stimh 
12925498Stimh 	/*
12935498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
12946689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
12956689Smaybee 	 * hold, because it could be inconsistent.
12965498Stimh 	 */
12975498Stimh 	if (zc->zc_nvlist_dst != NULL &&
12985498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
12995498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
13005498Stimh 		nvlist_t *nv;
13015498Stimh 
13025498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
13035498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
13045498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
13055498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
13065498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
13075498Stimh 			err = put_nvlist(zc, nv);
13085498Stimh 		nvlist_free(nv);
13095498Stimh 	} else {
13105498Stimh 		err = ENOENT;
13115498Stimh 	}
13125498Stimh 	dmu_objset_close(os);
13135498Stimh 	return (err);
13145498Stimh }
13155498Stimh 
13165367Sahrens /*
13175367Sahrens  * inputs:
13185367Sahrens  * zc_name		name of filesystem
13195367Sahrens  * zc_cookie		zap cursor
13205367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13215367Sahrens  *
13225367Sahrens  * outputs:
13235367Sahrens  * zc_name		name of next filesystem
13245367Sahrens  * zc_objset_stats	stats
13255367Sahrens  * zc_nvlist_dst	property nvlist
13265367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13275367Sahrens  */
1328789Sahrens static int
1329789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1330789Sahrens {
1331885Sahrens 	objset_t *os;
1332789Sahrens 	int error;
1333789Sahrens 	char *p;
1334789Sahrens 
13356689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13366689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1337885Sahrens 		if (error == ENOENT)
1338885Sahrens 			error = ESRCH;
1339885Sahrens 		return (error);
1340789Sahrens 	}
1341789Sahrens 
1342789Sahrens 	p = strrchr(zc->zc_name, '/');
1343789Sahrens 	if (p == NULL || p[1] != '\0')
1344789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1345789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1346789Sahrens 
13478697SRichard.Morris@Sun.COM 	/*
13488697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
13498697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
13508697SRichard.Morris@Sun.COM 	 */
13518415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
13528415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
13538415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
13548415SRichard.Morris@Sun.COM 
13558415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
13568697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
13578415SRichard.Morris@Sun.COM 	}
13588415SRichard.Morris@Sun.COM 
1359789Sahrens 	do {
1360885Sahrens 		error = dmu_dir_list_next(os,
1361885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1362885Sahrens 		    NULL, &zc->zc_cookie);
1363789Sahrens 		if (error == ENOENT)
1364789Sahrens 			error = ESRCH;
1365885Sahrens 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1366789Sahrens 	    !zone_dataset_visible(zc->zc_name, NULL));
13676689Smaybee 	dmu_objset_close(os);
1368789Sahrens 
1369885Sahrens 	/*
1370885Sahrens 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1371885Sahrens 	 * try to get stats for it.  Userland will skip over it.
1372885Sahrens 	 */
1373885Sahrens 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1374885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1375789Sahrens 
1376789Sahrens 	return (error);
1377789Sahrens }
1378789Sahrens 
13795367Sahrens /*
13805367Sahrens  * inputs:
13815367Sahrens  * zc_name		name of filesystem
13825367Sahrens  * zc_cookie		zap cursor
13835367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13845367Sahrens  *
13855367Sahrens  * outputs:
13865367Sahrens  * zc_name		name of next snapshot
13875367Sahrens  * zc_objset_stats	stats
13885367Sahrens  * zc_nvlist_dst	property nvlist
13895367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13905367Sahrens  */
1391789Sahrens static int
1392789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1393789Sahrens {
1394885Sahrens 	objset_t *os;
1395789Sahrens 	int error;
1396789Sahrens 
13976689Smaybee 	error = dmu_objset_open(zc->zc_name,
13986689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
13996689Smaybee 	if (error)
14006689Smaybee 		return (error == ENOENT ? ESRCH : error);
1401789Sahrens 
14028415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
14038697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
14048415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
14051003Slling 	/*
14061003Slling 	 * A dataset name of maximum length cannot have any snapshots,
14071003Slling 	 * so exit immediately.
14081003Slling 	 */
14091003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1410885Sahrens 		dmu_objset_close(os);
14111003Slling 		return (ESRCH);
1412789Sahrens 	}
1413789Sahrens 
1414885Sahrens 	error = dmu_snapshot_list_next(os,
1415885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
14165663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
14176689Smaybee 	dmu_objset_close(os);
1418885Sahrens 	if (error == 0)
1419885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
14206689Smaybee 	else if (error == ENOENT)
14216689Smaybee 		error = ESRCH;
1422789Sahrens 
14235367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
14246689Smaybee 	if (error)
14255367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1426789Sahrens 	return (error);
1427789Sahrens }
1428789Sahrens 
14296423Sgw25295 int
14304787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1431789Sahrens {
14322676Seschrock 	nvpair_t *elem;
14338724SRichard.Morris@Sun.COM 	int error = 0;
14342676Seschrock 	uint64_t intval;
14352676Seschrock 	char *strval;
14368697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
14372676Seschrock 
14384543Smarks 	/*
14394543Smarks 	 * First validate permission to set all of the properties
14404543Smarks 	 */
14412676Seschrock 	elem = NULL;
14422676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
14434670Sahrens 		const char *propname = nvpair_name(elem);
14444670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
14452676Seschrock 
14465094Slling 		if (prop == ZPROP_INVAL) {
14472676Seschrock 			/*
14482676Seschrock 			 * If this is a user-defined property, it must be a
14492676Seschrock 			 * string, and there is no further validation to do.
14502676Seschrock 			 */
14512676Seschrock 			if (!zfs_prop_user(propname) ||
14522676Seschrock 			    nvpair_type(elem) != DATA_TYPE_STRING)
14532676Seschrock 				return (EINVAL);
14542676Seschrock 
14555331Samw 			if (error = zfs_secpolicy_write_perms(name,
14565331Samw 			    ZFS_DELEG_PERM_USERPROP, CRED()))
14574670Sahrens 				return (error);
14584543Smarks 			continue;
14592676Seschrock 		}
14602676Seschrock 
14614787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
14624670Sahrens 			return (error);
14632676Seschrock 
14644670Sahrens 		/*
14654670Sahrens 		 * Check that this value is valid for this pool version
14664670Sahrens 		 */
14674670Sahrens 		switch (prop) {
14683886Sahl 		case ZFS_PROP_COMPRESSION:
14693886Sahl 			/*
14703886Sahl 			 * If the user specified gzip compression, make sure
14713886Sahl 			 * the SPA supports it. We ignore any errors here since
14723886Sahl 			 * we'll catch them later.
14733886Sahl 			 */
14743886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
14757042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
14767042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
14777042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
14787184Stimh 				    zfs_earlier_version(name,
14795331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
14805331Samw 					return (ENOTSUP);
14817042Sgw25295 
14827042Sgw25295 				/*
14837042Sgw25295 				 * If this is a bootable dataset then
14847042Sgw25295 				 * verify that the compression algorithm
14857042Sgw25295 				 * is supported for booting. We must return
14867042Sgw25295 				 * something other than ENOTSUP since it
14877042Sgw25295 				 * implies a downrev pool version.
14887042Sgw25295 				 */
14897042Sgw25295 				if (zfs_is_bootfs(name) &&
14907042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
14917042Sgw25295 					return (ERANGE);
14923886Sahl 			}
14933886Sahl 			break;
14944603Sahrens 
14954603Sahrens 		case ZFS_PROP_COPIES:
14967184Stimh 			if (zfs_earlier_version(name,
14977184Stimh 			    SPA_VERSION_DITTO_BLOCKS))
14985331Samw 				return (ENOTSUP);
14994603Sahrens 			break;
15005977Smarks 
15015977Smarks 		case ZFS_PROP_SHARESMB:
15026689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
15035977Smarks 				return (ENOTSUP);
15045977Smarks 			break;
15058053SMark.Shellenbaum@Sun.COM 
15068053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
15078053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
15088053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
15098053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
15108053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
15118053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
15128053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
15134603Sahrens 		}
15144543Smarks 	}
15154543Smarks 
15168697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15174543Smarks 	elem = NULL;
15184543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
15194670Sahrens 		const char *propname = nvpair_name(elem);
15204670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
15214543Smarks 
15225094Slling 		if (prop == ZPROP_INVAL) {
15234543Smarks 			VERIFY(nvpair_value_string(elem, &strval) == 0);
15244543Smarks 			error = dsl_prop_set(name, propname, 1,
15254543Smarks 			    strlen(strval) + 1, strval);
15264543Smarks 			if (error == 0)
15274543Smarks 				continue;
15284543Smarks 			else
15298697SRichard.Morris@Sun.COM 				goto out;
15304543Smarks 		}
15312676Seschrock 
15322676Seschrock 		switch (prop) {
15332676Seschrock 		case ZFS_PROP_QUOTA:
15342676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15354577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
15368697SRichard.Morris@Sun.COM 				goto out;
15372676Seschrock 			break;
15382676Seschrock 
15395378Sck153898 		case ZFS_PROP_REFQUOTA:
15405378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15415378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
15428697SRichard.Morris@Sun.COM 				goto out;
15435378Sck153898 			break;
15445378Sck153898 
15452676Seschrock 		case ZFS_PROP_RESERVATION:
15462676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15472676Seschrock 			    (error = dsl_dir_set_reservation(name,
15482676Seschrock 			    intval)) != 0)
15498697SRichard.Morris@Sun.COM 				goto out;
15502676Seschrock 			break;
1551789Sahrens 
15525378Sck153898 		case ZFS_PROP_REFRESERVATION:
15535378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15545378Sck153898 			    (error = dsl_dataset_set_reservation(name,
15555378Sck153898 			    intval)) != 0)
15568697SRichard.Morris@Sun.COM 				goto out;
15575378Sck153898 			break;
15585378Sck153898 
15592676Seschrock 		case ZFS_PROP_VOLSIZE:
15602676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15614787Sahrens 			    (error = zvol_set_volsize(name,
15624787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
15638697SRichard.Morris@Sun.COM 				goto out;
15642676Seschrock 			break;
15652676Seschrock 
15662676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
15672676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15684577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
15698697SRichard.Morris@Sun.COM 				goto out;
15704577Sahrens 			break;
15714577Sahrens 
15724577Sahrens 		case ZFS_PROP_VERSION:
15734577Sahrens 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
15744577Sahrens 			    (error = zfs_set_version(name, intval)) != 0)
15758697SRichard.Morris@Sun.COM 				goto out;
15762676Seschrock 			break;
15772676Seschrock 
15782676Seschrock 		default:
15792676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
15802676Seschrock 				if (zfs_prop_get_type(prop) !=
15818697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
15828697SRichard.Morris@Sun.COM 					error = EINVAL;
15838697SRichard.Morris@Sun.COM 					goto out;
15848697SRichard.Morris@Sun.COM 				}
15852676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
15862885Sahrens 				const char *unused;
15872885Sahrens 
15882717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
15892676Seschrock 
15902676Seschrock 				switch (zfs_prop_get_type(prop)) {
15914787Sahrens 				case PROP_TYPE_NUMBER:
15922676Seschrock 					break;
15934787Sahrens 				case PROP_TYPE_STRING:
15948697SRichard.Morris@Sun.COM 					error = EINVAL;
15958697SRichard.Morris@Sun.COM 					goto out;
15964787Sahrens 				case PROP_TYPE_INDEX:
15972717Seschrock 					if (zfs_prop_index_to_string(prop,
15988697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
15998697SRichard.Morris@Sun.COM 						error = EINVAL;
16008697SRichard.Morris@Sun.COM 						goto out;
16018697SRichard.Morris@Sun.COM 					}
16022676Seschrock 					break;
16032676Seschrock 				default:
16044577Sahrens 					cmn_err(CE_PANIC,
16054577Sahrens 					    "unknown property type");
16062676Seschrock 					break;
16072676Seschrock 				}
16082676Seschrock 			} else {
16098697SRichard.Morris@Sun.COM 				error = EINVAL;
16108697SRichard.Morris@Sun.COM 				goto out;
16112676Seschrock 			}
16128697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
16138697SRichard.Morris@Sun.COM 				goto out;
16142676Seschrock 		}
16152676Seschrock 	}
16162676Seschrock 
16178697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
16188697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
16198697SRichard.Morris@Sun.COM 	}
16208697SRichard.Morris@Sun.COM out:
16218697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
16228697SRichard.Morris@Sun.COM 	return (error);
1623789Sahrens }
1624789Sahrens 
16255367Sahrens /*
16265367Sahrens  * inputs:
16275367Sahrens  * zc_name		name of filesystem
16288697SRichard.Morris@Sun.COM  * zc_value		name of property to set
16295367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
16307265Sahrens  * zc_cookie		clear existing local props?
16315367Sahrens  *
16325367Sahrens  * outputs:		none
16335367Sahrens  */
1634789Sahrens static int
16352676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1636789Sahrens {
16372676Seschrock 	nvlist_t *nvl;
16382676Seschrock 	int error;
1639789Sahrens 
16405094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16415094Slling 	    &nvl)) != 0)
16422676Seschrock 		return (error);
16432676Seschrock 
16447265Sahrens 	if (zc->zc_cookie) {
16457265Sahrens 		nvlist_t *origprops;
16467265Sahrens 		objset_t *os;
16477265Sahrens 
16487265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
16497265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
16507265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
16518536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
16527265Sahrens 				nvlist_free(origprops);
16537265Sahrens 			}
16547265Sahrens 			dmu_objset_close(os);
16557265Sahrens 		}
16567265Sahrens 
16577265Sahrens 	}
16587265Sahrens 
16594787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
16604543Smarks 
16612676Seschrock 	nvlist_free(nvl);
16622676Seschrock 	return (error);
1663789Sahrens }
1664789Sahrens 
16655367Sahrens /*
16665367Sahrens  * inputs:
16675367Sahrens  * zc_name		name of filesystem
16685367Sahrens  * zc_value		name of property to inherit
16695367Sahrens  *
16705367Sahrens  * outputs:		none
16715367Sahrens  */
1672789Sahrens static int
16734849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
16744849Sahrens {
16754849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
16764849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
16774849Sahrens }
16784849Sahrens 
16794849Sahrens static int
16804098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
16813912Slling {
16825094Slling 	nvlist_t *props;
16833912Slling 	spa_t *spa;
16845094Slling 	int error;
16858525SEric.Schrock@Sun.COM 	nvpair_t *elem;
16863912Slling 
16875094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16885094Slling 	    &props)))
16893912Slling 		return (error);
16903912Slling 
16918525SEric.Schrock@Sun.COM 	/*
16928525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
16938525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
16948525SEric.Schrock@Sun.COM 	 */
16958525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
16968525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
16978525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
16988525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
16998525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
17008525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
17018525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
17028525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
17038525SEric.Schrock@Sun.COM 		}
17048525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
17058525SEric.Schrock@Sun.COM 		if (spa != NULL)
17068525SEric.Schrock@Sun.COM 			return (0);
17078525SEric.Schrock@Sun.COM 	}
17088525SEric.Schrock@Sun.COM 
17093912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
17105094Slling 		nvlist_free(props);
17113912Slling 		return (error);
17123912Slling 	}
17133912Slling 
17145094Slling 	error = spa_prop_set(spa, props);
17153912Slling 
17165094Slling 	nvlist_free(props);
17173912Slling 	spa_close(spa, FTAG);
17183912Slling 
17193912Slling 	return (error);
17203912Slling }
17213912Slling 
17223912Slling static int
17234098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
17243912Slling {
17253912Slling 	spa_t *spa;
17263912Slling 	int error;
17273912Slling 	nvlist_t *nvp = NULL;
17283912Slling 
17298525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
17308525SEric.Schrock@Sun.COM 		/*
17318525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
17328525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
17338525SEric.Schrock@Sun.COM 		 * anyway.
17348525SEric.Schrock@Sun.COM 		 */
17358525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
17368525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
17378525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
17388525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
17398525SEric.Schrock@Sun.COM 	} else {
17408525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
17418525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
17428525SEric.Schrock@Sun.COM 	}
17433912Slling 
17443912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
17453912Slling 		error = put_nvlist(zc, nvp);
17463912Slling 	else
17473912Slling 		error = EFAULT;
17483912Slling 
17498525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
17503912Slling 	return (error);
17513912Slling }
17523912Slling 
17533912Slling static int
17544543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
17554543Smarks {
17564543Smarks 	nvlist_t *nvp;
17574543Smarks 	int error;
17584543Smarks 	uint32_t uid;
17594543Smarks 	uint32_t gid;
17604543Smarks 	uint32_t *groups;
17614543Smarks 	uint_t group_cnt;
17624543Smarks 	cred_t	*usercred;
17634543Smarks 
17645094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17655094Slling 	    &nvp)) != 0) {
17664543Smarks 		return (error);
17674543Smarks 	}
17684543Smarks 
17694543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
17704543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
17714543Smarks 		nvlist_free(nvp);
17724543Smarks 		return (EPERM);
17734543Smarks 	}
17744543Smarks 
17754543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
17764543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
17774543Smarks 		nvlist_free(nvp);
17784543Smarks 		return (EPERM);
17794543Smarks 	}
17804543Smarks 
17814543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
17824543Smarks 	    &groups, &group_cnt)) != 0) {
17834543Smarks 		nvlist_free(nvp);
17844543Smarks 		return (EPERM);
17854543Smarks 	}
17864543Smarks 	usercred = cralloc();
17874543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
17884543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
17894543Smarks 		nvlist_free(nvp);
17904543Smarks 		crfree(usercred);
17914543Smarks 		return (EPERM);
17924543Smarks 	}
17934543Smarks 	nvlist_free(nvp);
17944543Smarks 	error = dsl_deleg_access(zc->zc_name,
17954787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
17964543Smarks 	crfree(usercred);
17974543Smarks 	return (error);
17984543Smarks }
17994543Smarks 
18005367Sahrens /*
18015367Sahrens  * inputs:
18025367Sahrens  * zc_name		name of filesystem
18035367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
18045367Sahrens  * zc_perm_action	allow/unallow flag
18055367Sahrens  *
18065367Sahrens  * outputs:		none
18075367Sahrens  */
18084543Smarks static int
18094543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
18104543Smarks {
18114543Smarks 	int error;
18124543Smarks 	nvlist_t *fsaclnv = NULL;
18134543Smarks 
18145094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18155094Slling 	    &fsaclnv)) != 0)
18164543Smarks 		return (error);
18174543Smarks 
18184543Smarks 	/*
18194543Smarks 	 * Verify nvlist is constructed correctly
18204543Smarks 	 */
18214543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
18224543Smarks 		nvlist_free(fsaclnv);
18234543Smarks 		return (EINVAL);
18244543Smarks 	}
18254543Smarks 
18264543Smarks 	/*
18274543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
18284543Smarks 	 * that user is allowed to hand out each permission in
18294543Smarks 	 * the nvlist(s)
18304543Smarks 	 */
18314543Smarks 
18324787Sahrens 	error = secpolicy_zfs(CRED());
18334543Smarks 	if (error) {
18344787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
18354787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
18364787Sahrens 			    fsaclnv, CRED());
18374787Sahrens 		} else {
18384787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
18394787Sahrens 			    fsaclnv, CRED());
18404787Sahrens 		}
18414543Smarks 	}
18424543Smarks 
18434543Smarks 	if (error == 0)
18444543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
18454543Smarks 
18464543Smarks 	nvlist_free(fsaclnv);
18474543Smarks 	return (error);
18484543Smarks }
18494543Smarks 
18505367Sahrens /*
18515367Sahrens  * inputs:
18525367Sahrens  * zc_name		name of filesystem
18535367Sahrens  *
18545367Sahrens  * outputs:
18555367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
18565367Sahrens  */
18574543Smarks static int
18584543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
18594543Smarks {
18604543Smarks 	nvlist_t *nvp;
18614543Smarks 	int error;
18624543Smarks 
18634543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
18644543Smarks 		error = put_nvlist(zc, nvp);
18654543Smarks 		nvlist_free(nvp);
18664543Smarks 	}
18674543Smarks 
18684543Smarks 	return (error);
18694543Smarks }
18704543Smarks 
18715367Sahrens /*
18725367Sahrens  * inputs:
18735367Sahrens  * zc_name		name of volume
18745367Sahrens  *
18755367Sahrens  * outputs:		none
18765367Sahrens  */
18774543Smarks static int
1878789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
1879789Sahrens {
18804787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1881789Sahrens }
1882789Sahrens 
18835367Sahrens /*
18845367Sahrens  * inputs:
18855367Sahrens  * zc_name		name of volume
18865367Sahrens  *
18875367Sahrens  * outputs:		none
18885367Sahrens  */
1889789Sahrens static int
1890789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
1891789Sahrens {
18922676Seschrock 	return (zvol_remove_minor(zc->zc_name));
1893789Sahrens }
1894789Sahrens 
1895789Sahrens /*
1896789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
1897789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
1898789Sahrens  * is responsible for releasing the returned vfs pointer.
1899789Sahrens  */
1900789Sahrens static vfs_t *
1901789Sahrens zfs_get_vfs(const char *resource)
1902789Sahrens {
1903789Sahrens 	struct vfs *vfsp;
1904789Sahrens 	struct vfs *vfs_found = NULL;
1905789Sahrens 
1906789Sahrens 	vfs_list_read_lock();
1907789Sahrens 	vfsp = rootvfs;
1908789Sahrens 	do {
1909789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1910789Sahrens 			VFS_HOLD(vfsp);
1911789Sahrens 			vfs_found = vfsp;
1912789Sahrens 			break;
1913789Sahrens 		}
1914789Sahrens 		vfsp = vfsp->vfs_next;
1915789Sahrens 	} while (vfsp != rootvfs);
1916789Sahrens 	vfs_list_unlock();
1917789Sahrens 	return (vfs_found);
1918789Sahrens }
1919789Sahrens 
19204543Smarks /* ARGSUSED */
1921789Sahrens static void
19224543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1923789Sahrens {
19245331Samw 	zfs_creat_t *zct = arg;
19255498Stimh 
19265498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
19275331Samw }
19285331Samw 
19295498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
19305498Stimh 
19315331Samw /*
19325498Stimh  * inputs:
19337184Stimh  * createprops		list of properties requested by creator
19347184Stimh  * default_zplver	zpl version to use if unspecified in createprops
19357184Stimh  * fuids_ok		fuids allowed in this version of the spa?
19367184Stimh  * os			parent objset pointer (NULL if root fs)
19375331Samw  *
19385498Stimh  * outputs:
19395498Stimh  * zplprops	values for the zplprops we attach to the master node object
19407184Stimh  * is_ci	true if requested file system will be purely case-insensitive
19415331Samw  *
19425498Stimh  * Determine the settings for utf8only, normalization and
19435498Stimh  * casesensitivity.  Specific values may have been requested by the
19445498Stimh  * creator and/or we can inherit values from the parent dataset.  If
19455498Stimh  * the file system is of too early a vintage, a creator can not
19465498Stimh  * request settings for these properties, even if the requested
19475498Stimh  * setting is the default value.  We don't actually want to create dsl
19485498Stimh  * properties for these, so remove them from the source nvlist after
19495498Stimh  * processing.
19505331Samw  */
19515331Samw static int
19527184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
19537184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
19547184Stimh     boolean_t *is_ci)
19555331Samw {
19567184Stimh 	uint64_t zplver = default_zplver;
19575498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
19585498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
19595498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
19605498Stimh 
19615498Stimh 	ASSERT(zplprops != NULL);
19625498Stimh 
19635375Stimh 	/*
19645498Stimh 	 * Pull out creator prop choices, if any.
19655375Stimh 	 */
19665498Stimh 	if (createprops) {
19675498Stimh 		(void) nvlist_lookup_uint64(createprops,
19687184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
19697184Stimh 		(void) nvlist_lookup_uint64(createprops,
19705498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
19715498Stimh 		(void) nvlist_remove_all(createprops,
19725498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
19735498Stimh 		(void) nvlist_lookup_uint64(createprops,
19745498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
19755498Stimh 		(void) nvlist_remove_all(createprops,
19765498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
19775498Stimh 		(void) nvlist_lookup_uint64(createprops,
19785498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
19795498Stimh 		(void) nvlist_remove_all(createprops,
19805498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
19815331Samw 	}
19825331Samw 
19835375Stimh 	/*
19847184Stimh 	 * If the zpl version requested is whacky or the file system
19857184Stimh 	 * or pool is version is too "young" to support normalization
19867184Stimh 	 * and the creator tried to set a value for one of the props,
19877184Stimh 	 * error out.
19885498Stimh 	 */
19897184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
19907184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
19917184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
19925498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
19937184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
19945498Stimh 		return (ENOTSUP);
19955498Stimh 
19965498Stimh 	/*
19975498Stimh 	 * Put the version in the zplprops
19985498Stimh 	 */
19995498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20005498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
20015498Stimh 
20025498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
20035498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
20045498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20055498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
20065498Stimh 
20075498Stimh 	/*
20085498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
20095498Stimh 	 */
20105498Stimh 	if (norm)
20115498Stimh 		u8 = 1;
20125498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
20135498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
20145498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20155498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
20165498Stimh 
20175498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
20185498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
20195498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20205498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
20215498Stimh 
20226492Stimh 	if (is_ci)
20236492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
20246492Stimh 
20257184Stimh 	return (0);
20267184Stimh }
20277184Stimh 
20287184Stimh static int
20297184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
20307184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20317184Stimh {
20327184Stimh 	boolean_t fuids_ok = B_TRUE;
20337184Stimh 	uint64_t zplver = ZPL_VERSION;
20347184Stimh 	objset_t *os = NULL;
20357184Stimh 	char parentname[MAXNAMELEN];
20367184Stimh 	char *cp;
20377184Stimh 	int error;
20387184Stimh 
20397184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
20407184Stimh 	cp = strrchr(parentname, '/');
20417184Stimh 	ASSERT(cp != NULL);
20427184Stimh 	cp[0] = '\0';
20437184Stimh 
20447184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
20457184Stimh 		zplver = ZPL_VERSION_FUID - 1;
20467184Stimh 		fuids_ok = B_FALSE;
20477184Stimh 	}
20487184Stimh 
20497184Stimh 	/*
20507184Stimh 	 * Open parent object set so we can inherit zplprop values.
20517184Stimh 	 */
20527184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
20537184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
20547184Stimh 		return (error);
20557184Stimh 
20567184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
20577184Stimh 	    zplprops, is_ci);
20585498Stimh 	dmu_objset_close(os);
20597184Stimh 	return (error);
20607184Stimh }
20617184Stimh 
20627184Stimh static int
20637184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
20647184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20657184Stimh {
20667184Stimh 	boolean_t fuids_ok = B_TRUE;
20677184Stimh 	uint64_t zplver = ZPL_VERSION;
20687184Stimh 	int error;
20697184Stimh 
20707184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
20717184Stimh 		zplver = ZPL_VERSION_FUID - 1;
20727184Stimh 		fuids_ok = B_FALSE;
20737184Stimh 	}
20747184Stimh 
20757184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
20767184Stimh 	    zplprops, is_ci);
20777184Stimh 	return (error);
2078789Sahrens }
2079789Sahrens 
20805367Sahrens /*
20815367Sahrens  * inputs:
20825367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
20835367Sahrens  * zc_name		name of new objset
20845367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
20855367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
20865367Sahrens  *
20875498Stimh  * outputs: none
20885367Sahrens  */
2089789Sahrens static int
2090789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2091789Sahrens {
2092789Sahrens 	objset_t *clone;
2093789Sahrens 	int error = 0;
20945331Samw 	zfs_creat_t zct;
20954543Smarks 	nvlist_t *nvprops = NULL;
20964543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2097789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2098789Sahrens 
2099789Sahrens 	switch (type) {
2100789Sahrens 
2101789Sahrens 	case DMU_OST_ZFS:
2102789Sahrens 		cbfunc = zfs_create_cb;
2103789Sahrens 		break;
2104789Sahrens 
2105789Sahrens 	case DMU_OST_ZVOL:
2106789Sahrens 		cbfunc = zvol_create_cb;
2107789Sahrens 		break;
2108789Sahrens 
2109789Sahrens 	default:
21102199Sahrens 		cbfunc = NULL;
21116423Sgw25295 		break;
21122199Sahrens 	}
21135326Sek110237 	if (strchr(zc->zc_name, '@') ||
21145326Sek110237 	    strchr(zc->zc_name, '%'))
2115789Sahrens 		return (EINVAL);
2116789Sahrens 
21172676Seschrock 	if (zc->zc_nvlist_src != NULL &&
21185094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
21195094Slling 	    &nvprops)) != 0)
21202676Seschrock 		return (error);
21212676Seschrock 
21225498Stimh 	zct.zct_zplprops = NULL;
21235331Samw 	zct.zct_props = nvprops;
21245331Samw 
21252676Seschrock 	if (zc->zc_value[0] != '\0') {
2126789Sahrens 		/*
2127789Sahrens 		 * We're creating a clone of an existing snapshot.
2128789Sahrens 		 */
21292676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
21302676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
21314543Smarks 			nvlist_free(nvprops);
2132789Sahrens 			return (EINVAL);
21332676Seschrock 		}
2134789Sahrens 
21352676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
21366689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
21372676Seschrock 		if (error) {
21384543Smarks 			nvlist_free(nvprops);
2139789Sahrens 			return (error);
21402676Seschrock 		}
21416492Stimh 
21426492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
21436492Stimh 		    NULL, NULL);
21445331Samw 		if (error) {
21455331Samw 			dmu_objset_close(clone);
21465331Samw 			nvlist_free(nvprops);
21475331Samw 			return (error);
21485331Samw 		}
2149789Sahrens 		dmu_objset_close(clone);
2150789Sahrens 	} else {
21516492Stimh 		boolean_t is_insensitive = B_FALSE;
21526492Stimh 
21532676Seschrock 		if (cbfunc == NULL) {
21544543Smarks 			nvlist_free(nvprops);
21552199Sahrens 			return (EINVAL);
21562676Seschrock 		}
21572676Seschrock 
2158789Sahrens 		if (type == DMU_OST_ZVOL) {
21592676Seschrock 			uint64_t volsize, volblocksize;
21602676Seschrock 
21614543Smarks 			if (nvprops == NULL ||
21624543Smarks 			    nvlist_lookup_uint64(nvprops,
21632676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
21642676Seschrock 			    &volsize) != 0) {
21654543Smarks 				nvlist_free(nvprops);
21662676Seschrock 				return (EINVAL);
21672676Seschrock 			}
21682676Seschrock 
21694543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
21702676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
21712676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
21724543Smarks 				nvlist_free(nvprops);
21732676Seschrock 				return (EINVAL);
21742676Seschrock 			}
21751133Seschrock 
21762676Seschrock 			if (error != 0)
21772676Seschrock 				volblocksize = zfs_prop_default_numeric(
21782676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
21792676Seschrock 
21802676Seschrock 			if ((error = zvol_check_volblocksize(
21812676Seschrock 			    volblocksize)) != 0 ||
21822676Seschrock 			    (error = zvol_check_volsize(volsize,
21832676Seschrock 			    volblocksize)) != 0) {
21844543Smarks 				nvlist_free(nvprops);
2185789Sahrens 				return (error);
21862676Seschrock 			}
21874577Sahrens 		} else if (type == DMU_OST_ZFS) {
21885331Samw 			int error;
21895331Samw 
21905498Stimh 			/*
21915331Samw 			 * We have to have normalization and
21925331Samw 			 * case-folding flags correct when we do the
21935331Samw 			 * file system creation, so go figure them out
21945498Stimh 			 * now.
21955331Samw 			 */
21965498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
21975498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21985498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
21997184Stimh 			    zct.zct_zplprops, &is_insensitive);
22005331Samw 			if (error != 0) {
22015331Samw 				nvlist_free(nvprops);
22025498Stimh 				nvlist_free(zct.zct_zplprops);
22035331Samw 				return (error);
22044577Sahrens 			}
22052676Seschrock 		}
22066492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
22076492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
22085498Stimh 		nvlist_free(zct.zct_zplprops);
2209789Sahrens 	}
22102676Seschrock 
22112676Seschrock 	/*
22122676Seschrock 	 * It would be nice to do this atomically.
22132676Seschrock 	 */
22142676Seschrock 	if (error == 0) {
22154787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
22162676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
22172676Seschrock 	}
22184543Smarks 	nvlist_free(nvprops);
2219789Sahrens 	return (error);
2220789Sahrens }
2221789Sahrens 
22227265Sahrens struct snap_prop_arg {
22237265Sahrens 	nvlist_t *nvprops;
22247265Sahrens 	const char *snapname;
22257265Sahrens };
22267265Sahrens 
22277265Sahrens static int
22287265Sahrens set_snap_props(char *name, void *arg)
22297265Sahrens {
22307265Sahrens 	struct snap_prop_arg *snpa = arg;
22317265Sahrens 	int len = strlen(name) + strlen(snpa->snapname) + 2;
22327265Sahrens 	char *buf = kmem_alloc(len, KM_SLEEP);
22337265Sahrens 	int err;
22347265Sahrens 
22357265Sahrens 	(void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
22367265Sahrens 	err = zfs_set_prop_nvlist(buf, snpa->nvprops);
22377265Sahrens 	if (err)
22387265Sahrens 		(void) dmu_objset_destroy(buf);
22397265Sahrens 	kmem_free(buf, len);
22407265Sahrens 	return (err);
22417265Sahrens }
22427265Sahrens 
22435367Sahrens /*
22445367Sahrens  * inputs:
22455367Sahrens  * zc_name	name of filesystem
22465367Sahrens  * zc_value	short name of snapshot
22475367Sahrens  * zc_cookie	recursive flag
22485367Sahrens  *
22495367Sahrens  * outputs:	none
22505367Sahrens  */
2251789Sahrens static int
22522199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
22532199Sahrens {
22547265Sahrens 	nvlist_t *nvprops = NULL;
22557265Sahrens 	int error;
22567265Sahrens 	boolean_t recursive = zc->zc_cookie;
22577265Sahrens 
22582676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
22592199Sahrens 		return (EINVAL);
22607265Sahrens 
22617265Sahrens 	if (zc->zc_nvlist_src != NULL &&
22627265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
22637265Sahrens 	    &nvprops)) != 0)
22647265Sahrens 		return (error);
22657265Sahrens 
22667265Sahrens 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
22677265Sahrens 
22687265Sahrens 	/*
22697265Sahrens 	 * It would be nice to do this atomically.
22707265Sahrens 	 */
22717265Sahrens 	if (error == 0) {
22727265Sahrens 		struct snap_prop_arg snpa;
22737265Sahrens 		snpa.nvprops = nvprops;
22747265Sahrens 		snpa.snapname = zc->zc_value;
22757265Sahrens 		if (recursive) {
22767265Sahrens 			error = dmu_objset_find(zc->zc_name,
22777265Sahrens 			    set_snap_props, &snpa, DS_FIND_CHILDREN);
22787265Sahrens 			if (error) {
22797265Sahrens 				(void) dmu_snapshots_destroy(zc->zc_name,
22807265Sahrens 				    zc->zc_value);
22817265Sahrens 			}
22827265Sahrens 		} else {
22837265Sahrens 			error = set_snap_props(zc->zc_name, &snpa);
22847265Sahrens 		}
22857265Sahrens 	}
22867265Sahrens 	nvlist_free(nvprops);
22877265Sahrens 	return (error);
22882199Sahrens }
22892199Sahrens 
22904007Smmusante int
22912199Sahrens zfs_unmount_snap(char *name, void *arg)
2292789Sahrens {
22932417Sahrens 	vfs_t *vfsp = NULL;
22942199Sahrens 
22956689Smaybee 	if (arg) {
22966689Smaybee 		char *snapname = arg;
22976689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
22986689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
22996689Smaybee 
23006689Smaybee 		(void) strcpy(buf, name);
23016689Smaybee 		(void) strcat(buf, "@");
23026689Smaybee 		(void) strcat(buf, snapname);
23036689Smaybee 		vfsp = zfs_get_vfs(buf);
23046689Smaybee 		kmem_free(buf, len);
23052417Sahrens 	} else if (strchr(name, '@')) {
23062199Sahrens 		vfsp = zfs_get_vfs(name);
23072199Sahrens 	}
23082199Sahrens 
23092199Sahrens 	if (vfsp) {
23102199Sahrens 		/*
23112199Sahrens 		 * Always force the unmount for snapshots.
23122199Sahrens 		 */
23132199Sahrens 		int flag = MS_FORCE;
2314789Sahrens 		int err;
2315789Sahrens 
23162199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
23172199Sahrens 			VFS_RELE(vfsp);
23182199Sahrens 			return (err);
23192199Sahrens 		}
23202199Sahrens 		VFS_RELE(vfsp);
23212199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
23222199Sahrens 			return (err);
23232199Sahrens 	}
23242199Sahrens 	return (0);
23252199Sahrens }
23262199Sahrens 
23275367Sahrens /*
23285367Sahrens  * inputs:
23295367Sahrens  * zc_name	name of filesystem
23305367Sahrens  * zc_value	short name of snapshot
23315367Sahrens  *
23325367Sahrens  * outputs:	none
23335367Sahrens  */
23342199Sahrens static int
23352199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
23362199Sahrens {
23372199Sahrens 	int err;
2338789Sahrens 
23392676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
23402199Sahrens 		return (EINVAL);
23412199Sahrens 	err = dmu_objset_find(zc->zc_name,
23422676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
23432199Sahrens 	if (err)
23442199Sahrens 		return (err);
23452676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
23462199Sahrens }
23472199Sahrens 
23485367Sahrens /*
23495367Sahrens  * inputs:
23505367Sahrens  * zc_name		name of dataset to destroy
23515367Sahrens  * zc_objset_type	type of objset
23525367Sahrens  *
23535367Sahrens  * outputs:		none
23545367Sahrens  */
23552199Sahrens static int
23562199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
23572199Sahrens {
23582199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
23592199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
23602199Sahrens 		if (err)
23612199Sahrens 			return (err);
2362789Sahrens 	}
2363789Sahrens 
2364789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2365789Sahrens }
2366789Sahrens 
23675367Sahrens /*
23685367Sahrens  * inputs:
23695446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
23705367Sahrens  *
23715367Sahrens  * outputs:	none
23725367Sahrens  */
2373789Sahrens static int
2374789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2375789Sahrens {
23765446Sahrens 	objset_t *os;
23775446Sahrens 	int error;
23785446Sahrens 	zfsvfs_t *zfsvfs = NULL;
23795446Sahrens 
23805446Sahrens 	/*
23815446Sahrens 	 * Get the zfsvfs for the receiving objset. There
23825446Sahrens 	 * won't be one if we're operating on a zvol, if the
23835446Sahrens 	 * objset doesn't exist yet, or is not mounted.
23845446Sahrens 	 */
23856689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
23865446Sahrens 	if (error)
23875446Sahrens 		return (error);
23885446Sahrens 
23895446Sahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
23905446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
23915446Sahrens 		zfsvfs = dmu_objset_get_user(os);
23925446Sahrens 		if (zfsvfs != NULL)
23935446Sahrens 			VFS_HOLD(zfsvfs->z_vfs);
23945446Sahrens 		mutex_exit(&os->os->os_user_ptr_lock);
23955446Sahrens 	}
23965446Sahrens 
23975446Sahrens 	if (zfsvfs != NULL) {
23988012SEric.Taylor@Sun.COM 		char *osname;
23995446Sahrens 		int mode;
24005446Sahrens 
24018012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
24026083Sek110237 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
24036083Sek110237 		if (error == 0) {
24046083Sek110237 			int resume_err;
24056083Sek110237 
24066083Sek110237 			ASSERT(strcmp(osname, zc->zc_name) == 0);
24076083Sek110237 			error = dmu_objset_rollback(os);
24086083Sek110237 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
24096083Sek110237 			error = error ? error : resume_err;
24106083Sek110237 		} else {
24116083Sek110237 			dmu_objset_close(os);
24126083Sek110237 		}
24138012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
24145446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
24155446Sahrens 	} else {
24165446Sahrens 		error = dmu_objset_rollback(os);
24175446Sahrens 	}
24186689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
24195446Sahrens 
24205446Sahrens 	return (error);
2421789Sahrens }
2422789Sahrens 
24235367Sahrens /*
24245367Sahrens  * inputs:
24255367Sahrens  * zc_name	old name of dataset
24265367Sahrens  * zc_value	new name of dataset
24275367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
24285367Sahrens  *
24295367Sahrens  * outputs:	none
24305367Sahrens  */
2431789Sahrens static int
2432789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2433789Sahrens {
24344490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
24354007Smmusante 
24362676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
24375326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
24385326Sek110237 	    strchr(zc->zc_value, '%'))
2439789Sahrens 		return (EINVAL);
2440789Sahrens 
24414007Smmusante 	/*
24424007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
24434007Smmusante 	 * in which case the dataset code figures out which snapshots
24444007Smmusante 	 * to unmount.
24454007Smmusante 	 */
24464007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2447789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
24482199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
24492199Sahrens 		if (err)
24502199Sahrens 			return (err);
2451789Sahrens 	}
24524007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2453789Sahrens }
2454789Sahrens 
24556689Smaybee static void
24568536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
24576689Smaybee {
24586689Smaybee 	zfs_cmd_t *zc;
24596689Smaybee 	nvpair_t *prop;
24606689Smaybee 
24616689Smaybee 	if (props == NULL)
24626689Smaybee 		return;
24636689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
24646689Smaybee 	(void) strcpy(zc->zc_name, dataset);
24656689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
24666689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
24678536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
24688536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
24698536SDavid.Pacheco@Sun.COM 			continue;
24706689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
24716689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
24726689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
24736689Smaybee 	}
24746689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
24756689Smaybee }
24766689Smaybee 
24775367Sahrens /*
24785367Sahrens  * inputs:
24795367Sahrens  * zc_name		name of containing filesystem
24805367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
24815367Sahrens  * zc_value		name of snapshot to create
24825367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
24835367Sahrens  * zc_cookie		file descriptor to recv from
24845367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
24855367Sahrens  * zc_guid		force flag
24865367Sahrens  *
24875367Sahrens  * outputs:
24885367Sahrens  * zc_cookie		number of bytes read
24895367Sahrens  */
2490789Sahrens static int
24915367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2492789Sahrens {
2493789Sahrens 	file_t *fp;
24945326Sek110237 	objset_t *os;
24955367Sahrens 	dmu_recv_cookie_t drc;
24965326Sek110237 	zfsvfs_t *zfsvfs = NULL;
24975326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2498789Sahrens 	int error, fd;
24995367Sahrens 	offset_t off;
25005367Sahrens 	nvlist_t *props = NULL;
25016689Smaybee 	nvlist_t *origprops = NULL;
25025367Sahrens 	objset_t *origin = NULL;
25035367Sahrens 	char *tosnap;
25045367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2505789Sahrens 
25063265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
25075326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
25085326Sek110237 	    strchr(zc->zc_value, '%'))
25093265Sahrens 		return (EINVAL);
25103265Sahrens 
25115367Sahrens 	(void) strcpy(tofs, zc->zc_value);
25125367Sahrens 	tosnap = strchr(tofs, '@');
25135367Sahrens 	*tosnap = '\0';
25145367Sahrens 	tosnap++;
25155367Sahrens 
25165367Sahrens 	if (zc->zc_nvlist_src != NULL &&
25175367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
25185367Sahrens 	    &props)) != 0)
25195367Sahrens 		return (error);
25205367Sahrens 
2521789Sahrens 	fd = zc->zc_cookie;
2522789Sahrens 	fp = getf(fd);
25235367Sahrens 	if (fp == NULL) {
25245367Sahrens 		nvlist_free(props);
2525789Sahrens 		return (EBADF);
25265367Sahrens 	}
25275326Sek110237 
25286689Smaybee 	if (dmu_objset_open(tofs, DMU_OST_ANY,
25296689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
25306689Smaybee 		/*
25316689Smaybee 		 * Try to get the zfsvfs for the receiving objset.
25326689Smaybee 		 * There won't be one if we're operating on a zvol,
25336689Smaybee 		 * if the objset doesn't exist yet, or is not mounted.
25346689Smaybee 		 */
25355446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
25366689Smaybee 		if (zfsvfs = dmu_objset_get_user(os)) {
25376083Sek110237 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
25386689Smaybee 				mutex_exit(&os->os->os_user_ptr_lock);
25396083Sek110237 				dmu_objset_close(os);
25406689Smaybee 				zfsvfs = NULL;
25416689Smaybee 				error = EBUSY;
25426689Smaybee 				goto out;
25436083Sek110237 			}
25446689Smaybee 			VFS_HOLD(zfsvfs->z_vfs);
25456083Sek110237 		}
25466689Smaybee 		mutex_exit(&os->os->os_user_ptr_lock);
25476689Smaybee 
25486689Smaybee 		/*
25496689Smaybee 		 * If new properties are supplied, they are to completely
25506689Smaybee 		 * replace the existing ones, so stash away the existing ones.
25516689Smaybee 		 */
25526689Smaybee 		if (props)
25536689Smaybee 			(void) dsl_prop_get_all(os, &origprops, TRUE);
25546689Smaybee 
25555326Sek110237 		dmu_objset_close(os);
25565326Sek110237 	}
25575326Sek110237 
25585367Sahrens 	if (zc->zc_string[0]) {
25595367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
25606689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
25616689Smaybee 		if (error)
25626689Smaybee 			goto out;
25635367Sahrens 	}
25645367Sahrens 
25655367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
25665367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
25675367Sahrens 	if (origin)
25685367Sahrens 		dmu_objset_close(origin);
25696689Smaybee 	if (error)
25706689Smaybee 		goto out;
25715326Sek110237 
25725326Sek110237 	/*
25736689Smaybee 	 * Reset properties.  We do this before we receive the stream
25746689Smaybee 	 * so that the properties are applied to the new data.
25755326Sek110237 	 */
25765367Sahrens 	if (props) {
25778536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
25786689Smaybee 		/*
25796689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
25806689Smaybee 		 */
25816689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
25825367Sahrens 	}
25835367Sahrens 
25845367Sahrens 	off = fp->f_offset;
25855367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
25865367Sahrens 
25876689Smaybee 	if (error == 0 && zfsvfs) {
25888012SEric.Taylor@Sun.COM 		char *osname;
25896689Smaybee 		int mode;
25906689Smaybee 
25916689Smaybee 		/* online recv */
25928012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25936689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
25946689Smaybee 		if (error == 0) {
25956689Smaybee 			int resume_err;
25966689Smaybee 
25976689Smaybee 			error = dmu_recv_end(&drc);
25986689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
25996689Smaybee 			error = error ? error : resume_err;
26005367Sahrens 		} else {
26016689Smaybee 			dmu_recv_abort_cleanup(&drc);
26025367Sahrens 		}
26038012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
26046689Smaybee 	} else if (error == 0) {
26056689Smaybee 		error = dmu_recv_end(&drc);
26066083Sek110237 	}
26075367Sahrens 
26085367Sahrens 	zc->zc_cookie = off - fp->f_offset;
26095367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
26105367Sahrens 		fp->f_offset = off;
26112885Sahrens 
26126689Smaybee 	/*
26136689Smaybee 	 * On error, restore the original props.
26146689Smaybee 	 */
26156689Smaybee 	if (error && props) {
26168536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
26176689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
26186689Smaybee 	}
26196689Smaybee out:
26206689Smaybee 	if (zfsvfs) {
26216689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
26226689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
26236689Smaybee 	}
26246689Smaybee 	nvlist_free(props);
26256689Smaybee 	nvlist_free(origprops);
2626789Sahrens 	releasef(fd);
2627789Sahrens 	return (error);
2628789Sahrens }
2629789Sahrens 
26305367Sahrens /*
26315367Sahrens  * inputs:
26325367Sahrens  * zc_name	name of snapshot to send
26335367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
26345367Sahrens  * zc_cookie	file descriptor to send stream to
26355367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
26365367Sahrens  *
26375367Sahrens  * outputs: none
26385367Sahrens  */
2639789Sahrens static int
26405367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2641789Sahrens {
2642789Sahrens 	objset_t *fromsnap = NULL;
2643789Sahrens 	objset_t *tosnap;
2644789Sahrens 	file_t *fp;
2645789Sahrens 	int error;
26465367Sahrens 	offset_t off;
2647789Sahrens 
2648789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
26496689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2650789Sahrens 	if (error)
2651789Sahrens 		return (error);
2652789Sahrens 
26532676Seschrock 	if (zc->zc_value[0] != '\0') {
26548012SEric.Taylor@Sun.COM 		char *buf;
26552885Sahrens 		char *cp;
26562885Sahrens 
26578012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26588012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
26592885Sahrens 		cp = strchr(buf, '@');
26602885Sahrens 		if (cp)
26612885Sahrens 			*(cp+1) = 0;
26628012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
26632885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
26646689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
26658012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2666789Sahrens 		if (error) {
2667789Sahrens 			dmu_objset_close(tosnap);
2668789Sahrens 			return (error);
2669789Sahrens 		}
2670789Sahrens 	}
2671789Sahrens 
2672789Sahrens 	fp = getf(zc->zc_cookie);
2673789Sahrens 	if (fp == NULL) {
2674789Sahrens 		dmu_objset_close(tosnap);
2675789Sahrens 		if (fromsnap)
2676789Sahrens 			dmu_objset_close(fromsnap);
2677789Sahrens 		return (EBADF);
2678789Sahrens 	}
2679789Sahrens 
26805367Sahrens 	off = fp->f_offset;
26815367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
26825367Sahrens 
26835367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
26845367Sahrens 		fp->f_offset = off;
2685789Sahrens 	releasef(zc->zc_cookie);
2686789Sahrens 	if (fromsnap)
2687789Sahrens 		dmu_objset_close(fromsnap);
2688789Sahrens 	dmu_objset_close(tosnap);
2689789Sahrens 	return (error);
2690789Sahrens }
2691789Sahrens 
26921544Seschrock static int
26931544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
26941544Seschrock {
26951544Seschrock 	int id, error;
26961544Seschrock 
26971544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
26981544Seschrock 	    &zc->zc_inject_record);
26991544Seschrock 
27001544Seschrock 	if (error == 0)
27011544Seschrock 		zc->zc_guid = (uint64_t)id;
27021544Seschrock 
27031544Seschrock 	return (error);
27041544Seschrock }
27051544Seschrock 
27061544Seschrock static int
27071544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
27081544Seschrock {
27091544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
27101544Seschrock }
27111544Seschrock 
27121544Seschrock static int
27131544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
27141544Seschrock {
27151544Seschrock 	int id = (int)zc->zc_guid;
27161544Seschrock 	int error;
27171544Seschrock 
27181544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
27191544Seschrock 	    &zc->zc_inject_record);
27201544Seschrock 
27211544Seschrock 	zc->zc_guid = id;
27221544Seschrock 
27231544Seschrock 	return (error);
27241544Seschrock }
27251544Seschrock 
27261544Seschrock static int
27271544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
27281544Seschrock {
27291544Seschrock 	spa_t *spa;
27301544Seschrock 	int error;
27312676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
27321544Seschrock 
27331544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27341544Seschrock 		return (error);
27351544Seschrock 
27362676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
27371544Seschrock 	    &count);
27381544Seschrock 	if (error == 0)
27392676Seschrock 		zc->zc_nvlist_dst_size = count;
27401544Seschrock 	else
27412676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
27421544Seschrock 
27431544Seschrock 	spa_close(spa, FTAG);
27441544Seschrock 
27451544Seschrock 	return (error);
27461544Seschrock }
27471544Seschrock 
27481544Seschrock static int
27491544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
27501544Seschrock {
27511544Seschrock 	spa_t *spa;
27521544Seschrock 	vdev_t *vd;
27531544Seschrock 	int error;
27541544Seschrock 
27557294Sperrin 	/*
27567294Sperrin 	 * On zpool clear we also fix up missing slogs
27577294Sperrin 	 */
27587294Sperrin 	mutex_enter(&spa_namespace_lock);
27597294Sperrin 	spa = spa_lookup(zc->zc_name);
27607294Sperrin 	if (spa == NULL) {
27617294Sperrin 		mutex_exit(&spa_namespace_lock);
27627294Sperrin 		return (EIO);
27637294Sperrin 	}
27647294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
27657294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
27667294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
27677294Sperrin 	}
27687294Sperrin 	mutex_exit(&spa_namespace_lock);
27697294Sperrin 
27701544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27711544Seschrock 		return (error);
27721544Seschrock 
27737754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
27741544Seschrock 
27752676Seschrock 	if (zc->zc_guid == 0) {
27761544Seschrock 		vd = NULL;
27776643Seschrock 	} else {
27786643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
27795450Sbrendan 		if (vd == NULL) {
27807754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
27815450Sbrendan 			spa_close(spa, FTAG);
27825450Sbrendan 			return (ENODEV);
27835450Sbrendan 		}
27841544Seschrock 	}
27851544Seschrock 
27867754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
27877754SJeff.Bonwick@Sun.COM 
27887754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
27897754SJeff.Bonwick@Sun.COM 
27907754SJeff.Bonwick@Sun.COM 	/*
27917754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
27927754SJeff.Bonwick@Sun.COM 	 */
2793*9234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
2794*9234SGeorge.Wilson@Sun.COM 		error = EIO;
27951544Seschrock 
27961544Seschrock 	spa_close(spa, FTAG);
27971544Seschrock 
2798*9234SGeorge.Wilson@Sun.COM 	return (error);
27991544Seschrock }
28001544Seschrock 
28015367Sahrens /*
28025367Sahrens  * inputs:
28035367Sahrens  * zc_name	name of filesystem
28045367Sahrens  * zc_value	name of origin snapshot
28055367Sahrens  *
28065367Sahrens  * outputs:	none
28075367Sahrens  */
28081544Seschrock static int
28092082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
28102082Seschrock {
28112417Sahrens 	char *cp;
28122417Sahrens 
28132417Sahrens 	/*
28142417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
28152417Sahrens 	 * it's easier.
28162417Sahrens 	 */
28172676Seschrock 	cp = strchr(zc->zc_value, '@');
28182417Sahrens 	if (cp)
28192417Sahrens 		*cp = '\0';
28202676Seschrock 	(void) dmu_objset_find(zc->zc_value,
28212417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
28222082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
28232082Seschrock }
28242082Seschrock 
28254543Smarks /*
28264543Smarks  * We don't want to have a hard dependency
28274543Smarks  * against some special symbols in sharefs
28285331Samw  * nfs, and smbsrv.  Determine them if needed when
28294543Smarks  * the first file system is shared.
28305331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
28314543Smarks  */
28325331Samw int (*znfsexport_fs)(void *arg);
28334543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
28345331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
28355331Samw 
28365331Samw int zfs_nfsshare_inited;
28375331Samw int zfs_smbshare_inited;
28385331Samw 
28394543Smarks ddi_modhandle_t nfs_mod;
28404543Smarks ddi_modhandle_t sharefs_mod;
28415331Samw ddi_modhandle_t smbsrv_mod;
28424543Smarks kmutex_t zfs_share_lock;
28434543Smarks 
28444543Smarks static int
28455331Samw zfs_init_sharefs()
28465331Samw {
28475331Samw 	int error;
28485331Samw 
28495331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
28505331Samw 	/* Both NFS and SMB shares also require sharetab support. */
28515331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
28525331Samw 	    ddi_modopen("fs/sharefs",
28535331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
28545331Samw 		return (ENOSYS);
28555331Samw 	}
28565331Samw 	if (zshare_fs == NULL && ((zshare_fs =
28575331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
28585331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
28595331Samw 		return (ENOSYS);
28605331Samw 	}
28615331Samw 	return (0);
28625331Samw }
28635331Samw 
28645331Samw static int
28654543Smarks zfs_ioc_share(zfs_cmd_t *zc)
28664543Smarks {
28674543Smarks 	int error;
28684543Smarks 	int opcode;
28694543Smarks 
28705331Samw 	switch (zc->zc_share.z_sharetype) {
28715331Samw 	case ZFS_SHARE_NFS:
28725331Samw 	case ZFS_UNSHARE_NFS:
28735331Samw 		if (zfs_nfsshare_inited == 0) {
28745331Samw 			mutex_enter(&zfs_share_lock);
28755331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
28765331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
28775331Samw 				mutex_exit(&zfs_share_lock);
28785331Samw 				return (ENOSYS);
28795331Samw 			}
28805331Samw 			if (znfsexport_fs == NULL &&
28815331Samw 			    ((znfsexport_fs = (int (*)(void *))
28825331Samw 			    ddi_modsym(nfs_mod,
28835331Samw 			    "nfs_export", &error)) == NULL)) {
28845331Samw 				mutex_exit(&zfs_share_lock);
28855331Samw 				return (ENOSYS);
28865331Samw 			}
28875331Samw 			error = zfs_init_sharefs();
28885331Samw 			if (error) {
28895331Samw 				mutex_exit(&zfs_share_lock);
28905331Samw 				return (ENOSYS);
28915331Samw 			}
28925331Samw 			zfs_nfsshare_inited = 1;
28934543Smarks 			mutex_exit(&zfs_share_lock);
28944543Smarks 		}
28955331Samw 		break;
28965331Samw 	case ZFS_SHARE_SMB:
28975331Samw 	case ZFS_UNSHARE_SMB:
28985331Samw 		if (zfs_smbshare_inited == 0) {
28995331Samw 			mutex_enter(&zfs_share_lock);
29005331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
29015331Samw 			    ddi_modopen("drv/smbsrv",
29025331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
29035331Samw 				mutex_exit(&zfs_share_lock);
29045331Samw 				return (ENOSYS);
29055331Samw 			}
29065331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
29075331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
29086139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
29095331Samw 				mutex_exit(&zfs_share_lock);
29105331Samw 				return (ENOSYS);
29115331Samw 			}
29125331Samw 			error = zfs_init_sharefs();
29135331Samw 			if (error) {
29145331Samw 				mutex_exit(&zfs_share_lock);
29155331Samw 				return (ENOSYS);
29165331Samw 			}
29175331Samw 			zfs_smbshare_inited = 1;
29184543Smarks 			mutex_exit(&zfs_share_lock);
29194543Smarks 		}
29205331Samw 		break;
29215331Samw 	default:
29225331Samw 		return (EINVAL);
29234543Smarks 	}
29244543Smarks 
29255331Samw 	switch (zc->zc_share.z_sharetype) {
29265331Samw 	case ZFS_SHARE_NFS:
29275331Samw 	case ZFS_UNSHARE_NFS:
29285331Samw 		if (error =
29295331Samw 		    znfsexport_fs((void *)
29305331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
29315331Samw 			return (error);
29325331Samw 		break;
29335331Samw 	case ZFS_SHARE_SMB:
29345331Samw 	case ZFS_UNSHARE_SMB:
29355331Samw 		if (error = zsmbexport_fs((void *)
29365331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
29375331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
29388845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
29395331Samw 			return (error);
29405331Samw 		}
29415331Samw 		break;
29425331Samw 	}
29435331Samw 
29445331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
29455331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
29464543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
29474543Smarks 
29485331Samw 	/*
29495331Samw 	 * Add or remove share from sharetab
29505331Samw 	 */
29514543Smarks 	error = zshare_fs(opcode,
29524543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
29534543Smarks 	    zc->zc_share.z_sharemax);
29544543Smarks 
29554543Smarks 	return (error);
29564543Smarks 
29574543Smarks }
29584543Smarks 
29598845Samw@Sun.COM ace_t full_access[] = {
29608845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
29618845Samw@Sun.COM };
29628845Samw@Sun.COM 
29638845Samw@Sun.COM /*
29648845Samw@Sun.COM  * Remove all ACL files in shares dir
29658845Samw@Sun.COM  */
29668845Samw@Sun.COM static int
29678845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
29688845Samw@Sun.COM {
29698845Samw@Sun.COM 	zap_cursor_t	zc;
29708845Samw@Sun.COM 	zap_attribute_t	zap;
29718845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
29728845Samw@Sun.COM 	int error;
29738845Samw@Sun.COM 
29748845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
29758845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
29768845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
29778845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
29788845Samw@Sun.COM 		    NULL, 0)) != 0)
29798845Samw@Sun.COM 			break;
29808845Samw@Sun.COM 	}
29818845Samw@Sun.COM 	zap_cursor_fini(&zc);
29828845Samw@Sun.COM 	return (error);
29838845Samw@Sun.COM }
29848845Samw@Sun.COM 
29858845Samw@Sun.COM static int
29868845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
29878845Samw@Sun.COM {
29888845Samw@Sun.COM 	vnode_t *vp;
29898845Samw@Sun.COM 	znode_t *dzp;
29908845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
29918845Samw@Sun.COM 	znode_t *sharedir;
29928845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
29938845Samw@Sun.COM 	nvlist_t *nvlist;
29948845Samw@Sun.COM 	char *src, *target;
29958845Samw@Sun.COM 	vattr_t vattr;
29968845Samw@Sun.COM 	vsecattr_t vsec;
29978845Samw@Sun.COM 	int error = 0;
29988845Samw@Sun.COM 
29998845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
30008845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
30018845Samw@Sun.COM 		return (error);
30028845Samw@Sun.COM 
30038845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
30048845Samw@Sun.COM 
30058845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
30068845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
30078845Samw@Sun.COM 	    zc->zc_name) != 0)) {
30088845Samw@Sun.COM 		VN_RELE(vp);
30098845Samw@Sun.COM 		return (EINVAL);
30108845Samw@Sun.COM 	}
30118845Samw@Sun.COM 
30128845Samw@Sun.COM 	dzp = VTOZ(vp);
30138845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
30148845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
30158845Samw@Sun.COM 
30169030SMark.Shellenbaum@Sun.COM 	/*
30179030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
30189030SMark.Shellenbaum@Sun.COM 	 */
30199030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
30209030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
30219030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
30229030SMark.Shellenbaum@Sun.COM 
30239030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
30249030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
30259030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
30269030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
30279030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
30289030SMark.Shellenbaum@Sun.COM 		if (error) {
30299030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
30309030SMark.Shellenbaum@Sun.COM 		} else {
30319030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
30329030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
30339030SMark.Shellenbaum@Sun.COM 		}
30349030SMark.Shellenbaum@Sun.COM 		if (error) {
30359030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
30369030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
30379030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
30389030SMark.Shellenbaum@Sun.COM 			return (error);
30399030SMark.Shellenbaum@Sun.COM 		}
30409030SMark.Shellenbaum@Sun.COM 	}
30419030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
30429030SMark.Shellenbaum@Sun.COM 
30439030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
30448845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
30459030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
30468845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
30478845Samw@Sun.COM 		return (error);
30488845Samw@Sun.COM 	}
30498845Samw@Sun.COM 
30508845Samw@Sun.COM 	switch (zc->zc_cookie) {
30518845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
30528845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
30538845Samw@Sun.COM 		vattr.va_type = VREG;
30548845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
30558845Samw@Sun.COM 		vattr.va_uid = 0;
30568845Samw@Sun.COM 		vattr.va_gid = 0;
30578845Samw@Sun.COM 
30588845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
30598845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
30608845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
30618845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
30628845Samw@Sun.COM 
30638845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
30648845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
30658845Samw@Sun.COM 		if (resourcevp)
30668845Samw@Sun.COM 			VN_RELE(resourcevp);
30678845Samw@Sun.COM 		break;
30688845Samw@Sun.COM 
30698845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
30708845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
30718845Samw@Sun.COM 		    NULL, 0);
30728845Samw@Sun.COM 		break;
30738845Samw@Sun.COM 
30748845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
30758845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
30768845Samw@Sun.COM 		    zc->zc_nvlist_src_size, &nvlist)) != 0) {
30778845Samw@Sun.COM 			VN_RELE(vp);
30788845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
30798845Samw@Sun.COM 			return (error);
30808845Samw@Sun.COM 		}
30818845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
30828845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
30838845Samw@Sun.COM 		    &target)) {
30848845Samw@Sun.COM 			VN_RELE(vp);
30859179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
30868845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
30878845Samw@Sun.COM 			return (error);
30888845Samw@Sun.COM 		}
30898845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
30908845Samw@Sun.COM 		    kcred, NULL, 0);
30918845Samw@Sun.COM 		nvlist_free(nvlist);
30928845Samw@Sun.COM 		break;
30938845Samw@Sun.COM 
30948845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
30958845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
30968845Samw@Sun.COM 		break;
30978845Samw@Sun.COM 
30988845Samw@Sun.COM 	default:
30998845Samw@Sun.COM 		error = EINVAL;
31008845Samw@Sun.COM 		break;
31018845Samw@Sun.COM 	}
31028845Samw@Sun.COM 
31038845Samw@Sun.COM 	VN_RELE(vp);
31048845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
31058845Samw@Sun.COM 
31068845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
31078845Samw@Sun.COM 
31088845Samw@Sun.COM 	return (error);
31098845Samw@Sun.COM }
31108845Samw@Sun.COM 
31114543Smarks /*
31124988Sek110237  * pool create, destroy, and export don't log the history as part of
31134988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
31144988Sek110237  * do the logging of those commands.
31154543Smarks  */
3116789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
3117*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3118*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3119*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
3120*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3121*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3122*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3123*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3124*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3125*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
3126*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3127*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3128*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3129*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
3130*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3131*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3132*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3133*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
3134*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3135*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
3136*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3137*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3138*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3139*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3140*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3141*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3142*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3143*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
3144*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3145*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3146*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3147*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3148*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3149*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
3150*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3151*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3152*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3153*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3154*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3155*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3156*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3157*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3158*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3159*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
3160*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3161*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3162*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3163*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3164*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
3165*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
3166*9234SGeorge.Wilson@Sun.COM 	    B_TRUE},
3167*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
3168*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3169*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
3170*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
3171*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
3172*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
3173*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3174*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3175*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3176*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3177*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3178*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
3179*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3180*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
3181*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
3182*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3183*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
3184*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3185*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
3186*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3187*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3188*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3189*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
3190*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3191*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
3192*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3193*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3194*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3195*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
3196*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3197*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3198*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3199*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
3200*9234SGeorge.Wilson@Sun.COM 	    B_FALSE },
3201*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
3202*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
3203*9234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3204*9234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
3205*9234SGeorge.Wilson@Sun.COM 	    B_FALSE }
3206789Sahrens };
3207789Sahrens 
3208*9234SGeorge.Wilson@Sun.COM int
3209*9234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
3210*9234SGeorge.Wilson@Sun.COM {
3211*9234SGeorge.Wilson@Sun.COM 	spa_t *spa;
3212*9234SGeorge.Wilson@Sun.COM 	char pool[ZFS_MAXNAMELEN];
3213*9234SGeorge.Wilson@Sun.COM 	int error;
3214*9234SGeorge.Wilson@Sun.COM 
3215*9234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
3216*9234SGeorge.Wilson@Sun.COM 
3217*9234SGeorge.Wilson@Sun.COM 	(void) strlcpy(pool, name, ZFS_MAXNAMELEN);
3218*9234SGeorge.Wilson@Sun.COM 	if (type == DATASET_NAME) {
3219*9234SGeorge.Wilson@Sun.COM 		char *p;
3220*9234SGeorge.Wilson@Sun.COM 
3221*9234SGeorge.Wilson@Sun.COM 		if ((p = strpbrk(pool, "/@")) != NULL)
3222*9234SGeorge.Wilson@Sun.COM 			*p = '\0';
3223*9234SGeorge.Wilson@Sun.COM 	}
3224*9234SGeorge.Wilson@Sun.COM 
3225*9234SGeorge.Wilson@Sun.COM 	error = spa_open(pool, &spa, FTAG);
3226*9234SGeorge.Wilson@Sun.COM 	if (error == 0) {
3227*9234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
3228*9234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
3229*9234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
3230*9234SGeorge.Wilson@Sun.COM 	}
3231*9234SGeorge.Wilson@Sun.COM 	return (error);
3232*9234SGeorge.Wilson@Sun.COM }
3233*9234SGeorge.Wilson@Sun.COM 
3234789Sahrens static int
3235789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3236789Sahrens {
3237789Sahrens 	zfs_cmd_t *zc;
3238789Sahrens 	uint_t vec;
32392199Sahrens 	int error, rc;
3240789Sahrens 
3241789Sahrens 	if (getminor(dev) != 0)
3242789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3243789Sahrens 
3244789Sahrens 	vec = cmd - ZFS_IOC;
32454787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3246789Sahrens 
3247789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3248789Sahrens 		return (EINVAL);
3249789Sahrens 
3250789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3251789Sahrens 
3252789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3253789Sahrens 
32544787Sahrens 	if (error == 0)
32554543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3256789Sahrens 
3257789Sahrens 	/*
3258789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3259789Sahrens 	 * the lower layers.
3260789Sahrens 	 */
3261789Sahrens 	if (error == 0) {
3262789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3263789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
32644577Sahrens 		case POOL_NAME:
3265789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3266789Sahrens 				error = EINVAL;
3267*9234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
3268*9234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
3269*9234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3270789Sahrens 			break;
3271789Sahrens 
32724577Sahrens 		case DATASET_NAME:
3273789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3274789Sahrens 				error = EINVAL;
3275*9234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
3276*9234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
3277*9234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3278789Sahrens 			break;
32792856Snd150628 
32804577Sahrens 		case NO_NAME:
32812856Snd150628 			break;
3282789Sahrens 		}
3283789Sahrens 	}
3284789Sahrens 
3285789Sahrens 	if (error == 0)
3286789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3287789Sahrens 
32882199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
32894543Smarks 	if (error == 0) {
32902199Sahrens 		error = rc;
32914543Smarks 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
32924543Smarks 			zfs_log_history(zc);
32934543Smarks 	}
3294789Sahrens 
3295789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3296789Sahrens 	return (error);
3297789Sahrens }
3298789Sahrens 
3299789Sahrens static int
3300789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3301789Sahrens {
3302789Sahrens 	if (cmd != DDI_ATTACH)
3303789Sahrens 		return (DDI_FAILURE);
3304789Sahrens 
3305789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3306789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3307789Sahrens 		return (DDI_FAILURE);
3308789Sahrens 
3309789Sahrens 	zfs_dip = dip;
3310789Sahrens 
3311789Sahrens 	ddi_report_dev(dip);
3312789Sahrens 
3313789Sahrens 	return (DDI_SUCCESS);
3314789Sahrens }
3315789Sahrens 
3316789Sahrens static int
3317789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3318789Sahrens {
3319789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3320789Sahrens 		return (DDI_FAILURE);
3321789Sahrens 
3322789Sahrens 	if (cmd != DDI_DETACH)
3323789Sahrens 		return (DDI_FAILURE);
3324789Sahrens 
3325789Sahrens 	zfs_dip = NULL;
3326789Sahrens 
3327789Sahrens 	ddi_prop_remove_all(dip);
3328789Sahrens 	ddi_remove_minor_node(dip, NULL);
3329789Sahrens 
3330789Sahrens 	return (DDI_SUCCESS);
3331789Sahrens }
3332789Sahrens 
3333789Sahrens /*ARGSUSED*/
3334789Sahrens static int
3335789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3336789Sahrens {
3337789Sahrens 	switch (infocmd) {
3338789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3339789Sahrens 		*result = zfs_dip;
3340789Sahrens 		return (DDI_SUCCESS);
3341789Sahrens 
3342789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3343849Sbonwick 		*result = (void *)0;
3344789Sahrens 		return (DDI_SUCCESS);
3345789Sahrens 	}
3346789Sahrens 
3347789Sahrens 	return (DDI_FAILURE);
3348789Sahrens }
3349789Sahrens 
3350789Sahrens /*
3351789Sahrens  * OK, so this is a little weird.
3352789Sahrens  *
3353789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3354789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3355789Sahrens  *
3356789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3357789Sahrens  * so most of the standard driver entry points are in zvol.c.
3358789Sahrens  */
3359789Sahrens static struct cb_ops zfs_cb_ops = {
3360789Sahrens 	zvol_open,	/* open */
3361789Sahrens 	zvol_close,	/* close */
3362789Sahrens 	zvol_strategy,	/* strategy */
3363789Sahrens 	nodev,		/* print */
33646423Sgw25295 	zvol_dump,	/* dump */
3365789Sahrens 	zvol_read,	/* read */
3366789Sahrens 	zvol_write,	/* write */
3367789Sahrens 	zfsdev_ioctl,	/* ioctl */
3368789Sahrens 	nodev,		/* devmap */
3369789Sahrens 	nodev,		/* mmap */
3370789Sahrens 	nodev,		/* segmap */
3371789Sahrens 	nochpoll,	/* poll */
3372789Sahrens 	ddi_prop_op,	/* prop_op */
3373789Sahrens 	NULL,		/* streamtab */
3374789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3375789Sahrens 	CB_REV,		/* version */
33763638Sbillm 	nodev,		/* async read */
33773638Sbillm 	nodev,		/* async write */
3378789Sahrens };
3379789Sahrens 
3380789Sahrens static struct dev_ops zfs_dev_ops = {
3381789Sahrens 	DEVO_REV,	/* version */
3382789Sahrens 	0,		/* refcnt */
3383789Sahrens 	zfs_info,	/* info */
3384789Sahrens 	nulldev,	/* identify */
3385789Sahrens 	nulldev,	/* probe */
3386789Sahrens 	zfs_attach,	/* attach */
3387789Sahrens 	zfs_detach,	/* detach */
3388789Sahrens 	nodev,		/* reset */
3389789Sahrens 	&zfs_cb_ops,	/* driver operations */
33907656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
33917656SSherry.Moore@Sun.COM 	NULL,		/* power */
33927656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3393789Sahrens };
3394789Sahrens 
3395789Sahrens static struct modldrv zfs_modldrv = {
33967656SSherry.Moore@Sun.COM 	&mod_driverops,
33977656SSherry.Moore@Sun.COM 	"ZFS storage pool",
33987656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3399789Sahrens };
3400789Sahrens 
3401789Sahrens static struct modlinkage modlinkage = {
3402789Sahrens 	MODREV_1,
3403789Sahrens 	(void *)&zfs_modlfs,
3404789Sahrens 	(void *)&zfs_modldrv,
3405789Sahrens 	NULL
3406789Sahrens };
3407789Sahrens 
34084720Sfr157268 
34094720Sfr157268 uint_t zfs_fsyncer_key;
34105326Sek110237 extern uint_t rrw_tsd_key;
34114720Sfr157268 
3412789Sahrens int
3413789Sahrens _init(void)
3414789Sahrens {
3415789Sahrens 	int error;
3416789Sahrens 
3417849Sbonwick 	spa_init(FREAD | FWRITE);
3418849Sbonwick 	zfs_init();
3419849Sbonwick 	zvol_init();
3420849Sbonwick 
3421849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3422849Sbonwick 		zvol_fini();
3423849Sbonwick 		zfs_fini();
3424849Sbonwick 		spa_fini();
3425789Sahrens 		return (error);
3426849Sbonwick 	}
3427789Sahrens 
34284720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
34295326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
34304720Sfr157268 
3431789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3432789Sahrens 	ASSERT(error == 0);
34334543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3434789Sahrens 
3435789Sahrens 	return (0);
3436789Sahrens }
3437789Sahrens 
3438789Sahrens int
3439789Sahrens _fini(void)
3440789Sahrens {
3441789Sahrens 	int error;
3442789Sahrens 
34431544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3444789Sahrens 		return (EBUSY);
3445789Sahrens 
3446789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3447789Sahrens 		return (error);
3448789Sahrens 
3449789Sahrens 	zvol_fini();
3450789Sahrens 	zfs_fini();
3451789Sahrens 	spa_fini();
34525331Samw 	if (zfs_nfsshare_inited)
34534543Smarks 		(void) ddi_modclose(nfs_mod);
34545331Samw 	if (zfs_smbshare_inited)
34555331Samw 		(void) ddi_modclose(smbsrv_mod);
34565331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
34574543Smarks 		(void) ddi_modclose(sharefs_mod);
3458789Sahrens 
34594720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3460789Sahrens 	ldi_ident_release(zfs_li);
3461789Sahrens 	zfs_li = NULL;
34624543Smarks 	mutex_destroy(&zfs_share_lock);
3463789Sahrens 
3464789Sahrens 	return (error);
3465789Sahrens }
3466789Sahrens 
3467789Sahrens int
3468789Sahrens _info(struct modinfo *modinfop)
3469789Sahrens {
3470789Sahrens 	return (mod_info(&modlinkage, modinfop));
3471789Sahrens }
3472