xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 19b94df933188a15d4f0d6c568f0bab3f127892e)
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 /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
231df56adaSMartin Matuska  * Portions Copyright 2011 Martin Matuska
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25e9103aaeSGarrett D'Amore  * Copyright (c) 2011 by Delphix. All rights reserved.
26e9103aaeSGarrett D'Amore  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/types.h>
29fa9e4066Sahrens #include <sys/param.h>
30fa9e4066Sahrens #include <sys/errno.h>
31fa9e4066Sahrens #include <sys/uio.h>
32fa9e4066Sahrens #include <sys/buf.h>
33fa9e4066Sahrens #include <sys/modctl.h>
34fa9e4066Sahrens #include <sys/open.h>
35fa9e4066Sahrens #include <sys/file.h>
36fa9e4066Sahrens #include <sys/kmem.h>
37fa9e4066Sahrens #include <sys/conf.h>
38fa9e4066Sahrens #include <sys/cmn_err.h>
39fa9e4066Sahrens #include <sys/stat.h>
40fa9e4066Sahrens #include <sys/zfs_ioctl.h>
414201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
42da6c28aaSamw #include <sys/zfs_znode.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/spa.h>
45b1b8ab34Slling #include <sys/spa_impl.h>
46fa9e4066Sahrens #include <sys/vdev.h>
474201a95eSRic Aleshire #include <sys/priv_impl.h>
48fa9e4066Sahrens #include <sys/dmu.h>
49fa9e4066Sahrens #include <sys/dsl_dir.h>
50fa9e4066Sahrens #include <sys/dsl_dataset.h>
51fa9e4066Sahrens #include <sys/dsl_prop.h>
52ecd6cf80Smarks #include <sys/dsl_deleg.h>
53ecd6cf80Smarks #include <sys/dmu_objset.h>
54fa9e4066Sahrens #include <sys/ddi.h>
55fa9e4066Sahrens #include <sys/sunddi.h>
56fa9e4066Sahrens #include <sys/sunldi.h>
57fa9e4066Sahrens #include <sys/policy.h>
58fa9e4066Sahrens #include <sys/zone.h>
59fa9e4066Sahrens #include <sys/nvpair.h>
60fa9e4066Sahrens #include <sys/pathname.h>
61fa9e4066Sahrens #include <sys/mount.h>
62fa9e4066Sahrens #include <sys/sdt.h>
63fa9e4066Sahrens #include <sys/fs/zfs.h>
64fa9e4066Sahrens #include <sys/zfs_ctldir.h>
65da6c28aaSamw #include <sys/zfs_dir.h>
66c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
67a2eea2e1Sahrens #include <sys/zvol.h>
683f9d6ad7SLin Ling #include <sys/dsl_scan.h>
69ecd6cf80Smarks #include <sharefs/share.h>
70f18faf3fSek #include <sys/dmu_objset.h>
71fa9e4066Sahrens 
72fa9e4066Sahrens #include "zfs_namecheck.h"
73e9dbad6fSeschrock #include "zfs_prop.h"
74ecd6cf80Smarks #include "zfs_deleg.h"
750a586ceaSMark Shellenbaum #include "zfs_comutil.h"
76fa9e4066Sahrens 
77fa9e4066Sahrens extern struct modlfs zfs_modlfs;
78fa9e4066Sahrens 
79fa9e4066Sahrens extern void zfs_init(void);
80fa9e4066Sahrens extern void zfs_fini(void);
81fa9e4066Sahrens 
82fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
83fa9e4066Sahrens dev_info_t *zfs_dip;
84fa9e4066Sahrens 
85fa9e4066Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *);
86ecd6cf80Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
87fa9e4066Sahrens 
8854d692b7SGeorge Wilson typedef enum {
8954d692b7SGeorge Wilson 	NO_NAME,
9054d692b7SGeorge Wilson 	POOL_NAME,
9154d692b7SGeorge Wilson 	DATASET_NAME
9254d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
9354d692b7SGeorge Wilson 
94f9af39baSGeorge Wilson typedef enum {
95f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
96f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
97f9af39baSGeorge Wilson 	POOL_CHECK_READONLY	= 1 << 2
98f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
99f9af39baSGeorge Wilson 
100fa9e4066Sahrens typedef struct zfs_ioc_vec {
101fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
102fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
10354d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
104ecd6cf80Smarks 	boolean_t		zvec_his_log;
105f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
106fa9e4066Sahrens } zfs_ioc_vec_t;
107fa9e4066Sahrens 
10814843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
10914843421SMatthew Ahrens static const char *userquota_perms[] = {
11014843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
11114843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
11214843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
11314843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
11414843421SMatthew Ahrens };
11514843421SMatthew Ahrens 
11614843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
11792241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
11892241e0bSTom Erickson     cred_t *cr);
11992241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
12092241e0bSTom Erickson     nvlist_t **errors);
1210a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
1220a48a24eStimh     boolean_t *);
12392241e0bSTom Erickson int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
1240a48a24eStimh 
125fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
126fa9e4066Sahrens void
127fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
128fa9e4066Sahrens {
129fa9e4066Sahrens 	const char *newfile;
1303f9d6ad7SLin Ling 	char buf[512];
131fa9e4066Sahrens 	va_list adx;
132fa9e4066Sahrens 
133fa9e4066Sahrens 	/*
134fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
135fa9e4066Sahrens 	 */
136fa9e4066Sahrens 	newfile = strrchr(file, '/');
137fa9e4066Sahrens 	if (newfile != NULL) {
138fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
139fa9e4066Sahrens 	} else {
140fa9e4066Sahrens 		newfile = file;
141fa9e4066Sahrens 	}
142fa9e4066Sahrens 
143fa9e4066Sahrens 	va_start(adx, fmt);
144fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
145fa9e4066Sahrens 	va_end(adx);
146fa9e4066Sahrens 
147fa9e4066Sahrens 	/*
148fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
149fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
150fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
151fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
152fa9e4066Sahrens 	 * arg0 = file name
153fa9e4066Sahrens 	 * arg1 = function name
154fa9e4066Sahrens 	 * arg2 = line number
155fa9e4066Sahrens 	 * arg3 = message
156fa9e4066Sahrens 	 */
157fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
158fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
159fa9e4066Sahrens }
160fa9e4066Sahrens 
161ecd6cf80Smarks static void
162228975ccSek history_str_free(char *buf)
163228975ccSek {
164228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
165228975ccSek }
166228975ccSek 
167228975ccSek static char *
168228975ccSek history_str_get(zfs_cmd_t *zc)
169ecd6cf80Smarks {
17040feaa91Sahrens 	char *buf;
171ecd6cf80Smarks 
172ecd6cf80Smarks 	if (zc->zc_history == NULL)
173228975ccSek 		return (NULL);
174e7437265Sahrens 
175ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
176ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
177ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
178228975ccSek 		history_str_free(buf);
179228975ccSek 		return (NULL);
180ecd6cf80Smarks 	}
181ecd6cf80Smarks 
182ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
183ecd6cf80Smarks 
184228975ccSek 	return (buf);
185228975ccSek }
186ecd6cf80Smarks 
18715e6edf1Sgw /*
18815e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
18915e6edf1Sgw  */
19015e6edf1Sgw static boolean_t
19115e6edf1Sgw zfs_is_bootfs(const char *name)
19215e6edf1Sgw {
193503ad85cSMatthew Ahrens 	objset_t *os;
19415e6edf1Sgw 
195503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
196503ad85cSMatthew Ahrens 		boolean_t ret;
197b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
198503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
199503ad85cSMatthew Ahrens 		return (ret);
20015e6edf1Sgw 	}
201503ad85cSMatthew Ahrens 	return (B_FALSE);
20215e6edf1Sgw }
20315e6edf1Sgw 
204c2a93d44Stimh /*
2050a48a24eStimh  * zfs_earlier_version
206c2a93d44Stimh  *
207c2a93d44Stimh  *	Return non-zero if the spa version is less than requested version.
208c2a93d44Stimh  */
209da6c28aaSamw static int
2100a48a24eStimh zfs_earlier_version(const char *name, int version)
211da6c28aaSamw {
212da6c28aaSamw 	spa_t *spa;
213da6c28aaSamw 
214da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
215da6c28aaSamw 		if (spa_version(spa) < version) {
216da6c28aaSamw 			spa_close(spa, FTAG);
217da6c28aaSamw 			return (1);
218da6c28aaSamw 		}
219da6c28aaSamw 		spa_close(spa, FTAG);
220da6c28aaSamw 	}
221da6c28aaSamw 	return (0);
222da6c28aaSamw }
223da6c28aaSamw 
2249e6eda55Smarks /*
225745cd3c5Smaybee  * zpl_earlier_version
2269e6eda55Smarks  *
227745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
2289e6eda55Smarks  */
229745cd3c5Smaybee static boolean_t
230745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
2319e6eda55Smarks {
2329e6eda55Smarks 	objset_t *os;
233745cd3c5Smaybee 	boolean_t rc = B_TRUE;
2349e6eda55Smarks 
235503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
236745cd3c5Smaybee 		uint64_t zplversion;
2379e6eda55Smarks 
238503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
239503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
240503ad85cSMatthew Ahrens 			return (B_TRUE);
241503ad85cSMatthew Ahrens 		}
242503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
243745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
244745cd3c5Smaybee 			rc = zplversion < version;
245503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2469e6eda55Smarks 	}
2479e6eda55Smarks 	return (rc);
2489e6eda55Smarks }
2499e6eda55Smarks 
250228975ccSek static void
251228975ccSek zfs_log_history(zfs_cmd_t *zc)
252228975ccSek {
253228975ccSek 	spa_t *spa;
254228975ccSek 	char *buf;
255ecd6cf80Smarks 
256228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
257228975ccSek 		return;
258228975ccSek 
259228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
260228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
261228975ccSek 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
262228975ccSek 		spa_close(spa, FTAG);
263228975ccSek 	}
264228975ccSek 	history_str_free(buf);
265ecd6cf80Smarks }
266ecd6cf80Smarks 
267fa9e4066Sahrens /*
268fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
269fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
270fa9e4066Sahrens  */
271fa9e4066Sahrens /* ARGSUSED */
272fa9e4066Sahrens static int
273ecd6cf80Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
274fa9e4066Sahrens {
275fa9e4066Sahrens 	return (0);
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens /*
279fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
280fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
281fa9e4066Sahrens  */
282fa9e4066Sahrens /* ARGSUSED */
283fa9e4066Sahrens static int
284ecd6cf80Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
287ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
288fa9e4066Sahrens 		return (0);
289fa9e4066Sahrens 
290fa9e4066Sahrens 	return (ENOENT);
291fa9e4066Sahrens }
292fa9e4066Sahrens 
293fa9e4066Sahrens static int
294a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
295fa9e4066Sahrens {
296fa9e4066Sahrens 	int writable = 1;
297fa9e4066Sahrens 
298fa9e4066Sahrens 	/*
299fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
300fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
301fa9e4066Sahrens 	 */
302fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
303fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
304fa9e4066Sahrens 		return (ENOENT);
305fa9e4066Sahrens 
306fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
307fa9e4066Sahrens 		/*
308fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
309fa9e4066Sahrens 		 * global zone.
310fa9e4066Sahrens 		 */
311fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
312fa9e4066Sahrens 			return (EPERM);
313fa9e4066Sahrens 	} else {
314fa9e4066Sahrens 		/*
315fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
316fa9e4066Sahrens 		 */
317fa9e4066Sahrens 		if (!zoned)
318fa9e4066Sahrens 			return (EPERM);
319fa9e4066Sahrens 
320fa9e4066Sahrens 		/* must be writable by this zone */
321fa9e4066Sahrens 		if (!writable)
322fa9e4066Sahrens 			return (EPERM);
323fa9e4066Sahrens 	}
324fa9e4066Sahrens 	return (0);
325fa9e4066Sahrens }
326fa9e4066Sahrens 
327a7f53a56SChris Kirby static int
328a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
329a7f53a56SChris Kirby {
330a7f53a56SChris Kirby 	uint64_t zoned;
331a7f53a56SChris Kirby 
332a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
333a7f53a56SChris Kirby 		return (ENOENT);
334a7f53a56SChris Kirby 
335a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
336a7f53a56SChris Kirby }
337a7f53a56SChris Kirby 
338a7f53a56SChris Kirby static int
339a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
340a7f53a56SChris Kirby {
341a7f53a56SChris Kirby 	uint64_t zoned;
342a7f53a56SChris Kirby 
343a7f53a56SChris Kirby 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
344a7f53a56SChris Kirby 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
345a7f53a56SChris Kirby 		rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
346a7f53a56SChris Kirby 		return (ENOENT);
347a7f53a56SChris Kirby 	}
348a7f53a56SChris Kirby 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
349a7f53a56SChris Kirby 
350a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
351a7f53a56SChris Kirby }
352a7f53a56SChris Kirby 
353*19b94df9SMatthew Ahrens /*
354*19b94df9SMatthew Ahrens  * If name ends in a '@', then require recursive permissions.
355*19b94df9SMatthew Ahrens  */
356fa9e4066Sahrens int
357ecd6cf80Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
358fa9e4066Sahrens {
359fa9e4066Sahrens 	int error;
360*19b94df9SMatthew Ahrens 	boolean_t descendent = B_FALSE;
361*19b94df9SMatthew Ahrens 	dsl_dataset_t *ds;
362*19b94df9SMatthew Ahrens 	char *at;
363*19b94df9SMatthew Ahrens 
364*19b94df9SMatthew Ahrens 	at = strchr(name, '@');
365*19b94df9SMatthew Ahrens 	if (at != NULL && at[1] == '\0') {
366*19b94df9SMatthew Ahrens 		*at = '\0';
367*19b94df9SMatthew Ahrens 		descendent = B_TRUE;
368*19b94df9SMatthew Ahrens 	}
369*19b94df9SMatthew Ahrens 
370*19b94df9SMatthew Ahrens 	error = dsl_dataset_hold(name, FTAG, &ds);
371*19b94df9SMatthew Ahrens 	if (at != NULL)
372*19b94df9SMatthew Ahrens 		*at = '@';
373*19b94df9SMatthew Ahrens 	if (error != 0)
374*19b94df9SMatthew Ahrens 		return (error);
375fa9e4066Sahrens 
376*19b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
377ecd6cf80Smarks 	if (error == 0) {
378ecd6cf80Smarks 		error = secpolicy_zfs(cr);
379db870a07Sahrens 		if (error)
380*19b94df9SMatthew Ahrens 			error = dsl_deleg_access_impl(ds, descendent, perm, cr);
381ecd6cf80Smarks 	}
382*19b94df9SMatthew Ahrens 
383*19b94df9SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
384ecd6cf80Smarks 	return (error);
385ecd6cf80Smarks }
386ecd6cf80Smarks 
387a7f53a56SChris Kirby int
388a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
389a7f53a56SChris Kirby     const char *perm, cred_t *cr)
390a7f53a56SChris Kirby {
391a7f53a56SChris Kirby 	int error;
392a7f53a56SChris Kirby 
393a7f53a56SChris Kirby 	error = zfs_dozonecheck_ds(name, ds, cr);
394a7f53a56SChris Kirby 	if (error == 0) {
395a7f53a56SChris Kirby 		error = secpolicy_zfs(cr);
396a7f53a56SChris Kirby 		if (error)
397*19b94df9SMatthew Ahrens 			error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
398a7f53a56SChris Kirby 	}
399a7f53a56SChris Kirby 	return (error);
400a7f53a56SChris Kirby }
401a7f53a56SChris Kirby 
4024201a95eSRic Aleshire /*
4034201a95eSRic Aleshire  * Policy for setting the security label property.
4044201a95eSRic Aleshire  *
4054201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
4064201a95eSRic Aleshire  */
4074201a95eSRic Aleshire static int
40892241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
4094201a95eSRic Aleshire {
4104201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
4114201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
4124201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
4134201a95eSRic Aleshire 	uint64_t	zoned;
4144201a95eSRic Aleshire 	int		needed_priv = -1;
4154201a95eSRic Aleshire 	int		error;
4164201a95eSRic Aleshire 
4174201a95eSRic Aleshire 	/* First get the existing dataset label. */
4184201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
4194201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
4204201a95eSRic Aleshire 	if (error)
4214201a95eSRic Aleshire 		return (EPERM);
4224201a95eSRic Aleshire 
4234201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
4244201a95eSRic Aleshire 		new_default = TRUE;
4254201a95eSRic Aleshire 
4264201a95eSRic Aleshire 	/* The label must be translatable */
4274201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
4284201a95eSRic Aleshire 		return (EINVAL);
4294201a95eSRic Aleshire 
4304201a95eSRic Aleshire 	/*
4314201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
4324201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
4334201a95eSRic Aleshire 	 * are needed.
4344201a95eSRic Aleshire 	 */
4354201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
4364201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
4374201a95eSRic Aleshire 			return (EPERM);
4384201a95eSRic Aleshire 		return (0);
4394201a95eSRic Aleshire 	}
4404201a95eSRic Aleshire 
4414201a95eSRic Aleshire 	/*
4424201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
4434201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
4444201a95eSRic Aleshire 	 * global zone.
4454201a95eSRic Aleshire 	 */
4464201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
4474201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
4484201a95eSRic Aleshire 		return (EPERM);
4494201a95eSRic Aleshire 	if (!zoned) {
4504201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
4514201a95eSRic Aleshire 			return (EPERM);
4524201a95eSRic Aleshire 	}
4534201a95eSRic Aleshire 
4544201a95eSRic Aleshire 	/*
4554201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
4564201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
4574201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
4584201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
4594201a95eSRic Aleshire 	 */
4604201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
46192241e0bSTom Erickson 		objset_t *os;
46292241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
46392241e0bSTom Erickson 
4644201a95eSRic Aleshire 		/*
4654201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
4664201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
4674201a95eSRic Aleshire 		 */
4684201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
46992241e0bSTom Erickson 		    setsl_tag, &os);
4704201a95eSRic Aleshire 		if (error)
4714201a95eSRic Aleshire 			return (EPERM);
4724201a95eSRic Aleshire 
47392241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
47492241e0bSTom Erickson 
4754201a95eSRic Aleshire 		if (new_default) {
4764201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
4774201a95eSRic Aleshire 			goto out_check;
4784201a95eSRic Aleshire 		}
4794201a95eSRic Aleshire 
4804201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
4814201a95eSRic Aleshire 			return (EPERM);
4824201a95eSRic Aleshire 
4834201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
4844201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
4854201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
4864201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
4874201a95eSRic Aleshire 	} else {
4884201a95eSRic Aleshire 		/* dataset currently has a default label */
4894201a95eSRic Aleshire 		if (!new_default)
4904201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
4914201a95eSRic Aleshire 	}
4924201a95eSRic Aleshire 
4934201a95eSRic Aleshire out_check:
4944201a95eSRic Aleshire 	if (needed_priv != -1)
4954201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
4964201a95eSRic Aleshire 	return (0);
4974201a95eSRic Aleshire }
4984201a95eSRic Aleshire 
499ecd6cf80Smarks static int
50092241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
50192241e0bSTom Erickson     cred_t *cr)
502ecd6cf80Smarks {
50392241e0bSTom Erickson 	char *strval;
50492241e0bSTom Erickson 
505ecd6cf80Smarks 	/*
506ecd6cf80Smarks 	 * Check permissions for special properties.
507ecd6cf80Smarks 	 */
508ecd6cf80Smarks 	switch (prop) {
509ecd6cf80Smarks 	case ZFS_PROP_ZONED:
510ecd6cf80Smarks 		/*
511ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
512ecd6cf80Smarks 		 */
513ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
514ecd6cf80Smarks 			return (EPERM);
515ecd6cf80Smarks 		break;
516ecd6cf80Smarks 
517ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
518ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
519ecd6cf80Smarks 			uint64_t zoned;
520ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
521ecd6cf80Smarks 			/*
522ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
523ecd6cf80Smarks 			 * quota on things *under* (ie. contained by)
524ecd6cf80Smarks 			 * the thing they own.
525ecd6cf80Smarks 			 */
52692241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
527ecd6cf80Smarks 			    setpoint))
528ecd6cf80Smarks 				return (EPERM);
52992241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
530ecd6cf80Smarks 				return (EPERM);
531ecd6cf80Smarks 		}
532db870a07Sahrens 		break;
5334201a95eSRic Aleshire 
5344201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
5354201a95eSRic Aleshire 		if (!is_system_labeled())
5364201a95eSRic Aleshire 			return (EPERM);
53792241e0bSTom Erickson 
53892241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
53992241e0bSTom Erickson 			int err;
54092241e0bSTom Erickson 
54192241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
54292241e0bSTom Erickson 			if (err != 0)
54392241e0bSTom Erickson 				return (err);
54492241e0bSTom Erickson 		}
5454201a95eSRic Aleshire 		break;
546ecd6cf80Smarks 	}
547ecd6cf80Smarks 
54892241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
549ecd6cf80Smarks }
550ecd6cf80Smarks 
551ecd6cf80Smarks int
552ecd6cf80Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
553ecd6cf80Smarks {
554ecd6cf80Smarks 	int error;
555ecd6cf80Smarks 
556ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
557ecd6cf80Smarks 	if (error)
558fa9e4066Sahrens 		return (error);
559fa9e4066Sahrens 
560ecd6cf80Smarks 	/*
561ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
562ecd6cf80Smarks 	 * dsl_deleg_can_allow()
563ecd6cf80Smarks 	 */
564ecd6cf80Smarks 	return (0);
565ecd6cf80Smarks }
566ecd6cf80Smarks 
567ecd6cf80Smarks int
568ecd6cf80Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
569ecd6cf80Smarks {
570681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
571681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
572ecd6cf80Smarks }
573ecd6cf80Smarks 
574ecd6cf80Smarks int
575ecd6cf80Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
576ecd6cf80Smarks {
577a7f53a56SChris Kirby 	spa_t *spa;
578a7f53a56SChris Kirby 	dsl_pool_t *dp;
579a7f53a56SChris Kirby 	dsl_dataset_t *ds;
580a7f53a56SChris Kirby 	char *cp;
581a7f53a56SChris Kirby 	int error;
582a7f53a56SChris Kirby 
583a7f53a56SChris Kirby 	/*
584a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
585a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
586a7f53a56SChris Kirby 	 */
587a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
588a7f53a56SChris Kirby 	if (cp == NULL)
589a7f53a56SChris Kirby 		return (EINVAL);
590a7f53a56SChris Kirby 	error = spa_open(zc->zc_name, &spa, FTAG);
591a7f53a56SChris Kirby 	if (error)
592a7f53a56SChris Kirby 		return (error);
593a7f53a56SChris Kirby 
594a7f53a56SChris Kirby 	dp = spa_get_dsl(spa);
595a7f53a56SChris Kirby 	rw_enter(&dp->dp_config_rwlock, RW_READER);
596a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
597a7f53a56SChris Kirby 	rw_exit(&dp->dp_config_rwlock);
598a7f53a56SChris Kirby 	spa_close(spa, FTAG);
599a7f53a56SChris Kirby 	if (error)
600a7f53a56SChris Kirby 		return (error);
601a7f53a56SChris Kirby 
602a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
603a7f53a56SChris Kirby 
604a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
605a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
606a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
607a7f53a56SChris Kirby 
608a7f53a56SChris Kirby 	return (error);
609ecd6cf80Smarks }
610ecd6cf80Smarks 
611743a77edSAlan Wright static int
612743a77edSAlan Wright zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
613743a77edSAlan Wright {
614743a77edSAlan Wright 	vnode_t *vp;
615743a77edSAlan Wright 	int error;
616743a77edSAlan Wright 
617743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
618743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
619743a77edSAlan Wright 		return (error);
620743a77edSAlan Wright 
621743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
622743a77edSAlan Wright 
623743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
624743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
625743a77edSAlan Wright 	    zc->zc_name) != 0)) {
626743a77edSAlan Wright 		VN_RELE(vp);
627743a77edSAlan Wright 		return (EPERM);
628743a77edSAlan Wright 	}
629743a77edSAlan Wright 
630743a77edSAlan Wright 	VN_RELE(vp);
631743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
632743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
633743a77edSAlan Wright }
634743a77edSAlan Wright 
635ecd6cf80Smarks int
636ecd6cf80Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
637ecd6cf80Smarks {
638ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
639ecd6cf80Smarks 		return (EPERM);
640ecd6cf80Smarks 
6413cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
642ecd6cf80Smarks 		return (0);
643ecd6cf80Smarks 	} else {
644743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
645743a77edSAlan Wright 	}
646743a77edSAlan Wright }
647ecd6cf80Smarks 
648743a77edSAlan Wright int
649743a77edSAlan Wright zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
650743a77edSAlan Wright {
651743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
652743a77edSAlan Wright 		return (EPERM);
653ecd6cf80Smarks 
654743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
655743a77edSAlan Wright 		return (0);
656743a77edSAlan Wright 	} else {
657743a77edSAlan Wright 		return (zfs_secpolicy_deleg_share(zc, cr));
658ecd6cf80Smarks 	}
659fa9e4066Sahrens }
660fa9e4066Sahrens 
661fa9e4066Sahrens static int
662ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
663fa9e4066Sahrens {
664fa9e4066Sahrens 	char *cp;
665fa9e4066Sahrens 
666fa9e4066Sahrens 	/*
667fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
668fa9e4066Sahrens 	 */
669ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
670ecd6cf80Smarks 	cp = strrchr(parent, '@');
671fa9e4066Sahrens 	if (cp != NULL) {
672fa9e4066Sahrens 		cp[0] = '\0';
673fa9e4066Sahrens 	} else {
674ecd6cf80Smarks 		cp = strrchr(parent, '/');
675fa9e4066Sahrens 		if (cp == NULL)
676fa9e4066Sahrens 			return (ENOENT);
677fa9e4066Sahrens 		cp[0] = '\0';
678ecd6cf80Smarks 	}
679ecd6cf80Smarks 
680ecd6cf80Smarks 	return (0);
681ecd6cf80Smarks }
682ecd6cf80Smarks 
683ecd6cf80Smarks int
684ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
685ecd6cf80Smarks {
686ecd6cf80Smarks 	int error;
687ecd6cf80Smarks 
688ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
689ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
690ecd6cf80Smarks 		return (error);
691ecd6cf80Smarks 
692ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
693ecd6cf80Smarks }
694ecd6cf80Smarks 
695ecd6cf80Smarks static int
696ecd6cf80Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
697ecd6cf80Smarks {
698ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
699ecd6cf80Smarks }
700ecd6cf80Smarks 
701cbf6f6aaSWilliam Gorrell /*
702cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
703cbf6f6aaSWilliam Gorrell  * descendent mount and destroy permissions.
704cbf6f6aaSWilliam Gorrell  */
705cbf6f6aaSWilliam Gorrell static int
706*19b94df9SMatthew Ahrens zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr)
707cbf6f6aaSWilliam Gorrell {
708cbf6f6aaSWilliam Gorrell 	int error;
709cbf6f6aaSWilliam Gorrell 	char *dsname;
710cbf6f6aaSWilliam Gorrell 
711*19b94df9SMatthew Ahrens 	dsname = kmem_asprintf("%s@", zc->zc_name);
712cbf6f6aaSWilliam Gorrell 
713cbf6f6aaSWilliam Gorrell 	error = zfs_secpolicy_destroy_perms(dsname, cr);
714cbf6f6aaSWilliam Gorrell 
715cbf6f6aaSWilliam Gorrell 	strfree(dsname);
716cbf6f6aaSWilliam Gorrell 	return (error);
717cbf6f6aaSWilliam Gorrell }
718cbf6f6aaSWilliam Gorrell 
719ecd6cf80Smarks int
720ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
721ecd6cf80Smarks {
72292241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
723ecd6cf80Smarks 	int	error;
724ecd6cf80Smarks 
725ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
726ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
727ecd6cf80Smarks 		return (error);
728ecd6cf80Smarks 
729ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
730ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
731ecd6cf80Smarks 		return (error);
732ecd6cf80Smarks 
733ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
734ecd6cf80Smarks 	    sizeof (parentname))) != 0)
735ecd6cf80Smarks 		return (error);
736ecd6cf80Smarks 
737ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
738ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
739ecd6cf80Smarks 		return (error);
740ecd6cf80Smarks 
741ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
742ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
743ecd6cf80Smarks 		return (error);
744ecd6cf80Smarks 
745ecd6cf80Smarks 	return (error);
746ecd6cf80Smarks }
747ecd6cf80Smarks 
748ecd6cf80Smarks static int
749ecd6cf80Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
750ecd6cf80Smarks {
751ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
752ecd6cf80Smarks }
753ecd6cf80Smarks 
754ecd6cf80Smarks static int
755ecd6cf80Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
756ecd6cf80Smarks {
75792241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
758ecd6cf80Smarks 	objset_t *clone;
759ecd6cf80Smarks 	int error;
760ecd6cf80Smarks 
761ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
762ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
763ecd6cf80Smarks 	if (error)
764ecd6cf80Smarks 		return (error);
765ecd6cf80Smarks 
766503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
767ecd6cf80Smarks 
768ecd6cf80Smarks 	if (error == 0) {
769ecd6cf80Smarks 		dsl_dataset_t *pclone = NULL;
770ecd6cf80Smarks 		dsl_dir_t *dd;
771503ad85cSMatthew Ahrens 		dd = clone->os_dsl_dataset->ds_dir;
772ecd6cf80Smarks 
773ecd6cf80Smarks 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
774745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
775745cd3c5Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
776ecd6cf80Smarks 		rw_exit(&dd->dd_pool->dp_config_rwlock);
777ecd6cf80Smarks 		if (error) {
778503ad85cSMatthew Ahrens 			dmu_objset_rele(clone, FTAG);
779ecd6cf80Smarks 			return (error);
780ecd6cf80Smarks 		}
781ecd6cf80Smarks 
782ecd6cf80Smarks 		error = zfs_secpolicy_write_perms(zc->zc_name,
783ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
784ecd6cf80Smarks 
785ecd6cf80Smarks 		dsl_dataset_name(pclone, parentname);
786503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
787745cd3c5Smaybee 		dsl_dataset_rele(pclone, FTAG);
788ecd6cf80Smarks 		if (error == 0)
789ecd6cf80Smarks 			error = zfs_secpolicy_write_perms(parentname,
790ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
791ecd6cf80Smarks 	}
792ecd6cf80Smarks 	return (error);
793ecd6cf80Smarks }
794ecd6cf80Smarks 
795ecd6cf80Smarks static int
796ecd6cf80Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
797ecd6cf80Smarks {
798ecd6cf80Smarks 	int error;
799ecd6cf80Smarks 
800ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
801ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
802ecd6cf80Smarks 		return (error);
803ecd6cf80Smarks 
804ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
805ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
806ecd6cf80Smarks 		return (error);
807ecd6cf80Smarks 
808ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
809ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
810ecd6cf80Smarks }
811ecd6cf80Smarks 
812ecd6cf80Smarks int
813ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
814ecd6cf80Smarks {
815681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
816681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
817ecd6cf80Smarks }
818ecd6cf80Smarks 
819ecd6cf80Smarks static int
820ecd6cf80Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
821ecd6cf80Smarks {
822ecd6cf80Smarks 
823ecd6cf80Smarks 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
824ecd6cf80Smarks }
825ecd6cf80Smarks 
826ecd6cf80Smarks static int
827ecd6cf80Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
828ecd6cf80Smarks {
82992241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
83092241e0bSTom Erickson 	int	error;
831ecd6cf80Smarks 
832ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
833ecd6cf80Smarks 	    sizeof (parentname))) != 0)
834ecd6cf80Smarks 		return (error);
835fa9e4066Sahrens 
836ecd6cf80Smarks 	if (zc->zc_value[0] != '\0') {
837ecd6cf80Smarks 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
838ecd6cf80Smarks 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
839ecd6cf80Smarks 			return (error);
840fa9e4066Sahrens 	}
841fa9e4066Sahrens 
842ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
843ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
844ecd6cf80Smarks 		return (error);
845ecd6cf80Smarks 
846ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(parentname,
847ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr);
848ecd6cf80Smarks 
849ecd6cf80Smarks 	return (error);
850ecd6cf80Smarks }
851ecd6cf80Smarks 
852ecd6cf80Smarks static int
853ecd6cf80Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
854ecd6cf80Smarks {
855ecd6cf80Smarks 	int error;
856ecd6cf80Smarks 
857ecd6cf80Smarks 	error = secpolicy_fs_unmount(cr, NULL);
858ecd6cf80Smarks 	if (error) {
859ecd6cf80Smarks 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
860ecd6cf80Smarks 	}
861ecd6cf80Smarks 	return (error);
862fa9e4066Sahrens }
863fa9e4066Sahrens 
864fa9e4066Sahrens /*
865fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
866fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
867fa9e4066Sahrens  */
868fa9e4066Sahrens /* ARGSUSED */
869fa9e4066Sahrens static int
870ecd6cf80Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
871fa9e4066Sahrens {
872fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
873fa9e4066Sahrens 		return (EPERM);
874fa9e4066Sahrens 
875fa9e4066Sahrens 	return (0);
876fa9e4066Sahrens }
877fa9e4066Sahrens 
87899d5e173STim Haley /*
87999d5e173STim Haley  * Policy for object to name lookups.
88099d5e173STim Haley  */
88199d5e173STim Haley /* ARGSUSED */
88299d5e173STim Haley static int
88399d5e173STim Haley zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
88499d5e173STim Haley {
88599d5e173STim Haley 	int error;
88699d5e173STim Haley 
88799d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
88899d5e173STim Haley 		return (0);
88999d5e173STim Haley 
89099d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
89199d5e173STim Haley 	return (error);
89299d5e173STim Haley }
89399d5e173STim Haley 
894ea8dc4b6Seschrock /*
895ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
896ea8dc4b6Seschrock  */
897ea8dc4b6Seschrock /* ARGSUSED */
898ea8dc4b6Seschrock static int
899ecd6cf80Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
900ea8dc4b6Seschrock {
901ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
902ea8dc4b6Seschrock }
903ea8dc4b6Seschrock 
904e45ce728Sahrens static int
905e45ce728Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
906e45ce728Sahrens {
907e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
908e45ce728Sahrens 
909990b4856Slling 	if (prop == ZPROP_INVAL) {
910e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
911e45ce728Sahrens 			return (EINVAL);
912e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
913e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
914e45ce728Sahrens 	} else {
91592241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
91692241e0bSTom Erickson 		    NULL, cr));
917e45ce728Sahrens 	}
918e45ce728Sahrens }
919e45ce728Sahrens 
92014843421SMatthew Ahrens static int
92114843421SMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
92214843421SMatthew Ahrens {
92314843421SMatthew Ahrens 	int err = zfs_secpolicy_read(zc, cr);
92414843421SMatthew Ahrens 	if (err)
92514843421SMatthew Ahrens 		return (err);
92614843421SMatthew Ahrens 
92714843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
92814843421SMatthew Ahrens 		return (EINVAL);
92914843421SMatthew Ahrens 
93014843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
93114843421SMatthew Ahrens 		/*
93214843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
93314843421SMatthew Ahrens 		 * themself, allow it.
93414843421SMatthew Ahrens 		 */
93514843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
93614843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
93714843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
93814843421SMatthew Ahrens 				return (0);
93914843421SMatthew Ahrens 		} else {
94014843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
94114843421SMatthew Ahrens 				return (0);
94214843421SMatthew Ahrens 		}
94314843421SMatthew Ahrens 	}
94414843421SMatthew Ahrens 
94514843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
94614843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
94714843421SMatthew Ahrens }
94814843421SMatthew Ahrens 
94914843421SMatthew Ahrens static int
95014843421SMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
95114843421SMatthew Ahrens {
95214843421SMatthew Ahrens 	int err = zfs_secpolicy_read(zc, cr);
95314843421SMatthew Ahrens 	if (err)
95414843421SMatthew Ahrens 		return (err);
95514843421SMatthew Ahrens 
95614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
95714843421SMatthew Ahrens 		return (EINVAL);
95814843421SMatthew Ahrens 
95914843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
96014843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
96114843421SMatthew Ahrens }
96214843421SMatthew Ahrens 
96314843421SMatthew Ahrens static int
96414843421SMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
96514843421SMatthew Ahrens {
96692241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
96792241e0bSTom Erickson 	    NULL, cr));
96814843421SMatthew Ahrens }
96914843421SMatthew Ahrens 
970842727c2SChris Kirby static int
971842727c2SChris Kirby zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
972842727c2SChris Kirby {
973842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
974842727c2SChris Kirby 	    ZFS_DELEG_PERM_HOLD, cr));
975842727c2SChris Kirby }
976842727c2SChris Kirby 
977842727c2SChris Kirby static int
978842727c2SChris Kirby zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
979842727c2SChris Kirby {
980842727c2SChris Kirby 	return (zfs_secpolicy_write_perms(zc->zc_name,
981842727c2SChris Kirby 	    ZFS_DELEG_PERM_RELEASE, cr));
982842727c2SChris Kirby }
983842727c2SChris Kirby 
98499d5e173STim Haley /*
98599d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
98699d5e173STim Haley  */
98799d5e173STim Haley static int
98899d5e173STim Haley zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
98999d5e173STim Haley {
99099d5e173STim Haley 	/*
99199d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
99299d5e173STim Haley 	 * hold, destroy and release all rolled into one.
99399d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
99499d5e173STim Haley 	 */
99599d5e173STim Haley 	int error;
99699d5e173STim Haley 
99799d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
99899d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
99999d5e173STim Haley 		return (0);
100099d5e173STim Haley 
100199d5e173STim Haley 	error = zfs_secpolicy_snapshot(zc, cr);
100299d5e173STim Haley 	if (!error)
100399d5e173STim Haley 		error = zfs_secpolicy_hold(zc, cr);
100499d5e173STim Haley 	if (!error)
100599d5e173STim Haley 		error = zfs_secpolicy_release(zc, cr);
100699d5e173STim Haley 	if (!error)
100799d5e173STim Haley 		error = zfs_secpolicy_destroy(zc, cr);
100899d5e173STim Haley 	return (error);
100999d5e173STim Haley }
101099d5e173STim Haley 
1011fa9e4066Sahrens /*
1012fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1013fa9e4066Sahrens  */
1014fa9e4066Sahrens static int
1015478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1016fa9e4066Sahrens {
1017fa9e4066Sahrens 	char *packed;
1018fa9e4066Sahrens 	int error;
1019990b4856Slling 	nvlist_t *list = NULL;
1020fa9e4066Sahrens 
1021fa9e4066Sahrens 	/*
1022e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1023fa9e4066Sahrens 	 */
1024990b4856Slling 	if (size == 0)
1025fa9e4066Sahrens 		return (EINVAL);
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1028fa9e4066Sahrens 
1029478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1030478ed9adSEric Taylor 	    iflag)) != 0) {
1031fa9e4066Sahrens 		kmem_free(packed, size);
1032fa9e4066Sahrens 		return (error);
1033fa9e4066Sahrens 	}
1034fa9e4066Sahrens 
1035990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1036fa9e4066Sahrens 		kmem_free(packed, size);
1037fa9e4066Sahrens 		return (error);
1038fa9e4066Sahrens 	}
1039fa9e4066Sahrens 
1040fa9e4066Sahrens 	kmem_free(packed, size);
1041fa9e4066Sahrens 
1042990b4856Slling 	*nvp = list;
1043fa9e4066Sahrens 	return (0);
1044fa9e4066Sahrens }
1045fa9e4066Sahrens 
104692241e0bSTom Erickson static int
104792241e0bSTom Erickson fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
104892241e0bSTom Erickson {
104992241e0bSTom Erickson 	size_t size;
105092241e0bSTom Erickson 
105192241e0bSTom Erickson 	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
105292241e0bSTom Erickson 
105392241e0bSTom Erickson 	if (size > zc->zc_nvlist_dst_size) {
105492241e0bSTom Erickson 		nvpair_t *more_errors;
105592241e0bSTom Erickson 		int n = 0;
105692241e0bSTom Erickson 
105792241e0bSTom Erickson 		if (zc->zc_nvlist_dst_size < 1024)
105892241e0bSTom Erickson 			return (ENOMEM);
105992241e0bSTom Erickson 
106092241e0bSTom Erickson 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
106192241e0bSTom Erickson 		more_errors = nvlist_prev_nvpair(*errors, NULL);
106292241e0bSTom Erickson 
106392241e0bSTom Erickson 		do {
106492241e0bSTom Erickson 			nvpair_t *pair = nvlist_prev_nvpair(*errors,
106592241e0bSTom Erickson 			    more_errors);
106692241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
106792241e0bSTom Erickson 			n++;
106892241e0bSTom Erickson 			VERIFY(nvlist_size(*errors, &size,
106992241e0bSTom Erickson 			    NV_ENCODE_NATIVE) == 0);
107092241e0bSTom Erickson 		} while (size > zc->zc_nvlist_dst_size);
107192241e0bSTom Erickson 
107292241e0bSTom Erickson 		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
107392241e0bSTom Erickson 		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
107492241e0bSTom Erickson 		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
107592241e0bSTom Erickson 		ASSERT(size <= zc->zc_nvlist_dst_size);
107692241e0bSTom Erickson 	}
107792241e0bSTom Erickson 
107892241e0bSTom Erickson 	return (0);
107992241e0bSTom Erickson }
108092241e0bSTom Erickson 
1081e9dbad6fSeschrock static int
1082e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1083e9dbad6fSeschrock {
1084e9dbad6fSeschrock 	char *packed = NULL;
10856e27f868SSam Falkner 	int error = 0;
1086e9dbad6fSeschrock 	size_t size;
1087e9dbad6fSeschrock 
1088e9dbad6fSeschrock 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1089e9dbad6fSeschrock 
1090e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1091e9dbad6fSeschrock 		error = ENOMEM;
1092e9dbad6fSeschrock 	} else {
1093da165920Smarks 		packed = kmem_alloc(size, KM_SLEEP);
1094e9dbad6fSeschrock 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1095e9dbad6fSeschrock 		    KM_SLEEP) == 0);
10966e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
10976e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
10986e27f868SSam Falkner 			error = EFAULT;
1099e9dbad6fSeschrock 		kmem_free(packed, size);
1100e9dbad6fSeschrock 	}
1101e9dbad6fSeschrock 
1102e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
1103e9dbad6fSeschrock 	return (error);
1104e9dbad6fSeschrock }
1105e9dbad6fSeschrock 
110614843421SMatthew Ahrens static int
1107af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
110814843421SMatthew Ahrens {
110914843421SMatthew Ahrens 	objset_t *os;
111014843421SMatthew Ahrens 	int error;
111114843421SMatthew Ahrens 
1112503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
111314843421SMatthew Ahrens 	if (error)
111414843421SMatthew Ahrens 		return (error);
1115503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1116503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1117503ad85cSMatthew Ahrens 		return (EINVAL);
1118503ad85cSMatthew Ahrens 	}
111914843421SMatthew Ahrens 
1120503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1121af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1122af4c679fSSean McEnroe 	if (*zfvp) {
1123af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
112414843421SMatthew Ahrens 	} else {
112514843421SMatthew Ahrens 		error = ESRCH;
112614843421SMatthew Ahrens 	}
1127503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1128503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
112914843421SMatthew Ahrens 	return (error);
113014843421SMatthew Ahrens }
113114843421SMatthew Ahrens 
113214843421SMatthew Ahrens /*
113314843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
113414843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
113514843421SMatthew Ahrens  */
113614843421SMatthew Ahrens static int
11371412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
113814843421SMatthew Ahrens {
113914843421SMatthew Ahrens 	int error = 0;
114014843421SMatthew Ahrens 
1141af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1142af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
114314843421SMatthew Ahrens 	if (error == 0) {
11441412a1a2SMark Shellenbaum 		rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
11451412a1a2SMark Shellenbaum 		    RW_READER, tag);
1146af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
114714843421SMatthew Ahrens 			/*
114814843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
114914843421SMatthew Ahrens 			 * thread should be just about to disassociate the
115014843421SMatthew Ahrens 			 * objset from the zfsvfs.
115114843421SMatthew Ahrens 			 */
1152af4c679fSSean McEnroe 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
115314843421SMatthew Ahrens 			return (EBUSY);
115414843421SMatthew Ahrens 		}
115514843421SMatthew Ahrens 	}
115614843421SMatthew Ahrens 	return (error);
115714843421SMatthew Ahrens }
115814843421SMatthew Ahrens 
115914843421SMatthew Ahrens static void
116014843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
116114843421SMatthew Ahrens {
116214843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
116314843421SMatthew Ahrens 
116414843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
116514843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
116614843421SMatthew Ahrens 	} else {
1167503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
116814843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
116914843421SMatthew Ahrens 	}
117014843421SMatthew Ahrens }
117114843421SMatthew Ahrens 
1172fa9e4066Sahrens static int
1173fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1174fa9e4066Sahrens {
1175fa9e4066Sahrens 	int error;
1176990b4856Slling 	nvlist_t *config, *props = NULL;
11770a48a24eStimh 	nvlist_t *rootprops = NULL;
11780a48a24eStimh 	nvlist_t *zplprops = NULL;
1179228975ccSek 	char *buf;
1180fa9e4066Sahrens 
1181990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1182478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1183fa9e4066Sahrens 		return (error);
11842a6b87f0Sek 
1185990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1186478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1187478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1188990b4856Slling 		nvlist_free(config);
1189990b4856Slling 		return (error);
1190990b4856Slling 	}
1191990b4856Slling 
11920a48a24eStimh 	if (props) {
11930a48a24eStimh 		nvlist_t *nvl = NULL;
11940a48a24eStimh 		uint64_t version = SPA_VERSION;
11950a48a24eStimh 
11960a48a24eStimh 		(void) nvlist_lookup_uint64(props,
11970a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
11980a48a24eStimh 		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
11990a48a24eStimh 			error = EINVAL;
12000a48a24eStimh 			goto pool_props_bad;
12010a48a24eStimh 		}
12020a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
12030a48a24eStimh 		if (nvl) {
12040a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
12050a48a24eStimh 			if (error != 0) {
12060a48a24eStimh 				nvlist_free(config);
12070a48a24eStimh 				nvlist_free(props);
12080a48a24eStimh 				return (error);
12090a48a24eStimh 			}
12100a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
12110a48a24eStimh 		}
12120a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
12130a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
12140a48a24eStimh 		    zplprops, NULL);
12150a48a24eStimh 		if (error)
12160a48a24eStimh 			goto pool_props_bad;
12170a48a24eStimh 	}
12180a48a24eStimh 
12192a6b87f0Sek 	buf = history_str_get(zc);
1220fa9e4066Sahrens 
12210a48a24eStimh 	error = spa_create(zc->zc_name, config, props, buf, zplprops);
12220a48a24eStimh 
12230a48a24eStimh 	/*
12240a48a24eStimh 	 * Set the remaining root properties
12250a48a24eStimh 	 */
122692241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
122792241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
12280a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1229fa9e4066Sahrens 
12302a6b87f0Sek 	if (buf != NULL)
12312a6b87f0Sek 		history_str_free(buf);
1232990b4856Slling 
12330a48a24eStimh pool_props_bad:
12340a48a24eStimh 	nvlist_free(rootprops);
12350a48a24eStimh 	nvlist_free(zplprops);
1236fa9e4066Sahrens 	nvlist_free(config);
12370a48a24eStimh 	nvlist_free(props);
1238990b4856Slling 
1239fa9e4066Sahrens 	return (error);
1240fa9e4066Sahrens }
1241fa9e4066Sahrens 
1242fa9e4066Sahrens static int
1243fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1244fa9e4066Sahrens {
1245ecd6cf80Smarks 	int error;
1246ecd6cf80Smarks 	zfs_log_history(zc);
1247ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1248681d9761SEric Taylor 	if (error == 0)
1249681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1250ecd6cf80Smarks 	return (error);
1251fa9e4066Sahrens }
1252fa9e4066Sahrens 
1253fa9e4066Sahrens static int
1254fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1255fa9e4066Sahrens {
1256990b4856Slling 	nvlist_t *config, *props = NULL;
1257fa9e4066Sahrens 	uint64_t guid;
1258468c413aSTim Haley 	int error;
1259fa9e4066Sahrens 
1260990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1261478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1262990b4856Slling 		return (error);
1263990b4856Slling 
1264990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1265478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1266478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1267990b4856Slling 		nvlist_free(config);
1268fa9e4066Sahrens 		return (error);
1269990b4856Slling 	}
1270fa9e4066Sahrens 
1271fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1272ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1273fa9e4066Sahrens 		error = EINVAL;
1274fa9e4066Sahrens 	else
12754b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1276fa9e4066Sahrens 
12774b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
12784b964adaSGeorge Wilson 		int err;
12794b964adaSGeorge Wilson 
12804b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
12814b964adaSGeorge Wilson 			error = err;
12824b964adaSGeorge Wilson 	}
1283468c413aSTim Haley 
1284fa9e4066Sahrens 	nvlist_free(config);
1285fa9e4066Sahrens 
1286990b4856Slling 	if (props)
1287990b4856Slling 		nvlist_free(props);
1288990b4856Slling 
1289fa9e4066Sahrens 	return (error);
1290fa9e4066Sahrens }
1291fa9e4066Sahrens 
1292fa9e4066Sahrens static int
1293fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1294fa9e4066Sahrens {
1295ecd6cf80Smarks 	int error;
129689a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1297394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
129889a89ebfSlling 
1299ecd6cf80Smarks 	zfs_log_history(zc);
1300394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1301681d9761SEric Taylor 	if (error == 0)
1302681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1303ecd6cf80Smarks 	return (error);
1304fa9e4066Sahrens }
1305fa9e4066Sahrens 
1306fa9e4066Sahrens static int
1307fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1308fa9e4066Sahrens {
1309fa9e4066Sahrens 	nvlist_t *configs;
1310fa9e4066Sahrens 	int error;
1311fa9e4066Sahrens 
1312fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1313fa9e4066Sahrens 		return (EEXIST);
1314fa9e4066Sahrens 
1315e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	nvlist_free(configs);
1318fa9e4066Sahrens 
1319fa9e4066Sahrens 	return (error);
1320fa9e4066Sahrens }
1321fa9e4066Sahrens 
1322fa9e4066Sahrens static int
1323fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1324fa9e4066Sahrens {
1325fa9e4066Sahrens 	nvlist_t *config;
1326fa9e4066Sahrens 	int error;
1327ea8dc4b6Seschrock 	int ret = 0;
1328fa9e4066Sahrens 
1329e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1330e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	if (config != NULL) {
1333e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1334fa9e4066Sahrens 		nvlist_free(config);
1335ea8dc4b6Seschrock 
1336ea8dc4b6Seschrock 		/*
1337ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1338ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1339ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1340ea8dc4b6Seschrock 		 */
1341ea8dc4b6Seschrock 		zc->zc_cookie = error;
1342fa9e4066Sahrens 	} else {
1343ea8dc4b6Seschrock 		ret = error;
1344fa9e4066Sahrens 	}
1345fa9e4066Sahrens 
1346ea8dc4b6Seschrock 	return (ret);
1347fa9e4066Sahrens }
1348fa9e4066Sahrens 
1349fa9e4066Sahrens /*
1350fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1351fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1352fa9e4066Sahrens  */
1353fa9e4066Sahrens static int
1354fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1355fa9e4066Sahrens {
1356fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1357fa9e4066Sahrens 	int error;
1358fa9e4066Sahrens 
1359990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1360478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1361fa9e4066Sahrens 		return (error);
1362fa9e4066Sahrens 
1363fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1364fa9e4066Sahrens 
1365fa9e4066Sahrens 	nvlist_free(tryconfig);
1366fa9e4066Sahrens 
1367fa9e4066Sahrens 	if (config == NULL)
1368fa9e4066Sahrens 		return (EINVAL);
1369fa9e4066Sahrens 
1370e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1371fa9e4066Sahrens 	nvlist_free(config);
1372fa9e4066Sahrens 
1373fa9e4066Sahrens 	return (error);
1374fa9e4066Sahrens }
1375fa9e4066Sahrens 
13763f9d6ad7SLin Ling /*
13773f9d6ad7SLin Ling  * inputs:
13783f9d6ad7SLin Ling  * zc_name              name of the pool
13793f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
13803f9d6ad7SLin Ling  */
1381fa9e4066Sahrens static int
13823f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1383fa9e4066Sahrens {
1384fa9e4066Sahrens 	spa_t *spa;
1385fa9e4066Sahrens 	int error;
1386fa9e4066Sahrens 
138706eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
138806eeb2adSek 		return (error);
138906eeb2adSek 
13903f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
13913f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
13923f9d6ad7SLin Ling 	else
13933f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
139406eeb2adSek 
139506eeb2adSek 	spa_close(spa, FTAG);
139606eeb2adSek 
1397fa9e4066Sahrens 	return (error);
1398fa9e4066Sahrens }
1399fa9e4066Sahrens 
1400fa9e4066Sahrens static int
1401fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1402fa9e4066Sahrens {
1403fa9e4066Sahrens 	spa_t *spa;
1404fa9e4066Sahrens 	int error;
1405fa9e4066Sahrens 
1406fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1407fa9e4066Sahrens 	if (error == 0) {
1408fa9e4066Sahrens 		spa_freeze(spa);
1409fa9e4066Sahrens 		spa_close(spa, FTAG);
1410fa9e4066Sahrens 	}
1411fa9e4066Sahrens 	return (error);
1412fa9e4066Sahrens }
1413fa9e4066Sahrens 
1414eaca9bbdSeschrock static int
1415eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1416eaca9bbdSeschrock {
1417eaca9bbdSeschrock 	spa_t *spa;
1418eaca9bbdSeschrock 	int error;
1419eaca9bbdSeschrock 
142006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
142106eeb2adSek 		return (error);
142206eeb2adSek 
1423558d2d50Slling 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1424558d2d50Slling 		spa_close(spa, FTAG);
1425558d2d50Slling 		return (EINVAL);
1426558d2d50Slling 	}
1427558d2d50Slling 
1428990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
142906eeb2adSek 	spa_close(spa, FTAG);
143006eeb2adSek 
143106eeb2adSek 	return (error);
143206eeb2adSek }
143306eeb2adSek 
143406eeb2adSek static int
143506eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
143606eeb2adSek {
143706eeb2adSek 	spa_t *spa;
143806eeb2adSek 	char *hist_buf;
143906eeb2adSek 	uint64_t size;
144006eeb2adSek 	int error;
144106eeb2adSek 
144206eeb2adSek 	if ((size = zc->zc_history_len) == 0)
144306eeb2adSek 		return (EINVAL);
144406eeb2adSek 
144506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
144606eeb2adSek 		return (error);
144706eeb2adSek 
1448e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1449d7306b64Sek 		spa_close(spa, FTAG);
1450d7306b64Sek 		return (ENOTSUP);
1451d7306b64Sek 	}
1452d7306b64Sek 
145306eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
145406eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
145506eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1456478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1457478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1458478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
145906eeb2adSek 	}
146006eeb2adSek 
146106eeb2adSek 	spa_close(spa, FTAG);
146206eeb2adSek 	kmem_free(hist_buf, size);
146306eeb2adSek 	return (error);
146406eeb2adSek }
146506eeb2adSek 
1466e9103aaeSGarrett D'Amore static int
1467e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1468e9103aaeSGarrett D'Amore {
1469e9103aaeSGarrett D'Amore 	spa_t *spa;
1470e9103aaeSGarrett D'Amore 	int error;
1471e9103aaeSGarrett D'Amore 
1472e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1473e9103aaeSGarrett D'Amore 	if (error == 0) {
1474e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1475e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1476e9103aaeSGarrett D'Amore 	}
1477e9103aaeSGarrett D'Amore 	return (error);
1478e9103aaeSGarrett D'Amore }
1479e9103aaeSGarrett D'Amore 
148055434c77Sek static int
148155434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
148255434c77Sek {
148355434c77Sek 	int error;
148455434c77Sek 
1485b1b8ab34Slling 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
148655434c77Sek 		return (error);
148755434c77Sek 
148855434c77Sek 	return (0);
148955434c77Sek }
149055434c77Sek 
1491503ad85cSMatthew Ahrens /*
1492503ad85cSMatthew Ahrens  * inputs:
1493503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1494503ad85cSMatthew Ahrens  * zc_obj		object to find
1495503ad85cSMatthew Ahrens  *
1496503ad85cSMatthew Ahrens  * outputs:
1497503ad85cSMatthew Ahrens  * zc_value		name of object
1498503ad85cSMatthew Ahrens  */
149955434c77Sek static int
150055434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
150155434c77Sek {
1502503ad85cSMatthew Ahrens 	objset_t *os;
150355434c77Sek 	int error;
150455434c77Sek 
1505503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1506503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
150755434c77Sek 		return (error);
1508503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1509503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1510503ad85cSMatthew Ahrens 		return (EINVAL);
1511503ad85cSMatthew Ahrens 	}
1512503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
151355434c77Sek 	    sizeof (zc->zc_value));
1514503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
151555434c77Sek 
151655434c77Sek 	return (error);
151755434c77Sek }
151855434c77Sek 
151999d5e173STim Haley /*
152099d5e173STim Haley  * inputs:
152199d5e173STim Haley  * zc_name		name of filesystem
152299d5e173STim Haley  * zc_obj		object to find
152399d5e173STim Haley  *
152499d5e173STim Haley  * outputs:
152599d5e173STim Haley  * zc_stat		stats on object
152699d5e173STim Haley  * zc_value		path to object
152799d5e173STim Haley  */
152899d5e173STim Haley static int
152999d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
153099d5e173STim Haley {
153199d5e173STim Haley 	objset_t *os;
153299d5e173STim Haley 	int error;
153399d5e173STim Haley 
153499d5e173STim Haley 	/* XXX reading from objset not owned */
153599d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
153699d5e173STim Haley 		return (error);
153799d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
153899d5e173STim Haley 		dmu_objset_rele(os, FTAG);
153999d5e173STim Haley 		return (EINVAL);
154099d5e173STim Haley 	}
154199d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
154299d5e173STim Haley 	    sizeof (zc->zc_value));
154399d5e173STim Haley 	dmu_objset_rele(os, FTAG);
154499d5e173STim Haley 
154599d5e173STim Haley 	return (error);
154699d5e173STim Haley }
154799d5e173STim Haley 
1548fa9e4066Sahrens static int
1549fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1550fa9e4066Sahrens {
1551fa9e4066Sahrens 	spa_t *spa;
1552fa9e4066Sahrens 	int error;
1553e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1554e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1557fa9e4066Sahrens 	if (error != 0)
1558fa9e4066Sahrens 		return (error);
1559fa9e4066Sahrens 
1560fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1561478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1562fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1563fa94a07fSbrendan 	    &l2cache, &nl2cache);
1564fa94a07fSbrendan 
1565e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1566e7cbe64fSgw 	    &spares, &nspares);
1567e7cbe64fSgw 
1568b1b8ab34Slling 	/*
1569b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1570e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1571e7cbe64fSgw 	 *
1572e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1573e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1574e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1575e7cbe64fSgw 	 *
1576e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1577b1b8ab34Slling 	 */
1578b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
15791195e687SMark J Musante 		nvlist_free(config);
1580b1b8ab34Slling 		spa_close(spa, FTAG);
1581b1b8ab34Slling 		return (EDOM);
1582b1b8ab34Slling 	}
1583b1b8ab34Slling 
1584fa94a07fSbrendan 	if (error == 0) {
1585fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1586fa9e4066Sahrens 		nvlist_free(config);
1587fa9e4066Sahrens 	}
1588fa9e4066Sahrens 	spa_close(spa, FTAG);
1589fa9e4066Sahrens 	return (error);
1590fa9e4066Sahrens }
1591fa9e4066Sahrens 
15923f9d6ad7SLin Ling /*
15933f9d6ad7SLin Ling  * inputs:
15943f9d6ad7SLin Ling  * zc_name		name of the pool
15953f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
15963f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
15973f9d6ad7SLin Ling  */
1598fa9e4066Sahrens static int
1599fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1600fa9e4066Sahrens {
160199653d4eSeschrock 	spa_t *spa;
160299653d4eSeschrock 	int error;
160399653d4eSeschrock 
160499653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
160599653d4eSeschrock 	if (error != 0)
160699653d4eSeschrock 		return (error);
160799653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
160899653d4eSeschrock 	spa_close(spa, FTAG);
160999653d4eSeschrock 	return (error);
1610fa9e4066Sahrens }
1611fa9e4066Sahrens 
1612fa9e4066Sahrens static int
16133d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1614fa9e4066Sahrens {
1615fa9e4066Sahrens 	spa_t *spa;
1616fa9e4066Sahrens 	int error;
16173d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1618fa9e4066Sahrens 
161906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1620fa9e4066Sahrens 		return (error);
16213d7072f8Seschrock 	switch (zc->zc_cookie) {
16223d7072f8Seschrock 	case VDEV_STATE_ONLINE:
16233d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
16243d7072f8Seschrock 		break;
1625fa9e4066Sahrens 
16263d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
16273d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
16283d7072f8Seschrock 		break;
1629fa9e4066Sahrens 
16303d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1631069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1632069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1633069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1634069f55e2SEric Schrock 
1635069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
16363d7072f8Seschrock 		break;
16373d7072f8Seschrock 
16383d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1639069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1640069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1641069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1642069f55e2SEric Schrock 
1643069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
16443d7072f8Seschrock 		break;
16453d7072f8Seschrock 
16463d7072f8Seschrock 	default:
16473d7072f8Seschrock 		error = EINVAL;
16483d7072f8Seschrock 	}
16493d7072f8Seschrock 	zc->zc_cookie = newstate;
1650fa9e4066Sahrens 	spa_close(spa, FTAG);
1651fa9e4066Sahrens 	return (error);
1652fa9e4066Sahrens }
1653fa9e4066Sahrens 
1654fa9e4066Sahrens static int
1655fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1656fa9e4066Sahrens {
1657fa9e4066Sahrens 	spa_t *spa;
1658fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1659fa9e4066Sahrens 	nvlist_t *config;
1660fa9e4066Sahrens 	int error;
1661fa9e4066Sahrens 
166206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1663fa9e4066Sahrens 		return (error);
1664fa9e4066Sahrens 
1665990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1666478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1667ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1668fa9e4066Sahrens 		nvlist_free(config);
1669fa9e4066Sahrens 	}
1670fa9e4066Sahrens 
1671fa9e4066Sahrens 	spa_close(spa, FTAG);
1672fa9e4066Sahrens 	return (error);
1673fa9e4066Sahrens }
1674fa9e4066Sahrens 
1675fa9e4066Sahrens static int
1676fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1677fa9e4066Sahrens {
1678fa9e4066Sahrens 	spa_t *spa;
1679fa9e4066Sahrens 	int error;
1680fa9e4066Sahrens 
168106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1682fa9e4066Sahrens 		return (error);
1683fa9e4066Sahrens 
16848ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1685fa9e4066Sahrens 
1686fa9e4066Sahrens 	spa_close(spa, FTAG);
1687fa9e4066Sahrens 	return (error);
1688fa9e4066Sahrens }
1689fa9e4066Sahrens 
16901195e687SMark J Musante static int
16911195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
16921195e687SMark J Musante {
16931195e687SMark J Musante 	spa_t *spa;
16941195e687SMark J Musante 	nvlist_t *config, *props = NULL;
16951195e687SMark J Musante 	int error;
16961195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
16971195e687SMark J Musante 
16981195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
16991195e687SMark J Musante 		return (error);
17001195e687SMark J Musante 
17011195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
17021195e687SMark J Musante 	    zc->zc_iflags, &config)) {
17031195e687SMark J Musante 		spa_close(spa, FTAG);
17041195e687SMark J Musante 		return (error);
17051195e687SMark J Musante 	}
17061195e687SMark J Musante 
17071195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
17081195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
17091195e687SMark J Musante 	    zc->zc_iflags, &props))) {
17101195e687SMark J Musante 		spa_close(spa, FTAG);
17111195e687SMark J Musante 		nvlist_free(config);
17121195e687SMark J Musante 		return (error);
17131195e687SMark J Musante 	}
17141195e687SMark J Musante 
17151195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
17161195e687SMark J Musante 
17171195e687SMark J Musante 	spa_close(spa, FTAG);
17181195e687SMark J Musante 
17191195e687SMark J Musante 	nvlist_free(config);
17201195e687SMark J Musante 	nvlist_free(props);
17211195e687SMark J Musante 
17221195e687SMark J Musante 	return (error);
17231195e687SMark J Musante }
17241195e687SMark J Musante 
1725c67d9675Seschrock static int
1726c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1727c67d9675Seschrock {
1728c67d9675Seschrock 	spa_t *spa;
1729e9dbad6fSeschrock 	char *path = zc->zc_value;
1730ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
1731c67d9675Seschrock 	int error;
1732c67d9675Seschrock 
1733c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
1734c67d9675Seschrock 	if (error != 0)
1735c67d9675Seschrock 		return (error);
1736c67d9675Seschrock 
1737c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
1738c67d9675Seschrock 	spa_close(spa, FTAG);
1739c67d9675Seschrock 	return (error);
1740c67d9675Seschrock }
1741c67d9675Seschrock 
17426809eb4eSEric Schrock static int
17436809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
17446809eb4eSEric Schrock {
17456809eb4eSEric Schrock 	spa_t *spa;
17466809eb4eSEric Schrock 	char *fru = zc->zc_value;
17476809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
17486809eb4eSEric Schrock 	int error;
17496809eb4eSEric Schrock 
17506809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
17516809eb4eSEric Schrock 	if (error != 0)
17526809eb4eSEric Schrock 		return (error);
17536809eb4eSEric Schrock 
17546809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
17556809eb4eSEric Schrock 	spa_close(spa, FTAG);
17566809eb4eSEric Schrock 	return (error);
17576809eb4eSEric Schrock }
17586809eb4eSEric Schrock 
1759fa9e4066Sahrens static int
1760a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1761fa9e4066Sahrens {
1762a7f53a56SChris Kirby 	int error = 0;
17637f7322feSeschrock 	nvlist_t *nv;
1764fa9e4066Sahrens 
1765a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1766fa9e4066Sahrens 
17675ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
176892241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1769a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
1770432f72fdSahrens 		/*
1771bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
1772432f72fdSahrens 		 * which we aren't supposed to do with a
1773745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
1774432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
1775503ad85cSMatthew Ahrens 		 * XXX reading with out owning
1776432f72fdSahrens 		 */
1777*19b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
1778*19b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
1779*19b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
1780*19b94df9SMatthew Ahrens 			if (error == EIO)
1781*19b94df9SMatthew Ahrens 				return (error);
1782*19b94df9SMatthew Ahrens 			VERIFY3S(error, ==, 0);
1783e7437265Sahrens 		}
1784e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
17857f7322feSeschrock 		nvlist_free(nv);
17867f7322feSeschrock 	}
1787fa9e4066Sahrens 
1788a7f53a56SChris Kirby 	return (error);
1789a7f53a56SChris Kirby }
1790a7f53a56SChris Kirby 
1791a7f53a56SChris Kirby /*
1792a7f53a56SChris Kirby  * inputs:
1793a7f53a56SChris Kirby  * zc_name		name of filesystem
1794a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
1795a7f53a56SChris Kirby  *
1796a7f53a56SChris Kirby  * outputs:
1797a7f53a56SChris Kirby  * zc_objset_stats	stats
1798a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
1799a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
1800a7f53a56SChris Kirby  */
1801a7f53a56SChris Kirby static int
1802a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
1803a7f53a56SChris Kirby {
1804a7f53a56SChris Kirby 	objset_t *os = NULL;
1805a7f53a56SChris Kirby 	int error;
1806a7f53a56SChris Kirby 
1807a7f53a56SChris Kirby 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1808a7f53a56SChris Kirby 		return (error);
1809a7f53a56SChris Kirby 
1810a7f53a56SChris Kirby 	error = zfs_ioc_objset_stats_impl(zc, os);
1811a7f53a56SChris Kirby 
1812503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1813a7f53a56SChris Kirby 
1814fa9e4066Sahrens 	return (error);
1815fa9e4066Sahrens }
1816fa9e4066Sahrens 
181792241e0bSTom Erickson /*
181892241e0bSTom Erickson  * inputs:
181992241e0bSTom Erickson  * zc_name		name of filesystem
182092241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
182192241e0bSTom Erickson  *
182292241e0bSTom Erickson  * outputs:
182392241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
182492241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
182592241e0bSTom Erickson  *
182692241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
182792241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
182892241e0bSTom Erickson  * local property values.
182992241e0bSTom Erickson  */
183092241e0bSTom Erickson static int
183192241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
183292241e0bSTom Erickson {
183392241e0bSTom Erickson 	objset_t *os = NULL;
183492241e0bSTom Erickson 	int error;
183592241e0bSTom Erickson 	nvlist_t *nv;
183692241e0bSTom Erickson 
183792241e0bSTom Erickson 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
183892241e0bSTom Erickson 		return (error);
183992241e0bSTom Erickson 
184092241e0bSTom Erickson 	/*
184192241e0bSTom Erickson 	 * Without this check, we would return local property values if the
184292241e0bSTom Erickson 	 * caller has not already received properties on or after
184392241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
184492241e0bSTom Erickson 	 */
184592241e0bSTom Erickson 	if (!dsl_prop_get_hasrecvd(os)) {
184692241e0bSTom Erickson 		dmu_objset_rele(os, FTAG);
184792241e0bSTom Erickson 		return (ENOTSUP);
184892241e0bSTom Erickson 	}
184992241e0bSTom Erickson 
185092241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
185192241e0bSTom Erickson 	    (error = dsl_prop_get_received(os, &nv)) == 0) {
185292241e0bSTom Erickson 		error = put_nvlist(zc, nv);
185392241e0bSTom Erickson 		nvlist_free(nv);
185492241e0bSTom Erickson 	}
185592241e0bSTom Erickson 
185692241e0bSTom Erickson 	dmu_objset_rele(os, FTAG);
185792241e0bSTom Erickson 	return (error);
185892241e0bSTom Erickson }
185992241e0bSTom Erickson 
1860de8267e0Stimh static int
1861de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1862de8267e0Stimh {
1863de8267e0Stimh 	uint64_t value;
1864de8267e0Stimh 	int error;
1865de8267e0Stimh 
1866de8267e0Stimh 	/*
1867de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
1868de8267e0Stimh 	 * the default value (if there is one).
1869de8267e0Stimh 	 */
1870de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1871de8267e0Stimh 		return (error);
1872de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1873de8267e0Stimh 	return (0);
1874de8267e0Stimh }
1875de8267e0Stimh 
18763cb34c60Sahrens /*
18773cb34c60Sahrens  * inputs:
18783cb34c60Sahrens  * zc_name		name of filesystem
1879de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
18803cb34c60Sahrens  *
18813cb34c60Sahrens  * outputs:
1882de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
1883de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
18843cb34c60Sahrens  */
1885bd00f61bSrm static int
1886de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1887bd00f61bSrm {
1888de8267e0Stimh 	objset_t *os;
1889de8267e0Stimh 	int err;
1890bd00f61bSrm 
1891503ad85cSMatthew Ahrens 	/* XXX reading without owning */
1892503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1893de8267e0Stimh 		return (err);
1894bd00f61bSrm 
1895bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1896bd00f61bSrm 
1897bd00f61bSrm 	/*
1898de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
1899745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
1900745cd3c5Smaybee 	 * hold, because it could be inconsistent.
1901bd00f61bSrm 	 */
1902de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
1903de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
1904de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
1905de8267e0Stimh 		nvlist_t *nv;
1906de8267e0Stimh 
1907de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1908de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1909de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1910de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1911de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1912de8267e0Stimh 			err = put_nvlist(zc, nv);
1913de8267e0Stimh 		nvlist_free(nv);
1914de8267e0Stimh 	} else {
1915de8267e0Stimh 		err = ENOENT;
1916de8267e0Stimh 	}
1917503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1918de8267e0Stimh 	return (err);
1919bd00f61bSrm }
1920bd00f61bSrm 
192114843421SMatthew Ahrens static boolean_t
192214843421SMatthew Ahrens dataset_name_hidden(const char *name)
192314843421SMatthew Ahrens {
192414843421SMatthew Ahrens 	/*
192514843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
192614843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
192714843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
192814843421SMatthew Ahrens 	 */
192914843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
193014843421SMatthew Ahrens 		return (B_TRUE);
193114843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
193214843421SMatthew Ahrens 		return (B_TRUE);
193314843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
193414843421SMatthew Ahrens 		return (B_TRUE);
193514843421SMatthew Ahrens 	return (B_FALSE);
193614843421SMatthew Ahrens }
193714843421SMatthew Ahrens 
1938de8267e0Stimh /*
1939de8267e0Stimh  * inputs:
1940de8267e0Stimh  * zc_name		name of filesystem
1941de8267e0Stimh  * zc_cookie		zap cursor
1942de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
1943de8267e0Stimh  *
1944de8267e0Stimh  * outputs:
1945de8267e0Stimh  * zc_name		name of next filesystem
194614843421SMatthew Ahrens  * zc_cookie		zap cursor
1947de8267e0Stimh  * zc_objset_stats	stats
1948de8267e0Stimh  * zc_nvlist_dst	property nvlist
1949de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
1950de8267e0Stimh  */
1951fa9e4066Sahrens static int
1952fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1953fa9e4066Sahrens {
195487e5029aSahrens 	objset_t *os;
1955fa9e4066Sahrens 	int error;
1956fa9e4066Sahrens 	char *p;
1957620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
1958fa9e4066Sahrens 
1959620252bcSChris Kirby top:
1960503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
196187e5029aSahrens 		if (error == ENOENT)
196287e5029aSahrens 			error = ESRCH;
196387e5029aSahrens 		return (error);
1964fa9e4066Sahrens 	}
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
1967fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
1968fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1969fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
1970fa9e4066Sahrens 
19715c0b6a79SRich Morris 	/*
19725c0b6a79SRich Morris 	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
19735c0b6a79SRich Morris 	 * but is not declared void because its called by dmu_objset_find().
19745c0b6a79SRich Morris 	 */
19757f73c863SRich Morris 	if (zc->zc_cookie == 0) {
19767f73c863SRich Morris 		uint64_t cookie = 0;
19777f73c863SRich Morris 		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
19787f73c863SRich Morris 
19791df56adaSMartin Matuska 		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
19801df56adaSMartin Matuska 			if (!dataset_name_hidden(zc->zc_name))
19811df56adaSMartin Matuska 				(void) dmu_objset_prefetch(zc->zc_name, NULL);
19821df56adaSMartin Matuska 		}
19837f73c863SRich Morris 	}
19847f73c863SRich Morris 
1985fa9e4066Sahrens 	do {
198687e5029aSahrens 		error = dmu_dir_list_next(os,
198787e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
198887e5029aSahrens 		    NULL, &zc->zc_cookie);
1989fa9e4066Sahrens 		if (error == ENOENT)
1990fa9e4066Sahrens 			error = ESRCH;
1991*19b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
1992503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1993fa9e4066Sahrens 
1994681d9761SEric Taylor 	/*
1995681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
1996681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
1997681d9761SEric Taylor 	 */
1998620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
199987e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2000620252bcSChris Kirby 		if (error == ENOENT) {
2001620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2002620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2003620252bcSChris Kirby 			goto top;
2004620252bcSChris Kirby 		}
2005620252bcSChris Kirby 	}
2006fa9e4066Sahrens 	return (error);
2007fa9e4066Sahrens }
2008fa9e4066Sahrens 
20093cb34c60Sahrens /*
20103cb34c60Sahrens  * inputs:
20113cb34c60Sahrens  * zc_name		name of filesystem
20123cb34c60Sahrens  * zc_cookie		zap cursor
20133cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
20143cb34c60Sahrens  *
20153cb34c60Sahrens  * outputs:
20163cb34c60Sahrens  * zc_name		name of next snapshot
20173cb34c60Sahrens  * zc_objset_stats	stats
20183cb34c60Sahrens  * zc_nvlist_dst	property nvlist
20193cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
20203cb34c60Sahrens  */
2021fa9e4066Sahrens static int
2022fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2023fa9e4066Sahrens {
202487e5029aSahrens 	objset_t *os;
2025fa9e4066Sahrens 	int error;
2026fa9e4066Sahrens 
2027620252bcSChris Kirby top:
20287cbf8b43SRich Morris 	if (zc->zc_cookie == 0)
20297cbf8b43SRich Morris 		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
20307cbf8b43SRich Morris 		    NULL, DS_FIND_SNAPSHOTS);
20317cbf8b43SRich Morris 
2032503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2033745cd3c5Smaybee 	if (error)
2034745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
2035fa9e4066Sahrens 
2036b81d61a6Slling 	/*
2037b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2038b81d61a6Slling 	 * so exit immediately.
2039b81d61a6Slling 	 */
2040b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2041503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2042b81d61a6Slling 		return (ESRCH);
2043fa9e4066Sahrens 	}
2044fa9e4066Sahrens 
204587e5029aSahrens 	error = dmu_snapshot_list_next(os,
204687e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2047a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2048a7f53a56SChris Kirby 	    NULL);
2049a7f53a56SChris Kirby 
2050620252bcSChris Kirby 	if (error == 0) {
2051a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2052a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2053a7f53a56SChris Kirby 
2054a7f53a56SChris Kirby 		/*
2055a7f53a56SChris Kirby 		 * Since we probably don't have a hold on this snapshot,
2056a7f53a56SChris Kirby 		 * it's possible that the objsetid could have been destroyed
2057a7f53a56SChris Kirby 		 * and reused for a new objset. It's OK if this happens during
2058a7f53a56SChris Kirby 		 * a zfs send operation, since the new createtxg will be
2059a7f53a56SChris Kirby 		 * beyond the range we're interested in.
2060a7f53a56SChris Kirby 		 */
2061a7f53a56SChris Kirby 		rw_enter(&dp->dp_config_rwlock, RW_READER);
2062a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2063a7f53a56SChris Kirby 		rw_exit(&dp->dp_config_rwlock);
2064a7f53a56SChris Kirby 		if (error) {
2065a7f53a56SChris Kirby 			if (error == ENOENT) {
2066a7f53a56SChris Kirby 				/* Racing with destroy, get the next one. */
2067a7f53a56SChris Kirby 				*strchr(zc->zc_name, '@') = '\0';
2068a7f53a56SChris Kirby 				dmu_objset_rele(os, FTAG);
2069a7f53a56SChris Kirby 				goto top;
2070a7f53a56SChris Kirby 			}
2071a7f53a56SChris Kirby 		} else {
2072a7f53a56SChris Kirby 			objset_t *ossnap;
2073a7f53a56SChris Kirby 
2074a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2075a7f53a56SChris Kirby 			if (error == 0)
2076a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2077a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2078620252bcSChris Kirby 		}
2079620252bcSChris Kirby 	} else if (error == ENOENT) {
2080745cd3c5Smaybee 		error = ESRCH;
2081620252bcSChris Kirby 	}
2082fa9e4066Sahrens 
2083a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
20843cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
2085745cd3c5Smaybee 	if (error)
20863cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2087fa9e4066Sahrens 	return (error);
2088fa9e4066Sahrens }
2089fa9e4066Sahrens 
209092241e0bSTom Erickson static int
209192241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2092fa9e4066Sahrens {
209392241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
209492241e0bSTom Erickson 	uint64_t *valary;
209592241e0bSTom Erickson 	unsigned int vallen;
209692241e0bSTom Erickson 	const char *domain;
2097eeb85002STim Haley 	char *dash;
209892241e0bSTom Erickson 	zfs_userquota_prop_t type;
209992241e0bSTom Erickson 	uint64_t rid;
210092241e0bSTom Erickson 	uint64_t quota;
210192241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
210292241e0bSTom Erickson 	int err;
210392241e0bSTom Erickson 
210492241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
210592241e0bSTom Erickson 		nvlist_t *attrs;
210692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2107eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2108eeb85002STim Haley 		    &pair) != 0)
2109eeb85002STim Haley 			return (EINVAL);
211092241e0bSTom Erickson 	}
2111e9dbad6fSeschrock 
2112ecd6cf80Smarks 	/*
2113eeb85002STim Haley 	 * A correctly constructed propname is encoded as
211492241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2115ecd6cf80Smarks 	 */
2116eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2117eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2118eeb85002STim Haley 	    vallen != 3)
2119eeb85002STim Haley 		return (EINVAL);
2120eeb85002STim Haley 
2121eeb85002STim Haley 	domain = dash + 1;
2122eeb85002STim Haley 	type = valary[0];
2123eeb85002STim Haley 	rid = valary[1];
2124eeb85002STim Haley 	quota = valary[2];
2125e9dbad6fSeschrock 
21261412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
212792241e0bSTom Erickson 	if (err == 0) {
212892241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
212992241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
213092241e0bSTom Erickson 	}
2131e9dbad6fSeschrock 
213292241e0bSTom Erickson 	return (err);
213392241e0bSTom Erickson }
213414843421SMatthew Ahrens 
213592241e0bSTom Erickson /*
213692241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
213792241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
213892241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
213992241e0bSTom Erickson  *
2140eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
214192241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
214292241e0bSTom Erickson  */
214392241e0bSTom Erickson static int
214492241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
214592241e0bSTom Erickson     nvpair_t *pair)
214692241e0bSTom Erickson {
214792241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
214892241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
214992241e0bSTom Erickson 	uint64_t intval;
215092241e0bSTom Erickson 	int err;
2151fa9e4066Sahrens 
215292241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
215392241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
215492241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
215592241e0bSTom Erickson 		return (-1);
215692241e0bSTom Erickson 	}
215714843421SMatthew Ahrens 
215892241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
215992241e0bSTom Erickson 		nvlist_t *attrs;
216092241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
216192241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
216292241e0bSTom Erickson 		    &pair) == 0);
216392241e0bSTom Erickson 	}
2164db870a07Sahrens 
216592241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
216692241e0bSTom Erickson 		return (-1);
2167b24ab676SJeff Bonwick 
216892241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
216940feaa91Sahrens 
217092241e0bSTom Erickson 	switch (prop) {
217192241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
217292241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
217392241e0bSTom Erickson 		break;
217492241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
217592241e0bSTom Erickson 		err = dsl_dataset_set_quota(dsname, source, intval);
217692241e0bSTom Erickson 		break;
217792241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
217892241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
217992241e0bSTom Erickson 		break;
218092241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
218192241e0bSTom Erickson 		err = dsl_dataset_set_reservation(dsname, source, intval);
218292241e0bSTom Erickson 		break;
218392241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
218492241e0bSTom Erickson 		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
218592241e0bSTom Erickson 		    intval);
218692241e0bSTom Erickson 		break;
218792241e0bSTom Erickson 	case ZFS_PROP_VERSION:
218892241e0bSTom Erickson 	{
218992241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
21909e6eda55Smarks 
21911412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2192b24ab676SJeff Bonwick 			break;
2193b24ab676SJeff Bonwick 
219492241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
219592241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2196d0f3f37eSMark Shellenbaum 
219792241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2198b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2199b16da2e2SGeorge Wilson 
2200b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2201b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2202b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2203b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
220440feaa91Sahrens 		}
220592241e0bSTom Erickson 		break;
2206ecd6cf80Smarks 	}
2207ecd6cf80Smarks 
220892241e0bSTom Erickson 	default:
220992241e0bSTom Erickson 		err = -1;
221092241e0bSTom Erickson 	}
2211e9dbad6fSeschrock 
221292241e0bSTom Erickson 	return (err);
221392241e0bSTom Erickson }
2214a9799022Sck 
221592241e0bSTom Erickson /*
221692241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
221792241e0bSTom Erickson  * it continues to set as many as it can and returns the first error
221892241e0bSTom Erickson  * encountered. If the caller provides a non-NULL errlist, it also gives the
221992241e0bSTom Erickson  * complete list of names of all the properties it failed to set along with the
222092241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
222192241e0bSTom Erickson  * returned errlist.
222292241e0bSTom Erickson  *
222392241e0bSTom Erickson  * If every property is set successfully, zero is returned and the list pointed
222492241e0bSTom Erickson  * at by errlist is NULL.
222592241e0bSTom Erickson  */
222692241e0bSTom Erickson int
222792241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
222892241e0bSTom Erickson     nvlist_t **errlist)
222992241e0bSTom Erickson {
223092241e0bSTom Erickson 	nvpair_t *pair;
223192241e0bSTom Erickson 	nvpair_t *propval;
223202e383d1STom Erickson 	int rv = 0;
223392241e0bSTom Erickson 	uint64_t intval;
223492241e0bSTom Erickson 	char *strval;
223592241e0bSTom Erickson 	nvlist_t *genericnvl;
223692241e0bSTom Erickson 	nvlist_t *errors;
223792241e0bSTom Erickson 	nvlist_t *retrynvl;
2238e9dbad6fSeschrock 
223992241e0bSTom Erickson 	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
224092241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
224192241e0bSTom Erickson 	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2242a9799022Sck 
224392241e0bSTom Erickson retry:
224492241e0bSTom Erickson 	pair = NULL;
224592241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
224692241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
224792241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2248cfa69fd2STom Erickson 		int err = 0;
2249e9dbad6fSeschrock 
225092241e0bSTom Erickson 		/* decode the property value */
225192241e0bSTom Erickson 		propval = pair;
225292241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
225392241e0bSTom Erickson 			nvlist_t *attrs;
225492241e0bSTom Erickson 			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2255eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2256eeb85002STim Haley 			    &propval) != 0)
2257eeb85002STim Haley 				err = EINVAL;
225814843421SMatthew Ahrens 		}
2259e9dbad6fSeschrock 
226092241e0bSTom Erickson 		/* Validate value type */
2261eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
226292241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
226392241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
226492241e0bSTom Erickson 					err = EINVAL;
226592241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
226692241e0bSTom Erickson 				if (nvpair_type(propval) !=
226792241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
226892241e0bSTom Erickson 					err = EINVAL;
2269*19b94df9SMatthew Ahrens 			} else {
2270*19b94df9SMatthew Ahrens 				err = EINVAL;
22714201a95eSRic Aleshire 			}
2272eeb85002STim Haley 		} else if (err == 0) {
227392241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
227492241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
227592241e0bSTom Erickson 					err = EINVAL;
227692241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2277a2eea2e1Sahrens 				const char *unused;
2278a2eea2e1Sahrens 
227992241e0bSTom Erickson 				VERIFY(nvpair_value_uint64(propval,
228092241e0bSTom Erickson 				    &intval) == 0);
2281e9dbad6fSeschrock 
2282e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
228391ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2284e9dbad6fSeschrock 					break;
228591ebeef5Sahrens 				case PROP_TYPE_STRING:
228692241e0bSTom Erickson 					err = EINVAL;
228792241e0bSTom Erickson 					break;
228891ebeef5Sahrens 				case PROP_TYPE_INDEX:
2289acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
229092241e0bSTom Erickson 					    intval, &unused) != 0)
229192241e0bSTom Erickson 						err = EINVAL;
2292e9dbad6fSeschrock 					break;
2293e9dbad6fSeschrock 				default:
2294e7437265Sahrens 					cmn_err(CE_PANIC,
2295e7437265Sahrens 					    "unknown property type");
2296e9dbad6fSeschrock 				}
2297e9dbad6fSeschrock 			} else {
229892241e0bSTom Erickson 				err = EINVAL;
2299e9dbad6fSeschrock 			}
2300e9dbad6fSeschrock 		}
230192241e0bSTom Erickson 
230292241e0bSTom Erickson 		/* Validate permissions */
230392241e0bSTom Erickson 		if (err == 0)
230492241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
230592241e0bSTom Erickson 
230692241e0bSTom Erickson 		if (err == 0) {
230792241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
230892241e0bSTom Erickson 			if (err == -1) {
230992241e0bSTom Erickson 				/*
231092241e0bSTom Erickson 				 * For better performance we build up a list of
231192241e0bSTom Erickson 				 * properties to set in a single transaction.
231292241e0bSTom Erickson 				 */
231392241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
231492241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
231592241e0bSTom Erickson 				/*
231692241e0bSTom Erickson 				 * This may be a spurious error caused by
231792241e0bSTom Erickson 				 * receiving quota and reservation out of order.
231892241e0bSTom Erickson 				 * Try again in a second pass.
231992241e0bSTom Erickson 				 */
232092241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
232192241e0bSTom Erickson 			}
232292241e0bSTom Erickson 		}
232392241e0bSTom Erickson 
232492241e0bSTom Erickson 		if (err != 0)
232592241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2326e9dbad6fSeschrock 	}
2327e9dbad6fSeschrock 
232892241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
232992241e0bSTom Erickson 		nvl = retrynvl;
233092241e0bSTom Erickson 		goto retry;
233192241e0bSTom Erickson 	}
233292241e0bSTom Erickson 
233392241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
233492241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
233592241e0bSTom Erickson 		/*
233692241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
233792241e0bSTom Erickson 		 * can, so try setting them individually.
233892241e0bSTom Erickson 		 */
233992241e0bSTom Erickson 		pair = NULL;
234092241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
234192241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2342cfa69fd2STom Erickson 			int err = 0;
234392241e0bSTom Erickson 
234492241e0bSTom Erickson 			propval = pair;
234592241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
234692241e0bSTom Erickson 				nvlist_t *attrs;
234792241e0bSTom Erickson 				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
234892241e0bSTom Erickson 				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
234992241e0bSTom Erickson 				    &propval) == 0);
235092241e0bSTom Erickson 			}
235192241e0bSTom Erickson 
235292241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
235392241e0bSTom Erickson 				VERIFY(nvpair_value_string(propval,
235492241e0bSTom Erickson 				    &strval) == 0);
235592241e0bSTom Erickson 				err = dsl_prop_set(dsname, propname, source, 1,
235692241e0bSTom Erickson 				    strlen(strval) + 1, strval);
235792241e0bSTom Erickson 			} else {
235892241e0bSTom Erickson 				VERIFY(nvpair_value_uint64(propval,
235992241e0bSTom Erickson 				    &intval) == 0);
236092241e0bSTom Erickson 				err = dsl_prop_set(dsname, propname, source, 8,
236192241e0bSTom Erickson 				    1, &intval);
236292241e0bSTom Erickson 			}
236392241e0bSTom Erickson 
236492241e0bSTom Erickson 			if (err != 0) {
236592241e0bSTom Erickson 				VERIFY(nvlist_add_int32(errors, propname,
236692241e0bSTom Erickson 				    err) == 0);
236792241e0bSTom Erickson 			}
236892241e0bSTom Erickson 		}
23695c0b6a79SRich Morris 	}
23705c0b6a79SRich Morris 	nvlist_free(genericnvl);
237192241e0bSTom Erickson 	nvlist_free(retrynvl);
237292241e0bSTom Erickson 
237392241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
237492241e0bSTom Erickson 		nvlist_free(errors);
237592241e0bSTom Erickson 		errors = NULL;
237692241e0bSTom Erickson 	} else {
237792241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
237892241e0bSTom Erickson 	}
237992241e0bSTom Erickson 
238092241e0bSTom Erickson 	if (errlist == NULL)
238192241e0bSTom Erickson 		nvlist_free(errors);
238292241e0bSTom Erickson 	else
238392241e0bSTom Erickson 		*errlist = errors;
238492241e0bSTom Erickson 
238592241e0bSTom Erickson 	return (rv);
2386fa9e4066Sahrens }
2387fa9e4066Sahrens 
2388ea2f5b9eSMatthew Ahrens /*
2389ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2390ea2f5b9eSMatthew Ahrens  */
2391ea2f5b9eSMatthew Ahrens static int
2392ea2f5b9eSMatthew Ahrens zfs_check_userprops(char *fsname, nvlist_t *nvl)
2393ea2f5b9eSMatthew Ahrens {
239492241e0bSTom Erickson 	nvpair_t *pair = NULL;
2395ea2f5b9eSMatthew Ahrens 	int error = 0;
2396ea2f5b9eSMatthew Ahrens 
239792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
239892241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2399ea2f5b9eSMatthew Ahrens 		char *valstr;
2400ea2f5b9eSMatthew Ahrens 
2401ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
240292241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2403ea2f5b9eSMatthew Ahrens 			return (EINVAL);
2404ea2f5b9eSMatthew Ahrens 
2405ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2406ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2407ea2f5b9eSMatthew Ahrens 			return (error);
2408ea2f5b9eSMatthew Ahrens 
2409ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2410ea2f5b9eSMatthew Ahrens 			return (ENAMETOOLONG);
2411ea2f5b9eSMatthew Ahrens 
241292241e0bSTom Erickson 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
2413ea2f5b9eSMatthew Ahrens 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
2414ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2415ea2f5b9eSMatthew Ahrens 	}
2416ea2f5b9eSMatthew Ahrens 	return (0);
2417ea2f5b9eSMatthew Ahrens }
2418ea2f5b9eSMatthew Ahrens 
241992241e0bSTom Erickson static void
242092241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
242192241e0bSTom Erickson {
242292241e0bSTom Erickson 	nvpair_t *pair;
242392241e0bSTom Erickson 
242492241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
242592241e0bSTom Erickson 
242692241e0bSTom Erickson 	pair = NULL;
242792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
242892241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
242992241e0bSTom Erickson 			continue;
243092241e0bSTom Erickson 
243192241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
243292241e0bSTom Erickson 	}
243392241e0bSTom Erickson }
243492241e0bSTom Erickson 
243592241e0bSTom Erickson static int
243692241e0bSTom Erickson clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
243792241e0bSTom Erickson     nvlist_t *skipped)
243892241e0bSTom Erickson {
243992241e0bSTom Erickson 	int err = 0;
244092241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
244192241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
244292241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
244392241e0bSTom Erickson 		/*
244492241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
244592241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
244692241e0bSTom Erickson 		 */
244792241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
244892241e0bSTom Erickson 		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
244992241e0bSTom Erickson 		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
245092241e0bSTom Erickson 	}
245192241e0bSTom Erickson 	nvlist_free(cleared_props);
245292241e0bSTom Erickson 	return (err);
245392241e0bSTom Erickson }
245492241e0bSTom Erickson 
24553cb34c60Sahrens /*
24563cb34c60Sahrens  * inputs:
24573cb34c60Sahrens  * zc_name		name of filesystem
24585c0b6a79SRich Morris  * zc_value		name of property to set
24593cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
246092241e0bSTom Erickson  * zc_cookie		received properties flag
24613cb34c60Sahrens  *
246292241e0bSTom Erickson  * outputs:
246392241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
24643cb34c60Sahrens  */
2465fa9e4066Sahrens static int
2466e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2467fa9e4066Sahrens {
2468e9dbad6fSeschrock 	nvlist_t *nvl;
246992241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
247092241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
247192241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
247292241e0bSTom Erickson 	nvlist_t *errors = NULL;
2473e9dbad6fSeschrock 	int error;
2474e9dbad6fSeschrock 
2475990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2476478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2477e9dbad6fSeschrock 		return (error);
2478e9dbad6fSeschrock 
247992241e0bSTom Erickson 	if (received) {
2480bb0ade09Sahrens 		nvlist_t *origprops;
2481bb0ade09Sahrens 		objset_t *os;
2482bb0ade09Sahrens 
2483503ad85cSMatthew Ahrens 		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
248492241e0bSTom Erickson 			if (dsl_prop_get_received(os, &origprops) == 0) {
248592241e0bSTom Erickson 				(void) clear_received_props(os,
248692241e0bSTom Erickson 				    zc->zc_name, origprops, nvl);
2487bb0ade09Sahrens 				nvlist_free(origprops);
2488bb0ade09Sahrens 			}
248992241e0bSTom Erickson 
249092241e0bSTom Erickson 			dsl_prop_set_hasrecvd(os);
2491503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
2492bb0ade09Sahrens 		}
2493bb0ade09Sahrens 	}
2494bb0ade09Sahrens 
249592241e0bSTom Erickson 	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
249692241e0bSTom Erickson 
249792241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
249892241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
249992241e0bSTom Erickson 	}
2500ecd6cf80Smarks 
250192241e0bSTom Erickson 	nvlist_free(errors);
2502e9dbad6fSeschrock 	nvlist_free(nvl);
2503e9dbad6fSeschrock 	return (error);
2504fa9e4066Sahrens }
2505fa9e4066Sahrens 
25063cb34c60Sahrens /*
25073cb34c60Sahrens  * inputs:
25083cb34c60Sahrens  * zc_name		name of filesystem
25093cb34c60Sahrens  * zc_value		name of property to inherit
251092241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
25113cb34c60Sahrens  *
25123cb34c60Sahrens  * outputs:		none
25133cb34c60Sahrens  */
2514e45ce728Sahrens static int
2515e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2516e45ce728Sahrens {
251792241e0bSTom Erickson 	const char *propname = zc->zc_value;
251892241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
251992241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
252092241e0bSTom Erickson 	zprop_source_t source = (received
252192241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
252292241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
252392241e0bSTom Erickson 
252492241e0bSTom Erickson 	if (received) {
252592241e0bSTom Erickson 		nvlist_t *dummy;
252692241e0bSTom Erickson 		nvpair_t *pair;
252792241e0bSTom Erickson 		zprop_type_t type;
252892241e0bSTom Erickson 		int err;
252992241e0bSTom Erickson 
253092241e0bSTom Erickson 		/*
253192241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
253292241e0bSTom Erickson 		 * nvpair with type info.
253392241e0bSTom Erickson 		 */
253492241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
253592241e0bSTom Erickson 			if (!zfs_prop_user(propname))
253692241e0bSTom Erickson 				return (EINVAL);
253792241e0bSTom Erickson 
253892241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2539a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2540a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2541a79992aaSTom Erickson 			return (EINVAL);
254292241e0bSTom Erickson 		} else {
254392241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
254492241e0bSTom Erickson 		}
254592241e0bSTom Erickson 
254692241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
254792241e0bSTom Erickson 
254892241e0bSTom Erickson 		switch (type) {
254992241e0bSTom Erickson 		case PROP_TYPE_STRING:
255092241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
255192241e0bSTom Erickson 			break;
255292241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
255392241e0bSTom Erickson 		case PROP_TYPE_INDEX:
255492241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
255592241e0bSTom Erickson 			break;
255692241e0bSTom Erickson 		default:
255792241e0bSTom Erickson 			nvlist_free(dummy);
255892241e0bSTom Erickson 			return (EINVAL);
255992241e0bSTom Erickson 		}
256092241e0bSTom Erickson 
256192241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
256292241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
256392241e0bSTom Erickson 		nvlist_free(dummy);
256492241e0bSTom Erickson 		if (err != -1)
256592241e0bSTom Erickson 			return (err); /* special property already handled */
256692241e0bSTom Erickson 	} else {
256792241e0bSTom Erickson 		/*
256892241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
256992241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
257092241e0bSTom Erickson 		 * and reservation to the received or default values even though
257192241e0bSTom Erickson 		 * they are not considered inheritable.
257292241e0bSTom Erickson 		 */
257392241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
257492241e0bSTom Erickson 			return (EINVAL);
257592241e0bSTom Erickson 	}
257692241e0bSTom Erickson 
2577e45ce728Sahrens 	/* the property name has been validated by zfs_secpolicy_inherit() */
257892241e0bSTom Erickson 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2579e45ce728Sahrens }
2580e45ce728Sahrens 
2581b1b8ab34Slling static int
258211a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2583b1b8ab34Slling {
2584990b4856Slling 	nvlist_t *props;
2585b1b8ab34Slling 	spa_t *spa;
2586990b4856Slling 	int error;
258792241e0bSTom Erickson 	nvpair_t *pair;
2588b1b8ab34Slling 
258992241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
259092241e0bSTom Erickson 	    zc->zc_iflags, &props))
2591b1b8ab34Slling 		return (error);
2592b1b8ab34Slling 
2593379c004dSEric Schrock 	/*
2594379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2595379c004dSEric Schrock 	 * to handle the faulted case.
2596379c004dSEric Schrock 	 */
259792241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
259892241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2599379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
260092241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2601379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2602379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2603379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2604379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2605379c004dSEric Schrock 		}
2606379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2607b693757aSEric Schrock 		if (spa != NULL) {
2608b693757aSEric Schrock 			nvlist_free(props);
2609379c004dSEric Schrock 			return (0);
2610b693757aSEric Schrock 		}
2611379c004dSEric Schrock 	}
2612379c004dSEric Schrock 
2613b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2614990b4856Slling 		nvlist_free(props);
2615b1b8ab34Slling 		return (error);
2616b1b8ab34Slling 	}
2617b1b8ab34Slling 
2618990b4856Slling 	error = spa_prop_set(spa, props);
2619b1b8ab34Slling 
2620990b4856Slling 	nvlist_free(props);
2621b1b8ab34Slling 	spa_close(spa, FTAG);
2622b1b8ab34Slling 
2623b1b8ab34Slling 	return (error);
2624b1b8ab34Slling }
2625b1b8ab34Slling 
2626b1b8ab34Slling static int
262711a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2628b1b8ab34Slling {
2629b1b8ab34Slling 	spa_t *spa;
2630b1b8ab34Slling 	int error;
2631b1b8ab34Slling 	nvlist_t *nvp = NULL;
2632b1b8ab34Slling 
2633379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2634379c004dSEric Schrock 		/*
2635379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2636379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2637379c004dSEric Schrock 		 * anyway.
2638379c004dSEric Schrock 		 */
2639379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2640379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2641379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2642379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2643379c004dSEric Schrock 	} else {
2644379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2645379c004dSEric Schrock 		spa_close(spa, FTAG);
2646379c004dSEric Schrock 	}
2647b1b8ab34Slling 
2648b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2649b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2650b1b8ab34Slling 	else
2651b1b8ab34Slling 		error = EFAULT;
2652b1b8ab34Slling 
2653379c004dSEric Schrock 	nvlist_free(nvp);
2654b1b8ab34Slling 	return (error);
2655b1b8ab34Slling }
2656b1b8ab34Slling 
26573cb34c60Sahrens /*
26583cb34c60Sahrens  * inputs:
26593cb34c60Sahrens  * zc_name		name of filesystem
26603cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
26613cb34c60Sahrens  * zc_perm_action	allow/unallow flag
26623cb34c60Sahrens  *
26633cb34c60Sahrens  * outputs:		none
26643cb34c60Sahrens  */
2665ecd6cf80Smarks static int
2666ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2667ecd6cf80Smarks {
2668ecd6cf80Smarks 	int error;
2669ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2670ecd6cf80Smarks 
2671990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2672478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2673ecd6cf80Smarks 		return (error);
2674ecd6cf80Smarks 
2675ecd6cf80Smarks 	/*
2676ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2677ecd6cf80Smarks 	 */
2678ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2679ecd6cf80Smarks 		nvlist_free(fsaclnv);
2680ecd6cf80Smarks 		return (EINVAL);
2681ecd6cf80Smarks 	}
2682ecd6cf80Smarks 
2683ecd6cf80Smarks 	/*
2684ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2685ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2686ecd6cf80Smarks 	 * the nvlist(s)
2687ecd6cf80Smarks 	 */
2688ecd6cf80Smarks 
268991ebeef5Sahrens 	error = secpolicy_zfs(CRED());
2690ecd6cf80Smarks 	if (error) {
269191ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
269291ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
269391ebeef5Sahrens 			    fsaclnv, CRED());
269491ebeef5Sahrens 		} else {
269591ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
269691ebeef5Sahrens 			    fsaclnv, CRED());
269791ebeef5Sahrens 		}
2698ecd6cf80Smarks 	}
2699ecd6cf80Smarks 
2700ecd6cf80Smarks 	if (error == 0)
2701ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2702ecd6cf80Smarks 
2703ecd6cf80Smarks 	nvlist_free(fsaclnv);
2704ecd6cf80Smarks 	return (error);
2705ecd6cf80Smarks }
2706ecd6cf80Smarks 
27073cb34c60Sahrens /*
27083cb34c60Sahrens  * inputs:
27093cb34c60Sahrens  * zc_name		name of filesystem
27103cb34c60Sahrens  *
27113cb34c60Sahrens  * outputs:
27123cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
27133cb34c60Sahrens  */
2714ecd6cf80Smarks static int
2715ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2716ecd6cf80Smarks {
2717ecd6cf80Smarks 	nvlist_t *nvp;
2718ecd6cf80Smarks 	int error;
2719ecd6cf80Smarks 
2720ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2721ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2722ecd6cf80Smarks 		nvlist_free(nvp);
2723ecd6cf80Smarks 	}
2724ecd6cf80Smarks 
2725ecd6cf80Smarks 	return (error);
2726ecd6cf80Smarks }
2727ecd6cf80Smarks 
2728fa9e4066Sahrens /*
2729fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2730fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2731fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2732fa9e4066Sahrens  */
2733fa9e4066Sahrens static vfs_t *
2734fa9e4066Sahrens zfs_get_vfs(const char *resource)
2735fa9e4066Sahrens {
2736fa9e4066Sahrens 	struct vfs *vfsp;
2737fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2738fa9e4066Sahrens 
2739fa9e4066Sahrens 	vfs_list_read_lock();
2740fa9e4066Sahrens 	vfsp = rootvfs;
2741fa9e4066Sahrens 	do {
2742fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2743fa9e4066Sahrens 			VFS_HOLD(vfsp);
2744fa9e4066Sahrens 			vfs_found = vfsp;
2745fa9e4066Sahrens 			break;
2746fa9e4066Sahrens 		}
2747fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2748fa9e4066Sahrens 	} while (vfsp != rootvfs);
2749fa9e4066Sahrens 	vfs_list_unlock();
2750fa9e4066Sahrens 	return (vfs_found);
2751fa9e4066Sahrens }
2752fa9e4066Sahrens 
2753ecd6cf80Smarks /* ARGSUSED */
2754fa9e4066Sahrens static void
2755ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2756fa9e4066Sahrens {
2757da6c28aaSamw 	zfs_creat_t *zct = arg;
2758da6c28aaSamw 
2759de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2760da6c28aaSamw }
2761da6c28aaSamw 
2762de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2763da6c28aaSamw 
2764da6c28aaSamw /*
2765de8267e0Stimh  * inputs:
27660a48a24eStimh  * createprops		list of properties requested by creator
27670a48a24eStimh  * default_zplver	zpl version to use if unspecified in createprops
27680a48a24eStimh  * fuids_ok		fuids allowed in this version of the spa?
27690a48a24eStimh  * os			parent objset pointer (NULL if root fs)
2770de8267e0Stimh  *
2771de8267e0Stimh  * outputs:
2772de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
27730a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
2774da6c28aaSamw  *
2775de8267e0Stimh  * Determine the settings for utf8only, normalization and
2776de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
2777de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
2778de8267e0Stimh  * the file system is of too early a vintage, a creator can not
2779de8267e0Stimh  * request settings for these properties, even if the requested
2780de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
2781de8267e0Stimh  * properties for these, so remove them from the source nvlist after
2782de8267e0Stimh  * processing.
2783da6c28aaSamw  */
2784da6c28aaSamw static int
278514843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
27860a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
27870a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
2788da6c28aaSamw {
2789de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
2790de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
2791de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2792da6c28aaSamw 
2793de8267e0Stimh 	ASSERT(zplprops != NULL);
2794da6c28aaSamw 
2795de8267e0Stimh 	/*
2796de8267e0Stimh 	 * Pull out creator prop choices, if any.
2797de8267e0Stimh 	 */
2798de8267e0Stimh 	if (createprops) {
27990a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
28000a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2801de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2802de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2803de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2804de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2805de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2806de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2807de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2808de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2809de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2810de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2811de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2812de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
2813de8267e0Stimh 	}
2814da6c28aaSamw 
2815c2a93d44Stimh 	/*
28160a48a24eStimh 	 * If the zpl version requested is whacky or the file system
28170a48a24eStimh 	 * or pool is version is too "young" to support normalization
28180a48a24eStimh 	 * and the creator tried to set a value for one of the props,
28190a48a24eStimh 	 * error out.
2820c2a93d44Stimh 	 */
28210a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
28220a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
28230a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
28240a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
2825de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
28260a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
2827de8267e0Stimh 		return (ENOTSUP);
2828c2a93d44Stimh 
2829de8267e0Stimh 	/*
2830de8267e0Stimh 	 * Put the version in the zplprops
2831de8267e0Stimh 	 */
2832de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2833de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2834da6c28aaSamw 
2835de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
2836de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2837de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2838de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2839da6c28aaSamw 
2840c2a93d44Stimh 	/*
2841de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
2842c2a93d44Stimh 	 */
2843de8267e0Stimh 	if (norm)
2844de8267e0Stimh 		u8 = 1;
2845de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
2846de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2847de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2848de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2849de8267e0Stimh 
2850de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
2851de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2852de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
2853de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2854c2a93d44Stimh 
2855ab04eb8eStimh 	if (is_ci)
2856ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2857ab04eb8eStimh 
2858da6c28aaSamw 	return (0);
2859fa9e4066Sahrens }
2860fa9e4066Sahrens 
28610a48a24eStimh static int
28620a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
28630a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
28640a48a24eStimh {
28650a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
28660a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
28670a48a24eStimh 	objset_t *os = NULL;
28680a48a24eStimh 	char parentname[MAXNAMELEN];
28690a48a24eStimh 	char *cp;
28700a586ceaSMark Shellenbaum 	spa_t *spa;
28710a586ceaSMark Shellenbaum 	uint64_t spa_vers;
28720a48a24eStimh 	int error;
28730a48a24eStimh 
28740a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
28750a48a24eStimh 	cp = strrchr(parentname, '/');
28760a48a24eStimh 	ASSERT(cp != NULL);
28770a48a24eStimh 	cp[0] = '\0';
28780a48a24eStimh 
28790a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
28800a586ceaSMark Shellenbaum 		return (error);
28810a586ceaSMark Shellenbaum 
28820a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
28830a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
28840a586ceaSMark Shellenbaum 
28850a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
28860a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
28870a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
28880a48a24eStimh 
28890a48a24eStimh 	/*
28900a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
28910a48a24eStimh 	 */
2892503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
28930a48a24eStimh 		return (error);
28940a48a24eStimh 
28950a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
28960a48a24eStimh 	    zplprops, is_ci);
2897503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
28980a48a24eStimh 	return (error);
28990a48a24eStimh }
29000a48a24eStimh 
29010a48a24eStimh static int
29020a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
29030a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
29040a48a24eStimh {
29050a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
29060a586ceaSMark Shellenbaum 	boolean_t sa_ok;
29070a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
29080a48a24eStimh 	int error;
29090a48a24eStimh 
29100a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
29110a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
29120a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
29130a48a24eStimh 
29140a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
29150a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
29160a48a24eStimh 	return (error);
29170a48a24eStimh }
29180a48a24eStimh 
29193cb34c60Sahrens /*
29203cb34c60Sahrens  * inputs:
29213cb34c60Sahrens  * zc_objset_type	type of objset to create (fs vs zvol)
29223cb34c60Sahrens  * zc_name		name of new objset
29233cb34c60Sahrens  * zc_value		name of snapshot to clone from (may be empty)
29243cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
29253cb34c60Sahrens  *
2926de8267e0Stimh  * outputs: none
29273cb34c60Sahrens  */
2928fa9e4066Sahrens static int
2929fa9e4066Sahrens zfs_ioc_create(zfs_cmd_t *zc)
2930fa9e4066Sahrens {
2931fa9e4066Sahrens 	objset_t *clone;
2932fa9e4066Sahrens 	int error = 0;
2933da6c28aaSamw 	zfs_creat_t zct;
2934ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
2935ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2936fa9e4066Sahrens 	dmu_objset_type_t type = zc->zc_objset_type;
2937fa9e4066Sahrens 
2938fa9e4066Sahrens 	switch (type) {
2939fa9e4066Sahrens 
2940fa9e4066Sahrens 	case DMU_OST_ZFS:
2941fa9e4066Sahrens 		cbfunc = zfs_create_cb;
2942fa9e4066Sahrens 		break;
2943fa9e4066Sahrens 
2944fa9e4066Sahrens 	case DMU_OST_ZVOL:
2945fa9e4066Sahrens 		cbfunc = zvol_create_cb;
2946fa9e4066Sahrens 		break;
2947fa9e4066Sahrens 
2948fa9e4066Sahrens 	default:
29491d452cf5Sahrens 		cbfunc = NULL;
2950e7cbe64fSgw 		break;
2951fa9e4066Sahrens 	}
2952f18faf3fSek 	if (strchr(zc->zc_name, '@') ||
2953f18faf3fSek 	    strchr(zc->zc_name, '%'))
29541d452cf5Sahrens 		return (EINVAL);
2955fa9e4066Sahrens 
2956e9dbad6fSeschrock 	if (zc->zc_nvlist_src != NULL &&
2957990b4856Slling 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2958478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
2959e9dbad6fSeschrock 		return (error);
2960e9dbad6fSeschrock 
2961de8267e0Stimh 	zct.zct_zplprops = NULL;
2962da6c28aaSamw 	zct.zct_props = nvprops;
2963da6c28aaSamw 
2964e9dbad6fSeschrock 	if (zc->zc_value[0] != '\0') {
2965fa9e4066Sahrens 		/*
2966fa9e4066Sahrens 		 * We're creating a clone of an existing snapshot.
2967fa9e4066Sahrens 		 */
2968e9dbad6fSeschrock 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2969e9dbad6fSeschrock 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2970ecd6cf80Smarks 			nvlist_free(nvprops);
2971fa9e4066Sahrens 			return (EINVAL);
2972e9dbad6fSeschrock 		}
2973fa9e4066Sahrens 
2974503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2975e9dbad6fSeschrock 		if (error) {
2976ecd6cf80Smarks 			nvlist_free(nvprops);
2977fa9e4066Sahrens 			return (error);
2978e9dbad6fSeschrock 		}
2979ab04eb8eStimh 
2980ae46e4c7SMatthew Ahrens 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2981503ad85cSMatthew Ahrens 		dmu_objset_rele(clone, FTAG);
2982da6c28aaSamw 		if (error) {
2983da6c28aaSamw 			nvlist_free(nvprops);
2984da6c28aaSamw 			return (error);
2985da6c28aaSamw 		}
2986fa9e4066Sahrens 	} else {
2987ab04eb8eStimh 		boolean_t is_insensitive = B_FALSE;
2988ab04eb8eStimh 
2989e9dbad6fSeschrock 		if (cbfunc == NULL) {
2990ecd6cf80Smarks 			nvlist_free(nvprops);
29911d452cf5Sahrens 			return (EINVAL);
2992e9dbad6fSeschrock 		}
29935c5460e9Seschrock 
2994e9dbad6fSeschrock 		if (type == DMU_OST_ZVOL) {
2995e9dbad6fSeschrock 			uint64_t volsize, volblocksize;
2996e9dbad6fSeschrock 
2997ecd6cf80Smarks 			if (nvprops == NULL ||
2998ecd6cf80Smarks 			    nvlist_lookup_uint64(nvprops,
2999e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3000e9dbad6fSeschrock 			    &volsize) != 0) {
3001ecd6cf80Smarks 				nvlist_free(nvprops);
3002e9dbad6fSeschrock 				return (EINVAL);
3003e9dbad6fSeschrock 			}
3004e9dbad6fSeschrock 
3005ecd6cf80Smarks 			if ((error = nvlist_lookup_uint64(nvprops,
3006e9dbad6fSeschrock 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3007e9dbad6fSeschrock 			    &volblocksize)) != 0 && error != ENOENT) {
3008ecd6cf80Smarks 				nvlist_free(nvprops);
3009e9dbad6fSeschrock 				return (EINVAL);
3010e9dbad6fSeschrock 			}
3011e9dbad6fSeschrock 
3012e9dbad6fSeschrock 			if (error != 0)
3013e9dbad6fSeschrock 				volblocksize = zfs_prop_default_numeric(
3014e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3015e9dbad6fSeschrock 
3016e9dbad6fSeschrock 			if ((error = zvol_check_volblocksize(
3017e9dbad6fSeschrock 			    volblocksize)) != 0 ||
3018e9dbad6fSeschrock 			    (error = zvol_check_volsize(volsize,
3019e9dbad6fSeschrock 			    volblocksize)) != 0) {
3020ecd6cf80Smarks 				nvlist_free(nvprops);
30215c5460e9Seschrock 				return (error);
3022e9dbad6fSeschrock 			}
3023e7437265Sahrens 		} else if (type == DMU_OST_ZFS) {
3024da6c28aaSamw 			int error;
3025da6c28aaSamw 
3026da6c28aaSamw 			/*
3027da6c28aaSamw 			 * We have to have normalization and
3028da6c28aaSamw 			 * case-folding flags correct when we do the
3029da6c28aaSamw 			 * file system creation, so go figure them out
3030de8267e0Stimh 			 * now.
3031da6c28aaSamw 			 */
3032de8267e0Stimh 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
3033de8267e0Stimh 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
3034de8267e0Stimh 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
30350a48a24eStimh 			    zct.zct_zplprops, &is_insensitive);
3036da6c28aaSamw 			if (error != 0) {
3037da6c28aaSamw 				nvlist_free(nvprops);
3038de8267e0Stimh 				nvlist_free(zct.zct_zplprops);
3039da6c28aaSamw 				return (error);
3040da6c28aaSamw 			}
3041da6c28aaSamw 		}
3042ae46e4c7SMatthew Ahrens 		error = dmu_objset_create(zc->zc_name, type,
3043ab04eb8eStimh 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3044de8267e0Stimh 		nvlist_free(zct.zct_zplprops);
3045fa9e4066Sahrens 	}
3046e9dbad6fSeschrock 
3047e9dbad6fSeschrock 	/*
3048e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3049e9dbad6fSeschrock 	 */
3050e9dbad6fSeschrock 	if (error == 0) {
305192241e0bSTom Erickson 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
305292241e0bSTom Erickson 		    nvprops, NULL);
305392241e0bSTom Erickson 		if (error != 0)
3054842727c2SChris Kirby 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3055e9dbad6fSeschrock 	}
3056ecd6cf80Smarks 	nvlist_free(nvprops);
3057fa9e4066Sahrens 	return (error);
3058fa9e4066Sahrens }
3059fa9e4066Sahrens 
30603cb34c60Sahrens /*
30613cb34c60Sahrens  * inputs:
30623cb34c60Sahrens  * zc_name	name of filesystem
30633cb34c60Sahrens  * zc_value	short name of snapshot
30643cb34c60Sahrens  * zc_cookie	recursive flag
306514843421SMatthew Ahrens  * zc_nvlist_src[_size] property list
30663cb34c60Sahrens  *
3067681d9761SEric Taylor  * outputs:
3068681d9761SEric Taylor  * zc_value	short snapname (i.e. part after the '@')
30693cb34c60Sahrens  */
3070fa9e4066Sahrens static int
30711d452cf5Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc)
3072fa9e4066Sahrens {
3073bb0ade09Sahrens 	nvlist_t *nvprops = NULL;
3074bb0ade09Sahrens 	int error;
3075bb0ade09Sahrens 	boolean_t recursive = zc->zc_cookie;
3076bb0ade09Sahrens 
3077e9dbad6fSeschrock 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
30781d452cf5Sahrens 		return (EINVAL);
3079bb0ade09Sahrens 
3080bb0ade09Sahrens 	if (zc->zc_nvlist_src != NULL &&
3081bb0ade09Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3082478ed9adSEric Taylor 	    zc->zc_iflags, &nvprops)) != 0)
3083bb0ade09Sahrens 		return (error);
3084bb0ade09Sahrens 
3085ea2f5b9eSMatthew Ahrens 	error = zfs_check_userprops(zc->zc_name, nvprops);
3086ea2f5b9eSMatthew Ahrens 	if (error)
3087ea2f5b9eSMatthew Ahrens 		goto out;
3088bb0ade09Sahrens 
308992241e0bSTom Erickson 	if (!nvlist_empty(nvprops) &&
3090ea2f5b9eSMatthew Ahrens 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3091ea2f5b9eSMatthew Ahrens 		error = ENOTSUP;
3092ea2f5b9eSMatthew Ahrens 		goto out;
3093bb0ade09Sahrens 	}
3094ea2f5b9eSMatthew Ahrens 
309599d5e173STim Haley 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
309699d5e173STim Haley 	    nvprops, recursive, B_FALSE, -1);
3097ea2f5b9eSMatthew Ahrens 
3098ea2f5b9eSMatthew Ahrens out:
3099bb0ade09Sahrens 	nvlist_free(nvprops);
3100bb0ade09Sahrens 	return (error);
31011d452cf5Sahrens }
3102fa9e4066Sahrens 
3103cdf5b4caSmmusante int
3104fd136879SMatthew Ahrens zfs_unmount_snap(const char *name, void *arg)
31051d452cf5Sahrens {
31060b69c2f0Sahrens 	vfs_t *vfsp = NULL;
31071d452cf5Sahrens 
3108745cd3c5Smaybee 	if (arg) {
3109745cd3c5Smaybee 		char *snapname = arg;
3110fd136879SMatthew Ahrens 		char *fullname = kmem_asprintf("%s@%s", name, snapname);
3111fd136879SMatthew Ahrens 		vfsp = zfs_get_vfs(fullname);
3112fd136879SMatthew Ahrens 		strfree(fullname);
31130b69c2f0Sahrens 	} else if (strchr(name, '@')) {
31141d452cf5Sahrens 		vfsp = zfs_get_vfs(name);
31151d452cf5Sahrens 	}
31161d452cf5Sahrens 
31171d452cf5Sahrens 	if (vfsp) {
3118fa9e4066Sahrens 		/*
31191d452cf5Sahrens 		 * Always force the unmount for snapshots.
3120fa9e4066Sahrens 		 */
31211d452cf5Sahrens 		int flag = MS_FORCE;
31221d452cf5Sahrens 		int err;
31231d452cf5Sahrens 
31241d452cf5Sahrens 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3125fa9e4066Sahrens 			VFS_RELE(vfsp);
31261d452cf5Sahrens 			return (err);
3127fa9e4066Sahrens 		}
31281d452cf5Sahrens 		VFS_RELE(vfsp);
31291d452cf5Sahrens 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
31301d452cf5Sahrens 			return (err);
31311d452cf5Sahrens 	}
31321d452cf5Sahrens 	return (0);
31331d452cf5Sahrens }
31341d452cf5Sahrens 
31353cb34c60Sahrens /*
31363cb34c60Sahrens  * inputs:
3137*19b94df9SMatthew Ahrens  * zc_name		name of filesystem, snaps must be under it
3138*19b94df9SMatthew Ahrens  * zc_nvlist_src[_size]	full names of snapshots to destroy
3139842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
31403cb34c60Sahrens  *
3141*19b94df9SMatthew Ahrens  * outputs:
3142*19b94df9SMatthew Ahrens  * zc_name		on failure, name of failed snapshot
31433cb34c60Sahrens  */
31441d452cf5Sahrens static int
3145*19b94df9SMatthew Ahrens zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc)
31461d452cf5Sahrens {
3147*19b94df9SMatthew Ahrens 	int err, len;
3148*19b94df9SMatthew Ahrens 	nvlist_t *nvl;
3149*19b94df9SMatthew Ahrens 	nvpair_t *pair;
31501d452cf5Sahrens 
3151*19b94df9SMatthew Ahrens 	if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3152*19b94df9SMatthew Ahrens 	    zc->zc_iflags, &nvl)) != 0)
31531d452cf5Sahrens 		return (err);
3154*19b94df9SMatthew Ahrens 
3155*19b94df9SMatthew Ahrens 	len = strlen(zc->zc_name);
3156*19b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
3157*19b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(nvl, pair)) {
3158*19b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
3159*19b94df9SMatthew Ahrens 		/*
3160*19b94df9SMatthew Ahrens 		 * The snap name must be underneath the zc_name.  This ensures
3161*19b94df9SMatthew Ahrens 		 * that our permission checks were legitimate.
3162*19b94df9SMatthew Ahrens 		 */
3163*19b94df9SMatthew Ahrens 		if (strncmp(zc->zc_name, name, len) != 0 ||
3164*19b94df9SMatthew Ahrens 		    (name[len] != '@' && name[len] != '/')) {
3165*19b94df9SMatthew Ahrens 			nvlist_free(nvl);
3166*19b94df9SMatthew Ahrens 			return (EINVAL);
3167*19b94df9SMatthew Ahrens 		}
3168*19b94df9SMatthew Ahrens 
3169*19b94df9SMatthew Ahrens 		(void) zfs_unmount_snap(name, NULL);
3170*19b94df9SMatthew Ahrens 	}
3171*19b94df9SMatthew Ahrens 
3172*19b94df9SMatthew Ahrens 	err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy,
3173*19b94df9SMatthew Ahrens 	    zc->zc_name);
3174*19b94df9SMatthew Ahrens 	nvlist_free(nvl);
3175*19b94df9SMatthew Ahrens 	return (err);
31761d452cf5Sahrens }
31771d452cf5Sahrens 
31783cb34c60Sahrens /*
31793cb34c60Sahrens  * inputs:
31803cb34c60Sahrens  * zc_name		name of dataset to destroy
31813cb34c60Sahrens  * zc_objset_type	type of objset
3182842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
31833cb34c60Sahrens  *
31843cb34c60Sahrens  * outputs:		none
31853cb34c60Sahrens  */
31861d452cf5Sahrens static int
31871d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
31881d452cf5Sahrens {
3189681d9761SEric Taylor 	int err;
31901d452cf5Sahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3191681d9761SEric Taylor 		err = zfs_unmount_snap(zc->zc_name, NULL);
31921d452cf5Sahrens 		if (err)
31931d452cf5Sahrens 			return (err);
3194fa9e4066Sahrens 	}
3195fa9e4066Sahrens 
3196681d9761SEric Taylor 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3197681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
31985c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3199681d9761SEric Taylor 	return (err);
3200fa9e4066Sahrens }
3201fa9e4066Sahrens 
32023cb34c60Sahrens /*
32033cb34c60Sahrens  * inputs:
32044ccbb6e7Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
32053cb34c60Sahrens  *
32063cb34c60Sahrens  * outputs:	none
32073cb34c60Sahrens  */
3208fa9e4066Sahrens static int
3209fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
3210fa9e4066Sahrens {
3211ae46e4c7SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
32124ccbb6e7Sahrens 	int error;
3213ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3214ae46e4c7SMatthew Ahrens 	char *clone_name;
3215ae46e4c7SMatthew Ahrens 
3216ae46e4c7SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3217ae46e4c7SMatthew Ahrens 	if (error)
3218ae46e4c7SMatthew Ahrens 		return (error);
3219ae46e4c7SMatthew Ahrens 
3220ae46e4c7SMatthew Ahrens 	/* must not be a snapshot */
3221ae46e4c7SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
3222ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3223ae46e4c7SMatthew Ahrens 		return (EINVAL);
3224ae46e4c7SMatthew Ahrens 	}
3225ae46e4c7SMatthew Ahrens 
3226ae46e4c7SMatthew Ahrens 	/* must have a most recent snapshot */
3227ae46e4c7SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3228ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3229ae46e4c7SMatthew Ahrens 		return (EINVAL);
3230ae46e4c7SMatthew Ahrens 	}
32314ccbb6e7Sahrens 
32324ccbb6e7Sahrens 	/*
3233ae46e4c7SMatthew Ahrens 	 * Create clone of most recent snapshot.
32344ccbb6e7Sahrens 	 */
3235ae46e4c7SMatthew Ahrens 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3236ae46e4c7SMatthew Ahrens 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
32374ccbb6e7Sahrens 	if (error)
3238ae46e4c7SMatthew Ahrens 		goto out;
32394ccbb6e7Sahrens 
3240503ad85cSMatthew Ahrens 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3241ae46e4c7SMatthew Ahrens 	if (error)
3242ae46e4c7SMatthew Ahrens 		goto out;
3243ae46e4c7SMatthew Ahrens 
3244ae46e4c7SMatthew Ahrens 	/*
3245ae46e4c7SMatthew Ahrens 	 * Do clone swap.
3246ae46e4c7SMatthew Ahrens 	 */
324714843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3248503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
324947f263f4Sek 		if (error == 0) {
325047f263f4Sek 			int resume_err;
32514ccbb6e7Sahrens 
3252ae46e4c7SMatthew Ahrens 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3253ae46e4c7SMatthew Ahrens 				error = dsl_dataset_clone_swap(clone, ds,
3254ae46e4c7SMatthew Ahrens 				    B_TRUE);
3255ae46e4c7SMatthew Ahrens 				dsl_dataset_disown(ds, FTAG);
3256ae46e4c7SMatthew Ahrens 				ds = NULL;
3257ae46e4c7SMatthew Ahrens 			} else {
3258ae46e4c7SMatthew Ahrens 				error = EBUSY;
3259ae46e4c7SMatthew Ahrens 			}
3260503ad85cSMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
326147f263f4Sek 			error = error ? error : resume_err;
326247f263f4Sek 		}
32634ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
32644ccbb6e7Sahrens 	} else {
3265ae46e4c7SMatthew Ahrens 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3266ae46e4c7SMatthew Ahrens 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3267ae46e4c7SMatthew Ahrens 			dsl_dataset_disown(ds, FTAG);
3268ae46e4c7SMatthew Ahrens 			ds = NULL;
3269ae46e4c7SMatthew Ahrens 		} else {
3270ae46e4c7SMatthew Ahrens 			error = EBUSY;
3271ae46e4c7SMatthew Ahrens 		}
32724ccbb6e7Sahrens 	}
32734ccbb6e7Sahrens 
3274ae46e4c7SMatthew Ahrens 	/*
3275ae46e4c7SMatthew Ahrens 	 * Destroy clone (which also closes it).
3276ae46e4c7SMatthew Ahrens 	 */
3277ae46e4c7SMatthew Ahrens 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3278ae46e4c7SMatthew Ahrens 
3279ae46e4c7SMatthew Ahrens out:
3280ae46e4c7SMatthew Ahrens 	strfree(clone_name);
3281ae46e4c7SMatthew Ahrens 	if (ds)
3282ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
32834ccbb6e7Sahrens 	return (error);
3284fa9e4066Sahrens }
3285fa9e4066Sahrens 
32863cb34c60Sahrens /*
32873cb34c60Sahrens  * inputs:
32883cb34c60Sahrens  * zc_name	old name of dataset
32893cb34c60Sahrens  * zc_value	new name of dataset
32903cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
32913cb34c60Sahrens  *
32923cb34c60Sahrens  * outputs:	none
32933cb34c60Sahrens  */
3294fa9e4066Sahrens static int
3295fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3296fa9e4066Sahrens {
32977f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
3298cdf5b4caSmmusante 
3299e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3300f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3301f18faf3fSek 	    strchr(zc->zc_value, '%'))
3302fa9e4066Sahrens 		return (EINVAL);
3303fa9e4066Sahrens 
3304cdf5b4caSmmusante 	/*
3305cdf5b4caSmmusante 	 * Unmount snapshot unless we're doing a recursive rename,
3306cdf5b4caSmmusante 	 * in which case the dataset code figures out which snapshots
3307cdf5b4caSmmusante 	 * to unmount.
3308cdf5b4caSmmusante 	 */
3309cdf5b4caSmmusante 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3310fa9e4066Sahrens 	    zc->zc_objset_type == DMU_OST_ZFS) {
33111d452cf5Sahrens 		int err = zfs_unmount_snap(zc->zc_name, NULL);
33121d452cf5Sahrens 		if (err)
33131d452cf5Sahrens 			return (err);
3314fa9e4066Sahrens 	}
3315681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL)
3316681d9761SEric Taylor 		(void) zvol_remove_minor(zc->zc_name);
3317cdf5b4caSmmusante 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3318fa9e4066Sahrens }
3319fa9e4066Sahrens 
332092241e0bSTom Erickson static int
332192241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
332292241e0bSTom Erickson {
332392241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
332492241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
332592241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
332692241e0bSTom Erickson 	uint64_t intval;
332792241e0bSTom Erickson 	int err;
332892241e0bSTom Erickson 
332992241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
333092241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
333192241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
333292241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
333392241e0bSTom Erickson 				return (err);
333492241e0bSTom Erickson 			return (0);
333592241e0bSTom Erickson 		}
333692241e0bSTom Erickson 
333792241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
333892241e0bSTom Erickson 			const char *perm = NULL;
333992241e0bSTom Erickson 			const char *uq_prefix =
334092241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
334192241e0bSTom Erickson 			const char *gq_prefix =
334292241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
334392241e0bSTom Erickson 
334492241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
334592241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
334692241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
334792241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
334892241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
334992241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
335092241e0bSTom Erickson 			} else {
335192241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
335292241e0bSTom Erickson 				return (EINVAL);
335392241e0bSTom Erickson 			}
335492241e0bSTom Erickson 
335592241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
335692241e0bSTom Erickson 				return (err);
335792241e0bSTom Erickson 			return (0);
335892241e0bSTom Erickson 		}
335992241e0bSTom Erickson 
336092241e0bSTom Erickson 		return (EINVAL);
336192241e0bSTom Erickson 	}
336292241e0bSTom Erickson 
336392241e0bSTom Erickson 	if (issnap)
336492241e0bSTom Erickson 		return (EINVAL);
336592241e0bSTom Erickson 
336692241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
336792241e0bSTom Erickson 		/*
336892241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
336992241e0bSTom Erickson 		 * format.
337092241e0bSTom Erickson 		 */
337192241e0bSTom Erickson 		nvlist_t *attrs;
337292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
337392241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
337492241e0bSTom Erickson 		    &pair) == 0);
337592241e0bSTom Erickson 	}
337692241e0bSTom Erickson 
337792241e0bSTom Erickson 	/*
337892241e0bSTom Erickson 	 * Check that this value is valid for this pool version
337992241e0bSTom Erickson 	 */
338092241e0bSTom Erickson 	switch (prop) {
338192241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
338292241e0bSTom Erickson 		/*
338392241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
338492241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
338592241e0bSTom Erickson 		 * we'll catch them later.
338692241e0bSTom Erickson 		 */
338792241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
338892241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
338992241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
339092241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
339192241e0bSTom Erickson 			    zfs_earlier_version(dsname,
339292241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
339392241e0bSTom Erickson 				return (ENOTSUP);
339492241e0bSTom Erickson 			}
339592241e0bSTom Erickson 
339692241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
339792241e0bSTom Erickson 			    zfs_earlier_version(dsname,
339892241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
339992241e0bSTom Erickson 				return (ENOTSUP);
340092241e0bSTom Erickson 
340192241e0bSTom Erickson 			/*
340292241e0bSTom Erickson 			 * If this is a bootable dataset then
340392241e0bSTom Erickson 			 * verify that the compression algorithm
340492241e0bSTom Erickson 			 * is supported for booting. We must return
340592241e0bSTom Erickson 			 * something other than ENOTSUP since it
340692241e0bSTom Erickson 			 * implies a downrev pool version.
340792241e0bSTom Erickson 			 */
340892241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
340992241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
341092241e0bSTom Erickson 				return (ERANGE);
341192241e0bSTom Erickson 			}
341292241e0bSTom Erickson 		}
341392241e0bSTom Erickson 		break;
341492241e0bSTom Erickson 
341592241e0bSTom Erickson 	case ZFS_PROP_COPIES:
341692241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
341792241e0bSTom Erickson 			return (ENOTSUP);
341892241e0bSTom Erickson 		break;
341992241e0bSTom Erickson 
342092241e0bSTom Erickson 	case ZFS_PROP_DEDUP:
342192241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
342292241e0bSTom Erickson 			return (ENOTSUP);
342392241e0bSTom Erickson 		break;
342492241e0bSTom Erickson 
342592241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
342692241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
342792241e0bSTom Erickson 			return (ENOTSUP);
342892241e0bSTom Erickson 		break;
342992241e0bSTom Erickson 
343092241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
343192241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
343292241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
343392241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
343492241e0bSTom Erickson 			    zfs_earlier_version(dsname,
343592241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
343692241e0bSTom Erickson 				return (ENOTSUP);
343792241e0bSTom Erickson 		}
343892241e0bSTom Erickson 		break;
343992241e0bSTom Erickson 	}
344092241e0bSTom Erickson 
344192241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
344292241e0bSTom Erickson }
344392241e0bSTom Erickson 
344492241e0bSTom Erickson /*
344592241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
344692241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
344792241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
344892241e0bSTom Erickson  *
344992241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
345092241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
345192241e0bSTom Erickson  * of all the properties that failed a permission check along with the
345292241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
345392241e0bSTom Erickson  * returned errlist.
345492241e0bSTom Erickson  *
345592241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
345692241e0bSTom Erickson  * pointed at by errlist is NULL.
345792241e0bSTom Erickson  */
345892241e0bSTom Erickson static int
345992241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3460745cd3c5Smaybee {
3461745cd3c5Smaybee 	zfs_cmd_t *zc;
346292241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
346392241e0bSTom Erickson 	nvlist_t *errors;
346492241e0bSTom Erickson 	int err, rv = 0;
3465745cd3c5Smaybee 
3466745cd3c5Smaybee 	if (props == NULL)
346792241e0bSTom Erickson 		return (0);
346892241e0bSTom Erickson 
346992241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
347092241e0bSTom Erickson 
3471745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3472745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
347392241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
347492241e0bSTom Erickson 	while (pair != NULL) {
347592241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
347692241e0bSTom Erickson 
347792241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
347892241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
347992241e0bSTom Erickson 		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
348092241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
348192241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
348292241e0bSTom Erickson 			    zc->zc_value, err) == 0);
348392241e0bSTom Erickson 		}
348492241e0bSTom Erickson 		pair = next_pair;
3485745cd3c5Smaybee 	}
3486745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
348792241e0bSTom Erickson 
348892241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
348992241e0bSTom Erickson 		nvlist_free(errors);
349092241e0bSTom Erickson 		errors = NULL;
349192241e0bSTom Erickson 	} else {
349292241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
349392241e0bSTom Erickson 	}
349492241e0bSTom Erickson 
349592241e0bSTom Erickson 	if (errlist == NULL)
349692241e0bSTom Erickson 		nvlist_free(errors);
349792241e0bSTom Erickson 	else
349892241e0bSTom Erickson 		*errlist = errors;
349992241e0bSTom Erickson 
350092241e0bSTom Erickson 	return (rv);
350192241e0bSTom Erickson }
350292241e0bSTom Erickson 
350392241e0bSTom Erickson static boolean_t
350492241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
350592241e0bSTom Erickson {
350692241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
350792241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
350892241e0bSTom Erickson 		nvlist_t *attrs;
350992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
351092241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
351192241e0bSTom Erickson 		    &p1) == 0);
351292241e0bSTom Erickson 	}
351392241e0bSTom Erickson 
351492241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
351592241e0bSTom Erickson 		nvlist_t *attrs;
351692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
351792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
351892241e0bSTom Erickson 		    &p2) == 0);
351992241e0bSTom Erickson 	}
352092241e0bSTom Erickson 
352192241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
352292241e0bSTom Erickson 		return (B_FALSE);
352392241e0bSTom Erickson 
352492241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
352592241e0bSTom Erickson 		char *valstr1, *valstr2;
352692241e0bSTom Erickson 
352792241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
352892241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
352992241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
353092241e0bSTom Erickson 	} else {
353192241e0bSTom Erickson 		uint64_t intval1, intval2;
353292241e0bSTom Erickson 
353392241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
353492241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
353592241e0bSTom Erickson 		return (intval1 == intval2);
353692241e0bSTom Erickson 	}
3537745cd3c5Smaybee }
3538745cd3c5Smaybee 
353992241e0bSTom Erickson /*
354092241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
354192241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
354292241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
354392241e0bSTom Erickson  */
354492241e0bSTom Erickson static void
354592241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
354692241e0bSTom Erickson {
354792241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
354892241e0bSTom Erickson 
354992241e0bSTom Erickson 	if (origprops == NULL)
355092241e0bSTom Erickson 		return; /* all props need to be received */
355192241e0bSTom Erickson 
355292241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
355392241e0bSTom Erickson 	while (pair != NULL) {
355492241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
355592241e0bSTom Erickson 		nvpair_t *match;
355692241e0bSTom Erickson 
355792241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
355892241e0bSTom Erickson 
355992241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
356092241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
356192241e0bSTom Erickson 			goto next; /* need to set received value */
356292241e0bSTom Erickson 
356392241e0bSTom Erickson 		/* don't clear the existing received value */
356492241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
356592241e0bSTom Erickson 		/* don't bother receiving the property */
356692241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
356792241e0bSTom Erickson next:
356892241e0bSTom Erickson 		pair = next_pair;
356992241e0bSTom Erickson 	}
357092241e0bSTom Erickson }
357192241e0bSTom Erickson 
357292241e0bSTom Erickson #ifdef	DEBUG
357392241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
357492241e0bSTom Erickson #endif
357592241e0bSTom Erickson 
35763cb34c60Sahrens /*
35773cb34c60Sahrens  * inputs:
35783cb34c60Sahrens  * zc_name		name of containing filesystem
35793cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
35803cb34c60Sahrens  * zc_value		name of snapshot to create
35813cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
35823cb34c60Sahrens  * zc_cookie		file descriptor to recv from
35833cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
35843cb34c60Sahrens  * zc_guid		force flag
3585c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
3586c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
35873cb34c60Sahrens  *
35883cb34c60Sahrens  * outputs:
35893cb34c60Sahrens  * zc_cookie		number of bytes read
359092241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
359192241e0bSTom Erickson  * zc_obj		zprop_errflags_t
3592c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
35933cb34c60Sahrens  */
3594fa9e4066Sahrens static int
35953cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3596fa9e4066Sahrens {
3597fa9e4066Sahrens 	file_t *fp;
3598f18faf3fSek 	objset_t *os;
35993cb34c60Sahrens 	dmu_recv_cookie_t drc;
3600f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
360192241e0bSTom Erickson 	int fd;
360292241e0bSTom Erickson 	int error = 0;
360392241e0bSTom Erickson 	int props_error = 0;
360492241e0bSTom Erickson 	nvlist_t *errors;
36053cb34c60Sahrens 	offset_t off;
360692241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
360792241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
36083cb34c60Sahrens 	objset_t *origin = NULL;
36093cb34c60Sahrens 	char *tosnap;
36103cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
361192241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
3612fa9e4066Sahrens 
36133ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3614f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
3615f18faf3fSek 	    strchr(zc->zc_value, '%'))
36163ccfa83cSahrens 		return (EINVAL);
36173ccfa83cSahrens 
36183cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
36193cb34c60Sahrens 	tosnap = strchr(tofs, '@');
362092241e0bSTom Erickson 	*tosnap++ = '\0';
36213cb34c60Sahrens 
36223cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
36233cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3624478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
36253cb34c60Sahrens 		return (error);
36263cb34c60Sahrens 
3627fa9e4066Sahrens 	fd = zc->zc_cookie;
3628fa9e4066Sahrens 	fp = getf(fd);
36293cb34c60Sahrens 	if (fp == NULL) {
36303cb34c60Sahrens 		nvlist_free(props);
3631fa9e4066Sahrens 		return (EBADF);
36323cb34c60Sahrens 	}
3633f18faf3fSek 
363492241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
363592241e0bSTom Erickson 
3636503ad85cSMatthew Ahrens 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
363792241e0bSTom Erickson 		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
363892241e0bSTom Erickson 		    !dsl_prop_get_hasrecvd(os)) {
363992241e0bSTom Erickson 			first_recvd_props = B_TRUE;
364092241e0bSTom Erickson 		}
364192241e0bSTom Erickson 
3642745cd3c5Smaybee 		/*
364392241e0bSTom Erickson 		 * If new received properties are supplied, they are to
364492241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
364592241e0bSTom Erickson 		 * away the existing ones.
3646745cd3c5Smaybee 		 */
364792241e0bSTom Erickson 		if (dsl_prop_get_received(os, &origprops) == 0) {
364892241e0bSTom Erickson 			nvlist_t *errlist = NULL;
364992241e0bSTom Erickson 			/*
365092241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
365192241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
365292241e0bSTom Erickson 			 *
365392241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
365492241e0bSTom Erickson 			 * special case where we blow away all local properties
365592241e0bSTom Erickson 			 * regardless.
365692241e0bSTom Erickson 			 */
365792241e0bSTom Erickson 			if (!first_recvd_props)
365892241e0bSTom Erickson 				props_reduce(props, origprops);
365992241e0bSTom Erickson 			if (zfs_check_clearable(tofs, origprops,
366092241e0bSTom Erickson 			    &errlist) != 0)
366192241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
366292241e0bSTom Erickson 			nvlist_free(errlist);
366392241e0bSTom Erickson 		}
3664745cd3c5Smaybee 
3665503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3666f18faf3fSek 	}
3667f18faf3fSek 
36683cb34c60Sahrens 	if (zc->zc_string[0]) {
3669503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3670745cd3c5Smaybee 		if (error)
3671745cd3c5Smaybee 			goto out;
36723cb34c60Sahrens 	}
36733cb34c60Sahrens 
36749e69d7d0SLori Alt 	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
36759e69d7d0SLori Alt 	    &zc->zc_begin_record, force, origin, &drc);
36763cb34c60Sahrens 	if (origin)
3677503ad85cSMatthew Ahrens 		dmu_objset_rele(origin, FTAG);
3678745cd3c5Smaybee 	if (error)
3679745cd3c5Smaybee 		goto out;
3680f18faf3fSek 
3681f18faf3fSek 	/*
368292241e0bSTom Erickson 	 * Set properties before we receive the stream so that they are applied
368392241e0bSTom Erickson 	 * to the new data. Note that we must call dmu_recv_stream() if
368492241e0bSTom Erickson 	 * dmu_recv_begin() succeeds.
3685f18faf3fSek 	 */
36863cb34c60Sahrens 	if (props) {
368792241e0bSTom Erickson 		nvlist_t *errlist;
368892241e0bSTom Erickson 
368992241e0bSTom Erickson 		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
369092241e0bSTom Erickson 			if (drc.drc_newfs) {
369192241e0bSTom Erickson 				if (spa_version(os->os_spa) >=
369292241e0bSTom Erickson 				    SPA_VERSION_RECVD_PROPS)
369392241e0bSTom Erickson 					first_recvd_props = B_TRUE;
369492241e0bSTom Erickson 			} else if (origprops != NULL) {
369592241e0bSTom Erickson 				if (clear_received_props(os, tofs, origprops,
369692241e0bSTom Erickson 				    first_recvd_props ? NULL : props) != 0)
369792241e0bSTom Erickson 					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
369892241e0bSTom Erickson 			} else {
369992241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
370092241e0bSTom Erickson 			}
370192241e0bSTom Erickson 			dsl_prop_set_hasrecvd(os);
370292241e0bSTom Erickson 		} else if (!drc.drc_newfs) {
370392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
370492241e0bSTom Erickson 		}
370592241e0bSTom Erickson 
370692241e0bSTom Erickson 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
370792241e0bSTom Erickson 		    props, &errlist);
370892241e0bSTom Erickson 		(void) nvlist_merge(errors, errlist, 0);
370992241e0bSTom Erickson 		nvlist_free(errlist);
371092241e0bSTom Erickson 	}
371192241e0bSTom Erickson 
371292241e0bSTom Erickson 	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3713745cd3c5Smaybee 		/*
371492241e0bSTom Erickson 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
371592241e0bSTom Erickson 		 * size or supplied an invalid address.
3716745cd3c5Smaybee 		 */
371792241e0bSTom Erickson 		props_error = EINVAL;
37183cb34c60Sahrens 	}
37193cb34c60Sahrens 
37203cb34c60Sahrens 	off = fp->f_offset;
3721c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3722c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
3723a2eea2e1Sahrens 
3724f4b94bdeSMatthew Ahrens 	if (error == 0) {
3725f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
3726745cd3c5Smaybee 
3727f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
3728f4b94bdeSMatthew Ahrens 			/* online recv */
3729f4b94bdeSMatthew Ahrens 			int end_err;
3730745cd3c5Smaybee 
3731503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
3732f4b94bdeSMatthew Ahrens 			/*
3733f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
3734f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
3735f4b94bdeSMatthew Ahrens 			 */
3736f4b94bdeSMatthew Ahrens 			end_err = dmu_recv_end(&drc);
37375c703fceSGeorge Wilson 			if (error == 0)
37385c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
3739f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
3740f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
3741745cd3c5Smaybee 		} else {
3742f4b94bdeSMatthew Ahrens 			error = dmu_recv_end(&drc);
37433cb34c60Sahrens 		}
374447f263f4Sek 	}
37453cb34c60Sahrens 
37463cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
37473cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
37483cb34c60Sahrens 		fp->f_offset = off;
3749a2eea2e1Sahrens 
375092241e0bSTom Erickson #ifdef	DEBUG
375192241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
375292241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
375392241e0bSTom Erickson 		error = 1;
375492241e0bSTom Erickson 	}
375592241e0bSTom Erickson #endif
3756745cd3c5Smaybee 	/*
3757745cd3c5Smaybee 	 * On error, restore the original props.
3758745cd3c5Smaybee 	 */
3759745cd3c5Smaybee 	if (error && props) {
376092241e0bSTom Erickson 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
376192241e0bSTom Erickson 			if (clear_received_props(os, tofs, props, NULL) != 0) {
376292241e0bSTom Erickson 				/*
376392241e0bSTom Erickson 				 * We failed to clear the received properties.
376492241e0bSTom Erickson 				 * Since we may have left a $recvd value on the
376592241e0bSTom Erickson 				 * system, we can't clear the $hasrecvd flag.
376692241e0bSTom Erickson 				 */
376792241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NORESTORE;
376892241e0bSTom Erickson 			} else if (first_recvd_props) {
376992241e0bSTom Erickson 				dsl_prop_unset_hasrecvd(os);
377092241e0bSTom Erickson 			}
377192241e0bSTom Erickson 			dmu_objset_rele(os, FTAG);
377292241e0bSTom Erickson 		} else if (!drc.drc_newfs) {
377392241e0bSTom Erickson 			/* We failed to clear the received properties. */
377492241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
377592241e0bSTom Erickson 		}
377692241e0bSTom Erickson 
377792241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
377892241e0bSTom Erickson 			/* We failed to stash the original properties. */
377992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
378092241e0bSTom Erickson 		}
378192241e0bSTom Erickson 
378292241e0bSTom Erickson 		/*
378392241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
378492241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
378592241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
378692241e0bSTom Erickson 		 * first new-style receive.
378792241e0bSTom Erickson 		 */
378892241e0bSTom Erickson 		if (origprops != NULL &&
378992241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
379092241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
379192241e0bSTom Erickson 		    origprops, NULL) != 0) {
379292241e0bSTom Erickson 			/*
379392241e0bSTom Erickson 			 * We stashed the original properties but failed to
379492241e0bSTom Erickson 			 * restore them.
379592241e0bSTom Erickson 			 */
379692241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
379792241e0bSTom Erickson 		}
3798745cd3c5Smaybee 	}
3799745cd3c5Smaybee out:
3800745cd3c5Smaybee 	nvlist_free(props);
3801745cd3c5Smaybee 	nvlist_free(origprops);
380292241e0bSTom Erickson 	nvlist_free(errors);
3803fa9e4066Sahrens 	releasef(fd);
380492241e0bSTom Erickson 
380592241e0bSTom Erickson 	if (error == 0)
380692241e0bSTom Erickson 		error = props_error;
380792241e0bSTom Erickson 
3808fa9e4066Sahrens 	return (error);
3809fa9e4066Sahrens }
3810fa9e4066Sahrens 
38113cb34c60Sahrens /*
38123cb34c60Sahrens  * inputs:
38133cb34c60Sahrens  * zc_name	name of snapshot to send
38143cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
3815a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
3816a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
3817a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
3818*19b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
3819*19b94df9SMatthew Ahrens  *		output size in zc_objset_type.
38203cb34c60Sahrens  *
38213cb34c60Sahrens  * outputs: none
38223cb34c60Sahrens  */
3823fa9e4066Sahrens static int
38243cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
3825fa9e4066Sahrens {
3826fa9e4066Sahrens 	objset_t *fromsnap = NULL;
3827fa9e4066Sahrens 	objset_t *tosnap;
3828fa9e4066Sahrens 	int error;
38293cb34c60Sahrens 	offset_t off;
3830a7f53a56SChris Kirby 	dsl_dataset_t *ds;
3831a7f53a56SChris Kirby 	dsl_dataset_t *dsfrom = NULL;
3832a7f53a56SChris Kirby 	spa_t *spa;
3833a7f53a56SChris Kirby 	dsl_pool_t *dp;
3834*19b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
3835fa9e4066Sahrens 
3836a7f53a56SChris Kirby 	error = spa_open(zc->zc_name, &spa, FTAG);
3837fa9e4066Sahrens 	if (error)
3838fa9e4066Sahrens 		return (error);
3839fa9e4066Sahrens 
3840a7f53a56SChris Kirby 	dp = spa_get_dsl(spa);
3841a7f53a56SChris Kirby 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3842a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3843a7f53a56SChris Kirby 	rw_exit(&dp->dp_config_rwlock);
3844a7f53a56SChris Kirby 	if (error) {
3845a7f53a56SChris Kirby 		spa_close(spa, FTAG);
3846a7f53a56SChris Kirby 		return (error);
3847a7f53a56SChris Kirby 	}
3848a7f53a56SChris Kirby 
3849a7f53a56SChris Kirby 	error = dmu_objset_from_ds(ds, &tosnap);
3850a7f53a56SChris Kirby 	if (error) {
3851a7f53a56SChris Kirby 		dsl_dataset_rele(ds, FTAG);
3852a7f53a56SChris Kirby 		spa_close(spa, FTAG);
3853a7f53a56SChris Kirby 		return (error);
3854a7f53a56SChris Kirby 	}
3855a7f53a56SChris Kirby 
3856a7f53a56SChris Kirby 	if (zc->zc_fromobj != 0) {
3857a7f53a56SChris Kirby 		rw_enter(&dp->dp_config_rwlock, RW_READER);
3858a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3859a7f53a56SChris Kirby 		rw_exit(&dp->dp_config_rwlock);
3860a7f53a56SChris Kirby 		spa_close(spa, FTAG);
3861a7f53a56SChris Kirby 		if (error) {
3862a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
3863a7f53a56SChris Kirby 			return (error);
3864a7f53a56SChris Kirby 		}
3865a7f53a56SChris Kirby 		error = dmu_objset_from_ds(dsfrom, &fromsnap);
3866fa9e4066Sahrens 		if (error) {
3867a7f53a56SChris Kirby 			dsl_dataset_rele(dsfrom, FTAG);
3868a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
3869fa9e4066Sahrens 			return (error);
3870fa9e4066Sahrens 		}
3871a7f53a56SChris Kirby 	} else {
3872a7f53a56SChris Kirby 		spa_close(spa, FTAG);
3873fa9e4066Sahrens 	}
3874fa9e4066Sahrens 
3875*19b94df9SMatthew Ahrens 	if (estimate) {
3876*19b94df9SMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj,
3877*19b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
3878*19b94df9SMatthew Ahrens 	} else {
3879*19b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
3880*19b94df9SMatthew Ahrens 		if (fp == NULL) {
3881*19b94df9SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3882*19b94df9SMatthew Ahrens 			if (dsfrom)
3883*19b94df9SMatthew Ahrens 				dsl_dataset_rele(dsfrom, FTAG);
3884*19b94df9SMatthew Ahrens 			return (EBADF);
3885*19b94df9SMatthew Ahrens 		}
3886fa9e4066Sahrens 
3887*19b94df9SMatthew Ahrens 		off = fp->f_offset;
3888*19b94df9SMatthew Ahrens 		error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj,
3889*19b94df9SMatthew Ahrens 		    fp->f_vnode, &off);
3890fa9e4066Sahrens 
3891*19b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3892*19b94df9SMatthew Ahrens 			fp->f_offset = off;
3893*19b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
3894*19b94df9SMatthew Ahrens 	}
3895a7f53a56SChris Kirby 	if (dsfrom)
3896a7f53a56SChris Kirby 		dsl_dataset_rele(dsfrom, FTAG);
3897a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3898fa9e4066Sahrens 	return (error);
3899fa9e4066Sahrens }
3900fa9e4066Sahrens 
3901ea8dc4b6Seschrock static int
3902ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
3903ea8dc4b6Seschrock {
3904ea8dc4b6Seschrock 	int id, error;
3905ea8dc4b6Seschrock 
3906ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3907ea8dc4b6Seschrock 	    &zc->zc_inject_record);
3908ea8dc4b6Seschrock 
3909ea8dc4b6Seschrock 	if (error == 0)
3910ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
3911ea8dc4b6Seschrock 
3912ea8dc4b6Seschrock 	return (error);
3913ea8dc4b6Seschrock }
3914ea8dc4b6Seschrock 
3915ea8dc4b6Seschrock static int
3916ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
3917ea8dc4b6Seschrock {
3918ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
3919ea8dc4b6Seschrock }
3920ea8dc4b6Seschrock 
3921ea8dc4b6Seschrock static int
3922ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3923ea8dc4b6Seschrock {
3924ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
3925ea8dc4b6Seschrock 	int error;
3926ea8dc4b6Seschrock 
3927ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3928ea8dc4b6Seschrock 	    &zc->zc_inject_record);
3929ea8dc4b6Seschrock 
3930ea8dc4b6Seschrock 	zc->zc_guid = id;
3931ea8dc4b6Seschrock 
3932ea8dc4b6Seschrock 	return (error);
3933ea8dc4b6Seschrock }
3934ea8dc4b6Seschrock 
3935ea8dc4b6Seschrock static int
3936ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
3937ea8dc4b6Seschrock {
3938ea8dc4b6Seschrock 	spa_t *spa;
3939ea8dc4b6Seschrock 	int error;
3940e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
3941ea8dc4b6Seschrock 
3942ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3943ea8dc4b6Seschrock 		return (error);
3944ea8dc4b6Seschrock 
3945e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3946ea8dc4b6Seschrock 	    &count);
3947ea8dc4b6Seschrock 	if (error == 0)
3948e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
3949ea8dc4b6Seschrock 	else
3950e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3951ea8dc4b6Seschrock 
3952ea8dc4b6Seschrock 	spa_close(spa, FTAG);
3953ea8dc4b6Seschrock 
3954ea8dc4b6Seschrock 	return (error);
3955ea8dc4b6Seschrock }
3956ea8dc4b6Seschrock 
3957ea8dc4b6Seschrock static int
3958ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
3959ea8dc4b6Seschrock {
3960ea8dc4b6Seschrock 	spa_t *spa;
3961ea8dc4b6Seschrock 	vdev_t *vd;
3962bb8b5132Sek 	int error;
3963ea8dc4b6Seschrock 
3964b87f3af3Sperrin 	/*
3965b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
3966b87f3af3Sperrin 	 */
3967b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
3968b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
3969b87f3af3Sperrin 	if (spa == NULL) {
3970b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
3971b87f3af3Sperrin 		return (EIO);
3972b87f3af3Sperrin 	}
3973b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3974b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
3975b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
3976b87f3af3Sperrin 	}
3977468c413aSTim Haley 	spa->spa_last_open_failed = 0;
3978b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
3979b87f3af3Sperrin 
3980c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3981468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
3982468c413aSTim Haley 	} else {
3983468c413aSTim Haley 		nvlist_t *policy;
3984468c413aSTim Haley 		nvlist_t *config = NULL;
3985468c413aSTim Haley 
3986468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
3987468c413aSTim Haley 			return (EINVAL);
3988468c413aSTim Haley 
3989468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
3990468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3991468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3992468c413aSTim Haley 			    policy, &config);
3993468c413aSTim Haley 			if (config != NULL) {
39944b964adaSGeorge Wilson 				int err;
39954b964adaSGeorge Wilson 
39964b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
39974b964adaSGeorge Wilson 					error = err;
3998468c413aSTim Haley 				nvlist_free(config);
3999468c413aSTim Haley 			}
4000468c413aSTim Haley 			nvlist_free(policy);
4001468c413aSTim Haley 		}
4002468c413aSTim Haley 	}
4003468c413aSTim Haley 
4004468c413aSTim Haley 	if (error)
4005ea8dc4b6Seschrock 		return (error);
4006ea8dc4b6Seschrock 
40078f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4008ea8dc4b6Seschrock 
4009e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4010ea8dc4b6Seschrock 		vd = NULL;
4011c5904d13Seschrock 	} else {
4012c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4013fa94a07fSbrendan 		if (vd == NULL) {
4014e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4015fa94a07fSbrendan 			spa_close(spa, FTAG);
4016fa94a07fSbrendan 			return (ENODEV);
4017fa94a07fSbrendan 		}
4018ea8dc4b6Seschrock 	}
4019ea8dc4b6Seschrock 
4020e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4021e14bb325SJeff Bonwick 
4022e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4023ea8dc4b6Seschrock 
4024e14bb325SJeff Bonwick 	/*
4025e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4026e14bb325SJeff Bonwick 	 */
402754d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
402854d692b7SGeorge Wilson 		error = EIO;
4029ea8dc4b6Seschrock 
4030ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4031ea8dc4b6Seschrock 
403254d692b7SGeorge Wilson 	return (error);
4033ea8dc4b6Seschrock }
4034ea8dc4b6Seschrock 
40353cb34c60Sahrens /*
40363cb34c60Sahrens  * inputs:
40373cb34c60Sahrens  * zc_name	name of filesystem
40383cb34c60Sahrens  * zc_value	name of origin snapshot
40393cb34c60Sahrens  *
4040681d9761SEric Taylor  * outputs:
4041681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
40423cb34c60Sahrens  */
404399653d4eSeschrock static int
404499653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
404599653d4eSeschrock {
40460b69c2f0Sahrens 	char *cp;
40470b69c2f0Sahrens 
40480b69c2f0Sahrens 	/*
40490b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
40500b69c2f0Sahrens 	 * it's easier.
40510b69c2f0Sahrens 	 */
4052e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
40530b69c2f0Sahrens 	if (cp)
40540b69c2f0Sahrens 		*cp = '\0';
4055e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
40560b69c2f0Sahrens 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4057681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
405899653d4eSeschrock }
405999653d4eSeschrock 
406014843421SMatthew Ahrens /*
406114843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
406214843421SMatthew Ahrens  *
406314843421SMatthew Ahrens  * inputs:
406414843421SMatthew Ahrens  * zc_name	name of filesystem
406514843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
406614843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
406714843421SMatthew Ahrens  * zc_guid	RID/UID/GID
406814843421SMatthew Ahrens  *
406914843421SMatthew Ahrens  * outputs:
407014843421SMatthew Ahrens  * zc_cookie	property value
407114843421SMatthew Ahrens  */
407214843421SMatthew Ahrens static int
407314843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
407414843421SMatthew Ahrens {
407514843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
407614843421SMatthew Ahrens 	int error;
407714843421SMatthew Ahrens 
407814843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
407914843421SMatthew Ahrens 		return (EINVAL);
408014843421SMatthew Ahrens 
40811412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
408214843421SMatthew Ahrens 	if (error)
408314843421SMatthew Ahrens 		return (error);
408414843421SMatthew Ahrens 
408514843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
408614843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
408714843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
408814843421SMatthew Ahrens 
408914843421SMatthew Ahrens 	return (error);
409014843421SMatthew Ahrens }
409114843421SMatthew Ahrens 
409214843421SMatthew Ahrens /*
409314843421SMatthew Ahrens  * inputs:
409414843421SMatthew Ahrens  * zc_name		name of filesystem
409514843421SMatthew Ahrens  * zc_cookie		zap cursor
409614843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
409714843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
409814843421SMatthew Ahrens  *
409914843421SMatthew Ahrens  * outputs:
410014843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
410114843421SMatthew Ahrens  * zc_cookie	zap cursor
410214843421SMatthew Ahrens  */
410314843421SMatthew Ahrens static int
410414843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
410514843421SMatthew Ahrens {
410614843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4107eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
410814843421SMatthew Ahrens 
4109eeb85002STim Haley 	if (bufsize <= 0)
4110eeb85002STim Haley 		return (ENOMEM);
4111eeb85002STim Haley 
41121412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
411314843421SMatthew Ahrens 	if (error)
411414843421SMatthew Ahrens 		return (error);
411514843421SMatthew Ahrens 
411614843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
411714843421SMatthew Ahrens 
411814843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
411914843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
412014843421SMatthew Ahrens 
412114843421SMatthew Ahrens 	if (error == 0) {
412214843421SMatthew Ahrens 		error = xcopyout(buf,
412314843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
412414843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
412514843421SMatthew Ahrens 	}
412614843421SMatthew Ahrens 	kmem_free(buf, bufsize);
412714843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
412814843421SMatthew Ahrens 
412914843421SMatthew Ahrens 	return (error);
413014843421SMatthew Ahrens }
413114843421SMatthew Ahrens 
413214843421SMatthew Ahrens /*
413314843421SMatthew Ahrens  * inputs:
413414843421SMatthew Ahrens  * zc_name		name of filesystem
413514843421SMatthew Ahrens  *
413614843421SMatthew Ahrens  * outputs:
413714843421SMatthew Ahrens  * none
413814843421SMatthew Ahrens  */
413914843421SMatthew Ahrens static int
414014843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
414114843421SMatthew Ahrens {
414214843421SMatthew Ahrens 	objset_t *os;
41431195e687SMark J Musante 	int error = 0;
414414843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
414514843421SMatthew Ahrens 
414614843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4147503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
414814843421SMatthew Ahrens 			/*
414914843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
415014843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
415114843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
415214843421SMatthew Ahrens 			 */
4153503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4154503ad85cSMatthew Ahrens 			if (error == 0)
4155503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
415614843421SMatthew Ahrens 		}
415714843421SMatthew Ahrens 		if (error == 0)
415814843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
415914843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
416014843421SMatthew Ahrens 	} else {
4161503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4162503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
416314843421SMatthew Ahrens 		if (error)
416414843421SMatthew Ahrens 			return (error);
416514843421SMatthew Ahrens 
416614843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4167503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
416814843421SMatthew Ahrens 	}
416914843421SMatthew Ahrens 
417014843421SMatthew Ahrens 	return (error);
417114843421SMatthew Ahrens }
417214843421SMatthew Ahrens 
4173ecd6cf80Smarks /*
4174ecd6cf80Smarks  * We don't want to have a hard dependency
4175ecd6cf80Smarks  * against some special symbols in sharefs
4176da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4177ecd6cf80Smarks  * the first file system is shared.
4178da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4179ecd6cf80Smarks  */
4180da6c28aaSamw int (*znfsexport_fs)(void *arg);
4181ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4182da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4183da6c28aaSamw 
4184da6c28aaSamw int zfs_nfsshare_inited;
4185da6c28aaSamw int zfs_smbshare_inited;
4186ecd6cf80Smarks 
4187ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4188ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4189da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4190ecd6cf80Smarks kmutex_t zfs_share_lock;
4191ecd6cf80Smarks 
4192da6c28aaSamw static int
4193da6c28aaSamw zfs_init_sharefs()
4194da6c28aaSamw {
4195da6c28aaSamw 	int error;
4196da6c28aaSamw 
4197da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4198da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4199da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4200da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4201da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4202da6c28aaSamw 		return (ENOSYS);
4203da6c28aaSamw 	}
4204da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4205da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4206da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4207da6c28aaSamw 		return (ENOSYS);
4208da6c28aaSamw 	}
4209da6c28aaSamw 	return (0);
4210da6c28aaSamw }
4211da6c28aaSamw 
4212ecd6cf80Smarks static int
4213ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4214ecd6cf80Smarks {
4215ecd6cf80Smarks 	int error;
4216ecd6cf80Smarks 	int opcode;
4217ecd6cf80Smarks 
4218da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4219da6c28aaSamw 	case ZFS_SHARE_NFS:
4220da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4221da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4222da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4223da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4224da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4225da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4226da6c28aaSamw 				return (ENOSYS);
4227da6c28aaSamw 			}
4228da6c28aaSamw 			if (znfsexport_fs == NULL &&
4229da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4230da6c28aaSamw 			    ddi_modsym(nfs_mod,
4231da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4232da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4233da6c28aaSamw 				return (ENOSYS);
4234da6c28aaSamw 			}
4235da6c28aaSamw 			error = zfs_init_sharefs();
4236da6c28aaSamw 			if (error) {
4237da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4238da6c28aaSamw 				return (ENOSYS);
4239da6c28aaSamw 			}
4240da6c28aaSamw 			zfs_nfsshare_inited = 1;
4241ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4242ecd6cf80Smarks 		}
4243da6c28aaSamw 		break;
4244da6c28aaSamw 	case ZFS_SHARE_SMB:
4245da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4246da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4247da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4248da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4249da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4250da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4251da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4252da6c28aaSamw 				return (ENOSYS);
4253da6c28aaSamw 			}
4254da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4255da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4256faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
4257da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4258da6c28aaSamw 				return (ENOSYS);
4259da6c28aaSamw 			}
4260da6c28aaSamw 			error = zfs_init_sharefs();
4261da6c28aaSamw 			if (error) {
4262da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4263da6c28aaSamw 				return (ENOSYS);
4264da6c28aaSamw 			}
4265da6c28aaSamw 			zfs_smbshare_inited = 1;
4266ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4267ecd6cf80Smarks 		}
4268da6c28aaSamw 		break;
4269da6c28aaSamw 	default:
4270da6c28aaSamw 		return (EINVAL);
4271da6c28aaSamw 	}
4272ecd6cf80Smarks 
4273da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4274da6c28aaSamw 	case ZFS_SHARE_NFS:
4275da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4276da6c28aaSamw 		if (error =
4277da6c28aaSamw 		    znfsexport_fs((void *)
4278da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4279da6c28aaSamw 			return (error);
4280da6c28aaSamw 		break;
4281da6c28aaSamw 	case ZFS_SHARE_SMB:
4282da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4283da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4284da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4285da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4286743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
4287da6c28aaSamw 			return (error);
4288ecd6cf80Smarks 		}
4289da6c28aaSamw 		break;
4290ecd6cf80Smarks 	}
4291ecd6cf80Smarks 
4292da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4293da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4294ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4295ecd6cf80Smarks 
4296da6c28aaSamw 	/*
4297da6c28aaSamw 	 * Add or remove share from sharetab
4298da6c28aaSamw 	 */
4299ecd6cf80Smarks 	error = zshare_fs(opcode,
4300ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4301ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4302ecd6cf80Smarks 
4303ecd6cf80Smarks 	return (error);
4304ecd6cf80Smarks 
4305ecd6cf80Smarks }
4306ecd6cf80Smarks 
4307743a77edSAlan Wright ace_t full_access[] = {
4308743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4309743a77edSAlan Wright };
4310743a77edSAlan Wright 
431199d5e173STim Haley /*
431299d5e173STim Haley  * inputs:
431399d5e173STim Haley  * zc_name		name of containing filesystem
431499d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
431599d5e173STim Haley  *
431699d5e173STim Haley  * outputs:
431799d5e173STim Haley  * zc_obj		next in-use object #
431899d5e173STim Haley  */
431999d5e173STim Haley static int
432099d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
432199d5e173STim Haley {
432299d5e173STim Haley 	objset_t *os = NULL;
432399d5e173STim Haley 	int error;
432499d5e173STim Haley 
432599d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
432699d5e173STim Haley 	if (error)
432799d5e173STim Haley 		return (error);
432899d5e173STim Haley 
432999d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
433099d5e173STim Haley 	    os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
433199d5e173STim Haley 
433299d5e173STim Haley 	dmu_objset_rele(os, FTAG);
433399d5e173STim Haley 	return (error);
433499d5e173STim Haley }
433599d5e173STim Haley 
433699d5e173STim Haley /*
433799d5e173STim Haley  * inputs:
433899d5e173STim Haley  * zc_name		name of filesystem
433999d5e173STim Haley  * zc_value		prefix name for snapshot
434099d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
434199d5e173STim Haley  *
434299d5e173STim Haley  * outputs:
434399d5e173STim Haley  */
434499d5e173STim Haley static int
434599d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
434699d5e173STim Haley {
434799d5e173STim Haley 	char *snap_name;
434899d5e173STim Haley 	int error;
434999d5e173STim Haley 
435099d5e173STim Haley 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
435199d5e173STim Haley 	    (u_longlong_t)ddi_get_lbolt64());
435299d5e173STim Haley 
435399d5e173STim Haley 	if (strlen(snap_name) >= MAXNAMELEN) {
435499d5e173STim Haley 		strfree(snap_name);
435599d5e173STim Haley 		return (E2BIG);
435699d5e173STim Haley 	}
435799d5e173STim Haley 
435899d5e173STim Haley 	error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
435999d5e173STim Haley 	    NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
436099d5e173STim Haley 	if (error != 0) {
436199d5e173STim Haley 		strfree(snap_name);
436299d5e173STim Haley 		return (error);
436399d5e173STim Haley 	}
436499d5e173STim Haley 
436599d5e173STim Haley 	(void) strcpy(zc->zc_value, snap_name);
436699d5e173STim Haley 	strfree(snap_name);
436799d5e173STim Haley 	return (0);
436899d5e173STim Haley }
436999d5e173STim Haley 
437099d5e173STim Haley /*
437199d5e173STim Haley  * inputs:
437299d5e173STim Haley  * zc_name		name of "to" snapshot
437399d5e173STim Haley  * zc_value		name of "from" snapshot
437499d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
437599d5e173STim Haley  *
437699d5e173STim Haley  * outputs:
437799d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
437899d5e173STim Haley  */
437999d5e173STim Haley static int
438099d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
438199d5e173STim Haley {
438299d5e173STim Haley 	objset_t *fromsnap;
438399d5e173STim Haley 	objset_t *tosnap;
438499d5e173STim Haley 	file_t *fp;
438599d5e173STim Haley 	offset_t off;
438699d5e173STim Haley 	int error;
438799d5e173STim Haley 
438899d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
438999d5e173STim Haley 	if (error)
439099d5e173STim Haley 		return (error);
439199d5e173STim Haley 
439299d5e173STim Haley 	error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
439399d5e173STim Haley 	if (error) {
439499d5e173STim Haley 		dmu_objset_rele(tosnap, FTAG);
439599d5e173STim Haley 		return (error);
439699d5e173STim Haley 	}
439799d5e173STim Haley 
439899d5e173STim Haley 	fp = getf(zc->zc_cookie);
439999d5e173STim Haley 	if (fp == NULL) {
440099d5e173STim Haley 		dmu_objset_rele(fromsnap, FTAG);
440199d5e173STim Haley 		dmu_objset_rele(tosnap, FTAG);
440299d5e173STim Haley 		return (EBADF);
440399d5e173STim Haley 	}
440499d5e173STim Haley 
440599d5e173STim Haley 	off = fp->f_offset;
440699d5e173STim Haley 
440799d5e173STim Haley 	error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
440899d5e173STim Haley 
440999d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
441099d5e173STim Haley 		fp->f_offset = off;
441199d5e173STim Haley 	releasef(zc->zc_cookie);
441299d5e173STim Haley 
441399d5e173STim Haley 	dmu_objset_rele(fromsnap, FTAG);
441499d5e173STim Haley 	dmu_objset_rele(tosnap, FTAG);
441599d5e173STim Haley 	return (error);
441699d5e173STim Haley }
441799d5e173STim Haley 
4418743a77edSAlan Wright /*
4419743a77edSAlan Wright  * Remove all ACL files in shares dir
4420743a77edSAlan Wright  */
4421743a77edSAlan Wright static int
4422743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
4423743a77edSAlan Wright {
4424743a77edSAlan Wright 	zap_cursor_t	zc;
4425743a77edSAlan Wright 	zap_attribute_t	zap;
4426743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4427743a77edSAlan Wright 	int error;
4428743a77edSAlan Wright 
4429743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4430743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4431743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
4432743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4433743a77edSAlan Wright 		    NULL, 0)) != 0)
4434743a77edSAlan Wright 			break;
4435743a77edSAlan Wright 	}
4436743a77edSAlan Wright 	zap_cursor_fini(&zc);
4437743a77edSAlan Wright 	return (error);
4438743a77edSAlan Wright }
4439743a77edSAlan Wright 
4440743a77edSAlan Wright static int
4441743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
4442743a77edSAlan Wright {
4443743a77edSAlan Wright 	vnode_t *vp;
4444743a77edSAlan Wright 	znode_t *dzp;
4445743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
4446743a77edSAlan Wright 	znode_t *sharedir;
4447743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
4448743a77edSAlan Wright 	nvlist_t *nvlist;
4449743a77edSAlan Wright 	char *src, *target;
4450743a77edSAlan Wright 	vattr_t vattr;
4451743a77edSAlan Wright 	vsecattr_t vsec;
4452743a77edSAlan Wright 	int error = 0;
4453743a77edSAlan Wright 
4454743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4455743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
4456743a77edSAlan Wright 		return (error);
4457743a77edSAlan Wright 
4458743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
4459743a77edSAlan Wright 
4460743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4461743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4462743a77edSAlan Wright 	    zc->zc_name) != 0)) {
4463743a77edSAlan Wright 		VN_RELE(vp);
4464743a77edSAlan Wright 		return (EINVAL);
4465743a77edSAlan Wright 	}
4466743a77edSAlan Wright 
4467743a77edSAlan Wright 	dzp = VTOZ(vp);
4468743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
4469743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
4470743a77edSAlan Wright 
44719e1320c0SMark Shellenbaum 	/*
44729e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
44739e1320c0SMark Shellenbaum 	 */
44749e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
44759e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
44769e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
44779e1320c0SMark Shellenbaum 
44789e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
44799e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
44809e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
44819e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
44829e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
44839e1320c0SMark Shellenbaum 		if (error) {
44849e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
44859e1320c0SMark Shellenbaum 		} else {
44869e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
44879e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
44889e1320c0SMark Shellenbaum 		}
44899e1320c0SMark Shellenbaum 		if (error) {
44909e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
44919e1320c0SMark Shellenbaum 			VN_RELE(vp);
44929e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
44939e1320c0SMark Shellenbaum 			return (error);
44949e1320c0SMark Shellenbaum 		}
44959e1320c0SMark Shellenbaum 	}
44969e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
44979e1320c0SMark Shellenbaum 
44989e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
4499743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
45009e1320c0SMark Shellenbaum 		VN_RELE(vp);
4501743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
4502743a77edSAlan Wright 		return (error);
4503743a77edSAlan Wright 	}
4504743a77edSAlan Wright 
4505743a77edSAlan Wright 	switch (zc->zc_cookie) {
4506743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4507743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4508743a77edSAlan Wright 		vattr.va_type = VREG;
4509743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
4510743a77edSAlan Wright 		vattr.va_uid = 0;
4511743a77edSAlan Wright 		vattr.va_gid = 0;
4512743a77edSAlan Wright 
4513743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
4514743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
4515743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
4516743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
4517743a77edSAlan Wright 
4518743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4519743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4520743a77edSAlan Wright 		if (resourcevp)
4521743a77edSAlan Wright 			VN_RELE(resourcevp);
4522743a77edSAlan Wright 		break;
4523743a77edSAlan Wright 
4524743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4525743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4526743a77edSAlan Wright 		    NULL, 0);
4527743a77edSAlan Wright 		break;
4528743a77edSAlan Wright 
4529743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4530743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
4531478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4532743a77edSAlan Wright 			VN_RELE(vp);
4533743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
4534743a77edSAlan Wright 			return (error);
4535743a77edSAlan Wright 		}
4536743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4537743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4538743a77edSAlan Wright 		    &target)) {
4539743a77edSAlan Wright 			VN_RELE(vp);
454089459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
4541743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
45421195e687SMark J Musante 			nvlist_free(nvlist);
4543743a77edSAlan Wright 			return (error);
4544743a77edSAlan Wright 		}
4545743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4546743a77edSAlan Wright 		    kcred, NULL, 0);
4547743a77edSAlan Wright 		nvlist_free(nvlist);
4548743a77edSAlan Wright 		break;
4549743a77edSAlan Wright 
4550743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4551743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
4552743a77edSAlan Wright 		break;
4553743a77edSAlan Wright 
4554743a77edSAlan Wright 	default:
4555743a77edSAlan Wright 		error = EINVAL;
4556743a77edSAlan Wright 		break;
4557743a77edSAlan Wright 	}
4558743a77edSAlan Wright 
4559743a77edSAlan Wright 	VN_RELE(vp);
4560743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
4561743a77edSAlan Wright 
4562743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
4563743a77edSAlan Wright 
4564743a77edSAlan Wright 	return (error);
4565743a77edSAlan Wright }
4566743a77edSAlan Wright 
4567842727c2SChris Kirby /*
4568842727c2SChris Kirby  * inputs:
4569c99e4bdcSChris Kirby  * zc_name		name of filesystem
4570c99e4bdcSChris Kirby  * zc_value		short name of snap
4571c99e4bdcSChris Kirby  * zc_string		user-supplied tag for this hold
4572c99e4bdcSChris Kirby  * zc_cookie		recursive flag
4573c99e4bdcSChris Kirby  * zc_temphold		set if hold is temporary
4574c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
4575a7f53a56SChris Kirby  * zc_sendobj		if non-zero, the objid for zc_name@zc_value
4576a7f53a56SChris Kirby  * zc_createtxg		if zc_sendobj is non-zero, snap must have zc_createtxg
4577842727c2SChris Kirby  *
4578842727c2SChris Kirby  * outputs:		none
4579842727c2SChris Kirby  */
4580842727c2SChris Kirby static int
4581842727c2SChris Kirby zfs_ioc_hold(zfs_cmd_t *zc)
4582842727c2SChris Kirby {
4583842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
4584a7f53a56SChris Kirby 	spa_t *spa;
4585a7f53a56SChris Kirby 	dsl_pool_t *dp;
4586a7f53a56SChris Kirby 	dsl_dataset_t *ds;
4587a7f53a56SChris Kirby 	int error;
4588a7f53a56SChris Kirby 	minor_t minor = 0;
4589842727c2SChris Kirby 
4590842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4591842727c2SChris Kirby 		return (EINVAL);
4592842727c2SChris Kirby 
4593a7f53a56SChris Kirby 	if (zc->zc_sendobj == 0) {
4594a7f53a56SChris Kirby 		return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4595a7f53a56SChris Kirby 		    zc->zc_string, recursive, zc->zc_temphold,
4596a7f53a56SChris Kirby 		    zc->zc_cleanup_fd));
4597a7f53a56SChris Kirby 	}
4598a7f53a56SChris Kirby 
4599a7f53a56SChris Kirby 	if (recursive)
4600a7f53a56SChris Kirby 		return (EINVAL);
4601a7f53a56SChris Kirby 
4602a7f53a56SChris Kirby 	error = spa_open(zc->zc_name, &spa, FTAG);
4603a7f53a56SChris Kirby 	if (error)
4604a7f53a56SChris Kirby 		return (error);
4605a7f53a56SChris Kirby 
4606a7f53a56SChris Kirby 	dp = spa_get_dsl(spa);
4607a7f53a56SChris Kirby 	rw_enter(&dp->dp_config_rwlock, RW_READER);
4608a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4609a7f53a56SChris Kirby 	rw_exit(&dp->dp_config_rwlock);
4610a7f53a56SChris Kirby 	spa_close(spa, FTAG);
4611a7f53a56SChris Kirby 	if (error)
4612a7f53a56SChris Kirby 		return (error);
4613a7f53a56SChris Kirby 
4614a7f53a56SChris Kirby 	/*
4615a7f53a56SChris Kirby 	 * Until we have a hold on this snapshot, it's possible that
4616a7f53a56SChris Kirby 	 * zc_sendobj could've been destroyed and reused as part
4617a7f53a56SChris Kirby 	 * of a later txg.  Make sure we're looking at the right object.
4618a7f53a56SChris Kirby 	 */
4619a7f53a56SChris Kirby 	if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4620a7f53a56SChris Kirby 		dsl_dataset_rele(ds, FTAG);
4621a7f53a56SChris Kirby 		return (ENOENT);
4622a7f53a56SChris Kirby 	}
4623a7f53a56SChris Kirby 
4624a7f53a56SChris Kirby 	if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4625a7f53a56SChris Kirby 		error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4626a7f53a56SChris Kirby 		if (error) {
4627a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
4628a7f53a56SChris Kirby 			return (error);
4629a7f53a56SChris Kirby 		}
4630a7f53a56SChris Kirby 	}
4631a7f53a56SChris Kirby 
4632a7f53a56SChris Kirby 	error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4633a7f53a56SChris Kirby 	    zc->zc_temphold);
4634a7f53a56SChris Kirby 	if (minor != 0) {
4635a7f53a56SChris Kirby 		if (error == 0) {
4636a7f53a56SChris Kirby 			dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4637a7f53a56SChris Kirby 			    minor);
4638a7f53a56SChris Kirby 		}
4639a7f53a56SChris Kirby 		zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4640a7f53a56SChris Kirby 	}
4641a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
4642a7f53a56SChris Kirby 
4643a7f53a56SChris Kirby 	return (error);
4644842727c2SChris Kirby }
4645842727c2SChris Kirby 
4646842727c2SChris Kirby /*
4647842727c2SChris Kirby  * inputs:
4648c99e4bdcSChris Kirby  * zc_name	name of dataset from which we're releasing a user hold
4649842727c2SChris Kirby  * zc_value	short name of snap
4650c99e4bdcSChris Kirby  * zc_string	user-supplied tag for this hold
4651842727c2SChris Kirby  * zc_cookie	recursive flag
4652842727c2SChris Kirby  *
4653c99e4bdcSChris Kirby  * outputs:	none
4654842727c2SChris Kirby  */
4655842727c2SChris Kirby static int
4656842727c2SChris Kirby zfs_ioc_release(zfs_cmd_t *zc)
4657842727c2SChris Kirby {
4658842727c2SChris Kirby 	boolean_t recursive = zc->zc_cookie;
4659842727c2SChris Kirby 
4660842727c2SChris Kirby 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4661842727c2SChris Kirby 		return (EINVAL);
4662842727c2SChris Kirby 
4663842727c2SChris Kirby 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4664842727c2SChris Kirby 	    zc->zc_string, recursive));
4665842727c2SChris Kirby }
4666842727c2SChris Kirby 
4667842727c2SChris Kirby /*
4668842727c2SChris Kirby  * inputs:
4669842727c2SChris Kirby  * zc_name		name of filesystem
4670842727c2SChris Kirby  *
4671842727c2SChris Kirby  * outputs:
4672842727c2SChris Kirby  * zc_nvlist_src{_size}	nvlist of snapshot holds
4673842727c2SChris Kirby  */
4674842727c2SChris Kirby static int
4675842727c2SChris Kirby zfs_ioc_get_holds(zfs_cmd_t *zc)
4676842727c2SChris Kirby {
4677842727c2SChris Kirby 	nvlist_t *nvp;
4678842727c2SChris Kirby 	int error;
4679842727c2SChris Kirby 
4680842727c2SChris Kirby 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4681842727c2SChris Kirby 		error = put_nvlist(zc, nvp);
4682842727c2SChris Kirby 		nvlist_free(nvp);
4683842727c2SChris Kirby 	}
4684842727c2SChris Kirby 
4685842727c2SChris Kirby 	return (error);
4686842727c2SChris Kirby }
4687842727c2SChris Kirby 
4688*19b94df9SMatthew Ahrens /*
4689*19b94df9SMatthew Ahrens  * inputs:
4690*19b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
4691*19b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
4692*19b94df9SMatthew Ahrens  *
4693*19b94df9SMatthew Ahrens  * outputs:
4694*19b94df9SMatthew Ahrens  * zc_cookie		space in bytes
4695*19b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
4696*19b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
4697*19b94df9SMatthew Ahrens  */
4698*19b94df9SMatthew Ahrens static int
4699*19b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
4700*19b94df9SMatthew Ahrens {
4701*19b94df9SMatthew Ahrens 	int error;
4702*19b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
4703*19b94df9SMatthew Ahrens 
4704*19b94df9SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4705*19b94df9SMatthew Ahrens 	if (error != 0)
4706*19b94df9SMatthew Ahrens 		return (error);
4707*19b94df9SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4708*19b94df9SMatthew Ahrens 	if (error != 0) {
4709*19b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
4710*19b94df9SMatthew Ahrens 		return (error);
4711*19b94df9SMatthew Ahrens 	}
4712*19b94df9SMatthew Ahrens 
4713*19b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4714*19b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
4715*19b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
4716*19b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
4717*19b94df9SMatthew Ahrens 	return (error);
4718*19b94df9SMatthew Ahrens }
4719*19b94df9SMatthew Ahrens 
4720*19b94df9SMatthew Ahrens /*
4721*19b94df9SMatthew Ahrens  * inputs:
4722*19b94df9SMatthew Ahrens  * zc_name		full name of last snapshot
4723*19b94df9SMatthew Ahrens  * zc_value		full name of first snapshot
4724*19b94df9SMatthew Ahrens  *
4725*19b94df9SMatthew Ahrens  * outputs:
4726*19b94df9SMatthew Ahrens  * zc_cookie		space in bytes
4727*19b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
4728*19b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
4729*19b94df9SMatthew Ahrens  */
4730*19b94df9SMatthew Ahrens static int
4731*19b94df9SMatthew Ahrens zfs_ioc_space_snaps(zfs_cmd_t *zc)
4732*19b94df9SMatthew Ahrens {
4733*19b94df9SMatthew Ahrens 	int error;
4734*19b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
4735*19b94df9SMatthew Ahrens 
4736*19b94df9SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4737*19b94df9SMatthew Ahrens 	if (error != 0)
4738*19b94df9SMatthew Ahrens 		return (error);
4739*19b94df9SMatthew Ahrens 	error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4740*19b94df9SMatthew Ahrens 	if (error != 0) {
4741*19b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
4742*19b94df9SMatthew Ahrens 		return (error);
4743*19b94df9SMatthew Ahrens 	}
4744*19b94df9SMatthew Ahrens 
4745*19b94df9SMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4746*19b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
4747*19b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
4748*19b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
4749*19b94df9SMatthew Ahrens 	return (error);
4750*19b94df9SMatthew Ahrens }
4751*19b94df9SMatthew Ahrens 
4752ecd6cf80Smarks /*
47532a6b87f0Sek  * pool create, destroy, and export don't log the history as part of
47542a6b87f0Sek  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
47552a6b87f0Sek  * do the logging of those commands.
4756ecd6cf80Smarks  */
4757fa9e4066Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = {
475854d692b7SGeorge Wilson 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4759f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
476054d692b7SGeorge Wilson 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4761f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
476254d692b7SGeorge Wilson 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4763f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
476454d692b7SGeorge Wilson 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4765f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
476654d692b7SGeorge Wilson 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
4767f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
476854d692b7SGeorge Wilson 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4769f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
477054d692b7SGeorge Wilson 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4771f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
47723f9d6ad7SLin Ling 	{ zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4773f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
477454d692b7SGeorge Wilson 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4775f9af39baSGeorge Wilson 	    POOL_CHECK_READONLY },
477654d692b7SGeorge Wilson 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
4777f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
477854d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4779f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
478054d692b7SGeorge Wilson 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4781f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
478254d692b7SGeorge Wilson 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4783f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
478454d692b7SGeorge Wilson 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4785f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
478654d692b7SGeorge Wilson 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4787f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
478854d692b7SGeorge Wilson 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4789f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
479054d692b7SGeorge Wilson 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4791f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
47926809eb4eSEric Schrock 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4793f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
479454d692b7SGeorge Wilson 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4795f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED },
479654d692b7SGeorge Wilson 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4797f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
479854d692b7SGeorge Wilson 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4799f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED },
480054d692b7SGeorge Wilson 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4801f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED },
4802f9af39baSGeorge Wilson 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4803f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4804f9af39baSGeorge Wilson 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4805f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
480654d692b7SGeorge Wilson 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4807f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
480854d692b7SGeorge Wilson 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4809f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4810f9af39baSGeorge Wilson 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE,
4811f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4812f9af39baSGeorge Wilson 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4813f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4814*19b94df9SMatthew Ahrens 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE,
4815f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
481654d692b7SGeorge Wilson 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
4817f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
481854d692b7SGeorge Wilson 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4819f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
482054d692b7SGeorge Wilson 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4821f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
482254d692b7SGeorge Wilson 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4823f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
4824f9af39baSGeorge Wilson 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4825f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
482654d692b7SGeorge Wilson 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4827f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
482854d692b7SGeorge Wilson 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4829f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
483099d5e173STim Haley 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4831f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
483299d5e173STim Haley 	{ zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4833f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED },
483454d692b7SGeorge Wilson 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4835f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
483654d692b7SGeorge Wilson 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4837f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
483854d692b7SGeorge Wilson 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4839f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
484054d692b7SGeorge Wilson 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4841f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
4842f9af39baSGeorge Wilson 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4843f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
484454d692b7SGeorge Wilson 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4845f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
484654d692b7SGeorge Wilson 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4847f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
4848f9af39baSGeorge Wilson 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4849f9af39baSGeorge Wilson 	    B_FALSE, POOL_CHECK_NONE },
4850f9af39baSGeorge Wilson 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4851f9af39baSGeorge Wilson 	    B_FALSE, POOL_CHECK_NONE },
485214843421SMatthew Ahrens 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4853f9af39baSGeorge Wilson 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4854f9af39baSGeorge Wilson 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4855f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4856842727c2SChris Kirby 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4857f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4858842727c2SChris Kirby 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4859f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED },
486092241e0bSTom Erickson 	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4861f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
48621195e687SMark J Musante 	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4863f9af39baSGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
486499d5e173STim Haley 	{ zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4865f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
4866f9af39baSGeorge Wilson 	{ zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4867f9af39baSGeorge Wilson 	    POOL_CHECK_NONE },
486899d5e173STim Haley 	{ zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4869f9af39baSGeorge Wilson 	    B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
487099d5e173STim Haley 	{ zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4871e9103aaeSGarrett D'Amore 	    POOL_CHECK_SUSPENDED },
4872*19b94df9SMatthew Ahrens 	{ zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4873*19b94df9SMatthew Ahrens 	    POOL_CHECK_SUSPENDED },
4874*19b94df9SMatthew Ahrens 	{ zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4875*19b94df9SMatthew Ahrens 	    POOL_CHECK_SUSPENDED },
4876*19b94df9SMatthew Ahrens 	{ zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive,
4877*19b94df9SMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4878e9103aaeSGarrett D'Amore 	{ zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4879e9103aaeSGarrett D'Amore 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY }
4880fa9e4066Sahrens };
4881fa9e4066Sahrens 
488254d692b7SGeorge Wilson int
4883f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4884f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
488554d692b7SGeorge Wilson {
488654d692b7SGeorge Wilson 	spa_t *spa;
488754d692b7SGeorge Wilson 	int error;
488854d692b7SGeorge Wilson 
488954d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
489054d692b7SGeorge Wilson 
4891f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
4892f9af39baSGeorge Wilson 		return (0);
4893f9af39baSGeorge Wilson 
489414843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
489554d692b7SGeorge Wilson 	if (error == 0) {
4896f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
489754d692b7SGeorge Wilson 			error = EAGAIN;
4898f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4899f9af39baSGeorge Wilson 			error = EROFS;
490054d692b7SGeorge Wilson 		spa_close(spa, FTAG);
490154d692b7SGeorge Wilson 	}
490254d692b7SGeorge Wilson 	return (error);
490354d692b7SGeorge Wilson }
490454d692b7SGeorge Wilson 
4905c99e4bdcSChris Kirby /*
4906c99e4bdcSChris Kirby  * Find a free minor number.
4907c99e4bdcSChris Kirby  */
4908c99e4bdcSChris Kirby minor_t
4909c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
4910c99e4bdcSChris Kirby {
4911c99e4bdcSChris Kirby 	static minor_t last_minor;
4912c99e4bdcSChris Kirby 	minor_t m;
4913c99e4bdcSChris Kirby 
4914c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4915c99e4bdcSChris Kirby 
4916c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
4917c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
4918c99e4bdcSChris Kirby 			m = 1;
4919c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
4920c99e4bdcSChris Kirby 			last_minor = m;
4921c99e4bdcSChris Kirby 			return (m);
4922c99e4bdcSChris Kirby 		}
4923c99e4bdcSChris Kirby 	}
4924c99e4bdcSChris Kirby 
4925c99e4bdcSChris Kirby 	return (0);
4926c99e4bdcSChris Kirby }
4927c99e4bdcSChris Kirby 
4928c99e4bdcSChris Kirby static int
4929c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
4930c99e4bdcSChris Kirby {
4931c99e4bdcSChris Kirby 	minor_t minor;
4932c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
4933c99e4bdcSChris Kirby 
4934c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4935c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
4936c99e4bdcSChris Kirby 
4937c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
4938c99e4bdcSChris Kirby 	if (minor == 0)
4939c99e4bdcSChris Kirby 		return (ENXIO);
4940c99e4bdcSChris Kirby 
4941c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
4942c99e4bdcSChris Kirby 		return (EAGAIN);
4943c99e4bdcSChris Kirby 
4944c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
4945c99e4bdcSChris Kirby 
4946c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
4947c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
4948c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
4949c99e4bdcSChris Kirby 
4950c99e4bdcSChris Kirby 	return (0);
4951c99e4bdcSChris Kirby }
4952c99e4bdcSChris Kirby 
4953c99e4bdcSChris Kirby static void
4954c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
4955c99e4bdcSChris Kirby {
4956c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4957c99e4bdcSChris Kirby 
4958c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
4959c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
4960c99e4bdcSChris Kirby }
4961c99e4bdcSChris Kirby 
4962c99e4bdcSChris Kirby void *
4963c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
4964c99e4bdcSChris Kirby {
4965c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
4966c99e4bdcSChris Kirby 
4967c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
4968c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
4969c99e4bdcSChris Kirby 		return (NULL);
4970c99e4bdcSChris Kirby 
4971c99e4bdcSChris Kirby 	return (zp->zss_data);
4972c99e4bdcSChris Kirby }
4973c99e4bdcSChris Kirby 
4974c99e4bdcSChris Kirby static int
4975c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
4976c99e4bdcSChris Kirby {
4977c99e4bdcSChris Kirby 	int error = 0;
4978c99e4bdcSChris Kirby 
4979c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
4980c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
4981c99e4bdcSChris Kirby 
4982c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
4983c99e4bdcSChris Kirby 	if (flag & FEXCL) {
4984c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
4985c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
4986c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
4987c99e4bdcSChris Kirby 	}
4988c99e4bdcSChris Kirby 
4989c99e4bdcSChris Kirby 	return (error);
4990c99e4bdcSChris Kirby }
4991c99e4bdcSChris Kirby 
4992c99e4bdcSChris Kirby static int
4993c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
4994c99e4bdcSChris Kirby {
4995c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
4996c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
4997c99e4bdcSChris Kirby 
4998c99e4bdcSChris Kirby 	if (minor == 0)
4999c99e4bdcSChris Kirby 		return (0);
5000c99e4bdcSChris Kirby 
5001c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5002c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5003c99e4bdcSChris Kirby 	if (zo == NULL) {
5004c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5005c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5006c99e4bdcSChris Kirby 	}
5007c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5008c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5009c99e4bdcSChris Kirby 
5010c99e4bdcSChris Kirby 	return (0);
5011c99e4bdcSChris Kirby }
5012c99e4bdcSChris Kirby 
5013fa9e4066Sahrens static int
5014fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5015fa9e4066Sahrens {
5016fa9e4066Sahrens 	zfs_cmd_t *zc;
5017fa9e4066Sahrens 	uint_t vec;
50181d452cf5Sahrens 	int error, rc;
5019c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5020fa9e4066Sahrens 
5021c99e4bdcSChris Kirby 	if (minor != 0 &&
5022c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5023fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5024fa9e4066Sahrens 
5025fa9e4066Sahrens 	vec = cmd - ZFS_IOC;
502691ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5027fa9e4066Sahrens 
5028fa9e4066Sahrens 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5029fa9e4066Sahrens 		return (EINVAL);
5030fa9e4066Sahrens 
5031fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5032fa9e4066Sahrens 
5033478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
50346e27f868SSam Falkner 	if (error != 0)
50356e27f868SSam Falkner 		error = EFAULT;
5036fa9e4066Sahrens 
5037681d9761SEric Taylor 	if ((error == 0) && !(flag & FKIOCTL))
5038ecd6cf80Smarks 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
5039fa9e4066Sahrens 
5040fa9e4066Sahrens 	/*
5041fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
5042fa9e4066Sahrens 	 * the lower layers.
5043fa9e4066Sahrens 	 */
5044fa9e4066Sahrens 	if (error == 0) {
5045fa9e4066Sahrens 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5046478ed9adSEric Taylor 		zc->zc_iflags = flag & FKIOCTL;
5047fa9e4066Sahrens 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
5048e7437265Sahrens 		case POOL_NAME:
5049fa9e4066Sahrens 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5050fa9e4066Sahrens 				error = EINVAL;
5051f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
5052f9af39baSGeorge Wilson 			    zfs_ioc_vec[vec].zvec_namecheck,
5053f9af39baSGeorge Wilson 			    zfs_ioc_vec[vec].zvec_pool_check);
5054fa9e4066Sahrens 			break;
5055fa9e4066Sahrens 
5056e7437265Sahrens 		case DATASET_NAME:
5057fa9e4066Sahrens 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5058fa9e4066Sahrens 				error = EINVAL;
5059f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
5060f9af39baSGeorge Wilson 			    zfs_ioc_vec[vec].zvec_namecheck,
5061f9af39baSGeorge Wilson 			    zfs_ioc_vec[vec].zvec_pool_check);
5062fa9e4066Sahrens 			break;
50635ad82045Snd 
5064e7437265Sahrens 		case NO_NAME:
50655ad82045Snd 			break;
5066fa9e4066Sahrens 		}
5067fa9e4066Sahrens 	}
5068fa9e4066Sahrens 
5069fa9e4066Sahrens 	if (error == 0)
5070fa9e4066Sahrens 		error = zfs_ioc_vec[vec].zvec_func(zc);
5071fa9e4066Sahrens 
5072478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5073ecd6cf80Smarks 	if (error == 0) {
50746e27f868SSam Falkner 		if (rc != 0)
50756e27f868SSam Falkner 			error = EFAULT;
507614843421SMatthew Ahrens 		if (zfs_ioc_vec[vec].zvec_his_log)
5077ecd6cf80Smarks 			zfs_log_history(zc);
5078ecd6cf80Smarks 	}
5079fa9e4066Sahrens 
5080fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
5081fa9e4066Sahrens 	return (error);
5082fa9e4066Sahrens }
5083fa9e4066Sahrens 
5084fa9e4066Sahrens static int
5085fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5086fa9e4066Sahrens {
5087fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
5088fa9e4066Sahrens 		return (DDI_FAILURE);
5089fa9e4066Sahrens 
5090fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5091fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
5092fa9e4066Sahrens 		return (DDI_FAILURE);
5093fa9e4066Sahrens 
5094fa9e4066Sahrens 	zfs_dip = dip;
5095fa9e4066Sahrens 
5096fa9e4066Sahrens 	ddi_report_dev(dip);
5097fa9e4066Sahrens 
5098fa9e4066Sahrens 	return (DDI_SUCCESS);
5099fa9e4066Sahrens }
5100fa9e4066Sahrens 
5101fa9e4066Sahrens static int
5102fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5103fa9e4066Sahrens {
5104fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
5105fa9e4066Sahrens 		return (DDI_FAILURE);
5106fa9e4066Sahrens 
5107fa9e4066Sahrens 	if (cmd != DDI_DETACH)
5108fa9e4066Sahrens 		return (DDI_FAILURE);
5109fa9e4066Sahrens 
5110fa9e4066Sahrens 	zfs_dip = NULL;
5111fa9e4066Sahrens 
5112fa9e4066Sahrens 	ddi_prop_remove_all(dip);
5113fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
5114fa9e4066Sahrens 
5115fa9e4066Sahrens 	return (DDI_SUCCESS);
5116fa9e4066Sahrens }
5117fa9e4066Sahrens 
5118fa9e4066Sahrens /*ARGSUSED*/
5119fa9e4066Sahrens static int
5120fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5121fa9e4066Sahrens {
5122fa9e4066Sahrens 	switch (infocmd) {
5123fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
5124fa9e4066Sahrens 		*result = zfs_dip;
5125fa9e4066Sahrens 		return (DDI_SUCCESS);
5126fa9e4066Sahrens 
5127fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
5128a0965f35Sbonwick 		*result = (void *)0;
5129fa9e4066Sahrens 		return (DDI_SUCCESS);
5130fa9e4066Sahrens 	}
5131fa9e4066Sahrens 
5132fa9e4066Sahrens 	return (DDI_FAILURE);
5133fa9e4066Sahrens }
5134fa9e4066Sahrens 
5135fa9e4066Sahrens /*
5136fa9e4066Sahrens  * OK, so this is a little weird.
5137fa9e4066Sahrens  *
5138fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
5139fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5140fa9e4066Sahrens  *
5141fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
5142fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
5143fa9e4066Sahrens  */
5144fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
5145c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
5146c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
5147fa9e4066Sahrens 	zvol_strategy,	/* strategy */
5148fa9e4066Sahrens 	nodev,		/* print */
5149e7cbe64fSgw 	zvol_dump,	/* dump */
5150fa9e4066Sahrens 	zvol_read,	/* read */
5151fa9e4066Sahrens 	zvol_write,	/* write */
5152fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
5153fa9e4066Sahrens 	nodev,		/* devmap */
5154fa9e4066Sahrens 	nodev,		/* mmap */
5155fa9e4066Sahrens 	nodev,		/* segmap */
5156fa9e4066Sahrens 	nochpoll,	/* poll */
5157fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
5158fa9e4066Sahrens 	NULL,		/* streamtab */
5159fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
5160fa9e4066Sahrens 	CB_REV,		/* version */
5161feb08c6bSbillm 	nodev,		/* async read */
5162feb08c6bSbillm 	nodev,		/* async write */
5163fa9e4066Sahrens };
5164fa9e4066Sahrens 
5165fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
5166fa9e4066Sahrens 	DEVO_REV,	/* version */
5167fa9e4066Sahrens 	0,		/* refcnt */
5168fa9e4066Sahrens 	zfs_info,	/* info */
5169fa9e4066Sahrens 	nulldev,	/* identify */
5170fa9e4066Sahrens 	nulldev,	/* probe */
5171fa9e4066Sahrens 	zfs_attach,	/* attach */
5172fa9e4066Sahrens 	zfs_detach,	/* detach */
5173fa9e4066Sahrens 	nodev,		/* reset */
5174fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
517519397407SSherry Moore 	NULL,		/* no bus operations */
517619397407SSherry Moore 	NULL,		/* power */
517719397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
5178fa9e4066Sahrens };
5179fa9e4066Sahrens 
5180fa9e4066Sahrens static struct modldrv zfs_modldrv = {
518119397407SSherry Moore 	&mod_driverops,
518219397407SSherry Moore 	"ZFS storage pool",
518319397407SSherry Moore 	&zfs_dev_ops
5184fa9e4066Sahrens };
5185fa9e4066Sahrens 
5186fa9e4066Sahrens static struct modlinkage modlinkage = {
5187fa9e4066Sahrens 	MODREV_1,
5188fa9e4066Sahrens 	(void *)&zfs_modlfs,
5189fa9e4066Sahrens 	(void *)&zfs_modldrv,
5190fa9e4066Sahrens 	NULL
5191fa9e4066Sahrens };
5192fa9e4066Sahrens 
5193ec533521Sfr 
5194ec533521Sfr uint_t zfs_fsyncer_key;
5195f18faf3fSek extern uint_t rrw_tsd_key;
5196ec533521Sfr 
5197fa9e4066Sahrens int
5198fa9e4066Sahrens _init(void)
5199fa9e4066Sahrens {
5200fa9e4066Sahrens 	int error;
5201fa9e4066Sahrens 
5202a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
5203a0965f35Sbonwick 	zfs_init();
5204a0965f35Sbonwick 	zvol_init();
5205a0965f35Sbonwick 
5206a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
5207a0965f35Sbonwick 		zvol_fini();
5208a0965f35Sbonwick 		zfs_fini();
5209a0965f35Sbonwick 		spa_fini();
5210fa9e4066Sahrens 		return (error);
5211a0965f35Sbonwick 	}
5212fa9e4066Sahrens 
5213ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
5214f18faf3fSek 	tsd_create(&rrw_tsd_key, NULL);
5215ec533521Sfr 
5216fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5217fa9e4066Sahrens 	ASSERT(error == 0);
5218ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5219fa9e4066Sahrens 
5220fa9e4066Sahrens 	return (0);
5221fa9e4066Sahrens }
5222fa9e4066Sahrens 
5223fa9e4066Sahrens int
5224fa9e4066Sahrens _fini(void)
5225fa9e4066Sahrens {
5226fa9e4066Sahrens 	int error;
5227fa9e4066Sahrens 
5228ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5229fa9e4066Sahrens 		return (EBUSY);
5230fa9e4066Sahrens 
5231fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
5232fa9e4066Sahrens 		return (error);
5233fa9e4066Sahrens 
5234fa9e4066Sahrens 	zvol_fini();
5235fa9e4066Sahrens 	zfs_fini();
5236fa9e4066Sahrens 	spa_fini();
5237da6c28aaSamw 	if (zfs_nfsshare_inited)
5238ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
5239da6c28aaSamw 	if (zfs_smbshare_inited)
5240da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
5241da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
5242ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
5243fa9e4066Sahrens 
5244ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
5245fa9e4066Sahrens 	ldi_ident_release(zfs_li);
5246fa9e4066Sahrens 	zfs_li = NULL;
5247ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
5248fa9e4066Sahrens 
5249fa9e4066Sahrens 	return (error);
5250fa9e4066Sahrens }
5251fa9e4066Sahrens 
5252fa9e4066Sahrens int
5253fa9e4066Sahrens _info(struct modinfo *modinfop)
5254fa9e4066Sahrens {
5255fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
5256fa9e4066Sahrens }
5257