xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 9355:09928982c591)
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 
829234SGeorge.Wilson@Sun.COM typedef enum {
839234SGeorge.Wilson@Sun.COM 	NO_NAME,
849234SGeorge.Wilson@Sun.COM 	POOL_NAME,
859234SGeorge.Wilson@Sun.COM 	DATASET_NAME
869234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
879234SGeorge.Wilson@Sun.COM 
88789Sahrens typedef struct zfs_ioc_vec {
89789Sahrens 	zfs_ioc_func_t		*zvec_func;
90789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
919234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
924543Smarks 	boolean_t		zvec_his_log;
939234SGeorge.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 /*
1626*9355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
1627*9355SMatthew.Ahrens@Sun.COM  */
1628*9355SMatthew.Ahrens@Sun.COM static int
1629*9355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
1630*9355SMatthew.Ahrens@Sun.COM {
1631*9355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
1632*9355SMatthew.Ahrens@Sun.COM 	int error = 0;
1633*9355SMatthew.Ahrens@Sun.COM 
1634*9355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1635*9355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
1636*9355SMatthew.Ahrens@Sun.COM 		char *valstr;
1637*9355SMatthew.Ahrens@Sun.COM 
1638*9355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
1639*9355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
1640*9355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
1641*9355SMatthew.Ahrens@Sun.COM 
1642*9355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
1643*9355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
1644*9355SMatthew.Ahrens@Sun.COM 			return (error);
1645*9355SMatthew.Ahrens@Sun.COM 
1646*9355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
1647*9355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
1648*9355SMatthew.Ahrens@Sun.COM 
1649*9355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
1650*9355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
1651*9355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
1652*9355SMatthew.Ahrens@Sun.COM 	}
1653*9355SMatthew.Ahrens@Sun.COM 	return (0);
1654*9355SMatthew.Ahrens@Sun.COM }
1655*9355SMatthew.Ahrens@Sun.COM 
1656*9355SMatthew.Ahrens@Sun.COM /*
16575367Sahrens  * inputs:
16585367Sahrens  * zc_name		name of filesystem
16598697SRichard.Morris@Sun.COM  * zc_value		name of property to set
16605367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
16617265Sahrens  * zc_cookie		clear existing local props?
16625367Sahrens  *
16635367Sahrens  * outputs:		none
16645367Sahrens  */
1665789Sahrens static int
16662676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1667789Sahrens {
16682676Seschrock 	nvlist_t *nvl;
16692676Seschrock 	int error;
1670789Sahrens 
16715094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
16725094Slling 	    &nvl)) != 0)
16732676Seschrock 		return (error);
16742676Seschrock 
16757265Sahrens 	if (zc->zc_cookie) {
16767265Sahrens 		nvlist_t *origprops;
16777265Sahrens 		objset_t *os;
16787265Sahrens 
16797265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
16807265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
16817265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
16828536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
16837265Sahrens 				nvlist_free(origprops);
16847265Sahrens 			}
16857265Sahrens 			dmu_objset_close(os);
16867265Sahrens 		}
16877265Sahrens 
16887265Sahrens 	}
16897265Sahrens 
16904787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
16914543Smarks 
16922676Seschrock 	nvlist_free(nvl);
16932676Seschrock 	return (error);
1694789Sahrens }
1695789Sahrens 
16965367Sahrens /*
16975367Sahrens  * inputs:
16985367Sahrens  * zc_name		name of filesystem
16995367Sahrens  * zc_value		name of property to inherit
17005367Sahrens  *
17015367Sahrens  * outputs:		none
17025367Sahrens  */
1703789Sahrens static int
17044849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
17054849Sahrens {
17064849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
17074849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
17084849Sahrens }
17094849Sahrens 
17104849Sahrens static int
17114098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
17123912Slling {
17135094Slling 	nvlist_t *props;
17143912Slling 	spa_t *spa;
17155094Slling 	int error;
17168525SEric.Schrock@Sun.COM 	nvpair_t *elem;
17173912Slling 
17185094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17195094Slling 	    &props)))
17203912Slling 		return (error);
17213912Slling 
17228525SEric.Schrock@Sun.COM 	/*
17238525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
17248525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
17258525SEric.Schrock@Sun.COM 	 */
17268525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
17278525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
17288525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
17298525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
17308525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
17318525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
17328525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
17338525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
17348525SEric.Schrock@Sun.COM 		}
17358525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
17368525SEric.Schrock@Sun.COM 		if (spa != NULL)
17378525SEric.Schrock@Sun.COM 			return (0);
17388525SEric.Schrock@Sun.COM 	}
17398525SEric.Schrock@Sun.COM 
17403912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
17415094Slling 		nvlist_free(props);
17423912Slling 		return (error);
17433912Slling 	}
17443912Slling 
17455094Slling 	error = spa_prop_set(spa, props);
17463912Slling 
17475094Slling 	nvlist_free(props);
17483912Slling 	spa_close(spa, FTAG);
17493912Slling 
17503912Slling 	return (error);
17513912Slling }
17523912Slling 
17533912Slling static int
17544098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
17553912Slling {
17563912Slling 	spa_t *spa;
17573912Slling 	int error;
17583912Slling 	nvlist_t *nvp = NULL;
17593912Slling 
17608525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
17618525SEric.Schrock@Sun.COM 		/*
17628525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
17638525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
17648525SEric.Schrock@Sun.COM 		 * anyway.
17658525SEric.Schrock@Sun.COM 		 */
17668525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
17678525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
17688525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
17698525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
17708525SEric.Schrock@Sun.COM 	} else {
17718525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
17728525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
17738525SEric.Schrock@Sun.COM 	}
17743912Slling 
17753912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
17763912Slling 		error = put_nvlist(zc, nvp);
17773912Slling 	else
17783912Slling 		error = EFAULT;
17793912Slling 
17808525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
17813912Slling 	return (error);
17823912Slling }
17833912Slling 
17843912Slling static int
17854543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
17864543Smarks {
17874543Smarks 	nvlist_t *nvp;
17884543Smarks 	int error;
17894543Smarks 	uint32_t uid;
17904543Smarks 	uint32_t gid;
17914543Smarks 	uint32_t *groups;
17924543Smarks 	uint_t group_cnt;
17934543Smarks 	cred_t	*usercred;
17944543Smarks 
17955094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17965094Slling 	    &nvp)) != 0) {
17974543Smarks 		return (error);
17984543Smarks 	}
17994543Smarks 
18004543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
18014543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
18024543Smarks 		nvlist_free(nvp);
18034543Smarks 		return (EPERM);
18044543Smarks 	}
18054543Smarks 
18064543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
18074543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
18084543Smarks 		nvlist_free(nvp);
18094543Smarks 		return (EPERM);
18104543Smarks 	}
18114543Smarks 
18124543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
18134543Smarks 	    &groups, &group_cnt)) != 0) {
18144543Smarks 		nvlist_free(nvp);
18154543Smarks 		return (EPERM);
18164543Smarks 	}
18174543Smarks 	usercred = cralloc();
18184543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
18194543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
18204543Smarks 		nvlist_free(nvp);
18214543Smarks 		crfree(usercred);
18224543Smarks 		return (EPERM);
18234543Smarks 	}
18244543Smarks 	nvlist_free(nvp);
18254543Smarks 	error = dsl_deleg_access(zc->zc_name,
18264787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
18274543Smarks 	crfree(usercred);
18284543Smarks 	return (error);
18294543Smarks }
18304543Smarks 
18315367Sahrens /*
18325367Sahrens  * inputs:
18335367Sahrens  * zc_name		name of filesystem
18345367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
18355367Sahrens  * zc_perm_action	allow/unallow flag
18365367Sahrens  *
18375367Sahrens  * outputs:		none
18385367Sahrens  */
18394543Smarks static int
18404543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
18414543Smarks {
18424543Smarks 	int error;
18434543Smarks 	nvlist_t *fsaclnv = NULL;
18444543Smarks 
18455094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18465094Slling 	    &fsaclnv)) != 0)
18474543Smarks 		return (error);
18484543Smarks 
18494543Smarks 	/*
18504543Smarks 	 * Verify nvlist is constructed correctly
18514543Smarks 	 */
18524543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
18534543Smarks 		nvlist_free(fsaclnv);
18544543Smarks 		return (EINVAL);
18554543Smarks 	}
18564543Smarks 
18574543Smarks 	/*
18584543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
18594543Smarks 	 * that user is allowed to hand out each permission in
18604543Smarks 	 * the nvlist(s)
18614543Smarks 	 */
18624543Smarks 
18634787Sahrens 	error = secpolicy_zfs(CRED());
18644543Smarks 	if (error) {
18654787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
18664787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
18674787Sahrens 			    fsaclnv, CRED());
18684787Sahrens 		} else {
18694787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
18704787Sahrens 			    fsaclnv, CRED());
18714787Sahrens 		}
18724543Smarks 	}
18734543Smarks 
18744543Smarks 	if (error == 0)
18754543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
18764543Smarks 
18774543Smarks 	nvlist_free(fsaclnv);
18784543Smarks 	return (error);
18794543Smarks }
18804543Smarks 
18815367Sahrens /*
18825367Sahrens  * inputs:
18835367Sahrens  * zc_name		name of filesystem
18845367Sahrens  *
18855367Sahrens  * outputs:
18865367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
18875367Sahrens  */
18884543Smarks static int
18894543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
18904543Smarks {
18914543Smarks 	nvlist_t *nvp;
18924543Smarks 	int error;
18934543Smarks 
18944543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
18954543Smarks 		error = put_nvlist(zc, nvp);
18964543Smarks 		nvlist_free(nvp);
18974543Smarks 	}
18984543Smarks 
18994543Smarks 	return (error);
19004543Smarks }
19014543Smarks 
19025367Sahrens /*
19035367Sahrens  * inputs:
19045367Sahrens  * zc_name		name of volume
19055367Sahrens  *
19065367Sahrens  * outputs:		none
19075367Sahrens  */
19084543Smarks static int
1909789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
1910789Sahrens {
19114787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1912789Sahrens }
1913789Sahrens 
19145367Sahrens /*
19155367Sahrens  * inputs:
19165367Sahrens  * zc_name		name of volume
19175367Sahrens  *
19185367Sahrens  * outputs:		none
19195367Sahrens  */
1920789Sahrens static int
1921789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
1922789Sahrens {
19232676Seschrock 	return (zvol_remove_minor(zc->zc_name));
1924789Sahrens }
1925789Sahrens 
1926789Sahrens /*
1927789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
1928789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
1929789Sahrens  * is responsible for releasing the returned vfs pointer.
1930789Sahrens  */
1931789Sahrens static vfs_t *
1932789Sahrens zfs_get_vfs(const char *resource)
1933789Sahrens {
1934789Sahrens 	struct vfs *vfsp;
1935789Sahrens 	struct vfs *vfs_found = NULL;
1936789Sahrens 
1937789Sahrens 	vfs_list_read_lock();
1938789Sahrens 	vfsp = rootvfs;
1939789Sahrens 	do {
1940789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1941789Sahrens 			VFS_HOLD(vfsp);
1942789Sahrens 			vfs_found = vfsp;
1943789Sahrens 			break;
1944789Sahrens 		}
1945789Sahrens 		vfsp = vfsp->vfs_next;
1946789Sahrens 	} while (vfsp != rootvfs);
1947789Sahrens 	vfs_list_unlock();
1948789Sahrens 	return (vfs_found);
1949789Sahrens }
1950789Sahrens 
19514543Smarks /* ARGSUSED */
1952789Sahrens static void
19534543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1954789Sahrens {
19555331Samw 	zfs_creat_t *zct = arg;
19565498Stimh 
19575498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
19585331Samw }
19595331Samw 
19605498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
19615498Stimh 
19625331Samw /*
19635498Stimh  * inputs:
19647184Stimh  * createprops		list of properties requested by creator
19657184Stimh  * default_zplver	zpl version to use if unspecified in createprops
19667184Stimh  * fuids_ok		fuids allowed in this version of the spa?
19677184Stimh  * os			parent objset pointer (NULL if root fs)
19685331Samw  *
19695498Stimh  * outputs:
19705498Stimh  * zplprops	values for the zplprops we attach to the master node object
19717184Stimh  * is_ci	true if requested file system will be purely case-insensitive
19725331Samw  *
19735498Stimh  * Determine the settings for utf8only, normalization and
19745498Stimh  * casesensitivity.  Specific values may have been requested by the
19755498Stimh  * creator and/or we can inherit values from the parent dataset.  If
19765498Stimh  * the file system is of too early a vintage, a creator can not
19775498Stimh  * request settings for these properties, even if the requested
19785498Stimh  * setting is the default value.  We don't actually want to create dsl
19795498Stimh  * properties for these, so remove them from the source nvlist after
19805498Stimh  * processing.
19815331Samw  */
19825331Samw static int
19837184Stimh zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
19847184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
19857184Stimh     boolean_t *is_ci)
19865331Samw {
19877184Stimh 	uint64_t zplver = default_zplver;
19885498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
19895498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
19905498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
19915498Stimh 
19925498Stimh 	ASSERT(zplprops != NULL);
19935498Stimh 
19945375Stimh 	/*
19955498Stimh 	 * Pull out creator prop choices, if any.
19965375Stimh 	 */
19975498Stimh 	if (createprops) {
19985498Stimh 		(void) nvlist_lookup_uint64(createprops,
19997184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
20007184Stimh 		(void) nvlist_lookup_uint64(createprops,
20015498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
20025498Stimh 		(void) nvlist_remove_all(createprops,
20035498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
20045498Stimh 		(void) nvlist_lookup_uint64(createprops,
20055498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
20065498Stimh 		(void) nvlist_remove_all(createprops,
20075498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
20085498Stimh 		(void) nvlist_lookup_uint64(createprops,
20095498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
20105498Stimh 		(void) nvlist_remove_all(createprops,
20115498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
20125331Samw 	}
20135331Samw 
20145375Stimh 	/*
20157184Stimh 	 * If the zpl version requested is whacky or the file system
20167184Stimh 	 * or pool is version is too "young" to support normalization
20177184Stimh 	 * and the creator tried to set a value for one of the props,
20187184Stimh 	 * error out.
20195498Stimh 	 */
20207184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
20217184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
20227184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
20235498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
20247184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
20255498Stimh 		return (ENOTSUP);
20265498Stimh 
20275498Stimh 	/*
20285498Stimh 	 * Put the version in the zplprops
20295498Stimh 	 */
20305498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20315498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
20325498Stimh 
20335498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
20345498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
20355498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20365498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
20375498Stimh 
20385498Stimh 	/*
20395498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
20405498Stimh 	 */
20415498Stimh 	if (norm)
20425498Stimh 		u8 = 1;
20435498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
20445498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
20455498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20465498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
20475498Stimh 
20485498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
20495498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
20505498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
20515498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
20525498Stimh 
20536492Stimh 	if (is_ci)
20546492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
20556492Stimh 
20567184Stimh 	return (0);
20577184Stimh }
20587184Stimh 
20597184Stimh static int
20607184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
20617184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20627184Stimh {
20637184Stimh 	boolean_t fuids_ok = B_TRUE;
20647184Stimh 	uint64_t zplver = ZPL_VERSION;
20657184Stimh 	objset_t *os = NULL;
20667184Stimh 	char parentname[MAXNAMELEN];
20677184Stimh 	char *cp;
20687184Stimh 	int error;
20697184Stimh 
20707184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
20717184Stimh 	cp = strrchr(parentname, '/');
20727184Stimh 	ASSERT(cp != NULL);
20737184Stimh 	cp[0] = '\0';
20747184Stimh 
20757184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
20767184Stimh 		zplver = ZPL_VERSION_FUID - 1;
20777184Stimh 		fuids_ok = B_FALSE;
20787184Stimh 	}
20797184Stimh 
20807184Stimh 	/*
20817184Stimh 	 * Open parent object set so we can inherit zplprop values.
20827184Stimh 	 */
20837184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
20847184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
20857184Stimh 		return (error);
20867184Stimh 
20877184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
20887184Stimh 	    zplprops, is_ci);
20895498Stimh 	dmu_objset_close(os);
20907184Stimh 	return (error);
20917184Stimh }
20927184Stimh 
20937184Stimh static int
20947184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
20957184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
20967184Stimh {
20977184Stimh 	boolean_t fuids_ok = B_TRUE;
20987184Stimh 	uint64_t zplver = ZPL_VERSION;
20997184Stimh 	int error;
21007184Stimh 
21017184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
21027184Stimh 		zplver = ZPL_VERSION_FUID - 1;
21037184Stimh 		fuids_ok = B_FALSE;
21047184Stimh 	}
21057184Stimh 
21067184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
21077184Stimh 	    zplprops, is_ci);
21087184Stimh 	return (error);
2109789Sahrens }
2110789Sahrens 
21115367Sahrens /*
21125367Sahrens  * inputs:
21135367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
21145367Sahrens  * zc_name		name of new objset
21155367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
21165367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
21175367Sahrens  *
21185498Stimh  * outputs: none
21195367Sahrens  */
2120789Sahrens static int
2121789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2122789Sahrens {
2123789Sahrens 	objset_t *clone;
2124789Sahrens 	int error = 0;
21255331Samw 	zfs_creat_t zct;
21264543Smarks 	nvlist_t *nvprops = NULL;
21274543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2128789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2129789Sahrens 
2130789Sahrens 	switch (type) {
2131789Sahrens 
2132789Sahrens 	case DMU_OST_ZFS:
2133789Sahrens 		cbfunc = zfs_create_cb;
2134789Sahrens 		break;
2135789Sahrens 
2136789Sahrens 	case DMU_OST_ZVOL:
2137789Sahrens 		cbfunc = zvol_create_cb;
2138789Sahrens 		break;
2139789Sahrens 
2140789Sahrens 	default:
21412199Sahrens 		cbfunc = NULL;
21426423Sgw25295 		break;
21432199Sahrens 	}
21445326Sek110237 	if (strchr(zc->zc_name, '@') ||
21455326Sek110237 	    strchr(zc->zc_name, '%'))
2146789Sahrens 		return (EINVAL);
2147789Sahrens 
21482676Seschrock 	if (zc->zc_nvlist_src != NULL &&
21495094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
21505094Slling 	    &nvprops)) != 0)
21512676Seschrock 		return (error);
21522676Seschrock 
21535498Stimh 	zct.zct_zplprops = NULL;
21545331Samw 	zct.zct_props = nvprops;
21555331Samw 
21562676Seschrock 	if (zc->zc_value[0] != '\0') {
2157789Sahrens 		/*
2158789Sahrens 		 * We're creating a clone of an existing snapshot.
2159789Sahrens 		 */
21602676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
21612676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
21624543Smarks 			nvlist_free(nvprops);
2163789Sahrens 			return (EINVAL);
21642676Seschrock 		}
2165789Sahrens 
21662676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
21676689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
21682676Seschrock 		if (error) {
21694543Smarks 			nvlist_free(nvprops);
2170789Sahrens 			return (error);
21712676Seschrock 		}
21726492Stimh 
21736492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
21746492Stimh 		    NULL, NULL);
21755331Samw 		if (error) {
21765331Samw 			dmu_objset_close(clone);
21775331Samw 			nvlist_free(nvprops);
21785331Samw 			return (error);
21795331Samw 		}
2180789Sahrens 		dmu_objset_close(clone);
2181789Sahrens 	} else {
21826492Stimh 		boolean_t is_insensitive = B_FALSE;
21836492Stimh 
21842676Seschrock 		if (cbfunc == NULL) {
21854543Smarks 			nvlist_free(nvprops);
21862199Sahrens 			return (EINVAL);
21872676Seschrock 		}
21882676Seschrock 
2189789Sahrens 		if (type == DMU_OST_ZVOL) {
21902676Seschrock 			uint64_t volsize, volblocksize;
21912676Seschrock 
21924543Smarks 			if (nvprops == NULL ||
21934543Smarks 			    nvlist_lookup_uint64(nvprops,
21942676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
21952676Seschrock 			    &volsize) != 0) {
21964543Smarks 				nvlist_free(nvprops);
21972676Seschrock 				return (EINVAL);
21982676Seschrock 			}
21992676Seschrock 
22004543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
22012676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
22022676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
22034543Smarks 				nvlist_free(nvprops);
22042676Seschrock 				return (EINVAL);
22052676Seschrock 			}
22061133Seschrock 
22072676Seschrock 			if (error != 0)
22082676Seschrock 				volblocksize = zfs_prop_default_numeric(
22092676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
22102676Seschrock 
22112676Seschrock 			if ((error = zvol_check_volblocksize(
22122676Seschrock 			    volblocksize)) != 0 ||
22132676Seschrock 			    (error = zvol_check_volsize(volsize,
22142676Seschrock 			    volblocksize)) != 0) {
22154543Smarks 				nvlist_free(nvprops);
2216789Sahrens 				return (error);
22172676Seschrock 			}
22184577Sahrens 		} else if (type == DMU_OST_ZFS) {
22195331Samw 			int error;
22205331Samw 
22215498Stimh 			/*
22225331Samw 			 * We have to have normalization and
22235331Samw 			 * case-folding flags correct when we do the
22245331Samw 			 * file system creation, so go figure them out
22255498Stimh 			 * now.
22265331Samw 			 */
22275498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
22285498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22295498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
22307184Stimh 			    zct.zct_zplprops, &is_insensitive);
22315331Samw 			if (error != 0) {
22325331Samw 				nvlist_free(nvprops);
22335498Stimh 				nvlist_free(zct.zct_zplprops);
22345331Samw 				return (error);
22354577Sahrens 			}
22362676Seschrock 		}
22376492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
22386492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
22395498Stimh 		nvlist_free(zct.zct_zplprops);
2240789Sahrens 	}
22412676Seschrock 
22422676Seschrock 	/*
22432676Seschrock 	 * It would be nice to do this atomically.
22442676Seschrock 	 */
22452676Seschrock 	if (error == 0) {
22464787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
22472676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
22482676Seschrock 	}
22494543Smarks 	nvlist_free(nvprops);
2250789Sahrens 	return (error);
2251789Sahrens }
2252789Sahrens 
22535367Sahrens /*
22545367Sahrens  * inputs:
22555367Sahrens  * zc_name	name of filesystem
22565367Sahrens  * zc_value	short name of snapshot
22575367Sahrens  * zc_cookie	recursive flag
22585367Sahrens  *
22595367Sahrens  * outputs:	none
22605367Sahrens  */
2261789Sahrens static int
22622199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
22632199Sahrens {
22647265Sahrens 	nvlist_t *nvprops = NULL;
22657265Sahrens 	int error;
22667265Sahrens 	boolean_t recursive = zc->zc_cookie;
22677265Sahrens 
22682676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
22692199Sahrens 		return (EINVAL);
22707265Sahrens 
22717265Sahrens 	if (zc->zc_nvlist_src != NULL &&
22727265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
22737265Sahrens 	    &nvprops)) != 0)
22747265Sahrens 		return (error);
22757265Sahrens 
2276*9355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
2277*9355SMatthew.Ahrens@Sun.COM 	if (error)
2278*9355SMatthew.Ahrens@Sun.COM 		goto out;
2279*9355SMatthew.Ahrens@Sun.COM 
2280*9355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
2281*9355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2282*9355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
2283*9355SMatthew.Ahrens@Sun.COM 		goto out;
22847265Sahrens 	}
2285*9355SMatthew.Ahrens@Sun.COM 
2286*9355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2287*9355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
2288*9355SMatthew.Ahrens@Sun.COM 
2289*9355SMatthew.Ahrens@Sun.COM out:
22907265Sahrens 	nvlist_free(nvprops);
22917265Sahrens 	return (error);
22922199Sahrens }
22932199Sahrens 
22944007Smmusante int
22952199Sahrens zfs_unmount_snap(char *name, void *arg)
2296789Sahrens {
22972417Sahrens 	vfs_t *vfsp = NULL;
22982199Sahrens 
22996689Smaybee 	if (arg) {
23006689Smaybee 		char *snapname = arg;
23016689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
23026689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
23036689Smaybee 
23046689Smaybee 		(void) strcpy(buf, name);
23056689Smaybee 		(void) strcat(buf, "@");
23066689Smaybee 		(void) strcat(buf, snapname);
23076689Smaybee 		vfsp = zfs_get_vfs(buf);
23086689Smaybee 		kmem_free(buf, len);
23092417Sahrens 	} else if (strchr(name, '@')) {
23102199Sahrens 		vfsp = zfs_get_vfs(name);
23112199Sahrens 	}
23122199Sahrens 
23132199Sahrens 	if (vfsp) {
23142199Sahrens 		/*
23152199Sahrens 		 * Always force the unmount for snapshots.
23162199Sahrens 		 */
23172199Sahrens 		int flag = MS_FORCE;
2318789Sahrens 		int err;
2319789Sahrens 
23202199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
23212199Sahrens 			VFS_RELE(vfsp);
23222199Sahrens 			return (err);
23232199Sahrens 		}
23242199Sahrens 		VFS_RELE(vfsp);
23252199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
23262199Sahrens 			return (err);
23272199Sahrens 	}
23282199Sahrens 	return (0);
23292199Sahrens }
23302199Sahrens 
23315367Sahrens /*
23325367Sahrens  * inputs:
23335367Sahrens  * zc_name	name of filesystem
23345367Sahrens  * zc_value	short name of snapshot
23355367Sahrens  *
23365367Sahrens  * outputs:	none
23375367Sahrens  */
23382199Sahrens static int
23392199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
23402199Sahrens {
23412199Sahrens 	int err;
2342789Sahrens 
23432676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
23442199Sahrens 		return (EINVAL);
23452199Sahrens 	err = dmu_objset_find(zc->zc_name,
23462676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
23472199Sahrens 	if (err)
23482199Sahrens 		return (err);
23492676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
23502199Sahrens }
23512199Sahrens 
23525367Sahrens /*
23535367Sahrens  * inputs:
23545367Sahrens  * zc_name		name of dataset to destroy
23555367Sahrens  * zc_objset_type	type of objset
23565367Sahrens  *
23575367Sahrens  * outputs:		none
23585367Sahrens  */
23592199Sahrens static int
23602199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
23612199Sahrens {
23622199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
23632199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
23642199Sahrens 		if (err)
23652199Sahrens 			return (err);
2366789Sahrens 	}
2367789Sahrens 
2368789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2369789Sahrens }
2370789Sahrens 
23715367Sahrens /*
23725367Sahrens  * inputs:
23735446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
23745367Sahrens  *
23755367Sahrens  * outputs:	none
23765367Sahrens  */
2377789Sahrens static int
2378789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2379789Sahrens {
23805446Sahrens 	objset_t *os;
23815446Sahrens 	int error;
23825446Sahrens 	zfsvfs_t *zfsvfs = NULL;
23835446Sahrens 
23845446Sahrens 	/*
23855446Sahrens 	 * Get the zfsvfs for the receiving objset. There
23865446Sahrens 	 * won't be one if we're operating on a zvol, if the
23875446Sahrens 	 * objset doesn't exist yet, or is not mounted.
23885446Sahrens 	 */
23896689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
23905446Sahrens 	if (error)
23915446Sahrens 		return (error);
23925446Sahrens 
23935446Sahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
23945446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
23955446Sahrens 		zfsvfs = dmu_objset_get_user(os);
23965446Sahrens 		if (zfsvfs != NULL)
23975446Sahrens 			VFS_HOLD(zfsvfs->z_vfs);
23985446Sahrens 		mutex_exit(&os->os->os_user_ptr_lock);
23995446Sahrens 	}
24005446Sahrens 
24015446Sahrens 	if (zfsvfs != NULL) {
24028012SEric.Taylor@Sun.COM 		char *osname;
24035446Sahrens 		int mode;
24045446Sahrens 
24058012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
24066083Sek110237 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
24076083Sek110237 		if (error == 0) {
24086083Sek110237 			int resume_err;
24096083Sek110237 
24106083Sek110237 			ASSERT(strcmp(osname, zc->zc_name) == 0);
24116083Sek110237 			error = dmu_objset_rollback(os);
24126083Sek110237 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
24136083Sek110237 			error = error ? error : resume_err;
24146083Sek110237 		} else {
24156083Sek110237 			dmu_objset_close(os);
24166083Sek110237 		}
24178012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
24185446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
24195446Sahrens 	} else {
24205446Sahrens 		error = dmu_objset_rollback(os);
24215446Sahrens 	}
24226689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
24235446Sahrens 
24245446Sahrens 	return (error);
2425789Sahrens }
2426789Sahrens 
24275367Sahrens /*
24285367Sahrens  * inputs:
24295367Sahrens  * zc_name	old name of dataset
24305367Sahrens  * zc_value	new name of dataset
24315367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
24325367Sahrens  *
24335367Sahrens  * outputs:	none
24345367Sahrens  */
2435789Sahrens static int
2436789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2437789Sahrens {
24384490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
24394007Smmusante 
24402676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
24415326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
24425326Sek110237 	    strchr(zc->zc_value, '%'))
2443789Sahrens 		return (EINVAL);
2444789Sahrens 
24454007Smmusante 	/*
24464007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
24474007Smmusante 	 * in which case the dataset code figures out which snapshots
24484007Smmusante 	 * to unmount.
24494007Smmusante 	 */
24504007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2451789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
24522199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
24532199Sahrens 		if (err)
24542199Sahrens 			return (err);
2455789Sahrens 	}
24564007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2457789Sahrens }
2458789Sahrens 
24596689Smaybee static void
24608536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
24616689Smaybee {
24626689Smaybee 	zfs_cmd_t *zc;
24636689Smaybee 	nvpair_t *prop;
24646689Smaybee 
24656689Smaybee 	if (props == NULL)
24666689Smaybee 		return;
24676689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
24686689Smaybee 	(void) strcpy(zc->zc_name, dataset);
24696689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
24706689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
24718536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
24728536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
24738536SDavid.Pacheco@Sun.COM 			continue;
24746689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
24756689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
24766689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
24776689Smaybee 	}
24786689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
24796689Smaybee }
24806689Smaybee 
24815367Sahrens /*
24825367Sahrens  * inputs:
24835367Sahrens  * zc_name		name of containing filesystem
24845367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
24855367Sahrens  * zc_value		name of snapshot to create
24865367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
24875367Sahrens  * zc_cookie		file descriptor to recv from
24885367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
24895367Sahrens  * zc_guid		force flag
24905367Sahrens  *
24915367Sahrens  * outputs:
24925367Sahrens  * zc_cookie		number of bytes read
24935367Sahrens  */
2494789Sahrens static int
24955367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2496789Sahrens {
2497789Sahrens 	file_t *fp;
24985326Sek110237 	objset_t *os;
24995367Sahrens 	dmu_recv_cookie_t drc;
25005326Sek110237 	zfsvfs_t *zfsvfs = NULL;
25015326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2502789Sahrens 	int error, fd;
25035367Sahrens 	offset_t off;
25045367Sahrens 	nvlist_t *props = NULL;
25056689Smaybee 	nvlist_t *origprops = NULL;
25065367Sahrens 	objset_t *origin = NULL;
25075367Sahrens 	char *tosnap;
25085367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2509789Sahrens 
25103265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
25115326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
25125326Sek110237 	    strchr(zc->zc_value, '%'))
25133265Sahrens 		return (EINVAL);
25143265Sahrens 
25155367Sahrens 	(void) strcpy(tofs, zc->zc_value);
25165367Sahrens 	tosnap = strchr(tofs, '@');
25175367Sahrens 	*tosnap = '\0';
25185367Sahrens 	tosnap++;
25195367Sahrens 
25205367Sahrens 	if (zc->zc_nvlist_src != NULL &&
25215367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
25225367Sahrens 	    &props)) != 0)
25235367Sahrens 		return (error);
25245367Sahrens 
2525789Sahrens 	fd = zc->zc_cookie;
2526789Sahrens 	fp = getf(fd);
25275367Sahrens 	if (fp == NULL) {
25285367Sahrens 		nvlist_free(props);
2529789Sahrens 		return (EBADF);
25305367Sahrens 	}
25315326Sek110237 
25326689Smaybee 	if (dmu_objset_open(tofs, DMU_OST_ANY,
25336689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
25346689Smaybee 		/*
25356689Smaybee 		 * Try to get the zfsvfs for the receiving objset.
25366689Smaybee 		 * There won't be one if we're operating on a zvol,
25376689Smaybee 		 * if the objset doesn't exist yet, or is not mounted.
25386689Smaybee 		 */
25395446Sahrens 		mutex_enter(&os->os->os_user_ptr_lock);
25406689Smaybee 		if (zfsvfs = dmu_objset_get_user(os)) {
25416083Sek110237 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
25426689Smaybee 				mutex_exit(&os->os->os_user_ptr_lock);
25436083Sek110237 				dmu_objset_close(os);
25446689Smaybee 				zfsvfs = NULL;
25456689Smaybee 				error = EBUSY;
25466689Smaybee 				goto out;
25476083Sek110237 			}
25486689Smaybee 			VFS_HOLD(zfsvfs->z_vfs);
25496083Sek110237 		}
25506689Smaybee 		mutex_exit(&os->os->os_user_ptr_lock);
25516689Smaybee 
25526689Smaybee 		/*
25536689Smaybee 		 * If new properties are supplied, they are to completely
25546689Smaybee 		 * replace the existing ones, so stash away the existing ones.
25556689Smaybee 		 */
25566689Smaybee 		if (props)
25576689Smaybee 			(void) dsl_prop_get_all(os, &origprops, TRUE);
25586689Smaybee 
25595326Sek110237 		dmu_objset_close(os);
25605326Sek110237 	}
25615326Sek110237 
25625367Sahrens 	if (zc->zc_string[0]) {
25635367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
25646689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
25656689Smaybee 		if (error)
25666689Smaybee 			goto out;
25675367Sahrens 	}
25685367Sahrens 
25695367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
25705367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
25715367Sahrens 	if (origin)
25725367Sahrens 		dmu_objset_close(origin);
25736689Smaybee 	if (error)
25746689Smaybee 		goto out;
25755326Sek110237 
25765326Sek110237 	/*
25776689Smaybee 	 * Reset properties.  We do this before we receive the stream
25786689Smaybee 	 * so that the properties are applied to the new data.
25795326Sek110237 	 */
25805367Sahrens 	if (props) {
25818536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
25826689Smaybee 		/*
25836689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
25846689Smaybee 		 */
25856689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
25865367Sahrens 	}
25875367Sahrens 
25885367Sahrens 	off = fp->f_offset;
25895367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
25905367Sahrens 
25916689Smaybee 	if (error == 0 && zfsvfs) {
25928012SEric.Taylor@Sun.COM 		char *osname;
25936689Smaybee 		int mode;
25946689Smaybee 
25956689Smaybee 		/* online recv */
25968012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
25976689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
25986689Smaybee 		if (error == 0) {
25996689Smaybee 			int resume_err;
26006689Smaybee 
26016689Smaybee 			error = dmu_recv_end(&drc);
26026689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
26036689Smaybee 			error = error ? error : resume_err;
26045367Sahrens 		} else {
26056689Smaybee 			dmu_recv_abort_cleanup(&drc);
26065367Sahrens 		}
26078012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
26086689Smaybee 	} else if (error == 0) {
26096689Smaybee 		error = dmu_recv_end(&drc);
26106083Sek110237 	}
26115367Sahrens 
26125367Sahrens 	zc->zc_cookie = off - fp->f_offset;
26135367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
26145367Sahrens 		fp->f_offset = off;
26152885Sahrens 
26166689Smaybee 	/*
26176689Smaybee 	 * On error, restore the original props.
26186689Smaybee 	 */
26196689Smaybee 	if (error && props) {
26208536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
26216689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
26226689Smaybee 	}
26236689Smaybee out:
26246689Smaybee 	if (zfsvfs) {
26256689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
26266689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
26276689Smaybee 	}
26286689Smaybee 	nvlist_free(props);
26296689Smaybee 	nvlist_free(origprops);
2630789Sahrens 	releasef(fd);
2631789Sahrens 	return (error);
2632789Sahrens }
2633789Sahrens 
26345367Sahrens /*
26355367Sahrens  * inputs:
26365367Sahrens  * zc_name	name of snapshot to send
26375367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
26385367Sahrens  * zc_cookie	file descriptor to send stream to
26395367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
26405367Sahrens  *
26415367Sahrens  * outputs: none
26425367Sahrens  */
2643789Sahrens static int
26445367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2645789Sahrens {
2646789Sahrens 	objset_t *fromsnap = NULL;
2647789Sahrens 	objset_t *tosnap;
2648789Sahrens 	file_t *fp;
2649789Sahrens 	int error;
26505367Sahrens 	offset_t off;
2651789Sahrens 
2652789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
26536689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2654789Sahrens 	if (error)
2655789Sahrens 		return (error);
2656789Sahrens 
26572676Seschrock 	if (zc->zc_value[0] != '\0') {
26588012SEric.Taylor@Sun.COM 		char *buf;
26592885Sahrens 		char *cp;
26602885Sahrens 
26618012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26628012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
26632885Sahrens 		cp = strchr(buf, '@');
26642885Sahrens 		if (cp)
26652885Sahrens 			*(cp+1) = 0;
26668012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
26672885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
26686689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
26698012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2670789Sahrens 		if (error) {
2671789Sahrens 			dmu_objset_close(tosnap);
2672789Sahrens 			return (error);
2673789Sahrens 		}
2674789Sahrens 	}
2675789Sahrens 
2676789Sahrens 	fp = getf(zc->zc_cookie);
2677789Sahrens 	if (fp == NULL) {
2678789Sahrens 		dmu_objset_close(tosnap);
2679789Sahrens 		if (fromsnap)
2680789Sahrens 			dmu_objset_close(fromsnap);
2681789Sahrens 		return (EBADF);
2682789Sahrens 	}
2683789Sahrens 
26845367Sahrens 	off = fp->f_offset;
26855367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
26865367Sahrens 
26875367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
26885367Sahrens 		fp->f_offset = off;
2689789Sahrens 	releasef(zc->zc_cookie);
2690789Sahrens 	if (fromsnap)
2691789Sahrens 		dmu_objset_close(fromsnap);
2692789Sahrens 	dmu_objset_close(tosnap);
2693789Sahrens 	return (error);
2694789Sahrens }
2695789Sahrens 
26961544Seschrock static int
26971544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
26981544Seschrock {
26991544Seschrock 	int id, error;
27001544Seschrock 
27011544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
27021544Seschrock 	    &zc->zc_inject_record);
27031544Seschrock 
27041544Seschrock 	if (error == 0)
27051544Seschrock 		zc->zc_guid = (uint64_t)id;
27061544Seschrock 
27071544Seschrock 	return (error);
27081544Seschrock }
27091544Seschrock 
27101544Seschrock static int
27111544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
27121544Seschrock {
27131544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
27141544Seschrock }
27151544Seschrock 
27161544Seschrock static int
27171544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
27181544Seschrock {
27191544Seschrock 	int id = (int)zc->zc_guid;
27201544Seschrock 	int error;
27211544Seschrock 
27221544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
27231544Seschrock 	    &zc->zc_inject_record);
27241544Seschrock 
27251544Seschrock 	zc->zc_guid = id;
27261544Seschrock 
27271544Seschrock 	return (error);
27281544Seschrock }
27291544Seschrock 
27301544Seschrock static int
27311544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
27321544Seschrock {
27331544Seschrock 	spa_t *spa;
27341544Seschrock 	int error;
27352676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
27361544Seschrock 
27371544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27381544Seschrock 		return (error);
27391544Seschrock 
27402676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
27411544Seschrock 	    &count);
27421544Seschrock 	if (error == 0)
27432676Seschrock 		zc->zc_nvlist_dst_size = count;
27441544Seschrock 	else
27452676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
27461544Seschrock 
27471544Seschrock 	spa_close(spa, FTAG);
27481544Seschrock 
27491544Seschrock 	return (error);
27501544Seschrock }
27511544Seschrock 
27521544Seschrock static int
27531544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
27541544Seschrock {
27551544Seschrock 	spa_t *spa;
27561544Seschrock 	vdev_t *vd;
27571544Seschrock 	int error;
27581544Seschrock 
27597294Sperrin 	/*
27607294Sperrin 	 * On zpool clear we also fix up missing slogs
27617294Sperrin 	 */
27627294Sperrin 	mutex_enter(&spa_namespace_lock);
27637294Sperrin 	spa = spa_lookup(zc->zc_name);
27647294Sperrin 	if (spa == NULL) {
27657294Sperrin 		mutex_exit(&spa_namespace_lock);
27667294Sperrin 		return (EIO);
27677294Sperrin 	}
27687294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
27697294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
27707294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
27717294Sperrin 	}
27727294Sperrin 	mutex_exit(&spa_namespace_lock);
27737294Sperrin 
27741544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
27751544Seschrock 		return (error);
27761544Seschrock 
27777754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
27781544Seschrock 
27792676Seschrock 	if (zc->zc_guid == 0) {
27801544Seschrock 		vd = NULL;
27816643Seschrock 	} else {
27826643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
27835450Sbrendan 		if (vd == NULL) {
27847754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
27855450Sbrendan 			spa_close(spa, FTAG);
27865450Sbrendan 			return (ENODEV);
27875450Sbrendan 		}
27881544Seschrock 	}
27891544Seschrock 
27907754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
27917754SJeff.Bonwick@Sun.COM 
27927754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
27937754SJeff.Bonwick@Sun.COM 
27947754SJeff.Bonwick@Sun.COM 	/*
27957754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
27967754SJeff.Bonwick@Sun.COM 	 */
27979234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
27989234SGeorge.Wilson@Sun.COM 		error = EIO;
27991544Seschrock 
28001544Seschrock 	spa_close(spa, FTAG);
28011544Seschrock 
28029234SGeorge.Wilson@Sun.COM 	return (error);
28031544Seschrock }
28041544Seschrock 
28055367Sahrens /*
28065367Sahrens  * inputs:
28075367Sahrens  * zc_name	name of filesystem
28085367Sahrens  * zc_value	name of origin snapshot
28095367Sahrens  *
28105367Sahrens  * outputs:	none
28115367Sahrens  */
28121544Seschrock static int
28132082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
28142082Seschrock {
28152417Sahrens 	char *cp;
28162417Sahrens 
28172417Sahrens 	/*
28182417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
28192417Sahrens 	 * it's easier.
28202417Sahrens 	 */
28212676Seschrock 	cp = strchr(zc->zc_value, '@');
28222417Sahrens 	if (cp)
28232417Sahrens 		*cp = '\0';
28242676Seschrock 	(void) dmu_objset_find(zc->zc_value,
28252417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
28262082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
28272082Seschrock }
28282082Seschrock 
28294543Smarks /*
28304543Smarks  * We don't want to have a hard dependency
28314543Smarks  * against some special symbols in sharefs
28325331Samw  * nfs, and smbsrv.  Determine them if needed when
28334543Smarks  * the first file system is shared.
28345331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
28354543Smarks  */
28365331Samw int (*znfsexport_fs)(void *arg);
28374543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
28385331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
28395331Samw 
28405331Samw int zfs_nfsshare_inited;
28415331Samw int zfs_smbshare_inited;
28425331Samw 
28434543Smarks ddi_modhandle_t nfs_mod;
28444543Smarks ddi_modhandle_t sharefs_mod;
28455331Samw ddi_modhandle_t smbsrv_mod;
28464543Smarks kmutex_t zfs_share_lock;
28474543Smarks 
28484543Smarks static int
28495331Samw zfs_init_sharefs()
28505331Samw {
28515331Samw 	int error;
28525331Samw 
28535331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
28545331Samw 	/* Both NFS and SMB shares also require sharetab support. */
28555331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
28565331Samw 	    ddi_modopen("fs/sharefs",
28575331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
28585331Samw 		return (ENOSYS);
28595331Samw 	}
28605331Samw 	if (zshare_fs == NULL && ((zshare_fs =
28615331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
28625331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
28635331Samw 		return (ENOSYS);
28645331Samw 	}
28655331Samw 	return (0);
28665331Samw }
28675331Samw 
28685331Samw static int
28694543Smarks zfs_ioc_share(zfs_cmd_t *zc)
28704543Smarks {
28714543Smarks 	int error;
28724543Smarks 	int opcode;
28734543Smarks 
28745331Samw 	switch (zc->zc_share.z_sharetype) {
28755331Samw 	case ZFS_SHARE_NFS:
28765331Samw 	case ZFS_UNSHARE_NFS:
28775331Samw 		if (zfs_nfsshare_inited == 0) {
28785331Samw 			mutex_enter(&zfs_share_lock);
28795331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
28805331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
28815331Samw 				mutex_exit(&zfs_share_lock);
28825331Samw 				return (ENOSYS);
28835331Samw 			}
28845331Samw 			if (znfsexport_fs == NULL &&
28855331Samw 			    ((znfsexport_fs = (int (*)(void *))
28865331Samw 			    ddi_modsym(nfs_mod,
28875331Samw 			    "nfs_export", &error)) == NULL)) {
28885331Samw 				mutex_exit(&zfs_share_lock);
28895331Samw 				return (ENOSYS);
28905331Samw 			}
28915331Samw 			error = zfs_init_sharefs();
28925331Samw 			if (error) {
28935331Samw 				mutex_exit(&zfs_share_lock);
28945331Samw 				return (ENOSYS);
28955331Samw 			}
28965331Samw 			zfs_nfsshare_inited = 1;
28974543Smarks 			mutex_exit(&zfs_share_lock);
28984543Smarks 		}
28995331Samw 		break;
29005331Samw 	case ZFS_SHARE_SMB:
29015331Samw 	case ZFS_UNSHARE_SMB:
29025331Samw 		if (zfs_smbshare_inited == 0) {
29035331Samw 			mutex_enter(&zfs_share_lock);
29045331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
29055331Samw 			    ddi_modopen("drv/smbsrv",
29065331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
29075331Samw 				mutex_exit(&zfs_share_lock);
29085331Samw 				return (ENOSYS);
29095331Samw 			}
29105331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
29115331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
29126139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
29135331Samw 				mutex_exit(&zfs_share_lock);
29145331Samw 				return (ENOSYS);
29155331Samw 			}
29165331Samw 			error = zfs_init_sharefs();
29175331Samw 			if (error) {
29185331Samw 				mutex_exit(&zfs_share_lock);
29195331Samw 				return (ENOSYS);
29205331Samw 			}
29215331Samw 			zfs_smbshare_inited = 1;
29224543Smarks 			mutex_exit(&zfs_share_lock);
29234543Smarks 		}
29245331Samw 		break;
29255331Samw 	default:
29265331Samw 		return (EINVAL);
29274543Smarks 	}
29284543Smarks 
29295331Samw 	switch (zc->zc_share.z_sharetype) {
29305331Samw 	case ZFS_SHARE_NFS:
29315331Samw 	case ZFS_UNSHARE_NFS:
29325331Samw 		if (error =
29335331Samw 		    znfsexport_fs((void *)
29345331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
29355331Samw 			return (error);
29365331Samw 		break;
29375331Samw 	case ZFS_SHARE_SMB:
29385331Samw 	case ZFS_UNSHARE_SMB:
29395331Samw 		if (error = zsmbexport_fs((void *)
29405331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
29415331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
29428845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
29435331Samw 			return (error);
29445331Samw 		}
29455331Samw 		break;
29465331Samw 	}
29475331Samw 
29485331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
29495331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
29504543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
29514543Smarks 
29525331Samw 	/*
29535331Samw 	 * Add or remove share from sharetab
29545331Samw 	 */
29554543Smarks 	error = zshare_fs(opcode,
29564543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
29574543Smarks 	    zc->zc_share.z_sharemax);
29584543Smarks 
29594543Smarks 	return (error);
29604543Smarks 
29614543Smarks }
29624543Smarks 
29638845Samw@Sun.COM ace_t full_access[] = {
29648845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
29658845Samw@Sun.COM };
29668845Samw@Sun.COM 
29678845Samw@Sun.COM /*
29688845Samw@Sun.COM  * Remove all ACL files in shares dir
29698845Samw@Sun.COM  */
29708845Samw@Sun.COM static int
29718845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
29728845Samw@Sun.COM {
29738845Samw@Sun.COM 	zap_cursor_t	zc;
29748845Samw@Sun.COM 	zap_attribute_t	zap;
29758845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
29768845Samw@Sun.COM 	int error;
29778845Samw@Sun.COM 
29788845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
29798845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
29808845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
29818845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
29828845Samw@Sun.COM 		    NULL, 0)) != 0)
29838845Samw@Sun.COM 			break;
29848845Samw@Sun.COM 	}
29858845Samw@Sun.COM 	zap_cursor_fini(&zc);
29868845Samw@Sun.COM 	return (error);
29878845Samw@Sun.COM }
29888845Samw@Sun.COM 
29898845Samw@Sun.COM static int
29908845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
29918845Samw@Sun.COM {
29928845Samw@Sun.COM 	vnode_t *vp;
29938845Samw@Sun.COM 	znode_t *dzp;
29948845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
29958845Samw@Sun.COM 	znode_t *sharedir;
29968845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
29978845Samw@Sun.COM 	nvlist_t *nvlist;
29988845Samw@Sun.COM 	char *src, *target;
29998845Samw@Sun.COM 	vattr_t vattr;
30008845Samw@Sun.COM 	vsecattr_t vsec;
30018845Samw@Sun.COM 	int error = 0;
30028845Samw@Sun.COM 
30038845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
30048845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
30058845Samw@Sun.COM 		return (error);
30068845Samw@Sun.COM 
30078845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
30088845Samw@Sun.COM 
30098845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
30108845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
30118845Samw@Sun.COM 	    zc->zc_name) != 0)) {
30128845Samw@Sun.COM 		VN_RELE(vp);
30138845Samw@Sun.COM 		return (EINVAL);
30148845Samw@Sun.COM 	}
30158845Samw@Sun.COM 
30168845Samw@Sun.COM 	dzp = VTOZ(vp);
30178845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
30188845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
30198845Samw@Sun.COM 
30209030SMark.Shellenbaum@Sun.COM 	/*
30219030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
30229030SMark.Shellenbaum@Sun.COM 	 */
30239030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
30249030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
30259030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
30269030SMark.Shellenbaum@Sun.COM 
30279030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
30289030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
30299030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
30309030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
30319030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
30329030SMark.Shellenbaum@Sun.COM 		if (error) {
30339030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
30349030SMark.Shellenbaum@Sun.COM 		} else {
30359030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
30369030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
30379030SMark.Shellenbaum@Sun.COM 		}
30389030SMark.Shellenbaum@Sun.COM 		if (error) {
30399030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
30409030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
30419030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
30429030SMark.Shellenbaum@Sun.COM 			return (error);
30439030SMark.Shellenbaum@Sun.COM 		}
30449030SMark.Shellenbaum@Sun.COM 	}
30459030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
30469030SMark.Shellenbaum@Sun.COM 
30479030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
30488845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
30499030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
30508845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
30518845Samw@Sun.COM 		return (error);
30528845Samw@Sun.COM 	}
30538845Samw@Sun.COM 
30548845Samw@Sun.COM 	switch (zc->zc_cookie) {
30558845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
30568845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
30578845Samw@Sun.COM 		vattr.va_type = VREG;
30588845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
30598845Samw@Sun.COM 		vattr.va_uid = 0;
30608845Samw@Sun.COM 		vattr.va_gid = 0;
30618845Samw@Sun.COM 
30628845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
30638845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
30648845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
30658845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
30668845Samw@Sun.COM 
30678845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
30688845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
30698845Samw@Sun.COM 		if (resourcevp)
30708845Samw@Sun.COM 			VN_RELE(resourcevp);
30718845Samw@Sun.COM 		break;
30728845Samw@Sun.COM 
30738845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
30748845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
30758845Samw@Sun.COM 		    NULL, 0);
30768845Samw@Sun.COM 		break;
30778845Samw@Sun.COM 
30788845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
30798845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
30808845Samw@Sun.COM 		    zc->zc_nvlist_src_size, &nvlist)) != 0) {
30818845Samw@Sun.COM 			VN_RELE(vp);
30828845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
30838845Samw@Sun.COM 			return (error);
30848845Samw@Sun.COM 		}
30858845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
30868845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
30878845Samw@Sun.COM 		    &target)) {
30888845Samw@Sun.COM 			VN_RELE(vp);
30899179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
30908845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
30918845Samw@Sun.COM 			return (error);
30928845Samw@Sun.COM 		}
30938845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
30948845Samw@Sun.COM 		    kcred, NULL, 0);
30958845Samw@Sun.COM 		nvlist_free(nvlist);
30968845Samw@Sun.COM 		break;
30978845Samw@Sun.COM 
30988845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
30998845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
31008845Samw@Sun.COM 		break;
31018845Samw@Sun.COM 
31028845Samw@Sun.COM 	default:
31038845Samw@Sun.COM 		error = EINVAL;
31048845Samw@Sun.COM 		break;
31058845Samw@Sun.COM 	}
31068845Samw@Sun.COM 
31078845Samw@Sun.COM 	VN_RELE(vp);
31088845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
31098845Samw@Sun.COM 
31108845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
31118845Samw@Sun.COM 
31128845Samw@Sun.COM 	return (error);
31138845Samw@Sun.COM }
31148845Samw@Sun.COM 
31154543Smarks /*
31164988Sek110237  * pool create, destroy, and export don't log the history as part of
31174988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
31184988Sek110237  * do the logging of those commands.
31194543Smarks  */
3120789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
31219234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
31229234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31239234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
31249234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31259234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31269234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31279234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
31289234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31299234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
31309234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31319234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
31329234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31339234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
31349234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31359234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31369234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31379234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
31389234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31399234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
31409234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31419234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
31429234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31439234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31449234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31459234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31469234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31479234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
31489234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31499234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31509234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31519234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
31529234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31539234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
31549234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31559234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
31569234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
31589234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
31609234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31619234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
31629234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
31649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
31659234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
31679234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
31699234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
31709234SGeorge.Wilson@Sun.COM 	    B_TRUE},
31719234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
31729234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31739234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
31749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
31759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
31769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
31779234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31789234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
31799234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
31819234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
31839234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
31859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
31869234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31879234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
31889234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
31909234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
31929234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
31949234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
31969234SGeorge.Wilson@Sun.COM 	    B_TRUE },
31979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
31989234SGeorge.Wilson@Sun.COM 	    B_FALSE },
31999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
32009234SGeorge.Wilson@Sun.COM 	    B_TRUE },
32019234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
32029234SGeorge.Wilson@Sun.COM 	    B_FALSE },
32039234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
32049234SGeorge.Wilson@Sun.COM 	    B_FALSE },
32059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
32069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
32079234SGeorge.Wilson@Sun.COM 	    B_TRUE },
32089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
32099234SGeorge.Wilson@Sun.COM 	    B_FALSE }
3210789Sahrens };
3211789Sahrens 
32129234SGeorge.Wilson@Sun.COM int
32139234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
32149234SGeorge.Wilson@Sun.COM {
32159234SGeorge.Wilson@Sun.COM 	spa_t *spa;
32169234SGeorge.Wilson@Sun.COM 	char pool[ZFS_MAXNAMELEN];
32179234SGeorge.Wilson@Sun.COM 	int error;
32189234SGeorge.Wilson@Sun.COM 
32199234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
32209234SGeorge.Wilson@Sun.COM 
32219234SGeorge.Wilson@Sun.COM 	(void) strlcpy(pool, name, ZFS_MAXNAMELEN);
32229234SGeorge.Wilson@Sun.COM 	if (type == DATASET_NAME) {
32239234SGeorge.Wilson@Sun.COM 		char *p;
32249234SGeorge.Wilson@Sun.COM 
32259234SGeorge.Wilson@Sun.COM 		if ((p = strpbrk(pool, "/@")) != NULL)
32269234SGeorge.Wilson@Sun.COM 			*p = '\0';
32279234SGeorge.Wilson@Sun.COM 	}
32289234SGeorge.Wilson@Sun.COM 
32299234SGeorge.Wilson@Sun.COM 	error = spa_open(pool, &spa, FTAG);
32309234SGeorge.Wilson@Sun.COM 	if (error == 0) {
32319234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
32329234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
32339234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
32349234SGeorge.Wilson@Sun.COM 	}
32359234SGeorge.Wilson@Sun.COM 	return (error);
32369234SGeorge.Wilson@Sun.COM }
32379234SGeorge.Wilson@Sun.COM 
3238789Sahrens static int
3239789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3240789Sahrens {
3241789Sahrens 	zfs_cmd_t *zc;
3242789Sahrens 	uint_t vec;
32432199Sahrens 	int error, rc;
3244789Sahrens 
3245789Sahrens 	if (getminor(dev) != 0)
3246789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3247789Sahrens 
3248789Sahrens 	vec = cmd - ZFS_IOC;
32494787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3250789Sahrens 
3251789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3252789Sahrens 		return (EINVAL);
3253789Sahrens 
3254789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3255789Sahrens 
3256789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3257789Sahrens 
32584787Sahrens 	if (error == 0)
32594543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3260789Sahrens 
3261789Sahrens 	/*
3262789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3263789Sahrens 	 * the lower layers.
3264789Sahrens 	 */
3265789Sahrens 	if (error == 0) {
3266789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3267789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
32684577Sahrens 		case POOL_NAME:
3269789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3270789Sahrens 				error = EINVAL;
32719234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
32729234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
32739234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3274789Sahrens 			break;
3275789Sahrens 
32764577Sahrens 		case DATASET_NAME:
3277789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3278789Sahrens 				error = EINVAL;
32799234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
32809234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
32819234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3282789Sahrens 			break;
32832856Snd150628 
32844577Sahrens 		case NO_NAME:
32852856Snd150628 			break;
3286789Sahrens 		}
3287789Sahrens 	}
3288789Sahrens 
3289789Sahrens 	if (error == 0)
3290789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3291789Sahrens 
32922199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
32934543Smarks 	if (error == 0) {
32942199Sahrens 		error = rc;
32954543Smarks 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
32964543Smarks 			zfs_log_history(zc);
32974543Smarks 	}
3298789Sahrens 
3299789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3300789Sahrens 	return (error);
3301789Sahrens }
3302789Sahrens 
3303789Sahrens static int
3304789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3305789Sahrens {
3306789Sahrens 	if (cmd != DDI_ATTACH)
3307789Sahrens 		return (DDI_FAILURE);
3308789Sahrens 
3309789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3310789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3311789Sahrens 		return (DDI_FAILURE);
3312789Sahrens 
3313789Sahrens 	zfs_dip = dip;
3314789Sahrens 
3315789Sahrens 	ddi_report_dev(dip);
3316789Sahrens 
3317789Sahrens 	return (DDI_SUCCESS);
3318789Sahrens }
3319789Sahrens 
3320789Sahrens static int
3321789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3322789Sahrens {
3323789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3324789Sahrens 		return (DDI_FAILURE);
3325789Sahrens 
3326789Sahrens 	if (cmd != DDI_DETACH)
3327789Sahrens 		return (DDI_FAILURE);
3328789Sahrens 
3329789Sahrens 	zfs_dip = NULL;
3330789Sahrens 
3331789Sahrens 	ddi_prop_remove_all(dip);
3332789Sahrens 	ddi_remove_minor_node(dip, NULL);
3333789Sahrens 
3334789Sahrens 	return (DDI_SUCCESS);
3335789Sahrens }
3336789Sahrens 
3337789Sahrens /*ARGSUSED*/
3338789Sahrens static int
3339789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3340789Sahrens {
3341789Sahrens 	switch (infocmd) {
3342789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3343789Sahrens 		*result = zfs_dip;
3344789Sahrens 		return (DDI_SUCCESS);
3345789Sahrens 
3346789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3347849Sbonwick 		*result = (void *)0;
3348789Sahrens 		return (DDI_SUCCESS);
3349789Sahrens 	}
3350789Sahrens 
3351789Sahrens 	return (DDI_FAILURE);
3352789Sahrens }
3353789Sahrens 
3354789Sahrens /*
3355789Sahrens  * OK, so this is a little weird.
3356789Sahrens  *
3357789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3358789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3359789Sahrens  *
3360789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3361789Sahrens  * so most of the standard driver entry points are in zvol.c.
3362789Sahrens  */
3363789Sahrens static struct cb_ops zfs_cb_ops = {
3364789Sahrens 	zvol_open,	/* open */
3365789Sahrens 	zvol_close,	/* close */
3366789Sahrens 	zvol_strategy,	/* strategy */
3367789Sahrens 	nodev,		/* print */
33686423Sgw25295 	zvol_dump,	/* dump */
3369789Sahrens 	zvol_read,	/* read */
3370789Sahrens 	zvol_write,	/* write */
3371789Sahrens 	zfsdev_ioctl,	/* ioctl */
3372789Sahrens 	nodev,		/* devmap */
3373789Sahrens 	nodev,		/* mmap */
3374789Sahrens 	nodev,		/* segmap */
3375789Sahrens 	nochpoll,	/* poll */
3376789Sahrens 	ddi_prop_op,	/* prop_op */
3377789Sahrens 	NULL,		/* streamtab */
3378789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3379789Sahrens 	CB_REV,		/* version */
33803638Sbillm 	nodev,		/* async read */
33813638Sbillm 	nodev,		/* async write */
3382789Sahrens };
3383789Sahrens 
3384789Sahrens static struct dev_ops zfs_dev_ops = {
3385789Sahrens 	DEVO_REV,	/* version */
3386789Sahrens 	0,		/* refcnt */
3387789Sahrens 	zfs_info,	/* info */
3388789Sahrens 	nulldev,	/* identify */
3389789Sahrens 	nulldev,	/* probe */
3390789Sahrens 	zfs_attach,	/* attach */
3391789Sahrens 	zfs_detach,	/* detach */
3392789Sahrens 	nodev,		/* reset */
3393789Sahrens 	&zfs_cb_ops,	/* driver operations */
33947656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
33957656SSherry.Moore@Sun.COM 	NULL,		/* power */
33967656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3397789Sahrens };
3398789Sahrens 
3399789Sahrens static struct modldrv zfs_modldrv = {
34007656SSherry.Moore@Sun.COM 	&mod_driverops,
34017656SSherry.Moore@Sun.COM 	"ZFS storage pool",
34027656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3403789Sahrens };
3404789Sahrens 
3405789Sahrens static struct modlinkage modlinkage = {
3406789Sahrens 	MODREV_1,
3407789Sahrens 	(void *)&zfs_modlfs,
3408789Sahrens 	(void *)&zfs_modldrv,
3409789Sahrens 	NULL
3410789Sahrens };
3411789Sahrens 
34124720Sfr157268 
34134720Sfr157268 uint_t zfs_fsyncer_key;
34145326Sek110237 extern uint_t rrw_tsd_key;
34154720Sfr157268 
3416789Sahrens int
3417789Sahrens _init(void)
3418789Sahrens {
3419789Sahrens 	int error;
3420789Sahrens 
3421849Sbonwick 	spa_init(FREAD | FWRITE);
3422849Sbonwick 	zfs_init();
3423849Sbonwick 	zvol_init();
3424849Sbonwick 
3425849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3426849Sbonwick 		zvol_fini();
3427849Sbonwick 		zfs_fini();
3428849Sbonwick 		spa_fini();
3429789Sahrens 		return (error);
3430849Sbonwick 	}
3431789Sahrens 
34324720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
34335326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
34344720Sfr157268 
3435789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3436789Sahrens 	ASSERT(error == 0);
34374543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3438789Sahrens 
3439789Sahrens 	return (0);
3440789Sahrens }
3441789Sahrens 
3442789Sahrens int
3443789Sahrens _fini(void)
3444789Sahrens {
3445789Sahrens 	int error;
3446789Sahrens 
34471544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3448789Sahrens 		return (EBUSY);
3449789Sahrens 
3450789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3451789Sahrens 		return (error);
3452789Sahrens 
3453789Sahrens 	zvol_fini();
3454789Sahrens 	zfs_fini();
3455789Sahrens 	spa_fini();
34565331Samw 	if (zfs_nfsshare_inited)
34574543Smarks 		(void) ddi_modclose(nfs_mod);
34585331Samw 	if (zfs_smbshare_inited)
34595331Samw 		(void) ddi_modclose(smbsrv_mod);
34605331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
34614543Smarks 		(void) ddi_modclose(sharefs_mod);
3462789Sahrens 
34634720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3464789Sahrens 	ldi_ident_release(zfs_li);
3465789Sahrens 	zfs_li = NULL;
34664543Smarks 	mutex_destroy(&zfs_share_lock);
3467789Sahrens 
3468789Sahrens 	return (error);
3469789Sahrens }
3470789Sahrens 
3471789Sahrens int
3472789Sahrens _info(struct modinfo *modinfop)
3473789Sahrens {
3474789Sahrens 	return (mod_info(&modlinkage, modinfop));
3475789Sahrens }
3476