xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10242:c40d075fbca6)
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 
764*10242Schris.kirby@sun.com static int
765*10242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
766*10242Schris.kirby@sun.com {
767*10242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
768*10242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
769*10242Schris.kirby@sun.com }
770*10242Schris.kirby@sun.com 
771*10242Schris.kirby@sun.com static int
772*10242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
773*10242Schris.kirby@sun.com {
774*10242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
775*10242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
776*10242Schris.kirby@sun.com }
777*10242Schris.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 
24096492Stimh 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
24106492Stimh 		    NULL, NULL);
24115331Samw 		if (error) {
24125331Samw 			dmu_objset_close(clone);
24135331Samw 			nvlist_free(nvprops);
24145331Samw 			return (error);
24155331Samw 		}
2416789Sahrens 		dmu_objset_close(clone);
2417789Sahrens 	} else {
24186492Stimh 		boolean_t is_insensitive = B_FALSE;
24196492Stimh 
24202676Seschrock 		if (cbfunc == NULL) {
24214543Smarks 			nvlist_free(nvprops);
24222199Sahrens 			return (EINVAL);
24232676Seschrock 		}
24242676Seschrock 
2425789Sahrens 		if (type == DMU_OST_ZVOL) {
24262676Seschrock 			uint64_t volsize, volblocksize;
24272676Seschrock 
24284543Smarks 			if (nvprops == NULL ||
24294543Smarks 			    nvlist_lookup_uint64(nvprops,
24302676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
24312676Seschrock 			    &volsize) != 0) {
24324543Smarks 				nvlist_free(nvprops);
24332676Seschrock 				return (EINVAL);
24342676Seschrock 			}
24352676Seschrock 
24364543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24372676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24382676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24394543Smarks 				nvlist_free(nvprops);
24402676Seschrock 				return (EINVAL);
24412676Seschrock 			}
24421133Seschrock 
24432676Seschrock 			if (error != 0)
24442676Seschrock 				volblocksize = zfs_prop_default_numeric(
24452676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24462676Seschrock 
24472676Seschrock 			if ((error = zvol_check_volblocksize(
24482676Seschrock 			    volblocksize)) != 0 ||
24492676Seschrock 			    (error = zvol_check_volsize(volsize,
24502676Seschrock 			    volblocksize)) != 0) {
24514543Smarks 				nvlist_free(nvprops);
2452789Sahrens 				return (error);
24532676Seschrock 			}
24544577Sahrens 		} else if (type == DMU_OST_ZFS) {
24555331Samw 			int error;
24565331Samw 
24575498Stimh 			/*
24585331Samw 			 * We have to have normalization and
24595331Samw 			 * case-folding flags correct when we do the
24605331Samw 			 * file system creation, so go figure them out
24615498Stimh 			 * now.
24625331Samw 			 */
24635498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24645498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24655498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24667184Stimh 			    zct.zct_zplprops, &is_insensitive);
24675331Samw 			if (error != 0) {
24685331Samw 				nvlist_free(nvprops);
24695498Stimh 				nvlist_free(zct.zct_zplprops);
24705331Samw 				return (error);
24714577Sahrens 			}
24722676Seschrock 		}
24736492Stimh 		error = dmu_objset_create(zc->zc_name, type, NULL,
24746492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24755498Stimh 		nvlist_free(zct.zct_zplprops);
2476789Sahrens 	}
24772676Seschrock 
24782676Seschrock 	/*
24792676Seschrock 	 * It would be nice to do this atomically.
24802676Seschrock 	 */
24812676Seschrock 	if (error == 0) {
24824787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2483*10242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
24842676Seschrock 	}
24854543Smarks 	nvlist_free(nvprops);
2486789Sahrens 	return (error);
2487789Sahrens }
2488789Sahrens 
24895367Sahrens /*
24905367Sahrens  * inputs:
24915367Sahrens  * zc_name	name of filesystem
24925367Sahrens  * zc_value	short name of snapshot
24935367Sahrens  * zc_cookie	recursive flag
24949396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24955367Sahrens  *
24965367Sahrens  * outputs:	none
24975367Sahrens  */
2498789Sahrens static int
24992199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
25002199Sahrens {
25017265Sahrens 	nvlist_t *nvprops = NULL;
25027265Sahrens 	int error;
25037265Sahrens 	boolean_t recursive = zc->zc_cookie;
25047265Sahrens 
25052676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25062199Sahrens 		return (EINVAL);
25077265Sahrens 
25087265Sahrens 	if (zc->zc_nvlist_src != NULL &&
25097265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
25109643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
25117265Sahrens 		return (error);
25127265Sahrens 
25139355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
25149355SMatthew.Ahrens@Sun.COM 	if (error)
25159355SMatthew.Ahrens@Sun.COM 		goto out;
25169355SMatthew.Ahrens@Sun.COM 
25179355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
25189355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
25199355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
25209355SMatthew.Ahrens@Sun.COM 		goto out;
25217265Sahrens 	}
25229355SMatthew.Ahrens@Sun.COM 
25239355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
25249355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
25259355SMatthew.Ahrens@Sun.COM 
25269355SMatthew.Ahrens@Sun.COM out:
25277265Sahrens 	nvlist_free(nvprops);
25287265Sahrens 	return (error);
25292199Sahrens }
25302199Sahrens 
25314007Smmusante int
25322199Sahrens zfs_unmount_snap(char *name, void *arg)
2533789Sahrens {
25342417Sahrens 	vfs_t *vfsp = NULL;
25352199Sahrens 
25366689Smaybee 	if (arg) {
25376689Smaybee 		char *snapname = arg;
25386689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25396689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25406689Smaybee 
25416689Smaybee 		(void) strcpy(buf, name);
25426689Smaybee 		(void) strcat(buf, "@");
25436689Smaybee 		(void) strcat(buf, snapname);
25446689Smaybee 		vfsp = zfs_get_vfs(buf);
25456689Smaybee 		kmem_free(buf, len);
25462417Sahrens 	} else if (strchr(name, '@')) {
25472199Sahrens 		vfsp = zfs_get_vfs(name);
25482199Sahrens 	}
25492199Sahrens 
25502199Sahrens 	if (vfsp) {
25512199Sahrens 		/*
25522199Sahrens 		 * Always force the unmount for snapshots.
25532199Sahrens 		 */
25542199Sahrens 		int flag = MS_FORCE;
2555789Sahrens 		int err;
2556789Sahrens 
25572199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25582199Sahrens 			VFS_RELE(vfsp);
25592199Sahrens 			return (err);
25602199Sahrens 		}
25612199Sahrens 		VFS_RELE(vfsp);
25622199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25632199Sahrens 			return (err);
25642199Sahrens 	}
25652199Sahrens 	return (0);
25662199Sahrens }
25672199Sahrens 
25685367Sahrens /*
25695367Sahrens  * inputs:
2570*10242Schris.kirby@sun.com  * zc_name		name of filesystem
2571*10242Schris.kirby@sun.com  * zc_value		short name of snapshot
2572*10242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25735367Sahrens  *
25745367Sahrens  * outputs:	none
25755367Sahrens  */
25762199Sahrens static int
25772199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25782199Sahrens {
25792199Sahrens 	int err;
2580789Sahrens 
25812676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25822199Sahrens 		return (EINVAL);
25832199Sahrens 	err = dmu_objset_find(zc->zc_name,
25842676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25852199Sahrens 	if (err)
25862199Sahrens 		return (err);
2587*10242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
2588*10242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
25892199Sahrens }
25902199Sahrens 
25915367Sahrens /*
25925367Sahrens  * inputs:
25935367Sahrens  * zc_name		name of dataset to destroy
25945367Sahrens  * zc_objset_type	type of objset
2595*10242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25965367Sahrens  *
25975367Sahrens  * outputs:		none
25985367Sahrens  */
25992199Sahrens static int
26002199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
26012199Sahrens {
26022199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
26032199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26042199Sahrens 		if (err)
26052199Sahrens 			return (err);
2606789Sahrens 	}
2607789Sahrens 
2608*10242Schris.kirby@sun.com 	return (dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy));
2609789Sahrens }
2610789Sahrens 
26115367Sahrens /*
26125367Sahrens  * inputs:
26135446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
26145367Sahrens  *
26155367Sahrens  * outputs:	none
26165367Sahrens  */
2617789Sahrens static int
2618789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2619789Sahrens {
26205446Sahrens 	objset_t *os;
26215446Sahrens 	int error;
26225446Sahrens 	zfsvfs_t *zfsvfs = NULL;
26235446Sahrens 
26245446Sahrens 	/*
26255446Sahrens 	 * Get the zfsvfs for the receiving objset. There
26265446Sahrens 	 * won't be one if we're operating on a zvol, if the
26275446Sahrens 	 * objset doesn't exist yet, or is not mounted.
26285446Sahrens 	 */
26296689Smaybee 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
26305446Sahrens 	if (error)
26315446Sahrens 		return (error);
26325446Sahrens 
26339396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
26345446Sahrens 		int mode;
26355446Sahrens 
26369396SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs, NULL, &mode);
26376083Sek110237 		if (error == 0) {
26386083Sek110237 			int resume_err;
26396083Sek110237 
26406083Sek110237 			error = dmu_objset_rollback(os);
26419396SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
26426083Sek110237 			error = error ? error : resume_err;
26436083Sek110237 		} else {
26446083Sek110237 			dmu_objset_close(os);
26456083Sek110237 		}
26465446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26475446Sahrens 	} else {
26485446Sahrens 		error = dmu_objset_rollback(os);
26495446Sahrens 	}
26506689Smaybee 	/* Note, the dmu_objset_rollback() releases the objset for us. */
26515446Sahrens 
26525446Sahrens 	return (error);
2653789Sahrens }
2654789Sahrens 
26555367Sahrens /*
26565367Sahrens  * inputs:
26575367Sahrens  * zc_name	old name of dataset
26585367Sahrens  * zc_value	new name of dataset
26595367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26605367Sahrens  *
26615367Sahrens  * outputs:	none
26625367Sahrens  */
2663789Sahrens static int
2664789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2665789Sahrens {
26664490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26674007Smmusante 
26682676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26695326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26705326Sek110237 	    strchr(zc->zc_value, '%'))
2671789Sahrens 		return (EINVAL);
2672789Sahrens 
26734007Smmusante 	/*
26744007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26754007Smmusante 	 * in which case the dataset code figures out which snapshots
26764007Smmusante 	 * to unmount.
26774007Smmusante 	 */
26784007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2679789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26802199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26812199Sahrens 		if (err)
26822199Sahrens 			return (err);
2683789Sahrens 	}
26844007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2685789Sahrens }
2686789Sahrens 
26876689Smaybee static void
26888536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26896689Smaybee {
26906689Smaybee 	zfs_cmd_t *zc;
26916689Smaybee 	nvpair_t *prop;
26926689Smaybee 
26936689Smaybee 	if (props == NULL)
26946689Smaybee 		return;
26956689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
26966689Smaybee 	(void) strcpy(zc->zc_name, dataset);
26976689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
26986689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
26998536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
27008536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
27018536SDavid.Pacheco@Sun.COM 			continue;
27026689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
27036689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
27046689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
27056689Smaybee 	}
27066689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
27076689Smaybee }
27086689Smaybee 
27095367Sahrens /*
27105367Sahrens  * inputs:
27115367Sahrens  * zc_name		name of containing filesystem
27125367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27135367Sahrens  * zc_value		name of snapshot to create
27145367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27155367Sahrens  * zc_cookie		file descriptor to recv from
27165367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27175367Sahrens  * zc_guid		force flag
27185367Sahrens  *
27195367Sahrens  * outputs:
27205367Sahrens  * zc_cookie		number of bytes read
27215367Sahrens  */
2722789Sahrens static int
27235367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2724789Sahrens {
2725789Sahrens 	file_t *fp;
27265326Sek110237 	objset_t *os;
27275367Sahrens 	dmu_recv_cookie_t drc;
27285326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2729789Sahrens 	int error, fd;
27305367Sahrens 	offset_t off;
27315367Sahrens 	nvlist_t *props = NULL;
27326689Smaybee 	nvlist_t *origprops = NULL;
27335367Sahrens 	objset_t *origin = NULL;
27345367Sahrens 	char *tosnap;
27355367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2736789Sahrens 
27373265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27385326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27395326Sek110237 	    strchr(zc->zc_value, '%'))
27403265Sahrens 		return (EINVAL);
27413265Sahrens 
27425367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27435367Sahrens 	tosnap = strchr(tofs, '@');
27445367Sahrens 	*tosnap = '\0';
27455367Sahrens 	tosnap++;
27465367Sahrens 
27475367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27485367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27499643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27505367Sahrens 		return (error);
27515367Sahrens 
2752789Sahrens 	fd = zc->zc_cookie;
2753789Sahrens 	fp = getf(fd);
27545367Sahrens 	if (fp == NULL) {
27555367Sahrens 		nvlist_free(props);
2756789Sahrens 		return (EBADF);
27575367Sahrens 	}
27585326Sek110237 
275910204SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_open(tofs, DMU_OST_ANY,
276010204SMatthew.Ahrens@Sun.COM 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
27616689Smaybee 		/*
27626689Smaybee 		 * If new properties are supplied, they are to completely
27636689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27646689Smaybee 		 */
27659396SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, TRUE);
27666689Smaybee 
27675326Sek110237 		dmu_objset_close(os);
27685326Sek110237 	}
27695326Sek110237 
27705367Sahrens 	if (zc->zc_string[0]) {
27715367Sahrens 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
27726689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
27736689Smaybee 		if (error)
27746689Smaybee 			goto out;
27755367Sahrens 	}
27765367Sahrens 
27775367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
277810204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
27795367Sahrens 	if (origin)
27805367Sahrens 		dmu_objset_close(origin);
27816689Smaybee 	if (error)
27826689Smaybee 		goto out;
27835326Sek110237 
27845326Sek110237 	/*
27856689Smaybee 	 * Reset properties.  We do this before we receive the stream
27866689Smaybee 	 * so that the properties are applied to the new data.
27875326Sek110237 	 */
27885367Sahrens 	if (props) {
27898536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27906689Smaybee 		/*
27916689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27926689Smaybee 		 */
27936689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27945367Sahrens 	}
27955367Sahrens 
27965367Sahrens 	off = fp->f_offset;
27975367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
27985367Sahrens 
279910204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
280010204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
280110204SMatthew.Ahrens@Sun.COM 
280210204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
280310204SMatthew.Ahrens@Sun.COM 			/* online recv */
280410204SMatthew.Ahrens@Sun.COM 			int end_err;
280510204SMatthew.Ahrens@Sun.COM 			char *osname;
280610204SMatthew.Ahrens@Sun.COM 			int mode;
280710204SMatthew.Ahrens@Sun.COM 
280810204SMatthew.Ahrens@Sun.COM 			osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
280910204SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, osname, &mode);
281010204SMatthew.Ahrens@Sun.COM 			/*
281110204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
281210204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
281310204SMatthew.Ahrens@Sun.COM 			 */
281410204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
281510204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
281610204SMatthew.Ahrens@Sun.COM 				int resume_err =
281710204SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, osname, mode);
281810204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
281910204SMatthew.Ahrens@Sun.COM 			}
282010204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
282110204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
282210204SMatthew.Ahrens@Sun.COM 			kmem_free(osname, MAXNAMELEN);
282310204SMatthew.Ahrens@Sun.COM 		} else {
28246689Smaybee 			error = dmu_recv_end(&drc);
28255367Sahrens 		}
28266083Sek110237 	}
28275367Sahrens 
28285367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28295367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28305367Sahrens 		fp->f_offset = off;
28312885Sahrens 
28326689Smaybee 	/*
28336689Smaybee 	 * On error, restore the original props.
28346689Smaybee 	 */
28356689Smaybee 	if (error && props) {
28368536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28376689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28386689Smaybee 	}
28396689Smaybee out:
28406689Smaybee 	nvlist_free(props);
28416689Smaybee 	nvlist_free(origprops);
2842789Sahrens 	releasef(fd);
2843789Sahrens 	return (error);
2844789Sahrens }
2845789Sahrens 
28465367Sahrens /*
28475367Sahrens  * inputs:
28485367Sahrens  * zc_name	name of snapshot to send
28495367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28505367Sahrens  * zc_cookie	file descriptor to send stream to
28515367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28525367Sahrens  *
28535367Sahrens  * outputs: none
28545367Sahrens  */
2855789Sahrens static int
28565367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2857789Sahrens {
2858789Sahrens 	objset_t *fromsnap = NULL;
2859789Sahrens 	objset_t *tosnap;
2860789Sahrens 	file_t *fp;
2861789Sahrens 	int error;
28625367Sahrens 	offset_t off;
2863789Sahrens 
2864789Sahrens 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
28656689Smaybee 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2866789Sahrens 	if (error)
2867789Sahrens 		return (error);
2868789Sahrens 
28692676Seschrock 	if (zc->zc_value[0] != '\0') {
28708012SEric.Taylor@Sun.COM 		char *buf;
28712885Sahrens 		char *cp;
28722885Sahrens 
28738012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28748012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28752885Sahrens 		cp = strchr(buf, '@');
28762885Sahrens 		if (cp)
28772885Sahrens 			*(cp+1) = 0;
28788012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
28792885Sahrens 		error = dmu_objset_open(buf, DMU_OST_ANY,
28806689Smaybee 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
28818012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2882789Sahrens 		if (error) {
2883789Sahrens 			dmu_objset_close(tosnap);
2884789Sahrens 			return (error);
2885789Sahrens 		}
2886789Sahrens 	}
2887789Sahrens 
2888789Sahrens 	fp = getf(zc->zc_cookie);
2889789Sahrens 	if (fp == NULL) {
2890789Sahrens 		dmu_objset_close(tosnap);
2891789Sahrens 		if (fromsnap)
2892789Sahrens 			dmu_objset_close(fromsnap);
2893789Sahrens 		return (EBADF);
2894789Sahrens 	}
2895789Sahrens 
28965367Sahrens 	off = fp->f_offset;
28975367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28985367Sahrens 
28995367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
29005367Sahrens 		fp->f_offset = off;
2901789Sahrens 	releasef(zc->zc_cookie);
2902789Sahrens 	if (fromsnap)
2903789Sahrens 		dmu_objset_close(fromsnap);
2904789Sahrens 	dmu_objset_close(tosnap);
2905789Sahrens 	return (error);
2906789Sahrens }
2907789Sahrens 
29081544Seschrock static int
29091544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29101544Seschrock {
29111544Seschrock 	int id, error;
29121544Seschrock 
29131544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29141544Seschrock 	    &zc->zc_inject_record);
29151544Seschrock 
29161544Seschrock 	if (error == 0)
29171544Seschrock 		zc->zc_guid = (uint64_t)id;
29181544Seschrock 
29191544Seschrock 	return (error);
29201544Seschrock }
29211544Seschrock 
29221544Seschrock static int
29231544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29241544Seschrock {
29251544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29261544Seschrock }
29271544Seschrock 
29281544Seschrock static int
29291544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29301544Seschrock {
29311544Seschrock 	int id = (int)zc->zc_guid;
29321544Seschrock 	int error;
29331544Seschrock 
29341544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29351544Seschrock 	    &zc->zc_inject_record);
29361544Seschrock 
29371544Seschrock 	zc->zc_guid = id;
29381544Seschrock 
29391544Seschrock 	return (error);
29401544Seschrock }
29411544Seschrock 
29421544Seschrock static int
29431544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29441544Seschrock {
29451544Seschrock 	spa_t *spa;
29461544Seschrock 	int error;
29472676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29481544Seschrock 
29491544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29501544Seschrock 		return (error);
29511544Seschrock 
29522676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29531544Seschrock 	    &count);
29541544Seschrock 	if (error == 0)
29552676Seschrock 		zc->zc_nvlist_dst_size = count;
29561544Seschrock 	else
29572676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29581544Seschrock 
29591544Seschrock 	spa_close(spa, FTAG);
29601544Seschrock 
29611544Seschrock 	return (error);
29621544Seschrock }
29631544Seschrock 
29641544Seschrock static int
29651544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29661544Seschrock {
29671544Seschrock 	spa_t *spa;
29681544Seschrock 	vdev_t *vd;
29691544Seschrock 	int error;
29701544Seschrock 
29717294Sperrin 	/*
29727294Sperrin 	 * On zpool clear we also fix up missing slogs
29737294Sperrin 	 */
29747294Sperrin 	mutex_enter(&spa_namespace_lock);
29757294Sperrin 	spa = spa_lookup(zc->zc_name);
29767294Sperrin 	if (spa == NULL) {
29777294Sperrin 		mutex_exit(&spa_namespace_lock);
29787294Sperrin 		return (EIO);
29797294Sperrin 	}
29807294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29817294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29827294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29837294Sperrin 	}
29847294Sperrin 	mutex_exit(&spa_namespace_lock);
29857294Sperrin 
29861544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29871544Seschrock 		return (error);
29881544Seschrock 
29897754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
29901544Seschrock 
29912676Seschrock 	if (zc->zc_guid == 0) {
29921544Seschrock 		vd = NULL;
29936643Seschrock 	} else {
29946643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
29955450Sbrendan 		if (vd == NULL) {
29967754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
29975450Sbrendan 			spa_close(spa, FTAG);
29985450Sbrendan 			return (ENODEV);
29995450Sbrendan 		}
30001544Seschrock 	}
30011544Seschrock 
30027754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
30037754SJeff.Bonwick@Sun.COM 
30047754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
30057754SJeff.Bonwick@Sun.COM 
30067754SJeff.Bonwick@Sun.COM 	/*
30077754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
30087754SJeff.Bonwick@Sun.COM 	 */
30099234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30109234SGeorge.Wilson@Sun.COM 		error = EIO;
30111544Seschrock 
30121544Seschrock 	spa_close(spa, FTAG);
30131544Seschrock 
30149234SGeorge.Wilson@Sun.COM 	return (error);
30151544Seschrock }
30161544Seschrock 
30175367Sahrens /*
30185367Sahrens  * inputs:
30195367Sahrens  * zc_name	name of filesystem
30205367Sahrens  * zc_value	name of origin snapshot
30215367Sahrens  *
30225367Sahrens  * outputs:	none
30235367Sahrens  */
30241544Seschrock static int
30252082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30262082Seschrock {
30272417Sahrens 	char *cp;
30282417Sahrens 
30292417Sahrens 	/*
30302417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30312417Sahrens 	 * it's easier.
30322417Sahrens 	 */
30332676Seschrock 	cp = strchr(zc->zc_value, '@');
30342417Sahrens 	if (cp)
30352417Sahrens 		*cp = '\0';
30362676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30372417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
30382082Seschrock 	return (dsl_dataset_promote(zc->zc_name));
30392082Seschrock }
30402082Seschrock 
30414543Smarks /*
30429396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30439396SMatthew.Ahrens@Sun.COM  *
30449396SMatthew.Ahrens@Sun.COM  * inputs:
30459396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30469396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30479396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30489396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30499396SMatthew.Ahrens@Sun.COM  *
30509396SMatthew.Ahrens@Sun.COM  * outputs:
30519396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30529396SMatthew.Ahrens@Sun.COM  */
30539396SMatthew.Ahrens@Sun.COM static int
30549396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30559396SMatthew.Ahrens@Sun.COM {
30569396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30579396SMatthew.Ahrens@Sun.COM 	int error;
30589396SMatthew.Ahrens@Sun.COM 
30599396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30609396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30619396SMatthew.Ahrens@Sun.COM 
30629396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30639396SMatthew.Ahrens@Sun.COM 	if (error)
30649396SMatthew.Ahrens@Sun.COM 		return (error);
30659396SMatthew.Ahrens@Sun.COM 
30669396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
30679396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
30689396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30699396SMatthew.Ahrens@Sun.COM 
30709396SMatthew.Ahrens@Sun.COM 	return (error);
30719396SMatthew.Ahrens@Sun.COM }
30729396SMatthew.Ahrens@Sun.COM 
30739396SMatthew.Ahrens@Sun.COM /*
30749396SMatthew.Ahrens@Sun.COM  * inputs:
30759396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
30769396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
30779396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
30789396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
30799396SMatthew.Ahrens@Sun.COM  *
30809396SMatthew.Ahrens@Sun.COM  * outputs:
30819396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
30829396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
30839396SMatthew.Ahrens@Sun.COM  */
30849396SMatthew.Ahrens@Sun.COM static int
30859396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
30869396SMatthew.Ahrens@Sun.COM {
30879396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30889396SMatthew.Ahrens@Sun.COM 	int error;
30899396SMatthew.Ahrens@Sun.COM 
30909396SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
30919396SMatthew.Ahrens@Sun.COM 	if (error)
30929396SMatthew.Ahrens@Sun.COM 		return (error);
30939396SMatthew.Ahrens@Sun.COM 
30949396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
30959396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
30969396SMatthew.Ahrens@Sun.COM 
30979396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
30989396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
30999396SMatthew.Ahrens@Sun.COM 
31009396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
31019396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
31029396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
31039396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
31049396SMatthew.Ahrens@Sun.COM 	}
31059396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
31069396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31079396SMatthew.Ahrens@Sun.COM 
31089396SMatthew.Ahrens@Sun.COM 	return (error);
31099396SMatthew.Ahrens@Sun.COM }
31109396SMatthew.Ahrens@Sun.COM 
31119396SMatthew.Ahrens@Sun.COM /*
31129396SMatthew.Ahrens@Sun.COM  * inputs:
31139396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31149396SMatthew.Ahrens@Sun.COM  *
31159396SMatthew.Ahrens@Sun.COM  * outputs:
31169396SMatthew.Ahrens@Sun.COM  * none
31179396SMatthew.Ahrens@Sun.COM  */
31189396SMatthew.Ahrens@Sun.COM static int
31199396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31209396SMatthew.Ahrens@Sun.COM {
31219396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31229396SMatthew.Ahrens@Sun.COM 	int error;
31239396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31249396SMatthew.Ahrens@Sun.COM 
31259396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
31269396SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
31279396SMatthew.Ahrens@Sun.COM 			/*
31289396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31299396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31309396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31319396SMatthew.Ahrens@Sun.COM 			 */
31329396SMatthew.Ahrens@Sun.COM 			int mode;
31339396SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs, NULL, &mode);
31349396SMatthew.Ahrens@Sun.COM 			if (error == 0) {
31359396SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs,
31369396SMatthew.Ahrens@Sun.COM 				    zc->zc_name, mode);
31379396SMatthew.Ahrens@Sun.COM 			}
31389396SMatthew.Ahrens@Sun.COM 		}
31399396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31409396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31419396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31429396SMatthew.Ahrens@Sun.COM 	} else {
31439396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
31449396SMatthew.Ahrens@Sun.COM 		    DS_MODE_USER, &os);
31459396SMatthew.Ahrens@Sun.COM 		if (error)
31469396SMatthew.Ahrens@Sun.COM 			return (error);
31479396SMatthew.Ahrens@Sun.COM 
31489396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
31499396SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
31509396SMatthew.Ahrens@Sun.COM 	}
31519396SMatthew.Ahrens@Sun.COM 
31529396SMatthew.Ahrens@Sun.COM 	return (error);
31539396SMatthew.Ahrens@Sun.COM }
31549396SMatthew.Ahrens@Sun.COM 
31559396SMatthew.Ahrens@Sun.COM /*
31564543Smarks  * We don't want to have a hard dependency
31574543Smarks  * against some special symbols in sharefs
31585331Samw  * nfs, and smbsrv.  Determine them if needed when
31594543Smarks  * the first file system is shared.
31605331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31614543Smarks  */
31625331Samw int (*znfsexport_fs)(void *arg);
31634543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31645331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31655331Samw 
31665331Samw int zfs_nfsshare_inited;
31675331Samw int zfs_smbshare_inited;
31685331Samw 
31694543Smarks ddi_modhandle_t nfs_mod;
31704543Smarks ddi_modhandle_t sharefs_mod;
31715331Samw ddi_modhandle_t smbsrv_mod;
31724543Smarks kmutex_t zfs_share_lock;
31734543Smarks 
31744543Smarks static int
31755331Samw zfs_init_sharefs()
31765331Samw {
31775331Samw 	int error;
31785331Samw 
31795331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
31805331Samw 	/* Both NFS and SMB shares also require sharetab support. */
31815331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
31825331Samw 	    ddi_modopen("fs/sharefs",
31835331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
31845331Samw 		return (ENOSYS);
31855331Samw 	}
31865331Samw 	if (zshare_fs == NULL && ((zshare_fs =
31875331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
31885331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
31895331Samw 		return (ENOSYS);
31905331Samw 	}
31915331Samw 	return (0);
31925331Samw }
31935331Samw 
31945331Samw static int
31954543Smarks zfs_ioc_share(zfs_cmd_t *zc)
31964543Smarks {
31974543Smarks 	int error;
31984543Smarks 	int opcode;
31994543Smarks 
32005331Samw 	switch (zc->zc_share.z_sharetype) {
32015331Samw 	case ZFS_SHARE_NFS:
32025331Samw 	case ZFS_UNSHARE_NFS:
32035331Samw 		if (zfs_nfsshare_inited == 0) {
32045331Samw 			mutex_enter(&zfs_share_lock);
32055331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
32065331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32075331Samw 				mutex_exit(&zfs_share_lock);
32085331Samw 				return (ENOSYS);
32095331Samw 			}
32105331Samw 			if (znfsexport_fs == NULL &&
32115331Samw 			    ((znfsexport_fs = (int (*)(void *))
32125331Samw 			    ddi_modsym(nfs_mod,
32135331Samw 			    "nfs_export", &error)) == NULL)) {
32145331Samw 				mutex_exit(&zfs_share_lock);
32155331Samw 				return (ENOSYS);
32165331Samw 			}
32175331Samw 			error = zfs_init_sharefs();
32185331Samw 			if (error) {
32195331Samw 				mutex_exit(&zfs_share_lock);
32205331Samw 				return (ENOSYS);
32215331Samw 			}
32225331Samw 			zfs_nfsshare_inited = 1;
32234543Smarks 			mutex_exit(&zfs_share_lock);
32244543Smarks 		}
32255331Samw 		break;
32265331Samw 	case ZFS_SHARE_SMB:
32275331Samw 	case ZFS_UNSHARE_SMB:
32285331Samw 		if (zfs_smbshare_inited == 0) {
32295331Samw 			mutex_enter(&zfs_share_lock);
32305331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32315331Samw 			    ddi_modopen("drv/smbsrv",
32325331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32335331Samw 				mutex_exit(&zfs_share_lock);
32345331Samw 				return (ENOSYS);
32355331Samw 			}
32365331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32375331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32386139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32395331Samw 				mutex_exit(&zfs_share_lock);
32405331Samw 				return (ENOSYS);
32415331Samw 			}
32425331Samw 			error = zfs_init_sharefs();
32435331Samw 			if (error) {
32445331Samw 				mutex_exit(&zfs_share_lock);
32455331Samw 				return (ENOSYS);
32465331Samw 			}
32475331Samw 			zfs_smbshare_inited = 1;
32484543Smarks 			mutex_exit(&zfs_share_lock);
32494543Smarks 		}
32505331Samw 		break;
32515331Samw 	default:
32525331Samw 		return (EINVAL);
32534543Smarks 	}
32544543Smarks 
32555331Samw 	switch (zc->zc_share.z_sharetype) {
32565331Samw 	case ZFS_SHARE_NFS:
32575331Samw 	case ZFS_UNSHARE_NFS:
32585331Samw 		if (error =
32595331Samw 		    znfsexport_fs((void *)
32605331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32615331Samw 			return (error);
32625331Samw 		break;
32635331Samw 	case ZFS_SHARE_SMB:
32645331Samw 	case ZFS_UNSHARE_SMB:
32655331Samw 		if (error = zsmbexport_fs((void *)
32665331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32675331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32688845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32695331Samw 			return (error);
32705331Samw 		}
32715331Samw 		break;
32725331Samw 	}
32735331Samw 
32745331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32755331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32764543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32774543Smarks 
32785331Samw 	/*
32795331Samw 	 * Add or remove share from sharetab
32805331Samw 	 */
32814543Smarks 	error = zshare_fs(opcode,
32824543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
32834543Smarks 	    zc->zc_share.z_sharemax);
32844543Smarks 
32854543Smarks 	return (error);
32864543Smarks 
32874543Smarks }
32884543Smarks 
32898845Samw@Sun.COM ace_t full_access[] = {
32908845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
32918845Samw@Sun.COM };
32928845Samw@Sun.COM 
32938845Samw@Sun.COM /*
32948845Samw@Sun.COM  * Remove all ACL files in shares dir
32958845Samw@Sun.COM  */
32968845Samw@Sun.COM static int
32978845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
32988845Samw@Sun.COM {
32998845Samw@Sun.COM 	zap_cursor_t	zc;
33008845Samw@Sun.COM 	zap_attribute_t	zap;
33018845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
33028845Samw@Sun.COM 	int error;
33038845Samw@Sun.COM 
33048845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
33058845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
33068845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
33078845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
33088845Samw@Sun.COM 		    NULL, 0)) != 0)
33098845Samw@Sun.COM 			break;
33108845Samw@Sun.COM 	}
33118845Samw@Sun.COM 	zap_cursor_fini(&zc);
33128845Samw@Sun.COM 	return (error);
33138845Samw@Sun.COM }
33148845Samw@Sun.COM 
33158845Samw@Sun.COM static int
33168845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33178845Samw@Sun.COM {
33188845Samw@Sun.COM 	vnode_t *vp;
33198845Samw@Sun.COM 	znode_t *dzp;
33208845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33218845Samw@Sun.COM 	znode_t *sharedir;
33228845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33238845Samw@Sun.COM 	nvlist_t *nvlist;
33248845Samw@Sun.COM 	char *src, *target;
33258845Samw@Sun.COM 	vattr_t vattr;
33268845Samw@Sun.COM 	vsecattr_t vsec;
33278845Samw@Sun.COM 	int error = 0;
33288845Samw@Sun.COM 
33298845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33308845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33318845Samw@Sun.COM 		return (error);
33328845Samw@Sun.COM 
33338845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33348845Samw@Sun.COM 
33358845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33368845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33378845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33388845Samw@Sun.COM 		VN_RELE(vp);
33398845Samw@Sun.COM 		return (EINVAL);
33408845Samw@Sun.COM 	}
33418845Samw@Sun.COM 
33428845Samw@Sun.COM 	dzp = VTOZ(vp);
33438845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33448845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33458845Samw@Sun.COM 
33469030SMark.Shellenbaum@Sun.COM 	/*
33479030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33489030SMark.Shellenbaum@Sun.COM 	 */
33499030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33509030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33519030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33529030SMark.Shellenbaum@Sun.COM 
33539030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33549030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33559030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33569030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33579030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33589030SMark.Shellenbaum@Sun.COM 		if (error) {
33599030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33609030SMark.Shellenbaum@Sun.COM 		} else {
33619030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33629030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33639030SMark.Shellenbaum@Sun.COM 		}
33649030SMark.Shellenbaum@Sun.COM 		if (error) {
33659030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33669030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33679030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33689030SMark.Shellenbaum@Sun.COM 			return (error);
33699030SMark.Shellenbaum@Sun.COM 		}
33709030SMark.Shellenbaum@Sun.COM 	}
33719030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33729030SMark.Shellenbaum@Sun.COM 
33739030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33748845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33759030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33768845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33778845Samw@Sun.COM 		return (error);
33788845Samw@Sun.COM 	}
33798845Samw@Sun.COM 
33808845Samw@Sun.COM 	switch (zc->zc_cookie) {
33818845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
33828845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
33838845Samw@Sun.COM 		vattr.va_type = VREG;
33848845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
33858845Samw@Sun.COM 		vattr.va_uid = 0;
33868845Samw@Sun.COM 		vattr.va_gid = 0;
33878845Samw@Sun.COM 
33888845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
33898845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
33908845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
33918845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
33928845Samw@Sun.COM 
33938845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
33948845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
33958845Samw@Sun.COM 		if (resourcevp)
33968845Samw@Sun.COM 			VN_RELE(resourcevp);
33978845Samw@Sun.COM 		break;
33988845Samw@Sun.COM 
33998845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
34008845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
34018845Samw@Sun.COM 		    NULL, 0);
34028845Samw@Sun.COM 		break;
34038845Samw@Sun.COM 
34048845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
34058845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
34069643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
34078845Samw@Sun.COM 			VN_RELE(vp);
34088845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34098845Samw@Sun.COM 			return (error);
34108845Samw@Sun.COM 		}
34118845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34128845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34138845Samw@Sun.COM 		    &target)) {
34148845Samw@Sun.COM 			VN_RELE(vp);
34159179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34168845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34178845Samw@Sun.COM 			return (error);
34188845Samw@Sun.COM 		}
34198845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34208845Samw@Sun.COM 		    kcred, NULL, 0);
34218845Samw@Sun.COM 		nvlist_free(nvlist);
34228845Samw@Sun.COM 		break;
34238845Samw@Sun.COM 
34248845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34258845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34268845Samw@Sun.COM 		break;
34278845Samw@Sun.COM 
34288845Samw@Sun.COM 	default:
34298845Samw@Sun.COM 		error = EINVAL;
34308845Samw@Sun.COM 		break;
34318845Samw@Sun.COM 	}
34328845Samw@Sun.COM 
34338845Samw@Sun.COM 	VN_RELE(vp);
34348845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34358845Samw@Sun.COM 
34368845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34378845Samw@Sun.COM 
34388845Samw@Sun.COM 	return (error);
34398845Samw@Sun.COM }
34408845Samw@Sun.COM 
34414543Smarks /*
3442*10242Schris.kirby@sun.com  * inputs:
3443*10242Schris.kirby@sun.com  * zc_name	name of filesystem
3444*10242Schris.kirby@sun.com  * zc_value	short name of snap
3445*10242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
3446*10242Schris.kirby@sun.com  * zc_cookie	recursive flag
3447*10242Schris.kirby@sun.com  *
3448*10242Schris.kirby@sun.com  * outputs:		none
3449*10242Schris.kirby@sun.com  */
3450*10242Schris.kirby@sun.com static int
3451*10242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
3452*10242Schris.kirby@sun.com {
3453*10242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
3454*10242Schris.kirby@sun.com 
3455*10242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3456*10242Schris.kirby@sun.com 		return (EINVAL);
3457*10242Schris.kirby@sun.com 
3458*10242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
3459*10242Schris.kirby@sun.com 	    zc->zc_string, recursive));
3460*10242Schris.kirby@sun.com }
3461*10242Schris.kirby@sun.com 
3462*10242Schris.kirby@sun.com /*
3463*10242Schris.kirby@sun.com  * inputs:
3464*10242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
3465*10242Schris.kirby@sun.com  * zc_value	short name of snap
3466*10242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
3467*10242Schris.kirby@sun.com  * zc_cookie	recursive flag
3468*10242Schris.kirby@sun.com  *
3469*10242Schris.kirby@sun.com  * outputs:		none
3470*10242Schris.kirby@sun.com  */
3471*10242Schris.kirby@sun.com static int
3472*10242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
3473*10242Schris.kirby@sun.com {
3474*10242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
3475*10242Schris.kirby@sun.com 
3476*10242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3477*10242Schris.kirby@sun.com 		return (EINVAL);
3478*10242Schris.kirby@sun.com 
3479*10242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
3480*10242Schris.kirby@sun.com 	    zc->zc_string, recursive));
3481*10242Schris.kirby@sun.com }
3482*10242Schris.kirby@sun.com 
3483*10242Schris.kirby@sun.com /*
3484*10242Schris.kirby@sun.com  * inputs:
3485*10242Schris.kirby@sun.com  * zc_name		name of filesystem
3486*10242Schris.kirby@sun.com  *
3487*10242Schris.kirby@sun.com  * outputs:
3488*10242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
3489*10242Schris.kirby@sun.com  */
3490*10242Schris.kirby@sun.com static int
3491*10242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
3492*10242Schris.kirby@sun.com {
3493*10242Schris.kirby@sun.com 	nvlist_t *nvp;
3494*10242Schris.kirby@sun.com 	int error;
3495*10242Schris.kirby@sun.com 
3496*10242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
3497*10242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
3498*10242Schris.kirby@sun.com 		nvlist_free(nvp);
3499*10242Schris.kirby@sun.com 	}
3500*10242Schris.kirby@sun.com 
3501*10242Schris.kirby@sun.com 	return (error);
3502*10242Schris.kirby@sun.com }
3503*10242Schris.kirby@sun.com 
3504*10242Schris.kirby@sun.com /*
35054988Sek110237  * pool create, destroy, and export don't log the history as part of
35064988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35074988Sek110237  * do the logging of those commands.
35084543Smarks  */
3509789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
35109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35119234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35149234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35159234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35169234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35179234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35189234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
35199234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35209234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35219234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35229234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
35239234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35259234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
35279234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
35299234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35319234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35339234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35359234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35399234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35419234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35439234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35449425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35459425SEric.Schrock@Sun.COM 	    B_TRUE },
35469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35479234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35499234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35519234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35539234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
35559234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
35569234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
35589234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
35609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
35619234SGeorge.Wilson@Sun.COM 	    B_TRUE},
35629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
35639234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
35659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
35669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
35679234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
35689234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35699234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35709234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35719234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35729234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35739234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
35749234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
35769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
35779234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35789234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
35799234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
35819234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35839234SGeorge.Wilson@Sun.COM 	    B_FALSE },
358410233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
358510233SGeorge.Wilson@Sun.COM 	    B_TRUE },
35869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35879234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35889234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35899234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35909234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
35919234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35929234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35939234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
35959234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
35979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
35989234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
36009396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
36019396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
36029396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36039396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
36049396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36059396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
36069396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
3607*10242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
3608*10242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
3609*10242Schris.kirby@sun.com 	    B_TRUE },
3610*10242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3611*10242Schris.kirby@sun.com 	    B_TRUE }
3612789Sahrens };
3613789Sahrens 
36149234SGeorge.Wilson@Sun.COM int
36159234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
36169234SGeorge.Wilson@Sun.COM {
36179234SGeorge.Wilson@Sun.COM 	spa_t *spa;
36189234SGeorge.Wilson@Sun.COM 	int error;
36199234SGeorge.Wilson@Sun.COM 
36209234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
36219234SGeorge.Wilson@Sun.COM 
36229396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
36239234SGeorge.Wilson@Sun.COM 	if (error == 0) {
36249234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
36259234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
36269234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
36279234SGeorge.Wilson@Sun.COM 	}
36289234SGeorge.Wilson@Sun.COM 	return (error);
36299234SGeorge.Wilson@Sun.COM }
36309234SGeorge.Wilson@Sun.COM 
3631789Sahrens static int
3632789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3633789Sahrens {
3634789Sahrens 	zfs_cmd_t *zc;
3635789Sahrens 	uint_t vec;
36362199Sahrens 	int error, rc;
3637789Sahrens 
3638789Sahrens 	if (getminor(dev) != 0)
3639789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3640789Sahrens 
3641789Sahrens 	vec = cmd - ZFS_IOC;
36424787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3643789Sahrens 
3644789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3645789Sahrens 		return (EINVAL);
3646789Sahrens 
3647789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3648789Sahrens 
36499643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3650789Sahrens 
36514787Sahrens 	if (error == 0)
36524543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3653789Sahrens 
3654789Sahrens 	/*
3655789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3656789Sahrens 	 * the lower layers.
3657789Sahrens 	 */
3658789Sahrens 	if (error == 0) {
3659789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
36609643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3661789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
36624577Sahrens 		case POOL_NAME:
3663789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3664789Sahrens 				error = EINVAL;
36659234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36669234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36679234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3668789Sahrens 			break;
3669789Sahrens 
36704577Sahrens 		case DATASET_NAME:
3671789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3672789Sahrens 				error = EINVAL;
36739234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36749234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36759234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3676789Sahrens 			break;
36772856Snd150628 
36784577Sahrens 		case NO_NAME:
36792856Snd150628 			break;
3680789Sahrens 		}
3681789Sahrens 	}
3682789Sahrens 
3683789Sahrens 	if (error == 0)
3684789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3685789Sahrens 
36869643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
36874543Smarks 	if (error == 0) {
36882199Sahrens 		error = rc;
36899396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
36904543Smarks 			zfs_log_history(zc);
36914543Smarks 	}
3692789Sahrens 
3693789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3694789Sahrens 	return (error);
3695789Sahrens }
3696789Sahrens 
3697789Sahrens static int
3698789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3699789Sahrens {
3700789Sahrens 	if (cmd != DDI_ATTACH)
3701789Sahrens 		return (DDI_FAILURE);
3702789Sahrens 
3703789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3704789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3705789Sahrens 		return (DDI_FAILURE);
3706789Sahrens 
3707789Sahrens 	zfs_dip = dip;
3708789Sahrens 
3709789Sahrens 	ddi_report_dev(dip);
3710789Sahrens 
3711789Sahrens 	return (DDI_SUCCESS);
3712789Sahrens }
3713789Sahrens 
3714789Sahrens static int
3715789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3716789Sahrens {
3717789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3718789Sahrens 		return (DDI_FAILURE);
3719789Sahrens 
3720789Sahrens 	if (cmd != DDI_DETACH)
3721789Sahrens 		return (DDI_FAILURE);
3722789Sahrens 
3723789Sahrens 	zfs_dip = NULL;
3724789Sahrens 
3725789Sahrens 	ddi_prop_remove_all(dip);
3726789Sahrens 	ddi_remove_minor_node(dip, NULL);
3727789Sahrens 
3728789Sahrens 	return (DDI_SUCCESS);
3729789Sahrens }
3730789Sahrens 
3731789Sahrens /*ARGSUSED*/
3732789Sahrens static int
3733789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3734789Sahrens {
3735789Sahrens 	switch (infocmd) {
3736789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3737789Sahrens 		*result = zfs_dip;
3738789Sahrens 		return (DDI_SUCCESS);
3739789Sahrens 
3740789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3741849Sbonwick 		*result = (void *)0;
3742789Sahrens 		return (DDI_SUCCESS);
3743789Sahrens 	}
3744789Sahrens 
3745789Sahrens 	return (DDI_FAILURE);
3746789Sahrens }
3747789Sahrens 
3748789Sahrens /*
3749789Sahrens  * OK, so this is a little weird.
3750789Sahrens  *
3751789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3752789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3753789Sahrens  *
3754789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3755789Sahrens  * so most of the standard driver entry points are in zvol.c.
3756789Sahrens  */
3757789Sahrens static struct cb_ops zfs_cb_ops = {
3758789Sahrens 	zvol_open,	/* open */
3759789Sahrens 	zvol_close,	/* close */
3760789Sahrens 	zvol_strategy,	/* strategy */
3761789Sahrens 	nodev,		/* print */
37626423Sgw25295 	zvol_dump,	/* dump */
3763789Sahrens 	zvol_read,	/* read */
3764789Sahrens 	zvol_write,	/* write */
3765789Sahrens 	zfsdev_ioctl,	/* ioctl */
3766789Sahrens 	nodev,		/* devmap */
3767789Sahrens 	nodev,		/* mmap */
3768789Sahrens 	nodev,		/* segmap */
3769789Sahrens 	nochpoll,	/* poll */
3770789Sahrens 	ddi_prop_op,	/* prop_op */
3771789Sahrens 	NULL,		/* streamtab */
3772789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3773789Sahrens 	CB_REV,		/* version */
37743638Sbillm 	nodev,		/* async read */
37753638Sbillm 	nodev,		/* async write */
3776789Sahrens };
3777789Sahrens 
3778789Sahrens static struct dev_ops zfs_dev_ops = {
3779789Sahrens 	DEVO_REV,	/* version */
3780789Sahrens 	0,		/* refcnt */
3781789Sahrens 	zfs_info,	/* info */
3782789Sahrens 	nulldev,	/* identify */
3783789Sahrens 	nulldev,	/* probe */
3784789Sahrens 	zfs_attach,	/* attach */
3785789Sahrens 	zfs_detach,	/* detach */
3786789Sahrens 	nodev,		/* reset */
3787789Sahrens 	&zfs_cb_ops,	/* driver operations */
37887656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
37897656SSherry.Moore@Sun.COM 	NULL,		/* power */
37907656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3791789Sahrens };
3792789Sahrens 
3793789Sahrens static struct modldrv zfs_modldrv = {
37947656SSherry.Moore@Sun.COM 	&mod_driverops,
37957656SSherry.Moore@Sun.COM 	"ZFS storage pool",
37967656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3797789Sahrens };
3798789Sahrens 
3799789Sahrens static struct modlinkage modlinkage = {
3800789Sahrens 	MODREV_1,
3801789Sahrens 	(void *)&zfs_modlfs,
3802789Sahrens 	(void *)&zfs_modldrv,
3803789Sahrens 	NULL
3804789Sahrens };
3805789Sahrens 
38064720Sfr157268 
38074720Sfr157268 uint_t zfs_fsyncer_key;
38085326Sek110237 extern uint_t rrw_tsd_key;
38094720Sfr157268 
3810789Sahrens int
3811789Sahrens _init(void)
3812789Sahrens {
3813789Sahrens 	int error;
3814789Sahrens 
3815849Sbonwick 	spa_init(FREAD | FWRITE);
3816849Sbonwick 	zfs_init();
3817849Sbonwick 	zvol_init();
3818849Sbonwick 
3819849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3820849Sbonwick 		zvol_fini();
3821849Sbonwick 		zfs_fini();
3822849Sbonwick 		spa_fini();
3823789Sahrens 		return (error);
3824849Sbonwick 	}
3825789Sahrens 
38264720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
38275326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
38284720Sfr157268 
3829789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3830789Sahrens 	ASSERT(error == 0);
38314543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3832789Sahrens 
3833789Sahrens 	return (0);
3834789Sahrens }
3835789Sahrens 
3836789Sahrens int
3837789Sahrens _fini(void)
3838789Sahrens {
3839789Sahrens 	int error;
3840789Sahrens 
38411544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3842789Sahrens 		return (EBUSY);
3843789Sahrens 
3844789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3845789Sahrens 		return (error);
3846789Sahrens 
3847789Sahrens 	zvol_fini();
3848789Sahrens 	zfs_fini();
3849789Sahrens 	spa_fini();
38505331Samw 	if (zfs_nfsshare_inited)
38514543Smarks 		(void) ddi_modclose(nfs_mod);
38525331Samw 	if (zfs_smbshare_inited)
38535331Samw 		(void) ddi_modclose(smbsrv_mod);
38545331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
38554543Smarks 		(void) ddi_modclose(sharefs_mod);
3856789Sahrens 
38574720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3858789Sahrens 	ldi_ident_release(zfs_li);
3859789Sahrens 	zfs_li = NULL;
38604543Smarks 	mutex_destroy(&zfs_share_lock);
3861789Sahrens 
3862789Sahrens 	return (error);
3863789Sahrens }
3864789Sahrens 
3865789Sahrens int
3866789Sahrens _info(struct modinfo *modinfop)
3867789Sahrens {
3868789Sahrens 	return (mod_info(&modlinkage, modinfop));
3869789Sahrens }
3870