xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10204:83c3a84aecef)
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
7689643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, 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 
7829643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
7839643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
784789Sahrens 		kmem_free(packed, size);
785789Sahrens 		return (error);
786789Sahrens 	}
787789Sahrens 
7885094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
789789Sahrens 		kmem_free(packed, size);
790789Sahrens 		return (error);
791789Sahrens 	}
792789Sahrens 
793789Sahrens 	kmem_free(packed, size);
794789Sahrens 
7955094Slling 	*nvp = list;
796789Sahrens 	return (0);
797789Sahrens }
798789Sahrens 
799789Sahrens static int
8002676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
8012676Seschrock {
8022676Seschrock 	char *packed = NULL;
8032676Seschrock 	size_t size;
8042676Seschrock 	int error;
8052676Seschrock 
8062676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
8072676Seschrock 
8082676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
8092676Seschrock 		error = ENOMEM;
8102676Seschrock 	} else {
8114611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
8122676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
8132676Seschrock 		    KM_SLEEP) == 0);
8149643SEric.Taylor@Sun.COM 		error = ddi_copyout(packed,
8159643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
8162676Seschrock 		kmem_free(packed, size);
8172676Seschrock 	}
8182676Seschrock 
8192676Seschrock 	zc->zc_nvlist_dst_size = size;
8202676Seschrock 	return (error);
8212676Seschrock }
8222676Seschrock 
8232676Seschrock static int
8249396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
8259396SMatthew.Ahrens@Sun.COM {
8269396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8279396SMatthew.Ahrens@Sun.COM 	int error;
8289396SMatthew.Ahrens@Sun.COM 
8299396SMatthew.Ahrens@Sun.COM 	error = dmu_objset_open(dsname, DMU_OST_ZFS,
8309396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os);
8319396SMatthew.Ahrens@Sun.COM 	if (error)
8329396SMatthew.Ahrens@Sun.COM 		return (error);
8339396SMatthew.Ahrens@Sun.COM 
8349396SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os->os_user_ptr_lock);
8359396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
8369396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
8379396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
8389396SMatthew.Ahrens@Sun.COM 	} else {
8399396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
8409396SMatthew.Ahrens@Sun.COM 	}
8419396SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os->os_user_ptr_lock);
8429396SMatthew.Ahrens@Sun.COM 	dmu_objset_close(os);
8439396SMatthew.Ahrens@Sun.COM 	return (error);
8449396SMatthew.Ahrens@Sun.COM }
8459396SMatthew.Ahrens@Sun.COM 
8469396SMatthew.Ahrens@Sun.COM /*
8479396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
8489396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
8499396SMatthew.Ahrens@Sun.COM  */
8509396SMatthew.Ahrens@Sun.COM static int
8519396SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, boolean_t readonly, void *tag, zfsvfs_t **zvp)
8529396SMatthew.Ahrens@Sun.COM {
8539396SMatthew.Ahrens@Sun.COM 	int error = 0;
8549396SMatthew.Ahrens@Sun.COM 	int mode = DS_MODE_OWNER | (readonly ? DS_MODE_READONLY : 0);
8559396SMatthew.Ahrens@Sun.COM 
8569396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
8579396SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, mode, zvp);
8589396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
8599396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
8609396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
8619396SMatthew.Ahrens@Sun.COM 			/*
8629396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
8639396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
8649396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
8659396SMatthew.Ahrens@Sun.COM 			 */
8669396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
8679396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
8689396SMatthew.Ahrens@Sun.COM 		}
8699396SMatthew.Ahrens@Sun.COM 	}
8709396SMatthew.Ahrens@Sun.COM 	return (error);
8719396SMatthew.Ahrens@Sun.COM }
8729396SMatthew.Ahrens@Sun.COM 
8739396SMatthew.Ahrens@Sun.COM static void
8749396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
8759396SMatthew.Ahrens@Sun.COM {
8769396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
8779396SMatthew.Ahrens@Sun.COM 
8789396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
8799396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
8809396SMatthew.Ahrens@Sun.COM 	} else {
8819396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(zfsvfs->z_os);
8829396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
8839396SMatthew.Ahrens@Sun.COM 	}
8849396SMatthew.Ahrens@Sun.COM }
8859396SMatthew.Ahrens@Sun.COM 
8869396SMatthew.Ahrens@Sun.COM static int
887789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
888789Sahrens {
889789Sahrens 	int error;
8905094Slling 	nvlist_t *config, *props = NULL;
8917184Stimh 	nvlist_t *rootprops = NULL;
8927184Stimh 	nvlist_t *zplprops = NULL;
8934715Sek110237 	char *buf;
894789Sahrens 
8955094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8969643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
8974988Sek110237 		return (error);
8984715Sek110237 
8995094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9009643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9019643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9025094Slling 		nvlist_free(config);
9035094Slling 		return (error);
9045094Slling 	}
9055094Slling 
9067184Stimh 	if (props) {
9077184Stimh 		nvlist_t *nvl = NULL;
9087184Stimh 		uint64_t version = SPA_VERSION;
9097184Stimh 
9107184Stimh 		(void) nvlist_lookup_uint64(props,
9117184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9127184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
9137184Stimh 			error = EINVAL;
9147184Stimh 			goto pool_props_bad;
9157184Stimh 		}
9167184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9177184Stimh 		if (nvl) {
9187184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9197184Stimh 			if (error != 0) {
9207184Stimh 				nvlist_free(config);
9217184Stimh 				nvlist_free(props);
9227184Stimh 				return (error);
9237184Stimh 			}
9247184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9257184Stimh 		}
9267184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9277184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
9287184Stimh 		    zplprops, NULL);
9297184Stimh 		if (error)
9307184Stimh 			goto pool_props_bad;
9317184Stimh 	}
9327184Stimh 
9334988Sek110237 	buf = history_str_get(zc);
934789Sahrens 
9357184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9367184Stimh 
9377184Stimh 	/*
9387184Stimh 	 * Set the remaining root properties
9397184Stimh 	 */
9407184Stimh 	if (!error &&
9417184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9427184Stimh 		(void) spa_destroy(zc->zc_name);
943789Sahrens 
9444988Sek110237 	if (buf != NULL)
9454988Sek110237 		history_str_free(buf);
9465094Slling 
9477184Stimh pool_props_bad:
9487184Stimh 	nvlist_free(rootprops);
9497184Stimh 	nvlist_free(zplprops);
950789Sahrens 	nvlist_free(config);
9517184Stimh 	nvlist_free(props);
9525094Slling 
953789Sahrens 	return (error);
954789Sahrens }
955789Sahrens 
956789Sahrens static int
957789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
958789Sahrens {
9594543Smarks 	int error;
9604543Smarks 	zfs_log_history(zc);
9614543Smarks 	error = spa_destroy(zc->zc_name);
9624543Smarks 	return (error);
963789Sahrens }
964789Sahrens 
965789Sahrens static int
966789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
967789Sahrens {
968789Sahrens 	int error;
9695094Slling 	nvlist_t *config, *props = NULL;
970789Sahrens 	uint64_t guid;
971789Sahrens 
9725094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9739643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
974789Sahrens 		return (error);
975789Sahrens 
9765094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9779643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9789643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9795094Slling 		nvlist_free(config);
9805094Slling 		return (error);
9815094Slling 	}
9825094Slling 
983789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
9841544Seschrock 	    guid != zc->zc_guid)
985789Sahrens 		error = EINVAL;
9866643Seschrock 	else if (zc->zc_cookie)
9879425SEric.Schrock@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config,
9886643Seschrock 		    props);
989789Sahrens 	else
9905094Slling 		error = spa_import(zc->zc_name, config, props);
991789Sahrens 
992789Sahrens 	nvlist_free(config);
993789Sahrens 
9945094Slling 	if (props)
9955094Slling 		nvlist_free(props);
9965094Slling 
997789Sahrens 	return (error);
998789Sahrens }
999789Sahrens 
1000789Sahrens static int
1001789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1002789Sahrens {
10034543Smarks 	int error;
10047214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
10058211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
10067214Slling 
10074543Smarks 	zfs_log_history(zc);
10088211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
10094543Smarks 	return (error);
1010789Sahrens }
1011789Sahrens 
1012789Sahrens static int
1013789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1014789Sahrens {
1015789Sahrens 	nvlist_t *configs;
1016789Sahrens 	int error;
1017789Sahrens 
1018789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1019789Sahrens 		return (EEXIST);
1020789Sahrens 
10212676Seschrock 	error = put_nvlist(zc, configs);
1022789Sahrens 
1023789Sahrens 	nvlist_free(configs);
1024789Sahrens 
1025789Sahrens 	return (error);
1026789Sahrens }
1027789Sahrens 
1028789Sahrens static int
1029789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1030789Sahrens {
1031789Sahrens 	nvlist_t *config;
1032789Sahrens 	int error;
10331544Seschrock 	int ret = 0;
1034789Sahrens 
10352676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10362676Seschrock 	    sizeof (zc->zc_value));
1037789Sahrens 
1038789Sahrens 	if (config != NULL) {
10392676Seschrock 		ret = put_nvlist(zc, config);
1040789Sahrens 		nvlist_free(config);
10411544Seschrock 
10421544Seschrock 		/*
10431544Seschrock 		 * The config may be present even if 'error' is non-zero.
10441544Seschrock 		 * In this case we return success, and preserve the real errno
10451544Seschrock 		 * in 'zc_cookie'.
10461544Seschrock 		 */
10471544Seschrock 		zc->zc_cookie = error;
1048789Sahrens 	} else {
10491544Seschrock 		ret = error;
1050789Sahrens 	}
1051789Sahrens 
10521544Seschrock 	return (ret);
1053789Sahrens }
1054789Sahrens 
1055789Sahrens /*
1056789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1057789Sahrens  * user land knows which devices are available and overall pool health.
1058789Sahrens  */
1059789Sahrens static int
1060789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1061789Sahrens {
1062789Sahrens 	nvlist_t *tryconfig, *config;
1063789Sahrens 	int error;
1064789Sahrens 
10655094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10669643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1067789Sahrens 		return (error);
1068789Sahrens 
1069789Sahrens 	config = spa_tryimport(tryconfig);
1070789Sahrens 
1071789Sahrens 	nvlist_free(tryconfig);
1072789Sahrens 
1073789Sahrens 	if (config == NULL)
1074789Sahrens 		return (EINVAL);
1075789Sahrens 
10762676Seschrock 	error = put_nvlist(zc, config);
1077789Sahrens 	nvlist_free(config);
1078789Sahrens 
1079789Sahrens 	return (error);
1080789Sahrens }
1081789Sahrens 
1082789Sahrens static int
1083789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1084789Sahrens {
1085789Sahrens 	spa_t *spa;
1086789Sahrens 	int error;
1087789Sahrens 
10882926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10892926Sek110237 		return (error);
10902926Sek110237 
10917046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
10922926Sek110237 
10932926Sek110237 	spa_close(spa, FTAG);
10942926Sek110237 
1095789Sahrens 	return (error);
1096789Sahrens }
1097789Sahrens 
1098789Sahrens static int
1099789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1100789Sahrens {
1101789Sahrens 	spa_t *spa;
1102789Sahrens 	int error;
1103789Sahrens 
1104789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1105789Sahrens 	if (error == 0) {
1106789Sahrens 		spa_freeze(spa);
1107789Sahrens 		spa_close(spa, FTAG);
1108789Sahrens 	}
1109789Sahrens 	return (error);
1110789Sahrens }
1111789Sahrens 
1112789Sahrens static int
11131760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11141760Seschrock {
11151760Seschrock 	spa_t *spa;
11161760Seschrock 	int error;
11171760Seschrock 
11182926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11192926Sek110237 		return (error);
11202926Sek110237 
11215118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11225118Slling 		spa_close(spa, FTAG);
11235118Slling 		return (EINVAL);
11245118Slling 	}
11255118Slling 
11265094Slling 	spa_upgrade(spa, zc->zc_cookie);
11272926Sek110237 	spa_close(spa, FTAG);
11282926Sek110237 
11292926Sek110237 	return (error);
11302926Sek110237 }
11312926Sek110237 
11322926Sek110237 static int
11332926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11342926Sek110237 {
11352926Sek110237 	spa_t *spa;
11362926Sek110237 	char *hist_buf;
11372926Sek110237 	uint64_t size;
11382926Sek110237 	int error;
11392926Sek110237 
11402926Sek110237 	if ((size = zc->zc_history_len) == 0)
11412926Sek110237 		return (EINVAL);
11422926Sek110237 
11432926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11442926Sek110237 		return (error);
11452926Sek110237 
11464577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11473863Sek110237 		spa_close(spa, FTAG);
11483863Sek110237 		return (ENOTSUP);
11493863Sek110237 	}
11503863Sek110237 
11512926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11522926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11532926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11549643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
11559643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
11569643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
11572926Sek110237 	}
11582926Sek110237 
11592926Sek110237 	spa_close(spa, FTAG);
11602926Sek110237 	kmem_free(hist_buf, size);
11612926Sek110237 	return (error);
11622926Sek110237 }
11632926Sek110237 
11642926Sek110237 static int
11653444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11663444Sek110237 {
11673444Sek110237 	int error;
11683444Sek110237 
11693912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11703444Sek110237 		return (error);
11713444Sek110237 
11723444Sek110237 	return (0);
11733444Sek110237 }
11743444Sek110237 
11753444Sek110237 static int
11763444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11773444Sek110237 {
11783444Sek110237 	objset_t *osp;
11793444Sek110237 	int error;
11803444Sek110237 
11813444Sek110237 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
11826689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
11833444Sek110237 		return (error);
11843444Sek110237 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
11853444Sek110237 	    sizeof (zc->zc_value));
11863444Sek110237 	dmu_objset_close(osp);
11873444Sek110237 
11883444Sek110237 	return (error);
11893444Sek110237 }
11903444Sek110237 
11913444Sek110237 static int
1192789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1193789Sahrens {
1194789Sahrens 	spa_t *spa;
1195789Sahrens 	int error;
11966423Sgw25295 	nvlist_t *config, **l2cache, **spares;
11976423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1198789Sahrens 
1199789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1200789Sahrens 	if (error != 0)
1201789Sahrens 		return (error);
1202789Sahrens 
12035450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12049643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
12055450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12065450Sbrendan 	    &l2cache, &nl2cache);
12075450Sbrendan 
12086423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12096423Sgw25295 	    &spares, &nspares);
12106423Sgw25295 
12113912Slling 	/*
12123912Slling 	 * A root pool with concatenated devices is not supported.
12136423Sgw25295 	 * Thus, can not add a device to a root pool.
12146423Sgw25295 	 *
12156423Sgw25295 	 * Intent log device can not be added to a rootpool because
12166423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12176423Sgw25295 	 * can not be accessed during the mountroot time.
12186423Sgw25295 	 *
12196423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12203912Slling 	 */
12216423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
12223912Slling 		spa_close(spa, FTAG);
12233912Slling 		return (EDOM);
12243912Slling 	}
12253912Slling 
12265450Sbrendan 	if (error == 0) {
1227789Sahrens 		error = spa_vdev_add(spa, config);
1228789Sahrens 		nvlist_free(config);
1229789Sahrens 	}
1230789Sahrens 	spa_close(spa, FTAG);
1231789Sahrens 	return (error);
1232789Sahrens }
1233789Sahrens 
1234789Sahrens static int
1235789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1236789Sahrens {
12372082Seschrock 	spa_t *spa;
12382082Seschrock 	int error;
12392082Seschrock 
12402082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12412082Seschrock 	if (error != 0)
12422082Seschrock 		return (error);
12432082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12442082Seschrock 	spa_close(spa, FTAG);
12452082Seschrock 	return (error);
1246789Sahrens }
1247789Sahrens 
1248789Sahrens static int
12494451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1250789Sahrens {
1251789Sahrens 	spa_t *spa;
1252789Sahrens 	int error;
12534451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1254789Sahrens 
12552926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1256789Sahrens 		return (error);
12574451Seschrock 	switch (zc->zc_cookie) {
12584451Seschrock 	case VDEV_STATE_ONLINE:
12594451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12604451Seschrock 		break;
12614451Seschrock 
12624451Seschrock 	case VDEV_STATE_OFFLINE:
12634451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12644451Seschrock 		break;
1265789Sahrens 
12664451Seschrock 	case VDEV_STATE_FAULTED:
12674451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12684451Seschrock 		break;
1269789Sahrens 
12704451Seschrock 	case VDEV_STATE_DEGRADED:
12714451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12724451Seschrock 		break;
12734451Seschrock 
12744451Seschrock 	default:
12754451Seschrock 		error = EINVAL;
12764451Seschrock 	}
12774451Seschrock 	zc->zc_cookie = newstate;
1278789Sahrens 	spa_close(spa, FTAG);
1279789Sahrens 	return (error);
1280789Sahrens }
1281789Sahrens 
1282789Sahrens static int
1283789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1284789Sahrens {
1285789Sahrens 	spa_t *spa;
1286789Sahrens 	int replacing = zc->zc_cookie;
1287789Sahrens 	nvlist_t *config;
1288789Sahrens 	int error;
1289789Sahrens 
12902926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1291789Sahrens 		return (error);
1292789Sahrens 
12935094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12949643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
12951544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1296789Sahrens 		nvlist_free(config);
1297789Sahrens 	}
1298789Sahrens 
1299789Sahrens 	spa_close(spa, FTAG);
1300789Sahrens 	return (error);
1301789Sahrens }
1302789Sahrens 
1303789Sahrens static int
1304789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1305789Sahrens {
1306789Sahrens 	spa_t *spa;
1307789Sahrens 	int error;
1308789Sahrens 
13092926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1310789Sahrens 		return (error);
1311789Sahrens 
13128241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1313789Sahrens 
1314789Sahrens 	spa_close(spa, FTAG);
1315789Sahrens 	return (error);
1316789Sahrens }
1317789Sahrens 
1318789Sahrens static int
13191354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13201354Seschrock {
13211354Seschrock 	spa_t *spa;
13222676Seschrock 	char *path = zc->zc_value;
13231544Seschrock 	uint64_t guid = zc->zc_guid;
13241354Seschrock 	int error;
13251354Seschrock 
13261354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13271354Seschrock 	if (error != 0)
13281354Seschrock 		return (error);
13291354Seschrock 
13301354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13311354Seschrock 	spa_close(spa, FTAG);
13321354Seschrock 	return (error);
13331354Seschrock }
13341354Seschrock 
13359425SEric.Schrock@Sun.COM static int
13369425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13379425SEric.Schrock@Sun.COM {
13389425SEric.Schrock@Sun.COM 	spa_t *spa;
13399425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
13409425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
13419425SEric.Schrock@Sun.COM 	int error;
13429425SEric.Schrock@Sun.COM 
13439425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
13449425SEric.Schrock@Sun.COM 	if (error != 0)
13459425SEric.Schrock@Sun.COM 		return (error);
13469425SEric.Schrock@Sun.COM 
13479425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
13489425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
13499425SEric.Schrock@Sun.COM 	return (error);
13509425SEric.Schrock@Sun.COM }
13519425SEric.Schrock@Sun.COM 
13525367Sahrens /*
13535367Sahrens  * inputs:
13545367Sahrens  * zc_name		name of filesystem
13555367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13565367Sahrens  *
13575367Sahrens  * outputs:
13585367Sahrens  * zc_objset_stats	stats
13595367Sahrens  * zc_nvlist_dst	property nvlist
13605367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13615367Sahrens  */
13621354Seschrock static int
1363789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1364789Sahrens {
1365789Sahrens 	objset_t *os = NULL;
1366789Sahrens 	int error;
13671356Seschrock 	nvlist_t *nv;
1368789Sahrens 
13696689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13706689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1371789Sahrens 		return (error);
1372789Sahrens 
13732885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1374789Sahrens 
13752856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13766689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13772885Sahrens 		dmu_objset_stats(os, nv);
13783087Sahrens 		/*
13795147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13803087Sahrens 		 * which we aren't supposed to do with a
13816689Smaybee 		 * DS_MODE_USER hold, because it could be
13823087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
13833087Sahrens 		 */
13844577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13854577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13864577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13874577Sahrens 		}
13882676Seschrock 		error = put_nvlist(zc, nv);
13891356Seschrock 		nvlist_free(nv);
13901356Seschrock 	}
1391789Sahrens 
1392789Sahrens 	dmu_objset_close(os);
1393789Sahrens 	return (error);
1394789Sahrens }
1395789Sahrens 
13965498Stimh static int
13975498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
13985498Stimh {
13995498Stimh 	uint64_t value;
14005498Stimh 	int error;
14015498Stimh 
14025498Stimh 	/*
14035498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14045498Stimh 	 * the default value (if there is one).
14055498Stimh 	 */
14065498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14075498Stimh 		return (error);
14085498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14095498Stimh 	return (0);
14105498Stimh }
14115498Stimh 
14125498Stimh /*
14135498Stimh  * inputs:
14145498Stimh  * zc_name		name of filesystem
14155498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14165498Stimh  *
14175498Stimh  * outputs:
14185498Stimh  * zc_nvlist_dst	zpl property nvlist
14195498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14205498Stimh  */
14215498Stimh static int
14225498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14235498Stimh {
14245498Stimh 	objset_t *os;
14255498Stimh 	int err;
14265498Stimh 
14276689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
14286689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
14295498Stimh 		return (err);
14305498Stimh 
14315498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14325498Stimh 
14335498Stimh 	/*
14345498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14356689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14366689Smaybee 	 * hold, because it could be inconsistent.
14375498Stimh 	 */
14385498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14395498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14405498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14415498Stimh 		nvlist_t *nv;
14425498Stimh 
14435498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14445498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14455498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14465498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14475498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14485498Stimh 			err = put_nvlist(zc, nv);
14495498Stimh 		nvlist_free(nv);
14505498Stimh 	} else {
14515498Stimh 		err = ENOENT;
14525498Stimh 	}
14535498Stimh 	dmu_objset_close(os);
14545498Stimh 	return (err);
14555498Stimh }
14565498Stimh 
14579396SMatthew.Ahrens@Sun.COM static boolean_t
14589396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14599396SMatthew.Ahrens@Sun.COM {
14609396SMatthew.Ahrens@Sun.COM 	/*
14619396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14629396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14639396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14649396SMatthew.Ahrens@Sun.COM 	 */
14659396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14669396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14679396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14689396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14699396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14709396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14719396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14729396SMatthew.Ahrens@Sun.COM }
14739396SMatthew.Ahrens@Sun.COM 
14745367Sahrens /*
14755367Sahrens  * inputs:
14765367Sahrens  * zc_name		name of filesystem
14775367Sahrens  * zc_cookie		zap cursor
14785367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14795367Sahrens  *
14805367Sahrens  * outputs:
14815367Sahrens  * zc_name		name of next filesystem
14829396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14835367Sahrens  * zc_objset_stats	stats
14845367Sahrens  * zc_nvlist_dst	property nvlist
14855367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14865367Sahrens  */
1487789Sahrens static int
1488789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1489789Sahrens {
1490885Sahrens 	objset_t *os;
1491789Sahrens 	int error;
1492789Sahrens 	char *p;
1493789Sahrens 
14946689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
14956689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1496885Sahrens 		if (error == ENOENT)
1497885Sahrens 			error = ESRCH;
1498885Sahrens 		return (error);
1499789Sahrens 	}
1500789Sahrens 
1501789Sahrens 	p = strrchr(zc->zc_name, '/');
1502789Sahrens 	if (p == NULL || p[1] != '\0')
1503789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1504789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1505789Sahrens 
15068697SRichard.Morris@Sun.COM 	/*
15078697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15088697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15098697SRichard.Morris@Sun.COM 	 */
15108415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15118415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15128415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15138415SRichard.Morris@Sun.COM 
15148415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15158697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15168415SRichard.Morris@Sun.COM 	}
15178415SRichard.Morris@Sun.COM 
1518789Sahrens 	do {
1519885Sahrens 		error = dmu_dir_list_next(os,
1520885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1521885Sahrens 		    NULL, &zc->zc_cookie);
1522789Sahrens 		if (error == ENOENT)
1523789Sahrens 			error = ESRCH;
15249396SMatthew.Ahrens@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
15256689Smaybee 	dmu_objset_close(os);
1526789Sahrens 
15279396SMatthew.Ahrens@Sun.COM 	if (error == 0)
1528885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1529789Sahrens 
1530789Sahrens 	return (error);
1531789Sahrens }
1532789Sahrens 
15335367Sahrens /*
15345367Sahrens  * inputs:
15355367Sahrens  * zc_name		name of filesystem
15365367Sahrens  * zc_cookie		zap cursor
15375367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15385367Sahrens  *
15395367Sahrens  * outputs:
15405367Sahrens  * zc_name		name of next snapshot
15415367Sahrens  * zc_objset_stats	stats
15425367Sahrens  * zc_nvlist_dst	property nvlist
15435367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15445367Sahrens  */
1545789Sahrens static int
1546789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1547789Sahrens {
1548885Sahrens 	objset_t *os;
1549789Sahrens 	int error;
1550789Sahrens 
15516689Smaybee 	error = dmu_objset_open(zc->zc_name,
15526689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
15536689Smaybee 	if (error)
15546689Smaybee 		return (error == ENOENT ? ESRCH : error);
1555789Sahrens 
15569396SMatthew.Ahrens@Sun.COM 	if (zc->zc_cookie == 0) {
15578697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15588415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
15599396SMatthew.Ahrens@Sun.COM 	}
15601003Slling 	/*
15611003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15621003Slling 	 * so exit immediately.
15631003Slling 	 */
15641003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1565885Sahrens 		dmu_objset_close(os);
15661003Slling 		return (ESRCH);
1567789Sahrens 	}
1568789Sahrens 
1569885Sahrens 	error = dmu_snapshot_list_next(os,
1570885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15715663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
15726689Smaybee 	dmu_objset_close(os);
1573885Sahrens 	if (error == 0)
1574885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15756689Smaybee 	else if (error == ENOENT)
15766689Smaybee 		error = ESRCH;
1577789Sahrens 
15785367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15796689Smaybee 	if (error)
15805367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1581789Sahrens 	return (error);
1582789Sahrens }
1583789Sahrens 
15846423Sgw25295 int
15854787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1586789Sahrens {
15872676Seschrock 	nvpair_t *elem;
15888724SRichard.Morris@Sun.COM 	int error = 0;
15892676Seschrock 	uint64_t intval;
15902676Seschrock 	char *strval;
15918697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
15929396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
15932676Seschrock 
15944543Smarks 	/*
15954543Smarks 	 * First validate permission to set all of the properties
15964543Smarks 	 */
15972676Seschrock 	elem = NULL;
15982676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
15994670Sahrens 		const char *propname = nvpair_name(elem);
16004670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16012676Seschrock 
16025094Slling 		if (prop == ZPROP_INVAL) {
16032676Seschrock 			/*
16042676Seschrock 			 * If this is a user-defined property, it must be a
16052676Seschrock 			 * string, and there is no further validation to do.
16062676Seschrock 			 */
16079396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16089396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16099396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16109396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16119396SMatthew.Ahrens@Sun.COM 					return (error);
16129396SMatthew.Ahrens@Sun.COM 				continue;
16139396SMatthew.Ahrens@Sun.COM 			}
16149396SMatthew.Ahrens@Sun.COM 
16159396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16169396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16179396SMatthew.Ahrens@Sun.COM 				const char *perm;
16189396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16199396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16209396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16219396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16229396SMatthew.Ahrens@Sun.COM 				else
16239396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16249396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16259396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16269396SMatthew.Ahrens@Sun.COM 					return (error);
16279396SMatthew.Ahrens@Sun.COM 				continue;
16289396SMatthew.Ahrens@Sun.COM 			}
16299396SMatthew.Ahrens@Sun.COM 
16309396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16312676Seschrock 		}
16322676Seschrock 
16339396SMatthew.Ahrens@Sun.COM 		if (issnap)
16349396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16359396SMatthew.Ahrens@Sun.COM 
16364787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16374670Sahrens 			return (error);
16382676Seschrock 
16394670Sahrens 		/*
16404670Sahrens 		 * Check that this value is valid for this pool version
16414670Sahrens 		 */
16424670Sahrens 		switch (prop) {
16433886Sahl 		case ZFS_PROP_COMPRESSION:
16443886Sahl 			/*
16453886Sahl 			 * If the user specified gzip compression, make sure
16463886Sahl 			 * the SPA supports it. We ignore any errors here since
16473886Sahl 			 * we'll catch them later.
16483886Sahl 			 */
16493886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16507042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16517042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16527042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16537184Stimh 				    zfs_earlier_version(name,
16545331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16555331Samw 					return (ENOTSUP);
16567042Sgw25295 
16577042Sgw25295 				/*
16587042Sgw25295 				 * If this is a bootable dataset then
16597042Sgw25295 				 * verify that the compression algorithm
16607042Sgw25295 				 * is supported for booting. We must return
16617042Sgw25295 				 * something other than ENOTSUP since it
16627042Sgw25295 				 * implies a downrev pool version.
16637042Sgw25295 				 */
16647042Sgw25295 				if (zfs_is_bootfs(name) &&
16657042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16667042Sgw25295 					return (ERANGE);
16673886Sahl 			}
16683886Sahl 			break;
16694603Sahrens 
16704603Sahrens 		case ZFS_PROP_COPIES:
16719396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16725331Samw 				return (ENOTSUP);
16734603Sahrens 			break;
16745977Smarks 
16755977Smarks 		case ZFS_PROP_SHARESMB:
16766689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16775977Smarks 				return (ENOTSUP);
16785977Smarks 			break;
16798053SMark.Shellenbaum@Sun.COM 
16808053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16818053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16828053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16838053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16848053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16858053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
16868053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
16874603Sahrens 		}
16884543Smarks 	}
16894543Smarks 
16908697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16914543Smarks 	elem = NULL;
16924543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16934670Sahrens 		const char *propname = nvpair_name(elem);
16944670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16954543Smarks 
16965094Slling 		if (prop == ZPROP_INVAL) {
16979396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
16989396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
16999396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
17009396SMatthew.Ahrens@Sun.COM 				const char *domain;
17019396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
17029396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17039396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17049396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17059396SMatthew.Ahrens@Sun.COM 
17069396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17079396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17089396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17099396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17109396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17119396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
17129396SMatthew.Ahrens@Sun.COM 				domain = propname +
17139396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
17149396SMatthew.Ahrens@Sun.COM 
17159396SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, B_FALSE, FTAG,
17169396SMatthew.Ahrens@Sun.COM 				    &zfsvfs);
17179396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17189396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17199396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17209396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17219396SMatthew.Ahrens@Sun.COM 				}
17229396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17239396SMatthew.Ahrens@Sun.COM 					continue;
17249396SMatthew.Ahrens@Sun.COM 				else
17259396SMatthew.Ahrens@Sun.COM 					goto out;
17269396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17279396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17289396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17299396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17309396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17319396SMatthew.Ahrens@Sun.COM 					continue;
17329396SMatthew.Ahrens@Sun.COM 				else
17339396SMatthew.Ahrens@Sun.COM 					goto out;
17349396SMatthew.Ahrens@Sun.COM 			}
17354543Smarks 		}
17362676Seschrock 
17372676Seschrock 		switch (prop) {
17382676Seschrock 		case ZFS_PROP_QUOTA:
17392676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17404577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17418697SRichard.Morris@Sun.COM 				goto out;
17422676Seschrock 			break;
17432676Seschrock 
17445378Sck153898 		case ZFS_PROP_REFQUOTA:
17455378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17465378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17478697SRichard.Morris@Sun.COM 				goto out;
17485378Sck153898 			break;
17495378Sck153898 
17502676Seschrock 		case ZFS_PROP_RESERVATION:
17512676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17522676Seschrock 			    (error = dsl_dir_set_reservation(name,
17532676Seschrock 			    intval)) != 0)
17548697SRichard.Morris@Sun.COM 				goto out;
17552676Seschrock 			break;
1756789Sahrens 
17575378Sck153898 		case ZFS_PROP_REFRESERVATION:
17585378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17595378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17605378Sck153898 			    intval)) != 0)
17618697SRichard.Morris@Sun.COM 				goto out;
17625378Sck153898 			break;
17635378Sck153898 
17642676Seschrock 		case ZFS_PROP_VOLSIZE:
17652676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17664787Sahrens 			    (error = zvol_set_volsize(name,
17674787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17688697SRichard.Morris@Sun.COM 				goto out;
17692676Seschrock 			break;
17702676Seschrock 
17712676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
17722676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17734577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
17748697SRichard.Morris@Sun.COM 				goto out;
17754577Sahrens 			break;
17764577Sahrens 
17774577Sahrens 		case ZFS_PROP_VERSION:
17789396SMatthew.Ahrens@Sun.COM 		{
17799396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17809396SMatthew.Ahrens@Sun.COM 
17819396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17829396SMatthew.Ahrens@Sun.COM 				goto out;
17839396SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, B_FALSE, FTAG,
17849396SMatthew.Ahrens@Sun.COM 			    &zfsvfs)) != 0)
17859396SMatthew.Ahrens@Sun.COM 				goto out;
17869396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
17879396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
17889396SMatthew.Ahrens@Sun.COM 
17899396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
17909396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
17919396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
17929396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
17939396SMatthew.Ahrens@Sun.COM 			}
17949396SMatthew.Ahrens@Sun.COM 			if (error)
17958697SRichard.Morris@Sun.COM 				goto out;
17962676Seschrock 			break;
17979396SMatthew.Ahrens@Sun.COM 		}
17982676Seschrock 
17992676Seschrock 		default:
18002676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
18012676Seschrock 				if (zfs_prop_get_type(prop) !=
18028697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18038697SRichard.Morris@Sun.COM 					error = EINVAL;
18048697SRichard.Morris@Sun.COM 					goto out;
18058697SRichard.Morris@Sun.COM 				}
18062676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18072885Sahrens 				const char *unused;
18082885Sahrens 
18092717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18102676Seschrock 
18112676Seschrock 				switch (zfs_prop_get_type(prop)) {
18124787Sahrens 				case PROP_TYPE_NUMBER:
18132676Seschrock 					break;
18144787Sahrens 				case PROP_TYPE_STRING:
18158697SRichard.Morris@Sun.COM 					error = EINVAL;
18168697SRichard.Morris@Sun.COM 					goto out;
18174787Sahrens 				case PROP_TYPE_INDEX:
18182717Seschrock 					if (zfs_prop_index_to_string(prop,
18198697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18208697SRichard.Morris@Sun.COM 						error = EINVAL;
18218697SRichard.Morris@Sun.COM 						goto out;
18228697SRichard.Morris@Sun.COM 					}
18232676Seschrock 					break;
18242676Seschrock 				default:
18254577Sahrens 					cmn_err(CE_PANIC,
18264577Sahrens 					    "unknown property type");
18272676Seschrock 					break;
18282676Seschrock 				}
18292676Seschrock 			} else {
18308697SRichard.Morris@Sun.COM 				error = EINVAL;
18318697SRichard.Morris@Sun.COM 				goto out;
18322676Seschrock 			}
18338697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18348697SRichard.Morris@Sun.COM 				goto out;
18352676Seschrock 		}
18362676Seschrock 	}
18372676Seschrock 
18388697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18398697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18408697SRichard.Morris@Sun.COM 	}
18418697SRichard.Morris@Sun.COM out:
18428697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18438697SRichard.Morris@Sun.COM 	return (error);
1844789Sahrens }
1845789Sahrens 
18465367Sahrens /*
18479355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18489355SMatthew.Ahrens@Sun.COM  */
18499355SMatthew.Ahrens@Sun.COM static int
18509355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18519355SMatthew.Ahrens@Sun.COM {
18529355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18539355SMatthew.Ahrens@Sun.COM 	int error = 0;
18549355SMatthew.Ahrens@Sun.COM 
18559355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18569355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18579355SMatthew.Ahrens@Sun.COM 		char *valstr;
18589355SMatthew.Ahrens@Sun.COM 
18599355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18609355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18619355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18629355SMatthew.Ahrens@Sun.COM 
18639355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18649355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18659355SMatthew.Ahrens@Sun.COM 			return (error);
18669355SMatthew.Ahrens@Sun.COM 
18679355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18689355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18699355SMatthew.Ahrens@Sun.COM 
18709355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18719355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18729355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18739355SMatthew.Ahrens@Sun.COM 	}
18749355SMatthew.Ahrens@Sun.COM 	return (0);
18759355SMatthew.Ahrens@Sun.COM }
18769355SMatthew.Ahrens@Sun.COM 
18779355SMatthew.Ahrens@Sun.COM /*
18785367Sahrens  * inputs:
18795367Sahrens  * zc_name		name of filesystem
18808697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18815367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18827265Sahrens  * zc_cookie		clear existing local props?
18835367Sahrens  *
18845367Sahrens  * outputs:		none
18855367Sahrens  */
1886789Sahrens static int
18872676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1888789Sahrens {
18892676Seschrock 	nvlist_t *nvl;
18902676Seschrock 	int error;
1891789Sahrens 
18925094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18939643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
18942676Seschrock 		return (error);
18952676Seschrock 
18967265Sahrens 	if (zc->zc_cookie) {
18977265Sahrens 		nvlist_t *origprops;
18987265Sahrens 		objset_t *os;
18997265Sahrens 
19007265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
19017265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
19027265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19038536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19047265Sahrens 				nvlist_free(origprops);
19057265Sahrens 			}
19067265Sahrens 			dmu_objset_close(os);
19077265Sahrens 		}
19087265Sahrens 
19097265Sahrens 	}
19107265Sahrens 
19114787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19124543Smarks 
19132676Seschrock 	nvlist_free(nvl);
19142676Seschrock 	return (error);
1915789Sahrens }
1916789Sahrens 
19175367Sahrens /*
19185367Sahrens  * inputs:
19195367Sahrens  * zc_name		name of filesystem
19205367Sahrens  * zc_value		name of property to inherit
19215367Sahrens  *
19225367Sahrens  * outputs:		none
19235367Sahrens  */
1924789Sahrens static int
19254849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19264849Sahrens {
19274849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19284849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19294849Sahrens }
19304849Sahrens 
19314849Sahrens static int
19324098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19333912Slling {
19345094Slling 	nvlist_t *props;
19353912Slling 	spa_t *spa;
19365094Slling 	int error;
19378525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19383912Slling 
19395094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19409643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)))
19413912Slling 		return (error);
19423912Slling 
19438525SEric.Schrock@Sun.COM 	/*
19448525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19458525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19468525SEric.Schrock@Sun.COM 	 */
19478525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19488525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19498525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19508525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19518525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19528525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19538525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19548525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19558525SEric.Schrock@Sun.COM 		}
19568525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19578525SEric.Schrock@Sun.COM 		if (spa != NULL)
19588525SEric.Schrock@Sun.COM 			return (0);
19598525SEric.Schrock@Sun.COM 	}
19608525SEric.Schrock@Sun.COM 
19613912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19625094Slling 		nvlist_free(props);
19633912Slling 		return (error);
19643912Slling 	}
19653912Slling 
19665094Slling 	error = spa_prop_set(spa, props);
19673912Slling 
19685094Slling 	nvlist_free(props);
19693912Slling 	spa_close(spa, FTAG);
19703912Slling 
19713912Slling 	return (error);
19723912Slling }
19733912Slling 
19743912Slling static int
19754098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19763912Slling {
19773912Slling 	spa_t *spa;
19783912Slling 	int error;
19793912Slling 	nvlist_t *nvp = NULL;
19803912Slling 
19818525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19828525SEric.Schrock@Sun.COM 		/*
19838525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19848525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19858525SEric.Schrock@Sun.COM 		 * anyway.
19868525SEric.Schrock@Sun.COM 		 */
19878525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19888525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
19898525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
19908525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19918525SEric.Schrock@Sun.COM 	} else {
19928525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
19938525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
19948525SEric.Schrock@Sun.COM 	}
19953912Slling 
19963912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
19973912Slling 		error = put_nvlist(zc, nvp);
19983912Slling 	else
19993912Slling 		error = EFAULT;
20003912Slling 
20018525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
20023912Slling 	return (error);
20033912Slling }
20043912Slling 
20053912Slling static int
20064543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20074543Smarks {
20084543Smarks 	nvlist_t *nvp;
20094543Smarks 	int error;
20104543Smarks 	uint32_t uid;
20114543Smarks 	uint32_t gid;
20124543Smarks 	uint32_t *groups;
20134543Smarks 	uint_t group_cnt;
20144543Smarks 	cred_t	*usercred;
20154543Smarks 
20165094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20179643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
20184543Smarks 		return (error);
20194543Smarks 	}
20204543Smarks 
20214543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20224543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20234543Smarks 		nvlist_free(nvp);
20244543Smarks 		return (EPERM);
20254543Smarks 	}
20264543Smarks 
20274543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20284543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20294543Smarks 		nvlist_free(nvp);
20304543Smarks 		return (EPERM);
20314543Smarks 	}
20324543Smarks 
20334543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20344543Smarks 	    &groups, &group_cnt)) != 0) {
20354543Smarks 		nvlist_free(nvp);
20364543Smarks 		return (EPERM);
20374543Smarks 	}
20384543Smarks 	usercred = cralloc();
20394543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20404543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20414543Smarks 		nvlist_free(nvp);
20424543Smarks 		crfree(usercred);
20434543Smarks 		return (EPERM);
20444543Smarks 	}
20454543Smarks 	nvlist_free(nvp);
20464543Smarks 	error = dsl_deleg_access(zc->zc_name,
20474787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20484543Smarks 	crfree(usercred);
20494543Smarks 	return (error);
20504543Smarks }
20514543Smarks 
20525367Sahrens /*
20535367Sahrens  * inputs:
20545367Sahrens  * zc_name		name of filesystem
20555367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20565367Sahrens  * zc_perm_action	allow/unallow flag
20575367Sahrens  *
20585367Sahrens  * outputs:		none
20595367Sahrens  */
20604543Smarks static int
20614543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20624543Smarks {
20634543Smarks 	int error;
20644543Smarks 	nvlist_t *fsaclnv = NULL;
20654543Smarks 
20665094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20679643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
20684543Smarks 		return (error);
20694543Smarks 
20704543Smarks 	/*
20714543Smarks 	 * Verify nvlist is constructed correctly
20724543Smarks 	 */
20734543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20744543Smarks 		nvlist_free(fsaclnv);
20754543Smarks 		return (EINVAL);
20764543Smarks 	}
20774543Smarks 
20784543Smarks 	/*
20794543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20804543Smarks 	 * that user is allowed to hand out each permission in
20814543Smarks 	 * the nvlist(s)
20824543Smarks 	 */
20834543Smarks 
20844787Sahrens 	error = secpolicy_zfs(CRED());
20854543Smarks 	if (error) {
20864787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
20874787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
20884787Sahrens 			    fsaclnv, CRED());
20894787Sahrens 		} else {
20904787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
20914787Sahrens 			    fsaclnv, CRED());
20924787Sahrens 		}
20934543Smarks 	}
20944543Smarks 
20954543Smarks 	if (error == 0)
20964543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
20974543Smarks 
20984543Smarks 	nvlist_free(fsaclnv);
20994543Smarks 	return (error);
21004543Smarks }
21014543Smarks 
21025367Sahrens /*
21035367Sahrens  * inputs:
21045367Sahrens  * zc_name		name of filesystem
21055367Sahrens  *
21065367Sahrens  * outputs:
21075367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21085367Sahrens  */
21094543Smarks static int
21104543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21114543Smarks {
21124543Smarks 	nvlist_t *nvp;
21134543Smarks 	int error;
21144543Smarks 
21154543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21164543Smarks 		error = put_nvlist(zc, nvp);
21174543Smarks 		nvlist_free(nvp);
21184543Smarks 	}
21194543Smarks 
21204543Smarks 	return (error);
21214543Smarks }
21224543Smarks 
21235367Sahrens /*
21245367Sahrens  * inputs:
21255367Sahrens  * zc_name		name of volume
21265367Sahrens  *
21275367Sahrens  * outputs:		none
21285367Sahrens  */
21294543Smarks static int
2130789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
2131789Sahrens {
21324787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2133789Sahrens }
2134789Sahrens 
21355367Sahrens /*
21365367Sahrens  * inputs:
21375367Sahrens  * zc_name		name of volume
21385367Sahrens  *
21395367Sahrens  * outputs:		none
21405367Sahrens  */
2141789Sahrens static int
2142789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
2143789Sahrens {
21442676Seschrock 	return (zvol_remove_minor(zc->zc_name));
2145789Sahrens }
2146789Sahrens 
2147789Sahrens /*
2148789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2149789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2150789Sahrens  * is responsible for releasing the returned vfs pointer.
2151789Sahrens  */
2152789Sahrens static vfs_t *
2153789Sahrens zfs_get_vfs(const char *resource)
2154789Sahrens {
2155789Sahrens 	struct vfs *vfsp;
2156789Sahrens 	struct vfs *vfs_found = NULL;
2157789Sahrens 
2158789Sahrens 	vfs_list_read_lock();
2159789Sahrens 	vfsp = rootvfs;
2160789Sahrens 	do {
2161789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2162789Sahrens 			VFS_HOLD(vfsp);
2163789Sahrens 			vfs_found = vfsp;
2164789Sahrens 			break;
2165789Sahrens 		}
2166789Sahrens 		vfsp = vfsp->vfs_next;
2167789Sahrens 	} while (vfsp != rootvfs);
2168789Sahrens 	vfs_list_unlock();
2169789Sahrens 	return (vfs_found);
2170789Sahrens }
2171789Sahrens 
21724543Smarks /* ARGSUSED */
2173789Sahrens static void
21744543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2175789Sahrens {
21765331Samw 	zfs_creat_t *zct = arg;
21775498Stimh 
21785498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21795331Samw }
21805331Samw 
21815498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21825498Stimh 
21835331Samw /*
21845498Stimh  * inputs:
21857184Stimh  * createprops		list of properties requested by creator
21867184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21877184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21887184Stimh  * os			parent objset pointer (NULL if root fs)
21895331Samw  *
21905498Stimh  * outputs:
21915498Stimh  * zplprops	values for the zplprops we attach to the master node object
21927184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21935331Samw  *
21945498Stimh  * Determine the settings for utf8only, normalization and
21955498Stimh  * casesensitivity.  Specific values may have been requested by the
21965498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21975498Stimh  * the file system is of too early a vintage, a creator can not
21985498Stimh  * request settings for these properties, even if the requested
21995498Stimh  * setting is the default value.  We don't actually want to create dsl
22005498Stimh  * properties for these, so remove them from the source nvlist after
22015498Stimh  * processing.
22025331Samw  */
22035331Samw static int
22049396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
22057184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
22067184Stimh     boolean_t *is_ci)
22075331Samw {
22085498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
22095498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
22105498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
22115498Stimh 
22125498Stimh 	ASSERT(zplprops != NULL);
22135498Stimh 
22145375Stimh 	/*
22155498Stimh 	 * Pull out creator prop choices, if any.
22165375Stimh 	 */
22175498Stimh 	if (createprops) {
22185498Stimh 		(void) nvlist_lookup_uint64(createprops,
22197184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22207184Stimh 		(void) nvlist_lookup_uint64(createprops,
22215498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22225498Stimh 		(void) nvlist_remove_all(createprops,
22235498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22245498Stimh 		(void) nvlist_lookup_uint64(createprops,
22255498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22265498Stimh 		(void) nvlist_remove_all(createprops,
22275498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22285498Stimh 		(void) nvlist_lookup_uint64(createprops,
22295498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22305498Stimh 		(void) nvlist_remove_all(createprops,
22315498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22325331Samw 	}
22335331Samw 
22345375Stimh 	/*
22357184Stimh 	 * If the zpl version requested is whacky or the file system
22367184Stimh 	 * or pool is version is too "young" to support normalization
22377184Stimh 	 * and the creator tried to set a value for one of the props,
22387184Stimh 	 * error out.
22395498Stimh 	 */
22407184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22417184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22427184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22435498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22447184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22455498Stimh 		return (ENOTSUP);
22465498Stimh 
22475498Stimh 	/*
22485498Stimh 	 * Put the version in the zplprops
22495498Stimh 	 */
22505498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22515498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22525498Stimh 
22535498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22545498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22555498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22565498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22575498Stimh 
22585498Stimh 	/*
22595498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22605498Stimh 	 */
22615498Stimh 	if (norm)
22625498Stimh 		u8 = 1;
22635498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22645498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22655498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22665498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22675498Stimh 
22685498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22695498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22705498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22715498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22725498Stimh 
22736492Stimh 	if (is_ci)
22746492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22756492Stimh 
22767184Stimh 	return (0);
22777184Stimh }
22787184Stimh 
22797184Stimh static int
22807184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22817184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22827184Stimh {
22837184Stimh 	boolean_t fuids_ok = B_TRUE;
22847184Stimh 	uint64_t zplver = ZPL_VERSION;
22857184Stimh 	objset_t *os = NULL;
22867184Stimh 	char parentname[MAXNAMELEN];
22877184Stimh 	char *cp;
22887184Stimh 	int error;
22897184Stimh 
22907184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22917184Stimh 	cp = strrchr(parentname, '/');
22927184Stimh 	ASSERT(cp != NULL);
22937184Stimh 	cp[0] = '\0';
22947184Stimh 
22959396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
22969396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22977184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22987184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22997184Stimh 		fuids_ok = B_FALSE;
23007184Stimh 	}
23017184Stimh 
23027184Stimh 	/*
23037184Stimh 	 * Open parent object set so we can inherit zplprop values.
23047184Stimh 	 */
23057184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
23067184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
23077184Stimh 		return (error);
23087184Stimh 
23097184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
23107184Stimh 	    zplprops, is_ci);
23115498Stimh 	dmu_objset_close(os);
23127184Stimh 	return (error);
23137184Stimh }
23147184Stimh 
23157184Stimh static int
23167184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
23177184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
23187184Stimh {
23197184Stimh 	boolean_t fuids_ok = B_TRUE;
23207184Stimh 	uint64_t zplver = ZPL_VERSION;
23217184Stimh 	int error;
23227184Stimh 
23237184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23247184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23257184Stimh 		fuids_ok = B_FALSE;
23267184Stimh 	}
23277184Stimh 
23287184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23297184Stimh 	    zplprops, is_ci);
23307184Stimh 	return (error);
2331789Sahrens }
2332789Sahrens 
23335367Sahrens /*
23345367Sahrens  * inputs:
23355367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23365367Sahrens  * zc_name		name of new objset
23375367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23385367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23395367Sahrens  *
23405498Stimh  * outputs: none
23415367Sahrens  */
2342789Sahrens static int
2343789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2344789Sahrens {
2345789Sahrens 	objset_t *clone;
2346789Sahrens 	int error = 0;
23475331Samw 	zfs_creat_t zct;
23484543Smarks 	nvlist_t *nvprops = NULL;
23494543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2350789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2351789Sahrens 
2352789Sahrens 	switch (type) {
2353789Sahrens 
2354789Sahrens 	case DMU_OST_ZFS:
2355789Sahrens 		cbfunc = zfs_create_cb;
2356789Sahrens 		break;
2357789Sahrens 
2358789Sahrens 	case DMU_OST_ZVOL:
2359789Sahrens 		cbfunc = zvol_create_cb;
2360789Sahrens 		break;
2361789Sahrens 
2362789Sahrens 	default:
23632199Sahrens 		cbfunc = NULL;
23646423Sgw25295 		break;
23652199Sahrens 	}
23665326Sek110237 	if (strchr(zc->zc_name, '@') ||
23675326Sek110237 	    strchr(zc->zc_name, '%'))
2368789Sahrens 		return (EINVAL);
2369789Sahrens 
23702676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23715094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23729643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
23732676Seschrock 		return (error);
23742676Seschrock 
23755498Stimh 	zct.zct_zplprops = NULL;
23765331Samw 	zct.zct_props = nvprops;
23775331Samw 
23782676Seschrock 	if (zc->zc_value[0] != '\0') {
2379789Sahrens 		/*
2380789Sahrens 		 * We're creating a clone of an existing snapshot.
2381789Sahrens 		 */
23822676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23832676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23844543Smarks 			nvlist_free(nvprops);
2385789Sahrens 			return (EINVAL);
23862676Seschrock 		}
2387789Sahrens 
23882676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
23896689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
23902676Seschrock 		if (error) {
23914543Smarks 			nvlist_free(nvprops);
2392789Sahrens 			return (error);
23932676Seschrock 		}
23946492Stimh 
23956492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
23966492Stimh 		    NULL, NULL);
23975331Samw 		if (error) {
23985331Samw 			dmu_objset_close(clone);
23995331Samw 			nvlist_free(nvprops);
24005331Samw 			return (error);
24015331Samw 		}
2402789Sahrens 		dmu_objset_close(clone);
2403789Sahrens 	} else {
24046492Stimh 		boolean_t is_insensitive = B_FALSE;
24056492Stimh 
24062676Seschrock 		if (cbfunc == NULL) {
24074543Smarks 			nvlist_free(nvprops);
24082199Sahrens 			return (EINVAL);
24092676Seschrock 		}
24102676Seschrock 
2411789Sahrens 		if (type == DMU_OST_ZVOL) {
24122676Seschrock 			uint64_t volsize, volblocksize;
24132676Seschrock 
24144543Smarks 			if (nvprops == NULL ||
24154543Smarks 			    nvlist_lookup_uint64(nvprops,
24162676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
24172676Seschrock 			    &volsize) != 0) {
24184543Smarks 				nvlist_free(nvprops);
24192676Seschrock 				return (EINVAL);
24202676Seschrock 			}
24212676Seschrock 
24224543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24232676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24242676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24254543Smarks 				nvlist_free(nvprops);
24262676Seschrock 				return (EINVAL);
24272676Seschrock 			}
24281133Seschrock 
24292676Seschrock 			if (error != 0)
24302676Seschrock 				volblocksize = zfs_prop_default_numeric(
24312676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24322676Seschrock 
24332676Seschrock 			if ((error = zvol_check_volblocksize(
24342676Seschrock 			    volblocksize)) != 0 ||
24352676Seschrock 			    (error = zvol_check_volsize(volsize,
24362676Seschrock 			    volblocksize)) != 0) {
24374543Smarks 				nvlist_free(nvprops);
2438789Sahrens 				return (error);
24392676Seschrock 			}
24404577Sahrens 		} else if (type == DMU_OST_ZFS) {
24415331Samw 			int error;
24425331Samw 
24435498Stimh 			/*
24445331Samw 			 * We have to have normalization and
24455331Samw 			 * case-folding flags correct when we do the
24465331Samw 			 * file system creation, so go figure them out
24475498Stimh 			 * now.
24485331Samw 			 */
24495498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24505498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24515498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24527184Stimh 			    zct.zct_zplprops, &is_insensitive);
24535331Samw 			if (error != 0) {
24545331Samw 				nvlist_free(nvprops);
24555498Stimh 				nvlist_free(zct.zct_zplprops);
24565331Samw 				return (error);
24574577Sahrens 			}
24582676Seschrock 		}
24596492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
24606492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24615498Stimh 		nvlist_free(zct.zct_zplprops);
2462789Sahrens 	}
24632676Seschrock 
24642676Seschrock 	/*
24652676Seschrock 	 * It would be nice to do this atomically.
24662676Seschrock 	 */
24672676Seschrock 	if (error == 0) {
24684787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
24692676Seschrock 			(void) dmu_objset_destroy(zc->zc_name);
24702676Seschrock 	}
24714543Smarks 	nvlist_free(nvprops);
2472789Sahrens 	return (error);
2473789Sahrens }
2474789Sahrens 
24755367Sahrens /*
24765367Sahrens  * inputs:
24775367Sahrens  * zc_name	name of filesystem
24785367Sahrens  * zc_value	short name of snapshot
24795367Sahrens  * zc_cookie	recursive flag
24809396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24815367Sahrens  *
24825367Sahrens  * outputs:	none
24835367Sahrens  */
2484789Sahrens static int
24852199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24862199Sahrens {
24877265Sahrens 	nvlist_t *nvprops = NULL;
24887265Sahrens 	int error;
24897265Sahrens 	boolean_t recursive = zc->zc_cookie;
24907265Sahrens 
24912676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24922199Sahrens 		return (EINVAL);
24937265Sahrens 
24947265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24957265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24969643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
24977265Sahrens 		return (error);
24987265Sahrens 
24999355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
25009355SMatthew.Ahrens@Sun.COM 	if (error)
25019355SMatthew.Ahrens@Sun.COM 		goto out;
25029355SMatthew.Ahrens@Sun.COM 
25039355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
25049355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
25059355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
25069355SMatthew.Ahrens@Sun.COM 		goto out;
25077265Sahrens 	}
25089355SMatthew.Ahrens@Sun.COM 
25099355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
25109355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
25119355SMatthew.Ahrens@Sun.COM 
25129355SMatthew.Ahrens@Sun.COM out:
25137265Sahrens 	nvlist_free(nvprops);
25147265Sahrens 	return (error);
25152199Sahrens }
25162199Sahrens 
25174007Smmusante int
25182199Sahrens zfs_unmount_snap(char *name, void *arg)
2519789Sahrens {
25202417Sahrens 	vfs_t *vfsp = NULL;
25212199Sahrens 
25226689Smaybee 	if (arg) {
25236689Smaybee 		char *snapname = arg;
25246689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25256689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25266689Smaybee 
25276689Smaybee 		(void) strcpy(buf, name);
25286689Smaybee 		(void) strcat(buf, "@");
25296689Smaybee 		(void) strcat(buf, snapname);
25306689Smaybee 		vfsp = zfs_get_vfs(buf);
25316689Smaybee 		kmem_free(buf, len);
25322417Sahrens 	} else if (strchr(name, '@')) {
25332199Sahrens 		vfsp = zfs_get_vfs(name);
25342199Sahrens 	}
25352199Sahrens 
25362199Sahrens 	if (vfsp) {
25372199Sahrens 		/*
25382199Sahrens 		 * Always force the unmount for snapshots.
25392199Sahrens 		 */
25402199Sahrens 		int flag = MS_FORCE;
2541789Sahrens 		int err;
2542789Sahrens 
25432199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25442199Sahrens 			VFS_RELE(vfsp);
25452199Sahrens 			return (err);
25462199Sahrens 		}
25472199Sahrens 		VFS_RELE(vfsp);
25482199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25492199Sahrens 			return (err);
25502199Sahrens 	}
25512199Sahrens 	return (0);
25522199Sahrens }
25532199Sahrens 
25545367Sahrens /*
25555367Sahrens  * inputs:
25565367Sahrens  * zc_name	name of filesystem
25575367Sahrens  * zc_value	short name of snapshot
25585367Sahrens  *
25595367Sahrens  * outputs:	none
25605367Sahrens  */
25612199Sahrens static int
25622199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25632199Sahrens {
25642199Sahrens 	int err;
2565789Sahrens 
25662676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25672199Sahrens 		return (EINVAL);
25682199Sahrens 	err = dmu_objset_find(zc->zc_name,
25692676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25702199Sahrens 	if (err)
25712199Sahrens 		return (err);
25722676Seschrock 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
25732199Sahrens }
25742199Sahrens 
25755367Sahrens /*
25765367Sahrens  * inputs:
25775367Sahrens  * zc_name		name of dataset to destroy
25785367Sahrens  * zc_objset_type	type of objset
25795367Sahrens  *
25805367Sahrens  * outputs:		none
25815367Sahrens  */
25822199Sahrens static int
25832199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25842199Sahrens {
25852199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
25862199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
25872199Sahrens 		if (err)
25882199Sahrens 			return (err);
2589789Sahrens 	}
2590789Sahrens 
2591789Sahrens 	return (dmu_objset_destroy(zc->zc_name));
2592789Sahrens }
2593789Sahrens 
25945367Sahrens /*
25955367Sahrens  * inputs:
25965446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25975367Sahrens  *
25985367Sahrens  * outputs:	none
25995367Sahrens  */
2600789Sahrens static int
2601789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2602789Sahrens {
26035446Sahrens 	objset_t *os;
26045446Sahrens 	int error;
26055446Sahrens 	zfsvfs_t *zfsvfs = NULL;
26065446Sahrens 
26075446Sahrens 	/*
26085446Sahrens 	 * Get the zfsvfs for the receiving objset. There
26095446Sahrens 	 * won't be one if we're operating on a zvol, if the
26105446Sahrens 	 * objset doesn't exist yet, or is not mounted.
26115446Sahrens 	 */
26126689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
26135446Sahrens 	if (error)
26145446Sahrens 		return (error);
26155446Sahrens 
26169396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
26175446Sahrens 		int mode;
26185446Sahrens 
26199396SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs, NULL, &mode);
26206083Sek110237 		if (error == 0) {
26216083Sek110237 			int resume_err;
26226083Sek110237 
26236083Sek110237 			error = dmu_objset_rollback(os);
26249396SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
26256083Sek110237 			error = error ? error : resume_err;
26266083Sek110237 		} else {
26276083Sek110237 			dmu_objset_close(os);
26286083Sek110237 		}
26295446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26305446Sahrens 	} else {
26315446Sahrens 		error = dmu_objset_rollback(os);
26325446Sahrens 	}
26336689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
26345446Sahrens 
26355446Sahrens 	return (error);
2636789Sahrens }
2637789Sahrens 
26385367Sahrens /*
26395367Sahrens  * inputs:
26405367Sahrens  * zc_name	old name of dataset
26415367Sahrens  * zc_value	new name of dataset
26425367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26435367Sahrens  *
26445367Sahrens  * outputs:	none
26455367Sahrens  */
2646789Sahrens static int
2647789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2648789Sahrens {
26494490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26504007Smmusante 
26512676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26525326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26535326Sek110237 	    strchr(zc->zc_value, '%'))
2654789Sahrens 		return (EINVAL);
2655789Sahrens 
26564007Smmusante 	/*
26574007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26584007Smmusante 	 * in which case the dataset code figures out which snapshots
26594007Smmusante 	 * to unmount.
26604007Smmusante 	 */
26614007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2662789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26632199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26642199Sahrens 		if (err)
26652199Sahrens 			return (err);
2666789Sahrens 	}
26674007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2668789Sahrens }
2669789Sahrens 
26706689Smaybee static void
26718536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26726689Smaybee {
26736689Smaybee 	zfs_cmd_t *zc;
26746689Smaybee 	nvpair_t *prop;
26756689Smaybee 
26766689Smaybee 	if (props == NULL)
26776689Smaybee 		return;
26786689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
26796689Smaybee 	(void) strcpy(zc->zc_name, dataset);
26806689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
26816689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
26828536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
26838536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
26848536SDavid.Pacheco@Sun.COM 			continue;
26856689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
26866689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
26876689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
26886689Smaybee 	}
26896689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
26906689Smaybee }
26916689Smaybee 
26925367Sahrens /*
26935367Sahrens  * inputs:
26945367Sahrens  * zc_name		name of containing filesystem
26955367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
26965367Sahrens  * zc_value		name of snapshot to create
26975367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
26985367Sahrens  * zc_cookie		file descriptor to recv from
26995367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27005367Sahrens  * zc_guid		force flag
27015367Sahrens  *
27025367Sahrens  * outputs:
27035367Sahrens  * zc_cookie		number of bytes read
27045367Sahrens  */
2705789Sahrens static int
27065367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2707789Sahrens {
2708789Sahrens 	file_t *fp;
27095326Sek110237 	objset_t *os;
27105367Sahrens 	dmu_recv_cookie_t drc;
27115326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2712789Sahrens 	int error, fd;
27135367Sahrens 	offset_t off;
27145367Sahrens 	nvlist_t *props = NULL;
27156689Smaybee 	nvlist_t *origprops = NULL;
27165367Sahrens 	objset_t *origin = NULL;
27175367Sahrens 	char *tosnap;
27185367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2719789Sahrens 
27203265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27215326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27225326Sek110237 	    strchr(zc->zc_value, '%'))
27233265Sahrens 		return (EINVAL);
27243265Sahrens 
27255367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27265367Sahrens 	tosnap = strchr(tofs, '@');
27275367Sahrens 	*tosnap = '\0';
27285367Sahrens 	tosnap++;
27295367Sahrens 
27305367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27315367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27329643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27335367Sahrens 		return (error);
27345367Sahrens 
2735789Sahrens 	fd = zc->zc_cookie;
2736789Sahrens 	fp = getf(fd);
27375367Sahrens 	if (fp == NULL) {
27385367Sahrens 		nvlist_free(props);
2739789Sahrens 		return (EBADF);
27405367Sahrens 	}
27415326Sek110237 
2742*10204SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_open(tofs, DMU_OST_ANY,
2743*10204SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
27446689Smaybee 		/*
27456689Smaybee 		 * If new properties are supplied, they are to completely
27466689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27476689Smaybee 		 */
27489396SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, TRUE);
27496689Smaybee 
27505326Sek110237 		dmu_objset_close(os);
27515326Sek110237 	}
27525326Sek110237 
27535367Sahrens 	if (zc->zc_string[0]) {
27545367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
27556689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
27566689Smaybee 		if (error)
27576689Smaybee 			goto out;
27585367Sahrens 	}
27595367Sahrens 
27605367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2761*10204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
27625367Sahrens 	if (origin)
27635367Sahrens 		dmu_objset_close(origin);
27646689Smaybee 	if (error)
27656689Smaybee 		goto out;
27665326Sek110237 
27675326Sek110237 	/*
27686689Smaybee 	 * Reset properties.  We do this before we receive the stream
27696689Smaybee 	 * so that the properties are applied to the new data.
27705326Sek110237 	 */
27715367Sahrens 	if (props) {
27728536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27736689Smaybee 		/*
27746689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27756689Smaybee 		 */
27766689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27775367Sahrens 	}
27785367Sahrens 
27795367Sahrens 	off = fp->f_offset;
27805367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
27815367Sahrens 
2782*10204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
2783*10204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
2784*10204SMatthew.Ahrens@Sun.COM 
2785*10204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
2786*10204SMatthew.Ahrens@Sun.COM 			/* online recv */
2787*10204SMatthew.Ahrens@Sun.COM 			int end_err;
2788*10204SMatthew.Ahrens@Sun.COM 			char *osname;
2789*10204SMatthew.Ahrens@Sun.COM 			int mode;
2790*10204SMatthew.Ahrens@Sun.COM 
2791*10204SMatthew.Ahrens@Sun.COM 			osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2792*10204SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, osname, &mode);
2793*10204SMatthew.Ahrens@Sun.COM 			/*
2794*10204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
2795*10204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
2796*10204SMatthew.Ahrens@Sun.COM 			 */
2797*10204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
2798*10204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
2799*10204SMatthew.Ahrens@Sun.COM 				int resume_err =
2800*10204SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, osname, mode);
2801*10204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
2802*10204SMatthew.Ahrens@Sun.COM 			}
2803*10204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
2804*10204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
2805*10204SMatthew.Ahrens@Sun.COM 			kmem_free(osname, MAXNAMELEN);
2806*10204SMatthew.Ahrens@Sun.COM 		} else {
28076689Smaybee 			error = dmu_recv_end(&drc);
28085367Sahrens 		}
28096083Sek110237 	}
28105367Sahrens 
28115367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28125367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28135367Sahrens 		fp->f_offset = off;
28142885Sahrens 
28156689Smaybee 	/*
28166689Smaybee 	 * On error, restore the original props.
28176689Smaybee 	 */
28186689Smaybee 	if (error && props) {
28198536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28206689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28216689Smaybee 	}
28226689Smaybee out:
28236689Smaybee 	nvlist_free(props);
28246689Smaybee 	nvlist_free(origprops);
2825789Sahrens 	releasef(fd);
2826789Sahrens 	return (error);
2827789Sahrens }
2828789Sahrens 
28295367Sahrens /*
28305367Sahrens  * inputs:
28315367Sahrens  * zc_name	name of snapshot to send
28325367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28335367Sahrens  * zc_cookie	file descriptor to send stream to
28345367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28355367Sahrens  *
28365367Sahrens  * outputs: none
28375367Sahrens  */
2838789Sahrens static int
28395367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2840789Sahrens {
2841789Sahrens 	objset_t *fromsnap = NULL;
2842789Sahrens 	objset_t *tosnap;
2843789Sahrens 	file_t *fp;
2844789Sahrens 	int error;
28455367Sahrens 	offset_t off;
2846789Sahrens 
2847789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
28486689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2849789Sahrens 	if (error)
2850789Sahrens 		return (error);
2851789Sahrens 
28522676Seschrock 	if (zc->zc_value[0] != '\0') {
28538012SEric.Taylor@Sun.COM 		char *buf;
28542885Sahrens 		char *cp;
28552885Sahrens 
28568012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28578012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28582885Sahrens 		cp = strchr(buf, '@');
28592885Sahrens 		if (cp)
28602885Sahrens 			*(cp+1) = 0;
28618012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
28622885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
28636689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
28648012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2865789Sahrens 		if (error) {
2866789Sahrens 			dmu_objset_close(tosnap);
2867789Sahrens 			return (error);
2868789Sahrens 		}
2869789Sahrens 	}
2870789Sahrens 
2871789Sahrens 	fp = getf(zc->zc_cookie);
2872789Sahrens 	if (fp == NULL) {
2873789Sahrens 		dmu_objset_close(tosnap);
2874789Sahrens 		if (fromsnap)
2875789Sahrens 			dmu_objset_close(fromsnap);
2876789Sahrens 		return (EBADF);
2877789Sahrens 	}
2878789Sahrens 
28795367Sahrens 	off = fp->f_offset;
28805367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28815367Sahrens 
28825367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28835367Sahrens 		fp->f_offset = off;
2884789Sahrens 	releasef(zc->zc_cookie);
2885789Sahrens 	if (fromsnap)
2886789Sahrens 		dmu_objset_close(fromsnap);
2887789Sahrens 	dmu_objset_close(tosnap);
2888789Sahrens 	return (error);
2889789Sahrens }
2890789Sahrens 
28911544Seschrock static int
28921544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
28931544Seschrock {
28941544Seschrock 	int id, error;
28951544Seschrock 
28961544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
28971544Seschrock 	    &zc->zc_inject_record);
28981544Seschrock 
28991544Seschrock 	if (error == 0)
29001544Seschrock 		zc->zc_guid = (uint64_t)id;
29011544Seschrock 
29021544Seschrock 	return (error);
29031544Seschrock }
29041544Seschrock 
29051544Seschrock static int
29061544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29071544Seschrock {
29081544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29091544Seschrock }
29101544Seschrock 
29111544Seschrock static int
29121544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29131544Seschrock {
29141544Seschrock 	int id = (int)zc->zc_guid;
29151544Seschrock 	int error;
29161544Seschrock 
29171544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29181544Seschrock 	    &zc->zc_inject_record);
29191544Seschrock 
29201544Seschrock 	zc->zc_guid = id;
29211544Seschrock 
29221544Seschrock 	return (error);
29231544Seschrock }
29241544Seschrock 
29251544Seschrock static int
29261544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29271544Seschrock {
29281544Seschrock 	spa_t *spa;
29291544Seschrock 	int error;
29302676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29311544Seschrock 
29321544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29331544Seschrock 		return (error);
29341544Seschrock 
29352676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29361544Seschrock 	    &count);
29371544Seschrock 	if (error == 0)
29382676Seschrock 		zc->zc_nvlist_dst_size = count;
29391544Seschrock 	else
29402676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29411544Seschrock 
29421544Seschrock 	spa_close(spa, FTAG);
29431544Seschrock 
29441544Seschrock 	return (error);
29451544Seschrock }
29461544Seschrock 
29471544Seschrock static int
29481544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29491544Seschrock {
29501544Seschrock 	spa_t *spa;
29511544Seschrock 	vdev_t *vd;
29521544Seschrock 	int error;
29531544Seschrock 
29547294Sperrin 	/*
29557294Sperrin 	 * On zpool clear we also fix up missing slogs
29567294Sperrin 	 */
29577294Sperrin 	mutex_enter(&spa_namespace_lock);
29587294Sperrin 	spa = spa_lookup(zc->zc_name);
29597294Sperrin 	if (spa == NULL) {
29607294Sperrin 		mutex_exit(&spa_namespace_lock);
29617294Sperrin 		return (EIO);
29627294Sperrin 	}
29637294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29647294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29657294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29667294Sperrin 	}
29677294Sperrin 	mutex_exit(&spa_namespace_lock);
29687294Sperrin 
29691544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29701544Seschrock 		return (error);
29711544Seschrock 
29727754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
29731544Seschrock 
29742676Seschrock 	if (zc->zc_guid == 0) {
29751544Seschrock 		vd = NULL;
29766643Seschrock 	} else {
29776643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
29785450Sbrendan 		if (vd == NULL) {
29797754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
29805450Sbrendan 			spa_close(spa, FTAG);
29815450Sbrendan 			return (ENODEV);
29825450Sbrendan 		}
29831544Seschrock 	}
29841544Seschrock 
29857754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
29867754SJeff.Bonwick@Sun.COM 
29877754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
29887754SJeff.Bonwick@Sun.COM 
29897754SJeff.Bonwick@Sun.COM 	/*
29907754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
29917754SJeff.Bonwick@Sun.COM 	 */
29929234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
29939234SGeorge.Wilson@Sun.COM 		error = EIO;
29941544Seschrock 
29951544Seschrock 	spa_close(spa, FTAG);
29961544Seschrock 
29979234SGeorge.Wilson@Sun.COM 	return (error);
29981544Seschrock }
29991544Seschrock 
30005367Sahrens /*
30015367Sahrens  * inputs:
30025367Sahrens  * zc_name	name of filesystem
30035367Sahrens  * zc_value	name of origin snapshot
30045367Sahrens  *
30055367Sahrens  * outputs:	none
30065367Sahrens  */
30071544Seschrock static int
30082082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30092082Seschrock {
30102417Sahrens 	char *cp;
30112417Sahrens 
30122417Sahrens 	/*
30132417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30142417Sahrens 	 * it's easier.
30152417Sahrens 	 */
30162676Seschrock 	cp = strchr(zc->zc_value, '@');
30172417Sahrens 	if (cp)
30182417Sahrens 		*cp = '\0';
30192676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30202417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
30212082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
30222082Seschrock }
30232082Seschrock 
30244543Smarks /*
30259396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30269396SMatthew.Ahrens@Sun.COM  *
30279396SMatthew.Ahrens@Sun.COM  * inputs:
30289396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30299396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30309396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30319396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30329396SMatthew.Ahrens@Sun.COM  *
30339396SMatthew.Ahrens@Sun.COM  * outputs:
30349396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30359396SMatthew.Ahrens@Sun.COM  */
30369396SMatthew.Ahrens@Sun.COM static int
30379396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30389396SMatthew.Ahrens@Sun.COM {
30399396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30409396SMatthew.Ahrens@Sun.COM 	int error;
30419396SMatthew.Ahrens@Sun.COM 
30429396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30439396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30449396SMatthew.Ahrens@Sun.COM 
30459396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30469396SMatthew.Ahrens@Sun.COM 	if (error)
30479396SMatthew.Ahrens@Sun.COM 		return (error);
30489396SMatthew.Ahrens@Sun.COM 
30499396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
30509396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
30519396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30529396SMatthew.Ahrens@Sun.COM 
30539396SMatthew.Ahrens@Sun.COM 	return (error);
30549396SMatthew.Ahrens@Sun.COM }
30559396SMatthew.Ahrens@Sun.COM 
30569396SMatthew.Ahrens@Sun.COM /*
30579396SMatthew.Ahrens@Sun.COM  * inputs:
30589396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
30599396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
30609396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
30619396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
30629396SMatthew.Ahrens@Sun.COM  *
30639396SMatthew.Ahrens@Sun.COM  * outputs:
30649396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
30659396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
30669396SMatthew.Ahrens@Sun.COM  */
30679396SMatthew.Ahrens@Sun.COM static int
30689396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
30699396SMatthew.Ahrens@Sun.COM {
30709396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30719396SMatthew.Ahrens@Sun.COM 	int error;
30729396SMatthew.Ahrens@Sun.COM 
30739396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30749396SMatthew.Ahrens@Sun.COM 	if (error)
30759396SMatthew.Ahrens@Sun.COM 		return (error);
30769396SMatthew.Ahrens@Sun.COM 
30779396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
30789396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
30799396SMatthew.Ahrens@Sun.COM 
30809396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
30819396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
30829396SMatthew.Ahrens@Sun.COM 
30839396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
30849396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
30859396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
30869396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
30879396SMatthew.Ahrens@Sun.COM 	}
30889396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
30899396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30909396SMatthew.Ahrens@Sun.COM 
30919396SMatthew.Ahrens@Sun.COM 	return (error);
30929396SMatthew.Ahrens@Sun.COM }
30939396SMatthew.Ahrens@Sun.COM 
30949396SMatthew.Ahrens@Sun.COM /*
30959396SMatthew.Ahrens@Sun.COM  * inputs:
30969396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
30979396SMatthew.Ahrens@Sun.COM  *
30989396SMatthew.Ahrens@Sun.COM  * outputs:
30999396SMatthew.Ahrens@Sun.COM  * none
31009396SMatthew.Ahrens@Sun.COM  */
31019396SMatthew.Ahrens@Sun.COM static int
31029396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31039396SMatthew.Ahrens@Sun.COM {
31049396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31059396SMatthew.Ahrens@Sun.COM 	int error;
31069396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31079396SMatthew.Ahrens@Sun.COM 
31089396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
31099396SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
31109396SMatthew.Ahrens@Sun.COM 			/*
31119396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31129396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31139396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31149396SMatthew.Ahrens@Sun.COM 			 */
31159396SMatthew.Ahrens@Sun.COM 			int mode;
31169396SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, NULL, &mode);
31179396SMatthew.Ahrens@Sun.COM 			if (error == 0) {
31189396SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs,
31199396SMatthew.Ahrens@Sun.COM 				    zc->zc_name, mode);
31209396SMatthew.Ahrens@Sun.COM 			}
31219396SMatthew.Ahrens@Sun.COM 		}
31229396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31239396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31249396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31259396SMatthew.Ahrens@Sun.COM 	} else {
31269396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
31279396SMatthew.Ahrens@Sun.COM 		    DS_MODE_USER, &os);
31289396SMatthew.Ahrens@Sun.COM 		if (error)
31299396SMatthew.Ahrens@Sun.COM 			return (error);
31309396SMatthew.Ahrens@Sun.COM 
31319396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
31329396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
31339396SMatthew.Ahrens@Sun.COM 	}
31349396SMatthew.Ahrens@Sun.COM 
31359396SMatthew.Ahrens@Sun.COM 	return (error);
31369396SMatthew.Ahrens@Sun.COM }
31379396SMatthew.Ahrens@Sun.COM 
31389396SMatthew.Ahrens@Sun.COM /*
31394543Smarks  * We don't want to have a hard dependency
31404543Smarks  * against some special symbols in sharefs
31415331Samw  * nfs, and smbsrv.  Determine them if needed when
31424543Smarks  * the first file system is shared.
31435331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31444543Smarks  */
31455331Samw int (*znfsexport_fs)(void *arg);
31464543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31475331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31485331Samw 
31495331Samw int zfs_nfsshare_inited;
31505331Samw int zfs_smbshare_inited;
31515331Samw 
31524543Smarks ddi_modhandle_t nfs_mod;
31534543Smarks ddi_modhandle_t sharefs_mod;
31545331Samw ddi_modhandle_t smbsrv_mod;
31554543Smarks kmutex_t zfs_share_lock;
31564543Smarks 
31574543Smarks static int
31585331Samw zfs_init_sharefs()
31595331Samw {
31605331Samw 	int error;
31615331Samw 
31625331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
31635331Samw 	/* Both NFS and SMB shares also require sharetab support. */
31645331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
31655331Samw 	    ddi_modopen("fs/sharefs",
31665331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
31675331Samw 		return (ENOSYS);
31685331Samw 	}
31695331Samw 	if (zshare_fs == NULL && ((zshare_fs =
31705331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
31715331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
31725331Samw 		return (ENOSYS);
31735331Samw 	}
31745331Samw 	return (0);
31755331Samw }
31765331Samw 
31775331Samw static int
31784543Smarks zfs_ioc_share(zfs_cmd_t *zc)
31794543Smarks {
31804543Smarks 	int error;
31814543Smarks 	int opcode;
31824543Smarks 
31835331Samw 	switch (zc->zc_share.z_sharetype) {
31845331Samw 	case ZFS_SHARE_NFS:
31855331Samw 	case ZFS_UNSHARE_NFS:
31865331Samw 		if (zfs_nfsshare_inited == 0) {
31875331Samw 			mutex_enter(&zfs_share_lock);
31885331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
31895331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
31905331Samw 				mutex_exit(&zfs_share_lock);
31915331Samw 				return (ENOSYS);
31925331Samw 			}
31935331Samw 			if (znfsexport_fs == NULL &&
31945331Samw 			    ((znfsexport_fs = (int (*)(void *))
31955331Samw 			    ddi_modsym(nfs_mod,
31965331Samw 			    "nfs_export", &error)) == NULL)) {
31975331Samw 				mutex_exit(&zfs_share_lock);
31985331Samw 				return (ENOSYS);
31995331Samw 			}
32005331Samw 			error = zfs_init_sharefs();
32015331Samw 			if (error) {
32025331Samw 				mutex_exit(&zfs_share_lock);
32035331Samw 				return (ENOSYS);
32045331Samw 			}
32055331Samw 			zfs_nfsshare_inited = 1;
32064543Smarks 			mutex_exit(&zfs_share_lock);
32074543Smarks 		}
32085331Samw 		break;
32095331Samw 	case ZFS_SHARE_SMB:
32105331Samw 	case ZFS_UNSHARE_SMB:
32115331Samw 		if (zfs_smbshare_inited == 0) {
32125331Samw 			mutex_enter(&zfs_share_lock);
32135331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32145331Samw 			    ddi_modopen("drv/smbsrv",
32155331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32165331Samw 				mutex_exit(&zfs_share_lock);
32175331Samw 				return (ENOSYS);
32185331Samw 			}
32195331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32205331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32216139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32225331Samw 				mutex_exit(&zfs_share_lock);
32235331Samw 				return (ENOSYS);
32245331Samw 			}
32255331Samw 			error = zfs_init_sharefs();
32265331Samw 			if (error) {
32275331Samw 				mutex_exit(&zfs_share_lock);
32285331Samw 				return (ENOSYS);
32295331Samw 			}
32305331Samw 			zfs_smbshare_inited = 1;
32314543Smarks 			mutex_exit(&zfs_share_lock);
32324543Smarks 		}
32335331Samw 		break;
32345331Samw 	default:
32355331Samw 		return (EINVAL);
32364543Smarks 	}
32374543Smarks 
32385331Samw 	switch (zc->zc_share.z_sharetype) {
32395331Samw 	case ZFS_SHARE_NFS:
32405331Samw 	case ZFS_UNSHARE_NFS:
32415331Samw 		if (error =
32425331Samw 		    znfsexport_fs((void *)
32435331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32445331Samw 			return (error);
32455331Samw 		break;
32465331Samw 	case ZFS_SHARE_SMB:
32475331Samw 	case ZFS_UNSHARE_SMB:
32485331Samw 		if (error = zsmbexport_fs((void *)
32495331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32505331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32518845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32525331Samw 			return (error);
32535331Samw 		}
32545331Samw 		break;
32555331Samw 	}
32565331Samw 
32575331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32585331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32594543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32604543Smarks 
32615331Samw 	/*
32625331Samw 	 * Add or remove share from sharetab
32635331Samw 	 */
32644543Smarks 	error = zshare_fs(opcode,
32654543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
32664543Smarks 	    zc->zc_share.z_sharemax);
32674543Smarks 
32684543Smarks 	return (error);
32694543Smarks 
32704543Smarks }
32714543Smarks 
32728845Samw@Sun.COM ace_t full_access[] = {
32738845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
32748845Samw@Sun.COM };
32758845Samw@Sun.COM 
32768845Samw@Sun.COM /*
32778845Samw@Sun.COM  * Remove all ACL files in shares dir
32788845Samw@Sun.COM  */
32798845Samw@Sun.COM static int
32808845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
32818845Samw@Sun.COM {
32828845Samw@Sun.COM 	zap_cursor_t	zc;
32838845Samw@Sun.COM 	zap_attribute_t	zap;
32848845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
32858845Samw@Sun.COM 	int error;
32868845Samw@Sun.COM 
32878845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
32888845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
32898845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
32908845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
32918845Samw@Sun.COM 		    NULL, 0)) != 0)
32928845Samw@Sun.COM 			break;
32938845Samw@Sun.COM 	}
32948845Samw@Sun.COM 	zap_cursor_fini(&zc);
32958845Samw@Sun.COM 	return (error);
32968845Samw@Sun.COM }
32978845Samw@Sun.COM 
32988845Samw@Sun.COM static int
32998845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33008845Samw@Sun.COM {
33018845Samw@Sun.COM 	vnode_t *vp;
33028845Samw@Sun.COM 	znode_t *dzp;
33038845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33048845Samw@Sun.COM 	znode_t *sharedir;
33058845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33068845Samw@Sun.COM 	nvlist_t *nvlist;
33078845Samw@Sun.COM 	char *src, *target;
33088845Samw@Sun.COM 	vattr_t vattr;
33098845Samw@Sun.COM 	vsecattr_t vsec;
33108845Samw@Sun.COM 	int error = 0;
33118845Samw@Sun.COM 
33128845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33138845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33148845Samw@Sun.COM 		return (error);
33158845Samw@Sun.COM 
33168845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33178845Samw@Sun.COM 
33188845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33198845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33208845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33218845Samw@Sun.COM 		VN_RELE(vp);
33228845Samw@Sun.COM 		return (EINVAL);
33238845Samw@Sun.COM 	}
33248845Samw@Sun.COM 
33258845Samw@Sun.COM 	dzp = VTOZ(vp);
33268845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33278845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33288845Samw@Sun.COM 
33299030SMark.Shellenbaum@Sun.COM 	/*
33309030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33319030SMark.Shellenbaum@Sun.COM 	 */
33329030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33339030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33349030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33359030SMark.Shellenbaum@Sun.COM 
33369030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33379030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33389030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33399030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33409030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33419030SMark.Shellenbaum@Sun.COM 		if (error) {
33429030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33439030SMark.Shellenbaum@Sun.COM 		} else {
33449030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33459030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33469030SMark.Shellenbaum@Sun.COM 		}
33479030SMark.Shellenbaum@Sun.COM 		if (error) {
33489030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33499030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33509030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33519030SMark.Shellenbaum@Sun.COM 			return (error);
33529030SMark.Shellenbaum@Sun.COM 		}
33539030SMark.Shellenbaum@Sun.COM 	}
33549030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33559030SMark.Shellenbaum@Sun.COM 
33569030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33578845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33589030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33598845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33608845Samw@Sun.COM 		return (error);
33618845Samw@Sun.COM 	}
33628845Samw@Sun.COM 
33638845Samw@Sun.COM 	switch (zc->zc_cookie) {
33648845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
33658845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
33668845Samw@Sun.COM 		vattr.va_type = VREG;
33678845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
33688845Samw@Sun.COM 		vattr.va_uid = 0;
33698845Samw@Sun.COM 		vattr.va_gid = 0;
33708845Samw@Sun.COM 
33718845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
33728845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
33738845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
33748845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
33758845Samw@Sun.COM 
33768845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
33778845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
33788845Samw@Sun.COM 		if (resourcevp)
33798845Samw@Sun.COM 			VN_RELE(resourcevp);
33808845Samw@Sun.COM 		break;
33818845Samw@Sun.COM 
33828845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
33838845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
33848845Samw@Sun.COM 		    NULL, 0);
33858845Samw@Sun.COM 		break;
33868845Samw@Sun.COM 
33878845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
33888845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
33899643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
33908845Samw@Sun.COM 			VN_RELE(vp);
33918845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
33928845Samw@Sun.COM 			return (error);
33938845Samw@Sun.COM 		}
33948845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
33958845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
33968845Samw@Sun.COM 		    &target)) {
33978845Samw@Sun.COM 			VN_RELE(vp);
33989179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
33998845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34008845Samw@Sun.COM 			return (error);
34018845Samw@Sun.COM 		}
34028845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34038845Samw@Sun.COM 		    kcred, NULL, 0);
34048845Samw@Sun.COM 		nvlist_free(nvlist);
34058845Samw@Sun.COM 		break;
34068845Samw@Sun.COM 
34078845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34088845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34098845Samw@Sun.COM 		break;
34108845Samw@Sun.COM 
34118845Samw@Sun.COM 	default:
34128845Samw@Sun.COM 		error = EINVAL;
34138845Samw@Sun.COM 		break;
34148845Samw@Sun.COM 	}
34158845Samw@Sun.COM 
34168845Samw@Sun.COM 	VN_RELE(vp);
34178845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34188845Samw@Sun.COM 
34198845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34208845Samw@Sun.COM 
34218845Samw@Sun.COM 	return (error);
34228845Samw@Sun.COM }
34238845Samw@Sun.COM 
34244543Smarks /*
34254988Sek110237  * pool create, destroy, and export don't log the history as part of
34264988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
34274988Sek110237  * do the logging of those commands.
34284543Smarks  */
3429789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
34309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34319234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34359234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
34399234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
34419234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
34439234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34459234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
34479234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
34499234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
34519234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34539234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34559234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
34579234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34599234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
34619234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34639234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34649425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
34659425SEric.Schrock@Sun.COM 	    B_TRUE },
34669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34679234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34699234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34719234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
34739234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
34759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34769234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
34789234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34799234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
34809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
34819234SGeorge.Wilson@Sun.COM 	    B_TRUE},
34829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
34839234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
34859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
34869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
34879234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
34889234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34909234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
34929234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
34949234SGeorge.Wilson@Sun.COM 	    B_FALSE },
34959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
34969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
34979234SGeorge.Wilson@Sun.COM 	    B_TRUE },
34989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
34999234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
35019234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35029234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35039234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
35059234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35079234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35099234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
35119234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35149234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
35159234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35169234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
35179234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
35189234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35199234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
35209396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
35219396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
35229396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35239396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
35249396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35259396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
35269396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
3527789Sahrens };
3528789Sahrens 
35299234SGeorge.Wilson@Sun.COM int
35309234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
35319234SGeorge.Wilson@Sun.COM {
35329234SGeorge.Wilson@Sun.COM 	spa_t *spa;
35339234SGeorge.Wilson@Sun.COM 	int error;
35349234SGeorge.Wilson@Sun.COM 
35359234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
35369234SGeorge.Wilson@Sun.COM 
35379396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
35389234SGeorge.Wilson@Sun.COM 	if (error == 0) {
35399234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
35409234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
35419234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
35429234SGeorge.Wilson@Sun.COM 	}
35439234SGeorge.Wilson@Sun.COM 	return (error);
35449234SGeorge.Wilson@Sun.COM }
35459234SGeorge.Wilson@Sun.COM 
3546789Sahrens static int
3547789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3548789Sahrens {
3549789Sahrens 	zfs_cmd_t *zc;
3550789Sahrens 	uint_t vec;
35512199Sahrens 	int error, rc;
3552789Sahrens 
3553789Sahrens 	if (getminor(dev) != 0)
3554789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3555789Sahrens 
3556789Sahrens 	vec = cmd - ZFS_IOC;
35574787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3558789Sahrens 
3559789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3560789Sahrens 		return (EINVAL);
3561789Sahrens 
3562789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3563789Sahrens 
35649643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3565789Sahrens 
35664787Sahrens 	if (error == 0)
35674543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3568789Sahrens 
3569789Sahrens 	/*
3570789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3571789Sahrens 	 * the lower layers.
3572789Sahrens 	 */
3573789Sahrens 	if (error == 0) {
3574789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
35759643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3576789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
35774577Sahrens 		case POOL_NAME:
3578789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3579789Sahrens 				error = EINVAL;
35809234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35819234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35829234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3583789Sahrens 			break;
3584789Sahrens 
35854577Sahrens 		case DATASET_NAME:
3586789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3587789Sahrens 				error = EINVAL;
35889234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
35899234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
35909234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3591789Sahrens 			break;
35922856Snd150628 
35934577Sahrens 		case NO_NAME:
35942856Snd150628 			break;
3595789Sahrens 		}
3596789Sahrens 	}
3597789Sahrens 
3598789Sahrens 	if (error == 0)
3599789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3600789Sahrens 
36019643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
36024543Smarks 	if (error == 0) {
36032199Sahrens 		error = rc;
36049396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
36054543Smarks 			zfs_log_history(zc);
36064543Smarks 	}
3607789Sahrens 
3608789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3609789Sahrens 	return (error);
3610789Sahrens }
3611789Sahrens 
3612789Sahrens static int
3613789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3614789Sahrens {
3615789Sahrens 	if (cmd != DDI_ATTACH)
3616789Sahrens 		return (DDI_FAILURE);
3617789Sahrens 
3618789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3619789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3620789Sahrens 		return (DDI_FAILURE);
3621789Sahrens 
3622789Sahrens 	zfs_dip = dip;
3623789Sahrens 
3624789Sahrens 	ddi_report_dev(dip);
3625789Sahrens 
3626789Sahrens 	return (DDI_SUCCESS);
3627789Sahrens }
3628789Sahrens 
3629789Sahrens static int
3630789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3631789Sahrens {
3632789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3633789Sahrens 		return (DDI_FAILURE);
3634789Sahrens 
3635789Sahrens 	if (cmd != DDI_DETACH)
3636789Sahrens 		return (DDI_FAILURE);
3637789Sahrens 
3638789Sahrens 	zfs_dip = NULL;
3639789Sahrens 
3640789Sahrens 	ddi_prop_remove_all(dip);
3641789Sahrens 	ddi_remove_minor_node(dip, NULL);
3642789Sahrens 
3643789Sahrens 	return (DDI_SUCCESS);
3644789Sahrens }
3645789Sahrens 
3646789Sahrens /*ARGSUSED*/
3647789Sahrens static int
3648789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3649789Sahrens {
3650789Sahrens 	switch (infocmd) {
3651789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3652789Sahrens 		*result = zfs_dip;
3653789Sahrens 		return (DDI_SUCCESS);
3654789Sahrens 
3655789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3656849Sbonwick 		*result = (void *)0;
3657789Sahrens 		return (DDI_SUCCESS);
3658789Sahrens 	}
3659789Sahrens 
3660789Sahrens 	return (DDI_FAILURE);
3661789Sahrens }
3662789Sahrens 
3663789Sahrens /*
3664789Sahrens  * OK, so this is a little weird.
3665789Sahrens  *
3666789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3667789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3668789Sahrens  *
3669789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3670789Sahrens  * so most of the standard driver entry points are in zvol.c.
3671789Sahrens  */
3672789Sahrens static struct cb_ops zfs_cb_ops = {
3673789Sahrens 	zvol_open,	/* open */
3674789Sahrens 	zvol_close,	/* close */
3675789Sahrens 	zvol_strategy,	/* strategy */
3676789Sahrens 	nodev,		/* print */
36776423Sgw25295 	zvol_dump,	/* dump */
3678789Sahrens 	zvol_read,	/* read */
3679789Sahrens 	zvol_write,	/* write */
3680789Sahrens 	zfsdev_ioctl,	/* ioctl */
3681789Sahrens 	nodev,		/* devmap */
3682789Sahrens 	nodev,		/* mmap */
3683789Sahrens 	nodev,		/* segmap */
3684789Sahrens 	nochpoll,	/* poll */
3685789Sahrens 	ddi_prop_op,	/* prop_op */
3686789Sahrens 	NULL,		/* streamtab */
3687789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3688789Sahrens 	CB_REV,		/* version */
36893638Sbillm 	nodev,		/* async read */
36903638Sbillm 	nodev,		/* async write */
3691789Sahrens };
3692789Sahrens 
3693789Sahrens static struct dev_ops zfs_dev_ops = {
3694789Sahrens 	DEVO_REV,	/* version */
3695789Sahrens 	0,		/* refcnt */
3696789Sahrens 	zfs_info,	/* info */
3697789Sahrens 	nulldev,	/* identify */
3698789Sahrens 	nulldev,	/* probe */
3699789Sahrens 	zfs_attach,	/* attach */
3700789Sahrens 	zfs_detach,	/* detach */
3701789Sahrens 	nodev,		/* reset */
3702789Sahrens 	&zfs_cb_ops,	/* driver operations */
37037656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
37047656SSherry.Moore@Sun.COM 	NULL,		/* power */
37057656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3706789Sahrens };
3707789Sahrens 
3708789Sahrens static struct modldrv zfs_modldrv = {
37097656SSherry.Moore@Sun.COM 	&mod_driverops,
37107656SSherry.Moore@Sun.COM 	"ZFS storage pool",
37117656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3712789Sahrens };
3713789Sahrens 
3714789Sahrens static struct modlinkage modlinkage = {
3715789Sahrens 	MODREV_1,
3716789Sahrens 	(void *)&zfs_modlfs,
3717789Sahrens 	(void *)&zfs_modldrv,
3718789Sahrens 	NULL
3719789Sahrens };
3720789Sahrens 
37214720Sfr157268 
37224720Sfr157268 uint_t zfs_fsyncer_key;
37235326Sek110237 extern uint_t rrw_tsd_key;
37244720Sfr157268 
3725789Sahrens int
3726789Sahrens _init(void)
3727789Sahrens {
3728789Sahrens 	int error;
3729789Sahrens 
3730849Sbonwick 	spa_init(FREAD | FWRITE);
3731849Sbonwick 	zfs_init();
3732849Sbonwick 	zvol_init();
3733849Sbonwick 
3734849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3735849Sbonwick 		zvol_fini();
3736849Sbonwick 		zfs_fini();
3737849Sbonwick 		spa_fini();
3738789Sahrens 		return (error);
3739849Sbonwick 	}
3740789Sahrens 
37414720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
37425326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
37434720Sfr157268 
3744789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3745789Sahrens 	ASSERT(error == 0);
37464543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3747789Sahrens 
3748789Sahrens 	return (0);
3749789Sahrens }
3750789Sahrens 
3751789Sahrens int
3752789Sahrens _fini(void)
3753789Sahrens {
3754789Sahrens 	int error;
3755789Sahrens 
37561544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3757789Sahrens 		return (EBUSY);
3758789Sahrens 
3759789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3760789Sahrens 		return (error);
3761789Sahrens 
3762789Sahrens 	zvol_fini();
3763789Sahrens 	zfs_fini();
3764789Sahrens 	spa_fini();
37655331Samw 	if (zfs_nfsshare_inited)
37664543Smarks 		(void) ddi_modclose(nfs_mod);
37675331Samw 	if (zfs_smbshare_inited)
37685331Samw 		(void) ddi_modclose(smbsrv_mod);
37695331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
37704543Smarks 		(void) ddi_modclose(sharefs_mod);
3771789Sahrens 
37724720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3773789Sahrens 	ldi_ident_release(zfs_li);
3774789Sahrens 	zfs_li = NULL;
37754543Smarks 	mutex_destroy(&zfs_share_lock);
3776789Sahrens 
3777789Sahrens 	return (error);
3778789Sahrens }
3779789Sahrens 
3780789Sahrens int
3781789Sahrens _info(struct modinfo *modinfop)
3782789Sahrens {
3783789Sahrens 	return (mod_info(&modlinkage, modinfop));
3784789Sahrens }
3785