xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 10969:ed9b19e85c90)
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>
44789Sahrens #include <sys/dmu.h>
45789Sahrens #include <sys/dsl_dir.h>
46789Sahrens #include <sys/dsl_dataset.h>
47789Sahrens #include <sys/dsl_prop.h>
484543Smarks #include <sys/dsl_deleg.h>
494543Smarks #include <sys/dmu_objset.h>
50789Sahrens #include <sys/ddi.h>
51789Sahrens #include <sys/sunddi.h>
52789Sahrens #include <sys/sunldi.h>
53789Sahrens #include <sys/policy.h>
54789Sahrens #include <sys/zone.h>
55789Sahrens #include <sys/nvpair.h>
56789Sahrens #include <sys/pathname.h>
57789Sahrens #include <sys/mount.h>
58789Sahrens #include <sys/sdt.h>
59789Sahrens #include <sys/fs/zfs.h>
60789Sahrens #include <sys/zfs_ctldir.h>
615331Samw #include <sys/zfs_dir.h>
622885Sahrens #include <sys/zvol.h>
634543Smarks #include <sharefs/share.h>
645326Sek110237 #include <sys/dmu_objset.h>
65789Sahrens 
66789Sahrens #include "zfs_namecheck.h"
672676Seschrock #include "zfs_prop.h"
684543Smarks #include "zfs_deleg.h"
69789Sahrens 
70789Sahrens extern struct modlfs zfs_modlfs;
71789Sahrens 
72789Sahrens extern void zfs_init(void);
73789Sahrens extern void zfs_fini(void);
74789Sahrens 
75789Sahrens ldi_ident_t zfs_li = NULL;
76789Sahrens dev_info_t *zfs_dip;
77789Sahrens 
78789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
794543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
80789Sahrens 
819234SGeorge.Wilson@Sun.COM typedef enum {
829234SGeorge.Wilson@Sun.COM 	NO_NAME,
839234SGeorge.Wilson@Sun.COM 	POOL_NAME,
849234SGeorge.Wilson@Sun.COM 	DATASET_NAME
859234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
869234SGeorge.Wilson@Sun.COM 
87789Sahrens typedef struct zfs_ioc_vec {
88789Sahrens 	zfs_ioc_func_t		*zvec_func;
89789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
909234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
914543Smarks 	boolean_t		zvec_his_log;
929234SGeorge.Wilson@Sun.COM 	boolean_t		zvec_pool_check;
93789Sahrens } zfs_ioc_vec_t;
94789Sahrens 
959396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
969396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
979396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
989396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
999396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
1009396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
1019396SMatthew.Ahrens@Sun.COM };
1029396SMatthew.Ahrens@Sun.COM 
1039396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
1048536SDavid.Pacheco@Sun.COM static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
1057184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1067184Stimh     boolean_t *);
1077184Stimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
1087184Stimh 
109789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
110789Sahrens void
111789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
112789Sahrens {
113789Sahrens 	const char *newfile;
114789Sahrens 	char buf[256];
115789Sahrens 	va_list adx;
116789Sahrens 
117789Sahrens 	/*
118789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
119789Sahrens 	 */
120789Sahrens 	newfile = strrchr(file, '/');
121789Sahrens 	if (newfile != NULL) {
122789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
123789Sahrens 	} else {
124789Sahrens 		newfile = file;
125789Sahrens 	}
126789Sahrens 
127789Sahrens 	va_start(adx, fmt);
128789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
129789Sahrens 	va_end(adx);
130789Sahrens 
131789Sahrens 	/*
132789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
133789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
134789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
135789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
136789Sahrens 	 * arg0 = file name
137789Sahrens 	 * arg1 = function name
138789Sahrens 	 * arg2 = line number
139789Sahrens 	 * arg3 = message
140789Sahrens 	 */
141789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
142789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
143789Sahrens }
144789Sahrens 
1454543Smarks static void
1464715Sek110237 history_str_free(char *buf)
1474715Sek110237 {
1484715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1494715Sek110237 }
1504715Sek110237 
1514715Sek110237 static char *
1524715Sek110237 history_str_get(zfs_cmd_t *zc)
1534715Sek110237 {
1544715Sek110237 	char *buf;
1554715Sek110237 
1564715Sek110237 	if (zc->zc_history == NULL)
1574715Sek110237 		return (NULL);
1584715Sek110237 
1594715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1604715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1614715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1624715Sek110237 		history_str_free(buf);
1634715Sek110237 		return (NULL);
1644715Sek110237 	}
1654715Sek110237 
1664715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1674715Sek110237 
1684715Sek110237 	return (buf);
1694715Sek110237 }
1704715Sek110237 
1715375Stimh /*
1727042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1737042Sgw25295  */
1747042Sgw25295 static boolean_t
1757042Sgw25295 zfs_is_bootfs(const char *name)
1767042Sgw25295 {
17710298SMatthew.Ahrens@Sun.COM 	objset_t *os;
17810298SMatthew.Ahrens@Sun.COM 
17910298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
18010298SMatthew.Ahrens@Sun.COM 		boolean_t ret;
18110922SJeff.Bonwick@Sun.COM 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
18210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18310298SMatthew.Ahrens@Sun.COM 		return (ret);
1847042Sgw25295 	}
18510298SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1867042Sgw25295 }
1877042Sgw25295 
1887042Sgw25295 /*
1897184Stimh  * zfs_earlier_version
1905375Stimh  *
1915375Stimh  *	Return non-zero if the spa version is less than requested version.
1925375Stimh  */
1935331Samw static int
1947184Stimh zfs_earlier_version(const char *name, int version)
1955331Samw {
1965331Samw 	spa_t *spa;
1975331Samw 
1985331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
1995331Samw 		if (spa_version(spa) < version) {
2005331Samw 			spa_close(spa, FTAG);
2015331Samw 			return (1);
2025331Samw 		}
2035331Samw 		spa_close(spa, FTAG);
2045331Samw 	}
2055331Samw 	return (0);
2065331Samw }
2075331Samw 
2085977Smarks /*
2096689Smaybee  * zpl_earlier_version
2105977Smarks  *
2116689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2125977Smarks  */
2136689Smaybee static boolean_t
2146689Smaybee zpl_earlier_version(const char *name, int version)
2155977Smarks {
2165977Smarks 	objset_t *os;
2176689Smaybee 	boolean_t rc = B_TRUE;
2185977Smarks 
21910298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
2206689Smaybee 		uint64_t zplversion;
2216689Smaybee 
22210298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
22310298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
22410298SMatthew.Ahrens@Sun.COM 			return (B_TRUE);
22510298SMatthew.Ahrens@Sun.COM 		}
22610298SMatthew.Ahrens@Sun.COM 		/* XXX reading from non-owned objset */
2276689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2286689Smaybee 			rc = zplversion < version;
22910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
2305977Smarks 	}
2315977Smarks 	return (rc);
2325977Smarks }
2335977Smarks 
2344715Sek110237 static void
2354543Smarks zfs_log_history(zfs_cmd_t *zc)
2364543Smarks {
2374543Smarks 	spa_t *spa;
2384603Sahrens 	char *buf;
2394543Smarks 
2404715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2414577Sahrens 		return;
2424577Sahrens 
2434715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2444715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2454715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2464715Sek110237 		spa_close(spa, FTAG);
2474543Smarks 	}
2484715Sek110237 	history_str_free(buf);
2494543Smarks }
2504543Smarks 
251789Sahrens /*
252789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
253789Sahrens  * and can be used in the local zone, as there is no associated dataset.
254789Sahrens  */
255789Sahrens /* ARGSUSED */
256789Sahrens static int
2574543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
258789Sahrens {
259789Sahrens 	return (0);
260789Sahrens }
261789Sahrens 
262789Sahrens /*
263789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
264789Sahrens  * no privileges, but must be visible in the local zone.
265789Sahrens  */
266789Sahrens /* ARGSUSED */
267789Sahrens static int
2684543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
269789Sahrens {
270789Sahrens 	if (INGLOBALZONE(curproc) ||
2714543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
272789Sahrens 		return (0);
273789Sahrens 
274789Sahrens 	return (ENOENT);
275789Sahrens }
276789Sahrens 
277789Sahrens static int
278789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
279789Sahrens {
280789Sahrens 	uint64_t zoned;
281789Sahrens 	int writable = 1;
282789Sahrens 
283789Sahrens 	/*
284789Sahrens 	 * The dataset must be visible by this zone -- check this first
285789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
286789Sahrens 	 */
287789Sahrens 	if (!INGLOBALZONE(curproc) &&
288789Sahrens 	    !zone_dataset_visible(dataset, &writable))
289789Sahrens 		return (ENOENT);
290789Sahrens 
291789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
292789Sahrens 		return (ENOENT);
293789Sahrens 
294789Sahrens 	if (INGLOBALZONE(curproc)) {
295789Sahrens 		/*
296789Sahrens 		 * If the fs is zoned, only root can access it from the
297789Sahrens 		 * global zone.
298789Sahrens 		 */
299789Sahrens 		if (secpolicy_zfs(cr) && zoned)
300789Sahrens 			return (EPERM);
301789Sahrens 	} else {
302789Sahrens 		/*
303789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
304789Sahrens 		 */
305789Sahrens 		if (!zoned)
306789Sahrens 			return (EPERM);
307789Sahrens 
308789Sahrens 		/* must be writable by this zone */
309789Sahrens 		if (!writable)
310789Sahrens 			return (EPERM);
311789Sahrens 	}
312789Sahrens 	return (0);
313789Sahrens }
314789Sahrens 
315789Sahrens int
3164543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
317789Sahrens {
318789Sahrens 	int error;
319789Sahrens 
3204543Smarks 	error = zfs_dozonecheck(name, cr);
3214543Smarks 	if (error == 0) {
3224543Smarks 		error = secpolicy_zfs(cr);
3234670Sahrens 		if (error)
3244543Smarks 			error = dsl_deleg_access(name, perm, cr);
3254543Smarks 	}
3264543Smarks 	return (error);
3274543Smarks }
3284543Smarks 
3294543Smarks static int
3304543Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
3314543Smarks {
3324543Smarks 	/*
3334543Smarks 	 * Check permissions for special properties.
3344543Smarks 	 */
3354543Smarks 	switch (prop) {
3364543Smarks 	case ZFS_PROP_ZONED:
3374543Smarks 		/*
3384543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
3394543Smarks 		 */
3404543Smarks 		if (!INGLOBALZONE(curproc))
3414543Smarks 			return (EPERM);
3424543Smarks 		break;
343789Sahrens 
3444543Smarks 	case ZFS_PROP_QUOTA:
3454543Smarks 		if (!INGLOBALZONE(curproc)) {
3464543Smarks 			uint64_t zoned;
3474543Smarks 			char setpoint[MAXNAMELEN];
3484543Smarks 			/*
3494543Smarks 			 * Unprivileged users are allowed to modify the
3504543Smarks 			 * quota on things *under* (ie. contained by)
3514543Smarks 			 * the thing they own.
3524543Smarks 			 */
3534543Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
3544543Smarks 			    setpoint))
3554543Smarks 				return (EPERM);
3564670Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
3574543Smarks 				return (EPERM);
3584543Smarks 		}
3594670Sahrens 		break;
3604543Smarks 	}
3614543Smarks 
3624787Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
363789Sahrens }
364789Sahrens 
3654543Smarks int
3664543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
3674543Smarks {
3684543Smarks 	int error;
3694543Smarks 
3704543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
3714543Smarks 	if (error)
3724543Smarks 		return (error);
3734543Smarks 
3744543Smarks 	/*
3754543Smarks 	 * permission to set permissions will be evaluated later in
3764543Smarks 	 * dsl_deleg_can_allow()
3774543Smarks 	 */
3784543Smarks 	return (0);
3794543Smarks }
3804543Smarks 
3814543Smarks int
3824543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
3834543Smarks {
38410588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
38510588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_ROLLBACK, cr));
3864543Smarks }
3874543Smarks 
3884543Smarks int
3894543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
3904543Smarks {
3914543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
3924543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
3934543Smarks }
3944543Smarks 
3958845Samw@Sun.COM static int
3968845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
3978845Samw@Sun.COM {
3988845Samw@Sun.COM 	vnode_t *vp;
3998845Samw@Sun.COM 	int error;
4008845Samw@Sun.COM 
4018845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4028845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
4038845Samw@Sun.COM 		return (error);
4048845Samw@Sun.COM 
4058845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
4068845Samw@Sun.COM 
4078845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4088845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4098845Samw@Sun.COM 	    zc->zc_name) != 0)) {
4108845Samw@Sun.COM 		VN_RELE(vp);
4118845Samw@Sun.COM 		return (EPERM);
4128845Samw@Sun.COM 	}
4138845Samw@Sun.COM 
4148845Samw@Sun.COM 	VN_RELE(vp);
4158845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
4168845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
4178845Samw@Sun.COM }
4188845Samw@Sun.COM 
4194543Smarks int
4204543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
4214543Smarks {
4224543Smarks 	if (!INGLOBALZONE(curproc))
4234543Smarks 		return (EPERM);
4244543Smarks 
4255367Sahrens 	if (secpolicy_nfs(cr) == 0) {
4264543Smarks 		return (0);
4274543Smarks 	} else {
4288845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4298845Samw@Sun.COM 	}
4308845Samw@Sun.COM }
4318845Samw@Sun.COM 
4328845Samw@Sun.COM int
4338845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
4348845Samw@Sun.COM {
4358845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
4368845Samw@Sun.COM 		return (EPERM);
4378845Samw@Sun.COM 
4388845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
4398845Samw@Sun.COM 		return (0);
4408845Samw@Sun.COM 	} else {
4418845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
4424543Smarks 	}
4434543Smarks }
4444543Smarks 
445789Sahrens static int
4464543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
447789Sahrens {
448789Sahrens 	char *cp;
449789Sahrens 
450789Sahrens 	/*
451789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
452789Sahrens 	 */
4534543Smarks 	(void) strncpy(parent, datasetname, parentsize);
4544543Smarks 	cp = strrchr(parent, '@');
455789Sahrens 	if (cp != NULL) {
456789Sahrens 		cp[0] = '\0';
457789Sahrens 	} else {
4584543Smarks 		cp = strrchr(parent, '/');
459789Sahrens 		if (cp == NULL)
460789Sahrens 			return (ENOENT);
461789Sahrens 		cp[0] = '\0';
462789Sahrens 	}
463789Sahrens 
4644543Smarks 	return (0);
4654543Smarks }
4664543Smarks 
4674543Smarks int
4684543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
4694543Smarks {
4704543Smarks 	int error;
4714543Smarks 
4724543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
4734543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
4744543Smarks 		return (error);
4754543Smarks 
4764543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
4774543Smarks }
4784543Smarks 
4794543Smarks static int
4804543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
4814543Smarks {
4824543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
4834543Smarks }
4844543Smarks 
4854543Smarks /*
4864543Smarks  * Must have sys_config privilege to check the iscsi permission
4874543Smarks  */
4884543Smarks /* ARGSUSED */
4894543Smarks static int
4904543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
4914543Smarks {
4924543Smarks 	return (secpolicy_zfs(cr));
4934543Smarks }
4944543Smarks 
4954543Smarks int
4964543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
4974543Smarks {
4984543Smarks 	char 	parentname[MAXNAMELEN];
4994543Smarks 	int	error;
5004543Smarks 
5014543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5024543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
5034543Smarks 		return (error);
5044543Smarks 
5054543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
5064543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5074543Smarks 		return (error);
5084543Smarks 
5094543Smarks 	if ((error = zfs_get_parent(to, parentname,
5104543Smarks 	    sizeof (parentname))) != 0)
5114543Smarks 		return (error);
5124543Smarks 
5134543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5144543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
5154543Smarks 		return (error);
5164543Smarks 
5174543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
5184543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5194543Smarks 		return (error);
5204543Smarks 
5214543Smarks 	return (error);
5224543Smarks }
5234543Smarks 
5244543Smarks static int
5254543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
5264543Smarks {
5274543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
5284543Smarks }
5294543Smarks 
5304543Smarks static int
5314543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
5324543Smarks {
5334543Smarks 	char 	parentname[MAXNAMELEN];
5344543Smarks 	objset_t *clone;
5354543Smarks 	int error;
5364543Smarks 
5374543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
5384543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
5394543Smarks 	if (error)
5404543Smarks 		return (error);
5414543Smarks 
54210298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
5434543Smarks 
5444543Smarks 	if (error == 0) {
5454543Smarks 		dsl_dataset_t *pclone = NULL;
5464543Smarks 		dsl_dir_t *dd;
54710298SMatthew.Ahrens@Sun.COM 		dd = clone->os_dsl_dataset->ds_dir;
5484543Smarks 
5494543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5506689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
5516689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
5524543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
5534543Smarks 		if (error) {
55410298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(clone, FTAG);
5554543Smarks 			return (error);
5564543Smarks 		}
5574543Smarks 
5584543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
5594543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
5604543Smarks 
5614543Smarks 		dsl_dataset_name(pclone, parentname);
56210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
5636689Smaybee 		dsl_dataset_rele(pclone, FTAG);
5644543Smarks 		if (error == 0)
5654543Smarks 			error = zfs_secpolicy_write_perms(parentname,
5664543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
5674543Smarks 	}
5684543Smarks 	return (error);
5694543Smarks }
5704543Smarks 
5714543Smarks static int
5724543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
5734543Smarks {
5744543Smarks 	int error;
5754543Smarks 
5764543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5774543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
5784543Smarks 		return (error);
5794543Smarks 
5804543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
5814543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5824543Smarks 		return (error);
5834543Smarks 
5844543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5854543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
5864543Smarks }
5874543Smarks 
5884543Smarks int
5894543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
5904543Smarks {
59110588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(name,
59210588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
5934543Smarks }
5944543Smarks 
5954543Smarks static int
5964543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
5974543Smarks {
5984543Smarks 
5994543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
6004543Smarks }
6014543Smarks 
6024543Smarks static int
6034543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
6044543Smarks {
6054543Smarks 	char 	parentname[MAXNAMELEN];
6064543Smarks 	int 	error;
6074543Smarks 
6084543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
6094543Smarks 	    sizeof (parentname))) != 0)
6104543Smarks 		return (error);
6114543Smarks 
6124543Smarks 	if (zc->zc_value[0] != '\0') {
6134543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
6144543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
6154543Smarks 			return (error);
6164543Smarks 	}
6174543Smarks 
6184543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6194543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6204543Smarks 		return (error);
6214543Smarks 
6224543Smarks 	error = zfs_secpolicy_write_perms(parentname,
6234543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
6244543Smarks 
6254543Smarks 	return (error);
6264543Smarks }
6274543Smarks 
6284543Smarks static int
6294543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
6304543Smarks {
6314543Smarks 	int error;
6324543Smarks 
6334543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
6344543Smarks 	if (error) {
6354543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
6364543Smarks 	}
6374543Smarks 	return (error);
638789Sahrens }
639789Sahrens 
640789Sahrens /*
641789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
642789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
643789Sahrens  */
644789Sahrens /* ARGSUSED */
645789Sahrens static int
6464543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
647789Sahrens {
648789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
649789Sahrens 		return (EPERM);
650789Sahrens 
651789Sahrens 	return (0);
652789Sahrens }
653789Sahrens 
654789Sahrens /*
6551544Seschrock  * Policy for fault injection.  Requires all privileges.
6561544Seschrock  */
6571544Seschrock /* ARGSUSED */
6581544Seschrock static int
6594543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
6601544Seschrock {
6611544Seschrock 	return (secpolicy_zinject(cr));
6621544Seschrock }
6631544Seschrock 
6644849Sahrens static int
6654849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
6664849Sahrens {
6674849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
6684849Sahrens 
6695094Slling 	if (prop == ZPROP_INVAL) {
6704849Sahrens 		if (!zfs_prop_user(zc->zc_value))
6714849Sahrens 			return (EINVAL);
6724849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
6734849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
6744849Sahrens 	} else {
6754849Sahrens 		if (!zfs_prop_inheritable(prop))
6764849Sahrens 			return (EINVAL);
6774849Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
6784849Sahrens 	}
6794849Sahrens }
6804849Sahrens 
6819396SMatthew.Ahrens@Sun.COM static int
6829396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
6839396SMatthew.Ahrens@Sun.COM {
6849396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
6859396SMatthew.Ahrens@Sun.COM 	if (err)
6869396SMatthew.Ahrens@Sun.COM 		return (err);
6879396SMatthew.Ahrens@Sun.COM 
6889396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
6899396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
6909396SMatthew.Ahrens@Sun.COM 
6919396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
6929396SMatthew.Ahrens@Sun.COM 		/*
6939396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
6949396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
6959396SMatthew.Ahrens@Sun.COM 		 */
6969396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
6979396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
6989396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
6999396SMatthew.Ahrens@Sun.COM 				return (0);
7009396SMatthew.Ahrens@Sun.COM 		} else {
7019396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
7029396SMatthew.Ahrens@Sun.COM 				return (0);
7039396SMatthew.Ahrens@Sun.COM 		}
7049396SMatthew.Ahrens@Sun.COM 	}
7059396SMatthew.Ahrens@Sun.COM 
7069396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7079396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7089396SMatthew.Ahrens@Sun.COM }
7099396SMatthew.Ahrens@Sun.COM 
7109396SMatthew.Ahrens@Sun.COM static int
7119396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
7129396SMatthew.Ahrens@Sun.COM {
7139396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
7149396SMatthew.Ahrens@Sun.COM 	if (err)
7159396SMatthew.Ahrens@Sun.COM 		return (err);
7169396SMatthew.Ahrens@Sun.COM 
7179396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
7189396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7199396SMatthew.Ahrens@Sun.COM 
7209396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
7219396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
7229396SMatthew.Ahrens@Sun.COM }
7239396SMatthew.Ahrens@Sun.COM 
7249396SMatthew.Ahrens@Sun.COM static int
7259396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
7269396SMatthew.Ahrens@Sun.COM {
7279396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
7289396SMatthew.Ahrens@Sun.COM }
7299396SMatthew.Ahrens@Sun.COM 
73010242Schris.kirby@sun.com static int
73110242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
73210242Schris.kirby@sun.com {
73310242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
73410242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
73510242Schris.kirby@sun.com }
73610242Schris.kirby@sun.com 
73710242Schris.kirby@sun.com static int
73810242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
73910242Schris.kirby@sun.com {
74010242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
74110242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
74210242Schris.kirby@sun.com }
74310242Schris.kirby@sun.com 
7441544Seschrock /*
745789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
746789Sahrens  */
747789Sahrens static int
7489643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
749789Sahrens {
750789Sahrens 	char *packed;
751789Sahrens 	int error;
7525094Slling 	nvlist_t *list = NULL;
753789Sahrens 
754789Sahrens 	/*
7552676Seschrock 	 * Read in and unpack the user-supplied nvlist.
756789Sahrens 	 */
7575094Slling 	if (size == 0)
758789Sahrens 		return (EINVAL);
759789Sahrens 
760789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
761789Sahrens 
7629643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
7639643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
764789Sahrens 		kmem_free(packed, size);
765789Sahrens 		return (error);
766789Sahrens 	}
767789Sahrens 
7685094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
769789Sahrens 		kmem_free(packed, size);
770789Sahrens 		return (error);
771789Sahrens 	}
772789Sahrens 
773789Sahrens 	kmem_free(packed, size);
774789Sahrens 
7755094Slling 	*nvp = list;
776789Sahrens 	return (0);
777789Sahrens }
778789Sahrens 
779789Sahrens static int
7802676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
7812676Seschrock {
7822676Seschrock 	char *packed = NULL;
7832676Seschrock 	size_t size;
7842676Seschrock 	int error;
7852676Seschrock 
7862676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
7872676Seschrock 
7882676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
7892676Seschrock 		error = ENOMEM;
7902676Seschrock 	} else {
7914611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
7922676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
7932676Seschrock 		    KM_SLEEP) == 0);
7949643SEric.Taylor@Sun.COM 		error = ddi_copyout(packed,
7959643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
7962676Seschrock 		kmem_free(packed, size);
7972676Seschrock 	}
7982676Seschrock 
7992676Seschrock 	zc->zc_nvlist_dst_size = size;
8002676Seschrock 	return (error);
8012676Seschrock }
8022676Seschrock 
8032676Seschrock static int
8049396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp)
8059396SMatthew.Ahrens@Sun.COM {
8069396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8079396SMatthew.Ahrens@Sun.COM 	int error;
8089396SMatthew.Ahrens@Sun.COM 
80910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(dsname, FTAG, &os);
8109396SMatthew.Ahrens@Sun.COM 	if (error)
8119396SMatthew.Ahrens@Sun.COM 		return (error);
81210298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
81310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
81410298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
81510298SMatthew.Ahrens@Sun.COM 	}
81610298SMatthew.Ahrens@Sun.COM 
81710298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_user_ptr_lock);
8189396SMatthew.Ahrens@Sun.COM 	*zvp = dmu_objset_get_user(os);
8199396SMatthew.Ahrens@Sun.COM 	if (*zvp) {
8209396SMatthew.Ahrens@Sun.COM 		VFS_HOLD((*zvp)->z_vfs);
8219396SMatthew.Ahrens@Sun.COM 	} else {
8229396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
8239396SMatthew.Ahrens@Sun.COM 	}
82410298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_user_ptr_lock);
82510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
8269396SMatthew.Ahrens@Sun.COM 	return (error);
8279396SMatthew.Ahrens@Sun.COM }
8289396SMatthew.Ahrens@Sun.COM 
8299396SMatthew.Ahrens@Sun.COM /*
8309396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
8319396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
8329396SMatthew.Ahrens@Sun.COM  */
8339396SMatthew.Ahrens@Sun.COM static int
83410298SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp)
8359396SMatthew.Ahrens@Sun.COM {
8369396SMatthew.Ahrens@Sun.COM 	int error = 0;
8379396SMatthew.Ahrens@Sun.COM 
8389396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(name, zvp) != 0)
83910298SMatthew.Ahrens@Sun.COM 		error = zfsvfs_create(name, zvp);
8409396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
8419396SMatthew.Ahrens@Sun.COM 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
8429396SMatthew.Ahrens@Sun.COM 		if ((*zvp)->z_unmounted) {
8439396SMatthew.Ahrens@Sun.COM 			/*
8449396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
8459396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
8469396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
8479396SMatthew.Ahrens@Sun.COM 			 */
8489396SMatthew.Ahrens@Sun.COM 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
8499396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
8509396SMatthew.Ahrens@Sun.COM 		}
8519396SMatthew.Ahrens@Sun.COM 	}
8529396SMatthew.Ahrens@Sun.COM 	return (error);
8539396SMatthew.Ahrens@Sun.COM }
8549396SMatthew.Ahrens@Sun.COM 
8559396SMatthew.Ahrens@Sun.COM static void
8569396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
8579396SMatthew.Ahrens@Sun.COM {
8589396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
8599396SMatthew.Ahrens@Sun.COM 
8609396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
8619396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
8629396SMatthew.Ahrens@Sun.COM 	} else {
86310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
8649396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
8659396SMatthew.Ahrens@Sun.COM 	}
8669396SMatthew.Ahrens@Sun.COM }
8679396SMatthew.Ahrens@Sun.COM 
8689396SMatthew.Ahrens@Sun.COM static int
869789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
870789Sahrens {
871789Sahrens 	int error;
8725094Slling 	nvlist_t *config, *props = NULL;
8737184Stimh 	nvlist_t *rootprops = NULL;
8747184Stimh 	nvlist_t *zplprops = NULL;
8754715Sek110237 	char *buf;
876789Sahrens 
8775094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
8789643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
8794988Sek110237 		return (error);
8804715Sek110237 
8815094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
8829643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
8839643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
8845094Slling 		nvlist_free(config);
8855094Slling 		return (error);
8865094Slling 	}
8875094Slling 
8887184Stimh 	if (props) {
8897184Stimh 		nvlist_t *nvl = NULL;
8907184Stimh 		uint64_t version = SPA_VERSION;
8917184Stimh 
8927184Stimh 		(void) nvlist_lookup_uint64(props,
8937184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
8947184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
8957184Stimh 			error = EINVAL;
8967184Stimh 			goto pool_props_bad;
8977184Stimh 		}
8987184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
8997184Stimh 		if (nvl) {
9007184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9017184Stimh 			if (error != 0) {
9027184Stimh 				nvlist_free(config);
9037184Stimh 				nvlist_free(props);
9047184Stimh 				return (error);
9057184Stimh 			}
9067184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9077184Stimh 		}
9087184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9097184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
9107184Stimh 		    zplprops, NULL);
9117184Stimh 		if (error)
9127184Stimh 			goto pool_props_bad;
9137184Stimh 	}
9147184Stimh 
9154988Sek110237 	buf = history_str_get(zc);
916789Sahrens 
9177184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9187184Stimh 
9197184Stimh 	/*
9207184Stimh 	 * Set the remaining root properties
9217184Stimh 	 */
9227184Stimh 	if (!error &&
9237184Stimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9247184Stimh 		(void) spa_destroy(zc->zc_name);
925789Sahrens 
9264988Sek110237 	if (buf != NULL)
9274988Sek110237 		history_str_free(buf);
9285094Slling 
9297184Stimh pool_props_bad:
9307184Stimh 	nvlist_free(rootprops);
9317184Stimh 	nvlist_free(zplprops);
932789Sahrens 	nvlist_free(config);
9337184Stimh 	nvlist_free(props);
9345094Slling 
935789Sahrens 	return (error);
936789Sahrens }
937789Sahrens 
938789Sahrens static int
939789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
940789Sahrens {
9414543Smarks 	int error;
9424543Smarks 	zfs_log_history(zc);
9434543Smarks 	error = spa_destroy(zc->zc_name);
94410588SEric.Taylor@Sun.COM 	if (error == 0)
94510588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
9464543Smarks 	return (error);
947789Sahrens }
948789Sahrens 
949789Sahrens static int
950789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
951789Sahrens {
9525094Slling 	nvlist_t *config, *props = NULL;
953789Sahrens 	uint64_t guid;
95410921STim.Haley@Sun.COM 	int error;
955789Sahrens 
9565094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9579643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
958789Sahrens 		return (error);
959789Sahrens 
9605094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
9619643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9629643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
9635094Slling 		nvlist_free(config);
9645094Slling 		return (error);
9655094Slling 	}
9665094Slling 
967789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
9681544Seschrock 	    guid != zc->zc_guid)
969789Sahrens 		error = EINVAL;
9706643Seschrock 	else if (zc->zc_cookie)
97110921STim.Haley@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config, props);
972789Sahrens 	else
9735094Slling 		error = spa_import(zc->zc_name, config, props);
974789Sahrens 
97510921STim.Haley@Sun.COM 	if (zc->zc_nvlist_dst != 0)
97610921STim.Haley@Sun.COM 		(void) put_nvlist(zc, config);
97710921STim.Haley@Sun.COM 
978789Sahrens 	nvlist_free(config);
979789Sahrens 
9805094Slling 	if (props)
9815094Slling 		nvlist_free(props);
9825094Slling 
983789Sahrens 	return (error);
984789Sahrens }
985789Sahrens 
986789Sahrens static int
987789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
988789Sahrens {
9894543Smarks 	int error;
9907214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
9918211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
9927214Slling 
9934543Smarks 	zfs_log_history(zc);
9948211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
99510588SEric.Taylor@Sun.COM 	if (error == 0)
99610588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
9974543Smarks 	return (error);
998789Sahrens }
999789Sahrens 
1000789Sahrens static int
1001789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1002789Sahrens {
1003789Sahrens 	nvlist_t *configs;
1004789Sahrens 	int error;
1005789Sahrens 
1006789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1007789Sahrens 		return (EEXIST);
1008789Sahrens 
10092676Seschrock 	error = put_nvlist(zc, configs);
1010789Sahrens 
1011789Sahrens 	nvlist_free(configs);
1012789Sahrens 
1013789Sahrens 	return (error);
1014789Sahrens }
1015789Sahrens 
1016789Sahrens static int
1017789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1018789Sahrens {
1019789Sahrens 	nvlist_t *config;
1020789Sahrens 	int error;
10211544Seschrock 	int ret = 0;
1022789Sahrens 
10232676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
10242676Seschrock 	    sizeof (zc->zc_value));
1025789Sahrens 
1026789Sahrens 	if (config != NULL) {
10272676Seschrock 		ret = put_nvlist(zc, config);
1028789Sahrens 		nvlist_free(config);
10291544Seschrock 
10301544Seschrock 		/*
10311544Seschrock 		 * The config may be present even if 'error' is non-zero.
10321544Seschrock 		 * In this case we return success, and preserve the real errno
10331544Seschrock 		 * in 'zc_cookie'.
10341544Seschrock 		 */
10351544Seschrock 		zc->zc_cookie = error;
1036789Sahrens 	} else {
10371544Seschrock 		ret = error;
1038789Sahrens 	}
1039789Sahrens 
10401544Seschrock 	return (ret);
1041789Sahrens }
1042789Sahrens 
1043789Sahrens /*
1044789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1045789Sahrens  * user land knows which devices are available and overall pool health.
1046789Sahrens  */
1047789Sahrens static int
1048789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1049789Sahrens {
1050789Sahrens 	nvlist_t *tryconfig, *config;
1051789Sahrens 	int error;
1052789Sahrens 
10535094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10549643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1055789Sahrens 		return (error);
1056789Sahrens 
1057789Sahrens 	config = spa_tryimport(tryconfig);
1058789Sahrens 
1059789Sahrens 	nvlist_free(tryconfig);
1060789Sahrens 
1061789Sahrens 	if (config == NULL)
1062789Sahrens 		return (EINVAL);
1063789Sahrens 
10642676Seschrock 	error = put_nvlist(zc, config);
1065789Sahrens 	nvlist_free(config);
1066789Sahrens 
1067789Sahrens 	return (error);
1068789Sahrens }
1069789Sahrens 
1070789Sahrens static int
1071789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1072789Sahrens {
1073789Sahrens 	spa_t *spa;
1074789Sahrens 	int error;
1075789Sahrens 
10762926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
10772926Sek110237 		return (error);
10782926Sek110237 
10797046Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
10802926Sek110237 
10812926Sek110237 	spa_close(spa, FTAG);
10822926Sek110237 
1083789Sahrens 	return (error);
1084789Sahrens }
1085789Sahrens 
1086789Sahrens static int
1087789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1088789Sahrens {
1089789Sahrens 	spa_t *spa;
1090789Sahrens 	int error;
1091789Sahrens 
1092789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1093789Sahrens 	if (error == 0) {
1094789Sahrens 		spa_freeze(spa);
1095789Sahrens 		spa_close(spa, FTAG);
1096789Sahrens 	}
1097789Sahrens 	return (error);
1098789Sahrens }
1099789Sahrens 
1100789Sahrens static int
11011760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
11021760Seschrock {
11031760Seschrock 	spa_t *spa;
11041760Seschrock 	int error;
11051760Seschrock 
11062926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11072926Sek110237 		return (error);
11082926Sek110237 
11095118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
11105118Slling 		spa_close(spa, FTAG);
11115118Slling 		return (EINVAL);
11125118Slling 	}
11135118Slling 
11145094Slling 	spa_upgrade(spa, zc->zc_cookie);
11152926Sek110237 	spa_close(spa, FTAG);
11162926Sek110237 
11172926Sek110237 	return (error);
11182926Sek110237 }
11192926Sek110237 
11202926Sek110237 static int
11212926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
11222926Sek110237 {
11232926Sek110237 	spa_t *spa;
11242926Sek110237 	char *hist_buf;
11252926Sek110237 	uint64_t size;
11262926Sek110237 	int error;
11272926Sek110237 
11282926Sek110237 	if ((size = zc->zc_history_len) == 0)
11292926Sek110237 		return (EINVAL);
11302926Sek110237 
11312926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
11322926Sek110237 		return (error);
11332926Sek110237 
11344577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
11353863Sek110237 		spa_close(spa, FTAG);
11363863Sek110237 		return (ENOTSUP);
11373863Sek110237 	}
11383863Sek110237 
11392926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
11402926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
11412926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
11429643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
11439643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
11449643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
11452926Sek110237 	}
11462926Sek110237 
11472926Sek110237 	spa_close(spa, FTAG);
11482926Sek110237 	kmem_free(hist_buf, size);
11492926Sek110237 	return (error);
11502926Sek110237 }
11512926Sek110237 
11522926Sek110237 static int
11533444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
11543444Sek110237 {
11553444Sek110237 	int error;
11563444Sek110237 
11573912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
11583444Sek110237 		return (error);
11593444Sek110237 
11603444Sek110237 	return (0);
11613444Sek110237 }
11623444Sek110237 
116310298SMatthew.Ahrens@Sun.COM /*
116410298SMatthew.Ahrens@Sun.COM  * inputs:
116510298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
116610298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
116710298SMatthew.Ahrens@Sun.COM  *
116810298SMatthew.Ahrens@Sun.COM  * outputs:
116910298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
117010298SMatthew.Ahrens@Sun.COM  */
11713444Sek110237 static int
11723444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
11733444Sek110237 {
117410298SMatthew.Ahrens@Sun.COM 	objset_t *os;
11753444Sek110237 	int error;
11763444Sek110237 
117710298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
117810298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
11793444Sek110237 		return (error);
118010298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
118110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
118210298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
118310298SMatthew.Ahrens@Sun.COM 	}
118410298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
11853444Sek110237 	    sizeof (zc->zc_value));
118610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
11873444Sek110237 
11883444Sek110237 	return (error);
11893444Sek110237 }
11903444Sek110237 
11913444Sek110237 static int
1192789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1193789Sahrens {
1194789Sahrens 	spa_t *spa;
1195789Sahrens 	int error;
11966423Sgw25295 	nvlist_t *config, **l2cache, **spares;
11976423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1198789Sahrens 
1199789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1200789Sahrens 	if (error != 0)
1201789Sahrens 		return (error);
1202789Sahrens 
12035450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12049643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
12055450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
12065450Sbrendan 	    &l2cache, &nl2cache);
12075450Sbrendan 
12086423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
12096423Sgw25295 	    &spares, &nspares);
12106423Sgw25295 
12113912Slling 	/*
12123912Slling 	 * A root pool with concatenated devices is not supported.
12136423Sgw25295 	 * Thus, can not add a device to a root pool.
12146423Sgw25295 	 *
12156423Sgw25295 	 * Intent log device can not be added to a rootpool because
12166423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
12176423Sgw25295 	 * can not be accessed during the mountroot time.
12186423Sgw25295 	 *
12196423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
12203912Slling 	 */
122110922SJeff.Bonwick@Sun.COM 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
12223912Slling 		spa_close(spa, FTAG);
12233912Slling 		return (EDOM);
12243912Slling 	}
12253912Slling 
12265450Sbrendan 	if (error == 0) {
1227789Sahrens 		error = spa_vdev_add(spa, config);
1228789Sahrens 		nvlist_free(config);
1229789Sahrens 	}
1230789Sahrens 	spa_close(spa, FTAG);
1231789Sahrens 	return (error);
1232789Sahrens }
1233789Sahrens 
1234789Sahrens static int
1235789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1236789Sahrens {
12372082Seschrock 	spa_t *spa;
12382082Seschrock 	int error;
12392082Seschrock 
12402082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
12412082Seschrock 	if (error != 0)
12422082Seschrock 		return (error);
12432082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
12442082Seschrock 	spa_close(spa, FTAG);
12452082Seschrock 	return (error);
1246789Sahrens }
1247789Sahrens 
1248789Sahrens static int
12494451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1250789Sahrens {
1251789Sahrens 	spa_t *spa;
1252789Sahrens 	int error;
12534451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1254789Sahrens 
12552926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1256789Sahrens 		return (error);
12574451Seschrock 	switch (zc->zc_cookie) {
12584451Seschrock 	case VDEV_STATE_ONLINE:
12594451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12604451Seschrock 		break;
12614451Seschrock 
12624451Seschrock 	case VDEV_STATE_OFFLINE:
12634451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12644451Seschrock 		break;
1265789Sahrens 
12664451Seschrock 	case VDEV_STATE_FAULTED:
126710817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
126810817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
126910817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
127010817SEric.Schrock@Sun.COM 
127110817SEric.Schrock@Sun.COM 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
12724451Seschrock 		break;
1273789Sahrens 
12744451Seschrock 	case VDEV_STATE_DEGRADED:
127510817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
127610817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
127710817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
127810817SEric.Schrock@Sun.COM 
127910817SEric.Schrock@Sun.COM 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
12804451Seschrock 		break;
12814451Seschrock 
12824451Seschrock 	default:
12834451Seschrock 		error = EINVAL;
12844451Seschrock 	}
12854451Seschrock 	zc->zc_cookie = newstate;
1286789Sahrens 	spa_close(spa, FTAG);
1287789Sahrens 	return (error);
1288789Sahrens }
1289789Sahrens 
1290789Sahrens static int
1291789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1292789Sahrens {
1293789Sahrens 	spa_t *spa;
1294789Sahrens 	int replacing = zc->zc_cookie;
1295789Sahrens 	nvlist_t *config;
1296789Sahrens 	int error;
1297789Sahrens 
12982926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1299789Sahrens 		return (error);
1300789Sahrens 
13015094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13029643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
13031544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1304789Sahrens 		nvlist_free(config);
1305789Sahrens 	}
1306789Sahrens 
1307789Sahrens 	spa_close(spa, FTAG);
1308789Sahrens 	return (error);
1309789Sahrens }
1310789Sahrens 
1311789Sahrens static int
1312789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1313789Sahrens {
1314789Sahrens 	spa_t *spa;
1315789Sahrens 	int error;
1316789Sahrens 
13172926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1318789Sahrens 		return (error);
1319789Sahrens 
13208241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1321789Sahrens 
1322789Sahrens 	spa_close(spa, FTAG);
1323789Sahrens 	return (error);
1324789Sahrens }
1325789Sahrens 
1326789Sahrens static int
13271354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
13281354Seschrock {
13291354Seschrock 	spa_t *spa;
13302676Seschrock 	char *path = zc->zc_value;
13311544Seschrock 	uint64_t guid = zc->zc_guid;
13321354Seschrock 	int error;
13331354Seschrock 
13341354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13351354Seschrock 	if (error != 0)
13361354Seschrock 		return (error);
13371354Seschrock 
13381354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
13391354Seschrock 	spa_close(spa, FTAG);
13401354Seschrock 	return (error);
13411354Seschrock }
13421354Seschrock 
13439425SEric.Schrock@Sun.COM static int
13449425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13459425SEric.Schrock@Sun.COM {
13469425SEric.Schrock@Sun.COM 	spa_t *spa;
13479425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
13489425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
13499425SEric.Schrock@Sun.COM 	int error;
13509425SEric.Schrock@Sun.COM 
13519425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
13529425SEric.Schrock@Sun.COM 	if (error != 0)
13539425SEric.Schrock@Sun.COM 		return (error);
13549425SEric.Schrock@Sun.COM 
13559425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
13569425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
13579425SEric.Schrock@Sun.COM 	return (error);
13589425SEric.Schrock@Sun.COM }
13599425SEric.Schrock@Sun.COM 
13605367Sahrens /*
13615367Sahrens  * inputs:
13625367Sahrens  * zc_name		name of filesystem
13635367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13645367Sahrens  *
13655367Sahrens  * outputs:
13665367Sahrens  * zc_objset_stats	stats
13675367Sahrens  * zc_nvlist_dst	property nvlist
13685367Sahrens  * zc_nvlist_dst_size	size of property nvlist
13695367Sahrens  */
13701354Seschrock static int
1371789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1372789Sahrens {
1373789Sahrens 	objset_t *os = NULL;
1374789Sahrens 	int error;
13751356Seschrock 	nvlist_t *nv;
1376789Sahrens 
137710298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1378789Sahrens 		return (error);
1379789Sahrens 
13802885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1381789Sahrens 
13822856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
13836689Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
13842885Sahrens 		dmu_objset_stats(os, nv);
13853087Sahrens 		/*
13865147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
13873087Sahrens 		 * which we aren't supposed to do with a
13886689Smaybee 		 * DS_MODE_USER hold, because it could be
13893087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
139010298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
13913087Sahrens 		 */
13924577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
13934577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
13944577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
13954577Sahrens 		}
13962676Seschrock 		error = put_nvlist(zc, nv);
13971356Seschrock 		nvlist_free(nv);
13981356Seschrock 	}
1399789Sahrens 
140010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1401789Sahrens 	return (error);
1402789Sahrens }
1403789Sahrens 
14045498Stimh static int
14055498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
14065498Stimh {
14075498Stimh 	uint64_t value;
14085498Stimh 	int error;
14095498Stimh 
14105498Stimh 	/*
14115498Stimh 	 * zfs_get_zplprop() will either find a value or give us
14125498Stimh 	 * the default value (if there is one).
14135498Stimh 	 */
14145498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
14155498Stimh 		return (error);
14165498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
14175498Stimh 	return (0);
14185498Stimh }
14195498Stimh 
14205498Stimh /*
14215498Stimh  * inputs:
14225498Stimh  * zc_name		name of filesystem
14235498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14245498Stimh  *
14255498Stimh  * outputs:
14265498Stimh  * zc_nvlist_dst	zpl property nvlist
14275498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14285498Stimh  */
14295498Stimh static int
14305498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
14315498Stimh {
14325498Stimh 	objset_t *os;
14335498Stimh 	int err;
14345498Stimh 
143510298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
143610298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
14375498Stimh 		return (err);
14385498Stimh 
14395498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
14405498Stimh 
14415498Stimh 	/*
14425498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
14436689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
14446689Smaybee 	 * hold, because it could be inconsistent.
14455498Stimh 	 */
14465498Stimh 	if (zc->zc_nvlist_dst != NULL &&
14475498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
14485498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
14495498Stimh 		nvlist_t *nv;
14505498Stimh 
14515498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14525498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
14535498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
14545498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
14555498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
14565498Stimh 			err = put_nvlist(zc, nv);
14575498Stimh 		nvlist_free(nv);
14585498Stimh 	} else {
14595498Stimh 		err = ENOENT;
14605498Stimh 	}
146110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
14625498Stimh 	return (err);
14635498Stimh }
14645498Stimh 
14659396SMatthew.Ahrens@Sun.COM static boolean_t
14669396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
14679396SMatthew.Ahrens@Sun.COM {
14689396SMatthew.Ahrens@Sun.COM 	/*
14699396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
14709396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
14719396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
14729396SMatthew.Ahrens@Sun.COM 	 */
14739396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
14749396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14759396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
14769396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14779396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
14789396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
14799396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
14809396SMatthew.Ahrens@Sun.COM }
14819396SMatthew.Ahrens@Sun.COM 
14825367Sahrens /*
14835367Sahrens  * inputs:
14845367Sahrens  * zc_name		name of filesystem
14855367Sahrens  * zc_cookie		zap cursor
14865367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
14875367Sahrens  *
14885367Sahrens  * outputs:
14895367Sahrens  * zc_name		name of next filesystem
14909396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
14915367Sahrens  * zc_objset_stats	stats
14925367Sahrens  * zc_nvlist_dst	property nvlist
14935367Sahrens  * zc_nvlist_dst_size	size of property nvlist
14945367Sahrens  */
1495789Sahrens static int
1496789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1497789Sahrens {
1498885Sahrens 	objset_t *os;
1499789Sahrens 	int error;
1500789Sahrens 	char *p;
1501789Sahrens 
150210298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1503885Sahrens 		if (error == ENOENT)
1504885Sahrens 			error = ESRCH;
1505885Sahrens 		return (error);
1506789Sahrens 	}
1507789Sahrens 
1508789Sahrens 	p = strrchr(zc->zc_name, '/');
1509789Sahrens 	if (p == NULL || p[1] != '\0')
1510789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1511789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1512789Sahrens 
15138697SRichard.Morris@Sun.COM 	/*
15148697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15158697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
15168697SRichard.Morris@Sun.COM 	 */
15178415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
15188415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
15198415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15208415SRichard.Morris@Sun.COM 
15218415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15228697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
15238415SRichard.Morris@Sun.COM 	}
15248415SRichard.Morris@Sun.COM 
1525789Sahrens 	do {
1526885Sahrens 		error = dmu_dir_list_next(os,
1527885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1528885Sahrens 		    NULL, &zc->zc_cookie);
1529789Sahrens 		if (error == ENOENT)
1530789Sahrens 			error = ESRCH;
153110588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
153210588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
153310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1534789Sahrens 
153510588SEric.Taylor@Sun.COM 	/*
153610588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
153710588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
153810588SEric.Taylor@Sun.COM 	 */
153910588SEric.Taylor@Sun.COM 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1540885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1541789Sahrens 	return (error);
1542789Sahrens }
1543789Sahrens 
15445367Sahrens /*
15455367Sahrens  * inputs:
15465367Sahrens  * zc_name		name of filesystem
15475367Sahrens  * zc_cookie		zap cursor
15485367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15495367Sahrens  *
15505367Sahrens  * outputs:
15515367Sahrens  * zc_name		name of next snapshot
15525367Sahrens  * zc_objset_stats	stats
15535367Sahrens  * zc_nvlist_dst	property nvlist
15545367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15555367Sahrens  */
1556789Sahrens static int
1557789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1558789Sahrens {
1559885Sahrens 	objset_t *os;
1560789Sahrens 	int error;
1561789Sahrens 
156210474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
156310474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
156410474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
156510474SRichard.Morris@Sun.COM 
156610298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
15676689Smaybee 	if (error)
15686689Smaybee 		return (error == ENOENT ? ESRCH : error);
1569789Sahrens 
15701003Slling 	/*
15711003Slling 	 * A dataset name of maximum length cannot have any snapshots,
15721003Slling 	 * so exit immediately.
15731003Slling 	 */
15741003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
157510298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
15761003Slling 		return (ESRCH);
1577789Sahrens 	}
1578789Sahrens 
1579885Sahrens 	error = dmu_snapshot_list_next(os,
1580885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
15815663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
158210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1583885Sahrens 	if (error == 0)
1584885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
15856689Smaybee 	else if (error == ENOENT)
15866689Smaybee 		error = ESRCH;
1587789Sahrens 
15885367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
15896689Smaybee 	if (error)
15905367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1591789Sahrens 	return (error);
1592789Sahrens }
1593789Sahrens 
15946423Sgw25295 int
15954787Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1596789Sahrens {
15972676Seschrock 	nvpair_t *elem;
15988724SRichard.Morris@Sun.COM 	int error = 0;
15992676Seschrock 	uint64_t intval;
16002676Seschrock 	char *strval;
16018697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
16029396SMatthew.Ahrens@Sun.COM 	boolean_t issnap = (strchr(name, '@') != NULL);
16032676Seschrock 
16044543Smarks 	/*
16054543Smarks 	 * First validate permission to set all of the properties
16064543Smarks 	 */
16072676Seschrock 	elem = NULL;
16082676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
16094670Sahrens 		const char *propname = nvpair_name(elem);
16104670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
16112676Seschrock 
16125094Slling 		if (prop == ZPROP_INVAL) {
16132676Seschrock 			/*
16142676Seschrock 			 * If this is a user-defined property, it must be a
16152676Seschrock 			 * string, and there is no further validation to do.
16162676Seschrock 			 */
16179396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_user(propname) &&
16189396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_STRING) {
16199396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16209396SMatthew.Ahrens@Sun.COM 				    ZFS_DELEG_PERM_USERPROP, CRED()))
16219396SMatthew.Ahrens@Sun.COM 					return (error);
16229396SMatthew.Ahrens@Sun.COM 				continue;
16239396SMatthew.Ahrens@Sun.COM 			}
16249396SMatthew.Ahrens@Sun.COM 
16259396SMatthew.Ahrens@Sun.COM 			if (!issnap && zfs_prop_userquota(propname) &&
16269396SMatthew.Ahrens@Sun.COM 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
16279396SMatthew.Ahrens@Sun.COM 				const char *perm;
16289396SMatthew.Ahrens@Sun.COM 				const char *up = zfs_userquota_prop_prefixes
16299396SMatthew.Ahrens@Sun.COM 				    [ZFS_PROP_USERQUOTA];
16309396SMatthew.Ahrens@Sun.COM 				if (strncmp(propname, up, strlen(up)) == 0)
16319396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_USERQUOTA;
16329396SMatthew.Ahrens@Sun.COM 				else
16339396SMatthew.Ahrens@Sun.COM 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
16349396SMatthew.Ahrens@Sun.COM 				if (error = zfs_secpolicy_write_perms(name,
16359396SMatthew.Ahrens@Sun.COM 				    perm, CRED()))
16369396SMatthew.Ahrens@Sun.COM 					return (error);
16379396SMatthew.Ahrens@Sun.COM 				continue;
16389396SMatthew.Ahrens@Sun.COM 			}
16399396SMatthew.Ahrens@Sun.COM 
16409396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16412676Seschrock 		}
16422676Seschrock 
16439396SMatthew.Ahrens@Sun.COM 		if (issnap)
16449396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
16459396SMatthew.Ahrens@Sun.COM 
16464787Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
16474670Sahrens 			return (error);
16482676Seschrock 
16494670Sahrens 		/*
16504670Sahrens 		 * Check that this value is valid for this pool version
16514670Sahrens 		 */
16524670Sahrens 		switch (prop) {
16533886Sahl 		case ZFS_PROP_COMPRESSION:
16543886Sahl 			/*
16553886Sahl 			 * If the user specified gzip compression, make sure
16563886Sahl 			 * the SPA supports it. We ignore any errors here since
16573886Sahl 			 * we'll catch them later.
16583886Sahl 			 */
16593886Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
16607042Sgw25295 			    nvpair_value_uint64(elem, &intval) == 0) {
16617042Sgw25295 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
16627042Sgw25295 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16637184Stimh 				    zfs_earlier_version(name,
16645331Samw 				    SPA_VERSION_GZIP_COMPRESSION))
16655331Samw 					return (ENOTSUP);
16667042Sgw25295 
166710922SJeff.Bonwick@Sun.COM 				if (intval == ZIO_COMPRESS_ZLE &&
166810922SJeff.Bonwick@Sun.COM 				    zfs_earlier_version(name,
166910922SJeff.Bonwick@Sun.COM 				    SPA_VERSION_ZLE_COMPRESSION))
167010922SJeff.Bonwick@Sun.COM 					return (ENOTSUP);
167110922SJeff.Bonwick@Sun.COM 
16727042Sgw25295 				/*
16737042Sgw25295 				 * If this is a bootable dataset then
16747042Sgw25295 				 * verify that the compression algorithm
16757042Sgw25295 				 * is supported for booting. We must return
16767042Sgw25295 				 * something other than ENOTSUP since it
16777042Sgw25295 				 * implies a downrev pool version.
16787042Sgw25295 				 */
16797042Sgw25295 				if (zfs_is_bootfs(name) &&
16807042Sgw25295 				    !BOOTFS_COMPRESS_VALID(intval))
16817042Sgw25295 					return (ERANGE);
16823886Sahl 			}
16833886Sahl 			break;
16844603Sahrens 
16854603Sahrens 		case ZFS_PROP_COPIES:
16869396SMatthew.Ahrens@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
16875331Samw 				return (ENOTSUP);
16884603Sahrens 			break;
16895977Smarks 
169010922SJeff.Bonwick@Sun.COM 		case ZFS_PROP_DEDUP:
169110922SJeff.Bonwick@Sun.COM 			if (zfs_earlier_version(name, SPA_VERSION_DEDUP))
169210922SJeff.Bonwick@Sun.COM 				return (ENOTSUP);
169310922SJeff.Bonwick@Sun.COM 			break;
169410922SJeff.Bonwick@Sun.COM 
16955977Smarks 		case ZFS_PROP_SHARESMB:
16966689Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16975977Smarks 				return (ENOTSUP);
16985977Smarks 			break;
16998053SMark.Shellenbaum@Sun.COM 
17008053SMark.Shellenbaum@Sun.COM 		case ZFS_PROP_ACLINHERIT:
17018053SMark.Shellenbaum@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
17028053SMark.Shellenbaum@Sun.COM 			    nvpair_value_uint64(elem, &intval) == 0)
17038053SMark.Shellenbaum@Sun.COM 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
17048053SMark.Shellenbaum@Sun.COM 				    zfs_earlier_version(name,
17058053SMark.Shellenbaum@Sun.COM 				    SPA_VERSION_PASSTHROUGH_X))
17068053SMark.Shellenbaum@Sun.COM 					return (ENOTSUP);
17074603Sahrens 		}
17084543Smarks 	}
17094543Smarks 
17108697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17114543Smarks 	elem = NULL;
17124543Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
17134670Sahrens 		const char *propname = nvpair_name(elem);
17144670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
17154543Smarks 
17165094Slling 		if (prop == ZPROP_INVAL) {
17179396SMatthew.Ahrens@Sun.COM 			if (zfs_prop_userquota(propname)) {
17189396SMatthew.Ahrens@Sun.COM 				uint64_t *valary;
17199396SMatthew.Ahrens@Sun.COM 				unsigned int vallen;
17209396SMatthew.Ahrens@Sun.COM 				const char *domain;
17219396SMatthew.Ahrens@Sun.COM 				zfs_userquota_prop_t type;
17229396SMatthew.Ahrens@Sun.COM 				uint64_t rid;
17239396SMatthew.Ahrens@Sun.COM 				uint64_t quota;
17249396SMatthew.Ahrens@Sun.COM 				zfsvfs_t *zfsvfs;
17259396SMatthew.Ahrens@Sun.COM 
17269396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_uint64_array(elem,
17279396SMatthew.Ahrens@Sun.COM 				    &valary, &vallen) == 0);
17289396SMatthew.Ahrens@Sun.COM 				VERIFY(vallen == 3);
17299396SMatthew.Ahrens@Sun.COM 				type = valary[0];
17309396SMatthew.Ahrens@Sun.COM 				rid = valary[1];
17319396SMatthew.Ahrens@Sun.COM 				quota = valary[2];
1732*10969SMatthew.Ahrens@Sun.COM 				/*
1733*10969SMatthew.Ahrens@Sun.COM 				 * The propname is encoded as
1734*10969SMatthew.Ahrens@Sun.COM 				 * userquota@<rid>-<domain>.
1735*10969SMatthew.Ahrens@Sun.COM 				 */
1736*10969SMatthew.Ahrens@Sun.COM 				domain = strchr(propname, '-') + 1;
17379396SMatthew.Ahrens@Sun.COM 
173810298SMatthew.Ahrens@Sun.COM 				error = zfsvfs_hold(name, FTAG, &zfsvfs);
17399396SMatthew.Ahrens@Sun.COM 				if (error == 0) {
17409396SMatthew.Ahrens@Sun.COM 					error = zfs_set_userquota(zfsvfs,
17419396SMatthew.Ahrens@Sun.COM 					    type, domain, rid, quota);
17429396SMatthew.Ahrens@Sun.COM 					zfsvfs_rele(zfsvfs, FTAG);
17439396SMatthew.Ahrens@Sun.COM 				}
17449396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17459396SMatthew.Ahrens@Sun.COM 					continue;
17469396SMatthew.Ahrens@Sun.COM 				else
17479396SMatthew.Ahrens@Sun.COM 					goto out;
17489396SMatthew.Ahrens@Sun.COM 			} else if (zfs_prop_user(propname)) {
17499396SMatthew.Ahrens@Sun.COM 				VERIFY(nvpair_value_string(elem, &strval) == 0);
17509396SMatthew.Ahrens@Sun.COM 				error = dsl_prop_set(name, propname, 1,
17519396SMatthew.Ahrens@Sun.COM 				    strlen(strval) + 1, strval);
17529396SMatthew.Ahrens@Sun.COM 				if (error == 0)
17539396SMatthew.Ahrens@Sun.COM 					continue;
17549396SMatthew.Ahrens@Sun.COM 				else
17559396SMatthew.Ahrens@Sun.COM 					goto out;
17569396SMatthew.Ahrens@Sun.COM 			}
17574543Smarks 		}
17582676Seschrock 
17592676Seschrock 		switch (prop) {
17602676Seschrock 		case ZFS_PROP_QUOTA:
17612676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17624577Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17638697SRichard.Morris@Sun.COM 				goto out;
17642676Seschrock 			break;
17652676Seschrock 
17665378Sck153898 		case ZFS_PROP_REFQUOTA:
17675378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17685378Sck153898 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17698697SRichard.Morris@Sun.COM 				goto out;
17705378Sck153898 			break;
17715378Sck153898 
17722676Seschrock 		case ZFS_PROP_RESERVATION:
17732676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17742676Seschrock 			    (error = dsl_dir_set_reservation(name,
17752676Seschrock 			    intval)) != 0)
17768697SRichard.Morris@Sun.COM 				goto out;
17772676Seschrock 			break;
1778789Sahrens 
17795378Sck153898 		case ZFS_PROP_REFRESERVATION:
17805378Sck153898 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17815378Sck153898 			    (error = dsl_dataset_set_reservation(name,
17825378Sck153898 			    intval)) != 0)
17838697SRichard.Morris@Sun.COM 				goto out;
17845378Sck153898 			break;
17855378Sck153898 
17862676Seschrock 		case ZFS_PROP_VOLSIZE:
17872676Seschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
17884787Sahrens 			    (error = zvol_set_volsize(name,
17894787Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17908697SRichard.Morris@Sun.COM 				goto out;
17912676Seschrock 			break;
17922676Seschrock 
17934577Sahrens 		case ZFS_PROP_VERSION:
17949396SMatthew.Ahrens@Sun.COM 		{
17959396SMatthew.Ahrens@Sun.COM 			zfsvfs_t *zfsvfs;
17969396SMatthew.Ahrens@Sun.COM 
17979396SMatthew.Ahrens@Sun.COM 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
17989396SMatthew.Ahrens@Sun.COM 				goto out;
179910298SMatthew.Ahrens@Sun.COM 			if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0)
18009396SMatthew.Ahrens@Sun.COM 				goto out;
18019396SMatthew.Ahrens@Sun.COM 			error = zfs_set_version(zfsvfs, intval);
18029396SMatthew.Ahrens@Sun.COM 			zfsvfs_rele(zfsvfs, FTAG);
18039396SMatthew.Ahrens@Sun.COM 
18049396SMatthew.Ahrens@Sun.COM 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
18059396SMatthew.Ahrens@Sun.COM 				zfs_cmd_t zc = { 0 };
18069396SMatthew.Ahrens@Sun.COM 				(void) strcpy(zc.zc_name, name);
18079396SMatthew.Ahrens@Sun.COM 				(void) zfs_ioc_userspace_upgrade(&zc);
18089396SMatthew.Ahrens@Sun.COM 			}
18099396SMatthew.Ahrens@Sun.COM 			if (error)
18108697SRichard.Morris@Sun.COM 				goto out;
18112676Seschrock 			break;
18129396SMatthew.Ahrens@Sun.COM 		}
18132676Seschrock 
18142676Seschrock 		default:
18152676Seschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
18162676Seschrock 				if (zfs_prop_get_type(prop) !=
18178697SRichard.Morris@Sun.COM 				    PROP_TYPE_STRING) {
18188697SRichard.Morris@Sun.COM 					error = EINVAL;
18198697SRichard.Morris@Sun.COM 					goto out;
18208697SRichard.Morris@Sun.COM 				}
18212676Seschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
18222885Sahrens 				const char *unused;
18232885Sahrens 
18242717Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
18252676Seschrock 
18262676Seschrock 				switch (zfs_prop_get_type(prop)) {
18274787Sahrens 				case PROP_TYPE_NUMBER:
18282676Seschrock 					break;
18294787Sahrens 				case PROP_TYPE_STRING:
18308697SRichard.Morris@Sun.COM 					error = EINVAL;
18318697SRichard.Morris@Sun.COM 					goto out;
18324787Sahrens 				case PROP_TYPE_INDEX:
18332717Seschrock 					if (zfs_prop_index_to_string(prop,
18348697SRichard.Morris@Sun.COM 					    intval, &unused) != 0) {
18358697SRichard.Morris@Sun.COM 						error = EINVAL;
18368697SRichard.Morris@Sun.COM 						goto out;
18378697SRichard.Morris@Sun.COM 					}
18382676Seschrock 					break;
18392676Seschrock 				default:
18404577Sahrens 					cmn_err(CE_PANIC,
18414577Sahrens 					    "unknown property type");
18422676Seschrock 					break;
18432676Seschrock 				}
18442676Seschrock 			} else {
18458697SRichard.Morris@Sun.COM 				error = EINVAL;
18468697SRichard.Morris@Sun.COM 				goto out;
18472676Seschrock 			}
18488697SRichard.Morris@Sun.COM 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18498697SRichard.Morris@Sun.COM 				goto out;
18502676Seschrock 		}
18512676Seschrock 	}
18522676Seschrock 
18538697SRichard.Morris@Sun.COM 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18548697SRichard.Morris@Sun.COM 		error = dsl_props_set(name, genericnvl);
18558697SRichard.Morris@Sun.COM 	}
18568697SRichard.Morris@Sun.COM out:
18578697SRichard.Morris@Sun.COM 	nvlist_free(genericnvl);
18588697SRichard.Morris@Sun.COM 	return (error);
1859789Sahrens }
1860789Sahrens 
18615367Sahrens /*
18629355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
18639355SMatthew.Ahrens@Sun.COM  */
18649355SMatthew.Ahrens@Sun.COM static int
18659355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
18669355SMatthew.Ahrens@Sun.COM {
18679355SMatthew.Ahrens@Sun.COM 	nvpair_t *elem = NULL;
18689355SMatthew.Ahrens@Sun.COM 	int error = 0;
18699355SMatthew.Ahrens@Sun.COM 
18709355SMatthew.Ahrens@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
18719355SMatthew.Ahrens@Sun.COM 		const char *propname = nvpair_name(elem);
18729355SMatthew.Ahrens@Sun.COM 		char *valstr;
18739355SMatthew.Ahrens@Sun.COM 
18749355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
18759355SMatthew.Ahrens@Sun.COM 		    nvpair_type(elem) != DATA_TYPE_STRING)
18769355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
18779355SMatthew.Ahrens@Sun.COM 
18789355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
18799355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
18809355SMatthew.Ahrens@Sun.COM 			return (error);
18819355SMatthew.Ahrens@Sun.COM 
18829355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
18839355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
18849355SMatthew.Ahrens@Sun.COM 
18859355SMatthew.Ahrens@Sun.COM 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
18869355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
18879355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
18889355SMatthew.Ahrens@Sun.COM 	}
18899355SMatthew.Ahrens@Sun.COM 	return (0);
18909355SMatthew.Ahrens@Sun.COM }
18919355SMatthew.Ahrens@Sun.COM 
18929355SMatthew.Ahrens@Sun.COM /*
18935367Sahrens  * inputs:
18945367Sahrens  * zc_name		name of filesystem
18958697SRichard.Morris@Sun.COM  * zc_value		name of property to set
18965367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
18977265Sahrens  * zc_cookie		clear existing local props?
18985367Sahrens  *
18995367Sahrens  * outputs:		none
19005367Sahrens  */
1901789Sahrens static int
19022676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1903789Sahrens {
19042676Seschrock 	nvlist_t *nvl;
19052676Seschrock 	int error;
1906789Sahrens 
19075094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19089643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
19092676Seschrock 		return (error);
19102676Seschrock 
19117265Sahrens 	if (zc->zc_cookie) {
19127265Sahrens 		nvlist_t *origprops;
19137265Sahrens 		objset_t *os;
19147265Sahrens 
191510298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
19167265Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19178536SDavid.Pacheco@Sun.COM 				clear_props(zc->zc_name, origprops, nvl);
19187265Sahrens 				nvlist_free(origprops);
19197265Sahrens 			}
192010298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
19217265Sahrens 		}
19227265Sahrens 
19237265Sahrens 	}
19247265Sahrens 
19254787Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
19264543Smarks 
19272676Seschrock 	nvlist_free(nvl);
19282676Seschrock 	return (error);
1929789Sahrens }
1930789Sahrens 
19315367Sahrens /*
19325367Sahrens  * inputs:
19335367Sahrens  * zc_name		name of filesystem
19345367Sahrens  * zc_value		name of property to inherit
19355367Sahrens  *
19365367Sahrens  * outputs:		none
19375367Sahrens  */
1938789Sahrens static int
19394849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
19404849Sahrens {
19414849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
19424849Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
19434849Sahrens }
19444849Sahrens 
19454849Sahrens static int
19464098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
19473912Slling {
19485094Slling 	nvlist_t *props;
19493912Slling 	spa_t *spa;
19505094Slling 	int error;
19518525SEric.Schrock@Sun.COM 	nvpair_t *elem;
19523912Slling 
19535094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19549643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)))
19553912Slling 		return (error);
19563912Slling 
19578525SEric.Schrock@Sun.COM 	/*
19588525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
19598525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
19608525SEric.Schrock@Sun.COM 	 */
19618525SEric.Schrock@Sun.COM 	elem = nvlist_next_nvpair(props, NULL);
19628525SEric.Schrock@Sun.COM 	if (elem != NULL && strcmp(nvpair_name(elem),
19638525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
19648525SEric.Schrock@Sun.COM 	    nvlist_next_nvpair(props, elem) == NULL) {
19658525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
19668525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
19678525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
19688525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
19698525SEric.Schrock@Sun.COM 		}
19708525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
197110672SEric.Schrock@Sun.COM 		if (spa != NULL) {
197210672SEric.Schrock@Sun.COM 			nvlist_free(props);
19738525SEric.Schrock@Sun.COM 			return (0);
197410672SEric.Schrock@Sun.COM 		}
19758525SEric.Schrock@Sun.COM 	}
19768525SEric.Schrock@Sun.COM 
19773912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19785094Slling 		nvlist_free(props);
19793912Slling 		return (error);
19803912Slling 	}
19813912Slling 
19825094Slling 	error = spa_prop_set(spa, props);
19833912Slling 
19845094Slling 	nvlist_free(props);
19853912Slling 	spa_close(spa, FTAG);
19863912Slling 
19873912Slling 	return (error);
19883912Slling }
19893912Slling 
19903912Slling static int
19914098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
19923912Slling {
19933912Slling 	spa_t *spa;
19943912Slling 	int error;
19953912Slling 	nvlist_t *nvp = NULL;
19963912Slling 
19978525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
19988525SEric.Schrock@Sun.COM 		/*
19998525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
20008525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
20018525SEric.Schrock@Sun.COM 		 * anyway.
20028525SEric.Schrock@Sun.COM 		 */
20038525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
20048525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
20058525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
20068525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
20078525SEric.Schrock@Sun.COM 	} else {
20088525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
20098525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
20108525SEric.Schrock@Sun.COM 	}
20113912Slling 
20123912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
20133912Slling 		error = put_nvlist(zc, nvp);
20143912Slling 	else
20153912Slling 		error = EFAULT;
20163912Slling 
20178525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
20183912Slling 	return (error);
20193912Slling }
20203912Slling 
20213912Slling static int
20224543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
20234543Smarks {
20244543Smarks 	nvlist_t *nvp;
20254543Smarks 	int error;
20264543Smarks 	uint32_t uid;
20274543Smarks 	uint32_t gid;
20284543Smarks 	uint32_t *groups;
20294543Smarks 	uint_t group_cnt;
20304543Smarks 	cred_t	*usercred;
20314543Smarks 
20325094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20339643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvp)) != 0) {
20344543Smarks 		return (error);
20354543Smarks 	}
20364543Smarks 
20374543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20384543Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
20394543Smarks 		nvlist_free(nvp);
20404543Smarks 		return (EPERM);
20414543Smarks 	}
20424543Smarks 
20434543Smarks 	if ((error = nvlist_lookup_uint32(nvp,
20444543Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
20454543Smarks 		nvlist_free(nvp);
20464543Smarks 		return (EPERM);
20474543Smarks 	}
20484543Smarks 
20494543Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
20504543Smarks 	    &groups, &group_cnt)) != 0) {
20514543Smarks 		nvlist_free(nvp);
20524543Smarks 		return (EPERM);
20534543Smarks 	}
20544543Smarks 	usercred = cralloc();
20554543Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
20564543Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
20574543Smarks 		nvlist_free(nvp);
20584543Smarks 		crfree(usercred);
20594543Smarks 		return (EPERM);
20604543Smarks 	}
20614543Smarks 	nvlist_free(nvp);
20624543Smarks 	error = dsl_deleg_access(zc->zc_name,
20634787Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
20644543Smarks 	crfree(usercred);
20654543Smarks 	return (error);
20664543Smarks }
20674543Smarks 
20685367Sahrens /*
20695367Sahrens  * inputs:
20705367Sahrens  * zc_name		name of filesystem
20715367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20725367Sahrens  * zc_perm_action	allow/unallow flag
20735367Sahrens  *
20745367Sahrens  * outputs:		none
20755367Sahrens  */
20764543Smarks static int
20774543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
20784543Smarks {
20794543Smarks 	int error;
20804543Smarks 	nvlist_t *fsaclnv = NULL;
20814543Smarks 
20825094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20839643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
20844543Smarks 		return (error);
20854543Smarks 
20864543Smarks 	/*
20874543Smarks 	 * Verify nvlist is constructed correctly
20884543Smarks 	 */
20894543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
20904543Smarks 		nvlist_free(fsaclnv);
20914543Smarks 		return (EINVAL);
20924543Smarks 	}
20934543Smarks 
20944543Smarks 	/*
20954543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
20964543Smarks 	 * that user is allowed to hand out each permission in
20974543Smarks 	 * the nvlist(s)
20984543Smarks 	 */
20994543Smarks 
21004787Sahrens 	error = secpolicy_zfs(CRED());
21014543Smarks 	if (error) {
21024787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
21034787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
21044787Sahrens 			    fsaclnv, CRED());
21054787Sahrens 		} else {
21064787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
21074787Sahrens 			    fsaclnv, CRED());
21084787Sahrens 		}
21094543Smarks 	}
21104543Smarks 
21114543Smarks 	if (error == 0)
21124543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
21134543Smarks 
21144543Smarks 	nvlist_free(fsaclnv);
21154543Smarks 	return (error);
21164543Smarks }
21174543Smarks 
21185367Sahrens /*
21195367Sahrens  * inputs:
21205367Sahrens  * zc_name		name of filesystem
21215367Sahrens  *
21225367Sahrens  * outputs:
21235367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21245367Sahrens  */
21254543Smarks static int
21264543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
21274543Smarks {
21284543Smarks 	nvlist_t *nvp;
21294543Smarks 	int error;
21304543Smarks 
21314543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
21324543Smarks 		error = put_nvlist(zc, nvp);
21334543Smarks 		nvlist_free(nvp);
21344543Smarks 	}
21354543Smarks 
21364543Smarks 	return (error);
21374543Smarks }
21384543Smarks 
21395367Sahrens /*
2140789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2141789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2142789Sahrens  * is responsible for releasing the returned vfs pointer.
2143789Sahrens  */
2144789Sahrens static vfs_t *
2145789Sahrens zfs_get_vfs(const char *resource)
2146789Sahrens {
2147789Sahrens 	struct vfs *vfsp;
2148789Sahrens 	struct vfs *vfs_found = NULL;
2149789Sahrens 
2150789Sahrens 	vfs_list_read_lock();
2151789Sahrens 	vfsp = rootvfs;
2152789Sahrens 	do {
2153789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2154789Sahrens 			VFS_HOLD(vfsp);
2155789Sahrens 			vfs_found = vfsp;
2156789Sahrens 			break;
2157789Sahrens 		}
2158789Sahrens 		vfsp = vfsp->vfs_next;
2159789Sahrens 	} while (vfsp != rootvfs);
2160789Sahrens 	vfs_list_unlock();
2161789Sahrens 	return (vfs_found);
2162789Sahrens }
2163789Sahrens 
21644543Smarks /* ARGSUSED */
2165789Sahrens static void
21664543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2167789Sahrens {
21685331Samw 	zfs_creat_t *zct = arg;
21695498Stimh 
21705498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
21715331Samw }
21725331Samw 
21735498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
21745498Stimh 
21755331Samw /*
21765498Stimh  * inputs:
21777184Stimh  * createprops		list of properties requested by creator
21787184Stimh  * default_zplver	zpl version to use if unspecified in createprops
21797184Stimh  * fuids_ok		fuids allowed in this version of the spa?
21807184Stimh  * os			parent objset pointer (NULL if root fs)
21815331Samw  *
21825498Stimh  * outputs:
21835498Stimh  * zplprops	values for the zplprops we attach to the master node object
21847184Stimh  * is_ci	true if requested file system will be purely case-insensitive
21855331Samw  *
21865498Stimh  * Determine the settings for utf8only, normalization and
21875498Stimh  * casesensitivity.  Specific values may have been requested by the
21885498Stimh  * creator and/or we can inherit values from the parent dataset.  If
21895498Stimh  * the file system is of too early a vintage, a creator can not
21905498Stimh  * request settings for these properties, even if the requested
21915498Stimh  * setting is the default value.  We don't actually want to create dsl
21925498Stimh  * properties for these, so remove them from the source nvlist after
21935498Stimh  * processing.
21945331Samw  */
21955331Samw static int
21969396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
21977184Stimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
21987184Stimh     boolean_t *is_ci)
21995331Samw {
22005498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
22015498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
22025498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
22035498Stimh 
22045498Stimh 	ASSERT(zplprops != NULL);
22055498Stimh 
22065375Stimh 	/*
22075498Stimh 	 * Pull out creator prop choices, if any.
22085375Stimh 	 */
22095498Stimh 	if (createprops) {
22105498Stimh 		(void) nvlist_lookup_uint64(createprops,
22117184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
22127184Stimh 		(void) nvlist_lookup_uint64(createprops,
22135498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
22145498Stimh 		(void) nvlist_remove_all(createprops,
22155498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
22165498Stimh 		(void) nvlist_lookup_uint64(createprops,
22175498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
22185498Stimh 		(void) nvlist_remove_all(createprops,
22195498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
22205498Stimh 		(void) nvlist_lookup_uint64(createprops,
22215498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
22225498Stimh 		(void) nvlist_remove_all(createprops,
22235498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
22245331Samw 	}
22255331Samw 
22265375Stimh 	/*
22277184Stimh 	 * If the zpl version requested is whacky or the file system
22287184Stimh 	 * or pool is version is too "young" to support normalization
22297184Stimh 	 * and the creator tried to set a value for one of the props,
22307184Stimh 	 * error out.
22315498Stimh 	 */
22327184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22337184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22347184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
22355498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22367184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
22375498Stimh 		return (ENOTSUP);
22385498Stimh 
22395498Stimh 	/*
22405498Stimh 	 * Put the version in the zplprops
22415498Stimh 	 */
22425498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22435498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
22445498Stimh 
22455498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
22465498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
22475498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22485498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
22495498Stimh 
22505498Stimh 	/*
22515498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
22525498Stimh 	 */
22535498Stimh 	if (norm)
22545498Stimh 		u8 = 1;
22555498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
22565498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
22575498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22585498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
22595498Stimh 
22605498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
22615498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
22625498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
22635498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
22645498Stimh 
22656492Stimh 	if (is_ci)
22666492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
22676492Stimh 
22687184Stimh 	return (0);
22697184Stimh }
22707184Stimh 
22717184Stimh static int
22727184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22737184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
22747184Stimh {
22757184Stimh 	boolean_t fuids_ok = B_TRUE;
22767184Stimh 	uint64_t zplver = ZPL_VERSION;
22777184Stimh 	objset_t *os = NULL;
22787184Stimh 	char parentname[MAXNAMELEN];
22797184Stimh 	char *cp;
22807184Stimh 	int error;
22817184Stimh 
22827184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22837184Stimh 	cp = strrchr(parentname, '/');
22847184Stimh 	ASSERT(cp != NULL);
22857184Stimh 	cp[0] = '\0';
22867184Stimh 
22879396SMatthew.Ahrens@Sun.COM 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
22889396SMatthew.Ahrens@Sun.COM 		zplver = ZPL_VERSION_USERSPACE - 1;
22897184Stimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22907184Stimh 		zplver = ZPL_VERSION_FUID - 1;
22917184Stimh 		fuids_ok = B_FALSE;
22927184Stimh 	}
22937184Stimh 
22947184Stimh 	/*
22957184Stimh 	 * Open parent object set so we can inherit zplprop values.
22967184Stimh 	 */
229710298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
22987184Stimh 		return (error);
22997184Stimh 
23007184Stimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
23017184Stimh 	    zplprops, is_ci);
230210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
23037184Stimh 	return (error);
23047184Stimh }
23057184Stimh 
23067184Stimh static int
23077184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
23087184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
23097184Stimh {
23107184Stimh 	boolean_t fuids_ok = B_TRUE;
23117184Stimh 	uint64_t zplver = ZPL_VERSION;
23127184Stimh 	int error;
23137184Stimh 
23147184Stimh 	if (spa_vers < SPA_VERSION_FUID) {
23157184Stimh 		zplver = ZPL_VERSION_FUID - 1;
23167184Stimh 		fuids_ok = B_FALSE;
23177184Stimh 	}
23187184Stimh 
23197184Stimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23207184Stimh 	    zplprops, is_ci);
23217184Stimh 	return (error);
2322789Sahrens }
2323789Sahrens 
23245367Sahrens /*
23255367Sahrens  * inputs:
23265367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23275367Sahrens  * zc_name		name of new objset
23285367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23295367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23305367Sahrens  *
23315498Stimh  * outputs: none
23325367Sahrens  */
2333789Sahrens static int
2334789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2335789Sahrens {
2336789Sahrens 	objset_t *clone;
2337789Sahrens 	int error = 0;
23385331Samw 	zfs_creat_t zct;
23394543Smarks 	nvlist_t *nvprops = NULL;
23404543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2341789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2342789Sahrens 
2343789Sahrens 	switch (type) {
2344789Sahrens 
2345789Sahrens 	case DMU_OST_ZFS:
2346789Sahrens 		cbfunc = zfs_create_cb;
2347789Sahrens 		break;
2348789Sahrens 
2349789Sahrens 	case DMU_OST_ZVOL:
2350789Sahrens 		cbfunc = zvol_create_cb;
2351789Sahrens 		break;
2352789Sahrens 
2353789Sahrens 	default:
23542199Sahrens 		cbfunc = NULL;
23556423Sgw25295 		break;
23562199Sahrens 	}
23575326Sek110237 	if (strchr(zc->zc_name, '@') ||
23585326Sek110237 	    strchr(zc->zc_name, '%'))
2359789Sahrens 		return (EINVAL);
2360789Sahrens 
23612676Seschrock 	if (zc->zc_nvlist_src != NULL &&
23625094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
23639643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
23642676Seschrock 		return (error);
23652676Seschrock 
23665498Stimh 	zct.zct_zplprops = NULL;
23675331Samw 	zct.zct_props = nvprops;
23685331Samw 
23692676Seschrock 	if (zc->zc_value[0] != '\0') {
2370789Sahrens 		/*
2371789Sahrens 		 * We're creating a clone of an existing snapshot.
2372789Sahrens 		 */
23732676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
23742676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
23754543Smarks 			nvlist_free(nvprops);
2376789Sahrens 			return (EINVAL);
23772676Seschrock 		}
2378789Sahrens 
237910298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
23802676Seschrock 		if (error) {
23814543Smarks 			nvlist_free(nvprops);
2382789Sahrens 			return (error);
23832676Seschrock 		}
23846492Stimh 
238510272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
238610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
23875331Samw 		if (error) {
23885331Samw 			nvlist_free(nvprops);
23895331Samw 			return (error);
23905331Samw 		}
2391789Sahrens 	} else {
23926492Stimh 		boolean_t is_insensitive = B_FALSE;
23936492Stimh 
23942676Seschrock 		if (cbfunc == NULL) {
23954543Smarks 			nvlist_free(nvprops);
23962199Sahrens 			return (EINVAL);
23972676Seschrock 		}
23982676Seschrock 
2399789Sahrens 		if (type == DMU_OST_ZVOL) {
24002676Seschrock 			uint64_t volsize, volblocksize;
24012676Seschrock 
24024543Smarks 			if (nvprops == NULL ||
24034543Smarks 			    nvlist_lookup_uint64(nvprops,
24042676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
24052676Seschrock 			    &volsize) != 0) {
24064543Smarks 				nvlist_free(nvprops);
24072676Seschrock 				return (EINVAL);
24082676Seschrock 			}
24092676Seschrock 
24104543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
24112676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
24122676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
24134543Smarks 				nvlist_free(nvprops);
24142676Seschrock 				return (EINVAL);
24152676Seschrock 			}
24161133Seschrock 
24172676Seschrock 			if (error != 0)
24182676Seschrock 				volblocksize = zfs_prop_default_numeric(
24192676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
24202676Seschrock 
24212676Seschrock 			if ((error = zvol_check_volblocksize(
24222676Seschrock 			    volblocksize)) != 0 ||
24232676Seschrock 			    (error = zvol_check_volsize(volsize,
24242676Seschrock 			    volblocksize)) != 0) {
24254543Smarks 				nvlist_free(nvprops);
2426789Sahrens 				return (error);
24272676Seschrock 			}
24284577Sahrens 		} else if (type == DMU_OST_ZFS) {
24295331Samw 			int error;
24305331Samw 
24315498Stimh 			/*
24325331Samw 			 * We have to have normalization and
24335331Samw 			 * case-folding flags correct when we do the
24345331Samw 			 * file system creation, so go figure them out
24355498Stimh 			 * now.
24365331Samw 			 */
24375498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
24385498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24395498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24407184Stimh 			    zct.zct_zplprops, &is_insensitive);
24415331Samw 			if (error != 0) {
24425331Samw 				nvlist_free(nvprops);
24435498Stimh 				nvlist_free(zct.zct_zplprops);
24445331Samw 				return (error);
24454577Sahrens 			}
24462676Seschrock 		}
244710272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
24486492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
24495498Stimh 		nvlist_free(zct.zct_zplprops);
2450789Sahrens 	}
24512676Seschrock 
24522676Seschrock 	/*
24532676Seschrock 	 * It would be nice to do this atomically.
24542676Seschrock 	 */
24552676Seschrock 	if (error == 0) {
24564787Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
245710242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
24582676Seschrock 	}
24594543Smarks 	nvlist_free(nvprops);
2460789Sahrens 	return (error);
2461789Sahrens }
2462789Sahrens 
24635367Sahrens /*
24645367Sahrens  * inputs:
24655367Sahrens  * zc_name	name of filesystem
24665367Sahrens  * zc_value	short name of snapshot
24675367Sahrens  * zc_cookie	recursive flag
24689396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
24695367Sahrens  *
247010588SEric.Taylor@Sun.COM  * outputs:
247110588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
24725367Sahrens  */
2473789Sahrens static int
24742199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
24752199Sahrens {
24767265Sahrens 	nvlist_t *nvprops = NULL;
24777265Sahrens 	int error;
24787265Sahrens 	boolean_t recursive = zc->zc_cookie;
24797265Sahrens 
24802676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24812199Sahrens 		return (EINVAL);
24827265Sahrens 
24837265Sahrens 	if (zc->zc_nvlist_src != NULL &&
24847265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24859643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
24867265Sahrens 		return (error);
24877265Sahrens 
24889355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
24899355SMatthew.Ahrens@Sun.COM 	if (error)
24909355SMatthew.Ahrens@Sun.COM 		goto out;
24919355SMatthew.Ahrens@Sun.COM 
24929355SMatthew.Ahrens@Sun.COM 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
24939355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
24949355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
24959355SMatthew.Ahrens@Sun.COM 		goto out;
24967265Sahrens 	}
24979355SMatthew.Ahrens@Sun.COM 
24989355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
24999355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
25009355SMatthew.Ahrens@Sun.COM 
25019355SMatthew.Ahrens@Sun.COM out:
25027265Sahrens 	nvlist_free(nvprops);
25037265Sahrens 	return (error);
25042199Sahrens }
25052199Sahrens 
25064007Smmusante int
25072199Sahrens zfs_unmount_snap(char *name, void *arg)
2508789Sahrens {
25092417Sahrens 	vfs_t *vfsp = NULL;
25102199Sahrens 
25116689Smaybee 	if (arg) {
25126689Smaybee 		char *snapname = arg;
25136689Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
25146689Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25156689Smaybee 
25166689Smaybee 		(void) strcpy(buf, name);
25176689Smaybee 		(void) strcat(buf, "@");
25186689Smaybee 		(void) strcat(buf, snapname);
25196689Smaybee 		vfsp = zfs_get_vfs(buf);
25206689Smaybee 		kmem_free(buf, len);
25212417Sahrens 	} else if (strchr(name, '@')) {
25222199Sahrens 		vfsp = zfs_get_vfs(name);
25232199Sahrens 	}
25242199Sahrens 
25252199Sahrens 	if (vfsp) {
25262199Sahrens 		/*
25272199Sahrens 		 * Always force the unmount for snapshots.
25282199Sahrens 		 */
25292199Sahrens 		int flag = MS_FORCE;
2530789Sahrens 		int err;
2531789Sahrens 
25322199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
25332199Sahrens 			VFS_RELE(vfsp);
25342199Sahrens 			return (err);
25352199Sahrens 		}
25362199Sahrens 		VFS_RELE(vfsp);
25372199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25382199Sahrens 			return (err);
25392199Sahrens 	}
25402199Sahrens 	return (0);
25412199Sahrens }
25422199Sahrens 
25435367Sahrens /*
25445367Sahrens  * inputs:
254510242Schris.kirby@sun.com  * zc_name		name of filesystem
254610242Schris.kirby@sun.com  * zc_value		short name of snapshot
254710242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25485367Sahrens  *
25495367Sahrens  * outputs:	none
25505367Sahrens  */
25512199Sahrens static int
25522199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25532199Sahrens {
25542199Sahrens 	int err;
2555789Sahrens 
25562676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25572199Sahrens 		return (EINVAL);
25582199Sahrens 	err = dmu_objset_find(zc->zc_name,
25592676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25602199Sahrens 	if (err)
25612199Sahrens 		return (err);
256210242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
256310242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
25642199Sahrens }
25652199Sahrens 
25665367Sahrens /*
25675367Sahrens  * inputs:
25685367Sahrens  * zc_name		name of dataset to destroy
25695367Sahrens  * zc_objset_type	type of objset
257010242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
25715367Sahrens  *
25725367Sahrens  * outputs:		none
25735367Sahrens  */
25742199Sahrens static int
25752199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25762199Sahrens {
257710588SEric.Taylor@Sun.COM 	int err;
25782199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
257910588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
25802199Sahrens 		if (err)
25812199Sahrens 			return (err);
2582789Sahrens 	}
2583789Sahrens 
258410588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
258510588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
258610693Schris.kirby@sun.com 		(void) zvol_remove_minor(zc->zc_name);
258710588SEric.Taylor@Sun.COM 	return (err);
2588789Sahrens }
2589789Sahrens 
25905367Sahrens /*
25915367Sahrens  * inputs:
25925446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25935367Sahrens  *
25945367Sahrens  * outputs:	none
25955367Sahrens  */
2596789Sahrens static int
2597789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2598789Sahrens {
259910272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
26005446Sahrens 	int error;
260110272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
260210272SMatthew.Ahrens@Sun.COM 	char *clone_name;
260310272SMatthew.Ahrens@Sun.COM 
260410272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
260510272SMatthew.Ahrens@Sun.COM 	if (error)
260610272SMatthew.Ahrens@Sun.COM 		return (error);
260710272SMatthew.Ahrens@Sun.COM 
260810272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
260910272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
261010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
261110272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
261210272SMatthew.Ahrens@Sun.COM 	}
261310272SMatthew.Ahrens@Sun.COM 
261410272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
261510272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
261610272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
261710272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
261810272SMatthew.Ahrens@Sun.COM 	}
26195446Sahrens 
26205446Sahrens 	/*
262110272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
26225446Sahrens 	 */
262310272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
262410272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26255446Sahrens 	if (error)
262610272SMatthew.Ahrens@Sun.COM 		goto out;
262710272SMatthew.Ahrens@Sun.COM 
262810298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
262910272SMatthew.Ahrens@Sun.COM 	if (error)
263010272SMatthew.Ahrens@Sun.COM 		goto out;
263110272SMatthew.Ahrens@Sun.COM 
263210272SMatthew.Ahrens@Sun.COM 	/*
263310272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
263410272SMatthew.Ahrens@Sun.COM 	 */
26359396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
263610298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
26376083Sek110237 		if (error == 0) {
26386083Sek110237 			int resume_err;
26396083Sek110237 
264010272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
264110272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
264210272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
264310272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
264410272SMatthew.Ahrens@Sun.COM 				ds = NULL;
264510272SMatthew.Ahrens@Sun.COM 			} else {
264610272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
264710272SMatthew.Ahrens@Sun.COM 			}
264810298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
26496083Sek110237 			error = error ? error : resume_err;
26506083Sek110237 		}
26515446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26525446Sahrens 	} else {
265310272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
265410272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
265510272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
265610272SMatthew.Ahrens@Sun.COM 			ds = NULL;
265710272SMatthew.Ahrens@Sun.COM 		} else {
265810272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
265910272SMatthew.Ahrens@Sun.COM 		}
26605446Sahrens 	}
266110272SMatthew.Ahrens@Sun.COM 
266210272SMatthew.Ahrens@Sun.COM 	/*
266310272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
266410272SMatthew.Ahrens@Sun.COM 	 */
266510272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
266610272SMatthew.Ahrens@Sun.COM 
266710272SMatthew.Ahrens@Sun.COM out:
266810272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
266910272SMatthew.Ahrens@Sun.COM 	if (ds)
267010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26715446Sahrens 	return (error);
2672789Sahrens }
2673789Sahrens 
26745367Sahrens /*
26755367Sahrens  * inputs:
26765367Sahrens  * zc_name	old name of dataset
26775367Sahrens  * zc_value	new name of dataset
26785367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26795367Sahrens  *
26805367Sahrens  * outputs:	none
26815367Sahrens  */
2682789Sahrens static int
2683789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2684789Sahrens {
26854490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
26864007Smmusante 
26872676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
26885326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
26895326Sek110237 	    strchr(zc->zc_value, '%'))
2690789Sahrens 		return (EINVAL);
2691789Sahrens 
26924007Smmusante 	/*
26934007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
26944007Smmusante 	 * in which case the dataset code figures out which snapshots
26954007Smmusante 	 * to unmount.
26964007Smmusante 	 */
26974007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2698789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26992199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
27002199Sahrens 		if (err)
27012199Sahrens 			return (err);
2702789Sahrens 	}
270310588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
270410588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
27054007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2706789Sahrens }
2707789Sahrens 
27086689Smaybee static void
27098536SDavid.Pacheco@Sun.COM clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
27106689Smaybee {
27116689Smaybee 	zfs_cmd_t *zc;
27126689Smaybee 	nvpair_t *prop;
27136689Smaybee 
27146689Smaybee 	if (props == NULL)
27156689Smaybee 		return;
27166689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
27176689Smaybee 	(void) strcpy(zc->zc_name, dataset);
27186689Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
27196689Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27208536SDavid.Pacheco@Sun.COM 		if (newprops != NULL &&
27218536SDavid.Pacheco@Sun.COM 		    nvlist_exists(newprops, nvpair_name(prop)))
27228536SDavid.Pacheco@Sun.COM 			continue;
27236689Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
27246689Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
27256689Smaybee 			(void) zfs_ioc_inherit_prop(zc);
27266689Smaybee 	}
27276689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
27286689Smaybee }
27296689Smaybee 
27305367Sahrens /*
27315367Sahrens  * inputs:
27325367Sahrens  * zc_name		name of containing filesystem
27335367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27345367Sahrens  * zc_value		name of snapshot to create
27355367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27365367Sahrens  * zc_cookie		file descriptor to recv from
27375367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27385367Sahrens  * zc_guid		force flag
27395367Sahrens  *
27405367Sahrens  * outputs:
27415367Sahrens  * zc_cookie		number of bytes read
27425367Sahrens  */
2743789Sahrens static int
27445367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2745789Sahrens {
2746789Sahrens 	file_t *fp;
27475326Sek110237 	objset_t *os;
27485367Sahrens 	dmu_recv_cookie_t drc;
27495326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
2750789Sahrens 	int error, fd;
27515367Sahrens 	offset_t off;
27525367Sahrens 	nvlist_t *props = NULL;
27536689Smaybee 	nvlist_t *origprops = NULL;
27545367Sahrens 	objset_t *origin = NULL;
27555367Sahrens 	char *tosnap;
27565367Sahrens 	char tofs[ZFS_MAXNAMELEN];
2757789Sahrens 
27583265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
27595326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
27605326Sek110237 	    strchr(zc->zc_value, '%'))
27613265Sahrens 		return (EINVAL);
27623265Sahrens 
27635367Sahrens 	(void) strcpy(tofs, zc->zc_value);
27645367Sahrens 	tosnap = strchr(tofs, '@');
27655367Sahrens 	*tosnap = '\0';
27665367Sahrens 	tosnap++;
27675367Sahrens 
27685367Sahrens 	if (zc->zc_nvlist_src != NULL &&
27695367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27709643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
27715367Sahrens 		return (error);
27725367Sahrens 
2773789Sahrens 	fd = zc->zc_cookie;
2774789Sahrens 	fp = getf(fd);
27755367Sahrens 	if (fp == NULL) {
27765367Sahrens 		nvlist_free(props);
2777789Sahrens 		return (EBADF);
27785367Sahrens 	}
27795326Sek110237 
278010298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
27816689Smaybee 		/*
27826689Smaybee 		 * If new properties are supplied, they are to completely
27836689Smaybee 		 * replace the existing ones, so stash away the existing ones.
27846689Smaybee 		 */
278510298SMatthew.Ahrens@Sun.COM 		(void) dsl_prop_get_all(os, &origprops, B_TRUE);
278610298SMatthew.Ahrens@Sun.COM 
278710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
27885326Sek110237 	}
27895326Sek110237 
27905367Sahrens 	if (zc->zc_string[0]) {
279110298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
27926689Smaybee 		if (error)
27936689Smaybee 			goto out;
27945367Sahrens 	}
27955367Sahrens 
27965367Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
279710204SMatthew.Ahrens@Sun.COM 	    force, origin, &drc);
27985367Sahrens 	if (origin)
279910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
28006689Smaybee 	if (error)
28016689Smaybee 		goto out;
28025326Sek110237 
28035326Sek110237 	/*
28046689Smaybee 	 * Reset properties.  We do this before we receive the stream
28056689Smaybee 	 * so that the properties are applied to the new data.
28065326Sek110237 	 */
28075367Sahrens 	if (props) {
28088536SDavid.Pacheco@Sun.COM 		clear_props(tofs, origprops, props);
28096689Smaybee 		/*
28106689Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
28116689Smaybee 		 */
28126689Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
28135367Sahrens 	}
28145367Sahrens 
28155367Sahrens 	off = fp->f_offset;
28165367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
28175367Sahrens 
281810204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
281910204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
282010204SMatthew.Ahrens@Sun.COM 
282110204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
282210204SMatthew.Ahrens@Sun.COM 			/* online recv */
282310204SMatthew.Ahrens@Sun.COM 			int end_err;
282410298SMatthew.Ahrens@Sun.COM 
282510298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
282610204SMatthew.Ahrens@Sun.COM 			/*
282710204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
282810204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
282910204SMatthew.Ahrens@Sun.COM 			 */
283010204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
283110204SMatthew.Ahrens@Sun.COM 			if (error == 0) {
283210204SMatthew.Ahrens@Sun.COM 				int resume_err =
283310298SMatthew.Ahrens@Sun.COM 				    zfs_resume_fs(zfsvfs, tofs);
283410204SMatthew.Ahrens@Sun.COM 				error = error ? error : resume_err;
283510204SMatthew.Ahrens@Sun.COM 			}
283610204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
283710204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
283810204SMatthew.Ahrens@Sun.COM 		} else {
28396689Smaybee 			error = dmu_recv_end(&drc);
28405367Sahrens 		}
28416083Sek110237 	}
28425367Sahrens 
28435367Sahrens 	zc->zc_cookie = off - fp->f_offset;
28445367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28455367Sahrens 		fp->f_offset = off;
28462885Sahrens 
28476689Smaybee 	/*
28486689Smaybee 	 * On error, restore the original props.
28496689Smaybee 	 */
28506689Smaybee 	if (error && props) {
28518536SDavid.Pacheco@Sun.COM 		clear_props(tofs, props, NULL);
28526689Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
28536689Smaybee 	}
28546689Smaybee out:
28556689Smaybee 	nvlist_free(props);
28566689Smaybee 	nvlist_free(origprops);
2857789Sahrens 	releasef(fd);
2858789Sahrens 	return (error);
2859789Sahrens }
2860789Sahrens 
28615367Sahrens /*
28625367Sahrens  * inputs:
28635367Sahrens  * zc_name	name of snapshot to send
28645367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28655367Sahrens  * zc_cookie	file descriptor to send stream to
28665367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28675367Sahrens  *
28685367Sahrens  * outputs: none
28695367Sahrens  */
2870789Sahrens static int
28715367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2872789Sahrens {
2873789Sahrens 	objset_t *fromsnap = NULL;
2874789Sahrens 	objset_t *tosnap;
2875789Sahrens 	file_t *fp;
2876789Sahrens 	int error;
28775367Sahrens 	offset_t off;
2878789Sahrens 
287910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
2880789Sahrens 	if (error)
2881789Sahrens 		return (error);
2882789Sahrens 
28832676Seschrock 	if (zc->zc_value[0] != '\0') {
28848012SEric.Taylor@Sun.COM 		char *buf;
28852885Sahrens 		char *cp;
28862885Sahrens 
28878012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28888012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
28892885Sahrens 		cp = strchr(buf, '@');
28902885Sahrens 		if (cp)
28912885Sahrens 			*(cp+1) = 0;
28928012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
289310298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
28948012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
2895789Sahrens 		if (error) {
289610298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(tosnap, FTAG);
2897789Sahrens 			return (error);
2898789Sahrens 		}
2899789Sahrens 	}
2900789Sahrens 
2901789Sahrens 	fp = getf(zc->zc_cookie);
2902789Sahrens 	if (fp == NULL) {
290310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
2904789Sahrens 		if (fromsnap)
290510298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(fromsnap, FTAG);
2906789Sahrens 		return (EBADF);
2907789Sahrens 	}
2908789Sahrens 
29095367Sahrens 	off = fp->f_offset;
29105367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
29115367Sahrens 
29125367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
29135367Sahrens 		fp->f_offset = off;
2914789Sahrens 	releasef(zc->zc_cookie);
2915789Sahrens 	if (fromsnap)
291610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
291710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
2918789Sahrens 	return (error);
2919789Sahrens }
2920789Sahrens 
29211544Seschrock static int
29221544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
29231544Seschrock {
29241544Seschrock 	int id, error;
29251544Seschrock 
29261544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
29271544Seschrock 	    &zc->zc_inject_record);
29281544Seschrock 
29291544Seschrock 	if (error == 0)
29301544Seschrock 		zc->zc_guid = (uint64_t)id;
29311544Seschrock 
29321544Seschrock 	return (error);
29331544Seschrock }
29341544Seschrock 
29351544Seschrock static int
29361544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
29371544Seschrock {
29381544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
29391544Seschrock }
29401544Seschrock 
29411544Seschrock static int
29421544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
29431544Seschrock {
29441544Seschrock 	int id = (int)zc->zc_guid;
29451544Seschrock 	int error;
29461544Seschrock 
29471544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
29481544Seschrock 	    &zc->zc_inject_record);
29491544Seschrock 
29501544Seschrock 	zc->zc_guid = id;
29511544Seschrock 
29521544Seschrock 	return (error);
29531544Seschrock }
29541544Seschrock 
29551544Seschrock static int
29561544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
29571544Seschrock {
29581544Seschrock 	spa_t *spa;
29591544Seschrock 	int error;
29602676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
29611544Seschrock 
29621544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
29631544Seschrock 		return (error);
29641544Seschrock 
29652676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
29661544Seschrock 	    &count);
29671544Seschrock 	if (error == 0)
29682676Seschrock 		zc->zc_nvlist_dst_size = count;
29691544Seschrock 	else
29702676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
29711544Seschrock 
29721544Seschrock 	spa_close(spa, FTAG);
29731544Seschrock 
29741544Seschrock 	return (error);
29751544Seschrock }
29761544Seschrock 
29771544Seschrock static int
29781544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
29791544Seschrock {
29801544Seschrock 	spa_t *spa;
29811544Seschrock 	vdev_t *vd;
29821544Seschrock 	int error;
29831544Seschrock 
29847294Sperrin 	/*
29857294Sperrin 	 * On zpool clear we also fix up missing slogs
29867294Sperrin 	 */
29877294Sperrin 	mutex_enter(&spa_namespace_lock);
29887294Sperrin 	spa = spa_lookup(zc->zc_name);
29897294Sperrin 	if (spa == NULL) {
29907294Sperrin 		mutex_exit(&spa_namespace_lock);
29917294Sperrin 		return (EIO);
29927294Sperrin 	}
299310922SJeff.Bonwick@Sun.COM 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
29947294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
299510922SJeff.Bonwick@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
29967294Sperrin 	}
299710921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
29987294Sperrin 	mutex_exit(&spa_namespace_lock);
29997294Sperrin 
300010921STim.Haley@Sun.COM 	if (zc->zc_cookie == ZPOOL_NO_REWIND) {
300110921STim.Haley@Sun.COM 		error = spa_open(zc->zc_name, &spa, FTAG);
300210921STim.Haley@Sun.COM 	} else {
300310921STim.Haley@Sun.COM 		nvlist_t *policy;
300410921STim.Haley@Sun.COM 		nvlist_t *config = NULL;
300510921STim.Haley@Sun.COM 
300610921STim.Haley@Sun.COM 		if (zc->zc_nvlist_src == NULL)
300710921STim.Haley@Sun.COM 			return (EINVAL);
300810921STim.Haley@Sun.COM 
300910921STim.Haley@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
301010921STim.Haley@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
301110921STim.Haley@Sun.COM 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
301210921STim.Haley@Sun.COM 			    policy, &config);
301310921STim.Haley@Sun.COM 			if (config != NULL) {
301410921STim.Haley@Sun.COM 				(void) put_nvlist(zc, config);
301510921STim.Haley@Sun.COM 				nvlist_free(config);
301610921STim.Haley@Sun.COM 			}
301710921STim.Haley@Sun.COM 			nvlist_free(policy);
301810921STim.Haley@Sun.COM 		}
301910921STim.Haley@Sun.COM 	}
302010921STim.Haley@Sun.COM 
302110921STim.Haley@Sun.COM 	if (error)
30221544Seschrock 		return (error);
30231544Seschrock 
302410685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
30251544Seschrock 
30262676Seschrock 	if (zc->zc_guid == 0) {
30271544Seschrock 		vd = NULL;
30286643Seschrock 	} else {
30296643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
30305450Sbrendan 		if (vd == NULL) {
30317754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
30325450Sbrendan 			spa_close(spa, FTAG);
30335450Sbrendan 			return (ENODEV);
30345450Sbrendan 		}
30351544Seschrock 	}
30361544Seschrock 
30377754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
30387754SJeff.Bonwick@Sun.COM 
30397754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
30407754SJeff.Bonwick@Sun.COM 
30417754SJeff.Bonwick@Sun.COM 	/*
30427754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
30437754SJeff.Bonwick@Sun.COM 	 */
30449234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
30459234SGeorge.Wilson@Sun.COM 		error = EIO;
30461544Seschrock 
30471544Seschrock 	spa_close(spa, FTAG);
30481544Seschrock 
30499234SGeorge.Wilson@Sun.COM 	return (error);
30501544Seschrock }
30511544Seschrock 
30525367Sahrens /*
30535367Sahrens  * inputs:
30545367Sahrens  * zc_name	name of filesystem
30555367Sahrens  * zc_value	name of origin snapshot
30565367Sahrens  *
305710588SEric.Taylor@Sun.COM  * outputs:
305810588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
30595367Sahrens  */
30601544Seschrock static int
30612082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
30622082Seschrock {
30632417Sahrens 	char *cp;
30642417Sahrens 
30652417Sahrens 	/*
30662417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30672417Sahrens 	 * it's easier.
30682417Sahrens 	 */
30692676Seschrock 	cp = strchr(zc->zc_value, '@');
30702417Sahrens 	if (cp)
30712417Sahrens 		*cp = '\0';
30722676Seschrock 	(void) dmu_objset_find(zc->zc_value,
30732417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
307410588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
30752082Seschrock }
30762082Seschrock 
30774543Smarks /*
30789396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
30799396SMatthew.Ahrens@Sun.COM  *
30809396SMatthew.Ahrens@Sun.COM  * inputs:
30819396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
30829396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
30839396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
30849396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
30859396SMatthew.Ahrens@Sun.COM  *
30869396SMatthew.Ahrens@Sun.COM  * outputs:
30879396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
30889396SMatthew.Ahrens@Sun.COM  */
30899396SMatthew.Ahrens@Sun.COM static int
30909396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
30919396SMatthew.Ahrens@Sun.COM {
30929396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
30939396SMatthew.Ahrens@Sun.COM 	int error;
30949396SMatthew.Ahrens@Sun.COM 
30959396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
30969396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
30979396SMatthew.Ahrens@Sun.COM 
309810298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
30999396SMatthew.Ahrens@Sun.COM 	if (error)
31009396SMatthew.Ahrens@Sun.COM 		return (error);
31019396SMatthew.Ahrens@Sun.COM 
31029396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
31039396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
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  * zc_cookie		zap cursor
31139396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
31149396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
31159396SMatthew.Ahrens@Sun.COM  *
31169396SMatthew.Ahrens@Sun.COM  * outputs:
31179396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
31189396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
31199396SMatthew.Ahrens@Sun.COM  */
31209396SMatthew.Ahrens@Sun.COM static int
31219396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
31229396SMatthew.Ahrens@Sun.COM {
31239396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31249396SMatthew.Ahrens@Sun.COM 	int error;
31259396SMatthew.Ahrens@Sun.COM 
312610298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
31279396SMatthew.Ahrens@Sun.COM 	if (error)
31289396SMatthew.Ahrens@Sun.COM 		return (error);
31299396SMatthew.Ahrens@Sun.COM 
31309396SMatthew.Ahrens@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
31319396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
31329396SMatthew.Ahrens@Sun.COM 
31339396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
31349396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
31359396SMatthew.Ahrens@Sun.COM 
31369396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
31379396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
31389396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
31399396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
31409396SMatthew.Ahrens@Sun.COM 	}
31419396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
31429396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
31439396SMatthew.Ahrens@Sun.COM 
31449396SMatthew.Ahrens@Sun.COM 	return (error);
31459396SMatthew.Ahrens@Sun.COM }
31469396SMatthew.Ahrens@Sun.COM 
31479396SMatthew.Ahrens@Sun.COM /*
31489396SMatthew.Ahrens@Sun.COM  * inputs:
31499396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
31509396SMatthew.Ahrens@Sun.COM  *
31519396SMatthew.Ahrens@Sun.COM  * outputs:
31529396SMatthew.Ahrens@Sun.COM  * none
31539396SMatthew.Ahrens@Sun.COM  */
31549396SMatthew.Ahrens@Sun.COM static int
31559396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
31569396SMatthew.Ahrens@Sun.COM {
31579396SMatthew.Ahrens@Sun.COM 	objset_t *os;
31589396SMatthew.Ahrens@Sun.COM 	int error;
31599396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
31609396SMatthew.Ahrens@Sun.COM 
31619396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
316210298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
31639396SMatthew.Ahrens@Sun.COM 			/*
31649396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
31659396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
31669396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
31679396SMatthew.Ahrens@Sun.COM 			 */
316810298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
316910298SMatthew.Ahrens@Sun.COM 			if (error == 0)
317010298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
31719396SMatthew.Ahrens@Sun.COM 		}
31729396SMatthew.Ahrens@Sun.COM 		if (error == 0)
31739396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
31749396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
31759396SMatthew.Ahrens@Sun.COM 	} else {
317610298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
317710298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
31789396SMatthew.Ahrens@Sun.COM 		if (error)
31799396SMatthew.Ahrens@Sun.COM 			return (error);
31809396SMatthew.Ahrens@Sun.COM 
31819396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
318210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
31839396SMatthew.Ahrens@Sun.COM 	}
31849396SMatthew.Ahrens@Sun.COM 
31859396SMatthew.Ahrens@Sun.COM 	return (error);
31869396SMatthew.Ahrens@Sun.COM }
31879396SMatthew.Ahrens@Sun.COM 
31889396SMatthew.Ahrens@Sun.COM /*
31894543Smarks  * We don't want to have a hard dependency
31904543Smarks  * against some special symbols in sharefs
31915331Samw  * nfs, and smbsrv.  Determine them if needed when
31924543Smarks  * the first file system is shared.
31935331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
31944543Smarks  */
31955331Samw int (*znfsexport_fs)(void *arg);
31964543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
31975331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
31985331Samw 
31995331Samw int zfs_nfsshare_inited;
32005331Samw int zfs_smbshare_inited;
32015331Samw 
32024543Smarks ddi_modhandle_t nfs_mod;
32034543Smarks ddi_modhandle_t sharefs_mod;
32045331Samw ddi_modhandle_t smbsrv_mod;
32054543Smarks kmutex_t zfs_share_lock;
32064543Smarks 
32074543Smarks static int
32085331Samw zfs_init_sharefs()
32095331Samw {
32105331Samw 	int error;
32115331Samw 
32125331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
32135331Samw 	/* Both NFS and SMB shares also require sharetab support. */
32145331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
32155331Samw 	    ddi_modopen("fs/sharefs",
32165331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
32175331Samw 		return (ENOSYS);
32185331Samw 	}
32195331Samw 	if (zshare_fs == NULL && ((zshare_fs =
32205331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
32215331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
32225331Samw 		return (ENOSYS);
32235331Samw 	}
32245331Samw 	return (0);
32255331Samw }
32265331Samw 
32275331Samw static int
32284543Smarks zfs_ioc_share(zfs_cmd_t *zc)
32294543Smarks {
32304543Smarks 	int error;
32314543Smarks 	int opcode;
32324543Smarks 
32335331Samw 	switch (zc->zc_share.z_sharetype) {
32345331Samw 	case ZFS_SHARE_NFS:
32355331Samw 	case ZFS_UNSHARE_NFS:
32365331Samw 		if (zfs_nfsshare_inited == 0) {
32375331Samw 			mutex_enter(&zfs_share_lock);
32385331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
32395331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32405331Samw 				mutex_exit(&zfs_share_lock);
32415331Samw 				return (ENOSYS);
32425331Samw 			}
32435331Samw 			if (znfsexport_fs == NULL &&
32445331Samw 			    ((znfsexport_fs = (int (*)(void *))
32455331Samw 			    ddi_modsym(nfs_mod,
32465331Samw 			    "nfs_export", &error)) == NULL)) {
32475331Samw 				mutex_exit(&zfs_share_lock);
32485331Samw 				return (ENOSYS);
32495331Samw 			}
32505331Samw 			error = zfs_init_sharefs();
32515331Samw 			if (error) {
32525331Samw 				mutex_exit(&zfs_share_lock);
32535331Samw 				return (ENOSYS);
32545331Samw 			}
32555331Samw 			zfs_nfsshare_inited = 1;
32564543Smarks 			mutex_exit(&zfs_share_lock);
32574543Smarks 		}
32585331Samw 		break;
32595331Samw 	case ZFS_SHARE_SMB:
32605331Samw 	case ZFS_UNSHARE_SMB:
32615331Samw 		if (zfs_smbshare_inited == 0) {
32625331Samw 			mutex_enter(&zfs_share_lock);
32635331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
32645331Samw 			    ddi_modopen("drv/smbsrv",
32655331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
32665331Samw 				mutex_exit(&zfs_share_lock);
32675331Samw 				return (ENOSYS);
32685331Samw 			}
32695331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
32705331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
32716139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
32725331Samw 				mutex_exit(&zfs_share_lock);
32735331Samw 				return (ENOSYS);
32745331Samw 			}
32755331Samw 			error = zfs_init_sharefs();
32765331Samw 			if (error) {
32775331Samw 				mutex_exit(&zfs_share_lock);
32785331Samw 				return (ENOSYS);
32795331Samw 			}
32805331Samw 			zfs_smbshare_inited = 1;
32814543Smarks 			mutex_exit(&zfs_share_lock);
32824543Smarks 		}
32835331Samw 		break;
32845331Samw 	default:
32855331Samw 		return (EINVAL);
32864543Smarks 	}
32874543Smarks 
32885331Samw 	switch (zc->zc_share.z_sharetype) {
32895331Samw 	case ZFS_SHARE_NFS:
32905331Samw 	case ZFS_UNSHARE_NFS:
32915331Samw 		if (error =
32925331Samw 		    znfsexport_fs((void *)
32935331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
32945331Samw 			return (error);
32955331Samw 		break;
32965331Samw 	case ZFS_SHARE_SMB:
32975331Samw 	case ZFS_UNSHARE_SMB:
32985331Samw 		if (error = zsmbexport_fs((void *)
32995331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
33005331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
33018845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
33025331Samw 			return (error);
33035331Samw 		}
33045331Samw 		break;
33055331Samw 	}
33065331Samw 
33075331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
33085331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
33094543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
33104543Smarks 
33115331Samw 	/*
33125331Samw 	 * Add or remove share from sharetab
33135331Samw 	 */
33144543Smarks 	error = zshare_fs(opcode,
33154543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
33164543Smarks 	    zc->zc_share.z_sharemax);
33174543Smarks 
33184543Smarks 	return (error);
33194543Smarks 
33204543Smarks }
33214543Smarks 
33228845Samw@Sun.COM ace_t full_access[] = {
33238845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
33248845Samw@Sun.COM };
33258845Samw@Sun.COM 
33268845Samw@Sun.COM /*
33278845Samw@Sun.COM  * Remove all ACL files in shares dir
33288845Samw@Sun.COM  */
33298845Samw@Sun.COM static int
33308845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
33318845Samw@Sun.COM {
33328845Samw@Sun.COM 	zap_cursor_t	zc;
33338845Samw@Sun.COM 	zap_attribute_t	zap;
33348845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
33358845Samw@Sun.COM 	int error;
33368845Samw@Sun.COM 
33378845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
33388845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
33398845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
33408845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
33418845Samw@Sun.COM 		    NULL, 0)) != 0)
33428845Samw@Sun.COM 			break;
33438845Samw@Sun.COM 	}
33448845Samw@Sun.COM 	zap_cursor_fini(&zc);
33458845Samw@Sun.COM 	return (error);
33468845Samw@Sun.COM }
33478845Samw@Sun.COM 
33488845Samw@Sun.COM static int
33498845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
33508845Samw@Sun.COM {
33518845Samw@Sun.COM 	vnode_t *vp;
33528845Samw@Sun.COM 	znode_t *dzp;
33538845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
33548845Samw@Sun.COM 	znode_t *sharedir;
33558845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
33568845Samw@Sun.COM 	nvlist_t *nvlist;
33578845Samw@Sun.COM 	char *src, *target;
33588845Samw@Sun.COM 	vattr_t vattr;
33598845Samw@Sun.COM 	vsecattr_t vsec;
33608845Samw@Sun.COM 	int error = 0;
33618845Samw@Sun.COM 
33628845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
33638845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
33648845Samw@Sun.COM 		return (error);
33658845Samw@Sun.COM 
33668845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
33678845Samw@Sun.COM 
33688845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
33698845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
33708845Samw@Sun.COM 	    zc->zc_name) != 0)) {
33718845Samw@Sun.COM 		VN_RELE(vp);
33728845Samw@Sun.COM 		return (EINVAL);
33738845Samw@Sun.COM 	}
33748845Samw@Sun.COM 
33758845Samw@Sun.COM 	dzp = VTOZ(vp);
33768845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
33778845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
33788845Samw@Sun.COM 
33799030SMark.Shellenbaum@Sun.COM 	/*
33809030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
33819030SMark.Shellenbaum@Sun.COM 	 */
33829030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
33839030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
33849030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
33859030SMark.Shellenbaum@Sun.COM 
33869030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
33879030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33889030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
33899030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33909030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
33919030SMark.Shellenbaum@Sun.COM 		if (error) {
33929030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
33939030SMark.Shellenbaum@Sun.COM 		} else {
33949030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
33959030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
33969030SMark.Shellenbaum@Sun.COM 		}
33979030SMark.Shellenbaum@Sun.COM 		if (error) {
33989030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
33999030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
34009030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
34019030SMark.Shellenbaum@Sun.COM 			return (error);
34029030SMark.Shellenbaum@Sun.COM 		}
34039030SMark.Shellenbaum@Sun.COM 	}
34049030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
34059030SMark.Shellenbaum@Sun.COM 
34069030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
34078845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
34089030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
34098845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
34108845Samw@Sun.COM 		return (error);
34118845Samw@Sun.COM 	}
34128845Samw@Sun.COM 
34138845Samw@Sun.COM 	switch (zc->zc_cookie) {
34148845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
34158845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
34168845Samw@Sun.COM 		vattr.va_type = VREG;
34178845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
34188845Samw@Sun.COM 		vattr.va_uid = 0;
34198845Samw@Sun.COM 		vattr.va_gid = 0;
34208845Samw@Sun.COM 
34218845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
34228845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
34238845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
34248845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
34258845Samw@Sun.COM 
34268845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
34278845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
34288845Samw@Sun.COM 		if (resourcevp)
34298845Samw@Sun.COM 			VN_RELE(resourcevp);
34308845Samw@Sun.COM 		break;
34318845Samw@Sun.COM 
34328845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
34338845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
34348845Samw@Sun.COM 		    NULL, 0);
34358845Samw@Sun.COM 		break;
34368845Samw@Sun.COM 
34378845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
34388845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
34399643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
34408845Samw@Sun.COM 			VN_RELE(vp);
34418845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34428845Samw@Sun.COM 			return (error);
34438845Samw@Sun.COM 		}
34448845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
34458845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
34468845Samw@Sun.COM 		    &target)) {
34478845Samw@Sun.COM 			VN_RELE(vp);
34489179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
34498845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
34508845Samw@Sun.COM 			return (error);
34518845Samw@Sun.COM 		}
34528845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
34538845Samw@Sun.COM 		    kcred, NULL, 0);
34548845Samw@Sun.COM 		nvlist_free(nvlist);
34558845Samw@Sun.COM 		break;
34568845Samw@Sun.COM 
34578845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
34588845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
34598845Samw@Sun.COM 		break;
34608845Samw@Sun.COM 
34618845Samw@Sun.COM 	default:
34628845Samw@Sun.COM 		error = EINVAL;
34638845Samw@Sun.COM 		break;
34648845Samw@Sun.COM 	}
34658845Samw@Sun.COM 
34668845Samw@Sun.COM 	VN_RELE(vp);
34678845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
34688845Samw@Sun.COM 
34698845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
34708845Samw@Sun.COM 
34718845Samw@Sun.COM 	return (error);
34728845Samw@Sun.COM }
34738845Samw@Sun.COM 
34744543Smarks /*
347510242Schris.kirby@sun.com  * inputs:
347610242Schris.kirby@sun.com  * zc_name	name of filesystem
347710242Schris.kirby@sun.com  * zc_value	short name of snap
347810242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
347910242Schris.kirby@sun.com  * zc_cookie	recursive flag
348010342Schris.kirby@sun.com  * zc_temphold	set if hold is temporary
348110242Schris.kirby@sun.com  *
348210242Schris.kirby@sun.com  * outputs:		none
348310242Schris.kirby@sun.com  */
348410242Schris.kirby@sun.com static int
348510242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
348610242Schris.kirby@sun.com {
348710242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
348810242Schris.kirby@sun.com 
348910242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
349010242Schris.kirby@sun.com 		return (EINVAL);
349110242Schris.kirby@sun.com 
349210242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
349310342Schris.kirby@sun.com 	    zc->zc_string, recursive, zc->zc_temphold));
349410242Schris.kirby@sun.com }
349510242Schris.kirby@sun.com 
349610242Schris.kirby@sun.com /*
349710242Schris.kirby@sun.com  * inputs:
349810242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
349910242Schris.kirby@sun.com  * zc_value	short name of snap
350010242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
350110242Schris.kirby@sun.com  * zc_cookie	recursive flag
350210242Schris.kirby@sun.com  *
350310242Schris.kirby@sun.com  * outputs:		none
350410242Schris.kirby@sun.com  */
350510242Schris.kirby@sun.com static int
350610242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
350710242Schris.kirby@sun.com {
350810242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
350910242Schris.kirby@sun.com 
351010242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
351110242Schris.kirby@sun.com 		return (EINVAL);
351210242Schris.kirby@sun.com 
351310242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
351410242Schris.kirby@sun.com 	    zc->zc_string, recursive));
351510242Schris.kirby@sun.com }
351610242Schris.kirby@sun.com 
351710242Schris.kirby@sun.com /*
351810242Schris.kirby@sun.com  * inputs:
351910242Schris.kirby@sun.com  * zc_name		name of filesystem
352010242Schris.kirby@sun.com  *
352110242Schris.kirby@sun.com  * outputs:
352210242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
352310242Schris.kirby@sun.com  */
352410242Schris.kirby@sun.com static int
352510242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
352610242Schris.kirby@sun.com {
352710242Schris.kirby@sun.com 	nvlist_t *nvp;
352810242Schris.kirby@sun.com 	int error;
352910242Schris.kirby@sun.com 
353010242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
353110242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
353210242Schris.kirby@sun.com 		nvlist_free(nvp);
353310242Schris.kirby@sun.com 	}
353410242Schris.kirby@sun.com 
353510242Schris.kirby@sun.com 	return (error);
353610242Schris.kirby@sun.com }
353710242Schris.kirby@sun.com 
353810242Schris.kirby@sun.com /*
35394988Sek110237  * pool create, destroy, and export don't log the history as part of
35404988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35414988Sek110237  * do the logging of those commands.
35424543Smarks  */
3543789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
35449234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35459234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35469234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35479234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35489234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35499234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35509234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35519234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35529234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
35539234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35549234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
35559234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35569234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
35579234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35589234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35599234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35609234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
35619234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35629234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
35639234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35649234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
35659234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35669234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35679234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35689234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35699234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35709234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
35719234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35729234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35739234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35749234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
35759234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35769234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35779234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35789425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35799425SEric.Schrock@Sun.COM 	    B_TRUE },
35809234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35819234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35829234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35839234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35849234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35859234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35869234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
35879234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35889234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
35899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
35909234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
35919234SGeorge.Wilson@Sun.COM 	    B_TRUE},
35929234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
35939234SGeorge.Wilson@Sun.COM 	    B_TRUE },
35949234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
35959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
35969234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
35979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
35989234SGeorge.Wilson@Sun.COM 	    B_FALSE },
35999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
36009234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36019234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
36029234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36039234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
36049234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
36069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
36079234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
36099234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
36119234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
36139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
361410233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
361510233SGeorge.Wilson@Sun.COM 	    B_TRUE },
36169234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
36179234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36189234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
36199234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36209234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
36219234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36229234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
36239234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36249234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
36259234SGeorge.Wilson@Sun.COM 	    B_FALSE },
36269234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
36279234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
36289234SGeorge.Wilson@Sun.COM 	    B_TRUE },
36299234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
36309396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
36319396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
36329396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36339396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
36349396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
36359396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
36369396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
363710242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
363810242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
363910242Schris.kirby@sun.com 	    B_TRUE },
364010242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
364110242Schris.kirby@sun.com 	    B_TRUE }
3642789Sahrens };
3643789Sahrens 
36449234SGeorge.Wilson@Sun.COM int
36459234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
36469234SGeorge.Wilson@Sun.COM {
36479234SGeorge.Wilson@Sun.COM 	spa_t *spa;
36489234SGeorge.Wilson@Sun.COM 	int error;
36499234SGeorge.Wilson@Sun.COM 
36509234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
36519234SGeorge.Wilson@Sun.COM 
36529396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
36539234SGeorge.Wilson@Sun.COM 	if (error == 0) {
36549234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
36559234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
36569234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
36579234SGeorge.Wilson@Sun.COM 	}
36589234SGeorge.Wilson@Sun.COM 	return (error);
36599234SGeorge.Wilson@Sun.COM }
36609234SGeorge.Wilson@Sun.COM 
3661789Sahrens static int
3662789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3663789Sahrens {
3664789Sahrens 	zfs_cmd_t *zc;
3665789Sahrens 	uint_t vec;
36662199Sahrens 	int error, rc;
3667789Sahrens 
3668789Sahrens 	if (getminor(dev) != 0)
3669789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3670789Sahrens 
3671789Sahrens 	vec = cmd - ZFS_IOC;
36724787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3673789Sahrens 
3674789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3675789Sahrens 		return (EINVAL);
3676789Sahrens 
3677789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3678789Sahrens 
36799643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3680789Sahrens 
368110588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
36824543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3683789Sahrens 
3684789Sahrens 	/*
3685789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3686789Sahrens 	 * the lower layers.
3687789Sahrens 	 */
3688789Sahrens 	if (error == 0) {
3689789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
36909643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
3691789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
36924577Sahrens 		case POOL_NAME:
3693789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3694789Sahrens 				error = EINVAL;
36959234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
36969234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
36979234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3698789Sahrens 			break;
3699789Sahrens 
37004577Sahrens 		case DATASET_NAME:
3701789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3702789Sahrens 				error = EINVAL;
37039234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
37049234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
37059234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
3706789Sahrens 			break;
37072856Snd150628 
37084577Sahrens 		case NO_NAME:
37092856Snd150628 			break;
3710789Sahrens 		}
3711789Sahrens 	}
3712789Sahrens 
3713789Sahrens 	if (error == 0)
3714789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3715789Sahrens 
37169643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
37174543Smarks 	if (error == 0) {
37182199Sahrens 		error = rc;
37199396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
37204543Smarks 			zfs_log_history(zc);
37214543Smarks 	}
3722789Sahrens 
3723789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3724789Sahrens 	return (error);
3725789Sahrens }
3726789Sahrens 
3727789Sahrens static int
3728789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3729789Sahrens {
3730789Sahrens 	if (cmd != DDI_ATTACH)
3731789Sahrens 		return (DDI_FAILURE);
3732789Sahrens 
3733789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3734789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3735789Sahrens 		return (DDI_FAILURE);
3736789Sahrens 
3737789Sahrens 	zfs_dip = dip;
3738789Sahrens 
3739789Sahrens 	ddi_report_dev(dip);
3740789Sahrens 
3741789Sahrens 	return (DDI_SUCCESS);
3742789Sahrens }
3743789Sahrens 
3744789Sahrens static int
3745789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3746789Sahrens {
3747789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3748789Sahrens 		return (DDI_FAILURE);
3749789Sahrens 
3750789Sahrens 	if (cmd != DDI_DETACH)
3751789Sahrens 		return (DDI_FAILURE);
3752789Sahrens 
3753789Sahrens 	zfs_dip = NULL;
3754789Sahrens 
3755789Sahrens 	ddi_prop_remove_all(dip);
3756789Sahrens 	ddi_remove_minor_node(dip, NULL);
3757789Sahrens 
3758789Sahrens 	return (DDI_SUCCESS);
3759789Sahrens }
3760789Sahrens 
3761789Sahrens /*ARGSUSED*/
3762789Sahrens static int
3763789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3764789Sahrens {
3765789Sahrens 	switch (infocmd) {
3766789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3767789Sahrens 		*result = zfs_dip;
3768789Sahrens 		return (DDI_SUCCESS);
3769789Sahrens 
3770789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3771849Sbonwick 		*result = (void *)0;
3772789Sahrens 		return (DDI_SUCCESS);
3773789Sahrens 	}
3774789Sahrens 
3775789Sahrens 	return (DDI_FAILURE);
3776789Sahrens }
3777789Sahrens 
3778789Sahrens /*
3779789Sahrens  * OK, so this is a little weird.
3780789Sahrens  *
3781789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3782789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3783789Sahrens  *
3784789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3785789Sahrens  * so most of the standard driver entry points are in zvol.c.
3786789Sahrens  */
3787789Sahrens static struct cb_ops zfs_cb_ops = {
3788789Sahrens 	zvol_open,	/* open */
3789789Sahrens 	zvol_close,	/* close */
3790789Sahrens 	zvol_strategy,	/* strategy */
3791789Sahrens 	nodev,		/* print */
37926423Sgw25295 	zvol_dump,	/* dump */
3793789Sahrens 	zvol_read,	/* read */
3794789Sahrens 	zvol_write,	/* write */
3795789Sahrens 	zfsdev_ioctl,	/* ioctl */
3796789Sahrens 	nodev,		/* devmap */
3797789Sahrens 	nodev,		/* mmap */
3798789Sahrens 	nodev,		/* segmap */
3799789Sahrens 	nochpoll,	/* poll */
3800789Sahrens 	ddi_prop_op,	/* prop_op */
3801789Sahrens 	NULL,		/* streamtab */
3802789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3803789Sahrens 	CB_REV,		/* version */
38043638Sbillm 	nodev,		/* async read */
38053638Sbillm 	nodev,		/* async write */
3806789Sahrens };
3807789Sahrens 
3808789Sahrens static struct dev_ops zfs_dev_ops = {
3809789Sahrens 	DEVO_REV,	/* version */
3810789Sahrens 	0,		/* refcnt */
3811789Sahrens 	zfs_info,	/* info */
3812789Sahrens 	nulldev,	/* identify */
3813789Sahrens 	nulldev,	/* probe */
3814789Sahrens 	zfs_attach,	/* attach */
3815789Sahrens 	zfs_detach,	/* detach */
3816789Sahrens 	nodev,		/* reset */
3817789Sahrens 	&zfs_cb_ops,	/* driver operations */
38187656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
38197656SSherry.Moore@Sun.COM 	NULL,		/* power */
38207656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
3821789Sahrens };
3822789Sahrens 
3823789Sahrens static struct modldrv zfs_modldrv = {
38247656SSherry.Moore@Sun.COM 	&mod_driverops,
38257656SSherry.Moore@Sun.COM 	"ZFS storage pool",
38267656SSherry.Moore@Sun.COM 	&zfs_dev_ops
3827789Sahrens };
3828789Sahrens 
3829789Sahrens static struct modlinkage modlinkage = {
3830789Sahrens 	MODREV_1,
3831789Sahrens 	(void *)&zfs_modlfs,
3832789Sahrens 	(void *)&zfs_modldrv,
3833789Sahrens 	NULL
3834789Sahrens };
3835789Sahrens 
38364720Sfr157268 
38374720Sfr157268 uint_t zfs_fsyncer_key;
38385326Sek110237 extern uint_t rrw_tsd_key;
38394720Sfr157268 
3840789Sahrens int
3841789Sahrens _init(void)
3842789Sahrens {
3843789Sahrens 	int error;
3844789Sahrens 
3845849Sbonwick 	spa_init(FREAD | FWRITE);
3846849Sbonwick 	zfs_init();
3847849Sbonwick 	zvol_init();
3848849Sbonwick 
3849849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3850849Sbonwick 		zvol_fini();
3851849Sbonwick 		zfs_fini();
3852849Sbonwick 		spa_fini();
3853789Sahrens 		return (error);
3854849Sbonwick 	}
3855789Sahrens 
38564720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
38575326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
38584720Sfr157268 
3859789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3860789Sahrens 	ASSERT(error == 0);
38614543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3862789Sahrens 
3863789Sahrens 	return (0);
3864789Sahrens }
3865789Sahrens 
3866789Sahrens int
3867789Sahrens _fini(void)
3868789Sahrens {
3869789Sahrens 	int error;
3870789Sahrens 
38711544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3872789Sahrens 		return (EBUSY);
3873789Sahrens 
3874789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3875789Sahrens 		return (error);
3876789Sahrens 
3877789Sahrens 	zvol_fini();
3878789Sahrens 	zfs_fini();
3879789Sahrens 	spa_fini();
38805331Samw 	if (zfs_nfsshare_inited)
38814543Smarks 		(void) ddi_modclose(nfs_mod);
38825331Samw 	if (zfs_smbshare_inited)
38835331Samw 		(void) ddi_modclose(smbsrv_mod);
38845331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
38854543Smarks 		(void) ddi_modclose(sharefs_mod);
3886789Sahrens 
38874720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
3888789Sahrens 	ldi_ident_release(zfs_li);
3889789Sahrens 	zfs_li = NULL;
38904543Smarks 	mutex_destroy(&zfs_share_lock);
3891789Sahrens 
3892789Sahrens 	return (error);
3893789Sahrens }
3894789Sahrens 
3895789Sahrens int
3896789Sahrens _info(struct modinfo *modinfop)
3897789Sahrens {
3898789Sahrens 	return (mod_info(&modlinkage, modinfop));
3899789Sahrens }
3900