xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 9396:f41cf682d0d3)
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 
96*9396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
97*9396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
98*9396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
99*9396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
100*9396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
101*9396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
102*9396SMatthew.Ahrens@Sun.COM };
103*9396SMatthew.Ahrens@Sun.COM 
104*9396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
1058536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
1067184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1077184Stimh     boolean_t *);
1087184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
1097184Stimh 
110789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
111789Sahrens void
112789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
113789Sahrens {
114789Sahrens 	const char *newfile;
115789Sahrens 	char buf[256];
116789Sahrens 	va_list adx;
117789Sahrens 
118789Sahrens 	/*
119789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
120789Sahrens 	 */
121789Sahrens 	newfile = strrchr(file, '/');
122789Sahrens 	if (newfile != NULL) {
123789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
124789Sahrens 	} else {
125789Sahrens 		newfile = file;
126789Sahrens 	}
127789Sahrens 
128789Sahrens 	va_start(adx, fmt);
129789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
130789Sahrens 	va_end(adx);
131789Sahrens 
132789Sahrens 	/*
133789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
134789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
135789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
136789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
137789Sahrens 	 * arg0 = file name
138789Sahrens 	 * arg1 = function name
139789Sahrens 	 * arg2 = line number
140789Sahrens 	 * arg3 = message
141789Sahrens 	 */
142789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
143789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
144789Sahrens }
145789Sahrens 
1464543Smarks static void
1474715Sek110237 history_str_free(char *buf)
1484715Sek110237 {
1494715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1504715Sek110237 }
1514715Sek110237 
1524715Sek110237 static char *
1534715Sek110237 history_str_get(zfs_cmd_t *zc)
1544715Sek110237 {
1554715Sek110237 	char *buf;
1564715Sek110237 
1574715Sek110237 	if (zc->zc_history == NULL)
1584715Sek110237 		return (NULL);
1594715Sek110237 
1604715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1614715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1624715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1634715Sek110237 		history_str_free(buf);
1644715Sek110237 		return (NULL);
1654715Sek110237 	}
1664715Sek110237 
1674715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1684715Sek110237 
1694715Sek110237 	return (buf);
1704715Sek110237 }
1714715Sek110237 
1725375Stimh /*
1737042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1747042Sgw25295  */
1757042Sgw25295 static boolean_t
1767042Sgw25295 zfs_is_bootfs(const char *name)
1777042Sgw25295 {
1787042Sgw25295 	spa_t *spa;
1797042Sgw25295 	boolean_t ret = B_FALSE;
1807042Sgw25295 
1817042Sgw25295 	if (spa_open(name, &spa, FTAG) == 0) {
1827042Sgw25295 		if (spa->spa_bootfs) {
1837042Sgw25295 			objset_t *os;
1847042Sgw25295 
1857042Sgw25295 			if (dmu_objset_open(name, DMU_OST_ZFS,
1867042Sgw25295 			    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1877042Sgw25295 				ret = (dmu_objset_id(os) == spa->spa_bootfs);
1887042Sgw25295 				dmu_objset_close(os);
1897042Sgw25295 			}
1907042Sgw25295 		}
1917042Sgw25295 		spa_close(spa, FTAG);
1927042Sgw25295 	}
1937042Sgw25295 	return (ret);
1947042Sgw25295 }
1957042Sgw25295 
1967042Sgw25295 /*
1977184Stimh  * zfs_earlier_version
1985375Stimh  *
1995375Stimh  *	Return non-zero if the spa version is less than requested version.
2005375Stimh  */
2015331Samw static int
2027184Stimh zfs_earlier_version(const char *name, int version)
2035331Samw {
2045331Samw 	spa_t *spa;
2055331Samw 
2065331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
2075331Samw 		if (spa_version(spa) < version) {
2085331Samw 			spa_close(spa, FTAG);
2095331Samw 			return (1);
2105331Samw 		}
2115331Samw 		spa_close(spa, FTAG);
2125331Samw 	}
2135331Samw 	return (0);
2145331Samw }
2155331Samw 
2165977Smarks /*
2176689Smaybee  * zpl_earlier_version
2185977Smarks  *
2196689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2205977Smarks  */
2216689Smaybee static boolean_t
2226689Smaybee zpl_earlier_version(const char *name, int version)
2235977Smarks {
2245977Smarks 	objset_t *os;
2256689Smaybee 	boolean_t rc = B_TRUE;
2265977Smarks 
2275977Smarks 	if (dmu_objset_open(name, DMU_OST_ANY,
2286689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2296689Smaybee 		uint64_t zplversion;
2306689Smaybee 
2316689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2326689Smaybee 			rc = zplversion < version;
2335977Smarks 		dmu_objset_close(os);
2345977Smarks 	}
2355977Smarks 	return (rc);
2365977Smarks }
2375977Smarks 
2384715Sek110237 static void
2394543Smarks zfs_log_history(zfs_cmd_t *zc)
2404543Smarks {
2414543Smarks 	spa_t *spa;
2424603Sahrens 	char *buf;
2434543Smarks 
2444715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2454577Sahrens 		return;
2464577Sahrens 
2474715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2484715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2494715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2504715Sek110237 		spa_close(spa, FTAG);
2514543Smarks 	}
2524715Sek110237 	history_str_free(buf);
2534543Smarks }
2544543Smarks 
255789Sahrens /*
256789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
257789Sahrens  * and can be used in the local zone, as there is no associated dataset.
258789Sahrens  */
259789Sahrens /* ARGSUSED */
260789Sahrens static int
2614543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
262789Sahrens {
263789Sahrens 	return (0);
264789Sahrens }
265789Sahrens 
266789Sahrens /*
267789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
268789Sahrens  * no privileges, but must be visible in the local zone.
269789Sahrens  */
270789Sahrens /* ARGSUSED */
271789Sahrens static int
2724543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
273789Sahrens {
274789Sahrens 	if (INGLOBALZONE(curproc) ||
2754543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
276789Sahrens 		return (0);
277789Sahrens 
278789Sahrens 	return (ENOENT);
279789Sahrens }
280789Sahrens 
281789Sahrens static int
282789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
283789Sahrens {
284789Sahrens 	uint64_t zoned;
285789Sahrens 	int writable = 1;
286789Sahrens 
287789Sahrens 	/*
288789Sahrens 	 * The dataset must be visible by this zone -- check this first
289789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
290789Sahrens 	 */
291789Sahrens 	if (!INGLOBALZONE(curproc) &&
292789Sahrens 	    !zone_dataset_visible(dataset, &writable))
293789Sahrens 		return (ENOENT);
294789Sahrens 
295789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
296789Sahrens 		return (ENOENT);
297789Sahrens 
298789Sahrens 	if (INGLOBALZONE(curproc)) {
299789Sahrens 		/*
300789Sahrens 		 * If the fs is zoned, only root can access it from the
301789Sahrens 		 * global zone.
302789Sahrens 		 */
303789Sahrens 		if (secpolicy_zfs(cr) && zoned)
304789Sahrens 			return (EPERM);
305789Sahrens 	} else {
306789Sahrens 		/*
307789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
308789Sahrens 		 */
309789Sahrens 		if (!zoned)
310789Sahrens 			return (EPERM);
311789Sahrens 
312789Sahrens 		/* must be writable by this zone */
313789Sahrens 		if (!writable)
314789Sahrens 			return (EPERM);
315789Sahrens 	}
316789Sahrens 	return (0);
317789Sahrens }
318789Sahrens 
319789Sahrens int
3204543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
321789Sahrens {
322789Sahrens 	int error;
323789Sahrens 
3244543Smarks 	error = zfs_dozonecheck(name, cr);
3254543Smarks 	if (error == 0) {
3264543Smarks 		error = secpolicy_zfs(cr);
3274670Sahrens 		if (error)
3284543Smarks 			error = dsl_deleg_access(name, perm, cr);
3294543Smarks 	}
3304543Smarks 	return (error);
3314543Smarks }
3324543Smarks 
3334543Smarks static int
3344543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3354543Smarks {
3364543Smarks 	/*
3374543Smarks 	 * Check permissions for special properties.
3384543Smarks 	 */
3394543Smarks 	switch (prop) {
3404543Smarks 	case ZFS_PROP_ZONED:
3414543Smarks 		/*
3424543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3434543Smarks 		 */
3444543Smarks 		if (!INGLOBALZONE(curproc))
3454543Smarks 			return (EPERM);
3464543Smarks 		break;
347789Sahrens 
3484543Smarks 	case ZFS_PROP_QUOTA:
3494543Smarks 		if (!INGLOBALZONE(curproc)) {
3504543Smarks 			uint64_t zoned;
3514543Smarks 			char setpoint[MAXNAMELEN];
3524543Smarks 			/*
3534543Smarks 			 * Unprivileged users are allowed to modify the
3544543Smarks 			 * quota on things *under* (ie. contained by)
3554543Smarks 			 * the thing they own.
3564543Smarks 			 */
3574543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3584543Smarks 			    setpoint))
3594543Smarks 				return (EPERM);
3604670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3614543Smarks 				return (EPERM);
3624543Smarks 		}
3634670Sahrens 		break;
3644543Smarks 	}
3654543Smarks 
3664787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
367789Sahrens }
368789Sahrens 
3694543Smarks int
3704543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3714543Smarks {
3724543Smarks 	int error;
3734543Smarks 
3744543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3754543Smarks 	if (error)
3764543Smarks 		return (error);
3774543Smarks 
3784543Smarks 	/*
3794543Smarks 	 * permission to set permissions will be evaluated later in
3804543Smarks 	 * dsl_deleg_can_allow()
3814543Smarks 	 */
3824543Smarks 	return (0);
3834543Smarks }
3844543Smarks 
3854543Smarks int
3864543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3874543Smarks {
3884543Smarks 	int error;
3894543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
3904543Smarks 	    ZFS_DELEG_PERM_ROLLBACK, cr);
3914543Smarks 	if (error == 0)
3924543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
3934543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
3944543Smarks 	return (error);
3954543Smarks }
3964543Smarks 
3974543Smarks int
3984543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3994543Smarks {
4004543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
4014543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
4024543Smarks }
4034543Smarks 
4048845Samw@Sun.COM static int
4058845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
4068845Samw@Sun.COM {
4078845Samw@Sun.COM 	vnode_t *vp;
4088845Samw@Sun.COM 	int error;
4098845Samw@Sun.COM 
4108845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4118845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
4128845Samw@Sun.COM 		return (error);
4138845Samw@Sun.COM 
4148845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
4158845Samw@Sun.COM 
4168845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4178845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4188845Samw@Sun.COM 	    zc->zc_name) != 0)) {
4198845Samw@Sun.COM 		VN_RELE(vp);
4208845Samw@Sun.COM 		return (EPERM);
4218845Samw@Sun.COM 	}
4228845Samw@Sun.COM 
4238845Samw@Sun.COM 	VN_RELE(vp);
4248845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
4258845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
4268845Samw@Sun.COM }
4278845Samw@Sun.COM 
4284543Smarks int
4294543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
4304543Smarks {
4314543Smarks 	if (!INGLOBALZONE(curproc))
4324543Smarks 		return (EPERM);
4334543Smarks 
4345367Sahrens 	if (secpolicy_nfs(cr) == 0) {
4354543Smarks 		return (0);
4364543Smarks 	} else {
4378845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4388845Samw@Sun.COM 	}
4398845Samw@Sun.COM }
4408845Samw@Sun.COM 
4418845Samw@Sun.COM int
4428845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
4438845Samw@Sun.COM {
4448845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
4458845Samw@Sun.COM 		return (EPERM);
4468845Samw@Sun.COM 
4478845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
4488845Samw@Sun.COM 		return (0);
4498845Samw@Sun.COM 	} else {
4508845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4514543Smarks 	}
4524543Smarks }
4534543Smarks 
454789Sahrens static int
4554543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
456789Sahrens {
457789Sahrens 	char *cp;
458789Sahrens 
459789Sahrens 	/*
460789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
461789Sahrens 	 */
4624543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4634543Smarks 	cp = strrchr(parent, '@');
464789Sahrens 	if (cp != NULL) {
465789Sahrens 		cp[0] = '\0';
466789Sahrens 	} else {
4674543Smarks 		cp = strrchr(parent, '/');
468789Sahrens 		if (cp == NULL)
469789Sahrens 			return (ENOENT);
470789Sahrens 		cp[0] = '\0';
471789Sahrens 	}
472789Sahrens 
4734543Smarks 	return (0);
4744543Smarks }
4754543Smarks 
4764543Smarks int
4774543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4784543Smarks {
4794543Smarks 	int error;
4804543Smarks 
4814543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4824543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4834543Smarks 		return (error);
4844543Smarks 
4854543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4864543Smarks }
4874543Smarks 
4884543Smarks static int
4894543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4904543Smarks {
4914543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4924543Smarks }
4934543Smarks 
4944543Smarks /*
4954543Smarks  * Must have sys_config privilege to check the iscsi permission
4964543Smarks  */
4974543Smarks /* ARGSUSED */
4984543Smarks static int
4994543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
5004543Smarks {
5014543Smarks 	return (secpolicy_zfs(cr));
5024543Smarks }
5034543Smarks 
5044543Smarks int
5054543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
5064543Smarks {
5074543Smarks 	char 	parentname[MAXNAMELEN];
5084543Smarks 	int	error;
5094543Smarks 
5104543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5114543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
5124543Smarks 		return (error);
5134543Smarks 
5144543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5154543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5164543Smarks 		return (error);
5174543Smarks 
5184543Smarks 	if ((error = zfs_get_parent(to, parentname,
5194543Smarks 	    sizeof (parentname))) != 0)
5204543Smarks 		return (error);
5214543Smarks 
5224543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5234543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
5244543Smarks 		return (error);
5254543Smarks 
5264543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5274543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5284543Smarks 		return (error);
5294543Smarks 
5304543Smarks 	return (error);
5314543Smarks }
5324543Smarks 
5334543Smarks static int
5344543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5354543Smarks {
5364543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5374543Smarks }
5384543Smarks 
5394543Smarks static int
5404543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5414543Smarks {
5424543Smarks 	char 	parentname[MAXNAMELEN];
5434543Smarks 	objset_t *clone;
5444543Smarks 	int error;
5454543Smarks 
5464543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5474543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5484543Smarks 	if (error)
5494543Smarks 		return (error);
5504543Smarks 
5514543Smarks 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
5526689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &clone);
5534543Smarks 
5544543Smarks 	if (error == 0) {
5554543Smarks 		dsl_dataset_t *pclone = NULL;
5564543Smarks 		dsl_dir_t *dd;
5574543Smarks 		dd = clone->os->os_dsl_dataset->ds_dir;
5584543Smarks 
5594543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5606689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5616689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5624543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5634543Smarks 		if (error) {
5644543Smarks 			dmu_objset_close(clone);
5654543Smarks 			return (error);
5664543Smarks 		}
5674543Smarks 
5684543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5694543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5704543Smarks 
5714543Smarks 		dsl_dataset_name(pclone, parentname);
5724543Smarks 		dmu_objset_close(clone);
5736689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5744543Smarks 		if (error == 0)
5754543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5764543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5774543Smarks 	}
5784543Smarks 	return (error);
5794543Smarks }
5804543Smarks 
5814543Smarks static int
5824543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5834543Smarks {
5844543Smarks 	int error;
5854543Smarks 
5864543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5874543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5884543Smarks 		return (error);
5894543Smarks 
5904543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5914543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5924543Smarks 		return (error);
5934543Smarks 
5944543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5954543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5964543Smarks }
5974543Smarks 
5984543Smarks int
5994543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
6004543Smarks {
6014543Smarks 	int error;
6024543Smarks 
6034543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
6044543Smarks 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
6054543Smarks 		return (error);
6064543Smarks 
6074543Smarks 	error = zfs_secpolicy_write_perms(name,
6084543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6094543Smarks 
6104543Smarks 	return (error);
6114543Smarks }
6124543Smarks 
6134543Smarks static int
6144543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
6154543Smarks {
6164543Smarks 
6174543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
6184543Smarks }
6194543Smarks 
6204543Smarks static int
6214543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
6224543Smarks {
6234543Smarks 	char 	parentname[MAXNAMELEN];
6244543Smarks 	int 	error;
6254543Smarks 
6264543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
6274543Smarks 	    sizeof (parentname))) != 0)
6284543Smarks 		return (error);
6294543Smarks 
6304543Smarks 	if (zc->zc_value[0] != '\0') {
6314543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6324543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6334543Smarks 			return (error);
6344543Smarks 	}
6354543Smarks 
6364543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6374543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6384543Smarks 		return (error);
6394543Smarks 
6404543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6414543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6424543Smarks 
6434543Smarks 	return (error);
6444543Smarks }
6454543Smarks 
6464543Smarks static int
6474543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6484543Smarks {
6494543Smarks 	int error;
6504543Smarks 
6514543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6524543Smarks 	if (error) {
6534543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6544543Smarks 	}
6554543Smarks 	return (error);
656789Sahrens }
657789Sahrens 
658789Sahrens /*
659789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
660789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
661789Sahrens  */
662789Sahrens /* ARGSUSED */
663789Sahrens static int
6644543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
665789Sahrens {
666789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
667789Sahrens 		return (EPERM);
668789Sahrens 
669789Sahrens 	return (0);
670789Sahrens }
671789Sahrens 
672789Sahrens /*
6734543Smarks  * Just like zfs_secpolicy_config, except that we will check for
6744543Smarks  * mount permission on the dataset for permission to create/remove
6754543Smarks  * the minor nodes.
6764543Smarks  */
6774543Smarks static int
6784543Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
6794543Smarks {
6804543Smarks 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
6814543Smarks 		return (dsl_deleg_access(zc->zc_name,
6824543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr));
6834543Smarks 	}
6844543Smarks 
6854543Smarks 	return (0);
6864543Smarks }
6874543Smarks 
6884543Smarks /*
6891544Seschrock  * Policy for fault injection.  Requires all privileges.
6901544Seschrock  */
6911544Seschrock /* ARGSUSED */
6921544Seschrock static int
6934543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6941544Seschrock {
6951544Seschrock 	return (secpolicy_zinject(cr));
6961544Seschrock }
6971544Seschrock 
6984849Sahrens static int
6994849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
7004849Sahrens {
7014849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
7024849Sahrens 
7035094Slling 	if (prop == ZPROP_INVAL) {
7044849Sahrens 		if (!zfs_prop_user(zc->zc_value))
7054849Sahrens 			return (EINVAL);
7064849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
7074849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
7084849Sahrens 	} else {
7094849Sahrens 		if (!zfs_prop_inheritable(prop))
7104849Sahrens 			return (EINVAL);
7114849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
7124849Sahrens 	}
7134849Sahrens }
7144849Sahrens 
715*9396SMatthew.Ahrens@Sun.COM static int
716*9396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
717*9396SMatthew.Ahrens@Sun.COM {
718*9396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
719*9396SMatthew.Ahrens@Sun.COM 	if (err)
720*9396SMatthew.Ahrens@Sun.COM 		return (err);
721*9396SMatthew.Ahrens@Sun.COM 
722*9396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
723*9396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
724*9396SMatthew.Ahrens@Sun.COM 
725*9396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
726*9396SMatthew.Ahrens@Sun.COM 		/*
727*9396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
728*9396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
729*9396SMatthew.Ahrens@Sun.COM 		 */
730*9396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
731*9396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
732*9396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
733*9396SMatthew.Ahrens@Sun.COM 				return (0);
734*9396SMatthew.Ahrens@Sun.COM 		} else {
735*9396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
736*9396SMatthew.Ahrens@Sun.COM 				return (0);
737*9396SMatthew.Ahrens@Sun.COM 		}
738*9396SMatthew.Ahrens@Sun.COM 	}
739*9396SMatthew.Ahrens@Sun.COM 
740*9396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
741*9396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
742*9396SMatthew.Ahrens@Sun.COM }
743*9396SMatthew.Ahrens@Sun.COM 
744*9396SMatthew.Ahrens@Sun.COM static int
745*9396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
746*9396SMatthew.Ahrens@Sun.COM {
747*9396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
748*9396SMatthew.Ahrens@Sun.COM 	if (err)
749*9396SMatthew.Ahrens@Sun.COM 		return (err);
750*9396SMatthew.Ahrens@Sun.COM 
751*9396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
752*9396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
753*9396SMatthew.Ahrens@Sun.COM 
754*9396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
755*9396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
756*9396SMatthew.Ahrens@Sun.COM }
757*9396SMatthew.Ahrens@Sun.COM 
758*9396SMatthew.Ahrens@Sun.COM static int
759*9396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
760*9396SMatthew.Ahrens@Sun.COM {
761*9396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
762*9396SMatthew.Ahrens@Sun.COM }
763*9396SMatthew.Ahrens@Sun.COM 
7641544Seschrock /*
765789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
766789Sahrens  */
767789Sahrens static int
7685094Slling get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
769789Sahrens {
770789Sahrens 	char *packed;
771789Sahrens 	int error;
7725094Slling 	nvlist_t *list = NULL;
773789Sahrens 
774789Sahrens 	/*
7752676Seschrock 	 * Read in and unpack the user-supplied nvlist.
776789Sahrens 	 */
7775094Slling 	if (size == 0)
778789Sahrens 		return (EINVAL);
779789Sahrens 
780789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
781789Sahrens 
7825094Slling 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
783789Sahrens 		kmem_free(packed, size);
784789Sahrens 		return (error);
785789Sahrens 	}
786789Sahrens 
7875094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
788789Sahrens 		kmem_free(packed, size);
789789Sahrens 		return (error);
790789Sahrens 	}
791789Sahrens 
792789Sahrens 	kmem_free(packed, size);
793789Sahrens 
7945094Slling 	*nvp = list;
795789Sahrens 	return (0);
796789Sahrens }
797789Sahrens 
798789Sahrens static int
7992676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
8002676Seschrock {
8012676Seschrock 	char *packed = NULL;
8022676Seschrock 	size_t size;
8032676Seschrock 	int error;
8042676Seschrock 
8052676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
8062676Seschrock 
8072676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
8082676Seschrock 		error = ENOMEM;
8092676Seschrock 	} else {
8104611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
8112676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
8122676Seschrock 		    KM_SLEEP) == 0);
8132676Seschrock 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
8142676Seschrock 		    size);
8152676Seschrock 		kmem_free(packed, size);
8162676Seschrock 	}
8172676Seschrock 
8182676Seschrock 	zc->zc_nvlist_dst_size = size;
8192676Seschrock 	return (error);
8202676Seschrock }
8212676Seschrock 
8222676Seschrock static int
823*9396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
824*9396SMatthew.Ahrens@Sun.COM {
825*9396SMatthew.Ahrens@Sun.COM 	objset_t *os;
826*9396SMatthew.Ahrens@Sun.COM 	int error;
827*9396SMatthew.Ahrens@Sun.COM 
828*9396SMatthew.Ahrens@Sun.COM 	error = dmu_objset_open(dsname, DMU_OST_ZFS,
829*9396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os);
830*9396SMatthew.Ahrens@Sun.COM 	if (error)
831*9396SMatthew.Ahrens@Sun.COM 		return (error);
832*9396SMatthew.Ahrens@Sun.COM 
833*9396SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os->os_user_ptr_lock);
834*9396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
835*9396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
836*9396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
837*9396SMatthew.Ahrens@Sun.COM 	} else {
838*9396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
839*9396SMatthew.Ahrens@Sun.COM 	}
840*9396SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os->os_user_ptr_lock);
841*9396SMatthew.Ahrens@Sun.COM 	dmu_objset_close(os);
842*9396SMatthew.Ahrens@Sun.COM 	return (error);
843*9396SMatthew.Ahrens@Sun.COM }
844*9396SMatthew.Ahrens@Sun.COM 
845*9396SMatthew.Ahrens@Sun.COM /*
846*9396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
847*9396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
848*9396SMatthew.Ahrens@Sun.COM  */
849*9396SMatthew.Ahrens@Sun.COM static int
850*9396SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, boolean_t readonly, void *tag, zfsvfs_t **zvp)
851*9396SMatthew.Ahrens@Sun.COM {
852*9396SMatthew.Ahrens@Sun.COM 	int error = 0;
853*9396SMatthew.Ahrens@Sun.COM 	int mode = DS_MODE_OWNER | (readonly ? DS_MODE_READONLY : 0);
854*9396SMatthew.Ahrens@Sun.COM 
855*9396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
856*9396SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, mode, zvp);
857*9396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
858*9396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
859*9396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
860*9396SMatthew.Ahrens@Sun.COM 			/*
861*9396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
862*9396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
863*9396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
864*9396SMatthew.Ahrens@Sun.COM 			 */
865*9396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
866*9396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
867*9396SMatthew.Ahrens@Sun.COM 		}
868*9396SMatthew.Ahrens@Sun.COM 	}
869*9396SMatthew.Ahrens@Sun.COM 	return (error);
870*9396SMatthew.Ahrens@Sun.COM }
871*9396SMatthew.Ahrens@Sun.COM 
872*9396SMatthew.Ahrens@Sun.COM static void
873*9396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
874*9396SMatthew.Ahrens@Sun.COM {
875*9396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
876*9396SMatthew.Ahrens@Sun.COM 
877*9396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
878*9396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
879*9396SMatthew.Ahrens@Sun.COM 	} else {
880*9396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(zfsvfs->z_os);
881*9396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
882*9396SMatthew.Ahrens@Sun.COM 	}
883*9396SMatthew.Ahrens@Sun.COM }
884*9396SMatthew.Ahrens@Sun.COM 
885*9396SMatthew.Ahrens@Sun.COM static int
886789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
887789Sahrens {
888789Sahrens 	int error;
8895094Slling 	nvlist_t *config, *props = NULL;
8907184Stimh 	nvlist_t *rootprops = NULL;
8917184Stimh 	nvlist_t *zplprops = NULL;
8924715Sek110237 	char *buf;
893789Sahrens 
8945094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8955094Slling 	    &config))
8964988Sek110237 		return (error);
8974715Sek110237 
8985094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8995094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
9005094Slling 		nvlist_free(config);
9015094Slling 		return (error);
9025094Slling 	}
9035094Slling 
9047184Stimh 	if (props) {
9057184Stimh 		nvlist_t *nvl = NULL;
9067184Stimh 		uint64_t version = SPA_VERSION;
9077184Stimh 
9087184Stimh 		(void) nvlist_lookup_uint64(props,
9097184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9107184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
9117184Stimh 			error = EINVAL;
9127184Stimh 			goto pool_props_bad;
9137184Stimh 		}
9147184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9157184Stimh 		if (nvl) {
9167184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9177184Stimh 			if (error != 0) {
9187184Stimh 				nvlist_free(config);
9197184Stimh 				nvlist_free(props);
9207184Stimh 				return (error);
9217184Stimh 			}
9227184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9237184Stimh 		}
9247184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9257184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
9267184Stimh 		    zplprops, NULL);
9277184Stimh 		if (error)
9287184Stimh 			goto pool_props_bad;
9297184Stimh 	}
9307184Stimh 
9314988Sek110237 	buf = history_str_get(zc);
932789Sahrens 
9337184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9347184Stimh 
9357184Stimh 	/*
9367184Stimh 	 * Set the remaining root properties
9377184Stimh 	 */
9387184Stimh 	if (!error &&
9397184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9407184Stimh 		(void) spa_destroy(zc->zc_name);
941789Sahrens 
9424988Sek110237 	if (buf != NULL)
9434988Sek110237 		history_str_free(buf);
9445094Slling 
9457184Stimh pool_props_bad:
9467184Stimh 	nvlist_free(rootprops);
9477184Stimh 	nvlist_free(zplprops);
948789Sahrens 	nvlist_free(config);
9497184Stimh 	nvlist_free(props);
9505094Slling 
951789Sahrens 	return (error);
952789Sahrens }
953789Sahrens 
954789Sahrens static int
955789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
956789Sahrens {
9574543Smarks 	int error;
9584543Smarks 	zfs_log_history(zc);
9594543Smarks 	error = spa_destroy(zc->zc_name);
9604543Smarks 	return (error);
961789Sahrens }
962789Sahrens 
963789Sahrens static int
964789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
965789Sahrens {
966789Sahrens 	int error;
9675094Slling 	nvlist_t *config, *props = NULL;
968789Sahrens 	uint64_t guid;
969789Sahrens 
9705094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9715094Slling 	    &config)) != 0)
972789Sahrens 		return (error);
973789Sahrens 
9745094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9755094Slling 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
9765094Slling 		nvlist_free(config);
9775094Slling 		return (error);
9785094Slling 	}
9795094Slling 
980789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
9811544Seschrock 	    guid != zc->zc_guid)
982789Sahrens 		error = EINVAL;
9836643Seschrock 	else if (zc->zc_cookie)
9846643Seschrock 		error = spa_import_faulted(zc->zc_name, config,
9856643Seschrock 		    props);
986789Sahrens 	else
9875094Slling 		error = spa_import(zc->zc_name, config, props);
988789Sahrens 
989789Sahrens 	nvlist_free(config);
990789Sahrens 
9915094Slling 	if (props)
9925094Slling 		nvlist_free(props);
9935094Slling 
994789Sahrens 	return (error);
995789Sahrens }
996789Sahrens 
997789Sahrens static int
998789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
999789Sahrens {
10004543Smarks 	int error;
10017214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
10028211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
10037214Slling 
10044543Smarks 	zfs_log_history(zc);
10058211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
10064543Smarks 	return (error);
1007789Sahrens }
1008789Sahrens 
1009789Sahrens static int
1010789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1011789Sahrens {
1012789Sahrens 	nvlist_t *configs;
1013789Sahrens 	int error;
1014789Sahrens 
1015789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1016789Sahrens 		return (EEXIST);
1017789Sahrens 
10182676Seschrock 	error = put_nvlist(zc, configs);
1019789Sahrens 
1020789Sahrens 	nvlist_free(configs);
1021789Sahrens 
1022789Sahrens 	return (error);
1023789Sahrens }
1024789Sahrens 
1025789Sahrens static int
1026789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1027789Sahrens {
1028789Sahrens 	nvlist_t *config;
1029789Sahrens 	int error;
10301544Seschrock 	int ret = 0;
1031789Sahrens 
10322676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10332676Seschrock 	    sizeof (zc->zc_value));
1034789Sahrens 
1035789Sahrens 	if (config != NULL) {
10362676Seschrock 		ret = put_nvlist(zc, config);
1037789Sahrens 		nvlist_free(config);
10381544Seschrock 
10391544Seschrock 		/*
10401544Seschrock 		 * The config may be present even if 'error' is non-zero.
10411544Seschrock 		 * In this case we return success, and preserve the real errno
10421544Seschrock 		 * in 'zc_cookie'.
10431544Seschrock 		 */
10441544Seschrock 		zc->zc_cookie = error;
1045789Sahrens 	} else {
10461544Seschrock 		ret = error;
1047789Sahrens 	}
1048789Sahrens 
10491544Seschrock 	return (ret);
1050789Sahrens }
1051789Sahrens 
1052789Sahrens /*
1053789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1054789Sahrens  * user land knows which devices are available and overall pool health.
1055789Sahrens  */
1056789Sahrens static int
1057789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1058789Sahrens {
1059789Sahrens 	nvlist_t *tryconfig, *config;
1060789Sahrens 	int error;
1061789Sahrens 
10625094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10635094Slling 	    &tryconfig)) != 0)
1064789Sahrens 		return (error);
1065789Sahrens 
1066789Sahrens 	config = spa_tryimport(tryconfig);
1067789Sahrens 
1068789Sahrens 	nvlist_free(tryconfig);
1069789Sahrens 
1070789Sahrens 	if (config == NULL)
1071789Sahrens 		return (EINVAL);
1072789Sahrens 
10732676Seschrock 	error = put_nvlist(zc, config);
1074789Sahrens 	nvlist_free(config);
1075789Sahrens 
1076789Sahrens 	return (error);
1077789Sahrens }
1078789Sahrens 
1079789Sahrens static int
1080789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1081789Sahrens {
1082789Sahrens 	spa_t *spa;
1083789Sahrens 	int error;
1084789Sahrens 
10852926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10862926Sek110237 		return (error);
10872926Sek110237 
10887046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
10892926Sek110237 
10902926Sek110237 	spa_close(spa, FTAG);
10912926Sek110237 
1092789Sahrens 	return (error);
1093789Sahrens }
1094789Sahrens 
1095789Sahrens static int
1096789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1097789Sahrens {
1098789Sahrens 	spa_t *spa;
1099789Sahrens 	int error;
1100789Sahrens 
1101789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1102789Sahrens 	if (error == 0) {
1103789Sahrens 		spa_freeze(spa);
1104789Sahrens 		spa_close(spa, FTAG);
1105789Sahrens 	}
1106789Sahrens 	return (error);
1107789Sahrens }
1108789Sahrens 
1109789Sahrens static int
11101760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11111760Seschrock {
11121760Seschrock 	spa_t *spa;
11131760Seschrock 	int error;
11141760Seschrock 
11152926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11162926Sek110237 		return (error);
11172926Sek110237 
11185118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11195118Slling 		spa_close(spa, FTAG);
11205118Slling 		return (EINVAL);
11215118Slling 	}
11225118Slling 
11235094Slling 	spa_upgrade(spa, zc->zc_cookie);
11242926Sek110237 	spa_close(spa, FTAG);
11252926Sek110237 
11262926Sek110237 	return (error);
11272926Sek110237 }
11282926Sek110237 
11292926Sek110237 static int
11302926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11312926Sek110237 {
11322926Sek110237 	spa_t *spa;
11332926Sek110237 	char *hist_buf;
11342926Sek110237 	uint64_t size;
11352926Sek110237 	int error;
11362926Sek110237 
11372926Sek110237 	if ((size = zc->zc_history_len) == 0)
11382926Sek110237 		return (EINVAL);
11392926Sek110237 
11402926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11412926Sek110237 		return (error);
11422926Sek110237 
11434577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11443863Sek110237 		spa_close(spa, FTAG);
11453863Sek110237 		return (ENOTSUP);
11463863Sek110237 	}
11473863Sek110237 
11482926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11492926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11502926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11514543Smarks 		error = xcopyout(hist_buf,
11524543Smarks 		    (char *)(uintptr_t)zc->zc_history,
11532926Sek110237 		    zc->zc_history_len);
11542926Sek110237 	}
11552926Sek110237 
11562926Sek110237 	spa_close(spa, FTAG);
11572926Sek110237 	kmem_free(hist_buf, size);
11582926Sek110237 	return (error);
11592926Sek110237 }
11602926Sek110237 
11612926Sek110237 static int
11623444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11633444Sek110237 {
11643444Sek110237 	int error;
11653444Sek110237 
11663912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11673444Sek110237 		return (error);
11683444Sek110237 
11693444Sek110237 	return (0);
11703444Sek110237 }
11713444Sek110237 
11723444Sek110237 static int
11733444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11743444Sek110237 {
11753444Sek110237 	objset_t *osp;
11763444Sek110237 	int error;
11773444Sek110237 
11783444Sek110237 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
11796689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
11803444Sek110237 		return (error);
11813444Sek110237 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
11823444Sek110237 	    sizeof (zc->zc_value));
11833444Sek110237 	dmu_objset_close(osp);
11843444Sek110237 
11853444Sek110237 	return (error);
11863444Sek110237 }
11873444Sek110237 
11883444Sek110237 static int
1189789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1190789Sahrens {
1191789Sahrens 	spa_t *spa;
1192789Sahrens 	int error;
11936423Sgw25295 	nvlist_t *config, **l2cache, **spares;
11946423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1195789Sahrens 
1196789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1197789Sahrens 	if (error != 0)
1198789Sahrens 		return (error);
1199789Sahrens 
12005450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12015450Sbrendan 	    &config);
12025450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12035450Sbrendan 	    &l2cache, &nl2cache);
12045450Sbrendan 
12056423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12066423Sgw25295 	    &spares, &nspares);
12076423Sgw25295 
12083912Slling 	/*
12093912Slling 	 * A root pool with concatenated devices is not supported.
12106423Sgw25295 	 * Thus, can not add a device to a root pool.
12116423Sgw25295 	 *
12126423Sgw25295 	 * Intent log device can not be added to a rootpool because
12136423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12146423Sgw25295 	 * can not be accessed during the mountroot time.
12156423Sgw25295 	 *
12166423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12173912Slling 	 */
12186423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
12193912Slling 		spa_close(spa, FTAG);
12203912Slling 		return (EDOM);
12213912Slling 	}
12223912Slling 
12235450Sbrendan 	if (error == 0) {
1224789Sahrens 		error = spa_vdev_add(spa, config);
1225789Sahrens 		nvlist_free(config);
1226789Sahrens 	}
1227789Sahrens 	spa_close(spa, FTAG);
1228789Sahrens 	return (error);
1229789Sahrens }
1230789Sahrens 
1231789Sahrens static int
1232789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1233789Sahrens {
12342082Seschrock 	spa_t *spa;
12352082Seschrock 	int error;
12362082Seschrock 
12372082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12382082Seschrock 	if (error != 0)
12392082Seschrock 		return (error);
12402082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12412082Seschrock 	spa_close(spa, FTAG);
12422082Seschrock 	return (error);
1243789Sahrens }
1244789Sahrens 
1245789Sahrens static int
12464451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1247789Sahrens {
1248789Sahrens 	spa_t *spa;
1249789Sahrens 	int error;
12504451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1251789Sahrens 
12522926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1253789Sahrens 		return (error);
12544451Seschrock 	switch (zc->zc_cookie) {
12554451Seschrock 	case VDEV_STATE_ONLINE:
12564451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12574451Seschrock 		break;
12584451Seschrock 
12594451Seschrock 	case VDEV_STATE_OFFLINE:
12604451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12614451Seschrock 		break;
1262789Sahrens 
12634451Seschrock 	case VDEV_STATE_FAULTED:
12644451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12654451Seschrock 		break;
1266789Sahrens 
12674451Seschrock 	case VDEV_STATE_DEGRADED:
12684451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12694451Seschrock 		break;
12704451Seschrock 
12714451Seschrock 	default:
12724451Seschrock 		error = EINVAL;
12734451Seschrock 	}
12744451Seschrock 	zc->zc_cookie = newstate;
1275789Sahrens 	spa_close(spa, FTAG);
1276789Sahrens 	return (error);
1277789Sahrens }
1278789Sahrens 
1279789Sahrens static int
1280789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1281789Sahrens {
1282789Sahrens 	spa_t *spa;
1283789Sahrens 	int replacing = zc->zc_cookie;
1284789Sahrens 	nvlist_t *config;
1285789Sahrens 	int error;
1286789Sahrens 
12872926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1288789Sahrens 		return (error);
1289789Sahrens 
12905094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12915094Slling 	    &config)) == 0) {
12921544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1293789Sahrens 		nvlist_free(config);
1294789Sahrens 	}
1295789Sahrens 
1296789Sahrens 	spa_close(spa, FTAG);
1297789Sahrens 	return (error);
1298789Sahrens }
1299789Sahrens 
1300789Sahrens static int
1301789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1302789Sahrens {
1303789Sahrens 	spa_t *spa;
1304789Sahrens 	int error;
1305789Sahrens 
13062926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1307789Sahrens 		return (error);
1308789Sahrens 
13098241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1310789Sahrens 
1311789Sahrens 	spa_close(spa, FTAG);
1312789Sahrens 	return (error);
1313789Sahrens }
1314789Sahrens 
1315789Sahrens static int
13161354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13171354Seschrock {
13181354Seschrock 	spa_t *spa;
13192676Seschrock 	char *path = zc->zc_value;
13201544Seschrock 	uint64_t guid = zc->zc_guid;
13211354Seschrock 	int error;
13221354Seschrock 
13231354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13241354Seschrock 	if (error != 0)
13251354Seschrock 		return (error);
13261354Seschrock 
13271354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13281354Seschrock 	spa_close(spa, FTAG);
13291354Seschrock 	return (error);
13301354Seschrock }
13311354Seschrock 
13325367Sahrens /*
13335367Sahrens  * inputs:
13345367Sahrens  * zc_name		name of filesystem
13355367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13365367Sahrens  *
13375367Sahrens  * outputs:
13385367Sahrens  * zc_objset_stats	stats
13395367Sahrens  * zc_nvlist_dst	property nvlist
13405367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13415367Sahrens  */
13421354Seschrock static int
1343789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1344789Sahrens {
1345789Sahrens 	objset_t *os = NULL;
1346789Sahrens 	int error;
13471356Seschrock 	nvlist_t *nv;
1348789Sahrens 
13496689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13506689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1351789Sahrens 		return (error);
1352789Sahrens 
13532885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1354789Sahrens 
13552856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13566689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13572885Sahrens 		dmu_objset_stats(os, nv);
13583087Sahrens 		/*
13595147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13603087Sahrens 		 * which we aren't supposed to do with a
13616689Smaybee 		 * DS_MODE_USER hold, because it could be
13623087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
13633087Sahrens 		 */
13644577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13654577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13664577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13674577Sahrens 		}
13682676Seschrock 		error = put_nvlist(zc, nv);
13691356Seschrock 		nvlist_free(nv);
13701356Seschrock 	}
1371789Sahrens 
1372789Sahrens 	dmu_objset_close(os);
1373789Sahrens 	return (error);
1374789Sahrens }
1375789Sahrens 
13765498Stimh static int
13775498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
13785498Stimh {
13795498Stimh 	uint64_t value;
13805498Stimh 	int error;
13815498Stimh 
13825498Stimh 	/*
13835498Stimh 	 * zfs_get_zplprop() will either find a value or give us
13845498Stimh 	 * the default value (if there is one).
13855498Stimh 	 */
13865498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
13875498Stimh 		return (error);
13885498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
13895498Stimh 	return (0);
13905498Stimh }
13915498Stimh 
13925498Stimh /*
13935498Stimh  * inputs:
13945498Stimh  * zc_name		name of filesystem
13955498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
13965498Stimh  *
13975498Stimh  * outputs:
13985498Stimh  * zc_nvlist_dst	zpl property nvlist
13995498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14005498Stimh  */
14015498Stimh static int
14025498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14035498Stimh {
14045498Stimh 	objset_t *os;
14055498Stimh 	int err;
14065498Stimh 
14076689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
14086689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
14095498Stimh 		return (err);
14105498Stimh 
14115498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14125498Stimh 
14135498Stimh 	/*
14145498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14156689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14166689Smaybee 	 * hold, because it could be inconsistent.
14175498Stimh 	 */
14185498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14195498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14205498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14215498Stimh 		nvlist_t *nv;
14225498Stimh 
14235498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14245498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14255498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14265498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14275498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14285498Stimh 			err = put_nvlist(zc, nv);
14295498Stimh 		nvlist_free(nv);
14305498Stimh 	} else {
14315498Stimh 		err = ENOENT;
14325498Stimh 	}
14335498Stimh 	dmu_objset_close(os);
14345498Stimh 	return (err);
14355498Stimh }
14365498Stimh 
1437*9396SMatthew.Ahrens@Sun.COM static boolean_t
1438*9396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
1439*9396SMatthew.Ahrens@Sun.COM {
1440*9396SMatthew.Ahrens@Sun.COM 	/*
1441*9396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
1442*9396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
1443*9396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
1444*9396SMatthew.Ahrens@Sun.COM 	 */
1445*9396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
1446*9396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
1447*9396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
1448*9396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
1449*9396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1450*9396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
1451*9396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1452*9396SMatthew.Ahrens@Sun.COM }
1453*9396SMatthew.Ahrens@Sun.COM 
14545367Sahrens /*
14555367Sahrens  * inputs:
14565367Sahrens  * zc_name		name of filesystem
14575367Sahrens  * zc_cookie		zap cursor
14585367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14595367Sahrens  *
14605367Sahrens  * outputs:
14615367Sahrens  * zc_name		name of next filesystem
1462*9396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14635367Sahrens  * zc_objset_stats	stats
14645367Sahrens  * zc_nvlist_dst	property nvlist
14655367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14665367Sahrens  */
1467789Sahrens static int
1468789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1469789Sahrens {
1470885Sahrens 	objset_t *os;
1471789Sahrens 	int error;
1472789Sahrens 	char *p;
1473789Sahrens 
14746689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
14756689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1476885Sahrens 		if (error == ENOENT)
1477885Sahrens 			error = ESRCH;
1478885Sahrens 		return (error);
1479789Sahrens 	}
1480789Sahrens 
1481789Sahrens 	p = strrchr(zc->zc_name, '/');
1482789Sahrens 	if (p == NULL || p[1] != '\0')
1483789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1484789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1485789Sahrens 
14868697SRichard.Morris@Sun.COM 	/*
14878697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
14888697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
14898697SRichard.Morris@Sun.COM 	 */
14908415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
14918415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
14928415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
14938415SRichard.Morris@Sun.COM 
14948415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
14958697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
14968415SRichard.Morris@Sun.COM 	}
14978415SRichard.Morris@Sun.COM 
1498789Sahrens 	do {
1499885Sahrens 		error = dmu_dir_list_next(os,
1500885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1501885Sahrens 		    NULL, &zc->zc_cookie);
1502789Sahrens 		if (error == ENOENT)
1503789Sahrens 			error = ESRCH;
1504*9396SMatthew.Ahrens@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
15056689Smaybee 	dmu_objset_close(os);
1506789Sahrens 
1507*9396SMatthew.Ahrens@Sun.COM 	if (error == 0)
1508885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1509789Sahrens 
1510789Sahrens 	return (error);
1511789Sahrens }
1512789Sahrens 
15135367Sahrens /*
15145367Sahrens  * inputs:
15155367Sahrens  * zc_name		name of filesystem
15165367Sahrens  * zc_cookie		zap cursor
15175367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15185367Sahrens  *
15195367Sahrens  * outputs:
15205367Sahrens  * zc_name		name of next snapshot
15215367Sahrens  * zc_objset_stats	stats
15225367Sahrens  * zc_nvlist_dst	property nvlist
15235367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15245367Sahrens  */
1525789Sahrens static int
1526789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1527789Sahrens {
1528885Sahrens 	objset_t *os;
1529789Sahrens 	int error;
1530789Sahrens 
15316689Smaybee 	error = dmu_objset_open(zc->zc_name,
15326689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
15336689Smaybee 	if (error)
15346689Smaybee 		return (error == ENOENT ? ESRCH : error);
1535789Sahrens 
1536*9396SMatthew.Ahrens@Sun.COM 	if (zc->zc_cookie == 0) {
15378697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15388415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
1539*9396SMatthew.Ahrens@Sun.COM 	}
15401003Slling 	/*
15411003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15421003Slling 	 * so exit immediately.
15431003Slling 	 */
15441003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1545885Sahrens 		dmu_objset_close(os);
15461003Slling 		return (ESRCH);
1547789Sahrens 	}
1548789Sahrens 
1549885Sahrens 	error = dmu_snapshot_list_next(os,
1550885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15515663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
15526689Smaybee 	dmu_objset_close(os);
1553885Sahrens 	if (error == 0)
1554885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15556689Smaybee 	else if (error == ENOENT)
15566689Smaybee 		error = ESRCH;
1557789Sahrens 
15585367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15596689Smaybee 	if (error)
15605367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1561789Sahrens 	return (error);
1562789Sahrens }
1563789Sahrens 
15646423Sgw25295 int
15654787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1566789Sahrens {
15672676Seschrock 	nvpair_t *elem;
15688724SRichard.Morris@Sun.COM 	int error = 0;
15692676Seschrock 	uint64_t intval;
15702676Seschrock 	char *strval;
15718697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
1572*9396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
15732676Seschrock 
15744543Smarks 	/*
15754543Smarks 	 * First validate permission to set all of the properties
15764543Smarks 	 */
15772676Seschrock 	elem = NULL;
15782676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
15794670Sahrens 		const char *propname = nvpair_name(elem);
15804670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
15812676Seschrock 
15825094Slling 		if (prop == ZPROP_INVAL) {
15832676Seschrock 			/*
15842676Seschrock 			 * If this is a user-defined property, it must be a
15852676Seschrock 			 * string, and there is no further validation to do.
15862676Seschrock 			 */
1587*9396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
1588*9396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
1589*9396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
1590*9396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
1591*9396SMatthew.Ahrens@Sun.COM 					return (error);
1592*9396SMatthew.Ahrens@Sun.COM 				continue;
1593*9396SMatthew.Ahrens@Sun.COM 			}
1594*9396SMatthew.Ahrens@Sun.COM 
1595*9396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
1596*9396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
1597*9396SMatthew.Ahrens@Sun.COM 				const char *perm;
1598*9396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
1599*9396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
1600*9396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
1601*9396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
1602*9396SMatthew.Ahrens@Sun.COM 				else
1603*9396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
1604*9396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
1605*9396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
1606*9396SMatthew.Ahrens@Sun.COM 					return (error);
1607*9396SMatthew.Ahrens@Sun.COM 				continue;
1608*9396SMatthew.Ahrens@Sun.COM 			}
1609*9396SMatthew.Ahrens@Sun.COM 
1610*9396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16112676Seschrock 		}
16122676Seschrock 
1613*9396SMatthew.Ahrens@Sun.COM 		if (issnap)
1614*9396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
1615*9396SMatthew.Ahrens@Sun.COM 
16164787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16174670Sahrens 			return (error);
16182676Seschrock 
16194670Sahrens 		/*
16204670Sahrens 		 * Check that this value is valid for this pool version
16214670Sahrens 		 */
16224670Sahrens 		switch (prop) {
16233886Sahl 		case ZFS_PROP_COMPRESSION:
16243886Sahl 			/*
16253886Sahl 			 * If the user specified gzip compression, make sure
16263886Sahl 			 * the SPA supports it. We ignore any errors here since
16273886Sahl 			 * we'll catch them later.
16283886Sahl 			 */
16293886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16307042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16317042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16327042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16337184Stimh 				    zfs_earlier_version(name,
16345331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16355331Samw 					return (ENOTSUP);
16367042Sgw25295 
16377042Sgw25295 				/*
16387042Sgw25295 				 * If this is a bootable dataset then
16397042Sgw25295 				 * verify that the compression algorithm
16407042Sgw25295 				 * is supported for booting. We must return
16417042Sgw25295 				 * something other than ENOTSUP since it
16427042Sgw25295 				 * implies a downrev pool version.
16437042Sgw25295 				 */
16447042Sgw25295 				if (zfs_is_bootfs(name) &&
16457042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16467042Sgw25295 					return (ERANGE);
16473886Sahl 			}
16483886Sahl 			break;
16494603Sahrens 
16504603Sahrens 		case ZFS_PROP_COPIES:
1651*9396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16525331Samw 				return (ENOTSUP);
16534603Sahrens 			break;
16545977Smarks 
16555977Smarks 		case ZFS_PROP_SHARESMB:
16566689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16575977Smarks 				return (ENOTSUP);
16585977Smarks 			break;
16598053SMark.Shellenbaum@Sun.COM 
16608053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16618053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16628053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16638053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16648053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16658053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
16668053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
16674603Sahrens 		}
16684543Smarks 	}
16694543Smarks 
16708697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16714543Smarks 	elem = NULL;
16724543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16734670Sahrens 		const char *propname = nvpair_name(elem);
16744670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16754543Smarks 
16765094Slling 		if (prop == ZPROP_INVAL) {
1677*9396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
1678*9396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
1679*9396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
1680*9396SMatthew.Ahrens@Sun.COM 				const char *domain;
1681*9396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
1682*9396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
1683*9396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
1684*9396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
1685*9396SMatthew.Ahrens@Sun.COM 
1686*9396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
1687*9396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
1688*9396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
1689*9396SMatthew.Ahrens@Sun.COM 				type = valary[0];
1690*9396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
1691*9396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
1692*9396SMatthew.Ahrens@Sun.COM 				domain = propname +
1693*9396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
1694*9396SMatthew.Ahrens@Sun.COM 
1695*9396SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, B_FALSE, FTAG,
1696*9396SMatthew.Ahrens@Sun.COM 				    &zfsvfs);
1697*9396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
1698*9396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
1699*9396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
1700*9396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
1701*9396SMatthew.Ahrens@Sun.COM 				}
1702*9396SMatthew.Ahrens@Sun.COM 				if (error == 0)
1703*9396SMatthew.Ahrens@Sun.COM 					continue;
1704*9396SMatthew.Ahrens@Sun.COM 				else
1705*9396SMatthew.Ahrens@Sun.COM 					goto out;
1706*9396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
1707*9396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1708*9396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
1709*9396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
1710*9396SMatthew.Ahrens@Sun.COM 				if (error == 0)
1711*9396SMatthew.Ahrens@Sun.COM 					continue;
1712*9396SMatthew.Ahrens@Sun.COM 				else
1713*9396SMatthew.Ahrens@Sun.COM 					goto out;
1714*9396SMatthew.Ahrens@Sun.COM 			}
17154543Smarks 		}
17162676Seschrock 
17172676Seschrock 		switch (prop) {
17182676Seschrock 		case ZFS_PROP_QUOTA:
17192676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17204577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17218697SRichard.Morris@Sun.COM 				goto out;
17222676Seschrock 			break;
17232676Seschrock 
17245378Sck153898 		case ZFS_PROP_REFQUOTA:
17255378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17265378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17278697SRichard.Morris@Sun.COM 				goto out;
17285378Sck153898 			break;
17295378Sck153898 
17302676Seschrock 		case ZFS_PROP_RESERVATION:
17312676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17322676Seschrock 			    (error = dsl_dir_set_reservation(name,
17332676Seschrock 			    intval)) != 0)
17348697SRichard.Morris@Sun.COM 				goto out;
17352676Seschrock 			break;
1736789Sahrens 
17375378Sck153898 		case ZFS_PROP_REFRESERVATION:
17385378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17395378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17405378Sck153898 			    intval)) != 0)
17418697SRichard.Morris@Sun.COM 				goto out;
17425378Sck153898 			break;
17435378Sck153898 
17442676Seschrock 		case ZFS_PROP_VOLSIZE:
17452676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17464787Sahrens 			    (error = zvol_set_volsize(name,
17474787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17488697SRichard.Morris@Sun.COM 				goto out;
17492676Seschrock 			break;
17502676Seschrock 
17512676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
17522676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17534577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
17548697SRichard.Morris@Sun.COM 				goto out;
17554577Sahrens 			break;
17564577Sahrens 
17574577Sahrens 		case ZFS_PROP_VERSION:
1758*9396SMatthew.Ahrens@Sun.COM 		{
1759*9396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
1760*9396SMatthew.Ahrens@Sun.COM 
1761*9396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
1762*9396SMatthew.Ahrens@Sun.COM 				goto out;
1763*9396SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, B_FALSE, FTAG,
1764*9396SMatthew.Ahrens@Sun.COM 			    &zfsvfs)) != 0)
1765*9396SMatthew.Ahrens@Sun.COM 				goto out;
1766*9396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
1767*9396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
1768*9396SMatthew.Ahrens@Sun.COM 
1769*9396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
1770*9396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
1771*9396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
1772*9396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
1773*9396SMatthew.Ahrens@Sun.COM 			}
1774*9396SMatthew.Ahrens@Sun.COM 			if (error)
17758697SRichard.Morris@Sun.COM 				goto out;
17762676Seschrock 			break;
1777*9396SMatthew.Ahrens@Sun.COM 		}
17782676Seschrock 
17792676Seschrock 		default:
17802676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
17812676Seschrock 				if (zfs_prop_get_type(prop) !=
17828697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
17838697SRichard.Morris@Sun.COM 					error = EINVAL;
17848697SRichard.Morris@Sun.COM 					goto out;
17858697SRichard.Morris@Sun.COM 				}
17862676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
17872885Sahrens 				const char *unused;
17882885Sahrens 
17892717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
17902676Seschrock 
17912676Seschrock 				switch (zfs_prop_get_type(prop)) {
17924787Sahrens 				case PROP_TYPE_NUMBER:
17932676Seschrock 					break;
17944787Sahrens 				case PROP_TYPE_STRING:
17958697SRichard.Morris@Sun.COM 					error = EINVAL;
17968697SRichard.Morris@Sun.COM 					goto out;
17974787Sahrens 				case PROP_TYPE_INDEX:
17982717Seschrock 					if (zfs_prop_index_to_string(prop,
17998697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18008697SRichard.Morris@Sun.COM 						error = EINVAL;
18018697SRichard.Morris@Sun.COM 						goto out;
18028697SRichard.Morris@Sun.COM 					}
18032676Seschrock 					break;
18042676Seschrock 				default:
18054577Sahrens 					cmn_err(CE_PANIC,
18064577Sahrens 					    "unknown property type");
18072676Seschrock 					break;
18082676Seschrock 				}
18092676Seschrock 			} else {
18108697SRichard.Morris@Sun.COM 				error = EINVAL;
18118697SRichard.Morris@Sun.COM 				goto out;
18122676Seschrock 			}
18138697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18148697SRichard.Morris@Sun.COM 				goto out;
18152676Seschrock 		}
18162676Seschrock 	}
18172676Seschrock 
18188697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18198697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18208697SRichard.Morris@Sun.COM 	}
18218697SRichard.Morris@Sun.COM out:
18228697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18238697SRichard.Morris@Sun.COM 	return (error);
1824789Sahrens }
1825789Sahrens 
18265367Sahrens /*
18279355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18289355SMatthew.Ahrens@Sun.COM  */
18299355SMatthew.Ahrens@Sun.COM static int
18309355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18319355SMatthew.Ahrens@Sun.COM {
18329355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18339355SMatthew.Ahrens@Sun.COM 	int error = 0;
18349355SMatthew.Ahrens@Sun.COM 
18359355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18369355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18379355SMatthew.Ahrens@Sun.COM 		char *valstr;
18389355SMatthew.Ahrens@Sun.COM 
18399355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18409355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18419355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18429355SMatthew.Ahrens@Sun.COM 
18439355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18449355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18459355SMatthew.Ahrens@Sun.COM 			return (error);
18469355SMatthew.Ahrens@Sun.COM 
18479355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18489355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18499355SMatthew.Ahrens@Sun.COM 
18509355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18519355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18529355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18539355SMatthew.Ahrens@Sun.COM 	}
18549355SMatthew.Ahrens@Sun.COM 	return (0);
18559355SMatthew.Ahrens@Sun.COM }
18569355SMatthew.Ahrens@Sun.COM 
18579355SMatthew.Ahrens@Sun.COM /*
18585367Sahrens  * inputs:
18595367Sahrens  * zc_name		name of filesystem
18608697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18615367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18627265Sahrens  * zc_cookie		clear existing local props?
18635367Sahrens  *
18645367Sahrens  * outputs:		none
18655367Sahrens  */
1866789Sahrens static int
18672676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1868789Sahrens {
18692676Seschrock 	nvlist_t *nvl;
18702676Seschrock 	int error;
1871789Sahrens 
18725094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18735094Slling 	    &nvl)) != 0)
18742676Seschrock 		return (error);
18752676Seschrock 
18767265Sahrens 	if (zc->zc_cookie) {
18777265Sahrens 		nvlist_t *origprops;
18787265Sahrens 		objset_t *os;
18797265Sahrens 
18807265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
18817265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
18827265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
18838536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
18847265Sahrens 				nvlist_free(origprops);
18857265Sahrens 			}
18867265Sahrens 			dmu_objset_close(os);
18877265Sahrens 		}
18887265Sahrens 
18897265Sahrens 	}
18907265Sahrens 
18914787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
18924543Smarks 
18932676Seschrock 	nvlist_free(nvl);
18942676Seschrock 	return (error);
1895789Sahrens }
1896789Sahrens 
18975367Sahrens /*
18985367Sahrens  * inputs:
18995367Sahrens  * zc_name		name of filesystem
19005367Sahrens  * zc_value		name of property to inherit
19015367Sahrens  *
19025367Sahrens  * outputs:		none
19035367Sahrens  */
1904789Sahrens static int
19054849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19064849Sahrens {
19074849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19084849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19094849Sahrens }
19104849Sahrens 
19114849Sahrens static int
19124098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19133912Slling {
19145094Slling 	nvlist_t *props;
19153912Slling 	spa_t *spa;
19165094Slling 	int error;
19178525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19183912Slling 
19195094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19205094Slling 	    &props)))
19213912Slling 		return (error);
19223912Slling 
19238525SEric.Schrock@Sun.COM 	/*
19248525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19258525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19268525SEric.Schrock@Sun.COM 	 */
19278525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19288525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19298525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19308525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19318525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19328525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19338525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19348525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19358525SEric.Schrock@Sun.COM 		}
19368525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19378525SEric.Schrock@Sun.COM 		if (spa != NULL)
19388525SEric.Schrock@Sun.COM 			return (0);
19398525SEric.Schrock@Sun.COM 	}
19408525SEric.Schrock@Sun.COM 
19413912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19425094Slling 		nvlist_free(props);
19433912Slling 		return (error);
19443912Slling 	}
19453912Slling 
19465094Slling 	error = spa_prop_set(spa, props);
19473912Slling 
19485094Slling 	nvlist_free(props);
19493912Slling 	spa_close(spa, FTAG);
19503912Slling 
19513912Slling 	return (error);
19523912Slling }
19533912Slling 
19543912Slling static int
19554098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19563912Slling {
19573912Slling 	spa_t *spa;
19583912Slling 	int error;
19593912Slling 	nvlist_t *nvp = NULL;
19603912Slling 
19618525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19628525SEric.Schrock@Sun.COM 		/*
19638525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19648525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19658525SEric.Schrock@Sun.COM 		 * anyway.
19668525SEric.Schrock@Sun.COM 		 */
19678525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19688525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
19698525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
19708525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19718525SEric.Schrock@Sun.COM 	} else {
19728525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
19738525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
19748525SEric.Schrock@Sun.COM 	}
19753912Slling 
19763912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
19773912Slling 		error = put_nvlist(zc, nvp);
19783912Slling 	else
19793912Slling 		error = EFAULT;
19803912Slling 
19818525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
19823912Slling 	return (error);
19833912Slling }
19843912Slling 
19853912Slling static int
19864543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
19874543Smarks {
19884543Smarks 	nvlist_t *nvp;
19894543Smarks 	int error;
19904543Smarks 	uint32_t uid;
19914543Smarks 	uint32_t gid;
19924543Smarks 	uint32_t *groups;
19934543Smarks 	uint_t group_cnt;
19944543Smarks 	cred_t	*usercred;
19954543Smarks 
19965094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19975094Slling 	    &nvp)) != 0) {
19984543Smarks 		return (error);
19994543Smarks 	}
20004543Smarks 
20014543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20024543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20034543Smarks 		nvlist_free(nvp);
20044543Smarks 		return (EPERM);
20054543Smarks 	}
20064543Smarks 
20074543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20084543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20094543Smarks 		nvlist_free(nvp);
20104543Smarks 		return (EPERM);
20114543Smarks 	}
20124543Smarks 
20134543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20144543Smarks 	    &groups, &group_cnt)) != 0) {
20154543Smarks 		nvlist_free(nvp);
20164543Smarks 		return (EPERM);
20174543Smarks 	}
20184543Smarks 	usercred = cralloc();
20194543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20204543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20214543Smarks 		nvlist_free(nvp);
20224543Smarks 		crfree(usercred);
20234543Smarks 		return (EPERM);
20244543Smarks 	}
20254543Smarks 	nvlist_free(nvp);
20264543Smarks 	error = dsl_deleg_access(zc->zc_name,
20274787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20284543Smarks 	crfree(usercred);
20294543Smarks 	return (error);
20304543Smarks }
20314543Smarks 
20325367Sahrens /*
20335367Sahrens  * inputs:
20345367Sahrens  * zc_name		name of filesystem
20355367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20365367Sahrens  * zc_perm_action	allow/unallow flag
20375367Sahrens  *
20385367Sahrens  * outputs:		none
20395367Sahrens  */
20404543Smarks static int
20414543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20424543Smarks {
20434543Smarks 	int error;
20444543Smarks 	nvlist_t *fsaclnv = NULL;
20454543Smarks 
20465094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20475094Slling 	    &fsaclnv)) != 0)
20484543Smarks 		return (error);
20494543Smarks 
20504543Smarks 	/*
20514543Smarks 	 * Verify nvlist is constructed correctly
20524543Smarks 	 */
20534543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20544543Smarks 		nvlist_free(fsaclnv);
20554543Smarks 		return (EINVAL);
20564543Smarks 	}
20574543Smarks 
20584543Smarks 	/*
20594543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20604543Smarks 	 * that user is allowed to hand out each permission in
20614543Smarks 	 * the nvlist(s)
20624543Smarks 	 */
20634543Smarks 
20644787Sahrens 	error = secpolicy_zfs(CRED());
20654543Smarks 	if (error) {
20664787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
20674787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
20684787Sahrens 			    fsaclnv, CRED());
20694787Sahrens 		} else {
20704787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
20714787Sahrens 			    fsaclnv, CRED());
20724787Sahrens 		}
20734543Smarks 	}
20744543Smarks 
20754543Smarks 	if (error == 0)
20764543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
20774543Smarks 
20784543Smarks 	nvlist_free(fsaclnv);
20794543Smarks 	return (error);
20804543Smarks }
20814543Smarks 
20825367Sahrens /*
20835367Sahrens  * inputs:
20845367Sahrens  * zc_name		name of filesystem
20855367Sahrens  *
20865367Sahrens  * outputs:
20875367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20885367Sahrens  */
20894543Smarks static int
20904543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
20914543Smarks {
20924543Smarks 	nvlist_t *nvp;
20934543Smarks 	int error;
20944543Smarks 
20954543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
20964543Smarks 		error = put_nvlist(zc, nvp);
20974543Smarks 		nvlist_free(nvp);
20984543Smarks 	}
20994543Smarks 
21004543Smarks 	return (error);
21014543Smarks }
21024543Smarks 
21035367Sahrens /*
21045367Sahrens  * inputs:
21055367Sahrens  * zc_name		name of volume
21065367Sahrens  *
21075367Sahrens  * outputs:		none
21085367Sahrens  */
21094543Smarks static int
2110789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
2111789Sahrens {
21124787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2113789Sahrens }
2114789Sahrens 
21155367Sahrens /*
21165367Sahrens  * inputs:
21175367Sahrens  * zc_name		name of volume
21185367Sahrens  *
21195367Sahrens  * outputs:		none
21205367Sahrens  */
2121789Sahrens static int
2122789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
2123789Sahrens {
21242676Seschrock 	return (zvol_remove_minor(zc->zc_name));
2125789Sahrens }
2126789Sahrens 
2127789Sahrens /*
2128789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2129789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2130789Sahrens  * is responsible for releasing the returned vfs pointer.
2131789Sahrens  */
2132789Sahrens static vfs_t *
2133789Sahrens zfs_get_vfs(const char *resource)
2134789Sahrens {
2135789Sahrens 	struct vfs *vfsp;
2136789Sahrens 	struct vfs *vfs_found = NULL;
2137789Sahrens 
2138789Sahrens 	vfs_list_read_lock();
2139789Sahrens 	vfsp = rootvfs;
2140789Sahrens 	do {
2141789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2142789Sahrens 			VFS_HOLD(vfsp);
2143789Sahrens 			vfs_found = vfsp;
2144789Sahrens 			break;
2145789Sahrens 		}
2146789Sahrens 		vfsp = vfsp->vfs_next;
2147789Sahrens 	} while (vfsp != rootvfs);
2148789Sahrens 	vfs_list_unlock();
2149789Sahrens 	return (vfs_found);
2150789Sahrens }
2151789Sahrens 
21524543Smarks /* ARGSUSED */
2153789Sahrens static void
21544543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2155789Sahrens {
21565331Samw 	zfs_creat_t *zct = arg;
21575498Stimh 
21585498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21595331Samw }
21605331Samw 
21615498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21625498Stimh 
21635331Samw /*
21645498Stimh  * inputs:
21657184Stimh  * createprops		list of properties requested by creator
21667184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21677184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21687184Stimh  * os			parent objset pointer (NULL if root fs)
21695331Samw  *
21705498Stimh  * outputs:
21715498Stimh  * zplprops	values for the zplprops we attach to the master node object
21727184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21735331Samw  *
21745498Stimh  * Determine the settings for utf8only, normalization and
21755498Stimh  * casesensitivity.  Specific values may have been requested by the
21765498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21775498Stimh  * the file system is of too early a vintage, a creator can not
21785498Stimh  * request settings for these properties, even if the requested
21795498Stimh  * setting is the default value.  We don't actually want to create dsl
21805498Stimh  * properties for these, so remove them from the source nvlist after
21815498Stimh  * processing.
21825331Samw  */
21835331Samw static int
2184*9396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
21857184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
21867184Stimh     boolean_t *is_ci)
21875331Samw {
21885498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
21895498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
21905498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
21915498Stimh 
21925498Stimh 	ASSERT(zplprops != NULL);
21935498Stimh 
21945375Stimh 	/*
21955498Stimh 	 * Pull out creator prop choices, if any.
21965375Stimh 	 */
21975498Stimh 	if (createprops) {
21985498Stimh 		(void) nvlist_lookup_uint64(createprops,
21997184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22007184Stimh 		(void) nvlist_lookup_uint64(createprops,
22015498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22025498Stimh 		(void) nvlist_remove_all(createprops,
22035498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22045498Stimh 		(void) nvlist_lookup_uint64(createprops,
22055498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22065498Stimh 		(void) nvlist_remove_all(createprops,
22075498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22085498Stimh 		(void) nvlist_lookup_uint64(createprops,
22095498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22105498Stimh 		(void) nvlist_remove_all(createprops,
22115498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22125331Samw 	}
22135331Samw 
22145375Stimh 	/*
22157184Stimh 	 * If the zpl version requested is whacky or the file system
22167184Stimh 	 * or pool is version is too "young" to support normalization
22177184Stimh 	 * and the creator tried to set a value for one of the props,
22187184Stimh 	 * error out.
22195498Stimh 	 */
22207184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22217184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22227184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22235498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22247184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22255498Stimh 		return (ENOTSUP);
22265498Stimh 
22275498Stimh 	/*
22285498Stimh 	 * Put the version in the zplprops
22295498Stimh 	 */
22305498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22315498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22325498Stimh 
22335498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22345498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22355498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22365498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22375498Stimh 
22385498Stimh 	/*
22395498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22405498Stimh 	 */
22415498Stimh 	if (norm)
22425498Stimh 		u8 = 1;
22435498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22445498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22455498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22465498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22475498Stimh 
22485498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22495498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22505498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22515498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22525498Stimh 
22536492Stimh 	if (is_ci)
22546492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22556492Stimh 
22567184Stimh 	return (0);
22577184Stimh }
22587184Stimh 
22597184Stimh static int
22607184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22617184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22627184Stimh {
22637184Stimh 	boolean_t fuids_ok = B_TRUE;
22647184Stimh 	uint64_t zplver = ZPL_VERSION;
22657184Stimh 	objset_t *os = NULL;
22667184Stimh 	char parentname[MAXNAMELEN];
22677184Stimh 	char *cp;
22687184Stimh 	int error;
22697184Stimh 
22707184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22717184Stimh 	cp = strrchr(parentname, '/');
22727184Stimh 	ASSERT(cp != NULL);
22737184Stimh 	cp[0] = '\0';
22747184Stimh 
2275*9396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
2276*9396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22777184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22787184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22797184Stimh 		fuids_ok = B_FALSE;
22807184Stimh 	}
22817184Stimh 
22827184Stimh 	/*
22837184Stimh 	 * Open parent object set so we can inherit zplprop values.
22847184Stimh 	 */
22857184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
22867184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
22877184Stimh 		return (error);
22887184Stimh 
22897184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
22907184Stimh 	    zplprops, is_ci);
22915498Stimh 	dmu_objset_close(os);
22927184Stimh 	return (error);
22937184Stimh }
22947184Stimh 
22957184Stimh static int
22967184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
22977184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22987184Stimh {
22997184Stimh 	boolean_t fuids_ok = B_TRUE;
23007184Stimh 	uint64_t zplver = ZPL_VERSION;
23017184Stimh 	int error;
23027184Stimh 
23037184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23047184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23057184Stimh 		fuids_ok = B_FALSE;
23067184Stimh 	}
23077184Stimh 
23087184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23097184Stimh 	    zplprops, is_ci);
23107184Stimh 	return (error);
2311789Sahrens }
2312789Sahrens 
23135367Sahrens /*
23145367Sahrens  * inputs:
23155367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23165367Sahrens  * zc_name		name of new objset
23175367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23185367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23195367Sahrens  *
23205498Stimh  * outputs: none
23215367Sahrens  */
2322789Sahrens static int
2323789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2324789Sahrens {
2325789Sahrens 	objset_t *clone;
2326789Sahrens 	int error = 0;
23275331Samw 	zfs_creat_t zct;
23284543Smarks 	nvlist_t *nvprops = NULL;
23294543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2330789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2331789Sahrens 
2332789Sahrens 	switch (type) {
2333789Sahrens 
2334789Sahrens 	case DMU_OST_ZFS:
2335789Sahrens 		cbfunc = zfs_create_cb;
2336789Sahrens 		break;
2337789Sahrens 
2338789Sahrens 	case DMU_OST_ZVOL:
2339789Sahrens 		cbfunc = zvol_create_cb;
2340789Sahrens 		break;
2341789Sahrens 
2342789Sahrens 	default:
23432199Sahrens 		cbfunc = NULL;
23446423Sgw25295 		break;
23452199Sahrens 	}
23465326Sek110237 	if (strchr(zc->zc_name, '@') ||
23475326Sek110237 	    strchr(zc->zc_name, '%'))
2348789Sahrens 		return (EINVAL);
2349789Sahrens 
23502676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23515094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23525094Slling 	    &nvprops)) != 0)
23532676Seschrock 		return (error);
23542676Seschrock 
23555498Stimh 	zct.zct_zplprops = NULL;
23565331Samw 	zct.zct_props = nvprops;
23575331Samw 
23582676Seschrock 	if (zc->zc_value[0] != '\0') {
2359789Sahrens 		/*
2360789Sahrens 		 * We're creating a clone of an existing snapshot.
2361789Sahrens 		 */
23622676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23632676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23644543Smarks 			nvlist_free(nvprops);
2365789Sahrens 			return (EINVAL);
23662676Seschrock 		}
2367789Sahrens 
23682676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
23696689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
23702676Seschrock 		if (error) {
23714543Smarks 			nvlist_free(nvprops);
2372789Sahrens 			return (error);
23732676Seschrock 		}
23746492Stimh 
23756492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
23766492Stimh 		    NULL, NULL);
23775331Samw 		if (error) {
23785331Samw 			dmu_objset_close(clone);
23795331Samw 			nvlist_free(nvprops);
23805331Samw 			return (error);
23815331Samw 		}
2382789Sahrens 		dmu_objset_close(clone);
2383789Sahrens 	} else {
23846492Stimh 		boolean_t is_insensitive = B_FALSE;
23856492Stimh 
23862676Seschrock 		if (cbfunc == NULL) {
23874543Smarks 			nvlist_free(nvprops);
23882199Sahrens 			return (EINVAL);
23892676Seschrock 		}
23902676Seschrock 
2391789Sahrens 		if (type == DMU_OST_ZVOL) {
23922676Seschrock 			uint64_t volsize, volblocksize;
23932676Seschrock 
23944543Smarks 			if (nvprops == NULL ||
23954543Smarks 			    nvlist_lookup_uint64(nvprops,
23962676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
23972676Seschrock 			    &volsize) != 0) {
23984543Smarks 				nvlist_free(nvprops);
23992676Seschrock 				return (EINVAL);
24002676Seschrock 			}
24012676Seschrock 
24024543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24032676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24042676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24054543Smarks 				nvlist_free(nvprops);
24062676Seschrock 				return (EINVAL);
24072676Seschrock 			}
24081133Seschrock 
24092676Seschrock 			if (error != 0)
24102676Seschrock 				volblocksize = zfs_prop_default_numeric(
24112676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24122676Seschrock 
24132676Seschrock 			if ((error = zvol_check_volblocksize(
24142676Seschrock 			    volblocksize)) != 0 ||
24152676Seschrock 			    (error = zvol_check_volsize(volsize,
24162676Seschrock 			    volblocksize)) != 0) {
24174543Smarks 				nvlist_free(nvprops);
2418789Sahrens 				return (error);
24192676Seschrock 			}
24204577Sahrens 		} else if (type == DMU_OST_ZFS) {
24215331Samw 			int error;
24225331Samw 
24235498Stimh 			/*
24245331Samw 			 * We have to have normalization and
24255331Samw 			 * case-folding flags correct when we do the
24265331Samw 			 * file system creation, so go figure them out
24275498Stimh 			 * now.
24285331Samw 			 */
24295498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24305498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24315498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24327184Stimh 			    zct.zct_zplprops, &is_insensitive);
24335331Samw 			if (error != 0) {
24345331Samw 				nvlist_free(nvprops);
24355498Stimh 				nvlist_free(zct.zct_zplprops);
24365331Samw 				return (error);
24374577Sahrens 			}
24382676Seschrock 		}
24396492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
24406492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24415498Stimh 		nvlist_free(zct.zct_zplprops);
2442789Sahrens 	}
24432676Seschrock 
24442676Seschrock 	/*
24452676Seschrock 	 * It would be nice to do this atomically.
24462676Seschrock 	 */
24472676Seschrock 	if (error == 0) {
24484787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
24492676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
24502676Seschrock 	}
24514543Smarks 	nvlist_free(nvprops);
2452789Sahrens 	return (error);
2453789Sahrens }
2454789Sahrens 
24555367Sahrens /*
24565367Sahrens  * inputs:
24575367Sahrens  * zc_name	name of filesystem
24585367Sahrens  * zc_value	short name of snapshot
24595367Sahrens  * zc_cookie	recursive flag
2460*9396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24615367Sahrens  *
24625367Sahrens  * outputs:	none
24635367Sahrens  */
2464789Sahrens static int
24652199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24662199Sahrens {
24677265Sahrens 	nvlist_t *nvprops = NULL;
24687265Sahrens 	int error;
24697265Sahrens 	boolean_t recursive = zc->zc_cookie;
24707265Sahrens 
24712676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24722199Sahrens 		return (EINVAL);
24737265Sahrens 
24747265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24757265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24767265Sahrens 	    &nvprops)) != 0)
24777265Sahrens 		return (error);
24787265Sahrens 
24799355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
24809355SMatthew.Ahrens@Sun.COM 	if (error)
24819355SMatthew.Ahrens@Sun.COM 		goto out;
24829355SMatthew.Ahrens@Sun.COM 
24839355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
24849355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
24859355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
24869355SMatthew.Ahrens@Sun.COM 		goto out;
24877265Sahrens 	}
24889355SMatthew.Ahrens@Sun.COM 
24899355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
24909355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
24919355SMatthew.Ahrens@Sun.COM 
24929355SMatthew.Ahrens@Sun.COM out:
24937265Sahrens 	nvlist_free(nvprops);
24947265Sahrens 	return (error);
24952199Sahrens }
24962199Sahrens 
24974007Smmusante int
24982199Sahrens zfs_unmount_snap(char *name, void *arg)
2499789Sahrens {
25002417Sahrens 	vfs_t *vfsp = NULL;
25012199Sahrens 
25026689Smaybee 	if (arg) {
25036689Smaybee 		char *snapname = arg;
25046689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25056689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25066689Smaybee 
25076689Smaybee 		(void) strcpy(buf, name);
25086689Smaybee 		(void) strcat(buf, "@");
25096689Smaybee 		(void) strcat(buf, snapname);
25106689Smaybee 		vfsp = zfs_get_vfs(buf);
25116689Smaybee 		kmem_free(buf, len);
25122417Sahrens 	} else if (strchr(name, '@')) {
25132199Sahrens 		vfsp = zfs_get_vfs(name);
25142199Sahrens 	}
25152199Sahrens 
25162199Sahrens 	if (vfsp) {
25172199Sahrens 		/*
25182199Sahrens 		 * Always force the unmount for snapshots.
25192199Sahrens 		 */
25202199Sahrens 		int flag = MS_FORCE;
2521789Sahrens 		int err;
2522789Sahrens 
25232199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25242199Sahrens 			VFS_RELE(vfsp);
25252199Sahrens 			return (err);
25262199Sahrens 		}
25272199Sahrens 		VFS_RELE(vfsp);
25282199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25292199Sahrens 			return (err);
25302199Sahrens 	}
25312199Sahrens 	return (0);
25322199Sahrens }
25332199Sahrens 
25345367Sahrens /*
25355367Sahrens  * inputs:
25365367Sahrens  * zc_name	name of filesystem
25375367Sahrens  * zc_value	short name of snapshot
25385367Sahrens  *
25395367Sahrens  * outputs:	none
25405367Sahrens  */
25412199Sahrens static int
25422199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25432199Sahrens {
25442199Sahrens 	int err;
2545789Sahrens 
25462676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25472199Sahrens 		return (EINVAL);
25482199Sahrens 	err = dmu_objset_find(zc->zc_name,
25492676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25502199Sahrens 	if (err)
25512199Sahrens 		return (err);
25522676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
25532199Sahrens }
25542199Sahrens 
25555367Sahrens /*
25565367Sahrens  * inputs:
25575367Sahrens  * zc_name		name of dataset to destroy
25585367Sahrens  * zc_objset_type	type of objset
25595367Sahrens  *
25605367Sahrens  * outputs:		none
25615367Sahrens  */
25622199Sahrens static int
25632199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25642199Sahrens {
25652199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
25662199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
25672199Sahrens 		if (err)
25682199Sahrens 			return (err);
2569789Sahrens 	}
2570789Sahrens 
2571789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2572789Sahrens }
2573789Sahrens 
25745367Sahrens /*
25755367Sahrens  * inputs:
25765446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25775367Sahrens  *
25785367Sahrens  * outputs:	none
25795367Sahrens  */
2580789Sahrens static int
2581789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2582789Sahrens {
25835446Sahrens 	objset_t *os;
25845446Sahrens 	int error;
25855446Sahrens 	zfsvfs_t *zfsvfs = NULL;
25865446Sahrens 
25875446Sahrens 	/*
25885446Sahrens 	 * Get the zfsvfs for the receiving objset. There
25895446Sahrens 	 * won't be one if we're operating on a zvol, if the
25905446Sahrens 	 * objset doesn't exist yet, or is not mounted.
25915446Sahrens 	 */
25926689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
25935446Sahrens 	if (error)
25945446Sahrens 		return (error);
25955446Sahrens 
2596*9396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
25975446Sahrens 		int mode;
25985446Sahrens 
2599*9396SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs, NULL, &mode);
26006083Sek110237 		if (error == 0) {
26016083Sek110237 			int resume_err;
26026083Sek110237 
26036083Sek110237 			error = dmu_objset_rollback(os);
2604*9396SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
26056083Sek110237 			error = error ? error : resume_err;
26066083Sek110237 		} else {
26076083Sek110237 			dmu_objset_close(os);
26086083Sek110237 		}
26095446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26105446Sahrens 	} else {
26115446Sahrens 		error = dmu_objset_rollback(os);
26125446Sahrens 	}
26136689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
26145446Sahrens 
26155446Sahrens 	return (error);
2616789Sahrens }
2617789Sahrens 
26185367Sahrens /*
26195367Sahrens  * inputs:
26205367Sahrens  * zc_name	old name of dataset
26215367Sahrens  * zc_value	new name of dataset
26225367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26235367Sahrens  *
26245367Sahrens  * outputs:	none
26255367Sahrens  */
2626789Sahrens static int
2627789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2628789Sahrens {
26294490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26304007Smmusante 
26312676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26325326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26335326Sek110237 	    strchr(zc->zc_value, '%'))
2634789Sahrens 		return (EINVAL);
2635789Sahrens 
26364007Smmusante 	/*
26374007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26384007Smmusante 	 * in which case the dataset code figures out which snapshots
26394007Smmusante 	 * to unmount.
26404007Smmusante 	 */
26414007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2642789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26432199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26442199Sahrens 		if (err)
26452199Sahrens 			return (err);
2646789Sahrens 	}
26474007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2648789Sahrens }
2649789Sahrens 
26506689Smaybee static void
26518536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26526689Smaybee {
26536689Smaybee 	zfs_cmd_t *zc;
26546689Smaybee 	nvpair_t *prop;
26556689Smaybee 
26566689Smaybee 	if (props == NULL)
26576689Smaybee 		return;
26586689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
26596689Smaybee 	(void) strcpy(zc->zc_name, dataset);
26606689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
26616689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
26628536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
26638536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
26648536SDavid.Pacheco@Sun.COM 			continue;
26656689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
26666689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
26676689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
26686689Smaybee 	}
26696689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
26706689Smaybee }
26716689Smaybee 
26725367Sahrens /*
26735367Sahrens  * inputs:
26745367Sahrens  * zc_name		name of containing filesystem
26755367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
26765367Sahrens  * zc_value		name of snapshot to create
26775367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
26785367Sahrens  * zc_cookie		file descriptor to recv from
26795367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
26805367Sahrens  * zc_guid		force flag
26815367Sahrens  *
26825367Sahrens  * outputs:
26835367Sahrens  * zc_cookie		number of bytes read
26845367Sahrens  */
2685789Sahrens static int
26865367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2687789Sahrens {
2688789Sahrens 	file_t *fp;
26895326Sek110237 	objset_t *os;
26905367Sahrens 	dmu_recv_cookie_t drc;
26915326Sek110237 	zfsvfs_t *zfsvfs = NULL;
26925326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2693789Sahrens 	int error, fd;
26945367Sahrens 	offset_t off;
26955367Sahrens 	nvlist_t *props = NULL;
26966689Smaybee 	nvlist_t *origprops = NULL;
26975367Sahrens 	objset_t *origin = NULL;
26985367Sahrens 	char *tosnap;
26995367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2700789Sahrens 
27013265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27025326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27035326Sek110237 	    strchr(zc->zc_value, '%'))
27043265Sahrens 		return (EINVAL);
27053265Sahrens 
27065367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27075367Sahrens 	tosnap = strchr(tofs, '@');
27085367Sahrens 	*tosnap = '\0';
27095367Sahrens 	tosnap++;
27105367Sahrens 
27115367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27125367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27135367Sahrens 	    &props)) != 0)
27145367Sahrens 		return (error);
27155367Sahrens 
2716789Sahrens 	fd = zc->zc_cookie;
2717789Sahrens 	fp = getf(fd);
27185367Sahrens 	if (fp == NULL) {
27195367Sahrens 		nvlist_free(props);
2720789Sahrens 		return (EBADF);
27215367Sahrens 	}
27225326Sek110237 
2723*9396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(tofs, &zfsvfs) == 0) {
2724*9396SMatthew.Ahrens@Sun.COM 		if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2725*9396SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
2726*9396SMatthew.Ahrens@Sun.COM 			zfsvfs = NULL;
2727*9396SMatthew.Ahrens@Sun.COM 			error = EBUSY;
2728*9396SMatthew.Ahrens@Sun.COM 			goto out;
27296083Sek110237 		}
27306689Smaybee 		/*
27316689Smaybee 		 * If new properties are supplied, they are to completely
27326689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27336689Smaybee 		 */
27346689Smaybee 		if (props)
2735*9396SMatthew.Ahrens@Sun.COM 			(void) dsl_prop_get_all(zfsvfs->z_os, &origprops, TRUE);
2736*9396SMatthew.Ahrens@Sun.COM 	} else if (props && dmu_objset_open(tofs, DMU_OST_ANY,
2737*9396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2738*9396SMatthew.Ahrens@Sun.COM 		/*
2739*9396SMatthew.Ahrens@Sun.COM 		 * Get the props even if there was no zfsvfs (zvol or
2740*9396SMatthew.Ahrens@Sun.COM 		 * unmounted zpl).
2741*9396SMatthew.Ahrens@Sun.COM 		 */
2742*9396SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, TRUE);
27436689Smaybee 
27445326Sek110237 		dmu_objset_close(os);
27455326Sek110237 	}
27465326Sek110237 
27475367Sahrens 	if (zc->zc_string[0]) {
27485367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
27496689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
27506689Smaybee 		if (error)
27516689Smaybee 			goto out;
27525367Sahrens 	}
27535367Sahrens 
27545367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
27555367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
27565367Sahrens 	if (origin)
27575367Sahrens 		dmu_objset_close(origin);
27586689Smaybee 	if (error)
27596689Smaybee 		goto out;
27605326Sek110237 
27615326Sek110237 	/*
27626689Smaybee 	 * Reset properties.  We do this before we receive the stream
27636689Smaybee 	 * so that the properties are applied to the new data.
27645326Sek110237 	 */
27655367Sahrens 	if (props) {
27668536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27676689Smaybee 		/*
27686689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27696689Smaybee 		 */
27706689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27715367Sahrens 	}
27725367Sahrens 
27735367Sahrens 	off = fp->f_offset;
27745367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
27755367Sahrens 
27766689Smaybee 	if (error == 0 && zfsvfs) {
27778012SEric.Taylor@Sun.COM 		char *osname;
27786689Smaybee 		int mode;
27796689Smaybee 
27806689Smaybee 		/* online recv */
27818012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
27826689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
27836689Smaybee 		if (error == 0) {
27846689Smaybee 			int resume_err;
27856689Smaybee 
27866689Smaybee 			error = dmu_recv_end(&drc);
27876689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
27886689Smaybee 			error = error ? error : resume_err;
27895367Sahrens 		} else {
27906689Smaybee 			dmu_recv_abort_cleanup(&drc);
27915367Sahrens 		}
27928012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
27936689Smaybee 	} else if (error == 0) {
27946689Smaybee 		error = dmu_recv_end(&drc);
27956083Sek110237 	}
27965367Sahrens 
27975367Sahrens 	zc->zc_cookie = off - fp->f_offset;
27985367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
27995367Sahrens 		fp->f_offset = off;
28002885Sahrens 
28016689Smaybee 	/*
28026689Smaybee 	 * On error, restore the original props.
28036689Smaybee 	 */
28046689Smaybee 	if (error && props) {
28058536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28066689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28076689Smaybee 	}
28086689Smaybee out:
28096689Smaybee 	if (zfsvfs) {
28106689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
28116689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
28126689Smaybee 	}
28136689Smaybee 	nvlist_free(props);
28146689Smaybee 	nvlist_free(origprops);
2815789Sahrens 	releasef(fd);
2816789Sahrens 	return (error);
2817789Sahrens }
2818789Sahrens 
28195367Sahrens /*
28205367Sahrens  * inputs:
28215367Sahrens  * zc_name	name of snapshot to send
28225367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28235367Sahrens  * zc_cookie	file descriptor to send stream to
28245367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28255367Sahrens  *
28265367Sahrens  * outputs: none
28275367Sahrens  */
2828789Sahrens static int
28295367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2830789Sahrens {
2831789Sahrens 	objset_t *fromsnap = NULL;
2832789Sahrens 	objset_t *tosnap;
2833789Sahrens 	file_t *fp;
2834789Sahrens 	int error;
28355367Sahrens 	offset_t off;
2836789Sahrens 
2837789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
28386689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2839789Sahrens 	if (error)
2840789Sahrens 		return (error);
2841789Sahrens 
28422676Seschrock 	if (zc->zc_value[0] != '\0') {
28438012SEric.Taylor@Sun.COM 		char *buf;
28442885Sahrens 		char *cp;
28452885Sahrens 
28468012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28478012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28482885Sahrens 		cp = strchr(buf, '@');
28492885Sahrens 		if (cp)
28502885Sahrens 			*(cp+1) = 0;
28518012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
28522885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
28536689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
28548012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2855789Sahrens 		if (error) {
2856789Sahrens 			dmu_objset_close(tosnap);
2857789Sahrens 			return (error);
2858789Sahrens 		}
2859789Sahrens 	}
2860789Sahrens 
2861789Sahrens 	fp = getf(zc->zc_cookie);
2862789Sahrens 	if (fp == NULL) {
2863789Sahrens 		dmu_objset_close(tosnap);
2864789Sahrens 		if (fromsnap)
2865789Sahrens 			dmu_objset_close(fromsnap);
2866789Sahrens 		return (EBADF);
2867789Sahrens 	}
2868789Sahrens 
28695367Sahrens 	off = fp->f_offset;
28705367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28715367Sahrens 
28725367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28735367Sahrens 		fp->f_offset = off;
2874789Sahrens 	releasef(zc->zc_cookie);
2875789Sahrens 	if (fromsnap)
2876789Sahrens 		dmu_objset_close(fromsnap);
2877789Sahrens 	dmu_objset_close(tosnap);
2878789Sahrens 	return (error);
2879789Sahrens }
2880789Sahrens 
28811544Seschrock static int
28821544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
28831544Seschrock {
28841544Seschrock 	int id, error;
28851544Seschrock 
28861544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
28871544Seschrock 	    &zc->zc_inject_record);
28881544Seschrock 
28891544Seschrock 	if (error == 0)
28901544Seschrock 		zc->zc_guid = (uint64_t)id;
28911544Seschrock 
28921544Seschrock 	return (error);
28931544Seschrock }
28941544Seschrock 
28951544Seschrock static int
28961544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
28971544Seschrock {
28981544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
28991544Seschrock }
29001544Seschrock 
29011544Seschrock static int
29021544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29031544Seschrock {
29041544Seschrock 	int id = (int)zc->zc_guid;
29051544Seschrock 	int error;
29061544Seschrock 
29071544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29081544Seschrock 	    &zc->zc_inject_record);
29091544Seschrock 
29101544Seschrock 	zc->zc_guid = id;
29111544Seschrock 
29121544Seschrock 	return (error);
29131544Seschrock }
29141544Seschrock 
29151544Seschrock static int
29161544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29171544Seschrock {
29181544Seschrock 	spa_t *spa;
29191544Seschrock 	int error;
29202676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29211544Seschrock 
29221544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29231544Seschrock 		return (error);
29241544Seschrock 
29252676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29261544Seschrock 	    &count);
29271544Seschrock 	if (error == 0)
29282676Seschrock 		zc->zc_nvlist_dst_size = count;
29291544Seschrock 	else
29302676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29311544Seschrock 
29321544Seschrock 	spa_close(spa, FTAG);
29331544Seschrock 
29341544Seschrock 	return (error);
29351544Seschrock }
29361544Seschrock 
29371544Seschrock static int
29381544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29391544Seschrock {
29401544Seschrock 	spa_t *spa;
29411544Seschrock 	vdev_t *vd;
29421544Seschrock 	int error;
29431544Seschrock 
29447294Sperrin 	/*
29457294Sperrin 	 * On zpool clear we also fix up missing slogs
29467294Sperrin 	 */
29477294Sperrin 	mutex_enter(&spa_namespace_lock);
29487294Sperrin 	spa = spa_lookup(zc->zc_name);
29497294Sperrin 	if (spa == NULL) {
29507294Sperrin 		mutex_exit(&spa_namespace_lock);
29517294Sperrin 		return (EIO);
29527294Sperrin 	}
29537294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29547294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29557294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29567294Sperrin 	}
29577294Sperrin 	mutex_exit(&spa_namespace_lock);
29587294Sperrin 
29591544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29601544Seschrock 		return (error);
29611544Seschrock 
29627754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
29631544Seschrock 
29642676Seschrock 	if (zc->zc_guid == 0) {
29651544Seschrock 		vd = NULL;
29666643Seschrock 	} else {
29676643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
29685450Sbrendan 		if (vd == NULL) {
29697754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
29705450Sbrendan 			spa_close(spa, FTAG);
29715450Sbrendan 			return (ENODEV);
29725450Sbrendan 		}
29731544Seschrock 	}
29741544Seschrock 
29757754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
29767754SJeff.Bonwick@Sun.COM 
29777754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
29787754SJeff.Bonwick@Sun.COM 
29797754SJeff.Bonwick@Sun.COM 	/*
29807754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
29817754SJeff.Bonwick@Sun.COM 	 */
29829234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
29839234SGeorge.Wilson@Sun.COM 		error = EIO;
29841544Seschrock 
29851544Seschrock 	spa_close(spa, FTAG);
29861544Seschrock 
29879234SGeorge.Wilson@Sun.COM 	return (error);
29881544Seschrock }
29891544Seschrock 
29905367Sahrens /*
29915367Sahrens  * inputs:
29925367Sahrens  * zc_name	name of filesystem
29935367Sahrens  * zc_value	name of origin snapshot
29945367Sahrens  *
29955367Sahrens  * outputs:	none
29965367Sahrens  */
29971544Seschrock static int
29982082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
29992082Seschrock {
30002417Sahrens 	char *cp;
30012417Sahrens 
30022417Sahrens 	/*
30032417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30042417Sahrens 	 * it's easier.
30052417Sahrens 	 */
30062676Seschrock 	cp = strchr(zc->zc_value, '@');
30072417Sahrens 	if (cp)
30082417Sahrens 		*cp = '\0';
30092676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30102417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
30112082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
30122082Seschrock }
30132082Seschrock 
30144543Smarks /*
3015*9396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
3016*9396SMatthew.Ahrens@Sun.COM  *
3017*9396SMatthew.Ahrens@Sun.COM  * inputs:
3018*9396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
3019*9396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
3020*9396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
3021*9396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
3022*9396SMatthew.Ahrens@Sun.COM  *
3023*9396SMatthew.Ahrens@Sun.COM  * outputs:
3024*9396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
3025*9396SMatthew.Ahrens@Sun.COM  */
3026*9396SMatthew.Ahrens@Sun.COM static int
3027*9396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
3028*9396SMatthew.Ahrens@Sun.COM {
3029*9396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
3030*9396SMatthew.Ahrens@Sun.COM 	int error;
3031*9396SMatthew.Ahrens@Sun.COM 
3032*9396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
3033*9396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
3034*9396SMatthew.Ahrens@Sun.COM 
3035*9396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
3036*9396SMatthew.Ahrens@Sun.COM 	if (error)
3037*9396SMatthew.Ahrens@Sun.COM 		return (error);
3038*9396SMatthew.Ahrens@Sun.COM 
3039*9396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
3040*9396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
3041*9396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
3042*9396SMatthew.Ahrens@Sun.COM 
3043*9396SMatthew.Ahrens@Sun.COM 	return (error);
3044*9396SMatthew.Ahrens@Sun.COM }
3045*9396SMatthew.Ahrens@Sun.COM 
3046*9396SMatthew.Ahrens@Sun.COM /*
3047*9396SMatthew.Ahrens@Sun.COM  * inputs:
3048*9396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
3049*9396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
3050*9396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
3051*9396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
3052*9396SMatthew.Ahrens@Sun.COM  *
3053*9396SMatthew.Ahrens@Sun.COM  * outputs:
3054*9396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
3055*9396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
3056*9396SMatthew.Ahrens@Sun.COM  */
3057*9396SMatthew.Ahrens@Sun.COM static int
3058*9396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
3059*9396SMatthew.Ahrens@Sun.COM {
3060*9396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
3061*9396SMatthew.Ahrens@Sun.COM 	int error;
3062*9396SMatthew.Ahrens@Sun.COM 
3063*9396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
3064*9396SMatthew.Ahrens@Sun.COM 	if (error)
3065*9396SMatthew.Ahrens@Sun.COM 		return (error);
3066*9396SMatthew.Ahrens@Sun.COM 
3067*9396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
3068*9396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
3069*9396SMatthew.Ahrens@Sun.COM 
3070*9396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
3071*9396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
3072*9396SMatthew.Ahrens@Sun.COM 
3073*9396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
3074*9396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
3075*9396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
3076*9396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
3077*9396SMatthew.Ahrens@Sun.COM 	}
3078*9396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
3079*9396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
3080*9396SMatthew.Ahrens@Sun.COM 
3081*9396SMatthew.Ahrens@Sun.COM 	return (error);
3082*9396SMatthew.Ahrens@Sun.COM }
3083*9396SMatthew.Ahrens@Sun.COM 
3084*9396SMatthew.Ahrens@Sun.COM /*
3085*9396SMatthew.Ahrens@Sun.COM  * inputs:
3086*9396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
3087*9396SMatthew.Ahrens@Sun.COM  *
3088*9396SMatthew.Ahrens@Sun.COM  * outputs:
3089*9396SMatthew.Ahrens@Sun.COM  * none
3090*9396SMatthew.Ahrens@Sun.COM  */
3091*9396SMatthew.Ahrens@Sun.COM static int
3092*9396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
3093*9396SMatthew.Ahrens@Sun.COM {
3094*9396SMatthew.Ahrens@Sun.COM 	objset_t *os;
3095*9396SMatthew.Ahrens@Sun.COM 	int error;
3096*9396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
3097*9396SMatthew.Ahrens@Sun.COM 
3098*9396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3099*9396SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
3100*9396SMatthew.Ahrens@Sun.COM 			/*
3101*9396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
3102*9396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
3103*9396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
3104*9396SMatthew.Ahrens@Sun.COM 			 */
3105*9396SMatthew.Ahrens@Sun.COM 			int mode;
3106*9396SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, NULL, &mode);
3107*9396SMatthew.Ahrens@Sun.COM 			if (error == 0) {
3108*9396SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs,
3109*9396SMatthew.Ahrens@Sun.COM 				    zc->zc_name, mode);
3110*9396SMatthew.Ahrens@Sun.COM 			}
3111*9396SMatthew.Ahrens@Sun.COM 		}
3112*9396SMatthew.Ahrens@Sun.COM 		if (error == 0)
3113*9396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
3114*9396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
3115*9396SMatthew.Ahrens@Sun.COM 	} else {
3116*9396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
3117*9396SMatthew.Ahrens@Sun.COM 		    DS_MODE_USER, &os);
3118*9396SMatthew.Ahrens@Sun.COM 		if (error)
3119*9396SMatthew.Ahrens@Sun.COM 			return (error);
3120*9396SMatthew.Ahrens@Sun.COM 
3121*9396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
3122*9396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
3123*9396SMatthew.Ahrens@Sun.COM 	}
3124*9396SMatthew.Ahrens@Sun.COM 
3125*9396SMatthew.Ahrens@Sun.COM 	return (error);
3126*9396SMatthew.Ahrens@Sun.COM }
3127*9396SMatthew.Ahrens@Sun.COM 
3128*9396SMatthew.Ahrens@Sun.COM /*
31294543Smarks  * We don't want to have a hard dependency
31304543Smarks  * against some special symbols in sharefs
31315331Samw  * nfs, and smbsrv.  Determine them if needed when
31324543Smarks  * the first file system is shared.
31335331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31344543Smarks  */
31355331Samw int (*znfsexport_fs)(void *arg);
31364543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31375331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31385331Samw 
31395331Samw int zfs_nfsshare_inited;
31405331Samw int zfs_smbshare_inited;
31415331Samw 
31424543Smarks ddi_modhandle_t nfs_mod;
31434543Smarks ddi_modhandle_t sharefs_mod;
31445331Samw ddi_modhandle_t smbsrv_mod;
31454543Smarks kmutex_t zfs_share_lock;
31464543Smarks 
31474543Smarks static int
31485331Samw zfs_init_sharefs()
31495331Samw {
31505331Samw 	int error;
31515331Samw 
31525331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
31535331Samw 	/* Both NFS and SMB shares also require sharetab support. */
31545331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
31555331Samw 	    ddi_modopen("fs/sharefs",
31565331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
31575331Samw 		return (ENOSYS);
31585331Samw 	}
31595331Samw 	if (zshare_fs == NULL && ((zshare_fs =
31605331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
31615331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
31625331Samw 		return (ENOSYS);
31635331Samw 	}
31645331Samw 	return (0);
31655331Samw }
31665331Samw 
31675331Samw static int
31684543Smarks zfs_ioc_share(zfs_cmd_t *zc)
31694543Smarks {
31704543Smarks 	int error;
31714543Smarks 	int opcode;
31724543Smarks 
31735331Samw 	switch (zc->zc_share.z_sharetype) {
31745331Samw 	case ZFS_SHARE_NFS:
31755331Samw 	case ZFS_UNSHARE_NFS:
31765331Samw 		if (zfs_nfsshare_inited == 0) {
31775331Samw 			mutex_enter(&zfs_share_lock);
31785331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
31795331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
31805331Samw 				mutex_exit(&zfs_share_lock);
31815331Samw 				return (ENOSYS);
31825331Samw 			}
31835331Samw 			if (znfsexport_fs == NULL &&
31845331Samw 			    ((znfsexport_fs = (int (*)(void *))
31855331Samw 			    ddi_modsym(nfs_mod,
31865331Samw 			    "nfs_export", &error)) == NULL)) {
31875331Samw 				mutex_exit(&zfs_share_lock);
31885331Samw 				return (ENOSYS);
31895331Samw 			}
31905331Samw 			error = zfs_init_sharefs();
31915331Samw 			if (error) {
31925331Samw 				mutex_exit(&zfs_share_lock);
31935331Samw 				return (ENOSYS);
31945331Samw 			}
31955331Samw 			zfs_nfsshare_inited = 1;
31964543Smarks 			mutex_exit(&zfs_share_lock);
31974543Smarks 		}
31985331Samw 		break;
31995331Samw 	case ZFS_SHARE_SMB:
32005331Samw 	case ZFS_UNSHARE_SMB:
32015331Samw 		if (zfs_smbshare_inited == 0) {
32025331Samw 			mutex_enter(&zfs_share_lock);
32035331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32045331Samw 			    ddi_modopen("drv/smbsrv",
32055331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32065331Samw 				mutex_exit(&zfs_share_lock);
32075331Samw 				return (ENOSYS);
32085331Samw 			}
32095331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32105331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32116139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32125331Samw 				mutex_exit(&zfs_share_lock);
32135331Samw 				return (ENOSYS);
32145331Samw 			}
32155331Samw 			error = zfs_init_sharefs();
32165331Samw 			if (error) {
32175331Samw 				mutex_exit(&zfs_share_lock);
32185331Samw 				return (ENOSYS);
32195331Samw 			}
32205331Samw 			zfs_smbshare_inited = 1;
32214543Smarks 			mutex_exit(&zfs_share_lock);
32224543Smarks 		}
32235331Samw 		break;
32245331Samw 	default:
32255331Samw 		return (EINVAL);
32264543Smarks 	}
32274543Smarks 
32285331Samw 	switch (zc->zc_share.z_sharetype) {
32295331Samw 	case ZFS_SHARE_NFS:
32305331Samw 	case ZFS_UNSHARE_NFS:
32315331Samw 		if (error =
32325331Samw 		    znfsexport_fs((void *)
32335331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32345331Samw 			return (error);
32355331Samw 		break;
32365331Samw 	case ZFS_SHARE_SMB:
32375331Samw 	case ZFS_UNSHARE_SMB:
32385331Samw 		if (error = zsmbexport_fs((void *)
32395331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32405331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32418845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32425331Samw 			return (error);
32435331Samw 		}
32445331Samw 		break;
32455331Samw 	}
32465331Samw 
32475331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32485331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32494543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32504543Smarks 
32515331Samw 	/*
32525331Samw 	 * Add or remove share from sharetab
32535331Samw 	 */
32544543Smarks 	error = zshare_fs(opcode,
32554543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
32564543Smarks 	    zc->zc_share.z_sharemax);
32574543Smarks 
32584543Smarks 	return (error);
32594543Smarks 
32604543Smarks }
32614543Smarks 
32628845Samw@Sun.COM ace_t full_access[] = {
32638845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
32648845Samw@Sun.COM };
32658845Samw@Sun.COM 
32668845Samw@Sun.COM /*
32678845Samw@Sun.COM  * Remove all ACL files in shares dir
32688845Samw@Sun.COM  */
32698845Samw@Sun.COM static int
32708845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
32718845Samw@Sun.COM {
32728845Samw@Sun.COM 	zap_cursor_t	zc;
32738845Samw@Sun.COM 	zap_attribute_t	zap;
32748845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
32758845Samw@Sun.COM 	int error;
32768845Samw@Sun.COM 
32778845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
32788845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
32798845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
32808845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
32818845Samw@Sun.COM 		    NULL, 0)) != 0)
32828845Samw@Sun.COM 			break;
32838845Samw@Sun.COM 	}
32848845Samw@Sun.COM 	zap_cursor_fini(&zc);
32858845Samw@Sun.COM 	return (error);
32868845Samw@Sun.COM }
32878845Samw@Sun.COM 
32888845Samw@Sun.COM static int
32898845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
32908845Samw@Sun.COM {
32918845Samw@Sun.COM 	vnode_t *vp;
32928845Samw@Sun.COM 	znode_t *dzp;
32938845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
32948845Samw@Sun.COM 	znode_t *sharedir;
32958845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
32968845Samw@Sun.COM 	nvlist_t *nvlist;
32978845Samw@Sun.COM 	char *src, *target;
32988845Samw@Sun.COM 	vattr_t vattr;
32998845Samw@Sun.COM 	vsecattr_t vsec;
33008845Samw@Sun.COM 	int error = 0;
33018845Samw@Sun.COM 
33028845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33038845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33048845Samw@Sun.COM 		return (error);
33058845Samw@Sun.COM 
33068845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33078845Samw@Sun.COM 
33088845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33098845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33108845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33118845Samw@Sun.COM 		VN_RELE(vp);
33128845Samw@Sun.COM 		return (EINVAL);
33138845Samw@Sun.COM 	}
33148845Samw@Sun.COM 
33158845Samw@Sun.COM 	dzp = VTOZ(vp);
33168845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33178845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33188845Samw@Sun.COM 
33199030SMark.Shellenbaum@Sun.COM 	/*
33209030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33219030SMark.Shellenbaum@Sun.COM 	 */
33229030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33239030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33249030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33259030SMark.Shellenbaum@Sun.COM 
33269030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33279030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33289030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33299030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33309030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33319030SMark.Shellenbaum@Sun.COM 		if (error) {
33329030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33339030SMark.Shellenbaum@Sun.COM 		} else {
33349030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33359030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33369030SMark.Shellenbaum@Sun.COM 		}
33379030SMark.Shellenbaum@Sun.COM 		if (error) {
33389030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33399030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33409030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33419030SMark.Shellenbaum@Sun.COM 			return (error);
33429030SMark.Shellenbaum@Sun.COM 		}
33439030SMark.Shellenbaum@Sun.COM 	}
33449030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33459030SMark.Shellenbaum@Sun.COM 
33469030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33478845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33489030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33498845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33508845Samw@Sun.COM 		return (error);
33518845Samw@Sun.COM 	}
33528845Samw@Sun.COM 
33538845Samw@Sun.COM 	switch (zc->zc_cookie) {
33548845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
33558845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
33568845Samw@Sun.COM 		vattr.va_type = VREG;
33578845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
33588845Samw@Sun.COM 		vattr.va_uid = 0;
33598845Samw@Sun.COM 		vattr.va_gid = 0;
33608845Samw@Sun.COM 
33618845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
33628845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
33638845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
33648845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
33658845Samw@Sun.COM 
33668845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
33678845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
33688845Samw@Sun.COM 		if (resourcevp)
33698845Samw@Sun.COM 			VN_RELE(resourcevp);
33708845Samw@Sun.COM 		break;
33718845Samw@Sun.COM 
33728845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
33738845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
33748845Samw@Sun.COM 		    NULL, 0);
33758845Samw@Sun.COM 		break;
33768845Samw@Sun.COM 
33778845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
33788845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
33798845Samw@Sun.COM 		    zc->zc_nvlist_src_size, &nvlist)) != 0) {
33808845Samw@Sun.COM 			VN_RELE(vp);
33818845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
33828845Samw@Sun.COM 			return (error);
33838845Samw@Sun.COM 		}
33848845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
33858845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
33868845Samw@Sun.COM 		    &target)) {
33878845Samw@Sun.COM 			VN_RELE(vp);
33889179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
33898845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
33908845Samw@Sun.COM 			return (error);
33918845Samw@Sun.COM 		}
33928845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
33938845Samw@Sun.COM 		    kcred, NULL, 0);
33948845Samw@Sun.COM 		nvlist_free(nvlist);
33958845Samw@Sun.COM 		break;
33968845Samw@Sun.COM 
33978845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
33988845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
33998845Samw@Sun.COM 		break;
34008845Samw@Sun.COM 
34018845Samw@Sun.COM 	default:
34028845Samw@Sun.COM 		error = EINVAL;
34038845Samw@Sun.COM 		break;
34048845Samw@Sun.COM 	}
34058845Samw@Sun.COM 
34068845Samw@Sun.COM 	VN_RELE(vp);
34078845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34088845Samw@Sun.COM 
34098845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34108845Samw@Sun.COM 
34118845Samw@Sun.COM 	return (error);
34128845Samw@Sun.COM }
34138845Samw@Sun.COM 
34144543Smarks /*
34154988Sek110237  * pool create, destroy, and export don't log the history as part of
34164988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
34174988Sek110237  * do the logging of those commands.
34184543Smarks  */
3419789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
34209234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34219234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34229234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34239234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34259234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34279234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
34299234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
34319234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
34339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34359234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
34379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
34399234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34419234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34439234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34459234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
34479234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34499234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34519234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34539234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34559234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34579234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34599234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34619234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
34639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34649234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34669234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34679234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
34689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
34699234SGeorge.Wilson@Sun.COM 	    B_TRUE},
34709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
34719234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
34739234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
34749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
34759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
34769234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34789234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34799234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34809234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34819234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
34829234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34839234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
34849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
34859234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
34879234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34889234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
34899234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34909234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34919234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34929234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
34939234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
34959234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
34979234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
34999234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35019234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35029234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
35039234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
35059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
35069234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35079234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
3508*9396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
3509*9396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
3510*9396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
3511*9396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
3512*9396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
3513*9396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
3514*9396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
3515789Sahrens };
3516789Sahrens 
35179234SGeorge.Wilson@Sun.COM int
35189234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
35199234SGeorge.Wilson@Sun.COM {
35209234SGeorge.Wilson@Sun.COM 	spa_t *spa;
35219234SGeorge.Wilson@Sun.COM 	int error;
35229234SGeorge.Wilson@Sun.COM 
35239234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
35249234SGeorge.Wilson@Sun.COM 
3525*9396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
35269234SGeorge.Wilson@Sun.COM 	if (error == 0) {
35279234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
35289234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
35299234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
35309234SGeorge.Wilson@Sun.COM 	}
35319234SGeorge.Wilson@Sun.COM 	return (error);
35329234SGeorge.Wilson@Sun.COM }
35339234SGeorge.Wilson@Sun.COM 
3534789Sahrens static int
3535789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3536789Sahrens {
3537789Sahrens 	zfs_cmd_t *zc;
3538789Sahrens 	uint_t vec;
35392199Sahrens 	int error, rc;
3540789Sahrens 
3541789Sahrens 	if (getminor(dev) != 0)
3542789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3543789Sahrens 
3544789Sahrens 	vec = cmd - ZFS_IOC;
35454787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3546789Sahrens 
3547789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3548789Sahrens 		return (EINVAL);
3549789Sahrens 
3550789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3551789Sahrens 
3552789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3553789Sahrens 
35544787Sahrens 	if (error == 0)
35554543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3556789Sahrens 
3557789Sahrens 	/*
3558789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3559789Sahrens 	 * the lower layers.
3560789Sahrens 	 */
3561789Sahrens 	if (error == 0) {
3562789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3563789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
35644577Sahrens 		case POOL_NAME:
3565789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3566789Sahrens 				error = EINVAL;
35679234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35689234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35699234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3570789Sahrens 			break;
3571789Sahrens 
35724577Sahrens 		case DATASET_NAME:
3573789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3574789Sahrens 				error = EINVAL;
35759234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35769234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35779234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3578789Sahrens 			break;
35792856Snd150628 
35804577Sahrens 		case NO_NAME:
35812856Snd150628 			break;
3582789Sahrens 		}
3583789Sahrens 	}
3584789Sahrens 
3585789Sahrens 	if (error == 0)
3586789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3587789Sahrens 
35882199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
35894543Smarks 	if (error == 0) {
35902199Sahrens 		error = rc;
3591*9396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
35924543Smarks 			zfs_log_history(zc);
35934543Smarks 	}
3594789Sahrens 
3595789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3596789Sahrens 	return (error);
3597789Sahrens }
3598789Sahrens 
3599789Sahrens static int
3600789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3601789Sahrens {
3602789Sahrens 	if (cmd != DDI_ATTACH)
3603789Sahrens 		return (DDI_FAILURE);
3604789Sahrens 
3605789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3606789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3607789Sahrens 		return (DDI_FAILURE);
3608789Sahrens 
3609789Sahrens 	zfs_dip = dip;
3610789Sahrens 
3611789Sahrens 	ddi_report_dev(dip);
3612789Sahrens 
3613789Sahrens 	return (DDI_SUCCESS);
3614789Sahrens }
3615789Sahrens 
3616789Sahrens static int
3617789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3618789Sahrens {
3619789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3620789Sahrens 		return (DDI_FAILURE);
3621789Sahrens 
3622789Sahrens 	if (cmd != DDI_DETACH)
3623789Sahrens 		return (DDI_FAILURE);
3624789Sahrens 
3625789Sahrens 	zfs_dip = NULL;
3626789Sahrens 
3627789Sahrens 	ddi_prop_remove_all(dip);
3628789Sahrens 	ddi_remove_minor_node(dip, NULL);
3629789Sahrens 
3630789Sahrens 	return (DDI_SUCCESS);
3631789Sahrens }
3632789Sahrens 
3633789Sahrens /*ARGSUSED*/
3634789Sahrens static int
3635789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3636789Sahrens {
3637789Sahrens 	switch (infocmd) {
3638789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3639789Sahrens 		*result = zfs_dip;
3640789Sahrens 		return (DDI_SUCCESS);
3641789Sahrens 
3642789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3643849Sbonwick 		*result = (void *)0;
3644789Sahrens 		return (DDI_SUCCESS);
3645789Sahrens 	}
3646789Sahrens 
3647789Sahrens 	return (DDI_FAILURE);
3648789Sahrens }
3649789Sahrens 
3650789Sahrens /*
3651789Sahrens  * OK, so this is a little weird.
3652789Sahrens  *
3653789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3654789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3655789Sahrens  *
3656789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3657789Sahrens  * so most of the standard driver entry points are in zvol.c.
3658789Sahrens  */
3659789Sahrens static struct cb_ops zfs_cb_ops = {
3660789Sahrens 	zvol_open,	/* open */
3661789Sahrens 	zvol_close,	/* close */
3662789Sahrens 	zvol_strategy,	/* strategy */
3663789Sahrens 	nodev,		/* print */
36646423Sgw25295 	zvol_dump,	/* dump */
3665789Sahrens 	zvol_read,	/* read */
3666789Sahrens 	zvol_write,	/* write */
3667789Sahrens 	zfsdev_ioctl,	/* ioctl */
3668789Sahrens 	nodev,		/* devmap */
3669789Sahrens 	nodev,		/* mmap */
3670789Sahrens 	nodev,		/* segmap */
3671789Sahrens 	nochpoll,	/* poll */
3672789Sahrens 	ddi_prop_op,	/* prop_op */
3673789Sahrens 	NULL,		/* streamtab */
3674789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3675789Sahrens 	CB_REV,		/* version */
36763638Sbillm 	nodev,		/* async read */
36773638Sbillm 	nodev,		/* async write */
3678789Sahrens };
3679789Sahrens 
3680789Sahrens static struct dev_ops zfs_dev_ops = {
3681789Sahrens 	DEVO_REV,	/* version */
3682789Sahrens 	0,		/* refcnt */
3683789Sahrens 	zfs_info,	/* info */
3684789Sahrens 	nulldev,	/* identify */
3685789Sahrens 	nulldev,	/* probe */
3686789Sahrens 	zfs_attach,	/* attach */
3687789Sahrens 	zfs_detach,	/* detach */
3688789Sahrens 	nodev,		/* reset */
3689789Sahrens 	&zfs_cb_ops,	/* driver operations */
36907656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
36917656SSherry.Moore@Sun.COM 	NULL,		/* power */
36927656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3693789Sahrens };
3694789Sahrens 
3695789Sahrens static struct modldrv zfs_modldrv = {
36967656SSherry.Moore@Sun.COM 	&mod_driverops,
36977656SSherry.Moore@Sun.COM 	"ZFS storage pool",
36987656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3699789Sahrens };
3700789Sahrens 
3701789Sahrens static struct modlinkage modlinkage = {
3702789Sahrens 	MODREV_1,
3703789Sahrens 	(void *)&zfs_modlfs,
3704789Sahrens 	(void *)&zfs_modldrv,
3705789Sahrens 	NULL
3706789Sahrens };
3707789Sahrens 
37084720Sfr157268 
37094720Sfr157268 uint_t zfs_fsyncer_key;
37105326Sek110237 extern uint_t rrw_tsd_key;
37114720Sfr157268 
3712789Sahrens int
3713789Sahrens _init(void)
3714789Sahrens {
3715789Sahrens 	int error;
3716789Sahrens 
3717849Sbonwick 	spa_init(FREAD | FWRITE);
3718849Sbonwick 	zfs_init();
3719849Sbonwick 	zvol_init();
3720849Sbonwick 
3721849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3722849Sbonwick 		zvol_fini();
3723849Sbonwick 		zfs_fini();
3724849Sbonwick 		spa_fini();
3725789Sahrens 		return (error);
3726849Sbonwick 	}
3727789Sahrens 
37284720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
37295326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
37304720Sfr157268 
3731789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3732789Sahrens 	ASSERT(error == 0);
37334543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3734789Sahrens 
3735789Sahrens 	return (0);
3736789Sahrens }
3737789Sahrens 
3738789Sahrens int
3739789Sahrens _fini(void)
3740789Sahrens {
3741789Sahrens 	int error;
3742789Sahrens 
37431544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3744789Sahrens 		return (EBUSY);
3745789Sahrens 
3746789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3747789Sahrens 		return (error);
3748789Sahrens 
3749789Sahrens 	zvol_fini();
3750789Sahrens 	zfs_fini();
3751789Sahrens 	spa_fini();
37525331Samw 	if (zfs_nfsshare_inited)
37534543Smarks 		(void) ddi_modclose(nfs_mod);
37545331Samw 	if (zfs_smbshare_inited)
37555331Samw 		(void) ddi_modclose(smbsrv_mod);
37565331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
37574543Smarks 		(void) ddi_modclose(sharefs_mod);
3758789Sahrens 
37594720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3760789Sahrens 	ldi_ident_release(zfs_li);
3761789Sahrens 	zfs_li = NULL;
37624543Smarks 	mutex_destroy(&zfs_share_lock);
3763789Sahrens 
3764789Sahrens 	return (error);
3765789Sahrens }
3766789Sahrens 
3767789Sahrens int
3768789Sahrens _info(struct modinfo *modinfop)
3769789Sahrens {
3770789Sahrens 	return (mod_info(&modlinkage, modinfop));
3771789Sahrens }
3772