xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 78f17100)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
241df56adaSMartin Matuska  * Portions Copyright 2011 Martin Matuska
25e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
264e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
28a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
304445fffbSMatthew Ahrens  */
314445fffbSMatthew Ahrens 
324445fffbSMatthew Ahrens /*
334445fffbSMatthew Ahrens  * ZFS ioctls.
344445fffbSMatthew Ahrens  *
354445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
364445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
374445fffbSMatthew Ahrens  *
384445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
394445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
404445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
414445fffbSMatthew Ahrens  *
424445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
434445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
444445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
454445fffbSMatthew Ahrens  *
464445fffbSMatthew Ahrens  * The registration arguments are as follows:
474445fffbSMatthew Ahrens  *
484445fffbSMatthew Ahrens  * const char *name
494445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
504445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
514445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
524445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
534445fffbSMatthew Ahrens  *
544445fffbSMatthew Ahrens  * zfs_ioc_t ioc
554445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
564445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
574445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
584445fffbSMatthew Ahrens  *
594445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
604445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
614445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
624445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
634445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
644445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
654445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
664445fffbSMatthew Ahrens  *
674445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
684445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
694445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
704445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
714445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
724445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
734445fffbSMatthew Ahrens  *   doesn't have invalid characters).
744445fffbSMatthew Ahrens  *
754445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
764445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
774445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
784445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
794445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
804445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
814445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
824445fffbSMatthew Ahrens  *
834445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
844445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
854445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
864445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
874445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
884445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
894445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
904445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
914445fffbSMatthew Ahrens  *
924445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
934445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
944445fffbSMatthew Ahrens  *
954445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
964445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
974445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
984445fffbSMatthew Ahrens  *   will be called with the following arguments:
994445fffbSMatthew Ahrens  *
1004445fffbSMatthew Ahrens  *   const char *name
1014445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1024445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1034445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1044445fffbSMatthew Ahrens  *
1054445fffbSMatthew Ahrens  *   nvlist_t *innvl
1064445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1074445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1084445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1094445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1104445fffbSMatthew Ahrens  *
1114445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1124445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1134445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1144445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1154445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1164445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1174445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1184445fffbSMatthew Ahrens  *
1194445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1204445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1214445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1224445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1234445fffbSMatthew Ahrens  *         the ioctl again.
1244445fffbSMatthew Ahrens  *
1254445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1264445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1274445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1284445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1294445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1304445fffbSMatthew Ahrens  *         deserialization failing.
131e9103aaeSGarrett D'Amore  */
132fa9e4066Sahrens 
133fa9e4066Sahrens #include <sys/types.h>
134fa9e4066Sahrens #include <sys/param.h>
135fa9e4066Sahrens #include <sys/errno.h>
136fa9e4066Sahrens #include <sys/uio.h>
137fa9e4066Sahrens #include <sys/buf.h>
138fa9e4066Sahrens #include <sys/modctl.h>
139fa9e4066Sahrens #include <sys/open.h>
140fa9e4066Sahrens #include <sys/file.h>
141fa9e4066Sahrens #include <sys/kmem.h>
142fa9e4066Sahrens #include <sys/conf.h>
143fa9e4066Sahrens #include <sys/cmn_err.h>
144fa9e4066Sahrens #include <sys/stat.h>
145fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1464201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
147da6c28aaSamw #include <sys/zfs_znode.h>
148fa9e4066Sahrens #include <sys/zap.h>
149fa9e4066Sahrens #include <sys/spa.h>
150b1b8ab34Slling #include <sys/spa_impl.h>
151fa9e4066Sahrens #include <sys/vdev.h>
1524201a95eSRic Aleshire #include <sys/priv_impl.h>
153fa9e4066Sahrens #include <sys/dmu.h>
154fa9e4066Sahrens #include <sys/dsl_dir.h>
155fa9e4066Sahrens #include <sys/dsl_dataset.h>
156fa9e4066Sahrens #include <sys/dsl_prop.h>
157ecd6cf80Smarks #include <sys/dsl_deleg.h>
158ecd6cf80Smarks #include <sys/dmu_objset.h>
1594e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1603b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
161fa9e4066Sahrens #include <sys/ddi.h>
162fa9e4066Sahrens #include <sys/sunddi.h>
163fa9e4066Sahrens #include <sys/sunldi.h>
164fa9e4066Sahrens #include <sys/policy.h>
165fa9e4066Sahrens #include <sys/zone.h>
166fa9e4066Sahrens #include <sys/nvpair.h>
167fa9e4066Sahrens #include <sys/pathname.h>
168fa9e4066Sahrens #include <sys/mount.h>
169fa9e4066Sahrens #include <sys/sdt.h>
170fa9e4066Sahrens #include <sys/fs/zfs.h>
171fa9e4066Sahrens #include <sys/zfs_ctldir.h>
172da6c28aaSamw #include <sys/zfs_dir.h>
173c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
174a2eea2e1Sahrens #include <sys/zvol.h>
1753f9d6ad7SLin Ling #include <sys/dsl_scan.h>
176ecd6cf80Smarks #include <sharefs/share.h>
177f18faf3fSek #include <sys/dmu_objset.h>
1783b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1793b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
180*78f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1813b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
182a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
183fa9e4066Sahrens 
184fa9e4066Sahrens #include "zfs_namecheck.h"
185e9dbad6fSeschrock #include "zfs_prop.h"
186ecd6cf80Smarks #include "zfs_deleg.h"
1870a586ceaSMark Shellenbaum #include "zfs_comutil.h"
188fa9e4066Sahrens 
189fa9e4066Sahrens extern struct modlfs zfs_modlfs;
190fa9e4066Sahrens 
191fa9e4066Sahrens extern void zfs_init(void);
192fa9e4066Sahrens extern void zfs_fini(void);
193fa9e4066Sahrens 
194fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
195fa9e4066Sahrens dev_info_t *zfs_dip;
196fa9e4066Sahrens 
1974445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
1984445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
1994445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2004445fffbSMatthew Ahrens 
2014445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2024445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2034445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
204fa9e4066Sahrens 
20554d692b7SGeorge Wilson typedef enum {
20654d692b7SGeorge Wilson 	NO_NAME,
20754d692b7SGeorge Wilson 	POOL_NAME,
20854d692b7SGeorge Wilson 	DATASET_NAME
20954d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21054d692b7SGeorge Wilson 
211f9af39baSGeorge Wilson typedef enum {
212f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
213f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2144445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
215f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
216f9af39baSGeorge Wilson 
217fa9e4066Sahrens typedef struct zfs_ioc_vec {
2184445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
219fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
220fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
22154d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2224445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
223f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2244445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2254445fffbSMatthew Ahrens 	const char		*zvec_name;
226fa9e4066Sahrens } zfs_ioc_vec_t;
227fa9e4066Sahrens 
22814843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
22914843421SMatthew Ahrens static const char *userquota_perms[] = {
23014843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23114843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23214843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23314843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23414843421SMatthew Ahrens };
23514843421SMatthew Ahrens 
23614843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
23792241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
23892241e0bSTom Erickson     cred_t *cr);
23992241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24092241e0bSTom Erickson     nvlist_t **errors);
2410a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2420a48a24eStimh     boolean_t *);
2434445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2444445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2450a48a24eStimh 
2462acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
247a6f561b4SSašo Kiselkov 
248fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
249fa9e4066Sahrens void
250fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
251fa9e4066Sahrens {
252fa9e4066Sahrens 	const char *newfile;
2533f9d6ad7SLin Ling 	char buf[512];
254fa9e4066Sahrens 	va_list adx;
255fa9e4066Sahrens 
256fa9e4066Sahrens 	/*
257fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
258fa9e4066Sahrens 	 */
259fa9e4066Sahrens 	newfile = strrchr(file, '/');
260fa9e4066Sahrens 	if (newfile != NULL) {
261fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
262fa9e4066Sahrens 	} else {
263fa9e4066Sahrens 		newfile = file;
264fa9e4066Sahrens 	}
265fa9e4066Sahrens 
266fa9e4066Sahrens 	va_start(adx, fmt);
267fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
268fa9e4066Sahrens 	va_end(adx);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	/*
271fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
272fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
273fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
274fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
275fa9e4066Sahrens 	 * arg0 = file name
276fa9e4066Sahrens 	 * arg1 = function name
277fa9e4066Sahrens 	 * arg2 = line number
278fa9e4066Sahrens 	 * arg3 = message
279fa9e4066Sahrens 	 */
280fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
281fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
282fa9e4066Sahrens }
283fa9e4066Sahrens 
284ecd6cf80Smarks static void
285228975ccSek history_str_free(char *buf)
286228975ccSek {
287228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
288228975ccSek }
289228975ccSek 
290228975ccSek static char *
291228975ccSek history_str_get(zfs_cmd_t *zc)
292ecd6cf80Smarks {
29340feaa91Sahrens 	char *buf;
294ecd6cf80Smarks 
295ecd6cf80Smarks 	if (zc->zc_history == NULL)
296228975ccSek 		return (NULL);
297e7437265Sahrens 
298ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
299ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
300ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
301228975ccSek 		history_str_free(buf);
302228975ccSek 		return (NULL);
303ecd6cf80Smarks 	}
304ecd6cf80Smarks 
305ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
306ecd6cf80Smarks 
307228975ccSek 	return (buf);
308228975ccSek }
309ecd6cf80Smarks 
31015e6edf1Sgw /*
31115e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
31215e6edf1Sgw  */
31315e6edf1Sgw static boolean_t
31415e6edf1Sgw zfs_is_bootfs(const char *name)
31515e6edf1Sgw {
316503ad85cSMatthew Ahrens 	objset_t *os;
31715e6edf1Sgw 
318503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
319503ad85cSMatthew Ahrens 		boolean_t ret;
320b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
321503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
322503ad85cSMatthew Ahrens 		return (ret);
32315e6edf1Sgw 	}
324503ad85cSMatthew Ahrens 	return (B_FALSE);
32515e6edf1Sgw }
32615e6edf1Sgw 
327c2a93d44Stimh /*
328f7170741SWill Andrews  * 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  * Return TRUE if the ZPL version is less than requested version.
3479e6eda55Smarks  */
348745cd3c5Smaybee static boolean_t
349745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3509e6eda55Smarks {
3519e6eda55Smarks 	objset_t *os;
352745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3539e6eda55Smarks 
354503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
355745cd3c5Smaybee 		uint64_t zplversion;
3569e6eda55Smarks 
357503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
358503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
359503ad85cSMatthew Ahrens 			return (B_TRUE);
360503ad85cSMatthew Ahrens 		}
361503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
362745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
363745cd3c5Smaybee 			rc = zplversion < version;
364503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3659e6eda55Smarks 	}
3669e6eda55Smarks 	return (rc);
3679e6eda55Smarks }
3689e6eda55Smarks 
369228975ccSek static void
370228975ccSek zfs_log_history(zfs_cmd_t *zc)
371228975ccSek {
372228975ccSek 	spa_t *spa;
373228975ccSek 	char *buf;
374ecd6cf80Smarks 
375228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
376228975ccSek 		return;
377228975ccSek 
378228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
379228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3804445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
381228975ccSek 		spa_close(spa, FTAG);
382228975ccSek 	}
383228975ccSek 	history_str_free(buf);
384ecd6cf80Smarks }
385ecd6cf80Smarks 
386fa9e4066Sahrens /*
387fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
388fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
389fa9e4066Sahrens  */
390fa9e4066Sahrens /* ARGSUSED */
391fa9e4066Sahrens static int
3924445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
393fa9e4066Sahrens {
394fa9e4066Sahrens 	return (0);
395fa9e4066Sahrens }
396fa9e4066Sahrens 
397fa9e4066Sahrens /*
398fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
399fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
400fa9e4066Sahrens  */
401fa9e4066Sahrens /* ARGSUSED */
402fa9e4066Sahrens static int
4034445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
404fa9e4066Sahrens {
405fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
406ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
407fa9e4066Sahrens 		return (0);
408fa9e4066Sahrens 
409be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412fa9e4066Sahrens static int
413a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
414fa9e4066Sahrens {
415fa9e4066Sahrens 	int writable = 1;
416fa9e4066Sahrens 
417fa9e4066Sahrens 	/*
418fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
419fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
420fa9e4066Sahrens 	 */
421fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
422fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
423be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
424fa9e4066Sahrens 
425fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
426fa9e4066Sahrens 		/*
427fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
428fa9e4066Sahrens 		 * global zone.
429fa9e4066Sahrens 		 */
430fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
431be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
432fa9e4066Sahrens 	} else {
433fa9e4066Sahrens 		/*
434fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
435fa9e4066Sahrens 		 */
436fa9e4066Sahrens 		if (!zoned)
437be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
438fa9e4066Sahrens 
439fa9e4066Sahrens 		/* must be writable by this zone */
440fa9e4066Sahrens 		if (!writable)
441be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
442fa9e4066Sahrens 	}
443fa9e4066Sahrens 	return (0);
444fa9e4066Sahrens }
445fa9e4066Sahrens 
446a7f53a56SChris Kirby static int
447a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
448a7f53a56SChris Kirby {
449a7f53a56SChris Kirby 	uint64_t zoned;
450a7f53a56SChris Kirby 
451a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
452be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
453a7f53a56SChris Kirby 
454a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
455a7f53a56SChris Kirby }
456a7f53a56SChris Kirby 
457a7f53a56SChris Kirby static int
458a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
459a7f53a56SChris Kirby {
460a7f53a56SChris Kirby 	uint64_t zoned;
461a7f53a56SChris Kirby 
4623b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
463be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
464a7f53a56SChris Kirby 
465a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
466a7f53a56SChris Kirby }
467a7f53a56SChris Kirby 
4684445fffbSMatthew Ahrens static int
4693b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4703b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
471fa9e4066Sahrens {
472fa9e4066Sahrens 	int error;
473fa9e4066Sahrens 
47419b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
475ecd6cf80Smarks 	if (error == 0) {
476ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4773b2aab18SMatthew Ahrens 		if (error != 0)
4784445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
479ecd6cf80Smarks 	}
480ecd6cf80Smarks 	return (error);
481ecd6cf80Smarks }
482ecd6cf80Smarks 
4834445fffbSMatthew Ahrens static int
4843b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
485a7f53a56SChris Kirby {
486a7f53a56SChris Kirby 	int error;
4873b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4883b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
489a7f53a56SChris Kirby 
4903b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4913b2aab18SMatthew Ahrens 	if (error != 0)
4923b2aab18SMatthew Ahrens 		return (error);
4933b2aab18SMatthew Ahrens 
4943b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
4953b2aab18SMatthew Ahrens 	if (error != 0) {
4963b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
4973b2aab18SMatthew Ahrens 		return (error);
498a7f53a56SChris Kirby 	}
4993b2aab18SMatthew Ahrens 
5003b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5013b2aab18SMatthew Ahrens 
5023b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5033b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
504a7f53a56SChris Kirby 	return (error);
505a7f53a56SChris Kirby }
506a7f53a56SChris Kirby 
5074201a95eSRic Aleshire /*
5084201a95eSRic Aleshire  * Policy for setting the security label property.
5094201a95eSRic Aleshire  *
5104201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5114201a95eSRic Aleshire  */
5124201a95eSRic Aleshire static int
51392241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5144201a95eSRic Aleshire {
5154201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5164201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5174201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5184201a95eSRic Aleshire 	uint64_t	zoned;
5194201a95eSRic Aleshire 	int		needed_priv = -1;
5204201a95eSRic Aleshire 	int		error;
5214201a95eSRic Aleshire 
5224201a95eSRic Aleshire 	/* First get the existing dataset label. */
5234201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5244201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5253b2aab18SMatthew Ahrens 	if (error != 0)
526be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5274201a95eSRic Aleshire 
5284201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5294201a95eSRic Aleshire 		new_default = TRUE;
5304201a95eSRic Aleshire 
5314201a95eSRic Aleshire 	/* The label must be translatable */
5324201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
533be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5344201a95eSRic Aleshire 
5354201a95eSRic Aleshire 	/*
5364201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5374201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5384201a95eSRic Aleshire 	 * are needed.
5394201a95eSRic Aleshire 	 */
5404201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5414201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
542be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5434201a95eSRic Aleshire 		return (0);
5444201a95eSRic Aleshire 	}
5454201a95eSRic Aleshire 
5464201a95eSRic Aleshire 	/*
5474201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5484201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5494201a95eSRic Aleshire 	 * global zone.
5504201a95eSRic Aleshire 	 */
5514201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5524201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
553be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5544201a95eSRic Aleshire 	if (!zoned) {
5554201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
556be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5574201a95eSRic Aleshire 	}
5584201a95eSRic Aleshire 
5594201a95eSRic Aleshire 	/*
5604201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5614201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5624201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5634201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5644201a95eSRic Aleshire 	 */
5654201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
56692241e0bSTom Erickson 		objset_t *os;
56792241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
56892241e0bSTom Erickson 
5694201a95eSRic Aleshire 		/*
5704201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5714201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5724201a95eSRic Aleshire 		 */
5734201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57492241e0bSTom Erickson 		    setsl_tag, &os);
5753b2aab18SMatthew Ahrens 		if (error != 0)
576be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5774201a95eSRic Aleshire 
57892241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
57992241e0bSTom Erickson 
5804201a95eSRic Aleshire 		if (new_default) {
5814201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5824201a95eSRic Aleshire 			goto out_check;
5834201a95eSRic Aleshire 		}
5844201a95eSRic Aleshire 
5854201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
586be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5874201a95eSRic Aleshire 
5884201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5894201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5904201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5914201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5924201a95eSRic Aleshire 	} else {
5934201a95eSRic Aleshire 		/* dataset currently has a default label */
5944201a95eSRic Aleshire 		if (!new_default)
5954201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5964201a95eSRic Aleshire 	}
5974201a95eSRic Aleshire 
5984201a95eSRic Aleshire out_check:
5994201a95eSRic Aleshire 	if (needed_priv != -1)
6004201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6014201a95eSRic Aleshire 	return (0);
6024201a95eSRic Aleshire }
6034201a95eSRic Aleshire 
604ecd6cf80Smarks static int
60592241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
60692241e0bSTom Erickson     cred_t *cr)
607ecd6cf80Smarks {
60892241e0bSTom Erickson 	char *strval;
60992241e0bSTom Erickson 
610ecd6cf80Smarks 	/*
611ecd6cf80Smarks 	 * Check permissions for special properties.
612ecd6cf80Smarks 	 */
613ecd6cf80Smarks 	switch (prop) {
614ecd6cf80Smarks 	case ZFS_PROP_ZONED:
615ecd6cf80Smarks 		/*
616ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
617ecd6cf80Smarks 		 */
618ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
619be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
620ecd6cf80Smarks 		break;
621ecd6cf80Smarks 
622ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
623ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
624ecd6cf80Smarks 			uint64_t zoned;
625ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
626ecd6cf80Smarks 			/*
627ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
628ecd6cf80Smarks 			 * quota on things *under* (ie. contained by)
629ecd6cf80Smarks 			 * the thing they own.
630ecd6cf80Smarks 			 */
63192241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
632ecd6cf80Smarks 			    setpoint))
633be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
63492241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
635be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
636ecd6cf80Smarks 		}
637db870a07Sahrens 		break;
6384201a95eSRic Aleshire 
6394201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6404201a95eSRic Aleshire 		if (!is_system_labeled())
641be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64292241e0bSTom Erickson 
64392241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
64492241e0bSTom Erickson 			int err;
64592241e0bSTom Erickson 
64692241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
64792241e0bSTom Erickson 			if (err != 0)
64892241e0bSTom Erickson 				return (err);
64992241e0bSTom Erickson 		}
6504201a95eSRic Aleshire 		break;
651ecd6cf80Smarks 	}
652ecd6cf80Smarks 
65392241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
654ecd6cf80Smarks }
655ecd6cf80Smarks 
6564445fffbSMatthew Ahrens /* ARGSUSED */
6574445fffbSMatthew Ahrens static int
6584445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
659ecd6cf80Smarks {
660ecd6cf80Smarks 	int error;
661ecd6cf80Smarks 
662ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6633b2aab18SMatthew Ahrens 	if (error != 0)
664fa9e4066Sahrens 		return (error);
665fa9e4066Sahrens 
666ecd6cf80Smarks 	/*
667ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
668ecd6cf80Smarks 	 * dsl_deleg_can_allow()
669ecd6cf80Smarks 	 */
670ecd6cf80Smarks 	return (0);
671ecd6cf80Smarks }
672ecd6cf80Smarks 
6734445fffbSMatthew Ahrens /* ARGSUSED */
6744445fffbSMatthew Ahrens static int
6754445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
676ecd6cf80Smarks {
677681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
678681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
679ecd6cf80Smarks }
680ecd6cf80Smarks 
6814445fffbSMatthew Ahrens /* ARGSUSED */
6824445fffbSMatthew Ahrens static int
6834445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
684ecd6cf80Smarks {
685a7f53a56SChris Kirby 	dsl_pool_t *dp;
686a7f53a56SChris Kirby 	dsl_dataset_t *ds;
687a7f53a56SChris Kirby 	char *cp;
688a7f53a56SChris Kirby 	int error;
689a7f53a56SChris Kirby 
690a7f53a56SChris Kirby 	/*
691a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
692a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
693a7f53a56SChris Kirby 	 */
694a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
695a7f53a56SChris Kirby 	if (cp == NULL)
696be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
6973b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6983b2aab18SMatthew Ahrens 	if (error != 0)
699a7f53a56SChris Kirby 		return (error);
700a7f53a56SChris Kirby 
701a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7023b2aab18SMatthew Ahrens 	if (error != 0) {
7033b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
704a7f53a56SChris Kirby 		return (error);
7053b2aab18SMatthew Ahrens 	}
706a7f53a56SChris Kirby 
707a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
708a7f53a56SChris Kirby 
709a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
710a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
711a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7123b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
713a7f53a56SChris Kirby 
714a7f53a56SChris Kirby 	return (error);
715ecd6cf80Smarks }
716ecd6cf80Smarks 
7174445fffbSMatthew Ahrens /* ARGSUSED */
718743a77edSAlan Wright static int
7194445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7204445fffbSMatthew Ahrens {
7214445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7224445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7234445fffbSMatthew Ahrens }
7244445fffbSMatthew Ahrens 
7254445fffbSMatthew Ahrens /* ARGSUSED */
7264445fffbSMatthew Ahrens static int
7274445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
728743a77edSAlan Wright {
729743a77edSAlan Wright 	vnode_t *vp;
730743a77edSAlan Wright 	int error;
731743a77edSAlan Wright 
732743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
733743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
734743a77edSAlan Wright 		return (error);
735743a77edSAlan Wright 
736743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
737743a77edSAlan Wright 
738743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
739743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
740743a77edSAlan Wright 	    zc->zc_name) != 0)) {
741743a77edSAlan Wright 		VN_RELE(vp);
742be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
743743a77edSAlan Wright 	}
744743a77edSAlan Wright 
745743a77edSAlan Wright 	VN_RELE(vp);
746743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
747743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
748743a77edSAlan Wright }
749743a77edSAlan Wright 
750ecd6cf80Smarks int
7514445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
752ecd6cf80Smarks {
753ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
754be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
755ecd6cf80Smarks 
7563cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
757ecd6cf80Smarks 		return (0);
758ecd6cf80Smarks 	} else {
7594445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
760743a77edSAlan Wright 	}
761743a77edSAlan Wright }
762ecd6cf80Smarks 
763743a77edSAlan Wright int
7644445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
765743a77edSAlan Wright {
766743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
767be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
768ecd6cf80Smarks 
769743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
770743a77edSAlan Wright 		return (0);
771743a77edSAlan Wright 	} else {
7724445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
773ecd6cf80Smarks 	}
774fa9e4066Sahrens }
775fa9e4066Sahrens 
776fa9e4066Sahrens static int
777ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
778fa9e4066Sahrens {
779fa9e4066Sahrens 	char *cp;
780fa9e4066Sahrens 
781fa9e4066Sahrens 	/*
782fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
783fa9e4066Sahrens 	 */
784ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
785ecd6cf80Smarks 	cp = strrchr(parent, '@');
786fa9e4066Sahrens 	if (cp != NULL) {
787fa9e4066Sahrens 		cp[0] = '\0';
788fa9e4066Sahrens 	} else {
789ecd6cf80Smarks 		cp = strrchr(parent, '/');
790fa9e4066Sahrens 		if (cp == NULL)
791be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
792fa9e4066Sahrens 		cp[0] = '\0';
793ecd6cf80Smarks 	}
794ecd6cf80Smarks 
795ecd6cf80Smarks 	return (0);
796ecd6cf80Smarks }
797ecd6cf80Smarks 
798ecd6cf80Smarks int
799ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
800ecd6cf80Smarks {
801ecd6cf80Smarks 	int error;
802ecd6cf80Smarks 
803ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
804ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
805ecd6cf80Smarks 		return (error);
806ecd6cf80Smarks 
807ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
808ecd6cf80Smarks }
809ecd6cf80Smarks 
8104445fffbSMatthew Ahrens /* ARGSUSED */
811ecd6cf80Smarks static int
8124445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
813ecd6cf80Smarks {
814ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
815ecd6cf80Smarks }
816ecd6cf80Smarks 
817cbf6f6aaSWilliam Gorrell /*
818cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8194445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
820cbf6f6aaSWilliam Gorrell  */
8214445fffbSMatthew Ahrens /* ARGSUSED */
822cbf6f6aaSWilliam Gorrell static int
8234445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
824cbf6f6aaSWilliam Gorrell {
8254445fffbSMatthew Ahrens 	nvlist_t *snaps;
8264445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8274445fffbSMatthew Ahrens 	int error = 0;
828cbf6f6aaSWilliam Gorrell 
8294445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
830be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8314445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8324445fffbSMatthew Ahrens 	    pair = nextpair) {
8334445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
834*78f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
835*78f17100SMatthew Ahrens 		if (error == ENOENT) {
8364445fffbSMatthew Ahrens 			/*
8374445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8384445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8394445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8404445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8414445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8424445fffbSMatthew Ahrens 			 * checked for permission).
8434445fffbSMatthew Ahrens 			 */
8444445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8454445fffbSMatthew Ahrens 			error = 0;
8464445fffbSMatthew Ahrens 		}
8474445fffbSMatthew Ahrens 		if (error != 0)
8484445fffbSMatthew Ahrens 			break;
8494445fffbSMatthew Ahrens 	}
850cbf6f6aaSWilliam Gorrell 
851cbf6f6aaSWilliam Gorrell 	return (error);
852cbf6f6aaSWilliam Gorrell }
853cbf6f6aaSWilliam Gorrell 
854ecd6cf80Smarks int
855ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
856ecd6cf80Smarks {
85792241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
858ecd6cf80Smarks 	int	error;
859ecd6cf80Smarks 
860ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
861ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
862ecd6cf80Smarks 		return (error);
863ecd6cf80Smarks 
864ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
865ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
866ecd6cf80Smarks 		return (error);
867ecd6cf80Smarks 
868ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
869ecd6cf80Smarks 	    sizeof (parentname))) != 0)
870ecd6cf80Smarks 		return (error);
871ecd6cf80Smarks 
872ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
873ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
874ecd6cf80Smarks 		return (error);
875ecd6cf80Smarks 
876ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
877ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
878ecd6cf80Smarks 		return (error);
879ecd6cf80Smarks 
880ecd6cf80Smarks 	return (error);
881ecd6cf80Smarks }
882ecd6cf80Smarks 
8834445fffbSMatthew Ahrens /* ARGSUSED */
884ecd6cf80Smarks static int
8854445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
886ecd6cf80Smarks {
887ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
888ecd6cf80Smarks }
889ecd6cf80Smarks 
8904445fffbSMatthew Ahrens /* ARGSUSED */
891ecd6cf80Smarks static int
8924445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
893ecd6cf80Smarks {
8943b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
8953b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
896ecd6cf80Smarks 	int error;
897ecd6cf80Smarks 
898ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
899ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9003b2aab18SMatthew Ahrens 	if (error != 0)
9013b2aab18SMatthew Ahrens 		return (error);
9023b2aab18SMatthew Ahrens 
9033b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9043b2aab18SMatthew Ahrens 	if (error != 0)
905ecd6cf80Smarks 		return (error);
906ecd6cf80Smarks 
9073b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
908ecd6cf80Smarks 
909ecd6cf80Smarks 	if (error == 0) {
9103b2aab18SMatthew Ahrens 		char parentname[MAXNAMELEN];
9113b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
912ecd6cf80Smarks 		dsl_dir_t *dd;
9133b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
914ecd6cf80Smarks 
915745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
9163b2aab18SMatthew Ahrens 		    dd->dd_phys->dd_origin_obj, FTAG, &origin);
9173b2aab18SMatthew Ahrens 		if (error != 0) {
9183b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9193b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
920ecd6cf80Smarks 			return (error);
921ecd6cf80Smarks 		}
922ecd6cf80Smarks 
9233b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
924ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
925ecd6cf80Smarks 
9263b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9273b2aab18SMatthew Ahrens 		if (error == 0) {
9283b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
929ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9303b2aab18SMatthew Ahrens 		}
9313b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9323b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
933ecd6cf80Smarks 	}
9343b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
935ecd6cf80Smarks 	return (error);
936ecd6cf80Smarks }
937ecd6cf80Smarks 
9384445fffbSMatthew Ahrens /* ARGSUSED */
939ecd6cf80Smarks static int
9404445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
941ecd6cf80Smarks {
942ecd6cf80Smarks 	int error;
943ecd6cf80Smarks 
944ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
945ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
946ecd6cf80Smarks 		return (error);
947ecd6cf80Smarks 
948ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
949ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
950ecd6cf80Smarks 		return (error);
951ecd6cf80Smarks 
952ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
953ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
954ecd6cf80Smarks }
955ecd6cf80Smarks 
956ecd6cf80Smarks int
957ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
958ecd6cf80Smarks {
959681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
960681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
961ecd6cf80Smarks }
962ecd6cf80Smarks 
9634445fffbSMatthew Ahrens /*
9644445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9654445fffbSMatthew Ahrens  */
9664445fffbSMatthew Ahrens /* ARGSUSED */
967ecd6cf80Smarks static int
9684445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
969ecd6cf80Smarks {
9704445fffbSMatthew Ahrens 	nvlist_t *snaps;
971d5285caeSGeorge Wilson 	int error = 0;
9724445fffbSMatthew Ahrens 	nvpair_t *pair;
973ecd6cf80Smarks 
9744445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
975be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9764445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9774445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9784445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9794445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9804445fffbSMatthew Ahrens 
9814445fffbSMatthew Ahrens 		if (atp == NULL) {
982be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9834445fffbSMatthew Ahrens 			break;
9844445fffbSMatthew Ahrens 		}
9854445fffbSMatthew Ahrens 		*atp = '\0';
9864445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
9874445fffbSMatthew Ahrens 		*atp = '@';
9884445fffbSMatthew Ahrens 		if (error != 0)
9894445fffbSMatthew Ahrens 			break;
9904445fffbSMatthew Ahrens 	}
9914445fffbSMatthew Ahrens 	return (error);
992ecd6cf80Smarks }
993ecd6cf80Smarks 
994*78f17100SMatthew Ahrens /*
995*78f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
996*78f17100SMatthew Ahrens  */
997*78f17100SMatthew Ahrens /* ARGSUSED */
998*78f17100SMatthew Ahrens static int
999*78f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1000*78f17100SMatthew Ahrens {
1001*78f17100SMatthew Ahrens 	int error = 0;
1002*78f17100SMatthew Ahrens 
1003*78f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1004*78f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1005*78f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
1006*78f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
1007*78f17100SMatthew Ahrens 
1008*78f17100SMatthew Ahrens 		if (hashp == NULL) {
1009*78f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
1010*78f17100SMatthew Ahrens 			break;
1011*78f17100SMatthew Ahrens 		}
1012*78f17100SMatthew Ahrens 		*hashp = '\0';
1013*78f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
1014*78f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
1015*78f17100SMatthew Ahrens 		*hashp = '#';
1016*78f17100SMatthew Ahrens 		if (error != 0)
1017*78f17100SMatthew Ahrens 			break;
1018*78f17100SMatthew Ahrens 	}
1019*78f17100SMatthew Ahrens 	return (error);
1020*78f17100SMatthew Ahrens }
1021*78f17100SMatthew Ahrens 
1022*78f17100SMatthew Ahrens /* ARGSUSED */
1023*78f17100SMatthew Ahrens static int
1024*78f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1025*78f17100SMatthew Ahrens {
1026*78f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
1027*78f17100SMatthew Ahrens 	int error = 0;
1028*78f17100SMatthew Ahrens 
1029*78f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1030*78f17100SMatthew Ahrens 	    pair = nextpair) {
1031*78f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
1032*78f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
1033*78f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
1034*78f17100SMatthew Ahrens 
1035*78f17100SMatthew Ahrens 		if (hashp == NULL) {
1036*78f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
1037*78f17100SMatthew Ahrens 			break;
1038*78f17100SMatthew Ahrens 		}
1039*78f17100SMatthew Ahrens 
1040*78f17100SMatthew Ahrens 		*hashp = '\0';
1041*78f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
1042*78f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
1043*78f17100SMatthew Ahrens 		*hashp = '#';
1044*78f17100SMatthew Ahrens 		if (error == ENOENT) {
1045*78f17100SMatthew Ahrens 			/*
1046*78f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
1047*78f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
1048*78f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
1049*78f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
1050*78f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
1051*78f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
1052*78f17100SMatthew Ahrens 			 */
1053*78f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
1054*78f17100SMatthew Ahrens 			error = 0;
1055*78f17100SMatthew Ahrens 		}
1056*78f17100SMatthew Ahrens 		if (error != 0)
1057*78f17100SMatthew Ahrens 			break;
1058*78f17100SMatthew Ahrens 	}
1059*78f17100SMatthew Ahrens 
1060*78f17100SMatthew Ahrens 	return (error);
1061*78f17100SMatthew Ahrens }
1062*78f17100SMatthew Ahrens 
10634445fffbSMatthew Ahrens /* ARGSUSED */
1064ecd6cf80Smarks static int
10654445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10664445fffbSMatthew Ahrens {
10674445fffbSMatthew Ahrens 	/*
10684445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10694445fffbSMatthew Ahrens 	 * to log to.
10704445fffbSMatthew Ahrens 	 */
10714445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1072be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10734445fffbSMatthew Ahrens 	return (0);
10744445fffbSMatthew Ahrens }
10754445fffbSMatthew Ahrens 
10764445fffbSMatthew Ahrens static int
10774445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1078ecd6cf80Smarks {
107992241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
108092241e0bSTom Erickson 	int	error;
10814445fffbSMatthew Ahrens 	char	*origin;
1082ecd6cf80Smarks 
1083ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1084ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1085ecd6cf80Smarks 		return (error);
1086fa9e4066Sahrens 
10874445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10884445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
10894445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
10904445fffbSMatthew Ahrens 		return (error);
1091fa9e4066Sahrens 
1092ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1093ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1094ecd6cf80Smarks 		return (error);
1095ecd6cf80Smarks 
10964445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
10974445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1098fa9e4066Sahrens }
1099fa9e4066Sahrens 
1100fa9e4066Sahrens /*
1101fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1102fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1103fa9e4066Sahrens  */
1104fa9e4066Sahrens /* ARGSUSED */
1105fa9e4066Sahrens static int
11064445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1107fa9e4066Sahrens {
1108fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1109be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1110fa9e4066Sahrens 
1111fa9e4066Sahrens 	return (0);
1112fa9e4066Sahrens }
1113fa9e4066Sahrens 
111499d5e173STim Haley /*
111599d5e173STim Haley  * Policy for object to name lookups.
111699d5e173STim Haley  */
111799d5e173STim Haley /* ARGSUSED */
111899d5e173STim Haley static int
11194445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
112099d5e173STim Haley {
112199d5e173STim Haley 	int error;
112299d5e173STim Haley 
112399d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
112499d5e173STim Haley 		return (0);
112599d5e173STim Haley 
112699d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
112799d5e173STim Haley 	return (error);
112899d5e173STim Haley }
112999d5e173STim Haley 
1130ea8dc4b6Seschrock /*
1131ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1132ea8dc4b6Seschrock  */
1133ea8dc4b6Seschrock /* ARGSUSED */
1134ea8dc4b6Seschrock static int
11354445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1136ea8dc4b6Seschrock {
1137ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1138ea8dc4b6Seschrock }
1139ea8dc4b6Seschrock 
11404445fffbSMatthew Ahrens /* ARGSUSED */
1141e45ce728Sahrens static int
11424445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1143e45ce728Sahrens {
1144e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1145e45ce728Sahrens 
1146990b4856Slling 	if (prop == ZPROP_INVAL) {
1147e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1148be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1149e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1150e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1151e45ce728Sahrens 	} else {
115292241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
115392241e0bSTom Erickson 		    NULL, cr));
1154e45ce728Sahrens 	}
1155e45ce728Sahrens }
1156e45ce728Sahrens 
115714843421SMatthew Ahrens static int
11584445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
115914843421SMatthew Ahrens {
11604445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
116114843421SMatthew Ahrens 	if (err)
116214843421SMatthew Ahrens 		return (err);
116314843421SMatthew Ahrens 
116414843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1165be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
116614843421SMatthew Ahrens 
116714843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
116814843421SMatthew Ahrens 		/*
116914843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
117014843421SMatthew Ahrens 		 * themself, allow it.
117114843421SMatthew Ahrens 		 */
117214843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
117314843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
117414843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
117514843421SMatthew Ahrens 				return (0);
117614843421SMatthew Ahrens 		} else {
117714843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
117814843421SMatthew Ahrens 				return (0);
117914843421SMatthew Ahrens 		}
118014843421SMatthew Ahrens 	}
118114843421SMatthew Ahrens 
118214843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
118314843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
118414843421SMatthew Ahrens }
118514843421SMatthew Ahrens 
118614843421SMatthew Ahrens static int
11874445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
118814843421SMatthew Ahrens {
11894445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
119014843421SMatthew Ahrens 	if (err)
119114843421SMatthew Ahrens 		return (err);
119214843421SMatthew Ahrens 
119314843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1194be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
119514843421SMatthew Ahrens 
119614843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
119714843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
119814843421SMatthew Ahrens }
119914843421SMatthew Ahrens 
12004445fffbSMatthew Ahrens /* ARGSUSED */
120114843421SMatthew Ahrens static int
12024445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120314843421SMatthew Ahrens {
120492241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
120592241e0bSTom Erickson 	    NULL, cr));
120614843421SMatthew Ahrens }
120714843421SMatthew Ahrens 
12084445fffbSMatthew Ahrens /* ARGSUSED */
1209842727c2SChris Kirby static int
12104445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1211842727c2SChris Kirby {
12123b2aab18SMatthew Ahrens 	nvpair_t *pair;
12133b2aab18SMatthew Ahrens 	nvlist_t *holds;
12143b2aab18SMatthew Ahrens 	int error;
12153b2aab18SMatthew Ahrens 
12163b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12173b2aab18SMatthew Ahrens 	if (error != 0)
1218be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12193b2aab18SMatthew Ahrens 
12203b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12213b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
12223b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
12233b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12243b2aab18SMatthew Ahrens 		if (error != 0)
12253b2aab18SMatthew Ahrens 			return (error);
12263b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12273b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12283b2aab18SMatthew Ahrens 		if (error != 0)
12293b2aab18SMatthew Ahrens 			return (error);
12303b2aab18SMatthew Ahrens 	}
12313b2aab18SMatthew Ahrens 	return (0);
1232842727c2SChris Kirby }
1233842727c2SChris Kirby 
12344445fffbSMatthew Ahrens /* ARGSUSED */
1235842727c2SChris Kirby static int
12364445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1237842727c2SChris Kirby {
12383b2aab18SMatthew Ahrens 	nvpair_t *pair;
12393b2aab18SMatthew Ahrens 	int error;
12403b2aab18SMatthew Ahrens 
12413b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12423b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
12433b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
12443b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12453b2aab18SMatthew Ahrens 		if (error != 0)
12463b2aab18SMatthew Ahrens 			return (error);
12473b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12483b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12493b2aab18SMatthew Ahrens 		if (error != 0)
12503b2aab18SMatthew Ahrens 			return (error);
12513b2aab18SMatthew Ahrens 	}
12523b2aab18SMatthew Ahrens 	return (0);
1253842727c2SChris Kirby }
1254842727c2SChris Kirby 
125599d5e173STim Haley /*
125699d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
125799d5e173STim Haley  */
125899d5e173STim Haley static int
12594445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
126099d5e173STim Haley {
126199d5e173STim Haley 	/*
126299d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
126399d5e173STim Haley 	 * hold, destroy and release all rolled into one.
126499d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
126599d5e173STim Haley 	 */
126699d5e173STim Haley 	int error;
126799d5e173STim Haley 
126899d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
126999d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
127099d5e173STim Haley 		return (0);
127199d5e173STim Haley 
12724445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12733b2aab18SMatthew Ahrens 	if (error == 0)
12744445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12753b2aab18SMatthew Ahrens 	if (error == 0)
12764445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12773b2aab18SMatthew Ahrens 	if (error == 0)
12784445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
127999d5e173STim Haley 	return (error);
128099d5e173STim Haley }
128199d5e173STim Haley 
1282fa9e4066Sahrens /*
1283fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1284fa9e4066Sahrens  */
1285fa9e4066Sahrens static int
1286478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1287fa9e4066Sahrens {
1288fa9e4066Sahrens 	char *packed;
1289fa9e4066Sahrens 	int error;
1290990b4856Slling 	nvlist_t *list = NULL;
1291fa9e4066Sahrens 
1292fa9e4066Sahrens 	/*
1293e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1294fa9e4066Sahrens 	 */
1295990b4856Slling 	if (size == 0)
1296be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1299fa9e4066Sahrens 
1300478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1301478ed9adSEric Taylor 	    iflag)) != 0) {
1302fa9e4066Sahrens 		kmem_free(packed, size);
1303fa9e4066Sahrens 		return (error);
1304fa9e4066Sahrens 	}
1305fa9e4066Sahrens 
1306990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1307fa9e4066Sahrens 		kmem_free(packed, size);
1308fa9e4066Sahrens 		return (error);
1309fa9e4066Sahrens 	}
1310fa9e4066Sahrens 
1311fa9e4066Sahrens 	kmem_free(packed, size);
1312fa9e4066Sahrens 
1313990b4856Slling 	*nvp = list;
1314fa9e4066Sahrens 	return (0);
1315fa9e4066Sahrens }
1316fa9e4066Sahrens 
13174445fffbSMatthew Ahrens /*
13184445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13194445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13204445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13214445fffbSMatthew Ahrens  * removed.
13224445fffbSMatthew Ahrens  */
132392241e0bSTom Erickson static int
13244445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
132592241e0bSTom Erickson {
132692241e0bSTom Erickson 	size_t size;
132792241e0bSTom Erickson 
13284445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
132992241e0bSTom Erickson 
13304445fffbSMatthew Ahrens 	if (size > max) {
133192241e0bSTom Erickson 		nvpair_t *more_errors;
133292241e0bSTom Erickson 		int n = 0;
133392241e0bSTom Erickson 
13344445fffbSMatthew Ahrens 		if (max < 1024)
1335be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
133692241e0bSTom Erickson 
13374445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13384445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
133992241e0bSTom Erickson 
134092241e0bSTom Erickson 		do {
13414445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
134292241e0bSTom Erickson 			    more_errors);
13434445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
134492241e0bSTom Erickson 			n++;
13454445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13464445fffbSMatthew Ahrens 		} while (size > max);
134792241e0bSTom Erickson 
13484445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13494445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13504445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
135192241e0bSTom Erickson 	}
135292241e0bSTom Erickson 
135392241e0bSTom Erickson 	return (0);
135492241e0bSTom Erickson }
135592241e0bSTom Erickson 
1356e9dbad6fSeschrock static int
1357e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1358e9dbad6fSeschrock {
1359e9dbad6fSeschrock 	char *packed = NULL;
13606e27f868SSam Falkner 	int error = 0;
1361e9dbad6fSeschrock 	size_t size;
1362e9dbad6fSeschrock 
13634445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1364e9dbad6fSeschrock 
1365e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1366be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1367e9dbad6fSeschrock 	} else {
13684445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
13696e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
13706e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1371be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
13724445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1373e9dbad6fSeschrock 	}
1374e9dbad6fSeschrock 
1375e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
13764445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1377e9dbad6fSeschrock 	return (error);
1378e9dbad6fSeschrock }
1379e9dbad6fSeschrock 
138014843421SMatthew Ahrens static int
1381af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
138214843421SMatthew Ahrens {
138314843421SMatthew Ahrens 	objset_t *os;
138414843421SMatthew Ahrens 	int error;
138514843421SMatthew Ahrens 
1386503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
13873b2aab18SMatthew Ahrens 	if (error != 0)
138814843421SMatthew Ahrens 		return (error);
1389503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1390503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1391be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1392503ad85cSMatthew Ahrens 	}
139314843421SMatthew Ahrens 
1394503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1395af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1396af4c679fSSean McEnroe 	if (*zfvp) {
1397af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
139814843421SMatthew Ahrens 	} else {
1399be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
140014843421SMatthew Ahrens 	}
1401503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1402503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
140314843421SMatthew Ahrens 	return (error);
140414843421SMatthew Ahrens }
140514843421SMatthew Ahrens 
140614843421SMatthew Ahrens /*
140714843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
140814843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1409ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1410ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
141114843421SMatthew Ahrens  */
141214843421SMatthew Ahrens static int
14131412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
141414843421SMatthew Ahrens {
141514843421SMatthew Ahrens 	int error = 0;
141614843421SMatthew Ahrens 
1417af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1418af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
141914843421SMatthew Ahrens 	if (error == 0) {
14201412a1a2SMark Shellenbaum 		rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14211412a1a2SMark Shellenbaum 		    RW_READER, tag);
1422af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
142314843421SMatthew Ahrens 			/*
142414843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
142514843421SMatthew Ahrens 			 * thread should be just about to disassociate the
142614843421SMatthew Ahrens 			 * objset from the zfsvfs.
142714843421SMatthew Ahrens 			 */
1428af4c679fSSean McEnroe 			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1429be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
143014843421SMatthew Ahrens 		}
143114843421SMatthew Ahrens 	}
143214843421SMatthew Ahrens 	return (error);
143314843421SMatthew Ahrens }
143414843421SMatthew Ahrens 
143514843421SMatthew Ahrens static void
143614843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
143714843421SMatthew Ahrens {
143814843421SMatthew Ahrens 	rrw_exit(&zfsvfs->z_teardown_lock, tag);
143914843421SMatthew Ahrens 
144014843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
144114843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
144214843421SMatthew Ahrens 	} else {
1443503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
144414843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
144514843421SMatthew Ahrens 	}
144614843421SMatthew Ahrens }
144714843421SMatthew Ahrens 
1448fa9e4066Sahrens static int
1449fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1450fa9e4066Sahrens {
1451fa9e4066Sahrens 	int error;
1452990b4856Slling 	nvlist_t *config, *props = NULL;
14530a48a24eStimh 	nvlist_t *rootprops = NULL;
14540a48a24eStimh 	nvlist_t *zplprops = NULL;
1455fa9e4066Sahrens 
1456990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1457478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1458fa9e4066Sahrens 		return (error);
14592a6b87f0Sek 
1460990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1461478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1462478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1463990b4856Slling 		nvlist_free(config);
1464990b4856Slling 		return (error);
1465990b4856Slling 	}
1466990b4856Slling 
14670a48a24eStimh 	if (props) {
14680a48a24eStimh 		nvlist_t *nvl = NULL;
14690a48a24eStimh 		uint64_t version = SPA_VERSION;
14700a48a24eStimh 
14710a48a24eStimh 		(void) nvlist_lookup_uint64(props,
14720a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1473ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1474be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
14750a48a24eStimh 			goto pool_props_bad;
14760a48a24eStimh 		}
14770a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
14780a48a24eStimh 		if (nvl) {
14790a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
14800a48a24eStimh 			if (error != 0) {
14810a48a24eStimh 				nvlist_free(config);
14820a48a24eStimh 				nvlist_free(props);
14830a48a24eStimh 				return (error);
14840a48a24eStimh 			}
14850a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
14860a48a24eStimh 		}
14870a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14880a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
14890a48a24eStimh 		    zplprops, NULL);
14903b2aab18SMatthew Ahrens 		if (error != 0)
14910a48a24eStimh 			goto pool_props_bad;
14920a48a24eStimh 	}
14930a48a24eStimh 
14944445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
14950a48a24eStimh 
14960a48a24eStimh 	/*
14970a48a24eStimh 	 * Set the remaining root properties
14980a48a24eStimh 	 */
149992241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
150092241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15010a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1502fa9e4066Sahrens 
15030a48a24eStimh pool_props_bad:
15040a48a24eStimh 	nvlist_free(rootprops);
15050a48a24eStimh 	nvlist_free(zplprops);
1506fa9e4066Sahrens 	nvlist_free(config);
15070a48a24eStimh 	nvlist_free(props);
1508990b4856Slling 
1509fa9e4066Sahrens 	return (error);
1510fa9e4066Sahrens }
1511fa9e4066Sahrens 
1512fa9e4066Sahrens static int
1513fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1514fa9e4066Sahrens {
1515ecd6cf80Smarks 	int error;
1516ecd6cf80Smarks 	zfs_log_history(zc);
1517ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1518681d9761SEric Taylor 	if (error == 0)
1519681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1520ecd6cf80Smarks 	return (error);
1521fa9e4066Sahrens }
1522fa9e4066Sahrens 
1523fa9e4066Sahrens static int
1524fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1525fa9e4066Sahrens {
1526990b4856Slling 	nvlist_t *config, *props = NULL;
1527fa9e4066Sahrens 	uint64_t guid;
1528468c413aSTim Haley 	int error;
1529fa9e4066Sahrens 
1530990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1531478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1532990b4856Slling 		return (error);
1533990b4856Slling 
1534990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1535478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1536478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1537990b4856Slling 		nvlist_free(config);
1538fa9e4066Sahrens 		return (error);
1539990b4856Slling 	}
1540fa9e4066Sahrens 
1541fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1542ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1543be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1544fa9e4066Sahrens 	else
15454b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1546fa9e4066Sahrens 
15474b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15484b964adaSGeorge Wilson 		int err;
15494b964adaSGeorge Wilson 
15504b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15514b964adaSGeorge Wilson 			error = err;
15524b964adaSGeorge Wilson 	}
1553468c413aSTim Haley 
1554fa9e4066Sahrens 	nvlist_free(config);
1555fa9e4066Sahrens 
1556990b4856Slling 	if (props)
1557990b4856Slling 		nvlist_free(props);
1558990b4856Slling 
1559fa9e4066Sahrens 	return (error);
1560fa9e4066Sahrens }
1561fa9e4066Sahrens 
1562fa9e4066Sahrens static int
1563fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1564fa9e4066Sahrens {
1565ecd6cf80Smarks 	int error;
156689a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1567394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
156889a89ebfSlling 
1569ecd6cf80Smarks 	zfs_log_history(zc);
1570394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1571681d9761SEric Taylor 	if (error == 0)
1572681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1573ecd6cf80Smarks 	return (error);
1574fa9e4066Sahrens }
1575fa9e4066Sahrens 
1576fa9e4066Sahrens static int
1577fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1578fa9e4066Sahrens {
1579fa9e4066Sahrens 	nvlist_t *configs;
1580fa9e4066Sahrens 	int error;
1581fa9e4066Sahrens 
1582fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1583be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1584fa9e4066Sahrens 
1585e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1586fa9e4066Sahrens 
1587fa9e4066Sahrens 	nvlist_free(configs);
1588fa9e4066Sahrens 
1589fa9e4066Sahrens 	return (error);
1590fa9e4066Sahrens }
1591fa9e4066Sahrens 
1592ad135b5dSChristopher Siden /*
1593ad135b5dSChristopher Siden  * inputs:
1594ad135b5dSChristopher Siden  * zc_name		name of the pool
1595ad135b5dSChristopher Siden  *
1596ad135b5dSChristopher Siden  * outputs:
1597ad135b5dSChristopher Siden  * zc_cookie		real errno
1598ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1599ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1600ad135b5dSChristopher Siden  */
1601fa9e4066Sahrens static int
1602fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1603fa9e4066Sahrens {
1604fa9e4066Sahrens 	nvlist_t *config;
1605fa9e4066Sahrens 	int error;
1606ea8dc4b6Seschrock 	int ret = 0;
1607fa9e4066Sahrens 
1608e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1609e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1610fa9e4066Sahrens 
1611fa9e4066Sahrens 	if (config != NULL) {
1612e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1613fa9e4066Sahrens 		nvlist_free(config);
1614ea8dc4b6Seschrock 
1615ea8dc4b6Seschrock 		/*
1616ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1617ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1618ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1619ea8dc4b6Seschrock 		 */
1620ea8dc4b6Seschrock 		zc->zc_cookie = error;
1621fa9e4066Sahrens 	} else {
1622ea8dc4b6Seschrock 		ret = error;
1623fa9e4066Sahrens 	}
1624fa9e4066Sahrens 
1625ea8dc4b6Seschrock 	return (ret);
1626fa9e4066Sahrens }
1627fa9e4066Sahrens 
1628fa9e4066Sahrens /*
1629fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1630fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1631fa9e4066Sahrens  */
1632fa9e4066Sahrens static int
1633fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1634fa9e4066Sahrens {
1635fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1636fa9e4066Sahrens 	int error;
1637fa9e4066Sahrens 
1638990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1639478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1640fa9e4066Sahrens 		return (error);
1641fa9e4066Sahrens 
1642fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1643fa9e4066Sahrens 
1644fa9e4066Sahrens 	nvlist_free(tryconfig);
1645fa9e4066Sahrens 
1646fa9e4066Sahrens 	if (config == NULL)
1647be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1648fa9e4066Sahrens 
1649e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1650fa9e4066Sahrens 	nvlist_free(config);
1651fa9e4066Sahrens 
1652fa9e4066Sahrens 	return (error);
1653fa9e4066Sahrens }
1654fa9e4066Sahrens 
16553f9d6ad7SLin Ling /*
16563f9d6ad7SLin Ling  * inputs:
16573f9d6ad7SLin Ling  * zc_name              name of the pool
16583f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16593f9d6ad7SLin Ling  */
1660fa9e4066Sahrens static int
16613f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1662fa9e4066Sahrens {
1663fa9e4066Sahrens 	spa_t *spa;
1664fa9e4066Sahrens 	int error;
1665fa9e4066Sahrens 
166606eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
166706eeb2adSek 		return (error);
166806eeb2adSek 
16693f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
16703f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
16713f9d6ad7SLin Ling 	else
16723f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
167306eeb2adSek 
167406eeb2adSek 	spa_close(spa, FTAG);
167506eeb2adSek 
1676fa9e4066Sahrens 	return (error);
1677fa9e4066Sahrens }
1678fa9e4066Sahrens 
1679fa9e4066Sahrens static int
1680fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1681fa9e4066Sahrens {
1682fa9e4066Sahrens 	spa_t *spa;
1683fa9e4066Sahrens 	int error;
1684fa9e4066Sahrens 
1685fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1686fa9e4066Sahrens 	if (error == 0) {
1687fa9e4066Sahrens 		spa_freeze(spa);
1688fa9e4066Sahrens 		spa_close(spa, FTAG);
1689fa9e4066Sahrens 	}
1690fa9e4066Sahrens 	return (error);
1691fa9e4066Sahrens }
1692fa9e4066Sahrens 
1693eaca9bbdSeschrock static int
1694eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1695eaca9bbdSeschrock {
1696eaca9bbdSeschrock 	spa_t *spa;
1697eaca9bbdSeschrock 	int error;
1698eaca9bbdSeschrock 
169906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
170006eeb2adSek 		return (error);
170106eeb2adSek 
1702ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1703ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1704558d2d50Slling 		spa_close(spa, FTAG);
1705be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1706558d2d50Slling 	}
1707558d2d50Slling 
1708990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
170906eeb2adSek 	spa_close(spa, FTAG);
171006eeb2adSek 
171106eeb2adSek 	return (error);
171206eeb2adSek }
171306eeb2adSek 
171406eeb2adSek static int
171506eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
171606eeb2adSek {
171706eeb2adSek 	spa_t *spa;
171806eeb2adSek 	char *hist_buf;
171906eeb2adSek 	uint64_t size;
172006eeb2adSek 	int error;
172106eeb2adSek 
172206eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1723be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
172406eeb2adSek 
172506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
172606eeb2adSek 		return (error);
172706eeb2adSek 
1728e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1729d7306b64Sek 		spa_close(spa, FTAG);
1730be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1731d7306b64Sek 	}
1732d7306b64Sek 
173306eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
173406eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
173506eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1736478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1737478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1738478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
173906eeb2adSek 	}
174006eeb2adSek 
174106eeb2adSek 	spa_close(spa, FTAG);
174206eeb2adSek 	kmem_free(hist_buf, size);
174306eeb2adSek 	return (error);
174406eeb2adSek }
174506eeb2adSek 
1746e9103aaeSGarrett D'Amore static int
1747e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1748e9103aaeSGarrett D'Amore {
1749e9103aaeSGarrett D'Amore 	spa_t *spa;
1750e9103aaeSGarrett D'Amore 	int error;
1751e9103aaeSGarrett D'Amore 
1752e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1753e9103aaeSGarrett D'Amore 	if (error == 0) {
1754e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1755e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1756e9103aaeSGarrett D'Amore 	}
1757e9103aaeSGarrett D'Amore 	return (error);
1758e9103aaeSGarrett D'Amore }
1759e9103aaeSGarrett D'Amore 
176055434c77Sek static int
176155434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
176255434c77Sek {
17633b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
176455434c77Sek }
176555434c77Sek 
1766503ad85cSMatthew Ahrens /*
1767503ad85cSMatthew Ahrens  * inputs:
1768503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1769503ad85cSMatthew Ahrens  * zc_obj		object to find
1770503ad85cSMatthew Ahrens  *
1771503ad85cSMatthew Ahrens  * outputs:
1772503ad85cSMatthew Ahrens  * zc_value		name of object
1773503ad85cSMatthew Ahrens  */
177455434c77Sek static int
177555434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
177655434c77Sek {
1777503ad85cSMatthew Ahrens 	objset_t *os;
177855434c77Sek 	int error;
177955434c77Sek 
1780503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1781503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
178255434c77Sek 		return (error);
1783503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1784503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1785be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1786503ad85cSMatthew Ahrens 	}
1787503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
178855434c77Sek 	    sizeof (zc->zc_value));
1789503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
179055434c77Sek 
179155434c77Sek 	return (error);
179255434c77Sek }
179355434c77Sek 
179499d5e173STim Haley /*
179599d5e173STim Haley  * inputs:
179699d5e173STim Haley  * zc_name		name of filesystem
179799d5e173STim Haley  * zc_obj		object to find
179899d5e173STim Haley  *
179999d5e173STim Haley  * outputs:
180099d5e173STim Haley  * zc_stat		stats on object
180199d5e173STim Haley  * zc_value		path to object
180299d5e173STim Haley  */
180399d5e173STim Haley static int
180499d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
180599d5e173STim Haley {
180699d5e173STim Haley 	objset_t *os;
180799d5e173STim Haley 	int error;
180899d5e173STim Haley 
180999d5e173STim Haley 	/* XXX reading from objset not owned */
181099d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
181199d5e173STim Haley 		return (error);
181299d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
181399d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1814be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
181599d5e173STim Haley 	}
181699d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
181799d5e173STim Haley 	    sizeof (zc->zc_value));
181899d5e173STim Haley 	dmu_objset_rele(os, FTAG);
181999d5e173STim Haley 
182099d5e173STim Haley 	return (error);
182199d5e173STim Haley }
182299d5e173STim Haley 
1823fa9e4066Sahrens static int
1824fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1825fa9e4066Sahrens {
1826fa9e4066Sahrens 	spa_t *spa;
1827fa9e4066Sahrens 	int error;
1828e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1829e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1830fa9e4066Sahrens 
1831fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1832fa9e4066Sahrens 	if (error != 0)
1833fa9e4066Sahrens 		return (error);
1834fa9e4066Sahrens 
1835fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1836478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1837fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1838fa94a07fSbrendan 	    &l2cache, &nl2cache);
1839fa94a07fSbrendan 
1840e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1841e7cbe64fSgw 	    &spares, &nspares);
1842e7cbe64fSgw 
1843b1b8ab34Slling 	/*
1844b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1845e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1846e7cbe64fSgw 	 *
1847e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1848e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1849e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1850e7cbe64fSgw 	 *
1851e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1852b1b8ab34Slling 	 */
1853b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
18541195e687SMark J Musante 		nvlist_free(config);
1855b1b8ab34Slling 		spa_close(spa, FTAG);
1856be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1857b1b8ab34Slling 	}
1858b1b8ab34Slling 
1859fa94a07fSbrendan 	if (error == 0) {
1860fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1861fa9e4066Sahrens 		nvlist_free(config);
1862fa9e4066Sahrens 	}
1863fa9e4066Sahrens 	spa_close(spa, FTAG);
1864fa9e4066Sahrens 	return (error);
1865fa9e4066Sahrens }
1866fa9e4066Sahrens 
18673f9d6ad7SLin Ling /*
18683f9d6ad7SLin Ling  * inputs:
18693f9d6ad7SLin Ling  * zc_name		name of the pool
18703f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
18713f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
18723f9d6ad7SLin Ling  */
1873fa9e4066Sahrens static int
1874fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1875fa9e4066Sahrens {
187699653d4eSeschrock 	spa_t *spa;
187799653d4eSeschrock 	int error;
187899653d4eSeschrock 
187999653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
188099653d4eSeschrock 	if (error != 0)
188199653d4eSeschrock 		return (error);
188299653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
188399653d4eSeschrock 	spa_close(spa, FTAG);
188499653d4eSeschrock 	return (error);
1885fa9e4066Sahrens }
1886fa9e4066Sahrens 
1887fa9e4066Sahrens static int
18883d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1889fa9e4066Sahrens {
1890fa9e4066Sahrens 	spa_t *spa;
1891fa9e4066Sahrens 	int error;
18923d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1893fa9e4066Sahrens 
189406eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1895fa9e4066Sahrens 		return (error);
18963d7072f8Seschrock 	switch (zc->zc_cookie) {
18973d7072f8Seschrock 	case VDEV_STATE_ONLINE:
18983d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
18993d7072f8Seschrock 		break;
1900fa9e4066Sahrens 
19013d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19023d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19033d7072f8Seschrock 		break;
1904fa9e4066Sahrens 
19053d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1906069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1907069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1908069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1909069f55e2SEric Schrock 
1910069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19113d7072f8Seschrock 		break;
19123d7072f8Seschrock 
19133d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1914069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1915069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1916069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1917069f55e2SEric Schrock 
1918069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19193d7072f8Seschrock 		break;
19203d7072f8Seschrock 
19213d7072f8Seschrock 	default:
1922be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
19233d7072f8Seschrock 	}
19243d7072f8Seschrock 	zc->zc_cookie = newstate;
1925fa9e4066Sahrens 	spa_close(spa, FTAG);
1926fa9e4066Sahrens 	return (error);
1927fa9e4066Sahrens }
1928fa9e4066Sahrens 
1929fa9e4066Sahrens static int
1930fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1931fa9e4066Sahrens {
1932fa9e4066Sahrens 	spa_t *spa;
1933fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1934fa9e4066Sahrens 	nvlist_t *config;
1935fa9e4066Sahrens 	int error;
1936fa9e4066Sahrens 
193706eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1938fa9e4066Sahrens 		return (error);
1939fa9e4066Sahrens 
1940990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1941478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1942ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1943fa9e4066Sahrens 		nvlist_free(config);
1944fa9e4066Sahrens 	}
1945fa9e4066Sahrens 
1946fa9e4066Sahrens 	spa_close(spa, FTAG);
1947fa9e4066Sahrens 	return (error);
1948fa9e4066Sahrens }
1949fa9e4066Sahrens 
1950fa9e4066Sahrens static int
1951fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1952fa9e4066Sahrens {
1953fa9e4066Sahrens 	spa_t *spa;
1954fa9e4066Sahrens 	int error;
1955fa9e4066Sahrens 
195606eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1957fa9e4066Sahrens 		return (error);
1958fa9e4066Sahrens 
19598ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1960fa9e4066Sahrens 
1961fa9e4066Sahrens 	spa_close(spa, FTAG);
1962fa9e4066Sahrens 	return (error);
1963fa9e4066Sahrens }
1964fa9e4066Sahrens 
19651195e687SMark J Musante static int
19661195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
19671195e687SMark J Musante {
19681195e687SMark J Musante 	spa_t *spa;
19691195e687SMark J Musante 	nvlist_t *config, *props = NULL;
19701195e687SMark J Musante 	int error;
19711195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
19721195e687SMark J Musante 
19731195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
19741195e687SMark J Musante 		return (error);
19751195e687SMark J Musante 
19761195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
19771195e687SMark J Musante 	    zc->zc_iflags, &config)) {
19781195e687SMark J Musante 		spa_close(spa, FTAG);
19791195e687SMark J Musante 		return (error);
19801195e687SMark J Musante 	}
19811195e687SMark J Musante 
19821195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
19831195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19841195e687SMark J Musante 	    zc->zc_iflags, &props))) {
19851195e687SMark J Musante 		spa_close(spa, FTAG);
19861195e687SMark J Musante 		nvlist_free(config);
19871195e687SMark J Musante 		return (error);
19881195e687SMark J Musante 	}
19891195e687SMark J Musante 
19901195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
19911195e687SMark J Musante 
19921195e687SMark J Musante 	spa_close(spa, FTAG);
19931195e687SMark J Musante 
19941195e687SMark J Musante 	nvlist_free(config);
19951195e687SMark J Musante 	nvlist_free(props);
19961195e687SMark J Musante 
19971195e687SMark J Musante 	return (error);
19981195e687SMark J Musante }
19991195e687SMark J Musante 
2000c67d9675Seschrock static int
2001c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2002c67d9675Seschrock {
2003c67d9675Seschrock 	spa_t *spa;
2004e9dbad6fSeschrock 	char *path = zc->zc_value;
2005ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2006c67d9675Seschrock 	int error;
2007c67d9675Seschrock 
2008c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2009c67d9675Seschrock 	if (error != 0)
2010c67d9675Seschrock 		return (error);
2011c67d9675Seschrock 
2012c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2013c67d9675Seschrock 	spa_close(spa, FTAG);
2014c67d9675Seschrock 	return (error);
2015c67d9675Seschrock }
2016c67d9675Seschrock 
20176809eb4eSEric Schrock static int
20186809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20196809eb4eSEric Schrock {
20206809eb4eSEric Schrock 	spa_t *spa;
20216809eb4eSEric Schrock 	char *fru = zc->zc_value;
20226809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20236809eb4eSEric Schrock 	int error;
20246809eb4eSEric Schrock 
20256809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20266809eb4eSEric Schrock 	if (error != 0)
20276809eb4eSEric Schrock 		return (error);
20286809eb4eSEric Schrock 
20296809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20306809eb4eSEric Schrock 	spa_close(spa, FTAG);
20316809eb4eSEric Schrock 	return (error);
20326809eb4eSEric Schrock }
20336809eb4eSEric Schrock 
2034fa9e4066Sahrens static int
2035a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2036fa9e4066Sahrens {
2037a7f53a56SChris Kirby 	int error = 0;
20387f7322feSeschrock 	nvlist_t *nv;
2039fa9e4066Sahrens 
2040a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2041fa9e4066Sahrens 
20425ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
204392241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2044a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2045432f72fdSahrens 		/*
2046bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
2047432f72fdSahrens 		 * which we aren't supposed to do with a
2048745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2049432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2050503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2051432f72fdSahrens 		 */
205219b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
205319b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
205419b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
205519b94df9SMatthew Ahrens 			if (error == EIO)
205619b94df9SMatthew Ahrens 				return (error);
2057fb09f5aaSMadhav Suresh 			VERIFY0(error);
2058e7437265Sahrens 		}
2059e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
20607f7322feSeschrock 		nvlist_free(nv);
20617f7322feSeschrock 	}
2062fa9e4066Sahrens 
2063a7f53a56SChris Kirby 	return (error);
2064a7f53a56SChris Kirby }
2065a7f53a56SChris Kirby 
2066a7f53a56SChris Kirby /*
2067a7f53a56SChris Kirby  * inputs:
2068a7f53a56SChris Kirby  * zc_name		name of filesystem
2069a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2070a7f53a56SChris Kirby  *
2071a7f53a56SChris Kirby  * outputs:
2072a7f53a56SChris Kirby  * zc_objset_stats	stats
2073a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2074a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2075a7f53a56SChris Kirby  */
2076a7f53a56SChris Kirby static int
2077a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2078a7f53a56SChris Kirby {
20793b2aab18SMatthew Ahrens 	objset_t *os;
2080a7f53a56SChris Kirby 	int error;
2081a7f53a56SChris Kirby 
20823b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
20833b2aab18SMatthew Ahrens 	if (error == 0) {
20843b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
20853b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
20863b2aab18SMatthew Ahrens 	}
2087a7f53a56SChris Kirby 
2088fa9e4066Sahrens 	return (error);
2089fa9e4066Sahrens }
2090fa9e4066Sahrens 
209192241e0bSTom Erickson /*
209292241e0bSTom Erickson  * inputs:
209392241e0bSTom Erickson  * zc_name		name of filesystem
209492241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
209592241e0bSTom Erickson  *
209692241e0bSTom Erickson  * outputs:
209792241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
209892241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
209992241e0bSTom Erickson  *
210092241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
210192241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
210292241e0bSTom Erickson  * local property values.
210392241e0bSTom Erickson  */
210492241e0bSTom Erickson static int
210592241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
210692241e0bSTom Erickson {
21073b2aab18SMatthew Ahrens 	int error = 0;
210892241e0bSTom Erickson 	nvlist_t *nv;
210992241e0bSTom Erickson 
211092241e0bSTom Erickson 	/*
211192241e0bSTom Erickson 	 * Without this check, we would return local property values if the
211292241e0bSTom Erickson 	 * caller has not already received properties on or after
211392241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
211492241e0bSTom Erickson 	 */
21153b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2116be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
211792241e0bSTom Erickson 
211892241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21193b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
212092241e0bSTom Erickson 		error = put_nvlist(zc, nv);
212192241e0bSTom Erickson 		nvlist_free(nv);
212292241e0bSTom Erickson 	}
212392241e0bSTom Erickson 
212492241e0bSTom Erickson 	return (error);
212592241e0bSTom Erickson }
212692241e0bSTom Erickson 
2127de8267e0Stimh static int
2128de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2129de8267e0Stimh {
2130de8267e0Stimh 	uint64_t value;
2131de8267e0Stimh 	int error;
2132de8267e0Stimh 
2133de8267e0Stimh 	/*
2134de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2135de8267e0Stimh 	 * the default value (if there is one).
2136de8267e0Stimh 	 */
2137de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2138de8267e0Stimh 		return (error);
2139de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2140de8267e0Stimh 	return (0);
2141de8267e0Stimh }
2142de8267e0Stimh 
21433cb34c60Sahrens /*
21443cb34c60Sahrens  * inputs:
21453cb34c60Sahrens  * zc_name		name of filesystem
2146de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
21473cb34c60Sahrens  *
21483cb34c60Sahrens  * outputs:
2149de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2150de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
21513cb34c60Sahrens  */
2152bd00f61bSrm static int
2153de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2154bd00f61bSrm {
2155de8267e0Stimh 	objset_t *os;
2156de8267e0Stimh 	int err;
2157bd00f61bSrm 
2158503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2159503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2160de8267e0Stimh 		return (err);
2161bd00f61bSrm 
2162bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2163bd00f61bSrm 
2164bd00f61bSrm 	/*
2165de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2166745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2167745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2168bd00f61bSrm 	 */
2169de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2170de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2171de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2172de8267e0Stimh 		nvlist_t *nv;
2173de8267e0Stimh 
2174de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2175de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2176de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2177de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2178de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2179de8267e0Stimh 			err = put_nvlist(zc, nv);
2180de8267e0Stimh 		nvlist_free(nv);
2181de8267e0Stimh 	} else {
2182be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2183de8267e0Stimh 	}
2184503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2185de8267e0Stimh 	return (err);
2186bd00f61bSrm }
2187bd00f61bSrm 
218814843421SMatthew Ahrens static boolean_t
218914843421SMatthew Ahrens dataset_name_hidden(const char *name)
219014843421SMatthew Ahrens {
219114843421SMatthew Ahrens 	/*
219214843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
219314843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
219414843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
219514843421SMatthew Ahrens 	 */
219614843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
219714843421SMatthew Ahrens 		return (B_TRUE);
219814843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
219914843421SMatthew Ahrens 		return (B_TRUE);
220014843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
220114843421SMatthew Ahrens 		return (B_TRUE);
220214843421SMatthew Ahrens 	return (B_FALSE);
220314843421SMatthew Ahrens }
220414843421SMatthew Ahrens 
2205de8267e0Stimh /*
2206de8267e0Stimh  * inputs:
2207de8267e0Stimh  * zc_name		name of filesystem
2208de8267e0Stimh  * zc_cookie		zap cursor
2209de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2210de8267e0Stimh  *
2211de8267e0Stimh  * outputs:
2212de8267e0Stimh  * zc_name		name of next filesystem
221314843421SMatthew Ahrens  * zc_cookie		zap cursor
2214de8267e0Stimh  * zc_objset_stats	stats
2215de8267e0Stimh  * zc_nvlist_dst	property nvlist
2216de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2217de8267e0Stimh  */
2218fa9e4066Sahrens static int
2219fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2220fa9e4066Sahrens {
222187e5029aSahrens 	objset_t *os;
2222fa9e4066Sahrens 	int error;
2223fa9e4066Sahrens 	char *p;
2224620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2225fa9e4066Sahrens 
2226620252bcSChris Kirby top:
2227503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
222887e5029aSahrens 		if (error == ENOENT)
2229be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
223087e5029aSahrens 		return (error);
2231fa9e4066Sahrens 	}
2232fa9e4066Sahrens 
2233fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2234fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2235fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2236fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2237fa9e4066Sahrens 
2238fa9e4066Sahrens 	do {
223987e5029aSahrens 		error = dmu_dir_list_next(os,
224087e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
224187e5029aSahrens 		    NULL, &zc->zc_cookie);
2242fa9e4066Sahrens 		if (error == ENOENT)
2243be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
224419b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2245503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2246fa9e4066Sahrens 
2247681d9761SEric Taylor 	/*
2248681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2249681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2250681d9761SEric Taylor 	 */
2251620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
225287e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2253620252bcSChris Kirby 		if (error == ENOENT) {
2254620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2255620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2256620252bcSChris Kirby 			goto top;
2257620252bcSChris Kirby 		}
2258620252bcSChris Kirby 	}
2259fa9e4066Sahrens 	return (error);
2260fa9e4066Sahrens }
2261fa9e4066Sahrens 
22623cb34c60Sahrens /*
22633cb34c60Sahrens  * inputs:
22643cb34c60Sahrens  * zc_name		name of filesystem
22653cb34c60Sahrens  * zc_cookie		zap cursor
22663cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22673cb34c60Sahrens  *
22683cb34c60Sahrens  * outputs:
22693cb34c60Sahrens  * zc_name		name of next snapshot
22703cb34c60Sahrens  * zc_objset_stats	stats
22713cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22723cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22733cb34c60Sahrens  */
2274fa9e4066Sahrens static int
2275fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2276fa9e4066Sahrens {
227787e5029aSahrens 	objset_t *os;
2278fa9e4066Sahrens 	int error;
2279fa9e4066Sahrens 
2280503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
22813b2aab18SMatthew Ahrens 	if (error != 0) {
2282745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
22833b2aab18SMatthew Ahrens 	}
2284fa9e4066Sahrens 
2285b81d61a6Slling 	/*
2286b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2287b81d61a6Slling 	 * so exit immediately.
2288b81d61a6Slling 	 */
2289b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2290503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2291be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2292fa9e4066Sahrens 	}
2293fa9e4066Sahrens 
229487e5029aSahrens 	error = dmu_snapshot_list_next(os,
229587e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2296a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2297a7f53a56SChris Kirby 	    NULL);
2298a7f53a56SChris Kirby 
2299620252bcSChris Kirby 	if (error == 0) {
2300a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2301a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2302a7f53a56SChris Kirby 
2303a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23043b2aab18SMatthew Ahrens 		if (error == 0) {
2305a7f53a56SChris Kirby 			objset_t *ossnap;
2306a7f53a56SChris Kirby 
2307a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2308a7f53a56SChris Kirby 			if (error == 0)
2309a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2310a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2311620252bcSChris Kirby 		}
2312620252bcSChris Kirby 	} else if (error == ENOENT) {
2313be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2314620252bcSChris Kirby 	}
2315fa9e4066Sahrens 
2316a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23173cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23183b2aab18SMatthew Ahrens 	if (error != 0)
23193cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2320fa9e4066Sahrens 	return (error);
2321fa9e4066Sahrens }
2322fa9e4066Sahrens 
232392241e0bSTom Erickson static int
232492241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2325fa9e4066Sahrens {
232692241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
232792241e0bSTom Erickson 	uint64_t *valary;
232892241e0bSTom Erickson 	unsigned int vallen;
232992241e0bSTom Erickson 	const char *domain;
2330eeb85002STim Haley 	char *dash;
233192241e0bSTom Erickson 	zfs_userquota_prop_t type;
233292241e0bSTom Erickson 	uint64_t rid;
233392241e0bSTom Erickson 	uint64_t quota;
233492241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
233592241e0bSTom Erickson 	int err;
233692241e0bSTom Erickson 
233792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
233892241e0bSTom Erickson 		nvlist_t *attrs;
233992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2340eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2341eeb85002STim Haley 		    &pair) != 0)
2342be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
234392241e0bSTom Erickson 	}
2344e9dbad6fSeschrock 
2345ecd6cf80Smarks 	/*
2346eeb85002STim Haley 	 * A correctly constructed propname is encoded as
234792241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2348ecd6cf80Smarks 	 */
2349eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2350eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2351eeb85002STim Haley 	    vallen != 3)
2352be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2353eeb85002STim Haley 
2354eeb85002STim Haley 	domain = dash + 1;
2355eeb85002STim Haley 	type = valary[0];
2356eeb85002STim Haley 	rid = valary[1];
2357eeb85002STim Haley 	quota = valary[2];
2358e9dbad6fSeschrock 
23591412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
236092241e0bSTom Erickson 	if (err == 0) {
236192241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
236292241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
236392241e0bSTom Erickson 	}
2364e9dbad6fSeschrock 
236592241e0bSTom Erickson 	return (err);
236692241e0bSTom Erickson }
236714843421SMatthew Ahrens 
236892241e0bSTom Erickson /*
236992241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
237092241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
237192241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
237292241e0bSTom Erickson  *
2373eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
237492241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
237592241e0bSTom Erickson  */
237692241e0bSTom Erickson static int
237792241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
237892241e0bSTom Erickson     nvpair_t *pair)
237992241e0bSTom Erickson {
238092241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
238192241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
238292241e0bSTom Erickson 	uint64_t intval;
238392241e0bSTom Erickson 	int err;
2384fa9e4066Sahrens 
238592241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
238692241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
238792241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
238892241e0bSTom Erickson 		return (-1);
238992241e0bSTom Erickson 	}
239014843421SMatthew Ahrens 
239192241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
239292241e0bSTom Erickson 		nvlist_t *attrs;
239392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
239492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
239592241e0bSTom Erickson 		    &pair) == 0);
239692241e0bSTom Erickson 	}
2397db870a07Sahrens 
239892241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
239992241e0bSTom Erickson 		return (-1);
2400b24ab676SJeff Bonwick 
240192241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
240240feaa91Sahrens 
240392241e0bSTom Erickson 	switch (prop) {
240492241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
240592241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
240692241e0bSTom Erickson 		break;
240792241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
24083b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
240992241e0bSTom Erickson 		break;
241092241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
241192241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
241292241e0bSTom Erickson 		break;
241392241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
24143b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
241592241e0bSTom Erickson 		break;
241692241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2417c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
241892241e0bSTom Erickson 		break;
241992241e0bSTom Erickson 	case ZFS_PROP_VERSION:
242092241e0bSTom Erickson 	{
242192241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
24229e6eda55Smarks 
24231412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2424b24ab676SJeff Bonwick 			break;
2425b24ab676SJeff Bonwick 
242692241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
242792241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2428d0f3f37eSMark Shellenbaum 
242992241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2430b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2431b16da2e2SGeorge Wilson 
2432b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2433b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2434b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2435b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
243640feaa91Sahrens 		}
243792241e0bSTom Erickson 		break;
2438ecd6cf80Smarks 	}
2439a6f561b4SSašo Kiselkov 	case ZFS_PROP_COMPRESSION:
2440a6f561b4SSašo Kiselkov 	{
2441a6f561b4SSašo Kiselkov 		if (intval == ZIO_COMPRESS_LZ4) {
2442a6f561b4SSašo Kiselkov 			spa_t *spa;
2443a6f561b4SSašo Kiselkov 
2444a6f561b4SSašo Kiselkov 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2445a6f561b4SSašo Kiselkov 				return (err);
2446a6f561b4SSašo Kiselkov 
2447a6f561b4SSašo Kiselkov 			/*
2448a6f561b4SSašo Kiselkov 			 * Setting the LZ4 compression algorithm activates
2449a6f561b4SSašo Kiselkov 			 * the feature.
2450a6f561b4SSašo Kiselkov 			 */
24512acef22dSMatthew Ahrens 			if (!spa_feature_is_active(spa,
24522acef22dSMatthew Ahrens 			    SPA_FEATURE_LZ4_COMPRESS)) {
24533b2aab18SMatthew Ahrens 				if ((err = zfs_prop_activate_feature(spa,
24542acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) != 0) {
2455a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
2456a6f561b4SSašo Kiselkov 					return (err);
2457a6f561b4SSašo Kiselkov 				}
2458a6f561b4SSašo Kiselkov 			}
2459a6f561b4SSašo Kiselkov 
2460a6f561b4SSašo Kiselkov 			spa_close(spa, FTAG);
2461a6f561b4SSašo Kiselkov 		}
2462a6f561b4SSašo Kiselkov 		/*
2463a6f561b4SSašo Kiselkov 		 * We still want the default set action to be performed in the
2464a6f561b4SSašo Kiselkov 		 * caller, we only performed zfeature settings here.
2465a6f561b4SSašo Kiselkov 		 */
2466a6f561b4SSašo Kiselkov 		err = -1;
2467a6f561b4SSašo Kiselkov 		break;
2468a6f561b4SSašo Kiselkov 	}
2469ecd6cf80Smarks 
247092241e0bSTom Erickson 	default:
247192241e0bSTom Erickson 		err = -1;
247292241e0bSTom Erickson 	}
2473e9dbad6fSeschrock 
247492241e0bSTom Erickson 	return (err);
247592241e0bSTom Erickson }
2476a9799022Sck 
247792241e0bSTom Erickson /*
247892241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
24794445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
24804445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
24814445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
24824445fffbSMatthew Ahrens  * corresponding error numbers.
248392241e0bSTom Erickson  *
24844445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
24854445fffbSMatthew Ahrens  * modified.
248692241e0bSTom Erickson  */
248792241e0bSTom Erickson int
248892241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
24894445fffbSMatthew Ahrens     nvlist_t *errlist)
249092241e0bSTom Erickson {
249192241e0bSTom Erickson 	nvpair_t *pair;
249292241e0bSTom Erickson 	nvpair_t *propval;
249302e383d1STom Erickson 	int rv = 0;
249492241e0bSTom Erickson 	uint64_t intval;
249592241e0bSTom Erickson 	char *strval;
24964445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
24974445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2498a9799022Sck 
249992241e0bSTom Erickson retry:
250092241e0bSTom Erickson 	pair = NULL;
250192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
250292241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
250392241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2504cfa69fd2STom Erickson 		int err = 0;
2505e9dbad6fSeschrock 
250692241e0bSTom Erickson 		/* decode the property value */
250792241e0bSTom Erickson 		propval = pair;
250892241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
250992241e0bSTom Erickson 			nvlist_t *attrs;
25104445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2511eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2512eeb85002STim Haley 			    &propval) != 0)
2513be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
251414843421SMatthew Ahrens 		}
2515e9dbad6fSeschrock 
251692241e0bSTom Erickson 		/* Validate value type */
2517eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
251892241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
251992241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2520be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
252192241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
252292241e0bSTom Erickson 				if (nvpair_type(propval) !=
252392241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2524be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
252519b94df9SMatthew Ahrens 			} else {
2526be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
25274201a95eSRic Aleshire 			}
2528eeb85002STim Haley 		} else if (err == 0) {
252992241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
253092241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2531be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
253292241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2533a2eea2e1Sahrens 				const char *unused;
2534a2eea2e1Sahrens 
25354445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2536e9dbad6fSeschrock 
2537e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
253891ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2539e9dbad6fSeschrock 					break;
254091ebeef5Sahrens 				case PROP_TYPE_STRING:
2541be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
254292241e0bSTom Erickson 					break;
254391ebeef5Sahrens 				case PROP_TYPE_INDEX:
2544acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
254592241e0bSTom Erickson 					    intval, &unused) != 0)
2546be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2547e9dbad6fSeschrock 					break;
2548e9dbad6fSeschrock 				default:
2549e7437265Sahrens 					cmn_err(CE_PANIC,
2550e7437265Sahrens 					    "unknown property type");
2551e9dbad6fSeschrock 				}
2552e9dbad6fSeschrock 			} else {
2553be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2554e9dbad6fSeschrock 			}
2555e9dbad6fSeschrock 		}
255692241e0bSTom Erickson 
255792241e0bSTom Erickson 		/* Validate permissions */
255892241e0bSTom Erickson 		if (err == 0)
255992241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
256092241e0bSTom Erickson 
256192241e0bSTom Erickson 		if (err == 0) {
256292241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
256392241e0bSTom Erickson 			if (err == -1) {
256492241e0bSTom Erickson 				/*
256592241e0bSTom Erickson 				 * For better performance we build up a list of
256692241e0bSTom Erickson 				 * properties to set in a single transaction.
256792241e0bSTom Erickson 				 */
256892241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
256992241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
257092241e0bSTom Erickson 				/*
257192241e0bSTom Erickson 				 * This may be a spurious error caused by
257292241e0bSTom Erickson 				 * receiving quota and reservation out of order.
257392241e0bSTom Erickson 				 * Try again in a second pass.
257492241e0bSTom Erickson 				 */
257592241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
257692241e0bSTom Erickson 			}
257792241e0bSTom Erickson 		}
257892241e0bSTom Erickson 
25794445fffbSMatthew Ahrens 		if (err != 0) {
25804445fffbSMatthew Ahrens 			if (errlist != NULL)
25814445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
25824445fffbSMatthew Ahrens 			rv = err;
25834445fffbSMatthew Ahrens 		}
2584e9dbad6fSeschrock 	}
2585e9dbad6fSeschrock 
258692241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
258792241e0bSTom Erickson 		nvl = retrynvl;
258892241e0bSTom Erickson 		goto retry;
258992241e0bSTom Erickson 	}
259092241e0bSTom Erickson 
259192241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
259292241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
259392241e0bSTom Erickson 		/*
259492241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
259592241e0bSTom Erickson 		 * can, so try setting them individually.
259692241e0bSTom Erickson 		 */
259792241e0bSTom Erickson 		pair = NULL;
259892241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
259992241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2600cfa69fd2STom Erickson 			int err = 0;
260192241e0bSTom Erickson 
260292241e0bSTom Erickson 			propval = pair;
260392241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
260492241e0bSTom Erickson 				nvlist_t *attrs;
26054445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
26064445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
26074445fffbSMatthew Ahrens 				    ZPROP_VALUE);
260892241e0bSTom Erickson 			}
260992241e0bSTom Erickson 
261092241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26114445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26123b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26133b2aab18SMatthew Ahrens 				    source, strval);
261492241e0bSTom Erickson 			} else {
26154445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26163b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26173b2aab18SMatthew Ahrens 				    intval);
261892241e0bSTom Erickson 			}
261992241e0bSTom Erickson 
262092241e0bSTom Erickson 			if (err != 0) {
26214445fffbSMatthew Ahrens 				if (errlist != NULL) {
26224445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26234445fffbSMatthew Ahrens 					    err);
26244445fffbSMatthew Ahrens 				}
26254445fffbSMatthew Ahrens 				rv = err;
262692241e0bSTom Erickson 			}
262792241e0bSTom Erickson 		}
26285c0b6a79SRich Morris 	}
26295c0b6a79SRich Morris 	nvlist_free(genericnvl);
263092241e0bSTom Erickson 	nvlist_free(retrynvl);
263192241e0bSTom Erickson 
263292241e0bSTom Erickson 	return (rv);
2633fa9e4066Sahrens }
2634fa9e4066Sahrens 
2635ea2f5b9eSMatthew Ahrens /*
2636ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2637ea2f5b9eSMatthew Ahrens  */
2638ea2f5b9eSMatthew Ahrens static int
26394445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2640ea2f5b9eSMatthew Ahrens {
264192241e0bSTom Erickson 	nvpair_t *pair = NULL;
2642ea2f5b9eSMatthew Ahrens 	int error = 0;
2643ea2f5b9eSMatthew Ahrens 
264492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
264592241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2646ea2f5b9eSMatthew Ahrens 
2647ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
264892241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2649be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2650ea2f5b9eSMatthew Ahrens 
2651ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2652ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2653ea2f5b9eSMatthew Ahrens 			return (error);
2654ea2f5b9eSMatthew Ahrens 
2655ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2656be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2657ea2f5b9eSMatthew Ahrens 
2658*78f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2659ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2660ea2f5b9eSMatthew Ahrens 	}
2661ea2f5b9eSMatthew Ahrens 	return (0);
2662ea2f5b9eSMatthew Ahrens }
2663ea2f5b9eSMatthew Ahrens 
266492241e0bSTom Erickson static void
266592241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
266692241e0bSTom Erickson {
266792241e0bSTom Erickson 	nvpair_t *pair;
266892241e0bSTom Erickson 
266992241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
267092241e0bSTom Erickson 
267192241e0bSTom Erickson 	pair = NULL;
267292241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
267392241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
267492241e0bSTom Erickson 			continue;
267592241e0bSTom Erickson 
267692241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
267792241e0bSTom Erickson 	}
267892241e0bSTom Erickson }
267992241e0bSTom Erickson 
268092241e0bSTom Erickson static int
26813b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
268292241e0bSTom Erickson     nvlist_t *skipped)
268392241e0bSTom Erickson {
268492241e0bSTom Erickson 	int err = 0;
268592241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
268692241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
268792241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
268892241e0bSTom Erickson 		/*
268992241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
269092241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
269192241e0bSTom Erickson 		 */
269292241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
26933b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
26943b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
269592241e0bSTom Erickson 	}
269692241e0bSTom Erickson 	nvlist_free(cleared_props);
269792241e0bSTom Erickson 	return (err);
269892241e0bSTom Erickson }
269992241e0bSTom Erickson 
27003cb34c60Sahrens /*
27013cb34c60Sahrens  * inputs:
27023cb34c60Sahrens  * zc_name		name of filesystem
27035c0b6a79SRich Morris  * zc_value		name of property to set
27043cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
270592241e0bSTom Erickson  * zc_cookie		received properties flag
27063cb34c60Sahrens  *
270792241e0bSTom Erickson  * outputs:
270892241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
27093cb34c60Sahrens  */
2710fa9e4066Sahrens static int
2711e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2712fa9e4066Sahrens {
2713e9dbad6fSeschrock 	nvlist_t *nvl;
271492241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
271592241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
271692241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27174445fffbSMatthew Ahrens 	nvlist_t *errors;
2718e9dbad6fSeschrock 	int error;
2719e9dbad6fSeschrock 
2720990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2721478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2722e9dbad6fSeschrock 		return (error);
2723e9dbad6fSeschrock 
272492241e0bSTom Erickson 	if (received) {
2725bb0ade09Sahrens 		nvlist_t *origprops;
2726bb0ade09Sahrens 
27273b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27283b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27293b2aab18SMatthew Ahrens 			    origprops, nvl);
27303b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2731bb0ade09Sahrens 		}
27323b2aab18SMatthew Ahrens 
27333b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2734bb0ade09Sahrens 	}
2735bb0ade09Sahrens 
27364445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27373b2aab18SMatthew Ahrens 	if (error == 0)
27383b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
273992241e0bSTom Erickson 
274092241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
274192241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
274292241e0bSTom Erickson 	}
2743ecd6cf80Smarks 
274492241e0bSTom Erickson 	nvlist_free(errors);
2745e9dbad6fSeschrock 	nvlist_free(nvl);
2746e9dbad6fSeschrock 	return (error);
2747fa9e4066Sahrens }
2748fa9e4066Sahrens 
27493cb34c60Sahrens /*
27503cb34c60Sahrens  * inputs:
27513cb34c60Sahrens  * zc_name		name of filesystem
27523cb34c60Sahrens  * zc_value		name of property to inherit
275392241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
27543cb34c60Sahrens  *
27553cb34c60Sahrens  * outputs:		none
27563cb34c60Sahrens  */
2757e45ce728Sahrens static int
2758e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2759e45ce728Sahrens {
276092241e0bSTom Erickson 	const char *propname = zc->zc_value;
276192241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
276292241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
276392241e0bSTom Erickson 	zprop_source_t source = (received
276492241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
276592241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
276692241e0bSTom Erickson 
276792241e0bSTom Erickson 	if (received) {
276892241e0bSTom Erickson 		nvlist_t *dummy;
276992241e0bSTom Erickson 		nvpair_t *pair;
277092241e0bSTom Erickson 		zprop_type_t type;
277192241e0bSTom Erickson 		int err;
277292241e0bSTom Erickson 
277392241e0bSTom Erickson 		/*
277492241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
277592241e0bSTom Erickson 		 * nvpair with type info.
277692241e0bSTom Erickson 		 */
277792241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
277892241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2779be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
278092241e0bSTom Erickson 
278192241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2782a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2783a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2784be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
278592241e0bSTom Erickson 		} else {
278692241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
278792241e0bSTom Erickson 		}
278892241e0bSTom Erickson 
278992241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
279092241e0bSTom Erickson 
279192241e0bSTom Erickson 		switch (type) {
279292241e0bSTom Erickson 		case PROP_TYPE_STRING:
279392241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
279492241e0bSTom Erickson 			break;
279592241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
279692241e0bSTom Erickson 		case PROP_TYPE_INDEX:
279792241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
279892241e0bSTom Erickson 			break;
279992241e0bSTom Erickson 		default:
280092241e0bSTom Erickson 			nvlist_free(dummy);
2801be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
280292241e0bSTom Erickson 		}
280392241e0bSTom Erickson 
280492241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
280592241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
280692241e0bSTom Erickson 		nvlist_free(dummy);
280792241e0bSTom Erickson 		if (err != -1)
280892241e0bSTom Erickson 			return (err); /* special property already handled */
280992241e0bSTom Erickson 	} else {
281092241e0bSTom Erickson 		/*
281192241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
281292241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
281392241e0bSTom Erickson 		 * and reservation to the received or default values even though
281492241e0bSTom Erickson 		 * they are not considered inheritable.
281592241e0bSTom Erickson 		 */
281692241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2817be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
281892241e0bSTom Erickson 	}
281992241e0bSTom Erickson 
28204445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28213b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2822e45ce728Sahrens }
2823e45ce728Sahrens 
2824b1b8ab34Slling static int
282511a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2826b1b8ab34Slling {
2827990b4856Slling 	nvlist_t *props;
2828b1b8ab34Slling 	spa_t *spa;
2829990b4856Slling 	int error;
283092241e0bSTom Erickson 	nvpair_t *pair;
2831b1b8ab34Slling 
283292241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
283392241e0bSTom Erickson 	    zc->zc_iflags, &props))
2834b1b8ab34Slling 		return (error);
2835b1b8ab34Slling 
2836379c004dSEric Schrock 	/*
2837379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2838379c004dSEric Schrock 	 * to handle the faulted case.
2839379c004dSEric Schrock 	 */
284092241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
284192241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2842379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
284392241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2844379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2845379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2846379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2847379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2848379c004dSEric Schrock 		}
2849379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2850b693757aSEric Schrock 		if (spa != NULL) {
2851b693757aSEric Schrock 			nvlist_free(props);
2852379c004dSEric Schrock 			return (0);
2853b693757aSEric Schrock 		}
2854379c004dSEric Schrock 	}
2855379c004dSEric Schrock 
2856b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2857990b4856Slling 		nvlist_free(props);
2858b1b8ab34Slling 		return (error);
2859b1b8ab34Slling 	}
2860b1b8ab34Slling 
2861990b4856Slling 	error = spa_prop_set(spa, props);
2862b1b8ab34Slling 
2863990b4856Slling 	nvlist_free(props);
2864b1b8ab34Slling 	spa_close(spa, FTAG);
2865b1b8ab34Slling 
2866b1b8ab34Slling 	return (error);
2867b1b8ab34Slling }
2868b1b8ab34Slling 
2869b1b8ab34Slling static int
287011a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2871b1b8ab34Slling {
2872b1b8ab34Slling 	spa_t *spa;
2873b1b8ab34Slling 	int error;
2874b1b8ab34Slling 	nvlist_t *nvp = NULL;
2875b1b8ab34Slling 
2876379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2877379c004dSEric Schrock 		/*
2878379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2879379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2880379c004dSEric Schrock 		 * anyway.
2881379c004dSEric Schrock 		 */
2882379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2883379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2884379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2885379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2886379c004dSEric Schrock 	} else {
2887379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2888379c004dSEric Schrock 		spa_close(spa, FTAG);
2889379c004dSEric Schrock 	}
2890b1b8ab34Slling 
2891b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2892b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2893b1b8ab34Slling 	else
2894be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2895b1b8ab34Slling 
2896379c004dSEric Schrock 	nvlist_free(nvp);
2897b1b8ab34Slling 	return (error);
2898b1b8ab34Slling }
2899b1b8ab34Slling 
29003cb34c60Sahrens /*
29013cb34c60Sahrens  * inputs:
29023cb34c60Sahrens  * zc_name		name of filesystem
29033cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29043cb34c60Sahrens  * zc_perm_action	allow/unallow flag
29053cb34c60Sahrens  *
29063cb34c60Sahrens  * outputs:		none
29073cb34c60Sahrens  */
2908ecd6cf80Smarks static int
2909ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2910ecd6cf80Smarks {
2911ecd6cf80Smarks 	int error;
2912ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2913ecd6cf80Smarks 
2914990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2915478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2916ecd6cf80Smarks 		return (error);
2917ecd6cf80Smarks 
2918ecd6cf80Smarks 	/*
2919ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2920ecd6cf80Smarks 	 */
2921ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2922ecd6cf80Smarks 		nvlist_free(fsaclnv);
2923be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2924ecd6cf80Smarks 	}
2925ecd6cf80Smarks 
2926ecd6cf80Smarks 	/*
2927ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2928ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2929ecd6cf80Smarks 	 * the nvlist(s)
2930ecd6cf80Smarks 	 */
2931ecd6cf80Smarks 
293291ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29333b2aab18SMatthew Ahrens 	if (error != 0) {
293491ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
293591ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
293691ebeef5Sahrens 			    fsaclnv, CRED());
293791ebeef5Sahrens 		} else {
293891ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
293991ebeef5Sahrens 			    fsaclnv, CRED());
294091ebeef5Sahrens 		}
2941ecd6cf80Smarks 	}
2942ecd6cf80Smarks 
2943ecd6cf80Smarks 	if (error == 0)
2944ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2945ecd6cf80Smarks 
2946ecd6cf80Smarks 	nvlist_free(fsaclnv);
2947ecd6cf80Smarks 	return (error);
2948ecd6cf80Smarks }
2949ecd6cf80Smarks 
29503cb34c60Sahrens /*
29513cb34c60Sahrens  * inputs:
29523cb34c60Sahrens  * zc_name		name of filesystem
29533cb34c60Sahrens  *
29543cb34c60Sahrens  * outputs:
29553cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29563cb34c60Sahrens  */
2957ecd6cf80Smarks static int
2958ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2959ecd6cf80Smarks {
2960ecd6cf80Smarks 	nvlist_t *nvp;
2961ecd6cf80Smarks 	int error;
2962ecd6cf80Smarks 
2963ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2964ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2965ecd6cf80Smarks 		nvlist_free(nvp);
2966ecd6cf80Smarks 	}
2967ecd6cf80Smarks 
2968ecd6cf80Smarks 	return (error);
2969ecd6cf80Smarks }
2970ecd6cf80Smarks 
2971fa9e4066Sahrens /*
2972fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2973fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2974fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2975fa9e4066Sahrens  */
2976fa9e4066Sahrens static vfs_t *
2977fa9e4066Sahrens zfs_get_vfs(const char *resource)
2978fa9e4066Sahrens {
2979fa9e4066Sahrens 	struct vfs *vfsp;
2980fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2981fa9e4066Sahrens 
2982fa9e4066Sahrens 	vfs_list_read_lock();
2983fa9e4066Sahrens 	vfsp = rootvfs;
2984fa9e4066Sahrens 	do {
2985fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2986fa9e4066Sahrens 			VFS_HOLD(vfsp);
2987fa9e4066Sahrens 			vfs_found = vfsp;
2988fa9e4066Sahrens 			break;
2989fa9e4066Sahrens 		}
2990fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2991fa9e4066Sahrens 	} while (vfsp != rootvfs);
2992fa9e4066Sahrens 	vfs_list_unlock();
2993fa9e4066Sahrens 	return (vfs_found);
2994fa9e4066Sahrens }
2995fa9e4066Sahrens 
2996ecd6cf80Smarks /* ARGSUSED */
2997fa9e4066Sahrens static void
2998ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2999fa9e4066Sahrens {
3000da6c28aaSamw 	zfs_creat_t *zct = arg;
3001da6c28aaSamw 
3002de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3003da6c28aaSamw }
3004da6c28aaSamw 
3005de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3006da6c28aaSamw 
3007da6c28aaSamw /*
3008de8267e0Stimh  * inputs:
30090a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3010f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3011f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3012f7170741SWill Andrews  * createprops		list of properties requested by creator
3013de8267e0Stimh  *
3014de8267e0Stimh  * outputs:
3015de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30160a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3017da6c28aaSamw  *
3018de8267e0Stimh  * Determine the settings for utf8only, normalization and
3019de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3020de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3021de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3022de8267e0Stimh  * request settings for these properties, even if the requested
3023de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3024de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3025de8267e0Stimh  * processing.
3026da6c28aaSamw  */
3027da6c28aaSamw static int
302814843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30290a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30300a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3031da6c28aaSamw {
3032de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3033de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3034de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3035da6c28aaSamw 
3036de8267e0Stimh 	ASSERT(zplprops != NULL);
3037da6c28aaSamw 
3038de8267e0Stimh 	/*
3039de8267e0Stimh 	 * Pull out creator prop choices, if any.
3040de8267e0Stimh 	 */
3041de8267e0Stimh 	if (createprops) {
30420a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
30430a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3044de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3045de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3046de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3047de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3048de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3049de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3050de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3051de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3052de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3053de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3054de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3055de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3056de8267e0Stimh 	}
3057da6c28aaSamw 
3058c2a93d44Stimh 	/*
30590a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30600a48a24eStimh 	 * or pool is version is too "young" to support normalization
30610a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30620a48a24eStimh 	 * error out.
3063c2a93d44Stimh 	 */
30640a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30650a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30660a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30670a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3068de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30690a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3070be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3071c2a93d44Stimh 
3072de8267e0Stimh 	/*
3073de8267e0Stimh 	 * Put the version in the zplprops
3074de8267e0Stimh 	 */
3075de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3076de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3077da6c28aaSamw 
3078de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3079de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3080de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3081de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3082da6c28aaSamw 
3083c2a93d44Stimh 	/*
3084de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3085c2a93d44Stimh 	 */
3086de8267e0Stimh 	if (norm)
3087de8267e0Stimh 		u8 = 1;
3088de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3089de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3090de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3091de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3092de8267e0Stimh 
3093de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3094de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3095de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3096de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3097c2a93d44Stimh 
3098ab04eb8eStimh 	if (is_ci)
3099ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3100ab04eb8eStimh 
3101da6c28aaSamw 	return (0);
3102fa9e4066Sahrens }
3103fa9e4066Sahrens 
31040a48a24eStimh static int
31050a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31060a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31070a48a24eStimh {
31080a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
31090a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31100a48a24eStimh 	objset_t *os = NULL;
31110a48a24eStimh 	char parentname[MAXNAMELEN];
31120a48a24eStimh 	char *cp;
31130a586ceaSMark Shellenbaum 	spa_t *spa;
31140a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31150a48a24eStimh 	int error;
31160a48a24eStimh 
31170a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31180a48a24eStimh 	cp = strrchr(parentname, '/');
31190a48a24eStimh 	ASSERT(cp != NULL);
31200a48a24eStimh 	cp[0] = '\0';
31210a48a24eStimh 
31220a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31230a586ceaSMark Shellenbaum 		return (error);
31240a586ceaSMark Shellenbaum 
31250a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31260a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31270a586ceaSMark Shellenbaum 
31280a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31290a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31300a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31310a48a24eStimh 
31320a48a24eStimh 	/*
31330a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31340a48a24eStimh 	 */
3135503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31360a48a24eStimh 		return (error);
31370a48a24eStimh 
31380a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31390a48a24eStimh 	    zplprops, is_ci);
3140503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31410a48a24eStimh 	return (error);
31420a48a24eStimh }
31430a48a24eStimh 
31440a48a24eStimh static int
31450a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31460a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31470a48a24eStimh {
31480a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
31490a586ceaSMark Shellenbaum 	boolean_t sa_ok;
31500a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31510a48a24eStimh 	int error;
31520a48a24eStimh 
31530a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31540a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31550a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31560a48a24eStimh 
31570a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31580a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31590a48a24eStimh 	return (error);
31600a48a24eStimh }
31610a48a24eStimh 
31623cb34c60Sahrens /*
31634445fffbSMatthew Ahrens  * innvl: {
31644445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31654445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31664445fffbSMatthew Ahrens  * }
31673cb34c60Sahrens  *
31684445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31693cb34c60Sahrens  */
3170fa9e4066Sahrens static int
31714445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3172fa9e4066Sahrens {
3173fa9e4066Sahrens 	int error = 0;
31744445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3175ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3176ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31774445fffbSMatthew Ahrens 	int32_t type32;
31784445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31794445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3180fa9e4066Sahrens 
31814445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3182be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31834445fffbSMatthew Ahrens 	type = type32;
31844445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3185fa9e4066Sahrens 
31864445fffbSMatthew Ahrens 	switch (type) {
3187fa9e4066Sahrens 	case DMU_OST_ZFS:
3188fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3189fa9e4066Sahrens 		break;
3190fa9e4066Sahrens 
3191fa9e4066Sahrens 	case DMU_OST_ZVOL:
3192fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3193fa9e4066Sahrens 		break;
3194fa9e4066Sahrens 
3195fa9e4066Sahrens 	default:
31961d452cf5Sahrens 		cbfunc = NULL;
3197e7cbe64fSgw 		break;
3198fa9e4066Sahrens 	}
31994445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32004445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3201be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3202fa9e4066Sahrens 
3203da6c28aaSamw 	zct.zct_props = nvprops;
3204da6c28aaSamw 
32054445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3206be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32074445fffbSMatthew Ahrens 
32084445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
32094445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
32104445fffbSMatthew Ahrens 
32114445fffbSMatthew Ahrens 		if (nvprops == NULL)
3212be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32134445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32144445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3215be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3216fa9e4066Sahrens 
32174445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
32184445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32194445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3220be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32214445fffbSMatthew Ahrens 
32224445fffbSMatthew Ahrens 		if (error != 0)
32234445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
32244445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
32254445fffbSMatthew Ahrens 
32264445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
32274445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
32284445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
32294445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3230fa9e4066Sahrens 			return (error);
32314445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
32324445fffbSMatthew Ahrens 		int error;
3233ab04eb8eStimh 
32344445fffbSMatthew Ahrens 		/*
32354445fffbSMatthew Ahrens 		 * We have to have normalization and
32364445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
32374445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
32384445fffbSMatthew Ahrens 		 * now.
32394445fffbSMatthew Ahrens 		 */
32404445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
32414445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32424445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32434445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
32444445fffbSMatthew Ahrens 		if (error != 0) {
32454445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3246da6c28aaSamw 			return (error);
3247da6c28aaSamw 		}
32484445fffbSMatthew Ahrens 	}
3249ab04eb8eStimh 
32504445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
32514445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
32524445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
32535c5460e9Seschrock 
32544445fffbSMatthew Ahrens 	/*
32554445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32564445fffbSMatthew Ahrens 	 */
32574445fffbSMatthew Ahrens 	if (error == 0) {
32584445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32594445fffbSMatthew Ahrens 		    nvprops, outnvl);
32604445fffbSMatthew Ahrens 		if (error != 0)
32613b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32624445fffbSMatthew Ahrens 	}
32634445fffbSMatthew Ahrens 	return (error);
32644445fffbSMatthew Ahrens }
3265e9dbad6fSeschrock 
32664445fffbSMatthew Ahrens /*
32674445fffbSMatthew Ahrens  * innvl: {
32684445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32694445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32704445fffbSMatthew Ahrens  * }
32714445fffbSMatthew Ahrens  *
32724445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32734445fffbSMatthew Ahrens  */
32744445fffbSMatthew Ahrens static int
32754445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32764445fffbSMatthew Ahrens {
32774445fffbSMatthew Ahrens 	int error = 0;
32784445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32794445fffbSMatthew Ahrens 	char *origin_name;
3280e9dbad6fSeschrock 
32814445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3282be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32834445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3284e9dbad6fSeschrock 
32854445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32864445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3287be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3288e9dbad6fSeschrock 
32894445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3290be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32913b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
32923b2aab18SMatthew Ahrens 	if (error != 0)
32934445fffbSMatthew Ahrens 		return (error);
3294e9dbad6fSeschrock 
3295e9dbad6fSeschrock 	/*
3296e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3297e9dbad6fSeschrock 	 */
3298e9dbad6fSeschrock 	if (error == 0) {
32994445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33004445fffbSMatthew Ahrens 		    nvprops, outnvl);
330192241e0bSTom Erickson 		if (error != 0)
33023b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3303e9dbad6fSeschrock 	}
3304fa9e4066Sahrens 	return (error);
3305fa9e4066Sahrens }
3306fa9e4066Sahrens 
33073cb34c60Sahrens /*
33084445fffbSMatthew Ahrens  * innvl: {
33094445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33104445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33114445fffbSMatthew Ahrens  * }
33124445fffbSMatthew Ahrens  *
33134445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33143cb34c60Sahrens  */
3315fa9e4066Sahrens static int
33164445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3317fa9e4066Sahrens {
33184445fffbSMatthew Ahrens 	nvlist_t *snaps;
33194445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33204445fffbSMatthew Ahrens 	int error, poollen;
33214445fffbSMatthew Ahrens 	nvpair_t *pair;
3322bb0ade09Sahrens 
33234445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33244445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33254445fffbSMatthew Ahrens 		return (error);
33264445fffbSMatthew Ahrens 
33274445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33284445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3329be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33304445fffbSMatthew Ahrens 
33314445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3332be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33334445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33344445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33354445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33364445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33374445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3338bb0ade09Sahrens 
33394445fffbSMatthew Ahrens 		/*
33404445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33414445fffbSMatthew Ahrens 		 * contain only valid characters.
33424445fffbSMatthew Ahrens 		 */
3343*78f17100SMatthew Ahrens 		if (cp == NULL ||
3344*78f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3345be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3346bb0ade09Sahrens 
33474445fffbSMatthew Ahrens 		/*
33484445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
33494445fffbSMatthew Ahrens 		 */
33504445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
33514445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3352be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
33534445fffbSMatthew Ahrens 
33544445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
33554445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33564445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33574445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33584445fffbSMatthew Ahrens 			    == 0) {
3359be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33604445fffbSMatthew Ahrens 			}
33614445fffbSMatthew Ahrens 		}
33624445fffbSMatthew Ahrens 	}
3363bb0ade09Sahrens 
33643b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33654445fffbSMatthew Ahrens 	return (error);
33664445fffbSMatthew Ahrens }
33674445fffbSMatthew Ahrens 
33684445fffbSMatthew Ahrens /*
33694445fffbSMatthew Ahrens  * innvl: "message" -> string
33704445fffbSMatthew Ahrens  */
33714445fffbSMatthew Ahrens /* ARGSUSED */
33724445fffbSMatthew Ahrens static int
33734445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
33744445fffbSMatthew Ahrens {
33754445fffbSMatthew Ahrens 	char *message;
33764445fffbSMatthew Ahrens 	spa_t *spa;
33774445fffbSMatthew Ahrens 	int error;
33784445fffbSMatthew Ahrens 	char *poolname;
33794445fffbSMatthew Ahrens 
33804445fffbSMatthew Ahrens 	/*
33814445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
33824445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
33834445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
33844445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
33854445fffbSMatthew Ahrens 	 * we clear the TSD here.
33864445fffbSMatthew Ahrens 	 */
33874445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
33884445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
33894445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
33904445fffbSMatthew Ahrens 	strfree(poolname);
33914445fffbSMatthew Ahrens 	if (error != 0)
33924445fffbSMatthew Ahrens 		return (error);
33934445fffbSMatthew Ahrens 
33944445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
33954445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3396be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3397bb0ade09Sahrens 	}
3398ea2f5b9eSMatthew Ahrens 
33994445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34004445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3401be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34024445fffbSMatthew Ahrens 	}
3403ea2f5b9eSMatthew Ahrens 
34044445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
34054445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3406bb0ade09Sahrens 	return (error);
34071d452cf5Sahrens }
3408fa9e4066Sahrens 
34093b2aab18SMatthew Ahrens /*
34103b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34113b2aab18SMatthew Ahrens  * unmount may need to write out data.
34123b2aab18SMatthew Ahrens  *
34133b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34143b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3415fc7a6e3fSWill Andrews  *
3416fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3417fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34183b2aab18SMatthew Ahrens  */
3419fc7a6e3fSWill Andrews int
34203b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34211d452cf5Sahrens {
34224445fffbSMatthew Ahrens 	vfs_t *vfsp;
34233b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3424fc7a6e3fSWill Andrews 	int err;
34251d452cf5Sahrens 
34263b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3427fc7a6e3fSWill Andrews 		return (0);
34281d452cf5Sahrens 
34293b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
34304445fffbSMatthew Ahrens 	if (vfsp == NULL)
3431fc7a6e3fSWill Andrews 		return (0);
34323b2aab18SMatthew Ahrens 
34333b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
34343b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34351d452cf5Sahrens 
3436fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
34374445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3438fc7a6e3fSWill Andrews 	if (err != 0)
3439fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
34404445fffbSMatthew Ahrens 
34414445fffbSMatthew Ahrens 	/*
34424445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34434445fffbSMatthew Ahrens 	 */
34443b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3445fc7a6e3fSWill Andrews 	return (0);
34463b2aab18SMatthew Ahrens }
34473b2aab18SMatthew Ahrens 
34483b2aab18SMatthew Ahrens /* ARGSUSED */
34493b2aab18SMatthew Ahrens static int
34503b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
34513b2aab18SMatthew Ahrens {
3452fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
34533b2aab18SMatthew Ahrens }
34543b2aab18SMatthew Ahrens 
34553b2aab18SMatthew Ahrens /*
34563b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34573b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34583b2aab18SMatthew Ahrens  * if necessary.
34593b2aab18SMatthew Ahrens  */
34603b2aab18SMatthew Ahrens void
34613b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34623b2aab18SMatthew Ahrens {
34633b2aab18SMatthew Ahrens 	int error;
34643b2aab18SMatthew Ahrens 	objset_t *os;
34653b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34663b2aab18SMatthew Ahrens 
34673b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
34683b2aab18SMatthew Ahrens 	if (error != 0)
34693b2aab18SMatthew Ahrens 		return;
34703b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
34713b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
34723b2aab18SMatthew Ahrens 		char originname[MAXNAMELEN];
34733b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
34743b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3475fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
34763b2aab18SMatthew Ahrens 	} else {
34773b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34783b2aab18SMatthew Ahrens 	}
34791d452cf5Sahrens }
34801d452cf5Sahrens 
34813cb34c60Sahrens /*
34824445fffbSMatthew Ahrens  * innvl: {
34834445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34844445fffbSMatthew Ahrens  *     (optional boolean) "defer"
34854445fffbSMatthew Ahrens  * }
34864445fffbSMatthew Ahrens  *
34874445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34883cb34c60Sahrens  *
34893cb34c60Sahrens  */
3490*78f17100SMatthew Ahrens /* ARGSUSED */
34911d452cf5Sahrens static int
34924445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34931d452cf5Sahrens {
34944445fffbSMatthew Ahrens 	nvlist_t *snaps;
349519b94df9SMatthew Ahrens 	nvpair_t *pair;
34964445fffbSMatthew Ahrens 	boolean_t defer;
34971d452cf5Sahrens 
34984445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3499be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
35004445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
350119b94df9SMatthew Ahrens 
35024445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35034445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
3504*78f17100SMatthew Ahrens 		(void) zfs_unmount_snap(nvpair_name(pair));
3505*78f17100SMatthew Ahrens 	}
3506*78f17100SMatthew Ahrens 
3507*78f17100SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3508*78f17100SMatthew Ahrens }
3509*78f17100SMatthew Ahrens 
3510*78f17100SMatthew Ahrens /*
3511*78f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
3512*78f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
3513*78f17100SMatthew Ahrens  *
3514*78f17100SMatthew Ahrens  * innvl: {
3515*78f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
3516*78f17100SMatthew Ahrens  * }
3517*78f17100SMatthew Ahrens  *
3518*78f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
3519*78f17100SMatthew Ahrens  *
3520*78f17100SMatthew Ahrens  */
3521*78f17100SMatthew Ahrens /* ARGSUSED */
3522*78f17100SMatthew Ahrens static int
3523*78f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3524*78f17100SMatthew Ahrens {
3525*78f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3526*78f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3527*78f17100SMatthew Ahrens 		char *snap_name;
3528*78f17100SMatthew Ahrens 
3529*78f17100SMatthew Ahrens 		/*
3530*78f17100SMatthew Ahrens 		 * Verify the snapshot argument.
3531*78f17100SMatthew Ahrens 		 */
3532*78f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
3533*78f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
3534*78f17100SMatthew Ahrens 
3535*78f17100SMatthew Ahrens 
3536*78f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
3537*78f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3538*78f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3539*78f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3540*78f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
3541*78f17100SMatthew Ahrens 		}
3542*78f17100SMatthew Ahrens 	}
3543*78f17100SMatthew Ahrens 
3544*78f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
3545*78f17100SMatthew Ahrens }
3546*78f17100SMatthew Ahrens 
3547*78f17100SMatthew Ahrens /*
3548*78f17100SMatthew Ahrens  * innvl: {
3549*78f17100SMatthew Ahrens  *     property 1, property 2, ...
3550*78f17100SMatthew Ahrens  * }
3551*78f17100SMatthew Ahrens  *
3552*78f17100SMatthew Ahrens  * outnvl: {
3553*78f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
3554*78f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
3555*78f17100SMatthew Ahrens  * }
3556*78f17100SMatthew Ahrens  *
3557*78f17100SMatthew Ahrens  */
3558*78f17100SMatthew Ahrens static int
3559*78f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3560*78f17100SMatthew Ahrens {
3561*78f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
3562*78f17100SMatthew Ahrens }
3563*78f17100SMatthew Ahrens 
3564*78f17100SMatthew Ahrens /*
3565*78f17100SMatthew Ahrens  * innvl: {
3566*78f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
3567*78f17100SMatthew Ahrens  * }
3568*78f17100SMatthew Ahrens  *
3569*78f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
3570*78f17100SMatthew Ahrens  *
3571*78f17100SMatthew Ahrens  */
3572*78f17100SMatthew Ahrens static int
3573*78f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3574*78f17100SMatthew Ahrens     nvlist_t *outnvl)
3575*78f17100SMatthew Ahrens {
3576*78f17100SMatthew Ahrens 	int error, poollen;
3577*78f17100SMatthew Ahrens 
3578*78f17100SMatthew Ahrens 	poollen = strlen(poolname);
3579*78f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3580*78f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
358119b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
3582*78f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
35834445fffbSMatthew Ahrens 
358419b94df9SMatthew Ahrens 		/*
3585*78f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
3586*78f17100SMatthew Ahrens 		 * must contain only valid characters.
3587*78f17100SMatthew Ahrens 		 */
3588*78f17100SMatthew Ahrens 		if (cp == NULL ||
3589*78f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3590*78f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
3591*78f17100SMatthew Ahrens 
3592*78f17100SMatthew Ahrens 		/*
3593*78f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
359419b94df9SMatthew Ahrens 		 */
35954445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
3596*78f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
3597be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
359819b94df9SMatthew Ahrens 	}
359919b94df9SMatthew Ahrens 
3600*78f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
3601*78f17100SMatthew Ahrens 	return (error);
36021d452cf5Sahrens }
36031d452cf5Sahrens 
36043cb34c60Sahrens /*
36053cb34c60Sahrens  * inputs:
36063cb34c60Sahrens  * zc_name		name of dataset to destroy
36073cb34c60Sahrens  * zc_objset_type	type of objset
3608842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
36093cb34c60Sahrens  *
36103cb34c60Sahrens  * outputs:		none
36113cb34c60Sahrens  */
36121d452cf5Sahrens static int
36131d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36141d452cf5Sahrens {
3615681d9761SEric Taylor 	int err;
3616fc7a6e3fSWill Andrews 
3617fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3618fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3619fc7a6e3fSWill Andrews 		if (err != 0)
3620fc7a6e3fSWill Andrews 			return (err);
3621fc7a6e3fSWill Andrews 	}
3622fa9e4066Sahrens 
36233b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
36243b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36253b2aab18SMatthew Ahrens 	else
36263b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3627681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36285c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3629681d9761SEric Taylor 	return (err);
3630fa9e4066Sahrens }
3631fa9e4066Sahrens 
36323cb34c60Sahrens /*
3633a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
36343cb34c60Sahrens  *
3635a7027df1SMatthew Ahrens  * innvl is not used.
3636a7027df1SMatthew Ahrens  *
3637a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3638a7027df1SMatthew Ahrens  * }
36393cb34c60Sahrens  */
3640a7027df1SMatthew Ahrens /* ARGSUSED */
3641fa9e4066Sahrens static int
3642a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3643fa9e4066Sahrens {
3644ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
36453b2aab18SMatthew Ahrens 	int error;
3646ae46e4c7SMatthew Ahrens 
3647a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3648503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
364947f263f4Sek 		if (error == 0) {
365047f263f4Sek 			int resume_err;
36514ccbb6e7Sahrens 
3652a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3653a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
365447f263f4Sek 			error = error ? error : resume_err;
365547f263f4Sek 		}
36564ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
36574ccbb6e7Sahrens 	} else {
3658a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
36594ccbb6e7Sahrens 	}
36603b2aab18SMatthew Ahrens 	return (error);
36613b2aab18SMatthew Ahrens }
36624ccbb6e7Sahrens 
36633b2aab18SMatthew Ahrens static int
36643b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
36653b2aab18SMatthew Ahrens {
36663b2aab18SMatthew Ahrens 	const char *snapname = arg;
36673b2aab18SMatthew Ahrens 	char fullname[MAXNAMELEN];
3668ae46e4c7SMatthew Ahrens 
36693b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3670fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
3671fa9e4066Sahrens }
3672fa9e4066Sahrens 
36733cb34c60Sahrens /*
36743cb34c60Sahrens  * inputs:
36753cb34c60Sahrens  * zc_name	old name of dataset
36763cb34c60Sahrens  * zc_value	new name of dataset
36773cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
36783cb34c60Sahrens  *
36793cb34c60Sahrens  * outputs:	none
36803cb34c60Sahrens  */
3681fa9e4066Sahrens static int
3682fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3683fa9e4066Sahrens {
36847f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
36853b2aab18SMatthew Ahrens 	char *at;
3686cdf5b4caSmmusante 
3687e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3688f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3689f18faf3fSek 	    strchr(zc->zc_value, '%'))
3690be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3691fa9e4066Sahrens 
36923b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
36933b2aab18SMatthew Ahrens 	if (at != NULL) {
36943b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3695a0c1127bSSteven Hartland 		int error;
3696a0c1127bSSteven Hartland 
36973b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3698be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
36993b2aab18SMatthew Ahrens 		*at = '\0';
37003b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3701a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
37023b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
37033b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3704a0c1127bSSteven Hartland 			if (error != 0) {
3705a0c1127bSSteven Hartland 				*at = '@';
37063b2aab18SMatthew Ahrens 				return (error);
3707a0c1127bSSteven Hartland 			}
37083b2aab18SMatthew Ahrens 		}
3709a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3710a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3711a0c1127bSSteven Hartland 		*at = '@';
3712a0c1127bSSteven Hartland 
3713a0c1127bSSteven Hartland 		return (error);
37143b2aab18SMatthew Ahrens 	} else {
37153b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZVOL)
37163b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
37173b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3718fa9e4066Sahrens 	}
3719fa9e4066Sahrens }
3720fa9e4066Sahrens 
372192241e0bSTom Erickson static int
372292241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
372392241e0bSTom Erickson {
372492241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
372592241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
372692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
372792241e0bSTom Erickson 	uint64_t intval;
372892241e0bSTom Erickson 	int err;
372992241e0bSTom Erickson 
373092241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
373192241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
373292241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
373392241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
373492241e0bSTom Erickson 				return (err);
373592241e0bSTom Erickson 			return (0);
373692241e0bSTom Erickson 		}
373792241e0bSTom Erickson 
373892241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
373992241e0bSTom Erickson 			const char *perm = NULL;
374092241e0bSTom Erickson 			const char *uq_prefix =
374192241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
374292241e0bSTom Erickson 			const char *gq_prefix =
374392241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
374492241e0bSTom Erickson 
374592241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
374692241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
374792241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
374892241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
374992241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
375092241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
375192241e0bSTom Erickson 			} else {
375292241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3753be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
375492241e0bSTom Erickson 			}
375592241e0bSTom Erickson 
375692241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
375792241e0bSTom Erickson 				return (err);
375892241e0bSTom Erickson 			return (0);
375992241e0bSTom Erickson 		}
376092241e0bSTom Erickson 
3761be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
376292241e0bSTom Erickson 	}
376392241e0bSTom Erickson 
376492241e0bSTom Erickson 	if (issnap)
3765be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
376692241e0bSTom Erickson 
376792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
376892241e0bSTom Erickson 		/*
376992241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
377092241e0bSTom Erickson 		 * format.
377192241e0bSTom Erickson 		 */
377292241e0bSTom Erickson 		nvlist_t *attrs;
377392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
377492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
377592241e0bSTom Erickson 		    &pair) == 0);
377692241e0bSTom Erickson 	}
377792241e0bSTom Erickson 
377892241e0bSTom Erickson 	/*
377992241e0bSTom Erickson 	 * Check that this value is valid for this pool version
378092241e0bSTom Erickson 	 */
378192241e0bSTom Erickson 	switch (prop) {
378292241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
378392241e0bSTom Erickson 		/*
378492241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
378592241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
378692241e0bSTom Erickson 		 * we'll catch them later.
378792241e0bSTom Erickson 		 */
378892241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
378992241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
379092241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
379192241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
379292241e0bSTom Erickson 			    zfs_earlier_version(dsname,
379392241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3794be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
379592241e0bSTom Erickson 			}
379692241e0bSTom Erickson 
379792241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
379892241e0bSTom Erickson 			    zfs_earlier_version(dsname,
379992241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3800be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
380192241e0bSTom Erickson 
3802a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3803a6f561b4SSašo Kiselkov 				spa_t *spa;
3804a6f561b4SSašo Kiselkov 
3805a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3806a6f561b4SSašo Kiselkov 					return (err);
3807a6f561b4SSašo Kiselkov 
38082acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
38092acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3810a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3811be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3812a6f561b4SSašo Kiselkov 				}
3813a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3814a6f561b4SSašo Kiselkov 			}
3815a6f561b4SSašo Kiselkov 
381692241e0bSTom Erickson 			/*
381792241e0bSTom Erickson 			 * If this is a bootable dataset then
381892241e0bSTom Erickson 			 * verify that the compression algorithm
381992241e0bSTom Erickson 			 * is supported for booting. We must return
382092241e0bSTom Erickson 			 * something other than ENOTSUP since it
382192241e0bSTom Erickson 			 * implies a downrev pool version.
382292241e0bSTom Erickson 			 */
382392241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
382492241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3825be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
382692241e0bSTom Erickson 			}
382792241e0bSTom Erickson 		}
382892241e0bSTom Erickson 		break;
382992241e0bSTom Erickson 
383092241e0bSTom Erickson 	case ZFS_PROP_COPIES:
383192241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3832be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
383392241e0bSTom Erickson 		break;
383492241e0bSTom Erickson 
383592241e0bSTom Erickson 	case ZFS_PROP_DEDUP:
383692241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3837be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
383892241e0bSTom Erickson 		break;
383992241e0bSTom Erickson 
384092241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
384192241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3842be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
384392241e0bSTom Erickson 		break;
384492241e0bSTom Erickson 
384592241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
384692241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
384792241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
384892241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
384992241e0bSTom Erickson 			    zfs_earlier_version(dsname,
385092241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3851be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
385292241e0bSTom Erickson 		}
385392241e0bSTom Erickson 		break;
385492241e0bSTom Erickson 	}
385592241e0bSTom Erickson 
385692241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
385792241e0bSTom Erickson }
385892241e0bSTom Erickson 
3859a6f561b4SSašo Kiselkov /*
3860a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3861a6f561b4SSašo Kiselkov  * multiple times.
3862a6f561b4SSašo Kiselkov  */
3863a6f561b4SSašo Kiselkov static int
38643b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3865a6f561b4SSašo Kiselkov {
38663b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
38672acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3868a6f561b4SSašo Kiselkov 
38692acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
3870a6f561b4SSašo Kiselkov 		return (0);
3871a6f561b4SSašo Kiselkov 	else
3872be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3873a6f561b4SSašo Kiselkov }
3874a6f561b4SSašo Kiselkov 
3875a6f561b4SSašo Kiselkov /*
3876a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3877a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3878a6f561b4SSašo Kiselkov  */
3879a6f561b4SSašo Kiselkov static void
38803b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3881a6f561b4SSašo Kiselkov {
38823b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
38832acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3884a6f561b4SSašo Kiselkov 
38852acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
3886a6f561b4SSašo Kiselkov }
3887a6f561b4SSašo Kiselkov 
38883b2aab18SMatthew Ahrens /*
38893b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
38903b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
38913b2aab18SMatthew Ahrens  * as being active.
38923b2aab18SMatthew Ahrens  */
38933b2aab18SMatthew Ahrens static int
38942acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
38953b2aab18SMatthew Ahrens {
38963b2aab18SMatthew Ahrens 	int err;
38973b2aab18SMatthew Ahrens 
38983b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
38993b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
39003b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
39012acef22dSMatthew Ahrens 	    &feature, 2);
39023b2aab18SMatthew Ahrens 
39033b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
39043b2aab18SMatthew Ahrens 		return (err);
39053b2aab18SMatthew Ahrens 	else
39063b2aab18SMatthew Ahrens 		return (0);
39073b2aab18SMatthew Ahrens }
39083b2aab18SMatthew Ahrens 
390992241e0bSTom Erickson /*
391092241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
391192241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
391292241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
391392241e0bSTom Erickson  *
391492241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
391592241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
391692241e0bSTom Erickson  * of all the properties that failed a permission check along with the
391792241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
391892241e0bSTom Erickson  * returned errlist.
391992241e0bSTom Erickson  *
392092241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
392192241e0bSTom Erickson  * pointed at by errlist is NULL.
392292241e0bSTom Erickson  */
392392241e0bSTom Erickson static int
392492241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3925745cd3c5Smaybee {
3926745cd3c5Smaybee 	zfs_cmd_t *zc;
392792241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
392892241e0bSTom Erickson 	nvlist_t *errors;
392992241e0bSTom Erickson 	int err, rv = 0;
3930745cd3c5Smaybee 
3931745cd3c5Smaybee 	if (props == NULL)
393292241e0bSTom Erickson 		return (0);
393392241e0bSTom Erickson 
393492241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
393592241e0bSTom Erickson 
3936745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3937745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
393892241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
393992241e0bSTom Erickson 	while (pair != NULL) {
394092241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
394192241e0bSTom Erickson 
394292241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
394392241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
39444445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
394592241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
394692241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
394792241e0bSTom Erickson 			    zc->zc_value, err) == 0);
394892241e0bSTom Erickson 		}
394992241e0bSTom Erickson 		pair = next_pair;
3950745cd3c5Smaybee 	}
3951745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
395292241e0bSTom Erickson 
395392241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
395492241e0bSTom Erickson 		nvlist_free(errors);
395592241e0bSTom Erickson 		errors = NULL;
395692241e0bSTom Erickson 	} else {
395792241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
395892241e0bSTom Erickson 	}
395992241e0bSTom Erickson 
396092241e0bSTom Erickson 	if (errlist == NULL)
396192241e0bSTom Erickson 		nvlist_free(errors);
396292241e0bSTom Erickson 	else
396392241e0bSTom Erickson 		*errlist = errors;
396492241e0bSTom Erickson 
396592241e0bSTom Erickson 	return (rv);
396692241e0bSTom Erickson }
396792241e0bSTom Erickson 
396892241e0bSTom Erickson static boolean_t
396992241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
397092241e0bSTom Erickson {
397192241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
397292241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
397392241e0bSTom Erickson 		nvlist_t *attrs;
397492241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
397592241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
397692241e0bSTom Erickson 		    &p1) == 0);
397792241e0bSTom Erickson 	}
397892241e0bSTom Erickson 
397992241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
398092241e0bSTom Erickson 		nvlist_t *attrs;
398192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
398292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
398392241e0bSTom Erickson 		    &p2) == 0);
398492241e0bSTom Erickson 	}
398592241e0bSTom Erickson 
398692241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
398792241e0bSTom Erickson 		return (B_FALSE);
398892241e0bSTom Erickson 
398992241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
399092241e0bSTom Erickson 		char *valstr1, *valstr2;
399192241e0bSTom Erickson 
399292241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
399392241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
399492241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
399592241e0bSTom Erickson 	} else {
399692241e0bSTom Erickson 		uint64_t intval1, intval2;
399792241e0bSTom Erickson 
399892241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
399992241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
400092241e0bSTom Erickson 		return (intval1 == intval2);
400192241e0bSTom Erickson 	}
4002745cd3c5Smaybee }
4003745cd3c5Smaybee 
400492241e0bSTom Erickson /*
400592241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
400692241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
400792241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
400892241e0bSTom Erickson  */
400992241e0bSTom Erickson static void
401092241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
401192241e0bSTom Erickson {
401292241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
401392241e0bSTom Erickson 
401492241e0bSTom Erickson 	if (origprops == NULL)
401592241e0bSTom Erickson 		return; /* all props need to be received */
401692241e0bSTom Erickson 
401792241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
401892241e0bSTom Erickson 	while (pair != NULL) {
401992241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
402092241e0bSTom Erickson 		nvpair_t *match;
402192241e0bSTom Erickson 
402292241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
402392241e0bSTom Erickson 
402492241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
402592241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
402692241e0bSTom Erickson 			goto next; /* need to set received value */
402792241e0bSTom Erickson 
402892241e0bSTom Erickson 		/* don't clear the existing received value */
402992241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
403092241e0bSTom Erickson 		/* don't bother receiving the property */
403192241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
403292241e0bSTom Erickson next:
403392241e0bSTom Erickson 		pair = next_pair;
403492241e0bSTom Erickson 	}
403592241e0bSTom Erickson }
403692241e0bSTom Erickson 
403792241e0bSTom Erickson #ifdef	DEBUG
403892241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
403992241e0bSTom Erickson #endif
404092241e0bSTom Erickson 
40413cb34c60Sahrens /*
40423cb34c60Sahrens  * inputs:
40433cb34c60Sahrens  * zc_name		name of containing filesystem
40443cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
40453cb34c60Sahrens  * zc_value		name of snapshot to create
40463cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
40473cb34c60Sahrens  * zc_cookie		file descriptor to recv from
40483cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
40493cb34c60Sahrens  * zc_guid		force flag
4050c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
4051c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
40523cb34c60Sahrens  *
40533cb34c60Sahrens  * outputs:
40543cb34c60Sahrens  * zc_cookie		number of bytes read
405592241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
405692241e0bSTom Erickson  * zc_obj		zprop_errflags_t
4057c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
40583cb34c60Sahrens  */
4059fa9e4066Sahrens static int
40603cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4061fa9e4066Sahrens {
4062fa9e4066Sahrens 	file_t *fp;
40633cb34c60Sahrens 	dmu_recv_cookie_t drc;
4064f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
406592241e0bSTom Erickson 	int fd;
406692241e0bSTom Erickson 	int error = 0;
406792241e0bSTom Erickson 	int props_error = 0;
406892241e0bSTom Erickson 	nvlist_t *errors;
40693cb34c60Sahrens 	offset_t off;
407092241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
407192241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
40723b2aab18SMatthew Ahrens 	char *origin = NULL;
40733cb34c60Sahrens 	char *tosnap;
40743cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
407592241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
4076fa9e4066Sahrens 
40773ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4078f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
4079f18faf3fSek 	    strchr(zc->zc_value, '%'))
4080be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
40813ccfa83cSahrens 
40823cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
40833cb34c60Sahrens 	tosnap = strchr(tofs, '@');
408492241e0bSTom Erickson 	*tosnap++ = '\0';
40853cb34c60Sahrens 
40863cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
40873cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4088478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
40893cb34c60Sahrens 		return (error);
40903cb34c60Sahrens 
4091fa9e4066Sahrens 	fd = zc->zc_cookie;
4092fa9e4066Sahrens 	fp = getf(fd);
40933cb34c60Sahrens 	if (fp == NULL) {
40943cb34c60Sahrens 		nvlist_free(props);
4095be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
40963cb34c60Sahrens 	}
4097f18faf3fSek 
409892241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
409992241e0bSTom Erickson 
41003b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
41013b2aab18SMatthew Ahrens 		origin = zc->zc_string;
41023b2aab18SMatthew Ahrens 
41033b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
41043b2aab18SMatthew Ahrens 	    &zc->zc_begin_record, force, origin, &drc);
41053b2aab18SMatthew Ahrens 	if (error != 0)
41063b2aab18SMatthew Ahrens 		goto out;
41073b2aab18SMatthew Ahrens 
41083b2aab18SMatthew Ahrens 	/*
41093b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
41103b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
41113b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
41123b2aab18SMatthew Ahrens 	 */
41133b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
41143b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
41153b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
41163b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
411792241e0bSTom Erickson 			first_recvd_props = B_TRUE;
411892241e0bSTom Erickson 
4119745cd3c5Smaybee 		/*
412092241e0bSTom Erickson 		 * If new received properties are supplied, they are to
412192241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
412292241e0bSTom Erickson 		 * away the existing ones.
4123745cd3c5Smaybee 		 */
41243b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
412592241e0bSTom Erickson 			nvlist_t *errlist = NULL;
412692241e0bSTom Erickson 			/*
412792241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
412892241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
412992241e0bSTom Erickson 			 *
413092241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
413192241e0bSTom Erickson 			 * special case where we blow away all local properties
413292241e0bSTom Erickson 			 * regardless.
413392241e0bSTom Erickson 			 */
413492241e0bSTom Erickson 			if (!first_recvd_props)
413592241e0bSTom Erickson 				props_reduce(props, origprops);
41363b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
413792241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
413892241e0bSTom Erickson 			nvlist_free(errlist);
41393cb34c60Sahrens 
41403b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
41413b2aab18SMatthew Ahrens 			    first_recvd_props ? NULL : props) != 0)
414292241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
41433b2aab18SMatthew Ahrens 		} else {
414492241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
414592241e0bSTom Erickson 		}
41463b2aab18SMatthew Ahrens 	}
414792241e0bSTom Erickson 
41483b2aab18SMatthew Ahrens 	if (props != NULL) {
41493b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
41503b2aab18SMatthew Ahrens 
41513b2aab18SMatthew Ahrens 		if (props_error == 0) {
41523b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
41533b2aab18SMatthew Ahrens 			    props, errors);
41543b2aab18SMatthew Ahrens 		}
415592241e0bSTom Erickson 	}
415692241e0bSTom Erickson 
41574445fffbSMatthew Ahrens 	if (zc->zc_nvlist_dst_size != 0 &&
41584445fffbSMatthew Ahrens 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
41594445fffbSMatthew Ahrens 	    put_nvlist(zc, errors) != 0)) {
4160745cd3c5Smaybee 		/*
416192241e0bSTom Erickson 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
416292241e0bSTom Erickson 		 * size or supplied an invalid address.
4163745cd3c5Smaybee 		 */
4164be6fd75aSMatthew Ahrens 		props_error = SET_ERROR(EINVAL);
41653cb34c60Sahrens 	}
41663cb34c60Sahrens 
41673cb34c60Sahrens 	off = fp->f_offset;
4168c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4169c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
4170a2eea2e1Sahrens 
4171f4b94bdeSMatthew Ahrens 	if (error == 0) {
4172f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4173745cd3c5Smaybee 
4174f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4175f4b94bdeSMatthew Ahrens 			/* online recv */
4176f4b94bdeSMatthew Ahrens 			int end_err;
4177745cd3c5Smaybee 
4178503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4179f4b94bdeSMatthew Ahrens 			/*
4180f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4181f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4182f4b94bdeSMatthew Ahrens 			 */
418391948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
41845c703fceSGeorge Wilson 			if (error == 0)
41855c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4186f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4187f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4188745cd3c5Smaybee 		} else {
418991948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
41903cb34c60Sahrens 		}
419147f263f4Sek 	}
41923cb34c60Sahrens 
41933cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
41943cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
41953cb34c60Sahrens 		fp->f_offset = off;
4196a2eea2e1Sahrens 
419792241e0bSTom Erickson #ifdef	DEBUG
419892241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
419992241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
420092241e0bSTom Erickson 		error = 1;
420192241e0bSTom Erickson 	}
420292241e0bSTom Erickson #endif
4203745cd3c5Smaybee 	/*
4204745cd3c5Smaybee 	 * On error, restore the original props.
4205745cd3c5Smaybee 	 */
42063b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
42073b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
42083b2aab18SMatthew Ahrens 			/*
42093b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
42103b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
42113b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
42123b2aab18SMatthew Ahrens 			 */
421392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
42143b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
42153b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
421692241e0bSTom Erickson 		}
421792241e0bSTom Erickson 
421892241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
421992241e0bSTom Erickson 			/* We failed to stash the original properties. */
422092241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
422192241e0bSTom Erickson 		}
422292241e0bSTom Erickson 
422392241e0bSTom Erickson 		/*
422492241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
422592241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
422692241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
422792241e0bSTom Erickson 		 * first new-style receive.
422892241e0bSTom Erickson 		 */
422992241e0bSTom Erickson 		if (origprops != NULL &&
423092241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
423192241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
423292241e0bSTom Erickson 		    origprops, NULL) != 0) {
423392241e0bSTom Erickson 			/*
423492241e0bSTom Erickson 			 * We stashed the original properties but failed to
423592241e0bSTom Erickson 			 * restore them.
423692241e0bSTom Erickson 			 */
423792241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
423892241e0bSTom Erickson 		}
4239745cd3c5Smaybee 	}
4240745cd3c5Smaybee out:
4241745cd3c5Smaybee 	nvlist_free(props);
4242745cd3c5Smaybee 	nvlist_free(origprops);
424392241e0bSTom Erickson 	nvlist_free(errors);
4244fa9e4066Sahrens 	releasef(fd);
424592241e0bSTom Erickson 
424692241e0bSTom Erickson 	if (error == 0)
424792241e0bSTom Erickson 		error = props_error;
424892241e0bSTom Erickson 
4249fa9e4066Sahrens 	return (error);
4250fa9e4066Sahrens }
4251fa9e4066Sahrens 
42523cb34c60Sahrens /*
42533cb34c60Sahrens  * inputs:
42543cb34c60Sahrens  * zc_name	name of snapshot to send
42553cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4256a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4257a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4258a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
425919b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
426019b94df9SMatthew Ahrens  *		output size in zc_objset_type.
42613cb34c60Sahrens  *
4262*78f17100SMatthew Ahrens  * outputs:
4263*78f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
42643cb34c60Sahrens  */
4265fa9e4066Sahrens static int
42663cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4267fa9e4066Sahrens {
4268fa9e4066Sahrens 	int error;
42693cb34c60Sahrens 	offset_t off;
427019b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
4271fa9e4066Sahrens 
42723b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
42733b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
42743b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4275a7f53a56SChris Kirby 
42763b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
42773b2aab18SMatthew Ahrens 		if (error != 0)
4278a7f53a56SChris Kirby 			return (error);
42793b2aab18SMatthew Ahrens 
42803b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
42813b2aab18SMatthew Ahrens 		if (error != 0) {
42823b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4283fa9e4066Sahrens 			return (error);
4284fa9e4066Sahrens 		}
42853b2aab18SMatthew Ahrens 
42863b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
42873b2aab18SMatthew Ahrens 			zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
42883b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
42893b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
42904445fffbSMatthew Ahrens 	}
42914445fffbSMatthew Ahrens 
42923b2aab18SMatthew Ahrens 	if (estimate) {
42933b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
42943b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
42953b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
42964445fffbSMatthew Ahrens 
42973b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
42983b2aab18SMatthew Ahrens 		if (error != 0)
42993b2aab18SMatthew Ahrens 			return (error);
43003b2aab18SMatthew Ahrens 
43013b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
43023b2aab18SMatthew Ahrens 		if (error != 0) {
43033b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
43043b2aab18SMatthew Ahrens 			return (error);
43054445fffbSMatthew Ahrens 		}
43064445fffbSMatthew Ahrens 
43073b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
43083b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
43093b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
43103b2aab18SMatthew Ahrens 			if (error != 0) {
43113b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
43123b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
43134445fffbSMatthew Ahrens 				return (error);
43144445fffbSMatthew Ahrens 			}
43154445fffbSMatthew Ahrens 		}
4316fa9e4066Sahrens 
43174445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
431819b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
43193b2aab18SMatthew Ahrens 
43203b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
43213b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
43223b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
43233b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
432419b94df9SMatthew Ahrens 	} else {
432519b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
43263b2aab18SMatthew Ahrens 		if (fp == NULL)
4327be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4328fa9e4066Sahrens 
432919b94df9SMatthew Ahrens 		off = fp->f_offset;
43303b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
43313b2aab18SMatthew Ahrens 		    zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
4332fa9e4066Sahrens 
433319b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
433419b94df9SMatthew Ahrens 			fp->f_offset = off;
433519b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
433619b94df9SMatthew Ahrens 	}
4337fa9e4066Sahrens 	return (error);
4338fa9e4066Sahrens }
4339fa9e4066Sahrens 
43404e3c9f44SBill Pijewski /*
43414e3c9f44SBill Pijewski  * inputs:
43424e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
43434e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
43444e3c9f44SBill Pijewski  *
43454e3c9f44SBill Pijewski  * outputs:
43464e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
43474e3c9f44SBill Pijewski  */
43484e3c9f44SBill Pijewski static int
43494e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
43504e3c9f44SBill Pijewski {
43513b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
43524e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
43534e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
43544e3c9f44SBill Pijewski 	int error;
43554e3c9f44SBill Pijewski 
43563b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
43573b2aab18SMatthew Ahrens 	if (error != 0)
43583b2aab18SMatthew Ahrens 		return (error);
43593b2aab18SMatthew Ahrens 
43603b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
43613b2aab18SMatthew Ahrens 	if (error != 0) {
43623b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
43634e3c9f44SBill Pijewski 		return (error);
43643b2aab18SMatthew Ahrens 	}
43654e3c9f44SBill Pijewski 
43664e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
43674e3c9f44SBill Pijewski 
43684e3c9f44SBill Pijewski 	/*
43694e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
43704e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
43714e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
43724e3c9f44SBill Pijewski 	 * that stream.
43734e3c9f44SBill Pijewski 	 */
43744e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
43754e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
43764e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
43774e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
43784e3c9f44SBill Pijewski 			break;
43794e3c9f44SBill Pijewski 	}
43804e3c9f44SBill Pijewski 
43814e3c9f44SBill Pijewski 	if (dsp != NULL)
43824e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
43834e3c9f44SBill Pijewski 	else
4384be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
43854e3c9f44SBill Pijewski 
43864e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
43874e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
43883b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
43894e3c9f44SBill Pijewski 	return (error);
43904e3c9f44SBill Pijewski }
43914e3c9f44SBill Pijewski 
4392ea8dc4b6Seschrock static int
4393ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4394ea8dc4b6Seschrock {
4395ea8dc4b6Seschrock 	int id, error;
4396ea8dc4b6Seschrock 
4397ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4398ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4399ea8dc4b6Seschrock 
4400ea8dc4b6Seschrock 	if (error == 0)
4401ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4402ea8dc4b6Seschrock 
4403ea8dc4b6Seschrock 	return (error);
4404ea8dc4b6Seschrock }
4405ea8dc4b6Seschrock 
4406ea8dc4b6Seschrock static int
4407ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4408ea8dc4b6Seschrock {
4409ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4410ea8dc4b6Seschrock }
4411ea8dc4b6Seschrock 
4412ea8dc4b6Seschrock static int
4413ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4414ea8dc4b6Seschrock {
4415ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4416ea8dc4b6Seschrock 	int error;
4417ea8dc4b6Seschrock 
4418ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4419ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4420ea8dc4b6Seschrock 
4421ea8dc4b6Seschrock 	zc->zc_guid = id;
4422ea8dc4b6Seschrock 
4423ea8dc4b6Seschrock 	return (error);
4424ea8dc4b6Seschrock }
4425ea8dc4b6Seschrock 
4426ea8dc4b6Seschrock static int
4427ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4428ea8dc4b6Seschrock {
4429ea8dc4b6Seschrock 	spa_t *spa;
4430ea8dc4b6Seschrock 	int error;
4431e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4432ea8dc4b6Seschrock 
4433ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4434ea8dc4b6Seschrock 		return (error);
4435ea8dc4b6Seschrock 
4436e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4437ea8dc4b6Seschrock 	    &count);
4438ea8dc4b6Seschrock 	if (error == 0)
4439e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4440ea8dc4b6Seschrock 	else
4441e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4442ea8dc4b6Seschrock 
4443ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4444ea8dc4b6Seschrock 
4445ea8dc4b6Seschrock 	return (error);
4446ea8dc4b6Seschrock }
4447ea8dc4b6Seschrock 
4448ea8dc4b6Seschrock static int
4449ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4450ea8dc4b6Seschrock {
4451ea8dc4b6Seschrock 	spa_t *spa;
4452ea8dc4b6Seschrock 	vdev_t *vd;
4453bb8b5132Sek 	int error;
4454ea8dc4b6Seschrock 
4455b87f3af3Sperrin 	/*
4456b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4457b87f3af3Sperrin 	 */
4458b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4459b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4460b87f3af3Sperrin 	if (spa == NULL) {
4461b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4462be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4463b87f3af3Sperrin 	}
4464b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4465b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4466b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4467b87f3af3Sperrin 	}
4468468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4469b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4470b87f3af3Sperrin 
4471c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4472468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4473468c413aSTim Haley 	} else {
4474468c413aSTim Haley 		nvlist_t *policy;
4475468c413aSTim Haley 		nvlist_t *config = NULL;
4476468c413aSTim Haley 
4477468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4478be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4479468c413aSTim Haley 
4480468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4481468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4482468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4483468c413aSTim Haley 			    policy, &config);
4484468c413aSTim Haley 			if (config != NULL) {
44854b964adaSGeorge Wilson 				int err;
44864b964adaSGeorge Wilson 
44874b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
44884b964adaSGeorge Wilson 					error = err;
4489468c413aSTim Haley 				nvlist_free(config);
4490468c413aSTim Haley 			}
4491468c413aSTim Haley 			nvlist_free(policy);
4492468c413aSTim Haley 		}
4493468c413aSTim Haley 	}
4494468c413aSTim Haley 
44953b2aab18SMatthew Ahrens 	if (error != 0)
4496ea8dc4b6Seschrock 		return (error);
4497ea8dc4b6Seschrock 
44988f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4499ea8dc4b6Seschrock 
4500e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4501ea8dc4b6Seschrock 		vd = NULL;
4502c5904d13Seschrock 	} else {
4503c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4504fa94a07fSbrendan 		if (vd == NULL) {
4505e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4506fa94a07fSbrendan 			spa_close(spa, FTAG);
4507be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4508fa94a07fSbrendan 		}
4509ea8dc4b6Seschrock 	}
4510ea8dc4b6Seschrock 
4511e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4512e14bb325SJeff Bonwick 
4513e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4514ea8dc4b6Seschrock 
4515e14bb325SJeff Bonwick 	/*
4516e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4517e14bb325SJeff Bonwick 	 */
451854d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4519be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4520ea8dc4b6Seschrock 
4521ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4522ea8dc4b6Seschrock 
452354d692b7SGeorge Wilson 	return (error);
4524ea8dc4b6Seschrock }
4525ea8dc4b6Seschrock 
45264263d13fSGeorge Wilson static int
45274263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
45284263d13fSGeorge Wilson {
45294263d13fSGeorge Wilson 	spa_t *spa;
45304263d13fSGeorge Wilson 	int error;
45314263d13fSGeorge Wilson 
45324263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
45333b2aab18SMatthew Ahrens 	if (error != 0)
45344263d13fSGeorge Wilson 		return (error);
45354263d13fSGeorge Wilson 
45364263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4537d6afdce2SGeorge Wilson 
4538d6afdce2SGeorge Wilson 	/*
4539d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4540d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4541d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4542d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4543d6afdce2SGeorge Wilson 	 */
4544d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
45454263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4546d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4547d6afdce2SGeorge Wilson 
45484263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
45494263d13fSGeorge Wilson 	spa_close(spa, FTAG);
45504263d13fSGeorge Wilson 	return (0);
45514263d13fSGeorge Wilson }
45523cb34c60Sahrens /*
45533cb34c60Sahrens  * inputs:
45543cb34c60Sahrens  * zc_name	name of filesystem
45553cb34c60Sahrens  * zc_value	name of origin snapshot
45563cb34c60Sahrens  *
4557681d9761SEric Taylor  * outputs:
4558681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
45593cb34c60Sahrens  */
456099653d4eSeschrock static int
456199653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
456299653d4eSeschrock {
45630b69c2f0Sahrens 	char *cp;
45640b69c2f0Sahrens 
45650b69c2f0Sahrens 	/*
45660b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
45670b69c2f0Sahrens 	 * it's easier.
45680b69c2f0Sahrens 	 */
4569e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
45700b69c2f0Sahrens 	if (cp)
45710b69c2f0Sahrens 		*cp = '\0';
4572e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
45733b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4574681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
457599653d4eSeschrock }
457699653d4eSeschrock 
457714843421SMatthew Ahrens /*
457814843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
457914843421SMatthew Ahrens  *
458014843421SMatthew Ahrens  * inputs:
458114843421SMatthew Ahrens  * zc_name	name of filesystem
458214843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
458314843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
458414843421SMatthew Ahrens  * zc_guid	RID/UID/GID
458514843421SMatthew Ahrens  *
458614843421SMatthew Ahrens  * outputs:
458714843421SMatthew Ahrens  * zc_cookie	property value
458814843421SMatthew Ahrens  */
458914843421SMatthew Ahrens static int
459014843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
459114843421SMatthew Ahrens {
459214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
459314843421SMatthew Ahrens 	int error;
459414843421SMatthew Ahrens 
459514843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4596be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
459714843421SMatthew Ahrens 
45981412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
45993b2aab18SMatthew Ahrens 	if (error != 0)
460014843421SMatthew Ahrens 		return (error);
460114843421SMatthew Ahrens 
460214843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
460314843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
460414843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
460514843421SMatthew Ahrens 
460614843421SMatthew Ahrens 	return (error);
460714843421SMatthew Ahrens }
460814843421SMatthew Ahrens 
460914843421SMatthew Ahrens /*
461014843421SMatthew Ahrens  * inputs:
461114843421SMatthew Ahrens  * zc_name		name of filesystem
461214843421SMatthew Ahrens  * zc_cookie		zap cursor
461314843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
461414843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
461514843421SMatthew Ahrens  *
461614843421SMatthew Ahrens  * outputs:
461714843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
461814843421SMatthew Ahrens  * zc_cookie	zap cursor
461914843421SMatthew Ahrens  */
462014843421SMatthew Ahrens static int
462114843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
462214843421SMatthew Ahrens {
462314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4624eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
462514843421SMatthew Ahrens 
4626eeb85002STim Haley 	if (bufsize <= 0)
4627be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4628eeb85002STim Haley 
46291412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
46303b2aab18SMatthew Ahrens 	if (error != 0)
463114843421SMatthew Ahrens 		return (error);
463214843421SMatthew Ahrens 
463314843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
463414843421SMatthew Ahrens 
463514843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
463614843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
463714843421SMatthew Ahrens 
463814843421SMatthew Ahrens 	if (error == 0) {
463914843421SMatthew Ahrens 		error = xcopyout(buf,
464014843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
464114843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
464214843421SMatthew Ahrens 	}
464314843421SMatthew Ahrens 	kmem_free(buf, bufsize);
464414843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
464514843421SMatthew Ahrens 
464614843421SMatthew Ahrens 	return (error);
464714843421SMatthew Ahrens }
464814843421SMatthew Ahrens 
464914843421SMatthew Ahrens /*
465014843421SMatthew Ahrens  * inputs:
465114843421SMatthew Ahrens  * zc_name		name of filesystem
465214843421SMatthew Ahrens  *
465314843421SMatthew Ahrens  * outputs:
465414843421SMatthew Ahrens  * none
465514843421SMatthew Ahrens  */
465614843421SMatthew Ahrens static int
465714843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
465814843421SMatthew Ahrens {
465914843421SMatthew Ahrens 	objset_t *os;
46601195e687SMark J Musante 	int error = 0;
466114843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
466214843421SMatthew Ahrens 
466314843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4664503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
466514843421SMatthew Ahrens 			/*
466614843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
466714843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
466814843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
466914843421SMatthew Ahrens 			 */
4670503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
467191948b51SKeith M Wesolowski 			if (error == 0) {
467291948b51SKeith M Wesolowski 				dmu_objset_refresh_ownership(zfsvfs->z_os,
467391948b51SKeith M Wesolowski 				    zfsvfs);
4674503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
467591948b51SKeith M Wesolowski 			}
467614843421SMatthew Ahrens 		}
467714843421SMatthew Ahrens 		if (error == 0)
467814843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
467914843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
468014843421SMatthew Ahrens 	} else {
4681503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4682503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
46833b2aab18SMatthew Ahrens 		if (error != 0)
468414843421SMatthew Ahrens 			return (error);
468514843421SMatthew Ahrens 
468614843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4687503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
468814843421SMatthew Ahrens 	}
468914843421SMatthew Ahrens 
469014843421SMatthew Ahrens 	return (error);
469114843421SMatthew Ahrens }
469214843421SMatthew Ahrens 
4693ecd6cf80Smarks /*
4694ecd6cf80Smarks  * We don't want to have a hard dependency
4695ecd6cf80Smarks  * against some special symbols in sharefs
4696da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4697ecd6cf80Smarks  * the first file system is shared.
4698da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4699ecd6cf80Smarks  */
4700da6c28aaSamw int (*znfsexport_fs)(void *arg);
4701ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4702da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4703da6c28aaSamw 
4704da6c28aaSamw int zfs_nfsshare_inited;
4705da6c28aaSamw int zfs_smbshare_inited;
4706ecd6cf80Smarks 
4707ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4708ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4709da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4710ecd6cf80Smarks kmutex_t zfs_share_lock;
4711ecd6cf80Smarks 
4712da6c28aaSamw static int
4713da6c28aaSamw zfs_init_sharefs()
4714da6c28aaSamw {
4715da6c28aaSamw 	int error;
4716da6c28aaSamw 
4717da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4718da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4719da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4720da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4721da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4722be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4723da6c28aaSamw 	}
4724da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4725da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4726da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4727be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4728da6c28aaSamw 	}
4729da6c28aaSamw 	return (0);
4730da6c28aaSamw }
4731da6c28aaSamw 
4732ecd6cf80Smarks static int
4733ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4734ecd6cf80Smarks {
4735ecd6cf80Smarks 	int error;
4736ecd6cf80Smarks 	int opcode;
4737ecd6cf80Smarks 
4738da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4739da6c28aaSamw 	case ZFS_SHARE_NFS:
4740da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4741da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4742da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4743da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4744da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4745da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4746be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4747da6c28aaSamw 			}
4748da6c28aaSamw 			if (znfsexport_fs == NULL &&
4749da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4750da6c28aaSamw 			    ddi_modsym(nfs_mod,
4751da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4752da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4753be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4754da6c28aaSamw 			}
4755da6c28aaSamw 			error = zfs_init_sharefs();
47563b2aab18SMatthew Ahrens 			if (error != 0) {
4757da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4758be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4759da6c28aaSamw 			}
4760da6c28aaSamw 			zfs_nfsshare_inited = 1;
4761ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4762ecd6cf80Smarks 		}
4763da6c28aaSamw 		break;
4764da6c28aaSamw 	case ZFS_SHARE_SMB:
4765da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4766da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4767da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4768da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4769da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4770da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4771da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4772be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4773da6c28aaSamw 			}
4774da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4775da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4776faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
4777da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4778be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4779da6c28aaSamw 			}
4780da6c28aaSamw 			error = zfs_init_sharefs();
47813b2aab18SMatthew Ahrens 			if (error != 0) {
4782da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4783be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4784da6c28aaSamw 			}
4785da6c28aaSamw 			zfs_smbshare_inited = 1;
4786ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4787ecd6cf80Smarks 		}
4788da6c28aaSamw 		break;
4789da6c28aaSamw 	default:
4790be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4791da6c28aaSamw 	}
4792ecd6cf80Smarks 
4793da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4794da6c28aaSamw 	case ZFS_SHARE_NFS:
4795da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4796da6c28aaSamw 		if (error =
4797da6c28aaSamw 		    znfsexport_fs((void *)
4798da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4799da6c28aaSamw 			return (error);
4800da6c28aaSamw 		break;
4801da6c28aaSamw 	case ZFS_SHARE_SMB:
4802da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4803da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4804da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4805da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4806743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
4807da6c28aaSamw 			return (error);
4808ecd6cf80Smarks 		}
4809da6c28aaSamw 		break;
4810ecd6cf80Smarks 	}
4811ecd6cf80Smarks 
4812da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4813da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4814ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4815ecd6cf80Smarks 
4816da6c28aaSamw 	/*
4817da6c28aaSamw 	 * Add or remove share from sharetab
4818da6c28aaSamw 	 */
4819ecd6cf80Smarks 	error = zshare_fs(opcode,
4820ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4821ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4822ecd6cf80Smarks 
4823ecd6cf80Smarks 	return (error);
4824ecd6cf80Smarks 
4825ecd6cf80Smarks }
4826ecd6cf80Smarks 
4827743a77edSAlan Wright ace_t full_access[] = {
4828743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4829743a77edSAlan Wright };
4830743a77edSAlan Wright 
483199d5e173STim Haley /*
483299d5e173STim Haley  * inputs:
483399d5e173STim Haley  * zc_name		name of containing filesystem
483499d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
483599d5e173STim Haley  *
483699d5e173STim Haley  * outputs:
483799d5e173STim Haley  * zc_obj		next in-use object #
483899d5e173STim Haley  */
483999d5e173STim Haley static int
484099d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
484199d5e173STim Haley {
484299d5e173STim Haley 	objset_t *os = NULL;
484399d5e173STim Haley 	int error;
484499d5e173STim Haley 
484599d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
48463b2aab18SMatthew Ahrens 	if (error != 0)
484799d5e173STim Haley 		return (error);
484899d5e173STim Haley 
484999d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
485099d5e173STim Haley 	    os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
485199d5e173STim Haley 
485299d5e173STim Haley 	dmu_objset_rele(os, FTAG);
485399d5e173STim Haley 	return (error);
485499d5e173STim Haley }
485599d5e173STim Haley 
485699d5e173STim Haley /*
485799d5e173STim Haley  * inputs:
485899d5e173STim Haley  * zc_name		name of filesystem
485999d5e173STim Haley  * zc_value		prefix name for snapshot
486099d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
486199d5e173STim Haley  *
486299d5e173STim Haley  * outputs:
48634445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
486499d5e173STim Haley  */
486599d5e173STim Haley static int
486699d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
486799d5e173STim Haley {
486899d5e173STim Haley 	char *snap_name;
48693b2aab18SMatthew Ahrens 	char *hold_name;
487099d5e173STim Haley 	int error;
48713b2aab18SMatthew Ahrens 	minor_t minor;
487299d5e173STim Haley 
48733b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
48743b2aab18SMatthew Ahrens 	if (error != 0)
487599d5e173STim Haley 		return (error);
487699d5e173STim Haley 
48773b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
48783b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
48793b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
48803b2aab18SMatthew Ahrens 
48813b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
48823b2aab18SMatthew Ahrens 	    hold_name);
48833b2aab18SMatthew Ahrens 	if (error == 0)
48843b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
488599d5e173STim Haley 	strfree(snap_name);
48863b2aab18SMatthew Ahrens 	strfree(hold_name);
48873b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
48883b2aab18SMatthew Ahrens 	return (error);
488999d5e173STim Haley }
489099d5e173STim Haley 
489199d5e173STim Haley /*
489299d5e173STim Haley  * inputs:
489399d5e173STim Haley  * zc_name		name of "to" snapshot
489499d5e173STim Haley  * zc_value		name of "from" snapshot
489599d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
489699d5e173STim Haley  *
489799d5e173STim Haley  * outputs:
489899d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
489999d5e173STim Haley  */
490099d5e173STim Haley static int
490199d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
490299d5e173STim Haley {
490399d5e173STim Haley 	file_t *fp;
490499d5e173STim Haley 	offset_t off;
490599d5e173STim Haley 	int error;
490699d5e173STim Haley 
490799d5e173STim Haley 	fp = getf(zc->zc_cookie);
49083b2aab18SMatthew Ahrens 	if (fp == NULL)
4909be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
491099d5e173STim Haley 
491199d5e173STim Haley 	off = fp->f_offset;
491299d5e173STim Haley 
49133b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
491499d5e173STim Haley 
491599d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
491699d5e173STim Haley 		fp->f_offset = off;
491799d5e173STim Haley 	releasef(zc->zc_cookie);
491899d5e173STim Haley 
491999d5e173STim Haley 	return (error);
492099d5e173STim Haley }
492199d5e173STim Haley 
4922743a77edSAlan Wright /*
4923743a77edSAlan Wright  * Remove all ACL files in shares dir
4924743a77edSAlan Wright  */
4925743a77edSAlan Wright static int
4926743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
4927743a77edSAlan Wright {
4928743a77edSAlan Wright 	zap_cursor_t	zc;
4929743a77edSAlan Wright 	zap_attribute_t	zap;
4930743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4931743a77edSAlan Wright 	int error;
4932743a77edSAlan Wright 
4933743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4934743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4935743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
4936743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4937743a77edSAlan Wright 		    NULL, 0)) != 0)
4938743a77edSAlan Wright 			break;
4939743a77edSAlan Wright 	}
4940743a77edSAlan Wright 	zap_cursor_fini(&zc);
4941743a77edSAlan Wright 	return (error);
4942743a77edSAlan Wright }
4943743a77edSAlan Wright 
4944743a77edSAlan Wright static int
4945743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
4946743a77edSAlan Wright {
4947743a77edSAlan Wright 	vnode_t *vp;
4948743a77edSAlan Wright 	znode_t *dzp;
4949743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
4950743a77edSAlan Wright 	znode_t *sharedir;
4951743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
4952743a77edSAlan Wright 	nvlist_t *nvlist;
4953743a77edSAlan Wright 	char *src, *target;
4954743a77edSAlan Wright 	vattr_t vattr;
4955743a77edSAlan Wright 	vsecattr_t vsec;
4956743a77edSAlan Wright 	int error = 0;
4957743a77edSAlan Wright 
4958743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4959743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
4960743a77edSAlan Wright 		return (error);
4961743a77edSAlan Wright 
4962743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
4963743a77edSAlan Wright 
4964743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4965743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4966743a77edSAlan Wright 	    zc->zc_name) != 0)) {
4967743a77edSAlan Wright 		VN_RELE(vp);
4968be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4969743a77edSAlan Wright 	}
4970743a77edSAlan Wright 
4971743a77edSAlan Wright 	dzp = VTOZ(vp);
4972743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
4973743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
4974743a77edSAlan Wright 
49759e1320c0SMark Shellenbaum 	/*
49769e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
49779e1320c0SMark Shellenbaum 	 */
49789e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
49799e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
49809e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
49819e1320c0SMark Shellenbaum 
49829e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
49839e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
49849e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
49859e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
49869e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
49873b2aab18SMatthew Ahrens 		if (error != 0) {
49889e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
49899e1320c0SMark Shellenbaum 		} else {
49909e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
49919e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
49929e1320c0SMark Shellenbaum 		}
49933b2aab18SMatthew Ahrens 		if (error != 0) {
49949e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
49959e1320c0SMark Shellenbaum 			VN_RELE(vp);
49969e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
49979e1320c0SMark Shellenbaum 			return (error);
49989e1320c0SMark Shellenbaum 		}
49999e1320c0SMark Shellenbaum 	}
50009e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
50019e1320c0SMark Shellenbaum 
50029e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5003743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
50049e1320c0SMark Shellenbaum 		VN_RELE(vp);
5005743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5006743a77edSAlan Wright 		return (error);
5007743a77edSAlan Wright 	}
5008743a77edSAlan Wright 
5009743a77edSAlan Wright 	switch (zc->zc_cookie) {
5010743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5011743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5012743a77edSAlan Wright 		vattr.va_type = VREG;
5013743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5014743a77edSAlan Wright 		vattr.va_uid = 0;
5015743a77edSAlan Wright 		vattr.va_gid = 0;
5016743a77edSAlan Wright 
5017743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5018743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5019743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5020743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5021743a77edSAlan Wright 
5022743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5023743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5024743a77edSAlan Wright 		if (resourcevp)
5025743a77edSAlan Wright 			VN_RELE(resourcevp);
5026743a77edSAlan Wright 		break;
5027743a77edSAlan Wright 
5028743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5029743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5030743a77edSAlan Wright 		    NULL, 0);
5031743a77edSAlan Wright 		break;
5032743a77edSAlan Wright 
5033743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5034743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5035478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5036743a77edSAlan Wright 			VN_RELE(vp);
5037743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5038743a77edSAlan Wright 			return (error);
5039743a77edSAlan Wright 		}
5040743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5041743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5042743a77edSAlan Wright 		    &target)) {
5043743a77edSAlan Wright 			VN_RELE(vp);
504489459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5045743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
50461195e687SMark J Musante 			nvlist_free(nvlist);
5047743a77edSAlan Wright 			return (error);
5048743a77edSAlan Wright 		}
5049743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5050743a77edSAlan Wright 		    kcred, NULL, 0);
5051743a77edSAlan Wright 		nvlist_free(nvlist);
5052743a77edSAlan Wright 		break;
5053743a77edSAlan Wright 
5054743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5055743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5056743a77edSAlan Wright 		break;
5057743a77edSAlan Wright 
5058743a77edSAlan Wright 	default:
5059be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5060743a77edSAlan Wright 		break;
5061743a77edSAlan Wright 	}
5062743a77edSAlan Wright 
5063743a77edSAlan Wright 	VN_RELE(vp);
5064743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5065743a77edSAlan Wright 
5066743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5067743a77edSAlan Wright 
5068743a77edSAlan Wright 	return (error);
5069743a77edSAlan Wright }
5070743a77edSAlan Wright 
5071842727c2SChris Kirby /*
50723b2aab18SMatthew Ahrens  * innvl: {
50733b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
50743b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
50753b2aab18SMatthew Ahrens  * }
5076842727c2SChris Kirby  *
50773b2aab18SMatthew Ahrens  * outnvl: {
50783b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
50793b2aab18SMatthew Ahrens  *     ...
50803b2aab18SMatthew Ahrens  * }
5081842727c2SChris Kirby  */
50823b2aab18SMatthew Ahrens /* ARGSUSED */
5083842727c2SChris Kirby static int
50843b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5085842727c2SChris Kirby {
50863b2aab18SMatthew Ahrens 	nvlist_t *holds;
50873b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5088a7f53a56SChris Kirby 	int error;
5089a7f53a56SChris Kirby 	minor_t minor = 0;
5090842727c2SChris Kirby 
50913b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
50923b2aab18SMatthew Ahrens 	if (error != 0)
5093be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5094a7f53a56SChris Kirby 
50953b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
50963b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
50973b2aab18SMatthew Ahrens 		if (error != 0)
5098a7f53a56SChris Kirby 			return (error);
5099a7f53a56SChris Kirby 	}
5100a7f53a56SChris Kirby 
51013b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
51023b2aab18SMatthew Ahrens 	if (minor != 0)
51033b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5104a7f53a56SChris Kirby 	return (error);
5105842727c2SChris Kirby }
5106842727c2SChris Kirby 
5107842727c2SChris Kirby /*
51083b2aab18SMatthew Ahrens  * innvl is not used.
5109842727c2SChris Kirby  *
51103b2aab18SMatthew Ahrens  * outnvl: {
51113b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
51123b2aab18SMatthew Ahrens  *    ...
51133b2aab18SMatthew Ahrens  * }
5114842727c2SChris Kirby  */
51153b2aab18SMatthew Ahrens /* ARGSUSED */
5116842727c2SChris Kirby static int
51173b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5118842727c2SChris Kirby {
51193b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5120842727c2SChris Kirby }
5121842727c2SChris Kirby 
5122842727c2SChris Kirby /*
51233b2aab18SMatthew Ahrens  * innvl: {
51243b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
51253b2aab18SMatthew Ahrens  *     ...
51263b2aab18SMatthew Ahrens  * }
5127842727c2SChris Kirby  *
51283b2aab18SMatthew Ahrens  * outnvl: {
51293b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
51303b2aab18SMatthew Ahrens  *     ...
51313b2aab18SMatthew Ahrens  * }
5132842727c2SChris Kirby  */
51333b2aab18SMatthew Ahrens /* ARGSUSED */
5134842727c2SChris Kirby static int
51353b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5136842727c2SChris Kirby {
51373b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5138842727c2SChris Kirby }
5139842727c2SChris Kirby 
514019b94df9SMatthew Ahrens /*
514119b94df9SMatthew Ahrens  * inputs:
514219b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
514319b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
514419b94df9SMatthew Ahrens  *
514519b94df9SMatthew Ahrens  * outputs:
514619b94df9SMatthew Ahrens  * zc_cookie		space in bytes
514719b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
514819b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
514919b94df9SMatthew Ahrens  */
515019b94df9SMatthew Ahrens static int
515119b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
515219b94df9SMatthew Ahrens {
515319b94df9SMatthew Ahrens 	int error;
51543b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
515519b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
515619b94df9SMatthew Ahrens 
51573b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
515819b94df9SMatthew Ahrens 	if (error != 0)
515919b94df9SMatthew Ahrens 		return (error);
51603b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
51613b2aab18SMatthew Ahrens 	if (error != 0) {
51623b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
51633b2aab18SMatthew Ahrens 		return (error);
51643b2aab18SMatthew Ahrens 	}
51653b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
516619b94df9SMatthew Ahrens 	if (error != 0) {
516719b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
51683b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
516919b94df9SMatthew Ahrens 		return (error);
517019b94df9SMatthew Ahrens 	}
517119b94df9SMatthew Ahrens 
517219b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
517319b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
517419b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
517519b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
51763b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
517719b94df9SMatthew Ahrens 	return (error);
517819b94df9SMatthew Ahrens }
51793b2aab18SMatthew Ahrens 
518019b94df9SMatthew Ahrens /*
51814445fffbSMatthew Ahrens  * innvl: {
51824445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
51834445fffbSMatthew Ahrens  * }
518419b94df9SMatthew Ahrens  *
51854445fffbSMatthew Ahrens  * outnvl: {
51864445fffbSMatthew Ahrens  *     "used" -> space in bytes
51874445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
51884445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
51894445fffbSMatthew Ahrens  * }
519019b94df9SMatthew Ahrens  */
519119b94df9SMatthew Ahrens static int
51924445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
519319b94df9SMatthew Ahrens {
519419b94df9SMatthew Ahrens 	int error;
51953b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
519619b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
51974445fffbSMatthew Ahrens 	char *firstsnap;
51984445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
519919b94df9SMatthew Ahrens 
52004445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5201be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
52024445fffbSMatthew Ahrens 
52033b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
520419b94df9SMatthew Ahrens 	if (error != 0)
520519b94df9SMatthew Ahrens 		return (error);
52063b2aab18SMatthew Ahrens 
52073b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
52083b2aab18SMatthew Ahrens 	if (error != 0) {
52093b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
52103b2aab18SMatthew Ahrens 		return (error);
52113b2aab18SMatthew Ahrens 	}
52123b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
521319b94df9SMatthew Ahrens 	if (error != 0) {
521419b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
52153b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
521619b94df9SMatthew Ahrens 		return (error);
521719b94df9SMatthew Ahrens 	}
521819b94df9SMatthew Ahrens 
52194445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
522019b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
522119b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
52223b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
52234445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
52244445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
52254445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
522619b94df9SMatthew Ahrens 	return (error);
522719b94df9SMatthew Ahrens }
522819b94df9SMatthew Ahrens 
5229ecd6cf80Smarks /*
52304445fffbSMatthew Ahrens  * innvl: {
52314445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
52324445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
52334445fffbSMatthew Ahrens  * }
52344445fffbSMatthew Ahrens  *
52354445fffbSMatthew Ahrens  * outnvl is unused
5236ecd6cf80Smarks  */
52374445fffbSMatthew Ahrens /* ARGSUSED */
52384445fffbSMatthew Ahrens static int
52394445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
52404445fffbSMatthew Ahrens {
52414445fffbSMatthew Ahrens 	int error;
52424445fffbSMatthew Ahrens 	offset_t off;
52433b2aab18SMatthew Ahrens 	char *fromname = NULL;
52444445fffbSMatthew Ahrens 	int fd;
52454445fffbSMatthew Ahrens 
52464445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
52474445fffbSMatthew Ahrens 	if (error != 0)
5248be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
52494445fffbSMatthew Ahrens 
52503b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
52514445fffbSMatthew Ahrens 
52524445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
52533b2aab18SMatthew Ahrens 	if (fp == NULL)
5254be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
52554445fffbSMatthew Ahrens 
52564445fffbSMatthew Ahrens 	off = fp->f_offset;
52573b2aab18SMatthew Ahrens 	error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
52584445fffbSMatthew Ahrens 
52594445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
52604445fffbSMatthew Ahrens 		fp->f_offset = off;
52614445fffbSMatthew Ahrens 	releasef(fd);
52624445fffbSMatthew Ahrens 	return (error);
52634445fffbSMatthew Ahrens }
52644445fffbSMatthew Ahrens 
52654445fffbSMatthew Ahrens /*
52664445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
52674445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
52684445fffbSMatthew Ahrens  *
52694445fffbSMatthew Ahrens  * innvl: {
52704445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
52714445fffbSMatthew Ahrens  * }
52724445fffbSMatthew Ahrens  *
52734445fffbSMatthew Ahrens  * outnvl: {
52744445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
52754445fffbSMatthew Ahrens  * }
52764445fffbSMatthew Ahrens  */
52774445fffbSMatthew Ahrens static int
52784445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
52794445fffbSMatthew Ahrens {
52803b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
52813b2aab18SMatthew Ahrens 	dsl_dataset_t *fromsnap = NULL;
52823b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
52834445fffbSMatthew Ahrens 	int error;
52844445fffbSMatthew Ahrens 	char *fromname;
52854445fffbSMatthew Ahrens 	uint64_t space;
52864445fffbSMatthew Ahrens 
52873b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
52883b2aab18SMatthew Ahrens 	if (error != 0)
52893b2aab18SMatthew Ahrens 		return (error);
52903b2aab18SMatthew Ahrens 
52913b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
52923b2aab18SMatthew Ahrens 	if (error != 0) {
52933b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
52944445fffbSMatthew Ahrens 		return (error);
52953b2aab18SMatthew Ahrens 	}
52964445fffbSMatthew Ahrens 
52974445fffbSMatthew Ahrens 	error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
52984445fffbSMatthew Ahrens 	if (error == 0) {
52993b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
53003b2aab18SMatthew Ahrens 		if (error != 0) {
53013b2aab18SMatthew Ahrens 			dsl_dataset_rele(tosnap, FTAG);
53023b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
53034445fffbSMatthew Ahrens 			return (error);
53044445fffbSMatthew Ahrens 		}
53054445fffbSMatthew Ahrens 	}
53064445fffbSMatthew Ahrens 
53074445fffbSMatthew Ahrens 	error = dmu_send_estimate(tosnap, fromsnap, &space);
53084445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
53094445fffbSMatthew Ahrens 
53104445fffbSMatthew Ahrens 	if (fromsnap != NULL)
53113b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromsnap, FTAG);
53123b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
53133b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
53144445fffbSMatthew Ahrens 	return (error);
53154445fffbSMatthew Ahrens }
53164445fffbSMatthew Ahrens 
53174445fffbSMatthew Ahrens 
53184445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
53194445fffbSMatthew Ahrens 
53204445fffbSMatthew Ahrens static void
53214445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
53224445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
53234445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
53244445fffbSMatthew Ahrens {
53254445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
53264445fffbSMatthew Ahrens 
53274445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
53284445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
53294445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
53304445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
53314445fffbSMatthew Ahrens 
53324445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
53334445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
53344445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
53354445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
53364445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
53374445fffbSMatthew Ahrens }
53384445fffbSMatthew Ahrens 
53394445fffbSMatthew Ahrens /*
53404445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
53414445fffbSMatthew Ahrens  * each argument to this function.
53424445fffbSMatthew Ahrens  */
53434445fffbSMatthew Ahrens static void
53444445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
53454445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
53464445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
53474445fffbSMatthew Ahrens     boolean_t allow_log)
53484445fffbSMatthew Ahrens {
53494445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
53504445fffbSMatthew Ahrens 
53514445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
53524445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
53534445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
53544445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
53554445fffbSMatthew Ahrens 
53564445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
53574445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
53584445fffbSMatthew Ahrens 
53594445fffbSMatthew Ahrens 	vec->zvec_name = name;
53604445fffbSMatthew Ahrens 	vec->zvec_func = func;
53614445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
53624445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
53634445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
53644445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
53654445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
53664445fffbSMatthew Ahrens }
53674445fffbSMatthew Ahrens 
53684445fffbSMatthew Ahrens static void
53694445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
53704445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
53714445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
53724445fffbSMatthew Ahrens {
53734445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
53744445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
53754445fffbSMatthew Ahrens }
53764445fffbSMatthew Ahrens 
53774445fffbSMatthew Ahrens static void
53784445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
53794445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
53804445fffbSMatthew Ahrens {
53814445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
53824445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
53834445fffbSMatthew Ahrens }
53844445fffbSMatthew Ahrens 
53854445fffbSMatthew Ahrens static void
53864445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
53874445fffbSMatthew Ahrens {
53884445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
53894445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
53904445fffbSMatthew Ahrens }
53914445fffbSMatthew Ahrens 
53924445fffbSMatthew Ahrens static void
53934445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
53944445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
53954445fffbSMatthew Ahrens {
53964445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
53974445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
53984445fffbSMatthew Ahrens }
53994445fffbSMatthew Ahrens 
54004445fffbSMatthew Ahrens static void
54014445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
54024445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
54034445fffbSMatthew Ahrens {
54044445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
54054445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
54064445fffbSMatthew Ahrens }
54074445fffbSMatthew Ahrens 
54084445fffbSMatthew Ahrens static void
54094445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
54104445fffbSMatthew Ahrens {
54114445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
54124445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
54134445fffbSMatthew Ahrens }
54144445fffbSMatthew Ahrens 
54154445fffbSMatthew Ahrens static void
54164445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
54174445fffbSMatthew Ahrens 	zfs_secpolicy_func_t *secpolicy)
54184445fffbSMatthew Ahrens {
54194445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
54204445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
54214445fffbSMatthew Ahrens }
54224445fffbSMatthew Ahrens 
54234445fffbSMatthew Ahrens static void
54244445fffbSMatthew Ahrens zfs_ioctl_init(void)
54254445fffbSMatthew Ahrens {
54264445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
54274445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
54284445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54294445fffbSMatthew Ahrens 
54304445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
54314445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
54324445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
54334445fffbSMatthew Ahrens 
54344445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
54354445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
54364445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
54374445fffbSMatthew Ahrens 
54384445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
54394445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
54404445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
54414445fffbSMatthew Ahrens 
54424445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
54434445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
54444445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
54454445fffbSMatthew Ahrens 
54464445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
54474445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
54484445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54494445fffbSMatthew Ahrens 
54504445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
54514445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
54524445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54534445fffbSMatthew Ahrens 
54544445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
54554445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
54564445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54574445fffbSMatthew Ahrens 
54583b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
54593b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
54603b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54613b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
54623b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
54633b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
54643b2aab18SMatthew Ahrens 
54653b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
54663b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
54673b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
54683b2aab18SMatthew Ahrens 
5469a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5470a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5471a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5472a7027df1SMatthew Ahrens 
5473*78f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5474*78f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5475*78f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5476*78f17100SMatthew Ahrens 
5477*78f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5478*78f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5479*78f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5480*78f17100SMatthew Ahrens 
5481*78f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5482*78f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5483*78f17100SMatthew Ahrens 	    POOL_NAME,
5484*78f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5485*78f17100SMatthew Ahrens 
54864445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
54874445fffbSMatthew Ahrens 
54884445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
54894445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
54904445fffbSMatthew Ahrens 
54914445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
54924445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
54934445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
54944445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
54954445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
54964445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
54974445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
54984445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
54994445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
55004445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
55014445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
55024445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
55034445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
55044445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
55054445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
55064445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
55074445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
55084445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
55094445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
55104445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
55114445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
55124445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
55134445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
55144445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
55154445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
55164445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
55174445fffbSMatthew Ahrens 
55184445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
55194445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
55204445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
55214445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
55224445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
55234445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
55244445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
55254445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
55264445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
55274445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
55284445fffbSMatthew Ahrens 
55294445fffbSMatthew Ahrens 	/*
55304445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
55314445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
55324445fffbSMatthew Ahrens 	 * does the logging of those commands.
55334445fffbSMatthew Ahrens 	 */
55344445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
55354445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
55364445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
55374445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
55384445fffbSMatthew Ahrens 
55394445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
55404445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
55414445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
55424445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
55434445fffbSMatthew Ahrens 
55444445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
55454445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
55464445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
55474445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
55484445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
55494445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
55504445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
55514445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
55524445fffbSMatthew Ahrens 
55534445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
55544445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
55554445fffbSMatthew Ahrens 
55564445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
555722e30981SGeorge Wilson 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
55584445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
55594445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
55604445fffbSMatthew Ahrens 
55614445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
55624445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
55634445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
55644445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
55654445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
55664445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
55674445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
55684445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
55694445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
55704445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
55714445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
55724445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
55734445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
55744445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
55754445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
55764445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
55774445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
55784445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
55794445fffbSMatthew Ahrens 
55804445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
55814445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
55824445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
55834445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
55844445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
55854445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
55864445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
55874445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
55884445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
55894445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
55904445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
55914445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
55924445fffbSMatthew Ahrens 
55934445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
55944445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
55954445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
55964445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
55974445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
55984445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
55994445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
56004445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
56014445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
56024445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
56034445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
56044445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
56054445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
56064445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
56074445fffbSMatthew Ahrens 
56084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
56094445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
56104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
56114445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
56124445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
56134445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
56144445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
56154445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
56164445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
56174445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
56184445fffbSMatthew Ahrens }
5619fa9e4066Sahrens 
562054d692b7SGeorge Wilson int
5621f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5622f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
562354d692b7SGeorge Wilson {
562454d692b7SGeorge Wilson 	spa_t *spa;
562554d692b7SGeorge Wilson 	int error;
562654d692b7SGeorge Wilson 
562754d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
562854d692b7SGeorge Wilson 
5629f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5630f9af39baSGeorge Wilson 		return (0);
5631f9af39baSGeorge Wilson 
563214843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
563354d692b7SGeorge Wilson 	if (error == 0) {
5634f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5635be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5636f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5637be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
563854d692b7SGeorge Wilson 		spa_close(spa, FTAG);
563954d692b7SGeorge Wilson 	}
564054d692b7SGeorge Wilson 	return (error);
564154d692b7SGeorge Wilson }
564254d692b7SGeorge Wilson 
5643c99e4bdcSChris Kirby /*
5644c99e4bdcSChris Kirby  * Find a free minor number.
5645c99e4bdcSChris Kirby  */
5646c99e4bdcSChris Kirby minor_t
5647c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5648c99e4bdcSChris Kirby {
5649c99e4bdcSChris Kirby 	static minor_t last_minor;
5650c99e4bdcSChris Kirby 	minor_t m;
5651c99e4bdcSChris Kirby 
5652c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5653c99e4bdcSChris Kirby 
5654c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5655c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5656c99e4bdcSChris Kirby 			m = 1;
5657c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5658c99e4bdcSChris Kirby 			last_minor = m;
5659c99e4bdcSChris Kirby 			return (m);
5660c99e4bdcSChris Kirby 		}
5661c99e4bdcSChris Kirby 	}
5662c99e4bdcSChris Kirby 
5663c99e4bdcSChris Kirby 	return (0);
5664c99e4bdcSChris Kirby }
5665c99e4bdcSChris Kirby 
5666c99e4bdcSChris Kirby static int
5667c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5668c99e4bdcSChris Kirby {
5669c99e4bdcSChris Kirby 	minor_t minor;
5670c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5671c99e4bdcSChris Kirby 
5672c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5673c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5674c99e4bdcSChris Kirby 
5675c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5676c99e4bdcSChris Kirby 	if (minor == 0)
5677be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5678c99e4bdcSChris Kirby 
5679c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5680be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5681c99e4bdcSChris Kirby 
5682c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5683c99e4bdcSChris Kirby 
5684c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
5685c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
5686c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5687c99e4bdcSChris Kirby 
5688c99e4bdcSChris Kirby 	return (0);
5689c99e4bdcSChris Kirby }
5690c99e4bdcSChris Kirby 
5691c99e4bdcSChris Kirby static void
5692c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5693c99e4bdcSChris Kirby {
5694c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5695c99e4bdcSChris Kirby 
5696c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
5697c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
5698c99e4bdcSChris Kirby }
5699c99e4bdcSChris Kirby 
5700c99e4bdcSChris Kirby void *
5701c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5702c99e4bdcSChris Kirby {
5703c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
5704c99e4bdcSChris Kirby 
5705c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
5706c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
5707c99e4bdcSChris Kirby 		return (NULL);
5708c99e4bdcSChris Kirby 
5709c99e4bdcSChris Kirby 	return (zp->zss_data);
5710c99e4bdcSChris Kirby }
5711c99e4bdcSChris Kirby 
5712c99e4bdcSChris Kirby static int
5713c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5714c99e4bdcSChris Kirby {
5715c99e4bdcSChris Kirby 	int error = 0;
5716c99e4bdcSChris Kirby 
5717c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
5718c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
5719c99e4bdcSChris Kirby 
5720c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
5721c99e4bdcSChris Kirby 	if (flag & FEXCL) {
5722c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
5723c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
5724c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5725c99e4bdcSChris Kirby 	}
5726c99e4bdcSChris Kirby 
5727c99e4bdcSChris Kirby 	return (error);
5728c99e4bdcSChris Kirby }
5729c99e4bdcSChris Kirby 
5730c99e4bdcSChris Kirby static int
5731c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5732c99e4bdcSChris Kirby {
5733c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
5734c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5735c99e4bdcSChris Kirby 
5736c99e4bdcSChris Kirby 	if (minor == 0)
5737c99e4bdcSChris Kirby 		return (0);
5738c99e4bdcSChris Kirby 
5739c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5740c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5741c99e4bdcSChris Kirby 	if (zo == NULL) {
5742c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5743c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5744c99e4bdcSChris Kirby 	}
5745c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5746c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5747c99e4bdcSChris Kirby 
5748c99e4bdcSChris Kirby 	return (0);
5749c99e4bdcSChris Kirby }
5750c99e4bdcSChris Kirby 
5751fa9e4066Sahrens static int
5752fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5753fa9e4066Sahrens {
5754fa9e4066Sahrens 	zfs_cmd_t *zc;
57554445fffbSMatthew Ahrens 	uint_t vecnum;
57564445fffbSMatthew Ahrens 	int error, rc, len;
5757c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
57584445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
57594445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
57604445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
5761fa9e4066Sahrens 
5762c99e4bdcSChris Kirby 	if (minor != 0 &&
5763c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5764fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5765fa9e4066Sahrens 
57664445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
576791ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5768fa9e4066Sahrens 
57694445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5770be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
57714445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
5772fa9e4066Sahrens 
5773fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5774fa9e4066Sahrens 
5775478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
57764445fffbSMatthew Ahrens 	if (error != 0) {
5777be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
57784445fffbSMatthew Ahrens 		goto out;
57794445fffbSMatthew Ahrens 	}
5780fa9e4066Sahrens 
57814445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
57824445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
57834445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
57844445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
57854445fffbSMatthew Ahrens 		if (error != 0)
57864445fffbSMatthew Ahrens 			goto out;
57874445fffbSMatthew Ahrens 	}
5788fa9e4066Sahrens 
5789fa9e4066Sahrens 	/*
5790fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
5791fa9e4066Sahrens 	 * the lower layers.
5792fa9e4066Sahrens 	 */
57934445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
57944445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
57954445fffbSMatthew Ahrens 	case POOL_NAME:
57964445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5797be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
57984445fffbSMatthew Ahrens 		else
5799f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
58004445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
58014445fffbSMatthew Ahrens 		break;
5802fa9e4066Sahrens 
58034445fffbSMatthew Ahrens 	case DATASET_NAME:
58044445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5805be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
58064445fffbSMatthew Ahrens 		else
5807f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
58084445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
58094445fffbSMatthew Ahrens 		break;
58105ad82045Snd 
58114445fffbSMatthew Ahrens 	case NO_NAME:
58124445fffbSMatthew Ahrens 		break;
5813fa9e4066Sahrens 	}
5814fa9e4066Sahrens 
5815fa9e4066Sahrens 
58164445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
58174445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
58184445fffbSMatthew Ahrens 
58194445fffbSMatthew Ahrens 	if (error != 0)
58204445fffbSMatthew Ahrens 		goto out;
58214445fffbSMatthew Ahrens 
58224445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
5823*78f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
58244445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
58254445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
58264445fffbSMatthew Ahrens 
58274445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
58284445fffbSMatthew Ahrens 		nvlist_t *outnvl;
58294445fffbSMatthew Ahrens 		int puterror = 0;
58304445fffbSMatthew Ahrens 		spa_t *spa;
58314445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
58324445fffbSMatthew Ahrens 
58334445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
58344445fffbSMatthew Ahrens 
58354445fffbSMatthew Ahrens 		/*
58364445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
58374445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
58384445fffbSMatthew Ahrens 		 */
58394445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
58404445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
58414445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
58424445fffbSMatthew Ahrens 			    vec->zvec_name);
58434445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
58444445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
58454445fffbSMatthew Ahrens 				    innvl);
58464445fffbSMatthew Ahrens 			}
58474445fffbSMatthew Ahrens 		}
58484445fffbSMatthew Ahrens 
58494445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
58504445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
58514445fffbSMatthew Ahrens 
58524445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
58534445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
58544445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
58554445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
58564445fffbSMatthew Ahrens 				    outnvl);
58574445fffbSMatthew Ahrens 			}
58584445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
58594445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
58604445fffbSMatthew Ahrens 		}
58614445fffbSMatthew Ahrens 		fnvlist_free(lognv);
58624445fffbSMatthew Ahrens 
58634445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
58644445fffbSMatthew Ahrens 			int smusherror = 0;
58654445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
58664445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
58674445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
58684445fffbSMatthew Ahrens 			}
58694445fffbSMatthew Ahrens 			if (smusherror == 0)
58704445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
58714445fffbSMatthew Ahrens 		}
58724445fffbSMatthew Ahrens 
58734445fffbSMatthew Ahrens 		if (puterror != 0)
58744445fffbSMatthew Ahrens 			error = puterror;
58754445fffbSMatthew Ahrens 
58764445fffbSMatthew Ahrens 		nvlist_free(outnvl);
58774445fffbSMatthew Ahrens 	} else {
58784445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
58794445fffbSMatthew Ahrens 	}
58804445fffbSMatthew Ahrens 
58814445fffbSMatthew Ahrens out:
58824445fffbSMatthew Ahrens 	nvlist_free(innvl);
5883478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
58844445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
5885be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
58864445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
58874445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
58884445fffbSMatthew Ahrens 		if (s != NULL)
58894445fffbSMatthew Ahrens 			strfree(s);
58904445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
58914445fffbSMatthew Ahrens 	} else {
58924445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
58934445fffbSMatthew Ahrens 			strfree(saved_poolname);
5894ecd6cf80Smarks 	}
5895fa9e4066Sahrens 
5896fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
5897fa9e4066Sahrens 	return (error);
5898fa9e4066Sahrens }
5899fa9e4066Sahrens 
5900fa9e4066Sahrens static int
5901fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5902fa9e4066Sahrens {
5903fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
5904fa9e4066Sahrens 		return (DDI_FAILURE);
5905fa9e4066Sahrens 
5906fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5907fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
5908fa9e4066Sahrens 		return (DDI_FAILURE);
5909fa9e4066Sahrens 
5910fa9e4066Sahrens 	zfs_dip = dip;
5911fa9e4066Sahrens 
5912fa9e4066Sahrens 	ddi_report_dev(dip);
5913fa9e4066Sahrens 
5914fa9e4066Sahrens 	return (DDI_SUCCESS);
5915fa9e4066Sahrens }
5916fa9e4066Sahrens 
5917fa9e4066Sahrens static int
5918fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5919fa9e4066Sahrens {
5920fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
5921fa9e4066Sahrens 		return (DDI_FAILURE);
5922fa9e4066Sahrens 
5923fa9e4066Sahrens 	if (cmd != DDI_DETACH)
5924fa9e4066Sahrens 		return (DDI_FAILURE);
5925fa9e4066Sahrens 
5926fa9e4066Sahrens 	zfs_dip = NULL;
5927fa9e4066Sahrens 
5928fa9e4066Sahrens 	ddi_prop_remove_all(dip);
5929fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
5930fa9e4066Sahrens 
5931fa9e4066Sahrens 	return (DDI_SUCCESS);
5932fa9e4066Sahrens }
5933fa9e4066Sahrens 
5934fa9e4066Sahrens /*ARGSUSED*/
5935fa9e4066Sahrens static int
5936fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5937fa9e4066Sahrens {
5938fa9e4066Sahrens 	switch (infocmd) {
5939fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
5940fa9e4066Sahrens 		*result = zfs_dip;
5941fa9e4066Sahrens 		return (DDI_SUCCESS);
5942fa9e4066Sahrens 
5943fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
5944a0965f35Sbonwick 		*result = (void *)0;
5945fa9e4066Sahrens 		return (DDI_SUCCESS);
5946fa9e4066Sahrens 	}
5947fa9e4066Sahrens 
5948fa9e4066Sahrens 	return (DDI_FAILURE);
5949fa9e4066Sahrens }
5950fa9e4066Sahrens 
5951fa9e4066Sahrens /*
5952fa9e4066Sahrens  * OK, so this is a little weird.
5953fa9e4066Sahrens  *
5954fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
5955fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5956fa9e4066Sahrens  *
5957fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
5958fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
5959fa9e4066Sahrens  */
5960fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
5961c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
5962c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
5963fa9e4066Sahrens 	zvol_strategy,	/* strategy */
5964fa9e4066Sahrens 	nodev,		/* print */
5965e7cbe64fSgw 	zvol_dump,	/* dump */
5966fa9e4066Sahrens 	zvol_read,	/* read */
5967fa9e4066Sahrens 	zvol_write,	/* write */
5968fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
5969fa9e4066Sahrens 	nodev,		/* devmap */
5970fa9e4066Sahrens 	nodev,		/* mmap */
5971fa9e4066Sahrens 	nodev,		/* segmap */
5972fa9e4066Sahrens 	nochpoll,	/* poll */
5973fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
5974fa9e4066Sahrens 	NULL,		/* streamtab */
5975fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
5976fa9e4066Sahrens 	CB_REV,		/* version */
5977feb08c6bSbillm 	nodev,		/* async read */
5978feb08c6bSbillm 	nodev,		/* async write */
5979fa9e4066Sahrens };
5980fa9e4066Sahrens 
5981fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
5982fa9e4066Sahrens 	DEVO_REV,	/* version */
5983fa9e4066Sahrens 	0,		/* refcnt */
5984fa9e4066Sahrens 	zfs_info,	/* info */
5985fa9e4066Sahrens 	nulldev,	/* identify */
5986fa9e4066Sahrens 	nulldev,	/* probe */
5987fa9e4066Sahrens 	zfs_attach,	/* attach */
5988fa9e4066Sahrens 	zfs_detach,	/* detach */
5989fa9e4066Sahrens 	nodev,		/* reset */
5990fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
599119397407SSherry Moore 	NULL,		/* no bus operations */
599219397407SSherry Moore 	NULL,		/* power */
599319397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
5994fa9e4066Sahrens };
5995fa9e4066Sahrens 
5996fa9e4066Sahrens static struct modldrv zfs_modldrv = {
599719397407SSherry Moore 	&mod_driverops,
599819397407SSherry Moore 	"ZFS storage pool",
599919397407SSherry Moore 	&zfs_dev_ops
6000fa9e4066Sahrens };
6001fa9e4066Sahrens 
6002fa9e4066Sahrens static struct modlinkage modlinkage = {
6003fa9e4066Sahrens 	MODREV_1,
6004fa9e4066Sahrens 	(void *)&zfs_modlfs,
6005fa9e4066Sahrens 	(void *)&zfs_modldrv,
6006fa9e4066Sahrens 	NULL
6007fa9e4066Sahrens };
6008fa9e4066Sahrens 
60094445fffbSMatthew Ahrens static void
60104445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
60114445fffbSMatthew Ahrens {
60124445fffbSMatthew Ahrens 	char *poolname = arg;
60134445fffbSMatthew Ahrens 	strfree(poolname);
60144445fffbSMatthew Ahrens }
6015ec533521Sfr 
6016fa9e4066Sahrens int
6017fa9e4066Sahrens _init(void)
6018fa9e4066Sahrens {
6019fa9e4066Sahrens 	int error;
6020fa9e4066Sahrens 
6021a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
6022a0965f35Sbonwick 	zfs_init();
6023a0965f35Sbonwick 	zvol_init();
60244445fffbSMatthew Ahrens 	zfs_ioctl_init();
6025a0965f35Sbonwick 
6026a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
6027a0965f35Sbonwick 		zvol_fini();
6028a0965f35Sbonwick 		zfs_fini();
6029a0965f35Sbonwick 		spa_fini();
6030fa9e4066Sahrens 		return (error);
6031a0965f35Sbonwick 	}
6032fa9e4066Sahrens 
6033ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
60344445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
60354445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6036ec533521Sfr 
6037fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6038fa9e4066Sahrens 	ASSERT(error == 0);
6039ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6040fa9e4066Sahrens 
6041fa9e4066Sahrens 	return (0);
6042fa9e4066Sahrens }
6043fa9e4066Sahrens 
6044fa9e4066Sahrens int
6045fa9e4066Sahrens _fini(void)
6046fa9e4066Sahrens {
6047fa9e4066Sahrens 	int error;
6048fa9e4066Sahrens 
6049ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6050be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6051fa9e4066Sahrens 
6052fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
6053fa9e4066Sahrens 		return (error);
6054fa9e4066Sahrens 
6055fa9e4066Sahrens 	zvol_fini();
6056fa9e4066Sahrens 	zfs_fini();
6057fa9e4066Sahrens 	spa_fini();
6058da6c28aaSamw 	if (zfs_nfsshare_inited)
6059ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
6060da6c28aaSamw 	if (zfs_smbshare_inited)
6061da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
6062da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
6063ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
6064fa9e4066Sahrens 
6065ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
6066fa9e4066Sahrens 	ldi_ident_release(zfs_li);
6067fa9e4066Sahrens 	zfs_li = NULL;
6068ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
6069fa9e4066Sahrens 
6070fa9e4066Sahrens 	return (error);
6071fa9e4066Sahrens }
6072fa9e4066Sahrens 
6073fa9e4066Sahrens int
6074fa9e4066Sahrens _info(struct modinfo *modinfop)
6075fa9e4066Sahrens {
6076fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
6077fa9e4066Sahrens }
6078