xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10672:cee5a0f557db)
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 {
953789Sahrens 	int error;
9545094Slling 	nvlist_t *config, *props = NULL;
955789Sahrens 	uint64_t guid;
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)
9729425SEric.Schrock@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config,
9736643Seschrock 		    props);
974789Sahrens 	else
9755094Slling 		error = spa_import(zc->zc_name, config, props);
976789Sahrens 
977789Sahrens 	nvlist_free(config);
978789Sahrens 
9795094Slling 	if (props)
9805094Slling 		nvlist_free(props);
9815094Slling 
982789Sahrens 	return (error);
983789Sahrens }
984789Sahrens 
985789Sahrens static int
986789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
987789Sahrens {
9884543Smarks 	int error;
9897214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
9908211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
9917214Slling 
9924543Smarks 	zfs_log_history(zc);
9938211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
99410588SEric.Taylor@Sun.COM 	if (error == 0)
99510588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
9964543Smarks 	return (error);
997789Sahrens }
998789Sahrens 
999789Sahrens static int
1000789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1001789Sahrens {
1002789Sahrens 	nvlist_t *configs;
1003789Sahrens 	int error;
1004789Sahrens 
1005789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1006789Sahrens 		return (EEXIST);
1007789Sahrens 
10082676Seschrock 	error = put_nvlist(zc, configs);
1009789Sahrens 
1010789Sahrens 	nvlist_free(configs);
1011789Sahrens 
1012789Sahrens 	return (error);
1013789Sahrens }
1014789Sahrens 
1015789Sahrens static int
1016789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1017789Sahrens {
1018789Sahrens 	nvlist_t *config;
1019789Sahrens 	int error;
10201544Seschrock 	int ret = 0;
1021789Sahrens 
10222676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10232676Seschrock 	    sizeof (zc->zc_value));
1024789Sahrens 
1025789Sahrens 	if (config != NULL) {
10262676Seschrock 		ret = put_nvlist(zc, config);
1027789Sahrens 		nvlist_free(config);
10281544Seschrock 
10291544Seschrock 		/*
10301544Seschrock 		 * The config may be present even if 'error' is non-zero.
10311544Seschrock 		 * In this case we return success, and preserve the real errno
10321544Seschrock 		 * in 'zc_cookie'.
10331544Seschrock 		 */
10341544Seschrock 		zc->zc_cookie = error;
1035789Sahrens 	} else {
10361544Seschrock 		ret = error;
1037789Sahrens 	}
1038789Sahrens 
10391544Seschrock 	return (ret);
1040789Sahrens }
1041789Sahrens 
1042789Sahrens /*
1043789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1044789Sahrens  * user land knows which devices are available and overall pool health.
1045789Sahrens  */
1046789Sahrens static int
1047789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1048789Sahrens {
1049789Sahrens 	nvlist_t *tryconfig, *config;
1050789Sahrens 	int error;
1051789Sahrens 
10525094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10539643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1054789Sahrens 		return (error);
1055789Sahrens 
1056789Sahrens 	config = spa_tryimport(tryconfig);
1057789Sahrens 
1058789Sahrens 	nvlist_free(tryconfig);
1059789Sahrens 
1060789Sahrens 	if (config == NULL)
1061789Sahrens 		return (EINVAL);
1062789Sahrens 
10632676Seschrock 	error = put_nvlist(zc, config);
1064789Sahrens 	nvlist_free(config);
1065789Sahrens 
1066789Sahrens 	return (error);
1067789Sahrens }
1068789Sahrens 
1069789Sahrens static int
1070789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1071789Sahrens {
1072789Sahrens 	spa_t *spa;
1073789Sahrens 	int error;
1074789Sahrens 
10752926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10762926Sek110237 		return (error);
10772926Sek110237 
10787046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
10792926Sek110237 
10802926Sek110237 	spa_close(spa, FTAG);
10812926Sek110237 
1082789Sahrens 	return (error);
1083789Sahrens }
1084789Sahrens 
1085789Sahrens static int
1086789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1087789Sahrens {
1088789Sahrens 	spa_t *spa;
1089789Sahrens 	int error;
1090789Sahrens 
1091789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1092789Sahrens 	if (error == 0) {
1093789Sahrens 		spa_freeze(spa);
1094789Sahrens 		spa_close(spa, FTAG);
1095789Sahrens 	}
1096789Sahrens 	return (error);
1097789Sahrens }
1098789Sahrens 
1099789Sahrens static int
11001760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11011760Seschrock {
11021760Seschrock 	spa_t *spa;
11031760Seschrock 	int error;
11041760Seschrock 
11052926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11062926Sek110237 		return (error);
11072926Sek110237 
11085118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11095118Slling 		spa_close(spa, FTAG);
11105118Slling 		return (EINVAL);
11115118Slling 	}
11125118Slling 
11135094Slling 	spa_upgrade(spa, zc->zc_cookie);
11142926Sek110237 	spa_close(spa, FTAG);
11152926Sek110237 
11162926Sek110237 	return (error);
11172926Sek110237 }
11182926Sek110237 
11192926Sek110237 static int
11202926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11212926Sek110237 {
11222926Sek110237 	spa_t *spa;
11232926Sek110237 	char *hist_buf;
11242926Sek110237 	uint64_t size;
11252926Sek110237 	int error;
11262926Sek110237 
11272926Sek110237 	if ((size = zc->zc_history_len) == 0)
11282926Sek110237 		return (EINVAL);
11292926Sek110237 
11302926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11312926Sek110237 		return (error);
11322926Sek110237 
11334577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11343863Sek110237 		spa_close(spa, FTAG);
11353863Sek110237 		return (ENOTSUP);
11363863Sek110237 	}
11373863Sek110237 
11382926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11392926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11402926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11419643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
11429643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
11439643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
11442926Sek110237 	}
11452926Sek110237 
11462926Sek110237 	spa_close(spa, FTAG);
11472926Sek110237 	kmem_free(hist_buf, size);
11482926Sek110237 	return (error);
11492926Sek110237 }
11502926Sek110237 
11512926Sek110237 static int
11523444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11533444Sek110237 {
11543444Sek110237 	int error;
11553444Sek110237 
11563912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11573444Sek110237 		return (error);
11583444Sek110237 
11593444Sek110237 	return (0);
11603444Sek110237 }
11613444Sek110237 
116210298SMatthew.Ahrens@Sun.COM /*
116310298SMatthew.Ahrens@Sun.COM  * inputs:
116410298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
116510298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
116610298SMatthew.Ahrens@Sun.COM  *
116710298SMatthew.Ahrens@Sun.COM  * outputs:
116810298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
116910298SMatthew.Ahrens@Sun.COM  */
11703444Sek110237 static int
11713444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11723444Sek110237 {
117310298SMatthew.Ahrens@Sun.COM 	objset_t *os;
11743444Sek110237 	int error;
11753444Sek110237 
117610298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
117710298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
11783444Sek110237 		return (error);
117910298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
118010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
118110298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
118210298SMatthew.Ahrens@Sun.COM 	}
118310298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
11843444Sek110237 	    sizeof (zc->zc_value));
118510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
11863444Sek110237 
11873444Sek110237 	return (error);
11883444Sek110237 }
11893444Sek110237 
11903444Sek110237 static int
1191789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1192789Sahrens {
1193789Sahrens 	spa_t *spa;
1194789Sahrens 	int error;
11956423Sgw25295 	nvlist_t *config, **l2cache, **spares;
11966423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1197789Sahrens 
1198789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1199789Sahrens 	if (error != 0)
1200789Sahrens 		return (error);
1201789Sahrens 
12025450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12039643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
12045450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12055450Sbrendan 	    &l2cache, &nl2cache);
12065450Sbrendan 
12076423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12086423Sgw25295 	    &spares, &nspares);
12096423Sgw25295 
12103912Slling 	/*
12113912Slling 	 * A root pool with concatenated devices is not supported.
12126423Sgw25295 	 * Thus, can not add a device to a root pool.
12136423Sgw25295 	 *
12146423Sgw25295 	 * Intent log device can not be added to a rootpool because
12156423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12166423Sgw25295 	 * can not be accessed during the mountroot time.
12176423Sgw25295 	 *
12186423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12193912Slling 	 */
12206423Sgw25295 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
12213912Slling 		spa_close(spa, FTAG);
12223912Slling 		return (EDOM);
12233912Slling 	}
12243912Slling 
12255450Sbrendan 	if (error == 0) {
1226789Sahrens 		error = spa_vdev_add(spa, config);
1227789Sahrens 		nvlist_free(config);
1228789Sahrens 	}
1229789Sahrens 	spa_close(spa, FTAG);
1230789Sahrens 	return (error);
1231789Sahrens }
1232789Sahrens 
1233789Sahrens static int
1234789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1235789Sahrens {
12362082Seschrock 	spa_t *spa;
12372082Seschrock 	int error;
12382082Seschrock 
12392082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12402082Seschrock 	if (error != 0)
12412082Seschrock 		return (error);
12422082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12432082Seschrock 	spa_close(spa, FTAG);
12442082Seschrock 	return (error);
1245789Sahrens }
1246789Sahrens 
1247789Sahrens static int
12484451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1249789Sahrens {
125010588SEric.Taylor@Sun.COM 	boolean_t nslock;
1251789Sahrens 	spa_t *spa;
1252789Sahrens 	int error;
12534451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1254789Sahrens 
12552926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1256789Sahrens 		return (error);
125710588SEric.Taylor@Sun.COM 	nslock = spa_uses_zvols(spa);
125810588SEric.Taylor@Sun.COM 	if (nslock)
125910588SEric.Taylor@Sun.COM 		mutex_enter(&spa_namespace_lock);
12604451Seschrock 	switch (zc->zc_cookie) {
12614451Seschrock 	case VDEV_STATE_ONLINE:
12624451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12634451Seschrock 		break;
12644451Seschrock 
12654451Seschrock 	case VDEV_STATE_OFFLINE:
12664451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12674451Seschrock 		break;
1268789Sahrens 
12694451Seschrock 	case VDEV_STATE_FAULTED:
12704451Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12714451Seschrock 		break;
1272789Sahrens 
12734451Seschrock 	case VDEV_STATE_DEGRADED:
12744451Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12754451Seschrock 		break;
12764451Seschrock 
12774451Seschrock 	default:
12784451Seschrock 		error = EINVAL;
12794451Seschrock 	}
128010588SEric.Taylor@Sun.COM 	if (nslock)
128110588SEric.Taylor@Sun.COM 		mutex_exit(&spa_namespace_lock);
12824451Seschrock 	zc->zc_cookie = newstate;
1283789Sahrens 	spa_close(spa, FTAG);
1284789Sahrens 	return (error);
1285789Sahrens }
1286789Sahrens 
1287789Sahrens static int
1288789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1289789Sahrens {
1290789Sahrens 	spa_t *spa;
1291789Sahrens 	int replacing = zc->zc_cookie;
1292789Sahrens 	nvlist_t *config;
1293789Sahrens 	int error;
1294789Sahrens 
12952926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1296789Sahrens 		return (error);
1297789Sahrens 
12985094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12999643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
13001544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1301789Sahrens 		nvlist_free(config);
1302789Sahrens 	}
1303789Sahrens 
1304789Sahrens 	spa_close(spa, FTAG);
1305789Sahrens 	return (error);
1306789Sahrens }
1307789Sahrens 
1308789Sahrens static int
1309789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1310789Sahrens {
1311789Sahrens 	spa_t *spa;
1312789Sahrens 	int error;
1313789Sahrens 
13142926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1315789Sahrens 		return (error);
1316789Sahrens 
13178241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1318789Sahrens 
1319789Sahrens 	spa_close(spa, FTAG);
1320789Sahrens 	return (error);
1321789Sahrens }
1322789Sahrens 
1323789Sahrens static int
13241354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13251354Seschrock {
13261354Seschrock 	spa_t *spa;
13272676Seschrock 	char *path = zc->zc_value;
13281544Seschrock 	uint64_t guid = zc->zc_guid;
13291354Seschrock 	int error;
13301354Seschrock 
13311354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13321354Seschrock 	if (error != 0)
13331354Seschrock 		return (error);
13341354Seschrock 
13351354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13361354Seschrock 	spa_close(spa, FTAG);
13371354Seschrock 	return (error);
13381354Seschrock }
13391354Seschrock 
13409425SEric.Schrock@Sun.COM static int
13419425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13429425SEric.Schrock@Sun.COM {
13439425SEric.Schrock@Sun.COM 	spa_t *spa;
13449425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
13459425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
13469425SEric.Schrock@Sun.COM 	int error;
13479425SEric.Schrock@Sun.COM 
13489425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
13499425SEric.Schrock@Sun.COM 	if (error != 0)
13509425SEric.Schrock@Sun.COM 		return (error);
13519425SEric.Schrock@Sun.COM 
13529425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
13539425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
13549425SEric.Schrock@Sun.COM 	return (error);
13559425SEric.Schrock@Sun.COM }
13569425SEric.Schrock@Sun.COM 
13575367Sahrens /*
13585367Sahrens  * inputs:
13595367Sahrens  * zc_name		name of filesystem
13605367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13615367Sahrens  *
13625367Sahrens  * outputs:
13635367Sahrens  * zc_objset_stats	stats
13645367Sahrens  * zc_nvlist_dst	property nvlist
13655367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13665367Sahrens  */
13671354Seschrock static int
1368789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1369789Sahrens {
1370789Sahrens 	objset_t *os = NULL;
1371789Sahrens 	int error;
13721356Seschrock 	nvlist_t *nv;
1373789Sahrens 
137410298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1375789Sahrens 		return (error);
1376789Sahrens 
13772885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1378789Sahrens 
13792856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13806689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13812885Sahrens 		dmu_objset_stats(os, nv);
13823087Sahrens 		/*
13835147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13843087Sahrens 		 * which we aren't supposed to do with a
13856689Smaybee 		 * DS_MODE_USER hold, because it could be
13863087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
138710298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
13883087Sahrens 		 */
13894577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13904577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13914577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13924577Sahrens 		}
13932676Seschrock 		error = put_nvlist(zc, nv);
13941356Seschrock 		nvlist_free(nv);
13951356Seschrock 	}
1396789Sahrens 
139710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1398789Sahrens 	return (error);
1399789Sahrens }
1400789Sahrens 
14015498Stimh static int
14025498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
14035498Stimh {
14045498Stimh 	uint64_t value;
14055498Stimh 	int error;
14065498Stimh 
14075498Stimh 	/*
14085498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14095498Stimh 	 * the default value (if there is one).
14105498Stimh 	 */
14115498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14125498Stimh 		return (error);
14135498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14145498Stimh 	return (0);
14155498Stimh }
14165498Stimh 
14175498Stimh /*
14185498Stimh  * inputs:
14195498Stimh  * zc_name		name of filesystem
14205498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14215498Stimh  *
14225498Stimh  * outputs:
14235498Stimh  * zc_nvlist_dst	zpl property nvlist
14245498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14255498Stimh  */
14265498Stimh static int
14275498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14285498Stimh {
14295498Stimh 	objset_t *os;
14305498Stimh 	int err;
14315498Stimh 
143210298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
143310298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
14345498Stimh 		return (err);
14355498Stimh 
14365498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14375498Stimh 
14385498Stimh 	/*
14395498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14406689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14416689Smaybee 	 * hold, because it could be inconsistent.
14425498Stimh 	 */
14435498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14445498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14455498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14465498Stimh 		nvlist_t *nv;
14475498Stimh 
14485498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14495498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14505498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14515498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14525498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14535498Stimh 			err = put_nvlist(zc, nv);
14545498Stimh 		nvlist_free(nv);
14555498Stimh 	} else {
14565498Stimh 		err = ENOENT;
14575498Stimh 	}
145810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
14595498Stimh 	return (err);
14605498Stimh }
14615498Stimh 
14629396SMatthew.Ahrens@Sun.COM static boolean_t
14639396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14649396SMatthew.Ahrens@Sun.COM {
14659396SMatthew.Ahrens@Sun.COM 	/*
14669396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14679396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14689396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14699396SMatthew.Ahrens@Sun.COM 	 */
14709396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14719396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14729396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14739396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14749396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14759396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14769396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14779396SMatthew.Ahrens@Sun.COM }
14789396SMatthew.Ahrens@Sun.COM 
14795367Sahrens /*
14805367Sahrens  * inputs:
14815367Sahrens  * zc_name		name of filesystem
14825367Sahrens  * zc_cookie		zap cursor
14835367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14845367Sahrens  *
14855367Sahrens  * outputs:
14865367Sahrens  * zc_name		name of next filesystem
14879396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14885367Sahrens  * zc_objset_stats	stats
14895367Sahrens  * zc_nvlist_dst	property nvlist
14905367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14915367Sahrens  */
1492789Sahrens static int
1493789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1494789Sahrens {
1495885Sahrens 	objset_t *os;
1496789Sahrens 	int error;
1497789Sahrens 	char *p;
1498789Sahrens 
149910298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1500885Sahrens 		if (error == ENOENT)
1501885Sahrens 			error = ESRCH;
1502885Sahrens 		return (error);
1503789Sahrens 	}
1504789Sahrens 
1505789Sahrens 	p = strrchr(zc->zc_name, '/');
1506789Sahrens 	if (p == NULL || p[1] != '\0')
1507789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1508789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1509789Sahrens 
15108697SRichard.Morris@Sun.COM 	/*
15118697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15128697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15138697SRichard.Morris@Sun.COM 	 */
15148415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15158415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15168415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15178415SRichard.Morris@Sun.COM 
15188415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15198697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15208415SRichard.Morris@Sun.COM 	}
15218415SRichard.Morris@Sun.COM 
1522789Sahrens 	do {
1523885Sahrens 		error = dmu_dir_list_next(os,
1524885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1525885Sahrens 		    NULL, &zc->zc_cookie);
1526789Sahrens 		if (error == ENOENT)
1527789Sahrens 			error = ESRCH;
152810588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
152910588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
153010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1531789Sahrens 
153210588SEric.Taylor@Sun.COM 	/*
153310588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
153410588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
153510588SEric.Taylor@Sun.COM 	 */
153610588SEric.Taylor@Sun.COM 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1537885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1538789Sahrens 	return (error);
1539789Sahrens }
1540789Sahrens 
15415367Sahrens /*
15425367Sahrens  * inputs:
15435367Sahrens  * zc_name		name of filesystem
15445367Sahrens  * zc_cookie		zap cursor
15455367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15465367Sahrens  *
15475367Sahrens  * outputs:
15485367Sahrens  * zc_name		name of next snapshot
15495367Sahrens  * zc_objset_stats	stats
15505367Sahrens  * zc_nvlist_dst	property nvlist
15515367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15525367Sahrens  */
1553789Sahrens static int
1554789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1555789Sahrens {
1556885Sahrens 	objset_t *os;
1557789Sahrens 	int error;
1558789Sahrens 
155910474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
156010474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
156110474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
156210474SRichard.Morris@Sun.COM 
156310298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
15646689Smaybee 	if (error)
15656689Smaybee 		return (error == ENOENT ? ESRCH : error);
1566789Sahrens 
15671003Slling 	/*
15681003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15691003Slling 	 * so exit immediately.
15701003Slling 	 */
15711003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
157210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
15731003Slling 		return (ESRCH);
1574789Sahrens 	}
1575789Sahrens 
1576885Sahrens 	error = dmu_snapshot_list_next(os,
1577885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15785663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
157910298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1580885Sahrens 	if (error == 0)
1581885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15826689Smaybee 	else if (error == ENOENT)
15836689Smaybee 		error = ESRCH;
1584789Sahrens 
15855367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15866689Smaybee 	if (error)
15875367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1588789Sahrens 	return (error);
1589789Sahrens }
1590789Sahrens 
15916423Sgw25295 int
15924787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1593789Sahrens {
15942676Seschrock 	nvpair_t *elem;
15958724SRichard.Morris@Sun.COM 	int error = 0;
15962676Seschrock 	uint64_t intval;
15972676Seschrock 	char *strval;
15988697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
15999396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
16002676Seschrock 
16014543Smarks 	/*
16024543Smarks 	 * First validate permission to set all of the properties
16034543Smarks 	 */
16042676Seschrock 	elem = NULL;
16052676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16064670Sahrens 		const char *propname = nvpair_name(elem);
16074670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16082676Seschrock 
16095094Slling 		if (prop == ZPROP_INVAL) {
16102676Seschrock 			/*
16112676Seschrock 			 * If this is a user-defined property, it must be a
16122676Seschrock 			 * string, and there is no further validation to do.
16132676Seschrock 			 */
16149396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16159396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16169396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16179396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16189396SMatthew.Ahrens@Sun.COM 					return (error);
16199396SMatthew.Ahrens@Sun.COM 				continue;
16209396SMatthew.Ahrens@Sun.COM 			}
16219396SMatthew.Ahrens@Sun.COM 
16229396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16239396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16249396SMatthew.Ahrens@Sun.COM 				const char *perm;
16259396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16269396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16279396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16289396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16299396SMatthew.Ahrens@Sun.COM 				else
16309396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16319396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16329396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16339396SMatthew.Ahrens@Sun.COM 					return (error);
16349396SMatthew.Ahrens@Sun.COM 				continue;
16359396SMatthew.Ahrens@Sun.COM 			}
16369396SMatthew.Ahrens@Sun.COM 
16379396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16382676Seschrock 		}
16392676Seschrock 
16409396SMatthew.Ahrens@Sun.COM 		if (issnap)
16419396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16429396SMatthew.Ahrens@Sun.COM 
16434787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16444670Sahrens 			return (error);
16452676Seschrock 
16464670Sahrens 		/*
16474670Sahrens 		 * Check that this value is valid for this pool version
16484670Sahrens 		 */
16494670Sahrens 		switch (prop) {
16503886Sahl 		case ZFS_PROP_COMPRESSION:
16513886Sahl 			/*
16523886Sahl 			 * If the user specified gzip compression, make sure
16533886Sahl 			 * the SPA supports it. We ignore any errors here since
16543886Sahl 			 * we'll catch them later.
16553886Sahl 			 */
16563886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16577042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16587042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16597042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16607184Stimh 				    zfs_earlier_version(name,
16615331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16625331Samw 					return (ENOTSUP);
16637042Sgw25295 
16647042Sgw25295 				/*
16657042Sgw25295 				 * If this is a bootable dataset then
16667042Sgw25295 				 * verify that the compression algorithm
16677042Sgw25295 				 * is supported for booting. We must return
16687042Sgw25295 				 * something other than ENOTSUP since it
16697042Sgw25295 				 * implies a downrev pool version.
16707042Sgw25295 				 */
16717042Sgw25295 				if (zfs_is_bootfs(name) &&
16727042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16737042Sgw25295 					return (ERANGE);
16743886Sahl 			}
16753886Sahl 			break;
16764603Sahrens 
16774603Sahrens 		case ZFS_PROP_COPIES:
16789396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16795331Samw 				return (ENOTSUP);
16804603Sahrens 			break;
16815977Smarks 
16825977Smarks 		case ZFS_PROP_SHARESMB:
16836689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16845977Smarks 				return (ENOTSUP);
16855977Smarks 			break;
16868053SMark.Shellenbaum@Sun.COM 
16878053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
16888053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16898053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
16908053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
16918053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
16928053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
16938053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
16944603Sahrens 		}
16954543Smarks 	}
16964543Smarks 
16978697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16984543Smarks 	elem = NULL;
16994543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
17004670Sahrens 		const char *propname = nvpair_name(elem);
17014670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
17024543Smarks 
17035094Slling 		if (prop == ZPROP_INVAL) {
17049396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
17059396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
17069396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
17079396SMatthew.Ahrens@Sun.COM 				const char *domain;
17089396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
17099396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17109396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17119396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17129396SMatthew.Ahrens@Sun.COM 
17139396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17149396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17159396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17169396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17179396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17189396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
17199396SMatthew.Ahrens@Sun.COM 				domain = propname +
17209396SMatthew.Ahrens@Sun.COM 				    strlen(zfs_userquota_prop_prefixes[type]);
17219396SMatthew.Ahrens@Sun.COM 
172210298SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, FTAG, &zfsvfs);
17239396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17249396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17259396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17269396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17279396SMatthew.Ahrens@Sun.COM 				}
17289396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17299396SMatthew.Ahrens@Sun.COM 					continue;
17309396SMatthew.Ahrens@Sun.COM 				else
17319396SMatthew.Ahrens@Sun.COM 					goto out;
17329396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17339396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17349396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17359396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17369396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17379396SMatthew.Ahrens@Sun.COM 					continue;
17389396SMatthew.Ahrens@Sun.COM 				else
17399396SMatthew.Ahrens@Sun.COM 					goto out;
17409396SMatthew.Ahrens@Sun.COM 			}
17414543Smarks 		}
17422676Seschrock 
17432676Seschrock 		switch (prop) {
17442676Seschrock 		case ZFS_PROP_QUOTA:
17452676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17464577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17478697SRichard.Morris@Sun.COM 				goto out;
17482676Seschrock 			break;
17492676Seschrock 
17505378Sck153898 		case ZFS_PROP_REFQUOTA:
17515378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17525378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17538697SRichard.Morris@Sun.COM 				goto out;
17545378Sck153898 			break;
17555378Sck153898 
17562676Seschrock 		case ZFS_PROP_RESERVATION:
17572676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17582676Seschrock 			    (error = dsl_dir_set_reservation(name,
17592676Seschrock 			    intval)) != 0)
17608697SRichard.Morris@Sun.COM 				goto out;
17612676Seschrock 			break;
1762789Sahrens 
17635378Sck153898 		case ZFS_PROP_REFRESERVATION:
17645378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17655378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17665378Sck153898 			    intval)) != 0)
17678697SRichard.Morris@Sun.COM 				goto out;
17685378Sck153898 			break;
17695378Sck153898 
17702676Seschrock 		case ZFS_PROP_VOLSIZE:
17712676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17724787Sahrens 			    (error = zvol_set_volsize(name,
17734787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17748697SRichard.Morris@Sun.COM 				goto out;
17752676Seschrock 			break;
17762676Seschrock 
17774577Sahrens 		case ZFS_PROP_VERSION:
17789396SMatthew.Ahrens@Sun.COM 		{
17799396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17809396SMatthew.Ahrens@Sun.COM 
17819396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17829396SMatthew.Ahrens@Sun.COM 				goto out;
178310298SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0)
17849396SMatthew.Ahrens@Sun.COM 				goto out;
17859396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
17869396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
17879396SMatthew.Ahrens@Sun.COM 
17889396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
17899396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
17909396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
17919396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
17929396SMatthew.Ahrens@Sun.COM 			}
17939396SMatthew.Ahrens@Sun.COM 			if (error)
17948697SRichard.Morris@Sun.COM 				goto out;
17952676Seschrock 			break;
17969396SMatthew.Ahrens@Sun.COM 		}
17972676Seschrock 
17982676Seschrock 		default:
17992676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
18002676Seschrock 				if (zfs_prop_get_type(prop) !=
18018697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18028697SRichard.Morris@Sun.COM 					error = EINVAL;
18038697SRichard.Morris@Sun.COM 					goto out;
18048697SRichard.Morris@Sun.COM 				}
18052676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18062885Sahrens 				const char *unused;
18072885Sahrens 
18082717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18092676Seschrock 
18102676Seschrock 				switch (zfs_prop_get_type(prop)) {
18114787Sahrens 				case PROP_TYPE_NUMBER:
18122676Seschrock 					break;
18134787Sahrens 				case PROP_TYPE_STRING:
18148697SRichard.Morris@Sun.COM 					error = EINVAL;
18158697SRichard.Morris@Sun.COM 					goto out;
18164787Sahrens 				case PROP_TYPE_INDEX:
18172717Seschrock 					if (zfs_prop_index_to_string(prop,
18188697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18198697SRichard.Morris@Sun.COM 						error = EINVAL;
18208697SRichard.Morris@Sun.COM 						goto out;
18218697SRichard.Morris@Sun.COM 					}
18222676Seschrock 					break;
18232676Seschrock 				default:
18244577Sahrens 					cmn_err(CE_PANIC,
18254577Sahrens 					    "unknown property type");
18262676Seschrock 					break;
18272676Seschrock 				}
18282676Seschrock 			} else {
18298697SRichard.Morris@Sun.COM 				error = EINVAL;
18308697SRichard.Morris@Sun.COM 				goto out;
18312676Seschrock 			}
18328697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18338697SRichard.Morris@Sun.COM 				goto out;
18342676Seschrock 		}
18352676Seschrock 	}
18362676Seschrock 
18378697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18388697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18398697SRichard.Morris@Sun.COM 	}
18408697SRichard.Morris@Sun.COM out:
18418697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18428697SRichard.Morris@Sun.COM 	return (error);
1843789Sahrens }
1844789Sahrens 
18455367Sahrens /*
18469355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18479355SMatthew.Ahrens@Sun.COM  */
18489355SMatthew.Ahrens@Sun.COM static int
18499355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18509355SMatthew.Ahrens@Sun.COM {
18519355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18529355SMatthew.Ahrens@Sun.COM 	int error = 0;
18539355SMatthew.Ahrens@Sun.COM 
18549355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18559355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18569355SMatthew.Ahrens@Sun.COM 		char *valstr;
18579355SMatthew.Ahrens@Sun.COM 
18589355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18599355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18609355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18619355SMatthew.Ahrens@Sun.COM 
18629355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18639355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18649355SMatthew.Ahrens@Sun.COM 			return (error);
18659355SMatthew.Ahrens@Sun.COM 
18669355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18679355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18689355SMatthew.Ahrens@Sun.COM 
18699355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18709355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18719355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18729355SMatthew.Ahrens@Sun.COM 	}
18739355SMatthew.Ahrens@Sun.COM 	return (0);
18749355SMatthew.Ahrens@Sun.COM }
18759355SMatthew.Ahrens@Sun.COM 
18769355SMatthew.Ahrens@Sun.COM /*
18775367Sahrens  * inputs:
18785367Sahrens  * zc_name		name of filesystem
18798697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18805367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18817265Sahrens  * zc_cookie		clear existing local props?
18825367Sahrens  *
18835367Sahrens  * outputs:		none
18845367Sahrens  */
1885789Sahrens static int
18862676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1887789Sahrens {
18882676Seschrock 	nvlist_t *nvl;
18892676Seschrock 	int error;
1890789Sahrens 
18915094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
18929643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
18932676Seschrock 		return (error);
18942676Seschrock 
18957265Sahrens 	if (zc->zc_cookie) {
18967265Sahrens 		nvlist_t *origprops;
18977265Sahrens 		objset_t *os;
18987265Sahrens 
189910298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
19007265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19018536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19027265Sahrens 				nvlist_free(origprops);
19037265Sahrens 			}
190410298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
19057265Sahrens 		}
19067265Sahrens 
19077265Sahrens 	}
19087265Sahrens 
19094787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19104543Smarks 
19112676Seschrock 	nvlist_free(nvl);
19122676Seschrock 	return (error);
1913789Sahrens }
1914789Sahrens 
19155367Sahrens /*
19165367Sahrens  * inputs:
19175367Sahrens  * zc_name		name of filesystem
19185367Sahrens  * zc_value		name of property to inherit
19195367Sahrens  *
19205367Sahrens  * outputs:		none
19215367Sahrens  */
1922789Sahrens static int
19234849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19244849Sahrens {
19254849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19264849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19274849Sahrens }
19284849Sahrens 
19294849Sahrens static int
19304098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19313912Slling {
19325094Slling 	nvlist_t *props;
19333912Slling 	spa_t *spa;
19345094Slling 	int error;
19358525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19363912Slling 
19375094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19389643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)))
19393912Slling 		return (error);
19403912Slling 
19418525SEric.Schrock@Sun.COM 	/*
19428525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19438525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19448525SEric.Schrock@Sun.COM 	 */
19458525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19468525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19478525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19488525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19498525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19508525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19518525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19528525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19538525SEric.Schrock@Sun.COM 		}
19548525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
1955*10672SEric.Schrock@Sun.COM 		if (spa != NULL) {
1956*10672SEric.Schrock@Sun.COM 			nvlist_free(props);
19578525SEric.Schrock@Sun.COM 			return (0);
1958*10672SEric.Schrock@Sun.COM 		}
19598525SEric.Schrock@Sun.COM 	}
19608525SEric.Schrock@Sun.COM 
19613912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19625094Slling 		nvlist_free(props);
19633912Slling 		return (error);
19643912Slling 	}
19653912Slling 
19665094Slling 	error = spa_prop_set(spa, props);
19673912Slling 
19685094Slling 	nvlist_free(props);
19693912Slling 	spa_close(spa, FTAG);
19703912Slling 
19713912Slling 	return (error);
19723912Slling }
19733912Slling 
19743912Slling static int
19754098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19763912Slling {
19773912Slling 	spa_t *spa;
19783912Slling 	int error;
19793912Slling 	nvlist_t *nvp = NULL;
19803912Slling 
19818525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19828525SEric.Schrock@Sun.COM 		/*
19838525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
19848525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
19858525SEric.Schrock@Sun.COM 		 * anyway.
19868525SEric.Schrock@Sun.COM 		 */
19878525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19888525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
19898525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
19908525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
19918525SEric.Schrock@Sun.COM 	} else {
19928525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
19938525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
19948525SEric.Schrock@Sun.COM 	}
19953912Slling 
19963912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
19973912Slling 		error = put_nvlist(zc, nvp);
19983912Slling 	else
19993912Slling 		error = EFAULT;
20003912Slling 
20018525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
20023912Slling 	return (error);
20033912Slling }
20043912Slling 
20053912Slling static int
20064543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20074543Smarks {
20084543Smarks 	nvlist_t *nvp;
20094543Smarks 	int error;
20104543Smarks 	uint32_t uid;
20114543Smarks 	uint32_t gid;
20124543Smarks 	uint32_t *groups;
20134543Smarks 	uint_t group_cnt;
20144543Smarks 	cred_t	*usercred;
20154543Smarks 
20165094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20179643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
20184543Smarks 		return (error);
20194543Smarks 	}
20204543Smarks 
20214543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20224543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20234543Smarks 		nvlist_free(nvp);
20244543Smarks 		return (EPERM);
20254543Smarks 	}
20264543Smarks 
20274543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20284543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20294543Smarks 		nvlist_free(nvp);
20304543Smarks 		return (EPERM);
20314543Smarks 	}
20324543Smarks 
20334543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20344543Smarks 	    &groups, &group_cnt)) != 0) {
20354543Smarks 		nvlist_free(nvp);
20364543Smarks 		return (EPERM);
20374543Smarks 	}
20384543Smarks 	usercred = cralloc();
20394543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20404543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20414543Smarks 		nvlist_free(nvp);
20424543Smarks 		crfree(usercred);
20434543Smarks 		return (EPERM);
20444543Smarks 	}
20454543Smarks 	nvlist_free(nvp);
20464543Smarks 	error = dsl_deleg_access(zc->zc_name,
20474787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20484543Smarks 	crfree(usercred);
20494543Smarks 	return (error);
20504543Smarks }
20514543Smarks 
20525367Sahrens /*
20535367Sahrens  * inputs:
20545367Sahrens  * zc_name		name of filesystem
20555367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20565367Sahrens  * zc_perm_action	allow/unallow flag
20575367Sahrens  *
20585367Sahrens  * outputs:		none
20595367Sahrens  */
20604543Smarks static int
20614543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20624543Smarks {
20634543Smarks 	int error;
20644543Smarks 	nvlist_t *fsaclnv = NULL;
20654543Smarks 
20665094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20679643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
20684543Smarks 		return (error);
20694543Smarks 
20704543Smarks 	/*
20714543Smarks 	 * Verify nvlist is constructed correctly
20724543Smarks 	 */
20734543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20744543Smarks 		nvlist_free(fsaclnv);
20754543Smarks 		return (EINVAL);
20764543Smarks 	}
20774543Smarks 
20784543Smarks 	/*
20794543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20804543Smarks 	 * that user is allowed to hand out each permission in
20814543Smarks 	 * the nvlist(s)
20824543Smarks 	 */
20834543Smarks 
20844787Sahrens 	error = secpolicy_zfs(CRED());
20854543Smarks 	if (error) {
20864787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
20874787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
20884787Sahrens 			    fsaclnv, CRED());
20894787Sahrens 		} else {
20904787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
20914787Sahrens 			    fsaclnv, CRED());
20924787Sahrens 		}
20934543Smarks 	}
20944543Smarks 
20954543Smarks 	if (error == 0)
20964543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
20974543Smarks 
20984543Smarks 	nvlist_free(fsaclnv);
20994543Smarks 	return (error);
21004543Smarks }
21014543Smarks 
21025367Sahrens /*
21035367Sahrens  * inputs:
21045367Sahrens  * zc_name		name of filesystem
21055367Sahrens  *
21065367Sahrens  * outputs:
21075367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21085367Sahrens  */
21094543Smarks static int
21104543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21114543Smarks {
21124543Smarks 	nvlist_t *nvp;
21134543Smarks 	int error;
21144543Smarks 
21154543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21164543Smarks 		error = put_nvlist(zc, nvp);
21174543Smarks 		nvlist_free(nvp);
21184543Smarks 	}
21194543Smarks 
21204543Smarks 	return (error);
21214543Smarks }
21224543Smarks 
21235367Sahrens /*
2124789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2125789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2126789Sahrens  * is responsible for releasing the returned vfs pointer.
2127789Sahrens  */
2128789Sahrens static vfs_t *
2129789Sahrens zfs_get_vfs(const char *resource)
2130789Sahrens {
2131789Sahrens 	struct vfs *vfsp;
2132789Sahrens 	struct vfs *vfs_found = NULL;
2133789Sahrens 
2134789Sahrens 	vfs_list_read_lock();
2135789Sahrens 	vfsp = rootvfs;
2136789Sahrens 	do {
2137789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2138789Sahrens 			VFS_HOLD(vfsp);
2139789Sahrens 			vfs_found = vfsp;
2140789Sahrens 			break;
2141789Sahrens 		}
2142789Sahrens 		vfsp = vfsp->vfs_next;
2143789Sahrens 	} while (vfsp != rootvfs);
2144789Sahrens 	vfs_list_unlock();
2145789Sahrens 	return (vfs_found);
2146789Sahrens }
2147789Sahrens 
21484543Smarks /* ARGSUSED */
2149789Sahrens static void
21504543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2151789Sahrens {
21525331Samw 	zfs_creat_t *zct = arg;
21535498Stimh 
21545498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21555331Samw }
21565331Samw 
21575498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21585498Stimh 
21595331Samw /*
21605498Stimh  * inputs:
21617184Stimh  * createprops		list of properties requested by creator
21627184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21637184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21647184Stimh  * os			parent objset pointer (NULL if root fs)
21655331Samw  *
21665498Stimh  * outputs:
21675498Stimh  * zplprops	values for the zplprops we attach to the master node object
21687184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21695331Samw  *
21705498Stimh  * Determine the settings for utf8only, normalization and
21715498Stimh  * casesensitivity.  Specific values may have been requested by the
21725498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21735498Stimh  * the file system is of too early a vintage, a creator can not
21745498Stimh  * request settings for these properties, even if the requested
21755498Stimh  * setting is the default value.  We don't actually want to create dsl
21765498Stimh  * properties for these, so remove them from the source nvlist after
21775498Stimh  * processing.
21785331Samw  */
21795331Samw static int
21809396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
21817184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
21827184Stimh     boolean_t *is_ci)
21835331Samw {
21845498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
21855498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
21865498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
21875498Stimh 
21885498Stimh 	ASSERT(zplprops != NULL);
21895498Stimh 
21905375Stimh 	/*
21915498Stimh 	 * Pull out creator prop choices, if any.
21925375Stimh 	 */
21935498Stimh 	if (createprops) {
21945498Stimh 		(void) nvlist_lookup_uint64(createprops,
21957184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
21967184Stimh 		(void) nvlist_lookup_uint64(createprops,
21975498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
21985498Stimh 		(void) nvlist_remove_all(createprops,
21995498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22005498Stimh 		(void) nvlist_lookup_uint64(createprops,
22015498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22025498Stimh 		(void) nvlist_remove_all(createprops,
22035498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22045498Stimh 		(void) nvlist_lookup_uint64(createprops,
22055498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22065498Stimh 		(void) nvlist_remove_all(createprops,
22075498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22085331Samw 	}
22095331Samw 
22105375Stimh 	/*
22117184Stimh 	 * If the zpl version requested is whacky or the file system
22127184Stimh 	 * or pool is version is too "young" to support normalization
22137184Stimh 	 * and the creator tried to set a value for one of the props,
22147184Stimh 	 * error out.
22155498Stimh 	 */
22167184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22177184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22187184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22195498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22207184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22215498Stimh 		return (ENOTSUP);
22225498Stimh 
22235498Stimh 	/*
22245498Stimh 	 * Put the version in the zplprops
22255498Stimh 	 */
22265498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22275498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22285498Stimh 
22295498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22305498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22315498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22325498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22335498Stimh 
22345498Stimh 	/*
22355498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22365498Stimh 	 */
22375498Stimh 	if (norm)
22385498Stimh 		u8 = 1;
22395498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22405498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22415498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22425498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22435498Stimh 
22445498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22455498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22465498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22475498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22485498Stimh 
22496492Stimh 	if (is_ci)
22506492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22516492Stimh 
22527184Stimh 	return (0);
22537184Stimh }
22547184Stimh 
22557184Stimh static int
22567184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22577184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22587184Stimh {
22597184Stimh 	boolean_t fuids_ok = B_TRUE;
22607184Stimh 	uint64_t zplver = ZPL_VERSION;
22617184Stimh 	objset_t *os = NULL;
22627184Stimh 	char parentname[MAXNAMELEN];
22637184Stimh 	char *cp;
22647184Stimh 	int error;
22657184Stimh 
22667184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22677184Stimh 	cp = strrchr(parentname, '/');
22687184Stimh 	ASSERT(cp != NULL);
22697184Stimh 	cp[0] = '\0';
22707184Stimh 
22719396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
22729396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22737184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22747184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22757184Stimh 		fuids_ok = B_FALSE;
22767184Stimh 	}
22777184Stimh 
22787184Stimh 	/*
22797184Stimh 	 * Open parent object set so we can inherit zplprop values.
22807184Stimh 	 */
228110298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
22827184Stimh 		return (error);
22837184Stimh 
22847184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
22857184Stimh 	    zplprops, is_ci);
228610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
22877184Stimh 	return (error);
22887184Stimh }
22897184Stimh 
22907184Stimh static int
22917184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
22927184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22937184Stimh {
22947184Stimh 	boolean_t fuids_ok = B_TRUE;
22957184Stimh 	uint64_t zplver = ZPL_VERSION;
22967184Stimh 	int error;
22977184Stimh 
22987184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
22997184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23007184Stimh 		fuids_ok = B_FALSE;
23017184Stimh 	}
23027184Stimh 
23037184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23047184Stimh 	    zplprops, is_ci);
23057184Stimh 	return (error);
2306789Sahrens }
2307789Sahrens 
23085367Sahrens /*
23095367Sahrens  * inputs:
23105367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23115367Sahrens  * zc_name		name of new objset
23125367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23135367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23145367Sahrens  *
23155498Stimh  * outputs: none
23165367Sahrens  */
2317789Sahrens static int
2318789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2319789Sahrens {
2320789Sahrens 	objset_t *clone;
2321789Sahrens 	int error = 0;
23225331Samw 	zfs_creat_t zct;
23234543Smarks 	nvlist_t *nvprops = NULL;
23244543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2325789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2326789Sahrens 
2327789Sahrens 	switch (type) {
2328789Sahrens 
2329789Sahrens 	case DMU_OST_ZFS:
2330789Sahrens 		cbfunc = zfs_create_cb;
2331789Sahrens 		break;
2332789Sahrens 
2333789Sahrens 	case DMU_OST_ZVOL:
2334789Sahrens 		cbfunc = zvol_create_cb;
2335789Sahrens 		break;
2336789Sahrens 
2337789Sahrens 	default:
23382199Sahrens 		cbfunc = NULL;
23396423Sgw25295 		break;
23402199Sahrens 	}
23415326Sek110237 	if (strchr(zc->zc_name, '@') ||
23425326Sek110237 	    strchr(zc->zc_name, '%'))
2343789Sahrens 		return (EINVAL);
2344789Sahrens 
23452676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23465094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23479643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
23482676Seschrock 		return (error);
23492676Seschrock 
23505498Stimh 	zct.zct_zplprops = NULL;
23515331Samw 	zct.zct_props = nvprops;
23525331Samw 
23532676Seschrock 	if (zc->zc_value[0] != '\0') {
2354789Sahrens 		/*
2355789Sahrens 		 * We're creating a clone of an existing snapshot.
2356789Sahrens 		 */
23572676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23582676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23594543Smarks 			nvlist_free(nvprops);
2360789Sahrens 			return (EINVAL);
23612676Seschrock 		}
2362789Sahrens 
236310298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
23642676Seschrock 		if (error) {
23654543Smarks 			nvlist_free(nvprops);
2366789Sahrens 			return (error);
23672676Seschrock 		}
23686492Stimh 
236910272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
237010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
23715331Samw 		if (error) {
23725331Samw 			nvlist_free(nvprops);
23735331Samw 			return (error);
23745331Samw 		}
2375789Sahrens 	} else {
23766492Stimh 		boolean_t is_insensitive = B_FALSE;
23776492Stimh 
23782676Seschrock 		if (cbfunc == NULL) {
23794543Smarks 			nvlist_free(nvprops);
23802199Sahrens 			return (EINVAL);
23812676Seschrock 		}
23822676Seschrock 
2383789Sahrens 		if (type == DMU_OST_ZVOL) {
23842676Seschrock 			uint64_t volsize, volblocksize;
23852676Seschrock 
23864543Smarks 			if (nvprops == NULL ||
23874543Smarks 			    nvlist_lookup_uint64(nvprops,
23882676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
23892676Seschrock 			    &volsize) != 0) {
23904543Smarks 				nvlist_free(nvprops);
23912676Seschrock 				return (EINVAL);
23922676Seschrock 			}
23932676Seschrock 
23944543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
23952676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
23962676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
23974543Smarks 				nvlist_free(nvprops);
23982676Seschrock 				return (EINVAL);
23992676Seschrock 			}
24001133Seschrock 
24012676Seschrock 			if (error != 0)
24022676Seschrock 				volblocksize = zfs_prop_default_numeric(
24032676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24042676Seschrock 
24052676Seschrock 			if ((error = zvol_check_volblocksize(
24062676Seschrock 			    volblocksize)) != 0 ||
24072676Seschrock 			    (error = zvol_check_volsize(volsize,
24082676Seschrock 			    volblocksize)) != 0) {
24094543Smarks 				nvlist_free(nvprops);
2410789Sahrens 				return (error);
24112676Seschrock 			}
24124577Sahrens 		} else if (type == DMU_OST_ZFS) {
24135331Samw 			int error;
24145331Samw 
24155498Stimh 			/*
24165331Samw 			 * We have to have normalization and
24175331Samw 			 * case-folding flags correct when we do the
24185331Samw 			 * file system creation, so go figure them out
24195498Stimh 			 * now.
24205331Samw 			 */
24215498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24225498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24235498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24247184Stimh 			    zct.zct_zplprops, &is_insensitive);
24255331Samw 			if (error != 0) {
24265331Samw 				nvlist_free(nvprops);
24275498Stimh 				nvlist_free(zct.zct_zplprops);
24285331Samw 				return (error);
24294577Sahrens 			}
24302676Seschrock 		}
243110272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
24326492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24335498Stimh 		nvlist_free(zct.zct_zplprops);
2434789Sahrens 	}
24352676Seschrock 
24362676Seschrock 	/*
24372676Seschrock 	 * It would be nice to do this atomically.
24382676Seschrock 	 */
24392676Seschrock 	if (error == 0) {
24404787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
244110242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
24422676Seschrock 	}
24434543Smarks 	nvlist_free(nvprops);
2444789Sahrens 	return (error);
2445789Sahrens }
2446789Sahrens 
24475367Sahrens /*
24485367Sahrens  * inputs:
24495367Sahrens  * zc_name	name of filesystem
24505367Sahrens  * zc_value	short name of snapshot
24515367Sahrens  * zc_cookie	recursive flag
24529396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24535367Sahrens  *
245410588SEric.Taylor@Sun.COM  * outputs:
245510588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
24565367Sahrens  */
2457789Sahrens static int
24582199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24592199Sahrens {
24607265Sahrens 	nvlist_t *nvprops = NULL;
24617265Sahrens 	int error;
24627265Sahrens 	boolean_t recursive = zc->zc_cookie;
24637265Sahrens 
24642676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24652199Sahrens 		return (EINVAL);
24667265Sahrens 
24677265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24687265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24699643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
24707265Sahrens 		return (error);
24717265Sahrens 
24729355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
24739355SMatthew.Ahrens@Sun.COM 	if (error)
24749355SMatthew.Ahrens@Sun.COM 		goto out;
24759355SMatthew.Ahrens@Sun.COM 
24769355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
24779355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
24789355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
24799355SMatthew.Ahrens@Sun.COM 		goto out;
24807265Sahrens 	}
24819355SMatthew.Ahrens@Sun.COM 
24829355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
24839355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
24849355SMatthew.Ahrens@Sun.COM 
24859355SMatthew.Ahrens@Sun.COM out:
24867265Sahrens 	nvlist_free(nvprops);
24877265Sahrens 	return (error);
24882199Sahrens }
24892199Sahrens 
24904007Smmusante int
24912199Sahrens zfs_unmount_snap(char *name, void *arg)
2492789Sahrens {
24932417Sahrens 	vfs_t *vfsp = NULL;
24942199Sahrens 
24956689Smaybee 	if (arg) {
24966689Smaybee 		char *snapname = arg;
24976689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
24986689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
24996689Smaybee 
25006689Smaybee 		(void) strcpy(buf, name);
25016689Smaybee 		(void) strcat(buf, "@");
25026689Smaybee 		(void) strcat(buf, snapname);
25036689Smaybee 		vfsp = zfs_get_vfs(buf);
25046689Smaybee 		kmem_free(buf, len);
25052417Sahrens 	} else if (strchr(name, '@')) {
25062199Sahrens 		vfsp = zfs_get_vfs(name);
25072199Sahrens 	}
25082199Sahrens 
25092199Sahrens 	if (vfsp) {
25102199Sahrens 		/*
25112199Sahrens 		 * Always force the unmount for snapshots.
25122199Sahrens 		 */
25132199Sahrens 		int flag = MS_FORCE;
2514789Sahrens 		int err;
2515789Sahrens 
25162199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25172199Sahrens 			VFS_RELE(vfsp);
25182199Sahrens 			return (err);
25192199Sahrens 		}
25202199Sahrens 		VFS_RELE(vfsp);
25212199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25222199Sahrens 			return (err);
25232199Sahrens 	}
25242199Sahrens 	return (0);
25252199Sahrens }
25262199Sahrens 
25275367Sahrens /*
25285367Sahrens  * inputs:
252910242Schris.kirby@sun.com  * zc_name		name of filesystem
253010242Schris.kirby@sun.com  * zc_value		short name of snapshot
253110242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25325367Sahrens  *
25335367Sahrens  * outputs:	none
25345367Sahrens  */
25352199Sahrens static int
25362199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25372199Sahrens {
25382199Sahrens 	int err;
2539789Sahrens 
25402676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25412199Sahrens 		return (EINVAL);
25422199Sahrens 	err = dmu_objset_find(zc->zc_name,
25432676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25442199Sahrens 	if (err)
25452199Sahrens 		return (err);
254610242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
254710242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
25482199Sahrens }
25492199Sahrens 
25505367Sahrens /*
25515367Sahrens  * inputs:
25525367Sahrens  * zc_name		name of dataset to destroy
25535367Sahrens  * zc_objset_type	type of objset
255410242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25555367Sahrens  *
25565367Sahrens  * outputs:		none
25575367Sahrens  */
25582199Sahrens static int
25592199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25602199Sahrens {
256110588SEric.Taylor@Sun.COM 	int err;
25622199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
256310588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
25642199Sahrens 		if (err)
25652199Sahrens 			return (err);
2566789Sahrens 	}
2567789Sahrens 
256810588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
256910588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
257010588SEric.Taylor@Sun.COM 		zvol_remove_minor(zc->zc_name);
257110588SEric.Taylor@Sun.COM 	return (err);
2572789Sahrens }
2573789Sahrens 
25745367Sahrens /*
25755367Sahrens  * inputs:
25765446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25775367Sahrens  *
25785367Sahrens  * outputs:	none
25795367Sahrens  */
2580789Sahrens static int
2581789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2582789Sahrens {
258310272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
25845446Sahrens 	int error;
258510272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
258610272SMatthew.Ahrens@Sun.COM 	char *clone_name;
258710272SMatthew.Ahrens@Sun.COM 
258810272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
258910272SMatthew.Ahrens@Sun.COM 	if (error)
259010272SMatthew.Ahrens@Sun.COM 		return (error);
259110272SMatthew.Ahrens@Sun.COM 
259210272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
259310272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
259410272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
259510272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
259610272SMatthew.Ahrens@Sun.COM 	}
259710272SMatthew.Ahrens@Sun.COM 
259810272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
259910272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
260010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
260110272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
260210272SMatthew.Ahrens@Sun.COM 	}
26035446Sahrens 
26045446Sahrens 	/*
260510272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
26065446Sahrens 	 */
260710272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
260810272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26095446Sahrens 	if (error)
261010272SMatthew.Ahrens@Sun.COM 		goto out;
261110272SMatthew.Ahrens@Sun.COM 
261210298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
261310272SMatthew.Ahrens@Sun.COM 	if (error)
261410272SMatthew.Ahrens@Sun.COM 		goto out;
261510272SMatthew.Ahrens@Sun.COM 
261610272SMatthew.Ahrens@Sun.COM 	/*
261710272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
261810272SMatthew.Ahrens@Sun.COM 	 */
26199396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
262010298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
26216083Sek110237 		if (error == 0) {
26226083Sek110237 			int resume_err;
26236083Sek110237 
262410272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
262510272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
262610272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
262710272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
262810272SMatthew.Ahrens@Sun.COM 				ds = NULL;
262910272SMatthew.Ahrens@Sun.COM 			} else {
263010272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
263110272SMatthew.Ahrens@Sun.COM 			}
263210298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
26336083Sek110237 			error = error ? error : resume_err;
26346083Sek110237 		}
26355446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26365446Sahrens 	} else {
263710272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
263810272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
263910272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
264010272SMatthew.Ahrens@Sun.COM 			ds = NULL;
264110272SMatthew.Ahrens@Sun.COM 		} else {
264210272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
264310272SMatthew.Ahrens@Sun.COM 		}
26445446Sahrens 	}
264510272SMatthew.Ahrens@Sun.COM 
264610272SMatthew.Ahrens@Sun.COM 	/*
264710272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
264810272SMatthew.Ahrens@Sun.COM 	 */
264910272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
265010272SMatthew.Ahrens@Sun.COM 
265110272SMatthew.Ahrens@Sun.COM out:
265210272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
265310272SMatthew.Ahrens@Sun.COM 	if (ds)
265410272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26555446Sahrens 	return (error);
2656789Sahrens }
2657789Sahrens 
26585367Sahrens /*
26595367Sahrens  * inputs:
26605367Sahrens  * zc_name	old name of dataset
26615367Sahrens  * zc_value	new name of dataset
26625367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26635367Sahrens  *
26645367Sahrens  * outputs:	none
26655367Sahrens  */
2666789Sahrens static int
2667789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2668789Sahrens {
26694490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26704007Smmusante 
26712676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26725326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26735326Sek110237 	    strchr(zc->zc_value, '%'))
2674789Sahrens 		return (EINVAL);
2675789Sahrens 
26764007Smmusante 	/*
26774007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26784007Smmusante 	 * in which case the dataset code figures out which snapshots
26794007Smmusante 	 * to unmount.
26804007Smmusante 	 */
26814007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2682789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26832199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26842199Sahrens 		if (err)
26852199Sahrens 			return (err);
2686789Sahrens 	}
268710588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
268810588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
26894007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2690789Sahrens }
2691789Sahrens 
26926689Smaybee static void
26938536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
26946689Smaybee {
26956689Smaybee 	zfs_cmd_t *zc;
26966689Smaybee 	nvpair_t *prop;
26976689Smaybee 
26986689Smaybee 	if (props == NULL)
26996689Smaybee 		return;
27006689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
27016689Smaybee 	(void) strcpy(zc->zc_name, dataset);
27026689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
27036689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27048536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
27058536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
27068536SDavid.Pacheco@Sun.COM 			continue;
27076689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
27086689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
27096689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
27106689Smaybee 	}
27116689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
27126689Smaybee }
27136689Smaybee 
27145367Sahrens /*
27155367Sahrens  * inputs:
27165367Sahrens  * zc_name		name of containing filesystem
27175367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27185367Sahrens  * zc_value		name of snapshot to create
27195367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27205367Sahrens  * zc_cookie		file descriptor to recv from
27215367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27225367Sahrens  * zc_guid		force flag
27235367Sahrens  *
27245367Sahrens  * outputs:
27255367Sahrens  * zc_cookie		number of bytes read
27265367Sahrens  */
2727789Sahrens static int
27285367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2729789Sahrens {
2730789Sahrens 	file_t *fp;
27315326Sek110237 	objset_t *os;
27325367Sahrens 	dmu_recv_cookie_t drc;
27335326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2734789Sahrens 	int error, fd;
27355367Sahrens 	offset_t off;
27365367Sahrens 	nvlist_t *props = NULL;
27376689Smaybee 	nvlist_t *origprops = NULL;
27385367Sahrens 	objset_t *origin = NULL;
27395367Sahrens 	char *tosnap;
27405367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2741789Sahrens 
27423265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27435326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27445326Sek110237 	    strchr(zc->zc_value, '%'))
27453265Sahrens 		return (EINVAL);
27463265Sahrens 
27475367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27485367Sahrens 	tosnap = strchr(tofs, '@');
27495367Sahrens 	*tosnap = '\0';
27505367Sahrens 	tosnap++;
27515367Sahrens 
27525367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27535367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27549643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27555367Sahrens 		return (error);
27565367Sahrens 
2757789Sahrens 	fd = zc->zc_cookie;
2758789Sahrens 	fp = getf(fd);
27595367Sahrens 	if (fp == NULL) {
27605367Sahrens 		nvlist_free(props);
2761789Sahrens 		return (EBADF);
27625367Sahrens 	}
27635326Sek110237 
276410298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
27656689Smaybee 		/*
27666689Smaybee 		 * If new properties are supplied, they are to completely
27676689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27686689Smaybee 		 */
276910298SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, B_TRUE);
277010298SMatthew.Ahrens@Sun.COM 
277110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
27725326Sek110237 	}
27735326Sek110237 
27745367Sahrens 	if (zc->zc_string[0]) {
277510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
27766689Smaybee 		if (error)
27776689Smaybee 			goto out;
27785367Sahrens 	}
27795367Sahrens 
27805367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
278110204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
27825367Sahrens 	if (origin)
278310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
27846689Smaybee 	if (error)
27856689Smaybee 		goto out;
27865326Sek110237 
27875326Sek110237 	/*
27886689Smaybee 	 * Reset properties.  We do this before we receive the stream
27896689Smaybee 	 * so that the properties are applied to the new data.
27905326Sek110237 	 */
27915367Sahrens 	if (props) {
27928536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
27936689Smaybee 		/*
27946689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
27956689Smaybee 		 */
27966689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27975367Sahrens 	}
27985367Sahrens 
27995367Sahrens 	off = fp->f_offset;
28005367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
28015367Sahrens 
280210204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
280310204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
280410204SMatthew.Ahrens@Sun.COM 
280510204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
280610204SMatthew.Ahrens@Sun.COM 			/* online recv */
280710204SMatthew.Ahrens@Sun.COM 			int end_err;
280810298SMatthew.Ahrens@Sun.COM 
280910298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
281010204SMatthew.Ahrens@Sun.COM 			/*
281110204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
281210204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
281310204SMatthew.Ahrens@Sun.COM 			 */
281410204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
281510204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
281610204SMatthew.Ahrens@Sun.COM 				int resume_err =
281710298SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, tofs);
281810204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
281910204SMatthew.Ahrens@Sun.COM 			}
282010204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
282110204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
282210204SMatthew.Ahrens@Sun.COM 		} else {
28236689Smaybee 			error = dmu_recv_end(&drc);
28245367Sahrens 		}
28256083Sek110237 	}
28265367Sahrens 
28275367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28285367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28295367Sahrens 		fp->f_offset = off;
28302885Sahrens 
28316689Smaybee 	/*
28326689Smaybee 	 * On error, restore the original props.
28336689Smaybee 	 */
28346689Smaybee 	if (error && props) {
28358536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28366689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28376689Smaybee 	}
28386689Smaybee out:
28396689Smaybee 	nvlist_free(props);
28406689Smaybee 	nvlist_free(origprops);
2841789Sahrens 	releasef(fd);
2842789Sahrens 	return (error);
2843789Sahrens }
2844789Sahrens 
28455367Sahrens /*
28465367Sahrens  * inputs:
28475367Sahrens  * zc_name	name of snapshot to send
28485367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28495367Sahrens  * zc_cookie	file descriptor to send stream to
28505367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28515367Sahrens  *
28525367Sahrens  * outputs: none
28535367Sahrens  */
2854789Sahrens static int
28555367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2856789Sahrens {
2857789Sahrens 	objset_t *fromsnap = NULL;
2858789Sahrens 	objset_t *tosnap;
2859789Sahrens 	file_t *fp;
2860789Sahrens 	int error;
28615367Sahrens 	offset_t off;
2862789Sahrens 
286310298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
2864789Sahrens 	if (error)
2865789Sahrens 		return (error);
2866789Sahrens 
28672676Seschrock 	if (zc->zc_value[0] != '\0') {
28688012SEric.Taylor@Sun.COM 		char *buf;
28692885Sahrens 		char *cp;
28702885Sahrens 
28718012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28728012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28732885Sahrens 		cp = strchr(buf, '@');
28742885Sahrens 		if (cp)
28752885Sahrens 			*(cp+1) = 0;
28768012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
287710298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
28788012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2879789Sahrens 		if (error) {
288010298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(tosnap, FTAG);
2881789Sahrens 			return (error);
2882789Sahrens 		}
2883789Sahrens 	}
2884789Sahrens 
2885789Sahrens 	fp = getf(zc->zc_cookie);
2886789Sahrens 	if (fp == NULL) {
288710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
2888789Sahrens 		if (fromsnap)
288910298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(fromsnap, FTAG);
2890789Sahrens 		return (EBADF);
2891789Sahrens 	}
2892789Sahrens 
28935367Sahrens 	off = fp->f_offset;
28945367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
28955367Sahrens 
28965367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28975367Sahrens 		fp->f_offset = off;
2898789Sahrens 	releasef(zc->zc_cookie);
2899789Sahrens 	if (fromsnap)
290010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
290110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
2902789Sahrens 	return (error);
2903789Sahrens }
2904789Sahrens 
29051544Seschrock static int
29061544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29071544Seschrock {
29081544Seschrock 	int id, error;
29091544Seschrock 
29101544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29111544Seschrock 	    &zc->zc_inject_record);
29121544Seschrock 
29131544Seschrock 	if (error == 0)
29141544Seschrock 		zc->zc_guid = (uint64_t)id;
29151544Seschrock 
29161544Seschrock 	return (error);
29171544Seschrock }
29181544Seschrock 
29191544Seschrock static int
29201544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29211544Seschrock {
29221544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29231544Seschrock }
29241544Seschrock 
29251544Seschrock static int
29261544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29271544Seschrock {
29281544Seschrock 	int id = (int)zc->zc_guid;
29291544Seschrock 	int error;
29301544Seschrock 
29311544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29321544Seschrock 	    &zc->zc_inject_record);
29331544Seschrock 
29341544Seschrock 	zc->zc_guid = id;
29351544Seschrock 
29361544Seschrock 	return (error);
29371544Seschrock }
29381544Seschrock 
29391544Seschrock static int
29401544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29411544Seschrock {
29421544Seschrock 	spa_t *spa;
29431544Seschrock 	int error;
29442676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29451544Seschrock 
29461544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29471544Seschrock 		return (error);
29481544Seschrock 
29492676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29501544Seschrock 	    &count);
29511544Seschrock 	if (error == 0)
29522676Seschrock 		zc->zc_nvlist_dst_size = count;
29531544Seschrock 	else
29542676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29551544Seschrock 
29561544Seschrock 	spa_close(spa, FTAG);
29571544Seschrock 
29581544Seschrock 	return (error);
29591544Seschrock }
29601544Seschrock 
29611544Seschrock static int
29621544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29631544Seschrock {
29641544Seschrock 	spa_t *spa;
29651544Seschrock 	vdev_t *vd;
29661544Seschrock 	int error;
29671544Seschrock 
29687294Sperrin 	/*
29697294Sperrin 	 * On zpool clear we also fix up missing slogs
29707294Sperrin 	 */
29717294Sperrin 	mutex_enter(&spa_namespace_lock);
29727294Sperrin 	spa = spa_lookup(zc->zc_name);
29737294Sperrin 	if (spa == NULL) {
29747294Sperrin 		mutex_exit(&spa_namespace_lock);
29757294Sperrin 		return (EIO);
29767294Sperrin 	}
29777294Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
29787294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
29797294Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
29807294Sperrin 	}
29817294Sperrin 	mutex_exit(&spa_namespace_lock);
29827294Sperrin 
29831544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29841544Seschrock 		return (error);
29851544Seschrock 
29867754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
29871544Seschrock 
29882676Seschrock 	if (zc->zc_guid == 0) {
29891544Seschrock 		vd = NULL;
29906643Seschrock 	} else {
29916643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
29925450Sbrendan 		if (vd == NULL) {
29937754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
29945450Sbrendan 			spa_close(spa, FTAG);
29955450Sbrendan 			return (ENODEV);
29965450Sbrendan 		}
29971544Seschrock 	}
29981544Seschrock 
29997754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
30007754SJeff.Bonwick@Sun.COM 
30017754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
30027754SJeff.Bonwick@Sun.COM 
30037754SJeff.Bonwick@Sun.COM 	/*
30047754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
30057754SJeff.Bonwick@Sun.COM 	 */
30069234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30079234SGeorge.Wilson@Sun.COM 		error = EIO;
30081544Seschrock 
30091544Seschrock 	spa_close(spa, FTAG);
30101544Seschrock 
30119234SGeorge.Wilson@Sun.COM 	return (error);
30121544Seschrock }
30131544Seschrock 
30145367Sahrens /*
30155367Sahrens  * inputs:
30165367Sahrens  * zc_name	name of filesystem
30175367Sahrens  * zc_value	name of origin snapshot
30185367Sahrens  *
301910588SEric.Taylor@Sun.COM  * outputs:
302010588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
30215367Sahrens  */
30221544Seschrock static int
30232082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30242082Seschrock {
30252417Sahrens 	char *cp;
30262417Sahrens 
30272417Sahrens 	/*
30282417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30292417Sahrens 	 * it's easier.
30302417Sahrens 	 */
30312676Seschrock 	cp = strchr(zc->zc_value, '@');
30322417Sahrens 	if (cp)
30332417Sahrens 		*cp = '\0';
30342676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30352417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
303610588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
30372082Seschrock }
30382082Seschrock 
30394543Smarks /*
30409396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30419396SMatthew.Ahrens@Sun.COM  *
30429396SMatthew.Ahrens@Sun.COM  * inputs:
30439396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30449396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30459396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30469396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30479396SMatthew.Ahrens@Sun.COM  *
30489396SMatthew.Ahrens@Sun.COM  * outputs:
30499396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30509396SMatthew.Ahrens@Sun.COM  */
30519396SMatthew.Ahrens@Sun.COM static int
30529396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30539396SMatthew.Ahrens@Sun.COM {
30549396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30559396SMatthew.Ahrens@Sun.COM 	int error;
30569396SMatthew.Ahrens@Sun.COM 
30579396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30589396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30599396SMatthew.Ahrens@Sun.COM 
306010298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
30619396SMatthew.Ahrens@Sun.COM 	if (error)
30629396SMatthew.Ahrens@Sun.COM 		return (error);
30639396SMatthew.Ahrens@Sun.COM 
30649396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
30659396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
30669396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
30679396SMatthew.Ahrens@Sun.COM 
30689396SMatthew.Ahrens@Sun.COM 	return (error);
30699396SMatthew.Ahrens@Sun.COM }
30709396SMatthew.Ahrens@Sun.COM 
30719396SMatthew.Ahrens@Sun.COM /*
30729396SMatthew.Ahrens@Sun.COM  * inputs:
30739396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
30749396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
30759396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
30769396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
30779396SMatthew.Ahrens@Sun.COM  *
30789396SMatthew.Ahrens@Sun.COM  * outputs:
30799396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
30809396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
30819396SMatthew.Ahrens@Sun.COM  */
30829396SMatthew.Ahrens@Sun.COM static int
30839396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
30849396SMatthew.Ahrens@Sun.COM {
30859396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30869396SMatthew.Ahrens@Sun.COM 	int error;
30879396SMatthew.Ahrens@Sun.COM 
308810298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
30899396SMatthew.Ahrens@Sun.COM 	if (error)
30909396SMatthew.Ahrens@Sun.COM 		return (error);
30919396SMatthew.Ahrens@Sun.COM 
30929396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
30939396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
30949396SMatthew.Ahrens@Sun.COM 
30959396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
30969396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
30979396SMatthew.Ahrens@Sun.COM 
30989396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
30999396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
31009396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
31019396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
31029396SMatthew.Ahrens@Sun.COM 	}
31039396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
31049396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31059396SMatthew.Ahrens@Sun.COM 
31069396SMatthew.Ahrens@Sun.COM 	return (error);
31079396SMatthew.Ahrens@Sun.COM }
31089396SMatthew.Ahrens@Sun.COM 
31099396SMatthew.Ahrens@Sun.COM /*
31109396SMatthew.Ahrens@Sun.COM  * inputs:
31119396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31129396SMatthew.Ahrens@Sun.COM  *
31139396SMatthew.Ahrens@Sun.COM  * outputs:
31149396SMatthew.Ahrens@Sun.COM  * none
31159396SMatthew.Ahrens@Sun.COM  */
31169396SMatthew.Ahrens@Sun.COM static int
31179396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31189396SMatthew.Ahrens@Sun.COM {
31199396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31209396SMatthew.Ahrens@Sun.COM 	int error;
31219396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31229396SMatthew.Ahrens@Sun.COM 
31239396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
312410298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
31259396SMatthew.Ahrens@Sun.COM 			/*
31269396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31279396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31289396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31299396SMatthew.Ahrens@Sun.COM 			 */
313010298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
313110298SMatthew.Ahrens@Sun.COM 			if (error == 0)
313210298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
31339396SMatthew.Ahrens@Sun.COM 		}
31349396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31359396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31369396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31379396SMatthew.Ahrens@Sun.COM 	} else {
313810298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
313910298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
31409396SMatthew.Ahrens@Sun.COM 		if (error)
31419396SMatthew.Ahrens@Sun.COM 			return (error);
31429396SMatthew.Ahrens@Sun.COM 
31439396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
314410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
31459396SMatthew.Ahrens@Sun.COM 	}
31469396SMatthew.Ahrens@Sun.COM 
31479396SMatthew.Ahrens@Sun.COM 	return (error);
31489396SMatthew.Ahrens@Sun.COM }
31499396SMatthew.Ahrens@Sun.COM 
31509396SMatthew.Ahrens@Sun.COM /*
31514543Smarks  * We don't want to have a hard dependency
31524543Smarks  * against some special symbols in sharefs
31535331Samw  * nfs, and smbsrv.  Determine them if needed when
31544543Smarks  * the first file system is shared.
31555331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31564543Smarks  */
31575331Samw int (*znfsexport_fs)(void *arg);
31584543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31595331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31605331Samw 
31615331Samw int zfs_nfsshare_inited;
31625331Samw int zfs_smbshare_inited;
31635331Samw 
31644543Smarks ddi_modhandle_t nfs_mod;
31654543Smarks ddi_modhandle_t sharefs_mod;
31665331Samw ddi_modhandle_t smbsrv_mod;
31674543Smarks kmutex_t zfs_share_lock;
31684543Smarks 
31694543Smarks static int
31705331Samw zfs_init_sharefs()
31715331Samw {
31725331Samw 	int error;
31735331Samw 
31745331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
31755331Samw 	/* Both NFS and SMB shares also require sharetab support. */
31765331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
31775331Samw 	    ddi_modopen("fs/sharefs",
31785331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
31795331Samw 		return (ENOSYS);
31805331Samw 	}
31815331Samw 	if (zshare_fs == NULL && ((zshare_fs =
31825331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
31835331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
31845331Samw 		return (ENOSYS);
31855331Samw 	}
31865331Samw 	return (0);
31875331Samw }
31885331Samw 
31895331Samw static int
31904543Smarks zfs_ioc_share(zfs_cmd_t *zc)
31914543Smarks {
31924543Smarks 	int error;
31934543Smarks 	int opcode;
31944543Smarks 
31955331Samw 	switch (zc->zc_share.z_sharetype) {
31965331Samw 	case ZFS_SHARE_NFS:
31975331Samw 	case ZFS_UNSHARE_NFS:
31985331Samw 		if (zfs_nfsshare_inited == 0) {
31995331Samw 			mutex_enter(&zfs_share_lock);
32005331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
32015331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32025331Samw 				mutex_exit(&zfs_share_lock);
32035331Samw 				return (ENOSYS);
32045331Samw 			}
32055331Samw 			if (znfsexport_fs == NULL &&
32065331Samw 			    ((znfsexport_fs = (int (*)(void *))
32075331Samw 			    ddi_modsym(nfs_mod,
32085331Samw 			    "nfs_export", &error)) == NULL)) {
32095331Samw 				mutex_exit(&zfs_share_lock);
32105331Samw 				return (ENOSYS);
32115331Samw 			}
32125331Samw 			error = zfs_init_sharefs();
32135331Samw 			if (error) {
32145331Samw 				mutex_exit(&zfs_share_lock);
32155331Samw 				return (ENOSYS);
32165331Samw 			}
32175331Samw 			zfs_nfsshare_inited = 1;
32184543Smarks 			mutex_exit(&zfs_share_lock);
32194543Smarks 		}
32205331Samw 		break;
32215331Samw 	case ZFS_SHARE_SMB:
32225331Samw 	case ZFS_UNSHARE_SMB:
32235331Samw 		if (zfs_smbshare_inited == 0) {
32245331Samw 			mutex_enter(&zfs_share_lock);
32255331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32265331Samw 			    ddi_modopen("drv/smbsrv",
32275331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32285331Samw 				mutex_exit(&zfs_share_lock);
32295331Samw 				return (ENOSYS);
32305331Samw 			}
32315331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32325331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32336139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32345331Samw 				mutex_exit(&zfs_share_lock);
32355331Samw 				return (ENOSYS);
32365331Samw 			}
32375331Samw 			error = zfs_init_sharefs();
32385331Samw 			if (error) {
32395331Samw 				mutex_exit(&zfs_share_lock);
32405331Samw 				return (ENOSYS);
32415331Samw 			}
32425331Samw 			zfs_smbshare_inited = 1;
32434543Smarks 			mutex_exit(&zfs_share_lock);
32444543Smarks 		}
32455331Samw 		break;
32465331Samw 	default:
32475331Samw 		return (EINVAL);
32484543Smarks 	}
32494543Smarks 
32505331Samw 	switch (zc->zc_share.z_sharetype) {
32515331Samw 	case ZFS_SHARE_NFS:
32525331Samw 	case ZFS_UNSHARE_NFS:
32535331Samw 		if (error =
32545331Samw 		    znfsexport_fs((void *)
32555331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32565331Samw 			return (error);
32575331Samw 		break;
32585331Samw 	case ZFS_SHARE_SMB:
32595331Samw 	case ZFS_UNSHARE_SMB:
32605331Samw 		if (error = zsmbexport_fs((void *)
32615331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
32625331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
32638845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
32645331Samw 			return (error);
32655331Samw 		}
32665331Samw 		break;
32675331Samw 	}
32685331Samw 
32695331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
32705331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
32714543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
32724543Smarks 
32735331Samw 	/*
32745331Samw 	 * Add or remove share from sharetab
32755331Samw 	 */
32764543Smarks 	error = zshare_fs(opcode,
32774543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
32784543Smarks 	    zc->zc_share.z_sharemax);
32794543Smarks 
32804543Smarks 	return (error);
32814543Smarks 
32824543Smarks }
32834543Smarks 
32848845Samw@Sun.COM ace_t full_access[] = {
32858845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
32868845Samw@Sun.COM };
32878845Samw@Sun.COM 
32888845Samw@Sun.COM /*
32898845Samw@Sun.COM  * Remove all ACL files in shares dir
32908845Samw@Sun.COM  */
32918845Samw@Sun.COM static int
32928845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
32938845Samw@Sun.COM {
32948845Samw@Sun.COM 	zap_cursor_t	zc;
32958845Samw@Sun.COM 	zap_attribute_t	zap;
32968845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
32978845Samw@Sun.COM 	int error;
32988845Samw@Sun.COM 
32998845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
33008845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
33018845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
33028845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
33038845Samw@Sun.COM 		    NULL, 0)) != 0)
33048845Samw@Sun.COM 			break;
33058845Samw@Sun.COM 	}
33068845Samw@Sun.COM 	zap_cursor_fini(&zc);
33078845Samw@Sun.COM 	return (error);
33088845Samw@Sun.COM }
33098845Samw@Sun.COM 
33108845Samw@Sun.COM static int
33118845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33128845Samw@Sun.COM {
33138845Samw@Sun.COM 	vnode_t *vp;
33148845Samw@Sun.COM 	znode_t *dzp;
33158845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33168845Samw@Sun.COM 	znode_t *sharedir;
33178845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33188845Samw@Sun.COM 	nvlist_t *nvlist;
33198845Samw@Sun.COM 	char *src, *target;
33208845Samw@Sun.COM 	vattr_t vattr;
33218845Samw@Sun.COM 	vsecattr_t vsec;
33228845Samw@Sun.COM 	int error = 0;
33238845Samw@Sun.COM 
33248845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33258845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33268845Samw@Sun.COM 		return (error);
33278845Samw@Sun.COM 
33288845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33298845Samw@Sun.COM 
33308845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33318845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33328845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33338845Samw@Sun.COM 		VN_RELE(vp);
33348845Samw@Sun.COM 		return (EINVAL);
33358845Samw@Sun.COM 	}
33368845Samw@Sun.COM 
33378845Samw@Sun.COM 	dzp = VTOZ(vp);
33388845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33398845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33408845Samw@Sun.COM 
33419030SMark.Shellenbaum@Sun.COM 	/*
33429030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33439030SMark.Shellenbaum@Sun.COM 	 */
33449030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33459030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33469030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33479030SMark.Shellenbaum@Sun.COM 
33489030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33499030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33509030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33519030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33529030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33539030SMark.Shellenbaum@Sun.COM 		if (error) {
33549030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33559030SMark.Shellenbaum@Sun.COM 		} else {
33569030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33579030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33589030SMark.Shellenbaum@Sun.COM 		}
33599030SMark.Shellenbaum@Sun.COM 		if (error) {
33609030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33619030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
33629030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
33639030SMark.Shellenbaum@Sun.COM 			return (error);
33649030SMark.Shellenbaum@Sun.COM 		}
33659030SMark.Shellenbaum@Sun.COM 	}
33669030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
33679030SMark.Shellenbaum@Sun.COM 
33689030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
33698845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33709030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
33718845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
33728845Samw@Sun.COM 		return (error);
33738845Samw@Sun.COM 	}
33748845Samw@Sun.COM 
33758845Samw@Sun.COM 	switch (zc->zc_cookie) {
33768845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
33778845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
33788845Samw@Sun.COM 		vattr.va_type = VREG;
33798845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
33808845Samw@Sun.COM 		vattr.va_uid = 0;
33818845Samw@Sun.COM 		vattr.va_gid = 0;
33828845Samw@Sun.COM 
33838845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
33848845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
33858845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
33868845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
33878845Samw@Sun.COM 
33888845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
33898845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
33908845Samw@Sun.COM 		if (resourcevp)
33918845Samw@Sun.COM 			VN_RELE(resourcevp);
33928845Samw@Sun.COM 		break;
33938845Samw@Sun.COM 
33948845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
33958845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
33968845Samw@Sun.COM 		    NULL, 0);
33978845Samw@Sun.COM 		break;
33988845Samw@Sun.COM 
33998845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
34008845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
34019643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
34028845Samw@Sun.COM 			VN_RELE(vp);
34038845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34048845Samw@Sun.COM 			return (error);
34058845Samw@Sun.COM 		}
34068845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34078845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34088845Samw@Sun.COM 		    &target)) {
34098845Samw@Sun.COM 			VN_RELE(vp);
34109179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34118845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34128845Samw@Sun.COM 			return (error);
34138845Samw@Sun.COM 		}
34148845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34158845Samw@Sun.COM 		    kcred, NULL, 0);
34168845Samw@Sun.COM 		nvlist_free(nvlist);
34178845Samw@Sun.COM 		break;
34188845Samw@Sun.COM 
34198845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34208845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34218845Samw@Sun.COM 		break;
34228845Samw@Sun.COM 
34238845Samw@Sun.COM 	default:
34248845Samw@Sun.COM 		error = EINVAL;
34258845Samw@Sun.COM 		break;
34268845Samw@Sun.COM 	}
34278845Samw@Sun.COM 
34288845Samw@Sun.COM 	VN_RELE(vp);
34298845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34308845Samw@Sun.COM 
34318845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34328845Samw@Sun.COM 
34338845Samw@Sun.COM 	return (error);
34348845Samw@Sun.COM }
34358845Samw@Sun.COM 
34364543Smarks /*
343710242Schris.kirby@sun.com  * inputs:
343810242Schris.kirby@sun.com  * zc_name	name of filesystem
343910242Schris.kirby@sun.com  * zc_value	short name of snap
344010242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
344110242Schris.kirby@sun.com  * zc_cookie	recursive flag
344210342Schris.kirby@sun.com  * zc_temphold	set if hold is temporary
344310242Schris.kirby@sun.com  *
344410242Schris.kirby@sun.com  * outputs:		none
344510242Schris.kirby@sun.com  */
344610242Schris.kirby@sun.com static int
344710242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
344810242Schris.kirby@sun.com {
344910242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
345010242Schris.kirby@sun.com 
345110242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
345210242Schris.kirby@sun.com 		return (EINVAL);
345310242Schris.kirby@sun.com 
345410242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
345510342Schris.kirby@sun.com 	    zc->zc_string, recursive, zc->zc_temphold));
345610242Schris.kirby@sun.com }
345710242Schris.kirby@sun.com 
345810242Schris.kirby@sun.com /*
345910242Schris.kirby@sun.com  * inputs:
346010242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
346110242Schris.kirby@sun.com  * zc_value	short name of snap
346210242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
346310242Schris.kirby@sun.com  * zc_cookie	recursive flag
346410242Schris.kirby@sun.com  *
346510242Schris.kirby@sun.com  * outputs:		none
346610242Schris.kirby@sun.com  */
346710242Schris.kirby@sun.com static int
346810242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
346910242Schris.kirby@sun.com {
347010242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
347110242Schris.kirby@sun.com 
347210242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
347310242Schris.kirby@sun.com 		return (EINVAL);
347410242Schris.kirby@sun.com 
347510242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
347610242Schris.kirby@sun.com 	    zc->zc_string, recursive));
347710242Schris.kirby@sun.com }
347810242Schris.kirby@sun.com 
347910242Schris.kirby@sun.com /*
348010242Schris.kirby@sun.com  * inputs:
348110242Schris.kirby@sun.com  * zc_name		name of filesystem
348210242Schris.kirby@sun.com  *
348310242Schris.kirby@sun.com  * outputs:
348410242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
348510242Schris.kirby@sun.com  */
348610242Schris.kirby@sun.com static int
348710242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
348810242Schris.kirby@sun.com {
348910242Schris.kirby@sun.com 	nvlist_t *nvp;
349010242Schris.kirby@sun.com 	int error;
349110242Schris.kirby@sun.com 
349210242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
349310242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
349410242Schris.kirby@sun.com 		nvlist_free(nvp);
349510242Schris.kirby@sun.com 	}
349610242Schris.kirby@sun.com 
349710242Schris.kirby@sun.com 	return (error);
349810242Schris.kirby@sun.com }
349910242Schris.kirby@sun.com 
350010242Schris.kirby@sun.com /*
35014988Sek110237  * pool create, destroy, and export don't log the history as part of
35024988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35034988Sek110237  * do the logging of those commands.
35044543Smarks  */
3505789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
35069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35079234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35099234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35119234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35149234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
35159234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35169234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35179234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35189234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
35199234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35209234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35219234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35229234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
35239234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
35259234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35279234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35289234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35299234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35309234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35319234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35329234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35339234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35359234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35379234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35389234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35399234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35409425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35419425SEric.Schrock@Sun.COM 	    B_TRUE },
35429234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35439234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35459234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35479234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35499234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
35519234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
35529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
35539234SGeorge.Wilson@Sun.COM 	    B_TRUE},
35549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
35559234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
35579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
35589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
35599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
35609234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35619234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35629234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
35649234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
35669234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35679234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
35689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
35699234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
35719234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
35739234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35759234SGeorge.Wilson@Sun.COM 	    B_FALSE },
357610233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
357710233SGeorge.Wilson@Sun.COM 	    B_TRUE },
35789234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35799234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35819234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
35839234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35859234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
35879234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35889234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
35899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
35909234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
35929396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
35939396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
35949396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35959396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
35969396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
35979396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
35989396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
359910242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
360010242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
360110242Schris.kirby@sun.com 	    B_TRUE },
360210242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
360310242Schris.kirby@sun.com 	    B_TRUE }
3604789Sahrens };
3605789Sahrens 
36069234SGeorge.Wilson@Sun.COM int
36079234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
36089234SGeorge.Wilson@Sun.COM {
36099234SGeorge.Wilson@Sun.COM 	spa_t *spa;
36109234SGeorge.Wilson@Sun.COM 	int error;
36119234SGeorge.Wilson@Sun.COM 
36129234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
36139234SGeorge.Wilson@Sun.COM 
36149396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
36159234SGeorge.Wilson@Sun.COM 	if (error == 0) {
36169234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
36179234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
36189234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
36199234SGeorge.Wilson@Sun.COM 	}
36209234SGeorge.Wilson@Sun.COM 	return (error);
36219234SGeorge.Wilson@Sun.COM }
36229234SGeorge.Wilson@Sun.COM 
3623789Sahrens static int
3624789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3625789Sahrens {
3626789Sahrens 	zfs_cmd_t *zc;
3627789Sahrens 	uint_t vec;
36282199Sahrens 	int error, rc;
3629789Sahrens 
3630789Sahrens 	if (getminor(dev) != 0)
3631789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3632789Sahrens 
3633789Sahrens 	vec = cmd - ZFS_IOC;
36344787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3635789Sahrens 
3636789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3637789Sahrens 		return (EINVAL);
3638789Sahrens 
3639789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3640789Sahrens 
36419643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3642789Sahrens 
364310588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
36444543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3645789Sahrens 
3646789Sahrens 	/*
3647789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3648789Sahrens 	 * the lower layers.
3649789Sahrens 	 */
3650789Sahrens 	if (error == 0) {
3651789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
36529643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3653789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
36544577Sahrens 		case POOL_NAME:
3655789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3656789Sahrens 				error = EINVAL;
36579234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36589234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36599234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3660789Sahrens 			break;
3661789Sahrens 
36624577Sahrens 		case DATASET_NAME:
3663789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3664789Sahrens 				error = EINVAL;
36659234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36669234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36679234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3668789Sahrens 			break;
36692856Snd150628 
36704577Sahrens 		case NO_NAME:
36712856Snd150628 			break;
3672789Sahrens 		}
3673789Sahrens 	}
3674789Sahrens 
3675789Sahrens 	if (error == 0)
3676789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3677789Sahrens 
36789643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
36794543Smarks 	if (error == 0) {
36802199Sahrens 		error = rc;
36819396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
36824543Smarks 			zfs_log_history(zc);
36834543Smarks 	}
3684789Sahrens 
3685789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3686789Sahrens 	return (error);
3687789Sahrens }
3688789Sahrens 
3689789Sahrens static int
3690789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3691789Sahrens {
3692789Sahrens 	if (cmd != DDI_ATTACH)
3693789Sahrens 		return (DDI_FAILURE);
3694789Sahrens 
3695789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3696789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3697789Sahrens 		return (DDI_FAILURE);
3698789Sahrens 
3699789Sahrens 	zfs_dip = dip;
3700789Sahrens 
3701789Sahrens 	ddi_report_dev(dip);
3702789Sahrens 
3703789Sahrens 	return (DDI_SUCCESS);
3704789Sahrens }
3705789Sahrens 
3706789Sahrens static int
3707789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3708789Sahrens {
3709789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3710789Sahrens 		return (DDI_FAILURE);
3711789Sahrens 
3712789Sahrens 	if (cmd != DDI_DETACH)
3713789Sahrens 		return (DDI_FAILURE);
3714789Sahrens 
3715789Sahrens 	zfs_dip = NULL;
3716789Sahrens 
3717789Sahrens 	ddi_prop_remove_all(dip);
3718789Sahrens 	ddi_remove_minor_node(dip, NULL);
3719789Sahrens 
3720789Sahrens 	return (DDI_SUCCESS);
3721789Sahrens }
3722789Sahrens 
3723789Sahrens /*ARGSUSED*/
3724789Sahrens static int
3725789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3726789Sahrens {
3727789Sahrens 	switch (infocmd) {
3728789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3729789Sahrens 		*result = zfs_dip;
3730789Sahrens 		return (DDI_SUCCESS);
3731789Sahrens 
3732789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3733849Sbonwick 		*result = (void *)0;
3734789Sahrens 		return (DDI_SUCCESS);
3735789Sahrens 	}
3736789Sahrens 
3737789Sahrens 	return (DDI_FAILURE);
3738789Sahrens }
3739789Sahrens 
3740789Sahrens /*
3741789Sahrens  * OK, so this is a little weird.
3742789Sahrens  *
3743789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3744789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3745789Sahrens  *
3746789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3747789Sahrens  * so most of the standard driver entry points are in zvol.c.
3748789Sahrens  */
3749789Sahrens static struct cb_ops zfs_cb_ops = {
3750789Sahrens 	zvol_open,	/* open */
3751789Sahrens 	zvol_close,	/* close */
3752789Sahrens 	zvol_strategy,	/* strategy */
3753789Sahrens 	nodev,		/* print */
37546423Sgw25295 	zvol_dump,	/* dump */
3755789Sahrens 	zvol_read,	/* read */
3756789Sahrens 	zvol_write,	/* write */
3757789Sahrens 	zfsdev_ioctl,	/* ioctl */
3758789Sahrens 	nodev,		/* devmap */
3759789Sahrens 	nodev,		/* mmap */
3760789Sahrens 	nodev,		/* segmap */
3761789Sahrens 	nochpoll,	/* poll */
3762789Sahrens 	ddi_prop_op,	/* prop_op */
3763789Sahrens 	NULL,		/* streamtab */
3764789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3765789Sahrens 	CB_REV,		/* version */
37663638Sbillm 	nodev,		/* async read */
37673638Sbillm 	nodev,		/* async write */
3768789Sahrens };
3769789Sahrens 
3770789Sahrens static struct dev_ops zfs_dev_ops = {
3771789Sahrens 	DEVO_REV,	/* version */
3772789Sahrens 	0,		/* refcnt */
3773789Sahrens 	zfs_info,	/* info */
3774789Sahrens 	nulldev,	/* identify */
3775789Sahrens 	nulldev,	/* probe */
3776789Sahrens 	zfs_attach,	/* attach */
3777789Sahrens 	zfs_detach,	/* detach */
3778789Sahrens 	nodev,		/* reset */
3779789Sahrens 	&zfs_cb_ops,	/* driver operations */
37807656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
37817656SSherry.Moore@Sun.COM 	NULL,		/* power */
37827656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3783789Sahrens };
3784789Sahrens 
3785789Sahrens static struct modldrv zfs_modldrv = {
37867656SSherry.Moore@Sun.COM 	&mod_driverops,
37877656SSherry.Moore@Sun.COM 	"ZFS storage pool",
37887656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3789789Sahrens };
3790789Sahrens 
3791789Sahrens static struct modlinkage modlinkage = {
3792789Sahrens 	MODREV_1,
3793789Sahrens 	(void *)&zfs_modlfs,
3794789Sahrens 	(void *)&zfs_modldrv,
3795789Sahrens 	NULL
3796789Sahrens };
3797789Sahrens 
37984720Sfr157268 
37994720Sfr157268 uint_t zfs_fsyncer_key;
38005326Sek110237 extern uint_t rrw_tsd_key;
38014720Sfr157268 
3802789Sahrens int
3803789Sahrens _init(void)
3804789Sahrens {
3805789Sahrens 	int error;
3806789Sahrens 
3807849Sbonwick 	spa_init(FREAD | FWRITE);
3808849Sbonwick 	zfs_init();
3809849Sbonwick 	zvol_init();
3810849Sbonwick 
3811849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3812849Sbonwick 		zvol_fini();
3813849Sbonwick 		zfs_fini();
3814849Sbonwick 		spa_fini();
3815789Sahrens 		return (error);
3816849Sbonwick 	}
3817789Sahrens 
38184720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
38195326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
38204720Sfr157268 
3821789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3822789Sahrens 	ASSERT(error == 0);
38234543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3824789Sahrens 
3825789Sahrens 	return (0);
3826789Sahrens }
3827789Sahrens 
3828789Sahrens int
3829789Sahrens _fini(void)
3830789Sahrens {
3831789Sahrens 	int error;
3832789Sahrens 
38331544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3834789Sahrens 		return (EBUSY);
3835789Sahrens 
3836789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3837789Sahrens 		return (error);
3838789Sahrens 
3839789Sahrens 	zvol_fini();
3840789Sahrens 	zfs_fini();
3841789Sahrens 	spa_fini();
38425331Samw 	if (zfs_nfsshare_inited)
38434543Smarks 		(void) ddi_modclose(nfs_mod);
38445331Samw 	if (zfs_smbshare_inited)
38455331Samw 		(void) ddi_modclose(smbsrv_mod);
38465331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
38474543Smarks 		(void) ddi_modclose(sharefs_mod);
3848789Sahrens 
38494720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3850789Sahrens 	ldi_ident_release(zfs_li);
3851789Sahrens 	zfs_li = NULL;
38524543Smarks 	mutex_destroy(&zfs_share_lock);
3853789Sahrens 
3854789Sahrens 	return (error);
3855789Sahrens }
3856789Sahrens 
3857789Sahrens int
3858789Sahrens _info(struct modinfo *modinfop)
3859789Sahrens {
3860789Sahrens 	return (mod_info(&modlinkage, modinfop));
3861789Sahrens }
3862