xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 12296:7cf402a7f374)
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 /*
22*12296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/types.h>
26789Sahrens #include <sys/param.h>
27789Sahrens #include <sys/errno.h>
28789Sahrens #include <sys/uio.h>
29789Sahrens #include <sys/buf.h>
30789Sahrens #include <sys/modctl.h>
31789Sahrens #include <sys/open.h>
32789Sahrens #include <sys/file.h>
33789Sahrens #include <sys/kmem.h>
34789Sahrens #include <sys/conf.h>
35789Sahrens #include <sys/cmn_err.h>
36789Sahrens #include <sys/stat.h>
37789Sahrens #include <sys/zfs_ioctl.h>
3810972SRic.Aleshire@Sun.COM #include <sys/zfs_vfsops.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>
4410972SRic.Aleshire@Sun.COM #include <sys/priv_impl.h>
45789Sahrens #include <sys/dmu.h>
46789Sahrens #include <sys/dsl_dir.h>
47789Sahrens #include <sys/dsl_dataset.h>
48789Sahrens #include <sys/dsl_prop.h>
494543Smarks #include <sys/dsl_deleg.h>
504543Smarks #include <sys/dmu_objset.h>
51789Sahrens #include <sys/ddi.h>
52789Sahrens #include <sys/sunddi.h>
53789Sahrens #include <sys/sunldi.h>
54789Sahrens #include <sys/policy.h>
55789Sahrens #include <sys/zone.h>
56789Sahrens #include <sys/nvpair.h>
57789Sahrens #include <sys/pathname.h>
58789Sahrens #include <sys/mount.h>
59789Sahrens #include <sys/sdt.h>
60789Sahrens #include <sys/fs/zfs.h>
61789Sahrens #include <sys/zfs_ctldir.h>
625331Samw #include <sys/zfs_dir.h>
632885Sahrens #include <sys/zvol.h>
64*12296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
654543Smarks #include <sharefs/share.h>
665326Sek110237 #include <sys/dmu_objset.h>
67789Sahrens 
68789Sahrens #include "zfs_namecheck.h"
692676Seschrock #include "zfs_prop.h"
704543Smarks #include "zfs_deleg.h"
7111935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
72789Sahrens 
73789Sahrens extern struct modlfs zfs_modlfs;
74789Sahrens 
75789Sahrens extern void zfs_init(void);
76789Sahrens extern void zfs_fini(void);
77789Sahrens 
78789Sahrens ldi_ident_t zfs_li = NULL;
79789Sahrens dev_info_t *zfs_dip;
80789Sahrens 
81789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
824543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
83789Sahrens 
849234SGeorge.Wilson@Sun.COM typedef enum {
859234SGeorge.Wilson@Sun.COM 	NO_NAME,
869234SGeorge.Wilson@Sun.COM 	POOL_NAME,
879234SGeorge.Wilson@Sun.COM 	DATASET_NAME
889234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t;
899234SGeorge.Wilson@Sun.COM 
90789Sahrens typedef struct zfs_ioc_vec {
91789Sahrens 	zfs_ioc_func_t		*zvec_func;
92789Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
939234SGeorge.Wilson@Sun.COM 	zfs_ioc_namecheck_t	zvec_namecheck;
944543Smarks 	boolean_t		zvec_his_log;
959234SGeorge.Wilson@Sun.COM 	boolean_t		zvec_pool_check;
96789Sahrens } zfs_ioc_vec_t;
97789Sahrens 
989396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */
999396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = {
1009396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERUSED,
1019396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_USERQUOTA,
1029396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPUSED,
1039396SMatthew.Ahrens@Sun.COM 	ZFS_DELEG_PERM_GROUPQUOTA,
1049396SMatthew.Ahrens@Sun.COM };
1059396SMatthew.Ahrens@Sun.COM 
1069396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
10711022STom.Erickson@Sun.COM static int zfs_check_settable(const char *name, nvpair_t *property,
10811022STom.Erickson@Sun.COM     cred_t *cr);
10911022STom.Erickson@Sun.COM static int zfs_check_clearable(char *dataset, nvlist_t *props,
11011022STom.Erickson@Sun.COM     nvlist_t **errors);
1117184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1127184Stimh     boolean_t *);
11311022STom.Erickson@Sun.COM int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
1147184Stimh 
115789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
116789Sahrens void
117789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
118789Sahrens {
119789Sahrens 	const char *newfile;
120*12296SLin.Ling@Sun.COM 	char buf[512];
121789Sahrens 	va_list adx;
122789Sahrens 
123789Sahrens 	/*
124789Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
125789Sahrens 	 */
126789Sahrens 	newfile = strrchr(file, '/');
127789Sahrens 	if (newfile != NULL) {
128789Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
129789Sahrens 	} else {
130789Sahrens 		newfile = file;
131789Sahrens 	}
132789Sahrens 
133789Sahrens 	va_start(adx, fmt);
134789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
135789Sahrens 	va_end(adx);
136789Sahrens 
137789Sahrens 	/*
138789Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
139789Sahrens 	 * dtrace -q -n 'zfs-dprintf \
140789Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
141789Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
142789Sahrens 	 * arg0 = file name
143789Sahrens 	 * arg1 = function name
144789Sahrens 	 * arg2 = line number
145789Sahrens 	 * arg3 = message
146789Sahrens 	 */
147789Sahrens 	DTRACE_PROBE4(zfs__dprintf,
148789Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
149789Sahrens }
150789Sahrens 
1514543Smarks static void
1524715Sek110237 history_str_free(char *buf)
1534715Sek110237 {
1544715Sek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
1554715Sek110237 }
1564715Sek110237 
1574715Sek110237 static char *
1584715Sek110237 history_str_get(zfs_cmd_t *zc)
1594715Sek110237 {
1604715Sek110237 	char *buf;
1614715Sek110237 
1624715Sek110237 	if (zc->zc_history == NULL)
1634715Sek110237 		return (NULL);
1644715Sek110237 
1654715Sek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
1664715Sek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
1674715Sek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
1684715Sek110237 		history_str_free(buf);
1694715Sek110237 		return (NULL);
1704715Sek110237 	}
1714715Sek110237 
1724715Sek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
1734715Sek110237 
1744715Sek110237 	return (buf);
1754715Sek110237 }
1764715Sek110237 
1775375Stimh /*
1787042Sgw25295  * Check to see if the named dataset is currently defined as bootable
1797042Sgw25295  */
1807042Sgw25295 static boolean_t
1817042Sgw25295 zfs_is_bootfs(const char *name)
1827042Sgw25295 {
18310298SMatthew.Ahrens@Sun.COM 	objset_t *os;
18410298SMatthew.Ahrens@Sun.COM 
18510298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
18610298SMatthew.Ahrens@Sun.COM 		boolean_t ret;
18710922SJeff.Bonwick@Sun.COM 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
18810298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18910298SMatthew.Ahrens@Sun.COM 		return (ret);
1907042Sgw25295 	}
19110298SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
1927042Sgw25295 }
1937042Sgw25295 
1947042Sgw25295 /*
1957184Stimh  * zfs_earlier_version
1965375Stimh  *
1975375Stimh  *	Return non-zero if the spa version is less than requested version.
1985375Stimh  */
1995331Samw static int
2007184Stimh zfs_earlier_version(const char *name, int version)
2015331Samw {
2025331Samw 	spa_t *spa;
2035331Samw 
2045331Samw 	if (spa_open(name, &spa, FTAG) == 0) {
2055331Samw 		if (spa_version(spa) < version) {
2065331Samw 			spa_close(spa, FTAG);
2075331Samw 			return (1);
2085331Samw 		}
2095331Samw 		spa_close(spa, FTAG);
2105331Samw 	}
2115331Samw 	return (0);
2125331Samw }
2135331Samw 
2145977Smarks /*
2156689Smaybee  * zpl_earlier_version
2165977Smarks  *
2176689Smaybee  * Return TRUE if the ZPL version is less than requested version.
2185977Smarks  */
2196689Smaybee static boolean_t
2206689Smaybee zpl_earlier_version(const char *name, int version)
2215977Smarks {
2225977Smarks 	objset_t *os;
2236689Smaybee 	boolean_t rc = B_TRUE;
2245977Smarks 
22510298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
2266689Smaybee 		uint64_t zplversion;
2276689Smaybee 
22810298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
22910298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
23010298SMatthew.Ahrens@Sun.COM 			return (B_TRUE);
23110298SMatthew.Ahrens@Sun.COM 		}
23210298SMatthew.Ahrens@Sun.COM 		/* XXX reading from non-owned objset */
2336689Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
2346689Smaybee 			rc = zplversion < version;
23510298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
2365977Smarks 	}
2375977Smarks 	return (rc);
2385977Smarks }
2395977Smarks 
2404715Sek110237 static void
2414543Smarks zfs_log_history(zfs_cmd_t *zc)
2424543Smarks {
2434543Smarks 	spa_t *spa;
2444603Sahrens 	char *buf;
2454543Smarks 
2464715Sek110237 	if ((buf = history_str_get(zc)) == NULL)
2474577Sahrens 		return;
2484577Sahrens 
2494715Sek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
2504715Sek110237 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
2514715Sek110237 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
2524715Sek110237 		spa_close(spa, FTAG);
2534543Smarks 	}
2544715Sek110237 	history_str_free(buf);
2554543Smarks }
2564543Smarks 
257789Sahrens /*
258789Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
259789Sahrens  * and can be used in the local zone, as there is no associated dataset.
260789Sahrens  */
261789Sahrens /* ARGSUSED */
262789Sahrens static int
2634543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
264789Sahrens {
265789Sahrens 	return (0);
266789Sahrens }
267789Sahrens 
268789Sahrens /*
269789Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
270789Sahrens  * no privileges, but must be visible in the local zone.
271789Sahrens  */
272789Sahrens /* ARGSUSED */
273789Sahrens static int
2744543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
275789Sahrens {
276789Sahrens 	if (INGLOBALZONE(curproc) ||
2774543Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
278789Sahrens 		return (0);
279789Sahrens 
280789Sahrens 	return (ENOENT);
281789Sahrens }
282789Sahrens 
283789Sahrens static int
284789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
285789Sahrens {
286789Sahrens 	uint64_t zoned;
287789Sahrens 	int writable = 1;
288789Sahrens 
289789Sahrens 	/*
290789Sahrens 	 * The dataset must be visible by this zone -- check this first
291789Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
292789Sahrens 	 */
293789Sahrens 	if (!INGLOBALZONE(curproc) &&
294789Sahrens 	    !zone_dataset_visible(dataset, &writable))
295789Sahrens 		return (ENOENT);
296789Sahrens 
297789Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
298789Sahrens 		return (ENOENT);
299789Sahrens 
300789Sahrens 	if (INGLOBALZONE(curproc)) {
301789Sahrens 		/*
302789Sahrens 		 * If the fs is zoned, only root can access it from the
303789Sahrens 		 * global zone.
304789Sahrens 		 */
305789Sahrens 		if (secpolicy_zfs(cr) && zoned)
306789Sahrens 			return (EPERM);
307789Sahrens 	} else {
308789Sahrens 		/*
309789Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
310789Sahrens 		 */
311789Sahrens 		if (!zoned)
312789Sahrens 			return (EPERM);
313789Sahrens 
314789Sahrens 		/* must be writable by this zone */
315789Sahrens 		if (!writable)
316789Sahrens 			return (EPERM);
317789Sahrens 	}
318789Sahrens 	return (0);
319789Sahrens }
320789Sahrens 
321789Sahrens int
3224543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
323789Sahrens {
324789Sahrens 	int error;
325789Sahrens 
3264543Smarks 	error = zfs_dozonecheck(name, cr);
3274543Smarks 	if (error == 0) {
3284543Smarks 		error = secpolicy_zfs(cr);
3294670Sahrens 		if (error)
3304543Smarks 			error = dsl_deleg_access(name, perm, cr);
3314543Smarks 	}
3324543Smarks 	return (error);
3334543Smarks }
3344543Smarks 
33510972SRic.Aleshire@Sun.COM /*
33610972SRic.Aleshire@Sun.COM  * Policy for setting the security label property.
33710972SRic.Aleshire@Sun.COM  *
33810972SRic.Aleshire@Sun.COM  * Returns 0 for success, non-zero for access and other errors.
33910972SRic.Aleshire@Sun.COM  */
34010972SRic.Aleshire@Sun.COM static int
34111022STom.Erickson@Sun.COM zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
34210972SRic.Aleshire@Sun.COM {
34310972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
34410972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl, new_sl;
34510972SRic.Aleshire@Sun.COM 	boolean_t	new_default = FALSE;
34610972SRic.Aleshire@Sun.COM 	uint64_t	zoned;
34710972SRic.Aleshire@Sun.COM 	int		needed_priv = -1;
34810972SRic.Aleshire@Sun.COM 	int		error;
34910972SRic.Aleshire@Sun.COM 
35010972SRic.Aleshire@Sun.COM 	/* First get the existing dataset label. */
35110972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
35210972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
35310972SRic.Aleshire@Sun.COM 	if (error)
35410972SRic.Aleshire@Sun.COM 		return (EPERM);
35510972SRic.Aleshire@Sun.COM 
35610972SRic.Aleshire@Sun.COM 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
35710972SRic.Aleshire@Sun.COM 		new_default = TRUE;
35810972SRic.Aleshire@Sun.COM 
35910972SRic.Aleshire@Sun.COM 	/* The label must be translatable */
36010972SRic.Aleshire@Sun.COM 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
36110972SRic.Aleshire@Sun.COM 		return (EINVAL);
36210972SRic.Aleshire@Sun.COM 
36310972SRic.Aleshire@Sun.COM 	/*
36410972SRic.Aleshire@Sun.COM 	 * In a non-global zone, disallow attempts to set a label that
36510972SRic.Aleshire@Sun.COM 	 * doesn't match that of the zone; otherwise no other checks
36610972SRic.Aleshire@Sun.COM 	 * are needed.
36710972SRic.Aleshire@Sun.COM 	 */
36810972SRic.Aleshire@Sun.COM 	if (!INGLOBALZONE(curproc)) {
36910972SRic.Aleshire@Sun.COM 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
37010972SRic.Aleshire@Sun.COM 			return (EPERM);
37110972SRic.Aleshire@Sun.COM 		return (0);
37210972SRic.Aleshire@Sun.COM 	}
37310972SRic.Aleshire@Sun.COM 
37410972SRic.Aleshire@Sun.COM 	/*
37510972SRic.Aleshire@Sun.COM 	 * For global-zone datasets (i.e., those whose zoned property is
37610972SRic.Aleshire@Sun.COM 	 * "off", verify that the specified new label is valid for the
37710972SRic.Aleshire@Sun.COM 	 * global zone.
37810972SRic.Aleshire@Sun.COM 	 */
37910972SRic.Aleshire@Sun.COM 	if (dsl_prop_get_integer(name,
38010972SRic.Aleshire@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
38110972SRic.Aleshire@Sun.COM 		return (EPERM);
38210972SRic.Aleshire@Sun.COM 	if (!zoned) {
38310972SRic.Aleshire@Sun.COM 		if (zfs_check_global_label(name, strval) != 0)
38410972SRic.Aleshire@Sun.COM 			return (EPERM);
38510972SRic.Aleshire@Sun.COM 	}
38610972SRic.Aleshire@Sun.COM 
38710972SRic.Aleshire@Sun.COM 	/*
38810972SRic.Aleshire@Sun.COM 	 * If the existing dataset label is nondefault, check if the
38910972SRic.Aleshire@Sun.COM 	 * dataset is mounted (label cannot be changed while mounted).
39010972SRic.Aleshire@Sun.COM 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
39110972SRic.Aleshire@Sun.COM 	 * mounted (or isn't a dataset, doesn't exist, ...).
39210972SRic.Aleshire@Sun.COM 	 */
39310972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
39411022STom.Erickson@Sun.COM 		objset_t *os;
39511022STom.Erickson@Sun.COM 		static char *setsl_tag = "setsl_tag";
39611022STom.Erickson@Sun.COM 
39710972SRic.Aleshire@Sun.COM 		/*
39810972SRic.Aleshire@Sun.COM 		 * Try to own the dataset; abort if there is any error,
39910972SRic.Aleshire@Sun.COM 		 * (e.g., already mounted, in use, or other error).
40010972SRic.Aleshire@Sun.COM 		 */
40110972SRic.Aleshire@Sun.COM 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
40211022STom.Erickson@Sun.COM 		    setsl_tag, &os);
40310972SRic.Aleshire@Sun.COM 		if (error)
40410972SRic.Aleshire@Sun.COM 			return (EPERM);
40510972SRic.Aleshire@Sun.COM 
40611022STom.Erickson@Sun.COM 		dmu_objset_disown(os, setsl_tag);
40711022STom.Erickson@Sun.COM 
40810972SRic.Aleshire@Sun.COM 		if (new_default) {
40910972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
41010972SRic.Aleshire@Sun.COM 			goto out_check;
41110972SRic.Aleshire@Sun.COM 		}
41210972SRic.Aleshire@Sun.COM 
41310972SRic.Aleshire@Sun.COM 		if (hexstr_to_label(strval, &new_sl) != 0)
41410972SRic.Aleshire@Sun.COM 			return (EPERM);
41510972SRic.Aleshire@Sun.COM 
41610972SRic.Aleshire@Sun.COM 		if (blstrictdom(&ds_sl, &new_sl))
41710972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
41810972SRic.Aleshire@Sun.COM 		else if (blstrictdom(&new_sl, &ds_sl))
41910972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
42010972SRic.Aleshire@Sun.COM 	} else {
42110972SRic.Aleshire@Sun.COM 		/* dataset currently has a default label */
42210972SRic.Aleshire@Sun.COM 		if (!new_default)
42310972SRic.Aleshire@Sun.COM 			needed_priv = PRIV_FILE_UPGRADE_SL;
42410972SRic.Aleshire@Sun.COM 	}
42510972SRic.Aleshire@Sun.COM 
42610972SRic.Aleshire@Sun.COM out_check:
42710972SRic.Aleshire@Sun.COM 	if (needed_priv != -1)
42810972SRic.Aleshire@Sun.COM 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
42910972SRic.Aleshire@Sun.COM 	return (0);
43010972SRic.Aleshire@Sun.COM }
43110972SRic.Aleshire@Sun.COM 
4324543Smarks static int
43311022STom.Erickson@Sun.COM zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
43411022STom.Erickson@Sun.COM     cred_t *cr)
4354543Smarks {
43611022STom.Erickson@Sun.COM 	char *strval;
43711022STom.Erickson@Sun.COM 
4384543Smarks 	/*
4394543Smarks 	 * Check permissions for special properties.
4404543Smarks 	 */
4414543Smarks 	switch (prop) {
4424543Smarks 	case ZFS_PROP_ZONED:
4434543Smarks 		/*
4444543Smarks 		 * Disallow setting of 'zoned' from within a local zone.
4454543Smarks 		 */
4464543Smarks 		if (!INGLOBALZONE(curproc))
4474543Smarks 			return (EPERM);
4484543Smarks 		break;
449789Sahrens 
4504543Smarks 	case ZFS_PROP_QUOTA:
4514543Smarks 		if (!INGLOBALZONE(curproc)) {
4524543Smarks 			uint64_t zoned;
4534543Smarks 			char setpoint[MAXNAMELEN];
4544543Smarks 			/*
4554543Smarks 			 * Unprivileged users are allowed to modify the
4564543Smarks 			 * quota on things *under* (ie. contained by)
4574543Smarks 			 * the thing they own.
4584543Smarks 			 */
45911022STom.Erickson@Sun.COM 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
4604543Smarks 			    setpoint))
4614543Smarks 				return (EPERM);
46211022STom.Erickson@Sun.COM 			if (!zoned || strlen(dsname) <= strlen(setpoint))
4634543Smarks 				return (EPERM);
4644543Smarks 		}
4654670Sahrens 		break;
46610972SRic.Aleshire@Sun.COM 
46710972SRic.Aleshire@Sun.COM 	case ZFS_PROP_MLSLABEL:
46810972SRic.Aleshire@Sun.COM 		if (!is_system_labeled())
46910972SRic.Aleshire@Sun.COM 			return (EPERM);
47011022STom.Erickson@Sun.COM 
47111022STom.Erickson@Sun.COM 		if (nvpair_value_string(propval, &strval) == 0) {
47211022STom.Erickson@Sun.COM 			int err;
47311022STom.Erickson@Sun.COM 
47411022STom.Erickson@Sun.COM 			err = zfs_set_slabel_policy(dsname, strval, CRED());
47511022STom.Erickson@Sun.COM 			if (err != 0)
47611022STom.Erickson@Sun.COM 				return (err);
47711022STom.Erickson@Sun.COM 		}
47810972SRic.Aleshire@Sun.COM 		break;
4794543Smarks 	}
4804543Smarks 
48111022STom.Erickson@Sun.COM 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
482789Sahrens }
483789Sahrens 
4844543Smarks int
4854543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
4864543Smarks {
4874543Smarks 	int error;
4884543Smarks 
4894543Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
4904543Smarks 	if (error)
4914543Smarks 		return (error);
4924543Smarks 
4934543Smarks 	/*
4944543Smarks 	 * permission to set permissions will be evaluated later in
4954543Smarks 	 * dsl_deleg_can_allow()
4964543Smarks 	 */
4974543Smarks 	return (0);
4984543Smarks }
4994543Smarks 
5004543Smarks int
5014543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
5024543Smarks {
50310588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
50410588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_ROLLBACK, cr));
5054543Smarks }
5064543Smarks 
5074543Smarks int
5084543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
5094543Smarks {
5104543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
5114543Smarks 	    ZFS_DELEG_PERM_SEND, cr));
5124543Smarks }
5134543Smarks 
5148845Samw@Sun.COM static int
5158845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
5168845Samw@Sun.COM {
5178845Samw@Sun.COM 	vnode_t *vp;
5188845Samw@Sun.COM 	int error;
5198845Samw@Sun.COM 
5208845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5218845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
5228845Samw@Sun.COM 		return (error);
5238845Samw@Sun.COM 
5248845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
5258845Samw@Sun.COM 
5268845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5278845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5288845Samw@Sun.COM 	    zc->zc_name) != 0)) {
5298845Samw@Sun.COM 		VN_RELE(vp);
5308845Samw@Sun.COM 		return (EPERM);
5318845Samw@Sun.COM 	}
5328845Samw@Sun.COM 
5338845Samw@Sun.COM 	VN_RELE(vp);
5348845Samw@Sun.COM 	return (dsl_deleg_access(zc->zc_name,
5358845Samw@Sun.COM 	    ZFS_DELEG_PERM_SHARE, cr));
5368845Samw@Sun.COM }
5378845Samw@Sun.COM 
5384543Smarks int
5394543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
5404543Smarks {
5414543Smarks 	if (!INGLOBALZONE(curproc))
5424543Smarks 		return (EPERM);
5434543Smarks 
5445367Sahrens 	if (secpolicy_nfs(cr) == 0) {
5454543Smarks 		return (0);
5464543Smarks 	} else {
5478845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
5488845Samw@Sun.COM 	}
5498845Samw@Sun.COM }
5508845Samw@Sun.COM 
5518845Samw@Sun.COM int
5528845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
5538845Samw@Sun.COM {
5548845Samw@Sun.COM 	if (!INGLOBALZONE(curproc))
5558845Samw@Sun.COM 		return (EPERM);
5568845Samw@Sun.COM 
5578845Samw@Sun.COM 	if (secpolicy_smb(cr) == 0) {
5588845Samw@Sun.COM 		return (0);
5598845Samw@Sun.COM 	} else {
5608845Samw@Sun.COM 		return (zfs_secpolicy_deleg_share(zc, cr));
5614543Smarks 	}
5624543Smarks }
5634543Smarks 
564789Sahrens static int
5654543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
566789Sahrens {
567789Sahrens 	char *cp;
568789Sahrens 
569789Sahrens 	/*
570789Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
571789Sahrens 	 */
5724543Smarks 	(void) strncpy(parent, datasetname, parentsize);
5734543Smarks 	cp = strrchr(parent, '@');
574789Sahrens 	if (cp != NULL) {
575789Sahrens 		cp[0] = '\0';
576789Sahrens 	} else {
5774543Smarks 		cp = strrchr(parent, '/');
578789Sahrens 		if (cp == NULL)
579789Sahrens 			return (ENOENT);
580789Sahrens 		cp[0] = '\0';
581789Sahrens 	}
582789Sahrens 
5834543Smarks 	return (0);
5844543Smarks }
5854543Smarks 
5864543Smarks int
5874543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
5884543Smarks {
5894543Smarks 	int error;
5904543Smarks 
5914543Smarks 	if ((error = zfs_secpolicy_write_perms(name,
5924543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
5934543Smarks 		return (error);
5944543Smarks 
5954543Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
5964543Smarks }
5974543Smarks 
5984543Smarks static int
5994543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
6004543Smarks {
6014543Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
6024543Smarks }
6034543Smarks 
6044543Smarks /*
60511314Swilliam.gorrell@sun.com  * Destroying snapshots with delegated permissions requires
60611314Swilliam.gorrell@sun.com  * descendent mount and destroy permissions.
60711314Swilliam.gorrell@sun.com  * Reassemble the full filesystem@snap name so dsl_deleg_access()
60811314Swilliam.gorrell@sun.com  * can do the correct permission check.
60911314Swilliam.gorrell@sun.com  *
61011314Swilliam.gorrell@sun.com  * Since this routine is used when doing a recursive destroy of snapshots
61111314Swilliam.gorrell@sun.com  * and destroying snapshots requires descendent permissions, a successfull
61211314Swilliam.gorrell@sun.com  * check of the top level snapshot applies to snapshots of all descendent
61311314Swilliam.gorrell@sun.com  * datasets as well.
61411314Swilliam.gorrell@sun.com  */
61511314Swilliam.gorrell@sun.com static int
61611314Swilliam.gorrell@sun.com zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
61711314Swilliam.gorrell@sun.com {
61811314Swilliam.gorrell@sun.com 	int error;
61911314Swilliam.gorrell@sun.com 	char *dsname;
62011314Swilliam.gorrell@sun.com 
62111314Swilliam.gorrell@sun.com 	dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
62211314Swilliam.gorrell@sun.com 
62311314Swilliam.gorrell@sun.com 	error = zfs_secpolicy_destroy_perms(dsname, cr);
62411314Swilliam.gorrell@sun.com 
62511314Swilliam.gorrell@sun.com 	strfree(dsname);
62611314Swilliam.gorrell@sun.com 	return (error);
62711314Swilliam.gorrell@sun.com }
62811314Swilliam.gorrell@sun.com 
6294543Smarks int
6304543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
6314543Smarks {
63211022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
6334543Smarks 	int	error;
6344543Smarks 
6354543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
6364543Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
6374543Smarks 		return (error);
6384543Smarks 
6394543Smarks 	if ((error = zfs_secpolicy_write_perms(from,
6404543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
6414543Smarks 		return (error);
6424543Smarks 
6434543Smarks 	if ((error = zfs_get_parent(to, parentname,
6444543Smarks 	    sizeof (parentname))) != 0)
6454543Smarks 		return (error);
6464543Smarks 
6474543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6484543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
6494543Smarks 		return (error);
6504543Smarks 
6514543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
6524543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
6534543Smarks 		return (error);
6544543Smarks 
6554543Smarks 	return (error);
6564543Smarks }
6574543Smarks 
6584543Smarks static int
6594543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
6604543Smarks {
6614543Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
6624543Smarks }
6634543Smarks 
6644543Smarks static int
6654543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
6664543Smarks {
66711022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
6684543Smarks 	objset_t *clone;
6694543Smarks 	int error;
6704543Smarks 
6714543Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
6724543Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
6734543Smarks 	if (error)
6744543Smarks 		return (error);
6754543Smarks 
67610298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
6774543Smarks 
6784543Smarks 	if (error == 0) {
6794543Smarks 		dsl_dataset_t *pclone = NULL;
6804543Smarks 		dsl_dir_t *dd;
68110298SMatthew.Ahrens@Sun.COM 		dd = clone->os_dsl_dataset->ds_dir;
6824543Smarks 
6834543Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
6846689Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
6856689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
6864543Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
6874543Smarks 		if (error) {
68810298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(clone, FTAG);
6894543Smarks 			return (error);
6904543Smarks 		}
6914543Smarks 
6924543Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
6934543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
6944543Smarks 
6954543Smarks 		dsl_dataset_name(pclone, parentname);
69610298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
6976689Smaybee 		dsl_dataset_rele(pclone, FTAG);
6984543Smarks 		if (error == 0)
6994543Smarks 			error = zfs_secpolicy_write_perms(parentname,
7004543Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
7014543Smarks 	}
7024543Smarks 	return (error);
7034543Smarks }
7044543Smarks 
7054543Smarks static int
7064543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
7074543Smarks {
7084543Smarks 	int error;
7094543Smarks 
7104543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7114543Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
7124543Smarks 		return (error);
7134543Smarks 
7144543Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
7154543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
7164543Smarks 		return (error);
7174543Smarks 
7184543Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
7194543Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
7204543Smarks }
7214543Smarks 
7224543Smarks int
7234543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
7244543Smarks {
72510588SEric.Taylor@Sun.COM 	return (zfs_secpolicy_write_perms(name,
72610588SEric.Taylor@Sun.COM 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
7274543Smarks }
7284543Smarks 
7294543Smarks static int
7304543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
7314543Smarks {
7324543Smarks 
7334543Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
7344543Smarks }
7354543Smarks 
7364543Smarks static int
7374543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
7384543Smarks {
73911022STom.Erickson@Sun.COM 	char	parentname[MAXNAMELEN];
74011022STom.Erickson@Sun.COM 	int	error;
7414543Smarks 
7424543Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
7434543Smarks 	    sizeof (parentname))) != 0)
7444543Smarks 		return (error);
7454543Smarks 
7464543Smarks 	if (zc->zc_value[0] != '\0') {
7474543Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
7484543Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
7494543Smarks 			return (error);
7504543Smarks 	}
7514543Smarks 
7524543Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
7534543Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
7544543Smarks 		return (error);
7554543Smarks 
7564543Smarks 	error = zfs_secpolicy_write_perms(parentname,
7574543Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
7584543Smarks 
7594543Smarks 	return (error);
7604543Smarks }
7614543Smarks 
7624543Smarks static int
7634543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
7644543Smarks {
7654543Smarks 	int error;
7664543Smarks 
7674543Smarks 	error = secpolicy_fs_unmount(cr, NULL);
7684543Smarks 	if (error) {
7694543Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
7704543Smarks 	}
7714543Smarks 	return (error);
772789Sahrens }
773789Sahrens 
774789Sahrens /*
775789Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
776789Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
777789Sahrens  */
778789Sahrens /* ARGSUSED */
779789Sahrens static int
7804543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
781789Sahrens {
782789Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
783789Sahrens 		return (EPERM);
784789Sahrens 
785789Sahrens 	return (0);
786789Sahrens }
787789Sahrens 
788789Sahrens /*
7891544Seschrock  * Policy for fault injection.  Requires all privileges.
7901544Seschrock  */
7911544Seschrock /* ARGSUSED */
7921544Seschrock static int
7934543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
7941544Seschrock {
7951544Seschrock 	return (secpolicy_zinject(cr));
7961544Seschrock }
7971544Seschrock 
7984849Sahrens static int
7994849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
8004849Sahrens {
8014849Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
8024849Sahrens 
8035094Slling 	if (prop == ZPROP_INVAL) {
8044849Sahrens 		if (!zfs_prop_user(zc->zc_value))
8054849Sahrens 			return (EINVAL);
8064849Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
8074849Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
8084849Sahrens 	} else {
80911022STom.Erickson@Sun.COM 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
81011022STom.Erickson@Sun.COM 		    NULL, cr));
8114849Sahrens 	}
8124849Sahrens }
8134849Sahrens 
8149396SMatthew.Ahrens@Sun.COM static int
8159396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
8169396SMatthew.Ahrens@Sun.COM {
8179396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
8189396SMatthew.Ahrens@Sun.COM 	if (err)
8199396SMatthew.Ahrens@Sun.COM 		return (err);
8209396SMatthew.Ahrens@Sun.COM 
8219396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
8229396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
8239396SMatthew.Ahrens@Sun.COM 
8249396SMatthew.Ahrens@Sun.COM 	if (zc->zc_value[0] == 0) {
8259396SMatthew.Ahrens@Sun.COM 		/*
8269396SMatthew.Ahrens@Sun.COM 		 * They are asking about a posix uid/gid.  If it's
8279396SMatthew.Ahrens@Sun.COM 		 * themself, allow it.
8289396SMatthew.Ahrens@Sun.COM 		 */
8299396SMatthew.Ahrens@Sun.COM 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
8309396SMatthew.Ahrens@Sun.COM 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
8319396SMatthew.Ahrens@Sun.COM 			if (zc->zc_guid == crgetuid(cr))
8329396SMatthew.Ahrens@Sun.COM 				return (0);
8339396SMatthew.Ahrens@Sun.COM 		} else {
8349396SMatthew.Ahrens@Sun.COM 			if (groupmember(zc->zc_guid, cr))
8359396SMatthew.Ahrens@Sun.COM 				return (0);
8369396SMatthew.Ahrens@Sun.COM 		}
8379396SMatthew.Ahrens@Sun.COM 	}
8389396SMatthew.Ahrens@Sun.COM 
8399396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
8409396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
8419396SMatthew.Ahrens@Sun.COM }
8429396SMatthew.Ahrens@Sun.COM 
8439396SMatthew.Ahrens@Sun.COM static int
8449396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
8459396SMatthew.Ahrens@Sun.COM {
8469396SMatthew.Ahrens@Sun.COM 	int err = zfs_secpolicy_read(zc, cr);
8479396SMatthew.Ahrens@Sun.COM 	if (err)
8489396SMatthew.Ahrens@Sun.COM 		return (err);
8499396SMatthew.Ahrens@Sun.COM 
8509396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
8519396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
8529396SMatthew.Ahrens@Sun.COM 
8539396SMatthew.Ahrens@Sun.COM 	return (zfs_secpolicy_write_perms(zc->zc_name,
8549396SMatthew.Ahrens@Sun.COM 	    userquota_perms[zc->zc_objset_type], cr));
8559396SMatthew.Ahrens@Sun.COM }
8569396SMatthew.Ahrens@Sun.COM 
8579396SMatthew.Ahrens@Sun.COM static int
8589396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
8599396SMatthew.Ahrens@Sun.COM {
86011022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
86111022STom.Erickson@Sun.COM 	    NULL, cr));
8629396SMatthew.Ahrens@Sun.COM }
8639396SMatthew.Ahrens@Sun.COM 
86410242Schris.kirby@sun.com static int
86510242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
86610242Schris.kirby@sun.com {
86710242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
86810242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_HOLD, cr));
86910242Schris.kirby@sun.com }
87010242Schris.kirby@sun.com 
87110242Schris.kirby@sun.com static int
87210242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
87310242Schris.kirby@sun.com {
87410242Schris.kirby@sun.com 	return (zfs_secpolicy_write_perms(zc->zc_name,
87510242Schris.kirby@sun.com 	    ZFS_DELEG_PERM_RELEASE, cr));
87610242Schris.kirby@sun.com }
87710242Schris.kirby@sun.com 
8781544Seschrock /*
879789Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
880789Sahrens  */
881789Sahrens static int
8829643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
883789Sahrens {
884789Sahrens 	char *packed;
885789Sahrens 	int error;
8865094Slling 	nvlist_t *list = NULL;
887789Sahrens 
888789Sahrens 	/*
8892676Seschrock 	 * Read in and unpack the user-supplied nvlist.
890789Sahrens 	 */
8915094Slling 	if (size == 0)
892789Sahrens 		return (EINVAL);
893789Sahrens 
894789Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
895789Sahrens 
8969643SEric.Taylor@Sun.COM 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
8979643SEric.Taylor@Sun.COM 	    iflag)) != 0) {
898789Sahrens 		kmem_free(packed, size);
899789Sahrens 		return (error);
900789Sahrens 	}
901789Sahrens 
9025094Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
903789Sahrens 		kmem_free(packed, size);
904789Sahrens 		return (error);
905789Sahrens 	}
906789Sahrens 
907789Sahrens 	kmem_free(packed, size);
908789Sahrens 
9095094Slling 	*nvp = list;
910789Sahrens 	return (0);
911789Sahrens }
912789Sahrens 
913789Sahrens static int
91411022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
91511022STom.Erickson@Sun.COM {
91611022STom.Erickson@Sun.COM 	size_t size;
91711022STom.Erickson@Sun.COM 
91811022STom.Erickson@Sun.COM 	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
91911022STom.Erickson@Sun.COM 
92011022STom.Erickson@Sun.COM 	if (size > zc->zc_nvlist_dst_size) {
92111022STom.Erickson@Sun.COM 		nvpair_t *more_errors;
92211022STom.Erickson@Sun.COM 		int n = 0;
92311022STom.Erickson@Sun.COM 
92411022STom.Erickson@Sun.COM 		if (zc->zc_nvlist_dst_size < 1024)
92511022STom.Erickson@Sun.COM 			return (ENOMEM);
92611022STom.Erickson@Sun.COM 
92711022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
92811022STom.Erickson@Sun.COM 		more_errors = nvlist_prev_nvpair(*errors, NULL);
92911022STom.Erickson@Sun.COM 
93011022STom.Erickson@Sun.COM 		do {
93111022STom.Erickson@Sun.COM 			nvpair_t *pair = nvlist_prev_nvpair(*errors,
93211022STom.Erickson@Sun.COM 			    more_errors);
93311022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
93411022STom.Erickson@Sun.COM 			n++;
93511022STom.Erickson@Sun.COM 			VERIFY(nvlist_size(*errors, &size,
93611022STom.Erickson@Sun.COM 			    NV_ENCODE_NATIVE) == 0);
93711022STom.Erickson@Sun.COM 		} while (size > zc->zc_nvlist_dst_size);
93811022STom.Erickson@Sun.COM 
93911022STom.Erickson@Sun.COM 		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
94011022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
94111022STom.Erickson@Sun.COM 		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
94211022STom.Erickson@Sun.COM 		ASSERT(size <= zc->zc_nvlist_dst_size);
94311022STom.Erickson@Sun.COM 	}
94411022STom.Erickson@Sun.COM 
94511022STom.Erickson@Sun.COM 	return (0);
94611022STom.Erickson@Sun.COM }
94711022STom.Erickson@Sun.COM 
94811022STom.Erickson@Sun.COM static int
9492676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
9502676Seschrock {
9512676Seschrock 	char *packed = NULL;
95211807SSam.Falkner@Sun.COM 	int error = 0;
9532676Seschrock 	size_t size;
9542676Seschrock 
9552676Seschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
9562676Seschrock 
9572676Seschrock 	if (size > zc->zc_nvlist_dst_size) {
9582676Seschrock 		error = ENOMEM;
9592676Seschrock 	} else {
9604611Smarks 		packed = kmem_alloc(size, KM_SLEEP);
9612676Seschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
9622676Seschrock 		    KM_SLEEP) == 0);
96311807SSam.Falkner@Sun.COM 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
96411807SSam.Falkner@Sun.COM 		    size, zc->zc_iflags) != 0)
96511807SSam.Falkner@Sun.COM 			error = EFAULT;
9662676Seschrock 		kmem_free(packed, size);
9672676Seschrock 	}
9682676Seschrock 
9692676Seschrock 	zc->zc_nvlist_dst_size = size;
9702676Seschrock 	return (error);
9712676Seschrock }
9722676Seschrock 
9732676Seschrock static int
97411185SSean.McEnroe@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
9759396SMatthew.Ahrens@Sun.COM {
9769396SMatthew.Ahrens@Sun.COM 	objset_t *os;
9779396SMatthew.Ahrens@Sun.COM 	int error;
9789396SMatthew.Ahrens@Sun.COM 
97910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(dsname, FTAG, &os);
9809396SMatthew.Ahrens@Sun.COM 	if (error)
9819396SMatthew.Ahrens@Sun.COM 		return (error);
98210298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
98310298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
98410298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
98510298SMatthew.Ahrens@Sun.COM 	}
98610298SMatthew.Ahrens@Sun.COM 
98710298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_user_ptr_lock);
98811185SSean.McEnroe@Sun.COM 	*zfvp = dmu_objset_get_user(os);
98911185SSean.McEnroe@Sun.COM 	if (*zfvp) {
99011185SSean.McEnroe@Sun.COM 		VFS_HOLD((*zfvp)->z_vfs);
9919396SMatthew.Ahrens@Sun.COM 	} else {
9929396SMatthew.Ahrens@Sun.COM 		error = ESRCH;
9939396SMatthew.Ahrens@Sun.COM 	}
99410298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_user_ptr_lock);
99510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
9969396SMatthew.Ahrens@Sun.COM 	return (error);
9979396SMatthew.Ahrens@Sun.COM }
9989396SMatthew.Ahrens@Sun.COM 
9999396SMatthew.Ahrens@Sun.COM /*
10009396SMatthew.Ahrens@Sun.COM  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
10019396SMatthew.Ahrens@Sun.COM  * case its z_vfs will be NULL, and it will be opened as the owner.
10029396SMatthew.Ahrens@Sun.COM  */
10039396SMatthew.Ahrens@Sun.COM static int
100411185SSean.McEnroe@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp)
10059396SMatthew.Ahrens@Sun.COM {
10069396SMatthew.Ahrens@Sun.COM 	int error = 0;
10079396SMatthew.Ahrens@Sun.COM 
100811185SSean.McEnroe@Sun.COM 	if (getzfsvfs(name, zfvp) != 0)
100911185SSean.McEnroe@Sun.COM 		error = zfsvfs_create(name, zfvp);
10109396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
101111185SSean.McEnroe@Sun.COM 		rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag);
101211185SSean.McEnroe@Sun.COM 		if ((*zfvp)->z_unmounted) {
10139396SMatthew.Ahrens@Sun.COM 			/*
10149396SMatthew.Ahrens@Sun.COM 			 * XXX we could probably try again, since the unmounting
10159396SMatthew.Ahrens@Sun.COM 			 * thread should be just about to disassociate the
10169396SMatthew.Ahrens@Sun.COM 			 * objset from the zfsvfs.
10179396SMatthew.Ahrens@Sun.COM 			 */
101811185SSean.McEnroe@Sun.COM 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
10199396SMatthew.Ahrens@Sun.COM 			return (EBUSY);
10209396SMatthew.Ahrens@Sun.COM 		}
10219396SMatthew.Ahrens@Sun.COM 	}
10229396SMatthew.Ahrens@Sun.COM 	return (error);
10239396SMatthew.Ahrens@Sun.COM }
10249396SMatthew.Ahrens@Sun.COM 
10259396SMatthew.Ahrens@Sun.COM static void
10269396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
10279396SMatthew.Ahrens@Sun.COM {
10289396SMatthew.Ahrens@Sun.COM 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
10299396SMatthew.Ahrens@Sun.COM 
10309396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_vfs) {
10319396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
10329396SMatthew.Ahrens@Sun.COM 	} else {
103310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
10349396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
10359396SMatthew.Ahrens@Sun.COM 	}
10369396SMatthew.Ahrens@Sun.COM }
10379396SMatthew.Ahrens@Sun.COM 
10389396SMatthew.Ahrens@Sun.COM static int
1039789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1040789Sahrens {
1041789Sahrens 	int error;
10425094Slling 	nvlist_t *config, *props = NULL;
10437184Stimh 	nvlist_t *rootprops = NULL;
10447184Stimh 	nvlist_t *zplprops = NULL;
10454715Sek110237 	char *buf;
1046789Sahrens 
10475094Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
10489643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config))
10494988Sek110237 		return (error);
10504715Sek110237 
10515094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
10529643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
10539643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
10545094Slling 		nvlist_free(config);
10555094Slling 		return (error);
10565094Slling 	}
10575094Slling 
10587184Stimh 	if (props) {
10597184Stimh 		nvlist_t *nvl = NULL;
10607184Stimh 		uint64_t version = SPA_VERSION;
10617184Stimh 
10627184Stimh 		(void) nvlist_lookup_uint64(props,
10637184Stimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
10647184Stimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
10657184Stimh 			error = EINVAL;
10667184Stimh 			goto pool_props_bad;
10677184Stimh 		}
10687184Stimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
10697184Stimh 		if (nvl) {
10707184Stimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
10717184Stimh 			if (error != 0) {
10727184Stimh 				nvlist_free(config);
10737184Stimh 				nvlist_free(props);
10747184Stimh 				return (error);
10757184Stimh 			}
10767184Stimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
10777184Stimh 		}
10787184Stimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10797184Stimh 		error = zfs_fill_zplprops_root(version, rootprops,
10807184Stimh 		    zplprops, NULL);
10817184Stimh 		if (error)
10827184Stimh 			goto pool_props_bad;
10837184Stimh 	}
10847184Stimh 
10854988Sek110237 	buf = history_str_get(zc);
1086789Sahrens 
10877184Stimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
10887184Stimh 
10897184Stimh 	/*
10907184Stimh 	 * Set the remaining root properties
10917184Stimh 	 */
109211022STom.Erickson@Sun.COM 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
109311022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
10947184Stimh 		(void) spa_destroy(zc->zc_name);
1095789Sahrens 
10964988Sek110237 	if (buf != NULL)
10974988Sek110237 		history_str_free(buf);
10985094Slling 
10997184Stimh pool_props_bad:
11007184Stimh 	nvlist_free(rootprops);
11017184Stimh 	nvlist_free(zplprops);
1102789Sahrens 	nvlist_free(config);
11037184Stimh 	nvlist_free(props);
11045094Slling 
1105789Sahrens 	return (error);
1106789Sahrens }
1107789Sahrens 
1108789Sahrens static int
1109789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1110789Sahrens {
11114543Smarks 	int error;
11124543Smarks 	zfs_log_history(zc);
11134543Smarks 	error = spa_destroy(zc->zc_name);
111410588SEric.Taylor@Sun.COM 	if (error == 0)
111510588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
11164543Smarks 	return (error);
1117789Sahrens }
1118789Sahrens 
1119789Sahrens static int
1120789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1121789Sahrens {
11225094Slling 	nvlist_t *config, *props = NULL;
1123789Sahrens 	uint64_t guid;
112410921STim.Haley@Sun.COM 	int error;
1125789Sahrens 
11265094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
11279643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) != 0)
1128789Sahrens 		return (error);
1129789Sahrens 
11305094Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
11319643SEric.Taylor@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
11329643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props))) {
11335094Slling 		nvlist_free(config);
11345094Slling 		return (error);
11355094Slling 	}
11365094Slling 
1137789Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
11381544Seschrock 	    guid != zc->zc_guid)
1139789Sahrens 		error = EINVAL;
11406643Seschrock 	else if (zc->zc_cookie)
114110921STim.Haley@Sun.COM 		error = spa_import_verbatim(zc->zc_name, config, props);
1142789Sahrens 	else
11435094Slling 		error = spa_import(zc->zc_name, config, props);
1144789Sahrens 
114510921STim.Haley@Sun.COM 	if (zc->zc_nvlist_dst != 0)
114610921STim.Haley@Sun.COM 		(void) put_nvlist(zc, config);
114710921STim.Haley@Sun.COM 
1148789Sahrens 	nvlist_free(config);
1149789Sahrens 
11505094Slling 	if (props)
11515094Slling 		nvlist_free(props);
11525094Slling 
1153789Sahrens 	return (error);
1154789Sahrens }
1155789Sahrens 
1156789Sahrens static int
1157789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1158789Sahrens {
11594543Smarks 	int error;
11607214Slling 	boolean_t force = (boolean_t)zc->zc_cookie;
11618211SGeorge.Wilson@Sun.COM 	boolean_t hardforce = (boolean_t)zc->zc_guid;
11627214Slling 
11634543Smarks 	zfs_log_history(zc);
11648211SGeorge.Wilson@Sun.COM 	error = spa_export(zc->zc_name, NULL, force, hardforce);
116510588SEric.Taylor@Sun.COM 	if (error == 0)
116610588SEric.Taylor@Sun.COM 		zvol_remove_minors(zc->zc_name);
11674543Smarks 	return (error);
1168789Sahrens }
1169789Sahrens 
1170789Sahrens static int
1171789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1172789Sahrens {
1173789Sahrens 	nvlist_t *configs;
1174789Sahrens 	int error;
1175789Sahrens 
1176789Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1177789Sahrens 		return (EEXIST);
1178789Sahrens 
11792676Seschrock 	error = put_nvlist(zc, configs);
1180789Sahrens 
1181789Sahrens 	nvlist_free(configs);
1182789Sahrens 
1183789Sahrens 	return (error);
1184789Sahrens }
1185789Sahrens 
1186789Sahrens static int
1187789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1188789Sahrens {
1189789Sahrens 	nvlist_t *config;
1190789Sahrens 	int error;
11911544Seschrock 	int ret = 0;
1192789Sahrens 
11932676Seschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
11942676Seschrock 	    sizeof (zc->zc_value));
1195789Sahrens 
1196789Sahrens 	if (config != NULL) {
11972676Seschrock 		ret = put_nvlist(zc, config);
1198789Sahrens 		nvlist_free(config);
11991544Seschrock 
12001544Seschrock 		/*
12011544Seschrock 		 * The config may be present even if 'error' is non-zero.
12021544Seschrock 		 * In this case we return success, and preserve the real errno
12031544Seschrock 		 * in 'zc_cookie'.
12041544Seschrock 		 */
12051544Seschrock 		zc->zc_cookie = error;
1206789Sahrens 	} else {
12071544Seschrock 		ret = error;
1208789Sahrens 	}
1209789Sahrens 
12101544Seschrock 	return (ret);
1211789Sahrens }
1212789Sahrens 
1213789Sahrens /*
1214789Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1215789Sahrens  * user land knows which devices are available and overall pool health.
1216789Sahrens  */
1217789Sahrens static int
1218789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1219789Sahrens {
1220789Sahrens 	nvlist_t *tryconfig, *config;
1221789Sahrens 	int error;
1222789Sahrens 
12235094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
12249643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &tryconfig)) != 0)
1225789Sahrens 		return (error);
1226789Sahrens 
1227789Sahrens 	config = spa_tryimport(tryconfig);
1228789Sahrens 
1229789Sahrens 	nvlist_free(tryconfig);
1230789Sahrens 
1231789Sahrens 	if (config == NULL)
1232789Sahrens 		return (EINVAL);
1233789Sahrens 
12342676Seschrock 	error = put_nvlist(zc, config);
1235789Sahrens 	nvlist_free(config);
1236789Sahrens 
1237789Sahrens 	return (error);
1238789Sahrens }
1239789Sahrens 
1240*12296SLin.Ling@Sun.COM /*
1241*12296SLin.Ling@Sun.COM  * inputs:
1242*12296SLin.Ling@Sun.COM  * zc_name              name of the pool
1243*12296SLin.Ling@Sun.COM  * zc_cookie            scan func (pool_scan_func_t)
1244*12296SLin.Ling@Sun.COM  */
1245789Sahrens static int
1246*12296SLin.Ling@Sun.COM zfs_ioc_pool_scan(zfs_cmd_t *zc)
1247789Sahrens {
1248789Sahrens 	spa_t *spa;
1249789Sahrens 	int error;
1250789Sahrens 
12512926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
12522926Sek110237 		return (error);
12532926Sek110237 
1254*12296SLin.Ling@Sun.COM 	if (zc->zc_cookie == POOL_SCAN_NONE)
1255*12296SLin.Ling@Sun.COM 		error = spa_scan_stop(spa);
1256*12296SLin.Ling@Sun.COM 	else
1257*12296SLin.Ling@Sun.COM 		error = spa_scan(spa, zc->zc_cookie);
12582926Sek110237 
12592926Sek110237 	spa_close(spa, FTAG);
12602926Sek110237 
1261789Sahrens 	return (error);
1262789Sahrens }
1263789Sahrens 
1264789Sahrens static int
1265789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1266789Sahrens {
1267789Sahrens 	spa_t *spa;
1268789Sahrens 	int error;
1269789Sahrens 
1270789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1271789Sahrens 	if (error == 0) {
1272789Sahrens 		spa_freeze(spa);
1273789Sahrens 		spa_close(spa, FTAG);
1274789Sahrens 	}
1275789Sahrens 	return (error);
1276789Sahrens }
1277789Sahrens 
1278789Sahrens static int
12791760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
12801760Seschrock {
12811760Seschrock 	spa_t *spa;
12821760Seschrock 	int error;
12831760Seschrock 
12842926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
12852926Sek110237 		return (error);
12862926Sek110237 
12875118Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
12885118Slling 		spa_close(spa, FTAG);
12895118Slling 		return (EINVAL);
12905118Slling 	}
12915118Slling 
12925094Slling 	spa_upgrade(spa, zc->zc_cookie);
12932926Sek110237 	spa_close(spa, FTAG);
12942926Sek110237 
12952926Sek110237 	return (error);
12962926Sek110237 }
12972926Sek110237 
12982926Sek110237 static int
12992926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
13002926Sek110237 {
13012926Sek110237 	spa_t *spa;
13022926Sek110237 	char *hist_buf;
13032926Sek110237 	uint64_t size;
13042926Sek110237 	int error;
13052926Sek110237 
13062926Sek110237 	if ((size = zc->zc_history_len) == 0)
13072926Sek110237 		return (EINVAL);
13082926Sek110237 
13092926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
13102926Sek110237 		return (error);
13112926Sek110237 
13124577Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
13133863Sek110237 		spa_close(spa, FTAG);
13143863Sek110237 		return (ENOTSUP);
13153863Sek110237 	}
13163863Sek110237 
13172926Sek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
13182926Sek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
13192926Sek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
13209643SEric.Taylor@Sun.COM 		error = ddi_copyout(hist_buf,
13219643SEric.Taylor@Sun.COM 		    (void *)(uintptr_t)zc->zc_history,
13229643SEric.Taylor@Sun.COM 		    zc->zc_history_len, zc->zc_iflags);
13232926Sek110237 	}
13242926Sek110237 
13252926Sek110237 	spa_close(spa, FTAG);
13262926Sek110237 	kmem_free(hist_buf, size);
13272926Sek110237 	return (error);
13282926Sek110237 }
13292926Sek110237 
13302926Sek110237 static int
13313444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
13323444Sek110237 {
13333444Sek110237 	int error;
13343444Sek110237 
13353912Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
13363444Sek110237 		return (error);
13373444Sek110237 
13383444Sek110237 	return (0);
13393444Sek110237 }
13403444Sek110237 
134110298SMatthew.Ahrens@Sun.COM /*
134210298SMatthew.Ahrens@Sun.COM  * inputs:
134310298SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
134410298SMatthew.Ahrens@Sun.COM  * zc_obj		object to find
134510298SMatthew.Ahrens@Sun.COM  *
134610298SMatthew.Ahrens@Sun.COM  * outputs:
134710298SMatthew.Ahrens@Sun.COM  * zc_value		name of object
134810298SMatthew.Ahrens@Sun.COM  */
13493444Sek110237 static int
13503444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
13513444Sek110237 {
135210298SMatthew.Ahrens@Sun.COM 	objset_t *os;
13533444Sek110237 	int error;
13543444Sek110237 
135510298SMatthew.Ahrens@Sun.COM 	/* XXX reading from objset not owned */
135610298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
13573444Sek110237 		return (error);
135810298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
135910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
136010298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
136110298SMatthew.Ahrens@Sun.COM 	}
136210298SMatthew.Ahrens@Sun.COM 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
13633444Sek110237 	    sizeof (zc->zc_value));
136410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
13653444Sek110237 
13663444Sek110237 	return (error);
13673444Sek110237 }
13683444Sek110237 
13693444Sek110237 static int
1370789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1371789Sahrens {
1372789Sahrens 	spa_t *spa;
1373789Sahrens 	int error;
13746423Sgw25295 	nvlist_t *config, **l2cache, **spares;
13756423Sgw25295 	uint_t nl2cache = 0, nspares = 0;
1376789Sahrens 
1377789Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1378789Sahrens 	if (error != 0)
1379789Sahrens 		return (error);
1380789Sahrens 
13815450Sbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
13829643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config);
13835450Sbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
13845450Sbrendan 	    &l2cache, &nl2cache);
13855450Sbrendan 
13866423Sgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
13876423Sgw25295 	    &spares, &nspares);
13886423Sgw25295 
13893912Slling 	/*
13903912Slling 	 * A root pool with concatenated devices is not supported.
13916423Sgw25295 	 * Thus, can not add a device to a root pool.
13926423Sgw25295 	 *
13936423Sgw25295 	 * Intent log device can not be added to a rootpool because
13946423Sgw25295 	 * during mountroot, zil is replayed, a seperated log device
13956423Sgw25295 	 * can not be accessed during the mountroot time.
13966423Sgw25295 	 *
13976423Sgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
13983912Slling 	 */
139910922SJeff.Bonwick@Sun.COM 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
140011422SMark.Musante@Sun.COM 		nvlist_free(config);
14013912Slling 		spa_close(spa, FTAG);
14023912Slling 		return (EDOM);
14033912Slling 	}
14043912Slling 
14055450Sbrendan 	if (error == 0) {
1406789Sahrens 		error = spa_vdev_add(spa, config);
1407789Sahrens 		nvlist_free(config);
1408789Sahrens 	}
1409789Sahrens 	spa_close(spa, FTAG);
1410789Sahrens 	return (error);
1411789Sahrens }
1412789Sahrens 
1413*12296SLin.Ling@Sun.COM /*
1414*12296SLin.Ling@Sun.COM  * inputs:
1415*12296SLin.Ling@Sun.COM  * zc_name		name of the pool
1416*12296SLin.Ling@Sun.COM  * zc_nvlist_conf	nvlist of devices to remove
1417*12296SLin.Ling@Sun.COM  * zc_cookie		to stop the remove?
1418*12296SLin.Ling@Sun.COM  */
1419789Sahrens static int
1420789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1421789Sahrens {
14222082Seschrock 	spa_t *spa;
14232082Seschrock 	int error;
14242082Seschrock 
14252082Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
14262082Seschrock 	if (error != 0)
14272082Seschrock 		return (error);
14282082Seschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
14292082Seschrock 	spa_close(spa, FTAG);
14302082Seschrock 	return (error);
1431789Sahrens }
1432789Sahrens 
1433789Sahrens static int
14344451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1435789Sahrens {
1436789Sahrens 	spa_t *spa;
1437789Sahrens 	int error;
14384451Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1439789Sahrens 
14402926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1441789Sahrens 		return (error);
14424451Seschrock 	switch (zc->zc_cookie) {
14434451Seschrock 	case VDEV_STATE_ONLINE:
14444451Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
14454451Seschrock 		break;
14464451Seschrock 
14474451Seschrock 	case VDEV_STATE_OFFLINE:
14484451Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
14494451Seschrock 		break;
1450789Sahrens 
14514451Seschrock 	case VDEV_STATE_FAULTED:
145210817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
145310817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
145410817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
145510817SEric.Schrock@Sun.COM 
145610817SEric.Schrock@Sun.COM 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
14574451Seschrock 		break;
1458789Sahrens 
14594451Seschrock 	case VDEV_STATE_DEGRADED:
146010817SEric.Schrock@Sun.COM 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
146110817SEric.Schrock@Sun.COM 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
146210817SEric.Schrock@Sun.COM 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
146310817SEric.Schrock@Sun.COM 
146410817SEric.Schrock@Sun.COM 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
14654451Seschrock 		break;
14664451Seschrock 
14674451Seschrock 	default:
14684451Seschrock 		error = EINVAL;
14694451Seschrock 	}
14704451Seschrock 	zc->zc_cookie = newstate;
1471789Sahrens 	spa_close(spa, FTAG);
1472789Sahrens 	return (error);
1473789Sahrens }
1474789Sahrens 
1475789Sahrens static int
1476789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1477789Sahrens {
1478789Sahrens 	spa_t *spa;
1479789Sahrens 	int replacing = zc->zc_cookie;
1480789Sahrens 	nvlist_t *config;
1481789Sahrens 	int error;
1482789Sahrens 
14832926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1484789Sahrens 		return (error);
1485789Sahrens 
14865094Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
14879643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &config)) == 0) {
14881544Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1489789Sahrens 		nvlist_free(config);
1490789Sahrens 	}
1491789Sahrens 
1492789Sahrens 	spa_close(spa, FTAG);
1493789Sahrens 	return (error);
1494789Sahrens }
1495789Sahrens 
1496789Sahrens static int
1497789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1498789Sahrens {
1499789Sahrens 	spa_t *spa;
1500789Sahrens 	int error;
1501789Sahrens 
15022926Sek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1503789Sahrens 		return (error);
1504789Sahrens 
15058241SJeff.Bonwick@Sun.COM 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1506789Sahrens 
1507789Sahrens 	spa_close(spa, FTAG);
1508789Sahrens 	return (error);
1509789Sahrens }
1510789Sahrens 
1511789Sahrens static int
151211422SMark.Musante@Sun.COM zfs_ioc_vdev_split(zfs_cmd_t *zc)
151311422SMark.Musante@Sun.COM {
151411422SMark.Musante@Sun.COM 	spa_t *spa;
151511422SMark.Musante@Sun.COM 	nvlist_t *config, *props = NULL;
151611422SMark.Musante@Sun.COM 	int error;
151711422SMark.Musante@Sun.COM 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
151811422SMark.Musante@Sun.COM 
151911422SMark.Musante@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
152011422SMark.Musante@Sun.COM 		return (error);
152111422SMark.Musante@Sun.COM 
152211422SMark.Musante@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
152311422SMark.Musante@Sun.COM 	    zc->zc_iflags, &config)) {
152411422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
152511422SMark.Musante@Sun.COM 		return (error);
152611422SMark.Musante@Sun.COM 	}
152711422SMark.Musante@Sun.COM 
152811422SMark.Musante@Sun.COM 	if (zc->zc_nvlist_src_size != 0 && (error =
152911422SMark.Musante@Sun.COM 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
153011422SMark.Musante@Sun.COM 	    zc->zc_iflags, &props))) {
153111422SMark.Musante@Sun.COM 		spa_close(spa, FTAG);
153211422SMark.Musante@Sun.COM 		nvlist_free(config);
153311422SMark.Musante@Sun.COM 		return (error);
153411422SMark.Musante@Sun.COM 	}
153511422SMark.Musante@Sun.COM 
153611422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
153711422SMark.Musante@Sun.COM 
153811422SMark.Musante@Sun.COM 	spa_close(spa, FTAG);
153911422SMark.Musante@Sun.COM 
154011422SMark.Musante@Sun.COM 	nvlist_free(config);
154111422SMark.Musante@Sun.COM 	nvlist_free(props);
154211422SMark.Musante@Sun.COM 
154311422SMark.Musante@Sun.COM 	return (error);
154411422SMark.Musante@Sun.COM }
154511422SMark.Musante@Sun.COM 
154611422SMark.Musante@Sun.COM static int
15471354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
15481354Seschrock {
15491354Seschrock 	spa_t *spa;
15502676Seschrock 	char *path = zc->zc_value;
15511544Seschrock 	uint64_t guid = zc->zc_guid;
15521354Seschrock 	int error;
15531354Seschrock 
15541354Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
15551354Seschrock 	if (error != 0)
15561354Seschrock 		return (error);
15571354Seschrock 
15581354Seschrock 	error = spa_vdev_setpath(spa, guid, path);
15591354Seschrock 	spa_close(spa, FTAG);
15601354Seschrock 	return (error);
15611354Seschrock }
15621354Seschrock 
15639425SEric.Schrock@Sun.COM static int
15649425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
15659425SEric.Schrock@Sun.COM {
15669425SEric.Schrock@Sun.COM 	spa_t *spa;
15679425SEric.Schrock@Sun.COM 	char *fru = zc->zc_value;
15689425SEric.Schrock@Sun.COM 	uint64_t guid = zc->zc_guid;
15699425SEric.Schrock@Sun.COM 	int error;
15709425SEric.Schrock@Sun.COM 
15719425SEric.Schrock@Sun.COM 	error = spa_open(zc->zc_name, &spa, FTAG);
15729425SEric.Schrock@Sun.COM 	if (error != 0)
15739425SEric.Schrock@Sun.COM 		return (error);
15749425SEric.Schrock@Sun.COM 
15759425SEric.Schrock@Sun.COM 	error = spa_vdev_setfru(spa, guid, fru);
15769425SEric.Schrock@Sun.COM 	spa_close(spa, FTAG);
15779425SEric.Schrock@Sun.COM 	return (error);
15789425SEric.Schrock@Sun.COM }
15799425SEric.Schrock@Sun.COM 
15805367Sahrens /*
15815367Sahrens  * inputs:
15825367Sahrens  * zc_name		name of filesystem
15835367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15845367Sahrens  *
15855367Sahrens  * outputs:
15865367Sahrens  * zc_objset_stats	stats
15875367Sahrens  * zc_nvlist_dst	property nvlist
15885367Sahrens  * zc_nvlist_dst_size	size of property nvlist
15895367Sahrens  */
15901354Seschrock static int
1591789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1592789Sahrens {
1593789Sahrens 	objset_t *os = NULL;
1594789Sahrens 	int error;
15951356Seschrock 	nvlist_t *nv;
1596789Sahrens 
159710298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1598789Sahrens 		return (error);
1599789Sahrens 
16002885Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1601789Sahrens 
16022856Snd150628 	if (zc->zc_nvlist_dst != 0 &&
160311022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
16042885Sahrens 		dmu_objset_stats(os, nv);
16053087Sahrens 		/*
16065147Srm160521 		 * NB: zvol_get_stats() will read the objset contents,
16073087Sahrens 		 * which we aren't supposed to do with a
16086689Smaybee 		 * DS_MODE_USER hold, because it could be
16093087Sahrens 		 * inconsistent.  So this is a bit of a workaround...
161010298SMatthew.Ahrens@Sun.COM 		 * XXX reading with out owning
16113087Sahrens 		 */
16124577Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
16134577Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
16144577Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
16154577Sahrens 		}
16162676Seschrock 		error = put_nvlist(zc, nv);
16171356Seschrock 		nvlist_free(nv);
16181356Seschrock 	}
1619789Sahrens 
162010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1621789Sahrens 	return (error);
1622789Sahrens }
1623789Sahrens 
162411022STom.Erickson@Sun.COM /*
162511022STom.Erickson@Sun.COM  * inputs:
162611022STom.Erickson@Sun.COM  * zc_name		name of filesystem
162711022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of buffer for property nvlist
162811022STom.Erickson@Sun.COM  *
162911022STom.Erickson@Sun.COM  * outputs:
163011022STom.Erickson@Sun.COM  * zc_nvlist_dst	received property nvlist
163111022STom.Erickson@Sun.COM  * zc_nvlist_dst_size	size of received property nvlist
163211022STom.Erickson@Sun.COM  *
163311022STom.Erickson@Sun.COM  * Gets received properties (distinct from local properties on or after
163411022STom.Erickson@Sun.COM  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
163511022STom.Erickson@Sun.COM  * local property values.
163611022STom.Erickson@Sun.COM  */
163711022STom.Erickson@Sun.COM static int
163811022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
163911022STom.Erickson@Sun.COM {
164011022STom.Erickson@Sun.COM 	objset_t *os = NULL;
164111022STom.Erickson@Sun.COM 	int error;
164211022STom.Erickson@Sun.COM 	nvlist_t *nv;
164311022STom.Erickson@Sun.COM 
164411022STom.Erickson@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
164511022STom.Erickson@Sun.COM 		return (error);
164611022STom.Erickson@Sun.COM 
164711022STom.Erickson@Sun.COM 	/*
164811022STom.Erickson@Sun.COM 	 * Without this check, we would return local property values if the
164911022STom.Erickson@Sun.COM 	 * caller has not already received properties on or after
165011022STom.Erickson@Sun.COM 	 * SPA_VERSION_RECVD_PROPS.
165111022STom.Erickson@Sun.COM 	 */
165211022STom.Erickson@Sun.COM 	if (!dsl_prop_get_hasrecvd(os)) {
165311022STom.Erickson@Sun.COM 		dmu_objset_rele(os, FTAG);
165411022STom.Erickson@Sun.COM 		return (ENOTSUP);
165511022STom.Erickson@Sun.COM 	}
165611022STom.Erickson@Sun.COM 
165711022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != 0 &&
165811022STom.Erickson@Sun.COM 	    (error = dsl_prop_get_received(os, &nv)) == 0) {
165911022STom.Erickson@Sun.COM 		error = put_nvlist(zc, nv);
166011022STom.Erickson@Sun.COM 		nvlist_free(nv);
166111022STom.Erickson@Sun.COM 	}
166211022STom.Erickson@Sun.COM 
166311022STom.Erickson@Sun.COM 	dmu_objset_rele(os, FTAG);
166411022STom.Erickson@Sun.COM 	return (error);
166511022STom.Erickson@Sun.COM }
166611022STom.Erickson@Sun.COM 
16675498Stimh static int
16685498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
16695498Stimh {
16705498Stimh 	uint64_t value;
16715498Stimh 	int error;
16725498Stimh 
16735498Stimh 	/*
16745498Stimh 	 * zfs_get_zplprop() will either find a value or give us
16755498Stimh 	 * the default value (if there is one).
16765498Stimh 	 */
16775498Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
16785498Stimh 		return (error);
16795498Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
16805498Stimh 	return (0);
16815498Stimh }
16825498Stimh 
16835498Stimh /*
16845498Stimh  * inputs:
16855498Stimh  * zc_name		name of filesystem
16865498Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
16875498Stimh  *
16885498Stimh  * outputs:
16895498Stimh  * zc_nvlist_dst	zpl property nvlist
16905498Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
16915498Stimh  */
16925498Stimh static int
16935498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
16945498Stimh {
16955498Stimh 	objset_t *os;
16965498Stimh 	int err;
16975498Stimh 
169810298SMatthew.Ahrens@Sun.COM 	/* XXX reading without owning */
169910298SMatthew.Ahrens@Sun.COM 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
17005498Stimh 		return (err);
17015498Stimh 
17025498Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
17035498Stimh 
17045498Stimh 	/*
17055498Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
17066689Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
17076689Smaybee 	 * hold, because it could be inconsistent.
17085498Stimh 	 */
17095498Stimh 	if (zc->zc_nvlist_dst != NULL &&
17105498Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
17115498Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
17125498Stimh 		nvlist_t *nv;
17135498Stimh 
17145498Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17155498Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
17165498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
17175498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
17185498Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
17195498Stimh 			err = put_nvlist(zc, nv);
17205498Stimh 		nvlist_free(nv);
17215498Stimh 	} else {
17225498Stimh 		err = ENOENT;
17235498Stimh 	}
172410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
17255498Stimh 	return (err);
17265498Stimh }
17275498Stimh 
17289396SMatthew.Ahrens@Sun.COM static boolean_t
17299396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name)
17309396SMatthew.Ahrens@Sun.COM {
17319396SMatthew.Ahrens@Sun.COM 	/*
17329396SMatthew.Ahrens@Sun.COM 	 * Skip over datasets that are not visible in this zone,
17339396SMatthew.Ahrens@Sun.COM 	 * internal datasets (which have a $ in their name), and
17349396SMatthew.Ahrens@Sun.COM 	 * temporary datasets (which have a % in their name).
17359396SMatthew.Ahrens@Sun.COM 	 */
17369396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '$') != NULL)
17379396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17389396SMatthew.Ahrens@Sun.COM 	if (strchr(name, '%') != NULL)
17399396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17409396SMatthew.Ahrens@Sun.COM 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
17419396SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
17429396SMatthew.Ahrens@Sun.COM 	return (B_FALSE);
17439396SMatthew.Ahrens@Sun.COM }
17449396SMatthew.Ahrens@Sun.COM 
17455367Sahrens /*
17465367Sahrens  * inputs:
17475367Sahrens  * zc_name		name of filesystem
17485367Sahrens  * zc_cookie		zap cursor
17495367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
17505367Sahrens  *
17515367Sahrens  * outputs:
17525367Sahrens  * zc_name		name of next filesystem
17539396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
17545367Sahrens  * zc_objset_stats	stats
17555367Sahrens  * zc_nvlist_dst	property nvlist
17565367Sahrens  * zc_nvlist_dst_size	size of property nvlist
17575367Sahrens  */
1758789Sahrens static int
1759789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1760789Sahrens {
1761885Sahrens 	objset_t *os;
1762789Sahrens 	int error;
1763789Sahrens 	char *p;
176411546SChris.Kirby@sun.com 	size_t orig_len = strlen(zc->zc_name);
176511546SChris.Kirby@sun.com 
176611546SChris.Kirby@sun.com top:
176710298SMatthew.Ahrens@Sun.COM 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1768885Sahrens 		if (error == ENOENT)
1769885Sahrens 			error = ESRCH;
1770885Sahrens 		return (error);
1771789Sahrens 	}
1772789Sahrens 
1773789Sahrens 	p = strrchr(zc->zc_name, '/');
1774789Sahrens 	if (p == NULL || p[1] != '\0')
1775789Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1776789Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1777789Sahrens 
17788697SRichard.Morris@Sun.COM 	/*
17798697SRichard.Morris@Sun.COM 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
17808697SRichard.Morris@Sun.COM 	 * but is not declared void because its called by dmu_objset_find().
17818697SRichard.Morris@Sun.COM 	 */
17828415SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0) {
17838415SRichard.Morris@Sun.COM 		uint64_t cookie = 0;
17848415SRichard.Morris@Sun.COM 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
17858415SRichard.Morris@Sun.COM 
17868415SRichard.Morris@Sun.COM 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
17878697SRichard.Morris@Sun.COM 			(void) dmu_objset_prefetch(p, NULL);
17888415SRichard.Morris@Sun.COM 	}
17898415SRichard.Morris@Sun.COM 
1790789Sahrens 	do {
1791885Sahrens 		error = dmu_dir_list_next(os,
1792885Sahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1793885Sahrens 		    NULL, &zc->zc_cookie);
1794789Sahrens 		if (error == ENOENT)
1795789Sahrens 			error = ESRCH;
179610588SEric.Taylor@Sun.COM 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
179710588SEric.Taylor@Sun.COM 	    !(zc->zc_iflags & FKIOCTL));
179810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
1799789Sahrens 
180010588SEric.Taylor@Sun.COM 	/*
180110588SEric.Taylor@Sun.COM 	 * If it's an internal dataset (ie. with a '$' in its name),
180210588SEric.Taylor@Sun.COM 	 * don't try to get stats for it, otherwise we'll return ENOENT.
180310588SEric.Taylor@Sun.COM 	 */
180411546SChris.Kirby@sun.com 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1805885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
180611546SChris.Kirby@sun.com 		if (error == ENOENT) {
180711546SChris.Kirby@sun.com 			/* We lost a race with destroy, get the next one. */
180811546SChris.Kirby@sun.com 			zc->zc_name[orig_len] = '\0';
180911546SChris.Kirby@sun.com 			goto top;
181011546SChris.Kirby@sun.com 		}
181111546SChris.Kirby@sun.com 	}
1812789Sahrens 	return (error);
1813789Sahrens }
1814789Sahrens 
18155367Sahrens /*
18165367Sahrens  * inputs:
18175367Sahrens  * zc_name		name of filesystem
18185367Sahrens  * zc_cookie		zap cursor
18195367Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
18205367Sahrens  *
18215367Sahrens  * outputs:
18225367Sahrens  * zc_name		name of next snapshot
18235367Sahrens  * zc_objset_stats	stats
18245367Sahrens  * zc_nvlist_dst	property nvlist
18255367Sahrens  * zc_nvlist_dst_size	size of property nvlist
18265367Sahrens  */
1827789Sahrens static int
1828789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1829789Sahrens {
1830885Sahrens 	objset_t *os;
1831789Sahrens 	int error;
1832789Sahrens 
183311546SChris.Kirby@sun.com top:
183410474SRichard.Morris@Sun.COM 	if (zc->zc_cookie == 0)
183510474SRichard.Morris@Sun.COM 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
183610474SRichard.Morris@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS);
183710474SRichard.Morris@Sun.COM 
183810298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
18396689Smaybee 	if (error)
18406689Smaybee 		return (error == ENOENT ? ESRCH : error);
1841789Sahrens 
18421003Slling 	/*
18431003Slling 	 * A dataset name of maximum length cannot have any snapshots,
18441003Slling 	 * so exit immediately.
18451003Slling 	 */
18461003Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
184710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
18481003Slling 		return (ESRCH);
1849789Sahrens 	}
1850789Sahrens 
1851885Sahrens 	error = dmu_snapshot_list_next(os,
1852885Sahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
18535663Sck153898 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
185410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
185511546SChris.Kirby@sun.com 	if (error == 0) {
1856885Sahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
185711546SChris.Kirby@sun.com 		if (error == ENOENT)  {
185811546SChris.Kirby@sun.com 			/* We lost a race with destroy, get the next one. */
185911546SChris.Kirby@sun.com 			*strchr(zc->zc_name, '@') = '\0';
186011546SChris.Kirby@sun.com 			goto top;
186111546SChris.Kirby@sun.com 		}
186211546SChris.Kirby@sun.com 	} else if (error == ENOENT) {
18636689Smaybee 		error = ESRCH;
186411546SChris.Kirby@sun.com 	}
1865789Sahrens 
18665367Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
18676689Smaybee 	if (error)
18685367Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1869789Sahrens 	return (error);
1870789Sahrens }
1871789Sahrens 
187211022STom.Erickson@Sun.COM static int
187311022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
187411022STom.Erickson@Sun.COM {
187511022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
187611022STom.Erickson@Sun.COM 	uint64_t *valary;
187711022STom.Erickson@Sun.COM 	unsigned int vallen;
187811022STom.Erickson@Sun.COM 	const char *domain;
187911933STim.Haley@Sun.COM 	char *dash;
188011022STom.Erickson@Sun.COM 	zfs_userquota_prop_t type;
188111022STom.Erickson@Sun.COM 	uint64_t rid;
188211022STom.Erickson@Sun.COM 	uint64_t quota;
188311022STom.Erickson@Sun.COM 	zfsvfs_t *zfsvfs;
188411022STom.Erickson@Sun.COM 	int err;
188511022STom.Erickson@Sun.COM 
188611022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
188711022STom.Erickson@Sun.COM 		nvlist_t *attrs;
188811022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
188911933STim.Haley@Sun.COM 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
189011933STim.Haley@Sun.COM 		    &pair) != 0)
189111933STim.Haley@Sun.COM 			return (EINVAL);
189211022STom.Erickson@Sun.COM 	}
189311022STom.Erickson@Sun.COM 
189411933STim.Haley@Sun.COM 	/*
189511933STim.Haley@Sun.COM 	 * A correctly constructed propname is encoded as
189611933STim.Haley@Sun.COM 	 * userquota@<rid>-<domain>.
189711933STim.Haley@Sun.COM 	 */
189811933STim.Haley@Sun.COM 	if ((dash = strchr(propname, '-')) == NULL ||
189911933STim.Haley@Sun.COM 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
190011933STim.Haley@Sun.COM 	    vallen != 3)
190111933STim.Haley@Sun.COM 		return (EINVAL);
190211933STim.Haley@Sun.COM 
190311933STim.Haley@Sun.COM 	domain = dash + 1;
190411022STom.Erickson@Sun.COM 	type = valary[0];
190511022STom.Erickson@Sun.COM 	rid = valary[1];
190611022STom.Erickson@Sun.COM 	quota = valary[2];
190711022STom.Erickson@Sun.COM 
190811022STom.Erickson@Sun.COM 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs);
190911022STom.Erickson@Sun.COM 	if (err == 0) {
191011022STom.Erickson@Sun.COM 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
191111022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
191211022STom.Erickson@Sun.COM 	}
191311022STom.Erickson@Sun.COM 
191411022STom.Erickson@Sun.COM 	return (err);
191511022STom.Erickson@Sun.COM }
191611022STom.Erickson@Sun.COM 
191711022STom.Erickson@Sun.COM /*
191811022STom.Erickson@Sun.COM  * If the named property is one that has a special function to set its value,
191911022STom.Erickson@Sun.COM  * return 0 on success and a positive error code on failure; otherwise if it is
192011022STom.Erickson@Sun.COM  * not one of the special properties handled by this function, return -1.
192111022STom.Erickson@Sun.COM  *
192211933STim.Haley@Sun.COM  * XXX: It would be better for callers of the property interface if we handled
192311022STom.Erickson@Sun.COM  * these special cases in dsl_prop.c (in the dsl layer).
192411022STom.Erickson@Sun.COM  */
192511022STom.Erickson@Sun.COM static int
192611022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source,
192711022STom.Erickson@Sun.COM     nvpair_t *pair)
1928789Sahrens {
192911022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
193011022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
193111022STom.Erickson@Sun.COM 	uint64_t intval;
193211022STom.Erickson@Sun.COM 	int err;
193311022STom.Erickson@Sun.COM 
193411022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
193511022STom.Erickson@Sun.COM 		if (zfs_prop_userquota(propname))
193611022STom.Erickson@Sun.COM 			return (zfs_prop_set_userquota(dsname, pair));
193711022STom.Erickson@Sun.COM 		return (-1);
193811022STom.Erickson@Sun.COM 	}
193911022STom.Erickson@Sun.COM 
194011022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
194111022STom.Erickson@Sun.COM 		nvlist_t *attrs;
194211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
194311022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
194411022STom.Erickson@Sun.COM 		    &pair) == 0);
194511022STom.Erickson@Sun.COM 	}
194611022STom.Erickson@Sun.COM 
194711022STom.Erickson@Sun.COM 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
194811022STom.Erickson@Sun.COM 		return (-1);
194911022STom.Erickson@Sun.COM 
195011022STom.Erickson@Sun.COM 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
195111022STom.Erickson@Sun.COM 
195211022STom.Erickson@Sun.COM 	switch (prop) {
195311022STom.Erickson@Sun.COM 	case ZFS_PROP_QUOTA:
195411022STom.Erickson@Sun.COM 		err = dsl_dir_set_quota(dsname, source, intval);
195511022STom.Erickson@Sun.COM 		break;
195611022STom.Erickson@Sun.COM 	case ZFS_PROP_REFQUOTA:
195711022STom.Erickson@Sun.COM 		err = dsl_dataset_set_quota(dsname, source, intval);
195811022STom.Erickson@Sun.COM 		break;
195911022STom.Erickson@Sun.COM 	case ZFS_PROP_RESERVATION:
196011022STom.Erickson@Sun.COM 		err = dsl_dir_set_reservation(dsname, source, intval);
196111022STom.Erickson@Sun.COM 		break;
196211022STom.Erickson@Sun.COM 	case ZFS_PROP_REFRESERVATION:
196311022STom.Erickson@Sun.COM 		err = dsl_dataset_set_reservation(dsname, source, intval);
196411022STom.Erickson@Sun.COM 		break;
196511022STom.Erickson@Sun.COM 	case ZFS_PROP_VOLSIZE:
196611022STom.Erickson@Sun.COM 		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
196711022STom.Erickson@Sun.COM 		    intval);
196811022STom.Erickson@Sun.COM 		break;
196911022STom.Erickson@Sun.COM 	case ZFS_PROP_VERSION:
197011022STom.Erickson@Sun.COM 	{
197111022STom.Erickson@Sun.COM 		zfsvfs_t *zfsvfs;
197211022STom.Erickson@Sun.COM 
197311022STom.Erickson@Sun.COM 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0)
197411022STom.Erickson@Sun.COM 			break;
197511022STom.Erickson@Sun.COM 
197611022STom.Erickson@Sun.COM 		err = zfs_set_version(zfsvfs, intval);
197711022STom.Erickson@Sun.COM 		zfsvfs_rele(zfsvfs, FTAG);
197811022STom.Erickson@Sun.COM 
197911022STom.Erickson@Sun.COM 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
198011147SGeorge.Wilson@Sun.COM 			zfs_cmd_t *zc;
198111147SGeorge.Wilson@Sun.COM 
198211147SGeorge.Wilson@Sun.COM 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
198311147SGeorge.Wilson@Sun.COM 			(void) strcpy(zc->zc_name, dsname);
198411147SGeorge.Wilson@Sun.COM 			(void) zfs_ioc_userspace_upgrade(zc);
198511147SGeorge.Wilson@Sun.COM 			kmem_free(zc, sizeof (zfs_cmd_t));
198611022STom.Erickson@Sun.COM 		}
198711022STom.Erickson@Sun.COM 		break;
198811022STom.Erickson@Sun.COM 	}
198911022STom.Erickson@Sun.COM 
199011022STom.Erickson@Sun.COM 	default:
199111022STom.Erickson@Sun.COM 		err = -1;
199211022STom.Erickson@Sun.COM 	}
199311022STom.Erickson@Sun.COM 
199411022STom.Erickson@Sun.COM 	return (err);
199511022STom.Erickson@Sun.COM }
199611022STom.Erickson@Sun.COM 
199711022STom.Erickson@Sun.COM /*
199811022STom.Erickson@Sun.COM  * This function is best effort. If it fails to set any of the given properties,
199911022STom.Erickson@Sun.COM  * it continues to set as many as it can and returns the first error
200011022STom.Erickson@Sun.COM  * encountered. If the caller provides a non-NULL errlist, it also gives the
200111022STom.Erickson@Sun.COM  * complete list of names of all the properties it failed to set along with the
200211022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
200311022STom.Erickson@Sun.COM  * returned errlist.
200411022STom.Erickson@Sun.COM  *
200511022STom.Erickson@Sun.COM  * If every property is set successfully, zero is returned and the list pointed
200611022STom.Erickson@Sun.COM  * at by errlist is NULL.
200711022STom.Erickson@Sun.COM  */
200811022STom.Erickson@Sun.COM int
200911022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
201011022STom.Erickson@Sun.COM     nvlist_t **errlist)
201111022STom.Erickson@Sun.COM {
201211022STom.Erickson@Sun.COM 	nvpair_t *pair;
201311022STom.Erickson@Sun.COM 	nvpair_t *propval;
201411045STom.Erickson@Sun.COM 	int rv = 0;
20152676Seschrock 	uint64_t intval;
20162676Seschrock 	char *strval;
20178697SRichard.Morris@Sun.COM 	nvlist_t *genericnvl;
201811022STom.Erickson@Sun.COM 	nvlist_t *errors;
201911022STom.Erickson@Sun.COM 	nvlist_t *retrynvl;
20204543Smarks 
20218697SRichard.Morris@Sun.COM 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
202211022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
202311022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
202411022STom.Erickson@Sun.COM 
202511022STom.Erickson@Sun.COM retry:
202611022STom.Erickson@Sun.COM 	pair = NULL;
202711022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
202811022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
20294670Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
203011181STom.Erickson@Sun.COM 		int err = 0;
20314543Smarks 
203211022STom.Erickson@Sun.COM 		/* decode the property value */
203311022STom.Erickson@Sun.COM 		propval = pair;
203411022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
203511022STom.Erickson@Sun.COM 			nvlist_t *attrs;
203611022STom.Erickson@Sun.COM 			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
203711933STim.Haley@Sun.COM 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
203811933STim.Haley@Sun.COM 			    &propval) != 0)
203911933STim.Haley@Sun.COM 				err = EINVAL;
20404543Smarks 		}
20412676Seschrock 
204211022STom.Erickson@Sun.COM 		/* Validate value type */
204311933STim.Haley@Sun.COM 		if (err == 0 && prop == ZPROP_INVAL) {
204411022STom.Erickson@Sun.COM 			if (zfs_prop_user(propname)) {
204511022STom.Erickson@Sun.COM 				if (nvpair_type(propval) != DATA_TYPE_STRING)
204611022STom.Erickson@Sun.COM 					err = EINVAL;
204711022STom.Erickson@Sun.COM 			} else if (zfs_prop_userquota(propname)) {
204811022STom.Erickson@Sun.COM 				if (nvpair_type(propval) !=
204911022STom.Erickson@Sun.COM 				    DATA_TYPE_UINT64_ARRAY)
205011022STom.Erickson@Sun.COM 					err = EINVAL;
20519396SMatthew.Ahrens@Sun.COM 			}
205211933STim.Haley@Sun.COM 		} else if (err == 0) {
205311022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
205411022STom.Erickson@Sun.COM 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
205511022STom.Erickson@Sun.COM 					err = EINVAL;
205611022STom.Erickson@Sun.COM 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
20572885Sahrens 				const char *unused;
20582885Sahrens 
205911022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
206011022STom.Erickson@Sun.COM 				    &intval) == 0);
20612676Seschrock 
20622676Seschrock 				switch (zfs_prop_get_type(prop)) {
20634787Sahrens 				case PROP_TYPE_NUMBER:
20642676Seschrock 					break;
20654787Sahrens 				case PROP_TYPE_STRING:
206611022STom.Erickson@Sun.COM 					err = EINVAL;
206711022STom.Erickson@Sun.COM 					break;
20684787Sahrens 				case PROP_TYPE_INDEX:
20692717Seschrock 					if (zfs_prop_index_to_string(prop,
207011022STom.Erickson@Sun.COM 					    intval, &unused) != 0)
207111022STom.Erickson@Sun.COM 						err = EINVAL;
20722676Seschrock 					break;
20732676Seschrock 				default:
20744577Sahrens 					cmn_err(CE_PANIC,
20754577Sahrens 					    "unknown property type");
20762676Seschrock 				}
20772676Seschrock 			} else {
207811022STom.Erickson@Sun.COM 				err = EINVAL;
207911022STom.Erickson@Sun.COM 			}
208011022STom.Erickson@Sun.COM 		}
208111022STom.Erickson@Sun.COM 
208211022STom.Erickson@Sun.COM 		/* Validate permissions */
208311022STom.Erickson@Sun.COM 		if (err == 0)
208411022STom.Erickson@Sun.COM 			err = zfs_check_settable(dsname, pair, CRED());
208511022STom.Erickson@Sun.COM 
208611022STom.Erickson@Sun.COM 		if (err == 0) {
208711022STom.Erickson@Sun.COM 			err = zfs_prop_set_special(dsname, source, pair);
208811022STom.Erickson@Sun.COM 			if (err == -1) {
208911022STom.Erickson@Sun.COM 				/*
209011022STom.Erickson@Sun.COM 				 * For better performance we build up a list of
209111022STom.Erickson@Sun.COM 				 * properties to set in a single transaction.
209211022STom.Erickson@Sun.COM 				 */
209311022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(genericnvl, pair);
209411022STom.Erickson@Sun.COM 			} else if (err != 0 && nvl != retrynvl) {
209511022STom.Erickson@Sun.COM 				/*
209611022STom.Erickson@Sun.COM 				 * This may be a spurious error caused by
209711022STom.Erickson@Sun.COM 				 * receiving quota and reservation out of order.
209811022STom.Erickson@Sun.COM 				 * Try again in a second pass.
209911022STom.Erickson@Sun.COM 				 */
210011022STom.Erickson@Sun.COM 				err = nvlist_add_nvpair(retrynvl, pair);
21012676Seschrock 			}
210211022STom.Erickson@Sun.COM 		}
210311022STom.Erickson@Sun.COM 
210411022STom.Erickson@Sun.COM 		if (err != 0)
210511022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
210611022STom.Erickson@Sun.COM 	}
210711022STom.Erickson@Sun.COM 
210811022STom.Erickson@Sun.COM 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
210911022STom.Erickson@Sun.COM 		nvl = retrynvl;
211011022STom.Erickson@Sun.COM 		goto retry;
211111022STom.Erickson@Sun.COM 	}
211211022STom.Erickson@Sun.COM 
211311022STom.Erickson@Sun.COM 	if (!nvlist_empty(genericnvl) &&
211411022STom.Erickson@Sun.COM 	    dsl_props_set(dsname, source, genericnvl) != 0) {
211511022STom.Erickson@Sun.COM 		/*
211611022STom.Erickson@Sun.COM 		 * If this fails, we still want to set as many properties as we
211711022STom.Erickson@Sun.COM 		 * can, so try setting them individually.
211811022STom.Erickson@Sun.COM 		 */
211911022STom.Erickson@Sun.COM 		pair = NULL;
212011022STom.Erickson@Sun.COM 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
212111022STom.Erickson@Sun.COM 			const char *propname = nvpair_name(pair);
212211181STom.Erickson@Sun.COM 			int err = 0;
212311022STom.Erickson@Sun.COM 
212411022STom.Erickson@Sun.COM 			propval = pair;
212511022STom.Erickson@Sun.COM 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
212611022STom.Erickson@Sun.COM 				nvlist_t *attrs;
212711022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
212811022STom.Erickson@Sun.COM 				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
212911022STom.Erickson@Sun.COM 				    &propval) == 0);
213011022STom.Erickson@Sun.COM 			}
213111022STom.Erickson@Sun.COM 
213211022STom.Erickson@Sun.COM 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
213311022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_string(propval,
213411022STom.Erickson@Sun.COM 				    &strval) == 0);
213511022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 1,
213611022STom.Erickson@Sun.COM 				    strlen(strval) + 1, strval);
213711022STom.Erickson@Sun.COM 			} else {
213811022STom.Erickson@Sun.COM 				VERIFY(nvpair_value_uint64(propval,
213911022STom.Erickson@Sun.COM 				    &intval) == 0);
214011022STom.Erickson@Sun.COM 				err = dsl_prop_set(dsname, propname, source, 8,
214111022STom.Erickson@Sun.COM 				    1, &intval);
214211022STom.Erickson@Sun.COM 			}
214311022STom.Erickson@Sun.COM 
214411022STom.Erickson@Sun.COM 			if (err != 0) {
214511022STom.Erickson@Sun.COM 				VERIFY(nvlist_add_int32(errors, propname,
214611022STom.Erickson@Sun.COM 				    err) == 0);
214711022STom.Erickson@Sun.COM 			}
21482676Seschrock 		}
21492676Seschrock 	}
215011022STom.Erickson@Sun.COM 	nvlist_free(genericnvl);
215111022STom.Erickson@Sun.COM 	nvlist_free(retrynvl);
215211022STom.Erickson@Sun.COM 
215311022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
215411022STom.Erickson@Sun.COM 		nvlist_free(errors);
215511022STom.Erickson@Sun.COM 		errors = NULL;
215611022STom.Erickson@Sun.COM 	} else {
215711022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
21588697SRichard.Morris@Sun.COM 	}
215911022STom.Erickson@Sun.COM 
216011022STom.Erickson@Sun.COM 	if (errlist == NULL)
216111022STom.Erickson@Sun.COM 		nvlist_free(errors);
216211022STom.Erickson@Sun.COM 	else
216311022STom.Erickson@Sun.COM 		*errlist = errors;
216411022STom.Erickson@Sun.COM 
216511022STom.Erickson@Sun.COM 	return (rv);
2166789Sahrens }
2167789Sahrens 
21685367Sahrens /*
21699355SMatthew.Ahrens@Sun.COM  * Check that all the properties are valid user properties.
21709355SMatthew.Ahrens@Sun.COM  */
21719355SMatthew.Ahrens@Sun.COM static int
21729355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl)
21739355SMatthew.Ahrens@Sun.COM {
217411022STom.Erickson@Sun.COM 	nvpair_t *pair = NULL;
21759355SMatthew.Ahrens@Sun.COM 	int error = 0;
21769355SMatthew.Ahrens@Sun.COM 
217711022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
217811022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
21799355SMatthew.Ahrens@Sun.COM 		char *valstr;
21809355SMatthew.Ahrens@Sun.COM 
21819355SMatthew.Ahrens@Sun.COM 		if (!zfs_prop_user(propname) ||
218211022STom.Erickson@Sun.COM 		    nvpair_type(pair) != DATA_TYPE_STRING)
21839355SMatthew.Ahrens@Sun.COM 			return (EINVAL);
21849355SMatthew.Ahrens@Sun.COM 
21859355SMatthew.Ahrens@Sun.COM 		if (error = zfs_secpolicy_write_perms(fsname,
21869355SMatthew.Ahrens@Sun.COM 		    ZFS_DELEG_PERM_USERPROP, CRED()))
21879355SMatthew.Ahrens@Sun.COM 			return (error);
21889355SMatthew.Ahrens@Sun.COM 
21899355SMatthew.Ahrens@Sun.COM 		if (strlen(propname) >= ZAP_MAXNAMELEN)
21909355SMatthew.Ahrens@Sun.COM 			return (ENAMETOOLONG);
21919355SMatthew.Ahrens@Sun.COM 
219211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
21939355SMatthew.Ahrens@Sun.COM 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
21949355SMatthew.Ahrens@Sun.COM 			return (E2BIG);
21959355SMatthew.Ahrens@Sun.COM 	}
21969355SMatthew.Ahrens@Sun.COM 	return (0);
21979355SMatthew.Ahrens@Sun.COM }
21989355SMatthew.Ahrens@Sun.COM 
219911022STom.Erickson@Sun.COM static void
220011022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
220111022STom.Erickson@Sun.COM {
220211022STom.Erickson@Sun.COM 	nvpair_t *pair;
220311022STom.Erickson@Sun.COM 
220411022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
220511022STom.Erickson@Sun.COM 
220611022STom.Erickson@Sun.COM 	pair = NULL;
220711022STom.Erickson@Sun.COM 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
220811022STom.Erickson@Sun.COM 		if (nvlist_exists(skipped, nvpair_name(pair)))
220911022STom.Erickson@Sun.COM 			continue;
221011022STom.Erickson@Sun.COM 
221111022STom.Erickson@Sun.COM 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
221211022STom.Erickson@Sun.COM 	}
221311022STom.Erickson@Sun.COM }
221411022STom.Erickson@Sun.COM 
221511022STom.Erickson@Sun.COM static int
221611022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
221711022STom.Erickson@Sun.COM     nvlist_t *skipped)
221811022STom.Erickson@Sun.COM {
221911022STom.Erickson@Sun.COM 	int err = 0;
222011022STom.Erickson@Sun.COM 	nvlist_t *cleared_props = NULL;
222111022STom.Erickson@Sun.COM 	props_skip(props, skipped, &cleared_props);
222211022STom.Erickson@Sun.COM 	if (!nvlist_empty(cleared_props)) {
222311022STom.Erickson@Sun.COM 		/*
222411022STom.Erickson@Sun.COM 		 * Acts on local properties until the dataset has received
222511022STom.Erickson@Sun.COM 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
222611022STom.Erickson@Sun.COM 		 */
222711022STom.Erickson@Sun.COM 		zprop_source_t flags = (ZPROP_SRC_NONE |
222811022STom.Erickson@Sun.COM 		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
222911022STom.Erickson@Sun.COM 		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
223011022STom.Erickson@Sun.COM 	}
223111022STom.Erickson@Sun.COM 	nvlist_free(cleared_props);
223211022STom.Erickson@Sun.COM 	return (err);
223311022STom.Erickson@Sun.COM }
223411022STom.Erickson@Sun.COM 
22359355SMatthew.Ahrens@Sun.COM /*
22365367Sahrens  * inputs:
22375367Sahrens  * zc_name		name of filesystem
22388697SRichard.Morris@Sun.COM  * zc_value		name of property to set
22395367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
224011022STom.Erickson@Sun.COM  * zc_cookie		received properties flag
22415367Sahrens  *
224211022STom.Erickson@Sun.COM  * outputs:
224311022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
22445367Sahrens  */
2245789Sahrens static int
22462676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2247789Sahrens {
22482676Seschrock 	nvlist_t *nvl;
224911022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
225011022STom.Erickson@Sun.COM 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
225111022STom.Erickson@Sun.COM 	    ZPROP_SRC_LOCAL);
225211022STom.Erickson@Sun.COM 	nvlist_t *errors = NULL;
22532676Seschrock 	int error;
2254789Sahrens 
22555094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
22569643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvl)) != 0)
22572676Seschrock 		return (error);
22582676Seschrock 
225911022STom.Erickson@Sun.COM 	if (received) {
22607265Sahrens 		nvlist_t *origprops;
22617265Sahrens 		objset_t *os;
22627265Sahrens 
226310298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
226411022STom.Erickson@Sun.COM 			if (dsl_prop_get_received(os, &origprops) == 0) {
226511022STom.Erickson@Sun.COM 				(void) clear_received_props(os,
226611022STom.Erickson@Sun.COM 				    zc->zc_name, origprops, nvl);
22677265Sahrens 				nvlist_free(origprops);
22687265Sahrens 			}
226911022STom.Erickson@Sun.COM 
227011022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
227110298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(os, FTAG);
22727265Sahrens 		}
22737265Sahrens 	}
22747265Sahrens 
227511022STom.Erickson@Sun.COM 	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
227611022STom.Erickson@Sun.COM 
227711022STom.Erickson@Sun.COM 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
227811022STom.Erickson@Sun.COM 		(void) put_nvlist(zc, errors);
227911022STom.Erickson@Sun.COM 	}
228011022STom.Erickson@Sun.COM 
228111022STom.Erickson@Sun.COM 	nvlist_free(errors);
22822676Seschrock 	nvlist_free(nvl);
22832676Seschrock 	return (error);
2284789Sahrens }
2285789Sahrens 
22865367Sahrens /*
22875367Sahrens  * inputs:
22885367Sahrens  * zc_name		name of filesystem
22895367Sahrens  * zc_value		name of property to inherit
229011022STom.Erickson@Sun.COM  * zc_cookie		revert to received value if TRUE
22915367Sahrens  *
22925367Sahrens  * outputs:		none
22935367Sahrens  */
2294789Sahrens static int
22954849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
22964849Sahrens {
229711022STom.Erickson@Sun.COM 	const char *propname = zc->zc_value;
229811022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
229911022STom.Erickson@Sun.COM 	boolean_t received = zc->zc_cookie;
230011022STom.Erickson@Sun.COM 	zprop_source_t source = (received
230111022STom.Erickson@Sun.COM 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
230211022STom.Erickson@Sun.COM 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
230311022STom.Erickson@Sun.COM 
230411022STom.Erickson@Sun.COM 	if (received) {
230511022STom.Erickson@Sun.COM 		nvlist_t *dummy;
230611022STom.Erickson@Sun.COM 		nvpair_t *pair;
230711022STom.Erickson@Sun.COM 		zprop_type_t type;
230811022STom.Erickson@Sun.COM 		int err;
230911022STom.Erickson@Sun.COM 
231011022STom.Erickson@Sun.COM 		/*
231111022STom.Erickson@Sun.COM 		 * zfs_prop_set_special() expects properties in the form of an
231211022STom.Erickson@Sun.COM 		 * nvpair with type info.
231311022STom.Erickson@Sun.COM 		 */
231411022STom.Erickson@Sun.COM 		if (prop == ZPROP_INVAL) {
231511022STom.Erickson@Sun.COM 			if (!zfs_prop_user(propname))
231611022STom.Erickson@Sun.COM 				return (EINVAL);
231711022STom.Erickson@Sun.COM 
231811022STom.Erickson@Sun.COM 			type = PROP_TYPE_STRING;
231911515STom.Erickson@Sun.COM 		} else if (prop == ZFS_PROP_VOLSIZE ||
232011515STom.Erickson@Sun.COM 		    prop == ZFS_PROP_VERSION) {
232111515STom.Erickson@Sun.COM 			return (EINVAL);
232211022STom.Erickson@Sun.COM 		} else {
232311022STom.Erickson@Sun.COM 			type = zfs_prop_get_type(prop);
232411022STom.Erickson@Sun.COM 		}
232511022STom.Erickson@Sun.COM 
232611022STom.Erickson@Sun.COM 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
232711022STom.Erickson@Sun.COM 
232811022STom.Erickson@Sun.COM 		switch (type) {
232911022STom.Erickson@Sun.COM 		case PROP_TYPE_STRING:
233011022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
233111022STom.Erickson@Sun.COM 			break;
233211022STom.Erickson@Sun.COM 		case PROP_TYPE_NUMBER:
233311022STom.Erickson@Sun.COM 		case PROP_TYPE_INDEX:
233411022STom.Erickson@Sun.COM 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
233511022STom.Erickson@Sun.COM 			break;
233611022STom.Erickson@Sun.COM 		default:
233711022STom.Erickson@Sun.COM 			nvlist_free(dummy);
233811022STom.Erickson@Sun.COM 			return (EINVAL);
233911022STom.Erickson@Sun.COM 		}
234011022STom.Erickson@Sun.COM 
234111022STom.Erickson@Sun.COM 		pair = nvlist_next_nvpair(dummy, NULL);
234211022STom.Erickson@Sun.COM 		err = zfs_prop_set_special(zc->zc_name, source, pair);
234311022STom.Erickson@Sun.COM 		nvlist_free(dummy);
234411022STom.Erickson@Sun.COM 		if (err != -1)
234511022STom.Erickson@Sun.COM 			return (err); /* special property already handled */
234611022STom.Erickson@Sun.COM 	} else {
234711022STom.Erickson@Sun.COM 		/*
234811022STom.Erickson@Sun.COM 		 * Only check this in the non-received case. We want to allow
234911022STom.Erickson@Sun.COM 		 * 'inherit -S' to revert non-inheritable properties like quota
235011022STom.Erickson@Sun.COM 		 * and reservation to the received or default values even though
235111022STom.Erickson@Sun.COM 		 * they are not considered inheritable.
235211022STom.Erickson@Sun.COM 		 */
235311022STom.Erickson@Sun.COM 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
235411022STom.Erickson@Sun.COM 			return (EINVAL);
235511022STom.Erickson@Sun.COM 	}
235611022STom.Erickson@Sun.COM 
23574849Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
235811022STom.Erickson@Sun.COM 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
23594849Sahrens }
23604849Sahrens 
23614849Sahrens static int
23624098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
23633912Slling {
23645094Slling 	nvlist_t *props;
23653912Slling 	spa_t *spa;
23665094Slling 	int error;
236711022STom.Erickson@Sun.COM 	nvpair_t *pair;
236811022STom.Erickson@Sun.COM 
236911022STom.Erickson@Sun.COM 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
237011022STom.Erickson@Sun.COM 	    zc->zc_iflags, &props))
23713912Slling 		return (error);
23723912Slling 
23738525SEric.Schrock@Sun.COM 	/*
23748525SEric.Schrock@Sun.COM 	 * If the only property is the configfile, then just do a spa_lookup()
23758525SEric.Schrock@Sun.COM 	 * to handle the faulted case.
23768525SEric.Schrock@Sun.COM 	 */
237711022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
237811022STom.Erickson@Sun.COM 	if (pair != NULL && strcmp(nvpair_name(pair),
23798525SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
238011022STom.Erickson@Sun.COM 	    nvlist_next_nvpair(props, pair) == NULL) {
23818525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
23828525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
23838525SEric.Schrock@Sun.COM 			spa_configfile_set(spa, props, B_FALSE);
23848525SEric.Schrock@Sun.COM 			spa_config_sync(spa, B_FALSE, B_TRUE);
23858525SEric.Schrock@Sun.COM 		}
23868525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
238710672SEric.Schrock@Sun.COM 		if (spa != NULL) {
238810672SEric.Schrock@Sun.COM 			nvlist_free(props);
23898525SEric.Schrock@Sun.COM 			return (0);
239010672SEric.Schrock@Sun.COM 		}
23918525SEric.Schrock@Sun.COM 	}
23928525SEric.Schrock@Sun.COM 
23933912Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
23945094Slling 		nvlist_free(props);
23953912Slling 		return (error);
23963912Slling 	}
23973912Slling 
23985094Slling 	error = spa_prop_set(spa, props);
23993912Slling 
24005094Slling 	nvlist_free(props);
24013912Slling 	spa_close(spa, FTAG);
24023912Slling 
24033912Slling 	return (error);
24043912Slling }
24053912Slling 
24063912Slling static int
24074098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
24083912Slling {
24093912Slling 	spa_t *spa;
24103912Slling 	int error;
24113912Slling 	nvlist_t *nvp = NULL;
24123912Slling 
24138525SEric.Schrock@Sun.COM 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
24148525SEric.Schrock@Sun.COM 		/*
24158525SEric.Schrock@Sun.COM 		 * If the pool is faulted, there may be properties we can still
24168525SEric.Schrock@Sun.COM 		 * get (such as altroot and cachefile), so attempt to get them
24178525SEric.Schrock@Sun.COM 		 * anyway.
24188525SEric.Schrock@Sun.COM 		 */
24198525SEric.Schrock@Sun.COM 		mutex_enter(&spa_namespace_lock);
24208525SEric.Schrock@Sun.COM 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
24218525SEric.Schrock@Sun.COM 			error = spa_prop_get(spa, &nvp);
24228525SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
24238525SEric.Schrock@Sun.COM 	} else {
24248525SEric.Schrock@Sun.COM 		error = spa_prop_get(spa, &nvp);
24258525SEric.Schrock@Sun.COM 		spa_close(spa, FTAG);
24268525SEric.Schrock@Sun.COM 	}
24273912Slling 
24283912Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
24293912Slling 		error = put_nvlist(zc, nvp);
24303912Slling 	else
24313912Slling 		error = EFAULT;
24323912Slling 
24338525SEric.Schrock@Sun.COM 	nvlist_free(nvp);
24343912Slling 	return (error);
24353912Slling }
24363912Slling 
24375367Sahrens /*
24385367Sahrens  * inputs:
24395367Sahrens  * zc_name		name of filesystem
24405367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
24415367Sahrens  * zc_perm_action	allow/unallow flag
24425367Sahrens  *
24435367Sahrens  * outputs:		none
24445367Sahrens  */
24454543Smarks static int
24464543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
24474543Smarks {
24484543Smarks 	int error;
24494543Smarks 	nvlist_t *fsaclnv = NULL;
24504543Smarks 
24515094Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
24529643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &fsaclnv)) != 0)
24534543Smarks 		return (error);
24544543Smarks 
24554543Smarks 	/*
24564543Smarks 	 * Verify nvlist is constructed correctly
24574543Smarks 	 */
24584543Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
24594543Smarks 		nvlist_free(fsaclnv);
24604543Smarks 		return (EINVAL);
24614543Smarks 	}
24624543Smarks 
24634543Smarks 	/*
24644543Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
24654543Smarks 	 * that user is allowed to hand out each permission in
24664543Smarks 	 * the nvlist(s)
24674543Smarks 	 */
24684543Smarks 
24694787Sahrens 	error = secpolicy_zfs(CRED());
24704543Smarks 	if (error) {
24714787Sahrens 		if (zc->zc_perm_action == B_FALSE) {
24724787Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
24734787Sahrens 			    fsaclnv, CRED());
24744787Sahrens 		} else {
24754787Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
24764787Sahrens 			    fsaclnv, CRED());
24774787Sahrens 		}
24784543Smarks 	}
24794543Smarks 
24804543Smarks 	if (error == 0)
24814543Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
24824543Smarks 
24834543Smarks 	nvlist_free(fsaclnv);
24844543Smarks 	return (error);
24854543Smarks }
24864543Smarks 
24875367Sahrens /*
24885367Sahrens  * inputs:
24895367Sahrens  * zc_name		name of filesystem
24905367Sahrens  *
24915367Sahrens  * outputs:
24925367Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
24935367Sahrens  */
24944543Smarks static int
24954543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
24964543Smarks {
24974543Smarks 	nvlist_t *nvp;
24984543Smarks 	int error;
24994543Smarks 
25004543Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
25014543Smarks 		error = put_nvlist(zc, nvp);
25024543Smarks 		nvlist_free(nvp);
25034543Smarks 	}
25044543Smarks 
25054543Smarks 	return (error);
25064543Smarks }
25074543Smarks 
25085367Sahrens /*
2509789Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2510789Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2511789Sahrens  * is responsible for releasing the returned vfs pointer.
2512789Sahrens  */
2513789Sahrens static vfs_t *
2514789Sahrens zfs_get_vfs(const char *resource)
2515789Sahrens {
2516789Sahrens 	struct vfs *vfsp;
2517789Sahrens 	struct vfs *vfs_found = NULL;
2518789Sahrens 
2519789Sahrens 	vfs_list_read_lock();
2520789Sahrens 	vfsp = rootvfs;
2521789Sahrens 	do {
2522789Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2523789Sahrens 			VFS_HOLD(vfsp);
2524789Sahrens 			vfs_found = vfsp;
2525789Sahrens 			break;
2526789Sahrens 		}
2527789Sahrens 		vfsp = vfsp->vfs_next;
2528789Sahrens 	} while (vfsp != rootvfs);
2529789Sahrens 	vfs_list_unlock();
2530789Sahrens 	return (vfs_found);
2531789Sahrens }
2532789Sahrens 
25334543Smarks /* ARGSUSED */
2534789Sahrens static void
25354543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2536789Sahrens {
25375331Samw 	zfs_creat_t *zct = arg;
25385498Stimh 
25395498Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
25405331Samw }
25415331Samw 
25425498Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
25435498Stimh 
25445331Samw /*
25455498Stimh  * inputs:
25467184Stimh  * createprops		list of properties requested by creator
25477184Stimh  * default_zplver	zpl version to use if unspecified in createprops
25487184Stimh  * fuids_ok		fuids allowed in this version of the spa?
25497184Stimh  * os			parent objset pointer (NULL if root fs)
25505331Samw  *
25515498Stimh  * outputs:
25525498Stimh  * zplprops	values for the zplprops we attach to the master node object
25537184Stimh  * is_ci	true if requested file system will be purely case-insensitive
25545331Samw  *
25555498Stimh  * Determine the settings for utf8only, normalization and
25565498Stimh  * casesensitivity.  Specific values may have been requested by the
25575498Stimh  * creator and/or we can inherit values from the parent dataset.  If
25585498Stimh  * the file system is of too early a vintage, a creator can not
25595498Stimh  * request settings for these properties, even if the requested
25605498Stimh  * setting is the default value.  We don't actually want to create dsl
25615498Stimh  * properties for these, so remove them from the source nvlist after
25625498Stimh  * processing.
25635331Samw  */
25645331Samw static int
25659396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
256611935SMark.Shellenbaum@Sun.COM     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
256711935SMark.Shellenbaum@Sun.COM     nvlist_t *zplprops, boolean_t *is_ci)
25685331Samw {
25695498Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
25705498Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
25715498Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
25725498Stimh 
25735498Stimh 	ASSERT(zplprops != NULL);
25745498Stimh 
25755375Stimh 	/*
25765498Stimh 	 * Pull out creator prop choices, if any.
25775375Stimh 	 */
25785498Stimh 	if (createprops) {
25795498Stimh 		(void) nvlist_lookup_uint64(createprops,
25807184Stimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
25817184Stimh 		(void) nvlist_lookup_uint64(createprops,
25825498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
25835498Stimh 		(void) nvlist_remove_all(createprops,
25845498Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
25855498Stimh 		(void) nvlist_lookup_uint64(createprops,
25865498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
25875498Stimh 		(void) nvlist_remove_all(createprops,
25885498Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
25895498Stimh 		(void) nvlist_lookup_uint64(createprops,
25905498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
25915498Stimh 		(void) nvlist_remove_all(createprops,
25925498Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
25935331Samw 	}
25945331Samw 
25955375Stimh 	/*
25967184Stimh 	 * If the zpl version requested is whacky or the file system
25977184Stimh 	 * or pool is version is too "young" to support normalization
25987184Stimh 	 * and the creator tried to set a value for one of the props,
25997184Stimh 	 * error out.
26005498Stimh 	 */
26017184Stimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
26027184Stimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
260311935SMark.Shellenbaum@Sun.COM 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
26047184Stimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
26055498Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
26067184Stimh 	    sense != ZFS_PROP_UNDEFINED)))
26075498Stimh 		return (ENOTSUP);
26085498Stimh 
26095498Stimh 	/*
26105498Stimh 	 * Put the version in the zplprops
26115498Stimh 	 */
26125498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26135498Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
26145498Stimh 
26155498Stimh 	if (norm == ZFS_PROP_UNDEFINED)
26165498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
26175498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26185498Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
26195498Stimh 
26205498Stimh 	/*
26215498Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
26225498Stimh 	 */
26235498Stimh 	if (norm)
26245498Stimh 		u8 = 1;
26255498Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
26265498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
26275498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26285498Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
26295498Stimh 
26305498Stimh 	if (sense == ZFS_PROP_UNDEFINED)
26315498Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
26325498Stimh 	VERIFY(nvlist_add_uint64(zplprops,
26335498Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
26345498Stimh 
26356492Stimh 	if (is_ci)
26366492Stimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
26376492Stimh 
26387184Stimh 	return (0);
26397184Stimh }
26407184Stimh 
26417184Stimh static int
26427184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
26437184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
26447184Stimh {
264511935SMark.Shellenbaum@Sun.COM 	boolean_t fuids_ok, sa_ok;
26467184Stimh 	uint64_t zplver = ZPL_VERSION;
26477184Stimh 	objset_t *os = NULL;
26487184Stimh 	char parentname[MAXNAMELEN];
26497184Stimh 	char *cp;
265011935SMark.Shellenbaum@Sun.COM 	spa_t *spa;
265111935SMark.Shellenbaum@Sun.COM 	uint64_t spa_vers;
26527184Stimh 	int error;
26537184Stimh 
26547184Stimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
26557184Stimh 	cp = strrchr(parentname, '/');
26567184Stimh 	ASSERT(cp != NULL);
26577184Stimh 	cp[0] = '\0';
26587184Stimh 
265911935SMark.Shellenbaum@Sun.COM 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
266011935SMark.Shellenbaum@Sun.COM 		return (error);
266111935SMark.Shellenbaum@Sun.COM 
266211935SMark.Shellenbaum@Sun.COM 	spa_vers = spa_version(spa);
266311935SMark.Shellenbaum@Sun.COM 	spa_close(spa, FTAG);
266411935SMark.Shellenbaum@Sun.COM 
266511935SMark.Shellenbaum@Sun.COM 	zplver = zfs_zpl_version_map(spa_vers);
266611935SMark.Shellenbaum@Sun.COM 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
266711935SMark.Shellenbaum@Sun.COM 	sa_ok = (zplver >= ZPL_VERSION_SA);
26687184Stimh 
26697184Stimh 	/*
26707184Stimh 	 * Open parent object set so we can inherit zplprop values.
26717184Stimh 	 */
267210298SMatthew.Ahrens@Sun.COM 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
26737184Stimh 		return (error);
26747184Stimh 
267511935SMark.Shellenbaum@Sun.COM 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
26767184Stimh 	    zplprops, is_ci);
267710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
26787184Stimh 	return (error);
26797184Stimh }
26807184Stimh 
26817184Stimh static int
26827184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
26837184Stimh     nvlist_t *zplprops, boolean_t *is_ci)
26847184Stimh {
268511935SMark.Shellenbaum@Sun.COM 	boolean_t fuids_ok;
268611935SMark.Shellenbaum@Sun.COM 	boolean_t sa_ok;
26877184Stimh 	uint64_t zplver = ZPL_VERSION;
26887184Stimh 	int error;
26897184Stimh 
269011935SMark.Shellenbaum@Sun.COM 	zplver = zfs_zpl_version_map(spa_vers);
269111935SMark.Shellenbaum@Sun.COM 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
269211935SMark.Shellenbaum@Sun.COM 	sa_ok = (zplver >= ZPL_VERSION_SA);
269311935SMark.Shellenbaum@Sun.COM 
269411935SMark.Shellenbaum@Sun.COM 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
269511935SMark.Shellenbaum@Sun.COM 	    createprops, zplprops, is_ci);
26967184Stimh 	return (error);
2697789Sahrens }
2698789Sahrens 
26995367Sahrens /*
27005367Sahrens  * inputs:
27015367Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
27025367Sahrens  * zc_name		name of new objset
27035367Sahrens  * zc_value		name of snapshot to clone from (may be empty)
27045367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27055367Sahrens  *
27065498Stimh  * outputs: none
27075367Sahrens  */
2708789Sahrens static int
2709789Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2710789Sahrens {
2711789Sahrens 	objset_t *clone;
2712789Sahrens 	int error = 0;
27135331Samw 	zfs_creat_t zct;
27144543Smarks 	nvlist_t *nvprops = NULL;
27154543Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2716789Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2717789Sahrens 
2718789Sahrens 	switch (type) {
2719789Sahrens 
2720789Sahrens 	case DMU_OST_ZFS:
2721789Sahrens 		cbfunc = zfs_create_cb;
2722789Sahrens 		break;
2723789Sahrens 
2724789Sahrens 	case DMU_OST_ZVOL:
2725789Sahrens 		cbfunc = zvol_create_cb;
2726789Sahrens 		break;
2727789Sahrens 
2728789Sahrens 	default:
27292199Sahrens 		cbfunc = NULL;
27306423Sgw25295 		break;
27312199Sahrens 	}
27325326Sek110237 	if (strchr(zc->zc_name, '@') ||
27335326Sek110237 	    strchr(zc->zc_name, '%'))
2734789Sahrens 		return (EINVAL);
2735789Sahrens 
27362676Seschrock 	if (zc->zc_nvlist_src != NULL &&
27375094Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
27389643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
27392676Seschrock 		return (error);
27402676Seschrock 
27415498Stimh 	zct.zct_zplprops = NULL;
27425331Samw 	zct.zct_props = nvprops;
27435331Samw 
27442676Seschrock 	if (zc->zc_value[0] != '\0') {
2745789Sahrens 		/*
2746789Sahrens 		 * We're creating a clone of an existing snapshot.
2747789Sahrens 		 */
27482676Seschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
27492676Seschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
27504543Smarks 			nvlist_free(nvprops);
2751789Sahrens 			return (EINVAL);
27522676Seschrock 		}
2753789Sahrens 
275410298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
27552676Seschrock 		if (error) {
27564543Smarks 			nvlist_free(nvprops);
2757789Sahrens 			return (error);
27582676Seschrock 		}
27596492Stimh 
276010272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
276110298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(clone, FTAG);
27625331Samw 		if (error) {
27635331Samw 			nvlist_free(nvprops);
27645331Samw 			return (error);
27655331Samw 		}
2766789Sahrens 	} else {
27676492Stimh 		boolean_t is_insensitive = B_FALSE;
27686492Stimh 
27692676Seschrock 		if (cbfunc == NULL) {
27704543Smarks 			nvlist_free(nvprops);
27712199Sahrens 			return (EINVAL);
27722676Seschrock 		}
27732676Seschrock 
2774789Sahrens 		if (type == DMU_OST_ZVOL) {
27752676Seschrock 			uint64_t volsize, volblocksize;
27762676Seschrock 
27774543Smarks 			if (nvprops == NULL ||
27784543Smarks 			    nvlist_lookup_uint64(nvprops,
27792676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
27802676Seschrock 			    &volsize) != 0) {
27814543Smarks 				nvlist_free(nvprops);
27822676Seschrock 				return (EINVAL);
27832676Seschrock 			}
27842676Seschrock 
27854543Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
27862676Seschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
27872676Seschrock 			    &volblocksize)) != 0 && error != ENOENT) {
27884543Smarks 				nvlist_free(nvprops);
27892676Seschrock 				return (EINVAL);
27902676Seschrock 			}
27911133Seschrock 
27922676Seschrock 			if (error != 0)
27932676Seschrock 				volblocksize = zfs_prop_default_numeric(
27942676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
27952676Seschrock 
27962676Seschrock 			if ((error = zvol_check_volblocksize(
27972676Seschrock 			    volblocksize)) != 0 ||
27982676Seschrock 			    (error = zvol_check_volsize(volsize,
27992676Seschrock 			    volblocksize)) != 0) {
28004543Smarks 				nvlist_free(nvprops);
2801789Sahrens 				return (error);
28022676Seschrock 			}
28034577Sahrens 		} else if (type == DMU_OST_ZFS) {
28045331Samw 			int error;
28055331Samw 
28065498Stimh 			/*
28075331Samw 			 * We have to have normalization and
28085331Samw 			 * case-folding flags correct when we do the
28095331Samw 			 * file system creation, so go figure them out
28105498Stimh 			 * now.
28115331Samw 			 */
28125498Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
28135498Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
28145498Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
28157184Stimh 			    zct.zct_zplprops, &is_insensitive);
28165331Samw 			if (error != 0) {
28175331Samw 				nvlist_free(nvprops);
28185498Stimh 				nvlist_free(zct.zct_zplprops);
28195331Samw 				return (error);
28204577Sahrens 			}
28212676Seschrock 		}
282210272SMatthew.Ahrens@Sun.COM 		error = dmu_objset_create(zc->zc_name, type,
28236492Stimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
28245498Stimh 		nvlist_free(zct.zct_zplprops);
2825789Sahrens 	}
28262676Seschrock 
28272676Seschrock 	/*
28282676Seschrock 	 * It would be nice to do this atomically.
28292676Seschrock 	 */
28302676Seschrock 	if (error == 0) {
283111022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
283211022STom.Erickson@Sun.COM 		    nvprops, NULL);
283311022STom.Erickson@Sun.COM 		if (error != 0)
283410242Schris.kirby@sun.com 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
28352676Seschrock 	}
28364543Smarks 	nvlist_free(nvprops);
2837789Sahrens 	return (error);
2838789Sahrens }
2839789Sahrens 
28405367Sahrens /*
28415367Sahrens  * inputs:
28425367Sahrens  * zc_name	name of filesystem
28435367Sahrens  * zc_value	short name of snapshot
28445367Sahrens  * zc_cookie	recursive flag
28459396SMatthew.Ahrens@Sun.COM  * zc_nvlist_src[_size] property list
28465367Sahrens  *
284710588SEric.Taylor@Sun.COM  * outputs:
284810588SEric.Taylor@Sun.COM  * zc_value	short snapname (i.e. part after the '@')
28495367Sahrens  */
2850789Sahrens static int
28512199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
28522199Sahrens {
28537265Sahrens 	nvlist_t *nvprops = NULL;
28547265Sahrens 	int error;
28557265Sahrens 	boolean_t recursive = zc->zc_cookie;
28567265Sahrens 
28572676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
28582199Sahrens 		return (EINVAL);
28597265Sahrens 
28607265Sahrens 	if (zc->zc_nvlist_src != NULL &&
28617265Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
28629643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &nvprops)) != 0)
28637265Sahrens 		return (error);
28647265Sahrens 
28659355SMatthew.Ahrens@Sun.COM 	error = zfs_check_userprops(zc->zc_name, nvprops);
28669355SMatthew.Ahrens@Sun.COM 	if (error)
28679355SMatthew.Ahrens@Sun.COM 		goto out;
28689355SMatthew.Ahrens@Sun.COM 
286911022STom.Erickson@Sun.COM 	if (!nvlist_empty(nvprops) &&
28709355SMatthew.Ahrens@Sun.COM 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
28719355SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
28729355SMatthew.Ahrens@Sun.COM 		goto out;
28737265Sahrens 	}
28749355SMatthew.Ahrens@Sun.COM 
28759355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
28769355SMatthew.Ahrens@Sun.COM 	    nvprops, recursive);
28779355SMatthew.Ahrens@Sun.COM 
28789355SMatthew.Ahrens@Sun.COM out:
28797265Sahrens 	nvlist_free(nvprops);
28807265Sahrens 	return (error);
28812199Sahrens }
28822199Sahrens 
28834007Smmusante int
288411209SMatthew.Ahrens@Sun.COM zfs_unmount_snap(const char *name, void *arg)
2885789Sahrens {
28862417Sahrens 	vfs_t *vfsp = NULL;
28872199Sahrens 
28886689Smaybee 	if (arg) {
28896689Smaybee 		char *snapname = arg;
289011209SMatthew.Ahrens@Sun.COM 		char *fullname = kmem_asprintf("%s@%s", name, snapname);
289111209SMatthew.Ahrens@Sun.COM 		vfsp = zfs_get_vfs(fullname);
289211209SMatthew.Ahrens@Sun.COM 		strfree(fullname);
28932417Sahrens 	} else if (strchr(name, '@')) {
28942199Sahrens 		vfsp = zfs_get_vfs(name);
28952199Sahrens 	}
28962199Sahrens 
28972199Sahrens 	if (vfsp) {
28982199Sahrens 		/*
28992199Sahrens 		 * Always force the unmount for snapshots.
29002199Sahrens 		 */
29012199Sahrens 		int flag = MS_FORCE;
2902789Sahrens 		int err;
2903789Sahrens 
29042199Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
29052199Sahrens 			VFS_RELE(vfsp);
29062199Sahrens 			return (err);
29072199Sahrens 		}
29082199Sahrens 		VFS_RELE(vfsp);
29092199Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
29102199Sahrens 			return (err);
29112199Sahrens 	}
29122199Sahrens 	return (0);
29132199Sahrens }
29142199Sahrens 
29155367Sahrens /*
29165367Sahrens  * inputs:
291710242Schris.kirby@sun.com  * zc_name		name of filesystem
291810242Schris.kirby@sun.com  * zc_value		short name of snapshot
291910242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
29205367Sahrens  *
29215367Sahrens  * outputs:	none
29225367Sahrens  */
29232199Sahrens static int
29242199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
29252199Sahrens {
29262199Sahrens 	int err;
2927789Sahrens 
29282676Seschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
29292199Sahrens 		return (EINVAL);
29302199Sahrens 	err = dmu_objset_find(zc->zc_name,
29312676Seschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
29322199Sahrens 	if (err)
29332199Sahrens 		return (err);
293410242Schris.kirby@sun.com 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
293510242Schris.kirby@sun.com 	    zc->zc_defer_destroy));
29362199Sahrens }
29372199Sahrens 
29385367Sahrens /*
29395367Sahrens  * inputs:
29405367Sahrens  * zc_name		name of dataset to destroy
29415367Sahrens  * zc_objset_type	type of objset
294210242Schris.kirby@sun.com  * zc_defer_destroy	mark for deferred destroy
29435367Sahrens  *
29445367Sahrens  * outputs:		none
29455367Sahrens  */
29462199Sahrens static int
29472199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
29482199Sahrens {
294910588SEric.Taylor@Sun.COM 	int err;
29502199Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
295110588SEric.Taylor@Sun.COM 		err = zfs_unmount_snap(zc->zc_name, NULL);
29522199Sahrens 		if (err)
29532199Sahrens 			return (err);
2954789Sahrens 	}
2955789Sahrens 
295610588SEric.Taylor@Sun.COM 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
295710588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
295810693Schris.kirby@sun.com 		(void) zvol_remove_minor(zc->zc_name);
295910588SEric.Taylor@Sun.COM 	return (err);
2960789Sahrens }
2961789Sahrens 
29625367Sahrens /*
29635367Sahrens  * inputs:
29645446Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
29655367Sahrens  *
29665367Sahrens  * outputs:	none
29675367Sahrens  */
2968789Sahrens static int
2969789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2970789Sahrens {
297110272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds, *clone;
29725446Sahrens 	int error;
297310272SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
297410272SMatthew.Ahrens@Sun.COM 	char *clone_name;
297510272SMatthew.Ahrens@Sun.COM 
297610272SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
297710272SMatthew.Ahrens@Sun.COM 	if (error)
297810272SMatthew.Ahrens@Sun.COM 		return (error);
297910272SMatthew.Ahrens@Sun.COM 
298010272SMatthew.Ahrens@Sun.COM 	/* must not be a snapshot */
298110272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
298210272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
298310272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
298410272SMatthew.Ahrens@Sun.COM 	}
298510272SMatthew.Ahrens@Sun.COM 
298610272SMatthew.Ahrens@Sun.COM 	/* must have a most recent snapshot */
298710272SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
298810272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
298910272SMatthew.Ahrens@Sun.COM 		return (EINVAL);
299010272SMatthew.Ahrens@Sun.COM 	}
29915446Sahrens 
29925446Sahrens 	/*
299310272SMatthew.Ahrens@Sun.COM 	 * Create clone of most recent snapshot.
29945446Sahrens 	 */
299510272SMatthew.Ahrens@Sun.COM 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
299610272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
29975446Sahrens 	if (error)
299810272SMatthew.Ahrens@Sun.COM 		goto out;
299910272SMatthew.Ahrens@Sun.COM 
300010298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
300110272SMatthew.Ahrens@Sun.COM 	if (error)
300210272SMatthew.Ahrens@Sun.COM 		goto out;
300310272SMatthew.Ahrens@Sun.COM 
300410272SMatthew.Ahrens@Sun.COM 	/*
300510272SMatthew.Ahrens@Sun.COM 	 * Do clone swap.
300610272SMatthew.Ahrens@Sun.COM 	 */
30079396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
300810298SMatthew.Ahrens@Sun.COM 		error = zfs_suspend_fs(zfsvfs);
30096083Sek110237 		if (error == 0) {
30106083Sek110237 			int resume_err;
30116083Sek110237 
301210272SMatthew.Ahrens@Sun.COM 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
301310272SMatthew.Ahrens@Sun.COM 				error = dsl_dataset_clone_swap(clone, ds,
301410272SMatthew.Ahrens@Sun.COM 				    B_TRUE);
301510272SMatthew.Ahrens@Sun.COM 				dsl_dataset_disown(ds, FTAG);
301610272SMatthew.Ahrens@Sun.COM 				ds = NULL;
301710272SMatthew.Ahrens@Sun.COM 			} else {
301810272SMatthew.Ahrens@Sun.COM 				error = EBUSY;
301910272SMatthew.Ahrens@Sun.COM 			}
302010298SMatthew.Ahrens@Sun.COM 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
30216083Sek110237 			error = error ? error : resume_err;
30226083Sek110237 		}
30235446Sahrens 		VFS_RELE(zfsvfs->z_vfs);
30245446Sahrens 	} else {
302510272SMatthew.Ahrens@Sun.COM 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
302610272SMatthew.Ahrens@Sun.COM 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
302710272SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
302810272SMatthew.Ahrens@Sun.COM 			ds = NULL;
302910272SMatthew.Ahrens@Sun.COM 		} else {
303010272SMatthew.Ahrens@Sun.COM 			error = EBUSY;
303110272SMatthew.Ahrens@Sun.COM 		}
30325446Sahrens 	}
303310272SMatthew.Ahrens@Sun.COM 
303410272SMatthew.Ahrens@Sun.COM 	/*
303510272SMatthew.Ahrens@Sun.COM 	 * Destroy clone (which also closes it).
303610272SMatthew.Ahrens@Sun.COM 	 */
303710272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
303810272SMatthew.Ahrens@Sun.COM 
303910272SMatthew.Ahrens@Sun.COM out:
304010272SMatthew.Ahrens@Sun.COM 	strfree(clone_name);
304110272SMatthew.Ahrens@Sun.COM 	if (ds)
304210272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
30435446Sahrens 	return (error);
3044789Sahrens }
3045789Sahrens 
30465367Sahrens /*
30475367Sahrens  * inputs:
30485367Sahrens  * zc_name	old name of dataset
30495367Sahrens  * zc_value	new name of dataset
30505367Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
30515367Sahrens  *
30525367Sahrens  * outputs:	none
30535367Sahrens  */
3054789Sahrens static int
3055789Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3056789Sahrens {
30574490Svb160487 	boolean_t recursive = zc->zc_cookie & 1;
30584007Smmusante 
30592676Seschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
30605326Sek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
30615326Sek110237 	    strchr(zc->zc_value, '%'))
3062789Sahrens 		return (EINVAL);
3063789Sahrens 
30644007Smmusante 	/*
30654007Smmusante 	 * Unmount snapshot unless we're doing a recursive rename,
30664007Smmusante 	 * in which case the dataset code figures out which snapshots
30674007Smmusante 	 * to unmount.
30684007Smmusante 	 */
30694007Smmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3070789Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
30712199Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
30722199Sahrens 		if (err)
30732199Sahrens 			return (err);
3074789Sahrens 	}
307510588SEric.Taylor@Sun.COM 	if (zc->zc_objset_type == DMU_OST_ZVOL)
307610588SEric.Taylor@Sun.COM 		(void) zvol_remove_minor(zc->zc_name);
30774007Smmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3078789Sahrens }
3079789Sahrens 
308011022STom.Erickson@Sun.COM static int
308111022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
308211022STom.Erickson@Sun.COM {
308311022STom.Erickson@Sun.COM 	const char *propname = nvpair_name(pair);
308411022STom.Erickson@Sun.COM 	boolean_t issnap = (strchr(dsname, '@') != NULL);
308511022STom.Erickson@Sun.COM 	zfs_prop_t prop = zfs_name_to_prop(propname);
308611022STom.Erickson@Sun.COM 	uint64_t intval;
308711022STom.Erickson@Sun.COM 	int err;
308811022STom.Erickson@Sun.COM 
308911022STom.Erickson@Sun.COM 	if (prop == ZPROP_INVAL) {
309011022STom.Erickson@Sun.COM 		if (zfs_prop_user(propname)) {
309111022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname,
309211022STom.Erickson@Sun.COM 			    ZFS_DELEG_PERM_USERPROP, cr))
309311022STom.Erickson@Sun.COM 				return (err);
309411022STom.Erickson@Sun.COM 			return (0);
309511022STom.Erickson@Sun.COM 		}
309611022STom.Erickson@Sun.COM 
309711022STom.Erickson@Sun.COM 		if (!issnap && zfs_prop_userquota(propname)) {
309811022STom.Erickson@Sun.COM 			const char *perm = NULL;
309911022STom.Erickson@Sun.COM 			const char *uq_prefix =
310011022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
310111022STom.Erickson@Sun.COM 			const char *gq_prefix =
310211022STom.Erickson@Sun.COM 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
310311022STom.Erickson@Sun.COM 
310411022STom.Erickson@Sun.COM 			if (strncmp(propname, uq_prefix,
310511022STom.Erickson@Sun.COM 			    strlen(uq_prefix)) == 0) {
310611022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_USERQUOTA;
310711022STom.Erickson@Sun.COM 			} else if (strncmp(propname, gq_prefix,
310811022STom.Erickson@Sun.COM 			    strlen(gq_prefix)) == 0) {
310911022STom.Erickson@Sun.COM 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
311011022STom.Erickson@Sun.COM 			} else {
311111022STom.Erickson@Sun.COM 				/* USERUSED and GROUPUSED are read-only */
311211022STom.Erickson@Sun.COM 				return (EINVAL);
311311022STom.Erickson@Sun.COM 			}
311411022STom.Erickson@Sun.COM 
311511022STom.Erickson@Sun.COM 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
311611022STom.Erickson@Sun.COM 				return (err);
311711022STom.Erickson@Sun.COM 			return (0);
311811022STom.Erickson@Sun.COM 		}
311911022STom.Erickson@Sun.COM 
312011022STom.Erickson@Sun.COM 		return (EINVAL);
312111022STom.Erickson@Sun.COM 	}
312211022STom.Erickson@Sun.COM 
312311022STom.Erickson@Sun.COM 	if (issnap)
312411022STom.Erickson@Sun.COM 		return (EINVAL);
312511022STom.Erickson@Sun.COM 
312611022STom.Erickson@Sun.COM 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
312711022STom.Erickson@Sun.COM 		/*
312811022STom.Erickson@Sun.COM 		 * dsl_prop_get_all_impl() returns properties in this
312911022STom.Erickson@Sun.COM 		 * format.
313011022STom.Erickson@Sun.COM 		 */
313111022STom.Erickson@Sun.COM 		nvlist_t *attrs;
313211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
313311022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
313411022STom.Erickson@Sun.COM 		    &pair) == 0);
313511022STom.Erickson@Sun.COM 	}
313611022STom.Erickson@Sun.COM 
313711022STom.Erickson@Sun.COM 	/*
313811022STom.Erickson@Sun.COM 	 * Check that this value is valid for this pool version
313911022STom.Erickson@Sun.COM 	 */
314011022STom.Erickson@Sun.COM 	switch (prop) {
314111022STom.Erickson@Sun.COM 	case ZFS_PROP_COMPRESSION:
314211022STom.Erickson@Sun.COM 		/*
314311022STom.Erickson@Sun.COM 		 * If the user specified gzip compression, make sure
314411022STom.Erickson@Sun.COM 		 * the SPA supports it. We ignore any errors here since
314511022STom.Erickson@Sun.COM 		 * we'll catch them later.
314611022STom.Erickson@Sun.COM 		 */
314711022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
314811022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
314911022STom.Erickson@Sun.COM 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
315011022STom.Erickson@Sun.COM 			    intval <= ZIO_COMPRESS_GZIP_9 &&
315111022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
315211022STom.Erickson@Sun.COM 			    SPA_VERSION_GZIP_COMPRESSION)) {
315311022STom.Erickson@Sun.COM 				return (ENOTSUP);
315411022STom.Erickson@Sun.COM 			}
315511022STom.Erickson@Sun.COM 
315611022STom.Erickson@Sun.COM 			if (intval == ZIO_COMPRESS_ZLE &&
315711022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
315811022STom.Erickson@Sun.COM 			    SPA_VERSION_ZLE_COMPRESSION))
315911022STom.Erickson@Sun.COM 				return (ENOTSUP);
316011022STom.Erickson@Sun.COM 
316111022STom.Erickson@Sun.COM 			/*
316211022STom.Erickson@Sun.COM 			 * If this is a bootable dataset then
316311022STom.Erickson@Sun.COM 			 * verify that the compression algorithm
316411022STom.Erickson@Sun.COM 			 * is supported for booting. We must return
316511022STom.Erickson@Sun.COM 			 * something other than ENOTSUP since it
316611022STom.Erickson@Sun.COM 			 * implies a downrev pool version.
316711022STom.Erickson@Sun.COM 			 */
316811022STom.Erickson@Sun.COM 			if (zfs_is_bootfs(dsname) &&
316911022STom.Erickson@Sun.COM 			    !BOOTFS_COMPRESS_VALID(intval)) {
317011022STom.Erickson@Sun.COM 				return (ERANGE);
317111022STom.Erickson@Sun.COM 			}
317211022STom.Erickson@Sun.COM 		}
317311022STom.Erickson@Sun.COM 		break;
317411022STom.Erickson@Sun.COM 
317511022STom.Erickson@Sun.COM 	case ZFS_PROP_COPIES:
317611022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
317711022STom.Erickson@Sun.COM 			return (ENOTSUP);
317811022STom.Erickson@Sun.COM 		break;
317911022STom.Erickson@Sun.COM 
318011022STom.Erickson@Sun.COM 	case ZFS_PROP_DEDUP:
318111022STom.Erickson@Sun.COM 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
318211022STom.Erickson@Sun.COM 			return (ENOTSUP);
318311022STom.Erickson@Sun.COM 		break;
318411022STom.Erickson@Sun.COM 
318511022STom.Erickson@Sun.COM 	case ZFS_PROP_SHARESMB:
318611022STom.Erickson@Sun.COM 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
318711022STom.Erickson@Sun.COM 			return (ENOTSUP);
318811022STom.Erickson@Sun.COM 		break;
318911022STom.Erickson@Sun.COM 
319011022STom.Erickson@Sun.COM 	case ZFS_PROP_ACLINHERIT:
319111022STom.Erickson@Sun.COM 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
319211022STom.Erickson@Sun.COM 		    nvpair_value_uint64(pair, &intval) == 0) {
319311022STom.Erickson@Sun.COM 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
319411022STom.Erickson@Sun.COM 			    zfs_earlier_version(dsname,
319511022STom.Erickson@Sun.COM 			    SPA_VERSION_PASSTHROUGH_X))
319611022STom.Erickson@Sun.COM 				return (ENOTSUP);
319711022STom.Erickson@Sun.COM 		}
319811022STom.Erickson@Sun.COM 		break;
319911022STom.Erickson@Sun.COM 	}
320011022STom.Erickson@Sun.COM 
320111022STom.Erickson@Sun.COM 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
320211022STom.Erickson@Sun.COM }
320311022STom.Erickson@Sun.COM 
320411022STom.Erickson@Sun.COM /*
320511022STom.Erickson@Sun.COM  * Removes properties from the given props list that fail permission checks
320611022STom.Erickson@Sun.COM  * needed to clear them and to restore them in case of a receive error. For each
320711022STom.Erickson@Sun.COM  * property, make sure we have both set and inherit permissions.
320811022STom.Erickson@Sun.COM  *
320911022STom.Erickson@Sun.COM  * Returns the first error encountered if any permission checks fail. If the
321011022STom.Erickson@Sun.COM  * caller provides a non-NULL errlist, it also gives the complete list of names
321111022STom.Erickson@Sun.COM  * of all the properties that failed a permission check along with the
321211022STom.Erickson@Sun.COM  * corresponding error numbers. The caller is responsible for freeing the
321311022STom.Erickson@Sun.COM  * returned errlist.
321411022STom.Erickson@Sun.COM  *
321511022STom.Erickson@Sun.COM  * If every property checks out successfully, zero is returned and the list
321611022STom.Erickson@Sun.COM  * pointed at by errlist is NULL.
321711022STom.Erickson@Sun.COM  */
321811022STom.Erickson@Sun.COM static int
321911022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
32206689Smaybee {
32216689Smaybee 	zfs_cmd_t *zc;
322211022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
322311022STom.Erickson@Sun.COM 	nvlist_t *errors;
322411022STom.Erickson@Sun.COM 	int err, rv = 0;
32256689Smaybee 
32266689Smaybee 	if (props == NULL)
322711022STom.Erickson@Sun.COM 		return (0);
322811022STom.Erickson@Sun.COM 
322911022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
323011022STom.Erickson@Sun.COM 
32316689Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
32326689Smaybee 	(void) strcpy(zc->zc_name, dataset);
323311022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
323411022STom.Erickson@Sun.COM 	while (pair != NULL) {
323511022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
323611022STom.Erickson@Sun.COM 
323711022STom.Erickson@Sun.COM 		(void) strcpy(zc->zc_value, nvpair_name(pair));
323811022STom.Erickson@Sun.COM 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
323911022STom.Erickson@Sun.COM 		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
324011022STom.Erickson@Sun.COM 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
324111022STom.Erickson@Sun.COM 			VERIFY(nvlist_add_int32(errors,
324211022STom.Erickson@Sun.COM 			    zc->zc_value, err) == 0);
324311022STom.Erickson@Sun.COM 		}
324411022STom.Erickson@Sun.COM 		pair = next_pair;
32456689Smaybee 	}
32466689Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
324711022STom.Erickson@Sun.COM 
324811022STom.Erickson@Sun.COM 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
324911022STom.Erickson@Sun.COM 		nvlist_free(errors);
325011022STom.Erickson@Sun.COM 		errors = NULL;
325111022STom.Erickson@Sun.COM 	} else {
325211022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
325311022STom.Erickson@Sun.COM 	}
325411022STom.Erickson@Sun.COM 
325511022STom.Erickson@Sun.COM 	if (errlist == NULL)
325611022STom.Erickson@Sun.COM 		nvlist_free(errors);
325711022STom.Erickson@Sun.COM 	else
325811022STom.Erickson@Sun.COM 		*errlist = errors;
325911022STom.Erickson@Sun.COM 
326011022STom.Erickson@Sun.COM 	return (rv);
32616689Smaybee }
32626689Smaybee 
326311022STom.Erickson@Sun.COM static boolean_t
326411022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2)
326511022STom.Erickson@Sun.COM {
326611022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
326711022STom.Erickson@Sun.COM 		/* dsl_prop_get_all_impl() format */
326811022STom.Erickson@Sun.COM 		nvlist_t *attrs;
326911022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
327011022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
327111022STom.Erickson@Sun.COM 		    &p1) == 0);
327211022STom.Erickson@Sun.COM 	}
327311022STom.Erickson@Sun.COM 
327411022STom.Erickson@Sun.COM 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
327511022STom.Erickson@Sun.COM 		nvlist_t *attrs;
327611022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
327711022STom.Erickson@Sun.COM 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
327811022STom.Erickson@Sun.COM 		    &p2) == 0);
327911022STom.Erickson@Sun.COM 	}
328011022STom.Erickson@Sun.COM 
328111022STom.Erickson@Sun.COM 	if (nvpair_type(p1) != nvpair_type(p2))
328211022STom.Erickson@Sun.COM 		return (B_FALSE);
328311022STom.Erickson@Sun.COM 
328411022STom.Erickson@Sun.COM 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
328511022STom.Erickson@Sun.COM 		char *valstr1, *valstr2;
328611022STom.Erickson@Sun.COM 
328711022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
328811022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
328911022STom.Erickson@Sun.COM 		return (strcmp(valstr1, valstr2) == 0);
329011022STom.Erickson@Sun.COM 	} else {
329111022STom.Erickson@Sun.COM 		uint64_t intval1, intval2;
329211022STom.Erickson@Sun.COM 
329311022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
329411022STom.Erickson@Sun.COM 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
329511022STom.Erickson@Sun.COM 		return (intval1 == intval2);
329611022STom.Erickson@Sun.COM 	}
329711022STom.Erickson@Sun.COM }
329811022STom.Erickson@Sun.COM 
329911022STom.Erickson@Sun.COM /*
330011022STom.Erickson@Sun.COM  * Remove properties from props if they are not going to change (as determined
330111022STom.Erickson@Sun.COM  * by comparison with origprops). Remove them from origprops as well, since we
330211022STom.Erickson@Sun.COM  * do not need to clear or restore properties that won't change.
330311022STom.Erickson@Sun.COM  */
330411022STom.Erickson@Sun.COM static void
330511022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops)
330611022STom.Erickson@Sun.COM {
330711022STom.Erickson@Sun.COM 	nvpair_t *pair, *next_pair;
330811022STom.Erickson@Sun.COM 
330911022STom.Erickson@Sun.COM 	if (origprops == NULL)
331011022STom.Erickson@Sun.COM 		return; /* all props need to be received */
331111022STom.Erickson@Sun.COM 
331211022STom.Erickson@Sun.COM 	pair = nvlist_next_nvpair(props, NULL);
331311022STom.Erickson@Sun.COM 	while (pair != NULL) {
331411022STom.Erickson@Sun.COM 		const char *propname = nvpair_name(pair);
331511022STom.Erickson@Sun.COM 		nvpair_t *match;
331611022STom.Erickson@Sun.COM 
331711022STom.Erickson@Sun.COM 		next_pair = nvlist_next_nvpair(props, pair);
331811022STom.Erickson@Sun.COM 
331911022STom.Erickson@Sun.COM 		if ((nvlist_lookup_nvpair(origprops, propname,
332011022STom.Erickson@Sun.COM 		    &match) != 0) || !propval_equals(pair, match))
332111022STom.Erickson@Sun.COM 			goto next; /* need to set received value */
332211022STom.Erickson@Sun.COM 
332311022STom.Erickson@Sun.COM 		/* don't clear the existing received value */
332411022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(origprops, match);
332511022STom.Erickson@Sun.COM 		/* don't bother receiving the property */
332611022STom.Erickson@Sun.COM 		(void) nvlist_remove_nvpair(props, pair);
332711022STom.Erickson@Sun.COM next:
332811022STom.Erickson@Sun.COM 		pair = next_pair;
332911022STom.Erickson@Sun.COM 	}
333011022STom.Erickson@Sun.COM }
333111022STom.Erickson@Sun.COM 
333211022STom.Erickson@Sun.COM #ifdef	DEBUG
333311022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err;
333411022STom.Erickson@Sun.COM #endif
333511022STom.Erickson@Sun.COM 
33365367Sahrens /*
33375367Sahrens  * inputs:
33385367Sahrens  * zc_name		name of containing filesystem
33395367Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
33405367Sahrens  * zc_value		name of snapshot to create
33415367Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
33425367Sahrens  * zc_cookie		file descriptor to recv from
33435367Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
33445367Sahrens  * zc_guid		force flag
33455367Sahrens  *
33465367Sahrens  * outputs:
33475367Sahrens  * zc_cookie		number of bytes read
334811022STom.Erickson@Sun.COM  * zc_nvlist_dst{_size} error for each unapplied received property
334911022STom.Erickson@Sun.COM  * zc_obj		zprop_errflags_t
33505367Sahrens  */
3351789Sahrens static int
33525367Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3353789Sahrens {
3354789Sahrens 	file_t *fp;
33555326Sek110237 	objset_t *os;
33565367Sahrens 	dmu_recv_cookie_t drc;
33575326Sek110237 	boolean_t force = (boolean_t)zc->zc_guid;
335811022STom.Erickson@Sun.COM 	int fd;
335911022STom.Erickson@Sun.COM 	int error = 0;
336011022STom.Erickson@Sun.COM 	int props_error = 0;
336111022STom.Erickson@Sun.COM 	nvlist_t *errors;
33625367Sahrens 	offset_t off;
336311022STom.Erickson@Sun.COM 	nvlist_t *props = NULL; /* sent properties */
336411022STom.Erickson@Sun.COM 	nvlist_t *origprops = NULL; /* existing properties */
33655367Sahrens 	objset_t *origin = NULL;
33665367Sahrens 	char *tosnap;
33675367Sahrens 	char tofs[ZFS_MAXNAMELEN];
336811022STom.Erickson@Sun.COM 	boolean_t first_recvd_props = B_FALSE;
3369789Sahrens 
33703265Sahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
33715326Sek110237 	    strchr(zc->zc_value, '@') == NULL ||
33725326Sek110237 	    strchr(zc->zc_value, '%'))
33733265Sahrens 		return (EINVAL);
33743265Sahrens 
33755367Sahrens 	(void) strcpy(tofs, zc->zc_value);
33765367Sahrens 	tosnap = strchr(tofs, '@');
337711022STom.Erickson@Sun.COM 	*tosnap++ = '\0';
33785367Sahrens 
33795367Sahrens 	if (zc->zc_nvlist_src != NULL &&
33805367Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
33819643SEric.Taylor@Sun.COM 	    zc->zc_iflags, &props)) != 0)
33825367Sahrens 		return (error);
33835367Sahrens 
3384789Sahrens 	fd = zc->zc_cookie;
3385789Sahrens 	fp = getf(fd);
33865367Sahrens 	if (fp == NULL) {
33875367Sahrens 		nvlist_free(props);
3388789Sahrens 		return (EBADF);
33895367Sahrens 	}
33905326Sek110237 
339111022STom.Erickson@Sun.COM 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
339211022STom.Erickson@Sun.COM 
339310298SMatthew.Ahrens@Sun.COM 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
339411022STom.Erickson@Sun.COM 		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
339511022STom.Erickson@Sun.COM 		    !dsl_prop_get_hasrecvd(os)) {
339611022STom.Erickson@Sun.COM 			first_recvd_props = B_TRUE;
339711022STom.Erickson@Sun.COM 		}
339811022STom.Erickson@Sun.COM 
33996689Smaybee 		/*
340011022STom.Erickson@Sun.COM 		 * If new received properties are supplied, they are to
340111022STom.Erickson@Sun.COM 		 * completely replace the existing received properties, so stash
340211022STom.Erickson@Sun.COM 		 * away the existing ones.
34036689Smaybee 		 */
340411022STom.Erickson@Sun.COM 		if (dsl_prop_get_received(os, &origprops) == 0) {
340511022STom.Erickson@Sun.COM 			nvlist_t *errlist = NULL;
340611022STom.Erickson@Sun.COM 			/*
340711022STom.Erickson@Sun.COM 			 * Don't bother writing a property if its value won't
340811022STom.Erickson@Sun.COM 			 * change (and avoid the unnecessary security checks).
340911022STom.Erickson@Sun.COM 			 *
341011022STom.Erickson@Sun.COM 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
341111022STom.Erickson@Sun.COM 			 * special case where we blow away all local properties
341211022STom.Erickson@Sun.COM 			 * regardless.
341311022STom.Erickson@Sun.COM 			 */
341411022STom.Erickson@Sun.COM 			if (!first_recvd_props)
341511022STom.Erickson@Sun.COM 				props_reduce(props, origprops);
341611022STom.Erickson@Sun.COM 			if (zfs_check_clearable(tofs, origprops,
341711022STom.Erickson@Sun.COM 			    &errlist) != 0)
341811022STom.Erickson@Sun.COM 				(void) nvlist_merge(errors, errlist, 0);
341911022STom.Erickson@Sun.COM 			nvlist_free(errlist);
342011022STom.Erickson@Sun.COM 		}
342110298SMatthew.Ahrens@Sun.COM 
342210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
34235326Sek110237 	}
34245326Sek110237 
34255367Sahrens 	if (zc->zc_string[0]) {
342610298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
34276689Smaybee 		if (error)
34286689Smaybee 			goto out;
34295367Sahrens 	}
34305367Sahrens 
343111007SLori.Alt@Sun.COM 	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
343211007SLori.Alt@Sun.COM 	    &zc->zc_begin_record, force, origin, &drc);
34335367Sahrens 	if (origin)
343410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(origin, FTAG);
34356689Smaybee 	if (error)
34366689Smaybee 		goto out;
34375326Sek110237 
34385326Sek110237 	/*
343911022STom.Erickson@Sun.COM 	 * Set properties before we receive the stream so that they are applied
344011022STom.Erickson@Sun.COM 	 * to the new data. Note that we must call dmu_recv_stream() if
344111022STom.Erickson@Sun.COM 	 * dmu_recv_begin() succeeds.
34425326Sek110237 	 */
34435367Sahrens 	if (props) {
344411022STom.Erickson@Sun.COM 		nvlist_t *errlist;
344511022STom.Erickson@Sun.COM 
344611022STom.Erickson@Sun.COM 		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
344711022STom.Erickson@Sun.COM 			if (drc.drc_newfs) {
344811022STom.Erickson@Sun.COM 				if (spa_version(os->os_spa) >=
344911022STom.Erickson@Sun.COM 				    SPA_VERSION_RECVD_PROPS)
345011022STom.Erickson@Sun.COM 					first_recvd_props = B_TRUE;
345111022STom.Erickson@Sun.COM 			} else if (origprops != NULL) {
345211022STom.Erickson@Sun.COM 				if (clear_received_props(os, tofs, origprops,
345311022STom.Erickson@Sun.COM 				    first_recvd_props ? NULL : props) != 0)
345411022STom.Erickson@Sun.COM 					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
345511022STom.Erickson@Sun.COM 			} else {
345611022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
345711022STom.Erickson@Sun.COM 			}
345811022STom.Erickson@Sun.COM 			dsl_prop_set_hasrecvd(os);
345911022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
346011022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
346111022STom.Erickson@Sun.COM 		}
346211022STom.Erickson@Sun.COM 
346311022STom.Erickson@Sun.COM 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
346411022STom.Erickson@Sun.COM 		    props, &errlist);
346511022STom.Erickson@Sun.COM 		(void) nvlist_merge(errors, errlist, 0);
346611022STom.Erickson@Sun.COM 		nvlist_free(errlist);
346711022STom.Erickson@Sun.COM 	}
346811022STom.Erickson@Sun.COM 
346911022STom.Erickson@Sun.COM 	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
34706689Smaybee 		/*
347111022STom.Erickson@Sun.COM 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
347211022STom.Erickson@Sun.COM 		 * size or supplied an invalid address.
34736689Smaybee 		 */
347411022STom.Erickson@Sun.COM 		props_error = EINVAL;
34755367Sahrens 	}
34765367Sahrens 
34775367Sahrens 	off = fp->f_offset;
34785367Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
34795367Sahrens 
348010204SMatthew.Ahrens@Sun.COM 	if (error == 0) {
348110204SMatthew.Ahrens@Sun.COM 		zfsvfs_t *zfsvfs = NULL;
348210204SMatthew.Ahrens@Sun.COM 
348310204SMatthew.Ahrens@Sun.COM 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
348410204SMatthew.Ahrens@Sun.COM 			/* online recv */
348510204SMatthew.Ahrens@Sun.COM 			int end_err;
348610298SMatthew.Ahrens@Sun.COM 
348710298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
348810204SMatthew.Ahrens@Sun.COM 			/*
348910204SMatthew.Ahrens@Sun.COM 			 * If the suspend fails, then the recv_end will
349010204SMatthew.Ahrens@Sun.COM 			 * likely also fail, and clean up after itself.
349110204SMatthew.Ahrens@Sun.COM 			 */
349210204SMatthew.Ahrens@Sun.COM 			end_err = dmu_recv_end(&drc);
349311812SGeorge.Wilson@Sun.COM 			if (error == 0)
349411812SGeorge.Wilson@Sun.COM 				error = zfs_resume_fs(zfsvfs, tofs);
349510204SMatthew.Ahrens@Sun.COM 			error = error ? error : end_err;
349610204SMatthew.Ahrens@Sun.COM 			VFS_RELE(zfsvfs->z_vfs);
349710204SMatthew.Ahrens@Sun.COM 		} else {
34986689Smaybee 			error = dmu_recv_end(&drc);
34995367Sahrens 		}
35006083Sek110237 	}
35015367Sahrens 
35025367Sahrens 	zc->zc_cookie = off - fp->f_offset;
35035367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
35045367Sahrens 		fp->f_offset = off;
35052885Sahrens 
350611022STom.Erickson@Sun.COM #ifdef	DEBUG
350711022STom.Erickson@Sun.COM 	if (zfs_ioc_recv_inject_err) {
350811022STom.Erickson@Sun.COM 		zfs_ioc_recv_inject_err = B_FALSE;
350911022STom.Erickson@Sun.COM 		error = 1;
351011022STom.Erickson@Sun.COM 	}
351111022STom.Erickson@Sun.COM #endif
35126689Smaybee 	/*
35136689Smaybee 	 * On error, restore the original props.
35146689Smaybee 	 */
35156689Smaybee 	if (error && props) {
351611022STom.Erickson@Sun.COM 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
351711022STom.Erickson@Sun.COM 			if (clear_received_props(os, tofs, props, NULL) != 0) {
351811022STom.Erickson@Sun.COM 				/*
351911022STom.Erickson@Sun.COM 				 * We failed to clear the received properties.
352011022STom.Erickson@Sun.COM 				 * Since we may have left a $recvd value on the
352111022STom.Erickson@Sun.COM 				 * system, we can't clear the $hasrecvd flag.
352211022STom.Erickson@Sun.COM 				 */
352311022STom.Erickson@Sun.COM 				zc->zc_obj |= ZPROP_ERR_NORESTORE;
352411022STom.Erickson@Sun.COM 			} else if (first_recvd_props) {
352511022STom.Erickson@Sun.COM 				dsl_prop_unset_hasrecvd(os);
352611022STom.Erickson@Sun.COM 			}
352711022STom.Erickson@Sun.COM 			dmu_objset_rele(os, FTAG);
352811022STom.Erickson@Sun.COM 		} else if (!drc.drc_newfs) {
352911022STom.Erickson@Sun.COM 			/* We failed to clear the received properties. */
353011022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
353111022STom.Erickson@Sun.COM 		}
353211022STom.Erickson@Sun.COM 
353311022STom.Erickson@Sun.COM 		if (origprops == NULL && !drc.drc_newfs) {
353411022STom.Erickson@Sun.COM 			/* We failed to stash the original properties. */
353511022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
353611022STom.Erickson@Sun.COM 		}
353711022STom.Erickson@Sun.COM 
353811022STom.Erickson@Sun.COM 		/*
353911022STom.Erickson@Sun.COM 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
354011022STom.Erickson@Sun.COM 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
354111022STom.Erickson@Sun.COM 		 * explictly if we're restoring local properties cleared in the
354211022STom.Erickson@Sun.COM 		 * first new-style receive.
354311022STom.Erickson@Sun.COM 		 */
354411022STom.Erickson@Sun.COM 		if (origprops != NULL &&
354511022STom.Erickson@Sun.COM 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
354611022STom.Erickson@Sun.COM 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
354711022STom.Erickson@Sun.COM 		    origprops, NULL) != 0) {
354811022STom.Erickson@Sun.COM 			/*
354911022STom.Erickson@Sun.COM 			 * We stashed the original properties but failed to
355011022STom.Erickson@Sun.COM 			 * restore them.
355111022STom.Erickson@Sun.COM 			 */
355211022STom.Erickson@Sun.COM 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
355311022STom.Erickson@Sun.COM 		}
35546689Smaybee 	}
35556689Smaybee out:
35566689Smaybee 	nvlist_free(props);
35576689Smaybee 	nvlist_free(origprops);
355811022STom.Erickson@Sun.COM 	nvlist_free(errors);
3559789Sahrens 	releasef(fd);
356011022STom.Erickson@Sun.COM 
356111022STom.Erickson@Sun.COM 	if (error == 0)
356211022STom.Erickson@Sun.COM 		error = props_error;
356311022STom.Erickson@Sun.COM 
3564789Sahrens 	return (error);
3565789Sahrens }
3566789Sahrens 
35675367Sahrens /*
35685367Sahrens  * inputs:
35695367Sahrens  * zc_name	name of snapshot to send
35705367Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
35715367Sahrens  * zc_cookie	file descriptor to send stream to
35725367Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
35735367Sahrens  *
35745367Sahrens  * outputs: none
35755367Sahrens  */
3576789Sahrens static int
35775367Sahrens zfs_ioc_send(zfs_cmd_t *zc)
3578789Sahrens {
3579789Sahrens 	objset_t *fromsnap = NULL;
3580789Sahrens 	objset_t *tosnap;
3581789Sahrens 	file_t *fp;
3582789Sahrens 	int error;
35835367Sahrens 	offset_t off;
3584789Sahrens 
358510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
3586789Sahrens 	if (error)
3587789Sahrens 		return (error);
3588789Sahrens 
35892676Seschrock 	if (zc->zc_value[0] != '\0') {
35908012SEric.Taylor@Sun.COM 		char *buf;
35912885Sahrens 		char *cp;
35922885Sahrens 
35938012SEric.Taylor@Sun.COM 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
35948012SEric.Taylor@Sun.COM 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
35952885Sahrens 		cp = strchr(buf, '@');
35962885Sahrens 		if (cp)
35972885Sahrens 			*(cp+1) = 0;
35988012SEric.Taylor@Sun.COM 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
359910298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
36008012SEric.Taylor@Sun.COM 		kmem_free(buf, MAXPATHLEN);
3601789Sahrens 		if (error) {
360210298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(tosnap, FTAG);
3603789Sahrens 			return (error);
3604789Sahrens 		}
3605789Sahrens 	}
3606789Sahrens 
3607789Sahrens 	fp = getf(zc->zc_cookie);
3608789Sahrens 	if (fp == NULL) {
360910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(tosnap, FTAG);
3610789Sahrens 		if (fromsnap)
361110298SMatthew.Ahrens@Sun.COM 			dmu_objset_rele(fromsnap, FTAG);
3612789Sahrens 		return (EBADF);
3613789Sahrens 	}
3614789Sahrens 
36155367Sahrens 	off = fp->f_offset;
36165367Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
36175367Sahrens 
36185367Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
36195367Sahrens 		fp->f_offset = off;
3620789Sahrens 	releasef(zc->zc_cookie);
3621789Sahrens 	if (fromsnap)
362210298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(fromsnap, FTAG);
362310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(tosnap, FTAG);
3624789Sahrens 	return (error);
3625789Sahrens }
3626789Sahrens 
36271544Seschrock static int
36281544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
36291544Seschrock {
36301544Seschrock 	int id, error;
36311544Seschrock 
36321544Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
36331544Seschrock 	    &zc->zc_inject_record);
36341544Seschrock 
36351544Seschrock 	if (error == 0)
36361544Seschrock 		zc->zc_guid = (uint64_t)id;
36371544Seschrock 
36381544Seschrock 	return (error);
36391544Seschrock }
36401544Seschrock 
36411544Seschrock static int
36421544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
36431544Seschrock {
36441544Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
36451544Seschrock }
36461544Seschrock 
36471544Seschrock static int
36481544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
36491544Seschrock {
36501544Seschrock 	int id = (int)zc->zc_guid;
36511544Seschrock 	int error;
36521544Seschrock 
36531544Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
36541544Seschrock 	    &zc->zc_inject_record);
36551544Seschrock 
36561544Seschrock 	zc->zc_guid = id;
36571544Seschrock 
36581544Seschrock 	return (error);
36591544Seschrock }
36601544Seschrock 
36611544Seschrock static int
36621544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
36631544Seschrock {
36641544Seschrock 	spa_t *spa;
36651544Seschrock 	int error;
36662676Seschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
36671544Seschrock 
36681544Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
36691544Seschrock 		return (error);
36701544Seschrock 
36712676Seschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
36721544Seschrock 	    &count);
36731544Seschrock 	if (error == 0)
36742676Seschrock 		zc->zc_nvlist_dst_size = count;
36751544Seschrock 	else
36762676Seschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
36771544Seschrock 
36781544Seschrock 	spa_close(spa, FTAG);
36791544Seschrock 
36801544Seschrock 	return (error);
36811544Seschrock }
36821544Seschrock 
36831544Seschrock static int
36841544Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
36851544Seschrock {
36861544Seschrock 	spa_t *spa;
36871544Seschrock 	vdev_t *vd;
36881544Seschrock 	int error;
36891544Seschrock 
36907294Sperrin 	/*
36917294Sperrin 	 * On zpool clear we also fix up missing slogs
36927294Sperrin 	 */
36937294Sperrin 	mutex_enter(&spa_namespace_lock);
36947294Sperrin 	spa = spa_lookup(zc->zc_name);
36957294Sperrin 	if (spa == NULL) {
36967294Sperrin 		mutex_exit(&spa_namespace_lock);
36977294Sperrin 		return (EIO);
36987294Sperrin 	}
369910922SJeff.Bonwick@Sun.COM 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
37007294Sperrin 		/* we need to let spa_open/spa_load clear the chains */
370110922SJeff.Bonwick@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
37027294Sperrin 	}
370310921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
37047294Sperrin 	mutex_exit(&spa_namespace_lock);
37057294Sperrin 
370611727SVictor.Latushkin@Sun.COM 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
370710921STim.Haley@Sun.COM 		error = spa_open(zc->zc_name, &spa, FTAG);
370810921STim.Haley@Sun.COM 	} else {
370910921STim.Haley@Sun.COM 		nvlist_t *policy;
371010921STim.Haley@Sun.COM 		nvlist_t *config = NULL;
371110921STim.Haley@Sun.COM 
371210921STim.Haley@Sun.COM 		if (zc->zc_nvlist_src == NULL)
371310921STim.Haley@Sun.COM 			return (EINVAL);
371410921STim.Haley@Sun.COM 
371510921STim.Haley@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
371610921STim.Haley@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
371710921STim.Haley@Sun.COM 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
371810921STim.Haley@Sun.COM 			    policy, &config);
371910921STim.Haley@Sun.COM 			if (config != NULL) {
372010921STim.Haley@Sun.COM 				(void) put_nvlist(zc, config);
372110921STim.Haley@Sun.COM 				nvlist_free(config);
372210921STim.Haley@Sun.COM 			}
372310921STim.Haley@Sun.COM 			nvlist_free(policy);
372410921STim.Haley@Sun.COM 		}
372510921STim.Haley@Sun.COM 	}
372610921STim.Haley@Sun.COM 
372710921STim.Haley@Sun.COM 	if (error)
37281544Seschrock 		return (error);
37291544Seschrock 
373010685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
37311544Seschrock 
37322676Seschrock 	if (zc->zc_guid == 0) {
37331544Seschrock 		vd = NULL;
37346643Seschrock 	} else {
37356643Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
37365450Sbrendan 		if (vd == NULL) {
37377754SJeff.Bonwick@Sun.COM 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
37385450Sbrendan 			spa_close(spa, FTAG);
37395450Sbrendan 			return (ENODEV);
37405450Sbrendan 		}
37411544Seschrock 	}
37421544Seschrock 
37437754SJeff.Bonwick@Sun.COM 	vdev_clear(spa, vd);
37447754SJeff.Bonwick@Sun.COM 
37457754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
37467754SJeff.Bonwick@Sun.COM 
37477754SJeff.Bonwick@Sun.COM 	/*
37487754SJeff.Bonwick@Sun.COM 	 * Resume any suspended I/Os.
37497754SJeff.Bonwick@Sun.COM 	 */
37509234SGeorge.Wilson@Sun.COM 	if (zio_resume(spa) != 0)
37519234SGeorge.Wilson@Sun.COM 		error = EIO;
37521544Seschrock 
37531544Seschrock 	spa_close(spa, FTAG);
37541544Seschrock 
37559234SGeorge.Wilson@Sun.COM 	return (error);
37561544Seschrock }
37571544Seschrock 
37585367Sahrens /*
37595367Sahrens  * inputs:
37605367Sahrens  * zc_name	name of filesystem
37615367Sahrens  * zc_value	name of origin snapshot
37625367Sahrens  *
376310588SEric.Taylor@Sun.COM  * outputs:
376410588SEric.Taylor@Sun.COM  * zc_string	name of conflicting snapshot, if there is one
37655367Sahrens  */
37661544Seschrock static int
37672082Seschrock zfs_ioc_promote(zfs_cmd_t *zc)
37682082Seschrock {
37692417Sahrens 	char *cp;
37702417Sahrens 
37712417Sahrens 	/*
37722417Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
37732417Sahrens 	 * it's easier.
37742417Sahrens 	 */
37752676Seschrock 	cp = strchr(zc->zc_value, '@');
37762417Sahrens 	if (cp)
37772417Sahrens 		*cp = '\0';
37782676Seschrock 	(void) dmu_objset_find(zc->zc_value,
37792417Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
378010588SEric.Taylor@Sun.COM 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
37812082Seschrock }
37822082Seschrock 
37834543Smarks /*
37849396SMatthew.Ahrens@Sun.COM  * Retrieve a single {user|group}{used|quota}@... property.
37859396SMatthew.Ahrens@Sun.COM  *
37869396SMatthew.Ahrens@Sun.COM  * inputs:
37879396SMatthew.Ahrens@Sun.COM  * zc_name	name of filesystem
37889396SMatthew.Ahrens@Sun.COM  * zc_objset_type zfs_userquota_prop_t
37899396SMatthew.Ahrens@Sun.COM  * zc_value	domain name (eg. "S-1-234-567-89")
37909396SMatthew.Ahrens@Sun.COM  * zc_guid	RID/UID/GID
37919396SMatthew.Ahrens@Sun.COM  *
37929396SMatthew.Ahrens@Sun.COM  * outputs:
37939396SMatthew.Ahrens@Sun.COM  * zc_cookie	property value
37949396SMatthew.Ahrens@Sun.COM  */
37959396SMatthew.Ahrens@Sun.COM static int
37969396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc)
37979396SMatthew.Ahrens@Sun.COM {
37989396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
37999396SMatthew.Ahrens@Sun.COM 	int error;
38009396SMatthew.Ahrens@Sun.COM 
38019396SMatthew.Ahrens@Sun.COM 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
38029396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
38039396SMatthew.Ahrens@Sun.COM 
380410298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
38059396SMatthew.Ahrens@Sun.COM 	if (error)
38069396SMatthew.Ahrens@Sun.COM 		return (error);
38079396SMatthew.Ahrens@Sun.COM 
38089396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_one(zfsvfs,
38099396SMatthew.Ahrens@Sun.COM 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
38109396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
38119396SMatthew.Ahrens@Sun.COM 
38129396SMatthew.Ahrens@Sun.COM 	return (error);
38139396SMatthew.Ahrens@Sun.COM }
38149396SMatthew.Ahrens@Sun.COM 
38159396SMatthew.Ahrens@Sun.COM /*
38169396SMatthew.Ahrens@Sun.COM  * inputs:
38179396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
38189396SMatthew.Ahrens@Sun.COM  * zc_cookie		zap cursor
38199396SMatthew.Ahrens@Sun.COM  * zc_objset_type	zfs_userquota_prop_t
38209396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
38219396SMatthew.Ahrens@Sun.COM  *
38229396SMatthew.Ahrens@Sun.COM  * outputs:
38239396SMatthew.Ahrens@Sun.COM  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
38249396SMatthew.Ahrens@Sun.COM  * zc_cookie	zap cursor
38259396SMatthew.Ahrens@Sun.COM  */
38269396SMatthew.Ahrens@Sun.COM static int
38279396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc)
38289396SMatthew.Ahrens@Sun.COM {
38299396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
383011933STim.Haley@Sun.COM 	int bufsize = zc->zc_nvlist_dst_size;
383111933STim.Haley@Sun.COM 
383211933STim.Haley@Sun.COM 	if (bufsize <= 0)
383311933STim.Haley@Sun.COM 		return (ENOMEM);
383411933STim.Haley@Sun.COM 
383511933STim.Haley@Sun.COM 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
38369396SMatthew.Ahrens@Sun.COM 	if (error)
38379396SMatthew.Ahrens@Sun.COM 		return (error);
38389396SMatthew.Ahrens@Sun.COM 
38399396SMatthew.Ahrens@Sun.COM 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
38409396SMatthew.Ahrens@Sun.COM 
38419396SMatthew.Ahrens@Sun.COM 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
38429396SMatthew.Ahrens@Sun.COM 	    buf, &zc->zc_nvlist_dst_size);
38439396SMatthew.Ahrens@Sun.COM 
38449396SMatthew.Ahrens@Sun.COM 	if (error == 0) {
38459396SMatthew.Ahrens@Sun.COM 		error = xcopyout(buf,
38469396SMatthew.Ahrens@Sun.COM 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
38479396SMatthew.Ahrens@Sun.COM 		    zc->zc_nvlist_dst_size);
38489396SMatthew.Ahrens@Sun.COM 	}
38499396SMatthew.Ahrens@Sun.COM 	kmem_free(buf, bufsize);
38509396SMatthew.Ahrens@Sun.COM 	zfsvfs_rele(zfsvfs, FTAG);
38519396SMatthew.Ahrens@Sun.COM 
38529396SMatthew.Ahrens@Sun.COM 	return (error);
38539396SMatthew.Ahrens@Sun.COM }
38549396SMatthew.Ahrens@Sun.COM 
38559396SMatthew.Ahrens@Sun.COM /*
38569396SMatthew.Ahrens@Sun.COM  * inputs:
38579396SMatthew.Ahrens@Sun.COM  * zc_name		name of filesystem
38589396SMatthew.Ahrens@Sun.COM  *
38599396SMatthew.Ahrens@Sun.COM  * outputs:
38609396SMatthew.Ahrens@Sun.COM  * none
38619396SMatthew.Ahrens@Sun.COM  */
38629396SMatthew.Ahrens@Sun.COM static int
38639396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
38649396SMatthew.Ahrens@Sun.COM {
38659396SMatthew.Ahrens@Sun.COM 	objset_t *os;
386611422SMark.Musante@Sun.COM 	int error = 0;
38679396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
38689396SMatthew.Ahrens@Sun.COM 
38699396SMatthew.Ahrens@Sun.COM 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
387010298SMatthew.Ahrens@Sun.COM 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
38719396SMatthew.Ahrens@Sun.COM 			/*
38729396SMatthew.Ahrens@Sun.COM 			 * If userused is not enabled, it may be because the
38739396SMatthew.Ahrens@Sun.COM 			 * objset needs to be closed & reopened (to grow the
38749396SMatthew.Ahrens@Sun.COM 			 * objset_phys_t).  Suspend/resume the fs will do that.
38759396SMatthew.Ahrens@Sun.COM 			 */
387610298SMatthew.Ahrens@Sun.COM 			error = zfs_suspend_fs(zfsvfs);
387710298SMatthew.Ahrens@Sun.COM 			if (error == 0)
387810298SMatthew.Ahrens@Sun.COM 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
38799396SMatthew.Ahrens@Sun.COM 		}
38809396SMatthew.Ahrens@Sun.COM 		if (error == 0)
38819396SMatthew.Ahrens@Sun.COM 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
38829396SMatthew.Ahrens@Sun.COM 		VFS_RELE(zfsvfs->z_vfs);
38839396SMatthew.Ahrens@Sun.COM 	} else {
388410298SMatthew.Ahrens@Sun.COM 		/* XXX kind of reading contents without owning */
388510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
38869396SMatthew.Ahrens@Sun.COM 		if (error)
38879396SMatthew.Ahrens@Sun.COM 			return (error);
38889396SMatthew.Ahrens@Sun.COM 
38899396SMatthew.Ahrens@Sun.COM 		error = dmu_objset_userspace_upgrade(os);
389010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
38919396SMatthew.Ahrens@Sun.COM 	}
38929396SMatthew.Ahrens@Sun.COM 
38939396SMatthew.Ahrens@Sun.COM 	return (error);
38949396SMatthew.Ahrens@Sun.COM }
38959396SMatthew.Ahrens@Sun.COM 
38969396SMatthew.Ahrens@Sun.COM /*
38974543Smarks  * We don't want to have a hard dependency
38984543Smarks  * against some special symbols in sharefs
38995331Samw  * nfs, and smbsrv.  Determine them if needed when
39004543Smarks  * the first file system is shared.
39015331Samw  * Neither sharefs, nfs or smbsrv are unloadable modules.
39024543Smarks  */
39035331Samw int (*znfsexport_fs)(void *arg);
39044543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
39055331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
39065331Samw 
39075331Samw int zfs_nfsshare_inited;
39085331Samw int zfs_smbshare_inited;
39095331Samw 
39104543Smarks ddi_modhandle_t nfs_mod;
39114543Smarks ddi_modhandle_t sharefs_mod;
39125331Samw ddi_modhandle_t smbsrv_mod;
39134543Smarks kmutex_t zfs_share_lock;
39144543Smarks 
39154543Smarks static int
39165331Samw zfs_init_sharefs()
39175331Samw {
39185331Samw 	int error;
39195331Samw 
39205331Samw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
39215331Samw 	/* Both NFS and SMB shares also require sharetab support. */
39225331Samw 	if (sharefs_mod == NULL && ((sharefs_mod =
39235331Samw 	    ddi_modopen("fs/sharefs",
39245331Samw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
39255331Samw 		return (ENOSYS);
39265331Samw 	}
39275331Samw 	if (zshare_fs == NULL && ((zshare_fs =
39285331Samw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
39295331Samw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
39305331Samw 		return (ENOSYS);
39315331Samw 	}
39325331Samw 	return (0);
39335331Samw }
39345331Samw 
39355331Samw static int
39364543Smarks zfs_ioc_share(zfs_cmd_t *zc)
39374543Smarks {
39384543Smarks 	int error;
39394543Smarks 	int opcode;
39404543Smarks 
39415331Samw 	switch (zc->zc_share.z_sharetype) {
39425331Samw 	case ZFS_SHARE_NFS:
39435331Samw 	case ZFS_UNSHARE_NFS:
39445331Samw 		if (zfs_nfsshare_inited == 0) {
39455331Samw 			mutex_enter(&zfs_share_lock);
39465331Samw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
39475331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
39485331Samw 				mutex_exit(&zfs_share_lock);
39495331Samw 				return (ENOSYS);
39505331Samw 			}
39515331Samw 			if (znfsexport_fs == NULL &&
39525331Samw 			    ((znfsexport_fs = (int (*)(void *))
39535331Samw 			    ddi_modsym(nfs_mod,
39545331Samw 			    "nfs_export", &error)) == NULL)) {
39555331Samw 				mutex_exit(&zfs_share_lock);
39565331Samw 				return (ENOSYS);
39575331Samw 			}
39585331Samw 			error = zfs_init_sharefs();
39595331Samw 			if (error) {
39605331Samw 				mutex_exit(&zfs_share_lock);
39615331Samw 				return (ENOSYS);
39625331Samw 			}
39635331Samw 			zfs_nfsshare_inited = 1;
39644543Smarks 			mutex_exit(&zfs_share_lock);
39654543Smarks 		}
39665331Samw 		break;
39675331Samw 	case ZFS_SHARE_SMB:
39685331Samw 	case ZFS_UNSHARE_SMB:
39695331Samw 		if (zfs_smbshare_inited == 0) {
39705331Samw 			mutex_enter(&zfs_share_lock);
39715331Samw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
39725331Samw 			    ddi_modopen("drv/smbsrv",
39735331Samw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
39745331Samw 				mutex_exit(&zfs_share_lock);
39755331Samw 				return (ENOSYS);
39765331Samw 			}
39775331Samw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
39785331Samw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
39796139Sjb150015 			    "smb_server_share", &error)) == NULL)) {
39805331Samw 				mutex_exit(&zfs_share_lock);
39815331Samw 				return (ENOSYS);
39825331Samw 			}
39835331Samw 			error = zfs_init_sharefs();
39845331Samw 			if (error) {
39855331Samw 				mutex_exit(&zfs_share_lock);
39865331Samw 				return (ENOSYS);
39875331Samw 			}
39885331Samw 			zfs_smbshare_inited = 1;
39894543Smarks 			mutex_exit(&zfs_share_lock);
39904543Smarks 		}
39915331Samw 		break;
39925331Samw 	default:
39935331Samw 		return (EINVAL);
39944543Smarks 	}
39954543Smarks 
39965331Samw 	switch (zc->zc_share.z_sharetype) {
39975331Samw 	case ZFS_SHARE_NFS:
39985331Samw 	case ZFS_UNSHARE_NFS:
39995331Samw 		if (error =
40005331Samw 		    znfsexport_fs((void *)
40015331Samw 		    (uintptr_t)zc->zc_share.z_exportdata))
40025331Samw 			return (error);
40035331Samw 		break;
40045331Samw 	case ZFS_SHARE_SMB:
40055331Samw 	case ZFS_UNSHARE_SMB:
40065331Samw 		if (error = zsmbexport_fs((void *)
40075331Samw 		    (uintptr_t)zc->zc_share.z_exportdata,
40085331Samw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
40098845Samw@Sun.COM 		    B_TRUE: B_FALSE)) {
40105331Samw 			return (error);
40115331Samw 		}
40125331Samw 		break;
40135331Samw 	}
40145331Samw 
40155331Samw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
40165331Samw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
40174543Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
40184543Smarks 
40195331Samw 	/*
40205331Samw 	 * Add or remove share from sharetab
40215331Samw 	 */
40224543Smarks 	error = zshare_fs(opcode,
40234543Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
40244543Smarks 	    zc->zc_share.z_sharemax);
40254543Smarks 
40264543Smarks 	return (error);
40274543Smarks 
40284543Smarks }
40294543Smarks 
40308845Samw@Sun.COM ace_t full_access[] = {
40318845Samw@Sun.COM 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
40328845Samw@Sun.COM };
40338845Samw@Sun.COM 
40348845Samw@Sun.COM /*
40358845Samw@Sun.COM  * Remove all ACL files in shares dir
40368845Samw@Sun.COM  */
40378845Samw@Sun.COM static int
40388845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp)
40398845Samw@Sun.COM {
40408845Samw@Sun.COM 	zap_cursor_t	zc;
40418845Samw@Sun.COM 	zap_attribute_t	zap;
40428845Samw@Sun.COM 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
40438845Samw@Sun.COM 	int error;
40448845Samw@Sun.COM 
40458845Samw@Sun.COM 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
40468845Samw@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
40478845Samw@Sun.COM 	    zap_cursor_advance(&zc)) {
40488845Samw@Sun.COM 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
40498845Samw@Sun.COM 		    NULL, 0)) != 0)
40508845Samw@Sun.COM 			break;
40518845Samw@Sun.COM 	}
40528845Samw@Sun.COM 	zap_cursor_fini(&zc);
40538845Samw@Sun.COM 	return (error);
40548845Samw@Sun.COM }
40558845Samw@Sun.COM 
40568845Samw@Sun.COM static int
40578845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc)
40588845Samw@Sun.COM {
40598845Samw@Sun.COM 	vnode_t *vp;
40608845Samw@Sun.COM 	znode_t *dzp;
40618845Samw@Sun.COM 	vnode_t *resourcevp = NULL;
40628845Samw@Sun.COM 	znode_t *sharedir;
40638845Samw@Sun.COM 	zfsvfs_t *zfsvfs;
40648845Samw@Sun.COM 	nvlist_t *nvlist;
40658845Samw@Sun.COM 	char *src, *target;
40668845Samw@Sun.COM 	vattr_t vattr;
40678845Samw@Sun.COM 	vsecattr_t vsec;
40688845Samw@Sun.COM 	int error = 0;
40698845Samw@Sun.COM 
40708845Samw@Sun.COM 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
40718845Samw@Sun.COM 	    NO_FOLLOW, NULL, &vp)) != 0)
40728845Samw@Sun.COM 		return (error);
40738845Samw@Sun.COM 
40748845Samw@Sun.COM 	/* Now make sure mntpnt and dataset are ZFS */
40758845Samw@Sun.COM 
40768845Samw@Sun.COM 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
40778845Samw@Sun.COM 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
40788845Samw@Sun.COM 	    zc->zc_name) != 0)) {
40798845Samw@Sun.COM 		VN_RELE(vp);
40808845Samw@Sun.COM 		return (EINVAL);
40818845Samw@Sun.COM 	}
40828845Samw@Sun.COM 
40838845Samw@Sun.COM 	dzp = VTOZ(vp);
40848845Samw@Sun.COM 	zfsvfs = dzp->z_zfsvfs;
40858845Samw@Sun.COM 	ZFS_ENTER(zfsvfs);
40868845Samw@Sun.COM 
40879030SMark.Shellenbaum@Sun.COM 	/*
40889030SMark.Shellenbaum@Sun.COM 	 * Create share dir if its missing.
40899030SMark.Shellenbaum@Sun.COM 	 */
40909030SMark.Shellenbaum@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
40919030SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_shares_dir == 0) {
40929030SMark.Shellenbaum@Sun.COM 		dmu_tx_t *tx;
40939030SMark.Shellenbaum@Sun.COM 
40949030SMark.Shellenbaum@Sun.COM 		tx = dmu_tx_create(zfsvfs->z_os);
40959030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
40969030SMark.Shellenbaum@Sun.COM 		    ZFS_SHARES_DIR);
40979030SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
40989030SMark.Shellenbaum@Sun.COM 		error = dmu_tx_assign(tx, TXG_WAIT);
40999030SMark.Shellenbaum@Sun.COM 		if (error) {
41009030SMark.Shellenbaum@Sun.COM 			dmu_tx_abort(tx);
41019030SMark.Shellenbaum@Sun.COM 		} else {
41029030SMark.Shellenbaum@Sun.COM 			error = zfs_create_share_dir(zfsvfs, tx);
41039030SMark.Shellenbaum@Sun.COM 			dmu_tx_commit(tx);
41049030SMark.Shellenbaum@Sun.COM 		}
41059030SMark.Shellenbaum@Sun.COM 		if (error) {
41069030SMark.Shellenbaum@Sun.COM 			mutex_exit(&zfsvfs->z_lock);
41079030SMark.Shellenbaum@Sun.COM 			VN_RELE(vp);
41089030SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
41099030SMark.Shellenbaum@Sun.COM 			return (error);
41109030SMark.Shellenbaum@Sun.COM 		}
41119030SMark.Shellenbaum@Sun.COM 	}
41129030SMark.Shellenbaum@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
41139030SMark.Shellenbaum@Sun.COM 
41149030SMark.Shellenbaum@Sun.COM 	ASSERT(zfsvfs->z_shares_dir);
41158845Samw@Sun.COM 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
41169030SMark.Shellenbaum@Sun.COM 		VN_RELE(vp);
41178845Samw@Sun.COM 		ZFS_EXIT(zfsvfs);
41188845Samw@Sun.COM 		return (error);
41198845Samw@Sun.COM 	}
41208845Samw@Sun.COM 
41218845Samw@Sun.COM 	switch (zc->zc_cookie) {
41228845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
41238845Samw@Sun.COM 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
41248845Samw@Sun.COM 		vattr.va_type = VREG;
41258845Samw@Sun.COM 		vattr.va_mode = S_IFREG|0777;
41268845Samw@Sun.COM 		vattr.va_uid = 0;
41278845Samw@Sun.COM 		vattr.va_gid = 0;
41288845Samw@Sun.COM 
41298845Samw@Sun.COM 		vsec.vsa_mask = VSA_ACE;
41308845Samw@Sun.COM 		vsec.vsa_aclentp = &full_access;
41318845Samw@Sun.COM 		vsec.vsa_aclentsz = sizeof (full_access);
41328845Samw@Sun.COM 		vsec.vsa_aclcnt = 1;
41338845Samw@Sun.COM 
41348845Samw@Sun.COM 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
41358845Samw@Sun.COM 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
41368845Samw@Sun.COM 		if (resourcevp)
41378845Samw@Sun.COM 			VN_RELE(resourcevp);
41388845Samw@Sun.COM 		break;
41398845Samw@Sun.COM 
41408845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
41418845Samw@Sun.COM 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
41428845Samw@Sun.COM 		    NULL, 0);
41438845Samw@Sun.COM 		break;
41448845Samw@Sun.COM 
41458845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
41468845Samw@Sun.COM 		if ((error = get_nvlist(zc->zc_nvlist_src,
41479643SEric.Taylor@Sun.COM 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
41488845Samw@Sun.COM 			VN_RELE(vp);
41498845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
41508845Samw@Sun.COM 			return (error);
41518845Samw@Sun.COM 		}
41528845Samw@Sun.COM 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
41538845Samw@Sun.COM 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
41548845Samw@Sun.COM 		    &target)) {
41558845Samw@Sun.COM 			VN_RELE(vp);
41569179SMark.Shellenbaum@Sun.COM 			VN_RELE(ZTOV(sharedir));
41578845Samw@Sun.COM 			ZFS_EXIT(zfsvfs);
415811422SMark.Musante@Sun.COM 			nvlist_free(nvlist);
41598845Samw@Sun.COM 			return (error);
41608845Samw@Sun.COM 		}
41618845Samw@Sun.COM 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
41628845Samw@Sun.COM 		    kcred, NULL, 0);
41638845Samw@Sun.COM 		nvlist_free(nvlist);
41648845Samw@Sun.COM 		break;
41658845Samw@Sun.COM 
41668845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
41678845Samw@Sun.COM 		error = zfs_smb_acl_purge(sharedir);
41688845Samw@Sun.COM 		break;
41698845Samw@Sun.COM 
41708845Samw@Sun.COM 	default:
41718845Samw@Sun.COM 		error = EINVAL;
41728845Samw@Sun.COM 		break;
41738845Samw@Sun.COM 	}
41748845Samw@Sun.COM 
41758845Samw@Sun.COM 	VN_RELE(vp);
41768845Samw@Sun.COM 	VN_RELE(ZTOV(sharedir));
41778845Samw@Sun.COM 
41788845Samw@Sun.COM 	ZFS_EXIT(zfsvfs);
41798845Samw@Sun.COM 
41808845Samw@Sun.COM 	return (error);
41818845Samw@Sun.COM }
41828845Samw@Sun.COM 
41834543Smarks /*
418410242Schris.kirby@sun.com  * inputs:
418510242Schris.kirby@sun.com  * zc_name	name of filesystem
418610242Schris.kirby@sun.com  * zc_value	short name of snap
418710242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
418810242Schris.kirby@sun.com  * zc_cookie	recursive flag
418910342Schris.kirby@sun.com  * zc_temphold	set if hold is temporary
419010242Schris.kirby@sun.com  *
419110242Schris.kirby@sun.com  * outputs:		none
419210242Schris.kirby@sun.com  */
419310242Schris.kirby@sun.com static int
419410242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc)
419510242Schris.kirby@sun.com {
419610242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
419710242Schris.kirby@sun.com 
419810242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
419910242Schris.kirby@sun.com 		return (EINVAL);
420010242Schris.kirby@sun.com 
420110242Schris.kirby@sun.com 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
420210342Schris.kirby@sun.com 	    zc->zc_string, recursive, zc->zc_temphold));
420310242Schris.kirby@sun.com }
420410242Schris.kirby@sun.com 
420510242Schris.kirby@sun.com /*
420610242Schris.kirby@sun.com  * inputs:
420710242Schris.kirby@sun.com  * zc_name	name of dataset from which we're releasing a user reference
420810242Schris.kirby@sun.com  * zc_value	short name of snap
420910242Schris.kirby@sun.com  * zc_string	user-supplied tag for this reference
421010242Schris.kirby@sun.com  * zc_cookie	recursive flag
421110242Schris.kirby@sun.com  *
421210242Schris.kirby@sun.com  * outputs:		none
421310242Schris.kirby@sun.com  */
421410242Schris.kirby@sun.com static int
421510242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc)
421610242Schris.kirby@sun.com {
421710242Schris.kirby@sun.com 	boolean_t recursive = zc->zc_cookie;
421810242Schris.kirby@sun.com 
421910242Schris.kirby@sun.com 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
422010242Schris.kirby@sun.com 		return (EINVAL);
422110242Schris.kirby@sun.com 
422210242Schris.kirby@sun.com 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
422310242Schris.kirby@sun.com 	    zc->zc_string, recursive));
422410242Schris.kirby@sun.com }
422510242Schris.kirby@sun.com 
422610242Schris.kirby@sun.com /*
422710242Schris.kirby@sun.com  * inputs:
422810242Schris.kirby@sun.com  * zc_name		name of filesystem
422910242Schris.kirby@sun.com  *
423010242Schris.kirby@sun.com  * outputs:
423110242Schris.kirby@sun.com  * zc_nvlist_src{_size}	nvlist of snapshot holds
423210242Schris.kirby@sun.com  */
423310242Schris.kirby@sun.com static int
423410242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc)
423510242Schris.kirby@sun.com {
423610242Schris.kirby@sun.com 	nvlist_t *nvp;
423710242Schris.kirby@sun.com 	int error;
423810242Schris.kirby@sun.com 
423910242Schris.kirby@sun.com 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
424010242Schris.kirby@sun.com 		error = put_nvlist(zc, nvp);
424110242Schris.kirby@sun.com 		nvlist_free(nvp);
424210242Schris.kirby@sun.com 	}
424310242Schris.kirby@sun.com 
424410242Schris.kirby@sun.com 	return (error);
424510242Schris.kirby@sun.com }
424610242Schris.kirby@sun.com 
424710242Schris.kirby@sun.com /*
42484988Sek110237  * pool create, destroy, and export don't log the history as part of
42494988Sek110237  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
42504988Sek110237  * do the logging of those commands.
42514543Smarks  */
4252789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
42539234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
42549234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42559234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
42569234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42579234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42589234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42599234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
42609234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42619234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
42629234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42639234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
42649234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42659234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
42669234SGeorge.Wilson@Sun.COM 	    B_FALSE },
4267*12296SLin.Ling@Sun.COM 	{ zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42689234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42699234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
42709234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42719234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
42729234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42739234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
42749234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42759234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42769234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42779234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42789234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42799234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
42809234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42819234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42829234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42839234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
42849234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42859234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
42869234SGeorge.Wilson@Sun.COM 	    B_TRUE },
42879425SEric.Schrock@Sun.COM 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
42889425SEric.Schrock@Sun.COM 	    B_TRUE },
42899234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
429011454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
42919234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
42929234SGeorge.Wilson@Sun.COM 	    B_FALSE },
42939234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
429411454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
42959234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
429611454SSanjeev.Bagewadi@Sun.COM 	    B_TRUE },
42979234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
42989234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
42999234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
43009234SGeorge.Wilson@Sun.COM 	    B_TRUE},
43019234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
43029234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43039234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
43049234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
43059234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
43069234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
43079234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43089234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
43099234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43109234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
43119234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43129234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
43139234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43149234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
43159234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
43169234SGeorge.Wilson@Sun.COM 	    B_TRUE },
431711314Swilliam.gorrell@sun.com 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
431811314Swilliam.gorrell@sun.com 	    B_TRUE, B_TRUE },
43199234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
43209234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43219234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
43229234SGeorge.Wilson@Sun.COM 	    B_FALSE },
432310233SGeorge.Wilson@Sun.COM 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
432410233SGeorge.Wilson@Sun.COM 	    B_TRUE },
43259234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
43269234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43279234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
43289234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43299234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
43309234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43319234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
43329234SGeorge.Wilson@Sun.COM 	    B_FALSE },
43339234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
43349234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
43359234SGeorge.Wilson@Sun.COM 	    B_TRUE },
43369234SGeorge.Wilson@Sun.COM 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
43379396SMatthew.Ahrens@Sun.COM 	    B_FALSE },
43389396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
43399396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
43409396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
43419396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_FALSE },
43429396SMatthew.Ahrens@Sun.COM 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
43439396SMatthew.Ahrens@Sun.COM 	    DATASET_NAME, B_FALSE, B_TRUE },
434410242Schris.kirby@sun.com 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
434510242Schris.kirby@sun.com 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
434610242Schris.kirby@sun.com 	    B_TRUE },
434710242Schris.kirby@sun.com 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
434811022STom.Erickson@Sun.COM 	    B_TRUE },
434911022STom.Erickson@Sun.COM 	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
435011422SMark.Musante@Sun.COM 	    B_FALSE },
435111422SMark.Musante@Sun.COM 	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
435211422SMark.Musante@Sun.COM 	    B_TRUE }
4353789Sahrens };
4354789Sahrens 
43559234SGeorge.Wilson@Sun.COM int
43569234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type)
43579234SGeorge.Wilson@Sun.COM {
43589234SGeorge.Wilson@Sun.COM 	spa_t *spa;
43599234SGeorge.Wilson@Sun.COM 	int error;
43609234SGeorge.Wilson@Sun.COM 
43619234SGeorge.Wilson@Sun.COM 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
43629234SGeorge.Wilson@Sun.COM 
43639396SMatthew.Ahrens@Sun.COM 	error = spa_open(name, &spa, FTAG);
43649234SGeorge.Wilson@Sun.COM 	if (error == 0) {
43659234SGeorge.Wilson@Sun.COM 		if (spa_suspended(spa))
43669234SGeorge.Wilson@Sun.COM 			error = EAGAIN;
43679234SGeorge.Wilson@Sun.COM 		spa_close(spa, FTAG);
43689234SGeorge.Wilson@Sun.COM 	}
43699234SGeorge.Wilson@Sun.COM 	return (error);
43709234SGeorge.Wilson@Sun.COM }
43719234SGeorge.Wilson@Sun.COM 
4372789Sahrens static int
4373789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4374789Sahrens {
4375789Sahrens 	zfs_cmd_t *zc;
4376789Sahrens 	uint_t vec;
43772199Sahrens 	int error, rc;
4378789Sahrens 
4379789Sahrens 	if (getminor(dev) != 0)
4380789Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4381789Sahrens 
4382789Sahrens 	vec = cmd - ZFS_IOC;
43834787Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4384789Sahrens 
4385789Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4386789Sahrens 		return (EINVAL);
4387789Sahrens 
4388789Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4389789Sahrens 
43909643SEric.Taylor@Sun.COM 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
439111807SSam.Falkner@Sun.COM 	if (error != 0)
439211807SSam.Falkner@Sun.COM 		error = EFAULT;
4393789Sahrens 
439410588SEric.Taylor@Sun.COM 	if ((error == 0) && !(flag & FKIOCTL))
43954543Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4396789Sahrens 
4397789Sahrens 	/*
4398789Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
4399789Sahrens 	 * the lower layers.
4400789Sahrens 	 */
4401789Sahrens 	if (error == 0) {
4402789Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
44039643SEric.Taylor@Sun.COM 		zc->zc_iflags = flag & FKIOCTL;
4404789Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
44054577Sahrens 		case POOL_NAME:
4406789Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4407789Sahrens 				error = EINVAL;
44089234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
44099234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
44109234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4411789Sahrens 			break;
4412789Sahrens 
44134577Sahrens 		case DATASET_NAME:
4414789Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4415789Sahrens 				error = EINVAL;
44169234SGeorge.Wilson@Sun.COM 			if (zfs_ioc_vec[vec].zvec_pool_check)
44179234SGeorge.Wilson@Sun.COM 				error = pool_status_check(zc->zc_name,
44189234SGeorge.Wilson@Sun.COM 				    zfs_ioc_vec[vec].zvec_namecheck);
4419789Sahrens 			break;
44202856Snd150628 
44214577Sahrens 		case NO_NAME:
44222856Snd150628 			break;
4423789Sahrens 		}
4424789Sahrens 	}
4425789Sahrens 
4426789Sahrens 	if (error == 0)
4427789Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
4428789Sahrens 
44299643SEric.Taylor@Sun.COM 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
44304543Smarks 	if (error == 0) {
443111807SSam.Falkner@Sun.COM 		if (rc != 0)
443211807SSam.Falkner@Sun.COM 			error = EFAULT;
44339396SMatthew.Ahrens@Sun.COM 		if (zfs_ioc_vec[vec].zvec_his_log)
44344543Smarks 			zfs_log_history(zc);
44354543Smarks 	}
4436789Sahrens 
4437789Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
4438789Sahrens 	return (error);
4439789Sahrens }
4440789Sahrens 
4441789Sahrens static int
4442789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4443789Sahrens {
4444789Sahrens 	if (cmd != DDI_ATTACH)
4445789Sahrens 		return (DDI_FAILURE);
4446789Sahrens 
4447789Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4448789Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
4449789Sahrens 		return (DDI_FAILURE);
4450789Sahrens 
4451789Sahrens 	zfs_dip = dip;
4452789Sahrens 
4453789Sahrens 	ddi_report_dev(dip);
4454789Sahrens 
4455789Sahrens 	return (DDI_SUCCESS);
4456789Sahrens }
4457789Sahrens 
4458789Sahrens static int
4459789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4460789Sahrens {
4461789Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
4462789Sahrens 		return (DDI_FAILURE);
4463789Sahrens 
4464789Sahrens 	if (cmd != DDI_DETACH)
4465789Sahrens 		return (DDI_FAILURE);
4466789Sahrens 
4467789Sahrens 	zfs_dip = NULL;
4468789Sahrens 
4469789Sahrens 	ddi_prop_remove_all(dip);
4470789Sahrens 	ddi_remove_minor_node(dip, NULL);
4471789Sahrens 
4472789Sahrens 	return (DDI_SUCCESS);
4473789Sahrens }
4474789Sahrens 
4475789Sahrens /*ARGSUSED*/
4476789Sahrens static int
4477789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4478789Sahrens {
4479789Sahrens 	switch (infocmd) {
4480789Sahrens 	case DDI_INFO_DEVT2DEVINFO:
4481789Sahrens 		*result = zfs_dip;
4482789Sahrens 		return (DDI_SUCCESS);
4483789Sahrens 
4484789Sahrens 	case DDI_INFO_DEVT2INSTANCE:
4485849Sbonwick 		*result = (void *)0;
4486789Sahrens 		return (DDI_SUCCESS);
4487789Sahrens 	}
4488789Sahrens 
4489789Sahrens 	return (DDI_FAILURE);
4490789Sahrens }
4491789Sahrens 
4492789Sahrens /*
4493789Sahrens  * OK, so this is a little weird.
4494789Sahrens  *
4495789Sahrens  * /dev/zfs is the control node, i.e. minor 0.
4496789Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
4497789Sahrens  *
4498789Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
4499789Sahrens  * so most of the standard driver entry points are in zvol.c.
4500789Sahrens  */
4501789Sahrens static struct cb_ops zfs_cb_ops = {
4502789Sahrens 	zvol_open,	/* open */
4503789Sahrens 	zvol_close,	/* close */
4504789Sahrens 	zvol_strategy,	/* strategy */
4505789Sahrens 	nodev,		/* print */
45066423Sgw25295 	zvol_dump,	/* dump */
4507789Sahrens 	zvol_read,	/* read */
4508789Sahrens 	zvol_write,	/* write */
4509789Sahrens 	zfsdev_ioctl,	/* ioctl */
4510789Sahrens 	nodev,		/* devmap */
4511789Sahrens 	nodev,		/* mmap */
4512789Sahrens 	nodev,		/* segmap */
4513789Sahrens 	nochpoll,	/* poll */
4514789Sahrens 	ddi_prop_op,	/* prop_op */
4515789Sahrens 	NULL,		/* streamtab */
4516789Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
4517789Sahrens 	CB_REV,		/* version */
45183638Sbillm 	nodev,		/* async read */
45193638Sbillm 	nodev,		/* async write */
4520789Sahrens };
4521789Sahrens 
4522789Sahrens static struct dev_ops zfs_dev_ops = {
4523789Sahrens 	DEVO_REV,	/* version */
4524789Sahrens 	0,		/* refcnt */
4525789Sahrens 	zfs_info,	/* info */
4526789Sahrens 	nulldev,	/* identify */
4527789Sahrens 	nulldev,	/* probe */
4528789Sahrens 	zfs_attach,	/* attach */
4529789Sahrens 	zfs_detach,	/* detach */
4530789Sahrens 	nodev,		/* reset */
4531789Sahrens 	&zfs_cb_ops,	/* driver operations */
45327656SSherry.Moore@Sun.COM 	NULL,		/* no bus operations */
45337656SSherry.Moore@Sun.COM 	NULL,		/* power */
45347656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
4535789Sahrens };
4536789Sahrens 
4537789Sahrens static struct modldrv zfs_modldrv = {
45387656SSherry.Moore@Sun.COM 	&mod_driverops,
45397656SSherry.Moore@Sun.COM 	"ZFS storage pool",
45407656SSherry.Moore@Sun.COM 	&zfs_dev_ops
4541789Sahrens };
4542789Sahrens 
4543789Sahrens static struct modlinkage modlinkage = {
4544789Sahrens 	MODREV_1,
4545789Sahrens 	(void *)&zfs_modlfs,
4546789Sahrens 	(void *)&zfs_modldrv,
4547789Sahrens 	NULL
4548789Sahrens };
4549789Sahrens 
45504720Sfr157268 
45514720Sfr157268 uint_t zfs_fsyncer_key;
45525326Sek110237 extern uint_t rrw_tsd_key;
45534720Sfr157268 
4554789Sahrens int
4555789Sahrens _init(void)
4556789Sahrens {
4557789Sahrens 	int error;
4558789Sahrens 
4559849Sbonwick 	spa_init(FREAD | FWRITE);
4560849Sbonwick 	zfs_init();
4561849Sbonwick 	zvol_init();
4562849Sbonwick 
4563849Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
4564849Sbonwick 		zvol_fini();
4565849Sbonwick 		zfs_fini();
4566849Sbonwick 		spa_fini();
4567789Sahrens 		return (error);
4568849Sbonwick 	}
4569789Sahrens 
45704720Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
45715326Sek110237 	tsd_create(&rrw_tsd_key, NULL);
45724720Sfr157268 
4573789Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
4574789Sahrens 	ASSERT(error == 0);
45754543Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
4576789Sahrens 
4577789Sahrens 	return (0);
4578789Sahrens }
4579789Sahrens 
4580789Sahrens int
4581789Sahrens _fini(void)
4582789Sahrens {
4583789Sahrens 	int error;
4584789Sahrens 
45851544Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
4586789Sahrens 		return (EBUSY);
4587789Sahrens 
4588789Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
4589789Sahrens 		return (error);
4590789Sahrens 
4591789Sahrens 	zvol_fini();
4592789Sahrens 	zfs_fini();
4593789Sahrens 	spa_fini();
45945331Samw 	if (zfs_nfsshare_inited)
45954543Smarks 		(void) ddi_modclose(nfs_mod);
45965331Samw 	if (zfs_smbshare_inited)
45975331Samw 		(void) ddi_modclose(smbsrv_mod);
45985331Samw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
45994543Smarks 		(void) ddi_modclose(sharefs_mod);
4600789Sahrens 
46014720Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
4602789Sahrens 	ldi_ident_release(zfs_li);
4603789Sahrens 	zfs_li = NULL;
46044543Smarks 	mutex_destroy(&zfs_share_lock);
4605789Sahrens 
4606789Sahrens 	return (error);
4607789Sahrens }
4608789Sahrens 
4609789Sahrens int
4610789Sahrens _info(struct modinfo *modinfop)
4611789Sahrens {
4612789Sahrens 	return (mod_info(&modlinkage, modinfop));
4613789Sahrens }
4614