xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 2acef22d)
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  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
241df56adaSMartin Matuska  * Portions Copyright 2011 Martin Matuska
25e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
264e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
28a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
304445fffbSMatthew Ahrens  */
314445fffbSMatthew Ahrens 
324445fffbSMatthew Ahrens /*
334445fffbSMatthew Ahrens  * ZFS ioctls.
344445fffbSMatthew Ahrens  *
354445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
364445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
374445fffbSMatthew Ahrens  *
384445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
394445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
404445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
414445fffbSMatthew Ahrens  *
424445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
434445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
444445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
454445fffbSMatthew Ahrens  *
464445fffbSMatthew Ahrens  * The registration arguments are as follows:
474445fffbSMatthew Ahrens  *
484445fffbSMatthew Ahrens  * const char *name
494445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
504445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
514445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
524445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
534445fffbSMatthew Ahrens  *
544445fffbSMatthew Ahrens  * zfs_ioc_t ioc
554445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
564445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
574445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
584445fffbSMatthew Ahrens  *
594445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
604445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
614445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
624445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
634445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
644445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
654445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
664445fffbSMatthew Ahrens  *
674445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
684445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
694445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
704445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
714445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
724445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
734445fffbSMatthew Ahrens  *   doesn't have invalid characters).
744445fffbSMatthew Ahrens  *
754445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
764445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
774445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
784445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
794445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
804445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
814445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
824445fffbSMatthew Ahrens  *
834445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
844445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
854445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
864445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
874445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
884445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
894445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
904445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
914445fffbSMatthew Ahrens  *
924445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
934445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
944445fffbSMatthew Ahrens  *
954445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
964445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
974445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
984445fffbSMatthew Ahrens  *   will be called with the following arguments:
994445fffbSMatthew Ahrens  *
1004445fffbSMatthew Ahrens  *   const char *name
1014445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1024445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1034445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1044445fffbSMatthew Ahrens  *
1054445fffbSMatthew Ahrens  *   nvlist_t *innvl
1064445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1074445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1084445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1094445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1104445fffbSMatthew Ahrens  *
1114445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1124445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1134445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1144445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1154445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1164445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1174445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1184445fffbSMatthew Ahrens  *
1194445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1204445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1214445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1224445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1234445fffbSMatthew Ahrens  *         the ioctl again.
1244445fffbSMatthew Ahrens  *
1254445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1264445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1274445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1284445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1294445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1304445fffbSMatthew Ahrens  *         deserialization failing.
131e9103aaeSGarrett D'Amore  */
132fa9e4066Sahrens 
133fa9e4066Sahrens #include <sys/types.h>
134fa9e4066Sahrens #include <sys/param.h>
135fa9e4066Sahrens #include <sys/errno.h>
136fa9e4066Sahrens #include <sys/uio.h>
137fa9e4066Sahrens #include <sys/buf.h>
138fa9e4066Sahrens #include <sys/modctl.h>
139fa9e4066Sahrens #include <sys/open.h>
140fa9e4066Sahrens #include <sys/file.h>
141fa9e4066Sahrens #include <sys/kmem.h>
142fa9e4066Sahrens #include <sys/conf.h>
143fa9e4066Sahrens #include <sys/cmn_err.h>
144fa9e4066Sahrens #include <sys/stat.h>
145fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1464201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
147da6c28aaSamw #include <sys/zfs_znode.h>
148fa9e4066Sahrens #include <sys/zap.h>
149fa9e4066Sahrens #include <sys/spa.h>
150b1b8ab34Slling #include <sys/spa_impl.h>
151fa9e4066Sahrens #include <sys/vdev.h>
1524201a95eSRic Aleshire #include <sys/priv_impl.h>
153fa9e4066Sahrens #include <sys/dmu.h>
154fa9e4066Sahrens #include <sys/dsl_dir.h>
155fa9e4066Sahrens #include <sys/dsl_dataset.h>
156fa9e4066Sahrens #include <sys/dsl_prop.h>
157ecd6cf80Smarks #include <sys/dsl_deleg.h>
158ecd6cf80Smarks #include <sys/dmu_objset.h>
1594e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1603b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
161fa9e4066Sahrens #include <sys/ddi.h>
162fa9e4066Sahrens #include <sys/sunddi.h>
163fa9e4066Sahrens #include <sys/sunldi.h>
164fa9e4066Sahrens #include <sys/policy.h>
165fa9e4066Sahrens #include <sys/zone.h>
166fa9e4066Sahrens #include <sys/nvpair.h>
167fa9e4066Sahrens #include <sys/pathname.h>
168fa9e4066Sahrens #include <sys/mount.h>
169fa9e4066Sahrens #include <sys/sdt.h>
170fa9e4066Sahrens #include <sys/fs/zfs.h>
171fa9e4066Sahrens #include <sys/zfs_ctldir.h>
172da6c28aaSamw #include <sys/zfs_dir.h>
173c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
174a2eea2e1Sahrens #include <sys/zvol.h>
1753f9d6ad7SLin Ling #include <sys/dsl_scan.h>
176ecd6cf80Smarks #include <sharefs/share.h>
177f18faf3fSek #include <sys/dmu_objset.h>
1783b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1793b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1803b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
181a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
182fa9e4066Sahrens 
183fa9e4066Sahrens #include "zfs_namecheck.h"
184e9dbad6fSeschrock #include "zfs_prop.h"
185ecd6cf80Smarks #include "zfs_deleg.h"
1860a586ceaSMark Shellenbaum #include "zfs_comutil.h"
187fa9e4066Sahrens 
188fa9e4066Sahrens extern struct modlfs zfs_modlfs;
189fa9e4066Sahrens 
190fa9e4066Sahrens extern void zfs_init(void);
191fa9e4066Sahrens extern void zfs_fini(void);
192fa9e4066Sahrens 
193fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
194fa9e4066Sahrens dev_info_t *zfs_dip;
195fa9e4066Sahrens 
1964445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
1974445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
1984445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
1994445fffbSMatthew Ahrens 
2004445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2014445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2024445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
203fa9e4066Sahrens 
20454d692b7SGeorge Wilson typedef enum {
20554d692b7SGeorge Wilson 	NO_NAME,
20654d692b7SGeorge Wilson 	POOL_NAME,
20754d692b7SGeorge Wilson 	DATASET_NAME
20854d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
20954d692b7SGeorge Wilson 
210f9af39baSGeorge Wilson typedef enum {
211f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
212f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2134445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
214f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
215f9af39baSGeorge Wilson 
216fa9e4066Sahrens typedef struct zfs_ioc_vec {
2174445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
218fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
219fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
22054d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2214445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
222f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2234445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2244445fffbSMatthew Ahrens 	const char		*zvec_name;
225fa9e4066Sahrens } zfs_ioc_vec_t;
226fa9e4066Sahrens 
22714843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
22814843421SMatthew Ahrens static const char *userquota_perms[] = {
22914843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23014843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23114843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23214843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23314843421SMatthew Ahrens };
23414843421SMatthew Ahrens 
23514843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
23692241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
23792241e0bSTom Erickson     cred_t *cr);
23892241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
23992241e0bSTom Erickson     nvlist_t **errors);
2400a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2410a48a24eStimh     boolean_t *);
2424445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2434445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2440a48a24eStimh 
245*2acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
246a6f561b4SSašo Kiselkov 
247fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
248fa9e4066Sahrens void
249fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
250fa9e4066Sahrens {
251fa9e4066Sahrens 	const char *newfile;
2523f9d6ad7SLin Ling 	char buf[512];
253fa9e4066Sahrens 	va_list adx;
254fa9e4066Sahrens 
255fa9e4066Sahrens 	/*
256fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
257fa9e4066Sahrens 	 */
258fa9e4066Sahrens 	newfile = strrchr(file, '/');
259fa9e4066Sahrens 	if (newfile != NULL) {
260fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
261fa9e4066Sahrens 	} else {
262fa9e4066Sahrens 		newfile = file;
263fa9e4066Sahrens 	}
264fa9e4066Sahrens 
265fa9e4066Sahrens 	va_start(adx, fmt);
266fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
267fa9e4066Sahrens 	va_end(adx);
268fa9e4066Sahrens 
269fa9e4066Sahrens 	/*
270fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
271fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
272fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
273fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
274fa9e4066Sahrens 	 * arg0 = file name
275fa9e4066Sahrens 	 * arg1 = function name
276fa9e4066Sahrens 	 * arg2 = line number
277fa9e4066Sahrens 	 * arg3 = message
278fa9e4066Sahrens 	 */
279fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
280fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
281fa9e4066Sahrens }
282fa9e4066Sahrens 
283ecd6cf80Smarks static void
284228975ccSek history_str_free(char *buf)
285228975ccSek {
286228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
287228975ccSek }
288228975ccSek 
289228975ccSek static char *
290228975ccSek history_str_get(zfs_cmd_t *zc)
291ecd6cf80Smarks {
29240feaa91Sahrens 	char *buf;
293ecd6cf80Smarks 
294ecd6cf80Smarks 	if (zc->zc_history == NULL)
295228975ccSek 		return (NULL);
296e7437265Sahrens 
297ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
298ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
299ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
300228975ccSek 		history_str_free(buf);
301228975ccSek 		return (NULL);
302ecd6cf80Smarks 	}
303ecd6cf80Smarks 
304ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
305ecd6cf80Smarks 
306228975ccSek 	return (buf);
307228975ccSek }
308ecd6cf80Smarks 
30915e6edf1Sgw /*
31015e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
31115e6edf1Sgw  */
31215e6edf1Sgw static boolean_t
31315e6edf1Sgw zfs_is_bootfs(const char *name)
31415e6edf1Sgw {
315503ad85cSMatthew Ahrens 	objset_t *os;
31615e6edf1Sgw 
317503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
318503ad85cSMatthew Ahrens 		boolean_t ret;
319b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
320503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
321503ad85cSMatthew Ahrens 		return (ret);
32215e6edf1Sgw 	}
323503ad85cSMatthew Ahrens 	return (B_FALSE);
32415e6edf1Sgw }
32515e6edf1Sgw 
326c2a93d44Stimh /*
327f7170741SWill Andrews  * Return non-zero if the spa version is less than requested version.
328c2a93d44Stimh  */
329da6c28aaSamw static int
3300a48a24eStimh zfs_earlier_version(const char *name, int version)
331da6c28aaSamw {
332da6c28aaSamw 	spa_t *spa;
333da6c28aaSamw 
334da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
335da6c28aaSamw 		if (spa_version(spa) < version) {
336da6c28aaSamw 			spa_close(spa, FTAG);
337da6c28aaSamw 			return (1);
338da6c28aaSamw 		}
339da6c28aaSamw 		spa_close(spa, FTAG);
340da6c28aaSamw 	}
341da6c28aaSamw 	return (0);
342da6c28aaSamw }
343da6c28aaSamw 
3449e6eda55Smarks /*
345745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3469e6eda55Smarks  */
347745cd3c5Smaybee static boolean_t
348745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3499e6eda55Smarks {
3509e6eda55Smarks 	objset_t *os;
351745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3529e6eda55Smarks 
353503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
354745cd3c5Smaybee 		uint64_t zplversion;
3559e6eda55Smarks 
356503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
357503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
358503ad85cSMatthew Ahrens 			return (B_TRUE);
359503ad85cSMatthew Ahrens 		}
360503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
361745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
362745cd3c5Smaybee 			rc = zplversion < version;
363503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3649e6eda55Smarks 	}
3659e6eda55Smarks 	return (rc);
3669e6eda55Smarks }
3679e6eda55Smarks 
368228975ccSek static void
369228975ccSek zfs_log_history(zfs_cmd_t *zc)
370228975ccSek {
371228975ccSek 	spa_t *spa;
372228975ccSek 	char *buf;
373ecd6cf80Smarks 
374228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
375228975ccSek 		return;
376228975ccSek 
377228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
378228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3794445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
380228975ccSek 		spa_close(spa, FTAG);
381228975ccSek 	}
382228975ccSek 	history_str_free(buf);
383ecd6cf80Smarks }
384ecd6cf80Smarks 
385fa9e4066Sahrens /*
386fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
387fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
388fa9e4066Sahrens  */
389fa9e4066Sahrens /* ARGSUSED */
390fa9e4066Sahrens static int
3914445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
392fa9e4066Sahrens {
393fa9e4066Sahrens 	return (0);
394fa9e4066Sahrens }
395fa9e4066Sahrens 
396fa9e4066Sahrens /*
397fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
398fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
399fa9e4066Sahrens  */
400fa9e4066Sahrens /* ARGSUSED */
401fa9e4066Sahrens static int
4024445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
403fa9e4066Sahrens {
404fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
405ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
406fa9e4066Sahrens 		return (0);
407fa9e4066Sahrens 
408be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
409fa9e4066Sahrens }
410fa9e4066Sahrens 
411fa9e4066Sahrens static int
412a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
413fa9e4066Sahrens {
414fa9e4066Sahrens 	int writable = 1;
415fa9e4066Sahrens 
416fa9e4066Sahrens 	/*
417fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
418fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
419fa9e4066Sahrens 	 */
420fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
421fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
422be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
423fa9e4066Sahrens 
424fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
425fa9e4066Sahrens 		/*
426fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
427fa9e4066Sahrens 		 * global zone.
428fa9e4066Sahrens 		 */
429fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
430be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
431fa9e4066Sahrens 	} else {
432fa9e4066Sahrens 		/*
433fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
434fa9e4066Sahrens 		 */
435fa9e4066Sahrens 		if (!zoned)
436be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
437fa9e4066Sahrens 
438fa9e4066Sahrens 		/* must be writable by this zone */
439fa9e4066Sahrens 		if (!writable)
440be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
441fa9e4066Sahrens 	}
442fa9e4066Sahrens 	return (0);
443fa9e4066Sahrens }
444fa9e4066Sahrens 
445a7f53a56SChris Kirby static int
446a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
447a7f53a56SChris Kirby {
448a7f53a56SChris Kirby 	uint64_t zoned;
449a7f53a56SChris Kirby 
450a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
451be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
452a7f53a56SChris Kirby 
453a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
454a7f53a56SChris Kirby }
455a7f53a56SChris Kirby 
456a7f53a56SChris Kirby static int
457a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
458a7f53a56SChris Kirby {
459a7f53a56SChris Kirby 	uint64_t zoned;
460a7f53a56SChris Kirby 
4613b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
462be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
463a7f53a56SChris Kirby 
464a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
465a7f53a56SChris Kirby }
466a7f53a56SChris Kirby 
4674445fffbSMatthew Ahrens static int
4683b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4693b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
470fa9e4066Sahrens {
471fa9e4066Sahrens 	int error;
472fa9e4066Sahrens 
47319b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
474ecd6cf80Smarks 	if (error == 0) {
475ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4763b2aab18SMatthew Ahrens 		if (error != 0)
4774445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
478ecd6cf80Smarks 	}
479ecd6cf80Smarks 	return (error);
480ecd6cf80Smarks }
481ecd6cf80Smarks 
4824445fffbSMatthew Ahrens static int
4833b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
484a7f53a56SChris Kirby {
485a7f53a56SChris Kirby 	int error;
4863b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4873b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
488a7f53a56SChris Kirby 
4893b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4903b2aab18SMatthew Ahrens 	if (error != 0)
4913b2aab18SMatthew Ahrens 		return (error);
4923b2aab18SMatthew Ahrens 
4933b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
4943b2aab18SMatthew Ahrens 	if (error != 0) {
4953b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
4963b2aab18SMatthew Ahrens 		return (error);
497a7f53a56SChris Kirby 	}
4983b2aab18SMatthew Ahrens 
4993b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5003b2aab18SMatthew Ahrens 
5013b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5023b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
503a7f53a56SChris Kirby 	return (error);
504a7f53a56SChris Kirby }
505a7f53a56SChris Kirby 
5064201a95eSRic Aleshire /*
5074201a95eSRic Aleshire  * Policy for setting the security label property.
5084201a95eSRic Aleshire  *
5094201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5104201a95eSRic Aleshire  */
5114201a95eSRic Aleshire static int
51292241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5134201a95eSRic Aleshire {
5144201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5154201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5164201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5174201a95eSRic Aleshire 	uint64_t	zoned;
5184201a95eSRic Aleshire 	int		needed_priv = -1;
5194201a95eSRic Aleshire 	int		error;
5204201a95eSRic Aleshire 
5214201a95eSRic Aleshire 	/* First get the existing dataset label. */
5224201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5234201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5243b2aab18SMatthew Ahrens 	if (error != 0)
525be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5264201a95eSRic Aleshire 
5274201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5284201a95eSRic Aleshire 		new_default = TRUE;
5294201a95eSRic Aleshire 
5304201a95eSRic Aleshire 	/* The label must be translatable */
5314201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
532be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5334201a95eSRic Aleshire 
5344201a95eSRic Aleshire 	/*
5354201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5364201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5374201a95eSRic Aleshire 	 * are needed.
5384201a95eSRic Aleshire 	 */
5394201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5404201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
541be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5424201a95eSRic Aleshire 		return (0);
5434201a95eSRic Aleshire 	}
5444201a95eSRic Aleshire 
5454201a95eSRic Aleshire 	/*
5464201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5474201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5484201a95eSRic Aleshire 	 * global zone.
5494201a95eSRic Aleshire 	 */
5504201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5514201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
552be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5534201a95eSRic Aleshire 	if (!zoned) {
5544201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
555be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5564201a95eSRic Aleshire 	}
5574201a95eSRic Aleshire 
5584201a95eSRic Aleshire 	/*
5594201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5604201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5614201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5624201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5634201a95eSRic Aleshire 	 */
5644201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
56592241e0bSTom Erickson 		objset_t *os;
56692241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
56792241e0bSTom Erickson 
5684201a95eSRic Aleshire 		/*
5694201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5704201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5714201a95eSRic Aleshire 		 */
5724201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57392241e0bSTom Erickson 		    setsl_tag, &os);
5743b2aab18SMatthew Ahrens 		if (error != 0)
575be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5764201a95eSRic Aleshire 
57792241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
57892241e0bSTom Erickson 
5794201a95eSRic Aleshire 		if (new_default) {
5804201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5814201a95eSRic Aleshire 			goto out_check;
5824201a95eSRic Aleshire 		}
5834201a95eSRic Aleshire 
5844201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
585be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5864201a95eSRic Aleshire 
5874201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5884201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5894201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5904201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5914201a95eSRic Aleshire 	} else {
5924201a95eSRic Aleshire 		/* dataset currently has a default label */
5934201a95eSRic Aleshire 		if (!new_default)
5944201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5954201a95eSRic Aleshire 	}
5964201a95eSRic Aleshire 
5974201a95eSRic Aleshire out_check:
5984201a95eSRic Aleshire 	if (needed_priv != -1)
5994201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6004201a95eSRic Aleshire 	return (0);
6014201a95eSRic Aleshire }
6024201a95eSRic Aleshire 
603ecd6cf80Smarks static int
60492241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
60592241e0bSTom Erickson     cred_t *cr)
606ecd6cf80Smarks {
60792241e0bSTom Erickson 	char *strval;
60892241e0bSTom Erickson 
609ecd6cf80Smarks 	/*
610ecd6cf80Smarks 	 * Check permissions for special properties.
611ecd6cf80Smarks 	 */
612ecd6cf80Smarks 	switch (prop) {
613ecd6cf80Smarks 	case ZFS_PROP_ZONED:
614ecd6cf80Smarks 		/*
615ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
616ecd6cf80Smarks 		 */
617ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
618be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
619ecd6cf80Smarks 		break;
620ecd6cf80Smarks 
621ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
622ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
623ecd6cf80Smarks 			uint64_t zoned;
624ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
625ecd6cf80Smarks 			/*
626ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
627ecd6cf80Smarks 			 * quota on things *under* (ie. contained by)
628ecd6cf80Smarks 			 * the thing they own.
629ecd6cf80Smarks 			 */
63092241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
631ecd6cf80Smarks 			    setpoint))
632be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
63392241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
634be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
635ecd6cf80Smarks 		}
636db870a07Sahrens 		break;
6374201a95eSRic Aleshire 
6384201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6394201a95eSRic Aleshire 		if (!is_system_labeled())
640be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64192241e0bSTom Erickson 
64292241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
64392241e0bSTom Erickson 			int err;
64492241e0bSTom Erickson 
64592241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
64692241e0bSTom Erickson 			if (err != 0)
64792241e0bSTom Erickson 				return (err);
64892241e0bSTom Erickson 		}
6494201a95eSRic Aleshire 		break;
650ecd6cf80Smarks 	}
651ecd6cf80Smarks 
65292241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
653ecd6cf80Smarks }
654ecd6cf80Smarks 
6554445fffbSMatthew Ahrens /* ARGSUSED */
6564445fffbSMatthew Ahrens static int
6574445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
658ecd6cf80Smarks {
659ecd6cf80Smarks 	int error;
660ecd6cf80Smarks 
661ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6623b2aab18SMatthew Ahrens 	if (error != 0)
663fa9e4066Sahrens 		return (error);
664fa9e4066Sahrens 
665ecd6cf80Smarks 	/*
666ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
667ecd6cf80Smarks 	 * dsl_deleg_can_allow()
668ecd6cf80Smarks 	 */
669ecd6cf80Smarks 	return (0);
670ecd6cf80Smarks }
671ecd6cf80Smarks 
6724445fffbSMatthew Ahrens /* ARGSUSED */
6734445fffbSMatthew Ahrens static int
6744445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
675ecd6cf80Smarks {
676681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
677681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
678ecd6cf80Smarks }
679ecd6cf80Smarks 
6804445fffbSMatthew Ahrens /* ARGSUSED */
6814445fffbSMatthew Ahrens static int
6824445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
683ecd6cf80Smarks {
684a7f53a56SChris Kirby 	dsl_pool_t *dp;
685a7f53a56SChris Kirby 	dsl_dataset_t *ds;
686a7f53a56SChris Kirby 	char *cp;
687a7f53a56SChris Kirby 	int error;
688a7f53a56SChris Kirby 
689a7f53a56SChris Kirby 	/*
690a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
691a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
692a7f53a56SChris Kirby 	 */
693a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
694a7f53a56SChris Kirby 	if (cp == NULL)
695be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
6963b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6973b2aab18SMatthew Ahrens 	if (error != 0)
698a7f53a56SChris Kirby 		return (error);
699a7f53a56SChris Kirby 
700a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7013b2aab18SMatthew Ahrens 	if (error != 0) {
7023b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
703a7f53a56SChris Kirby 		return (error);
7043b2aab18SMatthew Ahrens 	}
705a7f53a56SChris Kirby 
706a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
707a7f53a56SChris Kirby 
708a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
709a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
710a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7113b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
712a7f53a56SChris Kirby 
713a7f53a56SChris Kirby 	return (error);
714ecd6cf80Smarks }
715ecd6cf80Smarks 
7164445fffbSMatthew Ahrens /* ARGSUSED */
717743a77edSAlan Wright static int
7184445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7194445fffbSMatthew Ahrens {
7204445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7214445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7224445fffbSMatthew Ahrens }
7234445fffbSMatthew Ahrens 
7244445fffbSMatthew Ahrens /* ARGSUSED */
7254445fffbSMatthew Ahrens static int
7264445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
727743a77edSAlan Wright {
728743a77edSAlan Wright 	vnode_t *vp;
729743a77edSAlan Wright 	int error;
730743a77edSAlan Wright 
731743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
732743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
733743a77edSAlan Wright 		return (error);
734743a77edSAlan Wright 
735743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
736743a77edSAlan Wright 
737743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
738743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
739743a77edSAlan Wright 	    zc->zc_name) != 0)) {
740743a77edSAlan Wright 		VN_RELE(vp);
741be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
742743a77edSAlan Wright 	}
743743a77edSAlan Wright 
744743a77edSAlan Wright 	VN_RELE(vp);
745743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
746743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
747743a77edSAlan Wright }
748743a77edSAlan Wright 
749ecd6cf80Smarks int
7504445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
751ecd6cf80Smarks {
752ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
753be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
754ecd6cf80Smarks 
7553cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
756ecd6cf80Smarks 		return (0);
757ecd6cf80Smarks 	} else {
7584445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
759743a77edSAlan Wright 	}
760743a77edSAlan Wright }
761ecd6cf80Smarks 
762743a77edSAlan Wright int
7634445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
764743a77edSAlan Wright {
765743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
766be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
767ecd6cf80Smarks 
768743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
769743a77edSAlan Wright 		return (0);
770743a77edSAlan Wright 	} else {
7714445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
772ecd6cf80Smarks 	}
773fa9e4066Sahrens }
774fa9e4066Sahrens 
775fa9e4066Sahrens static int
776ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
777fa9e4066Sahrens {
778fa9e4066Sahrens 	char *cp;
779fa9e4066Sahrens 
780fa9e4066Sahrens 	/*
781fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
782fa9e4066Sahrens 	 */
783ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
784ecd6cf80Smarks 	cp = strrchr(parent, '@');
785fa9e4066Sahrens 	if (cp != NULL) {
786fa9e4066Sahrens 		cp[0] = '\0';
787fa9e4066Sahrens 	} else {
788ecd6cf80Smarks 		cp = strrchr(parent, '/');
789fa9e4066Sahrens 		if (cp == NULL)
790be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
791fa9e4066Sahrens 		cp[0] = '\0';
792ecd6cf80Smarks 	}
793ecd6cf80Smarks 
794ecd6cf80Smarks 	return (0);
795ecd6cf80Smarks }
796ecd6cf80Smarks 
797ecd6cf80Smarks int
798ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
799ecd6cf80Smarks {
800ecd6cf80Smarks 	int error;
801ecd6cf80Smarks 
802ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
803ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
804ecd6cf80Smarks 		return (error);
805ecd6cf80Smarks 
806ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
807ecd6cf80Smarks }
808ecd6cf80Smarks 
8094445fffbSMatthew Ahrens /* ARGSUSED */
810ecd6cf80Smarks static int
8114445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
812ecd6cf80Smarks {
813ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
814ecd6cf80Smarks }
815ecd6cf80Smarks 
816cbf6f6aaSWilliam Gorrell /*
817cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8184445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
819cbf6f6aaSWilliam Gorrell  */
8204445fffbSMatthew Ahrens /* ARGSUSED */
821cbf6f6aaSWilliam Gorrell static int
8224445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
823cbf6f6aaSWilliam Gorrell {
8244445fffbSMatthew Ahrens 	nvlist_t *snaps;
8254445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8264445fffbSMatthew Ahrens 	int error = 0;
827cbf6f6aaSWilliam Gorrell 
8284445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
829be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8304445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8314445fffbSMatthew Ahrens 	    pair = nextpair) {
8323b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
8334445fffbSMatthew Ahrens 		dsl_dataset_t *ds;
834cbf6f6aaSWilliam Gorrell 
8353b2aab18SMatthew Ahrens 		error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp);
8363b2aab18SMatthew Ahrens 		if (error != 0)
8373b2aab18SMatthew Ahrens 			break;
8384445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
8393b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds);
8403b2aab18SMatthew Ahrens 		if (error == 0)
8414445fffbSMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
8423b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
8433b2aab18SMatthew Ahrens 
8443b2aab18SMatthew Ahrens 		if (error == 0) {
8453b2aab18SMatthew Ahrens 			error = zfs_secpolicy_destroy_perms(nvpair_name(pair),
8463b2aab18SMatthew Ahrens 			    cr);
8474445fffbSMatthew Ahrens 		} else if (error == ENOENT) {
8484445fffbSMatthew Ahrens 			/*
8494445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8504445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8514445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8524445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8534445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8544445fffbSMatthew Ahrens 			 * checked for permission).
8554445fffbSMatthew Ahrens 			 */
8564445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8574445fffbSMatthew Ahrens 			error = 0;
8584445fffbSMatthew Ahrens 		}
8594445fffbSMatthew Ahrens 		if (error != 0)
8604445fffbSMatthew Ahrens 			break;
8614445fffbSMatthew Ahrens 	}
862cbf6f6aaSWilliam Gorrell 
863cbf6f6aaSWilliam Gorrell 	return (error);
864cbf6f6aaSWilliam Gorrell }
865cbf6f6aaSWilliam Gorrell 
866ecd6cf80Smarks int
867ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
868ecd6cf80Smarks {
86992241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
870ecd6cf80Smarks 	int	error;
871ecd6cf80Smarks 
872ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
873ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
874ecd6cf80Smarks 		return (error);
875ecd6cf80Smarks 
876ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
877ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
878ecd6cf80Smarks 		return (error);
879ecd6cf80Smarks 
880ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
881ecd6cf80Smarks 	    sizeof (parentname))) != 0)
882ecd6cf80Smarks 		return (error);
883ecd6cf80Smarks 
884ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
885ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
886ecd6cf80Smarks 		return (error);
887ecd6cf80Smarks 
888ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
889ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
890ecd6cf80Smarks 		return (error);
891ecd6cf80Smarks 
892ecd6cf80Smarks 	return (error);
893ecd6cf80Smarks }
894ecd6cf80Smarks 
8954445fffbSMatthew Ahrens /* ARGSUSED */
896ecd6cf80Smarks static int
8974445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
898ecd6cf80Smarks {
899ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
900ecd6cf80Smarks }
901ecd6cf80Smarks 
9024445fffbSMatthew Ahrens /* ARGSUSED */
903ecd6cf80Smarks static int
9044445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
905ecd6cf80Smarks {
9063b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9073b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
908ecd6cf80Smarks 	int error;
909ecd6cf80Smarks 
910ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
911ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9123b2aab18SMatthew Ahrens 	if (error != 0)
9133b2aab18SMatthew Ahrens 		return (error);
9143b2aab18SMatthew Ahrens 
9153b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9163b2aab18SMatthew Ahrens 	if (error != 0)
917ecd6cf80Smarks 		return (error);
918ecd6cf80Smarks 
9193b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
920ecd6cf80Smarks 
921ecd6cf80Smarks 	if (error == 0) {
9223b2aab18SMatthew Ahrens 		char parentname[MAXNAMELEN];
9233b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
924ecd6cf80Smarks 		dsl_dir_t *dd;
9253b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
926ecd6cf80Smarks 
927745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
9283b2aab18SMatthew Ahrens 		    dd->dd_phys->dd_origin_obj, FTAG, &origin);
9293b2aab18SMatthew Ahrens 		if (error != 0) {
9303b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9313b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
932ecd6cf80Smarks 			return (error);
933ecd6cf80Smarks 		}
934ecd6cf80Smarks 
9353b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
936ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
937ecd6cf80Smarks 
9383b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9393b2aab18SMatthew Ahrens 		if (error == 0) {
9403b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
941ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9423b2aab18SMatthew Ahrens 		}
9433b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9443b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
945ecd6cf80Smarks 	}
9463b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
947ecd6cf80Smarks 	return (error);
948ecd6cf80Smarks }
949ecd6cf80Smarks 
9504445fffbSMatthew Ahrens /* ARGSUSED */
951ecd6cf80Smarks static int
9524445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
953ecd6cf80Smarks {
954ecd6cf80Smarks 	int error;
955ecd6cf80Smarks 
956ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
957ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
958ecd6cf80Smarks 		return (error);
959ecd6cf80Smarks 
960ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
961ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
962ecd6cf80Smarks 		return (error);
963ecd6cf80Smarks 
964ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
965ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
966ecd6cf80Smarks }
967ecd6cf80Smarks 
968ecd6cf80Smarks int
969ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
970ecd6cf80Smarks {
971681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
972681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
973ecd6cf80Smarks }
974ecd6cf80Smarks 
9754445fffbSMatthew Ahrens /*
9764445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9774445fffbSMatthew Ahrens  */
9784445fffbSMatthew Ahrens /* ARGSUSED */
979ecd6cf80Smarks static int
9804445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
981ecd6cf80Smarks {
9824445fffbSMatthew Ahrens 	nvlist_t *snaps;
983d5285caeSGeorge Wilson 	int error = 0;
9844445fffbSMatthew Ahrens 	nvpair_t *pair;
985ecd6cf80Smarks 
9864445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
987be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9884445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9894445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9904445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9914445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9924445fffbSMatthew Ahrens 
9934445fffbSMatthew Ahrens 		if (atp == NULL) {
994be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9954445fffbSMatthew Ahrens 			break;
9964445fffbSMatthew Ahrens 		}
9974445fffbSMatthew Ahrens 		*atp = '\0';
9984445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
9994445fffbSMatthew Ahrens 		*atp = '@';
10004445fffbSMatthew Ahrens 		if (error != 0)
10014445fffbSMatthew Ahrens 			break;
10024445fffbSMatthew Ahrens 	}
10034445fffbSMatthew Ahrens 	return (error);
1004ecd6cf80Smarks }
1005ecd6cf80Smarks 
10064445fffbSMatthew Ahrens /* ARGSUSED */
1007ecd6cf80Smarks static int
10084445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10094445fffbSMatthew Ahrens {
10104445fffbSMatthew Ahrens 	/*
10114445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10124445fffbSMatthew Ahrens 	 * to log to.
10134445fffbSMatthew Ahrens 	 */
10144445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1015be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10164445fffbSMatthew Ahrens 	return (0);
10174445fffbSMatthew Ahrens }
10184445fffbSMatthew Ahrens 
10194445fffbSMatthew Ahrens static int
10204445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1021ecd6cf80Smarks {
102292241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
102392241e0bSTom Erickson 	int	error;
10244445fffbSMatthew Ahrens 	char	*origin;
1025ecd6cf80Smarks 
1026ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1027ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1028ecd6cf80Smarks 		return (error);
1029fa9e4066Sahrens 
10304445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10314445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
10324445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
10334445fffbSMatthew Ahrens 		return (error);
1034fa9e4066Sahrens 
1035ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1036ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1037ecd6cf80Smarks 		return (error);
1038ecd6cf80Smarks 
10394445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
10404445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1041fa9e4066Sahrens }
1042fa9e4066Sahrens 
1043fa9e4066Sahrens /*
1044fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1045fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1046fa9e4066Sahrens  */
1047fa9e4066Sahrens /* ARGSUSED */
1048fa9e4066Sahrens static int
10494445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1050fa9e4066Sahrens {
1051fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1052be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	return (0);
1055fa9e4066Sahrens }
1056fa9e4066Sahrens 
105799d5e173STim Haley /*
105899d5e173STim Haley  * Policy for object to name lookups.
105999d5e173STim Haley  */
106099d5e173STim Haley /* ARGSUSED */
106199d5e173STim Haley static int
10624445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
106399d5e173STim Haley {
106499d5e173STim Haley 	int error;
106599d5e173STim Haley 
106699d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
106799d5e173STim Haley 		return (0);
106899d5e173STim Haley 
106999d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
107099d5e173STim Haley 	return (error);
107199d5e173STim Haley }
107299d5e173STim Haley 
1073ea8dc4b6Seschrock /*
1074ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1075ea8dc4b6Seschrock  */
1076ea8dc4b6Seschrock /* ARGSUSED */
1077ea8dc4b6Seschrock static int
10784445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1079ea8dc4b6Seschrock {
1080ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1081ea8dc4b6Seschrock }
1082ea8dc4b6Seschrock 
10834445fffbSMatthew Ahrens /* ARGSUSED */
1084e45ce728Sahrens static int
10854445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1086e45ce728Sahrens {
1087e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1088e45ce728Sahrens 
1089990b4856Slling 	if (prop == ZPROP_INVAL) {
1090e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1091be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1092e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1093e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1094e45ce728Sahrens 	} else {
109592241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
109692241e0bSTom Erickson 		    NULL, cr));
1097e45ce728Sahrens 	}
1098e45ce728Sahrens }
1099e45ce728Sahrens 
110014843421SMatthew Ahrens static int
11014445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
110214843421SMatthew Ahrens {
11034445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
110414843421SMatthew Ahrens 	if (err)
110514843421SMatthew Ahrens 		return (err);
110614843421SMatthew Ahrens 
110714843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1108be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
110914843421SMatthew Ahrens 
111014843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
111114843421SMatthew Ahrens 		/*
111214843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
111314843421SMatthew Ahrens 		 * themself, allow it.
111414843421SMatthew Ahrens 		 */
111514843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
111614843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
111714843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
111814843421SMatthew Ahrens 				return (0);
111914843421SMatthew Ahrens 		} else {
112014843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
112114843421SMatthew Ahrens 				return (0);
112214843421SMatthew Ahrens 		}
112314843421SMatthew Ahrens 	}
112414843421SMatthew Ahrens 
112514843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
112614843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
112714843421SMatthew Ahrens }
112814843421SMatthew Ahrens 
112914843421SMatthew Ahrens static int
11304445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
113114843421SMatthew Ahrens {
11324445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
113314843421SMatthew Ahrens 	if (err)
113414843421SMatthew Ahrens 		return (err);
113514843421SMatthew Ahrens 
113614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1137be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
113814843421SMatthew Ahrens 
113914843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
114014843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
114114843421SMatthew Ahrens }
114214843421SMatthew Ahrens 
11434445fffbSMatthew Ahrens /* ARGSUSED */
114414843421SMatthew Ahrens static int
11454445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
114614843421SMatthew Ahrens {
114792241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
114892241e0bSTom Erickson 	    NULL, cr));
114914843421SMatthew Ahrens }
115014843421SMatthew Ahrens 
11514445fffbSMatthew Ahrens /* ARGSUSED */
1152842727c2SChris Kirby static int
11534445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1154842727c2SChris Kirby {
11553b2aab18SMatthew Ahrens 	nvpair_t *pair;
11563b2aab18SMatthew Ahrens 	nvlist_t *holds;
11573b2aab18SMatthew Ahrens 	int error;
11583b2aab18SMatthew Ahrens 
11593b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
11603b2aab18SMatthew Ahrens 	if (error != 0)
1161be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
11623b2aab18SMatthew Ahrens 
11633b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
11643b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
11653b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
11663b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
11673b2aab18SMatthew Ahrens 		if (error != 0)
11683b2aab18SMatthew Ahrens 			return (error);
11693b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
11703b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
11713b2aab18SMatthew Ahrens 		if (error != 0)
11723b2aab18SMatthew Ahrens 			return (error);
11733b2aab18SMatthew Ahrens 	}
11743b2aab18SMatthew Ahrens 	return (0);
1175842727c2SChris Kirby }
1176842727c2SChris Kirby 
11774445fffbSMatthew Ahrens /* ARGSUSED */
1178842727c2SChris Kirby static int
11794445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1180842727c2SChris Kirby {
11813b2aab18SMatthew Ahrens 	nvpair_t *pair;
11823b2aab18SMatthew Ahrens 	int error;
11833b2aab18SMatthew Ahrens 
11843b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
11853b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
11863b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
11873b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
11883b2aab18SMatthew Ahrens 		if (error != 0)
11893b2aab18SMatthew Ahrens 			return (error);
11903b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
11913b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
11923b2aab18SMatthew Ahrens 		if (error != 0)
11933b2aab18SMatthew Ahrens 			return (error);
11943b2aab18SMatthew Ahrens 	}
11953b2aab18SMatthew Ahrens 	return (0);
1196842727c2SChris Kirby }
1197842727c2SChris Kirby 
119899d5e173STim Haley /*
119999d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
120099d5e173STim Haley  */
120199d5e173STim Haley static int
12024445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120399d5e173STim Haley {
120499d5e173STim Haley 	/*
120599d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
120699d5e173STim Haley 	 * hold, destroy and release all rolled into one.
120799d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
120899d5e173STim Haley 	 */
120999d5e173STim Haley 	int error;
121099d5e173STim Haley 
121199d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
121299d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
121399d5e173STim Haley 		return (0);
121499d5e173STim Haley 
12154445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12163b2aab18SMatthew Ahrens 	if (error == 0)
12174445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12183b2aab18SMatthew Ahrens 	if (error == 0)
12194445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12203b2aab18SMatthew Ahrens 	if (error == 0)
12214445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
122299d5e173STim Haley 	return (error);
122399d5e173STim Haley }
122499d5e173STim Haley 
1225fa9e4066Sahrens /*
1226fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1227fa9e4066Sahrens  */
1228fa9e4066Sahrens static int
1229478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1230fa9e4066Sahrens {
1231fa9e4066Sahrens 	char *packed;
1232fa9e4066Sahrens 	int error;
1233990b4856Slling 	nvlist_t *list = NULL;
1234fa9e4066Sahrens 
1235fa9e4066Sahrens 	/*
1236e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1237fa9e4066Sahrens 	 */
1238990b4856Slling 	if (size == 0)
1239be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1240fa9e4066Sahrens 
1241fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1242fa9e4066Sahrens 
1243478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1244478ed9adSEric Taylor 	    iflag)) != 0) {
1245fa9e4066Sahrens 		kmem_free(packed, size);
1246fa9e4066Sahrens 		return (error);
1247fa9e4066Sahrens 	}
1248fa9e4066Sahrens 
1249990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1250fa9e4066Sahrens 		kmem_free(packed, size);
1251fa9e4066Sahrens 		return (error);
1252fa9e4066Sahrens 	}
1253fa9e4066Sahrens 
1254fa9e4066Sahrens 	kmem_free(packed, size);
1255fa9e4066Sahrens 
1256990b4856Slling 	*nvp = list;
1257fa9e4066Sahrens 	return (0);
1258fa9e4066Sahrens }
1259fa9e4066Sahrens 
12604445fffbSMatthew Ahrens /*
12614445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
12624445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
12634445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
12644445fffbSMatthew Ahrens  * removed.
12654445fffbSMatthew Ahrens  */
126692241e0bSTom Erickson static int
12674445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
126892241e0bSTom Erickson {
126992241e0bSTom Erickson 	size_t size;
127092241e0bSTom Erickson 
12714445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
127292241e0bSTom Erickson 
12734445fffbSMatthew Ahrens 	if (size > max) {
127492241e0bSTom Erickson 		nvpair_t *more_errors;
127592241e0bSTom Erickson 		int n = 0;
127692241e0bSTom Erickson 
12774445fffbSMatthew Ahrens 		if (max < 1024)
1278be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
127992241e0bSTom Erickson 
12804445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
12814445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
128292241e0bSTom Erickson 
128392241e0bSTom Erickson 		do {
12844445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
128592241e0bSTom Erickson 			    more_errors);
12864445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
128792241e0bSTom Erickson 			n++;
12884445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
12894445fffbSMatthew Ahrens 		} while (size > max);
129092241e0bSTom Erickson 
12914445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
12924445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
12934445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
129492241e0bSTom Erickson 	}
129592241e0bSTom Erickson 
129692241e0bSTom Erickson 	return (0);
129792241e0bSTom Erickson }
129892241e0bSTom Erickson 
1299e9dbad6fSeschrock static int
1300e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1301e9dbad6fSeschrock {
1302e9dbad6fSeschrock 	char *packed = NULL;
13036e27f868SSam Falkner 	int error = 0;
1304e9dbad6fSeschrock 	size_t size;
1305e9dbad6fSeschrock 
13064445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1307e9dbad6fSeschrock 
1308e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1309be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1310e9dbad6fSeschrock 	} else {
13114445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
13126e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
13136e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1314be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
13154445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1316e9dbad6fSeschrock 	}
1317e9dbad6fSeschrock 
1318e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
13194445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1320e9dbad6fSeschrock 	return (error);
1321e9dbad6fSeschrock }
1322e9dbad6fSeschrock 
132314843421SMatthew Ahrens static int
1324af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
132514843421SMatthew Ahrens {
132614843421SMatthew Ahrens 	objset_t *os;
132714843421SMatthew Ahrens 	int error;
132814843421SMatthew Ahrens 
1329503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
13303b2aab18SMatthew Ahrens 	if (error != 0)
133114843421SMatthew Ahrens 		return (error);
1332503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1333503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1334be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1335503ad85cSMatthew Ahrens 	}
133614843421SMatthew Ahrens 
1337503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1338af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1339af4c679fSSean McEnroe 	if (*zfvp) {
1340af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
134114843421SMatthew Ahrens 	} else {
1342be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
134314843421SMatthew Ahrens 	}
1344503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1345503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
134614843421SMatthew Ahrens 	return (error);
134714843421SMatthew Ahrens }
134814843421SMatthew Ahrens 
134914843421SMatthew Ahrens /*
135014843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
135114843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1352ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1353ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
135414843421SMatthew Ahrens  */
135514843421SMatthew Ahrens static int
13561412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
135714843421SMatthew Ahrens {
135814843421SMatthew Ahrens 	int error = 0;
135914843421SMatthew Ahrens 
1360af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1361af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
136214843421SMatthew Ahrens 	if (error == 0) {
13631412a1a2SMark Shellenbaum 		rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
13641412a1a2SMark Shellenbaum 		    RW_READER, tag);
1365af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
136614843421SMatthew Ahrens 			/*
136714843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
136814843421SMatthew Ahrens 			 * thread should be just about to disassociate the
136914843421SMatthew Ahrens 			 * objset from the zfsvfs.
137014843421SMatthew Ahrens 			 */
1371af4c679fSSean McEnroe 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1372be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
137314843421SMatthew Ahrens 		}
137414843421SMatthew Ahrens 	}
137514843421SMatthew Ahrens 	return (error);
137614843421SMatthew Ahrens }
137714843421SMatthew Ahrens 
137814843421SMatthew Ahrens static void
137914843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
138014843421SMatthew Ahrens {
138114843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
138214843421SMatthew Ahrens 
138314843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
138414843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
138514843421SMatthew Ahrens 	} else {
1386503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
138714843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
138814843421SMatthew Ahrens 	}
138914843421SMatthew Ahrens }
139014843421SMatthew Ahrens 
1391fa9e4066Sahrens static int
1392fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1393fa9e4066Sahrens {
1394fa9e4066Sahrens 	int error;
1395990b4856Slling 	nvlist_t *config, *props = NULL;
13960a48a24eStimh 	nvlist_t *rootprops = NULL;
13970a48a24eStimh 	nvlist_t *zplprops = NULL;
1398fa9e4066Sahrens 
1399990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1400478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1401fa9e4066Sahrens 		return (error);
14022a6b87f0Sek 
1403990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1404478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1405478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1406990b4856Slling 		nvlist_free(config);
1407990b4856Slling 		return (error);
1408990b4856Slling 	}
1409990b4856Slling 
14100a48a24eStimh 	if (props) {
14110a48a24eStimh 		nvlist_t *nvl = NULL;
14120a48a24eStimh 		uint64_t version = SPA_VERSION;
14130a48a24eStimh 
14140a48a24eStimh 		(void) nvlist_lookup_uint64(props,
14150a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1416ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1417be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
14180a48a24eStimh 			goto pool_props_bad;
14190a48a24eStimh 		}
14200a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
14210a48a24eStimh 		if (nvl) {
14220a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
14230a48a24eStimh 			if (error != 0) {
14240a48a24eStimh 				nvlist_free(config);
14250a48a24eStimh 				nvlist_free(props);
14260a48a24eStimh 				return (error);
14270a48a24eStimh 			}
14280a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
14290a48a24eStimh 		}
14300a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14310a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
14320a48a24eStimh 		    zplprops, NULL);
14333b2aab18SMatthew Ahrens 		if (error != 0)
14340a48a24eStimh 			goto pool_props_bad;
14350a48a24eStimh 	}
14360a48a24eStimh 
14374445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
14380a48a24eStimh 
14390a48a24eStimh 	/*
14400a48a24eStimh 	 * Set the remaining root properties
14410a48a24eStimh 	 */
144292241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
144392241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
14440a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1445fa9e4066Sahrens 
14460a48a24eStimh pool_props_bad:
14470a48a24eStimh 	nvlist_free(rootprops);
14480a48a24eStimh 	nvlist_free(zplprops);
1449fa9e4066Sahrens 	nvlist_free(config);
14500a48a24eStimh 	nvlist_free(props);
1451990b4856Slling 
1452fa9e4066Sahrens 	return (error);
1453fa9e4066Sahrens }
1454fa9e4066Sahrens 
1455fa9e4066Sahrens static int
1456fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1457fa9e4066Sahrens {
1458ecd6cf80Smarks 	int error;
1459ecd6cf80Smarks 	zfs_log_history(zc);
1460ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1461681d9761SEric Taylor 	if (error == 0)
1462681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1463ecd6cf80Smarks 	return (error);
1464fa9e4066Sahrens }
1465fa9e4066Sahrens 
1466fa9e4066Sahrens static int
1467fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1468fa9e4066Sahrens {
1469990b4856Slling 	nvlist_t *config, *props = NULL;
1470fa9e4066Sahrens 	uint64_t guid;
1471468c413aSTim Haley 	int error;
1472fa9e4066Sahrens 
1473990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1474478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1475990b4856Slling 		return (error);
1476990b4856Slling 
1477990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1478478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1479478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1480990b4856Slling 		nvlist_free(config);
1481fa9e4066Sahrens 		return (error);
1482990b4856Slling 	}
1483fa9e4066Sahrens 
1484fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1485ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1486be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1487fa9e4066Sahrens 	else
14884b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1489fa9e4066Sahrens 
14904b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
14914b964adaSGeorge Wilson 		int err;
14924b964adaSGeorge Wilson 
14934b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
14944b964adaSGeorge Wilson 			error = err;
14954b964adaSGeorge Wilson 	}
1496468c413aSTim Haley 
1497fa9e4066Sahrens 	nvlist_free(config);
1498fa9e4066Sahrens 
1499990b4856Slling 	if (props)
1500990b4856Slling 		nvlist_free(props);
1501990b4856Slling 
1502fa9e4066Sahrens 	return (error);
1503fa9e4066Sahrens }
1504fa9e4066Sahrens 
1505fa9e4066Sahrens static int
1506fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1507fa9e4066Sahrens {
1508ecd6cf80Smarks 	int error;
150989a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1510394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
151189a89ebfSlling 
1512ecd6cf80Smarks 	zfs_log_history(zc);
1513394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1514681d9761SEric Taylor 	if (error == 0)
1515681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1516ecd6cf80Smarks 	return (error);
1517fa9e4066Sahrens }
1518fa9e4066Sahrens 
1519fa9e4066Sahrens static int
1520fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1521fa9e4066Sahrens {
1522fa9e4066Sahrens 	nvlist_t *configs;
1523fa9e4066Sahrens 	int error;
1524fa9e4066Sahrens 
1525fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1526be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1527fa9e4066Sahrens 
1528e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1529fa9e4066Sahrens 
1530fa9e4066Sahrens 	nvlist_free(configs);
1531fa9e4066Sahrens 
1532fa9e4066Sahrens 	return (error);
1533fa9e4066Sahrens }
1534fa9e4066Sahrens 
1535ad135b5dSChristopher Siden /*
1536ad135b5dSChristopher Siden  * inputs:
1537ad135b5dSChristopher Siden  * zc_name		name of the pool
1538ad135b5dSChristopher Siden  *
1539ad135b5dSChristopher Siden  * outputs:
1540ad135b5dSChristopher Siden  * zc_cookie		real errno
1541ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1542ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1543ad135b5dSChristopher Siden  */
1544fa9e4066Sahrens static int
1545fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1546fa9e4066Sahrens {
1547fa9e4066Sahrens 	nvlist_t *config;
1548fa9e4066Sahrens 	int error;
1549ea8dc4b6Seschrock 	int ret = 0;
1550fa9e4066Sahrens 
1551e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1552e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1553fa9e4066Sahrens 
1554fa9e4066Sahrens 	if (config != NULL) {
1555e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1556fa9e4066Sahrens 		nvlist_free(config);
1557ea8dc4b6Seschrock 
1558ea8dc4b6Seschrock 		/*
1559ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1560ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1561ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1562ea8dc4b6Seschrock 		 */
1563ea8dc4b6Seschrock 		zc->zc_cookie = error;
1564fa9e4066Sahrens 	} else {
1565ea8dc4b6Seschrock 		ret = error;
1566fa9e4066Sahrens 	}
1567fa9e4066Sahrens 
1568ea8dc4b6Seschrock 	return (ret);
1569fa9e4066Sahrens }
1570fa9e4066Sahrens 
1571fa9e4066Sahrens /*
1572fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1573fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1574fa9e4066Sahrens  */
1575fa9e4066Sahrens static int
1576fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1577fa9e4066Sahrens {
1578fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1579fa9e4066Sahrens 	int error;
1580fa9e4066Sahrens 
1581990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1582478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1583fa9e4066Sahrens 		return (error);
1584fa9e4066Sahrens 
1585fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1586fa9e4066Sahrens 
1587fa9e4066Sahrens 	nvlist_free(tryconfig);
1588fa9e4066Sahrens 
1589fa9e4066Sahrens 	if (config == NULL)
1590be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1591fa9e4066Sahrens 
1592e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1593fa9e4066Sahrens 	nvlist_free(config);
1594fa9e4066Sahrens 
1595fa9e4066Sahrens 	return (error);
1596fa9e4066Sahrens }
1597fa9e4066Sahrens 
15983f9d6ad7SLin Ling /*
15993f9d6ad7SLin Ling  * inputs:
16003f9d6ad7SLin Ling  * zc_name              name of the pool
16013f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16023f9d6ad7SLin Ling  */
1603fa9e4066Sahrens static int
16043f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1605fa9e4066Sahrens {
1606fa9e4066Sahrens 	spa_t *spa;
1607fa9e4066Sahrens 	int error;
1608fa9e4066Sahrens 
160906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
161006eeb2adSek 		return (error);
161106eeb2adSek 
16123f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
16133f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
16143f9d6ad7SLin Ling 	else
16153f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
161606eeb2adSek 
161706eeb2adSek 	spa_close(spa, FTAG);
161806eeb2adSek 
1619fa9e4066Sahrens 	return (error);
1620fa9e4066Sahrens }
1621fa9e4066Sahrens 
1622fa9e4066Sahrens static int
1623fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1624fa9e4066Sahrens {
1625fa9e4066Sahrens 	spa_t *spa;
1626fa9e4066Sahrens 	int error;
1627fa9e4066Sahrens 
1628fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1629fa9e4066Sahrens 	if (error == 0) {
1630fa9e4066Sahrens 		spa_freeze(spa);
1631fa9e4066Sahrens 		spa_close(spa, FTAG);
1632fa9e4066Sahrens 	}
1633fa9e4066Sahrens 	return (error);
1634fa9e4066Sahrens }
1635fa9e4066Sahrens 
1636eaca9bbdSeschrock static int
1637eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1638eaca9bbdSeschrock {
1639eaca9bbdSeschrock 	spa_t *spa;
1640eaca9bbdSeschrock 	int error;
1641eaca9bbdSeschrock 
164206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
164306eeb2adSek 		return (error);
164406eeb2adSek 
1645ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1646ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1647558d2d50Slling 		spa_close(spa, FTAG);
1648be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1649558d2d50Slling 	}
1650558d2d50Slling 
1651990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
165206eeb2adSek 	spa_close(spa, FTAG);
165306eeb2adSek 
165406eeb2adSek 	return (error);
165506eeb2adSek }
165606eeb2adSek 
165706eeb2adSek static int
165806eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
165906eeb2adSek {
166006eeb2adSek 	spa_t *spa;
166106eeb2adSek 	char *hist_buf;
166206eeb2adSek 	uint64_t size;
166306eeb2adSek 	int error;
166406eeb2adSek 
166506eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1666be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
166706eeb2adSek 
166806eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
166906eeb2adSek 		return (error);
167006eeb2adSek 
1671e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1672d7306b64Sek 		spa_close(spa, FTAG);
1673be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1674d7306b64Sek 	}
1675d7306b64Sek 
167606eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
167706eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
167806eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1679478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1680478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1681478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
168206eeb2adSek 	}
168306eeb2adSek 
168406eeb2adSek 	spa_close(spa, FTAG);
168506eeb2adSek 	kmem_free(hist_buf, size);
168606eeb2adSek 	return (error);
168706eeb2adSek }
168806eeb2adSek 
1689e9103aaeSGarrett D'Amore static int
1690e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1691e9103aaeSGarrett D'Amore {
1692e9103aaeSGarrett D'Amore 	spa_t *spa;
1693e9103aaeSGarrett D'Amore 	int error;
1694e9103aaeSGarrett D'Amore 
1695e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1696e9103aaeSGarrett D'Amore 	if (error == 0) {
1697e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1698e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1699e9103aaeSGarrett D'Amore 	}
1700e9103aaeSGarrett D'Amore 	return (error);
1701e9103aaeSGarrett D'Amore }
1702e9103aaeSGarrett D'Amore 
170355434c77Sek static int
170455434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
170555434c77Sek {
17063b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
170755434c77Sek }
170855434c77Sek 
1709503ad85cSMatthew Ahrens /*
1710503ad85cSMatthew Ahrens  * inputs:
1711503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1712503ad85cSMatthew Ahrens  * zc_obj		object to find
1713503ad85cSMatthew Ahrens  *
1714503ad85cSMatthew Ahrens  * outputs:
1715503ad85cSMatthew Ahrens  * zc_value		name of object
1716503ad85cSMatthew Ahrens  */
171755434c77Sek static int
171855434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
171955434c77Sek {
1720503ad85cSMatthew Ahrens 	objset_t *os;
172155434c77Sek 	int error;
172255434c77Sek 
1723503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1724503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
172555434c77Sek 		return (error);
1726503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1727503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1728be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1729503ad85cSMatthew Ahrens 	}
1730503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
173155434c77Sek 	    sizeof (zc->zc_value));
1732503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
173355434c77Sek 
173455434c77Sek 	return (error);
173555434c77Sek }
173655434c77Sek 
173799d5e173STim Haley /*
173899d5e173STim Haley  * inputs:
173999d5e173STim Haley  * zc_name		name of filesystem
174099d5e173STim Haley  * zc_obj		object to find
174199d5e173STim Haley  *
174299d5e173STim Haley  * outputs:
174399d5e173STim Haley  * zc_stat		stats on object
174499d5e173STim Haley  * zc_value		path to object
174599d5e173STim Haley  */
174699d5e173STim Haley static int
174799d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
174899d5e173STim Haley {
174999d5e173STim Haley 	objset_t *os;
175099d5e173STim Haley 	int error;
175199d5e173STim Haley 
175299d5e173STim Haley 	/* XXX reading from objset not owned */
175399d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
175499d5e173STim Haley 		return (error);
175599d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
175699d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1757be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
175899d5e173STim Haley 	}
175999d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
176099d5e173STim Haley 	    sizeof (zc->zc_value));
176199d5e173STim Haley 	dmu_objset_rele(os, FTAG);
176299d5e173STim Haley 
176399d5e173STim Haley 	return (error);
176499d5e173STim Haley }
176599d5e173STim Haley 
1766fa9e4066Sahrens static int
1767fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1768fa9e4066Sahrens {
1769fa9e4066Sahrens 	spa_t *spa;
1770fa9e4066Sahrens 	int error;
1771e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1772e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1773fa9e4066Sahrens 
1774fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1775fa9e4066Sahrens 	if (error != 0)
1776fa9e4066Sahrens 		return (error);
1777fa9e4066Sahrens 
1778fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1779478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1780fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1781fa94a07fSbrendan 	    &l2cache, &nl2cache);
1782fa94a07fSbrendan 
1783e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1784e7cbe64fSgw 	    &spares, &nspares);
1785e7cbe64fSgw 
1786b1b8ab34Slling 	/*
1787b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1788e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1789e7cbe64fSgw 	 *
1790e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1791e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1792e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1793e7cbe64fSgw 	 *
1794e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1795b1b8ab34Slling 	 */
1796b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
17971195e687SMark J Musante 		nvlist_free(config);
1798b1b8ab34Slling 		spa_close(spa, FTAG);
1799be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1800b1b8ab34Slling 	}
1801b1b8ab34Slling 
1802fa94a07fSbrendan 	if (error == 0) {
1803fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1804fa9e4066Sahrens 		nvlist_free(config);
1805fa9e4066Sahrens 	}
1806fa9e4066Sahrens 	spa_close(spa, FTAG);
1807fa9e4066Sahrens 	return (error);
1808fa9e4066Sahrens }
1809fa9e4066Sahrens 
18103f9d6ad7SLin Ling /*
18113f9d6ad7SLin Ling  * inputs:
18123f9d6ad7SLin Ling  * zc_name		name of the pool
18133f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
18143f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
18153f9d6ad7SLin Ling  */
1816fa9e4066Sahrens static int
1817fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1818fa9e4066Sahrens {
181999653d4eSeschrock 	spa_t *spa;
182099653d4eSeschrock 	int error;
182199653d4eSeschrock 
182299653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
182399653d4eSeschrock 	if (error != 0)
182499653d4eSeschrock 		return (error);
182599653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
182699653d4eSeschrock 	spa_close(spa, FTAG);
182799653d4eSeschrock 	return (error);
1828fa9e4066Sahrens }
1829fa9e4066Sahrens 
1830fa9e4066Sahrens static int
18313d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1832fa9e4066Sahrens {
1833fa9e4066Sahrens 	spa_t *spa;
1834fa9e4066Sahrens 	int error;
18353d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1836fa9e4066Sahrens 
183706eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1838fa9e4066Sahrens 		return (error);
18393d7072f8Seschrock 	switch (zc->zc_cookie) {
18403d7072f8Seschrock 	case VDEV_STATE_ONLINE:
18413d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
18423d7072f8Seschrock 		break;
1843fa9e4066Sahrens 
18443d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
18453d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
18463d7072f8Seschrock 		break;
1847fa9e4066Sahrens 
18483d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1849069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1850069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1851069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1852069f55e2SEric Schrock 
1853069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
18543d7072f8Seschrock 		break;
18553d7072f8Seschrock 
18563d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1857069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1858069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1859069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1860069f55e2SEric Schrock 
1861069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
18623d7072f8Seschrock 		break;
18633d7072f8Seschrock 
18643d7072f8Seschrock 	default:
1865be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
18663d7072f8Seschrock 	}
18673d7072f8Seschrock 	zc->zc_cookie = newstate;
1868fa9e4066Sahrens 	spa_close(spa, FTAG);
1869fa9e4066Sahrens 	return (error);
1870fa9e4066Sahrens }
1871fa9e4066Sahrens 
1872fa9e4066Sahrens static int
1873fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1874fa9e4066Sahrens {
1875fa9e4066Sahrens 	spa_t *spa;
1876fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1877fa9e4066Sahrens 	nvlist_t *config;
1878fa9e4066Sahrens 	int error;
1879fa9e4066Sahrens 
188006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1881fa9e4066Sahrens 		return (error);
1882fa9e4066Sahrens 
1883990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1884478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1885ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1886fa9e4066Sahrens 		nvlist_free(config);
1887fa9e4066Sahrens 	}
1888fa9e4066Sahrens 
1889fa9e4066Sahrens 	spa_close(spa, FTAG);
1890fa9e4066Sahrens 	return (error);
1891fa9e4066Sahrens }
1892fa9e4066Sahrens 
1893fa9e4066Sahrens static int
1894fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1895fa9e4066Sahrens {
1896fa9e4066Sahrens 	spa_t *spa;
1897fa9e4066Sahrens 	int error;
1898fa9e4066Sahrens 
189906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1900fa9e4066Sahrens 		return (error);
1901fa9e4066Sahrens 
19028ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1903fa9e4066Sahrens 
1904fa9e4066Sahrens 	spa_close(spa, FTAG);
1905fa9e4066Sahrens 	return (error);
1906fa9e4066Sahrens }
1907fa9e4066Sahrens 
19081195e687SMark J Musante static int
19091195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
19101195e687SMark J Musante {
19111195e687SMark J Musante 	spa_t *spa;
19121195e687SMark J Musante 	nvlist_t *config, *props = NULL;
19131195e687SMark J Musante 	int error;
19141195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
19151195e687SMark J Musante 
19161195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
19171195e687SMark J Musante 		return (error);
19181195e687SMark J Musante 
19191195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
19201195e687SMark J Musante 	    zc->zc_iflags, &config)) {
19211195e687SMark J Musante 		spa_close(spa, FTAG);
19221195e687SMark J Musante 		return (error);
19231195e687SMark J Musante 	}
19241195e687SMark J Musante 
19251195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
19261195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19271195e687SMark J Musante 	    zc->zc_iflags, &props))) {
19281195e687SMark J Musante 		spa_close(spa, FTAG);
19291195e687SMark J Musante 		nvlist_free(config);
19301195e687SMark J Musante 		return (error);
19311195e687SMark J Musante 	}
19321195e687SMark J Musante 
19331195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
19341195e687SMark J Musante 
19351195e687SMark J Musante 	spa_close(spa, FTAG);
19361195e687SMark J Musante 
19371195e687SMark J Musante 	nvlist_free(config);
19381195e687SMark J Musante 	nvlist_free(props);
19391195e687SMark J Musante 
19401195e687SMark J Musante 	return (error);
19411195e687SMark J Musante }
19421195e687SMark J Musante 
1943c67d9675Seschrock static int
1944c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1945c67d9675Seschrock {
1946c67d9675Seschrock 	spa_t *spa;
1947e9dbad6fSeschrock 	char *path = zc->zc_value;
1948ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
1949c67d9675Seschrock 	int error;
1950c67d9675Seschrock 
1951c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
1952c67d9675Seschrock 	if (error != 0)
1953c67d9675Seschrock 		return (error);
1954c67d9675Seschrock 
1955c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
1956c67d9675Seschrock 	spa_close(spa, FTAG);
1957c67d9675Seschrock 	return (error);
1958c67d9675Seschrock }
1959c67d9675Seschrock 
19606809eb4eSEric Schrock static int
19616809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
19626809eb4eSEric Schrock {
19636809eb4eSEric Schrock 	spa_t *spa;
19646809eb4eSEric Schrock 	char *fru = zc->zc_value;
19656809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
19666809eb4eSEric Schrock 	int error;
19676809eb4eSEric Schrock 
19686809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
19696809eb4eSEric Schrock 	if (error != 0)
19706809eb4eSEric Schrock 		return (error);
19716809eb4eSEric Schrock 
19726809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
19736809eb4eSEric Schrock 	spa_close(spa, FTAG);
19746809eb4eSEric Schrock 	return (error);
19756809eb4eSEric Schrock }
19766809eb4eSEric Schrock 
1977fa9e4066Sahrens static int
1978a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1979fa9e4066Sahrens {
1980a7f53a56SChris Kirby 	int error = 0;
19817f7322feSeschrock 	nvlist_t *nv;
1982fa9e4066Sahrens 
1983a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1984fa9e4066Sahrens 
19855ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
198692241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1987a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
1988432f72fdSahrens 		/*
1989bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
1990432f72fdSahrens 		 * which we aren't supposed to do with a
1991745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
1992432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
1993503ad85cSMatthew Ahrens 		 * XXX reading with out owning
1994432f72fdSahrens 		 */
199519b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
199619b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
199719b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
199819b94df9SMatthew Ahrens 			if (error == EIO)
199919b94df9SMatthew Ahrens 				return (error);
2000fb09f5aaSMadhav Suresh 			VERIFY0(error);
2001e7437265Sahrens 		}
2002e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
20037f7322feSeschrock 		nvlist_free(nv);
20047f7322feSeschrock 	}
2005fa9e4066Sahrens 
2006a7f53a56SChris Kirby 	return (error);
2007a7f53a56SChris Kirby }
2008a7f53a56SChris Kirby 
2009a7f53a56SChris Kirby /*
2010a7f53a56SChris Kirby  * inputs:
2011a7f53a56SChris Kirby  * zc_name		name of filesystem
2012a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2013a7f53a56SChris Kirby  *
2014a7f53a56SChris Kirby  * outputs:
2015a7f53a56SChris Kirby  * zc_objset_stats	stats
2016a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2017a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2018a7f53a56SChris Kirby  */
2019a7f53a56SChris Kirby static int
2020a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2021a7f53a56SChris Kirby {
20223b2aab18SMatthew Ahrens 	objset_t *os;
2023a7f53a56SChris Kirby 	int error;
2024a7f53a56SChris Kirby 
20253b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
20263b2aab18SMatthew Ahrens 	if (error == 0) {
20273b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
20283b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
20293b2aab18SMatthew Ahrens 	}
2030a7f53a56SChris Kirby 
2031fa9e4066Sahrens 	return (error);
2032fa9e4066Sahrens }
2033fa9e4066Sahrens 
203492241e0bSTom Erickson /*
203592241e0bSTom Erickson  * inputs:
203692241e0bSTom Erickson  * zc_name		name of filesystem
203792241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
203892241e0bSTom Erickson  *
203992241e0bSTom Erickson  * outputs:
204092241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
204192241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
204292241e0bSTom Erickson  *
204392241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
204492241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
204592241e0bSTom Erickson  * local property values.
204692241e0bSTom Erickson  */
204792241e0bSTom Erickson static int
204892241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
204992241e0bSTom Erickson {
20503b2aab18SMatthew Ahrens 	int error = 0;
205192241e0bSTom Erickson 	nvlist_t *nv;
205292241e0bSTom Erickson 
205392241e0bSTom Erickson 	/*
205492241e0bSTom Erickson 	 * Without this check, we would return local property values if the
205592241e0bSTom Erickson 	 * caller has not already received properties on or after
205692241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
205792241e0bSTom Erickson 	 */
20583b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2059be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
206092241e0bSTom Erickson 
206192241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
20623b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
206392241e0bSTom Erickson 		error = put_nvlist(zc, nv);
206492241e0bSTom Erickson 		nvlist_free(nv);
206592241e0bSTom Erickson 	}
206692241e0bSTom Erickson 
206792241e0bSTom Erickson 	return (error);
206892241e0bSTom Erickson }
206992241e0bSTom Erickson 
2070de8267e0Stimh static int
2071de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2072de8267e0Stimh {
2073de8267e0Stimh 	uint64_t value;
2074de8267e0Stimh 	int error;
2075de8267e0Stimh 
2076de8267e0Stimh 	/*
2077de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2078de8267e0Stimh 	 * the default value (if there is one).
2079de8267e0Stimh 	 */
2080de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2081de8267e0Stimh 		return (error);
2082de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2083de8267e0Stimh 	return (0);
2084de8267e0Stimh }
2085de8267e0Stimh 
20863cb34c60Sahrens /*
20873cb34c60Sahrens  * inputs:
20883cb34c60Sahrens  * zc_name		name of filesystem
2089de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
20903cb34c60Sahrens  *
20913cb34c60Sahrens  * outputs:
2092de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2093de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
20943cb34c60Sahrens  */
2095bd00f61bSrm static int
2096de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2097bd00f61bSrm {
2098de8267e0Stimh 	objset_t *os;
2099de8267e0Stimh 	int err;
2100bd00f61bSrm 
2101503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2102503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2103de8267e0Stimh 		return (err);
2104bd00f61bSrm 
2105bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2106bd00f61bSrm 
2107bd00f61bSrm 	/*
2108de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2109745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2110745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2111bd00f61bSrm 	 */
2112de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2113de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2114de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2115de8267e0Stimh 		nvlist_t *nv;
2116de8267e0Stimh 
2117de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2118de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2119de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2120de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2121de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2122de8267e0Stimh 			err = put_nvlist(zc, nv);
2123de8267e0Stimh 		nvlist_free(nv);
2124de8267e0Stimh 	} else {
2125be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2126de8267e0Stimh 	}
2127503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2128de8267e0Stimh 	return (err);
2129bd00f61bSrm }
2130bd00f61bSrm 
213114843421SMatthew Ahrens static boolean_t
213214843421SMatthew Ahrens dataset_name_hidden(const char *name)
213314843421SMatthew Ahrens {
213414843421SMatthew Ahrens 	/*
213514843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
213614843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
213714843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
213814843421SMatthew Ahrens 	 */
213914843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
214014843421SMatthew Ahrens 		return (B_TRUE);
214114843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
214214843421SMatthew Ahrens 		return (B_TRUE);
214314843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
214414843421SMatthew Ahrens 		return (B_TRUE);
214514843421SMatthew Ahrens 	return (B_FALSE);
214614843421SMatthew Ahrens }
214714843421SMatthew Ahrens 
2148de8267e0Stimh /*
2149de8267e0Stimh  * inputs:
2150de8267e0Stimh  * zc_name		name of filesystem
2151de8267e0Stimh  * zc_cookie		zap cursor
2152de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2153de8267e0Stimh  *
2154de8267e0Stimh  * outputs:
2155de8267e0Stimh  * zc_name		name of next filesystem
215614843421SMatthew Ahrens  * zc_cookie		zap cursor
2157de8267e0Stimh  * zc_objset_stats	stats
2158de8267e0Stimh  * zc_nvlist_dst	property nvlist
2159de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2160de8267e0Stimh  */
2161fa9e4066Sahrens static int
2162fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2163fa9e4066Sahrens {
216487e5029aSahrens 	objset_t *os;
2165fa9e4066Sahrens 	int error;
2166fa9e4066Sahrens 	char *p;
2167620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2168fa9e4066Sahrens 
2169620252bcSChris Kirby top:
2170503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
217187e5029aSahrens 		if (error == ENOENT)
2172be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
217387e5029aSahrens 		return (error);
2174fa9e4066Sahrens 	}
2175fa9e4066Sahrens 
2176fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2177fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2178fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2179fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2180fa9e4066Sahrens 
2181fa9e4066Sahrens 	do {
218287e5029aSahrens 		error = dmu_dir_list_next(os,
218387e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
218487e5029aSahrens 		    NULL, &zc->zc_cookie);
2185fa9e4066Sahrens 		if (error == ENOENT)
2186be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
218719b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2188503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2189fa9e4066Sahrens 
2190681d9761SEric Taylor 	/*
2191681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2192681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2193681d9761SEric Taylor 	 */
2194620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
219587e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2196620252bcSChris Kirby 		if (error == ENOENT) {
2197620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2198620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2199620252bcSChris Kirby 			goto top;
2200620252bcSChris Kirby 		}
2201620252bcSChris Kirby 	}
2202fa9e4066Sahrens 	return (error);
2203fa9e4066Sahrens }
2204fa9e4066Sahrens 
22053cb34c60Sahrens /*
22063cb34c60Sahrens  * inputs:
22073cb34c60Sahrens  * zc_name		name of filesystem
22083cb34c60Sahrens  * zc_cookie		zap cursor
22093cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22103cb34c60Sahrens  *
22113cb34c60Sahrens  * outputs:
22123cb34c60Sahrens  * zc_name		name of next snapshot
22133cb34c60Sahrens  * zc_objset_stats	stats
22143cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22153cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22163cb34c60Sahrens  */
2217fa9e4066Sahrens static int
2218fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2219fa9e4066Sahrens {
222087e5029aSahrens 	objset_t *os;
2221fa9e4066Sahrens 	int error;
2222fa9e4066Sahrens 
2223503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
22243b2aab18SMatthew Ahrens 	if (error != 0) {
2225745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
22263b2aab18SMatthew Ahrens 	}
2227fa9e4066Sahrens 
2228b81d61a6Slling 	/*
2229b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2230b81d61a6Slling 	 * so exit immediately.
2231b81d61a6Slling 	 */
2232b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2233503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2234be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2235fa9e4066Sahrens 	}
2236fa9e4066Sahrens 
223787e5029aSahrens 	error = dmu_snapshot_list_next(os,
223887e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2239a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2240a7f53a56SChris Kirby 	    NULL);
2241a7f53a56SChris Kirby 
2242620252bcSChris Kirby 	if (error == 0) {
2243a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2244a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2245a7f53a56SChris Kirby 
2246a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
22473b2aab18SMatthew Ahrens 		if (error == 0) {
2248a7f53a56SChris Kirby 			objset_t *ossnap;
2249a7f53a56SChris Kirby 
2250a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2251a7f53a56SChris Kirby 			if (error == 0)
2252a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2253a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2254620252bcSChris Kirby 		}
2255620252bcSChris Kirby 	} else if (error == ENOENT) {
2256be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2257620252bcSChris Kirby 	}
2258fa9e4066Sahrens 
2259a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
22603cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
22613b2aab18SMatthew Ahrens 	if (error != 0)
22623cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2263fa9e4066Sahrens 	return (error);
2264fa9e4066Sahrens }
2265fa9e4066Sahrens 
226692241e0bSTom Erickson static int
226792241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2268fa9e4066Sahrens {
226992241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
227092241e0bSTom Erickson 	uint64_t *valary;
227192241e0bSTom Erickson 	unsigned int vallen;
227292241e0bSTom Erickson 	const char *domain;
2273eeb85002STim Haley 	char *dash;
227492241e0bSTom Erickson 	zfs_userquota_prop_t type;
227592241e0bSTom Erickson 	uint64_t rid;
227692241e0bSTom Erickson 	uint64_t quota;
227792241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
227892241e0bSTom Erickson 	int err;
227992241e0bSTom Erickson 
228092241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
228192241e0bSTom Erickson 		nvlist_t *attrs;
228292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2283eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2284eeb85002STim Haley 		    &pair) != 0)
2285be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
228692241e0bSTom Erickson 	}
2287e9dbad6fSeschrock 
2288ecd6cf80Smarks 	/*
2289eeb85002STim Haley 	 * A correctly constructed propname is encoded as
229092241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2291ecd6cf80Smarks 	 */
2292eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2293eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2294eeb85002STim Haley 	    vallen != 3)
2295be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2296eeb85002STim Haley 
2297eeb85002STim Haley 	domain = dash + 1;
2298eeb85002STim Haley 	type = valary[0];
2299eeb85002STim Haley 	rid = valary[1];
2300eeb85002STim Haley 	quota = valary[2];
2301e9dbad6fSeschrock 
23021412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
230392241e0bSTom Erickson 	if (err == 0) {
230492241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
230592241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
230692241e0bSTom Erickson 	}
2307e9dbad6fSeschrock 
230892241e0bSTom Erickson 	return (err);
230992241e0bSTom Erickson }
231014843421SMatthew Ahrens 
231192241e0bSTom Erickson /*
231292241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
231392241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
231492241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
231592241e0bSTom Erickson  *
2316eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
231792241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
231892241e0bSTom Erickson  */
231992241e0bSTom Erickson static int
232092241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
232192241e0bSTom Erickson     nvpair_t *pair)
232292241e0bSTom Erickson {
232392241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
232492241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
232592241e0bSTom Erickson 	uint64_t intval;
232692241e0bSTom Erickson 	int err;
2327fa9e4066Sahrens 
232892241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
232992241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
233092241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
233192241e0bSTom Erickson 		return (-1);
233292241e0bSTom Erickson 	}
233314843421SMatthew Ahrens 
233492241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
233592241e0bSTom Erickson 		nvlist_t *attrs;
233692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
233792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
233892241e0bSTom Erickson 		    &pair) == 0);
233992241e0bSTom Erickson 	}
2340db870a07Sahrens 
234192241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
234292241e0bSTom Erickson 		return (-1);
2343b24ab676SJeff Bonwick 
234492241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
234540feaa91Sahrens 
234692241e0bSTom Erickson 	switch (prop) {
234792241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
234892241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
234992241e0bSTom Erickson 		break;
235092241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
23513b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
235292241e0bSTom Erickson 		break;
235392241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
235492241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
235592241e0bSTom Erickson 		break;
235692241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
23573b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
235892241e0bSTom Erickson 		break;
235992241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2360c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
236192241e0bSTom Erickson 		break;
236292241e0bSTom Erickson 	case ZFS_PROP_VERSION:
236392241e0bSTom Erickson 	{
236492241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
23659e6eda55Smarks 
23661412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2367b24ab676SJeff Bonwick 			break;
2368b24ab676SJeff Bonwick 
236992241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
237092241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2371d0f3f37eSMark Shellenbaum 
237292241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2373b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2374b16da2e2SGeorge Wilson 
2375b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2376b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2377b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2378b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
237940feaa91Sahrens 		}
238092241e0bSTom Erickson 		break;
2381ecd6cf80Smarks 	}
2382a6f561b4SSašo Kiselkov 	case ZFS_PROP_COMPRESSION:
2383a6f561b4SSašo Kiselkov 	{
2384a6f561b4SSašo Kiselkov 		if (intval == ZIO_COMPRESS_LZ4) {
2385a6f561b4SSašo Kiselkov 			spa_t *spa;
2386a6f561b4SSašo Kiselkov 
2387a6f561b4SSašo Kiselkov 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2388a6f561b4SSašo Kiselkov 				return (err);
2389a6f561b4SSašo Kiselkov 
2390a6f561b4SSašo Kiselkov 			/*
2391a6f561b4SSašo Kiselkov 			 * Setting the LZ4 compression algorithm activates
2392a6f561b4SSašo Kiselkov 			 * the feature.
2393a6f561b4SSašo Kiselkov 			 */
2394*2acef22dSMatthew Ahrens 			if (!spa_feature_is_active(spa,
2395*2acef22dSMatthew Ahrens 			    SPA_FEATURE_LZ4_COMPRESS)) {
23963b2aab18SMatthew Ahrens 				if ((err = zfs_prop_activate_feature(spa,
2397*2acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) != 0) {
2398a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
2399a6f561b4SSašo Kiselkov 					return (err);
2400a6f561b4SSašo Kiselkov 				}
2401a6f561b4SSašo Kiselkov 			}
2402a6f561b4SSašo Kiselkov 
2403a6f561b4SSašo Kiselkov 			spa_close(spa, FTAG);
2404a6f561b4SSašo Kiselkov 		}
2405a6f561b4SSašo Kiselkov 		/*
2406a6f561b4SSašo Kiselkov 		 * We still want the default set action to be performed in the
2407a6f561b4SSašo Kiselkov 		 * caller, we only performed zfeature settings here.
2408a6f561b4SSašo Kiselkov 		 */
2409a6f561b4SSašo Kiselkov 		err = -1;
2410a6f561b4SSašo Kiselkov 		break;
2411a6f561b4SSašo Kiselkov 	}
2412ecd6cf80Smarks 
241392241e0bSTom Erickson 	default:
241492241e0bSTom Erickson 		err = -1;
241592241e0bSTom Erickson 	}
2416e9dbad6fSeschrock 
241792241e0bSTom Erickson 	return (err);
241892241e0bSTom Erickson }
2419a9799022Sck 
242092241e0bSTom Erickson /*
242192241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
24224445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
24234445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
24244445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
24254445fffbSMatthew Ahrens  * corresponding error numbers.
242692241e0bSTom Erickson  *
24274445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
24284445fffbSMatthew Ahrens  * modified.
242992241e0bSTom Erickson  */
243092241e0bSTom Erickson int
243192241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
24324445fffbSMatthew Ahrens     nvlist_t *errlist)
243392241e0bSTom Erickson {
243492241e0bSTom Erickson 	nvpair_t *pair;
243592241e0bSTom Erickson 	nvpair_t *propval;
243602e383d1STom Erickson 	int rv = 0;
243792241e0bSTom Erickson 	uint64_t intval;
243892241e0bSTom Erickson 	char *strval;
24394445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
24404445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2441a9799022Sck 
244292241e0bSTom Erickson retry:
244392241e0bSTom Erickson 	pair = NULL;
244492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
244592241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
244692241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2447cfa69fd2STom Erickson 		int err = 0;
2448e9dbad6fSeschrock 
244992241e0bSTom Erickson 		/* decode the property value */
245092241e0bSTom Erickson 		propval = pair;
245192241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
245292241e0bSTom Erickson 			nvlist_t *attrs;
24534445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2454eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2455eeb85002STim Haley 			    &propval) != 0)
2456be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
245714843421SMatthew Ahrens 		}
2458e9dbad6fSeschrock 
245992241e0bSTom Erickson 		/* Validate value type */
2460eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
246192241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
246292241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2463be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
246492241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
246592241e0bSTom Erickson 				if (nvpair_type(propval) !=
246692241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2467be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
246819b94df9SMatthew Ahrens 			} else {
2469be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
24704201a95eSRic Aleshire 			}
2471eeb85002STim Haley 		} else if (err == 0) {
247292241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
247392241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2474be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
247592241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2476a2eea2e1Sahrens 				const char *unused;
2477a2eea2e1Sahrens 
24784445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2479e9dbad6fSeschrock 
2480e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
248191ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2482e9dbad6fSeschrock 					break;
248391ebeef5Sahrens 				case PROP_TYPE_STRING:
2484be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
248592241e0bSTom Erickson 					break;
248691ebeef5Sahrens 				case PROP_TYPE_INDEX:
2487acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
248892241e0bSTom Erickson 					    intval, &unused) != 0)
2489be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2490e9dbad6fSeschrock 					break;
2491e9dbad6fSeschrock 				default:
2492e7437265Sahrens 					cmn_err(CE_PANIC,
2493e7437265Sahrens 					    "unknown property type");
2494e9dbad6fSeschrock 				}
2495e9dbad6fSeschrock 			} else {
2496be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2497e9dbad6fSeschrock 			}
2498e9dbad6fSeschrock 		}
249992241e0bSTom Erickson 
250092241e0bSTom Erickson 		/* Validate permissions */
250192241e0bSTom Erickson 		if (err == 0)
250292241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
250392241e0bSTom Erickson 
250492241e0bSTom Erickson 		if (err == 0) {
250592241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
250692241e0bSTom Erickson 			if (err == -1) {
250792241e0bSTom Erickson 				/*
250892241e0bSTom Erickson 				 * For better performance we build up a list of
250992241e0bSTom Erickson 				 * properties to set in a single transaction.
251092241e0bSTom Erickson 				 */
251192241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
251292241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
251392241e0bSTom Erickson 				/*
251492241e0bSTom Erickson 				 * This may be a spurious error caused by
251592241e0bSTom Erickson 				 * receiving quota and reservation out of order.
251692241e0bSTom Erickson 				 * Try again in a second pass.
251792241e0bSTom Erickson 				 */
251892241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
251992241e0bSTom Erickson 			}
252092241e0bSTom Erickson 		}
252192241e0bSTom Erickson 
25224445fffbSMatthew Ahrens 		if (err != 0) {
25234445fffbSMatthew Ahrens 			if (errlist != NULL)
25244445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
25254445fffbSMatthew Ahrens 			rv = err;
25264445fffbSMatthew Ahrens 		}
2527e9dbad6fSeschrock 	}
2528e9dbad6fSeschrock 
252992241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
253092241e0bSTom Erickson 		nvl = retrynvl;
253192241e0bSTom Erickson 		goto retry;
253292241e0bSTom Erickson 	}
253392241e0bSTom Erickson 
253492241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
253592241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
253692241e0bSTom Erickson 		/*
253792241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
253892241e0bSTom Erickson 		 * can, so try setting them individually.
253992241e0bSTom Erickson 		 */
254092241e0bSTom Erickson 		pair = NULL;
254192241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
254292241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2543cfa69fd2STom Erickson 			int err = 0;
254492241e0bSTom Erickson 
254592241e0bSTom Erickson 			propval = pair;
254692241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
254792241e0bSTom Erickson 				nvlist_t *attrs;
25484445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
25494445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
25504445fffbSMatthew Ahrens 				    ZPROP_VALUE);
255192241e0bSTom Erickson 			}
255292241e0bSTom Erickson 
255392241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
25544445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
25553b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
25563b2aab18SMatthew Ahrens 				    source, strval);
255792241e0bSTom Erickson 			} else {
25584445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
25593b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
25603b2aab18SMatthew Ahrens 				    intval);
256192241e0bSTom Erickson 			}
256292241e0bSTom Erickson 
256392241e0bSTom Erickson 			if (err != 0) {
25644445fffbSMatthew Ahrens 				if (errlist != NULL) {
25654445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
25664445fffbSMatthew Ahrens 					    err);
25674445fffbSMatthew Ahrens 				}
25684445fffbSMatthew Ahrens 				rv = err;
256992241e0bSTom Erickson 			}
257092241e0bSTom Erickson 		}
25715c0b6a79SRich Morris 	}
25725c0b6a79SRich Morris 	nvlist_free(genericnvl);
257392241e0bSTom Erickson 	nvlist_free(retrynvl);
257492241e0bSTom Erickson 
257592241e0bSTom Erickson 	return (rv);
2576fa9e4066Sahrens }
2577fa9e4066Sahrens 
2578ea2f5b9eSMatthew Ahrens /*
2579ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2580ea2f5b9eSMatthew Ahrens  */
2581ea2f5b9eSMatthew Ahrens static int
25824445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2583ea2f5b9eSMatthew Ahrens {
258492241e0bSTom Erickson 	nvpair_t *pair = NULL;
2585ea2f5b9eSMatthew Ahrens 	int error = 0;
2586ea2f5b9eSMatthew Ahrens 
258792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
258892241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2589ea2f5b9eSMatthew Ahrens 		char *valstr;
2590ea2f5b9eSMatthew Ahrens 
2591ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
259292241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2593be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2594ea2f5b9eSMatthew Ahrens 
2595ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2596ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2597ea2f5b9eSMatthew Ahrens 			return (error);
2598ea2f5b9eSMatthew Ahrens 
2599ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2600be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2601ea2f5b9eSMatthew Ahrens 
260292241e0bSTom Erickson 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
2603ea2f5b9eSMatthew Ahrens 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
2604ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2605ea2f5b9eSMatthew Ahrens 	}
2606ea2f5b9eSMatthew Ahrens 	return (0);
2607ea2f5b9eSMatthew Ahrens }
2608ea2f5b9eSMatthew Ahrens 
260992241e0bSTom Erickson static void
261092241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
261192241e0bSTom Erickson {
261292241e0bSTom Erickson 	nvpair_t *pair;
261392241e0bSTom Erickson 
261492241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
261592241e0bSTom Erickson 
261692241e0bSTom Erickson 	pair = NULL;
261792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
261892241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
261992241e0bSTom Erickson 			continue;
262092241e0bSTom Erickson 
262192241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
262292241e0bSTom Erickson 	}
262392241e0bSTom Erickson }
262492241e0bSTom Erickson 
262592241e0bSTom Erickson static int
26263b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
262792241e0bSTom Erickson     nvlist_t *skipped)
262892241e0bSTom Erickson {
262992241e0bSTom Erickson 	int err = 0;
263092241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
263192241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
263292241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
263392241e0bSTom Erickson 		/*
263492241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
263592241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
263692241e0bSTom Erickson 		 */
263792241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
26383b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
26393b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
264092241e0bSTom Erickson 	}
264192241e0bSTom Erickson 	nvlist_free(cleared_props);
264292241e0bSTom Erickson 	return (err);
264392241e0bSTom Erickson }
264492241e0bSTom Erickson 
26453cb34c60Sahrens /*
26463cb34c60Sahrens  * inputs:
26473cb34c60Sahrens  * zc_name		name of filesystem
26485c0b6a79SRich Morris  * zc_value		name of property to set
26493cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
265092241e0bSTom Erickson  * zc_cookie		received properties flag
26513cb34c60Sahrens  *
265292241e0bSTom Erickson  * outputs:
265392241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
26543cb34c60Sahrens  */
2655fa9e4066Sahrens static int
2656e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2657fa9e4066Sahrens {
2658e9dbad6fSeschrock 	nvlist_t *nvl;
265992241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
266092241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
266192241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
26624445fffbSMatthew Ahrens 	nvlist_t *errors;
2663e9dbad6fSeschrock 	int error;
2664e9dbad6fSeschrock 
2665990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2666478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2667e9dbad6fSeschrock 		return (error);
2668e9dbad6fSeschrock 
266992241e0bSTom Erickson 	if (received) {
2670bb0ade09Sahrens 		nvlist_t *origprops;
2671bb0ade09Sahrens 
26723b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
26733b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
26743b2aab18SMatthew Ahrens 			    origprops, nvl);
26753b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2676bb0ade09Sahrens 		}
26773b2aab18SMatthew Ahrens 
26783b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2679bb0ade09Sahrens 	}
2680bb0ade09Sahrens 
26814445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
26823b2aab18SMatthew Ahrens 	if (error == 0)
26833b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
268492241e0bSTom Erickson 
268592241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
268692241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
268792241e0bSTom Erickson 	}
2688ecd6cf80Smarks 
268992241e0bSTom Erickson 	nvlist_free(errors);
2690e9dbad6fSeschrock 	nvlist_free(nvl);
2691e9dbad6fSeschrock 	return (error);
2692fa9e4066Sahrens }
2693fa9e4066Sahrens 
26943cb34c60Sahrens /*
26953cb34c60Sahrens  * inputs:
26963cb34c60Sahrens  * zc_name		name of filesystem
26973cb34c60Sahrens  * zc_value		name of property to inherit
269892241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
26993cb34c60Sahrens  *
27003cb34c60Sahrens  * outputs:		none
27013cb34c60Sahrens  */
2702e45ce728Sahrens static int
2703e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2704e45ce728Sahrens {
270592241e0bSTom Erickson 	const char *propname = zc->zc_value;
270692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
270792241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
270892241e0bSTom Erickson 	zprop_source_t source = (received
270992241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
271092241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
271192241e0bSTom Erickson 
271292241e0bSTom Erickson 	if (received) {
271392241e0bSTom Erickson 		nvlist_t *dummy;
271492241e0bSTom Erickson 		nvpair_t *pair;
271592241e0bSTom Erickson 		zprop_type_t type;
271692241e0bSTom Erickson 		int err;
271792241e0bSTom Erickson 
271892241e0bSTom Erickson 		/*
271992241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
272092241e0bSTom Erickson 		 * nvpair with type info.
272192241e0bSTom Erickson 		 */
272292241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
272392241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2724be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
272592241e0bSTom Erickson 
272692241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2727a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2728a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2729be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
273092241e0bSTom Erickson 		} else {
273192241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
273292241e0bSTom Erickson 		}
273392241e0bSTom Erickson 
273492241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
273592241e0bSTom Erickson 
273692241e0bSTom Erickson 		switch (type) {
273792241e0bSTom Erickson 		case PROP_TYPE_STRING:
273892241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
273992241e0bSTom Erickson 			break;
274092241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
274192241e0bSTom Erickson 		case PROP_TYPE_INDEX:
274292241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
274392241e0bSTom Erickson 			break;
274492241e0bSTom Erickson 		default:
274592241e0bSTom Erickson 			nvlist_free(dummy);
2746be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
274792241e0bSTom Erickson 		}
274892241e0bSTom Erickson 
274992241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
275092241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
275192241e0bSTom Erickson 		nvlist_free(dummy);
275292241e0bSTom Erickson 		if (err != -1)
275392241e0bSTom Erickson 			return (err); /* special property already handled */
275492241e0bSTom Erickson 	} else {
275592241e0bSTom Erickson 		/*
275692241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
275792241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
275892241e0bSTom Erickson 		 * and reservation to the received or default values even though
275992241e0bSTom Erickson 		 * they are not considered inheritable.
276092241e0bSTom Erickson 		 */
276192241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2762be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
276392241e0bSTom Erickson 	}
276492241e0bSTom Erickson 
27654445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
27663b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2767e45ce728Sahrens }
2768e45ce728Sahrens 
2769b1b8ab34Slling static int
277011a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2771b1b8ab34Slling {
2772990b4856Slling 	nvlist_t *props;
2773b1b8ab34Slling 	spa_t *spa;
2774990b4856Slling 	int error;
277592241e0bSTom Erickson 	nvpair_t *pair;
2776b1b8ab34Slling 
277792241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
277892241e0bSTom Erickson 	    zc->zc_iflags, &props))
2779b1b8ab34Slling 		return (error);
2780b1b8ab34Slling 
2781379c004dSEric Schrock 	/*
2782379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2783379c004dSEric Schrock 	 * to handle the faulted case.
2784379c004dSEric Schrock 	 */
278592241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
278692241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2787379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
278892241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2789379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2790379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2791379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2792379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2793379c004dSEric Schrock 		}
2794379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2795b693757aSEric Schrock 		if (spa != NULL) {
2796b693757aSEric Schrock 			nvlist_free(props);
2797379c004dSEric Schrock 			return (0);
2798b693757aSEric Schrock 		}
2799379c004dSEric Schrock 	}
2800379c004dSEric Schrock 
2801b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2802990b4856Slling 		nvlist_free(props);
2803b1b8ab34Slling 		return (error);
2804b1b8ab34Slling 	}
2805b1b8ab34Slling 
2806990b4856Slling 	error = spa_prop_set(spa, props);
2807b1b8ab34Slling 
2808990b4856Slling 	nvlist_free(props);
2809b1b8ab34Slling 	spa_close(spa, FTAG);
2810b1b8ab34Slling 
2811b1b8ab34Slling 	return (error);
2812b1b8ab34Slling }
2813b1b8ab34Slling 
2814b1b8ab34Slling static int
281511a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2816b1b8ab34Slling {
2817b1b8ab34Slling 	spa_t *spa;
2818b1b8ab34Slling 	int error;
2819b1b8ab34Slling 	nvlist_t *nvp = NULL;
2820b1b8ab34Slling 
2821379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2822379c004dSEric Schrock 		/*
2823379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2824379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2825379c004dSEric Schrock 		 * anyway.
2826379c004dSEric Schrock 		 */
2827379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2828379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2829379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2830379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2831379c004dSEric Schrock 	} else {
2832379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2833379c004dSEric Schrock 		spa_close(spa, FTAG);
2834379c004dSEric Schrock 	}
2835b1b8ab34Slling 
2836b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2837b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2838b1b8ab34Slling 	else
2839be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2840b1b8ab34Slling 
2841379c004dSEric Schrock 	nvlist_free(nvp);
2842b1b8ab34Slling 	return (error);
2843b1b8ab34Slling }
2844b1b8ab34Slling 
28453cb34c60Sahrens /*
28463cb34c60Sahrens  * inputs:
28473cb34c60Sahrens  * zc_name		name of filesystem
28483cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
28493cb34c60Sahrens  * zc_perm_action	allow/unallow flag
28503cb34c60Sahrens  *
28513cb34c60Sahrens  * outputs:		none
28523cb34c60Sahrens  */
2853ecd6cf80Smarks static int
2854ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2855ecd6cf80Smarks {
2856ecd6cf80Smarks 	int error;
2857ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2858ecd6cf80Smarks 
2859990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2860478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2861ecd6cf80Smarks 		return (error);
2862ecd6cf80Smarks 
2863ecd6cf80Smarks 	/*
2864ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2865ecd6cf80Smarks 	 */
2866ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2867ecd6cf80Smarks 		nvlist_free(fsaclnv);
2868be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2869ecd6cf80Smarks 	}
2870ecd6cf80Smarks 
2871ecd6cf80Smarks 	/*
2872ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2873ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2874ecd6cf80Smarks 	 * the nvlist(s)
2875ecd6cf80Smarks 	 */
2876ecd6cf80Smarks 
287791ebeef5Sahrens 	error = secpolicy_zfs(CRED());
28783b2aab18SMatthew Ahrens 	if (error != 0) {
287991ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
288091ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
288191ebeef5Sahrens 			    fsaclnv, CRED());
288291ebeef5Sahrens 		} else {
288391ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
288491ebeef5Sahrens 			    fsaclnv, CRED());
288591ebeef5Sahrens 		}
2886ecd6cf80Smarks 	}
2887ecd6cf80Smarks 
2888ecd6cf80Smarks 	if (error == 0)
2889ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2890ecd6cf80Smarks 
2891ecd6cf80Smarks 	nvlist_free(fsaclnv);
2892ecd6cf80Smarks 	return (error);
2893ecd6cf80Smarks }
2894ecd6cf80Smarks 
28953cb34c60Sahrens /*
28963cb34c60Sahrens  * inputs:
28973cb34c60Sahrens  * zc_name		name of filesystem
28983cb34c60Sahrens  *
28993cb34c60Sahrens  * outputs:
29003cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29013cb34c60Sahrens  */
2902ecd6cf80Smarks static int
2903ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2904ecd6cf80Smarks {
2905ecd6cf80Smarks 	nvlist_t *nvp;
2906ecd6cf80Smarks 	int error;
2907ecd6cf80Smarks 
2908ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2909ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2910ecd6cf80Smarks 		nvlist_free(nvp);
2911ecd6cf80Smarks 	}
2912ecd6cf80Smarks 
2913ecd6cf80Smarks 	return (error);
2914ecd6cf80Smarks }
2915ecd6cf80Smarks 
2916fa9e4066Sahrens /*
2917fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2918fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2919fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2920fa9e4066Sahrens  */
2921fa9e4066Sahrens static vfs_t *
2922fa9e4066Sahrens zfs_get_vfs(const char *resource)
2923fa9e4066Sahrens {
2924fa9e4066Sahrens 	struct vfs *vfsp;
2925fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2926fa9e4066Sahrens 
2927fa9e4066Sahrens 	vfs_list_read_lock();
2928fa9e4066Sahrens 	vfsp = rootvfs;
2929fa9e4066Sahrens 	do {
2930fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2931fa9e4066Sahrens 			VFS_HOLD(vfsp);
2932fa9e4066Sahrens 			vfs_found = vfsp;
2933fa9e4066Sahrens 			break;
2934fa9e4066Sahrens 		}
2935fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2936fa9e4066Sahrens 	} while (vfsp != rootvfs);
2937fa9e4066Sahrens 	vfs_list_unlock();
2938fa9e4066Sahrens 	return (vfs_found);
2939fa9e4066Sahrens }
2940fa9e4066Sahrens 
2941ecd6cf80Smarks /* ARGSUSED */
2942fa9e4066Sahrens static void
2943ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2944fa9e4066Sahrens {
2945da6c28aaSamw 	zfs_creat_t *zct = arg;
2946da6c28aaSamw 
2947de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2948da6c28aaSamw }
2949da6c28aaSamw 
2950de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2951da6c28aaSamw 
2952da6c28aaSamw /*
2953de8267e0Stimh  * inputs:
29540a48a24eStimh  * os			parent objset pointer (NULL if root fs)
2955f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
2956f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
2957f7170741SWill Andrews  * createprops		list of properties requested by creator
2958de8267e0Stimh  *
2959de8267e0Stimh  * outputs:
2960de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
29610a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
2962da6c28aaSamw  *
2963de8267e0Stimh  * Determine the settings for utf8only, normalization and
2964de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
2965de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
2966de8267e0Stimh  * the file system is of too early a vintage, a creator can not
2967de8267e0Stimh  * request settings for these properties, even if the requested
2968de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
2969de8267e0Stimh  * properties for these, so remove them from the source nvlist after
2970de8267e0Stimh  * processing.
2971da6c28aaSamw  */
2972da6c28aaSamw static int
297314843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
29740a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
29750a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
2976da6c28aaSamw {
2977de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
2978de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
2979de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2980da6c28aaSamw 
2981de8267e0Stimh 	ASSERT(zplprops != NULL);
2982da6c28aaSamw 
2983de8267e0Stimh 	/*
2984de8267e0Stimh 	 * Pull out creator prop choices, if any.
2985de8267e0Stimh 	 */
2986de8267e0Stimh 	if (createprops) {
29870a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
29880a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2989de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2990de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2991de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2992de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2993de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2994de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2995de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2996de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2997de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2998de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2999de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3000de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3001de8267e0Stimh 	}
3002da6c28aaSamw 
3003c2a93d44Stimh 	/*
30040a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30050a48a24eStimh 	 * or pool is version is too "young" to support normalization
30060a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30070a48a24eStimh 	 * error out.
3008c2a93d44Stimh 	 */
30090a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30100a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30110a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30120a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3013de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30140a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3015be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3016c2a93d44Stimh 
3017de8267e0Stimh 	/*
3018de8267e0Stimh 	 * Put the version in the zplprops
3019de8267e0Stimh 	 */
3020de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3021de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3022da6c28aaSamw 
3023de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3024de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3025de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3026de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3027da6c28aaSamw 
3028c2a93d44Stimh 	/*
3029de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3030c2a93d44Stimh 	 */
3031de8267e0Stimh 	if (norm)
3032de8267e0Stimh 		u8 = 1;
3033de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3034de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3035de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3036de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3037de8267e0Stimh 
3038de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3039de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3040de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3041de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3042c2a93d44Stimh 
3043ab04eb8eStimh 	if (is_ci)
3044ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3045ab04eb8eStimh 
3046da6c28aaSamw 	return (0);
3047fa9e4066Sahrens }
3048fa9e4066Sahrens 
30490a48a24eStimh static int
30500a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
30510a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
30520a48a24eStimh {
30530a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
30540a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
30550a48a24eStimh 	objset_t *os = NULL;
30560a48a24eStimh 	char parentname[MAXNAMELEN];
30570a48a24eStimh 	char *cp;
30580a586ceaSMark Shellenbaum 	spa_t *spa;
30590a586ceaSMark Shellenbaum 	uint64_t spa_vers;
30600a48a24eStimh 	int error;
30610a48a24eStimh 
30620a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
30630a48a24eStimh 	cp = strrchr(parentname, '/');
30640a48a24eStimh 	ASSERT(cp != NULL);
30650a48a24eStimh 	cp[0] = '\0';
30660a48a24eStimh 
30670a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
30680a586ceaSMark Shellenbaum 		return (error);
30690a586ceaSMark Shellenbaum 
30700a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
30710a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
30720a586ceaSMark Shellenbaum 
30730a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
30740a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
30750a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
30760a48a24eStimh 
30770a48a24eStimh 	/*
30780a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
30790a48a24eStimh 	 */
3080503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
30810a48a24eStimh 		return (error);
30820a48a24eStimh 
30830a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
30840a48a24eStimh 	    zplprops, is_ci);
3085503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
30860a48a24eStimh 	return (error);
30870a48a24eStimh }
30880a48a24eStimh 
30890a48a24eStimh static int
30900a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
30910a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
30920a48a24eStimh {
30930a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
30940a586ceaSMark Shellenbaum 	boolean_t sa_ok;
30950a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
30960a48a24eStimh 	int error;
30970a48a24eStimh 
30980a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
30990a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31000a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31010a48a24eStimh 
31020a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31030a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31040a48a24eStimh 	return (error);
31050a48a24eStimh }
31060a48a24eStimh 
31073cb34c60Sahrens /*
31084445fffbSMatthew Ahrens  * innvl: {
31094445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31104445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31114445fffbSMatthew Ahrens  * }
31123cb34c60Sahrens  *
31134445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31143cb34c60Sahrens  */
3115fa9e4066Sahrens static int
31164445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3117fa9e4066Sahrens {
3118fa9e4066Sahrens 	int error = 0;
31194445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3120ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3121ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31224445fffbSMatthew Ahrens 	int32_t type32;
31234445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31244445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3125fa9e4066Sahrens 
31264445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3127be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31284445fffbSMatthew Ahrens 	type = type32;
31294445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3130fa9e4066Sahrens 
31314445fffbSMatthew Ahrens 	switch (type) {
3132fa9e4066Sahrens 	case DMU_OST_ZFS:
3133fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3134fa9e4066Sahrens 		break;
3135fa9e4066Sahrens 
3136fa9e4066Sahrens 	case DMU_OST_ZVOL:
3137fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3138fa9e4066Sahrens 		break;
3139fa9e4066Sahrens 
3140fa9e4066Sahrens 	default:
31411d452cf5Sahrens 		cbfunc = NULL;
3142e7cbe64fSgw 		break;
3143fa9e4066Sahrens 	}
31444445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
31454445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3146be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3147fa9e4066Sahrens 
3148da6c28aaSamw 	zct.zct_props = nvprops;
3149da6c28aaSamw 
31504445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3151be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31524445fffbSMatthew Ahrens 
31534445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
31544445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
31554445fffbSMatthew Ahrens 
31564445fffbSMatthew Ahrens 		if (nvprops == NULL)
3157be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
31584445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
31594445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3160be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3161fa9e4066Sahrens 
31624445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
31634445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
31644445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3165be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
31664445fffbSMatthew Ahrens 
31674445fffbSMatthew Ahrens 		if (error != 0)
31684445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
31694445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
31704445fffbSMatthew Ahrens 
31714445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
31724445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
31734445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
31744445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3175fa9e4066Sahrens 			return (error);
31764445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
31774445fffbSMatthew Ahrens 		int error;
3178ab04eb8eStimh 
31794445fffbSMatthew Ahrens 		/*
31804445fffbSMatthew Ahrens 		 * We have to have normalization and
31814445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
31824445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
31834445fffbSMatthew Ahrens 		 * now.
31844445fffbSMatthew Ahrens 		 */
31854445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
31864445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
31874445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
31884445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
31894445fffbSMatthew Ahrens 		if (error != 0) {
31904445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3191da6c28aaSamw 			return (error);
3192da6c28aaSamw 		}
31934445fffbSMatthew Ahrens 	}
3194ab04eb8eStimh 
31954445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
31964445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
31974445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
31985c5460e9Seschrock 
31994445fffbSMatthew Ahrens 	/*
32004445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32014445fffbSMatthew Ahrens 	 */
32024445fffbSMatthew Ahrens 	if (error == 0) {
32034445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32044445fffbSMatthew Ahrens 		    nvprops, outnvl);
32054445fffbSMatthew Ahrens 		if (error != 0)
32063b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32074445fffbSMatthew Ahrens 	}
32084445fffbSMatthew Ahrens 	return (error);
32094445fffbSMatthew Ahrens }
3210e9dbad6fSeschrock 
32114445fffbSMatthew Ahrens /*
32124445fffbSMatthew Ahrens  * innvl: {
32134445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32144445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32154445fffbSMatthew Ahrens  * }
32164445fffbSMatthew Ahrens  *
32174445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32184445fffbSMatthew Ahrens  */
32194445fffbSMatthew Ahrens static int
32204445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32214445fffbSMatthew Ahrens {
32224445fffbSMatthew Ahrens 	int error = 0;
32234445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32244445fffbSMatthew Ahrens 	char *origin_name;
3225e9dbad6fSeschrock 
32264445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3227be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32284445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3229e9dbad6fSeschrock 
32304445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32314445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3232be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3233e9dbad6fSeschrock 
32344445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3235be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32363b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
32373b2aab18SMatthew Ahrens 	if (error != 0)
32384445fffbSMatthew Ahrens 		return (error);
3239e9dbad6fSeschrock 
3240e9dbad6fSeschrock 	/*
3241e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3242e9dbad6fSeschrock 	 */
3243e9dbad6fSeschrock 	if (error == 0) {
32444445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32454445fffbSMatthew Ahrens 		    nvprops, outnvl);
324692241e0bSTom Erickson 		if (error != 0)
32473b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3248e9dbad6fSeschrock 	}
3249fa9e4066Sahrens 	return (error);
3250fa9e4066Sahrens }
3251fa9e4066Sahrens 
32523cb34c60Sahrens /*
32534445fffbSMatthew Ahrens  * innvl: {
32544445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
32554445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
32564445fffbSMatthew Ahrens  * }
32574445fffbSMatthew Ahrens  *
32584445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
32593cb34c60Sahrens  */
3260fa9e4066Sahrens static int
32614445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3262fa9e4066Sahrens {
32634445fffbSMatthew Ahrens 	nvlist_t *snaps;
32644445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
32654445fffbSMatthew Ahrens 	int error, poollen;
32664445fffbSMatthew Ahrens 	nvpair_t *pair;
3267bb0ade09Sahrens 
32684445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
32694445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
32704445fffbSMatthew Ahrens 		return (error);
32714445fffbSMatthew Ahrens 
32724445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
32734445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3274be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
32754445fffbSMatthew Ahrens 
32764445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3277be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32784445fffbSMatthew Ahrens 	poollen = strlen(poolname);
32794445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
32804445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
32814445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
32824445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3283bb0ade09Sahrens 
32844445fffbSMatthew Ahrens 		/*
32854445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
32864445fffbSMatthew Ahrens 		 * contain only valid characters.
32874445fffbSMatthew Ahrens 		 */
32884445fffbSMatthew Ahrens 		if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
3289be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3290bb0ade09Sahrens 
32914445fffbSMatthew Ahrens 		/*
32924445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
32934445fffbSMatthew Ahrens 		 */
32944445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
32954445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3296be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
32974445fffbSMatthew Ahrens 
32984445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
32994445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33004445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33014445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33024445fffbSMatthew Ahrens 			    == 0) {
3303be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33044445fffbSMatthew Ahrens 			}
33054445fffbSMatthew Ahrens 		}
33064445fffbSMatthew Ahrens 	}
3307bb0ade09Sahrens 
33083b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33094445fffbSMatthew Ahrens 	return (error);
33104445fffbSMatthew Ahrens }
33114445fffbSMatthew Ahrens 
33124445fffbSMatthew Ahrens /*
33134445fffbSMatthew Ahrens  * innvl: "message" -> string
33144445fffbSMatthew Ahrens  */
33154445fffbSMatthew Ahrens /* ARGSUSED */
33164445fffbSMatthew Ahrens static int
33174445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
33184445fffbSMatthew Ahrens {
33194445fffbSMatthew Ahrens 	char *message;
33204445fffbSMatthew Ahrens 	spa_t *spa;
33214445fffbSMatthew Ahrens 	int error;
33224445fffbSMatthew Ahrens 	char *poolname;
33234445fffbSMatthew Ahrens 
33244445fffbSMatthew Ahrens 	/*
33254445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
33264445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
33274445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
33284445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
33294445fffbSMatthew Ahrens 	 * we clear the TSD here.
33304445fffbSMatthew Ahrens 	 */
33314445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
33324445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
33334445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
33344445fffbSMatthew Ahrens 	strfree(poolname);
33354445fffbSMatthew Ahrens 	if (error != 0)
33364445fffbSMatthew Ahrens 		return (error);
33374445fffbSMatthew Ahrens 
33384445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
33394445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3340be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3341bb0ade09Sahrens 	}
3342ea2f5b9eSMatthew Ahrens 
33434445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
33444445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3345be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33464445fffbSMatthew Ahrens 	}
3347ea2f5b9eSMatthew Ahrens 
33484445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
33494445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3350bb0ade09Sahrens 	return (error);
33511d452cf5Sahrens }
3352fa9e4066Sahrens 
33533b2aab18SMatthew Ahrens /*
33543b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
33553b2aab18SMatthew Ahrens  * unmount may need to write out data.
33563b2aab18SMatthew Ahrens  *
33573b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
33583b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3359fc7a6e3fSWill Andrews  *
3360fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3361fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
33623b2aab18SMatthew Ahrens  */
3363fc7a6e3fSWill Andrews int
33643b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
33651d452cf5Sahrens {
33664445fffbSMatthew Ahrens 	vfs_t *vfsp;
33673b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3368fc7a6e3fSWill Andrews 	int err;
33691d452cf5Sahrens 
33703b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3371fc7a6e3fSWill Andrews 		return (0);
33721d452cf5Sahrens 
33733b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
33744445fffbSMatthew Ahrens 	if (vfsp == NULL)
3375fc7a6e3fSWill Andrews 		return (0);
33763b2aab18SMatthew Ahrens 
33773b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
33783b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
33791d452cf5Sahrens 
3380fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
33814445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3382fc7a6e3fSWill Andrews 	if (err != 0)
3383fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
33844445fffbSMatthew Ahrens 
33854445fffbSMatthew Ahrens 	/*
33864445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
33874445fffbSMatthew Ahrens 	 */
33883b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3389fc7a6e3fSWill Andrews 	return (0);
33903b2aab18SMatthew Ahrens }
33913b2aab18SMatthew Ahrens 
33923b2aab18SMatthew Ahrens /* ARGSUSED */
33933b2aab18SMatthew Ahrens static int
33943b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
33953b2aab18SMatthew Ahrens {
3396fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
33973b2aab18SMatthew Ahrens }
33983b2aab18SMatthew Ahrens 
33993b2aab18SMatthew Ahrens /*
34003b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34013b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34023b2aab18SMatthew Ahrens  * if necessary.
34033b2aab18SMatthew Ahrens  */
34043b2aab18SMatthew Ahrens void
34053b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34063b2aab18SMatthew Ahrens {
34073b2aab18SMatthew Ahrens 	int error;
34083b2aab18SMatthew Ahrens 	objset_t *os;
34093b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34103b2aab18SMatthew Ahrens 
34113b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
34123b2aab18SMatthew Ahrens 	if (error != 0)
34133b2aab18SMatthew Ahrens 		return;
34143b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
34153b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
34163b2aab18SMatthew Ahrens 		char originname[MAXNAMELEN];
34173b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
34183b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3419fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
34203b2aab18SMatthew Ahrens 	} else {
34213b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34223b2aab18SMatthew Ahrens 	}
34231d452cf5Sahrens }
34241d452cf5Sahrens 
34253cb34c60Sahrens /*
34264445fffbSMatthew Ahrens  * innvl: {
34274445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34284445fffbSMatthew Ahrens  *     (optional boolean) "defer"
34294445fffbSMatthew Ahrens  * }
34304445fffbSMatthew Ahrens  *
34314445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34323cb34c60Sahrens  *
34333cb34c60Sahrens  */
34341d452cf5Sahrens static int
34354445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34361d452cf5Sahrens {
3437fc7a6e3fSWill Andrews 	int error, poollen;
34384445fffbSMatthew Ahrens 	nvlist_t *snaps;
343919b94df9SMatthew Ahrens 	nvpair_t *pair;
34404445fffbSMatthew Ahrens 	boolean_t defer;
34411d452cf5Sahrens 
34424445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3443be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34444445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
344519b94df9SMatthew Ahrens 
34464445fffbSMatthew Ahrens 	poollen = strlen(poolname);
34474445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
34484445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
344919b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
34504445fffbSMatthew Ahrens 
345119b94df9SMatthew Ahrens 		/*
34524445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
345319b94df9SMatthew Ahrens 		 */
34544445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
34554445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3456be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
345719b94df9SMatthew Ahrens 
3458fc7a6e3fSWill Andrews 		error = zfs_unmount_snap(name);
3459fc7a6e3fSWill Andrews 		if (error != 0)
3460fc7a6e3fSWill Andrews 			return (error);
346119b94df9SMatthew Ahrens 	}
346219b94df9SMatthew Ahrens 
34633b2aab18SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
34641d452cf5Sahrens }
34651d452cf5Sahrens 
34663cb34c60Sahrens /*
34673cb34c60Sahrens  * inputs:
34683cb34c60Sahrens  * zc_name		name of dataset to destroy
34693cb34c60Sahrens  * zc_objset_type	type of objset
3470842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
34713cb34c60Sahrens  *
34723cb34c60Sahrens  * outputs:		none
34733cb34c60Sahrens  */
34741d452cf5Sahrens static int
34751d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
34761d452cf5Sahrens {
3477681d9761SEric Taylor 	int err;
3478fc7a6e3fSWill Andrews 
3479fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3480fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3481fc7a6e3fSWill Andrews 		if (err != 0)
3482fc7a6e3fSWill Andrews 			return (err);
3483fc7a6e3fSWill Andrews 	}
3484fa9e4066Sahrens 
34853b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
34863b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
34873b2aab18SMatthew Ahrens 	else
34883b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3489681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
34905c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3491681d9761SEric Taylor 	return (err);
3492fa9e4066Sahrens }
3493fa9e4066Sahrens 
34943cb34c60Sahrens /*
3495a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
34963cb34c60Sahrens  *
3497a7027df1SMatthew Ahrens  * innvl is not used.
3498a7027df1SMatthew Ahrens  *
3499a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3500a7027df1SMatthew Ahrens  * }
35013cb34c60Sahrens  */
3502a7027df1SMatthew Ahrens /* ARGSUSED */
3503fa9e4066Sahrens static int
3504a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3505fa9e4066Sahrens {
3506ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
35073b2aab18SMatthew Ahrens 	int error;
3508ae46e4c7SMatthew Ahrens 
3509a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3510503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
351147f263f4Sek 		if (error == 0) {
351247f263f4Sek 			int resume_err;
35134ccbb6e7Sahrens 
3514a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3515a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
351647f263f4Sek 			error = error ? error : resume_err;
351747f263f4Sek 		}
35184ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
35194ccbb6e7Sahrens 	} else {
3520a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
35214ccbb6e7Sahrens 	}
35223b2aab18SMatthew Ahrens 	return (error);
35233b2aab18SMatthew Ahrens }
35244ccbb6e7Sahrens 
35253b2aab18SMatthew Ahrens static int
35263b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
35273b2aab18SMatthew Ahrens {
35283b2aab18SMatthew Ahrens 	const char *snapname = arg;
35293b2aab18SMatthew Ahrens 	char fullname[MAXNAMELEN];
3530ae46e4c7SMatthew Ahrens 
35313b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3532fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
3533fa9e4066Sahrens }
3534fa9e4066Sahrens 
35353cb34c60Sahrens /*
35363cb34c60Sahrens  * inputs:
35373cb34c60Sahrens  * zc_name	old name of dataset
35383cb34c60Sahrens  * zc_value	new name of dataset
35393cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
35403cb34c60Sahrens  *
35413cb34c60Sahrens  * outputs:	none
35423cb34c60Sahrens  */
3543fa9e4066Sahrens static int
3544fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3545fa9e4066Sahrens {
35467f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
35473b2aab18SMatthew Ahrens 	char *at;
3548cdf5b4caSmmusante 
3549e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3550f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3551f18faf3fSek 	    strchr(zc->zc_value, '%'))
3552be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3553fa9e4066Sahrens 
35543b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
35553b2aab18SMatthew Ahrens 	if (at != NULL) {
35563b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3557a0c1127bSSteven Hartland 		int error;
3558a0c1127bSSteven Hartland 
35593b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3560be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
35613b2aab18SMatthew Ahrens 		*at = '\0';
35623b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3563a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
35643b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
35653b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3566a0c1127bSSteven Hartland 			if (error != 0) {
3567a0c1127bSSteven Hartland 				*at = '@';
35683b2aab18SMatthew Ahrens 				return (error);
3569a0c1127bSSteven Hartland 			}
35703b2aab18SMatthew Ahrens 		}
3571a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3572a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3573a0c1127bSSteven Hartland 		*at = '@';
3574a0c1127bSSteven Hartland 
3575a0c1127bSSteven Hartland 		return (error);
35763b2aab18SMatthew Ahrens 	} else {
35773b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZVOL)
35783b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
35793b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3580fa9e4066Sahrens 	}
3581fa9e4066Sahrens }
3582fa9e4066Sahrens 
358392241e0bSTom Erickson static int
358492241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
358592241e0bSTom Erickson {
358692241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
358792241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
358892241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
358992241e0bSTom Erickson 	uint64_t intval;
359092241e0bSTom Erickson 	int err;
359192241e0bSTom Erickson 
359292241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
359392241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
359492241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
359592241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
359692241e0bSTom Erickson 				return (err);
359792241e0bSTom Erickson 			return (0);
359892241e0bSTom Erickson 		}
359992241e0bSTom Erickson 
360092241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
360192241e0bSTom Erickson 			const char *perm = NULL;
360292241e0bSTom Erickson 			const char *uq_prefix =
360392241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
360492241e0bSTom Erickson 			const char *gq_prefix =
360592241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
360692241e0bSTom Erickson 
360792241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
360892241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
360992241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
361092241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
361192241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
361292241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
361392241e0bSTom Erickson 			} else {
361492241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3615be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
361692241e0bSTom Erickson 			}
361792241e0bSTom Erickson 
361892241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
361992241e0bSTom Erickson 				return (err);
362092241e0bSTom Erickson 			return (0);
362192241e0bSTom Erickson 		}
362292241e0bSTom Erickson 
3623be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
362492241e0bSTom Erickson 	}
362592241e0bSTom Erickson 
362692241e0bSTom Erickson 	if (issnap)
3627be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
362892241e0bSTom Erickson 
362992241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
363092241e0bSTom Erickson 		/*
363192241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
363292241e0bSTom Erickson 		 * format.
363392241e0bSTom Erickson 		 */
363492241e0bSTom Erickson 		nvlist_t *attrs;
363592241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
363692241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
363792241e0bSTom Erickson 		    &pair) == 0);
363892241e0bSTom Erickson 	}
363992241e0bSTom Erickson 
364092241e0bSTom Erickson 	/*
364192241e0bSTom Erickson 	 * Check that this value is valid for this pool version
364292241e0bSTom Erickson 	 */
364392241e0bSTom Erickson 	switch (prop) {
364492241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
364592241e0bSTom Erickson 		/*
364692241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
364792241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
364892241e0bSTom Erickson 		 * we'll catch them later.
364992241e0bSTom Erickson 		 */
365092241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
365192241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
365292241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
365392241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
365492241e0bSTom Erickson 			    zfs_earlier_version(dsname,
365592241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3656be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
365792241e0bSTom Erickson 			}
365892241e0bSTom Erickson 
365992241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
366092241e0bSTom Erickson 			    zfs_earlier_version(dsname,
366192241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3662be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
366392241e0bSTom Erickson 
3664a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3665a6f561b4SSašo Kiselkov 				spa_t *spa;
3666a6f561b4SSašo Kiselkov 
3667a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3668a6f561b4SSašo Kiselkov 					return (err);
3669a6f561b4SSašo Kiselkov 
3670*2acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
3671*2acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3672a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3673be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3674a6f561b4SSašo Kiselkov 				}
3675a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3676a6f561b4SSašo Kiselkov 			}
3677a6f561b4SSašo Kiselkov 
367892241e0bSTom Erickson 			/*
367992241e0bSTom Erickson 			 * If this is a bootable dataset then
368092241e0bSTom Erickson 			 * verify that the compression algorithm
368192241e0bSTom Erickson 			 * is supported for booting. We must return
368292241e0bSTom Erickson 			 * something other than ENOTSUP since it
368392241e0bSTom Erickson 			 * implies a downrev pool version.
368492241e0bSTom Erickson 			 */
368592241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
368692241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3687be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
368892241e0bSTom Erickson 			}
368992241e0bSTom Erickson 		}
369092241e0bSTom Erickson 		break;
369192241e0bSTom Erickson 
369292241e0bSTom Erickson 	case ZFS_PROP_COPIES:
369392241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3694be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
369592241e0bSTom Erickson 		break;
369692241e0bSTom Erickson 
369792241e0bSTom Erickson 	case ZFS_PROP_DEDUP:
369892241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3699be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
370092241e0bSTom Erickson 		break;
370192241e0bSTom Erickson 
370292241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
370392241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3704be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
370592241e0bSTom Erickson 		break;
370692241e0bSTom Erickson 
370792241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
370892241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
370992241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
371092241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
371192241e0bSTom Erickson 			    zfs_earlier_version(dsname,
371292241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3713be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
371492241e0bSTom Erickson 		}
371592241e0bSTom Erickson 		break;
371692241e0bSTom Erickson 	}
371792241e0bSTom Erickson 
371892241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
371992241e0bSTom Erickson }
372092241e0bSTom Erickson 
3721a6f561b4SSašo Kiselkov /*
3722a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3723a6f561b4SSašo Kiselkov  * multiple times.
3724a6f561b4SSašo Kiselkov  */
3725a6f561b4SSašo Kiselkov static int
37263b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3727a6f561b4SSašo Kiselkov {
37283b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3729*2acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3730a6f561b4SSašo Kiselkov 
3731*2acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
3732a6f561b4SSašo Kiselkov 		return (0);
3733a6f561b4SSašo Kiselkov 	else
3734be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3735a6f561b4SSašo Kiselkov }
3736a6f561b4SSašo Kiselkov 
3737a6f561b4SSašo Kiselkov /*
3738a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3739a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3740a6f561b4SSašo Kiselkov  */
3741a6f561b4SSašo Kiselkov static void
37423b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3743a6f561b4SSašo Kiselkov {
37443b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3745*2acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3746a6f561b4SSašo Kiselkov 
3747*2acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
3748a6f561b4SSašo Kiselkov }
3749a6f561b4SSašo Kiselkov 
37503b2aab18SMatthew Ahrens /*
37513b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
37523b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
37533b2aab18SMatthew Ahrens  * as being active.
37543b2aab18SMatthew Ahrens  */
37553b2aab18SMatthew Ahrens static int
3756*2acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
37573b2aab18SMatthew Ahrens {
37583b2aab18SMatthew Ahrens 	int err;
37593b2aab18SMatthew Ahrens 
37603b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
37613b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
37623b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3763*2acef22dSMatthew Ahrens 	    &feature, 2);
37643b2aab18SMatthew Ahrens 
37653b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
37663b2aab18SMatthew Ahrens 		return (err);
37673b2aab18SMatthew Ahrens 	else
37683b2aab18SMatthew Ahrens 		return (0);
37693b2aab18SMatthew Ahrens }
37703b2aab18SMatthew Ahrens 
377192241e0bSTom Erickson /*
377292241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
377392241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
377492241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
377592241e0bSTom Erickson  *
377692241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
377792241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
377892241e0bSTom Erickson  * of all the properties that failed a permission check along with the
377992241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
378092241e0bSTom Erickson  * returned errlist.
378192241e0bSTom Erickson  *
378292241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
378392241e0bSTom Erickson  * pointed at by errlist is NULL.
378492241e0bSTom Erickson  */
378592241e0bSTom Erickson static int
378692241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3787745cd3c5Smaybee {
3788745cd3c5Smaybee 	zfs_cmd_t *zc;
378992241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
379092241e0bSTom Erickson 	nvlist_t *errors;
379192241e0bSTom Erickson 	int err, rv = 0;
3792745cd3c5Smaybee 
3793745cd3c5Smaybee 	if (props == NULL)
379492241e0bSTom Erickson 		return (0);
379592241e0bSTom Erickson 
379692241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
379792241e0bSTom Erickson 
3798745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3799745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
380092241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
380192241e0bSTom Erickson 	while (pair != NULL) {
380292241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
380392241e0bSTom Erickson 
380492241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
380592241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
38064445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
380792241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
380892241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
380992241e0bSTom Erickson 			    zc->zc_value, err) == 0);
381092241e0bSTom Erickson 		}
381192241e0bSTom Erickson 		pair = next_pair;
3812745cd3c5Smaybee 	}
3813745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
381492241e0bSTom Erickson 
381592241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
381692241e0bSTom Erickson 		nvlist_free(errors);
381792241e0bSTom Erickson 		errors = NULL;
381892241e0bSTom Erickson 	} else {
381992241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
382092241e0bSTom Erickson 	}
382192241e0bSTom Erickson 
382292241e0bSTom Erickson 	if (errlist == NULL)
382392241e0bSTom Erickson 		nvlist_free(errors);
382492241e0bSTom Erickson 	else
382592241e0bSTom Erickson 		*errlist = errors;
382692241e0bSTom Erickson 
382792241e0bSTom Erickson 	return (rv);
382892241e0bSTom Erickson }
382992241e0bSTom Erickson 
383092241e0bSTom Erickson static boolean_t
383192241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
383292241e0bSTom Erickson {
383392241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
383492241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
383592241e0bSTom Erickson 		nvlist_t *attrs;
383692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
383792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
383892241e0bSTom Erickson 		    &p1) == 0);
383992241e0bSTom Erickson 	}
384092241e0bSTom Erickson 
384192241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
384292241e0bSTom Erickson 		nvlist_t *attrs;
384392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
384492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
384592241e0bSTom Erickson 		    &p2) == 0);
384692241e0bSTom Erickson 	}
384792241e0bSTom Erickson 
384892241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
384992241e0bSTom Erickson 		return (B_FALSE);
385092241e0bSTom Erickson 
385192241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
385292241e0bSTom Erickson 		char *valstr1, *valstr2;
385392241e0bSTom Erickson 
385492241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
385592241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
385692241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
385792241e0bSTom Erickson 	} else {
385892241e0bSTom Erickson 		uint64_t intval1, intval2;
385992241e0bSTom Erickson 
386092241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
386192241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
386292241e0bSTom Erickson 		return (intval1 == intval2);
386392241e0bSTom Erickson 	}
3864745cd3c5Smaybee }
3865745cd3c5Smaybee 
386692241e0bSTom Erickson /*
386792241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
386892241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
386992241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
387092241e0bSTom Erickson  */
387192241e0bSTom Erickson static void
387292241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
387392241e0bSTom Erickson {
387492241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
387592241e0bSTom Erickson 
387692241e0bSTom Erickson 	if (origprops == NULL)
387792241e0bSTom Erickson 		return; /* all props need to be received */
387892241e0bSTom Erickson 
387992241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
388092241e0bSTom Erickson 	while (pair != NULL) {
388192241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
388292241e0bSTom Erickson 		nvpair_t *match;
388392241e0bSTom Erickson 
388492241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
388592241e0bSTom Erickson 
388692241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
388792241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
388892241e0bSTom Erickson 			goto next; /* need to set received value */
388992241e0bSTom Erickson 
389092241e0bSTom Erickson 		/* don't clear the existing received value */
389192241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
389292241e0bSTom Erickson 		/* don't bother receiving the property */
389392241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
389492241e0bSTom Erickson next:
389592241e0bSTom Erickson 		pair = next_pair;
389692241e0bSTom Erickson 	}
389792241e0bSTom Erickson }
389892241e0bSTom Erickson 
389992241e0bSTom Erickson #ifdef	DEBUG
390092241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
390192241e0bSTom Erickson #endif
390292241e0bSTom Erickson 
39033cb34c60Sahrens /*
39043cb34c60Sahrens  * inputs:
39053cb34c60Sahrens  * zc_name		name of containing filesystem
39063cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
39073cb34c60Sahrens  * zc_value		name of snapshot to create
39083cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
39093cb34c60Sahrens  * zc_cookie		file descriptor to recv from
39103cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
39113cb34c60Sahrens  * zc_guid		force flag
3912c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
3913c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
39143cb34c60Sahrens  *
39153cb34c60Sahrens  * outputs:
39163cb34c60Sahrens  * zc_cookie		number of bytes read
391792241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
391892241e0bSTom Erickson  * zc_obj		zprop_errflags_t
3919c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
39203cb34c60Sahrens  */
3921fa9e4066Sahrens static int
39223cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3923fa9e4066Sahrens {
3924fa9e4066Sahrens 	file_t *fp;
39253cb34c60Sahrens 	dmu_recv_cookie_t drc;
3926f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
392792241e0bSTom Erickson 	int fd;
392892241e0bSTom Erickson 	int error = 0;
392992241e0bSTom Erickson 	int props_error = 0;
393092241e0bSTom Erickson 	nvlist_t *errors;
39313cb34c60Sahrens 	offset_t off;
393292241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
393392241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
39343b2aab18SMatthew Ahrens 	char *origin = NULL;
39353cb34c60Sahrens 	char *tosnap;
39363cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
393792241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
3938fa9e4066Sahrens 
39393ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3940f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
3941f18faf3fSek 	    strchr(zc->zc_value, '%'))
3942be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
39433ccfa83cSahrens 
39443cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
39453cb34c60Sahrens 	tosnap = strchr(tofs, '@');
394692241e0bSTom Erickson 	*tosnap++ = '\0';
39473cb34c60Sahrens 
39483cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
39493cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3950478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
39513cb34c60Sahrens 		return (error);
39523cb34c60Sahrens 
3953fa9e4066Sahrens 	fd = zc->zc_cookie;
3954fa9e4066Sahrens 	fp = getf(fd);
39553cb34c60Sahrens 	if (fp == NULL) {
39563cb34c60Sahrens 		nvlist_free(props);
3957be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
39583cb34c60Sahrens 	}
3959f18faf3fSek 
396092241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
396192241e0bSTom Erickson 
39623b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
39633b2aab18SMatthew Ahrens 		origin = zc->zc_string;
39643b2aab18SMatthew Ahrens 
39653b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
39663b2aab18SMatthew Ahrens 	    &zc->zc_begin_record, force, origin, &drc);
39673b2aab18SMatthew Ahrens 	if (error != 0)
39683b2aab18SMatthew Ahrens 		goto out;
39693b2aab18SMatthew Ahrens 
39703b2aab18SMatthew Ahrens 	/*
39713b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
39723b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
39733b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
39743b2aab18SMatthew Ahrens 	 */
39753b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
39763b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
39773b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
39783b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
397992241e0bSTom Erickson 			first_recvd_props = B_TRUE;
398092241e0bSTom Erickson 
3981745cd3c5Smaybee 		/*
398292241e0bSTom Erickson 		 * If new received properties are supplied, they are to
398392241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
398492241e0bSTom Erickson 		 * away the existing ones.
3985745cd3c5Smaybee 		 */
39863b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
398792241e0bSTom Erickson 			nvlist_t *errlist = NULL;
398892241e0bSTom Erickson 			/*
398992241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
399092241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
399192241e0bSTom Erickson 			 *
399292241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
399392241e0bSTom Erickson 			 * special case where we blow away all local properties
399492241e0bSTom Erickson 			 * regardless.
399592241e0bSTom Erickson 			 */
399692241e0bSTom Erickson 			if (!first_recvd_props)
399792241e0bSTom Erickson 				props_reduce(props, origprops);
39983b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
399992241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
400092241e0bSTom Erickson 			nvlist_free(errlist);
40013cb34c60Sahrens 
40023b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
40033b2aab18SMatthew Ahrens 			    first_recvd_props ? NULL : props) != 0)
400492241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
40053b2aab18SMatthew Ahrens 		} else {
400692241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
400792241e0bSTom Erickson 		}
40083b2aab18SMatthew Ahrens 	}
400992241e0bSTom Erickson 
40103b2aab18SMatthew Ahrens 	if (props != NULL) {
40113b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
40123b2aab18SMatthew Ahrens 
40133b2aab18SMatthew Ahrens 		if (props_error == 0) {
40143b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
40153b2aab18SMatthew Ahrens 			    props, errors);
40163b2aab18SMatthew Ahrens 		}
401792241e0bSTom Erickson 	}
401892241e0bSTom Erickson 
40194445fffbSMatthew Ahrens 	if (zc->zc_nvlist_dst_size != 0 &&
40204445fffbSMatthew Ahrens 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
40214445fffbSMatthew Ahrens 	    put_nvlist(zc, errors) != 0)) {
4022745cd3c5Smaybee 		/*
402392241e0bSTom Erickson 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
402492241e0bSTom Erickson 		 * size or supplied an invalid address.
4025745cd3c5Smaybee 		 */
4026be6fd75aSMatthew Ahrens 		props_error = SET_ERROR(EINVAL);
40273cb34c60Sahrens 	}
40283cb34c60Sahrens 
40293cb34c60Sahrens 	off = fp->f_offset;
4030c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4031c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
4032a2eea2e1Sahrens 
4033f4b94bdeSMatthew Ahrens 	if (error == 0) {
4034f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4035745cd3c5Smaybee 
4036f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4037f4b94bdeSMatthew Ahrens 			/* online recv */
4038f4b94bdeSMatthew Ahrens 			int end_err;
4039745cd3c5Smaybee 
4040503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4041f4b94bdeSMatthew Ahrens 			/*
4042f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4043f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4044f4b94bdeSMatthew Ahrens 			 */
404591948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
40465c703fceSGeorge Wilson 			if (error == 0)
40475c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4048f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4049f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4050745cd3c5Smaybee 		} else {
405191948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
40523cb34c60Sahrens 		}
405347f263f4Sek 	}
40543cb34c60Sahrens 
40553cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
40563cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
40573cb34c60Sahrens 		fp->f_offset = off;
4058a2eea2e1Sahrens 
405992241e0bSTom Erickson #ifdef	DEBUG
406092241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
406192241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
406292241e0bSTom Erickson 		error = 1;
406392241e0bSTom Erickson 	}
406492241e0bSTom Erickson #endif
4065745cd3c5Smaybee 	/*
4066745cd3c5Smaybee 	 * On error, restore the original props.
4067745cd3c5Smaybee 	 */
40683b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
40693b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
40703b2aab18SMatthew Ahrens 			/*
40713b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
40723b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
40733b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
40743b2aab18SMatthew Ahrens 			 */
407592241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
40763b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
40773b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
407892241e0bSTom Erickson 		}
407992241e0bSTom Erickson 
408092241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
408192241e0bSTom Erickson 			/* We failed to stash the original properties. */
408292241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
408392241e0bSTom Erickson 		}
408492241e0bSTom Erickson 
408592241e0bSTom Erickson 		/*
408692241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
408792241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
408892241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
408992241e0bSTom Erickson 		 * first new-style receive.
409092241e0bSTom Erickson 		 */
409192241e0bSTom Erickson 		if (origprops != NULL &&
409292241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
409392241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
409492241e0bSTom Erickson 		    origprops, NULL) != 0) {
409592241e0bSTom Erickson 			/*
409692241e0bSTom Erickson 			 * We stashed the original properties but failed to
409792241e0bSTom Erickson 			 * restore them.
409892241e0bSTom Erickson 			 */
409992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
410092241e0bSTom Erickson 		}
4101745cd3c5Smaybee 	}
4102745cd3c5Smaybee out:
4103745cd3c5Smaybee 	nvlist_free(props);
4104745cd3c5Smaybee 	nvlist_free(origprops);
410592241e0bSTom Erickson 	nvlist_free(errors);
4106fa9e4066Sahrens 	releasef(fd);
410792241e0bSTom Erickson 
410892241e0bSTom Erickson 	if (error == 0)
410992241e0bSTom Erickson 		error = props_error;
411092241e0bSTom Erickson 
4111fa9e4066Sahrens 	return (error);
4112fa9e4066Sahrens }
4113fa9e4066Sahrens 
41143cb34c60Sahrens /*
41153cb34c60Sahrens  * inputs:
41163cb34c60Sahrens  * zc_name	name of snapshot to send
41173cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4118a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4119a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4120a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
412119b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
412219b94df9SMatthew Ahrens  *		output size in zc_objset_type.
41233cb34c60Sahrens  *
41243cb34c60Sahrens  * outputs: none
41253cb34c60Sahrens  */
4126fa9e4066Sahrens static int
41273cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4128fa9e4066Sahrens {
4129fa9e4066Sahrens 	int error;
41303cb34c60Sahrens 	offset_t off;
413119b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
4132fa9e4066Sahrens 
41333b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
41343b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
41353b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4136a7f53a56SChris Kirby 
41373b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
41383b2aab18SMatthew Ahrens 		if (error != 0)
4139a7f53a56SChris Kirby 			return (error);
41403b2aab18SMatthew Ahrens 
41413b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
41423b2aab18SMatthew Ahrens 		if (error != 0) {
41433b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4144fa9e4066Sahrens 			return (error);
4145fa9e4066Sahrens 		}
41463b2aab18SMatthew Ahrens 
41473b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
41483b2aab18SMatthew Ahrens 			zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
41493b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
41503b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
41514445fffbSMatthew Ahrens 	}
41524445fffbSMatthew Ahrens 
41533b2aab18SMatthew Ahrens 	if (estimate) {
41543b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
41553b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
41563b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
41574445fffbSMatthew Ahrens 
41583b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
41593b2aab18SMatthew Ahrens 		if (error != 0)
41603b2aab18SMatthew Ahrens 			return (error);
41613b2aab18SMatthew Ahrens 
41623b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
41633b2aab18SMatthew Ahrens 		if (error != 0) {
41643b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
41653b2aab18SMatthew Ahrens 			return (error);
41664445fffbSMatthew Ahrens 		}
41674445fffbSMatthew Ahrens 
41683b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
41693b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
41703b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
41713b2aab18SMatthew Ahrens 			if (error != 0) {
41723b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
41733b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
41744445fffbSMatthew Ahrens 				return (error);
41754445fffbSMatthew Ahrens 			}
41764445fffbSMatthew Ahrens 		}
4177fa9e4066Sahrens 
41784445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
417919b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
41803b2aab18SMatthew Ahrens 
41813b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
41823b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
41833b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
41843b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
418519b94df9SMatthew Ahrens 	} else {
418619b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
41873b2aab18SMatthew Ahrens 		if (fp == NULL)
4188be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4189fa9e4066Sahrens 
419019b94df9SMatthew Ahrens 		off = fp->f_offset;
41913b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
41923b2aab18SMatthew Ahrens 		    zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
4193fa9e4066Sahrens 
419419b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
419519b94df9SMatthew Ahrens 			fp->f_offset = off;
419619b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
419719b94df9SMatthew Ahrens 	}
4198fa9e4066Sahrens 	return (error);
4199fa9e4066Sahrens }
4200fa9e4066Sahrens 
42014e3c9f44SBill Pijewski /*
42024e3c9f44SBill Pijewski  * inputs:
42034e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
42044e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
42054e3c9f44SBill Pijewski  *
42064e3c9f44SBill Pijewski  * outputs:
42074e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
42084e3c9f44SBill Pijewski  */
42094e3c9f44SBill Pijewski static int
42104e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
42114e3c9f44SBill Pijewski {
42123b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
42134e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
42144e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
42154e3c9f44SBill Pijewski 	int error;
42164e3c9f44SBill Pijewski 
42173b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
42183b2aab18SMatthew Ahrens 	if (error != 0)
42193b2aab18SMatthew Ahrens 		return (error);
42203b2aab18SMatthew Ahrens 
42213b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
42223b2aab18SMatthew Ahrens 	if (error != 0) {
42233b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
42244e3c9f44SBill Pijewski 		return (error);
42253b2aab18SMatthew Ahrens 	}
42264e3c9f44SBill Pijewski 
42274e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
42284e3c9f44SBill Pijewski 
42294e3c9f44SBill Pijewski 	/*
42304e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
42314e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
42324e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
42334e3c9f44SBill Pijewski 	 * that stream.
42344e3c9f44SBill Pijewski 	 */
42354e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
42364e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
42374e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
42384e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
42394e3c9f44SBill Pijewski 			break;
42404e3c9f44SBill Pijewski 	}
42414e3c9f44SBill Pijewski 
42424e3c9f44SBill Pijewski 	if (dsp != NULL)
42434e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
42444e3c9f44SBill Pijewski 	else
4245be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
42464e3c9f44SBill Pijewski 
42474e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
42484e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
42493b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
42504e3c9f44SBill Pijewski 	return (error);
42514e3c9f44SBill Pijewski }
42524e3c9f44SBill Pijewski 
4253ea8dc4b6Seschrock static int
4254ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4255ea8dc4b6Seschrock {
4256ea8dc4b6Seschrock 	int id, error;
4257ea8dc4b6Seschrock 
4258ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4259ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4260ea8dc4b6Seschrock 
4261ea8dc4b6Seschrock 	if (error == 0)
4262ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4263ea8dc4b6Seschrock 
4264ea8dc4b6Seschrock 	return (error);
4265ea8dc4b6Seschrock }
4266ea8dc4b6Seschrock 
4267ea8dc4b6Seschrock static int
4268ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4269ea8dc4b6Seschrock {
4270ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4271ea8dc4b6Seschrock }
4272ea8dc4b6Seschrock 
4273ea8dc4b6Seschrock static int
4274ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4275ea8dc4b6Seschrock {
4276ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4277ea8dc4b6Seschrock 	int error;
4278ea8dc4b6Seschrock 
4279ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4280ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4281ea8dc4b6Seschrock 
4282ea8dc4b6Seschrock 	zc->zc_guid = id;
4283ea8dc4b6Seschrock 
4284ea8dc4b6Seschrock 	return (error);
4285ea8dc4b6Seschrock }
4286ea8dc4b6Seschrock 
4287ea8dc4b6Seschrock static int
4288ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4289ea8dc4b6Seschrock {
4290ea8dc4b6Seschrock 	spa_t *spa;
4291ea8dc4b6Seschrock 	int error;
4292e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4293ea8dc4b6Seschrock 
4294ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4295ea8dc4b6Seschrock 		return (error);
4296ea8dc4b6Seschrock 
4297e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4298ea8dc4b6Seschrock 	    &count);
4299ea8dc4b6Seschrock 	if (error == 0)
4300e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4301ea8dc4b6Seschrock 	else
4302e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4303ea8dc4b6Seschrock 
4304ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4305ea8dc4b6Seschrock 
4306ea8dc4b6Seschrock 	return (error);
4307ea8dc4b6Seschrock }
4308ea8dc4b6Seschrock 
4309ea8dc4b6Seschrock static int
4310ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4311ea8dc4b6Seschrock {
4312ea8dc4b6Seschrock 	spa_t *spa;
4313ea8dc4b6Seschrock 	vdev_t *vd;
4314bb8b5132Sek 	int error;
4315ea8dc4b6Seschrock 
4316b87f3af3Sperrin 	/*
4317b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4318b87f3af3Sperrin 	 */
4319b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4320b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4321b87f3af3Sperrin 	if (spa == NULL) {
4322b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4323be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4324b87f3af3Sperrin 	}
4325b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4326b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4327b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4328b87f3af3Sperrin 	}
4329468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4330b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4331b87f3af3Sperrin 
4332c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4333468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4334468c413aSTim Haley 	} else {
4335468c413aSTim Haley 		nvlist_t *policy;
4336468c413aSTim Haley 		nvlist_t *config = NULL;
4337468c413aSTim Haley 
4338468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4339be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4340468c413aSTim Haley 
4341468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4342468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4343468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4344468c413aSTim Haley 			    policy, &config);
4345468c413aSTim Haley 			if (config != NULL) {
43464b964adaSGeorge Wilson 				int err;
43474b964adaSGeorge Wilson 
43484b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
43494b964adaSGeorge Wilson 					error = err;
4350468c413aSTim Haley 				nvlist_free(config);
4351468c413aSTim Haley 			}
4352468c413aSTim Haley 			nvlist_free(policy);
4353468c413aSTim Haley 		}
4354468c413aSTim Haley 	}
4355468c413aSTim Haley 
43563b2aab18SMatthew Ahrens 	if (error != 0)
4357ea8dc4b6Seschrock 		return (error);
4358ea8dc4b6Seschrock 
43598f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4360ea8dc4b6Seschrock 
4361e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4362ea8dc4b6Seschrock 		vd = NULL;
4363c5904d13Seschrock 	} else {
4364c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4365fa94a07fSbrendan 		if (vd == NULL) {
4366e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4367fa94a07fSbrendan 			spa_close(spa, FTAG);
4368be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4369fa94a07fSbrendan 		}
4370ea8dc4b6Seschrock 	}
4371ea8dc4b6Seschrock 
4372e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4373e14bb325SJeff Bonwick 
4374e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4375ea8dc4b6Seschrock 
4376e14bb325SJeff Bonwick 	/*
4377e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4378e14bb325SJeff Bonwick 	 */
437954d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4380be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4381ea8dc4b6Seschrock 
4382ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4383ea8dc4b6Seschrock 
438454d692b7SGeorge Wilson 	return (error);
4385ea8dc4b6Seschrock }
4386ea8dc4b6Seschrock 
43874263d13fSGeorge Wilson static int
43884263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
43894263d13fSGeorge Wilson {
43904263d13fSGeorge Wilson 	spa_t *spa;
43914263d13fSGeorge Wilson 	int error;
43924263d13fSGeorge Wilson 
43934263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
43943b2aab18SMatthew Ahrens 	if (error != 0)
43954263d13fSGeorge Wilson 		return (error);
43964263d13fSGeorge Wilson 
43974263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4398d6afdce2SGeorge Wilson 
4399d6afdce2SGeorge Wilson 	/*
4400d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4401d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4402d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4403d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4404d6afdce2SGeorge Wilson 	 */
4405d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
44064263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4407d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4408d6afdce2SGeorge Wilson 
44094263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
44104263d13fSGeorge Wilson 	spa_close(spa, FTAG);
44114263d13fSGeorge Wilson 	return (0);
44124263d13fSGeorge Wilson }
44133cb34c60Sahrens /*
44143cb34c60Sahrens  * inputs:
44153cb34c60Sahrens  * zc_name	name of filesystem
44163cb34c60Sahrens  * zc_value	name of origin snapshot
44173cb34c60Sahrens  *
4418681d9761SEric Taylor  * outputs:
4419681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
44203cb34c60Sahrens  */
442199653d4eSeschrock static int
442299653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
442399653d4eSeschrock {
44240b69c2f0Sahrens 	char *cp;
44250b69c2f0Sahrens 
44260b69c2f0Sahrens 	/*
44270b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
44280b69c2f0Sahrens 	 * it's easier.
44290b69c2f0Sahrens 	 */
4430e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
44310b69c2f0Sahrens 	if (cp)
44320b69c2f0Sahrens 		*cp = '\0';
4433e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
44343b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4435681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
443699653d4eSeschrock }
443799653d4eSeschrock 
443814843421SMatthew Ahrens /*
443914843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
444014843421SMatthew Ahrens  *
444114843421SMatthew Ahrens  * inputs:
444214843421SMatthew Ahrens  * zc_name	name of filesystem
444314843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
444414843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
444514843421SMatthew Ahrens  * zc_guid	RID/UID/GID
444614843421SMatthew Ahrens  *
444714843421SMatthew Ahrens  * outputs:
444814843421SMatthew Ahrens  * zc_cookie	property value
444914843421SMatthew Ahrens  */
445014843421SMatthew Ahrens static int
445114843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
445214843421SMatthew Ahrens {
445314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
445414843421SMatthew Ahrens 	int error;
445514843421SMatthew Ahrens 
445614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4457be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
445814843421SMatthew Ahrens 
44591412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
44603b2aab18SMatthew Ahrens 	if (error != 0)
446114843421SMatthew Ahrens 		return (error);
446214843421SMatthew Ahrens 
446314843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
446414843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
446514843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
446614843421SMatthew Ahrens 
446714843421SMatthew Ahrens 	return (error);
446814843421SMatthew Ahrens }
446914843421SMatthew Ahrens 
447014843421SMatthew Ahrens /*
447114843421SMatthew Ahrens  * inputs:
447214843421SMatthew Ahrens  * zc_name		name of filesystem
447314843421SMatthew Ahrens  * zc_cookie		zap cursor
447414843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
447514843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
447614843421SMatthew Ahrens  *
447714843421SMatthew Ahrens  * outputs:
447814843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
447914843421SMatthew Ahrens  * zc_cookie	zap cursor
448014843421SMatthew Ahrens  */
448114843421SMatthew Ahrens static int
448214843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
448314843421SMatthew Ahrens {
448414843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4485eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
448614843421SMatthew Ahrens 
4487eeb85002STim Haley 	if (bufsize <= 0)
4488be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4489eeb85002STim Haley 
44901412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
44913b2aab18SMatthew Ahrens 	if (error != 0)
449214843421SMatthew Ahrens 		return (error);
449314843421SMatthew Ahrens 
449414843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
449514843421SMatthew Ahrens 
449614843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
449714843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
449814843421SMatthew Ahrens 
449914843421SMatthew Ahrens 	if (error == 0) {
450014843421SMatthew Ahrens 		error = xcopyout(buf,
450114843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
450214843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
450314843421SMatthew Ahrens 	}
450414843421SMatthew Ahrens 	kmem_free(buf, bufsize);
450514843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
450614843421SMatthew Ahrens 
450714843421SMatthew Ahrens 	return (error);
450814843421SMatthew Ahrens }
450914843421SMatthew Ahrens 
451014843421SMatthew Ahrens /*
451114843421SMatthew Ahrens  * inputs:
451214843421SMatthew Ahrens  * zc_name		name of filesystem
451314843421SMatthew Ahrens  *
451414843421SMatthew Ahrens  * outputs:
451514843421SMatthew Ahrens  * none
451614843421SMatthew Ahrens  */
451714843421SMatthew Ahrens static int
451814843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
451914843421SMatthew Ahrens {
452014843421SMatthew Ahrens 	objset_t *os;
45211195e687SMark J Musante 	int error = 0;
452214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
452314843421SMatthew Ahrens 
452414843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4525503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
452614843421SMatthew Ahrens 			/*
452714843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
452814843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
452914843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
453014843421SMatthew Ahrens 			 */
4531503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
453291948b51SKeith M Wesolowski 			if (error == 0) {
453391948b51SKeith M Wesolowski 				dmu_objset_refresh_ownership(zfsvfs->z_os,
453491948b51SKeith M Wesolowski 				    zfsvfs);
4535503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
453691948b51SKeith M Wesolowski 			}
453714843421SMatthew Ahrens 		}
453814843421SMatthew Ahrens 		if (error == 0)
453914843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
454014843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
454114843421SMatthew Ahrens 	} else {
4542503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4543503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
45443b2aab18SMatthew Ahrens 		if (error != 0)
454514843421SMatthew Ahrens 			return (error);
454614843421SMatthew Ahrens 
454714843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4548503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
454914843421SMatthew Ahrens 	}
455014843421SMatthew Ahrens 
455114843421SMatthew Ahrens 	return (error);
455214843421SMatthew Ahrens }
455314843421SMatthew Ahrens 
4554ecd6cf80Smarks /*
4555ecd6cf80Smarks  * We don't want to have a hard dependency
4556ecd6cf80Smarks  * against some special symbols in sharefs
4557da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4558ecd6cf80Smarks  * the first file system is shared.
4559da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4560ecd6cf80Smarks  */
4561da6c28aaSamw int (*znfsexport_fs)(void *arg);
4562ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4563da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4564da6c28aaSamw 
4565da6c28aaSamw int zfs_nfsshare_inited;
4566da6c28aaSamw int zfs_smbshare_inited;
4567ecd6cf80Smarks 
4568ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4569ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4570da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4571ecd6cf80Smarks kmutex_t zfs_share_lock;
4572ecd6cf80Smarks 
4573da6c28aaSamw static int
4574da6c28aaSamw zfs_init_sharefs()
4575da6c28aaSamw {
4576da6c28aaSamw 	int error;
4577da6c28aaSamw 
4578da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4579da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4580da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4581da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4582da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4583be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4584da6c28aaSamw 	}
4585da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4586da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4587da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4588be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4589da6c28aaSamw 	}
4590da6c28aaSamw 	return (0);
4591da6c28aaSamw }
4592da6c28aaSamw 
4593ecd6cf80Smarks static int
4594ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4595ecd6cf80Smarks {
4596ecd6cf80Smarks 	int error;
4597ecd6cf80Smarks 	int opcode;
4598ecd6cf80Smarks 
4599da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4600da6c28aaSamw 	case ZFS_SHARE_NFS:
4601da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4602da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4603da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4604da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4605da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4606da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4607be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4608da6c28aaSamw 			}
4609da6c28aaSamw 			if (znfsexport_fs == NULL &&
4610da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4611da6c28aaSamw 			    ddi_modsym(nfs_mod,
4612da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4613da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4614be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4615da6c28aaSamw 			}
4616da6c28aaSamw 			error = zfs_init_sharefs();
46173b2aab18SMatthew Ahrens 			if (error != 0) {
4618da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4619be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4620da6c28aaSamw 			}
4621da6c28aaSamw 			zfs_nfsshare_inited = 1;
4622ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4623ecd6cf80Smarks 		}
4624da6c28aaSamw 		break;
4625da6c28aaSamw 	case ZFS_SHARE_SMB:
4626da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4627da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4628da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4629da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4630da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4631da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4632da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4633be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4634da6c28aaSamw 			}
4635da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4636da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4637faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
4638da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4639be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4640da6c28aaSamw 			}
4641da6c28aaSamw 			error = zfs_init_sharefs();
46423b2aab18SMatthew Ahrens 			if (error != 0) {
4643da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4644be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4645da6c28aaSamw 			}
4646da6c28aaSamw 			zfs_smbshare_inited = 1;
4647ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4648ecd6cf80Smarks 		}
4649da6c28aaSamw 		break;
4650da6c28aaSamw 	default:
4651be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4652da6c28aaSamw 	}
4653ecd6cf80Smarks 
4654da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4655da6c28aaSamw 	case ZFS_SHARE_NFS:
4656da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4657da6c28aaSamw 		if (error =
4658da6c28aaSamw 		    znfsexport_fs((void *)
4659da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4660da6c28aaSamw 			return (error);
4661da6c28aaSamw 		break;
4662da6c28aaSamw 	case ZFS_SHARE_SMB:
4663da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4664da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4665da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4666da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4667743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
4668da6c28aaSamw 			return (error);
4669ecd6cf80Smarks 		}
4670da6c28aaSamw 		break;
4671ecd6cf80Smarks 	}
4672ecd6cf80Smarks 
4673da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4674da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4675ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4676ecd6cf80Smarks 
4677da6c28aaSamw 	/*
4678da6c28aaSamw 	 * Add or remove share from sharetab
4679da6c28aaSamw 	 */
4680ecd6cf80Smarks 	error = zshare_fs(opcode,
4681ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4682ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4683ecd6cf80Smarks 
4684ecd6cf80Smarks 	return (error);
4685ecd6cf80Smarks 
4686ecd6cf80Smarks }
4687ecd6cf80Smarks 
4688743a77edSAlan Wright ace_t full_access[] = {
4689743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4690743a77edSAlan Wright };
4691743a77edSAlan Wright 
469299d5e173STim Haley /*
469399d5e173STim Haley  * inputs:
469499d5e173STim Haley  * zc_name		name of containing filesystem
469599d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
469699d5e173STim Haley  *
469799d5e173STim Haley  * outputs:
469899d5e173STim Haley  * zc_obj		next in-use object #
469999d5e173STim Haley  */
470099d5e173STim Haley static int
470199d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
470299d5e173STim Haley {
470399d5e173STim Haley 	objset_t *os = NULL;
470499d5e173STim Haley 	int error;
470599d5e173STim Haley 
470699d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
47073b2aab18SMatthew Ahrens 	if (error != 0)
470899d5e173STim Haley 		return (error);
470999d5e173STim Haley 
471099d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
471199d5e173STim Haley 	    os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
471299d5e173STim Haley 
471399d5e173STim Haley 	dmu_objset_rele(os, FTAG);
471499d5e173STim Haley 	return (error);
471599d5e173STim Haley }
471699d5e173STim Haley 
471799d5e173STim Haley /*
471899d5e173STim Haley  * inputs:
471999d5e173STim Haley  * zc_name		name of filesystem
472099d5e173STim Haley  * zc_value		prefix name for snapshot
472199d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
472299d5e173STim Haley  *
472399d5e173STim Haley  * outputs:
47244445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
472599d5e173STim Haley  */
472699d5e173STim Haley static int
472799d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
472899d5e173STim Haley {
472999d5e173STim Haley 	char *snap_name;
47303b2aab18SMatthew Ahrens 	char *hold_name;
473199d5e173STim Haley 	int error;
47323b2aab18SMatthew Ahrens 	minor_t minor;
473399d5e173STim Haley 
47343b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
47353b2aab18SMatthew Ahrens 	if (error != 0)
473699d5e173STim Haley 		return (error);
473799d5e173STim Haley 
47383b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
47393b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
47403b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
47413b2aab18SMatthew Ahrens 
47423b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
47433b2aab18SMatthew Ahrens 	    hold_name);
47443b2aab18SMatthew Ahrens 	if (error == 0)
47453b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
474699d5e173STim Haley 	strfree(snap_name);
47473b2aab18SMatthew Ahrens 	strfree(hold_name);
47483b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
47493b2aab18SMatthew Ahrens 	return (error);
475099d5e173STim Haley }
475199d5e173STim Haley 
475299d5e173STim Haley /*
475399d5e173STim Haley  * inputs:
475499d5e173STim Haley  * zc_name		name of "to" snapshot
475599d5e173STim Haley  * zc_value		name of "from" snapshot
475699d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
475799d5e173STim Haley  *
475899d5e173STim Haley  * outputs:
475999d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
476099d5e173STim Haley  */
476199d5e173STim Haley static int
476299d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
476399d5e173STim Haley {
476499d5e173STim Haley 	file_t *fp;
476599d5e173STim Haley 	offset_t off;
476699d5e173STim Haley 	int error;
476799d5e173STim Haley 
476899d5e173STim Haley 	fp = getf(zc->zc_cookie);
47693b2aab18SMatthew Ahrens 	if (fp == NULL)
4770be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
477199d5e173STim Haley 
477299d5e173STim Haley 	off = fp->f_offset;
477399d5e173STim Haley 
47743b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
477599d5e173STim Haley 
477699d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
477799d5e173STim Haley 		fp->f_offset = off;
477899d5e173STim Haley 	releasef(zc->zc_cookie);
477999d5e173STim Haley 
478099d5e173STim Haley 	return (error);
478199d5e173STim Haley }
478299d5e173STim Haley 
4783743a77edSAlan Wright /*
4784743a77edSAlan Wright  * Remove all ACL files in shares dir
4785743a77edSAlan Wright  */
4786743a77edSAlan Wright static int
4787743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
4788743a77edSAlan Wright {
4789743a77edSAlan Wright 	zap_cursor_t	zc;
4790743a77edSAlan Wright 	zap_attribute_t	zap;
4791743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4792743a77edSAlan Wright 	int error;
4793743a77edSAlan Wright 
4794743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4795743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4796743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
4797743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4798743a77edSAlan Wright 		    NULL, 0)) != 0)
4799743a77edSAlan Wright 			break;
4800743a77edSAlan Wright 	}
4801743a77edSAlan Wright 	zap_cursor_fini(&zc);
4802743a77edSAlan Wright 	return (error);
4803743a77edSAlan Wright }
4804743a77edSAlan Wright 
4805743a77edSAlan Wright static int
4806743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
4807743a77edSAlan Wright {
4808743a77edSAlan Wright 	vnode_t *vp;
4809743a77edSAlan Wright 	znode_t *dzp;
4810743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
4811743a77edSAlan Wright 	znode_t *sharedir;
4812743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
4813743a77edSAlan Wright 	nvlist_t *nvlist;
4814743a77edSAlan Wright 	char *src, *target;
4815743a77edSAlan Wright 	vattr_t vattr;
4816743a77edSAlan Wright 	vsecattr_t vsec;
4817743a77edSAlan Wright 	int error = 0;
4818743a77edSAlan Wright 
4819743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4820743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
4821743a77edSAlan Wright 		return (error);
4822743a77edSAlan Wright 
4823743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
4824743a77edSAlan Wright 
4825743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4826743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4827743a77edSAlan Wright 	    zc->zc_name) != 0)) {
4828743a77edSAlan Wright 		VN_RELE(vp);
4829be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4830743a77edSAlan Wright 	}
4831743a77edSAlan Wright 
4832743a77edSAlan Wright 	dzp = VTOZ(vp);
4833743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
4834743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
4835743a77edSAlan Wright 
48369e1320c0SMark Shellenbaum 	/*
48379e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
48389e1320c0SMark Shellenbaum 	 */
48399e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
48409e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
48419e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
48429e1320c0SMark Shellenbaum 
48439e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
48449e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
48459e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
48469e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
48479e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
48483b2aab18SMatthew Ahrens 		if (error != 0) {
48499e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
48509e1320c0SMark Shellenbaum 		} else {
48519e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
48529e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
48539e1320c0SMark Shellenbaum 		}
48543b2aab18SMatthew Ahrens 		if (error != 0) {
48559e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
48569e1320c0SMark Shellenbaum 			VN_RELE(vp);
48579e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
48589e1320c0SMark Shellenbaum 			return (error);
48599e1320c0SMark Shellenbaum 		}
48609e1320c0SMark Shellenbaum 	}
48619e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
48629e1320c0SMark Shellenbaum 
48639e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
4864743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
48659e1320c0SMark Shellenbaum 		VN_RELE(vp);
4866743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
4867743a77edSAlan Wright 		return (error);
4868743a77edSAlan Wright 	}
4869743a77edSAlan Wright 
4870743a77edSAlan Wright 	switch (zc->zc_cookie) {
4871743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4872743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4873743a77edSAlan Wright 		vattr.va_type = VREG;
4874743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
4875743a77edSAlan Wright 		vattr.va_uid = 0;
4876743a77edSAlan Wright 		vattr.va_gid = 0;
4877743a77edSAlan Wright 
4878743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
4879743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
4880743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
4881743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
4882743a77edSAlan Wright 
4883743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4884743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4885743a77edSAlan Wright 		if (resourcevp)
4886743a77edSAlan Wright 			VN_RELE(resourcevp);
4887743a77edSAlan Wright 		break;
4888743a77edSAlan Wright 
4889743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4890743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4891743a77edSAlan Wright 		    NULL, 0);
4892743a77edSAlan Wright 		break;
4893743a77edSAlan Wright 
4894743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4895743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
4896478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4897743a77edSAlan Wright 			VN_RELE(vp);
4898743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
4899743a77edSAlan Wright 			return (error);
4900743a77edSAlan Wright 		}
4901743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4902743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4903743a77edSAlan Wright 		    &target)) {
4904743a77edSAlan Wright 			VN_RELE(vp);
490589459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
4906743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
49071195e687SMark J Musante 			nvlist_free(nvlist);
4908743a77edSAlan Wright 			return (error);
4909743a77edSAlan Wright 		}
4910743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4911743a77edSAlan Wright 		    kcred, NULL, 0);
4912743a77edSAlan Wright 		nvlist_free(nvlist);
4913743a77edSAlan Wright 		break;
4914743a77edSAlan Wright 
4915743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4916743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
4917743a77edSAlan Wright 		break;
4918743a77edSAlan Wright 
4919743a77edSAlan Wright 	default:
4920be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
4921743a77edSAlan Wright 		break;
4922743a77edSAlan Wright 	}
4923743a77edSAlan Wright 
4924743a77edSAlan Wright 	VN_RELE(vp);
4925743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
4926743a77edSAlan Wright 
4927743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
4928743a77edSAlan Wright 
4929743a77edSAlan Wright 	return (error);
4930743a77edSAlan Wright }
4931743a77edSAlan Wright 
4932842727c2SChris Kirby /*
49333b2aab18SMatthew Ahrens  * innvl: {
49343b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
49353b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
49363b2aab18SMatthew Ahrens  * }
4937842727c2SChris Kirby  *
49383b2aab18SMatthew Ahrens  * outnvl: {
49393b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
49403b2aab18SMatthew Ahrens  *     ...
49413b2aab18SMatthew Ahrens  * }
4942842727c2SChris Kirby  */
49433b2aab18SMatthew Ahrens /* ARGSUSED */
4944842727c2SChris Kirby static int
49453b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
4946842727c2SChris Kirby {
49473b2aab18SMatthew Ahrens 	nvlist_t *holds;
49483b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
4949a7f53a56SChris Kirby 	int error;
4950a7f53a56SChris Kirby 	minor_t minor = 0;
4951842727c2SChris Kirby 
49523b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
49533b2aab18SMatthew Ahrens 	if (error != 0)
4954be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4955a7f53a56SChris Kirby 
49563b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
49573b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
49583b2aab18SMatthew Ahrens 		if (error != 0)
4959a7f53a56SChris Kirby 			return (error);
4960a7f53a56SChris Kirby 	}
4961a7f53a56SChris Kirby 
49623b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
49633b2aab18SMatthew Ahrens 	if (minor != 0)
49643b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
4965a7f53a56SChris Kirby 	return (error);
4966842727c2SChris Kirby }
4967842727c2SChris Kirby 
4968842727c2SChris Kirby /*
49693b2aab18SMatthew Ahrens  * innvl is not used.
4970842727c2SChris Kirby  *
49713b2aab18SMatthew Ahrens  * outnvl: {
49723b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
49733b2aab18SMatthew Ahrens  *    ...
49743b2aab18SMatthew Ahrens  * }
4975842727c2SChris Kirby  */
49763b2aab18SMatthew Ahrens /* ARGSUSED */
4977842727c2SChris Kirby static int
49783b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
4979842727c2SChris Kirby {
49803b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
4981842727c2SChris Kirby }
4982842727c2SChris Kirby 
4983842727c2SChris Kirby /*
49843b2aab18SMatthew Ahrens  * innvl: {
49853b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
49863b2aab18SMatthew Ahrens  *     ...
49873b2aab18SMatthew Ahrens  * }
4988842727c2SChris Kirby  *
49893b2aab18SMatthew Ahrens  * outnvl: {
49903b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
49913b2aab18SMatthew Ahrens  *     ...
49923b2aab18SMatthew Ahrens  * }
4993842727c2SChris Kirby  */
49943b2aab18SMatthew Ahrens /* ARGSUSED */
4995842727c2SChris Kirby static int
49963b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
4997842727c2SChris Kirby {
49983b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
4999842727c2SChris Kirby }
5000842727c2SChris Kirby 
500119b94df9SMatthew Ahrens /*
500219b94df9SMatthew Ahrens  * inputs:
500319b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
500419b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
500519b94df9SMatthew Ahrens  *
500619b94df9SMatthew Ahrens  * outputs:
500719b94df9SMatthew Ahrens  * zc_cookie		space in bytes
500819b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
500919b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
501019b94df9SMatthew Ahrens  */
501119b94df9SMatthew Ahrens static int
501219b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
501319b94df9SMatthew Ahrens {
501419b94df9SMatthew Ahrens 	int error;
50153b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
501619b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
501719b94df9SMatthew Ahrens 
50183b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
501919b94df9SMatthew Ahrens 	if (error != 0)
502019b94df9SMatthew Ahrens 		return (error);
50213b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
50223b2aab18SMatthew Ahrens 	if (error != 0) {
50233b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
50243b2aab18SMatthew Ahrens 		return (error);
50253b2aab18SMatthew Ahrens 	}
50263b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
502719b94df9SMatthew Ahrens 	if (error != 0) {
502819b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
50293b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
503019b94df9SMatthew Ahrens 		return (error);
503119b94df9SMatthew Ahrens 	}
503219b94df9SMatthew Ahrens 
503319b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
503419b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
503519b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
503619b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
50373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
503819b94df9SMatthew Ahrens 	return (error);
503919b94df9SMatthew Ahrens }
50403b2aab18SMatthew Ahrens 
504119b94df9SMatthew Ahrens /*
50424445fffbSMatthew Ahrens  * innvl: {
50434445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
50444445fffbSMatthew Ahrens  * }
504519b94df9SMatthew Ahrens  *
50464445fffbSMatthew Ahrens  * outnvl: {
50474445fffbSMatthew Ahrens  *     "used" -> space in bytes
50484445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
50494445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
50504445fffbSMatthew Ahrens  * }
505119b94df9SMatthew Ahrens  */
505219b94df9SMatthew Ahrens static int
50534445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
505419b94df9SMatthew Ahrens {
505519b94df9SMatthew Ahrens 	int error;
50563b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
505719b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
50584445fffbSMatthew Ahrens 	char *firstsnap;
50594445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
506019b94df9SMatthew Ahrens 
50614445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5062be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
50634445fffbSMatthew Ahrens 
50643b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
506519b94df9SMatthew Ahrens 	if (error != 0)
506619b94df9SMatthew Ahrens 		return (error);
50673b2aab18SMatthew Ahrens 
50683b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
50693b2aab18SMatthew Ahrens 	if (error != 0) {
50703b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
50713b2aab18SMatthew Ahrens 		return (error);
50723b2aab18SMatthew Ahrens 	}
50733b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
507419b94df9SMatthew Ahrens 	if (error != 0) {
507519b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
50763b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
507719b94df9SMatthew Ahrens 		return (error);
507819b94df9SMatthew Ahrens 	}
507919b94df9SMatthew Ahrens 
50804445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
508119b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
508219b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
50833b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
50844445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
50854445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
50864445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
508719b94df9SMatthew Ahrens 	return (error);
508819b94df9SMatthew Ahrens }
508919b94df9SMatthew Ahrens 
5090ecd6cf80Smarks /*
50914445fffbSMatthew Ahrens  * innvl: {
50924445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
50934445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
50944445fffbSMatthew Ahrens  * }
50954445fffbSMatthew Ahrens  *
50964445fffbSMatthew Ahrens  * outnvl is unused
5097ecd6cf80Smarks  */
50984445fffbSMatthew Ahrens /* ARGSUSED */
50994445fffbSMatthew Ahrens static int
51004445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
51014445fffbSMatthew Ahrens {
51024445fffbSMatthew Ahrens 	int error;
51034445fffbSMatthew Ahrens 	offset_t off;
51043b2aab18SMatthew Ahrens 	char *fromname = NULL;
51054445fffbSMatthew Ahrens 	int fd;
51064445fffbSMatthew Ahrens 
51074445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
51084445fffbSMatthew Ahrens 	if (error != 0)
5109be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
51104445fffbSMatthew Ahrens 
51113b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
51124445fffbSMatthew Ahrens 
51134445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
51143b2aab18SMatthew Ahrens 	if (fp == NULL)
5115be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
51164445fffbSMatthew Ahrens 
51174445fffbSMatthew Ahrens 	off = fp->f_offset;
51183b2aab18SMatthew Ahrens 	error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
51194445fffbSMatthew Ahrens 
51204445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
51214445fffbSMatthew Ahrens 		fp->f_offset = off;
51224445fffbSMatthew Ahrens 	releasef(fd);
51234445fffbSMatthew Ahrens 	return (error);
51244445fffbSMatthew Ahrens }
51254445fffbSMatthew Ahrens 
51264445fffbSMatthew Ahrens /*
51274445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
51284445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
51294445fffbSMatthew Ahrens  *
51304445fffbSMatthew Ahrens  * innvl: {
51314445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
51324445fffbSMatthew Ahrens  * }
51334445fffbSMatthew Ahrens  *
51344445fffbSMatthew Ahrens  * outnvl: {
51354445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
51364445fffbSMatthew Ahrens  * }
51374445fffbSMatthew Ahrens  */
51384445fffbSMatthew Ahrens static int
51394445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
51404445fffbSMatthew Ahrens {
51413b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
51423b2aab18SMatthew Ahrens 	dsl_dataset_t *fromsnap = NULL;
51433b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
51444445fffbSMatthew Ahrens 	int error;
51454445fffbSMatthew Ahrens 	char *fromname;
51464445fffbSMatthew Ahrens 	uint64_t space;
51474445fffbSMatthew Ahrens 
51483b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
51493b2aab18SMatthew Ahrens 	if (error != 0)
51503b2aab18SMatthew Ahrens 		return (error);
51513b2aab18SMatthew Ahrens 
51523b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
51533b2aab18SMatthew Ahrens 	if (error != 0) {
51543b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
51554445fffbSMatthew Ahrens 		return (error);
51563b2aab18SMatthew Ahrens 	}
51574445fffbSMatthew Ahrens 
51584445fffbSMatthew Ahrens 	error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
51594445fffbSMatthew Ahrens 	if (error == 0) {
51603b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
51613b2aab18SMatthew Ahrens 		if (error != 0) {
51623b2aab18SMatthew Ahrens 			dsl_dataset_rele(tosnap, FTAG);
51633b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
51644445fffbSMatthew Ahrens 			return (error);
51654445fffbSMatthew Ahrens 		}
51664445fffbSMatthew Ahrens 	}
51674445fffbSMatthew Ahrens 
51684445fffbSMatthew Ahrens 	error = dmu_send_estimate(tosnap, fromsnap, &space);
51694445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
51704445fffbSMatthew Ahrens 
51714445fffbSMatthew Ahrens 	if (fromsnap != NULL)
51723b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromsnap, FTAG);
51733b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
51743b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
51754445fffbSMatthew Ahrens 	return (error);
51764445fffbSMatthew Ahrens }
51774445fffbSMatthew Ahrens 
51784445fffbSMatthew Ahrens 
51794445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
51804445fffbSMatthew Ahrens 
51814445fffbSMatthew Ahrens static void
51824445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
51834445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
51844445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
51854445fffbSMatthew Ahrens {
51864445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
51874445fffbSMatthew Ahrens 
51884445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
51894445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
51904445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
51914445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
51924445fffbSMatthew Ahrens 
51934445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
51944445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
51954445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
51964445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
51974445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
51984445fffbSMatthew Ahrens }
51994445fffbSMatthew Ahrens 
52004445fffbSMatthew Ahrens /*
52014445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
52024445fffbSMatthew Ahrens  * each argument to this function.
52034445fffbSMatthew Ahrens  */
52044445fffbSMatthew Ahrens static void
52054445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
52064445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
52074445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
52084445fffbSMatthew Ahrens     boolean_t allow_log)
52094445fffbSMatthew Ahrens {
52104445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
52114445fffbSMatthew Ahrens 
52124445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
52134445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
52144445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
52154445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
52164445fffbSMatthew Ahrens 
52174445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
52184445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
52194445fffbSMatthew Ahrens 
52204445fffbSMatthew Ahrens 	vec->zvec_name = name;
52214445fffbSMatthew Ahrens 	vec->zvec_func = func;
52224445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
52234445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
52244445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
52254445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
52264445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
52274445fffbSMatthew Ahrens }
52284445fffbSMatthew Ahrens 
52294445fffbSMatthew Ahrens static void
52304445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52314445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
52324445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
52334445fffbSMatthew Ahrens {
52344445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52354445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
52364445fffbSMatthew Ahrens }
52374445fffbSMatthew Ahrens 
52384445fffbSMatthew Ahrens static void
52394445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52404445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
52414445fffbSMatthew Ahrens {
52424445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52434445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
52444445fffbSMatthew Ahrens }
52454445fffbSMatthew Ahrens 
52464445fffbSMatthew Ahrens static void
52474445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
52484445fffbSMatthew Ahrens {
52494445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
52504445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
52514445fffbSMatthew Ahrens }
52524445fffbSMatthew Ahrens 
52534445fffbSMatthew Ahrens static void
52544445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52554445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
52564445fffbSMatthew Ahrens {
52574445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52584445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
52594445fffbSMatthew Ahrens }
52604445fffbSMatthew Ahrens 
52614445fffbSMatthew Ahrens static void
52624445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
52634445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
52644445fffbSMatthew Ahrens {
52654445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52664445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
52674445fffbSMatthew Ahrens }
52684445fffbSMatthew Ahrens 
52694445fffbSMatthew Ahrens static void
52704445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
52714445fffbSMatthew Ahrens {
52724445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
52734445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
52744445fffbSMatthew Ahrens }
52754445fffbSMatthew Ahrens 
52764445fffbSMatthew Ahrens static void
52774445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52784445fffbSMatthew Ahrens 	zfs_secpolicy_func_t *secpolicy)
52794445fffbSMatthew Ahrens {
52804445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52814445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
52824445fffbSMatthew Ahrens }
52834445fffbSMatthew Ahrens 
52844445fffbSMatthew Ahrens static void
52854445fffbSMatthew Ahrens zfs_ioctl_init(void)
52864445fffbSMatthew Ahrens {
52874445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
52884445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
52894445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
52904445fffbSMatthew Ahrens 
52914445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
52924445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
52934445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
52944445fffbSMatthew Ahrens 
52954445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
52964445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
52974445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
52984445fffbSMatthew Ahrens 
52994445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
53004445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
53014445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
53024445fffbSMatthew Ahrens 
53034445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
53044445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
53054445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
53064445fffbSMatthew Ahrens 
53074445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
53084445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
53094445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53104445fffbSMatthew Ahrens 
53114445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
53124445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
53134445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53144445fffbSMatthew Ahrens 
53154445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
53164445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
53174445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53184445fffbSMatthew Ahrens 
53193b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
53203b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
53213b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53223b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
53233b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
53243b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53253b2aab18SMatthew Ahrens 
53263b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
53273b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
53283b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
53293b2aab18SMatthew Ahrens 
5330a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5331a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5332a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5333a7027df1SMatthew Ahrens 
53344445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
53354445fffbSMatthew Ahrens 
53364445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
53374445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
53384445fffbSMatthew Ahrens 
53394445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
53404445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
53414445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
53424445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
53434445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
53444445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
53454445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
53464445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
53474445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
53484445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
53494445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
53504445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
53514445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
53524445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
53534445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
53544445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
53554445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
53564445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
53574445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
53584445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
53594445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
53604445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
53614445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
53624445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
53634445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
53644445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
53654445fffbSMatthew Ahrens 
53664445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
53674445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
53684445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
53694445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
53704445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
53714445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
53724445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
53734445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
53744445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
53754445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
53764445fffbSMatthew Ahrens 
53774445fffbSMatthew Ahrens 	/*
53784445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
53794445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
53804445fffbSMatthew Ahrens 	 * does the logging of those commands.
53814445fffbSMatthew Ahrens 	 */
53824445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
53834445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
53844445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
53854445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
53864445fffbSMatthew Ahrens 
53874445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
53884445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
53894445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
53904445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
53914445fffbSMatthew Ahrens 
53924445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
53934445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
53944445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
53954445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
53964445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
53974445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
53984445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
53994445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
54004445fffbSMatthew Ahrens 
54014445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
54024445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
54034445fffbSMatthew Ahrens 
54044445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
540522e30981SGeorge Wilson 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
54064445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
54074445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
54084445fffbSMatthew Ahrens 
54094445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
54104445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
54114445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
54124445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
54134445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
54144445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
54154445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
54164445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
54174445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
54184445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
54194445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
54204445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
54214445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
54224445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
54234445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
54244445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
54254445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
54264445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
54274445fffbSMatthew Ahrens 
54284445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
54294445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
54304445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
54314445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
54324445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
54334445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
54344445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
54354445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
54364445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
54374445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
54384445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
54394445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
54404445fffbSMatthew Ahrens 
54414445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
54424445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
54434445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
54444445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
54454445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
54464445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
54474445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
54484445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
54494445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
54504445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
54514445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
54524445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
54534445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
54544445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
54554445fffbSMatthew Ahrens 
54564445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
54574445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
54584445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
54594445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
54604445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
54614445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
54624445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
54634445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
54644445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
54654445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
54664445fffbSMatthew Ahrens }
5467fa9e4066Sahrens 
546854d692b7SGeorge Wilson int
5469f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5470f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
547154d692b7SGeorge Wilson {
547254d692b7SGeorge Wilson 	spa_t *spa;
547354d692b7SGeorge Wilson 	int error;
547454d692b7SGeorge Wilson 
547554d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
547654d692b7SGeorge Wilson 
5477f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5478f9af39baSGeorge Wilson 		return (0);
5479f9af39baSGeorge Wilson 
548014843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
548154d692b7SGeorge Wilson 	if (error == 0) {
5482f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5483be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5484f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5485be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
548654d692b7SGeorge Wilson 		spa_close(spa, FTAG);
548754d692b7SGeorge Wilson 	}
548854d692b7SGeorge Wilson 	return (error);
548954d692b7SGeorge Wilson }
549054d692b7SGeorge Wilson 
5491c99e4bdcSChris Kirby /*
5492c99e4bdcSChris Kirby  * Find a free minor number.
5493c99e4bdcSChris Kirby  */
5494c99e4bdcSChris Kirby minor_t
5495c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5496c99e4bdcSChris Kirby {
5497c99e4bdcSChris Kirby 	static minor_t last_minor;
5498c99e4bdcSChris Kirby 	minor_t m;
5499c99e4bdcSChris Kirby 
5500c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5501c99e4bdcSChris Kirby 
5502c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5503c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5504c99e4bdcSChris Kirby 			m = 1;
5505c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5506c99e4bdcSChris Kirby 			last_minor = m;
5507c99e4bdcSChris Kirby 			return (m);
5508c99e4bdcSChris Kirby 		}
5509c99e4bdcSChris Kirby 	}
5510c99e4bdcSChris Kirby 
5511c99e4bdcSChris Kirby 	return (0);
5512c99e4bdcSChris Kirby }
5513c99e4bdcSChris Kirby 
5514c99e4bdcSChris Kirby static int
5515c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5516c99e4bdcSChris Kirby {
5517c99e4bdcSChris Kirby 	minor_t minor;
5518c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5519c99e4bdcSChris Kirby 
5520c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5521c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5522c99e4bdcSChris Kirby 
5523c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5524c99e4bdcSChris Kirby 	if (minor == 0)
5525be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5526c99e4bdcSChris Kirby 
5527c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5528be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5529c99e4bdcSChris Kirby 
5530c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5531c99e4bdcSChris Kirby 
5532c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
5533c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
5534c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5535c99e4bdcSChris Kirby 
5536c99e4bdcSChris Kirby 	return (0);
5537c99e4bdcSChris Kirby }
5538c99e4bdcSChris Kirby 
5539c99e4bdcSChris Kirby static void
5540c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5541c99e4bdcSChris Kirby {
5542c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5543c99e4bdcSChris Kirby 
5544c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
5545c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
5546c99e4bdcSChris Kirby }
5547c99e4bdcSChris Kirby 
5548c99e4bdcSChris Kirby void *
5549c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5550c99e4bdcSChris Kirby {
5551c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
5552c99e4bdcSChris Kirby 
5553c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
5554c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
5555c99e4bdcSChris Kirby 		return (NULL);
5556c99e4bdcSChris Kirby 
5557c99e4bdcSChris Kirby 	return (zp->zss_data);
5558c99e4bdcSChris Kirby }
5559c99e4bdcSChris Kirby 
5560c99e4bdcSChris Kirby static int
5561c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5562c99e4bdcSChris Kirby {
5563c99e4bdcSChris Kirby 	int error = 0;
5564c99e4bdcSChris Kirby 
5565c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
5566c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
5567c99e4bdcSChris Kirby 
5568c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
5569c99e4bdcSChris Kirby 	if (flag & FEXCL) {
5570c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
5571c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
5572c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5573c99e4bdcSChris Kirby 	}
5574c99e4bdcSChris Kirby 
5575c99e4bdcSChris Kirby 	return (error);
5576c99e4bdcSChris Kirby }
5577c99e4bdcSChris Kirby 
5578c99e4bdcSChris Kirby static int
5579c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5580c99e4bdcSChris Kirby {
5581c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
5582c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5583c99e4bdcSChris Kirby 
5584c99e4bdcSChris Kirby 	if (minor == 0)
5585c99e4bdcSChris Kirby 		return (0);
5586c99e4bdcSChris Kirby 
5587c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5588c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5589c99e4bdcSChris Kirby 	if (zo == NULL) {
5590c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5591c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5592c99e4bdcSChris Kirby 	}
5593c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5594c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5595c99e4bdcSChris Kirby 
5596c99e4bdcSChris Kirby 	return (0);
5597c99e4bdcSChris Kirby }
5598c99e4bdcSChris Kirby 
5599fa9e4066Sahrens static int
5600fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5601fa9e4066Sahrens {
5602fa9e4066Sahrens 	zfs_cmd_t *zc;
56034445fffbSMatthew Ahrens 	uint_t vecnum;
56044445fffbSMatthew Ahrens 	int error, rc, len;
5605c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
56064445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
56074445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
56084445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
5609fa9e4066Sahrens 
5610c99e4bdcSChris Kirby 	if (minor != 0 &&
5611c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5612fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5613fa9e4066Sahrens 
56144445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
561591ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5616fa9e4066Sahrens 
56174445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5618be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
56194445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
5620fa9e4066Sahrens 
5621fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5622fa9e4066Sahrens 
5623478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
56244445fffbSMatthew Ahrens 	if (error != 0) {
5625be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
56264445fffbSMatthew Ahrens 		goto out;
56274445fffbSMatthew Ahrens 	}
5628fa9e4066Sahrens 
56294445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
56304445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
56314445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
56324445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
56334445fffbSMatthew Ahrens 		if (error != 0)
56344445fffbSMatthew Ahrens 			goto out;
56354445fffbSMatthew Ahrens 	}
5636fa9e4066Sahrens 
5637fa9e4066Sahrens 	/*
5638fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
5639fa9e4066Sahrens 	 * the lower layers.
5640fa9e4066Sahrens 	 */
56414445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
56424445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
56434445fffbSMatthew Ahrens 	case POOL_NAME:
56444445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5645be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
56464445fffbSMatthew Ahrens 		else
5647f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
56484445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
56494445fffbSMatthew Ahrens 		break;
5650fa9e4066Sahrens 
56514445fffbSMatthew Ahrens 	case DATASET_NAME:
56524445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5653be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
56544445fffbSMatthew Ahrens 		else
5655f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
56564445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
56574445fffbSMatthew Ahrens 		break;
56585ad82045Snd 
56594445fffbSMatthew Ahrens 	case NO_NAME:
56604445fffbSMatthew Ahrens 		break;
5661fa9e4066Sahrens 	}
5662fa9e4066Sahrens 
5663fa9e4066Sahrens 
56644445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
56654445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
56664445fffbSMatthew Ahrens 
56674445fffbSMatthew Ahrens 	if (error != 0)
56684445fffbSMatthew Ahrens 		goto out;
56694445fffbSMatthew Ahrens 
56704445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
56714445fffbSMatthew Ahrens 	len = strcspn(zc->zc_name, "/@") + 1;
56724445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
56734445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
56744445fffbSMatthew Ahrens 
56754445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
56764445fffbSMatthew Ahrens 		nvlist_t *outnvl;
56774445fffbSMatthew Ahrens 		int puterror = 0;
56784445fffbSMatthew Ahrens 		spa_t *spa;
56794445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
56804445fffbSMatthew Ahrens 
56814445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
56824445fffbSMatthew Ahrens 
56834445fffbSMatthew Ahrens 		/*
56844445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
56854445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
56864445fffbSMatthew Ahrens 		 */
56874445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
56884445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
56894445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
56904445fffbSMatthew Ahrens 			    vec->zvec_name);
56914445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
56924445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
56934445fffbSMatthew Ahrens 				    innvl);
56944445fffbSMatthew Ahrens 			}
56954445fffbSMatthew Ahrens 		}
56964445fffbSMatthew Ahrens 
56974445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
56984445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
56994445fffbSMatthew Ahrens 
57004445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
57014445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
57024445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
57034445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
57044445fffbSMatthew Ahrens 				    outnvl);
57054445fffbSMatthew Ahrens 			}
57064445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
57074445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
57084445fffbSMatthew Ahrens 		}
57094445fffbSMatthew Ahrens 		fnvlist_free(lognv);
57104445fffbSMatthew Ahrens 
57114445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
57124445fffbSMatthew Ahrens 			int smusherror = 0;
57134445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
57144445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
57154445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
57164445fffbSMatthew Ahrens 			}
57174445fffbSMatthew Ahrens 			if (smusherror == 0)
57184445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
57194445fffbSMatthew Ahrens 		}
57204445fffbSMatthew Ahrens 
57214445fffbSMatthew Ahrens 		if (puterror != 0)
57224445fffbSMatthew Ahrens 			error = puterror;
57234445fffbSMatthew Ahrens 
57244445fffbSMatthew Ahrens 		nvlist_free(outnvl);
57254445fffbSMatthew Ahrens 	} else {
57264445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
57274445fffbSMatthew Ahrens 	}
57284445fffbSMatthew Ahrens 
57294445fffbSMatthew Ahrens out:
57304445fffbSMatthew Ahrens 	nvlist_free(innvl);
5731478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
57324445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
5733be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
57344445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
57354445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
57364445fffbSMatthew Ahrens 		if (s != NULL)
57374445fffbSMatthew Ahrens 			strfree(s);
57384445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
57394445fffbSMatthew Ahrens 	} else {
57404445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
57414445fffbSMatthew Ahrens 			strfree(saved_poolname);
5742ecd6cf80Smarks 	}
5743fa9e4066Sahrens 
5744fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
5745fa9e4066Sahrens 	return (error);
5746fa9e4066Sahrens }
5747fa9e4066Sahrens 
5748fa9e4066Sahrens static int
5749fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5750fa9e4066Sahrens {
5751fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
5752fa9e4066Sahrens 		return (DDI_FAILURE);
5753fa9e4066Sahrens 
5754fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5755fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
5756fa9e4066Sahrens 		return (DDI_FAILURE);
5757fa9e4066Sahrens 
5758fa9e4066Sahrens 	zfs_dip = dip;
5759fa9e4066Sahrens 
5760fa9e4066Sahrens 	ddi_report_dev(dip);
5761fa9e4066Sahrens 
5762fa9e4066Sahrens 	return (DDI_SUCCESS);
5763fa9e4066Sahrens }
5764fa9e4066Sahrens 
5765fa9e4066Sahrens static int
5766fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5767fa9e4066Sahrens {
5768fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
5769fa9e4066Sahrens 		return (DDI_FAILURE);
5770fa9e4066Sahrens 
5771fa9e4066Sahrens 	if (cmd != DDI_DETACH)
5772fa9e4066Sahrens 		return (DDI_FAILURE);
5773fa9e4066Sahrens 
5774fa9e4066Sahrens 	zfs_dip = NULL;
5775fa9e4066Sahrens 
5776fa9e4066Sahrens 	ddi_prop_remove_all(dip);
5777fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
5778fa9e4066Sahrens 
5779fa9e4066Sahrens 	return (DDI_SUCCESS);
5780fa9e4066Sahrens }
5781fa9e4066Sahrens 
5782fa9e4066Sahrens /*ARGSUSED*/
5783fa9e4066Sahrens static int
5784fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5785fa9e4066Sahrens {
5786fa9e4066Sahrens 	switch (infocmd) {
5787fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
5788fa9e4066Sahrens 		*result = zfs_dip;
5789fa9e4066Sahrens 		return (DDI_SUCCESS);
5790fa9e4066Sahrens 
5791fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
5792a0965f35Sbonwick 		*result = (void *)0;
5793fa9e4066Sahrens 		return (DDI_SUCCESS);
5794fa9e4066Sahrens 	}
5795fa9e4066Sahrens 
5796fa9e4066Sahrens 	return (DDI_FAILURE);
5797fa9e4066Sahrens }
5798fa9e4066Sahrens 
5799fa9e4066Sahrens /*
5800fa9e4066Sahrens  * OK, so this is a little weird.
5801fa9e4066Sahrens  *
5802fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
5803fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5804fa9e4066Sahrens  *
5805fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
5806fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
5807fa9e4066Sahrens  */
5808fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
5809c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
5810c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
5811fa9e4066Sahrens 	zvol_strategy,	/* strategy */
5812fa9e4066Sahrens 	nodev,		/* print */
5813e7cbe64fSgw 	zvol_dump,	/* dump */
5814fa9e4066Sahrens 	zvol_read,	/* read */
5815fa9e4066Sahrens 	zvol_write,	/* write */
5816fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
5817fa9e4066Sahrens 	nodev,		/* devmap */
5818fa9e4066Sahrens 	nodev,		/* mmap */
5819fa9e4066Sahrens 	nodev,		/* segmap */
5820fa9e4066Sahrens 	nochpoll,	/* poll */
5821fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
5822fa9e4066Sahrens 	NULL,		/* streamtab */
5823fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
5824fa9e4066Sahrens 	CB_REV,		/* version */
5825feb08c6bSbillm 	nodev,		/* async read */
5826feb08c6bSbillm 	nodev,		/* async write */
5827fa9e4066Sahrens };
5828fa9e4066Sahrens 
5829fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
5830fa9e4066Sahrens 	DEVO_REV,	/* version */
5831fa9e4066Sahrens 	0,		/* refcnt */
5832fa9e4066Sahrens 	zfs_info,	/* info */
5833fa9e4066Sahrens 	nulldev,	/* identify */
5834fa9e4066Sahrens 	nulldev,	/* probe */
5835fa9e4066Sahrens 	zfs_attach,	/* attach */
5836fa9e4066Sahrens 	zfs_detach,	/* detach */
5837fa9e4066Sahrens 	nodev,		/* reset */
5838fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
583919397407SSherry Moore 	NULL,		/* no bus operations */
584019397407SSherry Moore 	NULL,		/* power */
584119397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
5842fa9e4066Sahrens };
5843fa9e4066Sahrens 
5844fa9e4066Sahrens static struct modldrv zfs_modldrv = {
584519397407SSherry Moore 	&mod_driverops,
584619397407SSherry Moore 	"ZFS storage pool",
584719397407SSherry Moore 	&zfs_dev_ops
5848fa9e4066Sahrens };
5849fa9e4066Sahrens 
5850fa9e4066Sahrens static struct modlinkage modlinkage = {
5851fa9e4066Sahrens 	MODREV_1,
5852fa9e4066Sahrens 	(void *)&zfs_modlfs,
5853fa9e4066Sahrens 	(void *)&zfs_modldrv,
5854fa9e4066Sahrens 	NULL
5855fa9e4066Sahrens };
5856fa9e4066Sahrens 
58574445fffbSMatthew Ahrens static void
58584445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
58594445fffbSMatthew Ahrens {
58604445fffbSMatthew Ahrens 	char *poolname = arg;
58614445fffbSMatthew Ahrens 	strfree(poolname);
58624445fffbSMatthew Ahrens }
5863ec533521Sfr 
5864fa9e4066Sahrens int
5865fa9e4066Sahrens _init(void)
5866fa9e4066Sahrens {
5867fa9e4066Sahrens 	int error;
5868fa9e4066Sahrens 
5869a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
5870a0965f35Sbonwick 	zfs_init();
5871a0965f35Sbonwick 	zvol_init();
58724445fffbSMatthew Ahrens 	zfs_ioctl_init();
5873a0965f35Sbonwick 
5874a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
5875a0965f35Sbonwick 		zvol_fini();
5876a0965f35Sbonwick 		zfs_fini();
5877a0965f35Sbonwick 		spa_fini();
5878fa9e4066Sahrens 		return (error);
5879a0965f35Sbonwick 	}
5880fa9e4066Sahrens 
5881ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
58824445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
58834445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
5884ec533521Sfr 
5885fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5886fa9e4066Sahrens 	ASSERT(error == 0);
5887ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5888fa9e4066Sahrens 
5889fa9e4066Sahrens 	return (0);
5890fa9e4066Sahrens }
5891fa9e4066Sahrens 
5892fa9e4066Sahrens int
5893fa9e4066Sahrens _fini(void)
5894fa9e4066Sahrens {
5895fa9e4066Sahrens 	int error;
5896fa9e4066Sahrens 
5897ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5898be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
5899fa9e4066Sahrens 
5900fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
5901fa9e4066Sahrens 		return (error);
5902fa9e4066Sahrens 
5903fa9e4066Sahrens 	zvol_fini();
5904fa9e4066Sahrens 	zfs_fini();
5905fa9e4066Sahrens 	spa_fini();
5906da6c28aaSamw 	if (zfs_nfsshare_inited)
5907ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
5908da6c28aaSamw 	if (zfs_smbshare_inited)
5909da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
5910da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
5911ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
5912fa9e4066Sahrens 
5913ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
5914fa9e4066Sahrens 	ldi_ident_release(zfs_li);
5915fa9e4066Sahrens 	zfs_li = NULL;
5916ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
5917fa9e4066Sahrens 
5918fa9e4066Sahrens 	return (error);
5919fa9e4066Sahrens }
5920fa9e4066Sahrens 
5921fa9e4066Sahrens int
5922fa9e4066Sahrens _info(struct modinfo *modinfop)
5923fa9e4066Sahrens {
5924fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
5925fa9e4066Sahrens }
5926