xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10921:8aac17999e4d)
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 {
17810298SMatthew.Ahrens@Sun.COM 	objset_t *os;
17910298SMatthew.Ahrens@Sun.COM 
18010298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
18110298SMatthew.Ahrens@Sun.COM 		boolean_t ret;
18210298SMatthew.Ahrens@Sun.COM 		ret = (dmu_objset_id(os) == dmu_objset_spa(os)->spa_bootfs);
18310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18410298SMatthew.Ahrens@Sun.COM 		return (ret);
1857042Sgw25295 	}
18610298SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1877042Sgw25295 }
1887042Sgw25295 
1897042Sgw25295 /*
1907184Stimh  * zfs_earlier_version
1915375Stimh  *
1925375Stimh  *	Return non-zero if the spa version is less than requested version.
1935375Stimh  */
1945331Samw static int
1957184Stimh zfs_earlier_version(const char *name, int version)
1965331Samw {
1975331Samw 	spa_t *spa;
1985331Samw 
1995331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
2005331Samw 		if (spa_version(spa) < version) {
2015331Samw 			spa_close(spa, FTAG);
2025331Samw 			return (1);
2035331Samw 		}
2045331Samw 		spa_close(spa, FTAG);
2055331Samw 	}
2065331Samw 	return (0);
2075331Samw }
2085331Samw 
2095977Smarks /*
2106689Smaybee  * zpl_earlier_version
2115977Smarks  *
2126689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2135977Smarks  */
2146689Smaybee static boolean_t
2156689Smaybee zpl_earlier_version(const char *name, int version)
2165977Smarks {
2175977Smarks 	objset_t *os;
2186689Smaybee 	boolean_t rc = B_TRUE;
2195977Smarks 
22010298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
2216689Smaybee 		uint64_t zplversion;
2226689Smaybee 
22310298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
22410298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
22510298SMatthew.Ahrens@Sun.COM 			return (B_TRUE);
22610298SMatthew.Ahrens@Sun.COM 		}
22710298SMatthew.Ahrens@Sun.COM 		/* XXX reading from non-owned objset */
2286689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2296689Smaybee 			rc = zplversion < version;
23010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
2315977Smarks 	}
2325977Smarks 	return (rc);
2335977Smarks }
2345977Smarks 
2354715Sek110237 static void
2364543Smarks zfs_log_history(zfs_cmd_t *zc)
2374543Smarks {
2384543Smarks 	spa_t *spa;
2394603Sahrens 	char *buf;
2404543Smarks 
2414715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2424577Sahrens 		return;
2434577Sahrens 
2444715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2454715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2464715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2474715Sek110237 		spa_close(spa, FTAG);
2484543Smarks 	}
2494715Sek110237 	history_str_free(buf);
2504543Smarks }
2514543Smarks 
252789Sahrens /*
253789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
254789Sahrens  * and can be used in the local zone, as there is no associated dataset.
255789Sahrens  */
256789Sahrens /* ARGSUSED */
257789Sahrens static int
2584543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
259789Sahrens {
260789Sahrens 	return (0);
261789Sahrens }
262789Sahrens 
263789Sahrens /*
264789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
265789Sahrens  * no privileges, but must be visible in the local zone.
266789Sahrens  */
267789Sahrens /* ARGSUSED */
268789Sahrens static int
2694543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
270789Sahrens {
271789Sahrens 	if (INGLOBALZONE(curproc) ||
2724543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
273789Sahrens 		return (0);
274789Sahrens 
275789Sahrens 	return (ENOENT);
276789Sahrens }
277789Sahrens 
278789Sahrens static int
279789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
280789Sahrens {
281789Sahrens 	uint64_t zoned;
282789Sahrens 	int writable = 1;
283789Sahrens 
284789Sahrens 	/*
285789Sahrens 	 * The dataset must be visible by this zone -- check this first
286789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
287789Sahrens 	 */
288789Sahrens 	if (!INGLOBALZONE(curproc) &&
289789Sahrens 	    !zone_dataset_visible(dataset, &writable))
290789Sahrens 		return (ENOENT);
291789Sahrens 
292789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
293789Sahrens 		return (ENOENT);
294789Sahrens 
295789Sahrens 	if (INGLOBALZONE(curproc)) {
296789Sahrens 		/*
297789Sahrens 		 * If the fs is zoned, only root can access it from the
298789Sahrens 		 * global zone.
299789Sahrens 		 */
300789Sahrens 		if (secpolicy_zfs(cr) && zoned)
301789Sahrens 			return (EPERM);
302789Sahrens 	} else {
303789Sahrens 		/*
304789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
305789Sahrens 		 */
306789Sahrens 		if (!zoned)
307789Sahrens 			return (EPERM);
308789Sahrens 
309789Sahrens 		/* must be writable by this zone */
310789Sahrens 		if (!writable)
311789Sahrens 			return (EPERM);
312789Sahrens 	}
313789Sahrens 	return (0);
314789Sahrens }
315789Sahrens 
316789Sahrens int
3174543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
318789Sahrens {
319789Sahrens 	int error;
320789Sahrens 
3214543Smarks 	error = zfs_dozonecheck(name, cr);
3224543Smarks 	if (error == 0) {
3234543Smarks 		error = secpolicy_zfs(cr);
3244670Sahrens 		if (error)
3254543Smarks 			error = dsl_deleg_access(name, perm, cr);
3264543Smarks 	}
3274543Smarks 	return (error);
3284543Smarks }
3294543Smarks 
3304543Smarks static int
3314543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3324543Smarks {
3334543Smarks 	/*
3344543Smarks 	 * Check permissions for special properties.
3354543Smarks 	 */
3364543Smarks 	switch (prop) {
3374543Smarks 	case ZFS_PROP_ZONED:
3384543Smarks 		/*
3394543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3404543Smarks 		 */
3414543Smarks 		if (!INGLOBALZONE(curproc))
3424543Smarks 			return (EPERM);
3434543Smarks 		break;
344789Sahrens 
3454543Smarks 	case ZFS_PROP_QUOTA:
3464543Smarks 		if (!INGLOBALZONE(curproc)) {
3474543Smarks 			uint64_t zoned;
3484543Smarks 			char setpoint[MAXNAMELEN];
3494543Smarks 			/*
3504543Smarks 			 * Unprivileged users are allowed to modify the
3514543Smarks 			 * quota on things *under* (ie. contained by)
3524543Smarks 			 * the thing they own.
3534543Smarks 			 */
3544543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3554543Smarks 			    setpoint))
3564543Smarks 				return (EPERM);
3574670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3584543Smarks 				return (EPERM);
3594543Smarks 		}
3604670Sahrens 		break;
3614543Smarks 	}
3624543Smarks 
3634787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
364789Sahrens }
365789Sahrens 
3664543Smarks int
3674543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3684543Smarks {
3694543Smarks 	int error;
3704543Smarks 
3714543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3724543Smarks 	if (error)
3734543Smarks 		return (error);
3744543Smarks 
3754543Smarks 	/*
3764543Smarks 	 * permission to set permissions will be evaluated later in
3774543Smarks 	 * dsl_deleg_can_allow()
3784543Smarks 	 */
3794543Smarks 	return (0);
3804543Smarks }
3814543Smarks 
3824543Smarks int
3834543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3844543Smarks {
38510588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
38610588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_ROLLBACK, cr));
3874543Smarks }
3884543Smarks 
3894543Smarks int
3904543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3914543Smarks {
3924543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
3934543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
3944543Smarks }
3954543Smarks 
3968845Samw@Sun.COM static int
3978845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
3988845Samw@Sun.COM {
3998845Samw@Sun.COM 	vnode_t *vp;
4008845Samw@Sun.COM 	int error;
4018845Samw@Sun.COM 
4028845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4038845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
4048845Samw@Sun.COM 		return (error);
4058845Samw@Sun.COM 
4068845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
4078845Samw@Sun.COM 
4088845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4098845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4108845Samw@Sun.COM 	    zc->zc_name) != 0)) {
4118845Samw@Sun.COM 		VN_RELE(vp);
4128845Samw@Sun.COM 		return (EPERM);
4138845Samw@Sun.COM 	}
4148845Samw@Sun.COM 
4158845Samw@Sun.COM 	VN_RELE(vp);
4168845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
4178845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
4188845Samw@Sun.COM }
4198845Samw@Sun.COM 
4204543Smarks int
4214543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
4224543Smarks {
4234543Smarks 	if (!INGLOBALZONE(curproc))
4244543Smarks 		return (EPERM);
4254543Smarks 
4265367Sahrens 	if (secpolicy_nfs(cr) == 0) {
4274543Smarks 		return (0);
4284543Smarks 	} else {
4298845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4308845Samw@Sun.COM 	}
4318845Samw@Sun.COM }
4328845Samw@Sun.COM 
4338845Samw@Sun.COM int
4348845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
4358845Samw@Sun.COM {
4368845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
4378845Samw@Sun.COM 		return (EPERM);
4388845Samw@Sun.COM 
4398845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
4408845Samw@Sun.COM 		return (0);
4418845Samw@Sun.COM 	} else {
4428845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4434543Smarks 	}
4444543Smarks }
4454543Smarks 
446789Sahrens static int
4474543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
448789Sahrens {
449789Sahrens 	char *cp;
450789Sahrens 
451789Sahrens 	/*
452789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
453789Sahrens 	 */
4544543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4554543Smarks 	cp = strrchr(parent, '@');
456789Sahrens 	if (cp != NULL) {
457789Sahrens 		cp[0] = '\0';
458789Sahrens 	} else {
4594543Smarks 		cp = strrchr(parent, '/');
460789Sahrens 		if (cp == NULL)
461789Sahrens 			return (ENOENT);
462789Sahrens 		cp[0] = '\0';
463789Sahrens 	}
464789Sahrens 
4654543Smarks 	return (0);
4664543Smarks }
4674543Smarks 
4684543Smarks int
4694543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4704543Smarks {
4714543Smarks 	int error;
4724543Smarks 
4734543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4744543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4754543Smarks 		return (error);
4764543Smarks 
4774543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4784543Smarks }
4794543Smarks 
4804543Smarks static int
4814543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4824543Smarks {
4834543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4844543Smarks }
4854543Smarks 
4864543Smarks /*
4874543Smarks  * Must have sys_config privilege to check the iscsi permission
4884543Smarks  */
4894543Smarks /* ARGSUSED */
4904543Smarks static int
4914543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
4924543Smarks {
4934543Smarks 	return (secpolicy_zfs(cr));
4944543Smarks }
4954543Smarks 
4964543Smarks int
4974543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
4984543Smarks {
4994543Smarks 	char 	parentname[MAXNAMELEN];
5004543Smarks 	int	error;
5014543Smarks 
5024543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5034543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
5044543Smarks 		return (error);
5054543Smarks 
5064543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5074543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5084543Smarks 		return (error);
5094543Smarks 
5104543Smarks 	if ((error = zfs_get_parent(to, parentname,
5114543Smarks 	    sizeof (parentname))) != 0)
5124543Smarks 		return (error);
5134543Smarks 
5144543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5154543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
5164543Smarks 		return (error);
5174543Smarks 
5184543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5194543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5204543Smarks 		return (error);
5214543Smarks 
5224543Smarks 	return (error);
5234543Smarks }
5244543Smarks 
5254543Smarks static int
5264543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5274543Smarks {
5284543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5294543Smarks }
5304543Smarks 
5314543Smarks static int
5324543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5334543Smarks {
5344543Smarks 	char 	parentname[MAXNAMELEN];
5354543Smarks 	objset_t *clone;
5364543Smarks 	int error;
5374543Smarks 
5384543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5394543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5404543Smarks 	if (error)
5414543Smarks 		return (error);
5424543Smarks 
54310298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
5444543Smarks 
5454543Smarks 	if (error == 0) {
5464543Smarks 		dsl_dataset_t *pclone = NULL;
5474543Smarks 		dsl_dir_t *dd;
54810298SMatthew.Ahrens@Sun.COM 		dd = clone->os_dsl_dataset->ds_dir;
5494543Smarks 
5504543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5516689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5526689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5534543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5544543Smarks 		if (error) {
55510298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(clone, FTAG);
5564543Smarks 			return (error);
5574543Smarks 		}
5584543Smarks 
5594543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5604543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5614543Smarks 
5624543Smarks 		dsl_dataset_name(pclone, parentname);
56310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
5646689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5654543Smarks 		if (error == 0)
5664543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5674543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5684543Smarks 	}
5694543Smarks 	return (error);
5704543Smarks }
5714543Smarks 
5724543Smarks static int
5734543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5744543Smarks {
5754543Smarks 	int error;
5764543Smarks 
5774543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5784543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5794543Smarks 		return (error);
5804543Smarks 
5814543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5824543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5834543Smarks 		return (error);
5844543Smarks 
5854543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5864543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5874543Smarks }
5884543Smarks 
5894543Smarks int
5904543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
5914543Smarks {
59210588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(name,
59310588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
5944543Smarks }
5954543Smarks 
5964543Smarks static int
5974543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
5984543Smarks {
5994543Smarks 
6004543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
6014543Smarks }
6024543Smarks 
6034543Smarks static int
6044543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
6054543Smarks {
6064543Smarks 	char 	parentname[MAXNAMELEN];
6074543Smarks 	int 	error;
6084543Smarks 
6094543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
6104543Smarks 	    sizeof (parentname))) != 0)
6114543Smarks 		return (error);
6124543Smarks 
6134543Smarks 	if (zc->zc_value[0] != '\0') {
6144543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6154543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6164543Smarks 			return (error);
6174543Smarks 	}
6184543Smarks 
6194543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6204543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6214543Smarks 		return (error);
6224543Smarks 
6234543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6244543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6254543Smarks 
6264543Smarks 	return (error);
6274543Smarks }
6284543Smarks 
6294543Smarks static int
6304543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6314543Smarks {
6324543Smarks 	int error;
6334543Smarks 
6344543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6354543Smarks 	if (error) {
6364543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6374543Smarks 	}
6384543Smarks 	return (error);
639789Sahrens }
640789Sahrens 
641789Sahrens /*
642789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
643789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
644789Sahrens  */
645789Sahrens /* ARGSUSED */
646789Sahrens static int
6474543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
648789Sahrens {
649789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
650789Sahrens 		return (EPERM);
651789Sahrens 
652789Sahrens 	return (0);
653789Sahrens }
654789Sahrens 
655789Sahrens /*
6561544Seschrock  * Policy for fault injection.  Requires all privileges.
6571544Seschrock  */
6581544Seschrock /* ARGSUSED */
6591544Seschrock static int
6604543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6611544Seschrock {
6621544Seschrock 	return (secpolicy_zinject(cr));
6631544Seschrock }
6641544Seschrock 
6654849Sahrens static int
6664849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
6674849Sahrens {
6684849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
6694849Sahrens 
6705094Slling 	if (prop == ZPROP_INVAL) {
6714849Sahrens 		if (!zfs_prop_user(zc->zc_value))
6724849Sahrens 			return (EINVAL);
6734849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
6744849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
6754849Sahrens 	} else {
6764849Sahrens 		if (!zfs_prop_inheritable(prop))
6774849Sahrens 			return (EINVAL);
6784849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
6794849Sahrens 	}
6804849Sahrens }
6814849Sahrens 
6829396SMatthew.Ahrens@Sun.COM static int
6839396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
6849396SMatthew.Ahrens@Sun.COM {
6859396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
6869396SMatthew.Ahrens@Sun.COM 	if (err)
6879396SMatthew.Ahrens@Sun.COM 		return (err);
6889396SMatthew.Ahrens@Sun.COM 
6899396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
6909396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
6919396SMatthew.Ahrens@Sun.COM 
6929396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
6939396SMatthew.Ahrens@Sun.COM 		/*
6949396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
6959396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
6969396SMatthew.Ahrens@Sun.COM 		 */
6979396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
6989396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
6999396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
7009396SMatthew.Ahrens@Sun.COM 				return (0);
7019396SMatthew.Ahrens@Sun.COM 		} else {
7029396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
7039396SMatthew.Ahrens@Sun.COM 				return (0);
7049396SMatthew.Ahrens@Sun.COM 		}
7059396SMatthew.Ahrens@Sun.COM 	}
7069396SMatthew.Ahrens@Sun.COM 
7079396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7089396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7099396SMatthew.Ahrens@Sun.COM }
7109396SMatthew.Ahrens@Sun.COM 
7119396SMatthew.Ahrens@Sun.COM static int
7129396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
7139396SMatthew.Ahrens@Sun.COM {
7149396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
7159396SMatthew.Ahrens@Sun.COM 	if (err)
7169396SMatthew.Ahrens@Sun.COM 		return (err);
7179396SMatthew.Ahrens@Sun.COM 
7189396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
7199396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7209396SMatthew.Ahrens@Sun.COM 
7219396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7229396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7239396SMatthew.Ahrens@Sun.COM }
7249396SMatthew.Ahrens@Sun.COM 
7259396SMatthew.Ahrens@Sun.COM static int
7269396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
7279396SMatthew.Ahrens@Sun.COM {
7289396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
7299396SMatthew.Ahrens@Sun.COM }
7309396SMatthew.Ahrens@Sun.COM 
73110242Schris.kirby@sun.com static int
73210242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
73310242Schris.kirby@sun.com {
73410242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
73510242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
73610242Schris.kirby@sun.com }
73710242Schris.kirby@sun.com 
73810242Schris.kirby@sun.com static int
73910242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
74010242Schris.kirby@sun.com {
74110242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
74210242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
74310242Schris.kirby@sun.com }
74410242Schris.kirby@sun.com 
7451544Seschrock /*
746789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
747789Sahrens  */
748789Sahrens static int
7499643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
750789Sahrens {
751789Sahrens 	char *packed;
752789Sahrens 	int error;
7535094Slling 	nvlist_t *list = NULL;
754789Sahrens 
755789Sahrens 	/*
7562676Seschrock 	 * Read in and unpack the user-supplied nvlist.
757789Sahrens 	 */
7585094Slling 	if (size == 0)
759789Sahrens 		return (EINVAL);
760789Sahrens 
761789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
762789Sahrens 
7639643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
7649643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
765789Sahrens 		kmem_free(packed, size);
766789Sahrens 		return (error);
767789Sahrens 	}
768789Sahrens 
7695094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
770789Sahrens 		kmem_free(packed, size);
771789Sahrens 		return (error);
772789Sahrens 	}
773789Sahrens 
774789Sahrens 	kmem_free(packed, size);
775789Sahrens 
7765094Slling 	*nvp = list;
777789Sahrens 	return (0);
778789Sahrens }
779789Sahrens 
780789Sahrens static int
7812676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
7822676Seschrock {
7832676Seschrock 	char *packed = NULL;
7842676Seschrock 	size_t size;
7852676Seschrock 	int error;
7862676Seschrock 
7872676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
7882676Seschrock 
7892676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
7902676Seschrock 		error = ENOMEM;
7912676Seschrock 	} else {
7924611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
7932676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
7942676Seschrock 		    KM_SLEEP) == 0);
7959643SEric.Taylor@Sun.COM 		error = ddi_copyout(packed,
7969643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
7972676Seschrock 		kmem_free(packed, size);
7982676Seschrock 	}
7992676Seschrock 
8002676Seschrock 	zc->zc_nvlist_dst_size = size;
8012676Seschrock 	return (error);
8022676Seschrock }
8032676Seschrock 
8042676Seschrock static int
8059396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
8069396SMatthew.Ahrens@Sun.COM {
8079396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8089396SMatthew.Ahrens@Sun.COM 	int error;
8099396SMatthew.Ahrens@Sun.COM 
81010298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(dsname, FTAG, &os);
8119396SMatthew.Ahrens@Sun.COM 	if (error)
8129396SMatthew.Ahrens@Sun.COM 		return (error);
81310298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
81410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
81510298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
81610298SMatthew.Ahrens@Sun.COM 	}
81710298SMatthew.Ahrens@Sun.COM 
81810298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_user_ptr_lock);
8199396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
8209396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
8219396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
8229396SMatthew.Ahrens@Sun.COM 	} else {
8239396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
8249396SMatthew.Ahrens@Sun.COM 	}
82510298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_user_ptr_lock);
82610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
8279396SMatthew.Ahrens@Sun.COM 	return (error);
8289396SMatthew.Ahrens@Sun.COM }
8299396SMatthew.Ahrens@Sun.COM 
8309396SMatthew.Ahrens@Sun.COM /*
8319396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
8329396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
8339396SMatthew.Ahrens@Sun.COM  */
8349396SMatthew.Ahrens@Sun.COM static int
83510298SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp)
8369396SMatthew.Ahrens@Sun.COM {
8379396SMatthew.Ahrens@Sun.COM 	int error = 0;
8389396SMatthew.Ahrens@Sun.COM 
8399396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
84010298SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, zvp);
8419396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
8429396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
8439396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
8449396SMatthew.Ahrens@Sun.COM 			/*
8459396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
8469396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
8479396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
8489396SMatthew.Ahrens@Sun.COM 			 */
8499396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
8509396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
8519396SMatthew.Ahrens@Sun.COM 		}
8529396SMatthew.Ahrens@Sun.COM 	}
8539396SMatthew.Ahrens@Sun.COM 	return (error);
8549396SMatthew.Ahrens@Sun.COM }
8559396SMatthew.Ahrens@Sun.COM 
8569396SMatthew.Ahrens@Sun.COM static void
8579396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
8589396SMatthew.Ahrens@Sun.COM {
8599396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
8609396SMatthew.Ahrens@Sun.COM 
8619396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
8629396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
8639396SMatthew.Ahrens@Sun.COM 	} else {
86410298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
8659396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
8669396SMatthew.Ahrens@Sun.COM 	}
8679396SMatthew.Ahrens@Sun.COM }
8689396SMatthew.Ahrens@Sun.COM 
8699396SMatthew.Ahrens@Sun.COM static int
870789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
871789Sahrens {
872789Sahrens 	int error;
8735094Slling 	nvlist_t *config, *props = NULL;
8747184Stimh 	nvlist_t *rootprops = NULL;
8757184Stimh 	nvlist_t *zplprops = NULL;
8764715Sek110237 	char *buf;
877789Sahrens 
8785094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8799643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
8804988Sek110237 		return (error);
8814715Sek110237 
8825094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8839643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
8849643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
8855094Slling 		nvlist_free(config);
8865094Slling 		return (error);
8875094Slling 	}
8885094Slling 
8897184Stimh 	if (props) {
8907184Stimh 		nvlist_t *nvl = NULL;
8917184Stimh 		uint64_t version = SPA_VERSION;
8927184Stimh 
8937184Stimh 		(void) nvlist_lookup_uint64(props,
8947184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
8957184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
8967184Stimh 			error = EINVAL;
8977184Stimh 			goto pool_props_bad;
8987184Stimh 		}
8997184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9007184Stimh 		if (nvl) {
9017184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9027184Stimh 			if (error != 0) {
9037184Stimh 				nvlist_free(config);
9047184Stimh 				nvlist_free(props);
9057184Stimh 				return (error);
9067184Stimh 			}
9077184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9087184Stimh 		}
9097184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9107184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
9117184Stimh 		    zplprops, NULL);
9127184Stimh 		if (error)
9137184Stimh 			goto pool_props_bad;
9147184Stimh 	}
9157184Stimh 
9164988Sek110237 	buf = history_str_get(zc);
917789Sahrens 
9187184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9197184Stimh 
9207184Stimh 	/*
9217184Stimh 	 * Set the remaining root properties
9227184Stimh 	 */
9237184Stimh 	if (!error &&
9247184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9257184Stimh 		(void) spa_destroy(zc->zc_name);
926789Sahrens 
9274988Sek110237 	if (buf != NULL)
9284988Sek110237 		history_str_free(buf);
9295094Slling 
9307184Stimh pool_props_bad:
9317184Stimh 	nvlist_free(rootprops);
9327184Stimh 	nvlist_free(zplprops);
933789Sahrens 	nvlist_free(config);
9347184Stimh 	nvlist_free(props);
9355094Slling 
936789Sahrens 	return (error);
937789Sahrens }
938789Sahrens 
939789Sahrens static int
940789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
941789Sahrens {
9424543Smarks 	int error;
9434543Smarks 	zfs_log_history(zc);
9444543Smarks 	error = spa_destroy(zc->zc_name);
94510588SEric.Taylor@Sun.COM 	if (error == 0)
94610588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
9474543Smarks 	return (error);
948789Sahrens }
949789Sahrens 
950789Sahrens static int
951789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
952789Sahrens {
9535094Slling 	nvlist_t *config, *props = NULL;
954789Sahrens 	uint64_t guid;
955*10921STim.Haley@Sun.COM 	int error;
956789Sahrens 
9575094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9589643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
959789Sahrens 		return (error);
960789Sahrens 
9615094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9629643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9639643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9645094Slling 		nvlist_free(config);
9655094Slling 		return (error);
9665094Slling 	}
9675094Slling 
968789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
9691544Seschrock 	    guid != zc->zc_guid)
970789Sahrens 		error = EINVAL;
9716643Seschrock 	else if (zc->zc_cookie)
972*10921STim.Haley@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config, props);
973789Sahrens 	else
9745094Slling 		error = spa_import(zc->zc_name, config, props);
975789Sahrens 
976*10921STim.Haley@Sun.COM 	if (zc->zc_nvlist_dst != 0)
977*10921STim.Haley@Sun.COM 		(void) put_nvlist(zc, config);
978*10921STim.Haley@Sun.COM 
979789Sahrens 	nvlist_free(config);
980789Sahrens 
9815094Slling 	if (props)
9825094Slling 		nvlist_free(props);
9835094Slling 
984789Sahrens 	return (error);
985789Sahrens }
986789Sahrens 
987789Sahrens static int
988789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
989789Sahrens {
9904543Smarks 	int error;
9917214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
9928211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
9937214Slling 
9944543Smarks 	zfs_log_history(zc);
9958211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
99610588SEric.Taylor@Sun.COM 	if (error == 0)
99710588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
9984543Smarks 	return (error);
999789Sahrens }
1000789Sahrens 
1001789Sahrens static int
1002789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1003789Sahrens {
1004789Sahrens 	nvlist_t *configs;
1005789Sahrens 	int error;
1006789Sahrens 
1007789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1008789Sahrens 		return (EEXIST);
1009789Sahrens 
10102676Seschrock 	error = put_nvlist(zc, configs);
1011789Sahrens 
1012789Sahrens 	nvlist_free(configs);
1013789Sahrens 
1014789Sahrens 	return (error);
1015789Sahrens }
1016789Sahrens 
1017789Sahrens static int
1018789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1019789Sahrens {
1020789Sahrens 	nvlist_t *config;
1021789Sahrens 	int error;
10221544Seschrock 	int ret = 0;
1023789Sahrens 
10242676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10252676Seschrock 	    sizeof (zc->zc_value));
1026789Sahrens 
1027789Sahrens 	if (config != NULL) {
10282676Seschrock 		ret = put_nvlist(zc, config);
1029789Sahrens 		nvlist_free(config);
10301544Seschrock 
10311544Seschrock 		/*
10321544Seschrock 		 * The config may be present even if 'error' is non-zero.
10331544Seschrock 		 * In this case we return success, and preserve the real errno
10341544Seschrock 		 * in 'zc_cookie'.
10351544Seschrock 		 */
10361544Seschrock 		zc->zc_cookie = error;
1037789Sahrens 	} else {
10381544Seschrock 		ret = error;
1039789Sahrens 	}
1040789Sahrens 
10411544Seschrock 	return (ret);
1042789Sahrens }
1043789Sahrens 
1044789Sahrens /*
1045789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1046789Sahrens  * user land knows which devices are available and overall pool health.
1047789Sahrens  */
1048789Sahrens static int
1049789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1050789Sahrens {
1051789Sahrens 	nvlist_t *tryconfig, *config;
1052789Sahrens 	int error;
1053789Sahrens 
10545094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10559643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1056789Sahrens 		return (error);
1057789Sahrens 
1058789Sahrens 	config = spa_tryimport(tryconfig);
1059789Sahrens 
1060789Sahrens 	nvlist_free(tryconfig);
1061789Sahrens 
1062789Sahrens 	if (config == NULL)
1063789Sahrens 		return (EINVAL);
1064789Sahrens 
10652676Seschrock 	error = put_nvlist(zc, config);
1066789Sahrens 	nvlist_free(config);
1067789Sahrens 
1068789Sahrens 	return (error);
1069789Sahrens }
1070789Sahrens 
1071789Sahrens static int
1072789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1073789Sahrens {
1074789Sahrens 	spa_t *spa;
1075789Sahrens 	int error;
1076789Sahrens 
10772926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10782926Sek110237 		return (error);
10792926Sek110237 
10807046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
10812926Sek110237 
10822926Sek110237 	spa_close(spa, FTAG);
10832926Sek110237 
1084789Sahrens 	return (error);
1085789Sahrens }
1086789Sahrens 
1087789Sahrens static int
1088789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1089789Sahrens {
1090789Sahrens 	spa_t *spa;
1091789Sahrens 	int error;
1092789Sahrens 
1093789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1094789Sahrens 	if (error == 0) {
1095789Sahrens 		spa_freeze(spa);
1096789Sahrens 		spa_close(spa, FTAG);
1097789Sahrens 	}
1098789Sahrens 	return (error);
1099789Sahrens }
1100789Sahrens 
1101789Sahrens static int
11021760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11031760Seschrock {
11041760Seschrock 	spa_t *spa;
11051760Seschrock 	int error;
11061760Seschrock 
11072926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11082926Sek110237 		return (error);
11092926Sek110237 
11105118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11115118Slling 		spa_close(spa, FTAG);
11125118Slling 		return (EINVAL);
11135118Slling 	}
11145118Slling 
11155094Slling 	spa_upgrade(spa, zc->zc_cookie);
11162926Sek110237 	spa_close(spa, FTAG);
11172926Sek110237 
11182926Sek110237 	return (error);
11192926Sek110237 }
11202926Sek110237 
11212926Sek110237 static int
11222926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11232926Sek110237 {
11242926Sek110237 	spa_t *spa;
11252926Sek110237 	char *hist_buf;
11262926Sek110237 	uint64_t size;
11272926Sek110237 	int error;
11282926Sek110237 
11292926Sek110237 	if ((size = zc->zc_history_len) == 0)
11302926Sek110237 		return (EINVAL);
11312926Sek110237 
11322926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11332926Sek110237 		return (error);
11342926Sek110237 
11354577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11363863Sek110237 		spa_close(spa, FTAG);
11373863Sek110237 		return (ENOTSUP);
11383863Sek110237 	}
11393863Sek110237 
11402926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11412926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11422926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11439643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
11449643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
11459643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
11462926Sek110237 	}
11472926Sek110237 
11482926Sek110237 	spa_close(spa, FTAG);
11492926Sek110237 	kmem_free(hist_buf, size);
11502926Sek110237 	return (error);
11512926Sek110237 }
11522926Sek110237 
11532926Sek110237 static int
11543444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11553444Sek110237 {
11563444Sek110237 	int error;
11573444Sek110237 
11583912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11593444Sek110237 		return (error);
11603444Sek110237 
11613444Sek110237 	return (0);
11623444Sek110237 }
11633444Sek110237 
116410298SMatthew.Ahrens@Sun.COM /*
116510298SMatthew.Ahrens@Sun.COM  * inputs:
116610298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
116710298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
116810298SMatthew.Ahrens@Sun.COM  *
116910298SMatthew.Ahrens@Sun.COM  * outputs:
117010298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
117110298SMatthew.Ahrens@Sun.COM  */
11723444Sek110237 static int
11733444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11743444Sek110237 {
117510298SMatthew.Ahrens@Sun.COM 	objset_t *os;
11763444Sek110237 	int error;
11773444Sek110237 
117810298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
117910298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
11803444Sek110237 		return (error);
118110298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
118210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
118310298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
118410298SMatthew.Ahrens@Sun.COM 	}
118510298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
11863444Sek110237 	    sizeof (zc->zc_value));
118710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
11883444Sek110237 
11893444Sek110237 	return (error);
11903444Sek110237 }
11913444Sek110237 
11923444Sek110237 static int
1193789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1194789Sahrens {
1195789Sahrens 	spa_t *spa;
1196789Sahrens 	int error;
11976423Sgw25295 	nvlist_t *config, **l2cache, **spares;
11986423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1199789Sahrens 
1200789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1201789Sahrens 	if (error != 0)
1202789Sahrens 		return (error);
1203789Sahrens 
12045450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12059643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
12065450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12075450Sbrendan 	    &l2cache, &nl2cache);
12085450Sbrendan 
12096423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12106423Sgw25295 	    &spares, &nspares);
12116423Sgw25295 
12123912Slling 	/*
12133912Slling 	 * A root pool with concatenated devices is not supported.
12146423Sgw25295 	 * Thus, can not add a device to a root pool.
12156423Sgw25295 	 *
12166423Sgw25295 	 * Intent log device can not be added to a rootpool because
12176423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12186423Sgw25295 	 * can not be accessed during the mountroot time.
12196423Sgw25295 	 *
12206423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12213912Slling 	 */
12226423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
12233912Slling 		spa_close(spa, FTAG);
12243912Slling 		return (EDOM);
12253912Slling 	}
12263912Slling 
12275450Sbrendan 	if (error == 0) {
1228789Sahrens 		error = spa_vdev_add(spa, config);
1229789Sahrens 		nvlist_free(config);
1230789Sahrens 	}
1231789Sahrens 	spa_close(spa, FTAG);
1232789Sahrens 	return (error);
1233789Sahrens }
1234789Sahrens 
1235789Sahrens static int
1236789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1237789Sahrens {
12382082Seschrock 	spa_t *spa;
12392082Seschrock 	int error;
12402082Seschrock 
12412082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12422082Seschrock 	if (error != 0)
12432082Seschrock 		return (error);
12442082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12452082Seschrock 	spa_close(spa, FTAG);
12462082Seschrock 	return (error);
1247789Sahrens }
1248789Sahrens 
1249789Sahrens static int
12504451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1251789Sahrens {
1252789Sahrens 	spa_t *spa;
1253789Sahrens 	int error;
12544451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1255789Sahrens 
12562926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1257789Sahrens 		return (error);
12584451Seschrock 	switch (zc->zc_cookie) {
12594451Seschrock 	case VDEV_STATE_ONLINE:
12604451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12614451Seschrock 		break;
12624451Seschrock 
12634451Seschrock 	case VDEV_STATE_OFFLINE:
12644451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12654451Seschrock 		break;
1266789Sahrens 
12674451Seschrock 	case VDEV_STATE_FAULTED:
126810817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
126910817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
127010817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
127110817SEric.Schrock@Sun.COM 
127210817SEric.Schrock@Sun.COM 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
12734451Seschrock 		break;
1274789Sahrens 
12754451Seschrock 	case VDEV_STATE_DEGRADED:
127610817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
127710817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
127810817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
127910817SEric.Schrock@Sun.COM 
128010817SEric.Schrock@Sun.COM 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
12814451Seschrock 		break;
12824451Seschrock 
12834451Seschrock 	default:
12844451Seschrock 		error = EINVAL;
12854451Seschrock 	}
12864451Seschrock 	zc->zc_cookie = newstate;
1287789Sahrens 	spa_close(spa, FTAG);
1288789Sahrens 	return (error);
1289789Sahrens }
1290789Sahrens 
1291789Sahrens static int
1292789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1293789Sahrens {
1294789Sahrens 	spa_t *spa;
1295789Sahrens 	int replacing = zc->zc_cookie;
1296789Sahrens 	nvlist_t *config;
1297789Sahrens 	int error;
1298789Sahrens 
12992926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1300789Sahrens 		return (error);
1301789Sahrens 
13025094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13039643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
13041544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1305789Sahrens 		nvlist_free(config);
1306789Sahrens 	}
1307789Sahrens 
1308789Sahrens 	spa_close(spa, FTAG);
1309789Sahrens 	return (error);
1310789Sahrens }
1311789Sahrens 
1312789Sahrens static int
1313789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1314789Sahrens {
1315789Sahrens 	spa_t *spa;
1316789Sahrens 	int error;
1317789Sahrens 
13182926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1319789Sahrens 		return (error);
1320789Sahrens 
13218241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1322789Sahrens 
1323789Sahrens 	spa_close(spa, FTAG);
1324789Sahrens 	return (error);
1325789Sahrens }
1326789Sahrens 
1327789Sahrens static int
13281354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13291354Seschrock {
13301354Seschrock 	spa_t *spa;
13312676Seschrock 	char *path = zc->zc_value;
13321544Seschrock 	uint64_t guid = zc->zc_guid;
13331354Seschrock 	int error;
13341354Seschrock 
13351354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13361354Seschrock 	if (error != 0)
13371354Seschrock 		return (error);
13381354Seschrock 
13391354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13401354Seschrock 	spa_close(spa, FTAG);
13411354Seschrock 	return (error);
13421354Seschrock }
13431354Seschrock 
13449425SEric.Schrock@Sun.COM static int
13459425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13469425SEric.Schrock@Sun.COM {
13479425SEric.Schrock@Sun.COM 	spa_t *spa;
13489425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
13499425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
13509425SEric.Schrock@Sun.COM 	int error;
13519425SEric.Schrock@Sun.COM 
13529425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
13539425SEric.Schrock@Sun.COM 	if (error != 0)
13549425SEric.Schrock@Sun.COM 		return (error);
13559425SEric.Schrock@Sun.COM 
13569425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
13579425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
13589425SEric.Schrock@Sun.COM 	return (error);
13599425SEric.Schrock@Sun.COM }
13609425SEric.Schrock@Sun.COM 
13615367Sahrens /*
13625367Sahrens  * inputs:
13635367Sahrens  * zc_name		name of filesystem
13645367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13655367Sahrens  *
13665367Sahrens  * outputs:
13675367Sahrens  * zc_objset_stats	stats
13685367Sahrens  * zc_nvlist_dst	property nvlist
13695367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13705367Sahrens  */
13711354Seschrock static int
1372789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1373789Sahrens {
1374789Sahrens 	objset_t *os = NULL;
1375789Sahrens 	int error;
13761356Seschrock 	nvlist_t *nv;
1377789Sahrens 
137810298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1379789Sahrens 		return (error);
1380789Sahrens 
13812885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1382789Sahrens 
13832856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13846689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13852885Sahrens 		dmu_objset_stats(os, nv);
13863087Sahrens 		/*
13875147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13883087Sahrens 		 * which we aren't supposed to do with a
13896689Smaybee 		 * DS_MODE_USER hold, because it could be
13903087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
139110298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
13923087Sahrens 		 */
13934577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13944577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13954577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13964577Sahrens 		}
13972676Seschrock 		error = put_nvlist(zc, nv);
13981356Seschrock 		nvlist_free(nv);
13991356Seschrock 	}
1400789Sahrens 
140110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1402789Sahrens 	return (error);
1403789Sahrens }
1404789Sahrens 
14055498Stimh static int
14065498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
14075498Stimh {
14085498Stimh 	uint64_t value;
14095498Stimh 	int error;
14105498Stimh 
14115498Stimh 	/*
14125498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14135498Stimh 	 * the default value (if there is one).
14145498Stimh 	 */
14155498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14165498Stimh 		return (error);
14175498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14185498Stimh 	return (0);
14195498Stimh }
14205498Stimh 
14215498Stimh /*
14225498Stimh  * inputs:
14235498Stimh  * zc_name		name of filesystem
14245498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14255498Stimh  *
14265498Stimh  * outputs:
14275498Stimh  * zc_nvlist_dst	zpl property nvlist
14285498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14295498Stimh  */
14305498Stimh static int
14315498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14325498Stimh {
14335498Stimh 	objset_t *os;
14345498Stimh 	int err;
14355498Stimh 
143610298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
143710298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
14385498Stimh 		return (err);
14395498Stimh 
14405498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14415498Stimh 
14425498Stimh 	/*
14435498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14446689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14456689Smaybee 	 * hold, because it could be inconsistent.
14465498Stimh 	 */
14475498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14485498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14495498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14505498Stimh 		nvlist_t *nv;
14515498Stimh 
14525498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14535498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14545498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14555498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14565498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14575498Stimh 			err = put_nvlist(zc, nv);
14585498Stimh 		nvlist_free(nv);
14595498Stimh 	} else {
14605498Stimh 		err = ENOENT;
14615498Stimh 	}
146210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
14635498Stimh 	return (err);
14645498Stimh }
14655498Stimh 
14669396SMatthew.Ahrens@Sun.COM static boolean_t
14679396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14689396SMatthew.Ahrens@Sun.COM {
14699396SMatthew.Ahrens@Sun.COM 	/*
14709396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14719396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14729396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14739396SMatthew.Ahrens@Sun.COM 	 */
14749396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14759396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14769396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14779396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14789396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14799396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14809396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14819396SMatthew.Ahrens@Sun.COM }
14829396SMatthew.Ahrens@Sun.COM 
14835367Sahrens /*
14845367Sahrens  * inputs:
14855367Sahrens  * zc_name		name of filesystem
14865367Sahrens  * zc_cookie		zap cursor
14875367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14885367Sahrens  *
14895367Sahrens  * outputs:
14905367Sahrens  * zc_name		name of next filesystem
14919396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14925367Sahrens  * zc_objset_stats	stats
14935367Sahrens  * zc_nvlist_dst	property nvlist
14945367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14955367Sahrens  */
1496789Sahrens static int
1497789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1498789Sahrens {
1499885Sahrens 	objset_t *os;
1500789Sahrens 	int error;
1501789Sahrens 	char *p;
1502789Sahrens 
150310298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1504885Sahrens 		if (error == ENOENT)
1505885Sahrens 			error = ESRCH;
1506885Sahrens 		return (error);
1507789Sahrens 	}
1508789Sahrens 
1509789Sahrens 	p = strrchr(zc->zc_name, '/');
1510789Sahrens 	if (p == NULL || p[1] != '\0')
1511789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1512789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1513789Sahrens 
15148697SRichard.Morris@Sun.COM 	/*
15158697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15168697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15178697SRichard.Morris@Sun.COM 	 */
15188415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15198415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15208415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15218415SRichard.Morris@Sun.COM 
15228415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15238697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15248415SRichard.Morris@Sun.COM 	}
15258415SRichard.Morris@Sun.COM 
1526789Sahrens 	do {
1527885Sahrens 		error = dmu_dir_list_next(os,
1528885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1529885Sahrens 		    NULL, &zc->zc_cookie);
1530789Sahrens 		if (error == ENOENT)
1531789Sahrens 			error = ESRCH;
153210588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
153310588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
153410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1535789Sahrens 
153610588SEric.Taylor@Sun.COM 	/*
153710588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
153810588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
153910588SEric.Taylor@Sun.COM 	 */
154010588SEric.Taylor@Sun.COM 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1541885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1542789Sahrens 	return (error);
1543789Sahrens }
1544789Sahrens 
15455367Sahrens /*
15465367Sahrens  * inputs:
15475367Sahrens  * zc_name		name of filesystem
15485367Sahrens  * zc_cookie		zap cursor
15495367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15505367Sahrens  *
15515367Sahrens  * outputs:
15525367Sahrens  * zc_name		name of next snapshot
15535367Sahrens  * zc_objset_stats	stats
15545367Sahrens  * zc_nvlist_dst	property nvlist
15555367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15565367Sahrens  */
1557789Sahrens static int
1558789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1559789Sahrens {
1560885Sahrens 	objset_t *os;
1561789Sahrens 	int error;
1562789Sahrens 
156310474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
156410474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
156510474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
156610474SRichard.Morris@Sun.COM 
156710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
15686689Smaybee 	if (error)
15696689Smaybee 		return (error == ENOENT ? ESRCH : error);
1570789Sahrens 
15711003Slling 	/*
15721003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15731003Slling 	 * so exit immediately.
15741003Slling 	 */
15751003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
157610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
15771003Slling 		return (ESRCH);
1578789Sahrens 	}
1579789Sahrens 
1580885Sahrens 	error = dmu_snapshot_list_next(os,
1581885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15825663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
158310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1584885Sahrens 	if (error == 0)
1585885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15866689Smaybee 	else if (error == ENOENT)
15876689Smaybee 		error = ESRCH;
1588789Sahrens 
15895367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15906689Smaybee 	if (error)
15915367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1592789Sahrens 	return (error);
1593789Sahrens }
1594789Sahrens 
15956423Sgw25295 int
15964787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1597789Sahrens {
15982676Seschrock 	nvpair_t *elem;
15998724SRichard.Morris@Sun.COM 	int error = 0;
16002676Seschrock 	uint64_t intval;
16012676Seschrock 	char *strval;
16028697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
16039396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
16042676Seschrock 
16054543Smarks 	/*
16064543Smarks 	 * First validate permission to set all of the properties
16074543Smarks 	 */
16082676Seschrock 	elem = NULL;
16092676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16104670Sahrens 		const char *propname = nvpair_name(elem);
16114670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16122676Seschrock 
16135094Slling 		if (prop == ZPROP_INVAL) {
16142676Seschrock 			/*
16152676Seschrock 			 * If this is a user-defined property, it must be a
16162676Seschrock 			 * string, and there is no further validation to do.
16172676Seschrock 			 */
16189396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16199396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16209396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16219396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16229396SMatthew.Ahrens@Sun.COM 					return (error);
16239396SMatthew.Ahrens@Sun.COM 				continue;
16249396SMatthew.Ahrens@Sun.COM 			}
16259396SMatthew.Ahrens@Sun.COM 
16269396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16279396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16289396SMatthew.Ahrens@Sun.COM 				const char *perm;
16299396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16309396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16319396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16329396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16339396SMatthew.Ahrens@Sun.COM 				else
16349396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16359396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16369396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16379396SMatthew.Ahrens@Sun.COM 					return (error);
16389396SMatthew.Ahrens@Sun.COM 				continue;
16399396SMatthew.Ahrens@Sun.COM 			}
16409396SMatthew.Ahrens@Sun.COM 
16419396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16422676Seschrock 		}
16432676Seschrock 
16449396SMatthew.Ahrens@Sun.COM 		if (issnap)
16459396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16469396SMatthew.Ahrens@Sun.COM 
16474787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16484670Sahrens 			return (error);
16492676Seschrock 
16504670Sahrens 		/*
16514670Sahrens 		 * Check that this value is valid for this pool version
16524670Sahrens 		 */
16534670Sahrens 		switch (prop) {
16543886Sahl 		case ZFS_PROP_COMPRESSION:
16553886Sahl 			/*
16563886Sahl 			 * If the user specified gzip compression, make sure
16573886Sahl 			 * the SPA supports it. We ignore any errors here since
16583886Sahl 			 * we'll catch them later.
16593886Sahl 			 */
16603886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16617042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16627042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16637042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16647184Stimh 				    zfs_earlier_version(name,
16655331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16665331Samw 					return (ENOTSUP);
16677042Sgw25295 
16687042Sgw25295 				/*
16697042Sgw25295 				 * If this is a bootable dataset then
16707042Sgw25295 				 * verify that the compression algorithm
16717042Sgw25295 				 * is supported for booting. We must return
16727042Sgw25295 				 * something other than ENOTSUP since it
16737042Sgw25295 				 * implies a downrev pool version.
16747042Sgw25295 				 */
16757042Sgw25295 				if (zfs_is_bootfs(name) &&
16767042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16777042Sgw25295 					return (ERANGE);
16783886Sahl 			}
16793886Sahl 			break;
16804603Sahrens 
16814603Sahrens 		case ZFS_PROP_COPIES:
16829396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16835331Samw 				return (ENOTSUP);
16844603Sahrens 			break;
16855977Smarks 
16865977Smarks 		case ZFS_PROP_SHARESMB:
16876689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16885977Smarks 				return (ENOTSUP);
16895977Smarks 			break;
16908053SMark.Shellenbaum@Sun.COM 
16918053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16928053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16938053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16948053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16958053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16968053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
16978053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
16984603Sahrens 		}
16994543Smarks 	}
17004543Smarks 
17018697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17024543Smarks 	elem = NULL;
17034543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
17044670Sahrens 		const char *propname = nvpair_name(elem);
17054670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
17064543Smarks 
17075094Slling 		if (prop == ZPROP_INVAL) {
17089396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
17099396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
17109396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
17119396SMatthew.Ahrens@Sun.COM 				const char *domain;
17129396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
17139396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17149396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17159396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17169396SMatthew.Ahrens@Sun.COM 
17179396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17189396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17199396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17209396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17219396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17229396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
17239396SMatthew.Ahrens@Sun.COM 				domain = propname +
17249396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
17259396SMatthew.Ahrens@Sun.COM 
172610298SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, FTAG, &zfsvfs);
17279396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17289396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17299396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17309396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17319396SMatthew.Ahrens@Sun.COM 				}
17329396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17339396SMatthew.Ahrens@Sun.COM 					continue;
17349396SMatthew.Ahrens@Sun.COM 				else
17359396SMatthew.Ahrens@Sun.COM 					goto out;
17369396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17379396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17389396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17399396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17409396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17419396SMatthew.Ahrens@Sun.COM 					continue;
17429396SMatthew.Ahrens@Sun.COM 				else
17439396SMatthew.Ahrens@Sun.COM 					goto out;
17449396SMatthew.Ahrens@Sun.COM 			}
17454543Smarks 		}
17462676Seschrock 
17472676Seschrock 		switch (prop) {
17482676Seschrock 		case ZFS_PROP_QUOTA:
17492676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17504577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17518697SRichard.Morris@Sun.COM 				goto out;
17522676Seschrock 			break;
17532676Seschrock 
17545378Sck153898 		case ZFS_PROP_REFQUOTA:
17555378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17565378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17578697SRichard.Morris@Sun.COM 				goto out;
17585378Sck153898 			break;
17595378Sck153898 
17602676Seschrock 		case ZFS_PROP_RESERVATION:
17612676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17622676Seschrock 			    (error = dsl_dir_set_reservation(name,
17632676Seschrock 			    intval)) != 0)
17648697SRichard.Morris@Sun.COM 				goto out;
17652676Seschrock 			break;
1766789Sahrens 
17675378Sck153898 		case ZFS_PROP_REFRESERVATION:
17685378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17695378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17705378Sck153898 			    intval)) != 0)
17718697SRichard.Morris@Sun.COM 				goto out;
17725378Sck153898 			break;
17735378Sck153898 
17742676Seschrock 		case ZFS_PROP_VOLSIZE:
17752676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17764787Sahrens 			    (error = zvol_set_volsize(name,
17774787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17788697SRichard.Morris@Sun.COM 				goto out;
17792676Seschrock 			break;
17802676Seschrock 
17814577Sahrens 		case ZFS_PROP_VERSION:
17829396SMatthew.Ahrens@Sun.COM 		{
17839396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17849396SMatthew.Ahrens@Sun.COM 
17859396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17869396SMatthew.Ahrens@Sun.COM 				goto out;
178710298SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0)
17889396SMatthew.Ahrens@Sun.COM 				goto out;
17899396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
17909396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
17919396SMatthew.Ahrens@Sun.COM 
17929396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
17939396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
17949396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
17959396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
17969396SMatthew.Ahrens@Sun.COM 			}
17979396SMatthew.Ahrens@Sun.COM 			if (error)
17988697SRichard.Morris@Sun.COM 				goto out;
17992676Seschrock 			break;
18009396SMatthew.Ahrens@Sun.COM 		}
18012676Seschrock 
18022676Seschrock 		default:
18032676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
18042676Seschrock 				if (zfs_prop_get_type(prop) !=
18058697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18068697SRichard.Morris@Sun.COM 					error = EINVAL;
18078697SRichard.Morris@Sun.COM 					goto out;
18088697SRichard.Morris@Sun.COM 				}
18092676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18102885Sahrens 				const char *unused;
18112885Sahrens 
18122717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18132676Seschrock 
18142676Seschrock 				switch (zfs_prop_get_type(prop)) {
18154787Sahrens 				case PROP_TYPE_NUMBER:
18162676Seschrock 					break;
18174787Sahrens 				case PROP_TYPE_STRING:
18188697SRichard.Morris@Sun.COM 					error = EINVAL;
18198697SRichard.Morris@Sun.COM 					goto out;
18204787Sahrens 				case PROP_TYPE_INDEX:
18212717Seschrock 					if (zfs_prop_index_to_string(prop,
18228697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18238697SRichard.Morris@Sun.COM 						error = EINVAL;
18248697SRichard.Morris@Sun.COM 						goto out;
18258697SRichard.Morris@Sun.COM 					}
18262676Seschrock 					break;
18272676Seschrock 				default:
18284577Sahrens 					cmn_err(CE_PANIC,
18294577Sahrens 					    "unknown property type");
18302676Seschrock 					break;
18312676Seschrock 				}
18322676Seschrock 			} else {
18338697SRichard.Morris@Sun.COM 				error = EINVAL;
18348697SRichard.Morris@Sun.COM 				goto out;
18352676Seschrock 			}
18368697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18378697SRichard.Morris@Sun.COM 				goto out;
18382676Seschrock 		}
18392676Seschrock 	}
18402676Seschrock 
18418697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18428697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18438697SRichard.Morris@Sun.COM 	}
18448697SRichard.Morris@Sun.COM out:
18458697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18468697SRichard.Morris@Sun.COM 	return (error);
1847789Sahrens }
1848789Sahrens 
18495367Sahrens /*
18509355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18519355SMatthew.Ahrens@Sun.COM  */
18529355SMatthew.Ahrens@Sun.COM static int
18539355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18549355SMatthew.Ahrens@Sun.COM {
18559355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18569355SMatthew.Ahrens@Sun.COM 	int error = 0;
18579355SMatthew.Ahrens@Sun.COM 
18589355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18599355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18609355SMatthew.Ahrens@Sun.COM 		char *valstr;
18619355SMatthew.Ahrens@Sun.COM 
18629355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18639355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18649355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18659355SMatthew.Ahrens@Sun.COM 
18669355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18679355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18689355SMatthew.Ahrens@Sun.COM 			return (error);
18699355SMatthew.Ahrens@Sun.COM 
18709355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18719355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18729355SMatthew.Ahrens@Sun.COM 
18739355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18749355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18759355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18769355SMatthew.Ahrens@Sun.COM 	}
18779355SMatthew.Ahrens@Sun.COM 	return (0);
18789355SMatthew.Ahrens@Sun.COM }
18799355SMatthew.Ahrens@Sun.COM 
18809355SMatthew.Ahrens@Sun.COM /*
18815367Sahrens  * inputs:
18825367Sahrens  * zc_name		name of filesystem
18838697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18845367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18857265Sahrens  * zc_cookie		clear existing local props?
18865367Sahrens  *
18875367Sahrens  * outputs:		none
18885367Sahrens  */
1889789Sahrens static int
18902676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1891789Sahrens {
18922676Seschrock 	nvlist_t *nvl;
18932676Seschrock 	int error;
1894789Sahrens 
18955094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18969643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
18972676Seschrock 		return (error);
18982676Seschrock 
18997265Sahrens 	if (zc->zc_cookie) {
19007265Sahrens 		nvlist_t *origprops;
19017265Sahrens 		objset_t *os;
19027265Sahrens 
190310298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
19047265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19058536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19067265Sahrens 				nvlist_free(origprops);
19077265Sahrens 			}
190810298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
19097265Sahrens 		}
19107265Sahrens 
19117265Sahrens 	}
19127265Sahrens 
19134787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19144543Smarks 
19152676Seschrock 	nvlist_free(nvl);
19162676Seschrock 	return (error);
1917789Sahrens }
1918789Sahrens 
19195367Sahrens /*
19205367Sahrens  * inputs:
19215367Sahrens  * zc_name		name of filesystem
19225367Sahrens  * zc_value		name of property to inherit
19235367Sahrens  *
19245367Sahrens  * outputs:		none
19255367Sahrens  */
1926789Sahrens static int
19274849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19284849Sahrens {
19294849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19304849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19314849Sahrens }
19324849Sahrens 
19334849Sahrens static int
19344098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19353912Slling {
19365094Slling 	nvlist_t *props;
19373912Slling 	spa_t *spa;
19385094Slling 	int error;
19398525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19403912Slling 
19415094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19429643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)))
19433912Slling 		return (error);
19443912Slling 
19458525SEric.Schrock@Sun.COM 	/*
19468525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19478525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19488525SEric.Schrock@Sun.COM 	 */
19498525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19508525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19518525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19528525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19538525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19548525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19558525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19568525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19578525SEric.Schrock@Sun.COM 		}
19588525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
195910672SEric.Schrock@Sun.COM 		if (spa != NULL) {
196010672SEric.Schrock@Sun.COM 			nvlist_free(props);
19618525SEric.Schrock@Sun.COM 			return (0);
196210672SEric.Schrock@Sun.COM 		}
19638525SEric.Schrock@Sun.COM 	}
19648525SEric.Schrock@Sun.COM 
19653912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19665094Slling 		nvlist_free(props);
19673912Slling 		return (error);
19683912Slling 	}
19693912Slling 
19705094Slling 	error = spa_prop_set(spa, props);
19713912Slling 
19725094Slling 	nvlist_free(props);
19733912Slling 	spa_close(spa, FTAG);
19743912Slling 
19753912Slling 	return (error);
19763912Slling }
19773912Slling 
19783912Slling static int
19794098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19803912Slling {
19813912Slling 	spa_t *spa;
19823912Slling 	int error;
19833912Slling 	nvlist_t *nvp = NULL;
19843912Slling 
19858525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19868525SEric.Schrock@Sun.COM 		/*
19878525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19888525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19898525SEric.Schrock@Sun.COM 		 * anyway.
19908525SEric.Schrock@Sun.COM 		 */
19918525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19928525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
19938525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
19948525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19958525SEric.Schrock@Sun.COM 	} else {
19968525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
19978525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
19988525SEric.Schrock@Sun.COM 	}
19993912Slling 
20003912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
20013912Slling 		error = put_nvlist(zc, nvp);
20023912Slling 	else
20033912Slling 		error = EFAULT;
20043912Slling 
20058525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
20063912Slling 	return (error);
20073912Slling }
20083912Slling 
20093912Slling static int
20104543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20114543Smarks {
20124543Smarks 	nvlist_t *nvp;
20134543Smarks 	int error;
20144543Smarks 	uint32_t uid;
20154543Smarks 	uint32_t gid;
20164543Smarks 	uint32_t *groups;
20174543Smarks 	uint_t group_cnt;
20184543Smarks 	cred_t	*usercred;
20194543Smarks 
20205094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20219643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
20224543Smarks 		return (error);
20234543Smarks 	}
20244543Smarks 
20254543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20264543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20274543Smarks 		nvlist_free(nvp);
20284543Smarks 		return (EPERM);
20294543Smarks 	}
20304543Smarks 
20314543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20324543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20334543Smarks 		nvlist_free(nvp);
20344543Smarks 		return (EPERM);
20354543Smarks 	}
20364543Smarks 
20374543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20384543Smarks 	    &groups, &group_cnt)) != 0) {
20394543Smarks 		nvlist_free(nvp);
20404543Smarks 		return (EPERM);
20414543Smarks 	}
20424543Smarks 	usercred = cralloc();
20434543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20444543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20454543Smarks 		nvlist_free(nvp);
20464543Smarks 		crfree(usercred);
20474543Smarks 		return (EPERM);
20484543Smarks 	}
20494543Smarks 	nvlist_free(nvp);
20504543Smarks 	error = dsl_deleg_access(zc->zc_name,
20514787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20524543Smarks 	crfree(usercred);
20534543Smarks 	return (error);
20544543Smarks }
20554543Smarks 
20565367Sahrens /*
20575367Sahrens  * inputs:
20585367Sahrens  * zc_name		name of filesystem
20595367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20605367Sahrens  * zc_perm_action	allow/unallow flag
20615367Sahrens  *
20625367Sahrens  * outputs:		none
20635367Sahrens  */
20644543Smarks static int
20654543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20664543Smarks {
20674543Smarks 	int error;
20684543Smarks 	nvlist_t *fsaclnv = NULL;
20694543Smarks 
20705094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20719643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
20724543Smarks 		return (error);
20734543Smarks 
20744543Smarks 	/*
20754543Smarks 	 * Verify nvlist is constructed correctly
20764543Smarks 	 */
20774543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20784543Smarks 		nvlist_free(fsaclnv);
20794543Smarks 		return (EINVAL);
20804543Smarks 	}
20814543Smarks 
20824543Smarks 	/*
20834543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20844543Smarks 	 * that user is allowed to hand out each permission in
20854543Smarks 	 * the nvlist(s)
20864543Smarks 	 */
20874543Smarks 
20884787Sahrens 	error = secpolicy_zfs(CRED());
20894543Smarks 	if (error) {
20904787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
20914787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
20924787Sahrens 			    fsaclnv, CRED());
20934787Sahrens 		} else {
20944787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
20954787Sahrens 			    fsaclnv, CRED());
20964787Sahrens 		}
20974543Smarks 	}
20984543Smarks 
20994543Smarks 	if (error == 0)
21004543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
21014543Smarks 
21024543Smarks 	nvlist_free(fsaclnv);
21034543Smarks 	return (error);
21044543Smarks }
21054543Smarks 
21065367Sahrens /*
21075367Sahrens  * inputs:
21085367Sahrens  * zc_name		name of filesystem
21095367Sahrens  *
21105367Sahrens  * outputs:
21115367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21125367Sahrens  */
21134543Smarks static int
21144543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21154543Smarks {
21164543Smarks 	nvlist_t *nvp;
21174543Smarks 	int error;
21184543Smarks 
21194543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21204543Smarks 		error = put_nvlist(zc, nvp);
21214543Smarks 		nvlist_free(nvp);
21224543Smarks 	}
21234543Smarks 
21244543Smarks 	return (error);
21254543Smarks }
21264543Smarks 
21275367Sahrens /*
2128789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2129789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2130789Sahrens  * is responsible for releasing the returned vfs pointer.
2131789Sahrens  */
2132789Sahrens static vfs_t *
2133789Sahrens zfs_get_vfs(const char *resource)
2134789Sahrens {
2135789Sahrens 	struct vfs *vfsp;
2136789Sahrens 	struct vfs *vfs_found = NULL;
2137789Sahrens 
2138789Sahrens 	vfs_list_read_lock();
2139789Sahrens 	vfsp = rootvfs;
2140789Sahrens 	do {
2141789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2142789Sahrens 			VFS_HOLD(vfsp);
2143789Sahrens 			vfs_found = vfsp;
2144789Sahrens 			break;
2145789Sahrens 		}
2146789Sahrens 		vfsp = vfsp->vfs_next;
2147789Sahrens 	} while (vfsp != rootvfs);
2148789Sahrens 	vfs_list_unlock();
2149789Sahrens 	return (vfs_found);
2150789Sahrens }
2151789Sahrens 
21524543Smarks /* ARGSUSED */
2153789Sahrens static void
21544543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2155789Sahrens {
21565331Samw 	zfs_creat_t *zct = arg;
21575498Stimh 
21585498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21595331Samw }
21605331Samw 
21615498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21625498Stimh 
21635331Samw /*
21645498Stimh  * inputs:
21657184Stimh  * createprops		list of properties requested by creator
21667184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21677184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21687184Stimh  * os			parent objset pointer (NULL if root fs)
21695331Samw  *
21705498Stimh  * outputs:
21715498Stimh  * zplprops	values for the zplprops we attach to the master node object
21727184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21735331Samw  *
21745498Stimh  * Determine the settings for utf8only, normalization and
21755498Stimh  * casesensitivity.  Specific values may have been requested by the
21765498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21775498Stimh  * the file system is of too early a vintage, a creator can not
21785498Stimh  * request settings for these properties, even if the requested
21795498Stimh  * setting is the default value.  We don't actually want to create dsl
21805498Stimh  * properties for these, so remove them from the source nvlist after
21815498Stimh  * processing.
21825331Samw  */
21835331Samw static int
21849396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
21857184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
21867184Stimh     boolean_t *is_ci)
21875331Samw {
21885498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
21895498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
21905498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
21915498Stimh 
21925498Stimh 	ASSERT(zplprops != NULL);
21935498Stimh 
21945375Stimh 	/*
21955498Stimh 	 * Pull out creator prop choices, if any.
21965375Stimh 	 */
21975498Stimh 	if (createprops) {
21985498Stimh 		(void) nvlist_lookup_uint64(createprops,
21997184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22007184Stimh 		(void) nvlist_lookup_uint64(createprops,
22015498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22025498Stimh 		(void) nvlist_remove_all(createprops,
22035498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22045498Stimh 		(void) nvlist_lookup_uint64(createprops,
22055498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22065498Stimh 		(void) nvlist_remove_all(createprops,
22075498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22085498Stimh 		(void) nvlist_lookup_uint64(createprops,
22095498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22105498Stimh 		(void) nvlist_remove_all(createprops,
22115498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22125331Samw 	}
22135331Samw 
22145375Stimh 	/*
22157184Stimh 	 * If the zpl version requested is whacky or the file system
22167184Stimh 	 * or pool is version is too "young" to support normalization
22177184Stimh 	 * and the creator tried to set a value for one of the props,
22187184Stimh 	 * error out.
22195498Stimh 	 */
22207184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22217184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22227184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22235498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22247184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22255498Stimh 		return (ENOTSUP);
22265498Stimh 
22275498Stimh 	/*
22285498Stimh 	 * Put the version in the zplprops
22295498Stimh 	 */
22305498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22315498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22325498Stimh 
22335498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22345498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22355498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22365498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22375498Stimh 
22385498Stimh 	/*
22395498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22405498Stimh 	 */
22415498Stimh 	if (norm)
22425498Stimh 		u8 = 1;
22435498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22445498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22455498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22465498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22475498Stimh 
22485498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22495498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22505498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22515498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22525498Stimh 
22536492Stimh 	if (is_ci)
22546492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22556492Stimh 
22567184Stimh 	return (0);
22577184Stimh }
22587184Stimh 
22597184Stimh static int
22607184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22617184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22627184Stimh {
22637184Stimh 	boolean_t fuids_ok = B_TRUE;
22647184Stimh 	uint64_t zplver = ZPL_VERSION;
22657184Stimh 	objset_t *os = NULL;
22667184Stimh 	char parentname[MAXNAMELEN];
22677184Stimh 	char *cp;
22687184Stimh 	int error;
22697184Stimh 
22707184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22717184Stimh 	cp = strrchr(parentname, '/');
22727184Stimh 	ASSERT(cp != NULL);
22737184Stimh 	cp[0] = '\0';
22747184Stimh 
22759396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
22769396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22777184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22787184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22797184Stimh 		fuids_ok = B_FALSE;
22807184Stimh 	}
22817184Stimh 
22827184Stimh 	/*
22837184Stimh 	 * Open parent object set so we can inherit zplprop values.
22847184Stimh 	 */
228510298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
22867184Stimh 		return (error);
22877184Stimh 
22887184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
22897184Stimh 	    zplprops, is_ci);
229010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
22917184Stimh 	return (error);
22927184Stimh }
22937184Stimh 
22947184Stimh static int
22957184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
22967184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22977184Stimh {
22987184Stimh 	boolean_t fuids_ok = B_TRUE;
22997184Stimh 	uint64_t zplver = ZPL_VERSION;
23007184Stimh 	int error;
23017184Stimh 
23027184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23037184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23047184Stimh 		fuids_ok = B_FALSE;
23057184Stimh 	}
23067184Stimh 
23077184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23087184Stimh 	    zplprops, is_ci);
23097184Stimh 	return (error);
2310789Sahrens }
2311789Sahrens 
23125367Sahrens /*
23135367Sahrens  * inputs:
23145367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23155367Sahrens  * zc_name		name of new objset
23165367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23175367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23185367Sahrens  *
23195498Stimh  * outputs: none
23205367Sahrens  */
2321789Sahrens static int
2322789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2323789Sahrens {
2324789Sahrens 	objset_t *clone;
2325789Sahrens 	int error = 0;
23265331Samw 	zfs_creat_t zct;
23274543Smarks 	nvlist_t *nvprops = NULL;
23284543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2329789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2330789Sahrens 
2331789Sahrens 	switch (type) {
2332789Sahrens 
2333789Sahrens 	case DMU_OST_ZFS:
2334789Sahrens 		cbfunc = zfs_create_cb;
2335789Sahrens 		break;
2336789Sahrens 
2337789Sahrens 	case DMU_OST_ZVOL:
2338789Sahrens 		cbfunc = zvol_create_cb;
2339789Sahrens 		break;
2340789Sahrens 
2341789Sahrens 	default:
23422199Sahrens 		cbfunc = NULL;
23436423Sgw25295 		break;
23442199Sahrens 	}
23455326Sek110237 	if (strchr(zc->zc_name, '@') ||
23465326Sek110237 	    strchr(zc->zc_name, '%'))
2347789Sahrens 		return (EINVAL);
2348789Sahrens 
23492676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23505094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23519643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
23522676Seschrock 		return (error);
23532676Seschrock 
23545498Stimh 	zct.zct_zplprops = NULL;
23555331Samw 	zct.zct_props = nvprops;
23565331Samw 
23572676Seschrock 	if (zc->zc_value[0] != '\0') {
2358789Sahrens 		/*
2359789Sahrens 		 * We're creating a clone of an existing snapshot.
2360789Sahrens 		 */
23612676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23622676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23634543Smarks 			nvlist_free(nvprops);
2364789Sahrens 			return (EINVAL);
23652676Seschrock 		}
2366789Sahrens 
236710298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
23682676Seschrock 		if (error) {
23694543Smarks 			nvlist_free(nvprops);
2370789Sahrens 			return (error);
23712676Seschrock 		}
23726492Stimh 
237310272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
237410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
23755331Samw 		if (error) {
23765331Samw 			nvlist_free(nvprops);
23775331Samw 			return (error);
23785331Samw 		}
2379789Sahrens 	} else {
23806492Stimh 		boolean_t is_insensitive = B_FALSE;
23816492Stimh 
23822676Seschrock 		if (cbfunc == NULL) {
23834543Smarks 			nvlist_free(nvprops);
23842199Sahrens 			return (EINVAL);
23852676Seschrock 		}
23862676Seschrock 
2387789Sahrens 		if (type == DMU_OST_ZVOL) {
23882676Seschrock 			uint64_t volsize, volblocksize;
23892676Seschrock 
23904543Smarks 			if (nvprops == NULL ||
23914543Smarks 			    nvlist_lookup_uint64(nvprops,
23922676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
23932676Seschrock 			    &volsize) != 0) {
23944543Smarks 				nvlist_free(nvprops);
23952676Seschrock 				return (EINVAL);
23962676Seschrock 			}
23972676Seschrock 
23984543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
23992676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24002676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24014543Smarks 				nvlist_free(nvprops);
24022676Seschrock 				return (EINVAL);
24032676Seschrock 			}
24041133Seschrock 
24052676Seschrock 			if (error != 0)
24062676Seschrock 				volblocksize = zfs_prop_default_numeric(
24072676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24082676Seschrock 
24092676Seschrock 			if ((error = zvol_check_volblocksize(
24102676Seschrock 			    volblocksize)) != 0 ||
24112676Seschrock 			    (error = zvol_check_volsize(volsize,
24122676Seschrock 			    volblocksize)) != 0) {
24134543Smarks 				nvlist_free(nvprops);
2414789Sahrens 				return (error);
24152676Seschrock 			}
24164577Sahrens 		} else if (type == DMU_OST_ZFS) {
24175331Samw 			int error;
24185331Samw 
24195498Stimh 			/*
24205331Samw 			 * We have to have normalization and
24215331Samw 			 * case-folding flags correct when we do the
24225331Samw 			 * file system creation, so go figure them out
24235498Stimh 			 * now.
24245331Samw 			 */
24255498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24265498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24275498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24287184Stimh 			    zct.zct_zplprops, &is_insensitive);
24295331Samw 			if (error != 0) {
24305331Samw 				nvlist_free(nvprops);
24315498Stimh 				nvlist_free(zct.zct_zplprops);
24325331Samw 				return (error);
24334577Sahrens 			}
24342676Seschrock 		}
243510272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
24366492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24375498Stimh 		nvlist_free(zct.zct_zplprops);
2438789Sahrens 	}
24392676Seschrock 
24402676Seschrock 	/*
24412676Seschrock 	 * It would be nice to do this atomically.
24422676Seschrock 	 */
24432676Seschrock 	if (error == 0) {
24444787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
244510242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
24462676Seschrock 	}
24474543Smarks 	nvlist_free(nvprops);
2448789Sahrens 	return (error);
2449789Sahrens }
2450789Sahrens 
24515367Sahrens /*
24525367Sahrens  * inputs:
24535367Sahrens  * zc_name	name of filesystem
24545367Sahrens  * zc_value	short name of snapshot
24555367Sahrens  * zc_cookie	recursive flag
24569396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24575367Sahrens  *
245810588SEric.Taylor@Sun.COM  * outputs:
245910588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
24605367Sahrens  */
2461789Sahrens static int
24622199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24632199Sahrens {
24647265Sahrens 	nvlist_t *nvprops = NULL;
24657265Sahrens 	int error;
24667265Sahrens 	boolean_t recursive = zc->zc_cookie;
24677265Sahrens 
24682676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24692199Sahrens 		return (EINVAL);
24707265Sahrens 
24717265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24727265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24739643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
24747265Sahrens 		return (error);
24757265Sahrens 
24769355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
24779355SMatthew.Ahrens@Sun.COM 	if (error)
24789355SMatthew.Ahrens@Sun.COM 		goto out;
24799355SMatthew.Ahrens@Sun.COM 
24809355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
24819355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
24829355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
24839355SMatthew.Ahrens@Sun.COM 		goto out;
24847265Sahrens 	}
24859355SMatthew.Ahrens@Sun.COM 
24869355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
24879355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
24889355SMatthew.Ahrens@Sun.COM 
24899355SMatthew.Ahrens@Sun.COM out:
24907265Sahrens 	nvlist_free(nvprops);
24917265Sahrens 	return (error);
24922199Sahrens }
24932199Sahrens 
24944007Smmusante int
24952199Sahrens zfs_unmount_snap(char *name, void *arg)
2496789Sahrens {
24972417Sahrens 	vfs_t *vfsp = NULL;
24982199Sahrens 
24996689Smaybee 	if (arg) {
25006689Smaybee 		char *snapname = arg;
25016689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25026689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25036689Smaybee 
25046689Smaybee 		(void) strcpy(buf, name);
25056689Smaybee 		(void) strcat(buf, "@");
25066689Smaybee 		(void) strcat(buf, snapname);
25076689Smaybee 		vfsp = zfs_get_vfs(buf);
25086689Smaybee 		kmem_free(buf, len);
25092417Sahrens 	} else if (strchr(name, '@')) {
25102199Sahrens 		vfsp = zfs_get_vfs(name);
25112199Sahrens 	}
25122199Sahrens 
25132199Sahrens 	if (vfsp) {
25142199Sahrens 		/*
25152199Sahrens 		 * Always force the unmount for snapshots.
25162199Sahrens 		 */
25172199Sahrens 		int flag = MS_FORCE;
2518789Sahrens 		int err;
2519789Sahrens 
25202199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25212199Sahrens 			VFS_RELE(vfsp);
25222199Sahrens 			return (err);
25232199Sahrens 		}
25242199Sahrens 		VFS_RELE(vfsp);
25252199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25262199Sahrens 			return (err);
25272199Sahrens 	}
25282199Sahrens 	return (0);
25292199Sahrens }
25302199Sahrens 
25315367Sahrens /*
25325367Sahrens  * inputs:
253310242Schris.kirby@sun.com  * zc_name		name of filesystem
253410242Schris.kirby@sun.com  * zc_value		short name of snapshot
253510242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25365367Sahrens  *
25375367Sahrens  * outputs:	none
25385367Sahrens  */
25392199Sahrens static int
25402199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25412199Sahrens {
25422199Sahrens 	int err;
2543789Sahrens 
25442676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25452199Sahrens 		return (EINVAL);
25462199Sahrens 	err = dmu_objset_find(zc->zc_name,
25472676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25482199Sahrens 	if (err)
25492199Sahrens 		return (err);
255010242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
255110242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
25522199Sahrens }
25532199Sahrens 
25545367Sahrens /*
25555367Sahrens  * inputs:
25565367Sahrens  * zc_name		name of dataset to destroy
25575367Sahrens  * zc_objset_type	type of objset
255810242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25595367Sahrens  *
25605367Sahrens  * outputs:		none
25615367Sahrens  */
25622199Sahrens static int
25632199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25642199Sahrens {
256510588SEric.Taylor@Sun.COM 	int err;
25662199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
256710588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
25682199Sahrens 		if (err)
25692199Sahrens 			return (err);
2570789Sahrens 	}
2571789Sahrens 
257210588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
257310588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
257410693Schris.kirby@sun.com 		(void) zvol_remove_minor(zc->zc_name);
257510588SEric.Taylor@Sun.COM 	return (err);
2576789Sahrens }
2577789Sahrens 
25785367Sahrens /*
25795367Sahrens  * inputs:
25805446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25815367Sahrens  *
25825367Sahrens  * outputs:	none
25835367Sahrens  */
2584789Sahrens static int
2585789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2586789Sahrens {
258710272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
25885446Sahrens 	int error;
258910272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
259010272SMatthew.Ahrens@Sun.COM 	char *clone_name;
259110272SMatthew.Ahrens@Sun.COM 
259210272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
259310272SMatthew.Ahrens@Sun.COM 	if (error)
259410272SMatthew.Ahrens@Sun.COM 		return (error);
259510272SMatthew.Ahrens@Sun.COM 
259610272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
259710272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
259810272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
259910272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
260010272SMatthew.Ahrens@Sun.COM 	}
260110272SMatthew.Ahrens@Sun.COM 
260210272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
260310272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
260410272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
260510272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
260610272SMatthew.Ahrens@Sun.COM 	}
26075446Sahrens 
26085446Sahrens 	/*
260910272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
26105446Sahrens 	 */
261110272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
261210272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26135446Sahrens 	if (error)
261410272SMatthew.Ahrens@Sun.COM 		goto out;
261510272SMatthew.Ahrens@Sun.COM 
261610298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
261710272SMatthew.Ahrens@Sun.COM 	if (error)
261810272SMatthew.Ahrens@Sun.COM 		goto out;
261910272SMatthew.Ahrens@Sun.COM 
262010272SMatthew.Ahrens@Sun.COM 	/*
262110272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
262210272SMatthew.Ahrens@Sun.COM 	 */
26239396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
262410298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
26256083Sek110237 		if (error == 0) {
26266083Sek110237 			int resume_err;
26276083Sek110237 
262810272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
262910272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
263010272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
263110272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
263210272SMatthew.Ahrens@Sun.COM 				ds = NULL;
263310272SMatthew.Ahrens@Sun.COM 			} else {
263410272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
263510272SMatthew.Ahrens@Sun.COM 			}
263610298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
26376083Sek110237 			error = error ? error : resume_err;
26386083Sek110237 		}
26395446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26405446Sahrens 	} else {
264110272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
264210272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
264310272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
264410272SMatthew.Ahrens@Sun.COM 			ds = NULL;
264510272SMatthew.Ahrens@Sun.COM 		} else {
264610272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
264710272SMatthew.Ahrens@Sun.COM 		}
26485446Sahrens 	}
264910272SMatthew.Ahrens@Sun.COM 
265010272SMatthew.Ahrens@Sun.COM 	/*
265110272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
265210272SMatthew.Ahrens@Sun.COM 	 */
265310272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
265410272SMatthew.Ahrens@Sun.COM 
265510272SMatthew.Ahrens@Sun.COM out:
265610272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
265710272SMatthew.Ahrens@Sun.COM 	if (ds)
265810272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26595446Sahrens 	return (error);
2660789Sahrens }
2661789Sahrens 
26625367Sahrens /*
26635367Sahrens  * inputs:
26645367Sahrens  * zc_name	old name of dataset
26655367Sahrens  * zc_value	new name of dataset
26665367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26675367Sahrens  *
26685367Sahrens  * outputs:	none
26695367Sahrens  */
2670789Sahrens static int
2671789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2672789Sahrens {
26734490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26744007Smmusante 
26752676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26765326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26775326Sek110237 	    strchr(zc->zc_value, '%'))
2678789Sahrens 		return (EINVAL);
2679789Sahrens 
26804007Smmusante 	/*
26814007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26824007Smmusante 	 * in which case the dataset code figures out which snapshots
26834007Smmusante 	 * to unmount.
26844007Smmusante 	 */
26854007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2686789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26872199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26882199Sahrens 		if (err)
26892199Sahrens 			return (err);
2690789Sahrens 	}
269110588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
269210588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
26934007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2694789Sahrens }
2695789Sahrens 
26966689Smaybee static void
26978536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26986689Smaybee {
26996689Smaybee 	zfs_cmd_t *zc;
27006689Smaybee 	nvpair_t *prop;
27016689Smaybee 
27026689Smaybee 	if (props == NULL)
27036689Smaybee 		return;
27046689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
27056689Smaybee 	(void) strcpy(zc->zc_name, dataset);
27066689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
27076689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27088536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
27098536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
27108536SDavid.Pacheco@Sun.COM 			continue;
27116689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
27126689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
27136689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
27146689Smaybee 	}
27156689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
27166689Smaybee }
27176689Smaybee 
27185367Sahrens /*
27195367Sahrens  * inputs:
27205367Sahrens  * zc_name		name of containing filesystem
27215367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27225367Sahrens  * zc_value		name of snapshot to create
27235367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27245367Sahrens  * zc_cookie		file descriptor to recv from
27255367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27265367Sahrens  * zc_guid		force flag
27275367Sahrens  *
27285367Sahrens  * outputs:
27295367Sahrens  * zc_cookie		number of bytes read
27305367Sahrens  */
2731789Sahrens static int
27325367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2733789Sahrens {
2734789Sahrens 	file_t *fp;
27355326Sek110237 	objset_t *os;
27365367Sahrens 	dmu_recv_cookie_t drc;
27375326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2738789Sahrens 	int error, fd;
27395367Sahrens 	offset_t off;
27405367Sahrens 	nvlist_t *props = NULL;
27416689Smaybee 	nvlist_t *origprops = NULL;
27425367Sahrens 	objset_t *origin = NULL;
27435367Sahrens 	char *tosnap;
27445367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2745789Sahrens 
27463265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27475326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27485326Sek110237 	    strchr(zc->zc_value, '%'))
27493265Sahrens 		return (EINVAL);
27503265Sahrens 
27515367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27525367Sahrens 	tosnap = strchr(tofs, '@');
27535367Sahrens 	*tosnap = '\0';
27545367Sahrens 	tosnap++;
27555367Sahrens 
27565367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27575367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27589643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27595367Sahrens 		return (error);
27605367Sahrens 
2761789Sahrens 	fd = zc->zc_cookie;
2762789Sahrens 	fp = getf(fd);
27635367Sahrens 	if (fp == NULL) {
27645367Sahrens 		nvlist_free(props);
2765789Sahrens 		return (EBADF);
27665367Sahrens 	}
27675326Sek110237 
276810298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
27696689Smaybee 		/*
27706689Smaybee 		 * If new properties are supplied, they are to completely
27716689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27726689Smaybee 		 */
277310298SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, B_TRUE);
277410298SMatthew.Ahrens@Sun.COM 
277510298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
27765326Sek110237 	}
27775326Sek110237 
27785367Sahrens 	if (zc->zc_string[0]) {
277910298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
27806689Smaybee 		if (error)
27816689Smaybee 			goto out;
27825367Sahrens 	}
27835367Sahrens 
27845367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
278510204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
27865367Sahrens 	if (origin)
278710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
27886689Smaybee 	if (error)
27896689Smaybee 		goto out;
27905326Sek110237 
27915326Sek110237 	/*
27926689Smaybee 	 * Reset properties.  We do this before we receive the stream
27936689Smaybee 	 * so that the properties are applied to the new data.
27945326Sek110237 	 */
27955367Sahrens 	if (props) {
27968536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27976689Smaybee 		/*
27986689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27996689Smaybee 		 */
28006689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
28015367Sahrens 	}
28025367Sahrens 
28035367Sahrens 	off = fp->f_offset;
28045367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
28055367Sahrens 
280610204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
280710204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
280810204SMatthew.Ahrens@Sun.COM 
280910204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
281010204SMatthew.Ahrens@Sun.COM 			/* online recv */
281110204SMatthew.Ahrens@Sun.COM 			int end_err;
281210298SMatthew.Ahrens@Sun.COM 
281310298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
281410204SMatthew.Ahrens@Sun.COM 			/*
281510204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
281610204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
281710204SMatthew.Ahrens@Sun.COM 			 */
281810204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
281910204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
282010204SMatthew.Ahrens@Sun.COM 				int resume_err =
282110298SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, tofs);
282210204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
282310204SMatthew.Ahrens@Sun.COM 			}
282410204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
282510204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
282610204SMatthew.Ahrens@Sun.COM 		} else {
28276689Smaybee 			error = dmu_recv_end(&drc);
28285367Sahrens 		}
28296083Sek110237 	}
28305367Sahrens 
28315367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28325367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28335367Sahrens 		fp->f_offset = off;
28342885Sahrens 
28356689Smaybee 	/*
28366689Smaybee 	 * On error, restore the original props.
28376689Smaybee 	 */
28386689Smaybee 	if (error && props) {
28398536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28406689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28416689Smaybee 	}
28426689Smaybee out:
28436689Smaybee 	nvlist_free(props);
28446689Smaybee 	nvlist_free(origprops);
2845789Sahrens 	releasef(fd);
2846789Sahrens 	return (error);
2847789Sahrens }
2848789Sahrens 
28495367Sahrens /*
28505367Sahrens  * inputs:
28515367Sahrens  * zc_name	name of snapshot to send
28525367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28535367Sahrens  * zc_cookie	file descriptor to send stream to
28545367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28555367Sahrens  *
28565367Sahrens  * outputs: none
28575367Sahrens  */
2858789Sahrens static int
28595367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2860789Sahrens {
2861789Sahrens 	objset_t *fromsnap = NULL;
2862789Sahrens 	objset_t *tosnap;
2863789Sahrens 	file_t *fp;
2864789Sahrens 	int error;
28655367Sahrens 	offset_t off;
2866789Sahrens 
286710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
2868789Sahrens 	if (error)
2869789Sahrens 		return (error);
2870789Sahrens 
28712676Seschrock 	if (zc->zc_value[0] != '\0') {
28728012SEric.Taylor@Sun.COM 		char *buf;
28732885Sahrens 		char *cp;
28742885Sahrens 
28758012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28768012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28772885Sahrens 		cp = strchr(buf, '@');
28782885Sahrens 		if (cp)
28792885Sahrens 			*(cp+1) = 0;
28808012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
288110298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
28828012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2883789Sahrens 		if (error) {
288410298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(tosnap, FTAG);
2885789Sahrens 			return (error);
2886789Sahrens 		}
2887789Sahrens 	}
2888789Sahrens 
2889789Sahrens 	fp = getf(zc->zc_cookie);
2890789Sahrens 	if (fp == NULL) {
289110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
2892789Sahrens 		if (fromsnap)
289310298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(fromsnap, FTAG);
2894789Sahrens 		return (EBADF);
2895789Sahrens 	}
2896789Sahrens 
28975367Sahrens 	off = fp->f_offset;
28985367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28995367Sahrens 
29005367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
29015367Sahrens 		fp->f_offset = off;
2902789Sahrens 	releasef(zc->zc_cookie);
2903789Sahrens 	if (fromsnap)
290410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
290510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
2906789Sahrens 	return (error);
2907789Sahrens }
2908789Sahrens 
29091544Seschrock static int
29101544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29111544Seschrock {
29121544Seschrock 	int id, error;
29131544Seschrock 
29141544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29151544Seschrock 	    &zc->zc_inject_record);
29161544Seschrock 
29171544Seschrock 	if (error == 0)
29181544Seschrock 		zc->zc_guid = (uint64_t)id;
29191544Seschrock 
29201544Seschrock 	return (error);
29211544Seschrock }
29221544Seschrock 
29231544Seschrock static int
29241544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29251544Seschrock {
29261544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29271544Seschrock }
29281544Seschrock 
29291544Seschrock static int
29301544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29311544Seschrock {
29321544Seschrock 	int id = (int)zc->zc_guid;
29331544Seschrock 	int error;
29341544Seschrock 
29351544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29361544Seschrock 	    &zc->zc_inject_record);
29371544Seschrock 
29381544Seschrock 	zc->zc_guid = id;
29391544Seschrock 
29401544Seschrock 	return (error);
29411544Seschrock }
29421544Seschrock 
29431544Seschrock static int
29441544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29451544Seschrock {
29461544Seschrock 	spa_t *spa;
29471544Seschrock 	int error;
29482676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29491544Seschrock 
29501544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29511544Seschrock 		return (error);
29521544Seschrock 
29532676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29541544Seschrock 	    &count);
29551544Seschrock 	if (error == 0)
29562676Seschrock 		zc->zc_nvlist_dst_size = count;
29571544Seschrock 	else
29582676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29591544Seschrock 
29601544Seschrock 	spa_close(spa, FTAG);
29611544Seschrock 
29621544Seschrock 	return (error);
29631544Seschrock }
29641544Seschrock 
29651544Seschrock static int
29661544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29671544Seschrock {
29681544Seschrock 	spa_t *spa;
29691544Seschrock 	vdev_t *vd;
29701544Seschrock 	int error;
29711544Seschrock 
29727294Sperrin 	/*
29737294Sperrin 	 * On zpool clear we also fix up missing slogs
29747294Sperrin 	 */
29757294Sperrin 	mutex_enter(&spa_namespace_lock);
29767294Sperrin 	spa = spa_lookup(zc->zc_name);
29777294Sperrin 	if (spa == NULL) {
29787294Sperrin 		mutex_exit(&spa_namespace_lock);
29797294Sperrin 		return (EIO);
29807294Sperrin 	}
29817294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29827294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29837294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29847294Sperrin 	}
2985*10921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
29867294Sperrin 	mutex_exit(&spa_namespace_lock);
29877294Sperrin 
2988*10921STim.Haley@Sun.COM 	if (zc->zc_cookie == ZPOOL_NO_REWIND) {
2989*10921STim.Haley@Sun.COM 		error = spa_open(zc->zc_name, &spa, FTAG);
2990*10921STim.Haley@Sun.COM 	} else {
2991*10921STim.Haley@Sun.COM 		nvlist_t *policy;
2992*10921STim.Haley@Sun.COM 		nvlist_t *config = NULL;
2993*10921STim.Haley@Sun.COM 
2994*10921STim.Haley@Sun.COM 		if (zc->zc_nvlist_src == NULL)
2995*10921STim.Haley@Sun.COM 			return (EINVAL);
2996*10921STim.Haley@Sun.COM 
2997*10921STim.Haley@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
2998*10921STim.Haley@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
2999*10921STim.Haley@Sun.COM 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3000*10921STim.Haley@Sun.COM 			    policy, &config);
3001*10921STim.Haley@Sun.COM 			if (config != NULL) {
3002*10921STim.Haley@Sun.COM 				(void) put_nvlist(zc, config);
3003*10921STim.Haley@Sun.COM 				nvlist_free(config);
3004*10921STim.Haley@Sun.COM 			}
3005*10921STim.Haley@Sun.COM 			nvlist_free(policy);
3006*10921STim.Haley@Sun.COM 		}
3007*10921STim.Haley@Sun.COM 	}
3008*10921STim.Haley@Sun.COM 
3009*10921STim.Haley@Sun.COM 	if (error)
30101544Seschrock 		return (error);
30111544Seschrock 
301210685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
30131544Seschrock 
30142676Seschrock 	if (zc->zc_guid == 0) {
30151544Seschrock 		vd = NULL;
30166643Seschrock 	} else {
30176643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
30185450Sbrendan 		if (vd == NULL) {
30197754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
30205450Sbrendan 			spa_close(spa, FTAG);
30215450Sbrendan 			return (ENODEV);
30225450Sbrendan 		}
30231544Seschrock 	}
30241544Seschrock 
30257754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
30267754SJeff.Bonwick@Sun.COM 
30277754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
30287754SJeff.Bonwick@Sun.COM 
30297754SJeff.Bonwick@Sun.COM 	/*
30307754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
30317754SJeff.Bonwick@Sun.COM 	 */
30329234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30339234SGeorge.Wilson@Sun.COM 		error = EIO;
30341544Seschrock 
30351544Seschrock 	spa_close(spa, FTAG);
30361544Seschrock 
30379234SGeorge.Wilson@Sun.COM 	return (error);
30381544Seschrock }
30391544Seschrock 
30405367Sahrens /*
30415367Sahrens  * inputs:
30425367Sahrens  * zc_name	name of filesystem
30435367Sahrens  * zc_value	name of origin snapshot
30445367Sahrens  *
304510588SEric.Taylor@Sun.COM  * outputs:
304610588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
30475367Sahrens  */
30481544Seschrock static int
30492082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30502082Seschrock {
30512417Sahrens 	char *cp;
30522417Sahrens 
30532417Sahrens 	/*
30542417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30552417Sahrens 	 * it's easier.
30562417Sahrens 	 */
30572676Seschrock 	cp = strchr(zc->zc_value, '@');
30582417Sahrens 	if (cp)
30592417Sahrens 		*cp = '\0';
30602676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30612417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
306210588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
30632082Seschrock }
30642082Seschrock 
30654543Smarks /*
30669396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30679396SMatthew.Ahrens@Sun.COM  *
30689396SMatthew.Ahrens@Sun.COM  * inputs:
30699396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30709396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30719396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30729396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30739396SMatthew.Ahrens@Sun.COM  *
30749396SMatthew.Ahrens@Sun.COM  * outputs:
30759396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30769396SMatthew.Ahrens@Sun.COM  */
30779396SMatthew.Ahrens@Sun.COM static int
30789396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30799396SMatthew.Ahrens@Sun.COM {
30809396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30819396SMatthew.Ahrens@Sun.COM 	int error;
30829396SMatthew.Ahrens@Sun.COM 
30839396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30849396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30859396SMatthew.Ahrens@Sun.COM 
308610298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
30879396SMatthew.Ahrens@Sun.COM 	if (error)
30889396SMatthew.Ahrens@Sun.COM 		return (error);
30899396SMatthew.Ahrens@Sun.COM 
30909396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
30919396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
30929396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30939396SMatthew.Ahrens@Sun.COM 
30949396SMatthew.Ahrens@Sun.COM 	return (error);
30959396SMatthew.Ahrens@Sun.COM }
30969396SMatthew.Ahrens@Sun.COM 
30979396SMatthew.Ahrens@Sun.COM /*
30989396SMatthew.Ahrens@Sun.COM  * inputs:
30999396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31009396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
31019396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
31029396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
31039396SMatthew.Ahrens@Sun.COM  *
31049396SMatthew.Ahrens@Sun.COM  * outputs:
31059396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
31069396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
31079396SMatthew.Ahrens@Sun.COM  */
31089396SMatthew.Ahrens@Sun.COM static int
31099396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
31109396SMatthew.Ahrens@Sun.COM {
31119396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31129396SMatthew.Ahrens@Sun.COM 	int error;
31139396SMatthew.Ahrens@Sun.COM 
311410298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
31159396SMatthew.Ahrens@Sun.COM 	if (error)
31169396SMatthew.Ahrens@Sun.COM 		return (error);
31179396SMatthew.Ahrens@Sun.COM 
31189396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
31199396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
31209396SMatthew.Ahrens@Sun.COM 
31219396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
31229396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
31239396SMatthew.Ahrens@Sun.COM 
31249396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
31259396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
31269396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
31279396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
31289396SMatthew.Ahrens@Sun.COM 	}
31299396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
31309396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31319396SMatthew.Ahrens@Sun.COM 
31329396SMatthew.Ahrens@Sun.COM 	return (error);
31339396SMatthew.Ahrens@Sun.COM }
31349396SMatthew.Ahrens@Sun.COM 
31359396SMatthew.Ahrens@Sun.COM /*
31369396SMatthew.Ahrens@Sun.COM  * inputs:
31379396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31389396SMatthew.Ahrens@Sun.COM  *
31399396SMatthew.Ahrens@Sun.COM  * outputs:
31409396SMatthew.Ahrens@Sun.COM  * none
31419396SMatthew.Ahrens@Sun.COM  */
31429396SMatthew.Ahrens@Sun.COM static int
31439396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31449396SMatthew.Ahrens@Sun.COM {
31459396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31469396SMatthew.Ahrens@Sun.COM 	int error;
31479396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31489396SMatthew.Ahrens@Sun.COM 
31499396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
315010298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
31519396SMatthew.Ahrens@Sun.COM 			/*
31529396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31539396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31549396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31559396SMatthew.Ahrens@Sun.COM 			 */
315610298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
315710298SMatthew.Ahrens@Sun.COM 			if (error == 0)
315810298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
31599396SMatthew.Ahrens@Sun.COM 		}
31609396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31619396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31629396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31639396SMatthew.Ahrens@Sun.COM 	} else {
316410298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
316510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
31669396SMatthew.Ahrens@Sun.COM 		if (error)
31679396SMatthew.Ahrens@Sun.COM 			return (error);
31689396SMatthew.Ahrens@Sun.COM 
31699396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
317010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
31719396SMatthew.Ahrens@Sun.COM 	}
31729396SMatthew.Ahrens@Sun.COM 
31739396SMatthew.Ahrens@Sun.COM 	return (error);
31749396SMatthew.Ahrens@Sun.COM }
31759396SMatthew.Ahrens@Sun.COM 
31769396SMatthew.Ahrens@Sun.COM /*
31774543Smarks  * We don't want to have a hard dependency
31784543Smarks  * against some special symbols in sharefs
31795331Samw  * nfs, and smbsrv.  Determine them if needed when
31804543Smarks  * the first file system is shared.
31815331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31824543Smarks  */
31835331Samw int (*znfsexport_fs)(void *arg);
31844543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31855331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31865331Samw 
31875331Samw int zfs_nfsshare_inited;
31885331Samw int zfs_smbshare_inited;
31895331Samw 
31904543Smarks ddi_modhandle_t nfs_mod;
31914543Smarks ddi_modhandle_t sharefs_mod;
31925331Samw ddi_modhandle_t smbsrv_mod;
31934543Smarks kmutex_t zfs_share_lock;
31944543Smarks 
31954543Smarks static int
31965331Samw zfs_init_sharefs()
31975331Samw {
31985331Samw 	int error;
31995331Samw 
32005331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
32015331Samw 	/* Both NFS and SMB shares also require sharetab support. */
32025331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
32035331Samw 	    ddi_modopen("fs/sharefs",
32045331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
32055331Samw 		return (ENOSYS);
32065331Samw 	}
32075331Samw 	if (zshare_fs == NULL && ((zshare_fs =
32085331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
32095331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
32105331Samw 		return (ENOSYS);
32115331Samw 	}
32125331Samw 	return (0);
32135331Samw }
32145331Samw 
32155331Samw static int
32164543Smarks zfs_ioc_share(zfs_cmd_t *zc)
32174543Smarks {
32184543Smarks 	int error;
32194543Smarks 	int opcode;
32204543Smarks 
32215331Samw 	switch (zc->zc_share.z_sharetype) {
32225331Samw 	case ZFS_SHARE_NFS:
32235331Samw 	case ZFS_UNSHARE_NFS:
32245331Samw 		if (zfs_nfsshare_inited == 0) {
32255331Samw 			mutex_enter(&zfs_share_lock);
32265331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
32275331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32285331Samw 				mutex_exit(&zfs_share_lock);
32295331Samw 				return (ENOSYS);
32305331Samw 			}
32315331Samw 			if (znfsexport_fs == NULL &&
32325331Samw 			    ((znfsexport_fs = (int (*)(void *))
32335331Samw 			    ddi_modsym(nfs_mod,
32345331Samw 			    "nfs_export", &error)) == NULL)) {
32355331Samw 				mutex_exit(&zfs_share_lock);
32365331Samw 				return (ENOSYS);
32375331Samw 			}
32385331Samw 			error = zfs_init_sharefs();
32395331Samw 			if (error) {
32405331Samw 				mutex_exit(&zfs_share_lock);
32415331Samw 				return (ENOSYS);
32425331Samw 			}
32435331Samw 			zfs_nfsshare_inited = 1;
32444543Smarks 			mutex_exit(&zfs_share_lock);
32454543Smarks 		}
32465331Samw 		break;
32475331Samw 	case ZFS_SHARE_SMB:
32485331Samw 	case ZFS_UNSHARE_SMB:
32495331Samw 		if (zfs_smbshare_inited == 0) {
32505331Samw 			mutex_enter(&zfs_share_lock);
32515331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32525331Samw 			    ddi_modopen("drv/smbsrv",
32535331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32545331Samw 				mutex_exit(&zfs_share_lock);
32555331Samw 				return (ENOSYS);
32565331Samw 			}
32575331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32585331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32596139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32605331Samw 				mutex_exit(&zfs_share_lock);
32615331Samw 				return (ENOSYS);
32625331Samw 			}
32635331Samw 			error = zfs_init_sharefs();
32645331Samw 			if (error) {
32655331Samw 				mutex_exit(&zfs_share_lock);
32665331Samw 				return (ENOSYS);
32675331Samw 			}
32685331Samw 			zfs_smbshare_inited = 1;
32694543Smarks 			mutex_exit(&zfs_share_lock);
32704543Smarks 		}
32715331Samw 		break;
32725331Samw 	default:
32735331Samw 		return (EINVAL);
32744543Smarks 	}
32754543Smarks 
32765331Samw 	switch (zc->zc_share.z_sharetype) {
32775331Samw 	case ZFS_SHARE_NFS:
32785331Samw 	case ZFS_UNSHARE_NFS:
32795331Samw 		if (error =
32805331Samw 		    znfsexport_fs((void *)
32815331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32825331Samw 			return (error);
32835331Samw 		break;
32845331Samw 	case ZFS_SHARE_SMB:
32855331Samw 	case ZFS_UNSHARE_SMB:
32865331Samw 		if (error = zsmbexport_fs((void *)
32875331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32885331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32898845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32905331Samw 			return (error);
32915331Samw 		}
32925331Samw 		break;
32935331Samw 	}
32945331Samw 
32955331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32965331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32974543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32984543Smarks 
32995331Samw 	/*
33005331Samw 	 * Add or remove share from sharetab
33015331Samw 	 */
33024543Smarks 	error = zshare_fs(opcode,
33034543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
33044543Smarks 	    zc->zc_share.z_sharemax);
33054543Smarks 
33064543Smarks 	return (error);
33074543Smarks 
33084543Smarks }
33094543Smarks 
33108845Samw@Sun.COM ace_t full_access[] = {
33118845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
33128845Samw@Sun.COM };
33138845Samw@Sun.COM 
33148845Samw@Sun.COM /*
33158845Samw@Sun.COM  * Remove all ACL files in shares dir
33168845Samw@Sun.COM  */
33178845Samw@Sun.COM static int
33188845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
33198845Samw@Sun.COM {
33208845Samw@Sun.COM 	zap_cursor_t	zc;
33218845Samw@Sun.COM 	zap_attribute_t	zap;
33228845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
33238845Samw@Sun.COM 	int error;
33248845Samw@Sun.COM 
33258845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
33268845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
33278845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
33288845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
33298845Samw@Sun.COM 		    NULL, 0)) != 0)
33308845Samw@Sun.COM 			break;
33318845Samw@Sun.COM 	}
33328845Samw@Sun.COM 	zap_cursor_fini(&zc);
33338845Samw@Sun.COM 	return (error);
33348845Samw@Sun.COM }
33358845Samw@Sun.COM 
33368845Samw@Sun.COM static int
33378845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33388845Samw@Sun.COM {
33398845Samw@Sun.COM 	vnode_t *vp;
33408845Samw@Sun.COM 	znode_t *dzp;
33418845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33428845Samw@Sun.COM 	znode_t *sharedir;
33438845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33448845Samw@Sun.COM 	nvlist_t *nvlist;
33458845Samw@Sun.COM 	char *src, *target;
33468845Samw@Sun.COM 	vattr_t vattr;
33478845Samw@Sun.COM 	vsecattr_t vsec;
33488845Samw@Sun.COM 	int error = 0;
33498845Samw@Sun.COM 
33508845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33518845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33528845Samw@Sun.COM 		return (error);
33538845Samw@Sun.COM 
33548845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33558845Samw@Sun.COM 
33568845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33578845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33588845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33598845Samw@Sun.COM 		VN_RELE(vp);
33608845Samw@Sun.COM 		return (EINVAL);
33618845Samw@Sun.COM 	}
33628845Samw@Sun.COM 
33638845Samw@Sun.COM 	dzp = VTOZ(vp);
33648845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33658845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33668845Samw@Sun.COM 
33679030SMark.Shellenbaum@Sun.COM 	/*
33689030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33699030SMark.Shellenbaum@Sun.COM 	 */
33709030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33719030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33729030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33739030SMark.Shellenbaum@Sun.COM 
33749030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33759030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33769030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33779030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33789030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33799030SMark.Shellenbaum@Sun.COM 		if (error) {
33809030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33819030SMark.Shellenbaum@Sun.COM 		} else {
33829030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33839030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33849030SMark.Shellenbaum@Sun.COM 		}
33859030SMark.Shellenbaum@Sun.COM 		if (error) {
33869030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33879030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33889030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33899030SMark.Shellenbaum@Sun.COM 			return (error);
33909030SMark.Shellenbaum@Sun.COM 		}
33919030SMark.Shellenbaum@Sun.COM 	}
33929030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33939030SMark.Shellenbaum@Sun.COM 
33949030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33958845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33969030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33978845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33988845Samw@Sun.COM 		return (error);
33998845Samw@Sun.COM 	}
34008845Samw@Sun.COM 
34018845Samw@Sun.COM 	switch (zc->zc_cookie) {
34028845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
34038845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
34048845Samw@Sun.COM 		vattr.va_type = VREG;
34058845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
34068845Samw@Sun.COM 		vattr.va_uid = 0;
34078845Samw@Sun.COM 		vattr.va_gid = 0;
34088845Samw@Sun.COM 
34098845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
34108845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
34118845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
34128845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
34138845Samw@Sun.COM 
34148845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
34158845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
34168845Samw@Sun.COM 		if (resourcevp)
34178845Samw@Sun.COM 			VN_RELE(resourcevp);
34188845Samw@Sun.COM 		break;
34198845Samw@Sun.COM 
34208845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
34218845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
34228845Samw@Sun.COM 		    NULL, 0);
34238845Samw@Sun.COM 		break;
34248845Samw@Sun.COM 
34258845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
34268845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
34279643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
34288845Samw@Sun.COM 			VN_RELE(vp);
34298845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34308845Samw@Sun.COM 			return (error);
34318845Samw@Sun.COM 		}
34328845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34338845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34348845Samw@Sun.COM 		    &target)) {
34358845Samw@Sun.COM 			VN_RELE(vp);
34369179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34378845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34388845Samw@Sun.COM 			return (error);
34398845Samw@Sun.COM 		}
34408845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34418845Samw@Sun.COM 		    kcred, NULL, 0);
34428845Samw@Sun.COM 		nvlist_free(nvlist);
34438845Samw@Sun.COM 		break;
34448845Samw@Sun.COM 
34458845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34468845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34478845Samw@Sun.COM 		break;
34488845Samw@Sun.COM 
34498845Samw@Sun.COM 	default:
34508845Samw@Sun.COM 		error = EINVAL;
34518845Samw@Sun.COM 		break;
34528845Samw@Sun.COM 	}
34538845Samw@Sun.COM 
34548845Samw@Sun.COM 	VN_RELE(vp);
34558845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34568845Samw@Sun.COM 
34578845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34588845Samw@Sun.COM 
34598845Samw@Sun.COM 	return (error);
34608845Samw@Sun.COM }
34618845Samw@Sun.COM 
34624543Smarks /*
346310242Schris.kirby@sun.com  * inputs:
346410242Schris.kirby@sun.com  * zc_name	name of filesystem
346510242Schris.kirby@sun.com  * zc_value	short name of snap
346610242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
346710242Schris.kirby@sun.com  * zc_cookie	recursive flag
346810342Schris.kirby@sun.com  * zc_temphold	set if hold is temporary
346910242Schris.kirby@sun.com  *
347010242Schris.kirby@sun.com  * outputs:		none
347110242Schris.kirby@sun.com  */
347210242Schris.kirby@sun.com static int
347310242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
347410242Schris.kirby@sun.com {
347510242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
347610242Schris.kirby@sun.com 
347710242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
347810242Schris.kirby@sun.com 		return (EINVAL);
347910242Schris.kirby@sun.com 
348010242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
348110342Schris.kirby@sun.com 	    zc->zc_string, recursive, zc->zc_temphold));
348210242Schris.kirby@sun.com }
348310242Schris.kirby@sun.com 
348410242Schris.kirby@sun.com /*
348510242Schris.kirby@sun.com  * inputs:
348610242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
348710242Schris.kirby@sun.com  * zc_value	short name of snap
348810242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
348910242Schris.kirby@sun.com  * zc_cookie	recursive flag
349010242Schris.kirby@sun.com  *
349110242Schris.kirby@sun.com  * outputs:		none
349210242Schris.kirby@sun.com  */
349310242Schris.kirby@sun.com static int
349410242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
349510242Schris.kirby@sun.com {
349610242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
349710242Schris.kirby@sun.com 
349810242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
349910242Schris.kirby@sun.com 		return (EINVAL);
350010242Schris.kirby@sun.com 
350110242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
350210242Schris.kirby@sun.com 	    zc->zc_string, recursive));
350310242Schris.kirby@sun.com }
350410242Schris.kirby@sun.com 
350510242Schris.kirby@sun.com /*
350610242Schris.kirby@sun.com  * inputs:
350710242Schris.kirby@sun.com  * zc_name		name of filesystem
350810242Schris.kirby@sun.com  *
350910242Schris.kirby@sun.com  * outputs:
351010242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
351110242Schris.kirby@sun.com  */
351210242Schris.kirby@sun.com static int
351310242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
351410242Schris.kirby@sun.com {
351510242Schris.kirby@sun.com 	nvlist_t *nvp;
351610242Schris.kirby@sun.com 	int error;
351710242Schris.kirby@sun.com 
351810242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
351910242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
352010242Schris.kirby@sun.com 		nvlist_free(nvp);
352110242Schris.kirby@sun.com 	}
352210242Schris.kirby@sun.com 
352310242Schris.kirby@sun.com 	return (error);
352410242Schris.kirby@sun.com }
352510242Schris.kirby@sun.com 
352610242Schris.kirby@sun.com /*
35274988Sek110237  * pool create, destroy, and export don't log the history as part of
35284988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35294988Sek110237  * do the logging of those commands.
35304543Smarks  */
3531789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
35329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35359234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35379234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35399234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35409234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
35419234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35439234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
35459234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35479234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
35499234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
35519234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35539234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35559234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35579234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35599234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35619234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35639234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35659234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35669425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35679425SEric.Schrock@Sun.COM 	    B_TRUE },
35689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35699234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35719234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35739234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35759234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
35779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
35789234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
35799234SGeorge.Wilson@Sun.COM 	    B_TRUE},
35809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
35819234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
35839234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
35849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
35859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
35869234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35879234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35889234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35909234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
35929234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
35949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
35959234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
35979234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
35999234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36009234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
36019234SGeorge.Wilson@Sun.COM 	    B_FALSE },
360210233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
360310233SGeorge.Wilson@Sun.COM 	    B_TRUE },
36049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
36059234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
36079234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
36099234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
36119234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
36139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36149234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
36159234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
36169234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36179234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
36189396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
36199396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
36209396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36219396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
36229396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36239396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
36249396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
362510242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
362610242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
362710242Schris.kirby@sun.com 	    B_TRUE },
362810242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
362910242Schris.kirby@sun.com 	    B_TRUE }
3630789Sahrens };
3631789Sahrens 
36329234SGeorge.Wilson@Sun.COM int
36339234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
36349234SGeorge.Wilson@Sun.COM {
36359234SGeorge.Wilson@Sun.COM 	spa_t *spa;
36369234SGeorge.Wilson@Sun.COM 	int error;
36379234SGeorge.Wilson@Sun.COM 
36389234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
36399234SGeorge.Wilson@Sun.COM 
36409396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
36419234SGeorge.Wilson@Sun.COM 	if (error == 0) {
36429234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
36439234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
36449234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
36459234SGeorge.Wilson@Sun.COM 	}
36469234SGeorge.Wilson@Sun.COM 	return (error);
36479234SGeorge.Wilson@Sun.COM }
36489234SGeorge.Wilson@Sun.COM 
3649789Sahrens static int
3650789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3651789Sahrens {
3652789Sahrens 	zfs_cmd_t *zc;
3653789Sahrens 	uint_t vec;
36542199Sahrens 	int error, rc;
3655789Sahrens 
3656789Sahrens 	if (getminor(dev) != 0)
3657789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3658789Sahrens 
3659789Sahrens 	vec = cmd - ZFS_IOC;
36604787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3661789Sahrens 
3662789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3663789Sahrens 		return (EINVAL);
3664789Sahrens 
3665789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3666789Sahrens 
36679643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3668789Sahrens 
366910588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
36704543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3671789Sahrens 
3672789Sahrens 	/*
3673789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3674789Sahrens 	 * the lower layers.
3675789Sahrens 	 */
3676789Sahrens 	if (error == 0) {
3677789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
36789643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3679789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
36804577Sahrens 		case POOL_NAME:
3681789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3682789Sahrens 				error = EINVAL;
36839234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36849234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36859234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3686789Sahrens 			break;
3687789Sahrens 
36884577Sahrens 		case DATASET_NAME:
3689789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3690789Sahrens 				error = EINVAL;
36919234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36929234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36939234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3694789Sahrens 			break;
36952856Snd150628 
36964577Sahrens 		case NO_NAME:
36972856Snd150628 			break;
3698789Sahrens 		}
3699789Sahrens 	}
3700789Sahrens 
3701789Sahrens 	if (error == 0)
3702789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3703789Sahrens 
37049643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
37054543Smarks 	if (error == 0) {
37062199Sahrens 		error = rc;
37079396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
37084543Smarks 			zfs_log_history(zc);
37094543Smarks 	}
3710789Sahrens 
3711789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3712789Sahrens 	return (error);
3713789Sahrens }
3714789Sahrens 
3715789Sahrens static int
3716789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3717789Sahrens {
3718789Sahrens 	if (cmd != DDI_ATTACH)
3719789Sahrens 		return (DDI_FAILURE);
3720789Sahrens 
3721789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3722789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3723789Sahrens 		return (DDI_FAILURE);
3724789Sahrens 
3725789Sahrens 	zfs_dip = dip;
3726789Sahrens 
3727789Sahrens 	ddi_report_dev(dip);
3728789Sahrens 
3729789Sahrens 	return (DDI_SUCCESS);
3730789Sahrens }
3731789Sahrens 
3732789Sahrens static int
3733789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3734789Sahrens {
3735789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3736789Sahrens 		return (DDI_FAILURE);
3737789Sahrens 
3738789Sahrens 	if (cmd != DDI_DETACH)
3739789Sahrens 		return (DDI_FAILURE);
3740789Sahrens 
3741789Sahrens 	zfs_dip = NULL;
3742789Sahrens 
3743789Sahrens 	ddi_prop_remove_all(dip);
3744789Sahrens 	ddi_remove_minor_node(dip, NULL);
3745789Sahrens 
3746789Sahrens 	return (DDI_SUCCESS);
3747789Sahrens }
3748789Sahrens 
3749789Sahrens /*ARGSUSED*/
3750789Sahrens static int
3751789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3752789Sahrens {
3753789Sahrens 	switch (infocmd) {
3754789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3755789Sahrens 		*result = zfs_dip;
3756789Sahrens 		return (DDI_SUCCESS);
3757789Sahrens 
3758789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3759849Sbonwick 		*result = (void *)0;
3760789Sahrens 		return (DDI_SUCCESS);
3761789Sahrens 	}
3762789Sahrens 
3763789Sahrens 	return (DDI_FAILURE);
3764789Sahrens }
3765789Sahrens 
3766789Sahrens /*
3767789Sahrens  * OK, so this is a little weird.
3768789Sahrens  *
3769789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3770789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3771789Sahrens  *
3772789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3773789Sahrens  * so most of the standard driver entry points are in zvol.c.
3774789Sahrens  */
3775789Sahrens static struct cb_ops zfs_cb_ops = {
3776789Sahrens 	zvol_open,	/* open */
3777789Sahrens 	zvol_close,	/* close */
3778789Sahrens 	zvol_strategy,	/* strategy */
3779789Sahrens 	nodev,		/* print */
37806423Sgw25295 	zvol_dump,	/* dump */
3781789Sahrens 	zvol_read,	/* read */
3782789Sahrens 	zvol_write,	/* write */
3783789Sahrens 	zfsdev_ioctl,	/* ioctl */
3784789Sahrens 	nodev,		/* devmap */
3785789Sahrens 	nodev,		/* mmap */
3786789Sahrens 	nodev,		/* segmap */
3787789Sahrens 	nochpoll,	/* poll */
3788789Sahrens 	ddi_prop_op,	/* prop_op */
3789789Sahrens 	NULL,		/* streamtab */
3790789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3791789Sahrens 	CB_REV,		/* version */
37923638Sbillm 	nodev,		/* async read */
37933638Sbillm 	nodev,		/* async write */
3794789Sahrens };
3795789Sahrens 
3796789Sahrens static struct dev_ops zfs_dev_ops = {
3797789Sahrens 	DEVO_REV,	/* version */
3798789Sahrens 	0,		/* refcnt */
3799789Sahrens 	zfs_info,	/* info */
3800789Sahrens 	nulldev,	/* identify */
3801789Sahrens 	nulldev,	/* probe */
3802789Sahrens 	zfs_attach,	/* attach */
3803789Sahrens 	zfs_detach,	/* detach */
3804789Sahrens 	nodev,		/* reset */
3805789Sahrens 	&zfs_cb_ops,	/* driver operations */
38067656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
38077656SSherry.Moore@Sun.COM 	NULL,		/* power */
38087656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3809789Sahrens };
3810789Sahrens 
3811789Sahrens static struct modldrv zfs_modldrv = {
38127656SSherry.Moore@Sun.COM 	&mod_driverops,
38137656SSherry.Moore@Sun.COM 	"ZFS storage pool",
38147656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3815789Sahrens };
3816789Sahrens 
3817789Sahrens static struct modlinkage modlinkage = {
3818789Sahrens 	MODREV_1,
3819789Sahrens 	(void *)&zfs_modlfs,
3820789Sahrens 	(void *)&zfs_modldrv,
3821789Sahrens 	NULL
3822789Sahrens };
3823789Sahrens 
38244720Sfr157268 
38254720Sfr157268 uint_t zfs_fsyncer_key;
38265326Sek110237 extern uint_t rrw_tsd_key;
38274720Sfr157268 
3828789Sahrens int
3829789Sahrens _init(void)
3830789Sahrens {
3831789Sahrens 	int error;
3832789Sahrens 
3833849Sbonwick 	spa_init(FREAD | FWRITE);
3834849Sbonwick 	zfs_init();
3835849Sbonwick 	zvol_init();
3836849Sbonwick 
3837849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3838849Sbonwick 		zvol_fini();
3839849Sbonwick 		zfs_fini();
3840849Sbonwick 		spa_fini();
3841789Sahrens 		return (error);
3842849Sbonwick 	}
3843789Sahrens 
38444720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
38455326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
38464720Sfr157268 
3847789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3848789Sahrens 	ASSERT(error == 0);
38494543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3850789Sahrens 
3851789Sahrens 	return (0);
3852789Sahrens }
3853789Sahrens 
3854789Sahrens int
3855789Sahrens _fini(void)
3856789Sahrens {
3857789Sahrens 	int error;
3858789Sahrens 
38591544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3860789Sahrens 		return (EBUSY);
3861789Sahrens 
3862789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3863789Sahrens 		return (error);
3864789Sahrens 
3865789Sahrens 	zvol_fini();
3866789Sahrens 	zfs_fini();
3867789Sahrens 	spa_fini();
38685331Samw 	if (zfs_nfsshare_inited)
38694543Smarks 		(void) ddi_modclose(nfs_mod);
38705331Samw 	if (zfs_smbshare_inited)
38715331Samw 		(void) ddi_modclose(smbsrv_mod);
38725331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
38734543Smarks 		(void) ddi_modclose(sharefs_mod);
3874789Sahrens 
38754720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3876789Sahrens 	ldi_ident_release(zfs_li);
3877789Sahrens 	zfs_li = NULL;
38784543Smarks 	mutex_destroy(&zfs_share_lock);
3879789Sahrens 
3880789Sahrens 	return (error);
3881789Sahrens }
3882789Sahrens 
3883789Sahrens int
3884789Sahrens _info(struct modinfo *modinfop)
3885789Sahrens {
3886789Sahrens 	return (mod_info(&modlinkage, modinfop));
3887789Sahrens }
3888