xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 503ad85c168c7992ccc310af845a581cff3c72b5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22379c004dSEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/types.h>
27fa9e4066Sahrens #include <sys/param.h>
28fa9e4066Sahrens #include <sys/errno.h>
29fa9e4066Sahrens #include <sys/uio.h>
30fa9e4066Sahrens #include <sys/buf.h>
31fa9e4066Sahrens #include <sys/modctl.h>
32fa9e4066Sahrens #include <sys/open.h>
33fa9e4066Sahrens #include <sys/file.h>
34fa9e4066Sahrens #include <sys/kmem.h>
35fa9e4066Sahrens #include <sys/conf.h>
36fa9e4066Sahrens #include <sys/cmn_err.h>
37fa9e4066Sahrens #include <sys/stat.h>
38fa9e4066Sahrens #include <sys/zfs_ioctl.h>
39da6c28aaSamw #include <sys/zfs_znode.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/spa.h>
42b1b8ab34Slling #include <sys/spa_impl.h>
43fa9e4066Sahrens #include <sys/vdev.h>
44b1b8ab34Slling #include <sys/vdev_impl.h>
45fa9e4066Sahrens #include <sys/dmu.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_prop.h>
49ecd6cf80Smarks #include <sys/dsl_deleg.h>
50ecd6cf80Smarks #include <sys/dmu_objset.h>
51fa9e4066Sahrens #include <sys/ddi.h>
52fa9e4066Sahrens #include <sys/sunddi.h>
53fa9e4066Sahrens #include <sys/sunldi.h>
54fa9e4066Sahrens #include <sys/policy.h>
55fa9e4066Sahrens #include <sys/zone.h>
56fa9e4066Sahrens #include <sys/nvpair.h>
57fa9e4066Sahrens #include <sys/pathname.h>
58fa9e4066Sahrens #include <sys/mount.h>
59fa9e4066Sahrens #include <sys/sdt.h>
60fa9e4066Sahrens #include <sys/fs/zfs.h>
61fa9e4066Sahrens #include <sys/zfs_ctldir.h>
62da6c28aaSamw #include <sys/zfs_dir.h>
63a2eea2e1Sahrens #include <sys/zvol.h>
64ecd6cf80Smarks #include <sharefs/share.h>
65f18faf3fSek #include <sys/dmu_objset.h>
66fa9e4066Sahrens 
67fa9e4066Sahrens #include "zfs_namecheck.h"
68e9dbad6fSeschrock #include "zfs_prop.h"
69ecd6cf80Smarks #include "zfs_deleg.h"
70fa9e4066Sahrens 
71fa9e4066Sahrens extern struct modlfs zfs_modlfs;
72fa9e4066Sahrens 
73fa9e4066Sahrens extern void zfs_init(void);
74fa9e4066Sahrens extern void zfs_fini(void);
75fa9e4066Sahrens 
76fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
77fa9e4066Sahrens dev_info_t *zfs_dip;
78fa9e4066Sahrens 
79fa9e4066Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
80ecd6cf80Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
81fa9e4066Sahrens 
8254d692b7SGeorge Wilson typedef enum {
8354d692b7SGeorge Wilson 	NO_NAME,
8454d692b7SGeorge Wilson 	POOL_NAME,
8554d692b7SGeorge Wilson 	DATASET_NAME
8654d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
8754d692b7SGeorge Wilson 
88fa9e4066Sahrens typedef struct zfs_ioc_vec {
89fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
90fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
9154d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
92ecd6cf80Smarks 	boolean_t		zvec_his_log;
9354d692b7SGeorge Wilson 	boolean_t		zvec_pool_check;
94fa9e4066Sahrens } zfs_ioc_vec_t;
95fa9e4066Sahrens 
9614843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
9714843421SMatthew Ahrens static const char *userquota_perms[] = {
9814843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
9914843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
10014843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
10114843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
10214843421SMatthew Ahrens };
10314843421SMatthew Ahrens 
10414843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
1056e77af0aSDavid Pacheco static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
1060a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1070a48a24eStimh     boolean_t *);
1080a48a24eStimh int zfs_set_prop_nvlist(const char *, nvlist_t *);
1090a48a24eStimh 
110fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
111fa9e4066Sahrens void
112fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
113fa9e4066Sahrens {
114fa9e4066Sahrens 	const char *newfile;
115fa9e4066Sahrens 	char buf[256];
116fa9e4066Sahrens 	va_list adx;
117fa9e4066Sahrens 
118fa9e4066Sahrens 	/*
119fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
120fa9e4066Sahrens 	 */
121fa9e4066Sahrens 	newfile = strrchr(file, '/');
122fa9e4066Sahrens 	if (newfile != NULL) {
123fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
124fa9e4066Sahrens 	} else {
125fa9e4066Sahrens 		newfile = file;
126fa9e4066Sahrens 	}
127fa9e4066Sahrens 
128fa9e4066Sahrens 	va_start(adx, fmt);
129fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
130fa9e4066Sahrens 	va_end(adx);
131fa9e4066Sahrens 
132fa9e4066Sahrens 	/*
133fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
134fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
135fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
136fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
137fa9e4066Sahrens 	 * arg0 = file name
138fa9e4066Sahrens 	 * arg1 = function name
139fa9e4066Sahrens 	 * arg2 = line number
140fa9e4066Sahrens 	 * arg3 = message
141fa9e4066Sahrens 	 */
142fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
143fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
144fa9e4066Sahrens }
145fa9e4066Sahrens 
146ecd6cf80Smarks static void
147228975ccSek history_str_free(char *buf)
148228975ccSek {
149228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
150228975ccSek }
151228975ccSek 
152228975ccSek static char *
153228975ccSek history_str_get(zfs_cmd_t *zc)
154ecd6cf80Smarks {
15540feaa91Sahrens 	char *buf;
156ecd6cf80Smarks 
157ecd6cf80Smarks 	if (zc->zc_history == NULL)
158228975ccSek 		return (NULL);
159e7437265Sahrens 
160ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
161ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
162ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
163228975ccSek 		history_str_free(buf);
164228975ccSek 		return (NULL);
165ecd6cf80Smarks 	}
166ecd6cf80Smarks 
167ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
168ecd6cf80Smarks 
169228975ccSek 	return (buf);
170228975ccSek }
171ecd6cf80Smarks 
17215e6edf1Sgw /*
17315e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
17415e6edf1Sgw  */
17515e6edf1Sgw static boolean_t
17615e6edf1Sgw zfs_is_bootfs(const char *name)
17715e6edf1Sgw {
178*503ad85cSMatthew Ahrens 	objset_t *os;
17915e6edf1Sgw 
180*503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
181*503ad85cSMatthew Ahrens 		boolean_t ret;
182*503ad85cSMatthew Ahrens 		ret = (dmu_objset_id(os) == dmu_objset_spa(os)->spa_bootfs);
183*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
184*503ad85cSMatthew Ahrens 		return (ret);
18515e6edf1Sgw 	}
186*503ad85cSMatthew Ahrens 	return (B_FALSE);
18715e6edf1Sgw }
18815e6edf1Sgw 
189c2a93d44Stimh /*
1900a48a24eStimh  * zfs_earlier_version
191c2a93d44Stimh  *
192c2a93d44Stimh  *	Return non-zero if the spa version is less than requested version.
193c2a93d44Stimh  */
194da6c28aaSamw static int
1950a48a24eStimh zfs_earlier_version(const char *name, int version)
196da6c28aaSamw {
197da6c28aaSamw 	spa_t *spa;
198da6c28aaSamw 
199da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
200da6c28aaSamw 		if (spa_version(spa) < version) {
201da6c28aaSamw 			spa_close(spa, FTAG);
202da6c28aaSamw 			return (1);
203da6c28aaSamw 		}
204da6c28aaSamw 		spa_close(spa, FTAG);
205da6c28aaSamw 	}
206da6c28aaSamw 	return (0);
207da6c28aaSamw }
208da6c28aaSamw 
2099e6eda55Smarks /*
210745cd3c5Smaybee  * zpl_earlier_version
2119e6eda55Smarks  *
212745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
2139e6eda55Smarks  */
214745cd3c5Smaybee static boolean_t
215745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
2169e6eda55Smarks {
2179e6eda55Smarks 	objset_t *os;
218745cd3c5Smaybee 	boolean_t rc = B_TRUE;
2199e6eda55Smarks 
220*503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
221745cd3c5Smaybee 		uint64_t zplversion;
2229e6eda55Smarks 
223*503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
224*503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
225*503ad85cSMatthew Ahrens 			return (B_TRUE);
226*503ad85cSMatthew Ahrens 		}
227*503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
228745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
229745cd3c5Smaybee 			rc = zplversion < version;
230*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2319e6eda55Smarks 	}
2329e6eda55Smarks 	return (rc);
2339e6eda55Smarks }
2349e6eda55Smarks 
235228975ccSek static void
236228975ccSek zfs_log_history(zfs_cmd_t *zc)
237228975ccSek {
238228975ccSek 	spa_t *spa;
239228975ccSek 	char *buf;
240ecd6cf80Smarks 
241228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
242228975ccSek 		return;
243228975ccSek 
244228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
245228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
246228975ccSek 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
247228975ccSek 		spa_close(spa, FTAG);
248228975ccSek 	}
249228975ccSek 	history_str_free(buf);
250ecd6cf80Smarks }
251ecd6cf80Smarks 
252fa9e4066Sahrens /*
253fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
254fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
255fa9e4066Sahrens  */
256fa9e4066Sahrens /* ARGSUSED */
257fa9e4066Sahrens static int
258ecd6cf80Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
259fa9e4066Sahrens {
260fa9e4066Sahrens 	return (0);
261fa9e4066Sahrens }
262fa9e4066Sahrens 
263fa9e4066Sahrens /*
264fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
265fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
266fa9e4066Sahrens  */
267fa9e4066Sahrens /* ARGSUSED */
268fa9e4066Sahrens static int
269ecd6cf80Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
270fa9e4066Sahrens {
271fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
272ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
273fa9e4066Sahrens 		return (0);
274fa9e4066Sahrens 
275fa9e4066Sahrens 	return (ENOENT);
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens static int
279fa9e4066Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr)
280fa9e4066Sahrens {
281fa9e4066Sahrens 	uint64_t zoned;
282fa9e4066Sahrens 	int writable = 1;
283fa9e4066Sahrens 
284fa9e4066Sahrens 	/*
285fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
286fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
287fa9e4066Sahrens 	 */
288fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
289fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
290fa9e4066Sahrens 		return (ENOENT);
291fa9e4066Sahrens 
292fa9e4066Sahrens 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
293fa9e4066Sahrens 		return (ENOENT);
294fa9e4066Sahrens 
295fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
296fa9e4066Sahrens 		/*
297fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
298fa9e4066Sahrens 		 * global zone.
299fa9e4066Sahrens 		 */
300fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
301fa9e4066Sahrens 			return (EPERM);
302fa9e4066Sahrens 	} else {
303fa9e4066Sahrens 		/*
304fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
305fa9e4066Sahrens 		 */
306fa9e4066Sahrens 		if (!zoned)
307fa9e4066Sahrens 			return (EPERM);
308fa9e4066Sahrens 
309fa9e4066Sahrens 		/* must be writable by this zone */
310fa9e4066Sahrens 		if (!writable)
311fa9e4066Sahrens 			return (EPERM);
312fa9e4066Sahrens 	}
313fa9e4066Sahrens 	return (0);
314fa9e4066Sahrens }
315fa9e4066Sahrens 
316fa9e4066Sahrens int
317ecd6cf80Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
318fa9e4066Sahrens {
319fa9e4066Sahrens 	int error;
320fa9e4066Sahrens 
321ecd6cf80Smarks 	error = zfs_dozonecheck(name, cr);
322ecd6cf80Smarks 	if (error == 0) {
323ecd6cf80Smarks 		error = secpolicy_zfs(cr);
324db870a07Sahrens 		if (error)
325ecd6cf80Smarks 			error = dsl_deleg_access(name, perm, cr);
326ecd6cf80Smarks 	}
327ecd6cf80Smarks 	return (error);
328ecd6cf80Smarks }
329ecd6cf80Smarks 
330ecd6cf80Smarks static int
331ecd6cf80Smarks zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
332ecd6cf80Smarks {
333ecd6cf80Smarks 	/*
334ecd6cf80Smarks 	 * Check permissions for special properties.
335ecd6cf80Smarks 	 */
336ecd6cf80Smarks 	switch (prop) {
337ecd6cf80Smarks 	case ZFS_PROP_ZONED:
338ecd6cf80Smarks 		/*
339ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
340ecd6cf80Smarks 		 */
341ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
342ecd6cf80Smarks 			return (EPERM);
343ecd6cf80Smarks 		break;
344ecd6cf80Smarks 
345ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
346ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
347ecd6cf80Smarks 			uint64_t zoned;
348ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
349ecd6cf80Smarks 			/*
350ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
351ecd6cf80Smarks 			 * quota on things *under* (ie. contained by)
352ecd6cf80Smarks 			 * the thing they own.
353ecd6cf80Smarks 			 */
354ecd6cf80Smarks 			if (dsl_prop_get_integer(name, "zoned", &zoned,
355ecd6cf80Smarks 			    setpoint))
356ecd6cf80Smarks 				return (EPERM);
357db870a07Sahrens 			if (!zoned || strlen(name) <= strlen(setpoint))
358ecd6cf80Smarks 				return (EPERM);
359ecd6cf80Smarks 		}
360db870a07Sahrens 		break;
361ecd6cf80Smarks 	}
362ecd6cf80Smarks 
36391ebeef5Sahrens 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
364ecd6cf80Smarks }
365ecd6cf80Smarks 
366ecd6cf80Smarks int
367ecd6cf80Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
368ecd6cf80Smarks {
369ecd6cf80Smarks 	int error;
370ecd6cf80Smarks 
371ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
372ecd6cf80Smarks 	if (error)
373fa9e4066Sahrens 		return (error);
374fa9e4066Sahrens 
375ecd6cf80Smarks 	/*
376ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
377ecd6cf80Smarks 	 * dsl_deleg_can_allow()
378ecd6cf80Smarks 	 */
379ecd6cf80Smarks 	return (0);
380ecd6cf80Smarks }
381ecd6cf80Smarks 
382ecd6cf80Smarks int
383ecd6cf80Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
384ecd6cf80Smarks {
385ecd6cf80Smarks 	int error;
386ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
387ecd6cf80Smarks 	    ZFS_DELEG_PERM_ROLLBACK, cr);
388ecd6cf80Smarks 	if (error == 0)
389ecd6cf80Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
390ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
391ecd6cf80Smarks 	return (error);
392ecd6cf80Smarks }
393ecd6cf80Smarks 
394ecd6cf80Smarks int
395ecd6cf80Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
396ecd6cf80Smarks {
397ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
398ecd6cf80Smarks 	    ZFS_DELEG_PERM_SEND, cr));
399ecd6cf80Smarks }
400ecd6cf80Smarks 
401743a77edSAlan Wright static int
402743a77edSAlan Wright zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
403743a77edSAlan Wright {
404743a77edSAlan Wright 	vnode_t *vp;
405743a77edSAlan Wright 	int error;
406743a77edSAlan Wright 
407743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
408743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
409743a77edSAlan Wright 		return (error);
410743a77edSAlan Wright 
411743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
412743a77edSAlan Wright 
413743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
414743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
415743a77edSAlan Wright 	    zc->zc_name) != 0)) {
416743a77edSAlan Wright 		VN_RELE(vp);
417743a77edSAlan Wright 		return (EPERM);
418743a77edSAlan Wright 	}
419743a77edSAlan Wright 
420743a77edSAlan Wright 	VN_RELE(vp);
421743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
422743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
423743a77edSAlan Wright }
424743a77edSAlan Wright 
425ecd6cf80Smarks int
426ecd6cf80Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
427ecd6cf80Smarks {
428ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
429ecd6cf80Smarks 		return (EPERM);
430ecd6cf80Smarks 
4313cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
432ecd6cf80Smarks 		return (0);
433ecd6cf80Smarks 	} else {
434743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
435743a77edSAlan Wright 	}
436743a77edSAlan Wright }
437ecd6cf80Smarks 
438743a77edSAlan Wright int
439743a77edSAlan Wright zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
440743a77edSAlan Wright {
441743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
442743a77edSAlan Wright 		return (EPERM);
443ecd6cf80Smarks 
444743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
445743a77edSAlan Wright 		return (0);
446743a77edSAlan Wright 	} else {
447743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
448ecd6cf80Smarks 	}
449fa9e4066Sahrens }
450fa9e4066Sahrens 
451fa9e4066Sahrens static int
452ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
453fa9e4066Sahrens {
454fa9e4066Sahrens 	char *cp;
455fa9e4066Sahrens 
456fa9e4066Sahrens 	/*
457fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
458fa9e4066Sahrens 	 */
459ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
460ecd6cf80Smarks 	cp = strrchr(parent, '@');
461fa9e4066Sahrens 	if (cp != NULL) {
462fa9e4066Sahrens 		cp[0] = '\0';
463fa9e4066Sahrens 	} else {
464ecd6cf80Smarks 		cp = strrchr(parent, '/');
465fa9e4066Sahrens 		if (cp == NULL)
466fa9e4066Sahrens 			return (ENOENT);
467fa9e4066Sahrens 		cp[0] = '\0';
468ecd6cf80Smarks 	}
469ecd6cf80Smarks 
470ecd6cf80Smarks 	return (0);
471ecd6cf80Smarks }
472ecd6cf80Smarks 
473ecd6cf80Smarks int
474ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
475ecd6cf80Smarks {
476ecd6cf80Smarks 	int error;
477ecd6cf80Smarks 
478ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
479ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
480ecd6cf80Smarks 		return (error);
481ecd6cf80Smarks 
482ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
483ecd6cf80Smarks }
484ecd6cf80Smarks 
485ecd6cf80Smarks static int
486ecd6cf80Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
487ecd6cf80Smarks {
488ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
489ecd6cf80Smarks }
490ecd6cf80Smarks 
491ecd6cf80Smarks /*
492ecd6cf80Smarks  * Must have sys_config privilege to check the iscsi permission
493ecd6cf80Smarks  */
494ecd6cf80Smarks /* ARGSUSED */
495ecd6cf80Smarks static int
496ecd6cf80Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
497ecd6cf80Smarks {
498ecd6cf80Smarks 	return (secpolicy_zfs(cr));
499ecd6cf80Smarks }
500ecd6cf80Smarks 
501ecd6cf80Smarks int
502ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
503ecd6cf80Smarks {
504ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
505ecd6cf80Smarks 	int	error;
506ecd6cf80Smarks 
507ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
508ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
509ecd6cf80Smarks 		return (error);
510ecd6cf80Smarks 
511ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
512ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
513ecd6cf80Smarks 		return (error);
514ecd6cf80Smarks 
515ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
516ecd6cf80Smarks 	    sizeof (parentname))) != 0)
517ecd6cf80Smarks 		return (error);
518ecd6cf80Smarks 
519ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
520ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
521ecd6cf80Smarks 		return (error);
522ecd6cf80Smarks 
523ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
524ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
525ecd6cf80Smarks 		return (error);
526ecd6cf80Smarks 
527ecd6cf80Smarks 	return (error);
528ecd6cf80Smarks }
529ecd6cf80Smarks 
530ecd6cf80Smarks static int
531ecd6cf80Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
532ecd6cf80Smarks {
533ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
534ecd6cf80Smarks }
535ecd6cf80Smarks 
536ecd6cf80Smarks static int
537ecd6cf80Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
538ecd6cf80Smarks {
539ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
540ecd6cf80Smarks 	objset_t *clone;
541ecd6cf80Smarks 	int error;
542ecd6cf80Smarks 
543ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
544ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
545ecd6cf80Smarks 	if (error)
546ecd6cf80Smarks 		return (error);
547ecd6cf80Smarks 
548*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
549ecd6cf80Smarks 
550ecd6cf80Smarks 	if (error == 0) {
551ecd6cf80Smarks 		dsl_dataset_t *pclone = NULL;
552ecd6cf80Smarks 		dsl_dir_t *dd;
553*503ad85cSMatthew Ahrens 		dd = clone->os_dsl_dataset->ds_dir;
554ecd6cf80Smarks 
555ecd6cf80Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
556745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
557745cd3c5Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
558ecd6cf80Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
559ecd6cf80Smarks 		if (error) {
560*503ad85cSMatthew Ahrens 			dmu_objset_rele(clone, FTAG);
561ecd6cf80Smarks 			return (error);
562ecd6cf80Smarks 		}
563ecd6cf80Smarks 
564ecd6cf80Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
565ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
566ecd6cf80Smarks 
567ecd6cf80Smarks 		dsl_dataset_name(pclone, parentname);
568*503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
569745cd3c5Smaybee 		dsl_dataset_rele(pclone, FTAG);
570ecd6cf80Smarks 		if (error == 0)
571ecd6cf80Smarks 			error = zfs_secpolicy_write_perms(parentname,
572ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
573ecd6cf80Smarks 	}
574ecd6cf80Smarks 	return (error);
575ecd6cf80Smarks }
576ecd6cf80Smarks 
577ecd6cf80Smarks static int
578ecd6cf80Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
579ecd6cf80Smarks {
580ecd6cf80Smarks 	int error;
581ecd6cf80Smarks 
582ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
583ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
584ecd6cf80Smarks 		return (error);
585ecd6cf80Smarks 
586ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
587ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
588ecd6cf80Smarks 		return (error);
589ecd6cf80Smarks 
590ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
591ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
592ecd6cf80Smarks }
593ecd6cf80Smarks 
594ecd6cf80Smarks int
595ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
596ecd6cf80Smarks {
597ecd6cf80Smarks 	int error;
598ecd6cf80Smarks 
599ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
600ecd6cf80Smarks 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
601ecd6cf80Smarks 		return (error);
602ecd6cf80Smarks 
603ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(name,
604ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
605ecd6cf80Smarks 
606ecd6cf80Smarks 	return (error);
607ecd6cf80Smarks }
608ecd6cf80Smarks 
609ecd6cf80Smarks static int
610ecd6cf80Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
611ecd6cf80Smarks {
612ecd6cf80Smarks 
613ecd6cf80Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
614ecd6cf80Smarks }
615ecd6cf80Smarks 
616ecd6cf80Smarks static int
617ecd6cf80Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
618ecd6cf80Smarks {
619ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
620ecd6cf80Smarks 	int 	error;
621ecd6cf80Smarks 
622ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
623ecd6cf80Smarks 	    sizeof (parentname))) != 0)
624ecd6cf80Smarks 		return (error);
625fa9e4066Sahrens 
626ecd6cf80Smarks 	if (zc->zc_value[0] != '\0') {
627ecd6cf80Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
628ecd6cf80Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
629ecd6cf80Smarks 			return (error);
630fa9e4066Sahrens 	}
631fa9e4066Sahrens 
632ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
633ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
634ecd6cf80Smarks 		return (error);
635ecd6cf80Smarks 
636ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(parentname,
637ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
638ecd6cf80Smarks 
639ecd6cf80Smarks 	return (error);
640ecd6cf80Smarks }
641ecd6cf80Smarks 
642ecd6cf80Smarks static int
643ecd6cf80Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
644ecd6cf80Smarks {
645ecd6cf80Smarks 	int error;
646ecd6cf80Smarks 
647ecd6cf80Smarks 	error = secpolicy_fs_unmount(cr, NULL);
648ecd6cf80Smarks 	if (error) {
649ecd6cf80Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
650ecd6cf80Smarks 	}
651ecd6cf80Smarks 	return (error);
652fa9e4066Sahrens }
653fa9e4066Sahrens 
654fa9e4066Sahrens /*
655fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
656fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
657fa9e4066Sahrens  */
658fa9e4066Sahrens /* ARGSUSED */
659fa9e4066Sahrens static int
660ecd6cf80Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
661fa9e4066Sahrens {
662fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
663fa9e4066Sahrens 		return (EPERM);
664fa9e4066Sahrens 
665fa9e4066Sahrens 	return (0);
666fa9e4066Sahrens }
667fa9e4066Sahrens 
668ecd6cf80Smarks /*
669ecd6cf80Smarks  * Just like zfs_secpolicy_config, except that we will check for
670ecd6cf80Smarks  * mount permission on the dataset for permission to create/remove
671ecd6cf80Smarks  * the minor nodes.
672ecd6cf80Smarks  */
673ecd6cf80Smarks static int
674ecd6cf80Smarks zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
675ecd6cf80Smarks {
676ecd6cf80Smarks 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
677ecd6cf80Smarks 		return (dsl_deleg_access(zc->zc_name,
678ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr));
679ecd6cf80Smarks 	}
680ecd6cf80Smarks 
681ecd6cf80Smarks 	return (0);
682ecd6cf80Smarks }
683ecd6cf80Smarks 
684ea8dc4b6Seschrock /*
685ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
686ea8dc4b6Seschrock  */
687ea8dc4b6Seschrock /* ARGSUSED */
688ea8dc4b6Seschrock static int
689ecd6cf80Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
690ea8dc4b6Seschrock {
691ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
692ea8dc4b6Seschrock }
693ea8dc4b6Seschrock 
694e45ce728Sahrens static int
695e45ce728Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
696e45ce728Sahrens {
697e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
698e45ce728Sahrens 
699990b4856Slling 	if (prop == ZPROP_INVAL) {
700e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
701e45ce728Sahrens 			return (EINVAL);
702e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
703e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
704e45ce728Sahrens 	} else {
705e45ce728Sahrens 		if (!zfs_prop_inheritable(prop))
706e45ce728Sahrens 			return (EINVAL);
707e45ce728Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
708e45ce728Sahrens 	}
709e45ce728Sahrens }
710e45ce728Sahrens 
71114843421SMatthew Ahrens static int
71214843421SMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
71314843421SMatthew Ahrens {
71414843421SMatthew Ahrens 	int err = zfs_secpolicy_read(zc, cr);
71514843421SMatthew Ahrens 	if (err)
71614843421SMatthew Ahrens 		return (err);
71714843421SMatthew Ahrens 
71814843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
71914843421SMatthew Ahrens 		return (EINVAL);
72014843421SMatthew Ahrens 
72114843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
72214843421SMatthew Ahrens 		/*
72314843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
72414843421SMatthew Ahrens 		 * themself, allow it.
72514843421SMatthew Ahrens 		 */
72614843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
72714843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
72814843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
72914843421SMatthew Ahrens 				return (0);
73014843421SMatthew Ahrens 		} else {
73114843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
73214843421SMatthew Ahrens 				return (0);
73314843421SMatthew Ahrens 		}
73414843421SMatthew Ahrens 	}
73514843421SMatthew Ahrens 
73614843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
73714843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
73814843421SMatthew Ahrens }
73914843421SMatthew Ahrens 
74014843421SMatthew Ahrens static int
74114843421SMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
74214843421SMatthew Ahrens {
74314843421SMatthew Ahrens 	int err = zfs_secpolicy_read(zc, cr);
74414843421SMatthew Ahrens 	if (err)
74514843421SMatthew Ahrens 		return (err);
74614843421SMatthew Ahrens 
74714843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
74814843421SMatthew Ahrens 		return (EINVAL);
74914843421SMatthew Ahrens 
75014843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
75114843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
75214843421SMatthew Ahrens }
75314843421SMatthew Ahrens 
75414843421SMatthew Ahrens static int
75514843421SMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
75614843421SMatthew Ahrens {
75714843421SMatthew Ahrens 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
75814843421SMatthew Ahrens }
75914843421SMatthew Ahrens 
760842727c2SChris Kirby static int
761842727c2SChris Kirby zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
762842727c2SChris Kirby {
763842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
764842727c2SChris Kirby 	    ZFS_DELEG_PERM_HOLD, cr));
765842727c2SChris Kirby }
766842727c2SChris Kirby 
767842727c2SChris Kirby static int
768842727c2SChris Kirby zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
769842727c2SChris Kirby {
770842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
771842727c2SChris Kirby 	    ZFS_DELEG_PERM_RELEASE, cr));
772842727c2SChris Kirby }
773842727c2SChris Kirby 
774fa9e4066Sahrens /*
775fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
776fa9e4066Sahrens  */
777fa9e4066Sahrens static int
778478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
779fa9e4066Sahrens {
780fa9e4066Sahrens 	char *packed;
781fa9e4066Sahrens 	int error;
782990b4856Slling 	nvlist_t *list = NULL;
783fa9e4066Sahrens 
784fa9e4066Sahrens 	/*
785e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
786fa9e4066Sahrens 	 */
787990b4856Slling 	if (size == 0)
788fa9e4066Sahrens 		return (EINVAL);
789fa9e4066Sahrens 
790fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
791fa9e4066Sahrens 
792478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
793478ed9adSEric Taylor 	    iflag)) != 0) {
794fa9e4066Sahrens 		kmem_free(packed, size);
795fa9e4066Sahrens 		return (error);
796fa9e4066Sahrens 	}
797fa9e4066Sahrens 
798990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
799fa9e4066Sahrens 		kmem_free(packed, size);
800fa9e4066Sahrens 		return (error);
801fa9e4066Sahrens 	}
802fa9e4066Sahrens 
803fa9e4066Sahrens 	kmem_free(packed, size);
804fa9e4066Sahrens 
805990b4856Slling 	*nvp = list;
806fa9e4066Sahrens 	return (0);
807fa9e4066Sahrens }
808fa9e4066Sahrens 
809e9dbad6fSeschrock static int
810e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
811e9dbad6fSeschrock {
812e9dbad6fSeschrock 	char *packed = NULL;
813e9dbad6fSeschrock 	size_t size;
814e9dbad6fSeschrock 	int error;
815e9dbad6fSeschrock 
816e9dbad6fSeschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
817e9dbad6fSeschrock 
818e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
819e9dbad6fSeschrock 		error = ENOMEM;
820e9dbad6fSeschrock 	} else {
821da165920Smarks 		packed = kmem_alloc(size, KM_SLEEP);
822e9dbad6fSeschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
823e9dbad6fSeschrock 		    KM_SLEEP) == 0);
824478ed9adSEric Taylor 		error = ddi_copyout(packed,
825478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
826e9dbad6fSeschrock 		kmem_free(packed, size);
827e9dbad6fSeschrock 	}
828e9dbad6fSeschrock 
829e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
830e9dbad6fSeschrock 	return (error);
831e9dbad6fSeschrock }
832e9dbad6fSeschrock 
83314843421SMatthew Ahrens static int
83414843421SMatthew Ahrens getzfsvfs(const char *dsname, zfsvfs_t **zvp)
83514843421SMatthew Ahrens {
83614843421SMatthew Ahrens 	objset_t *os;
83714843421SMatthew Ahrens 	int error;
83814843421SMatthew Ahrens 
839*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
84014843421SMatthew Ahrens 	if (error)
84114843421SMatthew Ahrens 		return (error);
842*503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
843*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
844*503ad85cSMatthew Ahrens 		return (EINVAL);
845*503ad85cSMatthew Ahrens 	}
84614843421SMatthew Ahrens 
847*503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
84814843421SMatthew Ahrens 	*zvp = dmu_objset_get_user(os);
84914843421SMatthew Ahrens 	if (*zvp) {
85014843421SMatthew Ahrens 		VFS_HOLD((*zvp)->z_vfs);
85114843421SMatthew Ahrens 	} else {
85214843421SMatthew Ahrens 		error = ESRCH;
85314843421SMatthew Ahrens 	}
854*503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
855*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
85614843421SMatthew Ahrens 	return (error);
85714843421SMatthew Ahrens }
85814843421SMatthew Ahrens 
85914843421SMatthew Ahrens /*
86014843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
86114843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
86214843421SMatthew Ahrens  */
86314843421SMatthew Ahrens static int
864*503ad85cSMatthew Ahrens zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp)
86514843421SMatthew Ahrens {
86614843421SMatthew Ahrens 	int error = 0;
86714843421SMatthew Ahrens 
86814843421SMatthew Ahrens 	if (getzfsvfs(name, zvp) != 0)
869*503ad85cSMatthew Ahrens 		error = zfsvfs_create(name, zvp);
87014843421SMatthew Ahrens 	if (error == 0) {
87114843421SMatthew Ahrens 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
87214843421SMatthew Ahrens 		if ((*zvp)->z_unmounted) {
87314843421SMatthew Ahrens 			/*
87414843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
87514843421SMatthew Ahrens 			 * thread should be just about to disassociate the
87614843421SMatthew Ahrens 			 * objset from the zfsvfs.
87714843421SMatthew Ahrens 			 */
87814843421SMatthew Ahrens 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
87914843421SMatthew Ahrens 			return (EBUSY);
88014843421SMatthew Ahrens 		}
88114843421SMatthew Ahrens 	}
88214843421SMatthew Ahrens 	return (error);
88314843421SMatthew Ahrens }
88414843421SMatthew Ahrens 
88514843421SMatthew Ahrens static void
88614843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
88714843421SMatthew Ahrens {
88814843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
88914843421SMatthew Ahrens 
89014843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
89114843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
89214843421SMatthew Ahrens 	} else {
893*503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
89414843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
89514843421SMatthew Ahrens 	}
89614843421SMatthew Ahrens }
89714843421SMatthew Ahrens 
898fa9e4066Sahrens static int
899fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
900fa9e4066Sahrens {
901fa9e4066Sahrens 	int error;
902990b4856Slling 	nvlist_t *config, *props = NULL;
9030a48a24eStimh 	nvlist_t *rootprops = NULL;
9040a48a24eStimh 	nvlist_t *zplprops = NULL;
905228975ccSek 	char *buf;
906fa9e4066Sahrens 
907990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
908478ed9adSEric Taylor 	    zc->zc_iflags, &config))
909fa9e4066Sahrens 		return (error);
9102a6b87f0Sek 
911990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
912478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
913478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
914990b4856Slling 		nvlist_free(config);
915990b4856Slling 		return (error);
916990b4856Slling 	}
917990b4856Slling 
9180a48a24eStimh 	if (props) {
9190a48a24eStimh 		nvlist_t *nvl = NULL;
9200a48a24eStimh 		uint64_t version = SPA_VERSION;
9210a48a24eStimh 
9220a48a24eStimh 		(void) nvlist_lookup_uint64(props,
9230a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9240a48a24eStimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
9250a48a24eStimh 			error = EINVAL;
9260a48a24eStimh 			goto pool_props_bad;
9270a48a24eStimh 		}
9280a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9290a48a24eStimh 		if (nvl) {
9300a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9310a48a24eStimh 			if (error != 0) {
9320a48a24eStimh 				nvlist_free(config);
9330a48a24eStimh 				nvlist_free(props);
9340a48a24eStimh 				return (error);
9350a48a24eStimh 			}
9360a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9370a48a24eStimh 		}
9380a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9390a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
9400a48a24eStimh 		    zplprops, NULL);
9410a48a24eStimh 		if (error)
9420a48a24eStimh 			goto pool_props_bad;
9430a48a24eStimh 	}
9440a48a24eStimh 
9452a6b87f0Sek 	buf = history_str_get(zc);
946fa9e4066Sahrens 
9470a48a24eStimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9480a48a24eStimh 
9490a48a24eStimh 	/*
9500a48a24eStimh 	 * Set the remaining root properties
9510a48a24eStimh 	 */
9520a48a24eStimh 	if (!error &&
9530a48a24eStimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9540a48a24eStimh 		(void) spa_destroy(zc->zc_name);
955fa9e4066Sahrens 
9562a6b87f0Sek 	if (buf != NULL)
9572a6b87f0Sek 		history_str_free(buf);
958990b4856Slling 
9590a48a24eStimh pool_props_bad:
9600a48a24eStimh 	nvlist_free(rootprops);
9610a48a24eStimh 	nvlist_free(zplprops);
962fa9e4066Sahrens 	nvlist_free(config);
9630a48a24eStimh 	nvlist_free(props);
964990b4856Slling 
965fa9e4066Sahrens 	return (error);
966fa9e4066Sahrens }
967fa9e4066Sahrens 
968fa9e4066Sahrens static int
969fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
970fa9e4066Sahrens {
971ecd6cf80Smarks 	int error;
972ecd6cf80Smarks 	zfs_log_history(zc);
973ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
974ecd6cf80Smarks 	return (error);
975fa9e4066Sahrens }
976fa9e4066Sahrens 
977fa9e4066Sahrens static int
978fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
979fa9e4066Sahrens {
980fa9e4066Sahrens 	int error;
981990b4856Slling 	nvlist_t *config, *props = NULL;
982fa9e4066Sahrens 	uint64_t guid;
983fa9e4066Sahrens 
984990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
985478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
986990b4856Slling 		return (error);
987990b4856Slling 
988990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
989478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
990478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
991990b4856Slling 		nvlist_free(config);
992fa9e4066Sahrens 		return (error);
993990b4856Slling 	}
994fa9e4066Sahrens 
995fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
996ea8dc4b6Seschrock 	    guid != zc->zc_guid)
997fa9e4066Sahrens 		error = EINVAL;
998c5904d13Seschrock 	else if (zc->zc_cookie)
9996809eb4eSEric Schrock 		error = spa_import_verbatim(zc->zc_name, config,
1000c5904d13Seschrock 		    props);
1001fa9e4066Sahrens 	else
1002990b4856Slling 		error = spa_import(zc->zc_name, config, props);
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	nvlist_free(config);
1005fa9e4066Sahrens 
1006990b4856Slling 	if (props)
1007990b4856Slling 		nvlist_free(props);
1008990b4856Slling 
1009fa9e4066Sahrens 	return (error);
1010fa9e4066Sahrens }
1011fa9e4066Sahrens 
1012fa9e4066Sahrens static int
1013fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1014fa9e4066Sahrens {
1015ecd6cf80Smarks 	int error;
101689a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1017394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
101889a89ebfSlling 
1019ecd6cf80Smarks 	zfs_log_history(zc);
1020394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1021ecd6cf80Smarks 	return (error);
1022fa9e4066Sahrens }
1023fa9e4066Sahrens 
1024fa9e4066Sahrens static int
1025fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1026fa9e4066Sahrens {
1027fa9e4066Sahrens 	nvlist_t *configs;
1028fa9e4066Sahrens 	int error;
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1031fa9e4066Sahrens 		return (EEXIST);
1032fa9e4066Sahrens 
1033e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	nvlist_free(configs);
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	return (error);
1038fa9e4066Sahrens }
1039fa9e4066Sahrens 
1040fa9e4066Sahrens static int
1041fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1042fa9e4066Sahrens {
1043fa9e4066Sahrens 	nvlist_t *config;
1044fa9e4066Sahrens 	int error;
1045ea8dc4b6Seschrock 	int ret = 0;
1046fa9e4066Sahrens 
1047e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1048e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 	if (config != NULL) {
1051e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1052fa9e4066Sahrens 		nvlist_free(config);
1053ea8dc4b6Seschrock 
1054ea8dc4b6Seschrock 		/*
1055ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1056ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1057ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1058ea8dc4b6Seschrock 		 */
1059ea8dc4b6Seschrock 		zc->zc_cookie = error;
1060fa9e4066Sahrens 	} else {
1061ea8dc4b6Seschrock 		ret = error;
1062fa9e4066Sahrens 	}
1063fa9e4066Sahrens 
1064ea8dc4b6Seschrock 	return (ret);
1065fa9e4066Sahrens }
1066fa9e4066Sahrens 
1067fa9e4066Sahrens /*
1068fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1069fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1070fa9e4066Sahrens  */
1071fa9e4066Sahrens static int
1072fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1073fa9e4066Sahrens {
1074fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1075fa9e4066Sahrens 	int error;
1076fa9e4066Sahrens 
1077990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1078478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1079fa9e4066Sahrens 		return (error);
1080fa9e4066Sahrens 
1081fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	nvlist_free(tryconfig);
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	if (config == NULL)
1086fa9e4066Sahrens 		return (EINVAL);
1087fa9e4066Sahrens 
1088e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1089fa9e4066Sahrens 	nvlist_free(config);
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	return (error);
1092fa9e4066Sahrens }
1093fa9e4066Sahrens 
1094fa9e4066Sahrens static int
1095fa9e4066Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1096fa9e4066Sahrens {
1097fa9e4066Sahrens 	spa_t *spa;
1098fa9e4066Sahrens 	int error;
1099fa9e4066Sahrens 
110006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
110106eeb2adSek 		return (error);
110206eeb2adSek 
1103088f3894Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
110406eeb2adSek 
110506eeb2adSek 	spa_close(spa, FTAG);
110606eeb2adSek 
1107fa9e4066Sahrens 	return (error);
1108fa9e4066Sahrens }
1109fa9e4066Sahrens 
1110fa9e4066Sahrens static int
1111fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1112fa9e4066Sahrens {
1113fa9e4066Sahrens 	spa_t *spa;
1114fa9e4066Sahrens 	int error;
1115fa9e4066Sahrens 
1116fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1117fa9e4066Sahrens 	if (error == 0) {
1118fa9e4066Sahrens 		spa_freeze(spa);
1119fa9e4066Sahrens 		spa_close(spa, FTAG);
1120fa9e4066Sahrens 	}
1121fa9e4066Sahrens 	return (error);
1122fa9e4066Sahrens }
1123fa9e4066Sahrens 
1124eaca9bbdSeschrock static int
1125eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1126eaca9bbdSeschrock {
1127eaca9bbdSeschrock 	spa_t *spa;
1128eaca9bbdSeschrock 	int error;
1129eaca9bbdSeschrock 
113006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
113106eeb2adSek 		return (error);
113206eeb2adSek 
1133558d2d50Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1134558d2d50Slling 		spa_close(spa, FTAG);
1135558d2d50Slling 		return (EINVAL);
1136558d2d50Slling 	}
1137558d2d50Slling 
1138990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
113906eeb2adSek 	spa_close(spa, FTAG);
114006eeb2adSek 
114106eeb2adSek 	return (error);
114206eeb2adSek }
114306eeb2adSek 
114406eeb2adSek static int
114506eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
114606eeb2adSek {
114706eeb2adSek 	spa_t *spa;
114806eeb2adSek 	char *hist_buf;
114906eeb2adSek 	uint64_t size;
115006eeb2adSek 	int error;
115106eeb2adSek 
115206eeb2adSek 	if ((size = zc->zc_history_len) == 0)
115306eeb2adSek 		return (EINVAL);
115406eeb2adSek 
115506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
115606eeb2adSek 		return (error);
115706eeb2adSek 
1158e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1159d7306b64Sek 		spa_close(spa, FTAG);
1160d7306b64Sek 		return (ENOTSUP);
1161d7306b64Sek 	}
1162d7306b64Sek 
116306eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
116406eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
116506eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1166478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1167478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1168478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
116906eeb2adSek 	}
117006eeb2adSek 
117106eeb2adSek 	spa_close(spa, FTAG);
117206eeb2adSek 	kmem_free(hist_buf, size);
117306eeb2adSek 	return (error);
117406eeb2adSek }
117506eeb2adSek 
117655434c77Sek static int
117755434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
117855434c77Sek {
117955434c77Sek 	int error;
118055434c77Sek 
1181b1b8ab34Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
118255434c77Sek 		return (error);
118355434c77Sek 
118455434c77Sek 	return (0);
118555434c77Sek }
118655434c77Sek 
1187*503ad85cSMatthew Ahrens /*
1188*503ad85cSMatthew Ahrens  * inputs:
1189*503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1190*503ad85cSMatthew Ahrens  * zc_obj		object to find
1191*503ad85cSMatthew Ahrens  *
1192*503ad85cSMatthew Ahrens  * outputs:
1193*503ad85cSMatthew Ahrens  * zc_value		name of object
1194*503ad85cSMatthew Ahrens  */
119555434c77Sek static int
119655434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
119755434c77Sek {
1198*503ad85cSMatthew Ahrens 	objset_t *os;
119955434c77Sek 	int error;
120055434c77Sek 
1201*503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1202*503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
120355434c77Sek 		return (error);
1204*503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1205*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1206*503ad85cSMatthew Ahrens 		return (EINVAL);
1207*503ad85cSMatthew Ahrens 	}
1208*503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
120955434c77Sek 	    sizeof (zc->zc_value));
1210*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
121155434c77Sek 
121255434c77Sek 	return (error);
121355434c77Sek }
121455434c77Sek 
1215fa9e4066Sahrens static int
1216fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1217fa9e4066Sahrens {
1218fa9e4066Sahrens 	spa_t *spa;
1219fa9e4066Sahrens 	int error;
1220e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1221e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1224fa9e4066Sahrens 	if (error != 0)
1225fa9e4066Sahrens 		return (error);
1226fa9e4066Sahrens 
1227fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1228478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1229fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1230fa94a07fSbrendan 	    &l2cache, &nl2cache);
1231fa94a07fSbrendan 
1232e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1233e7cbe64fSgw 	    &spares, &nspares);
1234e7cbe64fSgw 
1235b1b8ab34Slling 	/*
1236b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1237e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1238e7cbe64fSgw 	 *
1239e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1240e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1241e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1242e7cbe64fSgw 	 *
1243e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1244b1b8ab34Slling 	 */
1245e7cbe64fSgw 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1246b1b8ab34Slling 		spa_close(spa, FTAG);
1247b1b8ab34Slling 		return (EDOM);
1248b1b8ab34Slling 	}
1249b1b8ab34Slling 
1250fa94a07fSbrendan 	if (error == 0) {
1251fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1252fa9e4066Sahrens 		nvlist_free(config);
1253fa9e4066Sahrens 	}
1254fa9e4066Sahrens 	spa_close(spa, FTAG);
1255fa9e4066Sahrens 	return (error);
1256fa9e4066Sahrens }
1257fa9e4066Sahrens 
1258fa9e4066Sahrens static int
1259fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1260fa9e4066Sahrens {
126199653d4eSeschrock 	spa_t *spa;
126299653d4eSeschrock 	int error;
126399653d4eSeschrock 
126499653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
126599653d4eSeschrock 	if (error != 0)
126699653d4eSeschrock 		return (error);
126799653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
126899653d4eSeschrock 	spa_close(spa, FTAG);
126999653d4eSeschrock 	return (error);
1270fa9e4066Sahrens }
1271fa9e4066Sahrens 
1272fa9e4066Sahrens static int
12733d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1274fa9e4066Sahrens {
1275fa9e4066Sahrens 	spa_t *spa;
1276fa9e4066Sahrens 	int error;
12773d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1278fa9e4066Sahrens 
127906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1280fa9e4066Sahrens 		return (error);
12813d7072f8Seschrock 	switch (zc->zc_cookie) {
12823d7072f8Seschrock 	case VDEV_STATE_ONLINE:
12833d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12843d7072f8Seschrock 		break;
1285fa9e4066Sahrens 
12863d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
12873d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12883d7072f8Seschrock 		break;
1289fa9e4066Sahrens 
12903d7072f8Seschrock 	case VDEV_STATE_FAULTED:
12913d7072f8Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12923d7072f8Seschrock 		break;
12933d7072f8Seschrock 
12943d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
12953d7072f8Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12963d7072f8Seschrock 		break;
12973d7072f8Seschrock 
12983d7072f8Seschrock 	default:
12993d7072f8Seschrock 		error = EINVAL;
13003d7072f8Seschrock 	}
13013d7072f8Seschrock 	zc->zc_cookie = newstate;
1302fa9e4066Sahrens 	spa_close(spa, FTAG);
1303fa9e4066Sahrens 	return (error);
1304fa9e4066Sahrens }
1305fa9e4066Sahrens 
1306fa9e4066Sahrens static int
1307fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1308fa9e4066Sahrens {
1309fa9e4066Sahrens 	spa_t *spa;
1310fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1311fa9e4066Sahrens 	nvlist_t *config;
1312fa9e4066Sahrens 	int error;
1313fa9e4066Sahrens 
131406eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1315fa9e4066Sahrens 		return (error);
1316fa9e4066Sahrens 
1317990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1318478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1319ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1320fa9e4066Sahrens 		nvlist_free(config);
1321fa9e4066Sahrens 	}
1322fa9e4066Sahrens 
1323fa9e4066Sahrens 	spa_close(spa, FTAG);
1324fa9e4066Sahrens 	return (error);
1325fa9e4066Sahrens }
1326fa9e4066Sahrens 
1327fa9e4066Sahrens static int
1328fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1329fa9e4066Sahrens {
1330fa9e4066Sahrens 	spa_t *spa;
1331fa9e4066Sahrens 	int error;
1332fa9e4066Sahrens 
133306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1334fa9e4066Sahrens 		return (error);
1335fa9e4066Sahrens 
13368ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1337fa9e4066Sahrens 
1338fa9e4066Sahrens 	spa_close(spa, FTAG);
1339fa9e4066Sahrens 	return (error);
1340fa9e4066Sahrens }
1341fa9e4066Sahrens 
1342c67d9675Seschrock static int
1343c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1344c67d9675Seschrock {
1345c67d9675Seschrock 	spa_t *spa;
1346e9dbad6fSeschrock 	char *path = zc->zc_value;
1347ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
1348c67d9675Seschrock 	int error;
1349c67d9675Seschrock 
1350c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
1351c67d9675Seschrock 	if (error != 0)
1352c67d9675Seschrock 		return (error);
1353c67d9675Seschrock 
1354c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
1355c67d9675Seschrock 	spa_close(spa, FTAG);
1356c67d9675Seschrock 	return (error);
1357c67d9675Seschrock }
1358c67d9675Seschrock 
13596809eb4eSEric Schrock static int
13606809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13616809eb4eSEric Schrock {
13626809eb4eSEric Schrock 	spa_t *spa;
13636809eb4eSEric Schrock 	char *fru = zc->zc_value;
13646809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
13656809eb4eSEric Schrock 	int error;
13666809eb4eSEric Schrock 
13676809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13686809eb4eSEric Schrock 	if (error != 0)
13696809eb4eSEric Schrock 		return (error);
13706809eb4eSEric Schrock 
13716809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
13726809eb4eSEric Schrock 	spa_close(spa, FTAG);
13736809eb4eSEric Schrock 	return (error);
13746809eb4eSEric Schrock }
13756809eb4eSEric Schrock 
13763cb34c60Sahrens /*
13773cb34c60Sahrens  * inputs:
13783cb34c60Sahrens  * zc_name		name of filesystem
13793cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13803cb34c60Sahrens  *
13813cb34c60Sahrens  * outputs:
13823cb34c60Sahrens  * zc_objset_stats	stats
13833cb34c60Sahrens  * zc_nvlist_dst	property nvlist
13843cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
13853cb34c60Sahrens  */
1386fa9e4066Sahrens static int
1387fa9e4066Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1388fa9e4066Sahrens {
1389fa9e4066Sahrens 	objset_t *os = NULL;
1390fa9e4066Sahrens 	int error;
13917f7322feSeschrock 	nvlist_t *nv;
1392fa9e4066Sahrens 
1393*503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1394fa9e4066Sahrens 		return (error);
1395fa9e4066Sahrens 
1396a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1397fa9e4066Sahrens 
13985ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
1399745cd3c5Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1400a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
1401432f72fdSahrens 		/*
1402bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
1403432f72fdSahrens 		 * which we aren't supposed to do with a
1404745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
1405432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
1406*503ad85cSMatthew Ahrens 		 * XXX reading with out owning
1407432f72fdSahrens 		 */
1408e7437265Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
1409e7437265Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1410e7437265Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
1411e7437265Sahrens 		}
1412e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
14137f7322feSeschrock 		nvlist_free(nv);
14147f7322feSeschrock 	}
1415fa9e4066Sahrens 
1416*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1417fa9e4066Sahrens 	return (error);
1418fa9e4066Sahrens }
1419fa9e4066Sahrens 
1420de8267e0Stimh static int
1421de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1422de8267e0Stimh {
1423de8267e0Stimh 	uint64_t value;
1424de8267e0Stimh 	int error;
1425de8267e0Stimh 
1426de8267e0Stimh 	/*
1427de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
1428de8267e0Stimh 	 * the default value (if there is one).
1429de8267e0Stimh 	 */
1430de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1431de8267e0Stimh 		return (error);
1432de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1433de8267e0Stimh 	return (0);
1434de8267e0Stimh }
1435de8267e0Stimh 
14363cb34c60Sahrens /*
14373cb34c60Sahrens  * inputs:
14383cb34c60Sahrens  * zc_name		name of filesystem
1439de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14403cb34c60Sahrens  *
14413cb34c60Sahrens  * outputs:
1442de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
1443de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14443cb34c60Sahrens  */
1445bd00f61bSrm static int
1446de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1447bd00f61bSrm {
1448de8267e0Stimh 	objset_t *os;
1449de8267e0Stimh 	int err;
1450bd00f61bSrm 
1451*503ad85cSMatthew Ahrens 	/* XXX reading without owning */
1452*503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1453de8267e0Stimh 		return (err);
1454bd00f61bSrm 
1455bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1456bd00f61bSrm 
1457bd00f61bSrm 	/*
1458de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
1459745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
1460745cd3c5Smaybee 	 * hold, because it could be inconsistent.
1461bd00f61bSrm 	 */
1462de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
1463de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
1464de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
1465de8267e0Stimh 		nvlist_t *nv;
1466de8267e0Stimh 
1467de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1468de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1469de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1470de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1471de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1472de8267e0Stimh 			err = put_nvlist(zc, nv);
1473de8267e0Stimh 		nvlist_free(nv);
1474de8267e0Stimh 	} else {
1475de8267e0Stimh 		err = ENOENT;
1476de8267e0Stimh 	}
1477*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1478de8267e0Stimh 	return (err);
1479bd00f61bSrm }
1480bd00f61bSrm 
148114843421SMatthew Ahrens static boolean_t
148214843421SMatthew Ahrens dataset_name_hidden(const char *name)
148314843421SMatthew Ahrens {
148414843421SMatthew Ahrens 	/*
148514843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
148614843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
148714843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
148814843421SMatthew Ahrens 	 */
148914843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
149014843421SMatthew Ahrens 		return (B_TRUE);
149114843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
149214843421SMatthew Ahrens 		return (B_TRUE);
149314843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
149414843421SMatthew Ahrens 		return (B_TRUE);
149514843421SMatthew Ahrens 	return (B_FALSE);
149614843421SMatthew Ahrens }
149714843421SMatthew Ahrens 
1498de8267e0Stimh /*
1499de8267e0Stimh  * inputs:
1500de8267e0Stimh  * zc_name		name of filesystem
1501de8267e0Stimh  * zc_cookie		zap cursor
1502de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
1503de8267e0Stimh  *
1504de8267e0Stimh  * outputs:
1505de8267e0Stimh  * zc_name		name of next filesystem
150614843421SMatthew Ahrens  * zc_cookie		zap cursor
1507de8267e0Stimh  * zc_objset_stats	stats
1508de8267e0Stimh  * zc_nvlist_dst	property nvlist
1509de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
1510de8267e0Stimh  */
1511fa9e4066Sahrens static int
1512fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1513fa9e4066Sahrens {
151487e5029aSahrens 	objset_t *os;
1515fa9e4066Sahrens 	int error;
1516fa9e4066Sahrens 	char *p;
1517fa9e4066Sahrens 
1518*503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
151987e5029aSahrens 		if (error == ENOENT)
152087e5029aSahrens 			error = ESRCH;
152187e5029aSahrens 		return (error);
1522fa9e4066Sahrens 	}
1523fa9e4066Sahrens 
1524fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
1525fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
1526fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1527fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1528fa9e4066Sahrens 
15295c0b6a79SRich Morris 	/*
15305c0b6a79SRich Morris 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15315c0b6a79SRich Morris 	 * but is not declared void because its called by dmu_objset_find().
15325c0b6a79SRich Morris 	 */
15337f73c863SRich Morris 	if (zc->zc_cookie == 0) {
15347f73c863SRich Morris 		uint64_t cookie = 0;
15357f73c863SRich Morris 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15367f73c863SRich Morris 
15377f73c863SRich Morris 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15385c0b6a79SRich Morris 			(void) dmu_objset_prefetch(p, NULL);
15397f73c863SRich Morris 	}
15407f73c863SRich Morris 
1541fa9e4066Sahrens 	do {
154287e5029aSahrens 		error = dmu_dir_list_next(os,
154387e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
154487e5029aSahrens 		    NULL, &zc->zc_cookie);
1545fa9e4066Sahrens 		if (error == ENOENT)
1546fa9e4066Sahrens 			error = ESRCH;
154714843421SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
1548*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1549fa9e4066Sahrens 
155014843421SMatthew Ahrens 	if (error == 0)
155187e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1552fa9e4066Sahrens 
1553fa9e4066Sahrens 	return (error);
1554fa9e4066Sahrens }
1555fa9e4066Sahrens 
15563cb34c60Sahrens /*
15573cb34c60Sahrens  * inputs:
15583cb34c60Sahrens  * zc_name		name of filesystem
15593cb34c60Sahrens  * zc_cookie		zap cursor
15603cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15613cb34c60Sahrens  *
15623cb34c60Sahrens  * outputs:
15633cb34c60Sahrens  * zc_name		name of next snapshot
15643cb34c60Sahrens  * zc_objset_stats	stats
15653cb34c60Sahrens  * zc_nvlist_dst	property nvlist
15663cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
15673cb34c60Sahrens  */
1568fa9e4066Sahrens static int
1569fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1570fa9e4066Sahrens {
157187e5029aSahrens 	objset_t *os;
1572fa9e4066Sahrens 	int error;
1573fa9e4066Sahrens 
1574*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
1575745cd3c5Smaybee 	if (error)
1576745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
1577fa9e4066Sahrens 
157814843421SMatthew Ahrens 	if (zc->zc_cookie == 0) {
15795c0b6a79SRich Morris 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15807f73c863SRich Morris 		    NULL, DS_FIND_SNAPSHOTS);
158114843421SMatthew Ahrens 	}
1582b81d61a6Slling 	/*
1583b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
1584b81d61a6Slling 	 * so exit immediately.
1585b81d61a6Slling 	 */
1586b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1587*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1588b81d61a6Slling 		return (ESRCH);
1589fa9e4066Sahrens 	}
1590fa9e4066Sahrens 
159187e5029aSahrens 	error = dmu_snapshot_list_next(os,
159287e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1593b38f0970Sck 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1594*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
159587e5029aSahrens 	if (error == 0)
159687e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1597745cd3c5Smaybee 	else if (error == ENOENT)
1598745cd3c5Smaybee 		error = ESRCH;
1599fa9e4066Sahrens 
16003cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
1601745cd3c5Smaybee 	if (error)
16023cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1603fa9e4066Sahrens 	return (error);
1604fa9e4066Sahrens }
1605fa9e4066Sahrens 
1606e7cbe64fSgw int
160791ebeef5Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1608fa9e4066Sahrens {
1609e9dbad6fSeschrock 	nvpair_t *elem;
16105f389de5SRich Morris 	int error = 0;
1611e9dbad6fSeschrock 	uint64_t intval;
1612e9dbad6fSeschrock 	char *strval;
16135c0b6a79SRich Morris 	nvlist_t *genericnvl;
161414843421SMatthew Ahrens 	boolean_t issnap = (strchr(name, '@') != NULL);
1615e9dbad6fSeschrock 
1616ecd6cf80Smarks 	/*
1617ecd6cf80Smarks 	 * First validate permission to set all of the properties
1618ecd6cf80Smarks 	 */
1619e9dbad6fSeschrock 	elem = NULL;
1620e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1621db870a07Sahrens 		const char *propname = nvpair_name(elem);
1622db870a07Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
1623e9dbad6fSeschrock 
1624990b4856Slling 		if (prop == ZPROP_INVAL) {
1625e9dbad6fSeschrock 			/*
1626e9dbad6fSeschrock 			 * If this is a user-defined property, it must be a
1627e9dbad6fSeschrock 			 * string, and there is no further validation to do.
1628e9dbad6fSeschrock 			 */
162914843421SMatthew Ahrens 			if (zfs_prop_user(propname) &&
163014843421SMatthew Ahrens 			    nvpair_type(elem) == DATA_TYPE_STRING) {
163114843421SMatthew Ahrens 				if (error = zfs_secpolicy_write_perms(name,
163214843421SMatthew Ahrens 				    ZFS_DELEG_PERM_USERPROP, CRED()))
163314843421SMatthew Ahrens 					return (error);
163414843421SMatthew Ahrens 				continue;
163514843421SMatthew Ahrens 			}
1636e9dbad6fSeschrock 
163714843421SMatthew Ahrens 			if (!issnap && zfs_prop_userquota(propname) &&
163814843421SMatthew Ahrens 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
163914843421SMatthew Ahrens 				const char *perm;
164014843421SMatthew Ahrens 				const char *up = zfs_userquota_prop_prefixes
164114843421SMatthew Ahrens 				    [ZFS_PROP_USERQUOTA];
164214843421SMatthew Ahrens 				if (strncmp(propname, up, strlen(up)) == 0)
164314843421SMatthew Ahrens 					perm = ZFS_DELEG_PERM_USERQUOTA;
164414843421SMatthew Ahrens 				else
164514843421SMatthew Ahrens 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
164614843421SMatthew Ahrens 				if (error = zfs_secpolicy_write_perms(name,
164714843421SMatthew Ahrens 				    perm, CRED()))
164814843421SMatthew Ahrens 					return (error);
164914843421SMatthew Ahrens 				continue;
165014843421SMatthew Ahrens 			}
165114843421SMatthew Ahrens 
165214843421SMatthew Ahrens 			return (EINVAL);
1653e9dbad6fSeschrock 		}
1654fa9e4066Sahrens 
165514843421SMatthew Ahrens 		if (issnap)
165614843421SMatthew Ahrens 			return (EINVAL);
165714843421SMatthew Ahrens 
165891ebeef5Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1659db870a07Sahrens 			return (error);
1660db870a07Sahrens 
1661e9dbad6fSeschrock 		/*
1662db870a07Sahrens 		 * Check that this value is valid for this pool version
1663e9dbad6fSeschrock 		 */
1664e9dbad6fSeschrock 		switch (prop) {
1665c9431fa1Sahl 		case ZFS_PROP_COMPRESSION:
1666c9431fa1Sahl 			/*
1667c9431fa1Sahl 			 * If the user specified gzip compression, make sure
1668c9431fa1Sahl 			 * the SPA supports it. We ignore any errors here since
1669c9431fa1Sahl 			 * we'll catch them later.
1670c9431fa1Sahl 			 */
1671c9431fa1Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
167215e6edf1Sgw 			    nvpair_value_uint64(elem, &intval) == 0) {
167315e6edf1Sgw 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
167415e6edf1Sgw 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16750a48a24eStimh 				    zfs_earlier_version(name,
1676da6c28aaSamw 				    SPA_VERSION_GZIP_COMPRESSION))
1677da6c28aaSamw 					return (ENOTSUP);
167815e6edf1Sgw 
167915e6edf1Sgw 				/*
168015e6edf1Sgw 				 * If this is a bootable dataset then
168115e6edf1Sgw 				 * verify that the compression algorithm
168215e6edf1Sgw 				 * is supported for booting. We must return
168315e6edf1Sgw 				 * something other than ENOTSUP since it
168415e6edf1Sgw 				 * implies a downrev pool version.
168515e6edf1Sgw 				 */
168615e6edf1Sgw 				if (zfs_is_bootfs(name) &&
168715e6edf1Sgw 				    !BOOTFS_COMPRESS_VALID(intval))
168815e6edf1Sgw 					return (ERANGE);
1689c9431fa1Sahl 			}
1690c9431fa1Sahl 			break;
169140feaa91Sahrens 
169240feaa91Sahrens 		case ZFS_PROP_COPIES:
169314843421SMatthew Ahrens 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
1694da6c28aaSamw 				return (ENOTSUP);
169540feaa91Sahrens 			break;
16969e6eda55Smarks 
16979e6eda55Smarks 		case ZFS_PROP_SHARESMB:
1698745cd3c5Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16999e6eda55Smarks 				return (ENOTSUP);
17009e6eda55Smarks 			break;
1701d0f3f37eSMark Shellenbaum 
1702d0f3f37eSMark Shellenbaum 		case ZFS_PROP_ACLINHERIT:
1703d0f3f37eSMark Shellenbaum 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1704d0f3f37eSMark Shellenbaum 			    nvpair_value_uint64(elem, &intval) == 0)
1705d0f3f37eSMark Shellenbaum 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
1706d0f3f37eSMark Shellenbaum 				    zfs_earlier_version(name,
1707d0f3f37eSMark Shellenbaum 				    SPA_VERSION_PASSTHROUGH_X))
1708d0f3f37eSMark Shellenbaum 					return (ENOTSUP);
170940feaa91Sahrens 		}
1710ecd6cf80Smarks 	}
1711ecd6cf80Smarks 
17125c0b6a79SRich Morris 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1713ecd6cf80Smarks 	elem = NULL;
1714ecd6cf80Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1715db870a07Sahrens 		const char *propname = nvpair_name(elem);
1716db870a07Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
1717ecd6cf80Smarks 
1718990b4856Slling 		if (prop == ZPROP_INVAL) {
171914843421SMatthew Ahrens 			if (zfs_prop_userquota(propname)) {
172014843421SMatthew Ahrens 				uint64_t *valary;
172114843421SMatthew Ahrens 				unsigned int vallen;
172214843421SMatthew Ahrens 				const char *domain;
172314843421SMatthew Ahrens 				zfs_userquota_prop_t type;
172414843421SMatthew Ahrens 				uint64_t rid;
172514843421SMatthew Ahrens 				uint64_t quota;
172614843421SMatthew Ahrens 				zfsvfs_t *zfsvfs;
172714843421SMatthew Ahrens 
172814843421SMatthew Ahrens 				VERIFY(nvpair_value_uint64_array(elem,
172914843421SMatthew Ahrens 				    &valary, &vallen) == 0);
173014843421SMatthew Ahrens 				VERIFY(vallen == 3);
173114843421SMatthew Ahrens 				type = valary[0];
173214843421SMatthew Ahrens 				rid = valary[1];
173314843421SMatthew Ahrens 				quota = valary[2];
173414843421SMatthew Ahrens 				domain = propname +
173514843421SMatthew Ahrens 				    strlen(zfs_userquota_prop_prefixes[type]);
173614843421SMatthew Ahrens 
1737*503ad85cSMatthew Ahrens 				error = zfsvfs_hold(name, FTAG, &zfsvfs);
173814843421SMatthew Ahrens 				if (error == 0) {
173914843421SMatthew Ahrens 					error = zfs_set_userquota(zfsvfs,
174014843421SMatthew Ahrens 					    type, domain, rid, quota);
174114843421SMatthew Ahrens 					zfsvfs_rele(zfsvfs, FTAG);
174214843421SMatthew Ahrens 				}
174314843421SMatthew Ahrens 				if (error == 0)
174414843421SMatthew Ahrens 					continue;
174514843421SMatthew Ahrens 				else
174614843421SMatthew Ahrens 					goto out;
174714843421SMatthew Ahrens 			} else if (zfs_prop_user(propname)) {
174814843421SMatthew Ahrens 				VERIFY(nvpair_value_string(elem, &strval) == 0);
174914843421SMatthew Ahrens 				error = dsl_prop_set(name, propname, 1,
175014843421SMatthew Ahrens 				    strlen(strval) + 1, strval);
175114843421SMatthew Ahrens 				if (error == 0)
175214843421SMatthew Ahrens 					continue;
175314843421SMatthew Ahrens 				else
175414843421SMatthew Ahrens 					goto out;
175514843421SMatthew Ahrens 			}
1756ecd6cf80Smarks 		}
1757e9dbad6fSeschrock 
1758e9dbad6fSeschrock 		switch (prop) {
1759e9dbad6fSeschrock 		case ZFS_PROP_QUOTA:
1760e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1761e7437265Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17625c0b6a79SRich Morris 				goto out;
1763e9dbad6fSeschrock 			break;
1764e9dbad6fSeschrock 
1765a9799022Sck 		case ZFS_PROP_REFQUOTA:
1766a9799022Sck 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1767a9799022Sck 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17685c0b6a79SRich Morris 				goto out;
1769a9799022Sck 			break;
1770a9799022Sck 
1771e9dbad6fSeschrock 		case ZFS_PROP_RESERVATION:
1772e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1773e9dbad6fSeschrock 			    (error = dsl_dir_set_reservation(name,
1774e9dbad6fSeschrock 			    intval)) != 0)
17755c0b6a79SRich Morris 				goto out;
1776e9dbad6fSeschrock 			break;
1777e9dbad6fSeschrock 
1778a9799022Sck 		case ZFS_PROP_REFRESERVATION:
1779a9799022Sck 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1780a9799022Sck 			    (error = dsl_dataset_set_reservation(name,
1781a9799022Sck 			    intval)) != 0)
17825c0b6a79SRich Morris 				goto out;
1783a9799022Sck 			break;
1784a9799022Sck 
1785e9dbad6fSeschrock 		case ZFS_PROP_VOLSIZE:
1786e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
178791ebeef5Sahrens 			    (error = zvol_set_volsize(name,
178891ebeef5Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17895c0b6a79SRich Morris 				goto out;
1790e9dbad6fSeschrock 			break;
1791e9dbad6fSeschrock 
1792e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1793e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1794e7437265Sahrens 			    (error = zvol_set_volblocksize(name, intval)) != 0)
17955c0b6a79SRich Morris 				goto out;
1796e7437265Sahrens 			break;
1797e7437265Sahrens 
1798e7437265Sahrens 		case ZFS_PROP_VERSION:
179914843421SMatthew Ahrens 		{
180014843421SMatthew Ahrens 			zfsvfs_t *zfsvfs;
180114843421SMatthew Ahrens 
180214843421SMatthew Ahrens 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
180314843421SMatthew Ahrens 				goto out;
1804*503ad85cSMatthew Ahrens 			if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0)
180514843421SMatthew Ahrens 				goto out;
180614843421SMatthew Ahrens 			error = zfs_set_version(zfsvfs, intval);
180714843421SMatthew Ahrens 			zfsvfs_rele(zfsvfs, FTAG);
180814843421SMatthew Ahrens 
180914843421SMatthew Ahrens 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
181014843421SMatthew Ahrens 				zfs_cmd_t zc = { 0 };
181114843421SMatthew Ahrens 				(void) strcpy(zc.zc_name, name);
181214843421SMatthew Ahrens 				(void) zfs_ioc_userspace_upgrade(&zc);
181314843421SMatthew Ahrens 			}
181414843421SMatthew Ahrens 			if (error)
18155c0b6a79SRich Morris 				goto out;
1816e9dbad6fSeschrock 			break;
181714843421SMatthew Ahrens 		}
1818e9dbad6fSeschrock 
1819e9dbad6fSeschrock 		default:
1820e9dbad6fSeschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1821e9dbad6fSeschrock 				if (zfs_prop_get_type(prop) !=
18225c0b6a79SRich Morris 				    PROP_TYPE_STRING) {
18235c0b6a79SRich Morris 					error = EINVAL;
18245c0b6a79SRich Morris 					goto out;
18255c0b6a79SRich Morris 				}
1826e9dbad6fSeschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1827a2eea2e1Sahrens 				const char *unused;
1828a2eea2e1Sahrens 
1829acd76fe5Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1830e9dbad6fSeschrock 
1831e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
183291ebeef5Sahrens 				case PROP_TYPE_NUMBER:
1833e9dbad6fSeschrock 					break;
183491ebeef5Sahrens 				case PROP_TYPE_STRING:
18355c0b6a79SRich Morris 					error = EINVAL;
18365c0b6a79SRich Morris 					goto out;
183791ebeef5Sahrens 				case PROP_TYPE_INDEX:
1838acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
18395c0b6a79SRich Morris 					    intval, &unused) != 0) {
18405c0b6a79SRich Morris 						error = EINVAL;
18415c0b6a79SRich Morris 						goto out;
18425c0b6a79SRich Morris 					}
1843e9dbad6fSeschrock 					break;
1844e9dbad6fSeschrock 				default:
1845e7437265Sahrens 					cmn_err(CE_PANIC,
1846e7437265Sahrens 					    "unknown property type");
1847e9dbad6fSeschrock 					break;
1848e9dbad6fSeschrock 				}
1849e9dbad6fSeschrock 			} else {
18505c0b6a79SRich Morris 				error = EINVAL;
18515c0b6a79SRich Morris 				goto out;
1852e9dbad6fSeschrock 			}
18535c0b6a79SRich Morris 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18545c0b6a79SRich Morris 				goto out;
1855e9dbad6fSeschrock 		}
1856e9dbad6fSeschrock 	}
1857e9dbad6fSeschrock 
18585c0b6a79SRich Morris 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18595c0b6a79SRich Morris 		error = dsl_props_set(name, genericnvl);
18605c0b6a79SRich Morris 	}
18615c0b6a79SRich Morris out:
18625c0b6a79SRich Morris 	nvlist_free(genericnvl);
18635c0b6a79SRich Morris 	return (error);
1864fa9e4066Sahrens }
1865fa9e4066Sahrens 
1866ea2f5b9eSMatthew Ahrens /*
1867ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
1868ea2f5b9eSMatthew Ahrens  */
1869ea2f5b9eSMatthew Ahrens static int
1870ea2f5b9eSMatthew Ahrens zfs_check_userprops(char *fsname, nvlist_t *nvl)
1871ea2f5b9eSMatthew Ahrens {
1872ea2f5b9eSMatthew Ahrens 	nvpair_t *elem = NULL;
1873ea2f5b9eSMatthew Ahrens 	int error = 0;
1874ea2f5b9eSMatthew Ahrens 
1875ea2f5b9eSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1876ea2f5b9eSMatthew Ahrens 		const char *propname = nvpair_name(elem);
1877ea2f5b9eSMatthew Ahrens 		char *valstr;
1878ea2f5b9eSMatthew Ahrens 
1879ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
1880ea2f5b9eSMatthew Ahrens 		    nvpair_type(elem) != DATA_TYPE_STRING)
1881ea2f5b9eSMatthew Ahrens 			return (EINVAL);
1882ea2f5b9eSMatthew Ahrens 
1883ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
1884ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
1885ea2f5b9eSMatthew Ahrens 			return (error);
1886ea2f5b9eSMatthew Ahrens 
1887ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
1888ea2f5b9eSMatthew Ahrens 			return (ENAMETOOLONG);
1889ea2f5b9eSMatthew Ahrens 
1890ea2f5b9eSMatthew Ahrens 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
1891ea2f5b9eSMatthew Ahrens 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
1892ea2f5b9eSMatthew Ahrens 			return (E2BIG);
1893ea2f5b9eSMatthew Ahrens 	}
1894ea2f5b9eSMatthew Ahrens 	return (0);
1895ea2f5b9eSMatthew Ahrens }
1896ea2f5b9eSMatthew Ahrens 
18973cb34c60Sahrens /*
18983cb34c60Sahrens  * inputs:
18993cb34c60Sahrens  * zc_name		name of filesystem
19005c0b6a79SRich Morris  * zc_value		name of property to set
19013cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
1902bb0ade09Sahrens  * zc_cookie		clear existing local props?
19033cb34c60Sahrens  *
19043cb34c60Sahrens  * outputs:		none
19053cb34c60Sahrens  */
1906fa9e4066Sahrens static int
1907e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1908fa9e4066Sahrens {
1909e9dbad6fSeschrock 	nvlist_t *nvl;
1910e9dbad6fSeschrock 	int error;
1911e9dbad6fSeschrock 
1912990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1913478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
1914e9dbad6fSeschrock 		return (error);
1915e9dbad6fSeschrock 
1916bb0ade09Sahrens 	if (zc->zc_cookie) {
1917bb0ade09Sahrens 		nvlist_t *origprops;
1918bb0ade09Sahrens 		objset_t *os;
1919bb0ade09Sahrens 
1920*503ad85cSMatthew Ahrens 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
1921bb0ade09Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19226e77af0aSDavid Pacheco 				clear_props(zc->zc_name, origprops, nvl);
1923bb0ade09Sahrens 				nvlist_free(origprops);
1924bb0ade09Sahrens 			}
1925*503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
1926bb0ade09Sahrens 		}
1927bb0ade09Sahrens 
1928bb0ade09Sahrens 	}
1929bb0ade09Sahrens 
193091ebeef5Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1931ecd6cf80Smarks 
1932e9dbad6fSeschrock 	nvlist_free(nvl);
1933e9dbad6fSeschrock 	return (error);
1934fa9e4066Sahrens }
1935fa9e4066Sahrens 
19363cb34c60Sahrens /*
19373cb34c60Sahrens  * inputs:
19383cb34c60Sahrens  * zc_name		name of filesystem
19393cb34c60Sahrens  * zc_value		name of property to inherit
19403cb34c60Sahrens  *
19413cb34c60Sahrens  * outputs:		none
19423cb34c60Sahrens  */
1943e45ce728Sahrens static int
1944e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1945e45ce728Sahrens {
1946e45ce728Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
1947e45ce728Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1948e45ce728Sahrens }
1949e45ce728Sahrens 
1950b1b8ab34Slling static int
195111a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1952b1b8ab34Slling {
1953990b4856Slling 	nvlist_t *props;
1954b1b8ab34Slling 	spa_t *spa;
1955990b4856Slling 	int error;
1956379c004dSEric Schrock 	nvpair_t *elem;
1957b1b8ab34Slling 
1958990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1959478ed9adSEric Taylor 	    zc->zc_iflags, &props)))
1960b1b8ab34Slling 		return (error);
1961b1b8ab34Slling 
1962379c004dSEric Schrock 	/*
1963379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
1964379c004dSEric Schrock 	 * to handle the faulted case.
1965379c004dSEric Schrock 	 */
1966379c004dSEric Schrock 	elem = nvlist_next_nvpair(props, NULL);
1967379c004dSEric Schrock 	if (elem != NULL && strcmp(nvpair_name(elem),
1968379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
1969379c004dSEric Schrock 	    nvlist_next_nvpair(props, elem) == NULL) {
1970379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
1971379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
1972379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
1973379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
1974379c004dSEric Schrock 		}
1975379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
1976379c004dSEric Schrock 		if (spa != NULL)
1977379c004dSEric Schrock 			return (0);
1978379c004dSEric Schrock 	}
1979379c004dSEric Schrock 
1980b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1981990b4856Slling 		nvlist_free(props);
1982b1b8ab34Slling 		return (error);
1983b1b8ab34Slling 	}
1984b1b8ab34Slling 
1985990b4856Slling 	error = spa_prop_set(spa, props);
1986b1b8ab34Slling 
1987990b4856Slling 	nvlist_free(props);
1988b1b8ab34Slling 	spa_close(spa, FTAG);
1989b1b8ab34Slling 
1990b1b8ab34Slling 	return (error);
1991b1b8ab34Slling }
1992b1b8ab34Slling 
1993b1b8ab34Slling static int
199411a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1995b1b8ab34Slling {
1996b1b8ab34Slling 	spa_t *spa;
1997b1b8ab34Slling 	int error;
1998b1b8ab34Slling 	nvlist_t *nvp = NULL;
1999b1b8ab34Slling 
2000379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2001379c004dSEric Schrock 		/*
2002379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2003379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2004379c004dSEric Schrock 		 * anyway.
2005379c004dSEric Schrock 		 */
2006379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2007379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2008379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2009379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2010379c004dSEric Schrock 	} else {
2011379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2012379c004dSEric Schrock 		spa_close(spa, FTAG);
2013379c004dSEric Schrock 	}
2014b1b8ab34Slling 
2015b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2016b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2017b1b8ab34Slling 	else
2018b1b8ab34Slling 		error = EFAULT;
2019b1b8ab34Slling 
2020379c004dSEric Schrock 	nvlist_free(nvp);
2021b1b8ab34Slling 	return (error);
2022b1b8ab34Slling }
2023b1b8ab34Slling 
2024ecd6cf80Smarks static int
2025ecd6cf80Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
2026ecd6cf80Smarks {
2027ecd6cf80Smarks 	nvlist_t *nvp;
2028ecd6cf80Smarks 	int error;
2029ecd6cf80Smarks 	uint32_t uid;
2030ecd6cf80Smarks 	uint32_t gid;
2031ecd6cf80Smarks 	uint32_t *groups;
2032ecd6cf80Smarks 	uint_t group_cnt;
2033ecd6cf80Smarks 	cred_t	*usercred;
2034ecd6cf80Smarks 
2035990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2036478ed9adSEric Taylor 	    zc->zc_iflags, &nvp)) != 0) {
2037ecd6cf80Smarks 		return (error);
2038ecd6cf80Smarks 	}
2039ecd6cf80Smarks 
2040ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32(nvp,
2041ecd6cf80Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
2042ecd6cf80Smarks 		nvlist_free(nvp);
2043ecd6cf80Smarks 		return (EPERM);
2044ecd6cf80Smarks 	}
2045ecd6cf80Smarks 
2046ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32(nvp,
2047ecd6cf80Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
2048ecd6cf80Smarks 		nvlist_free(nvp);
2049ecd6cf80Smarks 		return (EPERM);
2050ecd6cf80Smarks 	}
2051ecd6cf80Smarks 
2052ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
2053ecd6cf80Smarks 	    &groups, &group_cnt)) != 0) {
2054ecd6cf80Smarks 		nvlist_free(nvp);
2055ecd6cf80Smarks 		return (EPERM);
2056ecd6cf80Smarks 	}
2057ecd6cf80Smarks 	usercred = cralloc();
2058ecd6cf80Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
2059ecd6cf80Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
2060ecd6cf80Smarks 		nvlist_free(nvp);
2061ecd6cf80Smarks 		crfree(usercred);
2062ecd6cf80Smarks 		return (EPERM);
2063ecd6cf80Smarks 	}
2064ecd6cf80Smarks 	nvlist_free(nvp);
2065ecd6cf80Smarks 	error = dsl_deleg_access(zc->zc_name,
206691ebeef5Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
2067ecd6cf80Smarks 	crfree(usercred);
2068ecd6cf80Smarks 	return (error);
2069ecd6cf80Smarks }
2070ecd6cf80Smarks 
20713cb34c60Sahrens /*
20723cb34c60Sahrens  * inputs:
20733cb34c60Sahrens  * zc_name		name of filesystem
20743cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20753cb34c60Sahrens  * zc_perm_action	allow/unallow flag
20763cb34c60Sahrens  *
20773cb34c60Sahrens  * outputs:		none
20783cb34c60Sahrens  */
2079ecd6cf80Smarks static int
2080ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2081ecd6cf80Smarks {
2082ecd6cf80Smarks 	int error;
2083ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2084ecd6cf80Smarks 
2085990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2086478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2087ecd6cf80Smarks 		return (error);
2088ecd6cf80Smarks 
2089ecd6cf80Smarks 	/*
2090ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2091ecd6cf80Smarks 	 */
2092ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2093ecd6cf80Smarks 		nvlist_free(fsaclnv);
2094ecd6cf80Smarks 		return (EINVAL);
2095ecd6cf80Smarks 	}
2096ecd6cf80Smarks 
2097ecd6cf80Smarks 	/*
2098ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2099ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2100ecd6cf80Smarks 	 * the nvlist(s)
2101ecd6cf80Smarks 	 */
2102ecd6cf80Smarks 
210391ebeef5Sahrens 	error = secpolicy_zfs(CRED());
2104ecd6cf80Smarks 	if (error) {
210591ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
210691ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
210791ebeef5Sahrens 			    fsaclnv, CRED());
210891ebeef5Sahrens 		} else {
210991ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
211091ebeef5Sahrens 			    fsaclnv, CRED());
211191ebeef5Sahrens 		}
2112ecd6cf80Smarks 	}
2113ecd6cf80Smarks 
2114ecd6cf80Smarks 	if (error == 0)
2115ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2116ecd6cf80Smarks 
2117ecd6cf80Smarks 	nvlist_free(fsaclnv);
2118ecd6cf80Smarks 	return (error);
2119ecd6cf80Smarks }
2120ecd6cf80Smarks 
21213cb34c60Sahrens /*
21223cb34c60Sahrens  * inputs:
21233cb34c60Sahrens  * zc_name		name of filesystem
21243cb34c60Sahrens  *
21253cb34c60Sahrens  * outputs:
21263cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21273cb34c60Sahrens  */
2128ecd6cf80Smarks static int
2129ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2130ecd6cf80Smarks {
2131ecd6cf80Smarks 	nvlist_t *nvp;
2132ecd6cf80Smarks 	int error;
2133ecd6cf80Smarks 
2134ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2135ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2136ecd6cf80Smarks 		nvlist_free(nvp);
2137ecd6cf80Smarks 	}
2138ecd6cf80Smarks 
2139ecd6cf80Smarks 	return (error);
2140ecd6cf80Smarks }
2141ecd6cf80Smarks 
21423cb34c60Sahrens /*
21433cb34c60Sahrens  * inputs:
21443cb34c60Sahrens  * zc_name		name of volume
21453cb34c60Sahrens  *
21463cb34c60Sahrens  * outputs:		none
21473cb34c60Sahrens  */
2148fa9e4066Sahrens static int
2149fa9e4066Sahrens zfs_ioc_create_minor(zfs_cmd_t *zc)
2150fa9e4066Sahrens {
215191ebeef5Sahrens 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2152fa9e4066Sahrens }
2153fa9e4066Sahrens 
21543cb34c60Sahrens /*
21553cb34c60Sahrens  * inputs:
21563cb34c60Sahrens  * zc_name		name of volume
21573cb34c60Sahrens  *
21583cb34c60Sahrens  * outputs:		none
21593cb34c60Sahrens  */
2160fa9e4066Sahrens static int
2161fa9e4066Sahrens zfs_ioc_remove_minor(zfs_cmd_t *zc)
2162fa9e4066Sahrens {
2163e9dbad6fSeschrock 	return (zvol_remove_minor(zc->zc_name));
2164fa9e4066Sahrens }
2165fa9e4066Sahrens 
2166fa9e4066Sahrens /*
2167fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2168fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2169fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2170fa9e4066Sahrens  */
2171fa9e4066Sahrens static vfs_t *
2172fa9e4066Sahrens zfs_get_vfs(const char *resource)
2173fa9e4066Sahrens {
2174fa9e4066Sahrens 	struct vfs *vfsp;
2175fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2176fa9e4066Sahrens 
2177fa9e4066Sahrens 	vfs_list_read_lock();
2178fa9e4066Sahrens 	vfsp = rootvfs;
2179fa9e4066Sahrens 	do {
2180fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2181fa9e4066Sahrens 			VFS_HOLD(vfsp);
2182fa9e4066Sahrens 			vfs_found = vfsp;
2183fa9e4066Sahrens 			break;
2184fa9e4066Sahrens 		}
2185fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2186fa9e4066Sahrens 	} while (vfsp != rootvfs);
2187fa9e4066Sahrens 	vfs_list_unlock();
2188fa9e4066Sahrens 	return (vfs_found);
2189fa9e4066Sahrens }
2190fa9e4066Sahrens 
2191ecd6cf80Smarks /* ARGSUSED */
2192fa9e4066Sahrens static void
2193ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2194fa9e4066Sahrens {
2195da6c28aaSamw 	zfs_creat_t *zct = arg;
2196da6c28aaSamw 
2197de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2198da6c28aaSamw }
2199da6c28aaSamw 
2200de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2201da6c28aaSamw 
2202da6c28aaSamw /*
2203de8267e0Stimh  * inputs:
22040a48a24eStimh  * createprops		list of properties requested by creator
22050a48a24eStimh  * default_zplver	zpl version to use if unspecified in createprops
22060a48a24eStimh  * fuids_ok		fuids allowed in this version of the spa?
22070a48a24eStimh  * os			parent objset pointer (NULL if root fs)
2208de8267e0Stimh  *
2209de8267e0Stimh  * outputs:
2210de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
22110a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
2212da6c28aaSamw  *
2213de8267e0Stimh  * Determine the settings for utf8only, normalization and
2214de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
2215de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
2216de8267e0Stimh  * the file system is of too early a vintage, a creator can not
2217de8267e0Stimh  * request settings for these properties, even if the requested
2218de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
2219de8267e0Stimh  * properties for these, so remove them from the source nvlist after
2220de8267e0Stimh  * processing.
2221da6c28aaSamw  */
2222da6c28aaSamw static int
222314843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
22240a48a24eStimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
22250a48a24eStimh     boolean_t *is_ci)
2226da6c28aaSamw {
2227de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
2228de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
2229de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2230da6c28aaSamw 
2231de8267e0Stimh 	ASSERT(zplprops != NULL);
2232da6c28aaSamw 
2233de8267e0Stimh 	/*
2234de8267e0Stimh 	 * Pull out creator prop choices, if any.
2235de8267e0Stimh 	 */
2236de8267e0Stimh 	if (createprops) {
22370a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
22380a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2239de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2240de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2241de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2242de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2243de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2244de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2245de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2246de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2247de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2248de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2249de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2250de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
2251de8267e0Stimh 	}
2252da6c28aaSamw 
2253c2a93d44Stimh 	/*
22540a48a24eStimh 	 * If the zpl version requested is whacky or the file system
22550a48a24eStimh 	 * or pool is version is too "young" to support normalization
22560a48a24eStimh 	 * and the creator tried to set a value for one of the props,
22570a48a24eStimh 	 * error out.
2258c2a93d44Stimh 	 */
22590a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22600a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22610a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
2262de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22630a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
2264de8267e0Stimh 		return (ENOTSUP);
2265c2a93d44Stimh 
2266de8267e0Stimh 	/*
2267de8267e0Stimh 	 * Put the version in the zplprops
2268de8267e0Stimh 	 */
2269de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2270de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2271da6c28aaSamw 
2272de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
2273de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2274de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2275de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2276da6c28aaSamw 
2277c2a93d44Stimh 	/*
2278de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
2279c2a93d44Stimh 	 */
2280de8267e0Stimh 	if (norm)
2281de8267e0Stimh 		u8 = 1;
2282de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
2283de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2284de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2285de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2286de8267e0Stimh 
2287de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
2288de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2289de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2290de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2291c2a93d44Stimh 
2292ab04eb8eStimh 	if (is_ci)
2293ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2294ab04eb8eStimh 
2295da6c28aaSamw 	return (0);
2296fa9e4066Sahrens }
2297fa9e4066Sahrens 
22980a48a24eStimh static int
22990a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
23000a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
23010a48a24eStimh {
23020a48a24eStimh 	boolean_t fuids_ok = B_TRUE;
23030a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
23040a48a24eStimh 	objset_t *os = NULL;
23050a48a24eStimh 	char parentname[MAXNAMELEN];
23060a48a24eStimh 	char *cp;
23070a48a24eStimh 	int error;
23080a48a24eStimh 
23090a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
23100a48a24eStimh 	cp = strrchr(parentname, '/');
23110a48a24eStimh 	ASSERT(cp != NULL);
23120a48a24eStimh 	cp[0] = '\0';
23130a48a24eStimh 
231414843421SMatthew Ahrens 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
231514843421SMatthew Ahrens 		zplver = ZPL_VERSION_USERSPACE - 1;
23160a48a24eStimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
23170a48a24eStimh 		zplver = ZPL_VERSION_FUID - 1;
23180a48a24eStimh 		fuids_ok = B_FALSE;
23190a48a24eStimh 	}
23200a48a24eStimh 
23210a48a24eStimh 	/*
23220a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
23230a48a24eStimh 	 */
2324*503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
23250a48a24eStimh 		return (error);
23260a48a24eStimh 
23270a48a24eStimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
23280a48a24eStimh 	    zplprops, is_ci);
2329*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
23300a48a24eStimh 	return (error);
23310a48a24eStimh }
23320a48a24eStimh 
23330a48a24eStimh static int
23340a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
23350a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
23360a48a24eStimh {
23370a48a24eStimh 	boolean_t fuids_ok = B_TRUE;
23380a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
23390a48a24eStimh 	int error;
23400a48a24eStimh 
23410a48a24eStimh 	if (spa_vers < SPA_VERSION_FUID) {
23420a48a24eStimh 		zplver = ZPL_VERSION_FUID - 1;
23430a48a24eStimh 		fuids_ok = B_FALSE;
23440a48a24eStimh 	}
23450a48a24eStimh 
23460a48a24eStimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23470a48a24eStimh 	    zplprops, is_ci);
23480a48a24eStimh 	return (error);
23490a48a24eStimh }
23500a48a24eStimh 
23513cb34c60Sahrens /*
23523cb34c60Sahrens  * inputs:
23533cb34c60Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23543cb34c60Sahrens  * zc_name		name of new objset
23553cb34c60Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23563cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23573cb34c60Sahrens  *
2358de8267e0Stimh  * outputs: none
23593cb34c60Sahrens  */
2360fa9e4066Sahrens static int
2361fa9e4066Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2362fa9e4066Sahrens {
2363fa9e4066Sahrens 	objset_t *clone;
2364fa9e4066Sahrens 	int error = 0;
2365da6c28aaSamw 	zfs_creat_t zct;
2366ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
2367ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2368fa9e4066Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2369fa9e4066Sahrens 
2370fa9e4066Sahrens 	switch (type) {
2371fa9e4066Sahrens 
2372fa9e4066Sahrens 	case DMU_OST_ZFS:
2373fa9e4066Sahrens 		cbfunc = zfs_create_cb;
2374fa9e4066Sahrens 		break;
2375fa9e4066Sahrens 
2376fa9e4066Sahrens 	case DMU_OST_ZVOL:
2377fa9e4066Sahrens 		cbfunc = zvol_create_cb;
2378fa9e4066Sahrens 		break;
2379fa9e4066Sahrens 
2380fa9e4066Sahrens 	default:
23811d452cf5Sahrens 		cbfunc = NULL;
2382e7cbe64fSgw 		break;
2383fa9e4066Sahrens 	}
2384f18faf3fSek 	if (strchr(zc->zc_name, '@') ||
2385f18faf3fSek 	    strchr(zc->zc_name, '%'))
23861d452cf5Sahrens 		return (EINVAL);
2387fa9e4066Sahrens 
2388e9dbad6fSeschrock 	if (zc->zc_nvlist_src != NULL &&
2389990b4856Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2390478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
2391e9dbad6fSeschrock 		return (error);
2392e9dbad6fSeschrock 
2393de8267e0Stimh 	zct.zct_zplprops = NULL;
2394da6c28aaSamw 	zct.zct_props = nvprops;
2395da6c28aaSamw 
2396e9dbad6fSeschrock 	if (zc->zc_value[0] != '\0') {
2397fa9e4066Sahrens 		/*
2398fa9e4066Sahrens 		 * We're creating a clone of an existing snapshot.
2399fa9e4066Sahrens 		 */
2400e9dbad6fSeschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2401e9dbad6fSeschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2402ecd6cf80Smarks 			nvlist_free(nvprops);
2403fa9e4066Sahrens 			return (EINVAL);
2404e9dbad6fSeschrock 		}
2405fa9e4066Sahrens 
2406*503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2407e9dbad6fSeschrock 		if (error) {
2408ecd6cf80Smarks 			nvlist_free(nvprops);
2409fa9e4066Sahrens 			return (error);
2410e9dbad6fSeschrock 		}
2411ab04eb8eStimh 
2412ae46e4c7SMatthew Ahrens 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2413*503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
2414da6c28aaSamw 		if (error) {
2415da6c28aaSamw 			nvlist_free(nvprops);
2416da6c28aaSamw 			return (error);
2417da6c28aaSamw 		}
2418fa9e4066Sahrens 	} else {
2419ab04eb8eStimh 		boolean_t is_insensitive = B_FALSE;
2420ab04eb8eStimh 
2421e9dbad6fSeschrock 		if (cbfunc == NULL) {
2422ecd6cf80Smarks 			nvlist_free(nvprops);
24231d452cf5Sahrens 			return (EINVAL);
2424e9dbad6fSeschrock 		}
24255c5460e9Seschrock 
2426e9dbad6fSeschrock 		if (type == DMU_OST_ZVOL) {
2427e9dbad6fSeschrock 			uint64_t volsize, volblocksize;
2428e9dbad6fSeschrock 
2429ecd6cf80Smarks 			if (nvprops == NULL ||
2430ecd6cf80Smarks 			    nvlist_lookup_uint64(nvprops,
2431e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2432e9dbad6fSeschrock 			    &volsize) != 0) {
2433ecd6cf80Smarks 				nvlist_free(nvprops);
2434e9dbad6fSeschrock 				return (EINVAL);
2435e9dbad6fSeschrock 			}
2436e9dbad6fSeschrock 
2437ecd6cf80Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
2438e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2439e9dbad6fSeschrock 			    &volblocksize)) != 0 && error != ENOENT) {
2440ecd6cf80Smarks 				nvlist_free(nvprops);
2441e9dbad6fSeschrock 				return (EINVAL);
2442e9dbad6fSeschrock 			}
2443e9dbad6fSeschrock 
2444e9dbad6fSeschrock 			if (error != 0)
2445e9dbad6fSeschrock 				volblocksize = zfs_prop_default_numeric(
2446e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
2447e9dbad6fSeschrock 
2448e9dbad6fSeschrock 			if ((error = zvol_check_volblocksize(
2449e9dbad6fSeschrock 			    volblocksize)) != 0 ||
2450e9dbad6fSeschrock 			    (error = zvol_check_volsize(volsize,
2451e9dbad6fSeschrock 			    volblocksize)) != 0) {
2452ecd6cf80Smarks 				nvlist_free(nvprops);
24535c5460e9Seschrock 				return (error);
2454e9dbad6fSeschrock 			}
2455e7437265Sahrens 		} else if (type == DMU_OST_ZFS) {
2456da6c28aaSamw 			int error;
2457da6c28aaSamw 
2458da6c28aaSamw 			/*
2459da6c28aaSamw 			 * We have to have normalization and
2460da6c28aaSamw 			 * case-folding flags correct when we do the
2461da6c28aaSamw 			 * file system creation, so go figure them out
2462de8267e0Stimh 			 * now.
2463da6c28aaSamw 			 */
2464de8267e0Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2465de8267e0Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2466de8267e0Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24670a48a24eStimh 			    zct.zct_zplprops, &is_insensitive);
2468da6c28aaSamw 			if (error != 0) {
2469da6c28aaSamw 				nvlist_free(nvprops);
2470de8267e0Stimh 				nvlist_free(zct.zct_zplprops);
2471da6c28aaSamw 				return (error);
2472da6c28aaSamw 			}
2473da6c28aaSamw 		}
2474ae46e4c7SMatthew Ahrens 		error = dmu_objset_create(zc->zc_name, type,
2475ab04eb8eStimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2476de8267e0Stimh 		nvlist_free(zct.zct_zplprops);
2477fa9e4066Sahrens 	}
2478e9dbad6fSeschrock 
2479e9dbad6fSeschrock 	/*
2480e9dbad6fSeschrock 	 * It would be nice to do this atomically.
2481e9dbad6fSeschrock 	 */
2482e9dbad6fSeschrock 	if (error == 0) {
248391ebeef5Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2484842727c2SChris Kirby 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
2485e9dbad6fSeschrock 	}
2486ecd6cf80Smarks 	nvlist_free(nvprops);
2487fa9e4066Sahrens 	return (error);
2488fa9e4066Sahrens }
2489fa9e4066Sahrens 
24903cb34c60Sahrens /*
24913cb34c60Sahrens  * inputs:
24923cb34c60Sahrens  * zc_name	name of filesystem
24933cb34c60Sahrens  * zc_value	short name of snapshot
24943cb34c60Sahrens  * zc_cookie	recursive flag
249514843421SMatthew Ahrens  * zc_nvlist_src[_size] property list
24963cb34c60Sahrens  *
24973cb34c60Sahrens  * outputs:	none
24983cb34c60Sahrens  */
2499fa9e4066Sahrens static int
25001d452cf5Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
2501fa9e4066Sahrens {
2502bb0ade09Sahrens 	nvlist_t *nvprops = NULL;
2503bb0ade09Sahrens 	int error;
2504bb0ade09Sahrens 	boolean_t recursive = zc->zc_cookie;
2505bb0ade09Sahrens 
2506e9dbad6fSeschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25071d452cf5Sahrens 		return (EINVAL);
2508bb0ade09Sahrens 
2509bb0ade09Sahrens 	if (zc->zc_nvlist_src != NULL &&
2510bb0ade09Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2511478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
2512bb0ade09Sahrens 		return (error);
2513bb0ade09Sahrens 
2514ea2f5b9eSMatthew Ahrens 	error = zfs_check_userprops(zc->zc_name, nvprops);
2515ea2f5b9eSMatthew Ahrens 	if (error)
2516ea2f5b9eSMatthew Ahrens 		goto out;
2517bb0ade09Sahrens 
2518ea2f5b9eSMatthew Ahrens 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
2519ea2f5b9eSMatthew Ahrens 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2520ea2f5b9eSMatthew Ahrens 		error = ENOTSUP;
2521ea2f5b9eSMatthew Ahrens 		goto out;
2522bb0ade09Sahrens 	}
2523ea2f5b9eSMatthew Ahrens 
2524ea2f5b9eSMatthew Ahrens 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2525ea2f5b9eSMatthew Ahrens 	    nvprops, recursive);
2526ea2f5b9eSMatthew Ahrens 
2527ea2f5b9eSMatthew Ahrens out:
2528bb0ade09Sahrens 	nvlist_free(nvprops);
2529bb0ade09Sahrens 	return (error);
25301d452cf5Sahrens }
2531fa9e4066Sahrens 
2532cdf5b4caSmmusante int
25331d452cf5Sahrens zfs_unmount_snap(char *name, void *arg)
25341d452cf5Sahrens {
25350b69c2f0Sahrens 	vfs_t *vfsp = NULL;
25361d452cf5Sahrens 
2537745cd3c5Smaybee 	if (arg) {
2538745cd3c5Smaybee 		char *snapname = arg;
2539745cd3c5Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
2540745cd3c5Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
25411d452cf5Sahrens 
2542745cd3c5Smaybee 		(void) strcpy(buf, name);
2543745cd3c5Smaybee 		(void) strcat(buf, "@");
2544745cd3c5Smaybee 		(void) strcat(buf, snapname);
2545745cd3c5Smaybee 		vfsp = zfs_get_vfs(buf);
2546745cd3c5Smaybee 		kmem_free(buf, len);
25470b69c2f0Sahrens 	} else if (strchr(name, '@')) {
25481d452cf5Sahrens 		vfsp = zfs_get_vfs(name);
25491d452cf5Sahrens 	}
25501d452cf5Sahrens 
25511d452cf5Sahrens 	if (vfsp) {
2552fa9e4066Sahrens 		/*
25531d452cf5Sahrens 		 * Always force the unmount for snapshots.
2554fa9e4066Sahrens 		 */
25551d452cf5Sahrens 		int flag = MS_FORCE;
25561d452cf5Sahrens 		int err;
25571d452cf5Sahrens 
25581d452cf5Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2559fa9e4066Sahrens 			VFS_RELE(vfsp);
25601d452cf5Sahrens 			return (err);
2561fa9e4066Sahrens 		}
25621d452cf5Sahrens 		VFS_RELE(vfsp);
25631d452cf5Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25641d452cf5Sahrens 			return (err);
25651d452cf5Sahrens 	}
25661d452cf5Sahrens 	return (0);
25671d452cf5Sahrens }
25681d452cf5Sahrens 
25693cb34c60Sahrens /*
25703cb34c60Sahrens  * inputs:
2571842727c2SChris Kirby  * zc_name		name of filesystem
2572842727c2SChris Kirby  * zc_value		short name of snapshot
2573842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
25743cb34c60Sahrens  *
25753cb34c60Sahrens  * outputs:	none
25763cb34c60Sahrens  */
25771d452cf5Sahrens static int
25781d452cf5Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25791d452cf5Sahrens {
25801d452cf5Sahrens 	int err;
25811d452cf5Sahrens 
2582e9dbad6fSeschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25831d452cf5Sahrens 		return (EINVAL);
25841d452cf5Sahrens 	err = dmu_objset_find(zc->zc_name,
2585e9dbad6fSeschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25861d452cf5Sahrens 	if (err)
25871d452cf5Sahrens 		return (err);
2588842727c2SChris Kirby 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
2589842727c2SChris Kirby 	    zc->zc_defer_destroy));
25901d452cf5Sahrens }
25911d452cf5Sahrens 
25923cb34c60Sahrens /*
25933cb34c60Sahrens  * inputs:
25943cb34c60Sahrens  * zc_name		name of dataset to destroy
25953cb34c60Sahrens  * zc_objset_type	type of objset
2596842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
25973cb34c60Sahrens  *
25983cb34c60Sahrens  * outputs:		none
25993cb34c60Sahrens  */
26001d452cf5Sahrens static int
26011d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
26021d452cf5Sahrens {
26031d452cf5Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
26041d452cf5Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26051d452cf5Sahrens 		if (err)
26061d452cf5Sahrens 			return (err);
2607fa9e4066Sahrens 	}
2608fa9e4066Sahrens 
2609842727c2SChris Kirby 	return (dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy));
2610fa9e4066Sahrens }
2611fa9e4066Sahrens 
26123cb34c60Sahrens /*
26133cb34c60Sahrens  * inputs:
26144ccbb6e7Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
26153cb34c60Sahrens  *
26163cb34c60Sahrens  * outputs:	none
26173cb34c60Sahrens  */
2618fa9e4066Sahrens static int
2619fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2620fa9e4066Sahrens {
2621ae46e4c7SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
26224ccbb6e7Sahrens 	int error;
2623ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
2624ae46e4c7SMatthew Ahrens 	char *clone_name;
2625ae46e4c7SMatthew Ahrens 
2626ae46e4c7SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
2627ae46e4c7SMatthew Ahrens 	if (error)
2628ae46e4c7SMatthew Ahrens 		return (error);
2629ae46e4c7SMatthew Ahrens 
2630ae46e4c7SMatthew Ahrens 	/* must not be a snapshot */
2631ae46e4c7SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
2632ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2633ae46e4c7SMatthew Ahrens 		return (EINVAL);
2634ae46e4c7SMatthew Ahrens 	}
2635ae46e4c7SMatthew Ahrens 
2636ae46e4c7SMatthew Ahrens 	/* must have a most recent snapshot */
2637ae46e4c7SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
2638ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2639ae46e4c7SMatthew Ahrens 		return (EINVAL);
2640ae46e4c7SMatthew Ahrens 	}
26414ccbb6e7Sahrens 
26424ccbb6e7Sahrens 	/*
2643ae46e4c7SMatthew Ahrens 	 * Create clone of most recent snapshot.
26444ccbb6e7Sahrens 	 */
2645ae46e4c7SMatthew Ahrens 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
2646ae46e4c7SMatthew Ahrens 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26474ccbb6e7Sahrens 	if (error)
2648ae46e4c7SMatthew Ahrens 		goto out;
26494ccbb6e7Sahrens 
2650*503ad85cSMatthew Ahrens 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
2651ae46e4c7SMatthew Ahrens 	if (error)
2652ae46e4c7SMatthew Ahrens 		goto out;
2653ae46e4c7SMatthew Ahrens 
2654ae46e4c7SMatthew Ahrens 	/*
2655ae46e4c7SMatthew Ahrens 	 * Do clone swap.
2656ae46e4c7SMatthew Ahrens 	 */
265714843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
2658*503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
265947f263f4Sek 		if (error == 0) {
266047f263f4Sek 			int resume_err;
26614ccbb6e7Sahrens 
2662ae46e4c7SMatthew Ahrens 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2663ae46e4c7SMatthew Ahrens 				error = dsl_dataset_clone_swap(clone, ds,
2664ae46e4c7SMatthew Ahrens 				    B_TRUE);
2665ae46e4c7SMatthew Ahrens 				dsl_dataset_disown(ds, FTAG);
2666ae46e4c7SMatthew Ahrens 				ds = NULL;
2667ae46e4c7SMatthew Ahrens 			} else {
2668ae46e4c7SMatthew Ahrens 				error = EBUSY;
2669ae46e4c7SMatthew Ahrens 			}
2670*503ad85cSMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
267147f263f4Sek 			error = error ? error : resume_err;
267247f263f4Sek 		}
26734ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26744ccbb6e7Sahrens 	} else {
2675ae46e4c7SMatthew Ahrens 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2676ae46e4c7SMatthew Ahrens 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
2677ae46e4c7SMatthew Ahrens 			dsl_dataset_disown(ds, FTAG);
2678ae46e4c7SMatthew Ahrens 			ds = NULL;
2679ae46e4c7SMatthew Ahrens 		} else {
2680ae46e4c7SMatthew Ahrens 			error = EBUSY;
2681ae46e4c7SMatthew Ahrens 		}
26824ccbb6e7Sahrens 	}
26834ccbb6e7Sahrens 
2684ae46e4c7SMatthew Ahrens 	/*
2685ae46e4c7SMatthew Ahrens 	 * Destroy clone (which also closes it).
2686ae46e4c7SMatthew Ahrens 	 */
2687ae46e4c7SMatthew Ahrens 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
2688ae46e4c7SMatthew Ahrens 
2689ae46e4c7SMatthew Ahrens out:
2690ae46e4c7SMatthew Ahrens 	strfree(clone_name);
2691ae46e4c7SMatthew Ahrens 	if (ds)
2692ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
26934ccbb6e7Sahrens 	return (error);
2694fa9e4066Sahrens }
2695fa9e4066Sahrens 
26963cb34c60Sahrens /*
26973cb34c60Sahrens  * inputs:
26983cb34c60Sahrens  * zc_name	old name of dataset
26993cb34c60Sahrens  * zc_value	new name of dataset
27003cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
27013cb34c60Sahrens  *
27023cb34c60Sahrens  * outputs:	none
27033cb34c60Sahrens  */
2704fa9e4066Sahrens static int
2705fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2706fa9e4066Sahrens {
27077f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
2708cdf5b4caSmmusante 
2709e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2710f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2711f18faf3fSek 	    strchr(zc->zc_value, '%'))
2712fa9e4066Sahrens 		return (EINVAL);
2713fa9e4066Sahrens 
2714cdf5b4caSmmusante 	/*
2715cdf5b4caSmmusante 	 * Unmount snapshot unless we're doing a recursive rename,
2716cdf5b4caSmmusante 	 * in which case the dataset code figures out which snapshots
2717cdf5b4caSmmusante 	 * to unmount.
2718cdf5b4caSmmusante 	 */
2719cdf5b4caSmmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2720fa9e4066Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
27211d452cf5Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
27221d452cf5Sahrens 		if (err)
27231d452cf5Sahrens 			return (err);
2724fa9e4066Sahrens 	}
2725cdf5b4caSmmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2726fa9e4066Sahrens }
2727fa9e4066Sahrens 
2728745cd3c5Smaybee static void
27296e77af0aSDavid Pacheco clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
2730745cd3c5Smaybee {
2731745cd3c5Smaybee 	zfs_cmd_t *zc;
2732745cd3c5Smaybee 	nvpair_t *prop;
2733745cd3c5Smaybee 
2734745cd3c5Smaybee 	if (props == NULL)
2735745cd3c5Smaybee 		return;
2736745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2737745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
2738745cd3c5Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
2739745cd3c5Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27406e77af0aSDavid Pacheco 		if (newprops != NULL &&
27416e77af0aSDavid Pacheco 		    nvlist_exists(newprops, nvpair_name(prop)))
27426e77af0aSDavid Pacheco 			continue;
2743745cd3c5Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
2744745cd3c5Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2745745cd3c5Smaybee 			(void) zfs_ioc_inherit_prop(zc);
2746745cd3c5Smaybee 	}
2747745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
2748745cd3c5Smaybee }
2749745cd3c5Smaybee 
27503cb34c60Sahrens /*
27513cb34c60Sahrens  * inputs:
27523cb34c60Sahrens  * zc_name		name of containing filesystem
27533cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27543cb34c60Sahrens  * zc_value		name of snapshot to create
27553cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27563cb34c60Sahrens  * zc_cookie		file descriptor to recv from
27573cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27583cb34c60Sahrens  * zc_guid		force flag
27593cb34c60Sahrens  *
27603cb34c60Sahrens  * outputs:
27613cb34c60Sahrens  * zc_cookie		number of bytes read
27623cb34c60Sahrens  */
2763fa9e4066Sahrens static int
27643cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2765fa9e4066Sahrens {
2766fa9e4066Sahrens 	file_t *fp;
2767f18faf3fSek 	objset_t *os;
27683cb34c60Sahrens 	dmu_recv_cookie_t drc;
2769f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
2770f18faf3fSek 	int error, fd;
27713cb34c60Sahrens 	offset_t off;
27723cb34c60Sahrens 	nvlist_t *props = NULL;
2773745cd3c5Smaybee 	nvlist_t *origprops = NULL;
27743cb34c60Sahrens 	objset_t *origin = NULL;
27753cb34c60Sahrens 	char *tosnap;
27763cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
2777fa9e4066Sahrens 
27783ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2779f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
2780f18faf3fSek 	    strchr(zc->zc_value, '%'))
27813ccfa83cSahrens 		return (EINVAL);
27823ccfa83cSahrens 
27833cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
27843cb34c60Sahrens 	tosnap = strchr(tofs, '@');
27853cb34c60Sahrens 	*tosnap = '\0';
27863cb34c60Sahrens 	tosnap++;
27873cb34c60Sahrens 
27883cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
27893cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2790478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
27913cb34c60Sahrens 		return (error);
27923cb34c60Sahrens 
2793fa9e4066Sahrens 	fd = zc->zc_cookie;
2794fa9e4066Sahrens 	fp = getf(fd);
27953cb34c60Sahrens 	if (fp == NULL) {
27963cb34c60Sahrens 		nvlist_free(props);
2797fa9e4066Sahrens 		return (EBADF);
27983cb34c60Sahrens 	}
2799f18faf3fSek 
2800*503ad85cSMatthew Ahrens 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
2801745cd3c5Smaybee 		/*
2802745cd3c5Smaybee 		 * If new properties are supplied, they are to completely
2803745cd3c5Smaybee 		 * replace the existing ones, so stash away the existing ones.
2804745cd3c5Smaybee 		 */
2805*503ad85cSMatthew Ahrens 		(void) dsl_prop_get_all(os, &origprops, B_TRUE);
2806745cd3c5Smaybee 
2807*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2808f18faf3fSek 	}
2809f18faf3fSek 
28103cb34c60Sahrens 	if (zc->zc_string[0]) {
2811*503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
2812745cd3c5Smaybee 		if (error)
2813745cd3c5Smaybee 			goto out;
28143cb34c60Sahrens 	}
28153cb34c60Sahrens 
28163cb34c60Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2817f4b94bdeSMatthew Ahrens 	    force, origin, &drc);
28183cb34c60Sahrens 	if (origin)
2819*503ad85cSMatthew Ahrens 		dmu_objset_rele(origin, FTAG);
2820745cd3c5Smaybee 	if (error)
2821745cd3c5Smaybee 		goto out;
2822f18faf3fSek 
2823f18faf3fSek 	/*
2824745cd3c5Smaybee 	 * Reset properties.  We do this before we receive the stream
2825745cd3c5Smaybee 	 * so that the properties are applied to the new data.
2826f18faf3fSek 	 */
28273cb34c60Sahrens 	if (props) {
28286e77af0aSDavid Pacheco 		clear_props(tofs, origprops, props);
2829745cd3c5Smaybee 		/*
2830745cd3c5Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
2831745cd3c5Smaybee 		 */
2832745cd3c5Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
28333cb34c60Sahrens 	}
28343cb34c60Sahrens 
28353cb34c60Sahrens 	off = fp->f_offset;
28363cb34c60Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2837a2eea2e1Sahrens 
2838f4b94bdeSMatthew Ahrens 	if (error == 0) {
2839f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
2840745cd3c5Smaybee 
2841f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
2842f4b94bdeSMatthew Ahrens 			/* online recv */
2843f4b94bdeSMatthew Ahrens 			int end_err;
2844745cd3c5Smaybee 
2845*503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
2846f4b94bdeSMatthew Ahrens 			/*
2847f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
2848f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
2849f4b94bdeSMatthew Ahrens 			 */
2850f4b94bdeSMatthew Ahrens 			end_err = dmu_recv_end(&drc);
2851f4b94bdeSMatthew Ahrens 			if (error == 0) {
2852f4b94bdeSMatthew Ahrens 				int resume_err =
2853*503ad85cSMatthew Ahrens 				    zfs_resume_fs(zfsvfs, tofs);
2854f4b94bdeSMatthew Ahrens 				error = error ? error : resume_err;
2855f4b94bdeSMatthew Ahrens 			}
2856f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
2857f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
2858745cd3c5Smaybee 		} else {
2859f4b94bdeSMatthew Ahrens 			error = dmu_recv_end(&drc);
28603cb34c60Sahrens 		}
286147f263f4Sek 	}
28623cb34c60Sahrens 
28633cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
28643cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28653cb34c60Sahrens 		fp->f_offset = off;
2866a2eea2e1Sahrens 
2867745cd3c5Smaybee 	/*
2868745cd3c5Smaybee 	 * On error, restore the original props.
2869745cd3c5Smaybee 	 */
2870745cd3c5Smaybee 	if (error && props) {
28716e77af0aSDavid Pacheco 		clear_props(tofs, props, NULL);
2872745cd3c5Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
2873745cd3c5Smaybee 	}
2874745cd3c5Smaybee out:
2875745cd3c5Smaybee 	nvlist_free(props);
2876745cd3c5Smaybee 	nvlist_free(origprops);
2877fa9e4066Sahrens 	releasef(fd);
2878fa9e4066Sahrens 	return (error);
2879fa9e4066Sahrens }
2880fa9e4066Sahrens 
28813cb34c60Sahrens /*
28823cb34c60Sahrens  * inputs:
28833cb34c60Sahrens  * zc_name	name of snapshot to send
28843cb34c60Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28853cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
28863cb34c60Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28873cb34c60Sahrens  *
28883cb34c60Sahrens  * outputs: none
28893cb34c60Sahrens  */
2890fa9e4066Sahrens static int
28913cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2892fa9e4066Sahrens {
2893fa9e4066Sahrens 	objset_t *fromsnap = NULL;
2894fa9e4066Sahrens 	objset_t *tosnap;
2895fa9e4066Sahrens 	file_t *fp;
2896fa9e4066Sahrens 	int error;
28973cb34c60Sahrens 	offset_t off;
2898fa9e4066Sahrens 
2899*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
2900fa9e4066Sahrens 	if (error)
2901fa9e4066Sahrens 		return (error);
2902fa9e4066Sahrens 
2903e9dbad6fSeschrock 	if (zc->zc_value[0] != '\0') {
29046a0f0066SEric Taylor 		char *buf;
2905a2eea2e1Sahrens 		char *cp;
2906a2eea2e1Sahrens 
29076a0f0066SEric Taylor 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
29086a0f0066SEric Taylor 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
2909a2eea2e1Sahrens 		cp = strchr(buf, '@');
2910a2eea2e1Sahrens 		if (cp)
2911a2eea2e1Sahrens 			*(cp+1) = 0;
29126a0f0066SEric Taylor 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
2913*503ad85cSMatthew Ahrens 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
29146a0f0066SEric Taylor 		kmem_free(buf, MAXPATHLEN);
2915fa9e4066Sahrens 		if (error) {
2916*503ad85cSMatthew Ahrens 			dmu_objset_rele(tosnap, FTAG);
2917fa9e4066Sahrens 			return (error);
2918fa9e4066Sahrens 		}
2919fa9e4066Sahrens 	}
2920fa9e4066Sahrens 
2921fa9e4066Sahrens 	fp = getf(zc->zc_cookie);
2922fa9e4066Sahrens 	if (fp == NULL) {
2923*503ad85cSMatthew Ahrens 		dmu_objset_rele(tosnap, FTAG);
2924fa9e4066Sahrens 		if (fromsnap)
2925*503ad85cSMatthew Ahrens 			dmu_objset_rele(fromsnap, FTAG);
2926fa9e4066Sahrens 		return (EBADF);
2927fa9e4066Sahrens 	}
2928fa9e4066Sahrens 
29293cb34c60Sahrens 	off = fp->f_offset;
29303cb34c60Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2931fa9e4066Sahrens 
29323cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
29333cb34c60Sahrens 		fp->f_offset = off;
2934fa9e4066Sahrens 	releasef(zc->zc_cookie);
2935fa9e4066Sahrens 	if (fromsnap)
2936*503ad85cSMatthew Ahrens 		dmu_objset_rele(fromsnap, FTAG);
2937*503ad85cSMatthew Ahrens 	dmu_objset_rele(tosnap, FTAG);
2938fa9e4066Sahrens 	return (error);
2939fa9e4066Sahrens }
2940fa9e4066Sahrens 
2941ea8dc4b6Seschrock static int
2942ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
2943ea8dc4b6Seschrock {
2944ea8dc4b6Seschrock 	int id, error;
2945ea8dc4b6Seschrock 
2946ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2947ea8dc4b6Seschrock 	    &zc->zc_inject_record);
2948ea8dc4b6Seschrock 
2949ea8dc4b6Seschrock 	if (error == 0)
2950ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
2951ea8dc4b6Seschrock 
2952ea8dc4b6Seschrock 	return (error);
2953ea8dc4b6Seschrock }
2954ea8dc4b6Seschrock 
2955ea8dc4b6Seschrock static int
2956ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
2957ea8dc4b6Seschrock {
2958ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
2959ea8dc4b6Seschrock }
2960ea8dc4b6Seschrock 
2961ea8dc4b6Seschrock static int
2962ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2963ea8dc4b6Seschrock {
2964ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
2965ea8dc4b6Seschrock 	int error;
2966ea8dc4b6Seschrock 
2967ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2968ea8dc4b6Seschrock 	    &zc->zc_inject_record);
2969ea8dc4b6Seschrock 
2970ea8dc4b6Seschrock 	zc->zc_guid = id;
2971ea8dc4b6Seschrock 
2972ea8dc4b6Seschrock 	return (error);
2973ea8dc4b6Seschrock }
2974ea8dc4b6Seschrock 
2975ea8dc4b6Seschrock static int
2976ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
2977ea8dc4b6Seschrock {
2978ea8dc4b6Seschrock 	spa_t *spa;
2979ea8dc4b6Seschrock 	int error;
2980e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2981ea8dc4b6Seschrock 
2982ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2983ea8dc4b6Seschrock 		return (error);
2984ea8dc4b6Seschrock 
2985e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2986ea8dc4b6Seschrock 	    &count);
2987ea8dc4b6Seschrock 	if (error == 0)
2988e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
2989ea8dc4b6Seschrock 	else
2990e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2991ea8dc4b6Seschrock 
2992ea8dc4b6Seschrock 	spa_close(spa, FTAG);
2993ea8dc4b6Seschrock 
2994ea8dc4b6Seschrock 	return (error);
2995ea8dc4b6Seschrock }
2996ea8dc4b6Seschrock 
2997ea8dc4b6Seschrock static int
2998ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
2999ea8dc4b6Seschrock {
3000ea8dc4b6Seschrock 	spa_t *spa;
3001ea8dc4b6Seschrock 	vdev_t *vd;
3002bb8b5132Sek 	int error;
3003ea8dc4b6Seschrock 
3004b87f3af3Sperrin 	/*
3005b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
3006b87f3af3Sperrin 	 */
3007b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
3008b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
3009b87f3af3Sperrin 	if (spa == NULL) {
3010b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
3011b87f3af3Sperrin 		return (EIO);
3012b87f3af3Sperrin 	}
3013b87f3af3Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
3014b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
3015b87f3af3Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
3016b87f3af3Sperrin 	}
3017b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
3018b87f3af3Sperrin 
3019ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3020ea8dc4b6Seschrock 		return (error);
3021ea8dc4b6Seschrock 
3022e14bb325SJeff Bonwick 	spa_vdev_state_enter(spa);
3023ea8dc4b6Seschrock 
3024e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
3025ea8dc4b6Seschrock 		vd = NULL;
3026c5904d13Seschrock 	} else {
3027c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3028fa94a07fSbrendan 		if (vd == NULL) {
3029e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
3030fa94a07fSbrendan 			spa_close(spa, FTAG);
3031fa94a07fSbrendan 			return (ENODEV);
3032fa94a07fSbrendan 		}
3033ea8dc4b6Seschrock 	}
3034ea8dc4b6Seschrock 
3035e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
3036e14bb325SJeff Bonwick 
3037e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
3038ea8dc4b6Seschrock 
3039e14bb325SJeff Bonwick 	/*
3040e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
3041e14bb325SJeff Bonwick 	 */
304254d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
304354d692b7SGeorge Wilson 		error = EIO;
3044ea8dc4b6Seschrock 
3045ea8dc4b6Seschrock 	spa_close(spa, FTAG);
3046ea8dc4b6Seschrock 
304754d692b7SGeorge Wilson 	return (error);
3048ea8dc4b6Seschrock }
3049ea8dc4b6Seschrock 
30503cb34c60Sahrens /*
30513cb34c60Sahrens  * inputs:
30523cb34c60Sahrens  * zc_name	name of filesystem
30533cb34c60Sahrens  * zc_value	name of origin snapshot
30543cb34c60Sahrens  *
30553cb34c60Sahrens  * outputs:	none
30563cb34c60Sahrens  */
305799653d4eSeschrock static int
305899653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
305999653d4eSeschrock {
30600b69c2f0Sahrens 	char *cp;
30610b69c2f0Sahrens 
30620b69c2f0Sahrens 	/*
30630b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30640b69c2f0Sahrens 	 * it's easier.
30650b69c2f0Sahrens 	 */
3066e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
30670b69c2f0Sahrens 	if (cp)
30680b69c2f0Sahrens 		*cp = '\0';
3069e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
30700b69c2f0Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
307199653d4eSeschrock 	return (dsl_dataset_promote(zc->zc_name));
307299653d4eSeschrock }
307399653d4eSeschrock 
307414843421SMatthew Ahrens /*
307514843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
307614843421SMatthew Ahrens  *
307714843421SMatthew Ahrens  * inputs:
307814843421SMatthew Ahrens  * zc_name	name of filesystem
307914843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
308014843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
308114843421SMatthew Ahrens  * zc_guid	RID/UID/GID
308214843421SMatthew Ahrens  *
308314843421SMatthew Ahrens  * outputs:
308414843421SMatthew Ahrens  * zc_cookie	property value
308514843421SMatthew Ahrens  */
308614843421SMatthew Ahrens static int
308714843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
308814843421SMatthew Ahrens {
308914843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
309014843421SMatthew Ahrens 	int error;
309114843421SMatthew Ahrens 
309214843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
309314843421SMatthew Ahrens 		return (EINVAL);
309414843421SMatthew Ahrens 
3095*503ad85cSMatthew Ahrens 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
309614843421SMatthew Ahrens 	if (error)
309714843421SMatthew Ahrens 		return (error);
309814843421SMatthew Ahrens 
309914843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
310014843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
310114843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
310214843421SMatthew Ahrens 
310314843421SMatthew Ahrens 	return (error);
310414843421SMatthew Ahrens }
310514843421SMatthew Ahrens 
310614843421SMatthew Ahrens /*
310714843421SMatthew Ahrens  * inputs:
310814843421SMatthew Ahrens  * zc_name		name of filesystem
310914843421SMatthew Ahrens  * zc_cookie		zap cursor
311014843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
311114843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
311214843421SMatthew Ahrens  *
311314843421SMatthew Ahrens  * outputs:
311414843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
311514843421SMatthew Ahrens  * zc_cookie	zap cursor
311614843421SMatthew Ahrens  */
311714843421SMatthew Ahrens static int
311814843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
311914843421SMatthew Ahrens {
312014843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
312114843421SMatthew Ahrens 	int error;
312214843421SMatthew Ahrens 
3123*503ad85cSMatthew Ahrens 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
312414843421SMatthew Ahrens 	if (error)
312514843421SMatthew Ahrens 		return (error);
312614843421SMatthew Ahrens 
312714843421SMatthew Ahrens 	int bufsize = zc->zc_nvlist_dst_size;
312814843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
312914843421SMatthew Ahrens 
313014843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
313114843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
313214843421SMatthew Ahrens 
313314843421SMatthew Ahrens 	if (error == 0) {
313414843421SMatthew Ahrens 		error = xcopyout(buf,
313514843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
313614843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
313714843421SMatthew Ahrens 	}
313814843421SMatthew Ahrens 	kmem_free(buf, bufsize);
313914843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
314014843421SMatthew Ahrens 
314114843421SMatthew Ahrens 	return (error);
314214843421SMatthew Ahrens }
314314843421SMatthew Ahrens 
314414843421SMatthew Ahrens /*
314514843421SMatthew Ahrens  * inputs:
314614843421SMatthew Ahrens  * zc_name		name of filesystem
314714843421SMatthew Ahrens  *
314814843421SMatthew Ahrens  * outputs:
314914843421SMatthew Ahrens  * none
315014843421SMatthew Ahrens  */
315114843421SMatthew Ahrens static int
315214843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
315314843421SMatthew Ahrens {
315414843421SMatthew Ahrens 	objset_t *os;
315514843421SMatthew Ahrens 	int error;
315614843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
315714843421SMatthew Ahrens 
315814843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3159*503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
316014843421SMatthew Ahrens 			/*
316114843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
316214843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
316314843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
316414843421SMatthew Ahrens 			 */
3165*503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
3166*503ad85cSMatthew Ahrens 			if (error == 0)
3167*503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
316814843421SMatthew Ahrens 		}
316914843421SMatthew Ahrens 		if (error == 0)
317014843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
317114843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
317214843421SMatthew Ahrens 	} else {
3173*503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
3174*503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
317514843421SMatthew Ahrens 		if (error)
317614843421SMatthew Ahrens 			return (error);
317714843421SMatthew Ahrens 
317814843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
3179*503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
318014843421SMatthew Ahrens 	}
318114843421SMatthew Ahrens 
318214843421SMatthew Ahrens 	return (error);
318314843421SMatthew Ahrens }
318414843421SMatthew Ahrens 
3185ecd6cf80Smarks /*
3186ecd6cf80Smarks  * We don't want to have a hard dependency
3187ecd6cf80Smarks  * against some special symbols in sharefs
3188da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
3189ecd6cf80Smarks  * the first file system is shared.
3190da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
3191ecd6cf80Smarks  */
3192da6c28aaSamw int (*znfsexport_fs)(void *arg);
3193ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
3194da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
3195da6c28aaSamw 
3196da6c28aaSamw int zfs_nfsshare_inited;
3197da6c28aaSamw int zfs_smbshare_inited;
3198ecd6cf80Smarks 
3199ecd6cf80Smarks ddi_modhandle_t nfs_mod;
3200ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
3201da6c28aaSamw ddi_modhandle_t smbsrv_mod;
3202ecd6cf80Smarks kmutex_t zfs_share_lock;
3203ecd6cf80Smarks 
3204da6c28aaSamw static int
3205da6c28aaSamw zfs_init_sharefs()
3206da6c28aaSamw {
3207da6c28aaSamw 	int error;
3208da6c28aaSamw 
3209da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
3210da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
3211da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
3212da6c28aaSamw 	    ddi_modopen("fs/sharefs",
3213da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
3214da6c28aaSamw 		return (ENOSYS);
3215da6c28aaSamw 	}
3216da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
3217da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
3218da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
3219da6c28aaSamw 		return (ENOSYS);
3220da6c28aaSamw 	}
3221da6c28aaSamw 	return (0);
3222da6c28aaSamw }
3223da6c28aaSamw 
3224ecd6cf80Smarks static int
3225ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
3226ecd6cf80Smarks {
3227ecd6cf80Smarks 	int error;
3228ecd6cf80Smarks 	int opcode;
3229ecd6cf80Smarks 
3230da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
3231da6c28aaSamw 	case ZFS_SHARE_NFS:
3232da6c28aaSamw 	case ZFS_UNSHARE_NFS:
3233da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
3234da6c28aaSamw 			mutex_enter(&zfs_share_lock);
3235da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
3236da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3237da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3238da6c28aaSamw 				return (ENOSYS);
3239da6c28aaSamw 			}
3240da6c28aaSamw 			if (znfsexport_fs == NULL &&
3241da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
3242da6c28aaSamw 			    ddi_modsym(nfs_mod,
3243da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
3244da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3245da6c28aaSamw 				return (ENOSYS);
3246da6c28aaSamw 			}
3247da6c28aaSamw 			error = zfs_init_sharefs();
3248da6c28aaSamw 			if (error) {
3249da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3250da6c28aaSamw 				return (ENOSYS);
3251da6c28aaSamw 			}
3252da6c28aaSamw 			zfs_nfsshare_inited = 1;
3253ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
3254ecd6cf80Smarks 		}
3255da6c28aaSamw 		break;
3256da6c28aaSamw 	case ZFS_SHARE_SMB:
3257da6c28aaSamw 	case ZFS_UNSHARE_SMB:
3258da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
3259da6c28aaSamw 			mutex_enter(&zfs_share_lock);
3260da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
3261da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
3262da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3263da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3264da6c28aaSamw 				return (ENOSYS);
3265da6c28aaSamw 			}
3266da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
3267da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
3268faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
3269da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3270da6c28aaSamw 				return (ENOSYS);
3271da6c28aaSamw 			}
3272da6c28aaSamw 			error = zfs_init_sharefs();
3273da6c28aaSamw 			if (error) {
3274da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3275da6c28aaSamw 				return (ENOSYS);
3276da6c28aaSamw 			}
3277da6c28aaSamw 			zfs_smbshare_inited = 1;
3278ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
3279ecd6cf80Smarks 		}
3280da6c28aaSamw 		break;
3281da6c28aaSamw 	default:
3282da6c28aaSamw 		return (EINVAL);
3283da6c28aaSamw 	}
3284ecd6cf80Smarks 
3285da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
3286da6c28aaSamw 	case ZFS_SHARE_NFS:
3287da6c28aaSamw 	case ZFS_UNSHARE_NFS:
3288da6c28aaSamw 		if (error =
3289da6c28aaSamw 		    znfsexport_fs((void *)
3290da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
3291da6c28aaSamw 			return (error);
3292da6c28aaSamw 		break;
3293da6c28aaSamw 	case ZFS_SHARE_SMB:
3294da6c28aaSamw 	case ZFS_UNSHARE_SMB:
3295da6c28aaSamw 		if (error = zsmbexport_fs((void *)
3296da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
3297da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
3298743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
3299da6c28aaSamw 			return (error);
3300ecd6cf80Smarks 		}
3301da6c28aaSamw 		break;
3302ecd6cf80Smarks 	}
3303ecd6cf80Smarks 
3304da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
3305da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
3306ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
3307ecd6cf80Smarks 
3308da6c28aaSamw 	/*
3309da6c28aaSamw 	 * Add or remove share from sharetab
3310da6c28aaSamw 	 */
3311ecd6cf80Smarks 	error = zshare_fs(opcode,
3312ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
3313ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
3314ecd6cf80Smarks 
3315ecd6cf80Smarks 	return (error);
3316ecd6cf80Smarks 
3317ecd6cf80Smarks }
3318ecd6cf80Smarks 
3319743a77edSAlan Wright ace_t full_access[] = {
3320743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
3321743a77edSAlan Wright };
3322743a77edSAlan Wright 
3323743a77edSAlan Wright /*
3324743a77edSAlan Wright  * Remove all ACL files in shares dir
3325743a77edSAlan Wright  */
3326743a77edSAlan Wright static int
3327743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
3328743a77edSAlan Wright {
3329743a77edSAlan Wright 	zap_cursor_t	zc;
3330743a77edSAlan Wright 	zap_attribute_t	zap;
3331743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3332743a77edSAlan Wright 	int error;
3333743a77edSAlan Wright 
3334743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
3335743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
3336743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
3337743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
3338743a77edSAlan Wright 		    NULL, 0)) != 0)
3339743a77edSAlan Wright 			break;
3340743a77edSAlan Wright 	}
3341743a77edSAlan Wright 	zap_cursor_fini(&zc);
3342743a77edSAlan Wright 	return (error);
3343743a77edSAlan Wright }
3344743a77edSAlan Wright 
3345743a77edSAlan Wright static int
3346743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
3347743a77edSAlan Wright {
3348743a77edSAlan Wright 	vnode_t *vp;
3349743a77edSAlan Wright 	znode_t *dzp;
3350743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
3351743a77edSAlan Wright 	znode_t *sharedir;
3352743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
3353743a77edSAlan Wright 	nvlist_t *nvlist;
3354743a77edSAlan Wright 	char *src, *target;
3355743a77edSAlan Wright 	vattr_t vattr;
3356743a77edSAlan Wright 	vsecattr_t vsec;
3357743a77edSAlan Wright 	int error = 0;
3358743a77edSAlan Wright 
3359743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
3360743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
3361743a77edSAlan Wright 		return (error);
3362743a77edSAlan Wright 
3363743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
3364743a77edSAlan Wright 
3365743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
3366743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
3367743a77edSAlan Wright 	    zc->zc_name) != 0)) {
3368743a77edSAlan Wright 		VN_RELE(vp);
3369743a77edSAlan Wright 		return (EINVAL);
3370743a77edSAlan Wright 	}
3371743a77edSAlan Wright 
3372743a77edSAlan Wright 	dzp = VTOZ(vp);
3373743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
3374743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
3375743a77edSAlan Wright 
33769e1320c0SMark Shellenbaum 	/*
33779e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
33789e1320c0SMark Shellenbaum 	 */
33799e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
33809e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
33819e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
33829e1320c0SMark Shellenbaum 
33839e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
33849e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33859e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
33869e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33879e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
33889e1320c0SMark Shellenbaum 		if (error) {
33899e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
33909e1320c0SMark Shellenbaum 		} else {
33919e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
33929e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
33939e1320c0SMark Shellenbaum 		}
33949e1320c0SMark Shellenbaum 		if (error) {
33959e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
33969e1320c0SMark Shellenbaum 			VN_RELE(vp);
33979e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
33989e1320c0SMark Shellenbaum 			return (error);
33999e1320c0SMark Shellenbaum 		}
34009e1320c0SMark Shellenbaum 	}
34019e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
34029e1320c0SMark Shellenbaum 
34039e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
3404743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
34059e1320c0SMark Shellenbaum 		VN_RELE(vp);
3406743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
3407743a77edSAlan Wright 		return (error);
3408743a77edSAlan Wright 	}
3409743a77edSAlan Wright 
3410743a77edSAlan Wright 	switch (zc->zc_cookie) {
3411743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
3412743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
3413743a77edSAlan Wright 		vattr.va_type = VREG;
3414743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
3415743a77edSAlan Wright 		vattr.va_uid = 0;
3416743a77edSAlan Wright 		vattr.va_gid = 0;
3417743a77edSAlan Wright 
3418743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
3419743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
3420743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
3421743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
3422743a77edSAlan Wright 
3423743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
3424743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
3425743a77edSAlan Wright 		if (resourcevp)
3426743a77edSAlan Wright 			VN_RELE(resourcevp);
3427743a77edSAlan Wright 		break;
3428743a77edSAlan Wright 
3429743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
3430743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
3431743a77edSAlan Wright 		    NULL, 0);
3432743a77edSAlan Wright 		break;
3433743a77edSAlan Wright 
3434743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
3435743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
3436478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
3437743a77edSAlan Wright 			VN_RELE(vp);
3438743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
3439743a77edSAlan Wright 			return (error);
3440743a77edSAlan Wright 		}
3441743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
3442743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
3443743a77edSAlan Wright 		    &target)) {
3444743a77edSAlan Wright 			VN_RELE(vp);
344589459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
3446743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
3447743a77edSAlan Wright 			return (error);
3448743a77edSAlan Wright 		}
3449743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
3450743a77edSAlan Wright 		    kcred, NULL, 0);
3451743a77edSAlan Wright 		nvlist_free(nvlist);
3452743a77edSAlan Wright 		break;
3453743a77edSAlan Wright 
3454743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
3455743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
3456743a77edSAlan Wright 		break;
3457743a77edSAlan Wright 
3458743a77edSAlan Wright 	default:
3459743a77edSAlan Wright 		error = EINVAL;
3460743a77edSAlan Wright 		break;
3461743a77edSAlan Wright 	}
3462743a77edSAlan Wright 
3463743a77edSAlan Wright 	VN_RELE(vp);
3464743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
3465743a77edSAlan Wright 
3466743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
3467743a77edSAlan Wright 
3468743a77edSAlan Wright 	return (error);
3469743a77edSAlan Wright }
3470743a77edSAlan Wright 
3471842727c2SChris Kirby /*
3472842727c2SChris Kirby  * inputs:
3473842727c2SChris Kirby  * zc_name	name of filesystem
3474842727c2SChris Kirby  * zc_value	short name of snap
3475842727c2SChris Kirby  * zc_string	user-supplied tag for this reference
3476842727c2SChris Kirby  * zc_cookie	recursive flag
3477842727c2SChris Kirby  *
3478842727c2SChris Kirby  * outputs:		none
3479842727c2SChris Kirby  */
3480842727c2SChris Kirby static int
3481842727c2SChris Kirby zfs_ioc_hold(zfs_cmd_t *zc)
3482842727c2SChris Kirby {
3483842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
3484842727c2SChris Kirby 
3485842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3486842727c2SChris Kirby 		return (EINVAL);
3487842727c2SChris Kirby 
3488842727c2SChris Kirby 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
3489842727c2SChris Kirby 	    zc->zc_string, recursive));
3490842727c2SChris Kirby }
3491842727c2SChris Kirby 
3492842727c2SChris Kirby /*
3493842727c2SChris Kirby  * inputs:
3494842727c2SChris Kirby  * zc_name	name of dataset from which we're releasing a user reference
3495842727c2SChris Kirby  * zc_value	short name of snap
3496842727c2SChris Kirby  * zc_string	user-supplied tag for this reference
3497842727c2SChris Kirby  * zc_cookie	recursive flag
3498842727c2SChris Kirby  *
3499842727c2SChris Kirby  * outputs:		none
3500842727c2SChris Kirby  */
3501842727c2SChris Kirby static int
3502842727c2SChris Kirby zfs_ioc_release(zfs_cmd_t *zc)
3503842727c2SChris Kirby {
3504842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
3505842727c2SChris Kirby 
3506842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3507842727c2SChris Kirby 		return (EINVAL);
3508842727c2SChris Kirby 
3509842727c2SChris Kirby 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
3510842727c2SChris Kirby 	    zc->zc_string, recursive));
3511842727c2SChris Kirby }
3512842727c2SChris Kirby 
3513842727c2SChris Kirby /*
3514842727c2SChris Kirby  * inputs:
3515842727c2SChris Kirby  * zc_name		name of filesystem
3516842727c2SChris Kirby  *
3517842727c2SChris Kirby  * outputs:
3518842727c2SChris Kirby  * zc_nvlist_src{_size}	nvlist of snapshot holds
3519842727c2SChris Kirby  */
3520842727c2SChris Kirby static int
3521842727c2SChris Kirby zfs_ioc_get_holds(zfs_cmd_t *zc)
3522842727c2SChris Kirby {
3523842727c2SChris Kirby 	nvlist_t *nvp;
3524842727c2SChris Kirby 	int error;
3525842727c2SChris Kirby 
3526842727c2SChris Kirby 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
3527842727c2SChris Kirby 		error = put_nvlist(zc, nvp);
3528842727c2SChris Kirby 		nvlist_free(nvp);
3529842727c2SChris Kirby 	}
3530842727c2SChris Kirby 
3531842727c2SChris Kirby 	return (error);
3532842727c2SChris Kirby }
3533842727c2SChris Kirby 
3534ecd6cf80Smarks /*
35352a6b87f0Sek  * pool create, destroy, and export don't log the history as part of
35362a6b87f0Sek  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35372a6b87f0Sek  * do the logging of those commands.
3538ecd6cf80Smarks  */
3539fa9e4066Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
354054d692b7SGeorge Wilson 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
354154d692b7SGeorge Wilson 	    B_FALSE },
354254d692b7SGeorge Wilson 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
354354d692b7SGeorge Wilson 	    B_FALSE },
354454d692b7SGeorge Wilson 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
354554d692b7SGeorge Wilson 	    B_FALSE },
354654d692b7SGeorge Wilson 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
354754d692b7SGeorge Wilson 	    B_FALSE },
354854d692b7SGeorge Wilson 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
354954d692b7SGeorge Wilson 	    B_FALSE },
355054d692b7SGeorge Wilson 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
355154d692b7SGeorge Wilson 	    B_FALSE },
355254d692b7SGeorge Wilson 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
355354d692b7SGeorge Wilson 	    B_FALSE },
355454d692b7SGeorge Wilson 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
355554d692b7SGeorge Wilson 	    B_TRUE },
355654d692b7SGeorge Wilson 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
355754d692b7SGeorge Wilson 	    B_FALSE },
355854d692b7SGeorge Wilson 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
355954d692b7SGeorge Wilson 	    B_TRUE },
356054d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
356154d692b7SGeorge Wilson 	    B_FALSE },
356254d692b7SGeorge Wilson 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
356354d692b7SGeorge Wilson 	    B_TRUE },
356454d692b7SGeorge Wilson 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
356554d692b7SGeorge Wilson 	    B_TRUE },
356654d692b7SGeorge Wilson 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
356754d692b7SGeorge Wilson 	    B_FALSE },
356854d692b7SGeorge Wilson 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
356954d692b7SGeorge Wilson 	    B_TRUE },
357054d692b7SGeorge Wilson 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
357154d692b7SGeorge Wilson 	    B_TRUE },
357254d692b7SGeorge Wilson 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
357354d692b7SGeorge Wilson 	    B_TRUE },
35746809eb4eSEric Schrock 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35756809eb4eSEric Schrock 	    B_TRUE },
357654d692b7SGeorge Wilson 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
357754d692b7SGeorge Wilson 	    B_FALSE },
357854d692b7SGeorge Wilson 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
357954d692b7SGeorge Wilson 	    B_FALSE },
358054d692b7SGeorge Wilson 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
358154d692b7SGeorge Wilson 	    B_FALSE },
358254d692b7SGeorge Wilson 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
358354d692b7SGeorge Wilson 	    B_FALSE },
358454d692b7SGeorge Wilson 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
358554d692b7SGeorge Wilson 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
358654d692b7SGeorge Wilson 	    B_FALSE },
358754d692b7SGeorge Wilson 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
358854d692b7SGeorge Wilson 	    B_FALSE },
358954d692b7SGeorge Wilson 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
359054d692b7SGeorge Wilson 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
359154d692b7SGeorge Wilson 	    B_TRUE},
359254d692b7SGeorge Wilson 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
359354d692b7SGeorge Wilson 	    B_TRUE },
359454d692b7SGeorge Wilson 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
359554d692b7SGeorge Wilson 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
359654d692b7SGeorge Wilson 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
359754d692b7SGeorge Wilson 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
359854d692b7SGeorge Wilson 	    B_FALSE },
359954d692b7SGeorge Wilson 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
360054d692b7SGeorge Wilson 	    B_FALSE },
360154d692b7SGeorge Wilson 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
360254d692b7SGeorge Wilson 	    B_FALSE },
360354d692b7SGeorge Wilson 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
360454d692b7SGeorge Wilson 	    B_FALSE },
360554d692b7SGeorge Wilson 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
360654d692b7SGeorge Wilson 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
360754d692b7SGeorge Wilson 	    B_TRUE },
360854d692b7SGeorge Wilson 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
360954d692b7SGeorge Wilson 	    B_TRUE },
361054d692b7SGeorge Wilson 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
361154d692b7SGeorge Wilson 	    B_TRUE },
361254d692b7SGeorge Wilson 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
361354d692b7SGeorge Wilson 	    B_FALSE },
36146e8a0f56SGeorge Wilson 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
36156e8a0f56SGeorge Wilson 	    B_TRUE },
361654d692b7SGeorge Wilson 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
361754d692b7SGeorge Wilson 	    B_TRUE },
361854d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
361954d692b7SGeorge Wilson 	    B_FALSE },
362054d692b7SGeorge Wilson 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
362154d692b7SGeorge Wilson 	    B_TRUE },
362254d692b7SGeorge Wilson 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
362354d692b7SGeorge Wilson 	    B_FALSE },
362454d692b7SGeorge Wilson 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
362554d692b7SGeorge Wilson 	    B_FALSE },
362654d692b7SGeorge Wilson 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
362754d692b7SGeorge Wilson 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
362854d692b7SGeorge Wilson 	    B_TRUE },
362954d692b7SGeorge Wilson 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
363014843421SMatthew Ahrens 	    B_FALSE },
363114843421SMatthew Ahrens 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
363214843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_FALSE },
363314843421SMatthew Ahrens 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
363414843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_FALSE },
363514843421SMatthew Ahrens 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
363614843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_TRUE },
3637842727c2SChris Kirby 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
3638842727c2SChris Kirby 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
3639842727c2SChris Kirby 	    B_TRUE },
3640842727c2SChris Kirby 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3641842727c2SChris Kirby 	    B_TRUE }
3642fa9e4066Sahrens };
3643fa9e4066Sahrens 
364454d692b7SGeorge Wilson int
364554d692b7SGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type)
364654d692b7SGeorge Wilson {
364754d692b7SGeorge Wilson 	spa_t *spa;
364854d692b7SGeorge Wilson 	int error;
364954d692b7SGeorge Wilson 
365054d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
365154d692b7SGeorge Wilson 
365214843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
365354d692b7SGeorge Wilson 	if (error == 0) {
365454d692b7SGeorge Wilson 		if (spa_suspended(spa))
365554d692b7SGeorge Wilson 			error = EAGAIN;
365654d692b7SGeorge Wilson 		spa_close(spa, FTAG);
365754d692b7SGeorge Wilson 	}
365854d692b7SGeorge Wilson 	return (error);
365954d692b7SGeorge Wilson }
366054d692b7SGeorge Wilson 
3661fa9e4066Sahrens static int
3662fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3663fa9e4066Sahrens {
3664fa9e4066Sahrens 	zfs_cmd_t *zc;
3665fa9e4066Sahrens 	uint_t vec;
36661d452cf5Sahrens 	int error, rc;
3667fa9e4066Sahrens 
3668fa9e4066Sahrens 	if (getminor(dev) != 0)
3669fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3670fa9e4066Sahrens 
3671fa9e4066Sahrens 	vec = cmd - ZFS_IOC;
367291ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3673fa9e4066Sahrens 
3674fa9e4066Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3675fa9e4066Sahrens 		return (EINVAL);
3676fa9e4066Sahrens 
3677fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3678fa9e4066Sahrens 
3679478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3680fa9e4066Sahrens 
368191ebeef5Sahrens 	if (error == 0)
3682ecd6cf80Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3683fa9e4066Sahrens 
3684fa9e4066Sahrens 	/*
3685fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3686fa9e4066Sahrens 	 * the lower layers.
3687fa9e4066Sahrens 	 */
3688fa9e4066Sahrens 	if (error == 0) {
3689fa9e4066Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3690478ed9adSEric Taylor 		zc->zc_iflags = flag & FKIOCTL;
3691fa9e4066Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
3692e7437265Sahrens 		case POOL_NAME:
3693fa9e4066Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3694fa9e4066Sahrens 				error = EINVAL;
369554d692b7SGeorge Wilson 			if (zfs_ioc_vec[vec].zvec_pool_check)
369654d692b7SGeorge Wilson 				error = pool_status_check(zc->zc_name,
369754d692b7SGeorge Wilson 				    zfs_ioc_vec[vec].zvec_namecheck);
3698fa9e4066Sahrens 			break;
3699fa9e4066Sahrens 
3700e7437265Sahrens 		case DATASET_NAME:
3701fa9e4066Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3702fa9e4066Sahrens 				error = EINVAL;
370354d692b7SGeorge Wilson 			if (zfs_ioc_vec[vec].zvec_pool_check)
370454d692b7SGeorge Wilson 				error = pool_status_check(zc->zc_name,
370554d692b7SGeorge Wilson 				    zfs_ioc_vec[vec].zvec_namecheck);
3706fa9e4066Sahrens 			break;
37075ad82045Snd 
3708e7437265Sahrens 		case NO_NAME:
37095ad82045Snd 			break;
3710fa9e4066Sahrens 		}
3711fa9e4066Sahrens 	}
3712fa9e4066Sahrens 
3713fa9e4066Sahrens 	if (error == 0)
3714fa9e4066Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3715fa9e4066Sahrens 
3716478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
3717ecd6cf80Smarks 	if (error == 0) {
37181d452cf5Sahrens 		error = rc;
371914843421SMatthew Ahrens 		if (zfs_ioc_vec[vec].zvec_his_log)
3720ecd6cf80Smarks 			zfs_log_history(zc);
3721ecd6cf80Smarks 	}
3722fa9e4066Sahrens 
3723fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3724fa9e4066Sahrens 	return (error);
3725fa9e4066Sahrens }
3726fa9e4066Sahrens 
3727fa9e4066Sahrens static int
3728fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3729fa9e4066Sahrens {
3730fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
3731fa9e4066Sahrens 		return (DDI_FAILURE);
3732fa9e4066Sahrens 
3733fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3734fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3735fa9e4066Sahrens 		return (DDI_FAILURE);
3736fa9e4066Sahrens 
3737fa9e4066Sahrens 	zfs_dip = dip;
3738fa9e4066Sahrens 
3739fa9e4066Sahrens 	ddi_report_dev(dip);
3740fa9e4066Sahrens 
3741fa9e4066Sahrens 	return (DDI_SUCCESS);
3742fa9e4066Sahrens }
3743fa9e4066Sahrens 
3744fa9e4066Sahrens static int
3745fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3746fa9e4066Sahrens {
3747fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3748fa9e4066Sahrens 		return (DDI_FAILURE);
3749fa9e4066Sahrens 
3750fa9e4066Sahrens 	if (cmd != DDI_DETACH)
3751fa9e4066Sahrens 		return (DDI_FAILURE);
3752fa9e4066Sahrens 
3753fa9e4066Sahrens 	zfs_dip = NULL;
3754fa9e4066Sahrens 
3755fa9e4066Sahrens 	ddi_prop_remove_all(dip);
3756fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
3757fa9e4066Sahrens 
3758fa9e4066Sahrens 	return (DDI_SUCCESS);
3759fa9e4066Sahrens }
3760fa9e4066Sahrens 
3761fa9e4066Sahrens /*ARGSUSED*/
3762fa9e4066Sahrens static int
3763fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3764fa9e4066Sahrens {
3765fa9e4066Sahrens 	switch (infocmd) {
3766fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3767fa9e4066Sahrens 		*result = zfs_dip;
3768fa9e4066Sahrens 		return (DDI_SUCCESS);
3769fa9e4066Sahrens 
3770fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3771a0965f35Sbonwick 		*result = (void *)0;
3772fa9e4066Sahrens 		return (DDI_SUCCESS);
3773fa9e4066Sahrens 	}
3774fa9e4066Sahrens 
3775fa9e4066Sahrens 	return (DDI_FAILURE);
3776fa9e4066Sahrens }
3777fa9e4066Sahrens 
3778fa9e4066Sahrens /*
3779fa9e4066Sahrens  * OK, so this is a little weird.
3780fa9e4066Sahrens  *
3781fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3782fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3783fa9e4066Sahrens  *
3784fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3785fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
3786fa9e4066Sahrens  */
3787fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
3788fa9e4066Sahrens 	zvol_open,	/* open */
3789fa9e4066Sahrens 	zvol_close,	/* close */
3790fa9e4066Sahrens 	zvol_strategy,	/* strategy */
3791fa9e4066Sahrens 	nodev,		/* print */
3792e7cbe64fSgw 	zvol_dump,	/* dump */
3793fa9e4066Sahrens 	zvol_read,	/* read */
3794fa9e4066Sahrens 	zvol_write,	/* write */
3795fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
3796fa9e4066Sahrens 	nodev,		/* devmap */
3797fa9e4066Sahrens 	nodev,		/* mmap */
3798fa9e4066Sahrens 	nodev,		/* segmap */
3799fa9e4066Sahrens 	nochpoll,	/* poll */
3800fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
3801fa9e4066Sahrens 	NULL,		/* streamtab */
3802fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3803fa9e4066Sahrens 	CB_REV,		/* version */
3804feb08c6bSbillm 	nodev,		/* async read */
3805feb08c6bSbillm 	nodev,		/* async write */
3806fa9e4066Sahrens };
3807fa9e4066Sahrens 
3808fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
3809fa9e4066Sahrens 	DEVO_REV,	/* version */
3810fa9e4066Sahrens 	0,		/* refcnt */
3811fa9e4066Sahrens 	zfs_info,	/* info */
3812fa9e4066Sahrens 	nulldev,	/* identify */
3813fa9e4066Sahrens 	nulldev,	/* probe */
3814fa9e4066Sahrens 	zfs_attach,	/* attach */
3815fa9e4066Sahrens 	zfs_detach,	/* detach */
3816fa9e4066Sahrens 	nodev,		/* reset */
3817fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
381819397407SSherry Moore 	NULL,		/* no bus operations */
381919397407SSherry Moore 	NULL,		/* power */
382019397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
3821fa9e4066Sahrens };
3822fa9e4066Sahrens 
3823fa9e4066Sahrens static struct modldrv zfs_modldrv = {
382419397407SSherry Moore 	&mod_driverops,
382519397407SSherry Moore 	"ZFS storage pool",
382619397407SSherry Moore 	&zfs_dev_ops
3827fa9e4066Sahrens };
3828fa9e4066Sahrens 
3829fa9e4066Sahrens static struct modlinkage modlinkage = {
3830fa9e4066Sahrens 	MODREV_1,
3831fa9e4066Sahrens 	(void *)&zfs_modlfs,
3832fa9e4066Sahrens 	(void *)&zfs_modldrv,
3833fa9e4066Sahrens 	NULL
3834fa9e4066Sahrens };
3835fa9e4066Sahrens 
3836ec533521Sfr 
3837ec533521Sfr uint_t zfs_fsyncer_key;
3838f18faf3fSek extern uint_t rrw_tsd_key;
3839ec533521Sfr 
3840fa9e4066Sahrens int
3841fa9e4066Sahrens _init(void)
3842fa9e4066Sahrens {
3843fa9e4066Sahrens 	int error;
3844fa9e4066Sahrens 
3845a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
3846a0965f35Sbonwick 	zfs_init();
3847a0965f35Sbonwick 	zvol_init();
3848a0965f35Sbonwick 
3849a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3850a0965f35Sbonwick 		zvol_fini();
3851a0965f35Sbonwick 		zfs_fini();
3852a0965f35Sbonwick 		spa_fini();
3853fa9e4066Sahrens 		return (error);
3854a0965f35Sbonwick 	}
3855fa9e4066Sahrens 
3856ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
3857f18faf3fSek 	tsd_create(&rrw_tsd_key, NULL);
3858ec533521Sfr 
3859fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3860fa9e4066Sahrens 	ASSERT(error == 0);
3861ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3862fa9e4066Sahrens 
3863fa9e4066Sahrens 	return (0);
3864fa9e4066Sahrens }
3865fa9e4066Sahrens 
3866fa9e4066Sahrens int
3867fa9e4066Sahrens _fini(void)
3868fa9e4066Sahrens {
3869fa9e4066Sahrens 	int error;
3870fa9e4066Sahrens 
3871ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3872fa9e4066Sahrens 		return (EBUSY);
3873fa9e4066Sahrens 
3874fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3875fa9e4066Sahrens 		return (error);
3876fa9e4066Sahrens 
3877fa9e4066Sahrens 	zvol_fini();
3878fa9e4066Sahrens 	zfs_fini();
3879fa9e4066Sahrens 	spa_fini();
3880da6c28aaSamw 	if (zfs_nfsshare_inited)
3881ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
3882da6c28aaSamw 	if (zfs_smbshare_inited)
3883da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
3884da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
3885ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
3886fa9e4066Sahrens 
3887ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
3888fa9e4066Sahrens 	ldi_ident_release(zfs_li);
3889fa9e4066Sahrens 	zfs_li = NULL;
3890ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
3891fa9e4066Sahrens 
3892fa9e4066Sahrens 	return (error);
3893fa9e4066Sahrens }
3894fa9e4066Sahrens 
3895fa9e4066Sahrens int
3896fa9e4066Sahrens _info(struct modinfo *modinfop)
3897fa9e4066Sahrens {
3898fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
3899fa9e4066Sahrens }
3900