xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 9425:e7ffacaec3a8)
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 
969396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
979396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
989396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
999396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
1009396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
1019396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
1029396SMatthew.Ahrens@Sun.COM };
1039396SMatthew.Ahrens@Sun.COM 
1049396SMatthew.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 
7159396SMatthew.Ahrens@Sun.COM static int
7169396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
7179396SMatthew.Ahrens@Sun.COM {
7189396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
7199396SMatthew.Ahrens@Sun.COM 	if (err)
7209396SMatthew.Ahrens@Sun.COM 		return (err);
7219396SMatthew.Ahrens@Sun.COM 
7229396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
7239396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7249396SMatthew.Ahrens@Sun.COM 
7259396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
7269396SMatthew.Ahrens@Sun.COM 		/*
7279396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
7289396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
7299396SMatthew.Ahrens@Sun.COM 		 */
7309396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
7319396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
7329396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
7339396SMatthew.Ahrens@Sun.COM 				return (0);
7349396SMatthew.Ahrens@Sun.COM 		} else {
7359396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
7369396SMatthew.Ahrens@Sun.COM 				return (0);
7379396SMatthew.Ahrens@Sun.COM 		}
7389396SMatthew.Ahrens@Sun.COM 	}
7399396SMatthew.Ahrens@Sun.COM 
7409396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7419396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7429396SMatthew.Ahrens@Sun.COM }
7439396SMatthew.Ahrens@Sun.COM 
7449396SMatthew.Ahrens@Sun.COM static int
7459396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
7469396SMatthew.Ahrens@Sun.COM {
7479396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
7489396SMatthew.Ahrens@Sun.COM 	if (err)
7499396SMatthew.Ahrens@Sun.COM 		return (err);
7509396SMatthew.Ahrens@Sun.COM 
7519396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
7529396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7539396SMatthew.Ahrens@Sun.COM 
7549396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7559396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7569396SMatthew.Ahrens@Sun.COM }
7579396SMatthew.Ahrens@Sun.COM 
7589396SMatthew.Ahrens@Sun.COM static int
7599396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
7609396SMatthew.Ahrens@Sun.COM {
7619396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
7629396SMatthew.Ahrens@Sun.COM }
7639396SMatthew.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
8239396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
8249396SMatthew.Ahrens@Sun.COM {
8259396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8269396SMatthew.Ahrens@Sun.COM 	int error;
8279396SMatthew.Ahrens@Sun.COM 
8289396SMatthew.Ahrens@Sun.COM 	error = dmu_objset_open(dsname, DMU_OST_ZFS,
8299396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os);
8309396SMatthew.Ahrens@Sun.COM 	if (error)
8319396SMatthew.Ahrens@Sun.COM 		return (error);
8329396SMatthew.Ahrens@Sun.COM 
8339396SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os->os_user_ptr_lock);
8349396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
8359396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
8369396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
8379396SMatthew.Ahrens@Sun.COM 	} else {
8389396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
8399396SMatthew.Ahrens@Sun.COM 	}
8409396SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os->os_user_ptr_lock);
8419396SMatthew.Ahrens@Sun.COM 	dmu_objset_close(os);
8429396SMatthew.Ahrens@Sun.COM 	return (error);
8439396SMatthew.Ahrens@Sun.COM }
8449396SMatthew.Ahrens@Sun.COM 
8459396SMatthew.Ahrens@Sun.COM /*
8469396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
8479396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
8489396SMatthew.Ahrens@Sun.COM  */
8499396SMatthew.Ahrens@Sun.COM static int
8509396SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, boolean_t readonly, void *tag, zfsvfs_t **zvp)
8519396SMatthew.Ahrens@Sun.COM {
8529396SMatthew.Ahrens@Sun.COM 	int error = 0;
8539396SMatthew.Ahrens@Sun.COM 	int mode = DS_MODE_OWNER | (readonly ? DS_MODE_READONLY : 0);
8549396SMatthew.Ahrens@Sun.COM 
8559396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
8569396SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, mode, zvp);
8579396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
8589396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
8599396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
8609396SMatthew.Ahrens@Sun.COM 			/*
8619396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
8629396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
8639396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
8649396SMatthew.Ahrens@Sun.COM 			 */
8659396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
8669396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
8679396SMatthew.Ahrens@Sun.COM 		}
8689396SMatthew.Ahrens@Sun.COM 	}
8699396SMatthew.Ahrens@Sun.COM 	return (error);
8709396SMatthew.Ahrens@Sun.COM }
8719396SMatthew.Ahrens@Sun.COM 
8729396SMatthew.Ahrens@Sun.COM static void
8739396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
8749396SMatthew.Ahrens@Sun.COM {
8759396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
8769396SMatthew.Ahrens@Sun.COM 
8779396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
8789396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
8799396SMatthew.Ahrens@Sun.COM 	} else {
8809396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(zfsvfs->z_os);
8819396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
8829396SMatthew.Ahrens@Sun.COM 	}
8839396SMatthew.Ahrens@Sun.COM }
8849396SMatthew.Ahrens@Sun.COM 
8859396SMatthew.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)
984*9425SEric.Schrock@Sun.COM 		error = spa_import_verbatim(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 
1332*9425SEric.Schrock@Sun.COM static int
1333*9425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1334*9425SEric.Schrock@Sun.COM {
1335*9425SEric.Schrock@Sun.COM 	spa_t *spa;
1336*9425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
1337*9425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
1338*9425SEric.Schrock@Sun.COM 	int error;
1339*9425SEric.Schrock@Sun.COM 
1340*9425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
1341*9425SEric.Schrock@Sun.COM 	if (error != 0)
1342*9425SEric.Schrock@Sun.COM 		return (error);
1343*9425SEric.Schrock@Sun.COM 
1344*9425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
1345*9425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
1346*9425SEric.Schrock@Sun.COM 	return (error);
1347*9425SEric.Schrock@Sun.COM }
1348*9425SEric.Schrock@Sun.COM 
13495367Sahrens /*
13505367Sahrens  * inputs:
13515367Sahrens  * zc_name		name of filesystem
13525367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13535367Sahrens  *
13545367Sahrens  * outputs:
13555367Sahrens  * zc_objset_stats	stats
13565367Sahrens  * zc_nvlist_dst	property nvlist
13575367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13585367Sahrens  */
13591354Seschrock static int
1360789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1361789Sahrens {
1362789Sahrens 	objset_t *os = NULL;
1363789Sahrens 	int error;
13641356Seschrock 	nvlist_t *nv;
1365789Sahrens 
13666689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13676689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1368789Sahrens 		return (error);
1369789Sahrens 
13702885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1371789Sahrens 
13722856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13736689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13742885Sahrens 		dmu_objset_stats(os, nv);
13753087Sahrens 		/*
13765147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13773087Sahrens 		 * which we aren't supposed to do with a
13786689Smaybee 		 * DS_MODE_USER hold, because it could be
13793087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
13803087Sahrens 		 */
13814577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13824577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13834577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13844577Sahrens 		}
13852676Seschrock 		error = put_nvlist(zc, nv);
13861356Seschrock 		nvlist_free(nv);
13871356Seschrock 	}
1388789Sahrens 
1389789Sahrens 	dmu_objset_close(os);
1390789Sahrens 	return (error);
1391789Sahrens }
1392789Sahrens 
13935498Stimh static int
13945498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
13955498Stimh {
13965498Stimh 	uint64_t value;
13975498Stimh 	int error;
13985498Stimh 
13995498Stimh 	/*
14005498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14015498Stimh 	 * the default value (if there is one).
14025498Stimh 	 */
14035498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14045498Stimh 		return (error);
14055498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14065498Stimh 	return (0);
14075498Stimh }
14085498Stimh 
14095498Stimh /*
14105498Stimh  * inputs:
14115498Stimh  * zc_name		name of filesystem
14125498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14135498Stimh  *
14145498Stimh  * outputs:
14155498Stimh  * zc_nvlist_dst	zpl property nvlist
14165498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14175498Stimh  */
14185498Stimh static int
14195498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14205498Stimh {
14215498Stimh 	objset_t *os;
14225498Stimh 	int err;
14235498Stimh 
14246689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
14256689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
14265498Stimh 		return (err);
14275498Stimh 
14285498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14295498Stimh 
14305498Stimh 	/*
14315498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14326689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14336689Smaybee 	 * hold, because it could be inconsistent.
14345498Stimh 	 */
14355498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14365498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14375498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14385498Stimh 		nvlist_t *nv;
14395498Stimh 
14405498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14415498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14425498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14435498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14445498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14455498Stimh 			err = put_nvlist(zc, nv);
14465498Stimh 		nvlist_free(nv);
14475498Stimh 	} else {
14485498Stimh 		err = ENOENT;
14495498Stimh 	}
14505498Stimh 	dmu_objset_close(os);
14515498Stimh 	return (err);
14525498Stimh }
14535498Stimh 
14549396SMatthew.Ahrens@Sun.COM static boolean_t
14559396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14569396SMatthew.Ahrens@Sun.COM {
14579396SMatthew.Ahrens@Sun.COM 	/*
14589396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14599396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14609396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14619396SMatthew.Ahrens@Sun.COM 	 */
14629396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14639396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14649396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14659396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14669396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14679396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14689396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14699396SMatthew.Ahrens@Sun.COM }
14709396SMatthew.Ahrens@Sun.COM 
14715367Sahrens /*
14725367Sahrens  * inputs:
14735367Sahrens  * zc_name		name of filesystem
14745367Sahrens  * zc_cookie		zap cursor
14755367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14765367Sahrens  *
14775367Sahrens  * outputs:
14785367Sahrens  * zc_name		name of next filesystem
14799396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14805367Sahrens  * zc_objset_stats	stats
14815367Sahrens  * zc_nvlist_dst	property nvlist
14825367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14835367Sahrens  */
1484789Sahrens static int
1485789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1486789Sahrens {
1487885Sahrens 	objset_t *os;
1488789Sahrens 	int error;
1489789Sahrens 	char *p;
1490789Sahrens 
14916689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
14926689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1493885Sahrens 		if (error == ENOENT)
1494885Sahrens 			error = ESRCH;
1495885Sahrens 		return (error);
1496789Sahrens 	}
1497789Sahrens 
1498789Sahrens 	p = strrchr(zc->zc_name, '/');
1499789Sahrens 	if (p == NULL || p[1] != '\0')
1500789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1501789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1502789Sahrens 
15038697SRichard.Morris@Sun.COM 	/*
15048697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15058697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15068697SRichard.Morris@Sun.COM 	 */
15078415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15088415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15098415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15108415SRichard.Morris@Sun.COM 
15118415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15128697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15138415SRichard.Morris@Sun.COM 	}
15148415SRichard.Morris@Sun.COM 
1515789Sahrens 	do {
1516885Sahrens 		error = dmu_dir_list_next(os,
1517885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1518885Sahrens 		    NULL, &zc->zc_cookie);
1519789Sahrens 		if (error == ENOENT)
1520789Sahrens 			error = ESRCH;
15219396SMatthew.Ahrens@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
15226689Smaybee 	dmu_objset_close(os);
1523789Sahrens 
15249396SMatthew.Ahrens@Sun.COM 	if (error == 0)
1525885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1526789Sahrens 
1527789Sahrens 	return (error);
1528789Sahrens }
1529789Sahrens 
15305367Sahrens /*
15315367Sahrens  * inputs:
15325367Sahrens  * zc_name		name of filesystem
15335367Sahrens  * zc_cookie		zap cursor
15345367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15355367Sahrens  *
15365367Sahrens  * outputs:
15375367Sahrens  * zc_name		name of next snapshot
15385367Sahrens  * zc_objset_stats	stats
15395367Sahrens  * zc_nvlist_dst	property nvlist
15405367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15415367Sahrens  */
1542789Sahrens static int
1543789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1544789Sahrens {
1545885Sahrens 	objset_t *os;
1546789Sahrens 	int error;
1547789Sahrens 
15486689Smaybee 	error = dmu_objset_open(zc->zc_name,
15496689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
15506689Smaybee 	if (error)
15516689Smaybee 		return (error == ENOENT ? ESRCH : error);
1552789Sahrens 
15539396SMatthew.Ahrens@Sun.COM 	if (zc->zc_cookie == 0) {
15548697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15558415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
15569396SMatthew.Ahrens@Sun.COM 	}
15571003Slling 	/*
15581003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15591003Slling 	 * so exit immediately.
15601003Slling 	 */
15611003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1562885Sahrens 		dmu_objset_close(os);
15631003Slling 		return (ESRCH);
1564789Sahrens 	}
1565789Sahrens 
1566885Sahrens 	error = dmu_snapshot_list_next(os,
1567885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15685663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
15696689Smaybee 	dmu_objset_close(os);
1570885Sahrens 	if (error == 0)
1571885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15726689Smaybee 	else if (error == ENOENT)
15736689Smaybee 		error = ESRCH;
1574789Sahrens 
15755367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15766689Smaybee 	if (error)
15775367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1578789Sahrens 	return (error);
1579789Sahrens }
1580789Sahrens 
15816423Sgw25295 int
15824787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1583789Sahrens {
15842676Seschrock 	nvpair_t *elem;
15858724SRichard.Morris@Sun.COM 	int error = 0;
15862676Seschrock 	uint64_t intval;
15872676Seschrock 	char *strval;
15888697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
15899396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
15902676Seschrock 
15914543Smarks 	/*
15924543Smarks 	 * First validate permission to set all of the properties
15934543Smarks 	 */
15942676Seschrock 	elem = NULL;
15952676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
15964670Sahrens 		const char *propname = nvpair_name(elem);
15974670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
15982676Seschrock 
15995094Slling 		if (prop == ZPROP_INVAL) {
16002676Seschrock 			/*
16012676Seschrock 			 * If this is a user-defined property, it must be a
16022676Seschrock 			 * string, and there is no further validation to do.
16032676Seschrock 			 */
16049396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16059396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16069396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16079396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16089396SMatthew.Ahrens@Sun.COM 					return (error);
16099396SMatthew.Ahrens@Sun.COM 				continue;
16109396SMatthew.Ahrens@Sun.COM 			}
16119396SMatthew.Ahrens@Sun.COM 
16129396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16139396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16149396SMatthew.Ahrens@Sun.COM 				const char *perm;
16159396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16169396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16179396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16189396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16199396SMatthew.Ahrens@Sun.COM 				else
16209396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16219396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16229396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16239396SMatthew.Ahrens@Sun.COM 					return (error);
16249396SMatthew.Ahrens@Sun.COM 				continue;
16259396SMatthew.Ahrens@Sun.COM 			}
16269396SMatthew.Ahrens@Sun.COM 
16279396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16282676Seschrock 		}
16292676Seschrock 
16309396SMatthew.Ahrens@Sun.COM 		if (issnap)
16319396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16329396SMatthew.Ahrens@Sun.COM 
16334787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16344670Sahrens 			return (error);
16352676Seschrock 
16364670Sahrens 		/*
16374670Sahrens 		 * Check that this value is valid for this pool version
16384670Sahrens 		 */
16394670Sahrens 		switch (prop) {
16403886Sahl 		case ZFS_PROP_COMPRESSION:
16413886Sahl 			/*
16423886Sahl 			 * If the user specified gzip compression, make sure
16433886Sahl 			 * the SPA supports it. We ignore any errors here since
16443886Sahl 			 * we'll catch them later.
16453886Sahl 			 */
16463886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16477042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16487042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16497042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16507184Stimh 				    zfs_earlier_version(name,
16515331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16525331Samw 					return (ENOTSUP);
16537042Sgw25295 
16547042Sgw25295 				/*
16557042Sgw25295 				 * If this is a bootable dataset then
16567042Sgw25295 				 * verify that the compression algorithm
16577042Sgw25295 				 * is supported for booting. We must return
16587042Sgw25295 				 * something other than ENOTSUP since it
16597042Sgw25295 				 * implies a downrev pool version.
16607042Sgw25295 				 */
16617042Sgw25295 				if (zfs_is_bootfs(name) &&
16627042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16637042Sgw25295 					return (ERANGE);
16643886Sahl 			}
16653886Sahl 			break;
16664603Sahrens 
16674603Sahrens 		case ZFS_PROP_COPIES:
16689396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16695331Samw 				return (ENOTSUP);
16704603Sahrens 			break;
16715977Smarks 
16725977Smarks 		case ZFS_PROP_SHARESMB:
16736689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16745977Smarks 				return (ENOTSUP);
16755977Smarks 			break;
16768053SMark.Shellenbaum@Sun.COM 
16778053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16788053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16798053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16808053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16818053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16828053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
16838053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
16844603Sahrens 		}
16854543Smarks 	}
16864543Smarks 
16878697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16884543Smarks 	elem = NULL;
16894543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16904670Sahrens 		const char *propname = nvpair_name(elem);
16914670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16924543Smarks 
16935094Slling 		if (prop == ZPROP_INVAL) {
16949396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
16959396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
16969396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
16979396SMatthew.Ahrens@Sun.COM 				const char *domain;
16989396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
16999396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17009396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17019396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17029396SMatthew.Ahrens@Sun.COM 
17039396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17049396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17059396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17069396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17079396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17089396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
17099396SMatthew.Ahrens@Sun.COM 				domain = propname +
17109396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
17119396SMatthew.Ahrens@Sun.COM 
17129396SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, B_FALSE, FTAG,
17139396SMatthew.Ahrens@Sun.COM 				    &zfsvfs);
17149396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17159396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17169396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17179396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17189396SMatthew.Ahrens@Sun.COM 				}
17199396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17209396SMatthew.Ahrens@Sun.COM 					continue;
17219396SMatthew.Ahrens@Sun.COM 				else
17229396SMatthew.Ahrens@Sun.COM 					goto out;
17239396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17249396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17259396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17269396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17279396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17289396SMatthew.Ahrens@Sun.COM 					continue;
17299396SMatthew.Ahrens@Sun.COM 				else
17309396SMatthew.Ahrens@Sun.COM 					goto out;
17319396SMatthew.Ahrens@Sun.COM 			}
17324543Smarks 		}
17332676Seschrock 
17342676Seschrock 		switch (prop) {
17352676Seschrock 		case ZFS_PROP_QUOTA:
17362676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17374577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17388697SRichard.Morris@Sun.COM 				goto out;
17392676Seschrock 			break;
17402676Seschrock 
17415378Sck153898 		case ZFS_PROP_REFQUOTA:
17425378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17435378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17448697SRichard.Morris@Sun.COM 				goto out;
17455378Sck153898 			break;
17465378Sck153898 
17472676Seschrock 		case ZFS_PROP_RESERVATION:
17482676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17492676Seschrock 			    (error = dsl_dir_set_reservation(name,
17502676Seschrock 			    intval)) != 0)
17518697SRichard.Morris@Sun.COM 				goto out;
17522676Seschrock 			break;
1753789Sahrens 
17545378Sck153898 		case ZFS_PROP_REFRESERVATION:
17555378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17565378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17575378Sck153898 			    intval)) != 0)
17588697SRichard.Morris@Sun.COM 				goto out;
17595378Sck153898 			break;
17605378Sck153898 
17612676Seschrock 		case ZFS_PROP_VOLSIZE:
17622676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17634787Sahrens 			    (error = zvol_set_volsize(name,
17644787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17658697SRichard.Morris@Sun.COM 				goto out;
17662676Seschrock 			break;
17672676Seschrock 
17682676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
17692676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17704577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
17718697SRichard.Morris@Sun.COM 				goto out;
17724577Sahrens 			break;
17734577Sahrens 
17744577Sahrens 		case ZFS_PROP_VERSION:
17759396SMatthew.Ahrens@Sun.COM 		{
17769396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17779396SMatthew.Ahrens@Sun.COM 
17789396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17799396SMatthew.Ahrens@Sun.COM 				goto out;
17809396SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, B_FALSE, FTAG,
17819396SMatthew.Ahrens@Sun.COM 			    &zfsvfs)) != 0)
17829396SMatthew.Ahrens@Sun.COM 				goto out;
17839396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
17849396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
17859396SMatthew.Ahrens@Sun.COM 
17869396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
17879396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
17889396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
17899396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
17909396SMatthew.Ahrens@Sun.COM 			}
17919396SMatthew.Ahrens@Sun.COM 			if (error)
17928697SRichard.Morris@Sun.COM 				goto out;
17932676Seschrock 			break;
17949396SMatthew.Ahrens@Sun.COM 		}
17952676Seschrock 
17962676Seschrock 		default:
17972676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
17982676Seschrock 				if (zfs_prop_get_type(prop) !=
17998697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18008697SRichard.Morris@Sun.COM 					error = EINVAL;
18018697SRichard.Morris@Sun.COM 					goto out;
18028697SRichard.Morris@Sun.COM 				}
18032676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18042885Sahrens 				const char *unused;
18052885Sahrens 
18062717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18072676Seschrock 
18082676Seschrock 				switch (zfs_prop_get_type(prop)) {
18094787Sahrens 				case PROP_TYPE_NUMBER:
18102676Seschrock 					break;
18114787Sahrens 				case PROP_TYPE_STRING:
18128697SRichard.Morris@Sun.COM 					error = EINVAL;
18138697SRichard.Morris@Sun.COM 					goto out;
18144787Sahrens 				case PROP_TYPE_INDEX:
18152717Seschrock 					if (zfs_prop_index_to_string(prop,
18168697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18178697SRichard.Morris@Sun.COM 						error = EINVAL;
18188697SRichard.Morris@Sun.COM 						goto out;
18198697SRichard.Morris@Sun.COM 					}
18202676Seschrock 					break;
18212676Seschrock 				default:
18224577Sahrens 					cmn_err(CE_PANIC,
18234577Sahrens 					    "unknown property type");
18242676Seschrock 					break;
18252676Seschrock 				}
18262676Seschrock 			} else {
18278697SRichard.Morris@Sun.COM 				error = EINVAL;
18288697SRichard.Morris@Sun.COM 				goto out;
18292676Seschrock 			}
18308697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18318697SRichard.Morris@Sun.COM 				goto out;
18322676Seschrock 		}
18332676Seschrock 	}
18342676Seschrock 
18358697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18368697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18378697SRichard.Morris@Sun.COM 	}
18388697SRichard.Morris@Sun.COM out:
18398697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18408697SRichard.Morris@Sun.COM 	return (error);
1841789Sahrens }
1842789Sahrens 
18435367Sahrens /*
18449355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18459355SMatthew.Ahrens@Sun.COM  */
18469355SMatthew.Ahrens@Sun.COM static int
18479355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18489355SMatthew.Ahrens@Sun.COM {
18499355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18509355SMatthew.Ahrens@Sun.COM 	int error = 0;
18519355SMatthew.Ahrens@Sun.COM 
18529355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18539355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18549355SMatthew.Ahrens@Sun.COM 		char *valstr;
18559355SMatthew.Ahrens@Sun.COM 
18569355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18579355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18589355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18599355SMatthew.Ahrens@Sun.COM 
18609355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18619355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18629355SMatthew.Ahrens@Sun.COM 			return (error);
18639355SMatthew.Ahrens@Sun.COM 
18649355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18659355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18669355SMatthew.Ahrens@Sun.COM 
18679355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18689355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18699355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18709355SMatthew.Ahrens@Sun.COM 	}
18719355SMatthew.Ahrens@Sun.COM 	return (0);
18729355SMatthew.Ahrens@Sun.COM }
18739355SMatthew.Ahrens@Sun.COM 
18749355SMatthew.Ahrens@Sun.COM /*
18755367Sahrens  * inputs:
18765367Sahrens  * zc_name		name of filesystem
18778697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18785367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18797265Sahrens  * zc_cookie		clear existing local props?
18805367Sahrens  *
18815367Sahrens  * outputs:		none
18825367Sahrens  */
1883789Sahrens static int
18842676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1885789Sahrens {
18862676Seschrock 	nvlist_t *nvl;
18872676Seschrock 	int error;
1888789Sahrens 
18895094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18905094Slling 	    &nvl)) != 0)
18912676Seschrock 		return (error);
18922676Seschrock 
18937265Sahrens 	if (zc->zc_cookie) {
18947265Sahrens 		nvlist_t *origprops;
18957265Sahrens 		objset_t *os;
18967265Sahrens 
18977265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
18987265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
18997265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19008536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19017265Sahrens 				nvlist_free(origprops);
19027265Sahrens 			}
19037265Sahrens 			dmu_objset_close(os);
19047265Sahrens 		}
19057265Sahrens 
19067265Sahrens 	}
19077265Sahrens 
19084787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19094543Smarks 
19102676Seschrock 	nvlist_free(nvl);
19112676Seschrock 	return (error);
1912789Sahrens }
1913789Sahrens 
19145367Sahrens /*
19155367Sahrens  * inputs:
19165367Sahrens  * zc_name		name of filesystem
19175367Sahrens  * zc_value		name of property to inherit
19185367Sahrens  *
19195367Sahrens  * outputs:		none
19205367Sahrens  */
1921789Sahrens static int
19224849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19234849Sahrens {
19244849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19254849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19264849Sahrens }
19274849Sahrens 
19284849Sahrens static int
19294098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19303912Slling {
19315094Slling 	nvlist_t *props;
19323912Slling 	spa_t *spa;
19335094Slling 	int error;
19348525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19353912Slling 
19365094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19375094Slling 	    &props)))
19383912Slling 		return (error);
19393912Slling 
19408525SEric.Schrock@Sun.COM 	/*
19418525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19428525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19438525SEric.Schrock@Sun.COM 	 */
19448525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19458525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19468525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19478525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19488525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19498525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19508525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19518525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19528525SEric.Schrock@Sun.COM 		}
19538525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19548525SEric.Schrock@Sun.COM 		if (spa != NULL)
19558525SEric.Schrock@Sun.COM 			return (0);
19568525SEric.Schrock@Sun.COM 	}
19578525SEric.Schrock@Sun.COM 
19583912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19595094Slling 		nvlist_free(props);
19603912Slling 		return (error);
19613912Slling 	}
19623912Slling 
19635094Slling 	error = spa_prop_set(spa, props);
19643912Slling 
19655094Slling 	nvlist_free(props);
19663912Slling 	spa_close(spa, FTAG);
19673912Slling 
19683912Slling 	return (error);
19693912Slling }
19703912Slling 
19713912Slling static int
19724098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19733912Slling {
19743912Slling 	spa_t *spa;
19753912Slling 	int error;
19763912Slling 	nvlist_t *nvp = NULL;
19773912Slling 
19788525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19798525SEric.Schrock@Sun.COM 		/*
19808525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19818525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19828525SEric.Schrock@Sun.COM 		 * anyway.
19838525SEric.Schrock@Sun.COM 		 */
19848525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19858525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
19868525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
19878525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19888525SEric.Schrock@Sun.COM 	} else {
19898525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
19908525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
19918525SEric.Schrock@Sun.COM 	}
19923912Slling 
19933912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
19943912Slling 		error = put_nvlist(zc, nvp);
19953912Slling 	else
19963912Slling 		error = EFAULT;
19973912Slling 
19988525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
19993912Slling 	return (error);
20003912Slling }
20013912Slling 
20023912Slling static int
20034543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20044543Smarks {
20054543Smarks 	nvlist_t *nvp;
20064543Smarks 	int error;
20074543Smarks 	uint32_t uid;
20084543Smarks 	uint32_t gid;
20094543Smarks 	uint32_t *groups;
20104543Smarks 	uint_t group_cnt;
20114543Smarks 	cred_t	*usercred;
20124543Smarks 
20135094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20145094Slling 	    &nvp)) != 0) {
20154543Smarks 		return (error);
20164543Smarks 	}
20174543Smarks 
20184543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20194543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20204543Smarks 		nvlist_free(nvp);
20214543Smarks 		return (EPERM);
20224543Smarks 	}
20234543Smarks 
20244543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20254543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20264543Smarks 		nvlist_free(nvp);
20274543Smarks 		return (EPERM);
20284543Smarks 	}
20294543Smarks 
20304543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20314543Smarks 	    &groups, &group_cnt)) != 0) {
20324543Smarks 		nvlist_free(nvp);
20334543Smarks 		return (EPERM);
20344543Smarks 	}
20354543Smarks 	usercred = cralloc();
20364543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20374543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20384543Smarks 		nvlist_free(nvp);
20394543Smarks 		crfree(usercred);
20404543Smarks 		return (EPERM);
20414543Smarks 	}
20424543Smarks 	nvlist_free(nvp);
20434543Smarks 	error = dsl_deleg_access(zc->zc_name,
20444787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20454543Smarks 	crfree(usercred);
20464543Smarks 	return (error);
20474543Smarks }
20484543Smarks 
20495367Sahrens /*
20505367Sahrens  * inputs:
20515367Sahrens  * zc_name		name of filesystem
20525367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20535367Sahrens  * zc_perm_action	allow/unallow flag
20545367Sahrens  *
20555367Sahrens  * outputs:		none
20565367Sahrens  */
20574543Smarks static int
20584543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20594543Smarks {
20604543Smarks 	int error;
20614543Smarks 	nvlist_t *fsaclnv = NULL;
20624543Smarks 
20635094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20645094Slling 	    &fsaclnv)) != 0)
20654543Smarks 		return (error);
20664543Smarks 
20674543Smarks 	/*
20684543Smarks 	 * Verify nvlist is constructed correctly
20694543Smarks 	 */
20704543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20714543Smarks 		nvlist_free(fsaclnv);
20724543Smarks 		return (EINVAL);
20734543Smarks 	}
20744543Smarks 
20754543Smarks 	/*
20764543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20774543Smarks 	 * that user is allowed to hand out each permission in
20784543Smarks 	 * the nvlist(s)
20794543Smarks 	 */
20804543Smarks 
20814787Sahrens 	error = secpolicy_zfs(CRED());
20824543Smarks 	if (error) {
20834787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
20844787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
20854787Sahrens 			    fsaclnv, CRED());
20864787Sahrens 		} else {
20874787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
20884787Sahrens 			    fsaclnv, CRED());
20894787Sahrens 		}
20904543Smarks 	}
20914543Smarks 
20924543Smarks 	if (error == 0)
20934543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
20944543Smarks 
20954543Smarks 	nvlist_free(fsaclnv);
20964543Smarks 	return (error);
20974543Smarks }
20984543Smarks 
20995367Sahrens /*
21005367Sahrens  * inputs:
21015367Sahrens  * zc_name		name of filesystem
21025367Sahrens  *
21035367Sahrens  * outputs:
21045367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21055367Sahrens  */
21064543Smarks static int
21074543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21084543Smarks {
21094543Smarks 	nvlist_t *nvp;
21104543Smarks 	int error;
21114543Smarks 
21124543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21134543Smarks 		error = put_nvlist(zc, nvp);
21144543Smarks 		nvlist_free(nvp);
21154543Smarks 	}
21164543Smarks 
21174543Smarks 	return (error);
21184543Smarks }
21194543Smarks 
21205367Sahrens /*
21215367Sahrens  * inputs:
21225367Sahrens  * zc_name		name of volume
21235367Sahrens  *
21245367Sahrens  * outputs:		none
21255367Sahrens  */
21264543Smarks static int
2127789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
2128789Sahrens {
21294787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2130789Sahrens }
2131789Sahrens 
21325367Sahrens /*
21335367Sahrens  * inputs:
21345367Sahrens  * zc_name		name of volume
21355367Sahrens  *
21365367Sahrens  * outputs:		none
21375367Sahrens  */
2138789Sahrens static int
2139789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
2140789Sahrens {
21412676Seschrock 	return (zvol_remove_minor(zc->zc_name));
2142789Sahrens }
2143789Sahrens 
2144789Sahrens /*
2145789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2146789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2147789Sahrens  * is responsible for releasing the returned vfs pointer.
2148789Sahrens  */
2149789Sahrens static vfs_t *
2150789Sahrens zfs_get_vfs(const char *resource)
2151789Sahrens {
2152789Sahrens 	struct vfs *vfsp;
2153789Sahrens 	struct vfs *vfs_found = NULL;
2154789Sahrens 
2155789Sahrens 	vfs_list_read_lock();
2156789Sahrens 	vfsp = rootvfs;
2157789Sahrens 	do {
2158789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2159789Sahrens 			VFS_HOLD(vfsp);
2160789Sahrens 			vfs_found = vfsp;
2161789Sahrens 			break;
2162789Sahrens 		}
2163789Sahrens 		vfsp = vfsp->vfs_next;
2164789Sahrens 	} while (vfsp != rootvfs);
2165789Sahrens 	vfs_list_unlock();
2166789Sahrens 	return (vfs_found);
2167789Sahrens }
2168789Sahrens 
21694543Smarks /* ARGSUSED */
2170789Sahrens static void
21714543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2172789Sahrens {
21735331Samw 	zfs_creat_t *zct = arg;
21745498Stimh 
21755498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21765331Samw }
21775331Samw 
21785498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21795498Stimh 
21805331Samw /*
21815498Stimh  * inputs:
21827184Stimh  * createprops		list of properties requested by creator
21837184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21847184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21857184Stimh  * os			parent objset pointer (NULL if root fs)
21865331Samw  *
21875498Stimh  * outputs:
21885498Stimh  * zplprops	values for the zplprops we attach to the master node object
21897184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21905331Samw  *
21915498Stimh  * Determine the settings for utf8only, normalization and
21925498Stimh  * casesensitivity.  Specific values may have been requested by the
21935498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21945498Stimh  * the file system is of too early a vintage, a creator can not
21955498Stimh  * request settings for these properties, even if the requested
21965498Stimh  * setting is the default value.  We don't actually want to create dsl
21975498Stimh  * properties for these, so remove them from the source nvlist after
21985498Stimh  * processing.
21995331Samw  */
22005331Samw static int
22019396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
22027184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
22037184Stimh     boolean_t *is_ci)
22045331Samw {
22055498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
22065498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
22075498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
22085498Stimh 
22095498Stimh 	ASSERT(zplprops != NULL);
22105498Stimh 
22115375Stimh 	/*
22125498Stimh 	 * Pull out creator prop choices, if any.
22135375Stimh 	 */
22145498Stimh 	if (createprops) {
22155498Stimh 		(void) nvlist_lookup_uint64(createprops,
22167184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22177184Stimh 		(void) nvlist_lookup_uint64(createprops,
22185498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22195498Stimh 		(void) nvlist_remove_all(createprops,
22205498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22215498Stimh 		(void) nvlist_lookup_uint64(createprops,
22225498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22235498Stimh 		(void) nvlist_remove_all(createprops,
22245498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22255498Stimh 		(void) nvlist_lookup_uint64(createprops,
22265498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22275498Stimh 		(void) nvlist_remove_all(createprops,
22285498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22295331Samw 	}
22305331Samw 
22315375Stimh 	/*
22327184Stimh 	 * If the zpl version requested is whacky or the file system
22337184Stimh 	 * or pool is version is too "young" to support normalization
22347184Stimh 	 * and the creator tried to set a value for one of the props,
22357184Stimh 	 * error out.
22365498Stimh 	 */
22377184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22387184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22397184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22405498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22417184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22425498Stimh 		return (ENOTSUP);
22435498Stimh 
22445498Stimh 	/*
22455498Stimh 	 * Put the version in the zplprops
22465498Stimh 	 */
22475498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22485498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22495498Stimh 
22505498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22515498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22525498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22535498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22545498Stimh 
22555498Stimh 	/*
22565498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22575498Stimh 	 */
22585498Stimh 	if (norm)
22595498Stimh 		u8 = 1;
22605498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22615498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22625498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22635498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22645498Stimh 
22655498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22665498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22675498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22685498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22695498Stimh 
22706492Stimh 	if (is_ci)
22716492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22726492Stimh 
22737184Stimh 	return (0);
22747184Stimh }
22757184Stimh 
22767184Stimh static int
22777184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22787184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22797184Stimh {
22807184Stimh 	boolean_t fuids_ok = B_TRUE;
22817184Stimh 	uint64_t zplver = ZPL_VERSION;
22827184Stimh 	objset_t *os = NULL;
22837184Stimh 	char parentname[MAXNAMELEN];
22847184Stimh 	char *cp;
22857184Stimh 	int error;
22867184Stimh 
22877184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22887184Stimh 	cp = strrchr(parentname, '/');
22897184Stimh 	ASSERT(cp != NULL);
22907184Stimh 	cp[0] = '\0';
22917184Stimh 
22929396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
22939396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22947184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22957184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22967184Stimh 		fuids_ok = B_FALSE;
22977184Stimh 	}
22987184Stimh 
22997184Stimh 	/*
23007184Stimh 	 * Open parent object set so we can inherit zplprop values.
23017184Stimh 	 */
23027184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
23037184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
23047184Stimh 		return (error);
23057184Stimh 
23067184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
23077184Stimh 	    zplprops, is_ci);
23085498Stimh 	dmu_objset_close(os);
23097184Stimh 	return (error);
23107184Stimh }
23117184Stimh 
23127184Stimh static int
23137184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
23147184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
23157184Stimh {
23167184Stimh 	boolean_t fuids_ok = B_TRUE;
23177184Stimh 	uint64_t zplver = ZPL_VERSION;
23187184Stimh 	int error;
23197184Stimh 
23207184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23217184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23227184Stimh 		fuids_ok = B_FALSE;
23237184Stimh 	}
23247184Stimh 
23257184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23267184Stimh 	    zplprops, is_ci);
23277184Stimh 	return (error);
2328789Sahrens }
2329789Sahrens 
23305367Sahrens /*
23315367Sahrens  * inputs:
23325367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23335367Sahrens  * zc_name		name of new objset
23345367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23355367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23365367Sahrens  *
23375498Stimh  * outputs: none
23385367Sahrens  */
2339789Sahrens static int
2340789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2341789Sahrens {
2342789Sahrens 	objset_t *clone;
2343789Sahrens 	int error = 0;
23445331Samw 	zfs_creat_t zct;
23454543Smarks 	nvlist_t *nvprops = NULL;
23464543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2347789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2348789Sahrens 
2349789Sahrens 	switch (type) {
2350789Sahrens 
2351789Sahrens 	case DMU_OST_ZFS:
2352789Sahrens 		cbfunc = zfs_create_cb;
2353789Sahrens 		break;
2354789Sahrens 
2355789Sahrens 	case DMU_OST_ZVOL:
2356789Sahrens 		cbfunc = zvol_create_cb;
2357789Sahrens 		break;
2358789Sahrens 
2359789Sahrens 	default:
23602199Sahrens 		cbfunc = NULL;
23616423Sgw25295 		break;
23622199Sahrens 	}
23635326Sek110237 	if (strchr(zc->zc_name, '@') ||
23645326Sek110237 	    strchr(zc->zc_name, '%'))
2365789Sahrens 		return (EINVAL);
2366789Sahrens 
23672676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23685094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23695094Slling 	    &nvprops)) != 0)
23702676Seschrock 		return (error);
23712676Seschrock 
23725498Stimh 	zct.zct_zplprops = NULL;
23735331Samw 	zct.zct_props = nvprops;
23745331Samw 
23752676Seschrock 	if (zc->zc_value[0] != '\0') {
2376789Sahrens 		/*
2377789Sahrens 		 * We're creating a clone of an existing snapshot.
2378789Sahrens 		 */
23792676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23802676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23814543Smarks 			nvlist_free(nvprops);
2382789Sahrens 			return (EINVAL);
23832676Seschrock 		}
2384789Sahrens 
23852676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
23866689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
23872676Seschrock 		if (error) {
23884543Smarks 			nvlist_free(nvprops);
2389789Sahrens 			return (error);
23902676Seschrock 		}
23916492Stimh 
23926492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
23936492Stimh 		    NULL, NULL);
23945331Samw 		if (error) {
23955331Samw 			dmu_objset_close(clone);
23965331Samw 			nvlist_free(nvprops);
23975331Samw 			return (error);
23985331Samw 		}
2399789Sahrens 		dmu_objset_close(clone);
2400789Sahrens 	} else {
24016492Stimh 		boolean_t is_insensitive = B_FALSE;
24026492Stimh 
24032676Seschrock 		if (cbfunc == NULL) {
24044543Smarks 			nvlist_free(nvprops);
24052199Sahrens 			return (EINVAL);
24062676Seschrock 		}
24072676Seschrock 
2408789Sahrens 		if (type == DMU_OST_ZVOL) {
24092676Seschrock 			uint64_t volsize, volblocksize;
24102676Seschrock 
24114543Smarks 			if (nvprops == NULL ||
24124543Smarks 			    nvlist_lookup_uint64(nvprops,
24132676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
24142676Seschrock 			    &volsize) != 0) {
24154543Smarks 				nvlist_free(nvprops);
24162676Seschrock 				return (EINVAL);
24172676Seschrock 			}
24182676Seschrock 
24194543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24202676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24212676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24224543Smarks 				nvlist_free(nvprops);
24232676Seschrock 				return (EINVAL);
24242676Seschrock 			}
24251133Seschrock 
24262676Seschrock 			if (error != 0)
24272676Seschrock 				volblocksize = zfs_prop_default_numeric(
24282676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24292676Seschrock 
24302676Seschrock 			if ((error = zvol_check_volblocksize(
24312676Seschrock 			    volblocksize)) != 0 ||
24322676Seschrock 			    (error = zvol_check_volsize(volsize,
24332676Seschrock 			    volblocksize)) != 0) {
24344543Smarks 				nvlist_free(nvprops);
2435789Sahrens 				return (error);
24362676Seschrock 			}
24374577Sahrens 		} else if (type == DMU_OST_ZFS) {
24385331Samw 			int error;
24395331Samw 
24405498Stimh 			/*
24415331Samw 			 * We have to have normalization and
24425331Samw 			 * case-folding flags correct when we do the
24435331Samw 			 * file system creation, so go figure them out
24445498Stimh 			 * now.
24455331Samw 			 */
24465498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24475498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24485498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24497184Stimh 			    zct.zct_zplprops, &is_insensitive);
24505331Samw 			if (error != 0) {
24515331Samw 				nvlist_free(nvprops);
24525498Stimh 				nvlist_free(zct.zct_zplprops);
24535331Samw 				return (error);
24544577Sahrens 			}
24552676Seschrock 		}
24566492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
24576492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24585498Stimh 		nvlist_free(zct.zct_zplprops);
2459789Sahrens 	}
24602676Seschrock 
24612676Seschrock 	/*
24622676Seschrock 	 * It would be nice to do this atomically.
24632676Seschrock 	 */
24642676Seschrock 	if (error == 0) {
24654787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
24662676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
24672676Seschrock 	}
24684543Smarks 	nvlist_free(nvprops);
2469789Sahrens 	return (error);
2470789Sahrens }
2471789Sahrens 
24725367Sahrens /*
24735367Sahrens  * inputs:
24745367Sahrens  * zc_name	name of filesystem
24755367Sahrens  * zc_value	short name of snapshot
24765367Sahrens  * zc_cookie	recursive flag
24779396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24785367Sahrens  *
24795367Sahrens  * outputs:	none
24805367Sahrens  */
2481789Sahrens static int
24822199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24832199Sahrens {
24847265Sahrens 	nvlist_t *nvprops = NULL;
24857265Sahrens 	int error;
24867265Sahrens 	boolean_t recursive = zc->zc_cookie;
24877265Sahrens 
24882676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24892199Sahrens 		return (EINVAL);
24907265Sahrens 
24917265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24927265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24937265Sahrens 	    &nvprops)) != 0)
24947265Sahrens 		return (error);
24957265Sahrens 
24969355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
24979355SMatthew.Ahrens@Sun.COM 	if (error)
24989355SMatthew.Ahrens@Sun.COM 		goto out;
24999355SMatthew.Ahrens@Sun.COM 
25009355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
25019355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
25029355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
25039355SMatthew.Ahrens@Sun.COM 		goto out;
25047265Sahrens 	}
25059355SMatthew.Ahrens@Sun.COM 
25069355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
25079355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
25089355SMatthew.Ahrens@Sun.COM 
25099355SMatthew.Ahrens@Sun.COM out:
25107265Sahrens 	nvlist_free(nvprops);
25117265Sahrens 	return (error);
25122199Sahrens }
25132199Sahrens 
25144007Smmusante int
25152199Sahrens zfs_unmount_snap(char *name, void *arg)
2516789Sahrens {
25172417Sahrens 	vfs_t *vfsp = NULL;
25182199Sahrens 
25196689Smaybee 	if (arg) {
25206689Smaybee 		char *snapname = arg;
25216689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25226689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25236689Smaybee 
25246689Smaybee 		(void) strcpy(buf, name);
25256689Smaybee 		(void) strcat(buf, "@");
25266689Smaybee 		(void) strcat(buf, snapname);
25276689Smaybee 		vfsp = zfs_get_vfs(buf);
25286689Smaybee 		kmem_free(buf, len);
25292417Sahrens 	} else if (strchr(name, '@')) {
25302199Sahrens 		vfsp = zfs_get_vfs(name);
25312199Sahrens 	}
25322199Sahrens 
25332199Sahrens 	if (vfsp) {
25342199Sahrens 		/*
25352199Sahrens 		 * Always force the unmount for snapshots.
25362199Sahrens 		 */
25372199Sahrens 		int flag = MS_FORCE;
2538789Sahrens 		int err;
2539789Sahrens 
25402199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25412199Sahrens 			VFS_RELE(vfsp);
25422199Sahrens 			return (err);
25432199Sahrens 		}
25442199Sahrens 		VFS_RELE(vfsp);
25452199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25462199Sahrens 			return (err);
25472199Sahrens 	}
25482199Sahrens 	return (0);
25492199Sahrens }
25502199Sahrens 
25515367Sahrens /*
25525367Sahrens  * inputs:
25535367Sahrens  * zc_name	name of filesystem
25545367Sahrens  * zc_value	short name of snapshot
25555367Sahrens  *
25565367Sahrens  * outputs:	none
25575367Sahrens  */
25582199Sahrens static int
25592199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25602199Sahrens {
25612199Sahrens 	int err;
2562789Sahrens 
25632676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25642199Sahrens 		return (EINVAL);
25652199Sahrens 	err = dmu_objset_find(zc->zc_name,
25662676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25672199Sahrens 	if (err)
25682199Sahrens 		return (err);
25692676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
25702199Sahrens }
25712199Sahrens 
25725367Sahrens /*
25735367Sahrens  * inputs:
25745367Sahrens  * zc_name		name of dataset to destroy
25755367Sahrens  * zc_objset_type	type of objset
25765367Sahrens  *
25775367Sahrens  * outputs:		none
25785367Sahrens  */
25792199Sahrens static int
25802199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25812199Sahrens {
25822199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
25832199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
25842199Sahrens 		if (err)
25852199Sahrens 			return (err);
2586789Sahrens 	}
2587789Sahrens 
2588789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2589789Sahrens }
2590789Sahrens 
25915367Sahrens /*
25925367Sahrens  * inputs:
25935446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25945367Sahrens  *
25955367Sahrens  * outputs:	none
25965367Sahrens  */
2597789Sahrens static int
2598789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2599789Sahrens {
26005446Sahrens 	objset_t *os;
26015446Sahrens 	int error;
26025446Sahrens 	zfsvfs_t *zfsvfs = NULL;
26035446Sahrens 
26045446Sahrens 	/*
26055446Sahrens 	 * Get the zfsvfs for the receiving objset. There
26065446Sahrens 	 * won't be one if we're operating on a zvol, if the
26075446Sahrens 	 * objset doesn't exist yet, or is not mounted.
26085446Sahrens 	 */
26096689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
26105446Sahrens 	if (error)
26115446Sahrens 		return (error);
26125446Sahrens 
26139396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
26145446Sahrens 		int mode;
26155446Sahrens 
26169396SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs, NULL, &mode);
26176083Sek110237 		if (error == 0) {
26186083Sek110237 			int resume_err;
26196083Sek110237 
26206083Sek110237 			error = dmu_objset_rollback(os);
26219396SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
26226083Sek110237 			error = error ? error : resume_err;
26236083Sek110237 		} else {
26246083Sek110237 			dmu_objset_close(os);
26256083Sek110237 		}
26265446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26275446Sahrens 	} else {
26285446Sahrens 		error = dmu_objset_rollback(os);
26295446Sahrens 	}
26306689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
26315446Sahrens 
26325446Sahrens 	return (error);
2633789Sahrens }
2634789Sahrens 
26355367Sahrens /*
26365367Sahrens  * inputs:
26375367Sahrens  * zc_name	old name of dataset
26385367Sahrens  * zc_value	new name of dataset
26395367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26405367Sahrens  *
26415367Sahrens  * outputs:	none
26425367Sahrens  */
2643789Sahrens static int
2644789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2645789Sahrens {
26464490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26474007Smmusante 
26482676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26495326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26505326Sek110237 	    strchr(zc->zc_value, '%'))
2651789Sahrens 		return (EINVAL);
2652789Sahrens 
26534007Smmusante 	/*
26544007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26554007Smmusante 	 * in which case the dataset code figures out which snapshots
26564007Smmusante 	 * to unmount.
26574007Smmusante 	 */
26584007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2659789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26602199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26612199Sahrens 		if (err)
26622199Sahrens 			return (err);
2663789Sahrens 	}
26644007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2665789Sahrens }
2666789Sahrens 
26676689Smaybee static void
26688536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26696689Smaybee {
26706689Smaybee 	zfs_cmd_t *zc;
26716689Smaybee 	nvpair_t *prop;
26726689Smaybee 
26736689Smaybee 	if (props == NULL)
26746689Smaybee 		return;
26756689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
26766689Smaybee 	(void) strcpy(zc->zc_name, dataset);
26776689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
26786689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
26798536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
26808536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
26818536SDavid.Pacheco@Sun.COM 			continue;
26826689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
26836689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
26846689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
26856689Smaybee 	}
26866689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
26876689Smaybee }
26886689Smaybee 
26895367Sahrens /*
26905367Sahrens  * inputs:
26915367Sahrens  * zc_name		name of containing filesystem
26925367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
26935367Sahrens  * zc_value		name of snapshot to create
26945367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
26955367Sahrens  * zc_cookie		file descriptor to recv from
26965367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
26975367Sahrens  * zc_guid		force flag
26985367Sahrens  *
26995367Sahrens  * outputs:
27005367Sahrens  * zc_cookie		number of bytes read
27015367Sahrens  */
2702789Sahrens static int
27035367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2704789Sahrens {
2705789Sahrens 	file_t *fp;
27065326Sek110237 	objset_t *os;
27075367Sahrens 	dmu_recv_cookie_t drc;
27085326Sek110237 	zfsvfs_t *zfsvfs = NULL;
27095326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2710789Sahrens 	int error, fd;
27115367Sahrens 	offset_t off;
27125367Sahrens 	nvlist_t *props = NULL;
27136689Smaybee 	nvlist_t *origprops = NULL;
27145367Sahrens 	objset_t *origin = NULL;
27155367Sahrens 	char *tosnap;
27165367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2717789Sahrens 
27183265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27195326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27205326Sek110237 	    strchr(zc->zc_value, '%'))
27213265Sahrens 		return (EINVAL);
27223265Sahrens 
27235367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27245367Sahrens 	tosnap = strchr(tofs, '@');
27255367Sahrens 	*tosnap = '\0';
27265367Sahrens 	tosnap++;
27275367Sahrens 
27285367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27295367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27305367Sahrens 	    &props)) != 0)
27315367Sahrens 		return (error);
27325367Sahrens 
2733789Sahrens 	fd = zc->zc_cookie;
2734789Sahrens 	fp = getf(fd);
27355367Sahrens 	if (fp == NULL) {
27365367Sahrens 		nvlist_free(props);
2737789Sahrens 		return (EBADF);
27385367Sahrens 	}
27395326Sek110237 
27409396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(tofs, &zfsvfs) == 0) {
27419396SMatthew.Ahrens@Sun.COM 		if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
27429396SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
27439396SMatthew.Ahrens@Sun.COM 			zfsvfs = NULL;
27449396SMatthew.Ahrens@Sun.COM 			error = EBUSY;
27459396SMatthew.Ahrens@Sun.COM 			goto out;
27466083Sek110237 		}
27476689Smaybee 		/*
27486689Smaybee 		 * If new properties are supplied, they are to completely
27496689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27506689Smaybee 		 */
27516689Smaybee 		if (props)
27529396SMatthew.Ahrens@Sun.COM 			(void) dsl_prop_get_all(zfsvfs->z_os, &origprops, TRUE);
27539396SMatthew.Ahrens@Sun.COM 	} else if (props && dmu_objset_open(tofs, DMU_OST_ANY,
27549396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
27559396SMatthew.Ahrens@Sun.COM 		/*
27569396SMatthew.Ahrens@Sun.COM 		 * Get the props even if there was no zfsvfs (zvol or
27579396SMatthew.Ahrens@Sun.COM 		 * unmounted zpl).
27589396SMatthew.Ahrens@Sun.COM 		 */
27599396SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, TRUE);
27606689Smaybee 
27615326Sek110237 		dmu_objset_close(os);
27625326Sek110237 	}
27635326Sek110237 
27645367Sahrens 	if (zc->zc_string[0]) {
27655367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
27666689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
27676689Smaybee 		if (error)
27686689Smaybee 			goto out;
27695367Sahrens 	}
27705367Sahrens 
27715367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
27725367Sahrens 	    force, origin, zfsvfs != NULL, &drc);
27735367Sahrens 	if (origin)
27745367Sahrens 		dmu_objset_close(origin);
27756689Smaybee 	if (error)
27766689Smaybee 		goto out;
27775326Sek110237 
27785326Sek110237 	/*
27796689Smaybee 	 * Reset properties.  We do this before we receive the stream
27806689Smaybee 	 * so that the properties are applied to the new data.
27815326Sek110237 	 */
27825367Sahrens 	if (props) {
27838536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27846689Smaybee 		/*
27856689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27866689Smaybee 		 */
27876689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27885367Sahrens 	}
27895367Sahrens 
27905367Sahrens 	off = fp->f_offset;
27915367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
27925367Sahrens 
27936689Smaybee 	if (error == 0 && zfsvfs) {
27948012SEric.Taylor@Sun.COM 		char *osname;
27956689Smaybee 		int mode;
27966689Smaybee 
27976689Smaybee 		/* online recv */
27988012SEric.Taylor@Sun.COM 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
27996689Smaybee 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
28006689Smaybee 		if (error == 0) {
28016689Smaybee 			int resume_err;
28026689Smaybee 
28036689Smaybee 			error = dmu_recv_end(&drc);
28046689Smaybee 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
28056689Smaybee 			error = error ? error : resume_err;
28065367Sahrens 		} else {
28076689Smaybee 			dmu_recv_abort_cleanup(&drc);
28085367Sahrens 		}
28098012SEric.Taylor@Sun.COM 		kmem_free(osname, MAXNAMELEN);
28106689Smaybee 	} else if (error == 0) {
28116689Smaybee 		error = dmu_recv_end(&drc);
28126083Sek110237 	}
28135367Sahrens 
28145367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28155367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28165367Sahrens 		fp->f_offset = off;
28172885Sahrens 
28186689Smaybee 	/*
28196689Smaybee 	 * On error, restore the original props.
28206689Smaybee 	 */
28216689Smaybee 	if (error && props) {
28228536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28236689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28246689Smaybee 	}
28256689Smaybee out:
28266689Smaybee 	if (zfsvfs) {
28276689Smaybee 		mutex_exit(&zfsvfs->z_online_recv_lock);
28286689Smaybee 		VFS_RELE(zfsvfs->z_vfs);
28296689Smaybee 	}
28306689Smaybee 	nvlist_free(props);
28316689Smaybee 	nvlist_free(origprops);
2832789Sahrens 	releasef(fd);
2833789Sahrens 	return (error);
2834789Sahrens }
2835789Sahrens 
28365367Sahrens /*
28375367Sahrens  * inputs:
28385367Sahrens  * zc_name	name of snapshot to send
28395367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28405367Sahrens  * zc_cookie	file descriptor to send stream to
28415367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28425367Sahrens  *
28435367Sahrens  * outputs: none
28445367Sahrens  */
2845789Sahrens static int
28465367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2847789Sahrens {
2848789Sahrens 	objset_t *fromsnap = NULL;
2849789Sahrens 	objset_t *tosnap;
2850789Sahrens 	file_t *fp;
2851789Sahrens 	int error;
28525367Sahrens 	offset_t off;
2853789Sahrens 
2854789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
28556689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2856789Sahrens 	if (error)
2857789Sahrens 		return (error);
2858789Sahrens 
28592676Seschrock 	if (zc->zc_value[0] != '\0') {
28608012SEric.Taylor@Sun.COM 		char *buf;
28612885Sahrens 		char *cp;
28622885Sahrens 
28638012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28648012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28652885Sahrens 		cp = strchr(buf, '@');
28662885Sahrens 		if (cp)
28672885Sahrens 			*(cp+1) = 0;
28688012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
28692885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
28706689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
28718012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2872789Sahrens 		if (error) {
2873789Sahrens 			dmu_objset_close(tosnap);
2874789Sahrens 			return (error);
2875789Sahrens 		}
2876789Sahrens 	}
2877789Sahrens 
2878789Sahrens 	fp = getf(zc->zc_cookie);
2879789Sahrens 	if (fp == NULL) {
2880789Sahrens 		dmu_objset_close(tosnap);
2881789Sahrens 		if (fromsnap)
2882789Sahrens 			dmu_objset_close(fromsnap);
2883789Sahrens 		return (EBADF);
2884789Sahrens 	}
2885789Sahrens 
28865367Sahrens 	off = fp->f_offset;
28875367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28885367Sahrens 
28895367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28905367Sahrens 		fp->f_offset = off;
2891789Sahrens 	releasef(zc->zc_cookie);
2892789Sahrens 	if (fromsnap)
2893789Sahrens 		dmu_objset_close(fromsnap);
2894789Sahrens 	dmu_objset_close(tosnap);
2895789Sahrens 	return (error);
2896789Sahrens }
2897789Sahrens 
28981544Seschrock static int
28991544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29001544Seschrock {
29011544Seschrock 	int id, error;
29021544Seschrock 
29031544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29041544Seschrock 	    &zc->zc_inject_record);
29051544Seschrock 
29061544Seschrock 	if (error == 0)
29071544Seschrock 		zc->zc_guid = (uint64_t)id;
29081544Seschrock 
29091544Seschrock 	return (error);
29101544Seschrock }
29111544Seschrock 
29121544Seschrock static int
29131544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29141544Seschrock {
29151544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29161544Seschrock }
29171544Seschrock 
29181544Seschrock static int
29191544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29201544Seschrock {
29211544Seschrock 	int id = (int)zc->zc_guid;
29221544Seschrock 	int error;
29231544Seschrock 
29241544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29251544Seschrock 	    &zc->zc_inject_record);
29261544Seschrock 
29271544Seschrock 	zc->zc_guid = id;
29281544Seschrock 
29291544Seschrock 	return (error);
29301544Seschrock }
29311544Seschrock 
29321544Seschrock static int
29331544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29341544Seschrock {
29351544Seschrock 	spa_t *spa;
29361544Seschrock 	int error;
29372676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29381544Seschrock 
29391544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29401544Seschrock 		return (error);
29411544Seschrock 
29422676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29431544Seschrock 	    &count);
29441544Seschrock 	if (error == 0)
29452676Seschrock 		zc->zc_nvlist_dst_size = count;
29461544Seschrock 	else
29472676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29481544Seschrock 
29491544Seschrock 	spa_close(spa, FTAG);
29501544Seschrock 
29511544Seschrock 	return (error);
29521544Seschrock }
29531544Seschrock 
29541544Seschrock static int
29551544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29561544Seschrock {
29571544Seschrock 	spa_t *spa;
29581544Seschrock 	vdev_t *vd;
29591544Seschrock 	int error;
29601544Seschrock 
29617294Sperrin 	/*
29627294Sperrin 	 * On zpool clear we also fix up missing slogs
29637294Sperrin 	 */
29647294Sperrin 	mutex_enter(&spa_namespace_lock);
29657294Sperrin 	spa = spa_lookup(zc->zc_name);
29667294Sperrin 	if (spa == NULL) {
29677294Sperrin 		mutex_exit(&spa_namespace_lock);
29687294Sperrin 		return (EIO);
29697294Sperrin 	}
29707294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29717294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29727294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29737294Sperrin 	}
29747294Sperrin 	mutex_exit(&spa_namespace_lock);
29757294Sperrin 
29761544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29771544Seschrock 		return (error);
29781544Seschrock 
29797754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
29801544Seschrock 
29812676Seschrock 	if (zc->zc_guid == 0) {
29821544Seschrock 		vd = NULL;
29836643Seschrock 	} else {
29846643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
29855450Sbrendan 		if (vd == NULL) {
29867754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
29875450Sbrendan 			spa_close(spa, FTAG);
29885450Sbrendan 			return (ENODEV);
29895450Sbrendan 		}
29901544Seschrock 	}
29911544Seschrock 
29927754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
29937754SJeff.Bonwick@Sun.COM 
29947754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
29957754SJeff.Bonwick@Sun.COM 
29967754SJeff.Bonwick@Sun.COM 	/*
29977754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
29987754SJeff.Bonwick@Sun.COM 	 */
29999234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30009234SGeorge.Wilson@Sun.COM 		error = EIO;
30011544Seschrock 
30021544Seschrock 	spa_close(spa, FTAG);
30031544Seschrock 
30049234SGeorge.Wilson@Sun.COM 	return (error);
30051544Seschrock }
30061544Seschrock 
30075367Sahrens /*
30085367Sahrens  * inputs:
30095367Sahrens  * zc_name	name of filesystem
30105367Sahrens  * zc_value	name of origin snapshot
30115367Sahrens  *
30125367Sahrens  * outputs:	none
30135367Sahrens  */
30141544Seschrock static int
30152082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30162082Seschrock {
30172417Sahrens 	char *cp;
30182417Sahrens 
30192417Sahrens 	/*
30202417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30212417Sahrens 	 * it's easier.
30222417Sahrens 	 */
30232676Seschrock 	cp = strchr(zc->zc_value, '@');
30242417Sahrens 	if (cp)
30252417Sahrens 		*cp = '\0';
30262676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30272417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
30282082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
30292082Seschrock }
30302082Seschrock 
30314543Smarks /*
30329396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30339396SMatthew.Ahrens@Sun.COM  *
30349396SMatthew.Ahrens@Sun.COM  * inputs:
30359396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30369396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30379396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30389396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30399396SMatthew.Ahrens@Sun.COM  *
30409396SMatthew.Ahrens@Sun.COM  * outputs:
30419396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30429396SMatthew.Ahrens@Sun.COM  */
30439396SMatthew.Ahrens@Sun.COM static int
30449396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30459396SMatthew.Ahrens@Sun.COM {
30469396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30479396SMatthew.Ahrens@Sun.COM 	int error;
30489396SMatthew.Ahrens@Sun.COM 
30499396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30509396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30519396SMatthew.Ahrens@Sun.COM 
30529396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30539396SMatthew.Ahrens@Sun.COM 	if (error)
30549396SMatthew.Ahrens@Sun.COM 		return (error);
30559396SMatthew.Ahrens@Sun.COM 
30569396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
30579396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
30589396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30599396SMatthew.Ahrens@Sun.COM 
30609396SMatthew.Ahrens@Sun.COM 	return (error);
30619396SMatthew.Ahrens@Sun.COM }
30629396SMatthew.Ahrens@Sun.COM 
30639396SMatthew.Ahrens@Sun.COM /*
30649396SMatthew.Ahrens@Sun.COM  * inputs:
30659396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
30669396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
30679396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
30689396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
30699396SMatthew.Ahrens@Sun.COM  *
30709396SMatthew.Ahrens@Sun.COM  * outputs:
30719396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
30729396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
30739396SMatthew.Ahrens@Sun.COM  */
30749396SMatthew.Ahrens@Sun.COM static int
30759396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
30769396SMatthew.Ahrens@Sun.COM {
30779396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30789396SMatthew.Ahrens@Sun.COM 	int error;
30799396SMatthew.Ahrens@Sun.COM 
30809396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30819396SMatthew.Ahrens@Sun.COM 	if (error)
30829396SMatthew.Ahrens@Sun.COM 		return (error);
30839396SMatthew.Ahrens@Sun.COM 
30849396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
30859396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
30869396SMatthew.Ahrens@Sun.COM 
30879396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
30889396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
30899396SMatthew.Ahrens@Sun.COM 
30909396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
30919396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
30929396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
30939396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
30949396SMatthew.Ahrens@Sun.COM 	}
30959396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
30969396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30979396SMatthew.Ahrens@Sun.COM 
30989396SMatthew.Ahrens@Sun.COM 	return (error);
30999396SMatthew.Ahrens@Sun.COM }
31009396SMatthew.Ahrens@Sun.COM 
31019396SMatthew.Ahrens@Sun.COM /*
31029396SMatthew.Ahrens@Sun.COM  * inputs:
31039396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31049396SMatthew.Ahrens@Sun.COM  *
31059396SMatthew.Ahrens@Sun.COM  * outputs:
31069396SMatthew.Ahrens@Sun.COM  * none
31079396SMatthew.Ahrens@Sun.COM  */
31089396SMatthew.Ahrens@Sun.COM static int
31099396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31109396SMatthew.Ahrens@Sun.COM {
31119396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31129396SMatthew.Ahrens@Sun.COM 	int error;
31139396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31149396SMatthew.Ahrens@Sun.COM 
31159396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
31169396SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
31179396SMatthew.Ahrens@Sun.COM 			/*
31189396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31199396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31209396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31219396SMatthew.Ahrens@Sun.COM 			 */
31229396SMatthew.Ahrens@Sun.COM 			int mode;
31239396SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, NULL, &mode);
31249396SMatthew.Ahrens@Sun.COM 			if (error == 0) {
31259396SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs,
31269396SMatthew.Ahrens@Sun.COM 				    zc->zc_name, mode);
31279396SMatthew.Ahrens@Sun.COM 			}
31289396SMatthew.Ahrens@Sun.COM 		}
31299396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31309396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31319396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31329396SMatthew.Ahrens@Sun.COM 	} else {
31339396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
31349396SMatthew.Ahrens@Sun.COM 		    DS_MODE_USER, &os);
31359396SMatthew.Ahrens@Sun.COM 		if (error)
31369396SMatthew.Ahrens@Sun.COM 			return (error);
31379396SMatthew.Ahrens@Sun.COM 
31389396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
31399396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
31409396SMatthew.Ahrens@Sun.COM 	}
31419396SMatthew.Ahrens@Sun.COM 
31429396SMatthew.Ahrens@Sun.COM 	return (error);
31439396SMatthew.Ahrens@Sun.COM }
31449396SMatthew.Ahrens@Sun.COM 
31459396SMatthew.Ahrens@Sun.COM /*
31464543Smarks  * We don't want to have a hard dependency
31474543Smarks  * against some special symbols in sharefs
31485331Samw  * nfs, and smbsrv.  Determine them if needed when
31494543Smarks  * the first file system is shared.
31505331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31514543Smarks  */
31525331Samw int (*znfsexport_fs)(void *arg);
31534543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31545331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31555331Samw 
31565331Samw int zfs_nfsshare_inited;
31575331Samw int zfs_smbshare_inited;
31585331Samw 
31594543Smarks ddi_modhandle_t nfs_mod;
31604543Smarks ddi_modhandle_t sharefs_mod;
31615331Samw ddi_modhandle_t smbsrv_mod;
31624543Smarks kmutex_t zfs_share_lock;
31634543Smarks 
31644543Smarks static int
31655331Samw zfs_init_sharefs()
31665331Samw {
31675331Samw 	int error;
31685331Samw 
31695331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
31705331Samw 	/* Both NFS and SMB shares also require sharetab support. */
31715331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
31725331Samw 	    ddi_modopen("fs/sharefs",
31735331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
31745331Samw 		return (ENOSYS);
31755331Samw 	}
31765331Samw 	if (zshare_fs == NULL && ((zshare_fs =
31775331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
31785331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
31795331Samw 		return (ENOSYS);
31805331Samw 	}
31815331Samw 	return (0);
31825331Samw }
31835331Samw 
31845331Samw static int
31854543Smarks zfs_ioc_share(zfs_cmd_t *zc)
31864543Smarks {
31874543Smarks 	int error;
31884543Smarks 	int opcode;
31894543Smarks 
31905331Samw 	switch (zc->zc_share.z_sharetype) {
31915331Samw 	case ZFS_SHARE_NFS:
31925331Samw 	case ZFS_UNSHARE_NFS:
31935331Samw 		if (zfs_nfsshare_inited == 0) {
31945331Samw 			mutex_enter(&zfs_share_lock);
31955331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
31965331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
31975331Samw 				mutex_exit(&zfs_share_lock);
31985331Samw 				return (ENOSYS);
31995331Samw 			}
32005331Samw 			if (znfsexport_fs == NULL &&
32015331Samw 			    ((znfsexport_fs = (int (*)(void *))
32025331Samw 			    ddi_modsym(nfs_mod,
32035331Samw 			    "nfs_export", &error)) == NULL)) {
32045331Samw 				mutex_exit(&zfs_share_lock);
32055331Samw 				return (ENOSYS);
32065331Samw 			}
32075331Samw 			error = zfs_init_sharefs();
32085331Samw 			if (error) {
32095331Samw 				mutex_exit(&zfs_share_lock);
32105331Samw 				return (ENOSYS);
32115331Samw 			}
32125331Samw 			zfs_nfsshare_inited = 1;
32134543Smarks 			mutex_exit(&zfs_share_lock);
32144543Smarks 		}
32155331Samw 		break;
32165331Samw 	case ZFS_SHARE_SMB:
32175331Samw 	case ZFS_UNSHARE_SMB:
32185331Samw 		if (zfs_smbshare_inited == 0) {
32195331Samw 			mutex_enter(&zfs_share_lock);
32205331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32215331Samw 			    ddi_modopen("drv/smbsrv",
32225331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32235331Samw 				mutex_exit(&zfs_share_lock);
32245331Samw 				return (ENOSYS);
32255331Samw 			}
32265331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32275331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32286139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32295331Samw 				mutex_exit(&zfs_share_lock);
32305331Samw 				return (ENOSYS);
32315331Samw 			}
32325331Samw 			error = zfs_init_sharefs();
32335331Samw 			if (error) {
32345331Samw 				mutex_exit(&zfs_share_lock);
32355331Samw 				return (ENOSYS);
32365331Samw 			}
32375331Samw 			zfs_smbshare_inited = 1;
32384543Smarks 			mutex_exit(&zfs_share_lock);
32394543Smarks 		}
32405331Samw 		break;
32415331Samw 	default:
32425331Samw 		return (EINVAL);
32434543Smarks 	}
32444543Smarks 
32455331Samw 	switch (zc->zc_share.z_sharetype) {
32465331Samw 	case ZFS_SHARE_NFS:
32475331Samw 	case ZFS_UNSHARE_NFS:
32485331Samw 		if (error =
32495331Samw 		    znfsexport_fs((void *)
32505331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32515331Samw 			return (error);
32525331Samw 		break;
32535331Samw 	case ZFS_SHARE_SMB:
32545331Samw 	case ZFS_UNSHARE_SMB:
32555331Samw 		if (error = zsmbexport_fs((void *)
32565331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32575331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32588845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32595331Samw 			return (error);
32605331Samw 		}
32615331Samw 		break;
32625331Samw 	}
32635331Samw 
32645331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32655331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32664543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32674543Smarks 
32685331Samw 	/*
32695331Samw 	 * Add or remove share from sharetab
32705331Samw 	 */
32714543Smarks 	error = zshare_fs(opcode,
32724543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
32734543Smarks 	    zc->zc_share.z_sharemax);
32744543Smarks 
32754543Smarks 	return (error);
32764543Smarks 
32774543Smarks }
32784543Smarks 
32798845Samw@Sun.COM ace_t full_access[] = {
32808845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
32818845Samw@Sun.COM };
32828845Samw@Sun.COM 
32838845Samw@Sun.COM /*
32848845Samw@Sun.COM  * Remove all ACL files in shares dir
32858845Samw@Sun.COM  */
32868845Samw@Sun.COM static int
32878845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
32888845Samw@Sun.COM {
32898845Samw@Sun.COM 	zap_cursor_t	zc;
32908845Samw@Sun.COM 	zap_attribute_t	zap;
32918845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
32928845Samw@Sun.COM 	int error;
32938845Samw@Sun.COM 
32948845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
32958845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
32968845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
32978845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
32988845Samw@Sun.COM 		    NULL, 0)) != 0)
32998845Samw@Sun.COM 			break;
33008845Samw@Sun.COM 	}
33018845Samw@Sun.COM 	zap_cursor_fini(&zc);
33028845Samw@Sun.COM 	return (error);
33038845Samw@Sun.COM }
33048845Samw@Sun.COM 
33058845Samw@Sun.COM static int
33068845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33078845Samw@Sun.COM {
33088845Samw@Sun.COM 	vnode_t *vp;
33098845Samw@Sun.COM 	znode_t *dzp;
33108845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33118845Samw@Sun.COM 	znode_t *sharedir;
33128845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33138845Samw@Sun.COM 	nvlist_t *nvlist;
33148845Samw@Sun.COM 	char *src, *target;
33158845Samw@Sun.COM 	vattr_t vattr;
33168845Samw@Sun.COM 	vsecattr_t vsec;
33178845Samw@Sun.COM 	int error = 0;
33188845Samw@Sun.COM 
33198845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33208845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33218845Samw@Sun.COM 		return (error);
33228845Samw@Sun.COM 
33238845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33248845Samw@Sun.COM 
33258845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33268845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33278845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33288845Samw@Sun.COM 		VN_RELE(vp);
33298845Samw@Sun.COM 		return (EINVAL);
33308845Samw@Sun.COM 	}
33318845Samw@Sun.COM 
33328845Samw@Sun.COM 	dzp = VTOZ(vp);
33338845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33348845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33358845Samw@Sun.COM 
33369030SMark.Shellenbaum@Sun.COM 	/*
33379030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33389030SMark.Shellenbaum@Sun.COM 	 */
33399030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33409030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33419030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33429030SMark.Shellenbaum@Sun.COM 
33439030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33449030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33459030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33469030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33479030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33489030SMark.Shellenbaum@Sun.COM 		if (error) {
33499030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33509030SMark.Shellenbaum@Sun.COM 		} else {
33519030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33529030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33539030SMark.Shellenbaum@Sun.COM 		}
33549030SMark.Shellenbaum@Sun.COM 		if (error) {
33559030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33569030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33579030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33589030SMark.Shellenbaum@Sun.COM 			return (error);
33599030SMark.Shellenbaum@Sun.COM 		}
33609030SMark.Shellenbaum@Sun.COM 	}
33619030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33629030SMark.Shellenbaum@Sun.COM 
33639030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33648845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33659030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33668845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33678845Samw@Sun.COM 		return (error);
33688845Samw@Sun.COM 	}
33698845Samw@Sun.COM 
33708845Samw@Sun.COM 	switch (zc->zc_cookie) {
33718845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
33728845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
33738845Samw@Sun.COM 		vattr.va_type = VREG;
33748845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
33758845Samw@Sun.COM 		vattr.va_uid = 0;
33768845Samw@Sun.COM 		vattr.va_gid = 0;
33778845Samw@Sun.COM 
33788845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
33798845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
33808845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
33818845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
33828845Samw@Sun.COM 
33838845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
33848845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
33858845Samw@Sun.COM 		if (resourcevp)
33868845Samw@Sun.COM 			VN_RELE(resourcevp);
33878845Samw@Sun.COM 		break;
33888845Samw@Sun.COM 
33898845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
33908845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
33918845Samw@Sun.COM 		    NULL, 0);
33928845Samw@Sun.COM 		break;
33938845Samw@Sun.COM 
33948845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
33958845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
33968845Samw@Sun.COM 		    zc->zc_nvlist_src_size, &nvlist)) != 0) {
33978845Samw@Sun.COM 			VN_RELE(vp);
33988845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
33998845Samw@Sun.COM 			return (error);
34008845Samw@Sun.COM 		}
34018845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34028845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34038845Samw@Sun.COM 		    &target)) {
34048845Samw@Sun.COM 			VN_RELE(vp);
34059179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34068845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34078845Samw@Sun.COM 			return (error);
34088845Samw@Sun.COM 		}
34098845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34108845Samw@Sun.COM 		    kcred, NULL, 0);
34118845Samw@Sun.COM 		nvlist_free(nvlist);
34128845Samw@Sun.COM 		break;
34138845Samw@Sun.COM 
34148845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34158845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34168845Samw@Sun.COM 		break;
34178845Samw@Sun.COM 
34188845Samw@Sun.COM 	default:
34198845Samw@Sun.COM 		error = EINVAL;
34208845Samw@Sun.COM 		break;
34218845Samw@Sun.COM 	}
34228845Samw@Sun.COM 
34238845Samw@Sun.COM 	VN_RELE(vp);
34248845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34258845Samw@Sun.COM 
34268845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34278845Samw@Sun.COM 
34288845Samw@Sun.COM 	return (error);
34298845Samw@Sun.COM }
34308845Samw@Sun.COM 
34314543Smarks /*
34324988Sek110237  * pool create, destroy, and export don't log the history as part of
34334988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
34344988Sek110237  * do the logging of those commands.
34354543Smarks  */
3436789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
34379234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34389234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34399234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34409234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34419234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34429234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34439234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34449234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34459234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
34469234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34479234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
34489234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34499234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
34509234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34519234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34529234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34539234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
34549234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34559234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
34569234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34589234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34609234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34619234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34629234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
34649234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34669234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34679234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34689234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34699234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34709234SGeorge.Wilson@Sun.COM 	    B_TRUE },
3471*9425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
3472*9425SEric.Schrock@Sun.COM 	    B_TRUE },
34739234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34749234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34769234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34789234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34799234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34809234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34819234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
34829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34839234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34859234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
34879234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
34889234SGeorge.Wilson@Sun.COM 	    B_TRUE},
34899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
34909234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
34929234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
34939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
34949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
34959234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34979234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34999234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
35019234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35029234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
35039234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
35049234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
35069234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35079234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
35089234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35099234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35109234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35119234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
35129234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35139234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35149234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35159234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35169234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35179234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
35189234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35199234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35209234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35219234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
35229234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35239234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
35249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
35259234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
35279396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
35289396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
35299396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35309396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
35319396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35329396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
35339396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
3534789Sahrens };
3535789Sahrens 
35369234SGeorge.Wilson@Sun.COM int
35379234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
35389234SGeorge.Wilson@Sun.COM {
35399234SGeorge.Wilson@Sun.COM 	spa_t *spa;
35409234SGeorge.Wilson@Sun.COM 	int error;
35419234SGeorge.Wilson@Sun.COM 
35429234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
35439234SGeorge.Wilson@Sun.COM 
35449396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
35459234SGeorge.Wilson@Sun.COM 	if (error == 0) {
35469234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
35479234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
35489234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
35499234SGeorge.Wilson@Sun.COM 	}
35509234SGeorge.Wilson@Sun.COM 	return (error);
35519234SGeorge.Wilson@Sun.COM }
35529234SGeorge.Wilson@Sun.COM 
3553789Sahrens static int
3554789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3555789Sahrens {
3556789Sahrens 	zfs_cmd_t *zc;
3557789Sahrens 	uint_t vec;
35582199Sahrens 	int error, rc;
3559789Sahrens 
3560789Sahrens 	if (getminor(dev) != 0)
3561789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3562789Sahrens 
3563789Sahrens 	vec = cmd - ZFS_IOC;
35644787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3565789Sahrens 
3566789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3567789Sahrens 		return (EINVAL);
3568789Sahrens 
3569789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3570789Sahrens 
3571789Sahrens 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3572789Sahrens 
35734787Sahrens 	if (error == 0)
35744543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3575789Sahrens 
3576789Sahrens 	/*
3577789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3578789Sahrens 	 * the lower layers.
3579789Sahrens 	 */
3580789Sahrens 	if (error == 0) {
3581789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3582789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
35834577Sahrens 		case POOL_NAME:
3584789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3585789Sahrens 				error = EINVAL;
35869234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35879234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35889234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3589789Sahrens 			break;
3590789Sahrens 
35914577Sahrens 		case DATASET_NAME:
3592789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3593789Sahrens 				error = EINVAL;
35949234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35959234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35969234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3597789Sahrens 			break;
35982856Snd150628 
35994577Sahrens 		case NO_NAME:
36002856Snd150628 			break;
3601789Sahrens 		}
3602789Sahrens 	}
3603789Sahrens 
3604789Sahrens 	if (error == 0)
3605789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3606789Sahrens 
36072199Sahrens 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
36084543Smarks 	if (error == 0) {
36092199Sahrens 		error = rc;
36109396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
36114543Smarks 			zfs_log_history(zc);
36124543Smarks 	}
3613789Sahrens 
3614789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3615789Sahrens 	return (error);
3616789Sahrens }
3617789Sahrens 
3618789Sahrens static int
3619789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3620789Sahrens {
3621789Sahrens 	if (cmd != DDI_ATTACH)
3622789Sahrens 		return (DDI_FAILURE);
3623789Sahrens 
3624789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3625789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3626789Sahrens 		return (DDI_FAILURE);
3627789Sahrens 
3628789Sahrens 	zfs_dip = dip;
3629789Sahrens 
3630789Sahrens 	ddi_report_dev(dip);
3631789Sahrens 
3632789Sahrens 	return (DDI_SUCCESS);
3633789Sahrens }
3634789Sahrens 
3635789Sahrens static int
3636789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3637789Sahrens {
3638789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3639789Sahrens 		return (DDI_FAILURE);
3640789Sahrens 
3641789Sahrens 	if (cmd != DDI_DETACH)
3642789Sahrens 		return (DDI_FAILURE);
3643789Sahrens 
3644789Sahrens 	zfs_dip = NULL;
3645789Sahrens 
3646789Sahrens 	ddi_prop_remove_all(dip);
3647789Sahrens 	ddi_remove_minor_node(dip, NULL);
3648789Sahrens 
3649789Sahrens 	return (DDI_SUCCESS);
3650789Sahrens }
3651789Sahrens 
3652789Sahrens /*ARGSUSED*/
3653789Sahrens static int
3654789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3655789Sahrens {
3656789Sahrens 	switch (infocmd) {
3657789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3658789Sahrens 		*result = zfs_dip;
3659789Sahrens 		return (DDI_SUCCESS);
3660789Sahrens 
3661789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3662849Sbonwick 		*result = (void *)0;
3663789Sahrens 		return (DDI_SUCCESS);
3664789Sahrens 	}
3665789Sahrens 
3666789Sahrens 	return (DDI_FAILURE);
3667789Sahrens }
3668789Sahrens 
3669789Sahrens /*
3670789Sahrens  * OK, so this is a little weird.
3671789Sahrens  *
3672789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3673789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3674789Sahrens  *
3675789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3676789Sahrens  * so most of the standard driver entry points are in zvol.c.
3677789Sahrens  */
3678789Sahrens static struct cb_ops zfs_cb_ops = {
3679789Sahrens 	zvol_open,	/* open */
3680789Sahrens 	zvol_close,	/* close */
3681789Sahrens 	zvol_strategy,	/* strategy */
3682789Sahrens 	nodev,		/* print */
36836423Sgw25295 	zvol_dump,	/* dump */
3684789Sahrens 	zvol_read,	/* read */
3685789Sahrens 	zvol_write,	/* write */
3686789Sahrens 	zfsdev_ioctl,	/* ioctl */
3687789Sahrens 	nodev,		/* devmap */
3688789Sahrens 	nodev,		/* mmap */
3689789Sahrens 	nodev,		/* segmap */
3690789Sahrens 	nochpoll,	/* poll */
3691789Sahrens 	ddi_prop_op,	/* prop_op */
3692789Sahrens 	NULL,		/* streamtab */
3693789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3694789Sahrens 	CB_REV,		/* version */
36953638Sbillm 	nodev,		/* async read */
36963638Sbillm 	nodev,		/* async write */
3697789Sahrens };
3698789Sahrens 
3699789Sahrens static struct dev_ops zfs_dev_ops = {
3700789Sahrens 	DEVO_REV,	/* version */
3701789Sahrens 	0,		/* refcnt */
3702789Sahrens 	zfs_info,	/* info */
3703789Sahrens 	nulldev,	/* identify */
3704789Sahrens 	nulldev,	/* probe */
3705789Sahrens 	zfs_attach,	/* attach */
3706789Sahrens 	zfs_detach,	/* detach */
3707789Sahrens 	nodev,		/* reset */
3708789Sahrens 	&zfs_cb_ops,	/* driver operations */
37097656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
37107656SSherry.Moore@Sun.COM 	NULL,		/* power */
37117656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3712789Sahrens };
3713789Sahrens 
3714789Sahrens static struct modldrv zfs_modldrv = {
37157656SSherry.Moore@Sun.COM 	&mod_driverops,
37167656SSherry.Moore@Sun.COM 	"ZFS storage pool",
37177656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3718789Sahrens };
3719789Sahrens 
3720789Sahrens static struct modlinkage modlinkage = {
3721789Sahrens 	MODREV_1,
3722789Sahrens 	(void *)&zfs_modlfs,
3723789Sahrens 	(void *)&zfs_modldrv,
3724789Sahrens 	NULL
3725789Sahrens };
3726789Sahrens 
37274720Sfr157268 
37284720Sfr157268 uint_t zfs_fsyncer_key;
37295326Sek110237 extern uint_t rrw_tsd_key;
37304720Sfr157268 
3731789Sahrens int
3732789Sahrens _init(void)
3733789Sahrens {
3734789Sahrens 	int error;
3735789Sahrens 
3736849Sbonwick 	spa_init(FREAD | FWRITE);
3737849Sbonwick 	zfs_init();
3738849Sbonwick 	zvol_init();
3739849Sbonwick 
3740849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3741849Sbonwick 		zvol_fini();
3742849Sbonwick 		zfs_fini();
3743849Sbonwick 		spa_fini();
3744789Sahrens 		return (error);
3745849Sbonwick 	}
3746789Sahrens 
37474720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
37485326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
37494720Sfr157268 
3750789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3751789Sahrens 	ASSERT(error == 0);
37524543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3753789Sahrens 
3754789Sahrens 	return (0);
3755789Sahrens }
3756789Sahrens 
3757789Sahrens int
3758789Sahrens _fini(void)
3759789Sahrens {
3760789Sahrens 	int error;
3761789Sahrens 
37621544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3763789Sahrens 		return (EBUSY);
3764789Sahrens 
3765789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3766789Sahrens 		return (error);
3767789Sahrens 
3768789Sahrens 	zvol_fini();
3769789Sahrens 	zfs_fini();
3770789Sahrens 	spa_fini();
37715331Samw 	if (zfs_nfsshare_inited)
37724543Smarks 		(void) ddi_modclose(nfs_mod);
37735331Samw 	if (zfs_smbshare_inited)
37745331Samw 		(void) ddi_modclose(smbsrv_mod);
37755331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
37764543Smarks 		(void) ddi_modclose(sharefs_mod);
3777789Sahrens 
37784720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3779789Sahrens 	ldi_ident_release(zfs_li);
3780789Sahrens 	zfs_li = NULL;
37814543Smarks 	mutex_destroy(&zfs_share_lock);
3782789Sahrens 
3783789Sahrens 	return (error);
3784789Sahrens }
3785789Sahrens 
3786789Sahrens int
3787789Sahrens _info(struct modinfo *modinfop)
3788789Sahrens {
3789789Sahrens 	return (mod_info(&modlinkage, modinfop));
3790789Sahrens }
3791