xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 681d9761e8516a7dc5ab6589e2dfe717777e1123)
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 {
178503ad85cSMatthew Ahrens 	objset_t *os;
17915e6edf1Sgw 
180503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
181503ad85cSMatthew Ahrens 		boolean_t ret;
182503ad85cSMatthew Ahrens 		ret = (dmu_objset_id(os) == dmu_objset_spa(os)->spa_bootfs);
183503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
184503ad85cSMatthew Ahrens 		return (ret);
18515e6edf1Sgw 	}
186503ad85cSMatthew 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 
220503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
221745cd3c5Smaybee 		uint64_t zplversion;
2229e6eda55Smarks 
223503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
224503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
225503ad85cSMatthew Ahrens 			return (B_TRUE);
226503ad85cSMatthew Ahrens 		}
227503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
228745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
229745cd3c5Smaybee 			rc = zplversion < version;
230503ad85cSMatthew 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 {
385*681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
386*681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
387ecd6cf80Smarks }
388ecd6cf80Smarks 
389ecd6cf80Smarks int
390ecd6cf80Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
391ecd6cf80Smarks {
392ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
393ecd6cf80Smarks 	    ZFS_DELEG_PERM_SEND, cr));
394ecd6cf80Smarks }
395ecd6cf80Smarks 
396743a77edSAlan Wright static int
397743a77edSAlan Wright zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
398743a77edSAlan Wright {
399743a77edSAlan Wright 	vnode_t *vp;
400743a77edSAlan Wright 	int error;
401743a77edSAlan Wright 
402743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
403743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
404743a77edSAlan Wright 		return (error);
405743a77edSAlan Wright 
406743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
407743a77edSAlan Wright 
408743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
409743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
410743a77edSAlan Wright 	    zc->zc_name) != 0)) {
411743a77edSAlan Wright 		VN_RELE(vp);
412743a77edSAlan Wright 		return (EPERM);
413743a77edSAlan Wright 	}
414743a77edSAlan Wright 
415743a77edSAlan Wright 	VN_RELE(vp);
416743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
417743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
418743a77edSAlan Wright }
419743a77edSAlan Wright 
420ecd6cf80Smarks int
421ecd6cf80Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
422ecd6cf80Smarks {
423ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
424ecd6cf80Smarks 		return (EPERM);
425ecd6cf80Smarks 
4263cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
427ecd6cf80Smarks 		return (0);
428ecd6cf80Smarks 	} else {
429743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
430743a77edSAlan Wright 	}
431743a77edSAlan Wright }
432ecd6cf80Smarks 
433743a77edSAlan Wright int
434743a77edSAlan Wright zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
435743a77edSAlan Wright {
436743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
437743a77edSAlan Wright 		return (EPERM);
438ecd6cf80Smarks 
439743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
440743a77edSAlan Wright 		return (0);
441743a77edSAlan Wright 	} else {
442743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
443ecd6cf80Smarks 	}
444fa9e4066Sahrens }
445fa9e4066Sahrens 
446fa9e4066Sahrens static int
447ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
448fa9e4066Sahrens {
449fa9e4066Sahrens 	char *cp;
450fa9e4066Sahrens 
451fa9e4066Sahrens 	/*
452fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
453fa9e4066Sahrens 	 */
454ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
455ecd6cf80Smarks 	cp = strrchr(parent, '@');
456fa9e4066Sahrens 	if (cp != NULL) {
457fa9e4066Sahrens 		cp[0] = '\0';
458fa9e4066Sahrens 	} else {
459ecd6cf80Smarks 		cp = strrchr(parent, '/');
460fa9e4066Sahrens 		if (cp == NULL)
461fa9e4066Sahrens 			return (ENOENT);
462fa9e4066Sahrens 		cp[0] = '\0';
463ecd6cf80Smarks 	}
464ecd6cf80Smarks 
465ecd6cf80Smarks 	return (0);
466ecd6cf80Smarks }
467ecd6cf80Smarks 
468ecd6cf80Smarks int
469ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
470ecd6cf80Smarks {
471ecd6cf80Smarks 	int error;
472ecd6cf80Smarks 
473ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
474ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
475ecd6cf80Smarks 		return (error);
476ecd6cf80Smarks 
477ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
478ecd6cf80Smarks }
479ecd6cf80Smarks 
480ecd6cf80Smarks static int
481ecd6cf80Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
482ecd6cf80Smarks {
483ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
484ecd6cf80Smarks }
485ecd6cf80Smarks 
486ecd6cf80Smarks /*
487ecd6cf80Smarks  * Must have sys_config privilege to check the iscsi permission
488ecd6cf80Smarks  */
489ecd6cf80Smarks /* ARGSUSED */
490ecd6cf80Smarks static int
491ecd6cf80Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
492ecd6cf80Smarks {
493ecd6cf80Smarks 	return (secpolicy_zfs(cr));
494ecd6cf80Smarks }
495ecd6cf80Smarks 
496ecd6cf80Smarks int
497ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
498ecd6cf80Smarks {
499ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
500ecd6cf80Smarks 	int	error;
501ecd6cf80Smarks 
502ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
503ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
504ecd6cf80Smarks 		return (error);
505ecd6cf80Smarks 
506ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
507ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
508ecd6cf80Smarks 		return (error);
509ecd6cf80Smarks 
510ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
511ecd6cf80Smarks 	    sizeof (parentname))) != 0)
512ecd6cf80Smarks 		return (error);
513ecd6cf80Smarks 
514ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
515ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
516ecd6cf80Smarks 		return (error);
517ecd6cf80Smarks 
518ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
519ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
520ecd6cf80Smarks 		return (error);
521ecd6cf80Smarks 
522ecd6cf80Smarks 	return (error);
523ecd6cf80Smarks }
524ecd6cf80Smarks 
525ecd6cf80Smarks static int
526ecd6cf80Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
527ecd6cf80Smarks {
528ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
529ecd6cf80Smarks }
530ecd6cf80Smarks 
531ecd6cf80Smarks static int
532ecd6cf80Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
533ecd6cf80Smarks {
534ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
535ecd6cf80Smarks 	objset_t *clone;
536ecd6cf80Smarks 	int error;
537ecd6cf80Smarks 
538ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
539ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
540ecd6cf80Smarks 	if (error)
541ecd6cf80Smarks 		return (error);
542ecd6cf80Smarks 
543503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
544ecd6cf80Smarks 
545ecd6cf80Smarks 	if (error == 0) {
546ecd6cf80Smarks 		dsl_dataset_t *pclone = NULL;
547ecd6cf80Smarks 		dsl_dir_t *dd;
548503ad85cSMatthew Ahrens 		dd = clone->os_dsl_dataset->ds_dir;
549ecd6cf80Smarks 
550ecd6cf80Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
551745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
552745cd3c5Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
553ecd6cf80Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
554ecd6cf80Smarks 		if (error) {
555503ad85cSMatthew Ahrens 			dmu_objset_rele(clone, FTAG);
556ecd6cf80Smarks 			return (error);
557ecd6cf80Smarks 		}
558ecd6cf80Smarks 
559ecd6cf80Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
560ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
561ecd6cf80Smarks 
562ecd6cf80Smarks 		dsl_dataset_name(pclone, parentname);
563503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
564745cd3c5Smaybee 		dsl_dataset_rele(pclone, FTAG);
565ecd6cf80Smarks 		if (error == 0)
566ecd6cf80Smarks 			error = zfs_secpolicy_write_perms(parentname,
567ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
568ecd6cf80Smarks 	}
569ecd6cf80Smarks 	return (error);
570ecd6cf80Smarks }
571ecd6cf80Smarks 
572ecd6cf80Smarks static int
573ecd6cf80Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
574ecd6cf80Smarks {
575ecd6cf80Smarks 	int error;
576ecd6cf80Smarks 
577ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
578ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
579ecd6cf80Smarks 		return (error);
580ecd6cf80Smarks 
581ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
582ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
583ecd6cf80Smarks 		return (error);
584ecd6cf80Smarks 
585ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
586ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
587ecd6cf80Smarks }
588ecd6cf80Smarks 
589ecd6cf80Smarks int
590ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
591ecd6cf80Smarks {
592*681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
593*681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
594ecd6cf80Smarks }
595ecd6cf80Smarks 
596ecd6cf80Smarks static int
597ecd6cf80Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
598ecd6cf80Smarks {
599ecd6cf80Smarks 
600ecd6cf80Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
601ecd6cf80Smarks }
602ecd6cf80Smarks 
603ecd6cf80Smarks static int
604ecd6cf80Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
605ecd6cf80Smarks {
606ecd6cf80Smarks 	char 	parentname[MAXNAMELEN];
607ecd6cf80Smarks 	int 	error;
608ecd6cf80Smarks 
609ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
610ecd6cf80Smarks 	    sizeof (parentname))) != 0)
611ecd6cf80Smarks 		return (error);
612fa9e4066Sahrens 
613ecd6cf80Smarks 	if (zc->zc_value[0] != '\0') {
614ecd6cf80Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
615ecd6cf80Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
616ecd6cf80Smarks 			return (error);
617fa9e4066Sahrens 	}
618fa9e4066Sahrens 
619ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
620ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
621ecd6cf80Smarks 		return (error);
622ecd6cf80Smarks 
623ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(parentname,
624ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
625ecd6cf80Smarks 
626ecd6cf80Smarks 	return (error);
627ecd6cf80Smarks }
628ecd6cf80Smarks 
629ecd6cf80Smarks static int
630ecd6cf80Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
631ecd6cf80Smarks {
632ecd6cf80Smarks 	int error;
633ecd6cf80Smarks 
634ecd6cf80Smarks 	error = secpolicy_fs_unmount(cr, NULL);
635ecd6cf80Smarks 	if (error) {
636ecd6cf80Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
637ecd6cf80Smarks 	}
638ecd6cf80Smarks 	return (error);
639fa9e4066Sahrens }
640fa9e4066Sahrens 
641fa9e4066Sahrens /*
642fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
643fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
644fa9e4066Sahrens  */
645fa9e4066Sahrens /* ARGSUSED */
646fa9e4066Sahrens static int
647ecd6cf80Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
648fa9e4066Sahrens {
649fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
650fa9e4066Sahrens 		return (EPERM);
651fa9e4066Sahrens 
652fa9e4066Sahrens 	return (0);
653fa9e4066Sahrens }
654fa9e4066Sahrens 
655ea8dc4b6Seschrock /*
656ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
657ea8dc4b6Seschrock  */
658ea8dc4b6Seschrock /* ARGSUSED */
659ea8dc4b6Seschrock static int
660ecd6cf80Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
661ea8dc4b6Seschrock {
662ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
663ea8dc4b6Seschrock }
664ea8dc4b6Seschrock 
665e45ce728Sahrens static int
666e45ce728Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
667e45ce728Sahrens {
668e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
669e45ce728Sahrens 
670990b4856Slling 	if (prop == ZPROP_INVAL) {
671e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
672e45ce728Sahrens 			return (EINVAL);
673e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
674e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
675e45ce728Sahrens 	} else {
676e45ce728Sahrens 		if (!zfs_prop_inheritable(prop))
677e45ce728Sahrens 			return (EINVAL);
678e45ce728Sahrens 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
679e45ce728Sahrens 	}
680e45ce728Sahrens }
681e45ce728Sahrens 
68214843421SMatthew Ahrens static int
68314843421SMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
68414843421SMatthew Ahrens {
68514843421SMatthew Ahrens 	int err = zfs_secpolicy_read(zc, cr);
68614843421SMatthew Ahrens 	if (err)
68714843421SMatthew Ahrens 		return (err);
68814843421SMatthew Ahrens 
68914843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
69014843421SMatthew Ahrens 		return (EINVAL);
69114843421SMatthew Ahrens 
69214843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
69314843421SMatthew Ahrens 		/*
69414843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
69514843421SMatthew Ahrens 		 * themself, allow it.
69614843421SMatthew Ahrens 		 */
69714843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
69814843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
69914843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
70014843421SMatthew Ahrens 				return (0);
70114843421SMatthew Ahrens 		} else {
70214843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
70314843421SMatthew Ahrens 				return (0);
70414843421SMatthew Ahrens 		}
70514843421SMatthew Ahrens 	}
70614843421SMatthew Ahrens 
70714843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
70814843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
70914843421SMatthew Ahrens }
71014843421SMatthew Ahrens 
71114843421SMatthew Ahrens static int
71214843421SMatthew Ahrens zfs_secpolicy_userspace_many(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 	return (zfs_secpolicy_write_perms(zc->zc_name,
72214843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
72314843421SMatthew Ahrens }
72414843421SMatthew Ahrens 
72514843421SMatthew Ahrens static int
72614843421SMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
72714843421SMatthew Ahrens {
72814843421SMatthew Ahrens 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr));
72914843421SMatthew Ahrens }
73014843421SMatthew Ahrens 
731842727c2SChris Kirby static int
732842727c2SChris Kirby zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
733842727c2SChris Kirby {
734842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
735842727c2SChris Kirby 	    ZFS_DELEG_PERM_HOLD, cr));
736842727c2SChris Kirby }
737842727c2SChris Kirby 
738842727c2SChris Kirby static int
739842727c2SChris Kirby zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
740842727c2SChris Kirby {
741842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
742842727c2SChris Kirby 	    ZFS_DELEG_PERM_RELEASE, cr));
743842727c2SChris Kirby }
744842727c2SChris Kirby 
745fa9e4066Sahrens /*
746fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
747fa9e4066Sahrens  */
748fa9e4066Sahrens static int
749478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
750fa9e4066Sahrens {
751fa9e4066Sahrens 	char *packed;
752fa9e4066Sahrens 	int error;
753990b4856Slling 	nvlist_t *list = NULL;
754fa9e4066Sahrens 
755fa9e4066Sahrens 	/*
756e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
757fa9e4066Sahrens 	 */
758990b4856Slling 	if (size == 0)
759fa9e4066Sahrens 		return (EINVAL);
760fa9e4066Sahrens 
761fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
762fa9e4066Sahrens 
763478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
764478ed9adSEric Taylor 	    iflag)) != 0) {
765fa9e4066Sahrens 		kmem_free(packed, size);
766fa9e4066Sahrens 		return (error);
767fa9e4066Sahrens 	}
768fa9e4066Sahrens 
769990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
770fa9e4066Sahrens 		kmem_free(packed, size);
771fa9e4066Sahrens 		return (error);
772fa9e4066Sahrens 	}
773fa9e4066Sahrens 
774fa9e4066Sahrens 	kmem_free(packed, size);
775fa9e4066Sahrens 
776990b4856Slling 	*nvp = list;
777fa9e4066Sahrens 	return (0);
778fa9e4066Sahrens }
779fa9e4066Sahrens 
780e9dbad6fSeschrock static int
781e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
782e9dbad6fSeschrock {
783e9dbad6fSeschrock 	char *packed = NULL;
784e9dbad6fSeschrock 	size_t size;
785e9dbad6fSeschrock 	int error;
786e9dbad6fSeschrock 
787e9dbad6fSeschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
788e9dbad6fSeschrock 
789e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
790e9dbad6fSeschrock 		error = ENOMEM;
791e9dbad6fSeschrock 	} else {
792da165920Smarks 		packed = kmem_alloc(size, KM_SLEEP);
793e9dbad6fSeschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
794e9dbad6fSeschrock 		    KM_SLEEP) == 0);
795478ed9adSEric Taylor 		error = ddi_copyout(packed,
796478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
797e9dbad6fSeschrock 		kmem_free(packed, size);
798e9dbad6fSeschrock 	}
799e9dbad6fSeschrock 
800e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
801e9dbad6fSeschrock 	return (error);
802e9dbad6fSeschrock }
803e9dbad6fSeschrock 
80414843421SMatthew Ahrens static int
80514843421SMatthew Ahrens getzfsvfs(const char *dsname, zfsvfs_t **zvp)
80614843421SMatthew Ahrens {
80714843421SMatthew Ahrens 	objset_t *os;
80814843421SMatthew Ahrens 	int error;
80914843421SMatthew Ahrens 
810503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
81114843421SMatthew Ahrens 	if (error)
81214843421SMatthew Ahrens 		return (error);
813503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
814503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
815503ad85cSMatthew Ahrens 		return (EINVAL);
816503ad85cSMatthew Ahrens 	}
81714843421SMatthew Ahrens 
818503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
81914843421SMatthew Ahrens 	*zvp = dmu_objset_get_user(os);
82014843421SMatthew Ahrens 	if (*zvp) {
82114843421SMatthew Ahrens 		VFS_HOLD((*zvp)->z_vfs);
82214843421SMatthew Ahrens 	} else {
82314843421SMatthew Ahrens 		error = ESRCH;
82414843421SMatthew Ahrens 	}
825503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
826503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
82714843421SMatthew Ahrens 	return (error);
82814843421SMatthew Ahrens }
82914843421SMatthew Ahrens 
83014843421SMatthew Ahrens /*
83114843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
83214843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
83314843421SMatthew Ahrens  */
83414843421SMatthew Ahrens static int
835503ad85cSMatthew Ahrens zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp)
83614843421SMatthew Ahrens {
83714843421SMatthew Ahrens 	int error = 0;
83814843421SMatthew Ahrens 
83914843421SMatthew Ahrens 	if (getzfsvfs(name, zvp) != 0)
840503ad85cSMatthew Ahrens 		error = zfsvfs_create(name, zvp);
84114843421SMatthew Ahrens 	if (error == 0) {
84214843421SMatthew Ahrens 		rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag);
84314843421SMatthew Ahrens 		if ((*zvp)->z_unmounted) {
84414843421SMatthew Ahrens 			/*
84514843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
84614843421SMatthew Ahrens 			 * thread should be just about to disassociate the
84714843421SMatthew Ahrens 			 * objset from the zfsvfs.
84814843421SMatthew Ahrens 			 */
84914843421SMatthew Ahrens 			rrw_exit(&(*zvp)->z_teardown_lock, tag);
85014843421SMatthew Ahrens 			return (EBUSY);
85114843421SMatthew Ahrens 		}
85214843421SMatthew Ahrens 	}
85314843421SMatthew Ahrens 	return (error);
85414843421SMatthew Ahrens }
85514843421SMatthew Ahrens 
85614843421SMatthew Ahrens static void
85714843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
85814843421SMatthew Ahrens {
85914843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
86014843421SMatthew Ahrens 
86114843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
86214843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
86314843421SMatthew Ahrens 	} else {
864503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
86514843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
86614843421SMatthew Ahrens 	}
86714843421SMatthew Ahrens }
86814843421SMatthew Ahrens 
869fa9e4066Sahrens static int
870fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
871fa9e4066Sahrens {
872fa9e4066Sahrens 	int error;
873990b4856Slling 	nvlist_t *config, *props = NULL;
8740a48a24eStimh 	nvlist_t *rootprops = NULL;
8750a48a24eStimh 	nvlist_t *zplprops = NULL;
876228975ccSek 	char *buf;
877fa9e4066Sahrens 
878990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
879478ed9adSEric Taylor 	    zc->zc_iflags, &config))
880fa9e4066Sahrens 		return (error);
8812a6b87f0Sek 
882990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
883478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
884478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
885990b4856Slling 		nvlist_free(config);
886990b4856Slling 		return (error);
887990b4856Slling 	}
888990b4856Slling 
8890a48a24eStimh 	if (props) {
8900a48a24eStimh 		nvlist_t *nvl = NULL;
8910a48a24eStimh 		uint64_t version = SPA_VERSION;
8920a48a24eStimh 
8930a48a24eStimh 		(void) nvlist_lookup_uint64(props,
8940a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
8950a48a24eStimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
8960a48a24eStimh 			error = EINVAL;
8970a48a24eStimh 			goto pool_props_bad;
8980a48a24eStimh 		}
8990a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
9000a48a24eStimh 		if (nvl) {
9010a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
9020a48a24eStimh 			if (error != 0) {
9030a48a24eStimh 				nvlist_free(config);
9040a48a24eStimh 				nvlist_free(props);
9050a48a24eStimh 				return (error);
9060a48a24eStimh 			}
9070a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
9080a48a24eStimh 		}
9090a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9100a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
9110a48a24eStimh 		    zplprops, NULL);
9120a48a24eStimh 		if (error)
9130a48a24eStimh 			goto pool_props_bad;
9140a48a24eStimh 	}
9150a48a24eStimh 
9162a6b87f0Sek 	buf = history_str_get(zc);
917fa9e4066Sahrens 
9180a48a24eStimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
9190a48a24eStimh 
9200a48a24eStimh 	/*
9210a48a24eStimh 	 * Set the remaining root properties
9220a48a24eStimh 	 */
9230a48a24eStimh 	if (!error &&
9240a48a24eStimh 	    (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
9250a48a24eStimh 		(void) spa_destroy(zc->zc_name);
926fa9e4066Sahrens 
9272a6b87f0Sek 	if (buf != NULL)
9282a6b87f0Sek 		history_str_free(buf);
929990b4856Slling 
9300a48a24eStimh pool_props_bad:
9310a48a24eStimh 	nvlist_free(rootprops);
9320a48a24eStimh 	nvlist_free(zplprops);
933fa9e4066Sahrens 	nvlist_free(config);
9340a48a24eStimh 	nvlist_free(props);
935990b4856Slling 
936fa9e4066Sahrens 	return (error);
937fa9e4066Sahrens }
938fa9e4066Sahrens 
939fa9e4066Sahrens static int
940fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
941fa9e4066Sahrens {
942ecd6cf80Smarks 	int error;
943ecd6cf80Smarks 	zfs_log_history(zc);
944ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
945*681d9761SEric Taylor 	if (error == 0)
946*681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
947ecd6cf80Smarks 	return (error);
948fa9e4066Sahrens }
949fa9e4066Sahrens 
950fa9e4066Sahrens static int
951fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
952fa9e4066Sahrens {
953fa9e4066Sahrens 	int error;
954990b4856Slling 	nvlist_t *config, *props = NULL;
955fa9e4066Sahrens 	uint64_t guid;
956fa9e4066Sahrens 
957990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
958478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
959990b4856Slling 		return (error);
960990b4856Slling 
961990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
962478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
963478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
964990b4856Slling 		nvlist_free(config);
965fa9e4066Sahrens 		return (error);
966990b4856Slling 	}
967fa9e4066Sahrens 
968fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
969ea8dc4b6Seschrock 	    guid != zc->zc_guid)
970fa9e4066Sahrens 		error = EINVAL;
971c5904d13Seschrock 	else if (zc->zc_cookie)
9726809eb4eSEric Schrock 		error = spa_import_verbatim(zc->zc_name, config,
973c5904d13Seschrock 		    props);
974fa9e4066Sahrens 	else
975990b4856Slling 		error = spa_import(zc->zc_name, config, props);
976fa9e4066Sahrens 
977fa9e4066Sahrens 	nvlist_free(config);
978fa9e4066Sahrens 
979990b4856Slling 	if (props)
980990b4856Slling 		nvlist_free(props);
981990b4856Slling 
982fa9e4066Sahrens 	return (error);
983fa9e4066Sahrens }
984fa9e4066Sahrens 
985fa9e4066Sahrens static int
986fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
987fa9e4066Sahrens {
988ecd6cf80Smarks 	int error;
98989a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
990394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
99189a89ebfSlling 
992ecd6cf80Smarks 	zfs_log_history(zc);
993394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
994*681d9761SEric Taylor 	if (error == 0)
995*681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
996ecd6cf80Smarks 	return (error);
997fa9e4066Sahrens }
998fa9e4066Sahrens 
999fa9e4066Sahrens static int
1000fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1001fa9e4066Sahrens {
1002fa9e4066Sahrens 	nvlist_t *configs;
1003fa9e4066Sahrens 	int error;
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1006fa9e4066Sahrens 		return (EEXIST);
1007fa9e4066Sahrens 
1008e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1009fa9e4066Sahrens 
1010fa9e4066Sahrens 	nvlist_free(configs);
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	return (error);
1013fa9e4066Sahrens }
1014fa9e4066Sahrens 
1015fa9e4066Sahrens static int
1016fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1017fa9e4066Sahrens {
1018fa9e4066Sahrens 	nvlist_t *config;
1019fa9e4066Sahrens 	int error;
1020ea8dc4b6Seschrock 	int ret = 0;
1021fa9e4066Sahrens 
1022e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1023e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1024fa9e4066Sahrens 
1025fa9e4066Sahrens 	if (config != NULL) {
1026e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1027fa9e4066Sahrens 		nvlist_free(config);
1028ea8dc4b6Seschrock 
1029ea8dc4b6Seschrock 		/*
1030ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1031ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1032ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1033ea8dc4b6Seschrock 		 */
1034ea8dc4b6Seschrock 		zc->zc_cookie = error;
1035fa9e4066Sahrens 	} else {
1036ea8dc4b6Seschrock 		ret = error;
1037fa9e4066Sahrens 	}
1038fa9e4066Sahrens 
1039ea8dc4b6Seschrock 	return (ret);
1040fa9e4066Sahrens }
1041fa9e4066Sahrens 
1042fa9e4066Sahrens /*
1043fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1044fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1045fa9e4066Sahrens  */
1046fa9e4066Sahrens static int
1047fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1048fa9e4066Sahrens {
1049fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1050fa9e4066Sahrens 	int error;
1051fa9e4066Sahrens 
1052990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1053478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1054fa9e4066Sahrens 		return (error);
1055fa9e4066Sahrens 
1056fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	nvlist_free(tryconfig);
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	if (config == NULL)
1061fa9e4066Sahrens 		return (EINVAL);
1062fa9e4066Sahrens 
1063e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1064fa9e4066Sahrens 	nvlist_free(config);
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 	return (error);
1067fa9e4066Sahrens }
1068fa9e4066Sahrens 
1069fa9e4066Sahrens static int
1070fa9e4066Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1071fa9e4066Sahrens {
1072fa9e4066Sahrens 	spa_t *spa;
1073fa9e4066Sahrens 	int error;
1074fa9e4066Sahrens 
107506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
107606eeb2adSek 		return (error);
107706eeb2adSek 
1078088f3894Sahrens 	error = spa_scrub(spa, zc->zc_cookie);
107906eeb2adSek 
108006eeb2adSek 	spa_close(spa, FTAG);
108106eeb2adSek 
1082fa9e4066Sahrens 	return (error);
1083fa9e4066Sahrens }
1084fa9e4066Sahrens 
1085fa9e4066Sahrens static int
1086fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1087fa9e4066Sahrens {
1088fa9e4066Sahrens 	spa_t *spa;
1089fa9e4066Sahrens 	int error;
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1092fa9e4066Sahrens 	if (error == 0) {
1093fa9e4066Sahrens 		spa_freeze(spa);
1094fa9e4066Sahrens 		spa_close(spa, FTAG);
1095fa9e4066Sahrens 	}
1096fa9e4066Sahrens 	return (error);
1097fa9e4066Sahrens }
1098fa9e4066Sahrens 
1099eaca9bbdSeschrock static int
1100eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1101eaca9bbdSeschrock {
1102eaca9bbdSeschrock 	spa_t *spa;
1103eaca9bbdSeschrock 	int error;
1104eaca9bbdSeschrock 
110506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
110606eeb2adSek 		return (error);
110706eeb2adSek 
1108558d2d50Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1109558d2d50Slling 		spa_close(spa, FTAG);
1110558d2d50Slling 		return (EINVAL);
1111558d2d50Slling 	}
1112558d2d50Slling 
1113990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
111406eeb2adSek 	spa_close(spa, FTAG);
111506eeb2adSek 
111606eeb2adSek 	return (error);
111706eeb2adSek }
111806eeb2adSek 
111906eeb2adSek static int
112006eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
112106eeb2adSek {
112206eeb2adSek 	spa_t *spa;
112306eeb2adSek 	char *hist_buf;
112406eeb2adSek 	uint64_t size;
112506eeb2adSek 	int error;
112606eeb2adSek 
112706eeb2adSek 	if ((size = zc->zc_history_len) == 0)
112806eeb2adSek 		return (EINVAL);
112906eeb2adSek 
113006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
113106eeb2adSek 		return (error);
113206eeb2adSek 
1133e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1134d7306b64Sek 		spa_close(spa, FTAG);
1135d7306b64Sek 		return (ENOTSUP);
1136d7306b64Sek 	}
1137d7306b64Sek 
113806eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
113906eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
114006eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1141478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1142478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1143478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
114406eeb2adSek 	}
114506eeb2adSek 
114606eeb2adSek 	spa_close(spa, FTAG);
114706eeb2adSek 	kmem_free(hist_buf, size);
114806eeb2adSek 	return (error);
114906eeb2adSek }
115006eeb2adSek 
115155434c77Sek static int
115255434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
115355434c77Sek {
115455434c77Sek 	int error;
115555434c77Sek 
1156b1b8ab34Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
115755434c77Sek 		return (error);
115855434c77Sek 
115955434c77Sek 	return (0);
116055434c77Sek }
116155434c77Sek 
1162503ad85cSMatthew Ahrens /*
1163503ad85cSMatthew Ahrens  * inputs:
1164503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1165503ad85cSMatthew Ahrens  * zc_obj		object to find
1166503ad85cSMatthew Ahrens  *
1167503ad85cSMatthew Ahrens  * outputs:
1168503ad85cSMatthew Ahrens  * zc_value		name of object
1169503ad85cSMatthew Ahrens  */
117055434c77Sek static int
117155434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
117255434c77Sek {
1173503ad85cSMatthew Ahrens 	objset_t *os;
117455434c77Sek 	int error;
117555434c77Sek 
1176503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1177503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
117855434c77Sek 		return (error);
1179503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1180503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1181503ad85cSMatthew Ahrens 		return (EINVAL);
1182503ad85cSMatthew Ahrens 	}
1183503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
118455434c77Sek 	    sizeof (zc->zc_value));
1185503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
118655434c77Sek 
118755434c77Sek 	return (error);
118855434c77Sek }
118955434c77Sek 
1190fa9e4066Sahrens static int
1191fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1192fa9e4066Sahrens {
1193fa9e4066Sahrens 	spa_t *spa;
1194fa9e4066Sahrens 	int error;
1195e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1196e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1197fa9e4066Sahrens 
1198fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1199fa9e4066Sahrens 	if (error != 0)
1200fa9e4066Sahrens 		return (error);
1201fa9e4066Sahrens 
1202fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1203478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1204fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1205fa94a07fSbrendan 	    &l2cache, &nl2cache);
1206fa94a07fSbrendan 
1207e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1208e7cbe64fSgw 	    &spares, &nspares);
1209e7cbe64fSgw 
1210b1b8ab34Slling 	/*
1211b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1212e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1213e7cbe64fSgw 	 *
1214e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1215e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1216e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1217e7cbe64fSgw 	 *
1218e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1219b1b8ab34Slling 	 */
1220e7cbe64fSgw 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1221b1b8ab34Slling 		spa_close(spa, FTAG);
1222b1b8ab34Slling 		return (EDOM);
1223b1b8ab34Slling 	}
1224b1b8ab34Slling 
1225fa94a07fSbrendan 	if (error == 0) {
1226fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1227fa9e4066Sahrens 		nvlist_free(config);
1228fa9e4066Sahrens 	}
1229fa9e4066Sahrens 	spa_close(spa, FTAG);
1230fa9e4066Sahrens 	return (error);
1231fa9e4066Sahrens }
1232fa9e4066Sahrens 
1233fa9e4066Sahrens static int
1234fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1235fa9e4066Sahrens {
123699653d4eSeschrock 	spa_t *spa;
123799653d4eSeschrock 	int error;
123899653d4eSeschrock 
123999653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
124099653d4eSeschrock 	if (error != 0)
124199653d4eSeschrock 		return (error);
124299653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
124399653d4eSeschrock 	spa_close(spa, FTAG);
124499653d4eSeschrock 	return (error);
1245fa9e4066Sahrens }
1246fa9e4066Sahrens 
1247fa9e4066Sahrens static int
12483d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1249fa9e4066Sahrens {
1250*681d9761SEric Taylor 	boolean_t nslock;
1251fa9e4066Sahrens 	spa_t *spa;
1252fa9e4066Sahrens 	int error;
12533d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1254fa9e4066Sahrens 
125506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1256fa9e4066Sahrens 		return (error);
1257*681d9761SEric Taylor 	nslock = spa_uses_zvols(spa);
1258*681d9761SEric Taylor 	if (nslock)
1259*681d9761SEric Taylor 		mutex_enter(&spa_namespace_lock);
12603d7072f8Seschrock 	switch (zc->zc_cookie) {
12613d7072f8Seschrock 	case VDEV_STATE_ONLINE:
12623d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
12633d7072f8Seschrock 		break;
1264fa9e4066Sahrens 
12653d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
12663d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
12673d7072f8Seschrock 		break;
1268fa9e4066Sahrens 
12693d7072f8Seschrock 	case VDEV_STATE_FAULTED:
12703d7072f8Seschrock 		error = vdev_fault(spa, zc->zc_guid);
12713d7072f8Seschrock 		break;
12723d7072f8Seschrock 
12733d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
12743d7072f8Seschrock 		error = vdev_degrade(spa, zc->zc_guid);
12753d7072f8Seschrock 		break;
12763d7072f8Seschrock 
12773d7072f8Seschrock 	default:
12783d7072f8Seschrock 		error = EINVAL;
12793d7072f8Seschrock 	}
1280*681d9761SEric Taylor 	if (nslock)
1281*681d9761SEric Taylor 		mutex_exit(&spa_namespace_lock);
12823d7072f8Seschrock 	zc->zc_cookie = newstate;
1283fa9e4066Sahrens 	spa_close(spa, FTAG);
1284fa9e4066Sahrens 	return (error);
1285fa9e4066Sahrens }
1286fa9e4066Sahrens 
1287fa9e4066Sahrens static int
1288fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1289fa9e4066Sahrens {
1290fa9e4066Sahrens 	spa_t *spa;
1291fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1292fa9e4066Sahrens 	nvlist_t *config;
1293fa9e4066Sahrens 	int error;
1294fa9e4066Sahrens 
129506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1296fa9e4066Sahrens 		return (error);
1297fa9e4066Sahrens 
1298990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1299478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1300ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1301fa9e4066Sahrens 		nvlist_free(config);
1302fa9e4066Sahrens 	}
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	spa_close(spa, FTAG);
1305fa9e4066Sahrens 	return (error);
1306fa9e4066Sahrens }
1307fa9e4066Sahrens 
1308fa9e4066Sahrens static int
1309fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1310fa9e4066Sahrens {
1311fa9e4066Sahrens 	spa_t *spa;
1312fa9e4066Sahrens 	int error;
1313fa9e4066Sahrens 
131406eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1315fa9e4066Sahrens 		return (error);
1316fa9e4066Sahrens 
13178ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1318fa9e4066Sahrens 
1319fa9e4066Sahrens 	spa_close(spa, FTAG);
1320fa9e4066Sahrens 	return (error);
1321fa9e4066Sahrens }
1322fa9e4066Sahrens 
1323c67d9675Seschrock static int
1324c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1325c67d9675Seschrock {
1326c67d9675Seschrock 	spa_t *spa;
1327e9dbad6fSeschrock 	char *path = zc->zc_value;
1328ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
1329c67d9675Seschrock 	int error;
1330c67d9675Seschrock 
1331c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
1332c67d9675Seschrock 	if (error != 0)
1333c67d9675Seschrock 		return (error);
1334c67d9675Seschrock 
1335c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
1336c67d9675Seschrock 	spa_close(spa, FTAG);
1337c67d9675Seschrock 	return (error);
1338c67d9675Seschrock }
1339c67d9675Seschrock 
13406809eb4eSEric Schrock static int
13416809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
13426809eb4eSEric Schrock {
13436809eb4eSEric Schrock 	spa_t *spa;
13446809eb4eSEric Schrock 	char *fru = zc->zc_value;
13456809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
13466809eb4eSEric Schrock 	int error;
13476809eb4eSEric Schrock 
13486809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
13496809eb4eSEric Schrock 	if (error != 0)
13506809eb4eSEric Schrock 		return (error);
13516809eb4eSEric Schrock 
13526809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
13536809eb4eSEric Schrock 	spa_close(spa, FTAG);
13546809eb4eSEric Schrock 	return (error);
13556809eb4eSEric Schrock }
13566809eb4eSEric Schrock 
13573cb34c60Sahrens /*
13583cb34c60Sahrens  * inputs:
13593cb34c60Sahrens  * zc_name		name of filesystem
13603cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
13613cb34c60Sahrens  *
13623cb34c60Sahrens  * outputs:
13633cb34c60Sahrens  * zc_objset_stats	stats
13643cb34c60Sahrens  * zc_nvlist_dst	property nvlist
13653cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
13663cb34c60Sahrens  */
1367fa9e4066Sahrens static int
1368fa9e4066Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc)
1369fa9e4066Sahrens {
1370fa9e4066Sahrens 	objset_t *os = NULL;
1371fa9e4066Sahrens 	int error;
13727f7322feSeschrock 	nvlist_t *nv;
1373fa9e4066Sahrens 
1374503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1375fa9e4066Sahrens 		return (error);
1376fa9e4066Sahrens 
1377a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1378fa9e4066Sahrens 
13795ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
1380745cd3c5Smaybee 	    (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1381a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
1382432f72fdSahrens 		/*
1383bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
1384432f72fdSahrens 		 * which we aren't supposed to do with a
1385745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
1386432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
1387503ad85cSMatthew Ahrens 		 * XXX reading with out owning
1388432f72fdSahrens 		 */
1389e7437265Sahrens 		if (!zc->zc_objset_stats.dds_inconsistent) {
1390e7437265Sahrens 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1391e7437265Sahrens 				VERIFY(zvol_get_stats(os, nv) == 0);
1392e7437265Sahrens 		}
1393e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
13947f7322feSeschrock 		nvlist_free(nv);
13957f7322feSeschrock 	}
1396fa9e4066Sahrens 
1397503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1398fa9e4066Sahrens 	return (error);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
1401de8267e0Stimh static int
1402de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1403de8267e0Stimh {
1404de8267e0Stimh 	uint64_t value;
1405de8267e0Stimh 	int error;
1406de8267e0Stimh 
1407de8267e0Stimh 	/*
1408de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
1409de8267e0Stimh 	 * the default value (if there is one).
1410de8267e0Stimh 	 */
1411de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1412de8267e0Stimh 		return (error);
1413de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1414de8267e0Stimh 	return (0);
1415de8267e0Stimh }
1416de8267e0Stimh 
14173cb34c60Sahrens /*
14183cb34c60Sahrens  * inputs:
14193cb34c60Sahrens  * zc_name		name of filesystem
1420de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
14213cb34c60Sahrens  *
14223cb34c60Sahrens  * outputs:
1423de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
1424de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
14253cb34c60Sahrens  */
1426bd00f61bSrm static int
1427de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1428bd00f61bSrm {
1429de8267e0Stimh 	objset_t *os;
1430de8267e0Stimh 	int err;
1431bd00f61bSrm 
1432503ad85cSMatthew Ahrens 	/* XXX reading without owning */
1433503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1434de8267e0Stimh 		return (err);
1435bd00f61bSrm 
1436bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1437bd00f61bSrm 
1438bd00f61bSrm 	/*
1439de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
1440745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
1441745cd3c5Smaybee 	 * hold, because it could be inconsistent.
1442bd00f61bSrm 	 */
1443de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
1444de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
1445de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
1446de8267e0Stimh 		nvlist_t *nv;
1447de8267e0Stimh 
1448de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1449de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1450de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1451de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1452de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1453de8267e0Stimh 			err = put_nvlist(zc, nv);
1454de8267e0Stimh 		nvlist_free(nv);
1455de8267e0Stimh 	} else {
1456de8267e0Stimh 		err = ENOENT;
1457de8267e0Stimh 	}
1458503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1459de8267e0Stimh 	return (err);
1460bd00f61bSrm }
1461bd00f61bSrm 
146214843421SMatthew Ahrens static boolean_t
146314843421SMatthew Ahrens dataset_name_hidden(const char *name)
146414843421SMatthew Ahrens {
146514843421SMatthew Ahrens 	/*
146614843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
146714843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
146814843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
146914843421SMatthew Ahrens 	 */
147014843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
147114843421SMatthew Ahrens 		return (B_TRUE);
147214843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
147314843421SMatthew Ahrens 		return (B_TRUE);
147414843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
147514843421SMatthew Ahrens 		return (B_TRUE);
147614843421SMatthew Ahrens 	return (B_FALSE);
147714843421SMatthew Ahrens }
147814843421SMatthew Ahrens 
1479de8267e0Stimh /*
1480de8267e0Stimh  * inputs:
1481de8267e0Stimh  * zc_name		name of filesystem
1482de8267e0Stimh  * zc_cookie		zap cursor
1483de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
1484de8267e0Stimh  *
1485de8267e0Stimh  * outputs:
1486de8267e0Stimh  * zc_name		name of next filesystem
148714843421SMatthew Ahrens  * zc_cookie		zap cursor
1488de8267e0Stimh  * zc_objset_stats	stats
1489de8267e0Stimh  * zc_nvlist_dst	property nvlist
1490de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
1491de8267e0Stimh  */
1492fa9e4066Sahrens static int
1493fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1494fa9e4066Sahrens {
149587e5029aSahrens 	objset_t *os;
1496fa9e4066Sahrens 	int error;
1497fa9e4066Sahrens 	char *p;
1498fa9e4066Sahrens 
1499503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
150087e5029aSahrens 		if (error == ENOENT)
150187e5029aSahrens 			error = ESRCH;
150287e5029aSahrens 		return (error);
1503fa9e4066Sahrens 	}
1504fa9e4066Sahrens 
1505fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
1506fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
1507fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1508fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1509fa9e4066Sahrens 
15105c0b6a79SRich Morris 	/*
15115c0b6a79SRich Morris 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
15125c0b6a79SRich Morris 	 * but is not declared void because its called by dmu_objset_find().
15135c0b6a79SRich Morris 	 */
15147f73c863SRich Morris 	if (zc->zc_cookie == 0) {
15157f73c863SRich Morris 		uint64_t cookie = 0;
15167f73c863SRich Morris 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
15177f73c863SRich Morris 
15187f73c863SRich Morris 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
15195c0b6a79SRich Morris 			(void) dmu_objset_prefetch(p, NULL);
15207f73c863SRich Morris 	}
15217f73c863SRich Morris 
1522fa9e4066Sahrens 	do {
152387e5029aSahrens 		error = dmu_dir_list_next(os,
152487e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
152587e5029aSahrens 		    NULL, &zc->zc_cookie);
1526fa9e4066Sahrens 		if (error == ENOENT)
1527fa9e4066Sahrens 			error = ESRCH;
1528*681d9761SEric Taylor 	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1529*681d9761SEric Taylor 	    !(zc->zc_iflags & FKIOCTL));
1530503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1531fa9e4066Sahrens 
1532*681d9761SEric Taylor 	/*
1533*681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
1534*681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
1535*681d9761SEric Taylor 	 */
1536*681d9761SEric Taylor 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
153787e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1538fa9e4066Sahrens 	return (error);
1539fa9e4066Sahrens }
1540fa9e4066Sahrens 
15413cb34c60Sahrens /*
15423cb34c60Sahrens  * inputs:
15433cb34c60Sahrens  * zc_name		name of filesystem
15443cb34c60Sahrens  * zc_cookie		zap cursor
15453cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
15463cb34c60Sahrens  *
15473cb34c60Sahrens  * outputs:
15483cb34c60Sahrens  * zc_name		name of next snapshot
15493cb34c60Sahrens  * zc_objset_stats	stats
15503cb34c60Sahrens  * zc_nvlist_dst	property nvlist
15513cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
15523cb34c60Sahrens  */
1553fa9e4066Sahrens static int
1554fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1555fa9e4066Sahrens {
155687e5029aSahrens 	objset_t *os;
1557fa9e4066Sahrens 	int error;
1558fa9e4066Sahrens 
15597cbf8b43SRich Morris 	if (zc->zc_cookie == 0)
15607cbf8b43SRich Morris 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
15617cbf8b43SRich Morris 		    NULL, DS_FIND_SNAPSHOTS);
15627cbf8b43SRich Morris 
1563503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
1564745cd3c5Smaybee 	if (error)
1565745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
1566fa9e4066Sahrens 
1567b81d61a6Slling 	/*
1568b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
1569b81d61a6Slling 	 * so exit immediately.
1570b81d61a6Slling 	 */
1571b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1572503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1573b81d61a6Slling 		return (ESRCH);
1574fa9e4066Sahrens 	}
1575fa9e4066Sahrens 
157687e5029aSahrens 	error = dmu_snapshot_list_next(os,
157787e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1578b38f0970Sck 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1579503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
158087e5029aSahrens 	if (error == 0)
158187e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1582745cd3c5Smaybee 	else if (error == ENOENT)
1583745cd3c5Smaybee 		error = ESRCH;
1584fa9e4066Sahrens 
15853cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
1586745cd3c5Smaybee 	if (error)
15873cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
1588fa9e4066Sahrens 	return (error);
1589fa9e4066Sahrens }
1590fa9e4066Sahrens 
1591e7cbe64fSgw int
159291ebeef5Sahrens zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1593fa9e4066Sahrens {
1594e9dbad6fSeschrock 	nvpair_t *elem;
15955f389de5SRich Morris 	int error = 0;
1596e9dbad6fSeschrock 	uint64_t intval;
1597e9dbad6fSeschrock 	char *strval;
15985c0b6a79SRich Morris 	nvlist_t *genericnvl;
159914843421SMatthew Ahrens 	boolean_t issnap = (strchr(name, '@') != NULL);
1600e9dbad6fSeschrock 
1601ecd6cf80Smarks 	/*
1602ecd6cf80Smarks 	 * First validate permission to set all of the properties
1603ecd6cf80Smarks 	 */
1604e9dbad6fSeschrock 	elem = NULL;
1605e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1606db870a07Sahrens 		const char *propname = nvpair_name(elem);
1607db870a07Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
1608e9dbad6fSeschrock 
1609990b4856Slling 		if (prop == ZPROP_INVAL) {
1610e9dbad6fSeschrock 			/*
1611e9dbad6fSeschrock 			 * If this is a user-defined property, it must be a
1612e9dbad6fSeschrock 			 * string, and there is no further validation to do.
1613e9dbad6fSeschrock 			 */
161414843421SMatthew Ahrens 			if (zfs_prop_user(propname) &&
161514843421SMatthew Ahrens 			    nvpair_type(elem) == DATA_TYPE_STRING) {
161614843421SMatthew Ahrens 				if (error = zfs_secpolicy_write_perms(name,
161714843421SMatthew Ahrens 				    ZFS_DELEG_PERM_USERPROP, CRED()))
161814843421SMatthew Ahrens 					return (error);
161914843421SMatthew Ahrens 				continue;
162014843421SMatthew Ahrens 			}
1621e9dbad6fSeschrock 
162214843421SMatthew Ahrens 			if (!issnap && zfs_prop_userquota(propname) &&
162314843421SMatthew Ahrens 			    nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
162414843421SMatthew Ahrens 				const char *perm;
162514843421SMatthew Ahrens 				const char *up = zfs_userquota_prop_prefixes
162614843421SMatthew Ahrens 				    [ZFS_PROP_USERQUOTA];
162714843421SMatthew Ahrens 				if (strncmp(propname, up, strlen(up)) == 0)
162814843421SMatthew Ahrens 					perm = ZFS_DELEG_PERM_USERQUOTA;
162914843421SMatthew Ahrens 				else
163014843421SMatthew Ahrens 					perm = ZFS_DELEG_PERM_GROUPQUOTA;
163114843421SMatthew Ahrens 				if (error = zfs_secpolicy_write_perms(name,
163214843421SMatthew Ahrens 				    perm, CRED()))
163314843421SMatthew Ahrens 					return (error);
163414843421SMatthew Ahrens 				continue;
163514843421SMatthew Ahrens 			}
163614843421SMatthew Ahrens 
163714843421SMatthew Ahrens 			return (EINVAL);
1638e9dbad6fSeschrock 		}
1639fa9e4066Sahrens 
164014843421SMatthew Ahrens 		if (issnap)
164114843421SMatthew Ahrens 			return (EINVAL);
164214843421SMatthew Ahrens 
164391ebeef5Sahrens 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1644db870a07Sahrens 			return (error);
1645db870a07Sahrens 
1646e9dbad6fSeschrock 		/*
1647db870a07Sahrens 		 * Check that this value is valid for this pool version
1648e9dbad6fSeschrock 		 */
1649e9dbad6fSeschrock 		switch (prop) {
1650c9431fa1Sahl 		case ZFS_PROP_COMPRESSION:
1651c9431fa1Sahl 			/*
1652c9431fa1Sahl 			 * If the user specified gzip compression, make sure
1653c9431fa1Sahl 			 * the SPA supports it. We ignore any errors here since
1654c9431fa1Sahl 			 * we'll catch them later.
1655c9431fa1Sahl 			 */
1656c9431fa1Sahl 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
165715e6edf1Sgw 			    nvpair_value_uint64(elem, &intval) == 0) {
165815e6edf1Sgw 				if (intval >= ZIO_COMPRESS_GZIP_1 &&
165915e6edf1Sgw 				    intval <= ZIO_COMPRESS_GZIP_9 &&
16600a48a24eStimh 				    zfs_earlier_version(name,
1661da6c28aaSamw 				    SPA_VERSION_GZIP_COMPRESSION))
1662da6c28aaSamw 					return (ENOTSUP);
166315e6edf1Sgw 
166415e6edf1Sgw 				/*
166515e6edf1Sgw 				 * If this is a bootable dataset then
166615e6edf1Sgw 				 * verify that the compression algorithm
166715e6edf1Sgw 				 * is supported for booting. We must return
166815e6edf1Sgw 				 * something other than ENOTSUP since it
166915e6edf1Sgw 				 * implies a downrev pool version.
167015e6edf1Sgw 				 */
167115e6edf1Sgw 				if (zfs_is_bootfs(name) &&
167215e6edf1Sgw 				    !BOOTFS_COMPRESS_VALID(intval))
167315e6edf1Sgw 					return (ERANGE);
1674c9431fa1Sahl 			}
1675c9431fa1Sahl 			break;
167640feaa91Sahrens 
167740feaa91Sahrens 		case ZFS_PROP_COPIES:
167814843421SMatthew Ahrens 			if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
1679da6c28aaSamw 				return (ENOTSUP);
168040feaa91Sahrens 			break;
16819e6eda55Smarks 
16829e6eda55Smarks 		case ZFS_PROP_SHARESMB:
1683745cd3c5Smaybee 			if (zpl_earlier_version(name, ZPL_VERSION_FUID))
16849e6eda55Smarks 				return (ENOTSUP);
16859e6eda55Smarks 			break;
1686d0f3f37eSMark Shellenbaum 
1687d0f3f37eSMark Shellenbaum 		case ZFS_PROP_ACLINHERIT:
1688d0f3f37eSMark Shellenbaum 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1689d0f3f37eSMark Shellenbaum 			    nvpair_value_uint64(elem, &intval) == 0)
1690d0f3f37eSMark Shellenbaum 				if (intval == ZFS_ACL_PASSTHROUGH_X &&
1691d0f3f37eSMark Shellenbaum 				    zfs_earlier_version(name,
1692d0f3f37eSMark Shellenbaum 				    SPA_VERSION_PASSTHROUGH_X))
1693d0f3f37eSMark Shellenbaum 					return (ENOTSUP);
169440feaa91Sahrens 		}
1695ecd6cf80Smarks 	}
1696ecd6cf80Smarks 
16975c0b6a79SRich Morris 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1698ecd6cf80Smarks 	elem = NULL;
1699ecd6cf80Smarks 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1700db870a07Sahrens 		const char *propname = nvpair_name(elem);
1701db870a07Sahrens 		zfs_prop_t prop = zfs_name_to_prop(propname);
1702ecd6cf80Smarks 
1703990b4856Slling 		if (prop == ZPROP_INVAL) {
170414843421SMatthew Ahrens 			if (zfs_prop_userquota(propname)) {
170514843421SMatthew Ahrens 				uint64_t *valary;
170614843421SMatthew Ahrens 				unsigned int vallen;
170714843421SMatthew Ahrens 				const char *domain;
170814843421SMatthew Ahrens 				zfs_userquota_prop_t type;
170914843421SMatthew Ahrens 				uint64_t rid;
171014843421SMatthew Ahrens 				uint64_t quota;
171114843421SMatthew Ahrens 				zfsvfs_t *zfsvfs;
171214843421SMatthew Ahrens 
171314843421SMatthew Ahrens 				VERIFY(nvpair_value_uint64_array(elem,
171414843421SMatthew Ahrens 				    &valary, &vallen) == 0);
171514843421SMatthew Ahrens 				VERIFY(vallen == 3);
171614843421SMatthew Ahrens 				type = valary[0];
171714843421SMatthew Ahrens 				rid = valary[1];
171814843421SMatthew Ahrens 				quota = valary[2];
171914843421SMatthew Ahrens 				domain = propname +
172014843421SMatthew Ahrens 				    strlen(zfs_userquota_prop_prefixes[type]);
172114843421SMatthew Ahrens 
1722503ad85cSMatthew Ahrens 				error = zfsvfs_hold(name, FTAG, &zfsvfs);
172314843421SMatthew Ahrens 				if (error == 0) {
172414843421SMatthew Ahrens 					error = zfs_set_userquota(zfsvfs,
172514843421SMatthew Ahrens 					    type, domain, rid, quota);
172614843421SMatthew Ahrens 					zfsvfs_rele(zfsvfs, FTAG);
172714843421SMatthew Ahrens 				}
172814843421SMatthew Ahrens 				if (error == 0)
172914843421SMatthew Ahrens 					continue;
173014843421SMatthew Ahrens 				else
173114843421SMatthew Ahrens 					goto out;
173214843421SMatthew Ahrens 			} else if (zfs_prop_user(propname)) {
173314843421SMatthew Ahrens 				VERIFY(nvpair_value_string(elem, &strval) == 0);
173414843421SMatthew Ahrens 				error = dsl_prop_set(name, propname, 1,
173514843421SMatthew Ahrens 				    strlen(strval) + 1, strval);
173614843421SMatthew Ahrens 				if (error == 0)
173714843421SMatthew Ahrens 					continue;
173814843421SMatthew Ahrens 				else
173914843421SMatthew Ahrens 					goto out;
174014843421SMatthew Ahrens 			}
1741ecd6cf80Smarks 		}
1742e9dbad6fSeschrock 
1743e9dbad6fSeschrock 		switch (prop) {
1744e9dbad6fSeschrock 		case ZFS_PROP_QUOTA:
1745e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1746e7437265Sahrens 			    (error = dsl_dir_set_quota(name, intval)) != 0)
17475c0b6a79SRich Morris 				goto out;
1748e9dbad6fSeschrock 			break;
1749e9dbad6fSeschrock 
1750a9799022Sck 		case ZFS_PROP_REFQUOTA:
1751a9799022Sck 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1752a9799022Sck 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
17535c0b6a79SRich Morris 				goto out;
1754a9799022Sck 			break;
1755a9799022Sck 
1756e9dbad6fSeschrock 		case ZFS_PROP_RESERVATION:
1757e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1758e9dbad6fSeschrock 			    (error = dsl_dir_set_reservation(name,
1759e9dbad6fSeschrock 			    intval)) != 0)
17605c0b6a79SRich Morris 				goto out;
1761e9dbad6fSeschrock 			break;
1762e9dbad6fSeschrock 
1763a9799022Sck 		case ZFS_PROP_REFRESERVATION:
1764a9799022Sck 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1765a9799022Sck 			    (error = dsl_dataset_set_reservation(name,
1766a9799022Sck 			    intval)) != 0)
17675c0b6a79SRich Morris 				goto out;
1768a9799022Sck 			break;
1769a9799022Sck 
1770e9dbad6fSeschrock 		case ZFS_PROP_VOLSIZE:
1771e9dbad6fSeschrock 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
177291ebeef5Sahrens 			    (error = zvol_set_volsize(name,
177391ebeef5Sahrens 			    ddi_driver_major(zfs_dip), intval)) != 0)
17745c0b6a79SRich Morris 				goto out;
1775e9dbad6fSeschrock 			break;
1776e9dbad6fSeschrock 
1777e7437265Sahrens 		case ZFS_PROP_VERSION:
177814843421SMatthew Ahrens 		{
177914843421SMatthew Ahrens 			zfsvfs_t *zfsvfs;
178014843421SMatthew Ahrens 
178114843421SMatthew Ahrens 			if ((error = nvpair_value_uint64(elem, &intval)) != 0)
178214843421SMatthew Ahrens 				goto out;
1783503ad85cSMatthew Ahrens 			if ((error = zfsvfs_hold(name, FTAG, &zfsvfs)) != 0)
178414843421SMatthew Ahrens 				goto out;
178514843421SMatthew Ahrens 			error = zfs_set_version(zfsvfs, intval);
178614843421SMatthew Ahrens 			zfsvfs_rele(zfsvfs, FTAG);
178714843421SMatthew Ahrens 
178814843421SMatthew Ahrens 			if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
178914843421SMatthew Ahrens 				zfs_cmd_t zc = { 0 };
179014843421SMatthew Ahrens 				(void) strcpy(zc.zc_name, name);
179114843421SMatthew Ahrens 				(void) zfs_ioc_userspace_upgrade(&zc);
179214843421SMatthew Ahrens 			}
179314843421SMatthew Ahrens 			if (error)
17945c0b6a79SRich Morris 				goto out;
1795e9dbad6fSeschrock 			break;
179614843421SMatthew Ahrens 		}
1797e9dbad6fSeschrock 
1798e9dbad6fSeschrock 		default:
1799e9dbad6fSeschrock 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1800e9dbad6fSeschrock 				if (zfs_prop_get_type(prop) !=
18015c0b6a79SRich Morris 				    PROP_TYPE_STRING) {
18025c0b6a79SRich Morris 					error = EINVAL;
18035c0b6a79SRich Morris 					goto out;
18045c0b6a79SRich Morris 				}
1805e9dbad6fSeschrock 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1806a2eea2e1Sahrens 				const char *unused;
1807a2eea2e1Sahrens 
1808acd76fe5Seschrock 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1809e9dbad6fSeschrock 
1810e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
181191ebeef5Sahrens 				case PROP_TYPE_NUMBER:
1812e9dbad6fSeschrock 					break;
181391ebeef5Sahrens 				case PROP_TYPE_STRING:
18145c0b6a79SRich Morris 					error = EINVAL;
18155c0b6a79SRich Morris 					goto out;
181691ebeef5Sahrens 				case PROP_TYPE_INDEX:
1817acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
18185c0b6a79SRich Morris 					    intval, &unused) != 0) {
18195c0b6a79SRich Morris 						error = EINVAL;
18205c0b6a79SRich Morris 						goto out;
18215c0b6a79SRich Morris 					}
1822e9dbad6fSeschrock 					break;
1823e9dbad6fSeschrock 				default:
1824e7437265Sahrens 					cmn_err(CE_PANIC,
1825e7437265Sahrens 					    "unknown property type");
1826e9dbad6fSeschrock 					break;
1827e9dbad6fSeschrock 				}
1828e9dbad6fSeschrock 			} else {
18295c0b6a79SRich Morris 				error = EINVAL;
18305c0b6a79SRich Morris 				goto out;
1831e9dbad6fSeschrock 			}
18325c0b6a79SRich Morris 			if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
18335c0b6a79SRich Morris 				goto out;
1834e9dbad6fSeschrock 		}
1835e9dbad6fSeschrock 	}
1836e9dbad6fSeschrock 
18375c0b6a79SRich Morris 	if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
18385c0b6a79SRich Morris 		error = dsl_props_set(name, genericnvl);
18395c0b6a79SRich Morris 	}
18405c0b6a79SRich Morris out:
18415c0b6a79SRich Morris 	nvlist_free(genericnvl);
18425c0b6a79SRich Morris 	return (error);
1843fa9e4066Sahrens }
1844fa9e4066Sahrens 
1845ea2f5b9eSMatthew Ahrens /*
1846ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
1847ea2f5b9eSMatthew Ahrens  */
1848ea2f5b9eSMatthew Ahrens static int
1849ea2f5b9eSMatthew Ahrens zfs_check_userprops(char *fsname, nvlist_t *nvl)
1850ea2f5b9eSMatthew Ahrens {
1851ea2f5b9eSMatthew Ahrens 	nvpair_t *elem = NULL;
1852ea2f5b9eSMatthew Ahrens 	int error = 0;
1853ea2f5b9eSMatthew Ahrens 
1854ea2f5b9eSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1855ea2f5b9eSMatthew Ahrens 		const char *propname = nvpair_name(elem);
1856ea2f5b9eSMatthew Ahrens 		char *valstr;
1857ea2f5b9eSMatthew Ahrens 
1858ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
1859ea2f5b9eSMatthew Ahrens 		    nvpair_type(elem) != DATA_TYPE_STRING)
1860ea2f5b9eSMatthew Ahrens 			return (EINVAL);
1861ea2f5b9eSMatthew Ahrens 
1862ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
1863ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
1864ea2f5b9eSMatthew Ahrens 			return (error);
1865ea2f5b9eSMatthew Ahrens 
1866ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
1867ea2f5b9eSMatthew Ahrens 			return (ENAMETOOLONG);
1868ea2f5b9eSMatthew Ahrens 
1869ea2f5b9eSMatthew Ahrens 		VERIFY(nvpair_value_string(elem, &valstr) == 0);
1870ea2f5b9eSMatthew Ahrens 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
1871ea2f5b9eSMatthew Ahrens 			return (E2BIG);
1872ea2f5b9eSMatthew Ahrens 	}
1873ea2f5b9eSMatthew Ahrens 	return (0);
1874ea2f5b9eSMatthew Ahrens }
1875ea2f5b9eSMatthew Ahrens 
18763cb34c60Sahrens /*
18773cb34c60Sahrens  * inputs:
18783cb34c60Sahrens  * zc_name		name of filesystem
18795c0b6a79SRich Morris  * zc_value		name of property to set
18803cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
1881bb0ade09Sahrens  * zc_cookie		clear existing local props?
18823cb34c60Sahrens  *
18833cb34c60Sahrens  * outputs:		none
18843cb34c60Sahrens  */
1885fa9e4066Sahrens static int
1886e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
1887fa9e4066Sahrens {
1888e9dbad6fSeschrock 	nvlist_t *nvl;
1889e9dbad6fSeschrock 	int error;
1890e9dbad6fSeschrock 
1891990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1892478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
1893e9dbad6fSeschrock 		return (error);
1894e9dbad6fSeschrock 
1895bb0ade09Sahrens 	if (zc->zc_cookie) {
1896bb0ade09Sahrens 		nvlist_t *origprops;
1897bb0ade09Sahrens 		objset_t *os;
1898bb0ade09Sahrens 
1899503ad85cSMatthew Ahrens 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
1900bb0ade09Sahrens 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
19016e77af0aSDavid Pacheco 				clear_props(zc->zc_name, origprops, nvl);
1902bb0ade09Sahrens 				nvlist_free(origprops);
1903bb0ade09Sahrens 			}
1904503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
1905bb0ade09Sahrens 		}
1906bb0ade09Sahrens 
1907bb0ade09Sahrens 	}
1908bb0ade09Sahrens 
190991ebeef5Sahrens 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1910ecd6cf80Smarks 
1911e9dbad6fSeschrock 	nvlist_free(nvl);
1912e9dbad6fSeschrock 	return (error);
1913fa9e4066Sahrens }
1914fa9e4066Sahrens 
19153cb34c60Sahrens /*
19163cb34c60Sahrens  * inputs:
19173cb34c60Sahrens  * zc_name		name of filesystem
19183cb34c60Sahrens  * zc_value		name of property to inherit
19193cb34c60Sahrens  *
19203cb34c60Sahrens  * outputs:		none
19213cb34c60Sahrens  */
1922e45ce728Sahrens static int
1923e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1924e45ce728Sahrens {
1925e45ce728Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
1926e45ce728Sahrens 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1927e45ce728Sahrens }
1928e45ce728Sahrens 
1929b1b8ab34Slling static int
193011a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1931b1b8ab34Slling {
1932990b4856Slling 	nvlist_t *props;
1933b1b8ab34Slling 	spa_t *spa;
1934990b4856Slling 	int error;
1935379c004dSEric Schrock 	nvpair_t *elem;
1936b1b8ab34Slling 
1937990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1938478ed9adSEric Taylor 	    zc->zc_iflags, &props)))
1939b1b8ab34Slling 		return (error);
1940b1b8ab34Slling 
1941379c004dSEric Schrock 	/*
1942379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
1943379c004dSEric Schrock 	 * to handle the faulted case.
1944379c004dSEric Schrock 	 */
1945379c004dSEric Schrock 	elem = nvlist_next_nvpair(props, NULL);
1946379c004dSEric Schrock 	if (elem != NULL && strcmp(nvpair_name(elem),
1947379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
1948379c004dSEric Schrock 	    nvlist_next_nvpair(props, elem) == NULL) {
1949379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
1950379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
1951379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
1952379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
1953379c004dSEric Schrock 		}
1954379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
1955379c004dSEric Schrock 		if (spa != NULL)
1956379c004dSEric Schrock 			return (0);
1957379c004dSEric Schrock 	}
1958379c004dSEric Schrock 
1959b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1960990b4856Slling 		nvlist_free(props);
1961b1b8ab34Slling 		return (error);
1962b1b8ab34Slling 	}
1963b1b8ab34Slling 
1964990b4856Slling 	error = spa_prop_set(spa, props);
1965b1b8ab34Slling 
1966990b4856Slling 	nvlist_free(props);
1967b1b8ab34Slling 	spa_close(spa, FTAG);
1968b1b8ab34Slling 
1969b1b8ab34Slling 	return (error);
1970b1b8ab34Slling }
1971b1b8ab34Slling 
1972b1b8ab34Slling static int
197311a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1974b1b8ab34Slling {
1975b1b8ab34Slling 	spa_t *spa;
1976b1b8ab34Slling 	int error;
1977b1b8ab34Slling 	nvlist_t *nvp = NULL;
1978b1b8ab34Slling 
1979379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1980379c004dSEric Schrock 		/*
1981379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
1982379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
1983379c004dSEric Schrock 		 * anyway.
1984379c004dSEric Schrock 		 */
1985379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
1986379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
1987379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
1988379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
1989379c004dSEric Schrock 	} else {
1990379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
1991379c004dSEric Schrock 		spa_close(spa, FTAG);
1992379c004dSEric Schrock 	}
1993b1b8ab34Slling 
1994b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1995b1b8ab34Slling 		error = put_nvlist(zc, nvp);
1996b1b8ab34Slling 	else
1997b1b8ab34Slling 		error = EFAULT;
1998b1b8ab34Slling 
1999379c004dSEric Schrock 	nvlist_free(nvp);
2000b1b8ab34Slling 	return (error);
2001b1b8ab34Slling }
2002b1b8ab34Slling 
2003ecd6cf80Smarks static int
2004ecd6cf80Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
2005ecd6cf80Smarks {
2006ecd6cf80Smarks 	nvlist_t *nvp;
2007ecd6cf80Smarks 	int error;
2008ecd6cf80Smarks 	uint32_t uid;
2009ecd6cf80Smarks 	uint32_t gid;
2010ecd6cf80Smarks 	uint32_t *groups;
2011ecd6cf80Smarks 	uint_t group_cnt;
2012ecd6cf80Smarks 	cred_t	*usercred;
2013ecd6cf80Smarks 
2014990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2015478ed9adSEric Taylor 	    zc->zc_iflags, &nvp)) != 0) {
2016ecd6cf80Smarks 		return (error);
2017ecd6cf80Smarks 	}
2018ecd6cf80Smarks 
2019ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32(nvp,
2020ecd6cf80Smarks 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
2021ecd6cf80Smarks 		nvlist_free(nvp);
2022ecd6cf80Smarks 		return (EPERM);
2023ecd6cf80Smarks 	}
2024ecd6cf80Smarks 
2025ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32(nvp,
2026ecd6cf80Smarks 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
2027ecd6cf80Smarks 		nvlist_free(nvp);
2028ecd6cf80Smarks 		return (EPERM);
2029ecd6cf80Smarks 	}
2030ecd6cf80Smarks 
2031ecd6cf80Smarks 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
2032ecd6cf80Smarks 	    &groups, &group_cnt)) != 0) {
2033ecd6cf80Smarks 		nvlist_free(nvp);
2034ecd6cf80Smarks 		return (EPERM);
2035ecd6cf80Smarks 	}
2036ecd6cf80Smarks 	usercred = cralloc();
2037ecd6cf80Smarks 	if ((crsetugid(usercred, uid, gid) != 0) ||
2038ecd6cf80Smarks 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
2039ecd6cf80Smarks 		nvlist_free(nvp);
2040ecd6cf80Smarks 		crfree(usercred);
2041ecd6cf80Smarks 		return (EPERM);
2042ecd6cf80Smarks 	}
2043ecd6cf80Smarks 	nvlist_free(nvp);
2044ecd6cf80Smarks 	error = dsl_deleg_access(zc->zc_name,
204591ebeef5Sahrens 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
2046ecd6cf80Smarks 	crfree(usercred);
2047ecd6cf80Smarks 	return (error);
2048ecd6cf80Smarks }
2049ecd6cf80Smarks 
20503cb34c60Sahrens /*
20513cb34c60Sahrens  * inputs:
20523cb34c60Sahrens  * zc_name		name of filesystem
20533cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
20543cb34c60Sahrens  * zc_perm_action	allow/unallow flag
20553cb34c60Sahrens  *
20563cb34c60Sahrens  * outputs:		none
20573cb34c60Sahrens  */
2058ecd6cf80Smarks static int
2059ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2060ecd6cf80Smarks {
2061ecd6cf80Smarks 	int error;
2062ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2063ecd6cf80Smarks 
2064990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2065478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2066ecd6cf80Smarks 		return (error);
2067ecd6cf80Smarks 
2068ecd6cf80Smarks 	/*
2069ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2070ecd6cf80Smarks 	 */
2071ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2072ecd6cf80Smarks 		nvlist_free(fsaclnv);
2073ecd6cf80Smarks 		return (EINVAL);
2074ecd6cf80Smarks 	}
2075ecd6cf80Smarks 
2076ecd6cf80Smarks 	/*
2077ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2078ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2079ecd6cf80Smarks 	 * the nvlist(s)
2080ecd6cf80Smarks 	 */
2081ecd6cf80Smarks 
208291ebeef5Sahrens 	error = secpolicy_zfs(CRED());
2083ecd6cf80Smarks 	if (error) {
208491ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
208591ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
208691ebeef5Sahrens 			    fsaclnv, CRED());
208791ebeef5Sahrens 		} else {
208891ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
208991ebeef5Sahrens 			    fsaclnv, CRED());
209091ebeef5Sahrens 		}
2091ecd6cf80Smarks 	}
2092ecd6cf80Smarks 
2093ecd6cf80Smarks 	if (error == 0)
2094ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2095ecd6cf80Smarks 
2096ecd6cf80Smarks 	nvlist_free(fsaclnv);
2097ecd6cf80Smarks 	return (error);
2098ecd6cf80Smarks }
2099ecd6cf80Smarks 
21003cb34c60Sahrens /*
21013cb34c60Sahrens  * inputs:
21023cb34c60Sahrens  * zc_name		name of filesystem
21033cb34c60Sahrens  *
21043cb34c60Sahrens  * outputs:
21053cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
21063cb34c60Sahrens  */
2107ecd6cf80Smarks static int
2108ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2109ecd6cf80Smarks {
2110ecd6cf80Smarks 	nvlist_t *nvp;
2111ecd6cf80Smarks 	int error;
2112ecd6cf80Smarks 
2113ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2114ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2115ecd6cf80Smarks 		nvlist_free(nvp);
2116ecd6cf80Smarks 	}
2117ecd6cf80Smarks 
2118ecd6cf80Smarks 	return (error);
2119ecd6cf80Smarks }
2120ecd6cf80Smarks 
2121fa9e4066Sahrens /*
2122fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2123fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2124fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2125fa9e4066Sahrens  */
2126fa9e4066Sahrens static vfs_t *
2127fa9e4066Sahrens zfs_get_vfs(const char *resource)
2128fa9e4066Sahrens {
2129fa9e4066Sahrens 	struct vfs *vfsp;
2130fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2131fa9e4066Sahrens 
2132fa9e4066Sahrens 	vfs_list_read_lock();
2133fa9e4066Sahrens 	vfsp = rootvfs;
2134fa9e4066Sahrens 	do {
2135fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2136fa9e4066Sahrens 			VFS_HOLD(vfsp);
2137fa9e4066Sahrens 			vfs_found = vfsp;
2138fa9e4066Sahrens 			break;
2139fa9e4066Sahrens 		}
2140fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2141fa9e4066Sahrens 	} while (vfsp != rootvfs);
2142fa9e4066Sahrens 	vfs_list_unlock();
2143fa9e4066Sahrens 	return (vfs_found);
2144fa9e4066Sahrens }
2145fa9e4066Sahrens 
2146ecd6cf80Smarks /* ARGSUSED */
2147fa9e4066Sahrens static void
2148ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2149fa9e4066Sahrens {
2150da6c28aaSamw 	zfs_creat_t *zct = arg;
2151da6c28aaSamw 
2152de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2153da6c28aaSamw }
2154da6c28aaSamw 
2155de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2156da6c28aaSamw 
2157da6c28aaSamw /*
2158de8267e0Stimh  * inputs:
21590a48a24eStimh  * createprops		list of properties requested by creator
21600a48a24eStimh  * default_zplver	zpl version to use if unspecified in createprops
21610a48a24eStimh  * fuids_ok		fuids allowed in this version of the spa?
21620a48a24eStimh  * os			parent objset pointer (NULL if root fs)
2163de8267e0Stimh  *
2164de8267e0Stimh  * outputs:
2165de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
21660a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
2167da6c28aaSamw  *
2168de8267e0Stimh  * Determine the settings for utf8only, normalization and
2169de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
2170de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
2171de8267e0Stimh  * the file system is of too early a vintage, a creator can not
2172de8267e0Stimh  * request settings for these properties, even if the requested
2173de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
2174de8267e0Stimh  * properties for these, so remove them from the source nvlist after
2175de8267e0Stimh  * processing.
2176da6c28aaSamw  */
2177da6c28aaSamw static int
217814843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
21790a48a24eStimh     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
21800a48a24eStimh     boolean_t *is_ci)
2181da6c28aaSamw {
2182de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
2183de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
2184de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2185da6c28aaSamw 
2186de8267e0Stimh 	ASSERT(zplprops != NULL);
2187da6c28aaSamw 
2188de8267e0Stimh 	/*
2189de8267e0Stimh 	 * Pull out creator prop choices, if any.
2190de8267e0Stimh 	 */
2191de8267e0Stimh 	if (createprops) {
21920a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
21930a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2194de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2195de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2196de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2197de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2198de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2199de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2200de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2201de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2202de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2203de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2204de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2205de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
2206de8267e0Stimh 	}
2207da6c28aaSamw 
2208c2a93d44Stimh 	/*
22090a48a24eStimh 	 * If the zpl version requested is whacky or the file system
22100a48a24eStimh 	 * or pool is version is too "young" to support normalization
22110a48a24eStimh 	 * and the creator tried to set a value for one of the props,
22120a48a24eStimh 	 * error out.
2213c2a93d44Stimh 	 */
22140a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
22150a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
22160a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
2217de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
22180a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
2219de8267e0Stimh 		return (ENOTSUP);
2220c2a93d44Stimh 
2221de8267e0Stimh 	/*
2222de8267e0Stimh 	 * Put the version in the zplprops
2223de8267e0Stimh 	 */
2224de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2225de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2226da6c28aaSamw 
2227de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
2228de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2229de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2230de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2231da6c28aaSamw 
2232c2a93d44Stimh 	/*
2233de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
2234c2a93d44Stimh 	 */
2235de8267e0Stimh 	if (norm)
2236de8267e0Stimh 		u8 = 1;
2237de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
2238de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2239de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2240de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2241de8267e0Stimh 
2242de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
2243de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2244de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2245de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2246c2a93d44Stimh 
2247ab04eb8eStimh 	if (is_ci)
2248ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2249ab04eb8eStimh 
2250da6c28aaSamw 	return (0);
2251fa9e4066Sahrens }
2252fa9e4066Sahrens 
22530a48a24eStimh static int
22540a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
22550a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
22560a48a24eStimh {
22570a48a24eStimh 	boolean_t fuids_ok = B_TRUE;
22580a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
22590a48a24eStimh 	objset_t *os = NULL;
22600a48a24eStimh 	char parentname[MAXNAMELEN];
22610a48a24eStimh 	char *cp;
22620a48a24eStimh 	int error;
22630a48a24eStimh 
22640a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
22650a48a24eStimh 	cp = strrchr(parentname, '/');
22660a48a24eStimh 	ASSERT(cp != NULL);
22670a48a24eStimh 	cp[0] = '\0';
22680a48a24eStimh 
226914843421SMatthew Ahrens 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
227014843421SMatthew Ahrens 		zplver = ZPL_VERSION_USERSPACE - 1;
22710a48a24eStimh 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
22720a48a24eStimh 		zplver = ZPL_VERSION_FUID - 1;
22730a48a24eStimh 		fuids_ok = B_FALSE;
22740a48a24eStimh 	}
22750a48a24eStimh 
22760a48a24eStimh 	/*
22770a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
22780a48a24eStimh 	 */
2279503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
22800a48a24eStimh 		return (error);
22810a48a24eStimh 
22820a48a24eStimh 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
22830a48a24eStimh 	    zplprops, is_ci);
2284503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
22850a48a24eStimh 	return (error);
22860a48a24eStimh }
22870a48a24eStimh 
22880a48a24eStimh static int
22890a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
22900a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
22910a48a24eStimh {
22920a48a24eStimh 	boolean_t fuids_ok = B_TRUE;
22930a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
22940a48a24eStimh 	int error;
22950a48a24eStimh 
22960a48a24eStimh 	if (spa_vers < SPA_VERSION_FUID) {
22970a48a24eStimh 		zplver = ZPL_VERSION_FUID - 1;
22980a48a24eStimh 		fuids_ok = B_FALSE;
22990a48a24eStimh 	}
23000a48a24eStimh 
23010a48a24eStimh 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
23020a48a24eStimh 	    zplprops, is_ci);
23030a48a24eStimh 	return (error);
23040a48a24eStimh }
23050a48a24eStimh 
23063cb34c60Sahrens /*
23073cb34c60Sahrens  * inputs:
23083cb34c60Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
23093cb34c60Sahrens  * zc_name		name of new objset
23103cb34c60Sahrens  * zc_value		name of snapshot to clone from (may be empty)
23113cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
23123cb34c60Sahrens  *
2313de8267e0Stimh  * outputs: none
23143cb34c60Sahrens  */
2315fa9e4066Sahrens static int
2316fa9e4066Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2317fa9e4066Sahrens {
2318fa9e4066Sahrens 	objset_t *clone;
2319fa9e4066Sahrens 	int error = 0;
2320da6c28aaSamw 	zfs_creat_t zct;
2321ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
2322ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2323fa9e4066Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2324fa9e4066Sahrens 
2325fa9e4066Sahrens 	switch (type) {
2326fa9e4066Sahrens 
2327fa9e4066Sahrens 	case DMU_OST_ZFS:
2328fa9e4066Sahrens 		cbfunc = zfs_create_cb;
2329fa9e4066Sahrens 		break;
2330fa9e4066Sahrens 
2331fa9e4066Sahrens 	case DMU_OST_ZVOL:
2332fa9e4066Sahrens 		cbfunc = zvol_create_cb;
2333fa9e4066Sahrens 		break;
2334fa9e4066Sahrens 
2335fa9e4066Sahrens 	default:
23361d452cf5Sahrens 		cbfunc = NULL;
2337e7cbe64fSgw 		break;
2338fa9e4066Sahrens 	}
2339f18faf3fSek 	if (strchr(zc->zc_name, '@') ||
2340f18faf3fSek 	    strchr(zc->zc_name, '%'))
23411d452cf5Sahrens 		return (EINVAL);
2342fa9e4066Sahrens 
2343e9dbad6fSeschrock 	if (zc->zc_nvlist_src != NULL &&
2344990b4856Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2345478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
2346e9dbad6fSeschrock 		return (error);
2347e9dbad6fSeschrock 
2348de8267e0Stimh 	zct.zct_zplprops = NULL;
2349da6c28aaSamw 	zct.zct_props = nvprops;
2350da6c28aaSamw 
2351e9dbad6fSeschrock 	if (zc->zc_value[0] != '\0') {
2352fa9e4066Sahrens 		/*
2353fa9e4066Sahrens 		 * We're creating a clone of an existing snapshot.
2354fa9e4066Sahrens 		 */
2355e9dbad6fSeschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2356e9dbad6fSeschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2357ecd6cf80Smarks 			nvlist_free(nvprops);
2358fa9e4066Sahrens 			return (EINVAL);
2359e9dbad6fSeschrock 		}
2360fa9e4066Sahrens 
2361503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2362e9dbad6fSeschrock 		if (error) {
2363ecd6cf80Smarks 			nvlist_free(nvprops);
2364fa9e4066Sahrens 			return (error);
2365e9dbad6fSeschrock 		}
2366ab04eb8eStimh 
2367ae46e4c7SMatthew Ahrens 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2368503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
2369da6c28aaSamw 		if (error) {
2370da6c28aaSamw 			nvlist_free(nvprops);
2371da6c28aaSamw 			return (error);
2372da6c28aaSamw 		}
2373fa9e4066Sahrens 	} else {
2374ab04eb8eStimh 		boolean_t is_insensitive = B_FALSE;
2375ab04eb8eStimh 
2376e9dbad6fSeschrock 		if (cbfunc == NULL) {
2377ecd6cf80Smarks 			nvlist_free(nvprops);
23781d452cf5Sahrens 			return (EINVAL);
2379e9dbad6fSeschrock 		}
23805c5460e9Seschrock 
2381e9dbad6fSeschrock 		if (type == DMU_OST_ZVOL) {
2382e9dbad6fSeschrock 			uint64_t volsize, volblocksize;
2383e9dbad6fSeschrock 
2384ecd6cf80Smarks 			if (nvprops == NULL ||
2385ecd6cf80Smarks 			    nvlist_lookup_uint64(nvprops,
2386e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2387e9dbad6fSeschrock 			    &volsize) != 0) {
2388ecd6cf80Smarks 				nvlist_free(nvprops);
2389e9dbad6fSeschrock 				return (EINVAL);
2390e9dbad6fSeschrock 			}
2391e9dbad6fSeschrock 
2392ecd6cf80Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
2393e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2394e9dbad6fSeschrock 			    &volblocksize)) != 0 && error != ENOENT) {
2395ecd6cf80Smarks 				nvlist_free(nvprops);
2396e9dbad6fSeschrock 				return (EINVAL);
2397e9dbad6fSeschrock 			}
2398e9dbad6fSeschrock 
2399e9dbad6fSeschrock 			if (error != 0)
2400e9dbad6fSeschrock 				volblocksize = zfs_prop_default_numeric(
2401e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
2402e9dbad6fSeschrock 
2403e9dbad6fSeschrock 			if ((error = zvol_check_volblocksize(
2404e9dbad6fSeschrock 			    volblocksize)) != 0 ||
2405e9dbad6fSeschrock 			    (error = zvol_check_volsize(volsize,
2406e9dbad6fSeschrock 			    volblocksize)) != 0) {
2407ecd6cf80Smarks 				nvlist_free(nvprops);
24085c5460e9Seschrock 				return (error);
2409e9dbad6fSeschrock 			}
2410e7437265Sahrens 		} else if (type == DMU_OST_ZFS) {
2411da6c28aaSamw 			int error;
2412da6c28aaSamw 
2413da6c28aaSamw 			/*
2414da6c28aaSamw 			 * We have to have normalization and
2415da6c28aaSamw 			 * case-folding flags correct when we do the
2416da6c28aaSamw 			 * file system creation, so go figure them out
2417de8267e0Stimh 			 * now.
2418da6c28aaSamw 			 */
2419de8267e0Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2420de8267e0Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2421de8267e0Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
24220a48a24eStimh 			    zct.zct_zplprops, &is_insensitive);
2423da6c28aaSamw 			if (error != 0) {
2424da6c28aaSamw 				nvlist_free(nvprops);
2425de8267e0Stimh 				nvlist_free(zct.zct_zplprops);
2426da6c28aaSamw 				return (error);
2427da6c28aaSamw 			}
2428da6c28aaSamw 		}
2429ae46e4c7SMatthew Ahrens 		error = dmu_objset_create(zc->zc_name, type,
2430ab04eb8eStimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2431de8267e0Stimh 		nvlist_free(zct.zct_zplprops);
2432fa9e4066Sahrens 	}
2433e9dbad6fSeschrock 
2434e9dbad6fSeschrock 	/*
2435e9dbad6fSeschrock 	 * It would be nice to do this atomically.
2436e9dbad6fSeschrock 	 */
2437e9dbad6fSeschrock 	if (error == 0) {
243891ebeef5Sahrens 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2439842727c2SChris Kirby 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
2440e9dbad6fSeschrock 	}
2441ecd6cf80Smarks 	nvlist_free(nvprops);
2442fa9e4066Sahrens 	return (error);
2443fa9e4066Sahrens }
2444fa9e4066Sahrens 
24453cb34c60Sahrens /*
24463cb34c60Sahrens  * inputs:
24473cb34c60Sahrens  * zc_name	name of filesystem
24483cb34c60Sahrens  * zc_value	short name of snapshot
24493cb34c60Sahrens  * zc_cookie	recursive flag
245014843421SMatthew Ahrens  * zc_nvlist_src[_size] property list
24513cb34c60Sahrens  *
2452*681d9761SEric Taylor  * outputs:
2453*681d9761SEric Taylor  * zc_value	short snapname (i.e. part after the '@')
24543cb34c60Sahrens  */
2455fa9e4066Sahrens static int
24561d452cf5Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
2457fa9e4066Sahrens {
2458bb0ade09Sahrens 	nvlist_t *nvprops = NULL;
2459bb0ade09Sahrens 	int error;
2460bb0ade09Sahrens 	boolean_t recursive = zc->zc_cookie;
2461bb0ade09Sahrens 
2462e9dbad6fSeschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
24631d452cf5Sahrens 		return (EINVAL);
2464bb0ade09Sahrens 
2465bb0ade09Sahrens 	if (zc->zc_nvlist_src != NULL &&
2466bb0ade09Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2467478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
2468bb0ade09Sahrens 		return (error);
2469bb0ade09Sahrens 
2470ea2f5b9eSMatthew Ahrens 	error = zfs_check_userprops(zc->zc_name, nvprops);
2471ea2f5b9eSMatthew Ahrens 	if (error)
2472ea2f5b9eSMatthew Ahrens 		goto out;
2473bb0ade09Sahrens 
2474ea2f5b9eSMatthew Ahrens 	if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
2475ea2f5b9eSMatthew Ahrens 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2476ea2f5b9eSMatthew Ahrens 		error = ENOTSUP;
2477ea2f5b9eSMatthew Ahrens 		goto out;
2478bb0ade09Sahrens 	}
2479ea2f5b9eSMatthew Ahrens 
2480ea2f5b9eSMatthew Ahrens 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2481ea2f5b9eSMatthew Ahrens 	    nvprops, recursive);
2482ea2f5b9eSMatthew Ahrens 
2483ea2f5b9eSMatthew Ahrens out:
2484bb0ade09Sahrens 	nvlist_free(nvprops);
2485bb0ade09Sahrens 	return (error);
24861d452cf5Sahrens }
2487fa9e4066Sahrens 
2488cdf5b4caSmmusante int
24891d452cf5Sahrens zfs_unmount_snap(char *name, void *arg)
24901d452cf5Sahrens {
24910b69c2f0Sahrens 	vfs_t *vfsp = NULL;
24921d452cf5Sahrens 
2493745cd3c5Smaybee 	if (arg) {
2494745cd3c5Smaybee 		char *snapname = arg;
2495745cd3c5Smaybee 		int len = strlen(name) + strlen(snapname) + 2;
2496745cd3c5Smaybee 		char *buf = kmem_alloc(len, KM_SLEEP);
24971d452cf5Sahrens 
2498745cd3c5Smaybee 		(void) strcpy(buf, name);
2499745cd3c5Smaybee 		(void) strcat(buf, "@");
2500745cd3c5Smaybee 		(void) strcat(buf, snapname);
2501745cd3c5Smaybee 		vfsp = zfs_get_vfs(buf);
2502745cd3c5Smaybee 		kmem_free(buf, len);
25030b69c2f0Sahrens 	} else if (strchr(name, '@')) {
25041d452cf5Sahrens 		vfsp = zfs_get_vfs(name);
25051d452cf5Sahrens 	}
25061d452cf5Sahrens 
25071d452cf5Sahrens 	if (vfsp) {
2508fa9e4066Sahrens 		/*
25091d452cf5Sahrens 		 * Always force the unmount for snapshots.
2510fa9e4066Sahrens 		 */
25111d452cf5Sahrens 		int flag = MS_FORCE;
25121d452cf5Sahrens 		int err;
25131d452cf5Sahrens 
25141d452cf5Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2515fa9e4066Sahrens 			VFS_RELE(vfsp);
25161d452cf5Sahrens 			return (err);
2517fa9e4066Sahrens 		}
25181d452cf5Sahrens 		VFS_RELE(vfsp);
25191d452cf5Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
25201d452cf5Sahrens 			return (err);
25211d452cf5Sahrens 	}
25221d452cf5Sahrens 	return (0);
25231d452cf5Sahrens }
25241d452cf5Sahrens 
25253cb34c60Sahrens /*
25263cb34c60Sahrens  * inputs:
2527842727c2SChris Kirby  * zc_name		name of filesystem
2528842727c2SChris Kirby  * zc_value		short name of snapshot
2529842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
25303cb34c60Sahrens  *
25313cb34c60Sahrens  * outputs:	none
25323cb34c60Sahrens  */
25331d452cf5Sahrens static int
25341d452cf5Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
25351d452cf5Sahrens {
25361d452cf5Sahrens 	int err;
25371d452cf5Sahrens 
2538e9dbad6fSeschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
25391d452cf5Sahrens 		return (EINVAL);
25401d452cf5Sahrens 	err = dmu_objset_find(zc->zc_name,
2541e9dbad6fSeschrock 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
25421d452cf5Sahrens 	if (err)
25431d452cf5Sahrens 		return (err);
2544842727c2SChris Kirby 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
2545842727c2SChris Kirby 	    zc->zc_defer_destroy));
25461d452cf5Sahrens }
25471d452cf5Sahrens 
25483cb34c60Sahrens /*
25493cb34c60Sahrens  * inputs:
25503cb34c60Sahrens  * zc_name		name of dataset to destroy
25513cb34c60Sahrens  * zc_objset_type	type of objset
2552842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
25533cb34c60Sahrens  *
25543cb34c60Sahrens  * outputs:		none
25553cb34c60Sahrens  */
25561d452cf5Sahrens static int
25571d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
25581d452cf5Sahrens {
2559*681d9761SEric Taylor 	int err;
25601d452cf5Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2561*681d9761SEric Taylor 		err = zfs_unmount_snap(zc->zc_name, NULL);
25621d452cf5Sahrens 		if (err)
25631d452cf5Sahrens 			return (err);
2564fa9e4066Sahrens 	}
2565fa9e4066Sahrens 
2566*681d9761SEric Taylor 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
2567*681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
2568*681d9761SEric Taylor 		zvol_remove_minor(zc->zc_name);
2569*681d9761SEric Taylor 	return (err);
2570fa9e4066Sahrens }
2571fa9e4066Sahrens 
25723cb34c60Sahrens /*
25733cb34c60Sahrens  * inputs:
25744ccbb6e7Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
25753cb34c60Sahrens  *
25763cb34c60Sahrens  * outputs:	none
25773cb34c60Sahrens  */
2578fa9e4066Sahrens static int
2579fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
2580fa9e4066Sahrens {
2581ae46e4c7SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
25824ccbb6e7Sahrens 	int error;
2583ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
2584ae46e4c7SMatthew Ahrens 	char *clone_name;
2585ae46e4c7SMatthew Ahrens 
2586ae46e4c7SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
2587ae46e4c7SMatthew Ahrens 	if (error)
2588ae46e4c7SMatthew Ahrens 		return (error);
2589ae46e4c7SMatthew Ahrens 
2590ae46e4c7SMatthew Ahrens 	/* must not be a snapshot */
2591ae46e4c7SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
2592ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2593ae46e4c7SMatthew Ahrens 		return (EINVAL);
2594ae46e4c7SMatthew Ahrens 	}
2595ae46e4c7SMatthew Ahrens 
2596ae46e4c7SMatthew Ahrens 	/* must have a most recent snapshot */
2597ae46e4c7SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
2598ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2599ae46e4c7SMatthew Ahrens 		return (EINVAL);
2600ae46e4c7SMatthew Ahrens 	}
26014ccbb6e7Sahrens 
26024ccbb6e7Sahrens 	/*
2603ae46e4c7SMatthew Ahrens 	 * Create clone of most recent snapshot.
26044ccbb6e7Sahrens 	 */
2605ae46e4c7SMatthew Ahrens 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
2606ae46e4c7SMatthew Ahrens 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
26074ccbb6e7Sahrens 	if (error)
2608ae46e4c7SMatthew Ahrens 		goto out;
26094ccbb6e7Sahrens 
2610503ad85cSMatthew Ahrens 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
2611ae46e4c7SMatthew Ahrens 	if (error)
2612ae46e4c7SMatthew Ahrens 		goto out;
2613ae46e4c7SMatthew Ahrens 
2614ae46e4c7SMatthew Ahrens 	/*
2615ae46e4c7SMatthew Ahrens 	 * Do clone swap.
2616ae46e4c7SMatthew Ahrens 	 */
261714843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
2618503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
261947f263f4Sek 		if (error == 0) {
262047f263f4Sek 			int resume_err;
26214ccbb6e7Sahrens 
2622ae46e4c7SMatthew Ahrens 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2623ae46e4c7SMatthew Ahrens 				error = dsl_dataset_clone_swap(clone, ds,
2624ae46e4c7SMatthew Ahrens 				    B_TRUE);
2625ae46e4c7SMatthew Ahrens 				dsl_dataset_disown(ds, FTAG);
2626ae46e4c7SMatthew Ahrens 				ds = NULL;
2627ae46e4c7SMatthew Ahrens 			} else {
2628ae46e4c7SMatthew Ahrens 				error = EBUSY;
2629ae46e4c7SMatthew Ahrens 			}
2630503ad85cSMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
263147f263f4Sek 			error = error ? error : resume_err;
263247f263f4Sek 		}
26334ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
26344ccbb6e7Sahrens 	} else {
2635ae46e4c7SMatthew Ahrens 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
2636ae46e4c7SMatthew Ahrens 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
2637ae46e4c7SMatthew Ahrens 			dsl_dataset_disown(ds, FTAG);
2638ae46e4c7SMatthew Ahrens 			ds = NULL;
2639ae46e4c7SMatthew Ahrens 		} else {
2640ae46e4c7SMatthew Ahrens 			error = EBUSY;
2641ae46e4c7SMatthew Ahrens 		}
26424ccbb6e7Sahrens 	}
26434ccbb6e7Sahrens 
2644ae46e4c7SMatthew Ahrens 	/*
2645ae46e4c7SMatthew Ahrens 	 * Destroy clone (which also closes it).
2646ae46e4c7SMatthew Ahrens 	 */
2647ae46e4c7SMatthew Ahrens 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
2648ae46e4c7SMatthew Ahrens 
2649ae46e4c7SMatthew Ahrens out:
2650ae46e4c7SMatthew Ahrens 	strfree(clone_name);
2651ae46e4c7SMatthew Ahrens 	if (ds)
2652ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
26534ccbb6e7Sahrens 	return (error);
2654fa9e4066Sahrens }
2655fa9e4066Sahrens 
26563cb34c60Sahrens /*
26573cb34c60Sahrens  * inputs:
26583cb34c60Sahrens  * zc_name	old name of dataset
26593cb34c60Sahrens  * zc_value	new name of dataset
26603cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
26613cb34c60Sahrens  *
26623cb34c60Sahrens  * outputs:	none
26633cb34c60Sahrens  */
2664fa9e4066Sahrens static int
2665fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
2666fa9e4066Sahrens {
26677f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
2668cdf5b4caSmmusante 
2669e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2670f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2671f18faf3fSek 	    strchr(zc->zc_value, '%'))
2672fa9e4066Sahrens 		return (EINVAL);
2673fa9e4066Sahrens 
2674cdf5b4caSmmusante 	/*
2675cdf5b4caSmmusante 	 * Unmount snapshot unless we're doing a recursive rename,
2676cdf5b4caSmmusante 	 * in which case the dataset code figures out which snapshots
2677cdf5b4caSmmusante 	 * to unmount.
2678cdf5b4caSmmusante 	 */
2679cdf5b4caSmmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2680fa9e4066Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
26811d452cf5Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
26821d452cf5Sahrens 		if (err)
26831d452cf5Sahrens 			return (err);
2684fa9e4066Sahrens 	}
2685*681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL)
2686*681d9761SEric Taylor 		(void) zvol_remove_minor(zc->zc_name);
2687cdf5b4caSmmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2688fa9e4066Sahrens }
2689fa9e4066Sahrens 
2690745cd3c5Smaybee static void
26916e77af0aSDavid Pacheco clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
2692745cd3c5Smaybee {
2693745cd3c5Smaybee 	zfs_cmd_t *zc;
2694745cd3c5Smaybee 	nvpair_t *prop;
2695745cd3c5Smaybee 
2696745cd3c5Smaybee 	if (props == NULL)
2697745cd3c5Smaybee 		return;
2698745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2699745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
2700745cd3c5Smaybee 	for (prop = nvlist_next_nvpair(props, NULL); prop;
2701745cd3c5Smaybee 	    prop = nvlist_next_nvpair(props, prop)) {
27026e77af0aSDavid Pacheco 		if (newprops != NULL &&
27036e77af0aSDavid Pacheco 		    nvlist_exists(newprops, nvpair_name(prop)))
27046e77af0aSDavid Pacheco 			continue;
2705745cd3c5Smaybee 		(void) strcpy(zc->zc_value, nvpair_name(prop));
2706745cd3c5Smaybee 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2707745cd3c5Smaybee 			(void) zfs_ioc_inherit_prop(zc);
2708745cd3c5Smaybee 	}
2709745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
2710745cd3c5Smaybee }
2711745cd3c5Smaybee 
27123cb34c60Sahrens /*
27133cb34c60Sahrens  * inputs:
27143cb34c60Sahrens  * zc_name		name of containing filesystem
27153cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
27163cb34c60Sahrens  * zc_value		name of snapshot to create
27173cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
27183cb34c60Sahrens  * zc_cookie		file descriptor to recv from
27193cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
27203cb34c60Sahrens  * zc_guid		force flag
27213cb34c60Sahrens  *
27223cb34c60Sahrens  * outputs:
27233cb34c60Sahrens  * zc_cookie		number of bytes read
27243cb34c60Sahrens  */
2725fa9e4066Sahrens static int
27263cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
2727fa9e4066Sahrens {
2728fa9e4066Sahrens 	file_t *fp;
2729f18faf3fSek 	objset_t *os;
27303cb34c60Sahrens 	dmu_recv_cookie_t drc;
2731f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
2732f18faf3fSek 	int error, fd;
27333cb34c60Sahrens 	offset_t off;
27343cb34c60Sahrens 	nvlist_t *props = NULL;
2735745cd3c5Smaybee 	nvlist_t *origprops = NULL;
27363cb34c60Sahrens 	objset_t *origin = NULL;
27373cb34c60Sahrens 	char *tosnap;
27383cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
2739fa9e4066Sahrens 
27403ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2741f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
2742f18faf3fSek 	    strchr(zc->zc_value, '%'))
27433ccfa83cSahrens 		return (EINVAL);
27443ccfa83cSahrens 
27453cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
27463cb34c60Sahrens 	tosnap = strchr(tofs, '@');
27473cb34c60Sahrens 	*tosnap = '\0';
27483cb34c60Sahrens 	tosnap++;
27493cb34c60Sahrens 
27503cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
27513cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2752478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
27533cb34c60Sahrens 		return (error);
27543cb34c60Sahrens 
2755fa9e4066Sahrens 	fd = zc->zc_cookie;
2756fa9e4066Sahrens 	fp = getf(fd);
27573cb34c60Sahrens 	if (fp == NULL) {
27583cb34c60Sahrens 		nvlist_free(props);
2759fa9e4066Sahrens 		return (EBADF);
27603cb34c60Sahrens 	}
2761f18faf3fSek 
2762503ad85cSMatthew Ahrens 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
2763745cd3c5Smaybee 		/*
2764745cd3c5Smaybee 		 * If new properties are supplied, they are to completely
2765745cd3c5Smaybee 		 * replace the existing ones, so stash away the existing ones.
2766745cd3c5Smaybee 		 */
2767503ad85cSMatthew Ahrens 		(void) dsl_prop_get_all(os, &origprops, B_TRUE);
2768745cd3c5Smaybee 
2769503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2770f18faf3fSek 	}
2771f18faf3fSek 
27723cb34c60Sahrens 	if (zc->zc_string[0]) {
2773503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
2774745cd3c5Smaybee 		if (error)
2775745cd3c5Smaybee 			goto out;
27763cb34c60Sahrens 	}
27773cb34c60Sahrens 
27783cb34c60Sahrens 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2779f4b94bdeSMatthew Ahrens 	    force, origin, &drc);
27803cb34c60Sahrens 	if (origin)
2781503ad85cSMatthew Ahrens 		dmu_objset_rele(origin, FTAG);
2782745cd3c5Smaybee 	if (error)
2783745cd3c5Smaybee 		goto out;
2784f18faf3fSek 
2785f18faf3fSek 	/*
2786745cd3c5Smaybee 	 * Reset properties.  We do this before we receive the stream
2787745cd3c5Smaybee 	 * so that the properties are applied to the new data.
2788f18faf3fSek 	 */
27893cb34c60Sahrens 	if (props) {
27906e77af0aSDavid Pacheco 		clear_props(tofs, origprops, props);
2791745cd3c5Smaybee 		/*
2792745cd3c5Smaybee 		 * XXX - Note, this is all-or-nothing; should be best-effort.
2793745cd3c5Smaybee 		 */
2794745cd3c5Smaybee 		(void) zfs_set_prop_nvlist(tofs, props);
27953cb34c60Sahrens 	}
27963cb34c60Sahrens 
27973cb34c60Sahrens 	off = fp->f_offset;
27983cb34c60Sahrens 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2799a2eea2e1Sahrens 
2800f4b94bdeSMatthew Ahrens 	if (error == 0) {
2801f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
2802745cd3c5Smaybee 
2803f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
2804f4b94bdeSMatthew Ahrens 			/* online recv */
2805f4b94bdeSMatthew Ahrens 			int end_err;
2806745cd3c5Smaybee 
2807503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
2808f4b94bdeSMatthew Ahrens 			/*
2809f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
2810f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
2811f4b94bdeSMatthew Ahrens 			 */
2812f4b94bdeSMatthew Ahrens 			end_err = dmu_recv_end(&drc);
2813f4b94bdeSMatthew Ahrens 			if (error == 0) {
2814f4b94bdeSMatthew Ahrens 				int resume_err =
2815503ad85cSMatthew Ahrens 				    zfs_resume_fs(zfsvfs, tofs);
2816f4b94bdeSMatthew Ahrens 				error = error ? error : resume_err;
2817f4b94bdeSMatthew Ahrens 			}
2818f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
2819f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
2820745cd3c5Smaybee 		} else {
2821f4b94bdeSMatthew Ahrens 			error = dmu_recv_end(&drc);
28223cb34c60Sahrens 		}
282347f263f4Sek 	}
28243cb34c60Sahrens 
28253cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
28263cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28273cb34c60Sahrens 		fp->f_offset = off;
2828a2eea2e1Sahrens 
2829745cd3c5Smaybee 	/*
2830745cd3c5Smaybee 	 * On error, restore the original props.
2831745cd3c5Smaybee 	 */
2832745cd3c5Smaybee 	if (error && props) {
28336e77af0aSDavid Pacheco 		clear_props(tofs, props, NULL);
2834745cd3c5Smaybee 		(void) zfs_set_prop_nvlist(tofs, origprops);
2835745cd3c5Smaybee 	}
2836745cd3c5Smaybee out:
2837745cd3c5Smaybee 	nvlist_free(props);
2838745cd3c5Smaybee 	nvlist_free(origprops);
2839fa9e4066Sahrens 	releasef(fd);
2840fa9e4066Sahrens 	return (error);
2841fa9e4066Sahrens }
2842fa9e4066Sahrens 
28433cb34c60Sahrens /*
28443cb34c60Sahrens  * inputs:
28453cb34c60Sahrens  * zc_name	name of snapshot to send
28463cb34c60Sahrens  * zc_value	short name of incremental fromsnap (may be empty)
28473cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
28483cb34c60Sahrens  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
28493cb34c60Sahrens  *
28503cb34c60Sahrens  * outputs: none
28513cb34c60Sahrens  */
2852fa9e4066Sahrens static int
28533cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
2854fa9e4066Sahrens {
2855fa9e4066Sahrens 	objset_t *fromsnap = NULL;
2856fa9e4066Sahrens 	objset_t *tosnap;
2857fa9e4066Sahrens 	file_t *fp;
2858fa9e4066Sahrens 	int error;
28593cb34c60Sahrens 	offset_t off;
2860fa9e4066Sahrens 
2861503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
2862fa9e4066Sahrens 	if (error)
2863fa9e4066Sahrens 		return (error);
2864fa9e4066Sahrens 
2865e9dbad6fSeschrock 	if (zc->zc_value[0] != '\0') {
28666a0f0066SEric Taylor 		char *buf;
2867a2eea2e1Sahrens 		char *cp;
2868a2eea2e1Sahrens 
28696a0f0066SEric Taylor 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
28706a0f0066SEric Taylor 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
2871a2eea2e1Sahrens 		cp = strchr(buf, '@');
2872a2eea2e1Sahrens 		if (cp)
2873a2eea2e1Sahrens 			*(cp+1) = 0;
28746a0f0066SEric Taylor 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
2875503ad85cSMatthew Ahrens 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
28766a0f0066SEric Taylor 		kmem_free(buf, MAXPATHLEN);
2877fa9e4066Sahrens 		if (error) {
2878503ad85cSMatthew Ahrens 			dmu_objset_rele(tosnap, FTAG);
2879fa9e4066Sahrens 			return (error);
2880fa9e4066Sahrens 		}
2881fa9e4066Sahrens 	}
2882fa9e4066Sahrens 
2883fa9e4066Sahrens 	fp = getf(zc->zc_cookie);
2884fa9e4066Sahrens 	if (fp == NULL) {
2885503ad85cSMatthew Ahrens 		dmu_objset_rele(tosnap, FTAG);
2886fa9e4066Sahrens 		if (fromsnap)
2887503ad85cSMatthew Ahrens 			dmu_objset_rele(fromsnap, FTAG);
2888fa9e4066Sahrens 		return (EBADF);
2889fa9e4066Sahrens 	}
2890fa9e4066Sahrens 
28913cb34c60Sahrens 	off = fp->f_offset;
28923cb34c60Sahrens 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2893fa9e4066Sahrens 
28943cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
28953cb34c60Sahrens 		fp->f_offset = off;
2896fa9e4066Sahrens 	releasef(zc->zc_cookie);
2897fa9e4066Sahrens 	if (fromsnap)
2898503ad85cSMatthew Ahrens 		dmu_objset_rele(fromsnap, FTAG);
2899503ad85cSMatthew Ahrens 	dmu_objset_rele(tosnap, FTAG);
2900fa9e4066Sahrens 	return (error);
2901fa9e4066Sahrens }
2902fa9e4066Sahrens 
2903ea8dc4b6Seschrock static int
2904ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
2905ea8dc4b6Seschrock {
2906ea8dc4b6Seschrock 	int id, error;
2907ea8dc4b6Seschrock 
2908ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2909ea8dc4b6Seschrock 	    &zc->zc_inject_record);
2910ea8dc4b6Seschrock 
2911ea8dc4b6Seschrock 	if (error == 0)
2912ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
2913ea8dc4b6Seschrock 
2914ea8dc4b6Seschrock 	return (error);
2915ea8dc4b6Seschrock }
2916ea8dc4b6Seschrock 
2917ea8dc4b6Seschrock static int
2918ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
2919ea8dc4b6Seschrock {
2920ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
2921ea8dc4b6Seschrock }
2922ea8dc4b6Seschrock 
2923ea8dc4b6Seschrock static int
2924ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2925ea8dc4b6Seschrock {
2926ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
2927ea8dc4b6Seschrock 	int error;
2928ea8dc4b6Seschrock 
2929ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2930ea8dc4b6Seschrock 	    &zc->zc_inject_record);
2931ea8dc4b6Seschrock 
2932ea8dc4b6Seschrock 	zc->zc_guid = id;
2933ea8dc4b6Seschrock 
2934ea8dc4b6Seschrock 	return (error);
2935ea8dc4b6Seschrock }
2936ea8dc4b6Seschrock 
2937ea8dc4b6Seschrock static int
2938ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
2939ea8dc4b6Seschrock {
2940ea8dc4b6Seschrock 	spa_t *spa;
2941ea8dc4b6Seschrock 	int error;
2942e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2943ea8dc4b6Seschrock 
2944ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2945ea8dc4b6Seschrock 		return (error);
2946ea8dc4b6Seschrock 
2947e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2948ea8dc4b6Seschrock 	    &count);
2949ea8dc4b6Seschrock 	if (error == 0)
2950e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
2951ea8dc4b6Seschrock 	else
2952e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2953ea8dc4b6Seschrock 
2954ea8dc4b6Seschrock 	spa_close(spa, FTAG);
2955ea8dc4b6Seschrock 
2956ea8dc4b6Seschrock 	return (error);
2957ea8dc4b6Seschrock }
2958ea8dc4b6Seschrock 
2959ea8dc4b6Seschrock static int
2960ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
2961ea8dc4b6Seschrock {
2962ea8dc4b6Seschrock 	spa_t *spa;
2963ea8dc4b6Seschrock 	vdev_t *vd;
2964bb8b5132Sek 	int error;
2965ea8dc4b6Seschrock 
2966b87f3af3Sperrin 	/*
2967b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
2968b87f3af3Sperrin 	 */
2969b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
2970b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
2971b87f3af3Sperrin 	if (spa == NULL) {
2972b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
2973b87f3af3Sperrin 		return (EIO);
2974b87f3af3Sperrin 	}
2975b87f3af3Sperrin 	if (spa->spa_log_state == SPA_LOG_MISSING) {
2976b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
2977b87f3af3Sperrin 		spa->spa_log_state = SPA_LOG_CLEAR;
2978b87f3af3Sperrin 	}
2979b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
2980b87f3af3Sperrin 
2981ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2982ea8dc4b6Seschrock 		return (error);
2983ea8dc4b6Seschrock 
2984e14bb325SJeff Bonwick 	spa_vdev_state_enter(spa);
2985ea8dc4b6Seschrock 
2986e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
2987ea8dc4b6Seschrock 		vd = NULL;
2988c5904d13Seschrock 	} else {
2989c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
2990fa94a07fSbrendan 		if (vd == NULL) {
2991e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
2992fa94a07fSbrendan 			spa_close(spa, FTAG);
2993fa94a07fSbrendan 			return (ENODEV);
2994fa94a07fSbrendan 		}
2995ea8dc4b6Seschrock 	}
2996ea8dc4b6Seschrock 
2997e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
2998e14bb325SJeff Bonwick 
2999e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
3000ea8dc4b6Seschrock 
3001e14bb325SJeff Bonwick 	/*
3002e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
3003e14bb325SJeff Bonwick 	 */
300454d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
300554d692b7SGeorge Wilson 		error = EIO;
3006ea8dc4b6Seschrock 
3007ea8dc4b6Seschrock 	spa_close(spa, FTAG);
3008ea8dc4b6Seschrock 
300954d692b7SGeorge Wilson 	return (error);
3010ea8dc4b6Seschrock }
3011ea8dc4b6Seschrock 
30123cb34c60Sahrens /*
30133cb34c60Sahrens  * inputs:
30143cb34c60Sahrens  * zc_name	name of filesystem
30153cb34c60Sahrens  * zc_value	name of origin snapshot
30163cb34c60Sahrens  *
3017*681d9761SEric Taylor  * outputs:
3018*681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
30193cb34c60Sahrens  */
302099653d4eSeschrock static int
302199653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
302299653d4eSeschrock {
30230b69c2f0Sahrens 	char *cp;
30240b69c2f0Sahrens 
30250b69c2f0Sahrens 	/*
30260b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
30270b69c2f0Sahrens 	 * it's easier.
30280b69c2f0Sahrens 	 */
3029e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
30300b69c2f0Sahrens 	if (cp)
30310b69c2f0Sahrens 		*cp = '\0';
3032e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
30330b69c2f0Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3034*681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
303599653d4eSeschrock }
303699653d4eSeschrock 
303714843421SMatthew Ahrens /*
303814843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
303914843421SMatthew Ahrens  *
304014843421SMatthew Ahrens  * inputs:
304114843421SMatthew Ahrens  * zc_name	name of filesystem
304214843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
304314843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
304414843421SMatthew Ahrens  * zc_guid	RID/UID/GID
304514843421SMatthew Ahrens  *
304614843421SMatthew Ahrens  * outputs:
304714843421SMatthew Ahrens  * zc_cookie	property value
304814843421SMatthew Ahrens  */
304914843421SMatthew Ahrens static int
305014843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
305114843421SMatthew Ahrens {
305214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
305314843421SMatthew Ahrens 	int error;
305414843421SMatthew Ahrens 
305514843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
305614843421SMatthew Ahrens 		return (EINVAL);
305714843421SMatthew Ahrens 
3058503ad85cSMatthew Ahrens 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
305914843421SMatthew Ahrens 	if (error)
306014843421SMatthew Ahrens 		return (error);
306114843421SMatthew Ahrens 
306214843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
306314843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
306414843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
306514843421SMatthew Ahrens 
306614843421SMatthew Ahrens 	return (error);
306714843421SMatthew Ahrens }
306814843421SMatthew Ahrens 
306914843421SMatthew Ahrens /*
307014843421SMatthew Ahrens  * inputs:
307114843421SMatthew Ahrens  * zc_name		name of filesystem
307214843421SMatthew Ahrens  * zc_cookie		zap cursor
307314843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
307414843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
307514843421SMatthew Ahrens  *
307614843421SMatthew Ahrens  * outputs:
307714843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
307814843421SMatthew Ahrens  * zc_cookie	zap cursor
307914843421SMatthew Ahrens  */
308014843421SMatthew Ahrens static int
308114843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
308214843421SMatthew Ahrens {
308314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
308414843421SMatthew Ahrens 	int error;
308514843421SMatthew Ahrens 
3086503ad85cSMatthew Ahrens 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
308714843421SMatthew Ahrens 	if (error)
308814843421SMatthew Ahrens 		return (error);
308914843421SMatthew Ahrens 
309014843421SMatthew Ahrens 	int bufsize = zc->zc_nvlist_dst_size;
309114843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
309214843421SMatthew Ahrens 
309314843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
309414843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
309514843421SMatthew Ahrens 
309614843421SMatthew Ahrens 	if (error == 0) {
309714843421SMatthew Ahrens 		error = xcopyout(buf,
309814843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
309914843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
310014843421SMatthew Ahrens 	}
310114843421SMatthew Ahrens 	kmem_free(buf, bufsize);
310214843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
310314843421SMatthew Ahrens 
310414843421SMatthew Ahrens 	return (error);
310514843421SMatthew Ahrens }
310614843421SMatthew Ahrens 
310714843421SMatthew Ahrens /*
310814843421SMatthew Ahrens  * inputs:
310914843421SMatthew Ahrens  * zc_name		name of filesystem
311014843421SMatthew Ahrens  *
311114843421SMatthew Ahrens  * outputs:
311214843421SMatthew Ahrens  * none
311314843421SMatthew Ahrens  */
311414843421SMatthew Ahrens static int
311514843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
311614843421SMatthew Ahrens {
311714843421SMatthew Ahrens 	objset_t *os;
311814843421SMatthew Ahrens 	int error;
311914843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
312014843421SMatthew Ahrens 
312114843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3122503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
312314843421SMatthew Ahrens 			/*
312414843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
312514843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
312614843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
312714843421SMatthew Ahrens 			 */
3128503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
3129503ad85cSMatthew Ahrens 			if (error == 0)
3130503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
313114843421SMatthew Ahrens 		}
313214843421SMatthew Ahrens 		if (error == 0)
313314843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
313414843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
313514843421SMatthew Ahrens 	} else {
3136503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
3137503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
313814843421SMatthew Ahrens 		if (error)
313914843421SMatthew Ahrens 			return (error);
314014843421SMatthew Ahrens 
314114843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
3142503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
314314843421SMatthew Ahrens 	}
314414843421SMatthew Ahrens 
314514843421SMatthew Ahrens 	return (error);
314614843421SMatthew Ahrens }
314714843421SMatthew Ahrens 
3148ecd6cf80Smarks /*
3149ecd6cf80Smarks  * We don't want to have a hard dependency
3150ecd6cf80Smarks  * against some special symbols in sharefs
3151da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
3152ecd6cf80Smarks  * the first file system is shared.
3153da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
3154ecd6cf80Smarks  */
3155da6c28aaSamw int (*znfsexport_fs)(void *arg);
3156ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
3157da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
3158da6c28aaSamw 
3159da6c28aaSamw int zfs_nfsshare_inited;
3160da6c28aaSamw int zfs_smbshare_inited;
3161ecd6cf80Smarks 
3162ecd6cf80Smarks ddi_modhandle_t nfs_mod;
3163ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
3164da6c28aaSamw ddi_modhandle_t smbsrv_mod;
3165ecd6cf80Smarks kmutex_t zfs_share_lock;
3166ecd6cf80Smarks 
3167da6c28aaSamw static int
3168da6c28aaSamw zfs_init_sharefs()
3169da6c28aaSamw {
3170da6c28aaSamw 	int error;
3171da6c28aaSamw 
3172da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
3173da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
3174da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
3175da6c28aaSamw 	    ddi_modopen("fs/sharefs",
3176da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
3177da6c28aaSamw 		return (ENOSYS);
3178da6c28aaSamw 	}
3179da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
3180da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
3181da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
3182da6c28aaSamw 		return (ENOSYS);
3183da6c28aaSamw 	}
3184da6c28aaSamw 	return (0);
3185da6c28aaSamw }
3186da6c28aaSamw 
3187ecd6cf80Smarks static int
3188ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
3189ecd6cf80Smarks {
3190ecd6cf80Smarks 	int error;
3191ecd6cf80Smarks 	int opcode;
3192ecd6cf80Smarks 
3193da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
3194da6c28aaSamw 	case ZFS_SHARE_NFS:
3195da6c28aaSamw 	case ZFS_UNSHARE_NFS:
3196da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
3197da6c28aaSamw 			mutex_enter(&zfs_share_lock);
3198da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
3199da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3200da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3201da6c28aaSamw 				return (ENOSYS);
3202da6c28aaSamw 			}
3203da6c28aaSamw 			if (znfsexport_fs == NULL &&
3204da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
3205da6c28aaSamw 			    ddi_modsym(nfs_mod,
3206da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
3207da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3208da6c28aaSamw 				return (ENOSYS);
3209da6c28aaSamw 			}
3210da6c28aaSamw 			error = zfs_init_sharefs();
3211da6c28aaSamw 			if (error) {
3212da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3213da6c28aaSamw 				return (ENOSYS);
3214da6c28aaSamw 			}
3215da6c28aaSamw 			zfs_nfsshare_inited = 1;
3216ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
3217ecd6cf80Smarks 		}
3218da6c28aaSamw 		break;
3219da6c28aaSamw 	case ZFS_SHARE_SMB:
3220da6c28aaSamw 	case ZFS_UNSHARE_SMB:
3221da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
3222da6c28aaSamw 			mutex_enter(&zfs_share_lock);
3223da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
3224da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
3225da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3226da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3227da6c28aaSamw 				return (ENOSYS);
3228da6c28aaSamw 			}
3229da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
3230da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
3231faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
3232da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3233da6c28aaSamw 				return (ENOSYS);
3234da6c28aaSamw 			}
3235da6c28aaSamw 			error = zfs_init_sharefs();
3236da6c28aaSamw 			if (error) {
3237da6c28aaSamw 				mutex_exit(&zfs_share_lock);
3238da6c28aaSamw 				return (ENOSYS);
3239da6c28aaSamw 			}
3240da6c28aaSamw 			zfs_smbshare_inited = 1;
3241ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
3242ecd6cf80Smarks 		}
3243da6c28aaSamw 		break;
3244da6c28aaSamw 	default:
3245da6c28aaSamw 		return (EINVAL);
3246da6c28aaSamw 	}
3247ecd6cf80Smarks 
3248da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
3249da6c28aaSamw 	case ZFS_SHARE_NFS:
3250da6c28aaSamw 	case ZFS_UNSHARE_NFS:
3251da6c28aaSamw 		if (error =
3252da6c28aaSamw 		    znfsexport_fs((void *)
3253da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
3254da6c28aaSamw 			return (error);
3255da6c28aaSamw 		break;
3256da6c28aaSamw 	case ZFS_SHARE_SMB:
3257da6c28aaSamw 	case ZFS_UNSHARE_SMB:
3258da6c28aaSamw 		if (error = zsmbexport_fs((void *)
3259da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
3260da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
3261743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
3262da6c28aaSamw 			return (error);
3263ecd6cf80Smarks 		}
3264da6c28aaSamw 		break;
3265ecd6cf80Smarks 	}
3266ecd6cf80Smarks 
3267da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
3268da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
3269ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
3270ecd6cf80Smarks 
3271da6c28aaSamw 	/*
3272da6c28aaSamw 	 * Add or remove share from sharetab
3273da6c28aaSamw 	 */
3274ecd6cf80Smarks 	error = zshare_fs(opcode,
3275ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
3276ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
3277ecd6cf80Smarks 
3278ecd6cf80Smarks 	return (error);
3279ecd6cf80Smarks 
3280ecd6cf80Smarks }
3281ecd6cf80Smarks 
3282743a77edSAlan Wright ace_t full_access[] = {
3283743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
3284743a77edSAlan Wright };
3285743a77edSAlan Wright 
3286743a77edSAlan Wright /*
3287743a77edSAlan Wright  * Remove all ACL files in shares dir
3288743a77edSAlan Wright  */
3289743a77edSAlan Wright static int
3290743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
3291743a77edSAlan Wright {
3292743a77edSAlan Wright 	zap_cursor_t	zc;
3293743a77edSAlan Wright 	zap_attribute_t	zap;
3294743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3295743a77edSAlan Wright 	int error;
3296743a77edSAlan Wright 
3297743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
3298743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
3299743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
3300743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
3301743a77edSAlan Wright 		    NULL, 0)) != 0)
3302743a77edSAlan Wright 			break;
3303743a77edSAlan Wright 	}
3304743a77edSAlan Wright 	zap_cursor_fini(&zc);
3305743a77edSAlan Wright 	return (error);
3306743a77edSAlan Wright }
3307743a77edSAlan Wright 
3308743a77edSAlan Wright static int
3309743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
3310743a77edSAlan Wright {
3311743a77edSAlan Wright 	vnode_t *vp;
3312743a77edSAlan Wright 	znode_t *dzp;
3313743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
3314743a77edSAlan Wright 	znode_t *sharedir;
3315743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
3316743a77edSAlan Wright 	nvlist_t *nvlist;
3317743a77edSAlan Wright 	char *src, *target;
3318743a77edSAlan Wright 	vattr_t vattr;
3319743a77edSAlan Wright 	vsecattr_t vsec;
3320743a77edSAlan Wright 	int error = 0;
3321743a77edSAlan Wright 
3322743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
3323743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
3324743a77edSAlan Wright 		return (error);
3325743a77edSAlan Wright 
3326743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
3327743a77edSAlan Wright 
3328743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
3329743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
3330743a77edSAlan Wright 	    zc->zc_name) != 0)) {
3331743a77edSAlan Wright 		VN_RELE(vp);
3332743a77edSAlan Wright 		return (EINVAL);
3333743a77edSAlan Wright 	}
3334743a77edSAlan Wright 
3335743a77edSAlan Wright 	dzp = VTOZ(vp);
3336743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
3337743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
3338743a77edSAlan Wright 
33399e1320c0SMark Shellenbaum 	/*
33409e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
33419e1320c0SMark Shellenbaum 	 */
33429e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
33439e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
33449e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
33459e1320c0SMark Shellenbaum 
33469e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
33479e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
33489e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
33499e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
33509e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
33519e1320c0SMark Shellenbaum 		if (error) {
33529e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
33539e1320c0SMark Shellenbaum 		} else {
33549e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
33559e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
33569e1320c0SMark Shellenbaum 		}
33579e1320c0SMark Shellenbaum 		if (error) {
33589e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
33599e1320c0SMark Shellenbaum 			VN_RELE(vp);
33609e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
33619e1320c0SMark Shellenbaum 			return (error);
33629e1320c0SMark Shellenbaum 		}
33639e1320c0SMark Shellenbaum 	}
33649e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
33659e1320c0SMark Shellenbaum 
33669e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
3367743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
33689e1320c0SMark Shellenbaum 		VN_RELE(vp);
3369743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
3370743a77edSAlan Wright 		return (error);
3371743a77edSAlan Wright 	}
3372743a77edSAlan Wright 
3373743a77edSAlan Wright 	switch (zc->zc_cookie) {
3374743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
3375743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
3376743a77edSAlan Wright 		vattr.va_type = VREG;
3377743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
3378743a77edSAlan Wright 		vattr.va_uid = 0;
3379743a77edSAlan Wright 		vattr.va_gid = 0;
3380743a77edSAlan Wright 
3381743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
3382743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
3383743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
3384743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
3385743a77edSAlan Wright 
3386743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
3387743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
3388743a77edSAlan Wright 		if (resourcevp)
3389743a77edSAlan Wright 			VN_RELE(resourcevp);
3390743a77edSAlan Wright 		break;
3391743a77edSAlan Wright 
3392743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
3393743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
3394743a77edSAlan Wright 		    NULL, 0);
3395743a77edSAlan Wright 		break;
3396743a77edSAlan Wright 
3397743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
3398743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
3399478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
3400743a77edSAlan Wright 			VN_RELE(vp);
3401743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
3402743a77edSAlan Wright 			return (error);
3403743a77edSAlan Wright 		}
3404743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
3405743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
3406743a77edSAlan Wright 		    &target)) {
3407743a77edSAlan Wright 			VN_RELE(vp);
340889459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
3409743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
3410743a77edSAlan Wright 			return (error);
3411743a77edSAlan Wright 		}
3412743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
3413743a77edSAlan Wright 		    kcred, NULL, 0);
3414743a77edSAlan Wright 		nvlist_free(nvlist);
3415743a77edSAlan Wright 		break;
3416743a77edSAlan Wright 
3417743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
3418743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
3419743a77edSAlan Wright 		break;
3420743a77edSAlan Wright 
3421743a77edSAlan Wright 	default:
3422743a77edSAlan Wright 		error = EINVAL;
3423743a77edSAlan Wright 		break;
3424743a77edSAlan Wright 	}
3425743a77edSAlan Wright 
3426743a77edSAlan Wright 	VN_RELE(vp);
3427743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
3428743a77edSAlan Wright 
3429743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
3430743a77edSAlan Wright 
3431743a77edSAlan Wright 	return (error);
3432743a77edSAlan Wright }
3433743a77edSAlan Wright 
3434842727c2SChris Kirby /*
3435842727c2SChris Kirby  * inputs:
3436842727c2SChris Kirby  * zc_name	name of filesystem
3437842727c2SChris Kirby  * zc_value	short name of snap
3438842727c2SChris Kirby  * zc_string	user-supplied tag for this reference
3439842727c2SChris Kirby  * zc_cookie	recursive flag
3440ca45db41SChris Kirby  * zc_temphold	set if hold is temporary
3441842727c2SChris Kirby  *
3442842727c2SChris Kirby  * outputs:		none
3443842727c2SChris Kirby  */
3444842727c2SChris Kirby static int
3445842727c2SChris Kirby zfs_ioc_hold(zfs_cmd_t *zc)
3446842727c2SChris Kirby {
3447842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
3448842727c2SChris Kirby 
3449842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3450842727c2SChris Kirby 		return (EINVAL);
3451842727c2SChris Kirby 
3452842727c2SChris Kirby 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
3453ca45db41SChris Kirby 	    zc->zc_string, recursive, zc->zc_temphold));
3454842727c2SChris Kirby }
3455842727c2SChris Kirby 
3456842727c2SChris Kirby /*
3457842727c2SChris Kirby  * inputs:
3458842727c2SChris Kirby  * zc_name	name of dataset from which we're releasing a user reference
3459842727c2SChris Kirby  * zc_value	short name of snap
3460842727c2SChris Kirby  * zc_string	user-supplied tag for this reference
3461842727c2SChris Kirby  * zc_cookie	recursive flag
3462842727c2SChris Kirby  *
3463842727c2SChris Kirby  * outputs:		none
3464842727c2SChris Kirby  */
3465842727c2SChris Kirby static int
3466842727c2SChris Kirby zfs_ioc_release(zfs_cmd_t *zc)
3467842727c2SChris Kirby {
3468842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
3469842727c2SChris Kirby 
3470842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3471842727c2SChris Kirby 		return (EINVAL);
3472842727c2SChris Kirby 
3473842727c2SChris Kirby 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
3474842727c2SChris Kirby 	    zc->zc_string, recursive));
3475842727c2SChris Kirby }
3476842727c2SChris Kirby 
3477842727c2SChris Kirby /*
3478842727c2SChris Kirby  * inputs:
3479842727c2SChris Kirby  * zc_name		name of filesystem
3480842727c2SChris Kirby  *
3481842727c2SChris Kirby  * outputs:
3482842727c2SChris Kirby  * zc_nvlist_src{_size}	nvlist of snapshot holds
3483842727c2SChris Kirby  */
3484842727c2SChris Kirby static int
3485842727c2SChris Kirby zfs_ioc_get_holds(zfs_cmd_t *zc)
3486842727c2SChris Kirby {
3487842727c2SChris Kirby 	nvlist_t *nvp;
3488842727c2SChris Kirby 	int error;
3489842727c2SChris Kirby 
3490842727c2SChris Kirby 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
3491842727c2SChris Kirby 		error = put_nvlist(zc, nvp);
3492842727c2SChris Kirby 		nvlist_free(nvp);
3493842727c2SChris Kirby 	}
3494842727c2SChris Kirby 
3495842727c2SChris Kirby 	return (error);
3496842727c2SChris Kirby }
3497842727c2SChris Kirby 
3498ecd6cf80Smarks /*
34992a6b87f0Sek  * pool create, destroy, and export don't log the history as part of
35002a6b87f0Sek  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
35012a6b87f0Sek  * do the logging of those commands.
3502ecd6cf80Smarks  */
3503fa9e4066Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
350454d692b7SGeorge Wilson 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
350554d692b7SGeorge Wilson 	    B_FALSE },
350654d692b7SGeorge Wilson 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
350754d692b7SGeorge Wilson 	    B_FALSE },
350854d692b7SGeorge Wilson 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
350954d692b7SGeorge Wilson 	    B_FALSE },
351054d692b7SGeorge Wilson 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
351154d692b7SGeorge Wilson 	    B_FALSE },
351254d692b7SGeorge Wilson 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
351354d692b7SGeorge Wilson 	    B_FALSE },
351454d692b7SGeorge Wilson 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
351554d692b7SGeorge Wilson 	    B_FALSE },
351654d692b7SGeorge Wilson 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
351754d692b7SGeorge Wilson 	    B_FALSE },
351854d692b7SGeorge Wilson 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
351954d692b7SGeorge Wilson 	    B_TRUE },
352054d692b7SGeorge Wilson 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
352154d692b7SGeorge Wilson 	    B_FALSE },
352254d692b7SGeorge Wilson 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
352354d692b7SGeorge Wilson 	    B_TRUE },
352454d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
352554d692b7SGeorge Wilson 	    B_FALSE },
352654d692b7SGeorge Wilson 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
352754d692b7SGeorge Wilson 	    B_TRUE },
352854d692b7SGeorge Wilson 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
352954d692b7SGeorge Wilson 	    B_TRUE },
353054d692b7SGeorge Wilson 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
353154d692b7SGeorge Wilson 	    B_FALSE },
353254d692b7SGeorge Wilson 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
353354d692b7SGeorge Wilson 	    B_TRUE },
353454d692b7SGeorge Wilson 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
353554d692b7SGeorge Wilson 	    B_TRUE },
353654d692b7SGeorge Wilson 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
353754d692b7SGeorge Wilson 	    B_TRUE },
35386809eb4eSEric Schrock 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
35396809eb4eSEric Schrock 	    B_TRUE },
354054d692b7SGeorge Wilson 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
354154d692b7SGeorge Wilson 	    B_FALSE },
354254d692b7SGeorge Wilson 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
354354d692b7SGeorge Wilson 	    B_FALSE },
354454d692b7SGeorge Wilson 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
354554d692b7SGeorge Wilson 	    B_FALSE },
354654d692b7SGeorge Wilson 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
354754d692b7SGeorge Wilson 	    B_FALSE },
354854d692b7SGeorge Wilson 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
354954d692b7SGeorge Wilson 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
355054d692b7SGeorge Wilson 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
355154d692b7SGeorge Wilson 	    B_TRUE},
355254d692b7SGeorge Wilson 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
355354d692b7SGeorge Wilson 	    B_TRUE },
355454d692b7SGeorge Wilson 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
355554d692b7SGeorge Wilson 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
355654d692b7SGeorge Wilson 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
355754d692b7SGeorge Wilson 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
355854d692b7SGeorge Wilson 	    B_FALSE },
355954d692b7SGeorge Wilson 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
356054d692b7SGeorge Wilson 	    B_FALSE },
356154d692b7SGeorge Wilson 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
356254d692b7SGeorge Wilson 	    B_FALSE },
356354d692b7SGeorge Wilson 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
356454d692b7SGeorge Wilson 	    B_FALSE },
356554d692b7SGeorge Wilson 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
356654d692b7SGeorge Wilson 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
356754d692b7SGeorge Wilson 	    B_TRUE },
356854d692b7SGeorge Wilson 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
356954d692b7SGeorge Wilson 	    B_TRUE },
357054d692b7SGeorge Wilson 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
357154d692b7SGeorge Wilson 	    B_TRUE },
357254d692b7SGeorge Wilson 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
357354d692b7SGeorge Wilson 	    B_FALSE },
35746e8a0f56SGeorge Wilson 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
35756e8a0f56SGeorge Wilson 	    B_TRUE },
357654d692b7SGeorge Wilson 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
357754d692b7SGeorge Wilson 	    B_TRUE },
357854d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
357954d692b7SGeorge Wilson 	    B_FALSE },
358054d692b7SGeorge Wilson 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
358154d692b7SGeorge Wilson 	    B_TRUE },
358254d692b7SGeorge Wilson 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
358354d692b7SGeorge Wilson 	    B_FALSE },
358454d692b7SGeorge Wilson 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
358554d692b7SGeorge Wilson 	    B_FALSE },
358654d692b7SGeorge Wilson 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
358754d692b7SGeorge Wilson 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
358854d692b7SGeorge Wilson 	    B_TRUE },
358954d692b7SGeorge Wilson 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
359014843421SMatthew Ahrens 	    B_FALSE },
359114843421SMatthew Ahrens 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
359214843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_FALSE },
359314843421SMatthew Ahrens 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
359414843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_FALSE },
359514843421SMatthew Ahrens 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
359614843421SMatthew Ahrens 	    DATASET_NAME, B_FALSE, B_TRUE },
3597842727c2SChris Kirby 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
3598842727c2SChris Kirby 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
3599842727c2SChris Kirby 	    B_TRUE },
3600842727c2SChris Kirby 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3601842727c2SChris Kirby 	    B_TRUE }
3602fa9e4066Sahrens };
3603fa9e4066Sahrens 
360454d692b7SGeorge Wilson int
360554d692b7SGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type)
360654d692b7SGeorge Wilson {
360754d692b7SGeorge Wilson 	spa_t *spa;
360854d692b7SGeorge Wilson 	int error;
360954d692b7SGeorge Wilson 
361054d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
361154d692b7SGeorge Wilson 
361214843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
361354d692b7SGeorge Wilson 	if (error == 0) {
361454d692b7SGeorge Wilson 		if (spa_suspended(spa))
361554d692b7SGeorge Wilson 			error = EAGAIN;
361654d692b7SGeorge Wilson 		spa_close(spa, FTAG);
361754d692b7SGeorge Wilson 	}
361854d692b7SGeorge Wilson 	return (error);
361954d692b7SGeorge Wilson }
362054d692b7SGeorge Wilson 
3621fa9e4066Sahrens static int
3622fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3623fa9e4066Sahrens {
3624fa9e4066Sahrens 	zfs_cmd_t *zc;
3625fa9e4066Sahrens 	uint_t vec;
36261d452cf5Sahrens 	int error, rc;
3627fa9e4066Sahrens 
3628fa9e4066Sahrens 	if (getminor(dev) != 0)
3629fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3630fa9e4066Sahrens 
3631fa9e4066Sahrens 	vec = cmd - ZFS_IOC;
363291ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3633fa9e4066Sahrens 
3634fa9e4066Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3635fa9e4066Sahrens 		return (EINVAL);
3636fa9e4066Sahrens 
3637fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3638fa9e4066Sahrens 
3639478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
3640fa9e4066Sahrens 
3641*681d9761SEric Taylor 	if ((error == 0) && !(flag & FKIOCTL))
3642ecd6cf80Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3643fa9e4066Sahrens 
3644fa9e4066Sahrens 	/*
3645fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
3646fa9e4066Sahrens 	 * the lower layers.
3647fa9e4066Sahrens 	 */
3648fa9e4066Sahrens 	if (error == 0) {
3649fa9e4066Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3650478ed9adSEric Taylor 		zc->zc_iflags = flag & FKIOCTL;
3651fa9e4066Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
3652e7437265Sahrens 		case POOL_NAME:
3653fa9e4066Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3654fa9e4066Sahrens 				error = EINVAL;
365554d692b7SGeorge Wilson 			if (zfs_ioc_vec[vec].zvec_pool_check)
365654d692b7SGeorge Wilson 				error = pool_status_check(zc->zc_name,
365754d692b7SGeorge Wilson 				    zfs_ioc_vec[vec].zvec_namecheck);
3658fa9e4066Sahrens 			break;
3659fa9e4066Sahrens 
3660e7437265Sahrens 		case DATASET_NAME:
3661fa9e4066Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3662fa9e4066Sahrens 				error = EINVAL;
366354d692b7SGeorge Wilson 			if (zfs_ioc_vec[vec].zvec_pool_check)
366454d692b7SGeorge Wilson 				error = pool_status_check(zc->zc_name,
366554d692b7SGeorge Wilson 				    zfs_ioc_vec[vec].zvec_namecheck);
3666fa9e4066Sahrens 			break;
36675ad82045Snd 
3668e7437265Sahrens 		case NO_NAME:
36695ad82045Snd 			break;
3670fa9e4066Sahrens 		}
3671fa9e4066Sahrens 	}
3672fa9e4066Sahrens 
3673fa9e4066Sahrens 	if (error == 0)
3674fa9e4066Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
3675fa9e4066Sahrens 
3676478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
3677ecd6cf80Smarks 	if (error == 0) {
36781d452cf5Sahrens 		error = rc;
367914843421SMatthew Ahrens 		if (zfs_ioc_vec[vec].zvec_his_log)
3680ecd6cf80Smarks 			zfs_log_history(zc);
3681ecd6cf80Smarks 	}
3682fa9e4066Sahrens 
3683fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
3684fa9e4066Sahrens 	return (error);
3685fa9e4066Sahrens }
3686fa9e4066Sahrens 
3687fa9e4066Sahrens static int
3688fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3689fa9e4066Sahrens {
3690fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
3691fa9e4066Sahrens 		return (DDI_FAILURE);
3692fa9e4066Sahrens 
3693fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3694fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3695fa9e4066Sahrens 		return (DDI_FAILURE);
3696fa9e4066Sahrens 
3697fa9e4066Sahrens 	zfs_dip = dip;
3698fa9e4066Sahrens 
3699fa9e4066Sahrens 	ddi_report_dev(dip);
3700fa9e4066Sahrens 
3701fa9e4066Sahrens 	return (DDI_SUCCESS);
3702fa9e4066Sahrens }
3703fa9e4066Sahrens 
3704fa9e4066Sahrens static int
3705fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3706fa9e4066Sahrens {
3707fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
3708fa9e4066Sahrens 		return (DDI_FAILURE);
3709fa9e4066Sahrens 
3710fa9e4066Sahrens 	if (cmd != DDI_DETACH)
3711fa9e4066Sahrens 		return (DDI_FAILURE);
3712fa9e4066Sahrens 
3713fa9e4066Sahrens 	zfs_dip = NULL;
3714fa9e4066Sahrens 
3715fa9e4066Sahrens 	ddi_prop_remove_all(dip);
3716fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
3717fa9e4066Sahrens 
3718fa9e4066Sahrens 	return (DDI_SUCCESS);
3719fa9e4066Sahrens }
3720fa9e4066Sahrens 
3721fa9e4066Sahrens /*ARGSUSED*/
3722fa9e4066Sahrens static int
3723fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3724fa9e4066Sahrens {
3725fa9e4066Sahrens 	switch (infocmd) {
3726fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
3727fa9e4066Sahrens 		*result = zfs_dip;
3728fa9e4066Sahrens 		return (DDI_SUCCESS);
3729fa9e4066Sahrens 
3730fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
3731a0965f35Sbonwick 		*result = (void *)0;
3732fa9e4066Sahrens 		return (DDI_SUCCESS);
3733fa9e4066Sahrens 	}
3734fa9e4066Sahrens 
3735fa9e4066Sahrens 	return (DDI_FAILURE);
3736fa9e4066Sahrens }
3737fa9e4066Sahrens 
3738fa9e4066Sahrens /*
3739fa9e4066Sahrens  * OK, so this is a little weird.
3740fa9e4066Sahrens  *
3741fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
3742fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3743fa9e4066Sahrens  *
3744fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
3745fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
3746fa9e4066Sahrens  */
3747fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
3748fa9e4066Sahrens 	zvol_open,	/* open */
3749fa9e4066Sahrens 	zvol_close,	/* close */
3750fa9e4066Sahrens 	zvol_strategy,	/* strategy */
3751fa9e4066Sahrens 	nodev,		/* print */
3752e7cbe64fSgw 	zvol_dump,	/* dump */
3753fa9e4066Sahrens 	zvol_read,	/* read */
3754fa9e4066Sahrens 	zvol_write,	/* write */
3755fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
3756fa9e4066Sahrens 	nodev,		/* devmap */
3757fa9e4066Sahrens 	nodev,		/* mmap */
3758fa9e4066Sahrens 	nodev,		/* segmap */
3759fa9e4066Sahrens 	nochpoll,	/* poll */
3760fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
3761fa9e4066Sahrens 	NULL,		/* streamtab */
3762fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3763fa9e4066Sahrens 	CB_REV,		/* version */
3764feb08c6bSbillm 	nodev,		/* async read */
3765feb08c6bSbillm 	nodev,		/* async write */
3766fa9e4066Sahrens };
3767fa9e4066Sahrens 
3768fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
3769fa9e4066Sahrens 	DEVO_REV,	/* version */
3770fa9e4066Sahrens 	0,		/* refcnt */
3771fa9e4066Sahrens 	zfs_info,	/* info */
3772fa9e4066Sahrens 	nulldev,	/* identify */
3773fa9e4066Sahrens 	nulldev,	/* probe */
3774fa9e4066Sahrens 	zfs_attach,	/* attach */
3775fa9e4066Sahrens 	zfs_detach,	/* detach */
3776fa9e4066Sahrens 	nodev,		/* reset */
3777fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
377819397407SSherry Moore 	NULL,		/* no bus operations */
377919397407SSherry Moore 	NULL,		/* power */
378019397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
3781fa9e4066Sahrens };
3782fa9e4066Sahrens 
3783fa9e4066Sahrens static struct modldrv zfs_modldrv = {
378419397407SSherry Moore 	&mod_driverops,
378519397407SSherry Moore 	"ZFS storage pool",
378619397407SSherry Moore 	&zfs_dev_ops
3787fa9e4066Sahrens };
3788fa9e4066Sahrens 
3789fa9e4066Sahrens static struct modlinkage modlinkage = {
3790fa9e4066Sahrens 	MODREV_1,
3791fa9e4066Sahrens 	(void *)&zfs_modlfs,
3792fa9e4066Sahrens 	(void *)&zfs_modldrv,
3793fa9e4066Sahrens 	NULL
3794fa9e4066Sahrens };
3795fa9e4066Sahrens 
3796ec533521Sfr 
3797ec533521Sfr uint_t zfs_fsyncer_key;
3798f18faf3fSek extern uint_t rrw_tsd_key;
3799ec533521Sfr 
3800fa9e4066Sahrens int
3801fa9e4066Sahrens _init(void)
3802fa9e4066Sahrens {
3803fa9e4066Sahrens 	int error;
3804fa9e4066Sahrens 
3805a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
3806a0965f35Sbonwick 	zfs_init();
3807a0965f35Sbonwick 	zvol_init();
3808a0965f35Sbonwick 
3809a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
3810a0965f35Sbonwick 		zvol_fini();
3811a0965f35Sbonwick 		zfs_fini();
3812a0965f35Sbonwick 		spa_fini();
3813fa9e4066Sahrens 		return (error);
3814a0965f35Sbonwick 	}
3815fa9e4066Sahrens 
3816ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
3817f18faf3fSek 	tsd_create(&rrw_tsd_key, NULL);
3818ec533521Sfr 
3819fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3820fa9e4066Sahrens 	ASSERT(error == 0);
3821ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3822fa9e4066Sahrens 
3823fa9e4066Sahrens 	return (0);
3824fa9e4066Sahrens }
3825fa9e4066Sahrens 
3826fa9e4066Sahrens int
3827fa9e4066Sahrens _fini(void)
3828fa9e4066Sahrens {
3829fa9e4066Sahrens 	int error;
3830fa9e4066Sahrens 
3831ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3832fa9e4066Sahrens 		return (EBUSY);
3833fa9e4066Sahrens 
3834fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
3835fa9e4066Sahrens 		return (error);
3836fa9e4066Sahrens 
3837fa9e4066Sahrens 	zvol_fini();
3838fa9e4066Sahrens 	zfs_fini();
3839fa9e4066Sahrens 	spa_fini();
3840da6c28aaSamw 	if (zfs_nfsshare_inited)
3841ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
3842da6c28aaSamw 	if (zfs_smbshare_inited)
3843da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
3844da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
3845ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
3846fa9e4066Sahrens 
3847ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
3848fa9e4066Sahrens 	ldi_ident_release(zfs_li);
3849fa9e4066Sahrens 	zfs_li = NULL;
3850ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
3851fa9e4066Sahrens 
3852fa9e4066Sahrens 	return (error);
3853fa9e4066Sahrens }
3854fa9e4066Sahrens 
3855fa9e4066Sahrens int
3856fa9e4066Sahrens _info(struct modinfo *modinfop)
3857fa9e4066Sahrens {
3858fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
3859fa9e4066Sahrens }
3860