xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10272:a0669934e974)
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 
76410242Schris.kirby@sun.com static int
76510242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
76610242Schris.kirby@sun.com {
76710242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
76810242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
76910242Schris.kirby@sun.com }
77010242Schris.kirby@sun.com 
77110242Schris.kirby@sun.com static int
77210242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
77310242Schris.kirby@sun.com {
77410242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
77510242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
77610242Schris.kirby@sun.com }
77710242Schris.kirby@sun.com 
7781544Seschrock /*
779789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
780789Sahrens  */
781789Sahrens static int
7829643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
783789Sahrens {
784789Sahrens 	char *packed;
785789Sahrens 	int error;
7865094Slling 	nvlist_t *list = NULL;
787789Sahrens 
788789Sahrens 	/*
7892676Seschrock 	 * Read in and unpack the user-supplied nvlist.
790789Sahrens 	 */
7915094Slling 	if (size == 0)
792789Sahrens 		return (EINVAL);
793789Sahrens 
794789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
795789Sahrens 
7969643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
7979643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
798789Sahrens 		kmem_free(packed, size);
799789Sahrens 		return (error);
800789Sahrens 	}
801789Sahrens 
8025094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
803789Sahrens 		kmem_free(packed, size);
804789Sahrens 		return (error);
805789Sahrens 	}
806789Sahrens 
807789Sahrens 	kmem_free(packed, size);
808789Sahrens 
8095094Slling 	*nvp = list;
810789Sahrens 	return (0);
811789Sahrens }
812789Sahrens 
813789Sahrens static int
8142676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
8152676Seschrock {
8162676Seschrock 	char *packed = NULL;
8172676Seschrock 	size_t size;
8182676Seschrock 	int error;
8192676Seschrock 
8202676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
8212676Seschrock 
8222676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
8232676Seschrock 		error = ENOMEM;
8242676Seschrock 	} else {
8254611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
8262676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
8272676Seschrock 		    KM_SLEEP) == 0);
8289643SEric.Taylor@Sun.COM 		error = ddi_copyout(packed,
8299643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
8302676Seschrock 		kmem_free(packed, size);
8312676Seschrock 	}
8322676Seschrock 
8332676Seschrock 	zc->zc_nvlist_dst_size = size;
8342676Seschrock 	return (error);
8352676Seschrock }
8362676Seschrock 
8372676Seschrock static int
8389396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
8399396SMatthew.Ahrens@Sun.COM {
8409396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8419396SMatthew.Ahrens@Sun.COM 	int error;
8429396SMatthew.Ahrens@Sun.COM 
8439396SMatthew.Ahrens@Sun.COM 	error = dmu_objset_open(dsname, DMU_OST_ZFS,
8449396SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os);
8459396SMatthew.Ahrens@Sun.COM 	if (error)
8469396SMatthew.Ahrens@Sun.COM 		return (error);
8479396SMatthew.Ahrens@Sun.COM 
8489396SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os->os_user_ptr_lock);
8499396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
8509396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
8519396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
8529396SMatthew.Ahrens@Sun.COM 	} else {
8539396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
8549396SMatthew.Ahrens@Sun.COM 	}
8559396SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os->os_user_ptr_lock);
8569396SMatthew.Ahrens@Sun.COM 	dmu_objset_close(os);
8579396SMatthew.Ahrens@Sun.COM 	return (error);
8589396SMatthew.Ahrens@Sun.COM }
8599396SMatthew.Ahrens@Sun.COM 
8609396SMatthew.Ahrens@Sun.COM /*
8619396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
8629396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
8639396SMatthew.Ahrens@Sun.COM  */
8649396SMatthew.Ahrens@Sun.COM static int
8659396SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, boolean_t readonly, void *tag, zfsvfs_t **zvp)
8669396SMatthew.Ahrens@Sun.COM {
8679396SMatthew.Ahrens@Sun.COM 	int error = 0;
8689396SMatthew.Ahrens@Sun.COM 	int mode = DS_MODE_OWNER | (readonly ? DS_MODE_READONLY : 0);
8699396SMatthew.Ahrens@Sun.COM 
8709396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
8719396SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, mode, zvp);
8729396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
8739396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
8749396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
8759396SMatthew.Ahrens@Sun.COM 			/*
8769396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
8779396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
8789396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
8799396SMatthew.Ahrens@Sun.COM 			 */
8809396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
8819396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
8829396SMatthew.Ahrens@Sun.COM 		}
8839396SMatthew.Ahrens@Sun.COM 	}
8849396SMatthew.Ahrens@Sun.COM 	return (error);
8859396SMatthew.Ahrens@Sun.COM }
8869396SMatthew.Ahrens@Sun.COM 
8879396SMatthew.Ahrens@Sun.COM static void
8889396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
8899396SMatthew.Ahrens@Sun.COM {
8909396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
8919396SMatthew.Ahrens@Sun.COM 
8929396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
8939396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
8949396SMatthew.Ahrens@Sun.COM 	} else {
8959396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(zfsvfs->z_os);
8969396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
8979396SMatthew.Ahrens@Sun.COM 	}
8989396SMatthew.Ahrens@Sun.COM }
8999396SMatthew.Ahrens@Sun.COM 
9009396SMatthew.Ahrens@Sun.COM static int
901789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
902789Sahrens {
903789Sahrens 	int error;
9045094Slling 	nvlist_t *config, *props = NULL;
9057184Stimh 	nvlist_t *rootprops = NULL;
9067184Stimh 	nvlist_t *zplprops = NULL;
9074715Sek110237 	char *buf;
908789Sahrens 
9095094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9109643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
9114988Sek110237 		return (error);
9124715Sek110237 
9135094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9149643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9159643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9165094Slling 		nvlist_free(config);
9175094Slling 		return (error);
9185094Slling 	}
9195094Slling 
9207184Stimh 	if (props) {
9217184Stimh 		nvlist_t *nvl = NULL;
9227184Stimh 		uint64_t version = SPA_VERSION;
9237184Stimh 
9247184Stimh 		(void) nvlist_lookup_uint64(props,
9257184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9267184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
9277184Stimh 			error = EINVAL;
9287184Stimh 			goto pool_props_bad;
9297184Stimh 		}
9307184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9317184Stimh 		if (nvl) {
9327184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9337184Stimh 			if (error != 0) {
9347184Stimh 				nvlist_free(config);
9357184Stimh 				nvlist_free(props);
9367184Stimh 				return (error);
9377184Stimh 			}
9387184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9397184Stimh 		}
9407184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9417184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
9427184Stimh 		    zplprops, NULL);
9437184Stimh 		if (error)
9447184Stimh 			goto pool_props_bad;
9457184Stimh 	}
9467184Stimh 
9474988Sek110237 	buf = history_str_get(zc);
948789Sahrens 
9497184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9507184Stimh 
9517184Stimh 	/*
9527184Stimh 	 * Set the remaining root properties
9537184Stimh 	 */
9547184Stimh 	if (!error &&
9557184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9567184Stimh 		(void) spa_destroy(zc->zc_name);
957789Sahrens 
9584988Sek110237 	if (buf != NULL)
9594988Sek110237 		history_str_free(buf);
9605094Slling 
9617184Stimh pool_props_bad:
9627184Stimh 	nvlist_free(rootprops);
9637184Stimh 	nvlist_free(zplprops);
964789Sahrens 	nvlist_free(config);
9657184Stimh 	nvlist_free(props);
9665094Slling 
967789Sahrens 	return (error);
968789Sahrens }
969789Sahrens 
970789Sahrens static int
971789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
972789Sahrens {
9734543Smarks 	int error;
9744543Smarks 	zfs_log_history(zc);
9754543Smarks 	error = spa_destroy(zc->zc_name);
9764543Smarks 	return (error);
977789Sahrens }
978789Sahrens 
979789Sahrens static int
980789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
981789Sahrens {
982789Sahrens 	int error;
9835094Slling 	nvlist_t *config, *props = NULL;
984789Sahrens 	uint64_t guid;
985789Sahrens 
9865094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9879643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
988789Sahrens 		return (error);
989789Sahrens 
9905094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9919643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9929643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9935094Slling 		nvlist_free(config);
9945094Slling 		return (error);
9955094Slling 	}
9965094Slling 
997789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
9981544Seschrock 	    guid != zc->zc_guid)
999789Sahrens 		error = EINVAL;
10006643Seschrock 	else if (zc->zc_cookie)
10019425SEric.Schrock@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config,
10026643Seschrock 		    props);
1003789Sahrens 	else
10045094Slling 		error = spa_import(zc->zc_name, config, props);
1005789Sahrens 
1006789Sahrens 	nvlist_free(config);
1007789Sahrens 
10085094Slling 	if (props)
10095094Slling 		nvlist_free(props);
10105094Slling 
1011789Sahrens 	return (error);
1012789Sahrens }
1013789Sahrens 
1014789Sahrens static int
1015789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1016789Sahrens {
10174543Smarks 	int error;
10187214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
10198211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
10207214Slling 
10214543Smarks 	zfs_log_history(zc);
10228211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
10234543Smarks 	return (error);
1024789Sahrens }
1025789Sahrens 
1026789Sahrens static int
1027789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1028789Sahrens {
1029789Sahrens 	nvlist_t *configs;
1030789Sahrens 	int error;
1031789Sahrens 
1032789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1033789Sahrens 		return (EEXIST);
1034789Sahrens 
10352676Seschrock 	error = put_nvlist(zc, configs);
1036789Sahrens 
1037789Sahrens 	nvlist_free(configs);
1038789Sahrens 
1039789Sahrens 	return (error);
1040789Sahrens }
1041789Sahrens 
1042789Sahrens static int
1043789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1044789Sahrens {
1045789Sahrens 	nvlist_t *config;
1046789Sahrens 	int error;
10471544Seschrock 	int ret = 0;
1048789Sahrens 
10492676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10502676Seschrock 	    sizeof (zc->zc_value));
1051789Sahrens 
1052789Sahrens 	if (config != NULL) {
10532676Seschrock 		ret = put_nvlist(zc, config);
1054789Sahrens 		nvlist_free(config);
10551544Seschrock 
10561544Seschrock 		/*
10571544Seschrock 		 * The config may be present even if 'error' is non-zero.
10581544Seschrock 		 * In this case we return success, and preserve the real errno
10591544Seschrock 		 * in 'zc_cookie'.
10601544Seschrock 		 */
10611544Seschrock 		zc->zc_cookie = error;
1062789Sahrens 	} else {
10631544Seschrock 		ret = error;
1064789Sahrens 	}
1065789Sahrens 
10661544Seschrock 	return (ret);
1067789Sahrens }
1068789Sahrens 
1069789Sahrens /*
1070789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1071789Sahrens  * user land knows which devices are available and overall pool health.
1072789Sahrens  */
1073789Sahrens static int
1074789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1075789Sahrens {
1076789Sahrens 	nvlist_t *tryconfig, *config;
1077789Sahrens 	int error;
1078789Sahrens 
10795094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10809643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1081789Sahrens 		return (error);
1082789Sahrens 
1083789Sahrens 	config = spa_tryimport(tryconfig);
1084789Sahrens 
1085789Sahrens 	nvlist_free(tryconfig);
1086789Sahrens 
1087789Sahrens 	if (config == NULL)
1088789Sahrens 		return (EINVAL);
1089789Sahrens 
10902676Seschrock 	error = put_nvlist(zc, config);
1091789Sahrens 	nvlist_free(config);
1092789Sahrens 
1093789Sahrens 	return (error);
1094789Sahrens }
1095789Sahrens 
1096789Sahrens static int
1097789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1098789Sahrens {
1099789Sahrens 	spa_t *spa;
1100789Sahrens 	int error;
1101789Sahrens 
11022926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11032926Sek110237 		return (error);
11042926Sek110237 
11057046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
11062926Sek110237 
11072926Sek110237 	spa_close(spa, FTAG);
11082926Sek110237 
1109789Sahrens 	return (error);
1110789Sahrens }
1111789Sahrens 
1112789Sahrens static int
1113789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1114789Sahrens {
1115789Sahrens 	spa_t *spa;
1116789Sahrens 	int error;
1117789Sahrens 
1118789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1119789Sahrens 	if (error == 0) {
1120789Sahrens 		spa_freeze(spa);
1121789Sahrens 		spa_close(spa, FTAG);
1122789Sahrens 	}
1123789Sahrens 	return (error);
1124789Sahrens }
1125789Sahrens 
1126789Sahrens static int
11271760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11281760Seschrock {
11291760Seschrock 	spa_t *spa;
11301760Seschrock 	int error;
11311760Seschrock 
11322926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11332926Sek110237 		return (error);
11342926Sek110237 
11355118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11365118Slling 		spa_close(spa, FTAG);
11375118Slling 		return (EINVAL);
11385118Slling 	}
11395118Slling 
11405094Slling 	spa_upgrade(spa, zc->zc_cookie);
11412926Sek110237 	spa_close(spa, FTAG);
11422926Sek110237 
11432926Sek110237 	return (error);
11442926Sek110237 }
11452926Sek110237 
11462926Sek110237 static int
11472926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11482926Sek110237 {
11492926Sek110237 	spa_t *spa;
11502926Sek110237 	char *hist_buf;
11512926Sek110237 	uint64_t size;
11522926Sek110237 	int error;
11532926Sek110237 
11542926Sek110237 	if ((size = zc->zc_history_len) == 0)
11552926Sek110237 		return (EINVAL);
11562926Sek110237 
11572926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11582926Sek110237 		return (error);
11592926Sek110237 
11604577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11613863Sek110237 		spa_close(spa, FTAG);
11623863Sek110237 		return (ENOTSUP);
11633863Sek110237 	}
11643863Sek110237 
11652926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11662926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11672926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11689643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
11699643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
11709643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
11712926Sek110237 	}
11722926Sek110237 
11732926Sek110237 	spa_close(spa, FTAG);
11742926Sek110237 	kmem_free(hist_buf, size);
11752926Sek110237 	return (error);
11762926Sek110237 }
11772926Sek110237 
11782926Sek110237 static int
11793444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11803444Sek110237 {
11813444Sek110237 	int error;
11823444Sek110237 
11833912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11843444Sek110237 		return (error);
11853444Sek110237 
11863444Sek110237 	return (0);
11873444Sek110237 }
11883444Sek110237 
11893444Sek110237 static int
11903444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11913444Sek110237 {
11923444Sek110237 	objset_t *osp;
11933444Sek110237 	int error;
11943444Sek110237 
11953444Sek110237 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
11966689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
11973444Sek110237 		return (error);
11983444Sek110237 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
11993444Sek110237 	    sizeof (zc->zc_value));
12003444Sek110237 	dmu_objset_close(osp);
12013444Sek110237 
12023444Sek110237 	return (error);
12033444Sek110237 }
12043444Sek110237 
12053444Sek110237 static int
1206789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1207789Sahrens {
1208789Sahrens 	spa_t *spa;
1209789Sahrens 	int error;
12106423Sgw25295 	nvlist_t *config, **l2cache, **spares;
12116423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1212789Sahrens 
1213789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1214789Sahrens 	if (error != 0)
1215789Sahrens 		return (error);
1216789Sahrens 
12175450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12189643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
12195450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12205450Sbrendan 	    &l2cache, &nl2cache);
12215450Sbrendan 
12226423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12236423Sgw25295 	    &spares, &nspares);
12246423Sgw25295 
12253912Slling 	/*
12263912Slling 	 * A root pool with concatenated devices is not supported.
12276423Sgw25295 	 * Thus, can not add a device to a root pool.
12286423Sgw25295 	 *
12296423Sgw25295 	 * Intent log device can not be added to a rootpool because
12306423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12316423Sgw25295 	 * can not be accessed during the mountroot time.
12326423Sgw25295 	 *
12336423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12343912Slling 	 */
12356423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
12363912Slling 		spa_close(spa, FTAG);
12373912Slling 		return (EDOM);
12383912Slling 	}
12393912Slling 
12405450Sbrendan 	if (error == 0) {
1241789Sahrens 		error = spa_vdev_add(spa, config);
1242789Sahrens 		nvlist_free(config);
1243789Sahrens 	}
1244789Sahrens 	spa_close(spa, FTAG);
1245789Sahrens 	return (error);
1246789Sahrens }
1247789Sahrens 
1248789Sahrens static int
1249789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1250789Sahrens {
12512082Seschrock 	spa_t *spa;
12522082Seschrock 	int error;
12532082Seschrock 
12542082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12552082Seschrock 	if (error != 0)
12562082Seschrock 		return (error);
12572082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12582082Seschrock 	spa_close(spa, FTAG);
12592082Seschrock 	return (error);
1260789Sahrens }
1261789Sahrens 
1262789Sahrens static int
12634451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1264789Sahrens {
1265789Sahrens 	spa_t *spa;
1266789Sahrens 	int error;
12674451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1268789Sahrens 
12692926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1270789Sahrens 		return (error);
12714451Seschrock 	switch (zc->zc_cookie) {
12724451Seschrock 	case VDEV_STATE_ONLINE:
12734451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12744451Seschrock 		break;
12754451Seschrock 
12764451Seschrock 	case VDEV_STATE_OFFLINE:
12774451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12784451Seschrock 		break;
1279789Sahrens 
12804451Seschrock 	case VDEV_STATE_FAULTED:
12814451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12824451Seschrock 		break;
1283789Sahrens 
12844451Seschrock 	case VDEV_STATE_DEGRADED:
12854451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12864451Seschrock 		break;
12874451Seschrock 
12884451Seschrock 	default:
12894451Seschrock 		error = EINVAL;
12904451Seschrock 	}
12914451Seschrock 	zc->zc_cookie = newstate;
1292789Sahrens 	spa_close(spa, FTAG);
1293789Sahrens 	return (error);
1294789Sahrens }
1295789Sahrens 
1296789Sahrens static int
1297789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1298789Sahrens {
1299789Sahrens 	spa_t *spa;
1300789Sahrens 	int replacing = zc->zc_cookie;
1301789Sahrens 	nvlist_t *config;
1302789Sahrens 	int error;
1303789Sahrens 
13042926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1305789Sahrens 		return (error);
1306789Sahrens 
13075094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13089643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
13091544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1310789Sahrens 		nvlist_free(config);
1311789Sahrens 	}
1312789Sahrens 
1313789Sahrens 	spa_close(spa, FTAG);
1314789Sahrens 	return (error);
1315789Sahrens }
1316789Sahrens 
1317789Sahrens static int
1318789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1319789Sahrens {
1320789Sahrens 	spa_t *spa;
1321789Sahrens 	int error;
1322789Sahrens 
13232926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1324789Sahrens 		return (error);
1325789Sahrens 
13268241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1327789Sahrens 
1328789Sahrens 	spa_close(spa, FTAG);
1329789Sahrens 	return (error);
1330789Sahrens }
1331789Sahrens 
1332789Sahrens static int
13331354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13341354Seschrock {
13351354Seschrock 	spa_t *spa;
13362676Seschrock 	char *path = zc->zc_value;
13371544Seschrock 	uint64_t guid = zc->zc_guid;
13381354Seschrock 	int error;
13391354Seschrock 
13401354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13411354Seschrock 	if (error != 0)
13421354Seschrock 		return (error);
13431354Seschrock 
13441354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13451354Seschrock 	spa_close(spa, FTAG);
13461354Seschrock 	return (error);
13471354Seschrock }
13481354Seschrock 
13499425SEric.Schrock@Sun.COM static int
13509425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13519425SEric.Schrock@Sun.COM {
13529425SEric.Schrock@Sun.COM 	spa_t *spa;
13539425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
13549425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
13559425SEric.Schrock@Sun.COM 	int error;
13569425SEric.Schrock@Sun.COM 
13579425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
13589425SEric.Schrock@Sun.COM 	if (error != 0)
13599425SEric.Schrock@Sun.COM 		return (error);
13609425SEric.Schrock@Sun.COM 
13619425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
13629425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
13639425SEric.Schrock@Sun.COM 	return (error);
13649425SEric.Schrock@Sun.COM }
13659425SEric.Schrock@Sun.COM 
13665367Sahrens /*
13675367Sahrens  * inputs:
13685367Sahrens  * zc_name		name of filesystem
13695367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13705367Sahrens  *
13715367Sahrens  * outputs:
13725367Sahrens  * zc_objset_stats	stats
13735367Sahrens  * zc_nvlist_dst	property nvlist
13745367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13755367Sahrens  */
13761354Seschrock static int
1377789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1378789Sahrens {
1379789Sahrens 	objset_t *os = NULL;
1380789Sahrens 	int error;
13811356Seschrock 	nvlist_t *nv;
1382789Sahrens 
13836689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
13846689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1385789Sahrens 		return (error);
1386789Sahrens 
13872885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1388789Sahrens 
13892856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13906689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13912885Sahrens 		dmu_objset_stats(os, nv);
13923087Sahrens 		/*
13935147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13943087Sahrens 		 * which we aren't supposed to do with a
13956689Smaybee 		 * DS_MODE_USER hold, because it could be
13963087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
13973087Sahrens 		 */
13984577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13994577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
14004577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
14014577Sahrens 		}
14022676Seschrock 		error = put_nvlist(zc, nv);
14031356Seschrock 		nvlist_free(nv);
14041356Seschrock 	}
1405789Sahrens 
1406789Sahrens 	dmu_objset_close(os);
1407789Sahrens 	return (error);
1408789Sahrens }
1409789Sahrens 
14105498Stimh static int
14115498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
14125498Stimh {
14135498Stimh 	uint64_t value;
14145498Stimh 	int error;
14155498Stimh 
14165498Stimh 	/*
14175498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14185498Stimh 	 * the default value (if there is one).
14195498Stimh 	 */
14205498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14215498Stimh 		return (error);
14225498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14235498Stimh 	return (0);
14245498Stimh }
14255498Stimh 
14265498Stimh /*
14275498Stimh  * inputs:
14285498Stimh  * zc_name		name of filesystem
14295498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14305498Stimh  *
14315498Stimh  * outputs:
14325498Stimh  * zc_nvlist_dst	zpl property nvlist
14335498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14345498Stimh  */
14355498Stimh static int
14365498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14375498Stimh {
14385498Stimh 	objset_t *os;
14395498Stimh 	int err;
14405498Stimh 
14416689Smaybee 	if (err = dmu_objset_open(zc->zc_name,
14426689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
14435498Stimh 		return (err);
14445498Stimh 
14455498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14465498Stimh 
14475498Stimh 	/*
14485498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14496689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14506689Smaybee 	 * hold, because it could be inconsistent.
14515498Stimh 	 */
14525498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14535498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14545498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14555498Stimh 		nvlist_t *nv;
14565498Stimh 
14575498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14585498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14595498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14605498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14615498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14625498Stimh 			err = put_nvlist(zc, nv);
14635498Stimh 		nvlist_free(nv);
14645498Stimh 	} else {
14655498Stimh 		err = ENOENT;
14665498Stimh 	}
14675498Stimh 	dmu_objset_close(os);
14685498Stimh 	return (err);
14695498Stimh }
14705498Stimh 
14719396SMatthew.Ahrens@Sun.COM static boolean_t
14729396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14739396SMatthew.Ahrens@Sun.COM {
14749396SMatthew.Ahrens@Sun.COM 	/*
14759396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14769396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14779396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14789396SMatthew.Ahrens@Sun.COM 	 */
14799396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14809396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14819396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14829396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14839396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14849396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14859396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14869396SMatthew.Ahrens@Sun.COM }
14879396SMatthew.Ahrens@Sun.COM 
14885367Sahrens /*
14895367Sahrens  * inputs:
14905367Sahrens  * zc_name		name of filesystem
14915367Sahrens  * zc_cookie		zap cursor
14925367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14935367Sahrens  *
14945367Sahrens  * outputs:
14955367Sahrens  * zc_name		name of next filesystem
14969396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14975367Sahrens  * zc_objset_stats	stats
14985367Sahrens  * zc_nvlist_dst	property nvlist
14995367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15005367Sahrens  */
1501789Sahrens static int
1502789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1503789Sahrens {
1504885Sahrens 	objset_t *os;
1505789Sahrens 	int error;
1506789Sahrens 	char *p;
1507789Sahrens 
15086689Smaybee 	if (error = dmu_objset_open(zc->zc_name,
15096689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1510885Sahrens 		if (error == ENOENT)
1511885Sahrens 			error = ESRCH;
1512885Sahrens 		return (error);
1513789Sahrens 	}
1514789Sahrens 
1515789Sahrens 	p = strrchr(zc->zc_name, '/');
1516789Sahrens 	if (p == NULL || p[1] != '\0')
1517789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1518789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1519789Sahrens 
15208697SRichard.Morris@Sun.COM 	/*
15218697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15228697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15238697SRichard.Morris@Sun.COM 	 */
15248415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15258415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15268415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15278415SRichard.Morris@Sun.COM 
15288415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15298697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15308415SRichard.Morris@Sun.COM 	}
15318415SRichard.Morris@Sun.COM 
1532789Sahrens 	do {
1533885Sahrens 		error = dmu_dir_list_next(os,
1534885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1535885Sahrens 		    NULL, &zc->zc_cookie);
1536789Sahrens 		if (error == ENOENT)
1537789Sahrens 			error = ESRCH;
15389396SMatthew.Ahrens@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
15396689Smaybee 	dmu_objset_close(os);
1540789Sahrens 
15419396SMatthew.Ahrens@Sun.COM 	if (error == 0)
1542885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1543789Sahrens 
1544789Sahrens 	return (error);
1545789Sahrens }
1546789Sahrens 
15475367Sahrens /*
15485367Sahrens  * inputs:
15495367Sahrens  * zc_name		name of filesystem
15505367Sahrens  * zc_cookie		zap cursor
15515367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15525367Sahrens  *
15535367Sahrens  * outputs:
15545367Sahrens  * zc_name		name of next snapshot
15555367Sahrens  * zc_objset_stats	stats
15565367Sahrens  * zc_nvlist_dst	property nvlist
15575367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15585367Sahrens  */
1559789Sahrens static int
1560789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1561789Sahrens {
1562885Sahrens 	objset_t *os;
1563789Sahrens 	int error;
1564789Sahrens 
15656689Smaybee 	error = dmu_objset_open(zc->zc_name,
15666689Smaybee 	    DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
15676689Smaybee 	if (error)
15686689Smaybee 		return (error == ENOENT ? ESRCH : error);
1569789Sahrens 
15709396SMatthew.Ahrens@Sun.COM 	if (zc->zc_cookie == 0) {
15718697SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15728415SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
15739396SMatthew.Ahrens@Sun.COM 	}
15741003Slling 	/*
15751003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15761003Slling 	 * so exit immediately.
15771003Slling 	 */
15781003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1579885Sahrens 		dmu_objset_close(os);
15801003Slling 		return (ESRCH);
1581789Sahrens 	}
1582789Sahrens 
1583885Sahrens 	error = dmu_snapshot_list_next(os,
1584885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15855663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
15866689Smaybee 	dmu_objset_close(os);
1587885Sahrens 	if (error == 0)
1588885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15896689Smaybee 	else if (error == ENOENT)
15906689Smaybee 		error = ESRCH;
1591789Sahrens 
15925367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15936689Smaybee 	if (error)
15945367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1595789Sahrens 	return (error);
1596789Sahrens }
1597789Sahrens 
15986423Sgw25295 int
15994787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1600789Sahrens {
16012676Seschrock 	nvpair_t *elem;
16028724SRichard.Morris@Sun.COM 	int error = 0;
16032676Seschrock 	uint64_t intval;
16042676Seschrock 	char *strval;
16058697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
16069396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
16072676Seschrock 
16084543Smarks 	/*
16094543Smarks 	 * First validate permission to set all of the properties
16104543Smarks 	 */
16112676Seschrock 	elem = NULL;
16122676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16134670Sahrens 		const char *propname = nvpair_name(elem);
16144670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16152676Seschrock 
16165094Slling 		if (prop == ZPROP_INVAL) {
16172676Seschrock 			/*
16182676Seschrock 			 * If this is a user-defined property, it must be a
16192676Seschrock 			 * string, and there is no further validation to do.
16202676Seschrock 			 */
16219396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16229396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16239396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16249396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16259396SMatthew.Ahrens@Sun.COM 					return (error);
16269396SMatthew.Ahrens@Sun.COM 				continue;
16279396SMatthew.Ahrens@Sun.COM 			}
16289396SMatthew.Ahrens@Sun.COM 
16299396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16309396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16319396SMatthew.Ahrens@Sun.COM 				const char *perm;
16329396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16339396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16349396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16359396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16369396SMatthew.Ahrens@Sun.COM 				else
16379396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16389396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16399396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16409396SMatthew.Ahrens@Sun.COM 					return (error);
16419396SMatthew.Ahrens@Sun.COM 				continue;
16429396SMatthew.Ahrens@Sun.COM 			}
16439396SMatthew.Ahrens@Sun.COM 
16449396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16452676Seschrock 		}
16462676Seschrock 
16479396SMatthew.Ahrens@Sun.COM 		if (issnap)
16489396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16499396SMatthew.Ahrens@Sun.COM 
16504787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16514670Sahrens 			return (error);
16522676Seschrock 
16534670Sahrens 		/*
16544670Sahrens 		 * Check that this value is valid for this pool version
16554670Sahrens 		 */
16564670Sahrens 		switch (prop) {
16573886Sahl 		case ZFS_PROP_COMPRESSION:
16583886Sahl 			/*
16593886Sahl 			 * If the user specified gzip compression, make sure
16603886Sahl 			 * the SPA supports it. We ignore any errors here since
16613886Sahl 			 * we'll catch them later.
16623886Sahl 			 */
16633886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16647042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16657042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16667042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16677184Stimh 				    zfs_earlier_version(name,
16685331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16695331Samw 					return (ENOTSUP);
16707042Sgw25295 
16717042Sgw25295 				/*
16727042Sgw25295 				 * If this is a bootable dataset then
16737042Sgw25295 				 * verify that the compression algorithm
16747042Sgw25295 				 * is supported for booting. We must return
16757042Sgw25295 				 * something other than ENOTSUP since it
16767042Sgw25295 				 * implies a downrev pool version.
16777042Sgw25295 				 */
16787042Sgw25295 				if (zfs_is_bootfs(name) &&
16797042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16807042Sgw25295 					return (ERANGE);
16813886Sahl 			}
16823886Sahl 			break;
16834603Sahrens 
16844603Sahrens 		case ZFS_PROP_COPIES:
16859396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16865331Samw 				return (ENOTSUP);
16874603Sahrens 			break;
16885977Smarks 
16895977Smarks 		case ZFS_PROP_SHARESMB:
16906689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16915977Smarks 				return (ENOTSUP);
16925977Smarks 			break;
16938053SMark.Shellenbaum@Sun.COM 
16948053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16958053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16968053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16978053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16988053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16998053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
17008053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
17014603Sahrens 		}
17024543Smarks 	}
17034543Smarks 
17048697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17054543Smarks 	elem = NULL;
17064543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
17074670Sahrens 		const char *propname = nvpair_name(elem);
17084670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
17094543Smarks 
17105094Slling 		if (prop == ZPROP_INVAL) {
17119396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
17129396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
17139396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
17149396SMatthew.Ahrens@Sun.COM 				const char *domain;
17159396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
17169396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17179396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17189396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17199396SMatthew.Ahrens@Sun.COM 
17209396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17219396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17229396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17239396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17249396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17259396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
17269396SMatthew.Ahrens@Sun.COM 				domain = propname +
17279396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
17289396SMatthew.Ahrens@Sun.COM 
17299396SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, B_FALSE, FTAG,
17309396SMatthew.Ahrens@Sun.COM 				    &zfsvfs);
17319396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17329396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17339396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17349396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17359396SMatthew.Ahrens@Sun.COM 				}
17369396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17379396SMatthew.Ahrens@Sun.COM 					continue;
17389396SMatthew.Ahrens@Sun.COM 				else
17399396SMatthew.Ahrens@Sun.COM 					goto out;
17409396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17419396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17429396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17439396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17449396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17459396SMatthew.Ahrens@Sun.COM 					continue;
17469396SMatthew.Ahrens@Sun.COM 				else
17479396SMatthew.Ahrens@Sun.COM 					goto out;
17489396SMatthew.Ahrens@Sun.COM 			}
17494543Smarks 		}
17502676Seschrock 
17512676Seschrock 		switch (prop) {
17522676Seschrock 		case ZFS_PROP_QUOTA:
17532676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17544577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17558697SRichard.Morris@Sun.COM 				goto out;
17562676Seschrock 			break;
17572676Seschrock 
17585378Sck153898 		case ZFS_PROP_REFQUOTA:
17595378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17605378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17618697SRichard.Morris@Sun.COM 				goto out;
17625378Sck153898 			break;
17635378Sck153898 
17642676Seschrock 		case ZFS_PROP_RESERVATION:
17652676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17662676Seschrock 			    (error = dsl_dir_set_reservation(name,
17672676Seschrock 			    intval)) != 0)
17688697SRichard.Morris@Sun.COM 				goto out;
17692676Seschrock 			break;
1770789Sahrens 
17715378Sck153898 		case ZFS_PROP_REFRESERVATION:
17725378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17735378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17745378Sck153898 			    intval)) != 0)
17758697SRichard.Morris@Sun.COM 				goto out;
17765378Sck153898 			break;
17775378Sck153898 
17782676Seschrock 		case ZFS_PROP_VOLSIZE:
17792676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17804787Sahrens 			    (error = zvol_set_volsize(name,
17814787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17828697SRichard.Morris@Sun.COM 				goto out;
17832676Seschrock 			break;
17842676Seschrock 
17852676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
17862676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17874577Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
17888697SRichard.Morris@Sun.COM 				goto out;
17894577Sahrens 			break;
17904577Sahrens 
17914577Sahrens 		case ZFS_PROP_VERSION:
17929396SMatthew.Ahrens@Sun.COM 		{
17939396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17949396SMatthew.Ahrens@Sun.COM 
17959396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17969396SMatthew.Ahrens@Sun.COM 				goto out;
17979396SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, B_FALSE, FTAG,
17989396SMatthew.Ahrens@Sun.COM 			    &zfsvfs)) != 0)
17999396SMatthew.Ahrens@Sun.COM 				goto out;
18009396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
18019396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
18029396SMatthew.Ahrens@Sun.COM 
18039396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
18049396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
18059396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
18069396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
18079396SMatthew.Ahrens@Sun.COM 			}
18089396SMatthew.Ahrens@Sun.COM 			if (error)
18098697SRichard.Morris@Sun.COM 				goto out;
18102676Seschrock 			break;
18119396SMatthew.Ahrens@Sun.COM 		}
18122676Seschrock 
18132676Seschrock 		default:
18142676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
18152676Seschrock 				if (zfs_prop_get_type(prop) !=
18168697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18178697SRichard.Morris@Sun.COM 					error = EINVAL;
18188697SRichard.Morris@Sun.COM 					goto out;
18198697SRichard.Morris@Sun.COM 				}
18202676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18212885Sahrens 				const char *unused;
18222885Sahrens 
18232717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18242676Seschrock 
18252676Seschrock 				switch (zfs_prop_get_type(prop)) {
18264787Sahrens 				case PROP_TYPE_NUMBER:
18272676Seschrock 					break;
18284787Sahrens 				case PROP_TYPE_STRING:
18298697SRichard.Morris@Sun.COM 					error = EINVAL;
18308697SRichard.Morris@Sun.COM 					goto out;
18314787Sahrens 				case PROP_TYPE_INDEX:
18322717Seschrock 					if (zfs_prop_index_to_string(prop,
18338697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18348697SRichard.Morris@Sun.COM 						error = EINVAL;
18358697SRichard.Morris@Sun.COM 						goto out;
18368697SRichard.Morris@Sun.COM 					}
18372676Seschrock 					break;
18382676Seschrock 				default:
18394577Sahrens 					cmn_err(CE_PANIC,
18404577Sahrens 					    "unknown property type");
18412676Seschrock 					break;
18422676Seschrock 				}
18432676Seschrock 			} else {
18448697SRichard.Morris@Sun.COM 				error = EINVAL;
18458697SRichard.Morris@Sun.COM 				goto out;
18462676Seschrock 			}
18478697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18488697SRichard.Morris@Sun.COM 				goto out;
18492676Seschrock 		}
18502676Seschrock 	}
18512676Seschrock 
18528697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18538697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18548697SRichard.Morris@Sun.COM 	}
18558697SRichard.Morris@Sun.COM out:
18568697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18578697SRichard.Morris@Sun.COM 	return (error);
1858789Sahrens }
1859789Sahrens 
18605367Sahrens /*
18619355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18629355SMatthew.Ahrens@Sun.COM  */
18639355SMatthew.Ahrens@Sun.COM static int
18649355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18659355SMatthew.Ahrens@Sun.COM {
18669355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18679355SMatthew.Ahrens@Sun.COM 	int error = 0;
18689355SMatthew.Ahrens@Sun.COM 
18699355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18709355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18719355SMatthew.Ahrens@Sun.COM 		char *valstr;
18729355SMatthew.Ahrens@Sun.COM 
18739355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18749355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18759355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18769355SMatthew.Ahrens@Sun.COM 
18779355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18789355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18799355SMatthew.Ahrens@Sun.COM 			return (error);
18809355SMatthew.Ahrens@Sun.COM 
18819355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18829355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18839355SMatthew.Ahrens@Sun.COM 
18849355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18859355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18869355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18879355SMatthew.Ahrens@Sun.COM 	}
18889355SMatthew.Ahrens@Sun.COM 	return (0);
18899355SMatthew.Ahrens@Sun.COM }
18909355SMatthew.Ahrens@Sun.COM 
18919355SMatthew.Ahrens@Sun.COM /*
18925367Sahrens  * inputs:
18935367Sahrens  * zc_name		name of filesystem
18948697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18955367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18967265Sahrens  * zc_cookie		clear existing local props?
18975367Sahrens  *
18985367Sahrens  * outputs:		none
18995367Sahrens  */
1900789Sahrens static int
19012676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1902789Sahrens {
19032676Seschrock 	nvlist_t *nvl;
19042676Seschrock 	int error;
1905789Sahrens 
19065094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19079643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
19082676Seschrock 		return (error);
19092676Seschrock 
19107265Sahrens 	if (zc->zc_cookie) {
19117265Sahrens 		nvlist_t *origprops;
19127265Sahrens 		objset_t *os;
19137265Sahrens 
19147265Sahrens 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
19157265Sahrens 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
19167265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19178536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19187265Sahrens 				nvlist_free(origprops);
19197265Sahrens 			}
19207265Sahrens 			dmu_objset_close(os);
19217265Sahrens 		}
19227265Sahrens 
19237265Sahrens 	}
19247265Sahrens 
19254787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19264543Smarks 
19272676Seschrock 	nvlist_free(nvl);
19282676Seschrock 	return (error);
1929789Sahrens }
1930789Sahrens 
19315367Sahrens /*
19325367Sahrens  * inputs:
19335367Sahrens  * zc_name		name of filesystem
19345367Sahrens  * zc_value		name of property to inherit
19355367Sahrens  *
19365367Sahrens  * outputs:		none
19375367Sahrens  */
1938789Sahrens static int
19394849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19404849Sahrens {
19414849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19424849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19434849Sahrens }
19444849Sahrens 
19454849Sahrens static int
19464098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19473912Slling {
19485094Slling 	nvlist_t *props;
19493912Slling 	spa_t *spa;
19505094Slling 	int error;
19518525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19523912Slling 
19535094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19549643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)))
19553912Slling 		return (error);
19563912Slling 
19578525SEric.Schrock@Sun.COM 	/*
19588525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19598525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19608525SEric.Schrock@Sun.COM 	 */
19618525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19628525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19638525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19648525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19658525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19668525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19678525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19688525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19698525SEric.Schrock@Sun.COM 		}
19708525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19718525SEric.Schrock@Sun.COM 		if (spa != NULL)
19728525SEric.Schrock@Sun.COM 			return (0);
19738525SEric.Schrock@Sun.COM 	}
19748525SEric.Schrock@Sun.COM 
19753912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19765094Slling 		nvlist_free(props);
19773912Slling 		return (error);
19783912Slling 	}
19793912Slling 
19805094Slling 	error = spa_prop_set(spa, props);
19813912Slling 
19825094Slling 	nvlist_free(props);
19833912Slling 	spa_close(spa, FTAG);
19843912Slling 
19853912Slling 	return (error);
19863912Slling }
19873912Slling 
19883912Slling static int
19894098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19903912Slling {
19913912Slling 	spa_t *spa;
19923912Slling 	int error;
19933912Slling 	nvlist_t *nvp = NULL;
19943912Slling 
19958525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19968525SEric.Schrock@Sun.COM 		/*
19978525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19988525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19998525SEric.Schrock@Sun.COM 		 * anyway.
20008525SEric.Schrock@Sun.COM 		 */
20018525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
20028525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
20038525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
20048525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
20058525SEric.Schrock@Sun.COM 	} else {
20068525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
20078525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
20088525SEric.Schrock@Sun.COM 	}
20093912Slling 
20103912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
20113912Slling 		error = put_nvlist(zc, nvp);
20123912Slling 	else
20133912Slling 		error = EFAULT;
20143912Slling 
20158525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
20163912Slling 	return (error);
20173912Slling }
20183912Slling 
20193912Slling static int
20204543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20214543Smarks {
20224543Smarks 	nvlist_t *nvp;
20234543Smarks 	int error;
20244543Smarks 	uint32_t uid;
20254543Smarks 	uint32_t gid;
20264543Smarks 	uint32_t *groups;
20274543Smarks 	uint_t group_cnt;
20284543Smarks 	cred_t	*usercred;
20294543Smarks 
20305094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20319643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
20324543Smarks 		return (error);
20334543Smarks 	}
20344543Smarks 
20354543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20364543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20374543Smarks 		nvlist_free(nvp);
20384543Smarks 		return (EPERM);
20394543Smarks 	}
20404543Smarks 
20414543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20424543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20434543Smarks 		nvlist_free(nvp);
20444543Smarks 		return (EPERM);
20454543Smarks 	}
20464543Smarks 
20474543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20484543Smarks 	    &groups, &group_cnt)) != 0) {
20494543Smarks 		nvlist_free(nvp);
20504543Smarks 		return (EPERM);
20514543Smarks 	}
20524543Smarks 	usercred = cralloc();
20534543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20544543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20554543Smarks 		nvlist_free(nvp);
20564543Smarks 		crfree(usercred);
20574543Smarks 		return (EPERM);
20584543Smarks 	}
20594543Smarks 	nvlist_free(nvp);
20604543Smarks 	error = dsl_deleg_access(zc->zc_name,
20614787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20624543Smarks 	crfree(usercred);
20634543Smarks 	return (error);
20644543Smarks }
20654543Smarks 
20665367Sahrens /*
20675367Sahrens  * inputs:
20685367Sahrens  * zc_name		name of filesystem
20695367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20705367Sahrens  * zc_perm_action	allow/unallow flag
20715367Sahrens  *
20725367Sahrens  * outputs:		none
20735367Sahrens  */
20744543Smarks static int
20754543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20764543Smarks {
20774543Smarks 	int error;
20784543Smarks 	nvlist_t *fsaclnv = NULL;
20794543Smarks 
20805094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20819643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
20824543Smarks 		return (error);
20834543Smarks 
20844543Smarks 	/*
20854543Smarks 	 * Verify nvlist is constructed correctly
20864543Smarks 	 */
20874543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20884543Smarks 		nvlist_free(fsaclnv);
20894543Smarks 		return (EINVAL);
20904543Smarks 	}
20914543Smarks 
20924543Smarks 	/*
20934543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20944543Smarks 	 * that user is allowed to hand out each permission in
20954543Smarks 	 * the nvlist(s)
20964543Smarks 	 */
20974543Smarks 
20984787Sahrens 	error = secpolicy_zfs(CRED());
20994543Smarks 	if (error) {
21004787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
21014787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
21024787Sahrens 			    fsaclnv, CRED());
21034787Sahrens 		} else {
21044787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
21054787Sahrens 			    fsaclnv, CRED());
21064787Sahrens 		}
21074543Smarks 	}
21084543Smarks 
21094543Smarks 	if (error == 0)
21104543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
21114543Smarks 
21124543Smarks 	nvlist_free(fsaclnv);
21134543Smarks 	return (error);
21144543Smarks }
21154543Smarks 
21165367Sahrens /*
21175367Sahrens  * inputs:
21185367Sahrens  * zc_name		name of filesystem
21195367Sahrens  *
21205367Sahrens  * outputs:
21215367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21225367Sahrens  */
21234543Smarks static int
21244543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21254543Smarks {
21264543Smarks 	nvlist_t *nvp;
21274543Smarks 	int error;
21284543Smarks 
21294543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21304543Smarks 		error = put_nvlist(zc, nvp);
21314543Smarks 		nvlist_free(nvp);
21324543Smarks 	}
21334543Smarks 
21344543Smarks 	return (error);
21354543Smarks }
21364543Smarks 
21375367Sahrens /*
21385367Sahrens  * inputs:
21395367Sahrens  * zc_name		name of volume
21405367Sahrens  *
21415367Sahrens  * outputs:		none
21425367Sahrens  */
21434543Smarks static int
2144789Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
2145789Sahrens {
21464787Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2147789Sahrens }
2148789Sahrens 
21495367Sahrens /*
21505367Sahrens  * inputs:
21515367Sahrens  * zc_name		name of volume
21525367Sahrens  *
21535367Sahrens  * outputs:		none
21545367Sahrens  */
2155789Sahrens static int
2156789Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
2157789Sahrens {
21582676Seschrock 	return (zvol_remove_minor(zc->zc_name));
2159789Sahrens }
2160789Sahrens 
2161789Sahrens /*
2162789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2163789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2164789Sahrens  * is responsible for releasing the returned vfs pointer.
2165789Sahrens  */
2166789Sahrens static vfs_t *
2167789Sahrens zfs_get_vfs(const char *resource)
2168789Sahrens {
2169789Sahrens 	struct vfs *vfsp;
2170789Sahrens 	struct vfs *vfs_found = NULL;
2171789Sahrens 
2172789Sahrens 	vfs_list_read_lock();
2173789Sahrens 	vfsp = rootvfs;
2174789Sahrens 	do {
2175789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2176789Sahrens 			VFS_HOLD(vfsp);
2177789Sahrens 			vfs_found = vfsp;
2178789Sahrens 			break;
2179789Sahrens 		}
2180789Sahrens 		vfsp = vfsp->vfs_next;
2181789Sahrens 	} while (vfsp != rootvfs);
2182789Sahrens 	vfs_list_unlock();
2183789Sahrens 	return (vfs_found);
2184789Sahrens }
2185789Sahrens 
21864543Smarks /* ARGSUSED */
2187789Sahrens static void
21884543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2189789Sahrens {
21905331Samw 	zfs_creat_t *zct = arg;
21915498Stimh 
21925498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21935331Samw }
21945331Samw 
21955498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21965498Stimh 
21975331Samw /*
21985498Stimh  * inputs:
21997184Stimh  * createprops		list of properties requested by creator
22007184Stimh  * default_zplver	zpl version to use if unspecified in createprops
22017184Stimh  * fuids_ok		fuids allowed in this version of the spa?
22027184Stimh  * os			parent objset pointer (NULL if root fs)
22035331Samw  *
22045498Stimh  * outputs:
22055498Stimh  * zplprops	values for the zplprops we attach to the master node object
22067184Stimh  * is_ci	true if requested file system will be purely case-insensitive
22075331Samw  *
22085498Stimh  * Determine the settings for utf8only, normalization and
22095498Stimh  * casesensitivity.  Specific values may have been requested by the
22105498Stimh  * creator and/or we can inherit values from the parent dataset.  If
22115498Stimh  * the file system is of too early a vintage, a creator can not
22125498Stimh  * request settings for these properties, even if the requested
22135498Stimh  * setting is the default value.  We don't actually want to create dsl
22145498Stimh  * properties for these, so remove them from the source nvlist after
22155498Stimh  * processing.
22165331Samw  */
22175331Samw static int
22189396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
22197184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
22207184Stimh     boolean_t *is_ci)
22215331Samw {
22225498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
22235498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
22245498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
22255498Stimh 
22265498Stimh 	ASSERT(zplprops != NULL);
22275498Stimh 
22285375Stimh 	/*
22295498Stimh 	 * Pull out creator prop choices, if any.
22305375Stimh 	 */
22315498Stimh 	if (createprops) {
22325498Stimh 		(void) nvlist_lookup_uint64(createprops,
22337184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22347184Stimh 		(void) nvlist_lookup_uint64(createprops,
22355498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22365498Stimh 		(void) nvlist_remove_all(createprops,
22375498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22385498Stimh 		(void) nvlist_lookup_uint64(createprops,
22395498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22405498Stimh 		(void) nvlist_remove_all(createprops,
22415498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22425498Stimh 		(void) nvlist_lookup_uint64(createprops,
22435498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22445498Stimh 		(void) nvlist_remove_all(createprops,
22455498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22465331Samw 	}
22475331Samw 
22485375Stimh 	/*
22497184Stimh 	 * If the zpl version requested is whacky or the file system
22507184Stimh 	 * or pool is version is too "young" to support normalization
22517184Stimh 	 * and the creator tried to set a value for one of the props,
22527184Stimh 	 * error out.
22535498Stimh 	 */
22547184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22557184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22567184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22575498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22587184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22595498Stimh 		return (ENOTSUP);
22605498Stimh 
22615498Stimh 	/*
22625498Stimh 	 * Put the version in the zplprops
22635498Stimh 	 */
22645498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22655498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22665498Stimh 
22675498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22685498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22695498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22705498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22715498Stimh 
22725498Stimh 	/*
22735498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22745498Stimh 	 */
22755498Stimh 	if (norm)
22765498Stimh 		u8 = 1;
22775498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22785498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22795498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22805498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22815498Stimh 
22825498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22835498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22845498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22855498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22865498Stimh 
22876492Stimh 	if (is_ci)
22886492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22896492Stimh 
22907184Stimh 	return (0);
22917184Stimh }
22927184Stimh 
22937184Stimh static int
22947184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22957184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22967184Stimh {
22977184Stimh 	boolean_t fuids_ok = B_TRUE;
22987184Stimh 	uint64_t zplver = ZPL_VERSION;
22997184Stimh 	objset_t *os = NULL;
23007184Stimh 	char parentname[MAXNAMELEN];
23017184Stimh 	char *cp;
23027184Stimh 	int error;
23037184Stimh 
23047184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
23057184Stimh 	cp = strrchr(parentname, '/');
23067184Stimh 	ASSERT(cp != NULL);
23077184Stimh 	cp[0] = '\0';
23087184Stimh 
23099396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
23109396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
23117184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
23127184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23137184Stimh 		fuids_ok = B_FALSE;
23147184Stimh 	}
23157184Stimh 
23167184Stimh 	/*
23177184Stimh 	 * Open parent object set so we can inherit zplprop values.
23187184Stimh 	 */
23197184Stimh 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
23207184Stimh 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
23217184Stimh 		return (error);
23227184Stimh 
23237184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
23247184Stimh 	    zplprops, is_ci);
23255498Stimh 	dmu_objset_close(os);
23267184Stimh 	return (error);
23277184Stimh }
23287184Stimh 
23297184Stimh static int
23307184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
23317184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
23327184Stimh {
23337184Stimh 	boolean_t fuids_ok = B_TRUE;
23347184Stimh 	uint64_t zplver = ZPL_VERSION;
23357184Stimh 	int error;
23367184Stimh 
23377184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23387184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23397184Stimh 		fuids_ok = B_FALSE;
23407184Stimh 	}
23417184Stimh 
23427184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23437184Stimh 	    zplprops, is_ci);
23447184Stimh 	return (error);
2345789Sahrens }
2346789Sahrens 
23475367Sahrens /*
23485367Sahrens  * inputs:
23495367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23505367Sahrens  * zc_name		name of new objset
23515367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23525367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23535367Sahrens  *
23545498Stimh  * outputs: none
23555367Sahrens  */
2356789Sahrens static int
2357789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2358789Sahrens {
2359789Sahrens 	objset_t *clone;
2360789Sahrens 	int error = 0;
23615331Samw 	zfs_creat_t zct;
23624543Smarks 	nvlist_t *nvprops = NULL;
23634543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2364789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2365789Sahrens 
2366789Sahrens 	switch (type) {
2367789Sahrens 
2368789Sahrens 	case DMU_OST_ZFS:
2369789Sahrens 		cbfunc = zfs_create_cb;
2370789Sahrens 		break;
2371789Sahrens 
2372789Sahrens 	case DMU_OST_ZVOL:
2373789Sahrens 		cbfunc = zvol_create_cb;
2374789Sahrens 		break;
2375789Sahrens 
2376789Sahrens 	default:
23772199Sahrens 		cbfunc = NULL;
23786423Sgw25295 		break;
23792199Sahrens 	}
23805326Sek110237 	if (strchr(zc->zc_name, '@') ||
23815326Sek110237 	    strchr(zc->zc_name, '%'))
2382789Sahrens 		return (EINVAL);
2383789Sahrens 
23842676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23855094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23869643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
23872676Seschrock 		return (error);
23882676Seschrock 
23895498Stimh 	zct.zct_zplprops = NULL;
23905331Samw 	zct.zct_props = nvprops;
23915331Samw 
23922676Seschrock 	if (zc->zc_value[0] != '\0') {
2393789Sahrens 		/*
2394789Sahrens 		 * We're creating a clone of an existing snapshot.
2395789Sahrens 		 */
23962676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23972676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23984543Smarks 			nvlist_free(nvprops);
2399789Sahrens 			return (EINVAL);
24002676Seschrock 		}
2401789Sahrens 
24022676Seschrock 		error = dmu_objset_open(zc->zc_value, type,
24036689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
24042676Seschrock 		if (error) {
24054543Smarks 			nvlist_free(nvprops);
2406789Sahrens 			return (error);
24072676Seschrock 		}
24086492Stimh 
2409*10272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2410*10272SMatthew.Ahrens@Sun.COM 		dmu_objset_close(clone);
24115331Samw 		if (error) {
24125331Samw 			nvlist_free(nvprops);
24135331Samw 			return (error);
24145331Samw 		}
2415789Sahrens 	} else {
24166492Stimh 		boolean_t is_insensitive = B_FALSE;
24176492Stimh 
24182676Seschrock 		if (cbfunc == NULL) {
24194543Smarks 			nvlist_free(nvprops);
24202199Sahrens 			return (EINVAL);
24212676Seschrock 		}
24222676Seschrock 
2423789Sahrens 		if (type == DMU_OST_ZVOL) {
24242676Seschrock 			uint64_t volsize, volblocksize;
24252676Seschrock 
24264543Smarks 			if (nvprops == NULL ||
24274543Smarks 			    nvlist_lookup_uint64(nvprops,
24282676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
24292676Seschrock 			    &volsize) != 0) {
24304543Smarks 				nvlist_free(nvprops);
24312676Seschrock 				return (EINVAL);
24322676Seschrock 			}
24332676Seschrock 
24344543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24352676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24362676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24374543Smarks 				nvlist_free(nvprops);
24382676Seschrock 				return (EINVAL);
24392676Seschrock 			}
24401133Seschrock 
24412676Seschrock 			if (error != 0)
24422676Seschrock 				volblocksize = zfs_prop_default_numeric(
24432676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24442676Seschrock 
24452676Seschrock 			if ((error = zvol_check_volblocksize(
24462676Seschrock 			    volblocksize)) != 0 ||
24472676Seschrock 			    (error = zvol_check_volsize(volsize,
24482676Seschrock 			    volblocksize)) != 0) {
24494543Smarks 				nvlist_free(nvprops);
2450789Sahrens 				return (error);
24512676Seschrock 			}
24524577Sahrens 		} else if (type == DMU_OST_ZFS) {
24535331Samw 			int error;
24545331Samw 
24555498Stimh 			/*
24565331Samw 			 * We have to have normalization and
24575331Samw 			 * case-folding flags correct when we do the
24585331Samw 			 * file system creation, so go figure them out
24595498Stimh 			 * now.
24605331Samw 			 */
24615498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24625498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24635498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24647184Stimh 			    zct.zct_zplprops, &is_insensitive);
24655331Samw 			if (error != 0) {
24665331Samw 				nvlist_free(nvprops);
24675498Stimh 				nvlist_free(zct.zct_zplprops);
24685331Samw 				return (error);
24694577Sahrens 			}
24702676Seschrock 		}
2471*10272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
24726492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24735498Stimh 		nvlist_free(zct.zct_zplprops);
2474789Sahrens 	}
24752676Seschrock 
24762676Seschrock 	/*
24772676Seschrock 	 * It would be nice to do this atomically.
24782676Seschrock 	 */
24792676Seschrock 	if (error == 0) {
24804787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
248110242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
24822676Seschrock 	}
24834543Smarks 	nvlist_free(nvprops);
2484789Sahrens 	return (error);
2485789Sahrens }
2486789Sahrens 
24875367Sahrens /*
24885367Sahrens  * inputs:
24895367Sahrens  * zc_name	name of filesystem
24905367Sahrens  * zc_value	short name of snapshot
24915367Sahrens  * zc_cookie	recursive flag
24929396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24935367Sahrens  *
24945367Sahrens  * outputs:	none
24955367Sahrens  */
2496789Sahrens static int
24972199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24982199Sahrens {
24997265Sahrens 	nvlist_t *nvprops = NULL;
25007265Sahrens 	int error;
25017265Sahrens 	boolean_t recursive = zc->zc_cookie;
25027265Sahrens 
25032676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25042199Sahrens 		return (EINVAL);
25057265Sahrens 
25067265Sahrens 	if (zc->zc_nvlist_src != NULL &&
25077265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
25089643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
25097265Sahrens 		return (error);
25107265Sahrens 
25119355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
25129355SMatthew.Ahrens@Sun.COM 	if (error)
25139355SMatthew.Ahrens@Sun.COM 		goto out;
25149355SMatthew.Ahrens@Sun.COM 
25159355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
25169355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
25179355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
25189355SMatthew.Ahrens@Sun.COM 		goto out;
25197265Sahrens 	}
25209355SMatthew.Ahrens@Sun.COM 
25219355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
25229355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
25239355SMatthew.Ahrens@Sun.COM 
25249355SMatthew.Ahrens@Sun.COM out:
25257265Sahrens 	nvlist_free(nvprops);
25267265Sahrens 	return (error);
25272199Sahrens }
25282199Sahrens 
25294007Smmusante int
25302199Sahrens zfs_unmount_snap(char *name, void *arg)
2531789Sahrens {
25322417Sahrens 	vfs_t *vfsp = NULL;
25332199Sahrens 
25346689Smaybee 	if (arg) {
25356689Smaybee 		char *snapname = arg;
25366689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25376689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25386689Smaybee 
25396689Smaybee 		(void) strcpy(buf, name);
25406689Smaybee 		(void) strcat(buf, "@");
25416689Smaybee 		(void) strcat(buf, snapname);
25426689Smaybee 		vfsp = zfs_get_vfs(buf);
25436689Smaybee 		kmem_free(buf, len);
25442417Sahrens 	} else if (strchr(name, '@')) {
25452199Sahrens 		vfsp = zfs_get_vfs(name);
25462199Sahrens 	}
25472199Sahrens 
25482199Sahrens 	if (vfsp) {
25492199Sahrens 		/*
25502199Sahrens 		 * Always force the unmount for snapshots.
25512199Sahrens 		 */
25522199Sahrens 		int flag = MS_FORCE;
2553789Sahrens 		int err;
2554789Sahrens 
25552199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25562199Sahrens 			VFS_RELE(vfsp);
25572199Sahrens 			return (err);
25582199Sahrens 		}
25592199Sahrens 		VFS_RELE(vfsp);
25602199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25612199Sahrens 			return (err);
25622199Sahrens 	}
25632199Sahrens 	return (0);
25642199Sahrens }
25652199Sahrens 
25665367Sahrens /*
25675367Sahrens  * inputs:
256810242Schris.kirby@sun.com  * zc_name		name of filesystem
256910242Schris.kirby@sun.com  * zc_value		short name of snapshot
257010242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25715367Sahrens  *
25725367Sahrens  * outputs:	none
25735367Sahrens  */
25742199Sahrens static int
25752199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25762199Sahrens {
25772199Sahrens 	int err;
2578789Sahrens 
25792676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25802199Sahrens 		return (EINVAL);
25812199Sahrens 	err = dmu_objset_find(zc->zc_name,
25822676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25832199Sahrens 	if (err)
25842199Sahrens 		return (err);
258510242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
258610242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
25872199Sahrens }
25882199Sahrens 
25895367Sahrens /*
25905367Sahrens  * inputs:
25915367Sahrens  * zc_name		name of dataset to destroy
25925367Sahrens  * zc_objset_type	type of objset
259310242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25945367Sahrens  *
25955367Sahrens  * outputs:		none
25965367Sahrens  */
25972199Sahrens static int
25982199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25992199Sahrens {
26002199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
26012199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26022199Sahrens 		if (err)
26032199Sahrens 			return (err);
2604789Sahrens 	}
2605789Sahrens 
260610242Schris.kirby@sun.com 	return (dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy));
2607789Sahrens }
2608789Sahrens 
26095367Sahrens /*
26105367Sahrens  * inputs:
26115446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
26125367Sahrens  *
26135367Sahrens  * outputs:	none
26145367Sahrens  */
2615789Sahrens static int
2616789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2617789Sahrens {
2618*10272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
26195446Sahrens 	int error;
2620*10272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
2621*10272SMatthew.Ahrens@Sun.COM 	char *clone_name;
2622*10272SMatthew.Ahrens@Sun.COM 
2623*10272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
2624*10272SMatthew.Ahrens@Sun.COM 	if (error)
2625*10272SMatthew.Ahrens@Sun.COM 		return (error);
2626*10272SMatthew.Ahrens@Sun.COM 
2627*10272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
2628*10272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
2629*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
2630*10272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
2631*10272SMatthew.Ahrens@Sun.COM 	}
2632*10272SMatthew.Ahrens@Sun.COM 
2633*10272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
2634*10272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
2635*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
2636*10272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
2637*10272SMatthew.Ahrens@Sun.COM 	}
26385446Sahrens 
26395446Sahrens 	/*
2640*10272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
26415446Sahrens 	 */
2642*10272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
2643*10272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26445446Sahrens 	if (error)
2645*10272SMatthew.Ahrens@Sun.COM 		goto out;
2646*10272SMatthew.Ahrens@Sun.COM 
2647*10272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, DS_MODE_INCONSISTENT, FTAG, &clone);
2648*10272SMatthew.Ahrens@Sun.COM 	if (error)
2649*10272SMatthew.Ahrens@Sun.COM 		goto out;
2650*10272SMatthew.Ahrens@Sun.COM 
2651*10272SMatthew.Ahrens@Sun.COM 	/*
2652*10272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
2653*10272SMatthew.Ahrens@Sun.COM 	 */
26549396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
26555446Sahrens 		int mode;
26565446Sahrens 
26579396SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs, NULL, &mode);
26586083Sek110237 		if (error == 0) {
26596083Sek110237 			int resume_err;
26606083Sek110237 
2661*10272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2662*10272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
2663*10272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
2664*10272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
2665*10272SMatthew.Ahrens@Sun.COM 				ds = NULL;
2666*10272SMatthew.Ahrens@Sun.COM 			} else {
2667*10272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
2668*10272SMatthew.Ahrens@Sun.COM 			}
26699396SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
26706083Sek110237 			error = error ? error : resume_err;
26716083Sek110237 		}
26725446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26735446Sahrens 	} else {
2674*10272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2675*10272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
2676*10272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
2677*10272SMatthew.Ahrens@Sun.COM 			ds = NULL;
2678*10272SMatthew.Ahrens@Sun.COM 		} else {
2679*10272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
2680*10272SMatthew.Ahrens@Sun.COM 		}
26815446Sahrens 	}
2682*10272SMatthew.Ahrens@Sun.COM 
2683*10272SMatthew.Ahrens@Sun.COM 	/*
2684*10272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
2685*10272SMatthew.Ahrens@Sun.COM 	 */
2686*10272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
2687*10272SMatthew.Ahrens@Sun.COM 
2688*10272SMatthew.Ahrens@Sun.COM out:
2689*10272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
2690*10272SMatthew.Ahrens@Sun.COM 	if (ds)
2691*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26925446Sahrens 	return (error);
2693789Sahrens }
2694789Sahrens 
26955367Sahrens /*
26965367Sahrens  * inputs:
26975367Sahrens  * zc_name	old name of dataset
26985367Sahrens  * zc_value	new name of dataset
26995367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
27005367Sahrens  *
27015367Sahrens  * outputs:	none
27025367Sahrens  */
2703789Sahrens static int
2704789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2705789Sahrens {
27064490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
27074007Smmusante 
27082676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
27095326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27105326Sek110237 	    strchr(zc->zc_value, '%'))
2711789Sahrens 		return (EINVAL);
2712789Sahrens 
27134007Smmusante 	/*
27144007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
27154007Smmusante 	 * in which case the dataset code figures out which snapshots
27164007Smmusante 	 * to unmount.
27174007Smmusante 	 */
27184007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2719789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
27202199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
27212199Sahrens 		if (err)
27222199Sahrens 			return (err);
2723789Sahrens 	}
27244007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2725789Sahrens }
2726789Sahrens 
27276689Smaybee static void
27288536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
27296689Smaybee {
27306689Smaybee 	zfs_cmd_t *zc;
27316689Smaybee 	nvpair_t *prop;
27326689Smaybee 
27336689Smaybee 	if (props == NULL)
27346689Smaybee 		return;
27356689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
27366689Smaybee 	(void) strcpy(zc->zc_name, dataset);
27376689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
27386689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27398536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
27408536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
27418536SDavid.Pacheco@Sun.COM 			continue;
27426689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
27436689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
27446689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
27456689Smaybee 	}
27466689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
27476689Smaybee }
27486689Smaybee 
27495367Sahrens /*
27505367Sahrens  * inputs:
27515367Sahrens  * zc_name		name of containing filesystem
27525367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27535367Sahrens  * zc_value		name of snapshot to create
27545367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27555367Sahrens  * zc_cookie		file descriptor to recv from
27565367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27575367Sahrens  * zc_guid		force flag
27585367Sahrens  *
27595367Sahrens  * outputs:
27605367Sahrens  * zc_cookie		number of bytes read
27615367Sahrens  */
2762789Sahrens static int
27635367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2764789Sahrens {
2765789Sahrens 	file_t *fp;
27665326Sek110237 	objset_t *os;
27675367Sahrens 	dmu_recv_cookie_t drc;
27685326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2769789Sahrens 	int error, fd;
27705367Sahrens 	offset_t off;
27715367Sahrens 	nvlist_t *props = NULL;
27726689Smaybee 	nvlist_t *origprops = NULL;
27735367Sahrens 	objset_t *origin = NULL;
27745367Sahrens 	char *tosnap;
27755367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2776789Sahrens 
27773265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27785326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27795326Sek110237 	    strchr(zc->zc_value, '%'))
27803265Sahrens 		return (EINVAL);
27813265Sahrens 
27825367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27835367Sahrens 	tosnap = strchr(tofs, '@');
27845367Sahrens 	*tosnap = '\0';
27855367Sahrens 	tosnap++;
27865367Sahrens 
27875367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27885367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27899643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27905367Sahrens 		return (error);
27915367Sahrens 
2792789Sahrens 	fd = zc->zc_cookie;
2793789Sahrens 	fp = getf(fd);
27945367Sahrens 	if (fp == NULL) {
27955367Sahrens 		nvlist_free(props);
2796789Sahrens 		return (EBADF);
27975367Sahrens 	}
27985326Sek110237 
279910204SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_open(tofs, DMU_OST_ANY,
280010204SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
28016689Smaybee 		/*
28026689Smaybee 		 * If new properties are supplied, they are to completely
28036689Smaybee 		 * replace the existing ones, so stash away the existing ones.
28046689Smaybee 		 */
28059396SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, TRUE);
28066689Smaybee 
28075326Sek110237 		dmu_objset_close(os);
28085326Sek110237 	}
28095326Sek110237 
28105367Sahrens 	if (zc->zc_string[0]) {
28115367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
28126689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
28136689Smaybee 		if (error)
28146689Smaybee 			goto out;
28155367Sahrens 	}
28165367Sahrens 
28175367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
281810204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
28195367Sahrens 	if (origin)
28205367Sahrens 		dmu_objset_close(origin);
28216689Smaybee 	if (error)
28226689Smaybee 		goto out;
28235326Sek110237 
28245326Sek110237 	/*
28256689Smaybee 	 * Reset properties.  We do this before we receive the stream
28266689Smaybee 	 * so that the properties are applied to the new data.
28275326Sek110237 	 */
28285367Sahrens 	if (props) {
28298536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
28306689Smaybee 		/*
28316689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
28326689Smaybee 		 */
28336689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
28345367Sahrens 	}
28355367Sahrens 
28365367Sahrens 	off = fp->f_offset;
28375367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
28385367Sahrens 
283910204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
284010204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
284110204SMatthew.Ahrens@Sun.COM 
284210204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
284310204SMatthew.Ahrens@Sun.COM 			/* online recv */
284410204SMatthew.Ahrens@Sun.COM 			int end_err;
284510204SMatthew.Ahrens@Sun.COM 			char *osname;
284610204SMatthew.Ahrens@Sun.COM 			int mode;
284710204SMatthew.Ahrens@Sun.COM 
284810204SMatthew.Ahrens@Sun.COM 			osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
284910204SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, osname, &mode);
285010204SMatthew.Ahrens@Sun.COM 			/*
285110204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
285210204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
285310204SMatthew.Ahrens@Sun.COM 			 */
285410204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
285510204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
285610204SMatthew.Ahrens@Sun.COM 				int resume_err =
285710204SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, osname, mode);
285810204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
285910204SMatthew.Ahrens@Sun.COM 			}
286010204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
286110204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
286210204SMatthew.Ahrens@Sun.COM 			kmem_free(osname, MAXNAMELEN);
286310204SMatthew.Ahrens@Sun.COM 		} else {
28646689Smaybee 			error = dmu_recv_end(&drc);
28655367Sahrens 		}
28666083Sek110237 	}
28675367Sahrens 
28685367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28695367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28705367Sahrens 		fp->f_offset = off;
28712885Sahrens 
28726689Smaybee 	/*
28736689Smaybee 	 * On error, restore the original props.
28746689Smaybee 	 */
28756689Smaybee 	if (error && props) {
28768536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28776689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28786689Smaybee 	}
28796689Smaybee out:
28806689Smaybee 	nvlist_free(props);
28816689Smaybee 	nvlist_free(origprops);
2882789Sahrens 	releasef(fd);
2883789Sahrens 	return (error);
2884789Sahrens }
2885789Sahrens 
28865367Sahrens /*
28875367Sahrens  * inputs:
28885367Sahrens  * zc_name	name of snapshot to send
28895367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28905367Sahrens  * zc_cookie	file descriptor to send stream to
28915367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28925367Sahrens  *
28935367Sahrens  * outputs: none
28945367Sahrens  */
2895789Sahrens static int
28965367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2897789Sahrens {
2898789Sahrens 	objset_t *fromsnap = NULL;
2899789Sahrens 	objset_t *tosnap;
2900789Sahrens 	file_t *fp;
2901789Sahrens 	int error;
29025367Sahrens 	offset_t off;
2903789Sahrens 
2904789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
29056689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2906789Sahrens 	if (error)
2907789Sahrens 		return (error);
2908789Sahrens 
29092676Seschrock 	if (zc->zc_value[0] != '\0') {
29108012SEric.Taylor@Sun.COM 		char *buf;
29112885Sahrens 		char *cp;
29122885Sahrens 
29138012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
29148012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
29152885Sahrens 		cp = strchr(buf, '@');
29162885Sahrens 		if (cp)
29172885Sahrens 			*(cp+1) = 0;
29188012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
29192885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
29206689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
29218012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2922789Sahrens 		if (error) {
2923789Sahrens 			dmu_objset_close(tosnap);
2924789Sahrens 			return (error);
2925789Sahrens 		}
2926789Sahrens 	}
2927789Sahrens 
2928789Sahrens 	fp = getf(zc->zc_cookie);
2929789Sahrens 	if (fp == NULL) {
2930789Sahrens 		dmu_objset_close(tosnap);
2931789Sahrens 		if (fromsnap)
2932789Sahrens 			dmu_objset_close(fromsnap);
2933789Sahrens 		return (EBADF);
2934789Sahrens 	}
2935789Sahrens 
29365367Sahrens 	off = fp->f_offset;
29375367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
29385367Sahrens 
29395367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
29405367Sahrens 		fp->f_offset = off;
2941789Sahrens 	releasef(zc->zc_cookie);
2942789Sahrens 	if (fromsnap)
2943789Sahrens 		dmu_objset_close(fromsnap);
2944789Sahrens 	dmu_objset_close(tosnap);
2945789Sahrens 	return (error);
2946789Sahrens }
2947789Sahrens 
29481544Seschrock static int
29491544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29501544Seschrock {
29511544Seschrock 	int id, error;
29521544Seschrock 
29531544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29541544Seschrock 	    &zc->zc_inject_record);
29551544Seschrock 
29561544Seschrock 	if (error == 0)
29571544Seschrock 		zc->zc_guid = (uint64_t)id;
29581544Seschrock 
29591544Seschrock 	return (error);
29601544Seschrock }
29611544Seschrock 
29621544Seschrock static int
29631544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29641544Seschrock {
29651544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29661544Seschrock }
29671544Seschrock 
29681544Seschrock static int
29691544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29701544Seschrock {
29711544Seschrock 	int id = (int)zc->zc_guid;
29721544Seschrock 	int error;
29731544Seschrock 
29741544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29751544Seschrock 	    &zc->zc_inject_record);
29761544Seschrock 
29771544Seschrock 	zc->zc_guid = id;
29781544Seschrock 
29791544Seschrock 	return (error);
29801544Seschrock }
29811544Seschrock 
29821544Seschrock static int
29831544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29841544Seschrock {
29851544Seschrock 	spa_t *spa;
29861544Seschrock 	int error;
29872676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29881544Seschrock 
29891544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29901544Seschrock 		return (error);
29911544Seschrock 
29922676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29931544Seschrock 	    &count);
29941544Seschrock 	if (error == 0)
29952676Seschrock 		zc->zc_nvlist_dst_size = count;
29961544Seschrock 	else
29972676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29981544Seschrock 
29991544Seschrock 	spa_close(spa, FTAG);
30001544Seschrock 
30011544Seschrock 	return (error);
30021544Seschrock }
30031544Seschrock 
30041544Seschrock static int
30051544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
30061544Seschrock {
30071544Seschrock 	spa_t *spa;
30081544Seschrock 	vdev_t *vd;
30091544Seschrock 	int error;
30101544Seschrock 
30117294Sperrin 	/*
30127294Sperrin 	 * On zpool clear we also fix up missing slogs
30137294Sperrin 	 */
30147294Sperrin 	mutex_enter(&spa_namespace_lock);
30157294Sperrin 	spa = spa_lookup(zc->zc_name);
30167294Sperrin 	if (spa == NULL) {
30177294Sperrin 		mutex_exit(&spa_namespace_lock);
30187294Sperrin 		return (EIO);
30197294Sperrin 	}
30207294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
30217294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
30227294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
30237294Sperrin 	}
30247294Sperrin 	mutex_exit(&spa_namespace_lock);
30257294Sperrin 
30261544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
30271544Seschrock 		return (error);
30281544Seschrock 
30297754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
30301544Seschrock 
30312676Seschrock 	if (zc->zc_guid == 0) {
30321544Seschrock 		vd = NULL;
30336643Seschrock 	} else {
30346643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
30355450Sbrendan 		if (vd == NULL) {
30367754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
30375450Sbrendan 			spa_close(spa, FTAG);
30385450Sbrendan 			return (ENODEV);
30395450Sbrendan 		}
30401544Seschrock 	}
30411544Seschrock 
30427754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
30437754SJeff.Bonwick@Sun.COM 
30447754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
30457754SJeff.Bonwick@Sun.COM 
30467754SJeff.Bonwick@Sun.COM 	/*
30477754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
30487754SJeff.Bonwick@Sun.COM 	 */
30499234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30509234SGeorge.Wilson@Sun.COM 		error = EIO;
30511544Seschrock 
30521544Seschrock 	spa_close(spa, FTAG);
30531544Seschrock 
30549234SGeorge.Wilson@Sun.COM 	return (error);
30551544Seschrock }
30561544Seschrock 
30575367Sahrens /*
30585367Sahrens  * inputs:
30595367Sahrens  * zc_name	name of filesystem
30605367Sahrens  * zc_value	name of origin snapshot
30615367Sahrens  *
30625367Sahrens  * outputs:	none
30635367Sahrens  */
30641544Seschrock static int
30652082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30662082Seschrock {
30672417Sahrens 	char *cp;
30682417Sahrens 
30692417Sahrens 	/*
30702417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30712417Sahrens 	 * it's easier.
30722417Sahrens 	 */
30732676Seschrock 	cp = strchr(zc->zc_value, '@');
30742417Sahrens 	if (cp)
30752417Sahrens 		*cp = '\0';
30762676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30772417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
30782082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
30792082Seschrock }
30802082Seschrock 
30814543Smarks /*
30829396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30839396SMatthew.Ahrens@Sun.COM  *
30849396SMatthew.Ahrens@Sun.COM  * inputs:
30859396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30869396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30879396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30889396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30899396SMatthew.Ahrens@Sun.COM  *
30909396SMatthew.Ahrens@Sun.COM  * outputs:
30919396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30929396SMatthew.Ahrens@Sun.COM  */
30939396SMatthew.Ahrens@Sun.COM static int
30949396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30959396SMatthew.Ahrens@Sun.COM {
30969396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30979396SMatthew.Ahrens@Sun.COM 	int error;
30989396SMatthew.Ahrens@Sun.COM 
30999396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
31009396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
31019396SMatthew.Ahrens@Sun.COM 
31029396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
31039396SMatthew.Ahrens@Sun.COM 	if (error)
31049396SMatthew.Ahrens@Sun.COM 		return (error);
31059396SMatthew.Ahrens@Sun.COM 
31069396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
31079396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
31089396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31099396SMatthew.Ahrens@Sun.COM 
31109396SMatthew.Ahrens@Sun.COM 	return (error);
31119396SMatthew.Ahrens@Sun.COM }
31129396SMatthew.Ahrens@Sun.COM 
31139396SMatthew.Ahrens@Sun.COM /*
31149396SMatthew.Ahrens@Sun.COM  * inputs:
31159396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31169396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
31179396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
31189396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
31199396SMatthew.Ahrens@Sun.COM  *
31209396SMatthew.Ahrens@Sun.COM  * outputs:
31219396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
31229396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
31239396SMatthew.Ahrens@Sun.COM  */
31249396SMatthew.Ahrens@Sun.COM static int
31259396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
31269396SMatthew.Ahrens@Sun.COM {
31279396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31289396SMatthew.Ahrens@Sun.COM 	int error;
31299396SMatthew.Ahrens@Sun.COM 
31309396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
31319396SMatthew.Ahrens@Sun.COM 	if (error)
31329396SMatthew.Ahrens@Sun.COM 		return (error);
31339396SMatthew.Ahrens@Sun.COM 
31349396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
31359396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
31369396SMatthew.Ahrens@Sun.COM 
31379396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
31389396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
31399396SMatthew.Ahrens@Sun.COM 
31409396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
31419396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
31429396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
31439396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
31449396SMatthew.Ahrens@Sun.COM 	}
31459396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
31469396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31479396SMatthew.Ahrens@Sun.COM 
31489396SMatthew.Ahrens@Sun.COM 	return (error);
31499396SMatthew.Ahrens@Sun.COM }
31509396SMatthew.Ahrens@Sun.COM 
31519396SMatthew.Ahrens@Sun.COM /*
31529396SMatthew.Ahrens@Sun.COM  * inputs:
31539396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31549396SMatthew.Ahrens@Sun.COM  *
31559396SMatthew.Ahrens@Sun.COM  * outputs:
31569396SMatthew.Ahrens@Sun.COM  * none
31579396SMatthew.Ahrens@Sun.COM  */
31589396SMatthew.Ahrens@Sun.COM static int
31599396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31609396SMatthew.Ahrens@Sun.COM {
31619396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31629396SMatthew.Ahrens@Sun.COM 	int error;
31639396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31649396SMatthew.Ahrens@Sun.COM 
31659396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
31669396SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
31679396SMatthew.Ahrens@Sun.COM 			/*
31689396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31699396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31709396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31719396SMatthew.Ahrens@Sun.COM 			 */
31729396SMatthew.Ahrens@Sun.COM 			int mode;
31739396SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, NULL, &mode);
31749396SMatthew.Ahrens@Sun.COM 			if (error == 0) {
31759396SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs,
31769396SMatthew.Ahrens@Sun.COM 				    zc->zc_name, mode);
31779396SMatthew.Ahrens@Sun.COM 			}
31789396SMatthew.Ahrens@Sun.COM 		}
31799396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31809396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31819396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31829396SMatthew.Ahrens@Sun.COM 	} else {
31839396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
31849396SMatthew.Ahrens@Sun.COM 		    DS_MODE_USER, &os);
31859396SMatthew.Ahrens@Sun.COM 		if (error)
31869396SMatthew.Ahrens@Sun.COM 			return (error);
31879396SMatthew.Ahrens@Sun.COM 
31889396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
31899396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
31909396SMatthew.Ahrens@Sun.COM 	}
31919396SMatthew.Ahrens@Sun.COM 
31929396SMatthew.Ahrens@Sun.COM 	return (error);
31939396SMatthew.Ahrens@Sun.COM }
31949396SMatthew.Ahrens@Sun.COM 
31959396SMatthew.Ahrens@Sun.COM /*
31964543Smarks  * We don't want to have a hard dependency
31974543Smarks  * against some special symbols in sharefs
31985331Samw  * nfs, and smbsrv.  Determine them if needed when
31994543Smarks  * the first file system is shared.
32005331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
32014543Smarks  */
32025331Samw int (*znfsexport_fs)(void *arg);
32034543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
32045331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
32055331Samw 
32065331Samw int zfs_nfsshare_inited;
32075331Samw int zfs_smbshare_inited;
32085331Samw 
32094543Smarks ddi_modhandle_t nfs_mod;
32104543Smarks ddi_modhandle_t sharefs_mod;
32115331Samw ddi_modhandle_t smbsrv_mod;
32124543Smarks kmutex_t zfs_share_lock;
32134543Smarks 
32144543Smarks static int
32155331Samw zfs_init_sharefs()
32165331Samw {
32175331Samw 	int error;
32185331Samw 
32195331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
32205331Samw 	/* Both NFS and SMB shares also require sharetab support. */
32215331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
32225331Samw 	    ddi_modopen("fs/sharefs",
32235331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
32245331Samw 		return (ENOSYS);
32255331Samw 	}
32265331Samw 	if (zshare_fs == NULL && ((zshare_fs =
32275331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
32285331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
32295331Samw 		return (ENOSYS);
32305331Samw 	}
32315331Samw 	return (0);
32325331Samw }
32335331Samw 
32345331Samw static int
32354543Smarks zfs_ioc_share(zfs_cmd_t *zc)
32364543Smarks {
32374543Smarks 	int error;
32384543Smarks 	int opcode;
32394543Smarks 
32405331Samw 	switch (zc->zc_share.z_sharetype) {
32415331Samw 	case ZFS_SHARE_NFS:
32425331Samw 	case ZFS_UNSHARE_NFS:
32435331Samw 		if (zfs_nfsshare_inited == 0) {
32445331Samw 			mutex_enter(&zfs_share_lock);
32455331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
32465331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32475331Samw 				mutex_exit(&zfs_share_lock);
32485331Samw 				return (ENOSYS);
32495331Samw 			}
32505331Samw 			if (znfsexport_fs == NULL &&
32515331Samw 			    ((znfsexport_fs = (int (*)(void *))
32525331Samw 			    ddi_modsym(nfs_mod,
32535331Samw 			    "nfs_export", &error)) == NULL)) {
32545331Samw 				mutex_exit(&zfs_share_lock);
32555331Samw 				return (ENOSYS);
32565331Samw 			}
32575331Samw 			error = zfs_init_sharefs();
32585331Samw 			if (error) {
32595331Samw 				mutex_exit(&zfs_share_lock);
32605331Samw 				return (ENOSYS);
32615331Samw 			}
32625331Samw 			zfs_nfsshare_inited = 1;
32634543Smarks 			mutex_exit(&zfs_share_lock);
32644543Smarks 		}
32655331Samw 		break;
32665331Samw 	case ZFS_SHARE_SMB:
32675331Samw 	case ZFS_UNSHARE_SMB:
32685331Samw 		if (zfs_smbshare_inited == 0) {
32695331Samw 			mutex_enter(&zfs_share_lock);
32705331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32715331Samw 			    ddi_modopen("drv/smbsrv",
32725331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32735331Samw 				mutex_exit(&zfs_share_lock);
32745331Samw 				return (ENOSYS);
32755331Samw 			}
32765331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32775331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32786139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32795331Samw 				mutex_exit(&zfs_share_lock);
32805331Samw 				return (ENOSYS);
32815331Samw 			}
32825331Samw 			error = zfs_init_sharefs();
32835331Samw 			if (error) {
32845331Samw 				mutex_exit(&zfs_share_lock);
32855331Samw 				return (ENOSYS);
32865331Samw 			}
32875331Samw 			zfs_smbshare_inited = 1;
32884543Smarks 			mutex_exit(&zfs_share_lock);
32894543Smarks 		}
32905331Samw 		break;
32915331Samw 	default:
32925331Samw 		return (EINVAL);
32934543Smarks 	}
32944543Smarks 
32955331Samw 	switch (zc->zc_share.z_sharetype) {
32965331Samw 	case ZFS_SHARE_NFS:
32975331Samw 	case ZFS_UNSHARE_NFS:
32985331Samw 		if (error =
32995331Samw 		    znfsexport_fs((void *)
33005331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
33015331Samw 			return (error);
33025331Samw 		break;
33035331Samw 	case ZFS_SHARE_SMB:
33045331Samw 	case ZFS_UNSHARE_SMB:
33055331Samw 		if (error = zsmbexport_fs((void *)
33065331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
33075331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
33088845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
33095331Samw 			return (error);
33105331Samw 		}
33115331Samw 		break;
33125331Samw 	}
33135331Samw 
33145331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
33155331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
33164543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
33174543Smarks 
33185331Samw 	/*
33195331Samw 	 * Add or remove share from sharetab
33205331Samw 	 */
33214543Smarks 	error = zshare_fs(opcode,
33224543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
33234543Smarks 	    zc->zc_share.z_sharemax);
33244543Smarks 
33254543Smarks 	return (error);
33264543Smarks 
33274543Smarks }
33284543Smarks 
33298845Samw@Sun.COM ace_t full_access[] = {
33308845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
33318845Samw@Sun.COM };
33328845Samw@Sun.COM 
33338845Samw@Sun.COM /*
33348845Samw@Sun.COM  * Remove all ACL files in shares dir
33358845Samw@Sun.COM  */
33368845Samw@Sun.COM static int
33378845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
33388845Samw@Sun.COM {
33398845Samw@Sun.COM 	zap_cursor_t	zc;
33408845Samw@Sun.COM 	zap_attribute_t	zap;
33418845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
33428845Samw@Sun.COM 	int error;
33438845Samw@Sun.COM 
33448845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
33458845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
33468845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
33478845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
33488845Samw@Sun.COM 		    NULL, 0)) != 0)
33498845Samw@Sun.COM 			break;
33508845Samw@Sun.COM 	}
33518845Samw@Sun.COM 	zap_cursor_fini(&zc);
33528845Samw@Sun.COM 	return (error);
33538845Samw@Sun.COM }
33548845Samw@Sun.COM 
33558845Samw@Sun.COM static int
33568845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33578845Samw@Sun.COM {
33588845Samw@Sun.COM 	vnode_t *vp;
33598845Samw@Sun.COM 	znode_t *dzp;
33608845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33618845Samw@Sun.COM 	znode_t *sharedir;
33628845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33638845Samw@Sun.COM 	nvlist_t *nvlist;
33648845Samw@Sun.COM 	char *src, *target;
33658845Samw@Sun.COM 	vattr_t vattr;
33668845Samw@Sun.COM 	vsecattr_t vsec;
33678845Samw@Sun.COM 	int error = 0;
33688845Samw@Sun.COM 
33698845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33708845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33718845Samw@Sun.COM 		return (error);
33728845Samw@Sun.COM 
33738845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33748845Samw@Sun.COM 
33758845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33768845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33778845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33788845Samw@Sun.COM 		VN_RELE(vp);
33798845Samw@Sun.COM 		return (EINVAL);
33808845Samw@Sun.COM 	}
33818845Samw@Sun.COM 
33828845Samw@Sun.COM 	dzp = VTOZ(vp);
33838845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33848845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33858845Samw@Sun.COM 
33869030SMark.Shellenbaum@Sun.COM 	/*
33879030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33889030SMark.Shellenbaum@Sun.COM 	 */
33899030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33909030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33919030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33929030SMark.Shellenbaum@Sun.COM 
33939030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33949030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33959030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33969030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33979030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33989030SMark.Shellenbaum@Sun.COM 		if (error) {
33999030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
34009030SMark.Shellenbaum@Sun.COM 		} else {
34019030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
34029030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
34039030SMark.Shellenbaum@Sun.COM 		}
34049030SMark.Shellenbaum@Sun.COM 		if (error) {
34059030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
34069030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
34079030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
34089030SMark.Shellenbaum@Sun.COM 			return (error);
34099030SMark.Shellenbaum@Sun.COM 		}
34109030SMark.Shellenbaum@Sun.COM 	}
34119030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
34129030SMark.Shellenbaum@Sun.COM 
34139030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
34148845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
34159030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
34168845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
34178845Samw@Sun.COM 		return (error);
34188845Samw@Sun.COM 	}
34198845Samw@Sun.COM 
34208845Samw@Sun.COM 	switch (zc->zc_cookie) {
34218845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
34228845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
34238845Samw@Sun.COM 		vattr.va_type = VREG;
34248845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
34258845Samw@Sun.COM 		vattr.va_uid = 0;
34268845Samw@Sun.COM 		vattr.va_gid = 0;
34278845Samw@Sun.COM 
34288845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
34298845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
34308845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
34318845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
34328845Samw@Sun.COM 
34338845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
34348845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
34358845Samw@Sun.COM 		if (resourcevp)
34368845Samw@Sun.COM 			VN_RELE(resourcevp);
34378845Samw@Sun.COM 		break;
34388845Samw@Sun.COM 
34398845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
34408845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
34418845Samw@Sun.COM 		    NULL, 0);
34428845Samw@Sun.COM 		break;
34438845Samw@Sun.COM 
34448845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
34458845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
34469643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
34478845Samw@Sun.COM 			VN_RELE(vp);
34488845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34498845Samw@Sun.COM 			return (error);
34508845Samw@Sun.COM 		}
34518845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34528845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34538845Samw@Sun.COM 		    &target)) {
34548845Samw@Sun.COM 			VN_RELE(vp);
34559179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34568845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34578845Samw@Sun.COM 			return (error);
34588845Samw@Sun.COM 		}
34598845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34608845Samw@Sun.COM 		    kcred, NULL, 0);
34618845Samw@Sun.COM 		nvlist_free(nvlist);
34628845Samw@Sun.COM 		break;
34638845Samw@Sun.COM 
34648845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34658845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34668845Samw@Sun.COM 		break;
34678845Samw@Sun.COM 
34688845Samw@Sun.COM 	default:
34698845Samw@Sun.COM 		error = EINVAL;
34708845Samw@Sun.COM 		break;
34718845Samw@Sun.COM 	}
34728845Samw@Sun.COM 
34738845Samw@Sun.COM 	VN_RELE(vp);
34748845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34758845Samw@Sun.COM 
34768845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34778845Samw@Sun.COM 
34788845Samw@Sun.COM 	return (error);
34798845Samw@Sun.COM }
34808845Samw@Sun.COM 
34814543Smarks /*
348210242Schris.kirby@sun.com  * inputs:
348310242Schris.kirby@sun.com  * zc_name	name of filesystem
348410242Schris.kirby@sun.com  * zc_value	short name of snap
348510242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
348610242Schris.kirby@sun.com  * zc_cookie	recursive flag
348710242Schris.kirby@sun.com  *
348810242Schris.kirby@sun.com  * outputs:		none
348910242Schris.kirby@sun.com  */
349010242Schris.kirby@sun.com static int
349110242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
349210242Schris.kirby@sun.com {
349310242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
349410242Schris.kirby@sun.com 
349510242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
349610242Schris.kirby@sun.com 		return (EINVAL);
349710242Schris.kirby@sun.com 
349810242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
349910242Schris.kirby@sun.com 	    zc->zc_string, recursive));
350010242Schris.kirby@sun.com }
350110242Schris.kirby@sun.com 
350210242Schris.kirby@sun.com /*
350310242Schris.kirby@sun.com  * inputs:
350410242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
350510242Schris.kirby@sun.com  * zc_value	short name of snap
350610242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
350710242Schris.kirby@sun.com  * zc_cookie	recursive flag
350810242Schris.kirby@sun.com  *
350910242Schris.kirby@sun.com  * outputs:		none
351010242Schris.kirby@sun.com  */
351110242Schris.kirby@sun.com static int
351210242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
351310242Schris.kirby@sun.com {
351410242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
351510242Schris.kirby@sun.com 
351610242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
351710242Schris.kirby@sun.com 		return (EINVAL);
351810242Schris.kirby@sun.com 
351910242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
352010242Schris.kirby@sun.com 	    zc->zc_string, recursive));
352110242Schris.kirby@sun.com }
352210242Schris.kirby@sun.com 
352310242Schris.kirby@sun.com /*
352410242Schris.kirby@sun.com  * inputs:
352510242Schris.kirby@sun.com  * zc_name		name of filesystem
352610242Schris.kirby@sun.com  *
352710242Schris.kirby@sun.com  * outputs:
352810242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
352910242Schris.kirby@sun.com  */
353010242Schris.kirby@sun.com static int
353110242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
353210242Schris.kirby@sun.com {
353310242Schris.kirby@sun.com 	nvlist_t *nvp;
353410242Schris.kirby@sun.com 	int error;
353510242Schris.kirby@sun.com 
353610242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
353710242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
353810242Schris.kirby@sun.com 		nvlist_free(nvp);
353910242Schris.kirby@sun.com 	}
354010242Schris.kirby@sun.com 
354110242Schris.kirby@sun.com 	return (error);
354210242Schris.kirby@sun.com }
354310242Schris.kirby@sun.com 
354410242Schris.kirby@sun.com /*
35454988Sek110237  * pool create, destroy, and export don't log the history as part of
35464988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35474988Sek110237  * do the logging of those commands.
35484543Smarks  */
3549789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
35509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35519234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35539234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35559234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35579234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
35599234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35619234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
35639234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35659234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
35679234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
35699234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35719234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35739234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35759234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35779234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35789234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35799234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35819234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35839234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35849425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35859425SEric.Schrock@Sun.COM 	    B_TRUE },
35869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35879234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35889234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35899234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35909234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35919234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35929234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35939234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
35959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
35969234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
35989234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
36009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
36019234SGeorge.Wilson@Sun.COM 	    B_TRUE},
36029234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
36039234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
36059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
36069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
36079234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
36089234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36099234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
36109234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36119234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
36129234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36139234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
36149234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36159234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
36169234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
36179234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36189234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
36199234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36209234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
36219234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36229234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
36239234SGeorge.Wilson@Sun.COM 	    B_FALSE },
362410233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
362510233SGeorge.Wilson@Sun.COM 	    B_TRUE },
36269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
36279234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
36299234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
36319234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
36339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
36359234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
36379234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
36389234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36399234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
36409396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
36419396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
36429396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36439396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
36449396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36459396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
36469396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
364710242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
364810242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
364910242Schris.kirby@sun.com 	    B_TRUE },
365010242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
365110242Schris.kirby@sun.com 	    B_TRUE }
3652789Sahrens };
3653789Sahrens 
36549234SGeorge.Wilson@Sun.COM int
36559234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
36569234SGeorge.Wilson@Sun.COM {
36579234SGeorge.Wilson@Sun.COM 	spa_t *spa;
36589234SGeorge.Wilson@Sun.COM 	int error;
36599234SGeorge.Wilson@Sun.COM 
36609234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
36619234SGeorge.Wilson@Sun.COM 
36629396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
36639234SGeorge.Wilson@Sun.COM 	if (error == 0) {
36649234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
36659234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
36669234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
36679234SGeorge.Wilson@Sun.COM 	}
36689234SGeorge.Wilson@Sun.COM 	return (error);
36699234SGeorge.Wilson@Sun.COM }
36709234SGeorge.Wilson@Sun.COM 
3671789Sahrens static int
3672789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3673789Sahrens {
3674789Sahrens 	zfs_cmd_t *zc;
3675789Sahrens 	uint_t vec;
36762199Sahrens 	int error, rc;
3677789Sahrens 
3678789Sahrens 	if (getminor(dev) != 0)
3679789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3680789Sahrens 
3681789Sahrens 	vec = cmd - ZFS_IOC;
36824787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3683789Sahrens 
3684789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3685789Sahrens 		return (EINVAL);
3686789Sahrens 
3687789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3688789Sahrens 
36899643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3690789Sahrens 
36914787Sahrens 	if (error == 0)
36924543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3693789Sahrens 
3694789Sahrens 	/*
3695789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3696789Sahrens 	 * the lower layers.
3697789Sahrens 	 */
3698789Sahrens 	if (error == 0) {
3699789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
37009643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3701789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
37024577Sahrens 		case POOL_NAME:
3703789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3704789Sahrens 				error = EINVAL;
37059234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
37069234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
37079234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3708789Sahrens 			break;
3709789Sahrens 
37104577Sahrens 		case DATASET_NAME:
3711789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3712789Sahrens 				error = EINVAL;
37139234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
37149234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
37159234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3716789Sahrens 			break;
37172856Snd150628 
37184577Sahrens 		case NO_NAME:
37192856Snd150628 			break;
3720789Sahrens 		}
3721789Sahrens 	}
3722789Sahrens 
3723789Sahrens 	if (error == 0)
3724789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3725789Sahrens 
37269643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
37274543Smarks 	if (error == 0) {
37282199Sahrens 		error = rc;
37299396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
37304543Smarks 			zfs_log_history(zc);
37314543Smarks 	}
3732789Sahrens 
3733789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3734789Sahrens 	return (error);
3735789Sahrens }
3736789Sahrens 
3737789Sahrens static int
3738789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3739789Sahrens {
3740789Sahrens 	if (cmd != DDI_ATTACH)
3741789Sahrens 		return (DDI_FAILURE);
3742789Sahrens 
3743789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3744789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3745789Sahrens 		return (DDI_FAILURE);
3746789Sahrens 
3747789Sahrens 	zfs_dip = dip;
3748789Sahrens 
3749789Sahrens 	ddi_report_dev(dip);
3750789Sahrens 
3751789Sahrens 	return (DDI_SUCCESS);
3752789Sahrens }
3753789Sahrens 
3754789Sahrens static int
3755789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3756789Sahrens {
3757789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3758789Sahrens 		return (DDI_FAILURE);
3759789Sahrens 
3760789Sahrens 	if (cmd != DDI_DETACH)
3761789Sahrens 		return (DDI_FAILURE);
3762789Sahrens 
3763789Sahrens 	zfs_dip = NULL;
3764789Sahrens 
3765789Sahrens 	ddi_prop_remove_all(dip);
3766789Sahrens 	ddi_remove_minor_node(dip, NULL);
3767789Sahrens 
3768789Sahrens 	return (DDI_SUCCESS);
3769789Sahrens }
3770789Sahrens 
3771789Sahrens /*ARGSUSED*/
3772789Sahrens static int
3773789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3774789Sahrens {
3775789Sahrens 	switch (infocmd) {
3776789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3777789Sahrens 		*result = zfs_dip;
3778789Sahrens 		return (DDI_SUCCESS);
3779789Sahrens 
3780789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3781849Sbonwick 		*result = (void *)0;
3782789Sahrens 		return (DDI_SUCCESS);
3783789Sahrens 	}
3784789Sahrens 
3785789Sahrens 	return (DDI_FAILURE);
3786789Sahrens }
3787789Sahrens 
3788789Sahrens /*
3789789Sahrens  * OK, so this is a little weird.
3790789Sahrens  *
3791789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3792789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3793789Sahrens  *
3794789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3795789Sahrens  * so most of the standard driver entry points are in zvol.c.
3796789Sahrens  */
3797789Sahrens static struct cb_ops zfs_cb_ops = {
3798789Sahrens 	zvol_open,	/* open */
3799789Sahrens 	zvol_close,	/* close */
3800789Sahrens 	zvol_strategy,	/* strategy */
3801789Sahrens 	nodev,		/* print */
38026423Sgw25295 	zvol_dump,	/* dump */
3803789Sahrens 	zvol_read,	/* read */
3804789Sahrens 	zvol_write,	/* write */
3805789Sahrens 	zfsdev_ioctl,	/* ioctl */
3806789Sahrens 	nodev,		/* devmap */
3807789Sahrens 	nodev,		/* mmap */
3808789Sahrens 	nodev,		/* segmap */
3809789Sahrens 	nochpoll,	/* poll */
3810789Sahrens 	ddi_prop_op,	/* prop_op */
3811789Sahrens 	NULL,		/* streamtab */
3812789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3813789Sahrens 	CB_REV,		/* version */
38143638Sbillm 	nodev,		/* async read */
38153638Sbillm 	nodev,		/* async write */
3816789Sahrens };
3817789Sahrens 
3818789Sahrens static struct dev_ops zfs_dev_ops = {
3819789Sahrens 	DEVO_REV,	/* version */
3820789Sahrens 	0,		/* refcnt */
3821789Sahrens 	zfs_info,	/* info */
3822789Sahrens 	nulldev,	/* identify */
3823789Sahrens 	nulldev,	/* probe */
3824789Sahrens 	zfs_attach,	/* attach */
3825789Sahrens 	zfs_detach,	/* detach */
3826789Sahrens 	nodev,		/* reset */
3827789Sahrens 	&zfs_cb_ops,	/* driver operations */
38287656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
38297656SSherry.Moore@Sun.COM 	NULL,		/* power */
38307656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3831789Sahrens };
3832789Sahrens 
3833789Sahrens static struct modldrv zfs_modldrv = {
38347656SSherry.Moore@Sun.COM 	&mod_driverops,
38357656SSherry.Moore@Sun.COM 	"ZFS storage pool",
38367656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3837789Sahrens };
3838789Sahrens 
3839789Sahrens static struct modlinkage modlinkage = {
3840789Sahrens 	MODREV_1,
3841789Sahrens 	(void *)&zfs_modlfs,
3842789Sahrens 	(void *)&zfs_modldrv,
3843789Sahrens 	NULL
3844789Sahrens };
3845789Sahrens 
38464720Sfr157268 
38474720Sfr157268 uint_t zfs_fsyncer_key;
38485326Sek110237 extern uint_t rrw_tsd_key;
38494720Sfr157268 
3850789Sahrens int
3851789Sahrens _init(void)
3852789Sahrens {
3853789Sahrens 	int error;
3854789Sahrens 
3855849Sbonwick 	spa_init(FREAD | FWRITE);
3856849Sbonwick 	zfs_init();
3857849Sbonwick 	zvol_init();
3858849Sbonwick 
3859849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3860849Sbonwick 		zvol_fini();
3861849Sbonwick 		zfs_fini();
3862849Sbonwick 		spa_fini();
3863789Sahrens 		return (error);
3864849Sbonwick 	}
3865789Sahrens 
38664720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
38675326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
38684720Sfr157268 
3869789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3870789Sahrens 	ASSERT(error == 0);
38714543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3872789Sahrens 
3873789Sahrens 	return (0);
3874789Sahrens }
3875789Sahrens 
3876789Sahrens int
3877789Sahrens _fini(void)
3878789Sahrens {
3879789Sahrens 	int error;
3880789Sahrens 
38811544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3882789Sahrens 		return (EBUSY);
3883789Sahrens 
3884789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3885789Sahrens 		return (error);
3886789Sahrens 
3887789Sahrens 	zvol_fini();
3888789Sahrens 	zfs_fini();
3889789Sahrens 	spa_fini();
38905331Samw 	if (zfs_nfsshare_inited)
38914543Smarks 		(void) ddi_modclose(nfs_mod);
38925331Samw 	if (zfs_smbshare_inited)
38935331Samw 		(void) ddi_modclose(smbsrv_mod);
38945331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
38954543Smarks 		(void) ddi_modclose(sharefs_mod);
3896789Sahrens 
38974720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3898789Sahrens 	ldi_ident_release(zfs_li);
3899789Sahrens 	zfs_li = NULL;
39004543Smarks 	mutex_destroy(&zfs_share_lock);
3901789Sahrens 
3902789Sahrens 	return (error);
3903789Sahrens }
3904789Sahrens 
3905789Sahrens int
3906789Sahrens _info(struct modinfo *modinfop)
3907789Sahrens {
3908789Sahrens 	return (mod_info(&modlinkage, modinfop));
3909789Sahrens }
3910