xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision be6fd75a)
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.
27*be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
28a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
294445fffbSMatthew Ahrens  */
304445fffbSMatthew Ahrens 
314445fffbSMatthew Ahrens /*
324445fffbSMatthew Ahrens  * ZFS ioctls.
334445fffbSMatthew Ahrens  *
344445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
354445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
364445fffbSMatthew Ahrens  *
374445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
384445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
394445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
404445fffbSMatthew Ahrens  *
414445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
424445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
434445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
444445fffbSMatthew Ahrens  *
454445fffbSMatthew Ahrens  * The registration arguments are as follows:
464445fffbSMatthew Ahrens  *
474445fffbSMatthew Ahrens  * const char *name
484445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
494445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
504445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
514445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
524445fffbSMatthew Ahrens  *
534445fffbSMatthew Ahrens  * zfs_ioc_t ioc
544445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
554445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
564445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
574445fffbSMatthew Ahrens  *
584445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
594445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
604445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
614445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
624445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
634445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
644445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
654445fffbSMatthew Ahrens  *
664445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
674445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
684445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
694445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
704445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
714445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
724445fffbSMatthew Ahrens  *   doesn't have invalid characters).
734445fffbSMatthew Ahrens  *
744445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
754445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
764445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
774445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
784445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
794445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
804445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
814445fffbSMatthew Ahrens  *
824445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
834445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
844445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
854445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
864445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
874445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
884445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
894445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
904445fffbSMatthew Ahrens  *
914445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
924445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
934445fffbSMatthew Ahrens  *
944445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
954445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
964445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
974445fffbSMatthew Ahrens  *   will be called with the following arguments:
984445fffbSMatthew Ahrens  *
994445fffbSMatthew Ahrens  *   const char *name
1004445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1014445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1024445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1034445fffbSMatthew Ahrens  *
1044445fffbSMatthew Ahrens  *   nvlist_t *innvl
1054445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1064445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1074445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1084445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1094445fffbSMatthew Ahrens  *
1104445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1114445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1124445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1134445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1144445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1154445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1164445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1174445fffbSMatthew Ahrens  *
1184445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1194445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1204445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1214445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1224445fffbSMatthew Ahrens  *         the ioctl again.
1234445fffbSMatthew Ahrens  *
1244445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1254445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1264445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1274445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1284445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1294445fffbSMatthew Ahrens  *         deserialization failing.
130e9103aaeSGarrett D'Amore  */
131fa9e4066Sahrens 
132fa9e4066Sahrens #include <sys/types.h>
133fa9e4066Sahrens #include <sys/param.h>
134fa9e4066Sahrens #include <sys/errno.h>
135fa9e4066Sahrens #include <sys/uio.h>
136fa9e4066Sahrens #include <sys/buf.h>
137fa9e4066Sahrens #include <sys/modctl.h>
138fa9e4066Sahrens #include <sys/open.h>
139fa9e4066Sahrens #include <sys/file.h>
140fa9e4066Sahrens #include <sys/kmem.h>
141fa9e4066Sahrens #include <sys/conf.h>
142fa9e4066Sahrens #include <sys/cmn_err.h>
143fa9e4066Sahrens #include <sys/stat.h>
144fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1454201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
146da6c28aaSamw #include <sys/zfs_znode.h>
147fa9e4066Sahrens #include <sys/zap.h>
148fa9e4066Sahrens #include <sys/spa.h>
149b1b8ab34Slling #include <sys/spa_impl.h>
150fa9e4066Sahrens #include <sys/vdev.h>
1514201a95eSRic Aleshire #include <sys/priv_impl.h>
152fa9e4066Sahrens #include <sys/dmu.h>
153fa9e4066Sahrens #include <sys/dsl_dir.h>
154fa9e4066Sahrens #include <sys/dsl_dataset.h>
155fa9e4066Sahrens #include <sys/dsl_prop.h>
156ecd6cf80Smarks #include <sys/dsl_deleg.h>
157ecd6cf80Smarks #include <sys/dmu_objset.h>
1584e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1593b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
160fa9e4066Sahrens #include <sys/ddi.h>
161fa9e4066Sahrens #include <sys/sunddi.h>
162fa9e4066Sahrens #include <sys/sunldi.h>
163fa9e4066Sahrens #include <sys/policy.h>
164fa9e4066Sahrens #include <sys/zone.h>
165fa9e4066Sahrens #include <sys/nvpair.h>
166fa9e4066Sahrens #include <sys/pathname.h>
167fa9e4066Sahrens #include <sys/mount.h>
168fa9e4066Sahrens #include <sys/sdt.h>
169fa9e4066Sahrens #include <sys/fs/zfs.h>
170fa9e4066Sahrens #include <sys/zfs_ctldir.h>
171da6c28aaSamw #include <sys/zfs_dir.h>
172c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
173a2eea2e1Sahrens #include <sys/zvol.h>
1743f9d6ad7SLin Ling #include <sys/dsl_scan.h>
175ecd6cf80Smarks #include <sharefs/share.h>
176f18faf3fSek #include <sys/dmu_objset.h>
1773b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1783b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1793b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
180a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
181fa9e4066Sahrens 
182fa9e4066Sahrens #include "zfs_namecheck.h"
183e9dbad6fSeschrock #include "zfs_prop.h"
184ecd6cf80Smarks #include "zfs_deleg.h"
1850a586ceaSMark Shellenbaum #include "zfs_comutil.h"
186fa9e4066Sahrens 
187fa9e4066Sahrens extern struct modlfs zfs_modlfs;
188fa9e4066Sahrens 
189fa9e4066Sahrens extern void zfs_init(void);
190fa9e4066Sahrens extern void zfs_fini(void);
191fa9e4066Sahrens 
192fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
193fa9e4066Sahrens dev_info_t *zfs_dip;
194fa9e4066Sahrens 
1954445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
1964445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
1974445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
1984445fffbSMatthew Ahrens 
1994445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2004445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2014445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
202fa9e4066Sahrens 
20354d692b7SGeorge Wilson typedef enum {
20454d692b7SGeorge Wilson 	NO_NAME,
20554d692b7SGeorge Wilson 	POOL_NAME,
20654d692b7SGeorge Wilson 	DATASET_NAME
20754d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
20854d692b7SGeorge Wilson 
209f9af39baSGeorge Wilson typedef enum {
210f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
211f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2124445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
213f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
214f9af39baSGeorge Wilson 
215fa9e4066Sahrens typedef struct zfs_ioc_vec {
2164445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
217fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
218fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
21954d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2204445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
221f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2224445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2234445fffbSMatthew Ahrens 	const char		*zvec_name;
224fa9e4066Sahrens } zfs_ioc_vec_t;
225fa9e4066Sahrens 
22614843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
22714843421SMatthew Ahrens static const char *userquota_perms[] = {
22814843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
22914843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23014843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23114843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23214843421SMatthew Ahrens };
23314843421SMatthew Ahrens 
23414843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
23592241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
23692241e0bSTom Erickson     cred_t *cr);
23792241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
23892241e0bSTom Erickson     nvlist_t **errors);
2390a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2400a48a24eStimh     boolean_t *);
2414445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2424445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2430a48a24eStimh 
2443b2aab18SMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature);
245a6f561b4SSašo Kiselkov 
246fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
247fa9e4066Sahrens void
248fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
249fa9e4066Sahrens {
250fa9e4066Sahrens 	const char *newfile;
2513f9d6ad7SLin Ling 	char buf[512];
252fa9e4066Sahrens 	va_list adx;
253fa9e4066Sahrens 
254fa9e4066Sahrens 	/*
255fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
256fa9e4066Sahrens 	 */
257fa9e4066Sahrens 	newfile = strrchr(file, '/');
258fa9e4066Sahrens 	if (newfile != NULL) {
259fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
260fa9e4066Sahrens 	} else {
261fa9e4066Sahrens 		newfile = file;
262fa9e4066Sahrens 	}
263fa9e4066Sahrens 
264fa9e4066Sahrens 	va_start(adx, fmt);
265fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
266fa9e4066Sahrens 	va_end(adx);
267fa9e4066Sahrens 
268fa9e4066Sahrens 	/*
269fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
270fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
271fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
272fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
273fa9e4066Sahrens 	 * arg0 = file name
274fa9e4066Sahrens 	 * arg1 = function name
275fa9e4066Sahrens 	 * arg2 = line number
276fa9e4066Sahrens 	 * arg3 = message
277fa9e4066Sahrens 	 */
278fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
279fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
280fa9e4066Sahrens }
281fa9e4066Sahrens 
282ecd6cf80Smarks static void
283228975ccSek history_str_free(char *buf)
284228975ccSek {
285228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
286228975ccSek }
287228975ccSek 
288228975ccSek static char *
289228975ccSek history_str_get(zfs_cmd_t *zc)
290ecd6cf80Smarks {
29140feaa91Sahrens 	char *buf;
292ecd6cf80Smarks 
293ecd6cf80Smarks 	if (zc->zc_history == NULL)
294228975ccSek 		return (NULL);
295e7437265Sahrens 
296ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
297ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
298ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
299228975ccSek 		history_str_free(buf);
300228975ccSek 		return (NULL);
301ecd6cf80Smarks 	}
302ecd6cf80Smarks 
303ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
304ecd6cf80Smarks 
305228975ccSek 	return (buf);
306228975ccSek }
307ecd6cf80Smarks 
30815e6edf1Sgw /*
30915e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
31015e6edf1Sgw  */
31115e6edf1Sgw static boolean_t
31215e6edf1Sgw zfs_is_bootfs(const char *name)
31315e6edf1Sgw {
314503ad85cSMatthew Ahrens 	objset_t *os;
31515e6edf1Sgw 
316503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
317503ad85cSMatthew Ahrens 		boolean_t ret;
318b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
319503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
320503ad85cSMatthew Ahrens 		return (ret);
32115e6edf1Sgw 	}
322503ad85cSMatthew Ahrens 	return (B_FALSE);
32315e6edf1Sgw }
32415e6edf1Sgw 
325c2a93d44Stimh /*
3260a48a24eStimh  * zfs_earlier_version
327c2a93d44Stimh  *
328c2a93d44Stimh  *	Return non-zero if the spa version is less than requested version.
329c2a93d44Stimh  */
330da6c28aaSamw static int
3310a48a24eStimh zfs_earlier_version(const char *name, int version)
332da6c28aaSamw {
333da6c28aaSamw 	spa_t *spa;
334da6c28aaSamw 
335da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
336da6c28aaSamw 		if (spa_version(spa) < version) {
337da6c28aaSamw 			spa_close(spa, FTAG);
338da6c28aaSamw 			return (1);
339da6c28aaSamw 		}
340da6c28aaSamw 		spa_close(spa, FTAG);
341da6c28aaSamw 	}
342da6c28aaSamw 	return (0);
343da6c28aaSamw }
344da6c28aaSamw 
3459e6eda55Smarks /*
346745cd3c5Smaybee  * zpl_earlier_version
3479e6eda55Smarks  *
348745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3499e6eda55Smarks  */
350745cd3c5Smaybee static boolean_t
351745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3529e6eda55Smarks {
3539e6eda55Smarks 	objset_t *os;
354745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3559e6eda55Smarks 
356503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
357745cd3c5Smaybee 		uint64_t zplversion;
3589e6eda55Smarks 
359503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
360503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
361503ad85cSMatthew Ahrens 			return (B_TRUE);
362503ad85cSMatthew Ahrens 		}
363503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
364745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
365745cd3c5Smaybee 			rc = zplversion < version;
366503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3679e6eda55Smarks 	}
3689e6eda55Smarks 	return (rc);
3699e6eda55Smarks }
3709e6eda55Smarks 
371228975ccSek static void
372228975ccSek zfs_log_history(zfs_cmd_t *zc)
373228975ccSek {
374228975ccSek 	spa_t *spa;
375228975ccSek 	char *buf;
376ecd6cf80Smarks 
377228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
378228975ccSek 		return;
379228975ccSek 
380228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
381228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3824445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
383228975ccSek 		spa_close(spa, FTAG);
384228975ccSek 	}
385228975ccSek 	history_str_free(buf);
386ecd6cf80Smarks }
387ecd6cf80Smarks 
388fa9e4066Sahrens /*
389fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
390fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
391fa9e4066Sahrens  */
392fa9e4066Sahrens /* ARGSUSED */
393fa9e4066Sahrens static int
3944445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
395fa9e4066Sahrens {
396fa9e4066Sahrens 	return (0);
397fa9e4066Sahrens }
398fa9e4066Sahrens 
399fa9e4066Sahrens /*
400fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
401fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
402fa9e4066Sahrens  */
403fa9e4066Sahrens /* ARGSUSED */
404fa9e4066Sahrens static int
4054445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
406fa9e4066Sahrens {
407fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
408ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
409fa9e4066Sahrens 		return (0);
410fa9e4066Sahrens 
411*be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
412fa9e4066Sahrens }
413fa9e4066Sahrens 
414fa9e4066Sahrens static int
415a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
416fa9e4066Sahrens {
417fa9e4066Sahrens 	int writable = 1;
418fa9e4066Sahrens 
419fa9e4066Sahrens 	/*
420fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
421fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
422fa9e4066Sahrens 	 */
423fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
424fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
425*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
426fa9e4066Sahrens 
427fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
428fa9e4066Sahrens 		/*
429fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
430fa9e4066Sahrens 		 * global zone.
431fa9e4066Sahrens 		 */
432fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
433*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
434fa9e4066Sahrens 	} else {
435fa9e4066Sahrens 		/*
436fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
437fa9e4066Sahrens 		 */
438fa9e4066Sahrens 		if (!zoned)
439*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
440fa9e4066Sahrens 
441fa9e4066Sahrens 		/* must be writable by this zone */
442fa9e4066Sahrens 		if (!writable)
443*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
444fa9e4066Sahrens 	}
445fa9e4066Sahrens 	return (0);
446fa9e4066Sahrens }
447fa9e4066Sahrens 
448a7f53a56SChris Kirby static int
449a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
450a7f53a56SChris Kirby {
451a7f53a56SChris Kirby 	uint64_t zoned;
452a7f53a56SChris Kirby 
453a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
454*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
455a7f53a56SChris Kirby 
456a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
457a7f53a56SChris Kirby }
458a7f53a56SChris Kirby 
459a7f53a56SChris Kirby static int
460a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
461a7f53a56SChris Kirby {
462a7f53a56SChris Kirby 	uint64_t zoned;
463a7f53a56SChris Kirby 
4643b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
465*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
466a7f53a56SChris Kirby 
467a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
468a7f53a56SChris Kirby }
469a7f53a56SChris Kirby 
4704445fffbSMatthew Ahrens static int
4713b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4723b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
473fa9e4066Sahrens {
474fa9e4066Sahrens 	int error;
475fa9e4066Sahrens 
47619b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
477ecd6cf80Smarks 	if (error == 0) {
478ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4793b2aab18SMatthew Ahrens 		if (error != 0)
4804445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
481ecd6cf80Smarks 	}
482ecd6cf80Smarks 	return (error);
483ecd6cf80Smarks }
484ecd6cf80Smarks 
4854445fffbSMatthew Ahrens static int
4863b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
487a7f53a56SChris Kirby {
488a7f53a56SChris Kirby 	int error;
4893b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4903b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
491a7f53a56SChris Kirby 
4923b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4933b2aab18SMatthew Ahrens 	if (error != 0)
4943b2aab18SMatthew Ahrens 		return (error);
4953b2aab18SMatthew Ahrens 
4963b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
4973b2aab18SMatthew Ahrens 	if (error != 0) {
4983b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
4993b2aab18SMatthew Ahrens 		return (error);
500a7f53a56SChris Kirby 	}
5013b2aab18SMatthew Ahrens 
5023b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5033b2aab18SMatthew Ahrens 
5043b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5053b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
506a7f53a56SChris Kirby 	return (error);
507a7f53a56SChris Kirby }
508a7f53a56SChris Kirby 
5094201a95eSRic Aleshire /*
5104201a95eSRic Aleshire  * Policy for setting the security label property.
5114201a95eSRic Aleshire  *
5124201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5134201a95eSRic Aleshire  */
5144201a95eSRic Aleshire static int
51592241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5164201a95eSRic Aleshire {
5174201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5184201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5194201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5204201a95eSRic Aleshire 	uint64_t	zoned;
5214201a95eSRic Aleshire 	int		needed_priv = -1;
5224201a95eSRic Aleshire 	int		error;
5234201a95eSRic Aleshire 
5244201a95eSRic Aleshire 	/* First get the existing dataset label. */
5254201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5264201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5273b2aab18SMatthew Ahrens 	if (error != 0)
528*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5294201a95eSRic Aleshire 
5304201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5314201a95eSRic Aleshire 		new_default = TRUE;
5324201a95eSRic Aleshire 
5334201a95eSRic Aleshire 	/* The label must be translatable */
5344201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
535*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5364201a95eSRic Aleshire 
5374201a95eSRic Aleshire 	/*
5384201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5394201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5404201a95eSRic Aleshire 	 * are needed.
5414201a95eSRic Aleshire 	 */
5424201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5434201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
544*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5454201a95eSRic Aleshire 		return (0);
5464201a95eSRic Aleshire 	}
5474201a95eSRic Aleshire 
5484201a95eSRic Aleshire 	/*
5494201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5504201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5514201a95eSRic Aleshire 	 * global zone.
5524201a95eSRic Aleshire 	 */
5534201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5544201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
555*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5564201a95eSRic Aleshire 	if (!zoned) {
5574201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
558*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5594201a95eSRic Aleshire 	}
5604201a95eSRic Aleshire 
5614201a95eSRic Aleshire 	/*
5624201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5634201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5644201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5654201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5664201a95eSRic Aleshire 	 */
5674201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
56892241e0bSTom Erickson 		objset_t *os;
56992241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
57092241e0bSTom Erickson 
5714201a95eSRic Aleshire 		/*
5724201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5734201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5744201a95eSRic Aleshire 		 */
5754201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57692241e0bSTom Erickson 		    setsl_tag, &os);
5773b2aab18SMatthew Ahrens 		if (error != 0)
578*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5794201a95eSRic Aleshire 
58092241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
58192241e0bSTom Erickson 
5824201a95eSRic Aleshire 		if (new_default) {
5834201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5844201a95eSRic Aleshire 			goto out_check;
5854201a95eSRic Aleshire 		}
5864201a95eSRic Aleshire 
5874201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
588*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5894201a95eSRic Aleshire 
5904201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5914201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5924201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5934201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5944201a95eSRic Aleshire 	} else {
5954201a95eSRic Aleshire 		/* dataset currently has a default label */
5964201a95eSRic Aleshire 		if (!new_default)
5974201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5984201a95eSRic Aleshire 	}
5994201a95eSRic Aleshire 
6004201a95eSRic Aleshire out_check:
6014201a95eSRic Aleshire 	if (needed_priv != -1)
6024201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6034201a95eSRic Aleshire 	return (0);
6044201a95eSRic Aleshire }
6054201a95eSRic Aleshire 
606ecd6cf80Smarks static int
60792241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
60892241e0bSTom Erickson     cred_t *cr)
609ecd6cf80Smarks {
61092241e0bSTom Erickson 	char *strval;
61192241e0bSTom Erickson 
612ecd6cf80Smarks 	/*
613ecd6cf80Smarks 	 * Check permissions for special properties.
614ecd6cf80Smarks 	 */
615ecd6cf80Smarks 	switch (prop) {
616ecd6cf80Smarks 	case ZFS_PROP_ZONED:
617ecd6cf80Smarks 		/*
618ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
619ecd6cf80Smarks 		 */
620ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
621*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
622ecd6cf80Smarks 		break;
623ecd6cf80Smarks 
624ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
625ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
626ecd6cf80Smarks 			uint64_t zoned;
627ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
628ecd6cf80Smarks 			/*
629ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
630ecd6cf80Smarks 			 * quota on things *under* (ie. contained by)
631ecd6cf80Smarks 			 * the thing they own.
632ecd6cf80Smarks 			 */
63392241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
634ecd6cf80Smarks 			    setpoint))
635*be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
63692241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
637*be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
638ecd6cf80Smarks 		}
639db870a07Sahrens 		break;
6404201a95eSRic Aleshire 
6414201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6424201a95eSRic Aleshire 		if (!is_system_labeled())
643*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64492241e0bSTom Erickson 
64592241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
64692241e0bSTom Erickson 			int err;
64792241e0bSTom Erickson 
64892241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
64992241e0bSTom Erickson 			if (err != 0)
65092241e0bSTom Erickson 				return (err);
65192241e0bSTom Erickson 		}
6524201a95eSRic Aleshire 		break;
653ecd6cf80Smarks 	}
654ecd6cf80Smarks 
65592241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
656ecd6cf80Smarks }
657ecd6cf80Smarks 
6584445fffbSMatthew Ahrens /* ARGSUSED */
6594445fffbSMatthew Ahrens static int
6604445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
661ecd6cf80Smarks {
662ecd6cf80Smarks 	int error;
663ecd6cf80Smarks 
664ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6653b2aab18SMatthew Ahrens 	if (error != 0)
666fa9e4066Sahrens 		return (error);
667fa9e4066Sahrens 
668ecd6cf80Smarks 	/*
669ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
670ecd6cf80Smarks 	 * dsl_deleg_can_allow()
671ecd6cf80Smarks 	 */
672ecd6cf80Smarks 	return (0);
673ecd6cf80Smarks }
674ecd6cf80Smarks 
6754445fffbSMatthew Ahrens /* ARGSUSED */
6764445fffbSMatthew Ahrens static int
6774445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
678ecd6cf80Smarks {
679681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
680681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
681ecd6cf80Smarks }
682ecd6cf80Smarks 
6834445fffbSMatthew Ahrens /* ARGSUSED */
6844445fffbSMatthew Ahrens static int
6854445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
686ecd6cf80Smarks {
687a7f53a56SChris Kirby 	dsl_pool_t *dp;
688a7f53a56SChris Kirby 	dsl_dataset_t *ds;
689a7f53a56SChris Kirby 	char *cp;
690a7f53a56SChris Kirby 	int error;
691a7f53a56SChris Kirby 
692a7f53a56SChris Kirby 	/*
693a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
694a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
695a7f53a56SChris Kirby 	 */
696a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
697a7f53a56SChris Kirby 	if (cp == NULL)
698*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
6993b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7003b2aab18SMatthew Ahrens 	if (error != 0)
701a7f53a56SChris Kirby 		return (error);
702a7f53a56SChris Kirby 
703a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7043b2aab18SMatthew Ahrens 	if (error != 0) {
7053b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
706a7f53a56SChris Kirby 		return (error);
7073b2aab18SMatthew Ahrens 	}
708a7f53a56SChris Kirby 
709a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
710a7f53a56SChris Kirby 
711a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
712a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
713a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7143b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
715a7f53a56SChris Kirby 
716a7f53a56SChris Kirby 	return (error);
717ecd6cf80Smarks }
718ecd6cf80Smarks 
7194445fffbSMatthew Ahrens /* ARGSUSED */
720743a77edSAlan Wright static int
7214445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7224445fffbSMatthew Ahrens {
7234445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7244445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7254445fffbSMatthew Ahrens }
7264445fffbSMatthew Ahrens 
7274445fffbSMatthew Ahrens /* ARGSUSED */
7284445fffbSMatthew Ahrens static int
7294445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
730743a77edSAlan Wright {
731743a77edSAlan Wright 	vnode_t *vp;
732743a77edSAlan Wright 	int error;
733743a77edSAlan Wright 
734743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
735743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
736743a77edSAlan Wright 		return (error);
737743a77edSAlan Wright 
738743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
739743a77edSAlan Wright 
740743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
741743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
742743a77edSAlan Wright 	    zc->zc_name) != 0)) {
743743a77edSAlan Wright 		VN_RELE(vp);
744*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
745743a77edSAlan Wright 	}
746743a77edSAlan Wright 
747743a77edSAlan Wright 	VN_RELE(vp);
748743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
749743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
750743a77edSAlan Wright }
751743a77edSAlan Wright 
752ecd6cf80Smarks int
7534445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
754ecd6cf80Smarks {
755ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
756*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
757ecd6cf80Smarks 
7583cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
759ecd6cf80Smarks 		return (0);
760ecd6cf80Smarks 	} else {
7614445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
762743a77edSAlan Wright 	}
763743a77edSAlan Wright }
764ecd6cf80Smarks 
765743a77edSAlan Wright int
7664445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
767743a77edSAlan Wright {
768743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
769*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
770ecd6cf80Smarks 
771743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
772743a77edSAlan Wright 		return (0);
773743a77edSAlan Wright 	} else {
7744445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
775ecd6cf80Smarks 	}
776fa9e4066Sahrens }
777fa9e4066Sahrens 
778fa9e4066Sahrens static int
779ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
780fa9e4066Sahrens {
781fa9e4066Sahrens 	char *cp;
782fa9e4066Sahrens 
783fa9e4066Sahrens 	/*
784fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
785fa9e4066Sahrens 	 */
786ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
787ecd6cf80Smarks 	cp = strrchr(parent, '@');
788fa9e4066Sahrens 	if (cp != NULL) {
789fa9e4066Sahrens 		cp[0] = '\0';
790fa9e4066Sahrens 	} else {
791ecd6cf80Smarks 		cp = strrchr(parent, '/');
792fa9e4066Sahrens 		if (cp == NULL)
793*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
794fa9e4066Sahrens 		cp[0] = '\0';
795ecd6cf80Smarks 	}
796ecd6cf80Smarks 
797ecd6cf80Smarks 	return (0);
798ecd6cf80Smarks }
799ecd6cf80Smarks 
800ecd6cf80Smarks int
801ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
802ecd6cf80Smarks {
803ecd6cf80Smarks 	int error;
804ecd6cf80Smarks 
805ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
806ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
807ecd6cf80Smarks 		return (error);
808ecd6cf80Smarks 
809ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
810ecd6cf80Smarks }
811ecd6cf80Smarks 
8124445fffbSMatthew Ahrens /* ARGSUSED */
813ecd6cf80Smarks static int
8144445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
815ecd6cf80Smarks {
816ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
817ecd6cf80Smarks }
818ecd6cf80Smarks 
819cbf6f6aaSWilliam Gorrell /*
820cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8214445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
822cbf6f6aaSWilliam Gorrell  */
8234445fffbSMatthew Ahrens /* ARGSUSED */
824cbf6f6aaSWilliam Gorrell static int
8254445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
826cbf6f6aaSWilliam Gorrell {
8274445fffbSMatthew Ahrens 	nvlist_t *snaps;
8284445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8294445fffbSMatthew Ahrens 	int error = 0;
830cbf6f6aaSWilliam Gorrell 
8314445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
832*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8334445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8344445fffbSMatthew Ahrens 	    pair = nextpair) {
8353b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
8364445fffbSMatthew Ahrens 		dsl_dataset_t *ds;
837cbf6f6aaSWilliam Gorrell 
8383b2aab18SMatthew Ahrens 		error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp);
8393b2aab18SMatthew Ahrens 		if (error != 0)
8403b2aab18SMatthew Ahrens 			break;
8414445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
8423b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds);
8433b2aab18SMatthew Ahrens 		if (error == 0)
8444445fffbSMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
8453b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
8463b2aab18SMatthew Ahrens 
8473b2aab18SMatthew Ahrens 		if (error == 0) {
8483b2aab18SMatthew Ahrens 			error = zfs_secpolicy_destroy_perms(nvpair_name(pair),
8493b2aab18SMatthew Ahrens 			    cr);
8504445fffbSMatthew Ahrens 		} else if (error == ENOENT) {
8514445fffbSMatthew Ahrens 			/*
8524445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8534445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8544445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8554445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8564445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8574445fffbSMatthew Ahrens 			 * checked for permission).
8584445fffbSMatthew Ahrens 			 */
8594445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8604445fffbSMatthew Ahrens 			error = 0;
8614445fffbSMatthew Ahrens 		}
8624445fffbSMatthew Ahrens 		if (error != 0)
8634445fffbSMatthew Ahrens 			break;
8644445fffbSMatthew Ahrens 	}
865cbf6f6aaSWilliam Gorrell 
866cbf6f6aaSWilliam Gorrell 	return (error);
867cbf6f6aaSWilliam Gorrell }
868cbf6f6aaSWilliam Gorrell 
869ecd6cf80Smarks int
870ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
871ecd6cf80Smarks {
87292241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
873ecd6cf80Smarks 	int	error;
874ecd6cf80Smarks 
875ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
876ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
877ecd6cf80Smarks 		return (error);
878ecd6cf80Smarks 
879ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
880ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
881ecd6cf80Smarks 		return (error);
882ecd6cf80Smarks 
883ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
884ecd6cf80Smarks 	    sizeof (parentname))) != 0)
885ecd6cf80Smarks 		return (error);
886ecd6cf80Smarks 
887ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
888ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
889ecd6cf80Smarks 		return (error);
890ecd6cf80Smarks 
891ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
892ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
893ecd6cf80Smarks 		return (error);
894ecd6cf80Smarks 
895ecd6cf80Smarks 	return (error);
896ecd6cf80Smarks }
897ecd6cf80Smarks 
8984445fffbSMatthew Ahrens /* ARGSUSED */
899ecd6cf80Smarks static int
9004445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
901ecd6cf80Smarks {
902ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
903ecd6cf80Smarks }
904ecd6cf80Smarks 
9054445fffbSMatthew Ahrens /* ARGSUSED */
906ecd6cf80Smarks static int
9074445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
908ecd6cf80Smarks {
9093b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9103b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
911ecd6cf80Smarks 	int error;
912ecd6cf80Smarks 
913ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
914ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9153b2aab18SMatthew Ahrens 	if (error != 0)
9163b2aab18SMatthew Ahrens 		return (error);
9173b2aab18SMatthew Ahrens 
9183b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9193b2aab18SMatthew Ahrens 	if (error != 0)
920ecd6cf80Smarks 		return (error);
921ecd6cf80Smarks 
9223b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
923ecd6cf80Smarks 
924ecd6cf80Smarks 	if (error == 0) {
9253b2aab18SMatthew Ahrens 		char parentname[MAXNAMELEN];
9263b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
927ecd6cf80Smarks 		dsl_dir_t *dd;
9283b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
929ecd6cf80Smarks 
930745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
9313b2aab18SMatthew Ahrens 		    dd->dd_phys->dd_origin_obj, FTAG, &origin);
9323b2aab18SMatthew Ahrens 		if (error != 0) {
9333b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9343b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
935ecd6cf80Smarks 			return (error);
936ecd6cf80Smarks 		}
937ecd6cf80Smarks 
9383b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
939ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
940ecd6cf80Smarks 
9413b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9423b2aab18SMatthew Ahrens 		if (error == 0) {
9433b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
944ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9453b2aab18SMatthew Ahrens 		}
9463b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9473b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
948ecd6cf80Smarks 	}
9493b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
950ecd6cf80Smarks 	return (error);
951ecd6cf80Smarks }
952ecd6cf80Smarks 
9534445fffbSMatthew Ahrens /* ARGSUSED */
954ecd6cf80Smarks static int
9554445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
956ecd6cf80Smarks {
957ecd6cf80Smarks 	int error;
958ecd6cf80Smarks 
959ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
960ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
961ecd6cf80Smarks 		return (error);
962ecd6cf80Smarks 
963ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
964ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
965ecd6cf80Smarks 		return (error);
966ecd6cf80Smarks 
967ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
968ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
969ecd6cf80Smarks }
970ecd6cf80Smarks 
971ecd6cf80Smarks int
972ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
973ecd6cf80Smarks {
974681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
975681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
976ecd6cf80Smarks }
977ecd6cf80Smarks 
9784445fffbSMatthew Ahrens /*
9794445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9804445fffbSMatthew Ahrens  */
9814445fffbSMatthew Ahrens /* ARGSUSED */
982ecd6cf80Smarks static int
9834445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
984ecd6cf80Smarks {
9854445fffbSMatthew Ahrens 	nvlist_t *snaps;
986d5285caeSGeorge Wilson 	int error = 0;
9874445fffbSMatthew Ahrens 	nvpair_t *pair;
988ecd6cf80Smarks 
9894445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
990*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9914445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9924445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9934445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9944445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9954445fffbSMatthew Ahrens 
9964445fffbSMatthew Ahrens 		if (atp == NULL) {
997*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9984445fffbSMatthew Ahrens 			break;
9994445fffbSMatthew Ahrens 		}
10004445fffbSMatthew Ahrens 		*atp = '\0';
10014445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
10024445fffbSMatthew Ahrens 		*atp = '@';
10034445fffbSMatthew Ahrens 		if (error != 0)
10044445fffbSMatthew Ahrens 			break;
10054445fffbSMatthew Ahrens 	}
10064445fffbSMatthew Ahrens 	return (error);
1007ecd6cf80Smarks }
1008ecd6cf80Smarks 
10094445fffbSMatthew Ahrens /* ARGSUSED */
1010ecd6cf80Smarks static int
10114445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10124445fffbSMatthew Ahrens {
10134445fffbSMatthew Ahrens 	/*
10144445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10154445fffbSMatthew Ahrens 	 * to log to.
10164445fffbSMatthew Ahrens 	 */
10174445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1018*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10194445fffbSMatthew Ahrens 	return (0);
10204445fffbSMatthew Ahrens }
10214445fffbSMatthew Ahrens 
10224445fffbSMatthew Ahrens static int
10234445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1024ecd6cf80Smarks {
102592241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
102692241e0bSTom Erickson 	int	error;
10274445fffbSMatthew Ahrens 	char	*origin;
1028ecd6cf80Smarks 
1029ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1030ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1031ecd6cf80Smarks 		return (error);
1032fa9e4066Sahrens 
10334445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10344445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
10354445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
10364445fffbSMatthew Ahrens 		return (error);
1037fa9e4066Sahrens 
1038ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1039ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1040ecd6cf80Smarks 		return (error);
1041ecd6cf80Smarks 
10424445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
10434445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1044fa9e4066Sahrens }
1045fa9e4066Sahrens 
1046fa9e4066Sahrens /*
1047fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1048fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1049fa9e4066Sahrens  */
1050fa9e4066Sahrens /* ARGSUSED */
1051fa9e4066Sahrens static int
10524445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1053fa9e4066Sahrens {
1054fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1055*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1056fa9e4066Sahrens 
1057fa9e4066Sahrens 	return (0);
1058fa9e4066Sahrens }
1059fa9e4066Sahrens 
106099d5e173STim Haley /*
106199d5e173STim Haley  * Policy for object to name lookups.
106299d5e173STim Haley  */
106399d5e173STim Haley /* ARGSUSED */
106499d5e173STim Haley static int
10654445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
106699d5e173STim Haley {
106799d5e173STim Haley 	int error;
106899d5e173STim Haley 
106999d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
107099d5e173STim Haley 		return (0);
107199d5e173STim Haley 
107299d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
107399d5e173STim Haley 	return (error);
107499d5e173STim Haley }
107599d5e173STim Haley 
1076ea8dc4b6Seschrock /*
1077ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1078ea8dc4b6Seschrock  */
1079ea8dc4b6Seschrock /* ARGSUSED */
1080ea8dc4b6Seschrock static int
10814445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1082ea8dc4b6Seschrock {
1083ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1084ea8dc4b6Seschrock }
1085ea8dc4b6Seschrock 
10864445fffbSMatthew Ahrens /* ARGSUSED */
1087e45ce728Sahrens static int
10884445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1089e45ce728Sahrens {
1090e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1091e45ce728Sahrens 
1092990b4856Slling 	if (prop == ZPROP_INVAL) {
1093e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1094*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1095e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1096e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1097e45ce728Sahrens 	} else {
109892241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
109992241e0bSTom Erickson 		    NULL, cr));
1100e45ce728Sahrens 	}
1101e45ce728Sahrens }
1102e45ce728Sahrens 
110314843421SMatthew Ahrens static int
11044445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
110514843421SMatthew Ahrens {
11064445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
110714843421SMatthew Ahrens 	if (err)
110814843421SMatthew Ahrens 		return (err);
110914843421SMatthew Ahrens 
111014843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1111*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
111214843421SMatthew Ahrens 
111314843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
111414843421SMatthew Ahrens 		/*
111514843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
111614843421SMatthew Ahrens 		 * themself, allow it.
111714843421SMatthew Ahrens 		 */
111814843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
111914843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
112014843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
112114843421SMatthew Ahrens 				return (0);
112214843421SMatthew Ahrens 		} else {
112314843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
112414843421SMatthew Ahrens 				return (0);
112514843421SMatthew Ahrens 		}
112614843421SMatthew Ahrens 	}
112714843421SMatthew Ahrens 
112814843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
112914843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
113014843421SMatthew Ahrens }
113114843421SMatthew Ahrens 
113214843421SMatthew Ahrens static int
11334445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
113414843421SMatthew Ahrens {
11354445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
113614843421SMatthew Ahrens 	if (err)
113714843421SMatthew Ahrens 		return (err);
113814843421SMatthew Ahrens 
113914843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1140*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
114114843421SMatthew Ahrens 
114214843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
114314843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
114414843421SMatthew Ahrens }
114514843421SMatthew Ahrens 
11464445fffbSMatthew Ahrens /* ARGSUSED */
114714843421SMatthew Ahrens static int
11484445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
114914843421SMatthew Ahrens {
115092241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
115192241e0bSTom Erickson 	    NULL, cr));
115214843421SMatthew Ahrens }
115314843421SMatthew Ahrens 
11544445fffbSMatthew Ahrens /* ARGSUSED */
1155842727c2SChris Kirby static int
11564445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1157842727c2SChris Kirby {
11583b2aab18SMatthew Ahrens 	nvpair_t *pair;
11593b2aab18SMatthew Ahrens 	nvlist_t *holds;
11603b2aab18SMatthew Ahrens 	int error;
11613b2aab18SMatthew Ahrens 
11623b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
11633b2aab18SMatthew Ahrens 	if (error != 0)
1164*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
11653b2aab18SMatthew Ahrens 
11663b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
11673b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
11683b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
11693b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
11703b2aab18SMatthew Ahrens 		if (error != 0)
11713b2aab18SMatthew Ahrens 			return (error);
11723b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
11733b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
11743b2aab18SMatthew Ahrens 		if (error != 0)
11753b2aab18SMatthew Ahrens 			return (error);
11763b2aab18SMatthew Ahrens 	}
11773b2aab18SMatthew Ahrens 	return (0);
1178842727c2SChris Kirby }
1179842727c2SChris Kirby 
11804445fffbSMatthew Ahrens /* ARGSUSED */
1181842727c2SChris Kirby static int
11824445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1183842727c2SChris Kirby {
11843b2aab18SMatthew Ahrens 	nvpair_t *pair;
11853b2aab18SMatthew Ahrens 	int error;
11863b2aab18SMatthew Ahrens 
11873b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
11883b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
11893b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
11903b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
11913b2aab18SMatthew Ahrens 		if (error != 0)
11923b2aab18SMatthew Ahrens 			return (error);
11933b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
11943b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
11953b2aab18SMatthew Ahrens 		if (error != 0)
11963b2aab18SMatthew Ahrens 			return (error);
11973b2aab18SMatthew Ahrens 	}
11983b2aab18SMatthew Ahrens 	return (0);
1199842727c2SChris Kirby }
1200842727c2SChris Kirby 
120199d5e173STim Haley /*
120299d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
120399d5e173STim Haley  */
120499d5e173STim Haley static int
12054445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120699d5e173STim Haley {
120799d5e173STim Haley 	/*
120899d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
120999d5e173STim Haley 	 * hold, destroy and release all rolled into one.
121099d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
121199d5e173STim Haley 	 */
121299d5e173STim Haley 	int error;
121399d5e173STim Haley 
121499d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
121599d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
121699d5e173STim Haley 		return (0);
121799d5e173STim Haley 
12184445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12193b2aab18SMatthew Ahrens 	if (error == 0)
12204445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12213b2aab18SMatthew Ahrens 	if (error == 0)
12224445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12233b2aab18SMatthew Ahrens 	if (error == 0)
12244445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
122599d5e173STim Haley 	return (error);
122699d5e173STim Haley }
122799d5e173STim Haley 
1228fa9e4066Sahrens /*
1229fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1230fa9e4066Sahrens  */
1231fa9e4066Sahrens static int
1232478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1233fa9e4066Sahrens {
1234fa9e4066Sahrens 	char *packed;
1235fa9e4066Sahrens 	int error;
1236990b4856Slling 	nvlist_t *list = NULL;
1237fa9e4066Sahrens 
1238fa9e4066Sahrens 	/*
1239e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1240fa9e4066Sahrens 	 */
1241990b4856Slling 	if (size == 0)
1242*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1243fa9e4066Sahrens 
1244fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1245fa9e4066Sahrens 
1246478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1247478ed9adSEric Taylor 	    iflag)) != 0) {
1248fa9e4066Sahrens 		kmem_free(packed, size);
1249fa9e4066Sahrens 		return (error);
1250fa9e4066Sahrens 	}
1251fa9e4066Sahrens 
1252990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1253fa9e4066Sahrens 		kmem_free(packed, size);
1254fa9e4066Sahrens 		return (error);
1255fa9e4066Sahrens 	}
1256fa9e4066Sahrens 
1257fa9e4066Sahrens 	kmem_free(packed, size);
1258fa9e4066Sahrens 
1259990b4856Slling 	*nvp = list;
1260fa9e4066Sahrens 	return (0);
1261fa9e4066Sahrens }
1262fa9e4066Sahrens 
12634445fffbSMatthew Ahrens /*
12644445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
12654445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
12664445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
12674445fffbSMatthew Ahrens  * removed.
12684445fffbSMatthew Ahrens  */
126992241e0bSTom Erickson static int
12704445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
127192241e0bSTom Erickson {
127292241e0bSTom Erickson 	size_t size;
127392241e0bSTom Erickson 
12744445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
127592241e0bSTom Erickson 
12764445fffbSMatthew Ahrens 	if (size > max) {
127792241e0bSTom Erickson 		nvpair_t *more_errors;
127892241e0bSTom Erickson 		int n = 0;
127992241e0bSTom Erickson 
12804445fffbSMatthew Ahrens 		if (max < 1024)
1281*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
128292241e0bSTom Erickson 
12834445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
12844445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
128592241e0bSTom Erickson 
128692241e0bSTom Erickson 		do {
12874445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
128892241e0bSTom Erickson 			    more_errors);
12894445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
129092241e0bSTom Erickson 			n++;
12914445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
12924445fffbSMatthew Ahrens 		} while (size > max);
129392241e0bSTom Erickson 
12944445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
12954445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
12964445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
129792241e0bSTom Erickson 	}
129892241e0bSTom Erickson 
129992241e0bSTom Erickson 	return (0);
130092241e0bSTom Erickson }
130192241e0bSTom Erickson 
1302e9dbad6fSeschrock static int
1303e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1304e9dbad6fSeschrock {
1305e9dbad6fSeschrock 	char *packed = NULL;
13066e27f868SSam Falkner 	int error = 0;
1307e9dbad6fSeschrock 	size_t size;
1308e9dbad6fSeschrock 
13094445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1310e9dbad6fSeschrock 
1311e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1312*be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1313e9dbad6fSeschrock 	} else {
13144445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
13156e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
13166e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1317*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
13184445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1319e9dbad6fSeschrock 	}
1320e9dbad6fSeschrock 
1321e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
13224445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1323e9dbad6fSeschrock 	return (error);
1324e9dbad6fSeschrock }
1325e9dbad6fSeschrock 
132614843421SMatthew Ahrens static int
1327af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
132814843421SMatthew Ahrens {
132914843421SMatthew Ahrens 	objset_t *os;
133014843421SMatthew Ahrens 	int error;
133114843421SMatthew Ahrens 
1332503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
13333b2aab18SMatthew Ahrens 	if (error != 0)
133414843421SMatthew Ahrens 		return (error);
1335503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1336503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1337*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1338503ad85cSMatthew Ahrens 	}
133914843421SMatthew Ahrens 
1340503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1341af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1342af4c679fSSean McEnroe 	if (*zfvp) {
1343af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
134414843421SMatthew Ahrens 	} else {
1345*be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
134614843421SMatthew Ahrens 	}
1347503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1348503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
134914843421SMatthew Ahrens 	return (error);
135014843421SMatthew Ahrens }
135114843421SMatthew Ahrens 
135214843421SMatthew Ahrens /*
135314843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
135414843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1355ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1356ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
135714843421SMatthew Ahrens  */
135814843421SMatthew Ahrens static int
13591412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
136014843421SMatthew Ahrens {
136114843421SMatthew Ahrens 	int error = 0;
136214843421SMatthew Ahrens 
1363af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1364af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
136514843421SMatthew Ahrens 	if (error == 0) {
13661412a1a2SMark Shellenbaum 		rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
13671412a1a2SMark Shellenbaum 		    RW_READER, tag);
1368af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
136914843421SMatthew Ahrens 			/*
137014843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
137114843421SMatthew Ahrens 			 * thread should be just about to disassociate the
137214843421SMatthew Ahrens 			 * objset from the zfsvfs.
137314843421SMatthew Ahrens 			 */
1374af4c679fSSean McEnroe 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1375*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
137614843421SMatthew Ahrens 		}
137714843421SMatthew Ahrens 	}
137814843421SMatthew Ahrens 	return (error);
137914843421SMatthew Ahrens }
138014843421SMatthew Ahrens 
138114843421SMatthew Ahrens static void
138214843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
138314843421SMatthew Ahrens {
138414843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
138514843421SMatthew Ahrens 
138614843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
138714843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
138814843421SMatthew Ahrens 	} else {
1389503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
139014843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
139114843421SMatthew Ahrens 	}
139214843421SMatthew Ahrens }
139314843421SMatthew Ahrens 
1394fa9e4066Sahrens static int
1395fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1396fa9e4066Sahrens {
1397fa9e4066Sahrens 	int error;
1398990b4856Slling 	nvlist_t *config, *props = NULL;
13990a48a24eStimh 	nvlist_t *rootprops = NULL;
14000a48a24eStimh 	nvlist_t *zplprops = NULL;
1401fa9e4066Sahrens 
1402990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1403478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1404fa9e4066Sahrens 		return (error);
14052a6b87f0Sek 
1406990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1407478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1408478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1409990b4856Slling 		nvlist_free(config);
1410990b4856Slling 		return (error);
1411990b4856Slling 	}
1412990b4856Slling 
14130a48a24eStimh 	if (props) {
14140a48a24eStimh 		nvlist_t *nvl = NULL;
14150a48a24eStimh 		uint64_t version = SPA_VERSION;
14160a48a24eStimh 
14170a48a24eStimh 		(void) nvlist_lookup_uint64(props,
14180a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1419ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1420*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
14210a48a24eStimh 			goto pool_props_bad;
14220a48a24eStimh 		}
14230a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
14240a48a24eStimh 		if (nvl) {
14250a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
14260a48a24eStimh 			if (error != 0) {
14270a48a24eStimh 				nvlist_free(config);
14280a48a24eStimh 				nvlist_free(props);
14290a48a24eStimh 				return (error);
14300a48a24eStimh 			}
14310a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
14320a48a24eStimh 		}
14330a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14340a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
14350a48a24eStimh 		    zplprops, NULL);
14363b2aab18SMatthew Ahrens 		if (error != 0)
14370a48a24eStimh 			goto pool_props_bad;
14380a48a24eStimh 	}
14390a48a24eStimh 
14404445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
14410a48a24eStimh 
14420a48a24eStimh 	/*
14430a48a24eStimh 	 * Set the remaining root properties
14440a48a24eStimh 	 */
144592241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
144692241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
14470a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1448fa9e4066Sahrens 
14490a48a24eStimh pool_props_bad:
14500a48a24eStimh 	nvlist_free(rootprops);
14510a48a24eStimh 	nvlist_free(zplprops);
1452fa9e4066Sahrens 	nvlist_free(config);
14530a48a24eStimh 	nvlist_free(props);
1454990b4856Slling 
1455fa9e4066Sahrens 	return (error);
1456fa9e4066Sahrens }
1457fa9e4066Sahrens 
1458fa9e4066Sahrens static int
1459fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1460fa9e4066Sahrens {
1461ecd6cf80Smarks 	int error;
1462ecd6cf80Smarks 	zfs_log_history(zc);
1463ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1464681d9761SEric Taylor 	if (error == 0)
1465681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1466ecd6cf80Smarks 	return (error);
1467fa9e4066Sahrens }
1468fa9e4066Sahrens 
1469fa9e4066Sahrens static int
1470fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1471fa9e4066Sahrens {
1472990b4856Slling 	nvlist_t *config, *props = NULL;
1473fa9e4066Sahrens 	uint64_t guid;
1474468c413aSTim Haley 	int error;
1475fa9e4066Sahrens 
1476990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1477478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1478990b4856Slling 		return (error);
1479990b4856Slling 
1480990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1481478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1482478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1483990b4856Slling 		nvlist_free(config);
1484fa9e4066Sahrens 		return (error);
1485990b4856Slling 	}
1486fa9e4066Sahrens 
1487fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1488ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1489*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1490fa9e4066Sahrens 	else
14914b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1492fa9e4066Sahrens 
14934b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
14944b964adaSGeorge Wilson 		int err;
14954b964adaSGeorge Wilson 
14964b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
14974b964adaSGeorge Wilson 			error = err;
14984b964adaSGeorge Wilson 	}
1499468c413aSTim Haley 
1500fa9e4066Sahrens 	nvlist_free(config);
1501fa9e4066Sahrens 
1502990b4856Slling 	if (props)
1503990b4856Slling 		nvlist_free(props);
1504990b4856Slling 
1505fa9e4066Sahrens 	return (error);
1506fa9e4066Sahrens }
1507fa9e4066Sahrens 
1508fa9e4066Sahrens static int
1509fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1510fa9e4066Sahrens {
1511ecd6cf80Smarks 	int error;
151289a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1513394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
151489a89ebfSlling 
1515ecd6cf80Smarks 	zfs_log_history(zc);
1516394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1517681d9761SEric Taylor 	if (error == 0)
1518681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1519ecd6cf80Smarks 	return (error);
1520fa9e4066Sahrens }
1521fa9e4066Sahrens 
1522fa9e4066Sahrens static int
1523fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1524fa9e4066Sahrens {
1525fa9e4066Sahrens 	nvlist_t *configs;
1526fa9e4066Sahrens 	int error;
1527fa9e4066Sahrens 
1528fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1529*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1530fa9e4066Sahrens 
1531e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1532fa9e4066Sahrens 
1533fa9e4066Sahrens 	nvlist_free(configs);
1534fa9e4066Sahrens 
1535fa9e4066Sahrens 	return (error);
1536fa9e4066Sahrens }
1537fa9e4066Sahrens 
1538ad135b5dSChristopher Siden /*
1539ad135b5dSChristopher Siden  * inputs:
1540ad135b5dSChristopher Siden  * zc_name		name of the pool
1541ad135b5dSChristopher Siden  *
1542ad135b5dSChristopher Siden  * outputs:
1543ad135b5dSChristopher Siden  * zc_cookie		real errno
1544ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1545ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1546ad135b5dSChristopher Siden  */
1547fa9e4066Sahrens static int
1548fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1549fa9e4066Sahrens {
1550fa9e4066Sahrens 	nvlist_t *config;
1551fa9e4066Sahrens 	int error;
1552ea8dc4b6Seschrock 	int ret = 0;
1553fa9e4066Sahrens 
1554e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1555e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1556fa9e4066Sahrens 
1557fa9e4066Sahrens 	if (config != NULL) {
1558e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1559fa9e4066Sahrens 		nvlist_free(config);
1560ea8dc4b6Seschrock 
1561ea8dc4b6Seschrock 		/*
1562ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1563ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1564ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1565ea8dc4b6Seschrock 		 */
1566ea8dc4b6Seschrock 		zc->zc_cookie = error;
1567fa9e4066Sahrens 	} else {
1568ea8dc4b6Seschrock 		ret = error;
1569fa9e4066Sahrens 	}
1570fa9e4066Sahrens 
1571ea8dc4b6Seschrock 	return (ret);
1572fa9e4066Sahrens }
1573fa9e4066Sahrens 
1574fa9e4066Sahrens /*
1575fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1576fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1577fa9e4066Sahrens  */
1578fa9e4066Sahrens static int
1579fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1580fa9e4066Sahrens {
1581fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1582fa9e4066Sahrens 	int error;
1583fa9e4066Sahrens 
1584990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1585478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1586fa9e4066Sahrens 		return (error);
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 	nvlist_free(tryconfig);
1591fa9e4066Sahrens 
1592fa9e4066Sahrens 	if (config == NULL)
1593*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1594fa9e4066Sahrens 
1595e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1596fa9e4066Sahrens 	nvlist_free(config);
1597fa9e4066Sahrens 
1598fa9e4066Sahrens 	return (error);
1599fa9e4066Sahrens }
1600fa9e4066Sahrens 
16013f9d6ad7SLin Ling /*
16023f9d6ad7SLin Ling  * inputs:
16033f9d6ad7SLin Ling  * zc_name              name of the pool
16043f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16053f9d6ad7SLin Ling  */
1606fa9e4066Sahrens static int
16073f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1608fa9e4066Sahrens {
1609fa9e4066Sahrens 	spa_t *spa;
1610fa9e4066Sahrens 	int error;
1611fa9e4066Sahrens 
161206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
161306eeb2adSek 		return (error);
161406eeb2adSek 
16153f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
16163f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
16173f9d6ad7SLin Ling 	else
16183f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
161906eeb2adSek 
162006eeb2adSek 	spa_close(spa, FTAG);
162106eeb2adSek 
1622fa9e4066Sahrens 	return (error);
1623fa9e4066Sahrens }
1624fa9e4066Sahrens 
1625fa9e4066Sahrens static int
1626fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1627fa9e4066Sahrens {
1628fa9e4066Sahrens 	spa_t *spa;
1629fa9e4066Sahrens 	int error;
1630fa9e4066Sahrens 
1631fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1632fa9e4066Sahrens 	if (error == 0) {
1633fa9e4066Sahrens 		spa_freeze(spa);
1634fa9e4066Sahrens 		spa_close(spa, FTAG);
1635fa9e4066Sahrens 	}
1636fa9e4066Sahrens 	return (error);
1637fa9e4066Sahrens }
1638fa9e4066Sahrens 
1639eaca9bbdSeschrock static int
1640eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1641eaca9bbdSeschrock {
1642eaca9bbdSeschrock 	spa_t *spa;
1643eaca9bbdSeschrock 	int error;
1644eaca9bbdSeschrock 
164506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
164606eeb2adSek 		return (error);
164706eeb2adSek 
1648ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1649ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1650558d2d50Slling 		spa_close(spa, FTAG);
1651*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1652558d2d50Slling 	}
1653558d2d50Slling 
1654990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
165506eeb2adSek 	spa_close(spa, FTAG);
165606eeb2adSek 
165706eeb2adSek 	return (error);
165806eeb2adSek }
165906eeb2adSek 
166006eeb2adSek static int
166106eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
166206eeb2adSek {
166306eeb2adSek 	spa_t *spa;
166406eeb2adSek 	char *hist_buf;
166506eeb2adSek 	uint64_t size;
166606eeb2adSek 	int error;
166706eeb2adSek 
166806eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1669*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
167006eeb2adSek 
167106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
167206eeb2adSek 		return (error);
167306eeb2adSek 
1674e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1675d7306b64Sek 		spa_close(spa, FTAG);
1676*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1677d7306b64Sek 	}
1678d7306b64Sek 
167906eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
168006eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
168106eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1682478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1683478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1684478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
168506eeb2adSek 	}
168606eeb2adSek 
168706eeb2adSek 	spa_close(spa, FTAG);
168806eeb2adSek 	kmem_free(hist_buf, size);
168906eeb2adSek 	return (error);
169006eeb2adSek }
169106eeb2adSek 
1692e9103aaeSGarrett D'Amore static int
1693e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1694e9103aaeSGarrett D'Amore {
1695e9103aaeSGarrett D'Amore 	spa_t *spa;
1696e9103aaeSGarrett D'Amore 	int error;
1697e9103aaeSGarrett D'Amore 
1698e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1699e9103aaeSGarrett D'Amore 	if (error == 0) {
1700e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1701e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1702e9103aaeSGarrett D'Amore 	}
1703e9103aaeSGarrett D'Amore 	return (error);
1704e9103aaeSGarrett D'Amore }
1705e9103aaeSGarrett D'Amore 
170655434c77Sek static int
170755434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
170855434c77Sek {
17093b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
171055434c77Sek }
171155434c77Sek 
1712503ad85cSMatthew Ahrens /*
1713503ad85cSMatthew Ahrens  * inputs:
1714503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1715503ad85cSMatthew Ahrens  * zc_obj		object to find
1716503ad85cSMatthew Ahrens  *
1717503ad85cSMatthew Ahrens  * outputs:
1718503ad85cSMatthew Ahrens  * zc_value		name of object
1719503ad85cSMatthew Ahrens  */
172055434c77Sek static int
172155434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
172255434c77Sek {
1723503ad85cSMatthew Ahrens 	objset_t *os;
172455434c77Sek 	int error;
172555434c77Sek 
1726503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1727503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
172855434c77Sek 		return (error);
1729503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1730503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1731*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1732503ad85cSMatthew Ahrens 	}
1733503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
173455434c77Sek 	    sizeof (zc->zc_value));
1735503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
173655434c77Sek 
173755434c77Sek 	return (error);
173855434c77Sek }
173955434c77Sek 
174099d5e173STim Haley /*
174199d5e173STim Haley  * inputs:
174299d5e173STim Haley  * zc_name		name of filesystem
174399d5e173STim Haley  * zc_obj		object to find
174499d5e173STim Haley  *
174599d5e173STim Haley  * outputs:
174699d5e173STim Haley  * zc_stat		stats on object
174799d5e173STim Haley  * zc_value		path to object
174899d5e173STim Haley  */
174999d5e173STim Haley static int
175099d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
175199d5e173STim Haley {
175299d5e173STim Haley 	objset_t *os;
175399d5e173STim Haley 	int error;
175499d5e173STim Haley 
175599d5e173STim Haley 	/* XXX reading from objset not owned */
175699d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
175799d5e173STim Haley 		return (error);
175899d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
175999d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1760*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
176199d5e173STim Haley 	}
176299d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
176399d5e173STim Haley 	    sizeof (zc->zc_value));
176499d5e173STim Haley 	dmu_objset_rele(os, FTAG);
176599d5e173STim Haley 
176699d5e173STim Haley 	return (error);
176799d5e173STim Haley }
176899d5e173STim Haley 
1769fa9e4066Sahrens static int
1770fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1771fa9e4066Sahrens {
1772fa9e4066Sahrens 	spa_t *spa;
1773fa9e4066Sahrens 	int error;
1774e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1775e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1776fa9e4066Sahrens 
1777fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1778fa9e4066Sahrens 	if (error != 0)
1779fa9e4066Sahrens 		return (error);
1780fa9e4066Sahrens 
1781fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1782478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1783fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1784fa94a07fSbrendan 	    &l2cache, &nl2cache);
1785fa94a07fSbrendan 
1786e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1787e7cbe64fSgw 	    &spares, &nspares);
1788e7cbe64fSgw 
1789b1b8ab34Slling 	/*
1790b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1791e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1792e7cbe64fSgw 	 *
1793e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1794e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1795e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1796e7cbe64fSgw 	 *
1797e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1798b1b8ab34Slling 	 */
1799b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
18001195e687SMark J Musante 		nvlist_free(config);
1801b1b8ab34Slling 		spa_close(spa, FTAG);
1802*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1803b1b8ab34Slling 	}
1804b1b8ab34Slling 
1805fa94a07fSbrendan 	if (error == 0) {
1806fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1807fa9e4066Sahrens 		nvlist_free(config);
1808fa9e4066Sahrens 	}
1809fa9e4066Sahrens 	spa_close(spa, FTAG);
1810fa9e4066Sahrens 	return (error);
1811fa9e4066Sahrens }
1812fa9e4066Sahrens 
18133f9d6ad7SLin Ling /*
18143f9d6ad7SLin Ling  * inputs:
18153f9d6ad7SLin Ling  * zc_name		name of the pool
18163f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
18173f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
18183f9d6ad7SLin Ling  */
1819fa9e4066Sahrens static int
1820fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1821fa9e4066Sahrens {
182299653d4eSeschrock 	spa_t *spa;
182399653d4eSeschrock 	int error;
182499653d4eSeschrock 
182599653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
182699653d4eSeschrock 	if (error != 0)
182799653d4eSeschrock 		return (error);
182899653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
182999653d4eSeschrock 	spa_close(spa, FTAG);
183099653d4eSeschrock 	return (error);
1831fa9e4066Sahrens }
1832fa9e4066Sahrens 
1833fa9e4066Sahrens static int
18343d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1835fa9e4066Sahrens {
1836fa9e4066Sahrens 	spa_t *spa;
1837fa9e4066Sahrens 	int error;
18383d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1839fa9e4066Sahrens 
184006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1841fa9e4066Sahrens 		return (error);
18423d7072f8Seschrock 	switch (zc->zc_cookie) {
18433d7072f8Seschrock 	case VDEV_STATE_ONLINE:
18443d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
18453d7072f8Seschrock 		break;
1846fa9e4066Sahrens 
18473d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
18483d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
18493d7072f8Seschrock 		break;
1850fa9e4066Sahrens 
18513d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1852069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1853069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1854069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1855069f55e2SEric Schrock 
1856069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
18573d7072f8Seschrock 		break;
18583d7072f8Seschrock 
18593d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1860069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1861069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1862069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1863069f55e2SEric Schrock 
1864069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
18653d7072f8Seschrock 		break;
18663d7072f8Seschrock 
18673d7072f8Seschrock 	default:
1868*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
18693d7072f8Seschrock 	}
18703d7072f8Seschrock 	zc->zc_cookie = newstate;
1871fa9e4066Sahrens 	spa_close(spa, FTAG);
1872fa9e4066Sahrens 	return (error);
1873fa9e4066Sahrens }
1874fa9e4066Sahrens 
1875fa9e4066Sahrens static int
1876fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1877fa9e4066Sahrens {
1878fa9e4066Sahrens 	spa_t *spa;
1879fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1880fa9e4066Sahrens 	nvlist_t *config;
1881fa9e4066Sahrens 	int error;
1882fa9e4066Sahrens 
188306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1884fa9e4066Sahrens 		return (error);
1885fa9e4066Sahrens 
1886990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1887478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1888ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1889fa9e4066Sahrens 		nvlist_free(config);
1890fa9e4066Sahrens 	}
1891fa9e4066Sahrens 
1892fa9e4066Sahrens 	spa_close(spa, FTAG);
1893fa9e4066Sahrens 	return (error);
1894fa9e4066Sahrens }
1895fa9e4066Sahrens 
1896fa9e4066Sahrens static int
1897fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1898fa9e4066Sahrens {
1899fa9e4066Sahrens 	spa_t *spa;
1900fa9e4066Sahrens 	int error;
1901fa9e4066Sahrens 
190206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1903fa9e4066Sahrens 		return (error);
1904fa9e4066Sahrens 
19058ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1906fa9e4066Sahrens 
1907fa9e4066Sahrens 	spa_close(spa, FTAG);
1908fa9e4066Sahrens 	return (error);
1909fa9e4066Sahrens }
1910fa9e4066Sahrens 
19111195e687SMark J Musante static int
19121195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
19131195e687SMark J Musante {
19141195e687SMark J Musante 	spa_t *spa;
19151195e687SMark J Musante 	nvlist_t *config, *props = NULL;
19161195e687SMark J Musante 	int error;
19171195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
19181195e687SMark J Musante 
19191195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
19201195e687SMark J Musante 		return (error);
19211195e687SMark J Musante 
19221195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
19231195e687SMark J Musante 	    zc->zc_iflags, &config)) {
19241195e687SMark J Musante 		spa_close(spa, FTAG);
19251195e687SMark J Musante 		return (error);
19261195e687SMark J Musante 	}
19271195e687SMark J Musante 
19281195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
19291195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19301195e687SMark J Musante 	    zc->zc_iflags, &props))) {
19311195e687SMark J Musante 		spa_close(spa, FTAG);
19321195e687SMark J Musante 		nvlist_free(config);
19331195e687SMark J Musante 		return (error);
19341195e687SMark J Musante 	}
19351195e687SMark J Musante 
19361195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
19371195e687SMark J Musante 
19381195e687SMark J Musante 	spa_close(spa, FTAG);
19391195e687SMark J Musante 
19401195e687SMark J Musante 	nvlist_free(config);
19411195e687SMark J Musante 	nvlist_free(props);
19421195e687SMark J Musante 
19431195e687SMark J Musante 	return (error);
19441195e687SMark J Musante }
19451195e687SMark J Musante 
1946c67d9675Seschrock static int
1947c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1948c67d9675Seschrock {
1949c67d9675Seschrock 	spa_t *spa;
1950e9dbad6fSeschrock 	char *path = zc->zc_value;
1951ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
1952c67d9675Seschrock 	int error;
1953c67d9675Seschrock 
1954c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
1955c67d9675Seschrock 	if (error != 0)
1956c67d9675Seschrock 		return (error);
1957c67d9675Seschrock 
1958c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
1959c67d9675Seschrock 	spa_close(spa, FTAG);
1960c67d9675Seschrock 	return (error);
1961c67d9675Seschrock }
1962c67d9675Seschrock 
19636809eb4eSEric Schrock static int
19646809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
19656809eb4eSEric Schrock {
19666809eb4eSEric Schrock 	spa_t *spa;
19676809eb4eSEric Schrock 	char *fru = zc->zc_value;
19686809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
19696809eb4eSEric Schrock 	int error;
19706809eb4eSEric Schrock 
19716809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
19726809eb4eSEric Schrock 	if (error != 0)
19736809eb4eSEric Schrock 		return (error);
19746809eb4eSEric Schrock 
19756809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
19766809eb4eSEric Schrock 	spa_close(spa, FTAG);
19776809eb4eSEric Schrock 	return (error);
19786809eb4eSEric Schrock }
19796809eb4eSEric Schrock 
1980fa9e4066Sahrens static int
1981a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1982fa9e4066Sahrens {
1983a7f53a56SChris Kirby 	int error = 0;
19847f7322feSeschrock 	nvlist_t *nv;
1985fa9e4066Sahrens 
1986a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1987fa9e4066Sahrens 
19885ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
198992241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1990a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
1991432f72fdSahrens 		/*
1992bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
1993432f72fdSahrens 		 * which we aren't supposed to do with a
1994745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
1995432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
1996503ad85cSMatthew Ahrens 		 * XXX reading with out owning
1997432f72fdSahrens 		 */
199819b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
199919b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
200019b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
200119b94df9SMatthew Ahrens 			if (error == EIO)
200219b94df9SMatthew Ahrens 				return (error);
2003fb09f5aaSMadhav Suresh 			VERIFY0(error);
2004e7437265Sahrens 		}
2005e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
20067f7322feSeschrock 		nvlist_free(nv);
20077f7322feSeschrock 	}
2008fa9e4066Sahrens 
2009a7f53a56SChris Kirby 	return (error);
2010a7f53a56SChris Kirby }
2011a7f53a56SChris Kirby 
2012a7f53a56SChris Kirby /*
2013a7f53a56SChris Kirby  * inputs:
2014a7f53a56SChris Kirby  * zc_name		name of filesystem
2015a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2016a7f53a56SChris Kirby  *
2017a7f53a56SChris Kirby  * outputs:
2018a7f53a56SChris Kirby  * zc_objset_stats	stats
2019a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2020a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2021a7f53a56SChris Kirby  */
2022a7f53a56SChris Kirby static int
2023a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2024a7f53a56SChris Kirby {
20253b2aab18SMatthew Ahrens 	objset_t *os;
2026a7f53a56SChris Kirby 	int error;
2027a7f53a56SChris Kirby 
20283b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
20293b2aab18SMatthew Ahrens 	if (error == 0) {
20303b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
20313b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
20323b2aab18SMatthew Ahrens 	}
2033a7f53a56SChris Kirby 
2034fa9e4066Sahrens 	return (error);
2035fa9e4066Sahrens }
2036fa9e4066Sahrens 
203792241e0bSTom Erickson /*
203892241e0bSTom Erickson  * inputs:
203992241e0bSTom Erickson  * zc_name		name of filesystem
204092241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
204192241e0bSTom Erickson  *
204292241e0bSTom Erickson  * outputs:
204392241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
204492241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
204592241e0bSTom Erickson  *
204692241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
204792241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
204892241e0bSTom Erickson  * local property values.
204992241e0bSTom Erickson  */
205092241e0bSTom Erickson static int
205192241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
205292241e0bSTom Erickson {
20533b2aab18SMatthew Ahrens 	int error = 0;
205492241e0bSTom Erickson 	nvlist_t *nv;
205592241e0bSTom Erickson 
205692241e0bSTom Erickson 	/*
205792241e0bSTom Erickson 	 * Without this check, we would return local property values if the
205892241e0bSTom Erickson 	 * caller has not already received properties on or after
205992241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
206092241e0bSTom Erickson 	 */
20613b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2062*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
206392241e0bSTom Erickson 
206492241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
20653b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
206692241e0bSTom Erickson 		error = put_nvlist(zc, nv);
206792241e0bSTom Erickson 		nvlist_free(nv);
206892241e0bSTom Erickson 	}
206992241e0bSTom Erickson 
207092241e0bSTom Erickson 	return (error);
207192241e0bSTom Erickson }
207292241e0bSTom Erickson 
2073de8267e0Stimh static int
2074de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2075de8267e0Stimh {
2076de8267e0Stimh 	uint64_t value;
2077de8267e0Stimh 	int error;
2078de8267e0Stimh 
2079de8267e0Stimh 	/*
2080de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2081de8267e0Stimh 	 * the default value (if there is one).
2082de8267e0Stimh 	 */
2083de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2084de8267e0Stimh 		return (error);
2085de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2086de8267e0Stimh 	return (0);
2087de8267e0Stimh }
2088de8267e0Stimh 
20893cb34c60Sahrens /*
20903cb34c60Sahrens  * inputs:
20913cb34c60Sahrens  * zc_name		name of filesystem
2092de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
20933cb34c60Sahrens  *
20943cb34c60Sahrens  * outputs:
2095de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2096de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
20973cb34c60Sahrens  */
2098bd00f61bSrm static int
2099de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2100bd00f61bSrm {
2101de8267e0Stimh 	objset_t *os;
2102de8267e0Stimh 	int err;
2103bd00f61bSrm 
2104503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2105503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2106de8267e0Stimh 		return (err);
2107bd00f61bSrm 
2108bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2109bd00f61bSrm 
2110bd00f61bSrm 	/*
2111de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2112745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2113745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2114bd00f61bSrm 	 */
2115de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2116de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2117de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2118de8267e0Stimh 		nvlist_t *nv;
2119de8267e0Stimh 
2120de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2121de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2122de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2123de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2124de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2125de8267e0Stimh 			err = put_nvlist(zc, nv);
2126de8267e0Stimh 		nvlist_free(nv);
2127de8267e0Stimh 	} else {
2128*be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2129de8267e0Stimh 	}
2130503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2131de8267e0Stimh 	return (err);
2132bd00f61bSrm }
2133bd00f61bSrm 
213414843421SMatthew Ahrens static boolean_t
213514843421SMatthew Ahrens dataset_name_hidden(const char *name)
213614843421SMatthew Ahrens {
213714843421SMatthew Ahrens 	/*
213814843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
213914843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
214014843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
214114843421SMatthew Ahrens 	 */
214214843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
214314843421SMatthew Ahrens 		return (B_TRUE);
214414843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
214514843421SMatthew Ahrens 		return (B_TRUE);
214614843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
214714843421SMatthew Ahrens 		return (B_TRUE);
214814843421SMatthew Ahrens 	return (B_FALSE);
214914843421SMatthew Ahrens }
215014843421SMatthew Ahrens 
2151de8267e0Stimh /*
2152de8267e0Stimh  * inputs:
2153de8267e0Stimh  * zc_name		name of filesystem
2154de8267e0Stimh  * zc_cookie		zap cursor
2155de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2156de8267e0Stimh  *
2157de8267e0Stimh  * outputs:
2158de8267e0Stimh  * zc_name		name of next filesystem
215914843421SMatthew Ahrens  * zc_cookie		zap cursor
2160de8267e0Stimh  * zc_objset_stats	stats
2161de8267e0Stimh  * zc_nvlist_dst	property nvlist
2162de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2163de8267e0Stimh  */
2164fa9e4066Sahrens static int
2165fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2166fa9e4066Sahrens {
216787e5029aSahrens 	objset_t *os;
2168fa9e4066Sahrens 	int error;
2169fa9e4066Sahrens 	char *p;
2170620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2171fa9e4066Sahrens 
2172620252bcSChris Kirby top:
2173503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
217487e5029aSahrens 		if (error == ENOENT)
2175*be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
217687e5029aSahrens 		return (error);
2177fa9e4066Sahrens 	}
2178fa9e4066Sahrens 
2179fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2180fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2181fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2182fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2183fa9e4066Sahrens 
2184fa9e4066Sahrens 	do {
218587e5029aSahrens 		error = dmu_dir_list_next(os,
218687e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
218787e5029aSahrens 		    NULL, &zc->zc_cookie);
2188fa9e4066Sahrens 		if (error == ENOENT)
2189*be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
219019b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2191503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2192fa9e4066Sahrens 
2193681d9761SEric Taylor 	/*
2194681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2195681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2196681d9761SEric Taylor 	 */
2197620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
219887e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2199620252bcSChris Kirby 		if (error == ENOENT) {
2200620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2201620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2202620252bcSChris Kirby 			goto top;
2203620252bcSChris Kirby 		}
2204620252bcSChris Kirby 	}
2205fa9e4066Sahrens 	return (error);
2206fa9e4066Sahrens }
2207fa9e4066Sahrens 
22083cb34c60Sahrens /*
22093cb34c60Sahrens  * inputs:
22103cb34c60Sahrens  * zc_name		name of filesystem
22113cb34c60Sahrens  * zc_cookie		zap cursor
22123cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22133cb34c60Sahrens  *
22143cb34c60Sahrens  * outputs:
22153cb34c60Sahrens  * zc_name		name of next snapshot
22163cb34c60Sahrens  * zc_objset_stats	stats
22173cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22183cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22193cb34c60Sahrens  */
2220fa9e4066Sahrens static int
2221fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2222fa9e4066Sahrens {
222387e5029aSahrens 	objset_t *os;
2224fa9e4066Sahrens 	int error;
2225fa9e4066Sahrens 
2226503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
22273b2aab18SMatthew Ahrens 	if (error != 0) {
2228745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
22293b2aab18SMatthew Ahrens 	}
2230fa9e4066Sahrens 
2231b81d61a6Slling 	/*
2232b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2233b81d61a6Slling 	 * so exit immediately.
2234b81d61a6Slling 	 */
2235b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2236503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2237*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2238fa9e4066Sahrens 	}
2239fa9e4066Sahrens 
224087e5029aSahrens 	error = dmu_snapshot_list_next(os,
224187e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2242a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2243a7f53a56SChris Kirby 	    NULL);
2244a7f53a56SChris Kirby 
2245620252bcSChris Kirby 	if (error == 0) {
2246a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2247a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2248a7f53a56SChris Kirby 
2249a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
22503b2aab18SMatthew Ahrens 		if (error == 0) {
2251a7f53a56SChris Kirby 			objset_t *ossnap;
2252a7f53a56SChris Kirby 
2253a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2254a7f53a56SChris Kirby 			if (error == 0)
2255a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2256a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2257620252bcSChris Kirby 		}
2258620252bcSChris Kirby 	} else if (error == ENOENT) {
2259*be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2260620252bcSChris Kirby 	}
2261fa9e4066Sahrens 
2262a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
22633cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
22643b2aab18SMatthew Ahrens 	if (error != 0)
22653cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2266fa9e4066Sahrens 	return (error);
2267fa9e4066Sahrens }
2268fa9e4066Sahrens 
226992241e0bSTom Erickson static int
227092241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2271fa9e4066Sahrens {
227292241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
227392241e0bSTom Erickson 	uint64_t *valary;
227492241e0bSTom Erickson 	unsigned int vallen;
227592241e0bSTom Erickson 	const char *domain;
2276eeb85002STim Haley 	char *dash;
227792241e0bSTom Erickson 	zfs_userquota_prop_t type;
227892241e0bSTom Erickson 	uint64_t rid;
227992241e0bSTom Erickson 	uint64_t quota;
228092241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
228192241e0bSTom Erickson 	int err;
228292241e0bSTom Erickson 
228392241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
228492241e0bSTom Erickson 		nvlist_t *attrs;
228592241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2286eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2287eeb85002STim Haley 		    &pair) != 0)
2288*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
228992241e0bSTom Erickson 	}
2290e9dbad6fSeschrock 
2291ecd6cf80Smarks 	/*
2292eeb85002STim Haley 	 * A correctly constructed propname is encoded as
229392241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2294ecd6cf80Smarks 	 */
2295eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2296eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2297eeb85002STim Haley 	    vallen != 3)
2298*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2299eeb85002STim Haley 
2300eeb85002STim Haley 	domain = dash + 1;
2301eeb85002STim Haley 	type = valary[0];
2302eeb85002STim Haley 	rid = valary[1];
2303eeb85002STim Haley 	quota = valary[2];
2304e9dbad6fSeschrock 
23051412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
230692241e0bSTom Erickson 	if (err == 0) {
230792241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
230892241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
230992241e0bSTom Erickson 	}
2310e9dbad6fSeschrock 
231192241e0bSTom Erickson 	return (err);
231292241e0bSTom Erickson }
231314843421SMatthew Ahrens 
231492241e0bSTom Erickson /*
231592241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
231692241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
231792241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
231892241e0bSTom Erickson  *
2319eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
232092241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
232192241e0bSTom Erickson  */
232292241e0bSTom Erickson static int
232392241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
232492241e0bSTom Erickson     nvpair_t *pair)
232592241e0bSTom Erickson {
232692241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
232792241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
232892241e0bSTom Erickson 	uint64_t intval;
232992241e0bSTom Erickson 	int err;
2330fa9e4066Sahrens 
233192241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
233292241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
233392241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
233492241e0bSTom Erickson 		return (-1);
233592241e0bSTom Erickson 	}
233614843421SMatthew Ahrens 
233792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
233892241e0bSTom Erickson 		nvlist_t *attrs;
233992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
234092241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
234192241e0bSTom Erickson 		    &pair) == 0);
234292241e0bSTom Erickson 	}
2343db870a07Sahrens 
234492241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
234592241e0bSTom Erickson 		return (-1);
2346b24ab676SJeff Bonwick 
234792241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
234840feaa91Sahrens 
234992241e0bSTom Erickson 	switch (prop) {
235092241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
235192241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
235292241e0bSTom Erickson 		break;
235392241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
23543b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
235592241e0bSTom Erickson 		break;
235692241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
235792241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
235892241e0bSTom Erickson 		break;
235992241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
23603b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
236192241e0bSTom Erickson 		break;
236292241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2363c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
236492241e0bSTom Erickson 		break;
236592241e0bSTom Erickson 	case ZFS_PROP_VERSION:
236692241e0bSTom Erickson 	{
236792241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
23689e6eda55Smarks 
23691412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2370b24ab676SJeff Bonwick 			break;
2371b24ab676SJeff Bonwick 
237292241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
237392241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2374d0f3f37eSMark Shellenbaum 
237592241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2376b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2377b16da2e2SGeorge Wilson 
2378b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2379b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2380b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2381b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
238240feaa91Sahrens 		}
238392241e0bSTom Erickson 		break;
2384ecd6cf80Smarks 	}
2385a6f561b4SSašo Kiselkov 	case ZFS_PROP_COMPRESSION:
2386a6f561b4SSašo Kiselkov 	{
2387a6f561b4SSašo Kiselkov 		if (intval == ZIO_COMPRESS_LZ4) {
2388a6f561b4SSašo Kiselkov 			zfeature_info_t *feature =
2389a6f561b4SSašo Kiselkov 			    &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
2390a6f561b4SSašo Kiselkov 			spa_t *spa;
2391a6f561b4SSašo Kiselkov 
2392a6f561b4SSašo Kiselkov 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2393a6f561b4SSašo Kiselkov 				return (err);
2394a6f561b4SSašo Kiselkov 
2395a6f561b4SSašo Kiselkov 			/*
2396a6f561b4SSašo Kiselkov 			 * Setting the LZ4 compression algorithm activates
2397a6f561b4SSašo Kiselkov 			 * the feature.
2398a6f561b4SSašo Kiselkov 			 */
2399a6f561b4SSašo Kiselkov 			if (!spa_feature_is_active(spa, feature)) {
24003b2aab18SMatthew Ahrens 				if ((err = zfs_prop_activate_feature(spa,
2401a6f561b4SSašo Kiselkov 				    feature)) != 0) {
2402a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
2403a6f561b4SSašo Kiselkov 					return (err);
2404a6f561b4SSašo Kiselkov 				}
2405a6f561b4SSašo Kiselkov 			}
2406a6f561b4SSašo Kiselkov 
2407a6f561b4SSašo Kiselkov 			spa_close(spa, FTAG);
2408a6f561b4SSašo Kiselkov 		}
2409a6f561b4SSašo Kiselkov 		/*
2410a6f561b4SSašo Kiselkov 		 * We still want the default set action to be performed in the
2411a6f561b4SSašo Kiselkov 		 * caller, we only performed zfeature settings here.
2412a6f561b4SSašo Kiselkov 		 */
2413a6f561b4SSašo Kiselkov 		err = -1;
2414a6f561b4SSašo Kiselkov 		break;
2415a6f561b4SSašo Kiselkov 	}
2416ecd6cf80Smarks 
241792241e0bSTom Erickson 	default:
241892241e0bSTom Erickson 		err = -1;
241992241e0bSTom Erickson 	}
2420e9dbad6fSeschrock 
242192241e0bSTom Erickson 	return (err);
242292241e0bSTom Erickson }
2423a9799022Sck 
242492241e0bSTom Erickson /*
242592241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
24264445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
24274445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
24284445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
24294445fffbSMatthew Ahrens  * corresponding error numbers.
243092241e0bSTom Erickson  *
24314445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
24324445fffbSMatthew Ahrens  * modified.
243392241e0bSTom Erickson  */
243492241e0bSTom Erickson int
243592241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
24364445fffbSMatthew Ahrens     nvlist_t *errlist)
243792241e0bSTom Erickson {
243892241e0bSTom Erickson 	nvpair_t *pair;
243992241e0bSTom Erickson 	nvpair_t *propval;
244002e383d1STom Erickson 	int rv = 0;
244192241e0bSTom Erickson 	uint64_t intval;
244292241e0bSTom Erickson 	char *strval;
24434445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
24444445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2445a9799022Sck 
244692241e0bSTom Erickson retry:
244792241e0bSTom Erickson 	pair = NULL;
244892241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
244992241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
245092241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2451cfa69fd2STom Erickson 		int err = 0;
2452e9dbad6fSeschrock 
245392241e0bSTom Erickson 		/* decode the property value */
245492241e0bSTom Erickson 		propval = pair;
245592241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
245692241e0bSTom Erickson 			nvlist_t *attrs;
24574445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2458eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2459eeb85002STim Haley 			    &propval) != 0)
2460*be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
246114843421SMatthew Ahrens 		}
2462e9dbad6fSeschrock 
246392241e0bSTom Erickson 		/* Validate value type */
2464eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
246592241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
246692241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2467*be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
246892241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
246992241e0bSTom Erickson 				if (nvpair_type(propval) !=
247092241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2471*be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
247219b94df9SMatthew Ahrens 			} else {
2473*be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
24744201a95eSRic Aleshire 			}
2475eeb85002STim Haley 		} else if (err == 0) {
247692241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
247792241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2478*be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
247992241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2480a2eea2e1Sahrens 				const char *unused;
2481a2eea2e1Sahrens 
24824445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2483e9dbad6fSeschrock 
2484e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
248591ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2486e9dbad6fSeschrock 					break;
248791ebeef5Sahrens 				case PROP_TYPE_STRING:
2488*be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
248992241e0bSTom Erickson 					break;
249091ebeef5Sahrens 				case PROP_TYPE_INDEX:
2491acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
249292241e0bSTom Erickson 					    intval, &unused) != 0)
2493*be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2494e9dbad6fSeschrock 					break;
2495e9dbad6fSeschrock 				default:
2496e7437265Sahrens 					cmn_err(CE_PANIC,
2497e7437265Sahrens 					    "unknown property type");
2498e9dbad6fSeschrock 				}
2499e9dbad6fSeschrock 			} else {
2500*be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2501e9dbad6fSeschrock 			}
2502e9dbad6fSeschrock 		}
250392241e0bSTom Erickson 
250492241e0bSTom Erickson 		/* Validate permissions */
250592241e0bSTom Erickson 		if (err == 0)
250692241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
250792241e0bSTom Erickson 
250892241e0bSTom Erickson 		if (err == 0) {
250992241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
251092241e0bSTom Erickson 			if (err == -1) {
251192241e0bSTom Erickson 				/*
251292241e0bSTom Erickson 				 * For better performance we build up a list of
251392241e0bSTom Erickson 				 * properties to set in a single transaction.
251492241e0bSTom Erickson 				 */
251592241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
251692241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
251792241e0bSTom Erickson 				/*
251892241e0bSTom Erickson 				 * This may be a spurious error caused by
251992241e0bSTom Erickson 				 * receiving quota and reservation out of order.
252092241e0bSTom Erickson 				 * Try again in a second pass.
252192241e0bSTom Erickson 				 */
252292241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
252392241e0bSTom Erickson 			}
252492241e0bSTom Erickson 		}
252592241e0bSTom Erickson 
25264445fffbSMatthew Ahrens 		if (err != 0) {
25274445fffbSMatthew Ahrens 			if (errlist != NULL)
25284445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
25294445fffbSMatthew Ahrens 			rv = err;
25304445fffbSMatthew Ahrens 		}
2531e9dbad6fSeschrock 	}
2532e9dbad6fSeschrock 
253392241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
253492241e0bSTom Erickson 		nvl = retrynvl;
253592241e0bSTom Erickson 		goto retry;
253692241e0bSTom Erickson 	}
253792241e0bSTom Erickson 
253892241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
253992241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
254092241e0bSTom Erickson 		/*
254192241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
254292241e0bSTom Erickson 		 * can, so try setting them individually.
254392241e0bSTom Erickson 		 */
254492241e0bSTom Erickson 		pair = NULL;
254592241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
254692241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2547cfa69fd2STom Erickson 			int err = 0;
254892241e0bSTom Erickson 
254992241e0bSTom Erickson 			propval = pair;
255092241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
255192241e0bSTom Erickson 				nvlist_t *attrs;
25524445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
25534445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
25544445fffbSMatthew Ahrens 				    ZPROP_VALUE);
255592241e0bSTom Erickson 			}
255692241e0bSTom Erickson 
255792241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
25584445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
25593b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
25603b2aab18SMatthew Ahrens 				    source, strval);
256192241e0bSTom Erickson 			} else {
25624445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
25633b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
25643b2aab18SMatthew Ahrens 				    intval);
256592241e0bSTom Erickson 			}
256692241e0bSTom Erickson 
256792241e0bSTom Erickson 			if (err != 0) {
25684445fffbSMatthew Ahrens 				if (errlist != NULL) {
25694445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
25704445fffbSMatthew Ahrens 					    err);
25714445fffbSMatthew Ahrens 				}
25724445fffbSMatthew Ahrens 				rv = err;
257392241e0bSTom Erickson 			}
257492241e0bSTom Erickson 		}
25755c0b6a79SRich Morris 	}
25765c0b6a79SRich Morris 	nvlist_free(genericnvl);
257792241e0bSTom Erickson 	nvlist_free(retrynvl);
257892241e0bSTom Erickson 
257992241e0bSTom Erickson 	return (rv);
2580fa9e4066Sahrens }
2581fa9e4066Sahrens 
2582ea2f5b9eSMatthew Ahrens /*
2583ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2584ea2f5b9eSMatthew Ahrens  */
2585ea2f5b9eSMatthew Ahrens static int
25864445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2587ea2f5b9eSMatthew Ahrens {
258892241e0bSTom Erickson 	nvpair_t *pair = NULL;
2589ea2f5b9eSMatthew Ahrens 	int error = 0;
2590ea2f5b9eSMatthew Ahrens 
259192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
259292241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2593ea2f5b9eSMatthew Ahrens 		char *valstr;
2594ea2f5b9eSMatthew Ahrens 
2595ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
259692241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2597*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2598ea2f5b9eSMatthew Ahrens 
2599ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2600ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2601ea2f5b9eSMatthew Ahrens 			return (error);
2602ea2f5b9eSMatthew Ahrens 
2603ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2604*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2605ea2f5b9eSMatthew Ahrens 
260692241e0bSTom Erickson 		VERIFY(nvpair_value_string(pair, &valstr) == 0);
2607ea2f5b9eSMatthew Ahrens 		if (strlen(valstr) >= ZAP_MAXVALUELEN)
2608ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2609ea2f5b9eSMatthew Ahrens 	}
2610ea2f5b9eSMatthew Ahrens 	return (0);
2611ea2f5b9eSMatthew Ahrens }
2612ea2f5b9eSMatthew Ahrens 
261392241e0bSTom Erickson static void
261492241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
261592241e0bSTom Erickson {
261692241e0bSTom Erickson 	nvpair_t *pair;
261792241e0bSTom Erickson 
261892241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
261992241e0bSTom Erickson 
262092241e0bSTom Erickson 	pair = NULL;
262192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
262292241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
262392241e0bSTom Erickson 			continue;
262492241e0bSTom Erickson 
262592241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
262692241e0bSTom Erickson 	}
262792241e0bSTom Erickson }
262892241e0bSTom Erickson 
262992241e0bSTom Erickson static int
26303b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
263192241e0bSTom Erickson     nvlist_t *skipped)
263292241e0bSTom Erickson {
263392241e0bSTom Erickson 	int err = 0;
263492241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
263592241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
263692241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
263792241e0bSTom Erickson 		/*
263892241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
263992241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
264092241e0bSTom Erickson 		 */
264192241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
26423b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
26433b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
264492241e0bSTom Erickson 	}
264592241e0bSTom Erickson 	nvlist_free(cleared_props);
264692241e0bSTom Erickson 	return (err);
264792241e0bSTom Erickson }
264892241e0bSTom Erickson 
26493cb34c60Sahrens /*
26503cb34c60Sahrens  * inputs:
26513cb34c60Sahrens  * zc_name		name of filesystem
26525c0b6a79SRich Morris  * zc_value		name of property to set
26533cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
265492241e0bSTom Erickson  * zc_cookie		received properties flag
26553cb34c60Sahrens  *
265692241e0bSTom Erickson  * outputs:
265792241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
26583cb34c60Sahrens  */
2659fa9e4066Sahrens static int
2660e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2661fa9e4066Sahrens {
2662e9dbad6fSeschrock 	nvlist_t *nvl;
266392241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
266492241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
266592241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
26664445fffbSMatthew Ahrens 	nvlist_t *errors;
2667e9dbad6fSeschrock 	int error;
2668e9dbad6fSeschrock 
2669990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2670478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2671e9dbad6fSeschrock 		return (error);
2672e9dbad6fSeschrock 
267392241e0bSTom Erickson 	if (received) {
2674bb0ade09Sahrens 		nvlist_t *origprops;
2675bb0ade09Sahrens 
26763b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
26773b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
26783b2aab18SMatthew Ahrens 			    origprops, nvl);
26793b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2680bb0ade09Sahrens 		}
26813b2aab18SMatthew Ahrens 
26823b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2683bb0ade09Sahrens 	}
2684bb0ade09Sahrens 
26854445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
26863b2aab18SMatthew Ahrens 	if (error == 0)
26873b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
268892241e0bSTom Erickson 
268992241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
269092241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
269192241e0bSTom Erickson 	}
2692ecd6cf80Smarks 
269392241e0bSTom Erickson 	nvlist_free(errors);
2694e9dbad6fSeschrock 	nvlist_free(nvl);
2695e9dbad6fSeschrock 	return (error);
2696fa9e4066Sahrens }
2697fa9e4066Sahrens 
26983cb34c60Sahrens /*
26993cb34c60Sahrens  * inputs:
27003cb34c60Sahrens  * zc_name		name of filesystem
27013cb34c60Sahrens  * zc_value		name of property to inherit
270292241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
27033cb34c60Sahrens  *
27043cb34c60Sahrens  * outputs:		none
27053cb34c60Sahrens  */
2706e45ce728Sahrens static int
2707e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2708e45ce728Sahrens {
270992241e0bSTom Erickson 	const char *propname = zc->zc_value;
271092241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
271192241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
271292241e0bSTom Erickson 	zprop_source_t source = (received
271392241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
271492241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
271592241e0bSTom Erickson 
271692241e0bSTom Erickson 	if (received) {
271792241e0bSTom Erickson 		nvlist_t *dummy;
271892241e0bSTom Erickson 		nvpair_t *pair;
271992241e0bSTom Erickson 		zprop_type_t type;
272092241e0bSTom Erickson 		int err;
272192241e0bSTom Erickson 
272292241e0bSTom Erickson 		/*
272392241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
272492241e0bSTom Erickson 		 * nvpair with type info.
272592241e0bSTom Erickson 		 */
272692241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
272792241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2728*be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
272992241e0bSTom Erickson 
273092241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2731a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2732a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2733*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
273492241e0bSTom Erickson 		} else {
273592241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
273692241e0bSTom Erickson 		}
273792241e0bSTom Erickson 
273892241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
273992241e0bSTom Erickson 
274092241e0bSTom Erickson 		switch (type) {
274192241e0bSTom Erickson 		case PROP_TYPE_STRING:
274292241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
274392241e0bSTom Erickson 			break;
274492241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
274592241e0bSTom Erickson 		case PROP_TYPE_INDEX:
274692241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
274792241e0bSTom Erickson 			break;
274892241e0bSTom Erickson 		default:
274992241e0bSTom Erickson 			nvlist_free(dummy);
2750*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
275192241e0bSTom Erickson 		}
275292241e0bSTom Erickson 
275392241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
275492241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
275592241e0bSTom Erickson 		nvlist_free(dummy);
275692241e0bSTom Erickson 		if (err != -1)
275792241e0bSTom Erickson 			return (err); /* special property already handled */
275892241e0bSTom Erickson 	} else {
275992241e0bSTom Erickson 		/*
276092241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
276192241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
276292241e0bSTom Erickson 		 * and reservation to the received or default values even though
276392241e0bSTom Erickson 		 * they are not considered inheritable.
276492241e0bSTom Erickson 		 */
276592241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2766*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
276792241e0bSTom Erickson 	}
276892241e0bSTom Erickson 
27694445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
27703b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2771e45ce728Sahrens }
2772e45ce728Sahrens 
2773b1b8ab34Slling static int
277411a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2775b1b8ab34Slling {
2776990b4856Slling 	nvlist_t *props;
2777b1b8ab34Slling 	spa_t *spa;
2778990b4856Slling 	int error;
277992241e0bSTom Erickson 	nvpair_t *pair;
2780b1b8ab34Slling 
278192241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
278292241e0bSTom Erickson 	    zc->zc_iflags, &props))
2783b1b8ab34Slling 		return (error);
2784b1b8ab34Slling 
2785379c004dSEric Schrock 	/*
2786379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2787379c004dSEric Schrock 	 * to handle the faulted case.
2788379c004dSEric Schrock 	 */
278992241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
279092241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2791379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
279292241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2793379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2794379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2795379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2796379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2797379c004dSEric Schrock 		}
2798379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2799b693757aSEric Schrock 		if (spa != NULL) {
2800b693757aSEric Schrock 			nvlist_free(props);
2801379c004dSEric Schrock 			return (0);
2802b693757aSEric Schrock 		}
2803379c004dSEric Schrock 	}
2804379c004dSEric Schrock 
2805b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2806990b4856Slling 		nvlist_free(props);
2807b1b8ab34Slling 		return (error);
2808b1b8ab34Slling 	}
2809b1b8ab34Slling 
2810990b4856Slling 	error = spa_prop_set(spa, props);
2811b1b8ab34Slling 
2812990b4856Slling 	nvlist_free(props);
2813b1b8ab34Slling 	spa_close(spa, FTAG);
2814b1b8ab34Slling 
2815b1b8ab34Slling 	return (error);
2816b1b8ab34Slling }
2817b1b8ab34Slling 
2818b1b8ab34Slling static int
281911a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2820b1b8ab34Slling {
2821b1b8ab34Slling 	spa_t *spa;
2822b1b8ab34Slling 	int error;
2823b1b8ab34Slling 	nvlist_t *nvp = NULL;
2824b1b8ab34Slling 
2825379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2826379c004dSEric Schrock 		/*
2827379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2828379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2829379c004dSEric Schrock 		 * anyway.
2830379c004dSEric Schrock 		 */
2831379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2832379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2833379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2834379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2835379c004dSEric Schrock 	} else {
2836379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2837379c004dSEric Schrock 		spa_close(spa, FTAG);
2838379c004dSEric Schrock 	}
2839b1b8ab34Slling 
2840b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2841b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2842b1b8ab34Slling 	else
2843*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2844b1b8ab34Slling 
2845379c004dSEric Schrock 	nvlist_free(nvp);
2846b1b8ab34Slling 	return (error);
2847b1b8ab34Slling }
2848b1b8ab34Slling 
28493cb34c60Sahrens /*
28503cb34c60Sahrens  * inputs:
28513cb34c60Sahrens  * zc_name		name of filesystem
28523cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
28533cb34c60Sahrens  * zc_perm_action	allow/unallow flag
28543cb34c60Sahrens  *
28553cb34c60Sahrens  * outputs:		none
28563cb34c60Sahrens  */
2857ecd6cf80Smarks static int
2858ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2859ecd6cf80Smarks {
2860ecd6cf80Smarks 	int error;
2861ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2862ecd6cf80Smarks 
2863990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2864478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2865ecd6cf80Smarks 		return (error);
2866ecd6cf80Smarks 
2867ecd6cf80Smarks 	/*
2868ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2869ecd6cf80Smarks 	 */
2870ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2871ecd6cf80Smarks 		nvlist_free(fsaclnv);
2872*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2873ecd6cf80Smarks 	}
2874ecd6cf80Smarks 
2875ecd6cf80Smarks 	/*
2876ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2877ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2878ecd6cf80Smarks 	 * the nvlist(s)
2879ecd6cf80Smarks 	 */
2880ecd6cf80Smarks 
288191ebeef5Sahrens 	error = secpolicy_zfs(CRED());
28823b2aab18SMatthew Ahrens 	if (error != 0) {
288391ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
288491ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
288591ebeef5Sahrens 			    fsaclnv, CRED());
288691ebeef5Sahrens 		} else {
288791ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
288891ebeef5Sahrens 			    fsaclnv, CRED());
288991ebeef5Sahrens 		}
2890ecd6cf80Smarks 	}
2891ecd6cf80Smarks 
2892ecd6cf80Smarks 	if (error == 0)
2893ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2894ecd6cf80Smarks 
2895ecd6cf80Smarks 	nvlist_free(fsaclnv);
2896ecd6cf80Smarks 	return (error);
2897ecd6cf80Smarks }
2898ecd6cf80Smarks 
28993cb34c60Sahrens /*
29003cb34c60Sahrens  * inputs:
29013cb34c60Sahrens  * zc_name		name of filesystem
29023cb34c60Sahrens  *
29033cb34c60Sahrens  * outputs:
29043cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29053cb34c60Sahrens  */
2906ecd6cf80Smarks static int
2907ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2908ecd6cf80Smarks {
2909ecd6cf80Smarks 	nvlist_t *nvp;
2910ecd6cf80Smarks 	int error;
2911ecd6cf80Smarks 
2912ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2913ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2914ecd6cf80Smarks 		nvlist_free(nvp);
2915ecd6cf80Smarks 	}
2916ecd6cf80Smarks 
2917ecd6cf80Smarks 	return (error);
2918ecd6cf80Smarks }
2919ecd6cf80Smarks 
2920fa9e4066Sahrens /*
2921fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2922fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2923fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2924fa9e4066Sahrens  */
2925fa9e4066Sahrens static vfs_t *
2926fa9e4066Sahrens zfs_get_vfs(const char *resource)
2927fa9e4066Sahrens {
2928fa9e4066Sahrens 	struct vfs *vfsp;
2929fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2930fa9e4066Sahrens 
2931fa9e4066Sahrens 	vfs_list_read_lock();
2932fa9e4066Sahrens 	vfsp = rootvfs;
2933fa9e4066Sahrens 	do {
2934fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2935fa9e4066Sahrens 			VFS_HOLD(vfsp);
2936fa9e4066Sahrens 			vfs_found = vfsp;
2937fa9e4066Sahrens 			break;
2938fa9e4066Sahrens 		}
2939fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2940fa9e4066Sahrens 	} while (vfsp != rootvfs);
2941fa9e4066Sahrens 	vfs_list_unlock();
2942fa9e4066Sahrens 	return (vfs_found);
2943fa9e4066Sahrens }
2944fa9e4066Sahrens 
2945ecd6cf80Smarks /* ARGSUSED */
2946fa9e4066Sahrens static void
2947ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2948fa9e4066Sahrens {
2949da6c28aaSamw 	zfs_creat_t *zct = arg;
2950da6c28aaSamw 
2951de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2952da6c28aaSamw }
2953da6c28aaSamw 
2954de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2955da6c28aaSamw 
2956da6c28aaSamw /*
2957de8267e0Stimh  * inputs:
29580a48a24eStimh  * createprops		list of properties requested by creator
29590a48a24eStimh  * default_zplver	zpl version to use if unspecified in createprops
29600a48a24eStimh  * fuids_ok		fuids allowed in this version of the spa?
29610a48a24eStimh  * os			parent objset pointer (NULL if root fs)
2962de8267e0Stimh  *
2963de8267e0Stimh  * outputs:
2964de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
29650a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
2966da6c28aaSamw  *
2967de8267e0Stimh  * Determine the settings for utf8only, normalization and
2968de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
2969de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
2970de8267e0Stimh  * the file system is of too early a vintage, a creator can not
2971de8267e0Stimh  * request settings for these properties, even if the requested
2972de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
2973de8267e0Stimh  * properties for these, so remove them from the source nvlist after
2974de8267e0Stimh  * processing.
2975da6c28aaSamw  */
2976da6c28aaSamw static int
297714843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
29780a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
29790a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
2980da6c28aaSamw {
2981de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
2982de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
2983de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2984da6c28aaSamw 
2985de8267e0Stimh 	ASSERT(zplprops != NULL);
2986da6c28aaSamw 
2987de8267e0Stimh 	/*
2988de8267e0Stimh 	 * Pull out creator prop choices, if any.
2989de8267e0Stimh 	 */
2990de8267e0Stimh 	if (createprops) {
29910a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
29920a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2993de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2994de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2995de8267e0Stimh 		(void) nvlist_remove_all(createprops,
2996de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2997de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
2998de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2999de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3000de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3001de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3002de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3003de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3004de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3005de8267e0Stimh 	}
3006da6c28aaSamw 
3007c2a93d44Stimh 	/*
30080a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30090a48a24eStimh 	 * or pool is version is too "young" to support normalization
30100a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30110a48a24eStimh 	 * error out.
3012c2a93d44Stimh 	 */
30130a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30140a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30150a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30160a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3017de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30180a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3019*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3020c2a93d44Stimh 
3021de8267e0Stimh 	/*
3022de8267e0Stimh 	 * Put the version in the zplprops
3023de8267e0Stimh 	 */
3024de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3025de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3026da6c28aaSamw 
3027de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3028de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3029de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3030de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3031da6c28aaSamw 
3032c2a93d44Stimh 	/*
3033de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3034c2a93d44Stimh 	 */
3035de8267e0Stimh 	if (norm)
3036de8267e0Stimh 		u8 = 1;
3037de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3038de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3039de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3040de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3041de8267e0Stimh 
3042de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3043de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3044de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3045de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3046c2a93d44Stimh 
3047ab04eb8eStimh 	if (is_ci)
3048ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3049ab04eb8eStimh 
3050da6c28aaSamw 	return (0);
3051fa9e4066Sahrens }
3052fa9e4066Sahrens 
30530a48a24eStimh static int
30540a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
30550a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
30560a48a24eStimh {
30570a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
30580a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
30590a48a24eStimh 	objset_t *os = NULL;
30600a48a24eStimh 	char parentname[MAXNAMELEN];
30610a48a24eStimh 	char *cp;
30620a586ceaSMark Shellenbaum 	spa_t *spa;
30630a586ceaSMark Shellenbaum 	uint64_t spa_vers;
30640a48a24eStimh 	int error;
30650a48a24eStimh 
30660a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
30670a48a24eStimh 	cp = strrchr(parentname, '/');
30680a48a24eStimh 	ASSERT(cp != NULL);
30690a48a24eStimh 	cp[0] = '\0';
30700a48a24eStimh 
30710a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
30720a586ceaSMark Shellenbaum 		return (error);
30730a586ceaSMark Shellenbaum 
30740a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
30750a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
30760a586ceaSMark Shellenbaum 
30770a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
30780a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
30790a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
30800a48a24eStimh 
30810a48a24eStimh 	/*
30820a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
30830a48a24eStimh 	 */
3084503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
30850a48a24eStimh 		return (error);
30860a48a24eStimh 
30870a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
30880a48a24eStimh 	    zplprops, is_ci);
3089503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
30900a48a24eStimh 	return (error);
30910a48a24eStimh }
30920a48a24eStimh 
30930a48a24eStimh static int
30940a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
30950a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
30960a48a24eStimh {
30970a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
30980a586ceaSMark Shellenbaum 	boolean_t sa_ok;
30990a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31000a48a24eStimh 	int error;
31010a48a24eStimh 
31020a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31030a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31040a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31050a48a24eStimh 
31060a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31070a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31080a48a24eStimh 	return (error);
31090a48a24eStimh }
31100a48a24eStimh 
31113cb34c60Sahrens /*
31124445fffbSMatthew Ahrens  * innvl: {
31134445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31144445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31154445fffbSMatthew Ahrens  * }
31163cb34c60Sahrens  *
31174445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31183cb34c60Sahrens  */
3119fa9e4066Sahrens static int
31204445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3121fa9e4066Sahrens {
3122fa9e4066Sahrens 	int error = 0;
31234445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3124ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3125ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31264445fffbSMatthew Ahrens 	int32_t type32;
31274445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31284445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3129fa9e4066Sahrens 
31304445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3131*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31324445fffbSMatthew Ahrens 	type = type32;
31334445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3134fa9e4066Sahrens 
31354445fffbSMatthew Ahrens 	switch (type) {
3136fa9e4066Sahrens 	case DMU_OST_ZFS:
3137fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3138fa9e4066Sahrens 		break;
3139fa9e4066Sahrens 
3140fa9e4066Sahrens 	case DMU_OST_ZVOL:
3141fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3142fa9e4066Sahrens 		break;
3143fa9e4066Sahrens 
3144fa9e4066Sahrens 	default:
31451d452cf5Sahrens 		cbfunc = NULL;
3146e7cbe64fSgw 		break;
3147fa9e4066Sahrens 	}
31484445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
31494445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3150*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3151fa9e4066Sahrens 
3152da6c28aaSamw 	zct.zct_props = nvprops;
3153da6c28aaSamw 
31544445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3155*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31564445fffbSMatthew Ahrens 
31574445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
31584445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
31594445fffbSMatthew Ahrens 
31604445fffbSMatthew Ahrens 		if (nvprops == NULL)
3161*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
31624445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
31634445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3164*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3165fa9e4066Sahrens 
31664445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
31674445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
31684445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3169*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
31704445fffbSMatthew Ahrens 
31714445fffbSMatthew Ahrens 		if (error != 0)
31724445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
31734445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
31744445fffbSMatthew Ahrens 
31754445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
31764445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
31774445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
31784445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3179fa9e4066Sahrens 			return (error);
31804445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
31814445fffbSMatthew Ahrens 		int error;
3182ab04eb8eStimh 
31834445fffbSMatthew Ahrens 		/*
31844445fffbSMatthew Ahrens 		 * We have to have normalization and
31854445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
31864445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
31874445fffbSMatthew Ahrens 		 * now.
31884445fffbSMatthew Ahrens 		 */
31894445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
31904445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
31914445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
31924445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
31934445fffbSMatthew Ahrens 		if (error != 0) {
31944445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3195da6c28aaSamw 			return (error);
3196da6c28aaSamw 		}
31974445fffbSMatthew Ahrens 	}
3198ab04eb8eStimh 
31994445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
32004445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
32014445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
32025c5460e9Seschrock 
32034445fffbSMatthew Ahrens 	/*
32044445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32054445fffbSMatthew Ahrens 	 */
32064445fffbSMatthew Ahrens 	if (error == 0) {
32074445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32084445fffbSMatthew Ahrens 		    nvprops, outnvl);
32094445fffbSMatthew Ahrens 		if (error != 0)
32103b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32114445fffbSMatthew Ahrens 	}
32124445fffbSMatthew Ahrens 	return (error);
32134445fffbSMatthew Ahrens }
3214e9dbad6fSeschrock 
32154445fffbSMatthew Ahrens /*
32164445fffbSMatthew Ahrens  * innvl: {
32174445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32184445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32194445fffbSMatthew Ahrens  * }
32204445fffbSMatthew Ahrens  *
32214445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32224445fffbSMatthew Ahrens  */
32234445fffbSMatthew Ahrens static int
32244445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32254445fffbSMatthew Ahrens {
32264445fffbSMatthew Ahrens 	int error = 0;
32274445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32284445fffbSMatthew Ahrens 	char *origin_name;
3229e9dbad6fSeschrock 
32304445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3231*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32324445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3233e9dbad6fSeschrock 
32344445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32354445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3236*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3237e9dbad6fSeschrock 
32384445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3239*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32403b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
32413b2aab18SMatthew Ahrens 	if (error != 0)
32424445fffbSMatthew Ahrens 		return (error);
3243e9dbad6fSeschrock 
3244e9dbad6fSeschrock 	/*
3245e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3246e9dbad6fSeschrock 	 */
3247e9dbad6fSeschrock 	if (error == 0) {
32484445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32494445fffbSMatthew Ahrens 		    nvprops, outnvl);
325092241e0bSTom Erickson 		if (error != 0)
32513b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3252e9dbad6fSeschrock 	}
3253fa9e4066Sahrens 	return (error);
3254fa9e4066Sahrens }
3255fa9e4066Sahrens 
32563cb34c60Sahrens /*
32574445fffbSMatthew Ahrens  * innvl: {
32584445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
32594445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
32604445fffbSMatthew Ahrens  * }
32614445fffbSMatthew Ahrens  *
32624445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
32633cb34c60Sahrens  */
3264fa9e4066Sahrens static int
32654445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3266fa9e4066Sahrens {
32674445fffbSMatthew Ahrens 	nvlist_t *snaps;
32684445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
32694445fffbSMatthew Ahrens 	int error, poollen;
32704445fffbSMatthew Ahrens 	nvpair_t *pair;
3271bb0ade09Sahrens 
32724445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
32734445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
32744445fffbSMatthew Ahrens 		return (error);
32754445fffbSMatthew Ahrens 
32764445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
32774445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3278*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
32794445fffbSMatthew Ahrens 
32804445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3281*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32824445fffbSMatthew Ahrens 	poollen = strlen(poolname);
32834445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
32844445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
32854445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
32864445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3287bb0ade09Sahrens 
32884445fffbSMatthew Ahrens 		/*
32894445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
32904445fffbSMatthew Ahrens 		 * contain only valid characters.
32914445fffbSMatthew Ahrens 		 */
32924445fffbSMatthew Ahrens 		if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
3293*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3294bb0ade09Sahrens 
32954445fffbSMatthew Ahrens 		/*
32964445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
32974445fffbSMatthew Ahrens 		 */
32984445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
32994445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3300*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
33014445fffbSMatthew Ahrens 
33024445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
33034445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33044445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33054445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33064445fffbSMatthew Ahrens 			    == 0) {
3307*be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33084445fffbSMatthew Ahrens 			}
33094445fffbSMatthew Ahrens 		}
33104445fffbSMatthew Ahrens 	}
3311bb0ade09Sahrens 
33123b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33134445fffbSMatthew Ahrens 	return (error);
33144445fffbSMatthew Ahrens }
33154445fffbSMatthew Ahrens 
33164445fffbSMatthew Ahrens /*
33174445fffbSMatthew Ahrens  * innvl: "message" -> string
33184445fffbSMatthew Ahrens  */
33194445fffbSMatthew Ahrens /* ARGSUSED */
33204445fffbSMatthew Ahrens static int
33214445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
33224445fffbSMatthew Ahrens {
33234445fffbSMatthew Ahrens 	char *message;
33244445fffbSMatthew Ahrens 	spa_t *spa;
33254445fffbSMatthew Ahrens 	int error;
33264445fffbSMatthew Ahrens 	char *poolname;
33274445fffbSMatthew Ahrens 
33284445fffbSMatthew Ahrens 	/*
33294445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
33304445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
33314445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
33324445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
33334445fffbSMatthew Ahrens 	 * we clear the TSD here.
33344445fffbSMatthew Ahrens 	 */
33354445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
33364445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
33374445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
33384445fffbSMatthew Ahrens 	strfree(poolname);
33394445fffbSMatthew Ahrens 	if (error != 0)
33404445fffbSMatthew Ahrens 		return (error);
33414445fffbSMatthew Ahrens 
33424445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
33434445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3344*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3345bb0ade09Sahrens 	}
3346ea2f5b9eSMatthew Ahrens 
33474445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
33484445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3349*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33504445fffbSMatthew Ahrens 	}
3351ea2f5b9eSMatthew Ahrens 
33524445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
33534445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3354bb0ade09Sahrens 	return (error);
33551d452cf5Sahrens }
3356fa9e4066Sahrens 
33573b2aab18SMatthew Ahrens /*
33583b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
33593b2aab18SMatthew Ahrens  * unmount may need to write out data.
33603b2aab18SMatthew Ahrens  *
33613b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
33623b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
33633b2aab18SMatthew Ahrens  */
33643b2aab18SMatthew Ahrens void
33653b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
33661d452cf5Sahrens {
33674445fffbSMatthew Ahrens 	vfs_t *vfsp;
33683b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
33691d452cf5Sahrens 
33703b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
33713b2aab18SMatthew Ahrens 		return;
33721d452cf5Sahrens 
33733b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
33744445fffbSMatthew Ahrens 	if (vfsp == NULL)
33753b2aab18SMatthew Ahrens 		return;
33763b2aab18SMatthew Ahrens 
33773b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
33783b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
33791d452cf5Sahrens 
33803b2aab18SMatthew Ahrens 	if (vn_vfswlock(vfsp->vfs_vnodecovered) != 0) {
33811d452cf5Sahrens 		VFS_RELE(vfsp);
33823b2aab18SMatthew Ahrens 		return;
33831d452cf5Sahrens 	}
33844445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
33854445fffbSMatthew Ahrens 
33864445fffbSMatthew Ahrens 	/*
33874445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
33884445fffbSMatthew Ahrens 	 */
33893b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
33903b2aab18SMatthew Ahrens }
33913b2aab18SMatthew Ahrens 
33923b2aab18SMatthew Ahrens /* ARGSUSED */
33933b2aab18SMatthew Ahrens static int
33943b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
33953b2aab18SMatthew Ahrens {
33963b2aab18SMatthew Ahrens 	zfs_unmount_snap(snapname);
33973b2aab18SMatthew Ahrens 	return (0);
33983b2aab18SMatthew Ahrens }
33993b2aab18SMatthew Ahrens 
34003b2aab18SMatthew Ahrens /*
34013b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34023b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34033b2aab18SMatthew Ahrens  * if necessary.
34043b2aab18SMatthew Ahrens  */
34053b2aab18SMatthew Ahrens void
34063b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34073b2aab18SMatthew Ahrens {
34083b2aab18SMatthew Ahrens 	int error;
34093b2aab18SMatthew Ahrens 	objset_t *os;
34103b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34113b2aab18SMatthew Ahrens 
34123b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
34133b2aab18SMatthew Ahrens 	if (error != 0)
34143b2aab18SMatthew Ahrens 		return;
34153b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
34163b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
34173b2aab18SMatthew Ahrens 		char originname[MAXNAMELEN];
34183b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
34193b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34203b2aab18SMatthew Ahrens 		zfs_unmount_snap(originname);
34213b2aab18SMatthew Ahrens 	} else {
34223b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34233b2aab18SMatthew Ahrens 	}
34241d452cf5Sahrens }
34251d452cf5Sahrens 
34263cb34c60Sahrens /*
34274445fffbSMatthew Ahrens  * innvl: {
34284445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34294445fffbSMatthew Ahrens  *     (optional boolean) "defer"
34304445fffbSMatthew Ahrens  * }
34314445fffbSMatthew Ahrens  *
34324445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34333cb34c60Sahrens  *
34343cb34c60Sahrens  */
34351d452cf5Sahrens static int
34364445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34371d452cf5Sahrens {
34384445fffbSMatthew Ahrens 	int poollen;
34394445fffbSMatthew Ahrens 	nvlist_t *snaps;
344019b94df9SMatthew Ahrens 	nvpair_t *pair;
34414445fffbSMatthew Ahrens 	boolean_t defer;
34421d452cf5Sahrens 
34434445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3444*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34454445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
344619b94df9SMatthew Ahrens 
34474445fffbSMatthew Ahrens 	poollen = strlen(poolname);
34484445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
34494445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
345019b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
34514445fffbSMatthew Ahrens 
345219b94df9SMatthew Ahrens 		/*
34534445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
345419b94df9SMatthew Ahrens 		 */
34554445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
34564445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3457*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
345819b94df9SMatthew Ahrens 
34593b2aab18SMatthew Ahrens 		zfs_unmount_snap(name);
346019b94df9SMatthew Ahrens 	}
346119b94df9SMatthew Ahrens 
34623b2aab18SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
34631d452cf5Sahrens }
34641d452cf5Sahrens 
34653cb34c60Sahrens /*
34663cb34c60Sahrens  * inputs:
34673cb34c60Sahrens  * zc_name		name of dataset to destroy
34683cb34c60Sahrens  * zc_objset_type	type of objset
3469842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
34703cb34c60Sahrens  *
34713cb34c60Sahrens  * outputs:		none
34723cb34c60Sahrens  */
34731d452cf5Sahrens static int
34741d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
34751d452cf5Sahrens {
3476681d9761SEric Taylor 	int err;
34773b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS)
34783b2aab18SMatthew Ahrens 		zfs_unmount_snap(zc->zc_name);
3479fa9e4066Sahrens 
34803b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
34813b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
34823b2aab18SMatthew Ahrens 	else
34833b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3484681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
34855c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3486681d9761SEric Taylor 	return (err);
3487fa9e4066Sahrens }
3488fa9e4066Sahrens 
34893cb34c60Sahrens /*
34903cb34c60Sahrens  * inputs:
34914ccbb6e7Sahrens  * zc_name	name of dataset to rollback (to most recent snapshot)
34923cb34c60Sahrens  *
34933cb34c60Sahrens  * outputs:	none
34943cb34c60Sahrens  */
3495fa9e4066Sahrens static int
3496fa9e4066Sahrens zfs_ioc_rollback(zfs_cmd_t *zc)
3497fa9e4066Sahrens {
3498ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
34993b2aab18SMatthew Ahrens 	int error;
3500ae46e4c7SMatthew Ahrens 
350114843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3502503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
350347f263f4Sek 		if (error == 0) {
350447f263f4Sek 			int resume_err;
35054ccbb6e7Sahrens 
35063b2aab18SMatthew Ahrens 			error = dsl_dataset_rollback(zc->zc_name);
3507503ad85cSMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
350847f263f4Sek 			error = error ? error : resume_err;
350947f263f4Sek 		}
35104ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
35114ccbb6e7Sahrens 	} else {
35123b2aab18SMatthew Ahrens 		error = dsl_dataset_rollback(zc->zc_name);
35134ccbb6e7Sahrens 	}
35143b2aab18SMatthew Ahrens 	return (error);
35153b2aab18SMatthew Ahrens }
35164ccbb6e7Sahrens 
35173b2aab18SMatthew Ahrens static int
35183b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
35193b2aab18SMatthew Ahrens {
35203b2aab18SMatthew Ahrens 	const char *snapname = arg;
35213b2aab18SMatthew Ahrens 	char fullname[MAXNAMELEN];
3522ae46e4c7SMatthew Ahrens 
35233b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
35243b2aab18SMatthew Ahrens 	zfs_unmount_snap(fullname);
35253b2aab18SMatthew Ahrens 	return (0);
3526fa9e4066Sahrens }
3527fa9e4066Sahrens 
35283cb34c60Sahrens /*
35293cb34c60Sahrens  * inputs:
35303cb34c60Sahrens  * zc_name	old name of dataset
35313cb34c60Sahrens  * zc_value	new name of dataset
35323cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
35333cb34c60Sahrens  *
35343cb34c60Sahrens  * outputs:	none
35353cb34c60Sahrens  */
3536fa9e4066Sahrens static int
3537fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3538fa9e4066Sahrens {
35397f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
35403b2aab18SMatthew Ahrens 	char *at;
3541cdf5b4caSmmusante 
3542e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3543f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3544f18faf3fSek 	    strchr(zc->zc_value, '%'))
3545*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3546fa9e4066Sahrens 
35473b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
35483b2aab18SMatthew Ahrens 	if (at != NULL) {
35493b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
35503b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3551*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
35523b2aab18SMatthew Ahrens 		*at = '\0';
35533b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
35543b2aab18SMatthew Ahrens 			int error = dmu_objset_find(zc->zc_name,
35553b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
35563b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
35573b2aab18SMatthew Ahrens 			if (error != 0)
35583b2aab18SMatthew Ahrens 				return (error);
35593b2aab18SMatthew Ahrens 		}
35603b2aab18SMatthew Ahrens 		return (dsl_dataset_rename_snapshot(zc->zc_name,
35613b2aab18SMatthew Ahrens 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive));
35623b2aab18SMatthew Ahrens 	} else {
35633b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZVOL)
35643b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
35653b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3566fa9e4066Sahrens 	}
3567fa9e4066Sahrens }
3568fa9e4066Sahrens 
356992241e0bSTom Erickson static int
357092241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
357192241e0bSTom Erickson {
357292241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
357392241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
357492241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
357592241e0bSTom Erickson 	uint64_t intval;
357692241e0bSTom Erickson 	int err;
357792241e0bSTom Erickson 
357892241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
357992241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
358092241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
358192241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
358292241e0bSTom Erickson 				return (err);
358392241e0bSTom Erickson 			return (0);
358492241e0bSTom Erickson 		}
358592241e0bSTom Erickson 
358692241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
358792241e0bSTom Erickson 			const char *perm = NULL;
358892241e0bSTom Erickson 			const char *uq_prefix =
358992241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
359092241e0bSTom Erickson 			const char *gq_prefix =
359192241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
359292241e0bSTom Erickson 
359392241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
359492241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
359592241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
359692241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
359792241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
359892241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
359992241e0bSTom Erickson 			} else {
360092241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3601*be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
360292241e0bSTom Erickson 			}
360392241e0bSTom Erickson 
360492241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
360592241e0bSTom Erickson 				return (err);
360692241e0bSTom Erickson 			return (0);
360792241e0bSTom Erickson 		}
360892241e0bSTom Erickson 
3609*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
361092241e0bSTom Erickson 	}
361192241e0bSTom Erickson 
361292241e0bSTom Erickson 	if (issnap)
3613*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
361492241e0bSTom Erickson 
361592241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
361692241e0bSTom Erickson 		/*
361792241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
361892241e0bSTom Erickson 		 * format.
361992241e0bSTom Erickson 		 */
362092241e0bSTom Erickson 		nvlist_t *attrs;
362192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
362292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
362392241e0bSTom Erickson 		    &pair) == 0);
362492241e0bSTom Erickson 	}
362592241e0bSTom Erickson 
362692241e0bSTom Erickson 	/*
362792241e0bSTom Erickson 	 * Check that this value is valid for this pool version
362892241e0bSTom Erickson 	 */
362992241e0bSTom Erickson 	switch (prop) {
363092241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
363192241e0bSTom Erickson 		/*
363292241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
363392241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
363492241e0bSTom Erickson 		 * we'll catch them later.
363592241e0bSTom Erickson 		 */
363692241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
363792241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
363892241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
363992241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
364092241e0bSTom Erickson 			    zfs_earlier_version(dsname,
364192241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3642*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
364392241e0bSTom Erickson 			}
364492241e0bSTom Erickson 
364592241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
364692241e0bSTom Erickson 			    zfs_earlier_version(dsname,
364792241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3648*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
364992241e0bSTom Erickson 
3650a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3651a6f561b4SSašo Kiselkov 				zfeature_info_t *feature =
3652a6f561b4SSašo Kiselkov 				    &spa_feature_table[
3653a6f561b4SSašo Kiselkov 				    SPA_FEATURE_LZ4_COMPRESS];
3654a6f561b4SSašo Kiselkov 				spa_t *spa;
3655a6f561b4SSašo Kiselkov 
3656a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3657a6f561b4SSašo Kiselkov 					return (err);
3658a6f561b4SSašo Kiselkov 
3659a6f561b4SSašo Kiselkov 				if (!spa_feature_is_enabled(spa, feature)) {
3660a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3661*be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3662a6f561b4SSašo Kiselkov 				}
3663a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3664a6f561b4SSašo Kiselkov 			}
3665a6f561b4SSašo Kiselkov 
366692241e0bSTom Erickson 			/*
366792241e0bSTom Erickson 			 * If this is a bootable dataset then
366892241e0bSTom Erickson 			 * verify that the compression algorithm
366992241e0bSTom Erickson 			 * is supported for booting. We must return
367092241e0bSTom Erickson 			 * something other than ENOTSUP since it
367192241e0bSTom Erickson 			 * implies a downrev pool version.
367292241e0bSTom Erickson 			 */
367392241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
367492241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3675*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
367692241e0bSTom Erickson 			}
367792241e0bSTom Erickson 		}
367892241e0bSTom Erickson 		break;
367992241e0bSTom Erickson 
368092241e0bSTom Erickson 	case ZFS_PROP_COPIES:
368192241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3682*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
368392241e0bSTom Erickson 		break;
368492241e0bSTom Erickson 
368592241e0bSTom Erickson 	case ZFS_PROP_DEDUP:
368692241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3687*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
368892241e0bSTom Erickson 		break;
368992241e0bSTom Erickson 
369092241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
369192241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3692*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
369392241e0bSTom Erickson 		break;
369492241e0bSTom Erickson 
369592241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
369692241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
369792241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
369892241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
369992241e0bSTom Erickson 			    zfs_earlier_version(dsname,
370092241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3701*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
370292241e0bSTom Erickson 		}
370392241e0bSTom Erickson 		break;
370492241e0bSTom Erickson 	}
370592241e0bSTom Erickson 
370692241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
370792241e0bSTom Erickson }
370892241e0bSTom Erickson 
3709a6f561b4SSašo Kiselkov /*
3710a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3711a6f561b4SSašo Kiselkov  * multiple times.
3712a6f561b4SSašo Kiselkov  */
3713a6f561b4SSašo Kiselkov static int
37143b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3715a6f561b4SSašo Kiselkov {
37163b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
37173b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
3718a6f561b4SSašo Kiselkov 
3719a6f561b4SSašo Kiselkov 	if (!spa_feature_is_active(spa, feature))
3720a6f561b4SSašo Kiselkov 		return (0);
3721a6f561b4SSašo Kiselkov 	else
3722*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3723a6f561b4SSašo Kiselkov }
3724a6f561b4SSašo Kiselkov 
3725a6f561b4SSašo Kiselkov /*
3726a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3727a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3728a6f561b4SSašo Kiselkov  */
3729a6f561b4SSašo Kiselkov static void
37303b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3731a6f561b4SSašo Kiselkov {
37323b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
37333b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
3734a6f561b4SSašo Kiselkov 
3735a6f561b4SSašo Kiselkov 	spa_feature_incr(spa, feature, tx);
3736a6f561b4SSašo Kiselkov }
3737a6f561b4SSašo Kiselkov 
37383b2aab18SMatthew Ahrens /*
37393b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
37403b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
37413b2aab18SMatthew Ahrens  * as being active.
37423b2aab18SMatthew Ahrens  */
37433b2aab18SMatthew Ahrens static int
37443b2aab18SMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature)
37453b2aab18SMatthew Ahrens {
37463b2aab18SMatthew Ahrens 	int err;
37473b2aab18SMatthew Ahrens 
37483b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
37493b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
37503b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
37513b2aab18SMatthew Ahrens 	    feature, 2);
37523b2aab18SMatthew Ahrens 
37533b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
37543b2aab18SMatthew Ahrens 		return (err);
37553b2aab18SMatthew Ahrens 	else
37563b2aab18SMatthew Ahrens 		return (0);
37573b2aab18SMatthew Ahrens }
37583b2aab18SMatthew Ahrens 
375992241e0bSTom Erickson /*
376092241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
376192241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
376292241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
376392241e0bSTom Erickson  *
376492241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
376592241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
376692241e0bSTom Erickson  * of all the properties that failed a permission check along with the
376792241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
376892241e0bSTom Erickson  * returned errlist.
376992241e0bSTom Erickson  *
377092241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
377192241e0bSTom Erickson  * pointed at by errlist is NULL.
377292241e0bSTom Erickson  */
377392241e0bSTom Erickson static int
377492241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3775745cd3c5Smaybee {
3776745cd3c5Smaybee 	zfs_cmd_t *zc;
377792241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
377892241e0bSTom Erickson 	nvlist_t *errors;
377992241e0bSTom Erickson 	int err, rv = 0;
3780745cd3c5Smaybee 
3781745cd3c5Smaybee 	if (props == NULL)
378292241e0bSTom Erickson 		return (0);
378392241e0bSTom Erickson 
378492241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
378592241e0bSTom Erickson 
3786745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3787745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
378892241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
378992241e0bSTom Erickson 	while (pair != NULL) {
379092241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
379192241e0bSTom Erickson 
379292241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
379392241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
37944445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
379592241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
379692241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
379792241e0bSTom Erickson 			    zc->zc_value, err) == 0);
379892241e0bSTom Erickson 		}
379992241e0bSTom Erickson 		pair = next_pair;
3800745cd3c5Smaybee 	}
3801745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
380292241e0bSTom Erickson 
380392241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
380492241e0bSTom Erickson 		nvlist_free(errors);
380592241e0bSTom Erickson 		errors = NULL;
380692241e0bSTom Erickson 	} else {
380792241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
380892241e0bSTom Erickson 	}
380992241e0bSTom Erickson 
381092241e0bSTom Erickson 	if (errlist == NULL)
381192241e0bSTom Erickson 		nvlist_free(errors);
381292241e0bSTom Erickson 	else
381392241e0bSTom Erickson 		*errlist = errors;
381492241e0bSTom Erickson 
381592241e0bSTom Erickson 	return (rv);
381692241e0bSTom Erickson }
381792241e0bSTom Erickson 
381892241e0bSTom Erickson static boolean_t
381992241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
382092241e0bSTom Erickson {
382192241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
382292241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
382392241e0bSTom Erickson 		nvlist_t *attrs;
382492241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
382592241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
382692241e0bSTom Erickson 		    &p1) == 0);
382792241e0bSTom Erickson 	}
382892241e0bSTom Erickson 
382992241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
383092241e0bSTom Erickson 		nvlist_t *attrs;
383192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
383292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
383392241e0bSTom Erickson 		    &p2) == 0);
383492241e0bSTom Erickson 	}
383592241e0bSTom Erickson 
383692241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
383792241e0bSTom Erickson 		return (B_FALSE);
383892241e0bSTom Erickson 
383992241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
384092241e0bSTom Erickson 		char *valstr1, *valstr2;
384192241e0bSTom Erickson 
384292241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
384392241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
384492241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
384592241e0bSTom Erickson 	} else {
384692241e0bSTom Erickson 		uint64_t intval1, intval2;
384792241e0bSTom Erickson 
384892241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
384992241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
385092241e0bSTom Erickson 		return (intval1 == intval2);
385192241e0bSTom Erickson 	}
3852745cd3c5Smaybee }
3853745cd3c5Smaybee 
385492241e0bSTom Erickson /*
385592241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
385692241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
385792241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
385892241e0bSTom Erickson  */
385992241e0bSTom Erickson static void
386092241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
386192241e0bSTom Erickson {
386292241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
386392241e0bSTom Erickson 
386492241e0bSTom Erickson 	if (origprops == NULL)
386592241e0bSTom Erickson 		return; /* all props need to be received */
386692241e0bSTom Erickson 
386792241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
386892241e0bSTom Erickson 	while (pair != NULL) {
386992241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
387092241e0bSTom Erickson 		nvpair_t *match;
387192241e0bSTom Erickson 
387292241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
387392241e0bSTom Erickson 
387492241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
387592241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
387692241e0bSTom Erickson 			goto next; /* need to set received value */
387792241e0bSTom Erickson 
387892241e0bSTom Erickson 		/* don't clear the existing received value */
387992241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
388092241e0bSTom Erickson 		/* don't bother receiving the property */
388192241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
388292241e0bSTom Erickson next:
388392241e0bSTom Erickson 		pair = next_pair;
388492241e0bSTom Erickson 	}
388592241e0bSTom Erickson }
388692241e0bSTom Erickson 
388792241e0bSTom Erickson #ifdef	DEBUG
388892241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
388992241e0bSTom Erickson #endif
389092241e0bSTom Erickson 
38913cb34c60Sahrens /*
38923cb34c60Sahrens  * inputs:
38933cb34c60Sahrens  * zc_name		name of containing filesystem
38943cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
38953cb34c60Sahrens  * zc_value		name of snapshot to create
38963cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
38973cb34c60Sahrens  * zc_cookie		file descriptor to recv from
38983cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
38993cb34c60Sahrens  * zc_guid		force flag
3900c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
3901c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
39023cb34c60Sahrens  *
39033cb34c60Sahrens  * outputs:
39043cb34c60Sahrens  * zc_cookie		number of bytes read
390592241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
390692241e0bSTom Erickson  * zc_obj		zprop_errflags_t
3907c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
39083cb34c60Sahrens  */
3909fa9e4066Sahrens static int
39103cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
3911fa9e4066Sahrens {
3912fa9e4066Sahrens 	file_t *fp;
39133cb34c60Sahrens 	dmu_recv_cookie_t drc;
3914f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
391592241e0bSTom Erickson 	int fd;
391692241e0bSTom Erickson 	int error = 0;
391792241e0bSTom Erickson 	int props_error = 0;
391892241e0bSTom Erickson 	nvlist_t *errors;
39193cb34c60Sahrens 	offset_t off;
392092241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
392192241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
39223b2aab18SMatthew Ahrens 	char *origin = NULL;
39233cb34c60Sahrens 	char *tosnap;
39243cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
392592241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
3926fa9e4066Sahrens 
39273ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3928f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
3929f18faf3fSek 	    strchr(zc->zc_value, '%'))
3930*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
39313ccfa83cSahrens 
39323cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
39333cb34c60Sahrens 	tosnap = strchr(tofs, '@');
393492241e0bSTom Erickson 	*tosnap++ = '\0';
39353cb34c60Sahrens 
39363cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
39373cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3938478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
39393cb34c60Sahrens 		return (error);
39403cb34c60Sahrens 
3941fa9e4066Sahrens 	fd = zc->zc_cookie;
3942fa9e4066Sahrens 	fp = getf(fd);
39433cb34c60Sahrens 	if (fp == NULL) {
39443cb34c60Sahrens 		nvlist_free(props);
3945*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
39463cb34c60Sahrens 	}
3947f18faf3fSek 
394892241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
394992241e0bSTom Erickson 
39503b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
39513b2aab18SMatthew Ahrens 		origin = zc->zc_string;
39523b2aab18SMatthew Ahrens 
39533b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
39543b2aab18SMatthew Ahrens 	    &zc->zc_begin_record, force, origin, &drc);
39553b2aab18SMatthew Ahrens 	if (error != 0)
39563b2aab18SMatthew Ahrens 		goto out;
39573b2aab18SMatthew Ahrens 
39583b2aab18SMatthew Ahrens 	/*
39593b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
39603b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
39613b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
39623b2aab18SMatthew Ahrens 	 */
39633b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
39643b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
39653b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
39663b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
396792241e0bSTom Erickson 			first_recvd_props = B_TRUE;
396892241e0bSTom Erickson 
3969745cd3c5Smaybee 		/*
397092241e0bSTom Erickson 		 * If new received properties are supplied, they are to
397192241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
397292241e0bSTom Erickson 		 * away the existing ones.
3973745cd3c5Smaybee 		 */
39743b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
397592241e0bSTom Erickson 			nvlist_t *errlist = NULL;
397692241e0bSTom Erickson 			/*
397792241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
397892241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
397992241e0bSTom Erickson 			 *
398092241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
398192241e0bSTom Erickson 			 * special case where we blow away all local properties
398292241e0bSTom Erickson 			 * regardless.
398392241e0bSTom Erickson 			 */
398492241e0bSTom Erickson 			if (!first_recvd_props)
398592241e0bSTom Erickson 				props_reduce(props, origprops);
39863b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
398792241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
398892241e0bSTom Erickson 			nvlist_free(errlist);
39893cb34c60Sahrens 
39903b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
39913b2aab18SMatthew Ahrens 			    first_recvd_props ? NULL : props) != 0)
399292241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
39933b2aab18SMatthew Ahrens 		} else {
399492241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
399592241e0bSTom Erickson 		}
39963b2aab18SMatthew Ahrens 	}
399792241e0bSTom Erickson 
39983b2aab18SMatthew Ahrens 	if (props != NULL) {
39993b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
40003b2aab18SMatthew Ahrens 
40013b2aab18SMatthew Ahrens 		if (props_error == 0) {
40023b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
40033b2aab18SMatthew Ahrens 			    props, errors);
40043b2aab18SMatthew Ahrens 		}
400592241e0bSTom Erickson 	}
400692241e0bSTom Erickson 
40074445fffbSMatthew Ahrens 	if (zc->zc_nvlist_dst_size != 0 &&
40084445fffbSMatthew Ahrens 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
40094445fffbSMatthew Ahrens 	    put_nvlist(zc, errors) != 0)) {
4010745cd3c5Smaybee 		/*
401192241e0bSTom Erickson 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
401292241e0bSTom Erickson 		 * size or supplied an invalid address.
4013745cd3c5Smaybee 		 */
4014*be6fd75aSMatthew Ahrens 		props_error = SET_ERROR(EINVAL);
40153cb34c60Sahrens 	}
40163cb34c60Sahrens 
40173cb34c60Sahrens 	off = fp->f_offset;
4018c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4019c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
4020a2eea2e1Sahrens 
4021f4b94bdeSMatthew Ahrens 	if (error == 0) {
4022f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4023745cd3c5Smaybee 
4024f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4025f4b94bdeSMatthew Ahrens 			/* online recv */
4026f4b94bdeSMatthew Ahrens 			int end_err;
4027745cd3c5Smaybee 
4028503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4029f4b94bdeSMatthew Ahrens 			/*
4030f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4031f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4032f4b94bdeSMatthew Ahrens 			 */
4033f4b94bdeSMatthew Ahrens 			end_err = dmu_recv_end(&drc);
40345c703fceSGeorge Wilson 			if (error == 0)
40355c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4036f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4037f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4038745cd3c5Smaybee 		} else {
4039f4b94bdeSMatthew Ahrens 			error = dmu_recv_end(&drc);
40403cb34c60Sahrens 		}
404147f263f4Sek 	}
40423cb34c60Sahrens 
40433cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
40443cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
40453cb34c60Sahrens 		fp->f_offset = off;
4046a2eea2e1Sahrens 
404792241e0bSTom Erickson #ifdef	DEBUG
404892241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
404992241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
405092241e0bSTom Erickson 		error = 1;
405192241e0bSTom Erickson 	}
405292241e0bSTom Erickson #endif
4053745cd3c5Smaybee 	/*
4054745cd3c5Smaybee 	 * On error, restore the original props.
4055745cd3c5Smaybee 	 */
40563b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
40573b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
40583b2aab18SMatthew Ahrens 			/*
40593b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
40603b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
40613b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
40623b2aab18SMatthew Ahrens 			 */
406392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
40643b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
40653b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
406692241e0bSTom Erickson 		}
406792241e0bSTom Erickson 
406892241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
406992241e0bSTom Erickson 			/* We failed to stash the original properties. */
407092241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
407192241e0bSTom Erickson 		}
407292241e0bSTom Erickson 
407392241e0bSTom Erickson 		/*
407492241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
407592241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
407692241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
407792241e0bSTom Erickson 		 * first new-style receive.
407892241e0bSTom Erickson 		 */
407992241e0bSTom Erickson 		if (origprops != NULL &&
408092241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
408192241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
408292241e0bSTom Erickson 		    origprops, NULL) != 0) {
408392241e0bSTom Erickson 			/*
408492241e0bSTom Erickson 			 * We stashed the original properties but failed to
408592241e0bSTom Erickson 			 * restore them.
408692241e0bSTom Erickson 			 */
408792241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
408892241e0bSTom Erickson 		}
4089745cd3c5Smaybee 	}
4090745cd3c5Smaybee out:
4091745cd3c5Smaybee 	nvlist_free(props);
4092745cd3c5Smaybee 	nvlist_free(origprops);
409392241e0bSTom Erickson 	nvlist_free(errors);
4094fa9e4066Sahrens 	releasef(fd);
409592241e0bSTom Erickson 
409692241e0bSTom Erickson 	if (error == 0)
409792241e0bSTom Erickson 		error = props_error;
409892241e0bSTom Erickson 
4099fa9e4066Sahrens 	return (error);
4100fa9e4066Sahrens }
4101fa9e4066Sahrens 
41023cb34c60Sahrens /*
41033cb34c60Sahrens  * inputs:
41043cb34c60Sahrens  * zc_name	name of snapshot to send
41053cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4106a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4107a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4108a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
410919b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
411019b94df9SMatthew Ahrens  *		output size in zc_objset_type.
41113cb34c60Sahrens  *
41123cb34c60Sahrens  * outputs: none
41133cb34c60Sahrens  */
4114fa9e4066Sahrens static int
41153cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4116fa9e4066Sahrens {
4117fa9e4066Sahrens 	int error;
41183cb34c60Sahrens 	offset_t off;
411919b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
4120fa9e4066Sahrens 
41213b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
41223b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
41233b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4124a7f53a56SChris Kirby 
41253b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
41263b2aab18SMatthew Ahrens 		if (error != 0)
4127a7f53a56SChris Kirby 			return (error);
41283b2aab18SMatthew Ahrens 
41293b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
41303b2aab18SMatthew Ahrens 		if (error != 0) {
41313b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4132fa9e4066Sahrens 			return (error);
4133fa9e4066Sahrens 		}
41343b2aab18SMatthew Ahrens 
41353b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
41363b2aab18SMatthew Ahrens 			zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
41373b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
41383b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
41394445fffbSMatthew Ahrens 	}
41404445fffbSMatthew Ahrens 
41413b2aab18SMatthew Ahrens 	if (estimate) {
41423b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
41433b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
41443b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
41454445fffbSMatthew Ahrens 
41463b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
41473b2aab18SMatthew Ahrens 		if (error != 0)
41483b2aab18SMatthew Ahrens 			return (error);
41493b2aab18SMatthew Ahrens 
41503b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
41513b2aab18SMatthew Ahrens 		if (error != 0) {
41523b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
41533b2aab18SMatthew Ahrens 			return (error);
41544445fffbSMatthew Ahrens 		}
41554445fffbSMatthew Ahrens 
41563b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
41573b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
41583b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
41593b2aab18SMatthew Ahrens 			if (error != 0) {
41603b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
41613b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
41624445fffbSMatthew Ahrens 				return (error);
41634445fffbSMatthew Ahrens 			}
41644445fffbSMatthew Ahrens 		}
4165fa9e4066Sahrens 
41664445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
416719b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
41683b2aab18SMatthew Ahrens 
41693b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
41703b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
41713b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
41723b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
417319b94df9SMatthew Ahrens 	} else {
417419b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
41753b2aab18SMatthew Ahrens 		if (fp == NULL)
4176*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4177fa9e4066Sahrens 
417819b94df9SMatthew Ahrens 		off = fp->f_offset;
41793b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
41803b2aab18SMatthew Ahrens 		    zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
4181fa9e4066Sahrens 
418219b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
418319b94df9SMatthew Ahrens 			fp->f_offset = off;
418419b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
418519b94df9SMatthew Ahrens 	}
4186fa9e4066Sahrens 	return (error);
4187fa9e4066Sahrens }
4188fa9e4066Sahrens 
41894e3c9f44SBill Pijewski /*
41904e3c9f44SBill Pijewski  * inputs:
41914e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
41924e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
41934e3c9f44SBill Pijewski  *
41944e3c9f44SBill Pijewski  * outputs:
41954e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
41964e3c9f44SBill Pijewski  */
41974e3c9f44SBill Pijewski static int
41984e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
41994e3c9f44SBill Pijewski {
42003b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
42014e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
42024e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
42034e3c9f44SBill Pijewski 	int error;
42044e3c9f44SBill Pijewski 
42053b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
42063b2aab18SMatthew Ahrens 	if (error != 0)
42073b2aab18SMatthew Ahrens 		return (error);
42083b2aab18SMatthew Ahrens 
42093b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
42103b2aab18SMatthew Ahrens 	if (error != 0) {
42113b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
42124e3c9f44SBill Pijewski 		return (error);
42133b2aab18SMatthew Ahrens 	}
42144e3c9f44SBill Pijewski 
42154e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
42164e3c9f44SBill Pijewski 
42174e3c9f44SBill Pijewski 	/*
42184e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
42194e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
42204e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
42214e3c9f44SBill Pijewski 	 * that stream.
42224e3c9f44SBill Pijewski 	 */
42234e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
42244e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
42254e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
42264e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
42274e3c9f44SBill Pijewski 			break;
42284e3c9f44SBill Pijewski 	}
42294e3c9f44SBill Pijewski 
42304e3c9f44SBill Pijewski 	if (dsp != NULL)
42314e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
42324e3c9f44SBill Pijewski 	else
4233*be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
42344e3c9f44SBill Pijewski 
42354e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
42364e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
42373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
42384e3c9f44SBill Pijewski 	return (error);
42394e3c9f44SBill Pijewski }
42404e3c9f44SBill Pijewski 
4241ea8dc4b6Seschrock static int
4242ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4243ea8dc4b6Seschrock {
4244ea8dc4b6Seschrock 	int id, error;
4245ea8dc4b6Seschrock 
4246ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4247ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4248ea8dc4b6Seschrock 
4249ea8dc4b6Seschrock 	if (error == 0)
4250ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4251ea8dc4b6Seschrock 
4252ea8dc4b6Seschrock 	return (error);
4253ea8dc4b6Seschrock }
4254ea8dc4b6Seschrock 
4255ea8dc4b6Seschrock static int
4256ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4257ea8dc4b6Seschrock {
4258ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4259ea8dc4b6Seschrock }
4260ea8dc4b6Seschrock 
4261ea8dc4b6Seschrock static int
4262ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4263ea8dc4b6Seschrock {
4264ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4265ea8dc4b6Seschrock 	int error;
4266ea8dc4b6Seschrock 
4267ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4268ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4269ea8dc4b6Seschrock 
4270ea8dc4b6Seschrock 	zc->zc_guid = id;
4271ea8dc4b6Seschrock 
4272ea8dc4b6Seschrock 	return (error);
4273ea8dc4b6Seschrock }
4274ea8dc4b6Seschrock 
4275ea8dc4b6Seschrock static int
4276ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4277ea8dc4b6Seschrock {
4278ea8dc4b6Seschrock 	spa_t *spa;
4279ea8dc4b6Seschrock 	int error;
4280e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4281ea8dc4b6Seschrock 
4282ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4283ea8dc4b6Seschrock 		return (error);
4284ea8dc4b6Seschrock 
4285e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4286ea8dc4b6Seschrock 	    &count);
4287ea8dc4b6Seschrock 	if (error == 0)
4288e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4289ea8dc4b6Seschrock 	else
4290e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4291ea8dc4b6Seschrock 
4292ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4293ea8dc4b6Seschrock 
4294ea8dc4b6Seschrock 	return (error);
4295ea8dc4b6Seschrock }
4296ea8dc4b6Seschrock 
4297ea8dc4b6Seschrock static int
4298ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4299ea8dc4b6Seschrock {
4300ea8dc4b6Seschrock 	spa_t *spa;
4301ea8dc4b6Seschrock 	vdev_t *vd;
4302bb8b5132Sek 	int error;
4303ea8dc4b6Seschrock 
4304b87f3af3Sperrin 	/*
4305b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4306b87f3af3Sperrin 	 */
4307b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4308b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4309b87f3af3Sperrin 	if (spa == NULL) {
4310b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4311*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4312b87f3af3Sperrin 	}
4313b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4314b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4315b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4316b87f3af3Sperrin 	}
4317468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4318b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4319b87f3af3Sperrin 
4320c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4321468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4322468c413aSTim Haley 	} else {
4323468c413aSTim Haley 		nvlist_t *policy;
4324468c413aSTim Haley 		nvlist_t *config = NULL;
4325468c413aSTim Haley 
4326468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4327*be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4328468c413aSTim Haley 
4329468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4330468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4331468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4332468c413aSTim Haley 			    policy, &config);
4333468c413aSTim Haley 			if (config != NULL) {
43344b964adaSGeorge Wilson 				int err;
43354b964adaSGeorge Wilson 
43364b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
43374b964adaSGeorge Wilson 					error = err;
4338468c413aSTim Haley 				nvlist_free(config);
4339468c413aSTim Haley 			}
4340468c413aSTim Haley 			nvlist_free(policy);
4341468c413aSTim Haley 		}
4342468c413aSTim Haley 	}
4343468c413aSTim Haley 
43443b2aab18SMatthew Ahrens 	if (error != 0)
4345ea8dc4b6Seschrock 		return (error);
4346ea8dc4b6Seschrock 
43478f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4348ea8dc4b6Seschrock 
4349e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4350ea8dc4b6Seschrock 		vd = NULL;
4351c5904d13Seschrock 	} else {
4352c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4353fa94a07fSbrendan 		if (vd == NULL) {
4354e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4355fa94a07fSbrendan 			spa_close(spa, FTAG);
4356*be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4357fa94a07fSbrendan 		}
4358ea8dc4b6Seschrock 	}
4359ea8dc4b6Seschrock 
4360e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4361e14bb325SJeff Bonwick 
4362e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4363ea8dc4b6Seschrock 
4364e14bb325SJeff Bonwick 	/*
4365e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4366e14bb325SJeff Bonwick 	 */
436754d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4368*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4369ea8dc4b6Seschrock 
4370ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4371ea8dc4b6Seschrock 
437254d692b7SGeorge Wilson 	return (error);
4373ea8dc4b6Seschrock }
4374ea8dc4b6Seschrock 
43754263d13fSGeorge Wilson static int
43764263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
43774263d13fSGeorge Wilson {
43784263d13fSGeorge Wilson 	spa_t *spa;
43794263d13fSGeorge Wilson 	int error;
43804263d13fSGeorge Wilson 
43814263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
43823b2aab18SMatthew Ahrens 	if (error != 0)
43834263d13fSGeorge Wilson 		return (error);
43844263d13fSGeorge Wilson 
43854263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4386d6afdce2SGeorge Wilson 
4387d6afdce2SGeorge Wilson 	/*
4388d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4389d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4390d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4391d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4392d6afdce2SGeorge Wilson 	 */
4393d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
43944263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4395d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4396d6afdce2SGeorge Wilson 
43974263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
43984263d13fSGeorge Wilson 	spa_close(spa, FTAG);
43994263d13fSGeorge Wilson 	return (0);
44004263d13fSGeorge Wilson }
44013cb34c60Sahrens /*
44023cb34c60Sahrens  * inputs:
44033cb34c60Sahrens  * zc_name	name of filesystem
44043cb34c60Sahrens  * zc_value	name of origin snapshot
44053cb34c60Sahrens  *
4406681d9761SEric Taylor  * outputs:
4407681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
44083cb34c60Sahrens  */
440999653d4eSeschrock static int
441099653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
441199653d4eSeschrock {
44120b69c2f0Sahrens 	char *cp;
44130b69c2f0Sahrens 
44140b69c2f0Sahrens 	/*
44150b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
44160b69c2f0Sahrens 	 * it's easier.
44170b69c2f0Sahrens 	 */
4418e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
44190b69c2f0Sahrens 	if (cp)
44200b69c2f0Sahrens 		*cp = '\0';
4421e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
44223b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4423681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
442499653d4eSeschrock }
442599653d4eSeschrock 
442614843421SMatthew Ahrens /*
442714843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
442814843421SMatthew Ahrens  *
442914843421SMatthew Ahrens  * inputs:
443014843421SMatthew Ahrens  * zc_name	name of filesystem
443114843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
443214843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
443314843421SMatthew Ahrens  * zc_guid	RID/UID/GID
443414843421SMatthew Ahrens  *
443514843421SMatthew Ahrens  * outputs:
443614843421SMatthew Ahrens  * zc_cookie	property value
443714843421SMatthew Ahrens  */
443814843421SMatthew Ahrens static int
443914843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
444014843421SMatthew Ahrens {
444114843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
444214843421SMatthew Ahrens 	int error;
444314843421SMatthew Ahrens 
444414843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4445*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
444614843421SMatthew Ahrens 
44471412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
44483b2aab18SMatthew Ahrens 	if (error != 0)
444914843421SMatthew Ahrens 		return (error);
445014843421SMatthew Ahrens 
445114843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
445214843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
445314843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
445414843421SMatthew Ahrens 
445514843421SMatthew Ahrens 	return (error);
445614843421SMatthew Ahrens }
445714843421SMatthew Ahrens 
445814843421SMatthew Ahrens /*
445914843421SMatthew Ahrens  * inputs:
446014843421SMatthew Ahrens  * zc_name		name of filesystem
446114843421SMatthew Ahrens  * zc_cookie		zap cursor
446214843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
446314843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
446414843421SMatthew Ahrens  *
446514843421SMatthew Ahrens  * outputs:
446614843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
446714843421SMatthew Ahrens  * zc_cookie	zap cursor
446814843421SMatthew Ahrens  */
446914843421SMatthew Ahrens static int
447014843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
447114843421SMatthew Ahrens {
447214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4473eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
447414843421SMatthew Ahrens 
4475eeb85002STim Haley 	if (bufsize <= 0)
4476*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4477eeb85002STim Haley 
44781412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
44793b2aab18SMatthew Ahrens 	if (error != 0)
448014843421SMatthew Ahrens 		return (error);
448114843421SMatthew Ahrens 
448214843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
448314843421SMatthew Ahrens 
448414843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
448514843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
448614843421SMatthew Ahrens 
448714843421SMatthew Ahrens 	if (error == 0) {
448814843421SMatthew Ahrens 		error = xcopyout(buf,
448914843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
449014843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
449114843421SMatthew Ahrens 	}
449214843421SMatthew Ahrens 	kmem_free(buf, bufsize);
449314843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
449414843421SMatthew Ahrens 
449514843421SMatthew Ahrens 	return (error);
449614843421SMatthew Ahrens }
449714843421SMatthew Ahrens 
449814843421SMatthew Ahrens /*
449914843421SMatthew Ahrens  * inputs:
450014843421SMatthew Ahrens  * zc_name		name of filesystem
450114843421SMatthew Ahrens  *
450214843421SMatthew Ahrens  * outputs:
450314843421SMatthew Ahrens  * none
450414843421SMatthew Ahrens  */
450514843421SMatthew Ahrens static int
450614843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
450714843421SMatthew Ahrens {
450814843421SMatthew Ahrens 	objset_t *os;
45091195e687SMark J Musante 	int error = 0;
451014843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
451114843421SMatthew Ahrens 
451214843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4513503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
451414843421SMatthew Ahrens 			/*
451514843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
451614843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
451714843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
451814843421SMatthew Ahrens 			 */
4519503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4520503ad85cSMatthew Ahrens 			if (error == 0)
4521503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
452214843421SMatthew Ahrens 		}
452314843421SMatthew Ahrens 		if (error == 0)
452414843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
452514843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
452614843421SMatthew Ahrens 	} else {
4527503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4528503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
45293b2aab18SMatthew Ahrens 		if (error != 0)
453014843421SMatthew Ahrens 			return (error);
453114843421SMatthew Ahrens 
453214843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4533503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
453414843421SMatthew Ahrens 	}
453514843421SMatthew Ahrens 
453614843421SMatthew Ahrens 	return (error);
453714843421SMatthew Ahrens }
453814843421SMatthew Ahrens 
4539ecd6cf80Smarks /*
4540ecd6cf80Smarks  * We don't want to have a hard dependency
4541ecd6cf80Smarks  * against some special symbols in sharefs
4542da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4543ecd6cf80Smarks  * the first file system is shared.
4544da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4545ecd6cf80Smarks  */
4546da6c28aaSamw int (*znfsexport_fs)(void *arg);
4547ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4548da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4549da6c28aaSamw 
4550da6c28aaSamw int zfs_nfsshare_inited;
4551da6c28aaSamw int zfs_smbshare_inited;
4552ecd6cf80Smarks 
4553ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4554ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4555da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4556ecd6cf80Smarks kmutex_t zfs_share_lock;
4557ecd6cf80Smarks 
4558da6c28aaSamw static int
4559da6c28aaSamw zfs_init_sharefs()
4560da6c28aaSamw {
4561da6c28aaSamw 	int error;
4562da6c28aaSamw 
4563da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4564da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4565da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4566da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4567da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4568*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4569da6c28aaSamw 	}
4570da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4571da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4572da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4573*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4574da6c28aaSamw 	}
4575da6c28aaSamw 	return (0);
4576da6c28aaSamw }
4577da6c28aaSamw 
4578ecd6cf80Smarks static int
4579ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4580ecd6cf80Smarks {
4581ecd6cf80Smarks 	int error;
4582ecd6cf80Smarks 	int opcode;
4583ecd6cf80Smarks 
4584da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4585da6c28aaSamw 	case ZFS_SHARE_NFS:
4586da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4587da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4588da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4589da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4590da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4591da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4592*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4593da6c28aaSamw 			}
4594da6c28aaSamw 			if (znfsexport_fs == NULL &&
4595da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4596da6c28aaSamw 			    ddi_modsym(nfs_mod,
4597da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4598da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4599*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4600da6c28aaSamw 			}
4601da6c28aaSamw 			error = zfs_init_sharefs();
46023b2aab18SMatthew Ahrens 			if (error != 0) {
4603da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4604*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4605da6c28aaSamw 			}
4606da6c28aaSamw 			zfs_nfsshare_inited = 1;
4607ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4608ecd6cf80Smarks 		}
4609da6c28aaSamw 		break;
4610da6c28aaSamw 	case ZFS_SHARE_SMB:
4611da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4612da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4613da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4614da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4615da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4616da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4617da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4618*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4619da6c28aaSamw 			}
4620da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4621da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4622faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
4623da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4624*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4625da6c28aaSamw 			}
4626da6c28aaSamw 			error = zfs_init_sharefs();
46273b2aab18SMatthew Ahrens 			if (error != 0) {
4628da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4629*be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4630da6c28aaSamw 			}
4631da6c28aaSamw 			zfs_smbshare_inited = 1;
4632ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4633ecd6cf80Smarks 		}
4634da6c28aaSamw 		break;
4635da6c28aaSamw 	default:
4636*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4637da6c28aaSamw 	}
4638ecd6cf80Smarks 
4639da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4640da6c28aaSamw 	case ZFS_SHARE_NFS:
4641da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4642da6c28aaSamw 		if (error =
4643da6c28aaSamw 		    znfsexport_fs((void *)
4644da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4645da6c28aaSamw 			return (error);
4646da6c28aaSamw 		break;
4647da6c28aaSamw 	case ZFS_SHARE_SMB:
4648da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4649da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4650da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4651da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4652743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
4653da6c28aaSamw 			return (error);
4654ecd6cf80Smarks 		}
4655da6c28aaSamw 		break;
4656ecd6cf80Smarks 	}
4657ecd6cf80Smarks 
4658da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4659da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4660ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4661ecd6cf80Smarks 
4662da6c28aaSamw 	/*
4663da6c28aaSamw 	 * Add or remove share from sharetab
4664da6c28aaSamw 	 */
4665ecd6cf80Smarks 	error = zshare_fs(opcode,
4666ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4667ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4668ecd6cf80Smarks 
4669ecd6cf80Smarks 	return (error);
4670ecd6cf80Smarks 
4671ecd6cf80Smarks }
4672ecd6cf80Smarks 
4673743a77edSAlan Wright ace_t full_access[] = {
4674743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4675743a77edSAlan Wright };
4676743a77edSAlan Wright 
467799d5e173STim Haley /*
467899d5e173STim Haley  * inputs:
467999d5e173STim Haley  * zc_name		name of containing filesystem
468099d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
468199d5e173STim Haley  *
468299d5e173STim Haley  * outputs:
468399d5e173STim Haley  * zc_obj		next in-use object #
468499d5e173STim Haley  */
468599d5e173STim Haley static int
468699d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
468799d5e173STim Haley {
468899d5e173STim Haley 	objset_t *os = NULL;
468999d5e173STim Haley 	int error;
469099d5e173STim Haley 
469199d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
46923b2aab18SMatthew Ahrens 	if (error != 0)
469399d5e173STim Haley 		return (error);
469499d5e173STim Haley 
469599d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
469699d5e173STim Haley 	    os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
469799d5e173STim Haley 
469899d5e173STim Haley 	dmu_objset_rele(os, FTAG);
469999d5e173STim Haley 	return (error);
470099d5e173STim Haley }
470199d5e173STim Haley 
470299d5e173STim Haley /*
470399d5e173STim Haley  * inputs:
470499d5e173STim Haley  * zc_name		name of filesystem
470599d5e173STim Haley  * zc_value		prefix name for snapshot
470699d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
470799d5e173STim Haley  *
470899d5e173STim Haley  * outputs:
47094445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
471099d5e173STim Haley  */
471199d5e173STim Haley static int
471299d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
471399d5e173STim Haley {
471499d5e173STim Haley 	char *snap_name;
47153b2aab18SMatthew Ahrens 	char *hold_name;
471699d5e173STim Haley 	int error;
47173b2aab18SMatthew Ahrens 	minor_t minor;
471899d5e173STim Haley 
47193b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
47203b2aab18SMatthew Ahrens 	if (error != 0)
472199d5e173STim Haley 		return (error);
472299d5e173STim Haley 
47233b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
47243b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
47253b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
47263b2aab18SMatthew Ahrens 
47273b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
47283b2aab18SMatthew Ahrens 	    hold_name);
47293b2aab18SMatthew Ahrens 	if (error == 0)
47303b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
473199d5e173STim Haley 	strfree(snap_name);
47323b2aab18SMatthew Ahrens 	strfree(hold_name);
47333b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
47343b2aab18SMatthew Ahrens 	return (error);
473599d5e173STim Haley }
473699d5e173STim Haley 
473799d5e173STim Haley /*
473899d5e173STim Haley  * inputs:
473999d5e173STim Haley  * zc_name		name of "to" snapshot
474099d5e173STim Haley  * zc_value		name of "from" snapshot
474199d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
474299d5e173STim Haley  *
474399d5e173STim Haley  * outputs:
474499d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
474599d5e173STim Haley  */
474699d5e173STim Haley static int
474799d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
474899d5e173STim Haley {
474999d5e173STim Haley 	file_t *fp;
475099d5e173STim Haley 	offset_t off;
475199d5e173STim Haley 	int error;
475299d5e173STim Haley 
475399d5e173STim Haley 	fp = getf(zc->zc_cookie);
47543b2aab18SMatthew Ahrens 	if (fp == NULL)
4755*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
475699d5e173STim Haley 
475799d5e173STim Haley 	off = fp->f_offset;
475899d5e173STim Haley 
47593b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
476099d5e173STim Haley 
476199d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
476299d5e173STim Haley 		fp->f_offset = off;
476399d5e173STim Haley 	releasef(zc->zc_cookie);
476499d5e173STim Haley 
476599d5e173STim Haley 	return (error);
476699d5e173STim Haley }
476799d5e173STim Haley 
4768743a77edSAlan Wright /*
4769743a77edSAlan Wright  * Remove all ACL files in shares dir
4770743a77edSAlan Wright  */
4771743a77edSAlan Wright static int
4772743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
4773743a77edSAlan Wright {
4774743a77edSAlan Wright 	zap_cursor_t	zc;
4775743a77edSAlan Wright 	zap_attribute_t	zap;
4776743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4777743a77edSAlan Wright 	int error;
4778743a77edSAlan Wright 
4779743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4780743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4781743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
4782743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4783743a77edSAlan Wright 		    NULL, 0)) != 0)
4784743a77edSAlan Wright 			break;
4785743a77edSAlan Wright 	}
4786743a77edSAlan Wright 	zap_cursor_fini(&zc);
4787743a77edSAlan Wright 	return (error);
4788743a77edSAlan Wright }
4789743a77edSAlan Wright 
4790743a77edSAlan Wright static int
4791743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
4792743a77edSAlan Wright {
4793743a77edSAlan Wright 	vnode_t *vp;
4794743a77edSAlan Wright 	znode_t *dzp;
4795743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
4796743a77edSAlan Wright 	znode_t *sharedir;
4797743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
4798743a77edSAlan Wright 	nvlist_t *nvlist;
4799743a77edSAlan Wright 	char *src, *target;
4800743a77edSAlan Wright 	vattr_t vattr;
4801743a77edSAlan Wright 	vsecattr_t vsec;
4802743a77edSAlan Wright 	int error = 0;
4803743a77edSAlan Wright 
4804743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4805743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
4806743a77edSAlan Wright 		return (error);
4807743a77edSAlan Wright 
4808743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
4809743a77edSAlan Wright 
4810743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4811743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4812743a77edSAlan Wright 	    zc->zc_name) != 0)) {
4813743a77edSAlan Wright 		VN_RELE(vp);
4814*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4815743a77edSAlan Wright 	}
4816743a77edSAlan Wright 
4817743a77edSAlan Wright 	dzp = VTOZ(vp);
4818743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
4819743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
4820743a77edSAlan Wright 
48219e1320c0SMark Shellenbaum 	/*
48229e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
48239e1320c0SMark Shellenbaum 	 */
48249e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
48259e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
48269e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
48279e1320c0SMark Shellenbaum 
48289e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
48299e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
48309e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
48319e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
48329e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
48333b2aab18SMatthew Ahrens 		if (error != 0) {
48349e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
48359e1320c0SMark Shellenbaum 		} else {
48369e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
48379e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
48389e1320c0SMark Shellenbaum 		}
48393b2aab18SMatthew Ahrens 		if (error != 0) {
48409e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
48419e1320c0SMark Shellenbaum 			VN_RELE(vp);
48429e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
48439e1320c0SMark Shellenbaum 			return (error);
48449e1320c0SMark Shellenbaum 		}
48459e1320c0SMark Shellenbaum 	}
48469e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
48479e1320c0SMark Shellenbaum 
48489e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
4849743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
48509e1320c0SMark Shellenbaum 		VN_RELE(vp);
4851743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
4852743a77edSAlan Wright 		return (error);
4853743a77edSAlan Wright 	}
4854743a77edSAlan Wright 
4855743a77edSAlan Wright 	switch (zc->zc_cookie) {
4856743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4857743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4858743a77edSAlan Wright 		vattr.va_type = VREG;
4859743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
4860743a77edSAlan Wright 		vattr.va_uid = 0;
4861743a77edSAlan Wright 		vattr.va_gid = 0;
4862743a77edSAlan Wright 
4863743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
4864743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
4865743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
4866743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
4867743a77edSAlan Wright 
4868743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4869743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4870743a77edSAlan Wright 		if (resourcevp)
4871743a77edSAlan Wright 			VN_RELE(resourcevp);
4872743a77edSAlan Wright 		break;
4873743a77edSAlan Wright 
4874743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4875743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4876743a77edSAlan Wright 		    NULL, 0);
4877743a77edSAlan Wright 		break;
4878743a77edSAlan Wright 
4879743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4880743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
4881478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4882743a77edSAlan Wright 			VN_RELE(vp);
4883743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
4884743a77edSAlan Wright 			return (error);
4885743a77edSAlan Wright 		}
4886743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4887743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4888743a77edSAlan Wright 		    &target)) {
4889743a77edSAlan Wright 			VN_RELE(vp);
489089459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
4891743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
48921195e687SMark J Musante 			nvlist_free(nvlist);
4893743a77edSAlan Wright 			return (error);
4894743a77edSAlan Wright 		}
4895743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4896743a77edSAlan Wright 		    kcred, NULL, 0);
4897743a77edSAlan Wright 		nvlist_free(nvlist);
4898743a77edSAlan Wright 		break;
4899743a77edSAlan Wright 
4900743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4901743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
4902743a77edSAlan Wright 		break;
4903743a77edSAlan Wright 
4904743a77edSAlan Wright 	default:
4905*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
4906743a77edSAlan Wright 		break;
4907743a77edSAlan Wright 	}
4908743a77edSAlan Wright 
4909743a77edSAlan Wright 	VN_RELE(vp);
4910743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
4911743a77edSAlan Wright 
4912743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
4913743a77edSAlan Wright 
4914743a77edSAlan Wright 	return (error);
4915743a77edSAlan Wright }
4916743a77edSAlan Wright 
4917842727c2SChris Kirby /*
49183b2aab18SMatthew Ahrens  * innvl: {
49193b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
49203b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
49213b2aab18SMatthew Ahrens  * }
4922842727c2SChris Kirby  *
49233b2aab18SMatthew Ahrens  * outnvl: {
49243b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
49253b2aab18SMatthew Ahrens  *     ...
49263b2aab18SMatthew Ahrens  * }
4927842727c2SChris Kirby  */
49283b2aab18SMatthew Ahrens /* ARGSUSED */
4929842727c2SChris Kirby static int
49303b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
4931842727c2SChris Kirby {
49323b2aab18SMatthew Ahrens 	nvlist_t *holds;
49333b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
4934a7f53a56SChris Kirby 	int error;
4935a7f53a56SChris Kirby 	minor_t minor = 0;
4936842727c2SChris Kirby 
49373b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
49383b2aab18SMatthew Ahrens 	if (error != 0)
4939*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4940a7f53a56SChris Kirby 
49413b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
49423b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
49433b2aab18SMatthew Ahrens 		if (error != 0)
4944a7f53a56SChris Kirby 			return (error);
4945a7f53a56SChris Kirby 	}
4946a7f53a56SChris Kirby 
49473b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
49483b2aab18SMatthew Ahrens 	if (minor != 0)
49493b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
4950a7f53a56SChris Kirby 	return (error);
4951842727c2SChris Kirby }
4952842727c2SChris Kirby 
4953842727c2SChris Kirby /*
49543b2aab18SMatthew Ahrens  * innvl is not used.
4955842727c2SChris Kirby  *
49563b2aab18SMatthew Ahrens  * outnvl: {
49573b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
49583b2aab18SMatthew Ahrens  *    ...
49593b2aab18SMatthew Ahrens  * }
4960842727c2SChris Kirby  */
49613b2aab18SMatthew Ahrens /* ARGSUSED */
4962842727c2SChris Kirby static int
49633b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
4964842727c2SChris Kirby {
49653b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
4966842727c2SChris Kirby }
4967842727c2SChris Kirby 
4968842727c2SChris Kirby /*
49693b2aab18SMatthew Ahrens  * innvl: {
49703b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
49713b2aab18SMatthew Ahrens  *     ...
49723b2aab18SMatthew Ahrens  * }
4973842727c2SChris Kirby  *
49743b2aab18SMatthew Ahrens  * outnvl: {
49753b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
49763b2aab18SMatthew Ahrens  *     ...
49773b2aab18SMatthew Ahrens  * }
4978842727c2SChris Kirby  */
49793b2aab18SMatthew Ahrens /* ARGSUSED */
4980842727c2SChris Kirby static int
49813b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
4982842727c2SChris Kirby {
49833b2aab18SMatthew Ahrens 	nvpair_t *pair;
4984842727c2SChris Kirby 
49853b2aab18SMatthew Ahrens 	/*
49863b2aab18SMatthew Ahrens 	 * The release may cause the snapshot to be destroyed; make sure it
49873b2aab18SMatthew Ahrens 	 * is not mounted.
49883b2aab18SMatthew Ahrens 	 */
49893b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
49903b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair))
49913b2aab18SMatthew Ahrens 		zfs_unmount_snap(nvpair_name(pair));
4992842727c2SChris Kirby 
49933b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
4994842727c2SChris Kirby }
4995842727c2SChris Kirby 
499619b94df9SMatthew Ahrens /*
499719b94df9SMatthew Ahrens  * inputs:
499819b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
499919b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
500019b94df9SMatthew Ahrens  *
500119b94df9SMatthew Ahrens  * outputs:
500219b94df9SMatthew Ahrens  * zc_cookie		space in bytes
500319b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
500419b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
500519b94df9SMatthew Ahrens  */
500619b94df9SMatthew Ahrens static int
500719b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
500819b94df9SMatthew Ahrens {
500919b94df9SMatthew Ahrens 	int error;
50103b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
501119b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
501219b94df9SMatthew Ahrens 
50133b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
501419b94df9SMatthew Ahrens 	if (error != 0)
501519b94df9SMatthew Ahrens 		return (error);
50163b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
50173b2aab18SMatthew Ahrens 	if (error != 0) {
50183b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
50193b2aab18SMatthew Ahrens 		return (error);
50203b2aab18SMatthew Ahrens 	}
50213b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
502219b94df9SMatthew Ahrens 	if (error != 0) {
502319b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
50243b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
502519b94df9SMatthew Ahrens 		return (error);
502619b94df9SMatthew Ahrens 	}
502719b94df9SMatthew Ahrens 
502819b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
502919b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
503019b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
503119b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
50323b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
503319b94df9SMatthew Ahrens 	return (error);
503419b94df9SMatthew Ahrens }
50353b2aab18SMatthew Ahrens 
503619b94df9SMatthew Ahrens /*
50374445fffbSMatthew Ahrens  * innvl: {
50384445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
50394445fffbSMatthew Ahrens  * }
504019b94df9SMatthew Ahrens  *
50414445fffbSMatthew Ahrens  * outnvl: {
50424445fffbSMatthew Ahrens  *     "used" -> space in bytes
50434445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
50444445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
50454445fffbSMatthew Ahrens  * }
504619b94df9SMatthew Ahrens  */
504719b94df9SMatthew Ahrens static int
50484445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
504919b94df9SMatthew Ahrens {
505019b94df9SMatthew Ahrens 	int error;
50513b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
505219b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
50534445fffbSMatthew Ahrens 	char *firstsnap;
50544445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
505519b94df9SMatthew Ahrens 
50564445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5057*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
50584445fffbSMatthew Ahrens 
50593b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
506019b94df9SMatthew Ahrens 	if (error != 0)
506119b94df9SMatthew Ahrens 		return (error);
50623b2aab18SMatthew Ahrens 
50633b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
50643b2aab18SMatthew Ahrens 	if (error != 0) {
50653b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
50663b2aab18SMatthew Ahrens 		return (error);
50673b2aab18SMatthew Ahrens 	}
50683b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
506919b94df9SMatthew Ahrens 	if (error != 0) {
507019b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
50713b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
507219b94df9SMatthew Ahrens 		return (error);
507319b94df9SMatthew Ahrens 	}
507419b94df9SMatthew Ahrens 
50754445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
507619b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
507719b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
50783b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
50794445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
50804445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
50814445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
508219b94df9SMatthew Ahrens 	return (error);
508319b94df9SMatthew Ahrens }
508419b94df9SMatthew Ahrens 
5085ecd6cf80Smarks /*
50864445fffbSMatthew Ahrens  * innvl: {
50874445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
50884445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
50894445fffbSMatthew Ahrens  * }
50904445fffbSMatthew Ahrens  *
50914445fffbSMatthew Ahrens  * outnvl is unused
5092ecd6cf80Smarks  */
50934445fffbSMatthew Ahrens /* ARGSUSED */
50944445fffbSMatthew Ahrens static int
50954445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
50964445fffbSMatthew Ahrens {
50974445fffbSMatthew Ahrens 	int error;
50984445fffbSMatthew Ahrens 	offset_t off;
50993b2aab18SMatthew Ahrens 	char *fromname = NULL;
51004445fffbSMatthew Ahrens 	int fd;
51014445fffbSMatthew Ahrens 
51024445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
51034445fffbSMatthew Ahrens 	if (error != 0)
5104*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
51054445fffbSMatthew Ahrens 
51063b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
51074445fffbSMatthew Ahrens 
51084445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
51093b2aab18SMatthew Ahrens 	if (fp == NULL)
5110*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
51114445fffbSMatthew Ahrens 
51124445fffbSMatthew Ahrens 	off = fp->f_offset;
51133b2aab18SMatthew Ahrens 	error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
51144445fffbSMatthew Ahrens 
51154445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
51164445fffbSMatthew Ahrens 		fp->f_offset = off;
51174445fffbSMatthew Ahrens 	releasef(fd);
51184445fffbSMatthew Ahrens 	return (error);
51194445fffbSMatthew Ahrens }
51204445fffbSMatthew Ahrens 
51214445fffbSMatthew Ahrens /*
51224445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
51234445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
51244445fffbSMatthew Ahrens  *
51254445fffbSMatthew Ahrens  * innvl: {
51264445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
51274445fffbSMatthew Ahrens  * }
51284445fffbSMatthew Ahrens  *
51294445fffbSMatthew Ahrens  * outnvl: {
51304445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
51314445fffbSMatthew Ahrens  * }
51324445fffbSMatthew Ahrens  */
51334445fffbSMatthew Ahrens static int
51344445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
51354445fffbSMatthew Ahrens {
51363b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
51373b2aab18SMatthew Ahrens 	dsl_dataset_t *fromsnap = NULL;
51383b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
51394445fffbSMatthew Ahrens 	int error;
51404445fffbSMatthew Ahrens 	char *fromname;
51414445fffbSMatthew Ahrens 	uint64_t space;
51424445fffbSMatthew Ahrens 
51433b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
51443b2aab18SMatthew Ahrens 	if (error != 0)
51453b2aab18SMatthew Ahrens 		return (error);
51463b2aab18SMatthew Ahrens 
51473b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
51483b2aab18SMatthew Ahrens 	if (error != 0) {
51493b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
51504445fffbSMatthew Ahrens 		return (error);
51513b2aab18SMatthew Ahrens 	}
51524445fffbSMatthew Ahrens 
51534445fffbSMatthew Ahrens 	error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
51544445fffbSMatthew Ahrens 	if (error == 0) {
51553b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
51563b2aab18SMatthew Ahrens 		if (error != 0) {
51573b2aab18SMatthew Ahrens 			dsl_dataset_rele(tosnap, FTAG);
51583b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
51594445fffbSMatthew Ahrens 			return (error);
51604445fffbSMatthew Ahrens 		}
51614445fffbSMatthew Ahrens 	}
51624445fffbSMatthew Ahrens 
51634445fffbSMatthew Ahrens 	error = dmu_send_estimate(tosnap, fromsnap, &space);
51644445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
51654445fffbSMatthew Ahrens 
51664445fffbSMatthew Ahrens 	if (fromsnap != NULL)
51673b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromsnap, FTAG);
51683b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
51693b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
51704445fffbSMatthew Ahrens 	return (error);
51714445fffbSMatthew Ahrens }
51724445fffbSMatthew Ahrens 
51734445fffbSMatthew Ahrens 
51744445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
51754445fffbSMatthew Ahrens 
51764445fffbSMatthew Ahrens static void
51774445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
51784445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
51794445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
51804445fffbSMatthew Ahrens {
51814445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
51824445fffbSMatthew Ahrens 
51834445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
51844445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
51854445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
51864445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
51874445fffbSMatthew Ahrens 
51884445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
51894445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
51904445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
51914445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
51924445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
51934445fffbSMatthew Ahrens }
51944445fffbSMatthew Ahrens 
51954445fffbSMatthew Ahrens /*
51964445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
51974445fffbSMatthew Ahrens  * each argument to this function.
51984445fffbSMatthew Ahrens  */
51994445fffbSMatthew Ahrens static void
52004445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
52014445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
52024445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
52034445fffbSMatthew Ahrens     boolean_t allow_log)
52044445fffbSMatthew Ahrens {
52054445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
52064445fffbSMatthew Ahrens 
52074445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
52084445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
52094445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
52104445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
52114445fffbSMatthew Ahrens 
52124445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
52134445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
52144445fffbSMatthew Ahrens 
52154445fffbSMatthew Ahrens 	vec->zvec_name = name;
52164445fffbSMatthew Ahrens 	vec->zvec_func = func;
52174445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
52184445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
52194445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
52204445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
52214445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
52224445fffbSMatthew Ahrens }
52234445fffbSMatthew Ahrens 
52244445fffbSMatthew Ahrens static void
52254445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52264445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
52274445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
52284445fffbSMatthew Ahrens {
52294445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52304445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
52314445fffbSMatthew Ahrens }
52324445fffbSMatthew Ahrens 
52334445fffbSMatthew Ahrens static void
52344445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52354445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
52364445fffbSMatthew Ahrens {
52374445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52384445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
52394445fffbSMatthew Ahrens }
52404445fffbSMatthew Ahrens 
52414445fffbSMatthew Ahrens static void
52424445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
52434445fffbSMatthew Ahrens {
52444445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
52454445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
52464445fffbSMatthew Ahrens }
52474445fffbSMatthew Ahrens 
52484445fffbSMatthew Ahrens static void
52494445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52504445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
52514445fffbSMatthew Ahrens {
52524445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52534445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
52544445fffbSMatthew Ahrens }
52554445fffbSMatthew Ahrens 
52564445fffbSMatthew Ahrens static void
52574445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
52584445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
52594445fffbSMatthew Ahrens {
52604445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52614445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
52624445fffbSMatthew Ahrens }
52634445fffbSMatthew Ahrens 
52644445fffbSMatthew Ahrens static void
52654445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
52664445fffbSMatthew Ahrens {
52674445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
52684445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
52694445fffbSMatthew Ahrens }
52704445fffbSMatthew Ahrens 
52714445fffbSMatthew Ahrens static void
52724445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
52734445fffbSMatthew Ahrens 	zfs_secpolicy_func_t *secpolicy)
52744445fffbSMatthew Ahrens {
52754445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
52764445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
52774445fffbSMatthew Ahrens }
52784445fffbSMatthew Ahrens 
52794445fffbSMatthew Ahrens static void
52804445fffbSMatthew Ahrens zfs_ioctl_init(void)
52814445fffbSMatthew Ahrens {
52824445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
52834445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
52844445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
52854445fffbSMatthew Ahrens 
52864445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
52874445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
52884445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
52894445fffbSMatthew Ahrens 
52904445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
52914445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
52924445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
52934445fffbSMatthew Ahrens 
52944445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
52954445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
52964445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
52974445fffbSMatthew Ahrens 
52984445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
52994445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
53004445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
53014445fffbSMatthew Ahrens 
53024445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
53034445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
53044445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53054445fffbSMatthew Ahrens 
53064445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
53074445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
53084445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53094445fffbSMatthew Ahrens 
53104445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
53114445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
53124445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53134445fffbSMatthew Ahrens 
53143b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
53153b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
53163b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53173b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
53183b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
53193b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
53203b2aab18SMatthew Ahrens 
53213b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
53223b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
53233b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
53243b2aab18SMatthew Ahrens 
53254445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
53264445fffbSMatthew Ahrens 
53274445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
53284445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
53294445fffbSMatthew Ahrens 
53304445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
53314445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
53324445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
53334445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
53344445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
53354445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
53364445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
53374445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
53384445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
53394445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
53404445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
53414445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
53424445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
53434445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
53444445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
53454445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
53464445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
53474445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
53484445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
53494445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
53504445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
53514445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
53524445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
53534445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
53544445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
53554445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
53564445fffbSMatthew Ahrens 
53574445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
53584445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
53594445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
53604445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
53614445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
53624445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
53634445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
53644445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
53654445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
53664445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
53674445fffbSMatthew Ahrens 
53684445fffbSMatthew Ahrens 	/*
53694445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
53704445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
53714445fffbSMatthew Ahrens 	 * does the logging of those commands.
53724445fffbSMatthew Ahrens 	 */
53734445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
53744445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
53754445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
53764445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
53774445fffbSMatthew Ahrens 
53784445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
53794445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
53804445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
53814445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
53824445fffbSMatthew Ahrens 
53834445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
53844445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
53854445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
53864445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
53874445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
53884445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
53894445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
53904445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
53914445fffbSMatthew Ahrens 
53924445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
53934445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
53944445fffbSMatthew Ahrens 
53954445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
53964445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
53974445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
53984445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
53994445fffbSMatthew Ahrens 
54004445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
54014445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
54024445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
54034445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
54044445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
54054445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
54064445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
54074445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
54084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
54094445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
54104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
54114445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
54124445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
54134445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
54144445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
54154445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
54164445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
54174445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
54184445fffbSMatthew Ahrens 
54194445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
54204445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
54214445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
54224445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
54234445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
54244445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
54254445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
54264445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
54274445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
54284445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
54294445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
54304445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
54314445fffbSMatthew Ahrens 
54324445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
54334445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
54344445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
54354445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
54364445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK, zfs_ioc_rollback,
54374445fffbSMatthew Ahrens 	    zfs_secpolicy_rollback);
54384445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
54394445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
54404445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
54414445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
54424445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
54434445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
54444445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
54454445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
54464445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
54474445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
54484445fffbSMatthew Ahrens 
54494445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
54504445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
54514445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
54524445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
54534445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
54544445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
54554445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
54564445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
54574445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
54584445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
54594445fffbSMatthew Ahrens }
5460fa9e4066Sahrens 
546154d692b7SGeorge Wilson int
5462f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5463f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
546454d692b7SGeorge Wilson {
546554d692b7SGeorge Wilson 	spa_t *spa;
546654d692b7SGeorge Wilson 	int error;
546754d692b7SGeorge Wilson 
546854d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
546954d692b7SGeorge Wilson 
5470f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5471f9af39baSGeorge Wilson 		return (0);
5472f9af39baSGeorge Wilson 
547314843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
547454d692b7SGeorge Wilson 	if (error == 0) {
5475f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5476*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5477f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5478*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
547954d692b7SGeorge Wilson 		spa_close(spa, FTAG);
548054d692b7SGeorge Wilson 	}
548154d692b7SGeorge Wilson 	return (error);
548254d692b7SGeorge Wilson }
548354d692b7SGeorge Wilson 
5484c99e4bdcSChris Kirby /*
5485c99e4bdcSChris Kirby  * Find a free minor number.
5486c99e4bdcSChris Kirby  */
5487c99e4bdcSChris Kirby minor_t
5488c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5489c99e4bdcSChris Kirby {
5490c99e4bdcSChris Kirby 	static minor_t last_minor;
5491c99e4bdcSChris Kirby 	minor_t m;
5492c99e4bdcSChris Kirby 
5493c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5494c99e4bdcSChris Kirby 
5495c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5496c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5497c99e4bdcSChris Kirby 			m = 1;
5498c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5499c99e4bdcSChris Kirby 			last_minor = m;
5500c99e4bdcSChris Kirby 			return (m);
5501c99e4bdcSChris Kirby 		}
5502c99e4bdcSChris Kirby 	}
5503c99e4bdcSChris Kirby 
5504c99e4bdcSChris Kirby 	return (0);
5505c99e4bdcSChris Kirby }
5506c99e4bdcSChris Kirby 
5507c99e4bdcSChris Kirby static int
5508c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5509c99e4bdcSChris Kirby {
5510c99e4bdcSChris Kirby 	minor_t minor;
5511c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5512c99e4bdcSChris Kirby 
5513c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5514c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5515c99e4bdcSChris Kirby 
5516c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5517c99e4bdcSChris Kirby 	if (minor == 0)
5518*be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5519c99e4bdcSChris Kirby 
5520c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5521*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5522c99e4bdcSChris Kirby 
5523c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5524c99e4bdcSChris Kirby 
5525c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
5526c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
5527c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5528c99e4bdcSChris Kirby 
5529c99e4bdcSChris Kirby 	return (0);
5530c99e4bdcSChris Kirby }
5531c99e4bdcSChris Kirby 
5532c99e4bdcSChris Kirby static void
5533c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5534c99e4bdcSChris Kirby {
5535c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5536c99e4bdcSChris Kirby 
5537c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
5538c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
5539c99e4bdcSChris Kirby }
5540c99e4bdcSChris Kirby 
5541c99e4bdcSChris Kirby void *
5542c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5543c99e4bdcSChris Kirby {
5544c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
5545c99e4bdcSChris Kirby 
5546c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
5547c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
5548c99e4bdcSChris Kirby 		return (NULL);
5549c99e4bdcSChris Kirby 
5550c99e4bdcSChris Kirby 	return (zp->zss_data);
5551c99e4bdcSChris Kirby }
5552c99e4bdcSChris Kirby 
5553c99e4bdcSChris Kirby static int
5554c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5555c99e4bdcSChris Kirby {
5556c99e4bdcSChris Kirby 	int error = 0;
5557c99e4bdcSChris Kirby 
5558c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
5559c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
5560c99e4bdcSChris Kirby 
5561c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
5562c99e4bdcSChris Kirby 	if (flag & FEXCL) {
5563c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
5564c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
5565c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5566c99e4bdcSChris Kirby 	}
5567c99e4bdcSChris Kirby 
5568c99e4bdcSChris Kirby 	return (error);
5569c99e4bdcSChris Kirby }
5570c99e4bdcSChris Kirby 
5571c99e4bdcSChris Kirby static int
5572c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5573c99e4bdcSChris Kirby {
5574c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
5575c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5576c99e4bdcSChris Kirby 
5577c99e4bdcSChris Kirby 	if (minor == 0)
5578c99e4bdcSChris Kirby 		return (0);
5579c99e4bdcSChris Kirby 
5580c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5581c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5582c99e4bdcSChris Kirby 	if (zo == NULL) {
5583c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5584c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5585c99e4bdcSChris Kirby 	}
5586c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5587c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5588c99e4bdcSChris Kirby 
5589c99e4bdcSChris Kirby 	return (0);
5590c99e4bdcSChris Kirby }
5591c99e4bdcSChris Kirby 
5592fa9e4066Sahrens static int
5593fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5594fa9e4066Sahrens {
5595fa9e4066Sahrens 	zfs_cmd_t *zc;
55964445fffbSMatthew Ahrens 	uint_t vecnum;
55974445fffbSMatthew Ahrens 	int error, rc, len;
5598c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
55994445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
56004445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
56014445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
5602fa9e4066Sahrens 
5603c99e4bdcSChris Kirby 	if (minor != 0 &&
5604c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5605fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5606fa9e4066Sahrens 
56074445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
560891ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5609fa9e4066Sahrens 
56104445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5611*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
56124445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
5613fa9e4066Sahrens 
5614fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5615fa9e4066Sahrens 
5616478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
56174445fffbSMatthew Ahrens 	if (error != 0) {
5618*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
56194445fffbSMatthew Ahrens 		goto out;
56204445fffbSMatthew Ahrens 	}
5621fa9e4066Sahrens 
56224445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
56234445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
56244445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
56254445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
56264445fffbSMatthew Ahrens 		if (error != 0)
56274445fffbSMatthew Ahrens 			goto out;
56284445fffbSMatthew Ahrens 	}
5629fa9e4066Sahrens 
5630fa9e4066Sahrens 	/*
5631fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
5632fa9e4066Sahrens 	 * the lower layers.
5633fa9e4066Sahrens 	 */
56344445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
56354445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
56364445fffbSMatthew Ahrens 	case POOL_NAME:
56374445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5638*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
56394445fffbSMatthew Ahrens 		else
5640f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
56414445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
56424445fffbSMatthew Ahrens 		break;
5643fa9e4066Sahrens 
56444445fffbSMatthew Ahrens 	case DATASET_NAME:
56454445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5646*be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
56474445fffbSMatthew Ahrens 		else
5648f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
56494445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
56504445fffbSMatthew Ahrens 		break;
56515ad82045Snd 
56524445fffbSMatthew Ahrens 	case NO_NAME:
56534445fffbSMatthew Ahrens 		break;
5654fa9e4066Sahrens 	}
5655fa9e4066Sahrens 
5656fa9e4066Sahrens 
56574445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
56584445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
56594445fffbSMatthew Ahrens 
56604445fffbSMatthew Ahrens 	if (error != 0)
56614445fffbSMatthew Ahrens 		goto out;
56624445fffbSMatthew Ahrens 
56634445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
56644445fffbSMatthew Ahrens 	len = strcspn(zc->zc_name, "/@") + 1;
56654445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
56664445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
56674445fffbSMatthew Ahrens 
56684445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
56694445fffbSMatthew Ahrens 		nvlist_t *outnvl;
56704445fffbSMatthew Ahrens 		int puterror = 0;
56714445fffbSMatthew Ahrens 		spa_t *spa;
56724445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
56734445fffbSMatthew Ahrens 
56744445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
56754445fffbSMatthew Ahrens 
56764445fffbSMatthew Ahrens 		/*
56774445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
56784445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
56794445fffbSMatthew Ahrens 		 */
56804445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
56814445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
56824445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
56834445fffbSMatthew Ahrens 			    vec->zvec_name);
56844445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
56854445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
56864445fffbSMatthew Ahrens 				    innvl);
56874445fffbSMatthew Ahrens 			}
56884445fffbSMatthew Ahrens 		}
56894445fffbSMatthew Ahrens 
56904445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
56914445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
56924445fffbSMatthew Ahrens 
56934445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
56944445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
56954445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
56964445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
56974445fffbSMatthew Ahrens 				    outnvl);
56984445fffbSMatthew Ahrens 			}
56994445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
57004445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
57014445fffbSMatthew Ahrens 		}
57024445fffbSMatthew Ahrens 		fnvlist_free(lognv);
57034445fffbSMatthew Ahrens 
57044445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
57054445fffbSMatthew Ahrens 			int smusherror = 0;
57064445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
57074445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
57084445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
57094445fffbSMatthew Ahrens 			}
57104445fffbSMatthew Ahrens 			if (smusherror == 0)
57114445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
57124445fffbSMatthew Ahrens 		}
57134445fffbSMatthew Ahrens 
57144445fffbSMatthew Ahrens 		if (puterror != 0)
57154445fffbSMatthew Ahrens 			error = puterror;
57164445fffbSMatthew Ahrens 
57174445fffbSMatthew Ahrens 		nvlist_free(outnvl);
57184445fffbSMatthew Ahrens 	} else {
57194445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
57204445fffbSMatthew Ahrens 	}
57214445fffbSMatthew Ahrens 
57224445fffbSMatthew Ahrens out:
57234445fffbSMatthew Ahrens 	nvlist_free(innvl);
5724478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
57254445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
5726*be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
57274445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
57284445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
57294445fffbSMatthew Ahrens 		if (s != NULL)
57304445fffbSMatthew Ahrens 			strfree(s);
57314445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
57324445fffbSMatthew Ahrens 	} else {
57334445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
57344445fffbSMatthew Ahrens 			strfree(saved_poolname);
5735ecd6cf80Smarks 	}
5736fa9e4066Sahrens 
5737fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
5738fa9e4066Sahrens 	return (error);
5739fa9e4066Sahrens }
5740fa9e4066Sahrens 
5741fa9e4066Sahrens static int
5742fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5743fa9e4066Sahrens {
5744fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
5745fa9e4066Sahrens 		return (DDI_FAILURE);
5746fa9e4066Sahrens 
5747fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5748fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
5749fa9e4066Sahrens 		return (DDI_FAILURE);
5750fa9e4066Sahrens 
5751fa9e4066Sahrens 	zfs_dip = dip;
5752fa9e4066Sahrens 
5753fa9e4066Sahrens 	ddi_report_dev(dip);
5754fa9e4066Sahrens 
5755fa9e4066Sahrens 	return (DDI_SUCCESS);
5756fa9e4066Sahrens }
5757fa9e4066Sahrens 
5758fa9e4066Sahrens static int
5759fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5760fa9e4066Sahrens {
5761fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
5762fa9e4066Sahrens 		return (DDI_FAILURE);
5763fa9e4066Sahrens 
5764fa9e4066Sahrens 	if (cmd != DDI_DETACH)
5765fa9e4066Sahrens 		return (DDI_FAILURE);
5766fa9e4066Sahrens 
5767fa9e4066Sahrens 	zfs_dip = NULL;
5768fa9e4066Sahrens 
5769fa9e4066Sahrens 	ddi_prop_remove_all(dip);
5770fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
5771fa9e4066Sahrens 
5772fa9e4066Sahrens 	return (DDI_SUCCESS);
5773fa9e4066Sahrens }
5774fa9e4066Sahrens 
5775fa9e4066Sahrens /*ARGSUSED*/
5776fa9e4066Sahrens static int
5777fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5778fa9e4066Sahrens {
5779fa9e4066Sahrens 	switch (infocmd) {
5780fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
5781fa9e4066Sahrens 		*result = zfs_dip;
5782fa9e4066Sahrens 		return (DDI_SUCCESS);
5783fa9e4066Sahrens 
5784fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
5785a0965f35Sbonwick 		*result = (void *)0;
5786fa9e4066Sahrens 		return (DDI_SUCCESS);
5787fa9e4066Sahrens 	}
5788fa9e4066Sahrens 
5789fa9e4066Sahrens 	return (DDI_FAILURE);
5790fa9e4066Sahrens }
5791fa9e4066Sahrens 
5792fa9e4066Sahrens /*
5793fa9e4066Sahrens  * OK, so this is a little weird.
5794fa9e4066Sahrens  *
5795fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
5796fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5797fa9e4066Sahrens  *
5798fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
5799fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
5800fa9e4066Sahrens  */
5801fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
5802c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
5803c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
5804fa9e4066Sahrens 	zvol_strategy,	/* strategy */
5805fa9e4066Sahrens 	nodev,		/* print */
5806e7cbe64fSgw 	zvol_dump,	/* dump */
5807fa9e4066Sahrens 	zvol_read,	/* read */
5808fa9e4066Sahrens 	zvol_write,	/* write */
5809fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
5810fa9e4066Sahrens 	nodev,		/* devmap */
5811fa9e4066Sahrens 	nodev,		/* mmap */
5812fa9e4066Sahrens 	nodev,		/* segmap */
5813fa9e4066Sahrens 	nochpoll,	/* poll */
5814fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
5815fa9e4066Sahrens 	NULL,		/* streamtab */
5816fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
5817fa9e4066Sahrens 	CB_REV,		/* version */
5818feb08c6bSbillm 	nodev,		/* async read */
5819feb08c6bSbillm 	nodev,		/* async write */
5820fa9e4066Sahrens };
5821fa9e4066Sahrens 
5822fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
5823fa9e4066Sahrens 	DEVO_REV,	/* version */
5824fa9e4066Sahrens 	0,		/* refcnt */
5825fa9e4066Sahrens 	zfs_info,	/* info */
5826fa9e4066Sahrens 	nulldev,	/* identify */
5827fa9e4066Sahrens 	nulldev,	/* probe */
5828fa9e4066Sahrens 	zfs_attach,	/* attach */
5829fa9e4066Sahrens 	zfs_detach,	/* detach */
5830fa9e4066Sahrens 	nodev,		/* reset */
5831fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
583219397407SSherry Moore 	NULL,		/* no bus operations */
583319397407SSherry Moore 	NULL,		/* power */
583419397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
5835fa9e4066Sahrens };
5836fa9e4066Sahrens 
5837fa9e4066Sahrens static struct modldrv zfs_modldrv = {
583819397407SSherry Moore 	&mod_driverops,
583919397407SSherry Moore 	"ZFS storage pool",
584019397407SSherry Moore 	&zfs_dev_ops
5841fa9e4066Sahrens };
5842fa9e4066Sahrens 
5843fa9e4066Sahrens static struct modlinkage modlinkage = {
5844fa9e4066Sahrens 	MODREV_1,
5845fa9e4066Sahrens 	(void *)&zfs_modlfs,
5846fa9e4066Sahrens 	(void *)&zfs_modldrv,
5847fa9e4066Sahrens 	NULL
5848fa9e4066Sahrens };
5849fa9e4066Sahrens 
58504445fffbSMatthew Ahrens static void
58514445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
58524445fffbSMatthew Ahrens {
58534445fffbSMatthew Ahrens 	char *poolname = arg;
58544445fffbSMatthew Ahrens 	strfree(poolname);
58554445fffbSMatthew Ahrens }
5856ec533521Sfr 
5857fa9e4066Sahrens int
5858fa9e4066Sahrens _init(void)
5859fa9e4066Sahrens {
5860fa9e4066Sahrens 	int error;
5861fa9e4066Sahrens 
5862a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
5863a0965f35Sbonwick 	zfs_init();
5864a0965f35Sbonwick 	zvol_init();
58654445fffbSMatthew Ahrens 	zfs_ioctl_init();
5866a0965f35Sbonwick 
5867a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
5868a0965f35Sbonwick 		zvol_fini();
5869a0965f35Sbonwick 		zfs_fini();
5870a0965f35Sbonwick 		spa_fini();
5871fa9e4066Sahrens 		return (error);
5872a0965f35Sbonwick 	}
5873fa9e4066Sahrens 
5874ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
58754445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
58764445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
5877ec533521Sfr 
5878fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5879fa9e4066Sahrens 	ASSERT(error == 0);
5880ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5881fa9e4066Sahrens 
5882fa9e4066Sahrens 	return (0);
5883fa9e4066Sahrens }
5884fa9e4066Sahrens 
5885fa9e4066Sahrens int
5886fa9e4066Sahrens _fini(void)
5887fa9e4066Sahrens {
5888fa9e4066Sahrens 	int error;
5889fa9e4066Sahrens 
5890ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5891*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
5892fa9e4066Sahrens 
5893fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
5894fa9e4066Sahrens 		return (error);
5895fa9e4066Sahrens 
5896fa9e4066Sahrens 	zvol_fini();
5897fa9e4066Sahrens 	zfs_fini();
5898fa9e4066Sahrens 	spa_fini();
5899da6c28aaSamw 	if (zfs_nfsshare_inited)
5900ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
5901da6c28aaSamw 	if (zfs_smbshare_inited)
5902da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
5903da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
5904ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
5905fa9e4066Sahrens 
5906ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
5907fa9e4066Sahrens 	ldi_ident_release(zfs_li);
5908fa9e4066Sahrens 	zfs_li = NULL;
5909ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
5910fa9e4066Sahrens 
5911fa9e4066Sahrens 	return (error);
5912fa9e4066Sahrens }
5913fa9e4066Sahrens 
5914fa9e4066Sahrens int
5915fa9e4066Sahrens _info(struct modinfo *modinfop)
5916fa9e4066Sahrens {
5917fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
5918fa9e4066Sahrens }
5919