xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 9b6cddcfb35f476ab090ca2318bb30d98c21ecb2)
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
255878fad7SDan McDonald  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26752fd8daSJosef 'Jeff' Sipek  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
27a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
286de9bb56SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
29a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
30a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
31c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
32*9b6cddcfSToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
334445fffbSMatthew Ahrens  */
344445fffbSMatthew Ahrens 
354445fffbSMatthew Ahrens /*
364445fffbSMatthew Ahrens  * ZFS ioctls.
374445fffbSMatthew Ahrens  *
384445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
394445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
404445fffbSMatthew Ahrens  *
414445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
424445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
434445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
444445fffbSMatthew Ahrens  *
454445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
464445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
474445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
484445fffbSMatthew Ahrens  *
494445fffbSMatthew Ahrens  * The registration arguments are as follows:
504445fffbSMatthew Ahrens  *
514445fffbSMatthew Ahrens  * const char *name
524445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
534445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
544445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
554445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
564445fffbSMatthew Ahrens  *
574445fffbSMatthew Ahrens  * zfs_ioc_t ioc
584445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
594445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
604445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
614445fffbSMatthew Ahrens  *
624445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
634445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
644445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
654445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
664445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
674445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
684445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
694445fffbSMatthew Ahrens  *
704445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
714445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
724445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
734445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
744445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
754445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
764445fffbSMatthew Ahrens  *   doesn't have invalid characters).
774445fffbSMatthew Ahrens  *
784445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
794445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
804445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
814445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
824445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
834445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
844445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
854445fffbSMatthew Ahrens  *
864445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
874445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
884445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
894445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
904445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
914445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
924445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
934445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
944445fffbSMatthew Ahrens  *
954445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
964445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
974445fffbSMatthew Ahrens  *
984445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
994445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
1004445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
1014445fffbSMatthew Ahrens  *   will be called with the following arguments:
1024445fffbSMatthew Ahrens  *
1034445fffbSMatthew Ahrens  *   const char *name
1044445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1054445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1064445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1074445fffbSMatthew Ahrens  *
1084445fffbSMatthew Ahrens  *   nvlist_t *innvl
1094445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1104445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1114445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1124445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1134445fffbSMatthew Ahrens  *
1144445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1154445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1164445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1174445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1184445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1194445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1204445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1214445fffbSMatthew Ahrens  *
1224445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1234445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1244445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1254445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1264445fffbSMatthew Ahrens  *         the ioctl again.
1274445fffbSMatthew Ahrens  *
1284445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1294445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1304445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1314445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1324445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1334445fffbSMatthew Ahrens  *         deserialization failing.
134e9103aaeSGarrett D'Amore  */
135fa9e4066Sahrens 
136fa9e4066Sahrens #include <sys/types.h>
137fa9e4066Sahrens #include <sys/param.h>
138fa9e4066Sahrens #include <sys/errno.h>
139fa9e4066Sahrens #include <sys/uio.h>
140fa9e4066Sahrens #include <sys/buf.h>
141fa9e4066Sahrens #include <sys/modctl.h>
142fa9e4066Sahrens #include <sys/open.h>
143fa9e4066Sahrens #include <sys/file.h>
144fa9e4066Sahrens #include <sys/kmem.h>
145fa9e4066Sahrens #include <sys/conf.h>
146fa9e4066Sahrens #include <sys/cmn_err.h>
147fa9e4066Sahrens #include <sys/stat.h>
148fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1494201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
150da6c28aaSamw #include <sys/zfs_znode.h>
151fa9e4066Sahrens #include <sys/zap.h>
152fa9e4066Sahrens #include <sys/spa.h>
153b1b8ab34Slling #include <sys/spa_impl.h>
154fa9e4066Sahrens #include <sys/vdev.h>
1554201a95eSRic Aleshire #include <sys/priv_impl.h>
156fa9e4066Sahrens #include <sys/dmu.h>
157fa9e4066Sahrens #include <sys/dsl_dir.h>
158fa9e4066Sahrens #include <sys/dsl_dataset.h>
159fa9e4066Sahrens #include <sys/dsl_prop.h>
160ecd6cf80Smarks #include <sys/dsl_deleg.h>
161ecd6cf80Smarks #include <sys/dmu_objset.h>
1624e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1633b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
164fa9e4066Sahrens #include <sys/ddi.h>
165fa9e4066Sahrens #include <sys/sunddi.h>
166fa9e4066Sahrens #include <sys/sunldi.h>
167fa9e4066Sahrens #include <sys/policy.h>
168fa9e4066Sahrens #include <sys/zone.h>
169fa9e4066Sahrens #include <sys/nvpair.h>
170fa9e4066Sahrens #include <sys/pathname.h>
171fa9e4066Sahrens #include <sys/mount.h>
172fa9e4066Sahrens #include <sys/sdt.h>
173fa9e4066Sahrens #include <sys/fs/zfs.h>
174fa9e4066Sahrens #include <sys/zfs_ctldir.h>
175da6c28aaSamw #include <sys/zfs_dir.h>
176c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
177a2eea2e1Sahrens #include <sys/zvol.h>
1783f9d6ad7SLin Ling #include <sys/dsl_scan.h>
179ecd6cf80Smarks #include <sharefs/share.h>
180f18faf3fSek110237 #include <sys/dmu_objset.h>
1813b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1823b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18378f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1843b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
185a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
18645818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
187d78b796cSAndreas Jaekel #include <sys/zfs_events.h>
188fa9e4066Sahrens 
189fa9e4066Sahrens #include "zfs_namecheck.h"
190e9dbad6fSeschrock #include "zfs_prop.h"
191ecd6cf80Smarks #include "zfs_deleg.h"
1920a586ceaSMark Shellenbaum #include "zfs_comutil.h"
193fa9e4066Sahrens 
194fa9e4066Sahrens extern struct modlfs zfs_modlfs;
195fa9e4066Sahrens 
196fa9e4066Sahrens extern void zfs_init(void);
197fa9e4066Sahrens extern void zfs_fini(void);
198fa9e4066Sahrens 
199fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
200fa9e4066Sahrens dev_info_t *zfs_dip;
201fa9e4066Sahrens 
2024445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2034445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2044445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2054445fffbSMatthew Ahrens 
2064445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2074445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2084445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
209fa9e4066Sahrens 
21054d692b7SGeorge Wilson typedef enum {
211e7437265Sahrens 	NO_NAME,
212e7437265Sahrens 	POOL_NAME,
213e7437265Sahrens 	DATASET_NAME
21454d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21554d692b7SGeorge Wilson 
216f9af39baSGeorge Wilson typedef enum {
217f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
218f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2194445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
220f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
221f9af39baSGeorge Wilson 
22254d692b7SGeorge Wilson typedef struct zfs_ioc_vec {
2234445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
22454d692b7SGeorge Wilson 	zfs_ioc_func_t		*zvec_func;
22554d692b7SGeorge Wilson 	zfs_secpolicy_func_t	*zvec_secpolicy;
22654d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2274445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
228f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2294445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2304445fffbSMatthew Ahrens 	const char		*zvec_name;
231fa9e4066Sahrens } zfs_ioc_vec_t;
232fa9e4066Sahrens 
23314843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
23414843421SMatthew Ahrens static const char *userquota_perms[] = {
23514843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23614843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23714843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23814843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23914843421SMatthew Ahrens };
24014843421SMatthew Ahrens 
24114843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
24292241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
24392241e0bSTom Erickson     cred_t *cr);
24492241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24592241e0bSTom Erickson     nvlist_t **errors);
2460a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2470a48a24eStimh     boolean_t *);
2484445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2494445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2500a48a24eStimh 
2512acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
252a6f561b4SSašo Kiselkov 
253fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
254fa9e4066Sahrens void
255fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	const char *newfile;
2583f9d6ad7SLin Ling 	char buf[512];
259fa9e4066Sahrens 	va_list adx;
260fa9e4066Sahrens 
261fa9e4066Sahrens 	/*
262fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
263fa9e4066Sahrens 	 */
264fa9e4066Sahrens 	newfile = strrchr(file, '/');
265fa9e4066Sahrens 	if (newfile != NULL) {
266fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
267fa9e4066Sahrens 	} else {
268fa9e4066Sahrens 		newfile = file;
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 
271fa9e4066Sahrens 	va_start(adx, fmt);
272fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
273fa9e4066Sahrens 	va_end(adx);
274fa9e4066Sahrens 
275fa9e4066Sahrens 	/*
276fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
277fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
278fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
279fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
280fa9e4066Sahrens 	 * arg0 = file name
281fa9e4066Sahrens 	 * arg1 = function name
282fa9e4066Sahrens 	 * arg2 = line number
283fa9e4066Sahrens 	 * arg3 = message
284fa9e4066Sahrens 	 */
285fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
286fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
287fa9e4066Sahrens }
288fa9e4066Sahrens 
289ecd6cf80Smarks static void
290228975ccSek110237 history_str_free(char *buf)
291228975ccSek110237 {
292228975ccSek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
293228975ccSek110237 }
294228975ccSek110237 
295228975ccSek110237 static char *
296228975ccSek110237 history_str_get(zfs_cmd_t *zc)
297228975ccSek110237 {
298228975ccSek110237 	char *buf;
299228975ccSek110237 
300228975ccSek110237 	if (zc->zc_history == NULL)
301228975ccSek110237 		return (NULL);
302228975ccSek110237 
303228975ccSek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
304228975ccSek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
305228975ccSek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
306228975ccSek110237 		history_str_free(buf);
307228975ccSek110237 		return (NULL);
308228975ccSek110237 	}
309228975ccSek110237 
310228975ccSek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
311228975ccSek110237 
312228975ccSek110237 	return (buf);
313228975ccSek110237 }
314228975ccSek110237 
315c2a93d44Stimh /*
31615e6edf1Sgw25295  * Check to see if the named dataset is currently defined as bootable
31715e6edf1Sgw25295  */
31815e6edf1Sgw25295 static boolean_t
31915e6edf1Sgw25295 zfs_is_bootfs(const char *name)
32015e6edf1Sgw25295 {
32115e6edf1Sgw25295 	objset_t *os;
32215e6edf1Sgw25295 
323503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
324503ad85cSMatthew Ahrens 		boolean_t ret;
325b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
326503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
32715e6edf1Sgw25295 		return (ret);
32815e6edf1Sgw25295 	}
329503ad85cSMatthew Ahrens 	return (B_FALSE);
330503ad85cSMatthew Ahrens }
33115e6edf1Sgw25295 
33215e6edf1Sgw25295 /*
333c2a93d44Stimh  * Return non-zero if the spa version is less than requested version.
334c2a93d44Stimh  */
335da6c28aaSamw static int
3360a48a24eStimh zfs_earlier_version(const char *name, int version)
337da6c28aaSamw {
338da6c28aaSamw 	spa_t *spa;
339da6c28aaSamw 
340da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
341da6c28aaSamw 		if (spa_version(spa) < version) {
342da6c28aaSamw 			spa_close(spa, FTAG);
343da6c28aaSamw 			return (1);
344da6c28aaSamw 		}
345da6c28aaSamw 		spa_close(spa, FTAG);
346da6c28aaSamw 	}
347da6c28aaSamw 	return (0);
348da6c28aaSamw }
349da6c28aaSamw 
3509e6eda55Smarks /*
351745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3529e6eda55Smarks  */
353745cd3c5Smaybee static boolean_t
354745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3559e6eda55Smarks {
3569e6eda55Smarks 	objset_t *os;
357745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3589e6eda55Smarks 
359503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
360745cd3c5Smaybee 		uint64_t zplversion;
3619e6eda55Smarks 
362503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
363503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
364503ad85cSMatthew Ahrens 			return (B_TRUE);
365503ad85cSMatthew Ahrens 		}
366503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
367745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
368745cd3c5Smaybee 			rc = zplversion < version;
369503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3709e6eda55Smarks 	}
3719e6eda55Smarks 	return (rc);
3729e6eda55Smarks }
3739e6eda55Smarks 
374228975ccSek110237 static void
375ecd6cf80Smarks zfs_log_history(zfs_cmd_t *zc)
376ecd6cf80Smarks {
377ecd6cf80Smarks 	spa_t *spa;
37840feaa91Sahrens 	char *buf;
379ecd6cf80Smarks 
380228975ccSek110237 	if ((buf = history_str_get(zc)) == NULL)
381ecd6cf80Smarks 		return;
382ecd6cf80Smarks 
383228975ccSek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
384e7437265Sahrens 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3854445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
386ecd6cf80Smarks 		spa_close(spa, FTAG);
387228975ccSek110237 	}
388228975ccSek110237 	history_str_free(buf);
389ecd6cf80Smarks }
390ecd6cf80Smarks 
391fa9e4066Sahrens /*
392fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
393fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
394fa9e4066Sahrens  */
395fa9e4066Sahrens /* ARGSUSED */
396fa9e4066Sahrens static int
3974445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
398fa9e4066Sahrens {
399fa9e4066Sahrens 	return (0);
400fa9e4066Sahrens }
401fa9e4066Sahrens 
402fa9e4066Sahrens /*
403fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
404fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
405fa9e4066Sahrens  */
406fa9e4066Sahrens /* ARGSUSED */
407fa9e4066Sahrens static int
4084445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
409fa9e4066Sahrens {
410fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
411ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
412fa9e4066Sahrens 		return (0);
413fa9e4066Sahrens 
414be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417fa9e4066Sahrens static int
418a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	int writable = 1;
421fa9e4066Sahrens 
422fa9e4066Sahrens 	/*
423fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
424fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
425fa9e4066Sahrens 	 */
426fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
427fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
428be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
429fa9e4066Sahrens 
430fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
431fa9e4066Sahrens 		/*
432fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
433fa9e4066Sahrens 		 * global zone.
434fa9e4066Sahrens 		 */
435fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
436be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
437fa9e4066Sahrens 	} else {
438fa9e4066Sahrens 		/*
439fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
440fa9e4066Sahrens 		 */
441fa9e4066Sahrens 		if (!zoned)
442be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
443fa9e4066Sahrens 
444fa9e4066Sahrens 		/* must be writable by this zone */
445fa9e4066Sahrens 		if (!writable)
446be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
447fa9e4066Sahrens 	}
448fa9e4066Sahrens 	return (0);
449fa9e4066Sahrens }
450fa9e4066Sahrens 
451a7f53a56SChris Kirby static int
452a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
453a7f53a56SChris Kirby {
454a7f53a56SChris Kirby 	uint64_t zoned;
455a7f53a56SChris Kirby 
456a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
457be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
458a7f53a56SChris Kirby 
459a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
460a7f53a56SChris Kirby }
461a7f53a56SChris Kirby 
462a7f53a56SChris Kirby static int
463a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
464a7f53a56SChris Kirby {
465a7f53a56SChris Kirby 	uint64_t zoned;
466a7f53a56SChris Kirby 
4673b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
468be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
469a7f53a56SChris Kirby 
470a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
471a7f53a56SChris Kirby }
472a7f53a56SChris Kirby 
4734445fffbSMatthew Ahrens static int
474a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
475a7f53a56SChris Kirby     const char *perm, cred_t *cr)
476a7f53a56SChris Kirby {
477a7f53a56SChris Kirby 	int error;
478a7f53a56SChris Kirby 
479a7f53a56SChris Kirby 	error = zfs_dozonecheck_ds(name, ds, cr);
480a7f53a56SChris Kirby 	if (error == 0) {
481a7f53a56SChris Kirby 		error = secpolicy_zfs(cr);
4823b2aab18SMatthew Ahrens 		if (error != 0)
4834445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
484a7f53a56SChris Kirby 	}
485a7f53a56SChris Kirby 	return (error);
486a7f53a56SChris Kirby }
487a7f53a56SChris Kirby 
4883b2aab18SMatthew Ahrens static int
4893b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
4903b2aab18SMatthew Ahrens {
4913b2aab18SMatthew Ahrens 	int error;
4923b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4933b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
4943b2aab18SMatthew Ahrens 
4953b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4963b2aab18SMatthew Ahrens 	if (error != 0)
4973b2aab18SMatthew Ahrens 		return (error);
4983b2aab18SMatthew Ahrens 
4993b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
5003b2aab18SMatthew Ahrens 	if (error != 0) {
5013b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5023b2aab18SMatthew Ahrens 		return (error);
5033b2aab18SMatthew Ahrens 	}
5043b2aab18SMatthew Ahrens 
5053b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5063b2aab18SMatthew Ahrens 
5073b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5083b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
5093b2aab18SMatthew Ahrens 	return (error);
5103b2aab18SMatthew Ahrens }
5113b2aab18SMatthew Ahrens 
5124201a95eSRic Aleshire /*
5134201a95eSRic Aleshire  * Policy for setting the security label property.
5144201a95eSRic Aleshire  *
5154201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5164201a95eSRic Aleshire  */
5174201a95eSRic Aleshire static int
51892241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5194201a95eSRic Aleshire {
5204201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5214201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5224201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5234201a95eSRic Aleshire 	uint64_t	zoned;
5244201a95eSRic Aleshire 	int		needed_priv = -1;
5254201a95eSRic Aleshire 	int		error;
5264201a95eSRic Aleshire 
5274201a95eSRic Aleshire 	/* First get the existing dataset label. */
5284201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5294201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5303b2aab18SMatthew Ahrens 	if (error != 0)
531be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5324201a95eSRic Aleshire 
5334201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5344201a95eSRic Aleshire 		new_default = TRUE;
5354201a95eSRic Aleshire 
5364201a95eSRic Aleshire 	/* The label must be translatable */
5374201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
538be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5394201a95eSRic Aleshire 
5404201a95eSRic Aleshire 	/*
5414201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5424201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5434201a95eSRic Aleshire 	 * are needed.
5444201a95eSRic Aleshire 	 */
5454201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5464201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
547be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5484201a95eSRic Aleshire 		return (0);
5494201a95eSRic Aleshire 	}
5504201a95eSRic Aleshire 
5514201a95eSRic Aleshire 	/*
5524201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5534201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5544201a95eSRic Aleshire 	 * global zone.
5554201a95eSRic Aleshire 	 */
5564201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5574201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
558be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5594201a95eSRic Aleshire 	if (!zoned) {
5604201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
561be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5624201a95eSRic Aleshire 	}
5634201a95eSRic Aleshire 
5644201a95eSRic Aleshire 	/*
5654201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5664201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5674201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5684201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5694201a95eSRic Aleshire 	 */
5704201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
57192241e0bSTom Erickson 		objset_t *os;
57292241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
57392241e0bSTom Erickson 
5744201a95eSRic Aleshire 		/*
5754201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5764201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5774201a95eSRic Aleshire 		 */
5784201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57992241e0bSTom Erickson 		    setsl_tag, &os);
5803b2aab18SMatthew Ahrens 		if (error != 0)
581be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5824201a95eSRic Aleshire 
58392241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
58492241e0bSTom Erickson 
5854201a95eSRic Aleshire 		if (new_default) {
5864201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5874201a95eSRic Aleshire 			goto out_check;
5884201a95eSRic Aleshire 		}
5894201a95eSRic Aleshire 
5904201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
591be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5924201a95eSRic Aleshire 
5934201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5944201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5954201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5964201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5974201a95eSRic Aleshire 	} else {
5984201a95eSRic Aleshire 		/* dataset currently has a default label */
5994201a95eSRic Aleshire 		if (!new_default)
6004201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6014201a95eSRic Aleshire 	}
6024201a95eSRic Aleshire 
6034201a95eSRic Aleshire out_check:
6044201a95eSRic Aleshire 	if (needed_priv != -1)
6054201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6064201a95eSRic Aleshire 	return (0);
6074201a95eSRic Aleshire }
6084201a95eSRic Aleshire 
609fa9e4066Sahrens static int
61092241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
61192241e0bSTom Erickson     cred_t *cr)
612fa9e4066Sahrens {
61392241e0bSTom Erickson 	char *strval;
61492241e0bSTom Erickson 
615ecd6cf80Smarks 	/*
616ecd6cf80Smarks 	 * Check permissions for special properties.
617ecd6cf80Smarks 	 */
618ecd6cf80Smarks 	switch (prop) {
619ecd6cf80Smarks 	case ZFS_PROP_ZONED:
620ecd6cf80Smarks 		/*
621ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
622ecd6cf80Smarks 		 */
623ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
624be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
625ecd6cf80Smarks 		break;
626ecd6cf80Smarks 
627ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
628a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
629a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
630ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
631ecd6cf80Smarks 			uint64_t zoned;
63240a5c998SMatthew Ahrens 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
633ecd6cf80Smarks 			/*
634ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
635a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
636ecd6cf80Smarks 			 * the thing they own.
637ecd6cf80Smarks 			 */
63892241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
639ecd6cf80Smarks 			    setpoint))
640be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
64192241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
642be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
643ecd6cf80Smarks 		}
644db870a07Sahrens 		break;
6454201a95eSRic Aleshire 
6464201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6474201a95eSRic Aleshire 		if (!is_system_labeled())
648be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64992241e0bSTom Erickson 
65092241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
65192241e0bSTom Erickson 			int err;
65292241e0bSTom Erickson 
65392241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
65492241e0bSTom Erickson 			if (err != 0)
65592241e0bSTom Erickson 				return (err);
65692241e0bSTom Erickson 		}
6574201a95eSRic Aleshire 		break;
658ecd6cf80Smarks 	}
659ecd6cf80Smarks 
66092241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
661ecd6cf80Smarks }
662ecd6cf80Smarks 
6634445fffbSMatthew Ahrens /* ARGSUSED */
6644445fffbSMatthew Ahrens static int
6654445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
666ecd6cf80Smarks {
667ecd6cf80Smarks 	int error;
668ecd6cf80Smarks 
669ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6703b2aab18SMatthew Ahrens 	if (error != 0)
671ecd6cf80Smarks 		return (error);
672ecd6cf80Smarks 
673ecd6cf80Smarks 	/*
674ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
675ecd6cf80Smarks 	 * dsl_deleg_can_allow()
676ecd6cf80Smarks 	 */
677ecd6cf80Smarks 	return (0);
678ecd6cf80Smarks }
679ecd6cf80Smarks 
6804445fffbSMatthew Ahrens /* ARGSUSED */
6814445fffbSMatthew Ahrens static int
6824445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
683ecd6cf80Smarks {
684681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
685681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
686ecd6cf80Smarks }
687ecd6cf80Smarks 
6884445fffbSMatthew Ahrens /* ARGSUSED */
6894445fffbSMatthew Ahrens static int
6904445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
691ecd6cf80Smarks {
692a7f53a56SChris Kirby 	dsl_pool_t *dp;
693a7f53a56SChris Kirby 	dsl_dataset_t *ds;
694a7f53a56SChris Kirby 	char *cp;
695a7f53a56SChris Kirby 	int error;
696a7f53a56SChris Kirby 
697a7f53a56SChris Kirby 	/*
698a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
699a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
700a7f53a56SChris Kirby 	 */
701a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
702a7f53a56SChris Kirby 	if (cp == NULL)
703be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7043b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7053b2aab18SMatthew Ahrens 	if (error != 0)
706a7f53a56SChris Kirby 		return (error);
707a7f53a56SChris Kirby 
708a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7093b2aab18SMatthew Ahrens 	if (error != 0) {
7103b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
711a7f53a56SChris Kirby 		return (error);
7123b2aab18SMatthew Ahrens 	}
713a7f53a56SChris Kirby 
714a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
715a7f53a56SChris Kirby 
716a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
717a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
718a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7193b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
720a7f53a56SChris Kirby 
721a7f53a56SChris Kirby 	return (error);
722ecd6cf80Smarks }
723ecd6cf80Smarks 
7244445fffbSMatthew Ahrens /* ARGSUSED */
725743a77edSAlan Wright static int
7264445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7274445fffbSMatthew Ahrens {
7284445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7294445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7304445fffbSMatthew Ahrens }
7314445fffbSMatthew Ahrens 
7324445fffbSMatthew Ahrens /* ARGSUSED */
7334445fffbSMatthew Ahrens static int
7344445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
735ecd6cf80Smarks {
736ecd6cf80Smarks 	vnode_t *vp;
737ecd6cf80Smarks 	int error;
738ecd6cf80Smarks 
739ecd6cf80Smarks 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
740ecd6cf80Smarks 	    NO_FOLLOW, NULL, &vp)) != 0)
741ecd6cf80Smarks 		return (error);
742ecd6cf80Smarks 
743ecd6cf80Smarks 	/* Now make sure mntpnt and dataset are ZFS */
744ecd6cf80Smarks 
745ecd6cf80Smarks 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
746ecd6cf80Smarks 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
747ecd6cf80Smarks 	    zc->zc_name) != 0)) {
748ecd6cf80Smarks 		VN_RELE(vp);
749be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
750ecd6cf80Smarks 	}
751ecd6cf80Smarks 
752ecd6cf80Smarks 	VN_RELE(vp);
753ecd6cf80Smarks 	return (dsl_deleg_access(zc->zc_name,
754ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHARE, cr));
755ecd6cf80Smarks }
756743a77edSAlan Wright 
757743a77edSAlan Wright int
7584445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
759743a77edSAlan Wright {
760743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
761be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
762743a77edSAlan Wright 
763743a77edSAlan Wright 	if (secpolicy_nfs(cr) == 0) {
764743a77edSAlan Wright 		return (0);
765743a77edSAlan Wright 	} else {
7664445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
767743a77edSAlan Wright 	}
768743a77edSAlan Wright }
769743a77edSAlan Wright 
770743a77edSAlan Wright int
7714445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
772743a77edSAlan Wright {
773743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
774be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
775743a77edSAlan Wright 
776743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
777743a77edSAlan Wright 		return (0);
778743a77edSAlan Wright 	} else {
7794445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
780743a77edSAlan Wright 	}
781ecd6cf80Smarks }
782ecd6cf80Smarks 
783ecd6cf80Smarks static int
784ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
785ecd6cf80Smarks {
786fa9e4066Sahrens 	char *cp;
787fa9e4066Sahrens 
788fa9e4066Sahrens 	/*
789fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
790fa9e4066Sahrens 	 */
791ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
792ecd6cf80Smarks 	cp = strrchr(parent, '@');
793fa9e4066Sahrens 	if (cp != NULL) {
794fa9e4066Sahrens 		cp[0] = '\0';
795fa9e4066Sahrens 	} else {
796ecd6cf80Smarks 		cp = strrchr(parent, '/');
797fa9e4066Sahrens 		if (cp == NULL)
798be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
799fa9e4066Sahrens 		cp[0] = '\0';
800fa9e4066Sahrens 	}
801fa9e4066Sahrens 
802ecd6cf80Smarks 	return (0);
803ecd6cf80Smarks }
804ecd6cf80Smarks 
805ecd6cf80Smarks int
806ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
807ecd6cf80Smarks {
808ecd6cf80Smarks 	int error;
809ecd6cf80Smarks 
810ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
811ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
812ecd6cf80Smarks 		return (error);
813ecd6cf80Smarks 
814ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
815ecd6cf80Smarks }
816ecd6cf80Smarks 
8174445fffbSMatthew Ahrens /* ARGSUSED */
818ecd6cf80Smarks static int
8194445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
820ecd6cf80Smarks {
821ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
822ecd6cf80Smarks }
823ecd6cf80Smarks 
824ecd6cf80Smarks /*
825cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8264445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
827cbf6f6aaSWilliam Gorrell  */
8284445fffbSMatthew Ahrens /* ARGSUSED */
829cbf6f6aaSWilliam Gorrell static int
8304445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
831cbf6f6aaSWilliam Gorrell {
8324445fffbSMatthew Ahrens 	nvlist_t *snaps;
8334445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8344445fffbSMatthew Ahrens 	int error = 0;
835cbf6f6aaSWilliam Gorrell 
8364445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
837be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8384445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8394445fffbSMatthew Ahrens 	    pair = nextpair) {
8404445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
84178f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
84278f17100SMatthew Ahrens 		if (error == ENOENT) {
8434445fffbSMatthew Ahrens 			/*
8444445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8454445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8464445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8474445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8484445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8494445fffbSMatthew Ahrens 			 * checked for permission).
8504445fffbSMatthew Ahrens 			 */
8514445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8524445fffbSMatthew Ahrens 			error = 0;
8534445fffbSMatthew Ahrens 		}
8544445fffbSMatthew Ahrens 		if (error != 0)
8554445fffbSMatthew Ahrens 			break;
8564445fffbSMatthew Ahrens 	}
857cbf6f6aaSWilliam Gorrell 
858cbf6f6aaSWilliam Gorrell 	return (error);
859cbf6f6aaSWilliam Gorrell }
860cbf6f6aaSWilliam Gorrell 
861ecd6cf80Smarks int
862ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
863ecd6cf80Smarks {
86440a5c998SMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
865ecd6cf80Smarks 	int	error;
866ecd6cf80Smarks 
867ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
868ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
869ecd6cf80Smarks 		return (error);
870ecd6cf80Smarks 
871ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
872ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
873ecd6cf80Smarks 		return (error);
874ecd6cf80Smarks 
875ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
876ecd6cf80Smarks 	    sizeof (parentname))) != 0)
877ecd6cf80Smarks 		return (error);
878ecd6cf80Smarks 
879ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
880ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
881ecd6cf80Smarks 		return (error);
882ecd6cf80Smarks 
883ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
884ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
885ecd6cf80Smarks 		return (error);
886ecd6cf80Smarks 
887ecd6cf80Smarks 	return (error);
888ecd6cf80Smarks }
889ecd6cf80Smarks 
8904445fffbSMatthew Ahrens /* ARGSUSED */
891ecd6cf80Smarks static int
8924445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
893ecd6cf80Smarks {
894ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
895ecd6cf80Smarks }
896ecd6cf80Smarks 
8974445fffbSMatthew Ahrens /* ARGSUSED */
898ecd6cf80Smarks static int
8994445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
900ecd6cf80Smarks {
9013b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9023b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
903ecd6cf80Smarks 	int error;
904ecd6cf80Smarks 
905ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
906ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9073b2aab18SMatthew Ahrens 	if (error != 0)
908ecd6cf80Smarks 		return (error);
909ecd6cf80Smarks 
9103b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9113b2aab18SMatthew Ahrens 	if (error != 0)
9123b2aab18SMatthew Ahrens 		return (error);
9133b2aab18SMatthew Ahrens 
9143b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
915ecd6cf80Smarks 
916ecd6cf80Smarks 	if (error == 0) {
91740a5c998SMatthew Ahrens 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
9183b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
919ecd6cf80Smarks 		dsl_dir_t *dd;
9203b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
921ecd6cf80Smarks 
922745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
923c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9243b2aab18SMatthew Ahrens 		if (error != 0) {
9253b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9263b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
927ecd6cf80Smarks 			return (error);
928ecd6cf80Smarks 		}
929ecd6cf80Smarks 
9303b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
931ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
932ecd6cf80Smarks 
9333b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9343b2aab18SMatthew Ahrens 		if (error == 0) {
9353b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
936ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
937ecd6cf80Smarks 		}
9383b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9393b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
9403b2aab18SMatthew Ahrens 	}
9413b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
942ecd6cf80Smarks 	return (error);
943ecd6cf80Smarks }
944ecd6cf80Smarks 
9454445fffbSMatthew Ahrens /* ARGSUSED */
946ecd6cf80Smarks static int
9474445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
948ecd6cf80Smarks {
949ecd6cf80Smarks 	int error;
950ecd6cf80Smarks 
951ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
952ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
953ecd6cf80Smarks 		return (error);
954ecd6cf80Smarks 
955ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
956ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
957ecd6cf80Smarks 		return (error);
958ecd6cf80Smarks 
959ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
960ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
961ecd6cf80Smarks }
962ecd6cf80Smarks 
963ecd6cf80Smarks int
964ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
965ecd6cf80Smarks {
966681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
967681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
968ecd6cf80Smarks }
969ecd6cf80Smarks 
9704445fffbSMatthew Ahrens /*
9714445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9724445fffbSMatthew Ahrens  */
9734445fffbSMatthew Ahrens /* ARGSUSED */
974ecd6cf80Smarks static int
9754445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
976ecd6cf80Smarks {
9774445fffbSMatthew Ahrens 	nvlist_t *snaps;
978d5285caeSGeorge Wilson 	int error = 0;
9794445fffbSMatthew Ahrens 	nvpair_t *pair;
980ecd6cf80Smarks 
9814445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
982be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9834445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9844445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9854445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9864445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9874445fffbSMatthew Ahrens 
9884445fffbSMatthew Ahrens 		if (atp == NULL) {
989be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9904445fffbSMatthew Ahrens 			break;
9914445fffbSMatthew Ahrens 		}
9924445fffbSMatthew Ahrens 		*atp = '\0';
9934445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
9944445fffbSMatthew Ahrens 		*atp = '@';
9954445fffbSMatthew Ahrens 		if (error != 0)
9964445fffbSMatthew Ahrens 			break;
9974445fffbSMatthew Ahrens 	}
9984445fffbSMatthew Ahrens 	return (error);
9994445fffbSMatthew Ahrens }
10004445fffbSMatthew Ahrens 
100178f17100SMatthew Ahrens /*
100278f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
100378f17100SMatthew Ahrens  */
100478f17100SMatthew Ahrens /* ARGSUSED */
100578f17100SMatthew Ahrens static int
100678f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
100778f17100SMatthew Ahrens {
100878f17100SMatthew Ahrens 	int error = 0;
100978f17100SMatthew Ahrens 
101078f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
101178f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
101278f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
101378f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
101478f17100SMatthew Ahrens 
101578f17100SMatthew Ahrens 		if (hashp == NULL) {
101678f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
101778f17100SMatthew Ahrens 			break;
101878f17100SMatthew Ahrens 		}
101978f17100SMatthew Ahrens 		*hashp = '\0';
102078f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
102178f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
102278f17100SMatthew Ahrens 		*hashp = '#';
102378f17100SMatthew Ahrens 		if (error != 0)
102478f17100SMatthew Ahrens 			break;
102578f17100SMatthew Ahrens 	}
102678f17100SMatthew Ahrens 	return (error);
102778f17100SMatthew Ahrens }
102878f17100SMatthew Ahrens 
102978f17100SMatthew Ahrens /* ARGSUSED */
103078f17100SMatthew Ahrens static int
103178f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
103278f17100SMatthew Ahrens {
103378f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
103478f17100SMatthew Ahrens 	int error = 0;
103578f17100SMatthew Ahrens 
103678f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
103778f17100SMatthew Ahrens 	    pair = nextpair) {
103878f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
103978f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
104078f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
104178f17100SMatthew Ahrens 
104278f17100SMatthew Ahrens 		if (hashp == NULL) {
104378f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
104478f17100SMatthew Ahrens 			break;
104578f17100SMatthew Ahrens 		}
104678f17100SMatthew Ahrens 
104778f17100SMatthew Ahrens 		*hashp = '\0';
104878f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
104978f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
105078f17100SMatthew Ahrens 		*hashp = '#';
105178f17100SMatthew Ahrens 		if (error == ENOENT) {
105278f17100SMatthew Ahrens 			/*
105378f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
105478f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
105578f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
105678f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
105778f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
105878f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
105978f17100SMatthew Ahrens 			 */
106078f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
106178f17100SMatthew Ahrens 			error = 0;
106278f17100SMatthew Ahrens 		}
106378f17100SMatthew Ahrens 		if (error != 0)
106478f17100SMatthew Ahrens 			break;
106578f17100SMatthew Ahrens 	}
106678f17100SMatthew Ahrens 
106778f17100SMatthew Ahrens 	return (error);
106878f17100SMatthew Ahrens }
106978f17100SMatthew Ahrens 
10704445fffbSMatthew Ahrens /* ARGSUSED */
10714445fffbSMatthew Ahrens static int
10724445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10734445fffbSMatthew Ahrens {
10744445fffbSMatthew Ahrens 	/*
10754445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10764445fffbSMatthew Ahrens 	 * to log to.
10774445fffbSMatthew Ahrens 	 */
10784445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1079be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10804445fffbSMatthew Ahrens 	return (0);
1081ecd6cf80Smarks }
1082ecd6cf80Smarks 
1083ecd6cf80Smarks static int
10844445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1085ecd6cf80Smarks {
108640a5c998SMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
1087ecd6cf80Smarks 	int	error;
10884445fffbSMatthew Ahrens 	char	*origin;
1089ecd6cf80Smarks 
1090ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1091ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1092ecd6cf80Smarks 		return (error);
1093ecd6cf80Smarks 
10944445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10954445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
1096ecd6cf80Smarks 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
1097ecd6cf80Smarks 		return (error);
1098ecd6cf80Smarks 
1099ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1100ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1101ecd6cf80Smarks 		return (error);
1102ecd6cf80Smarks 
11034445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11044445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1105fa9e4066Sahrens }
1106fa9e4066Sahrens 
1107fa9e4066Sahrens /*
1108fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1109fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1110fa9e4066Sahrens  */
1111fa9e4066Sahrens /* ARGSUSED */
1112fa9e4066Sahrens static int
11134445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1114fa9e4066Sahrens {
1115fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1116be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1117fa9e4066Sahrens 
1118fa9e4066Sahrens 	return (0);
1119fa9e4066Sahrens }
1120fa9e4066Sahrens 
1121fa9e4066Sahrens /*
112299d5e173STim Haley  * Policy for object to name lookups.
112399d5e173STim Haley  */
112499d5e173STim Haley /* ARGSUSED */
112599d5e173STim Haley static int
11264445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
112799d5e173STim Haley {
112899d5e173STim Haley 	int error;
112999d5e173STim Haley 
113099d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
113199d5e173STim Haley 		return (0);
113299d5e173STim Haley 
113399d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
113499d5e173STim Haley 	return (error);
113599d5e173STim Haley }
113699d5e173STim Haley 
113799d5e173STim Haley /*
1138ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1139ea8dc4b6Seschrock  */
1140ea8dc4b6Seschrock /* ARGSUSED */
1141ea8dc4b6Seschrock static int
11424445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1143ea8dc4b6Seschrock {
1144ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1145ea8dc4b6Seschrock }
1146ea8dc4b6Seschrock 
11474445fffbSMatthew Ahrens /* ARGSUSED */
1148e45ce728Sahrens static int
11494445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1150e45ce728Sahrens {
1151e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1152e45ce728Sahrens 
1153990b4856Slling 	if (prop == ZPROP_INVAL) {
1154e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1155be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1156e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1157e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1158e45ce728Sahrens 	} else {
115992241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
116092241e0bSTom Erickson 		    NULL, cr));
1161e45ce728Sahrens 	}
1162e45ce728Sahrens }
1163e45ce728Sahrens 
116414843421SMatthew Ahrens static int
11654445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
116614843421SMatthew Ahrens {
11674445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
116814843421SMatthew Ahrens 	if (err)
116914843421SMatthew Ahrens 		return (err);
117014843421SMatthew Ahrens 
117114843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1172be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
117314843421SMatthew Ahrens 
117414843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
117514843421SMatthew Ahrens 		/*
117614843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
117714843421SMatthew Ahrens 		 * themself, allow it.
117814843421SMatthew Ahrens 		 */
117914843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
118014843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
118114843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
118214843421SMatthew Ahrens 				return (0);
118314843421SMatthew Ahrens 		} else {
118414843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
118514843421SMatthew Ahrens 				return (0);
118614843421SMatthew Ahrens 		}
118714843421SMatthew Ahrens 	}
118814843421SMatthew Ahrens 
118914843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
119014843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
119114843421SMatthew Ahrens }
119214843421SMatthew Ahrens 
119314843421SMatthew Ahrens static int
11944445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
119514843421SMatthew Ahrens {
11964445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
119714843421SMatthew Ahrens 	if (err)
119814843421SMatthew Ahrens 		return (err);
119914843421SMatthew Ahrens 
120014843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1201be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
120214843421SMatthew Ahrens 
120314843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
120414843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
120514843421SMatthew Ahrens }
120614843421SMatthew Ahrens 
12074445fffbSMatthew Ahrens /* ARGSUSED */
120814843421SMatthew Ahrens static int
12094445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
121014843421SMatthew Ahrens {
121192241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
121292241e0bSTom Erickson 	    NULL, cr));
121314843421SMatthew Ahrens }
121414843421SMatthew Ahrens 
12154445fffbSMatthew Ahrens /* ARGSUSED */
1216842727c2SChris Kirby static int
12174445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1218842727c2SChris Kirby {
12193b2aab18SMatthew Ahrens 	nvpair_t *pair;
12203b2aab18SMatthew Ahrens 	nvlist_t *holds;
12213b2aab18SMatthew Ahrens 	int error;
12223b2aab18SMatthew Ahrens 
12233b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12243b2aab18SMatthew Ahrens 	if (error != 0)
1225be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12263b2aab18SMatthew Ahrens 
12273b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12283b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
122940a5c998SMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12303b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12313b2aab18SMatthew Ahrens 		if (error != 0)
12323b2aab18SMatthew Ahrens 			return (error);
12333b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12343b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12353b2aab18SMatthew Ahrens 		if (error != 0)
12363b2aab18SMatthew Ahrens 			return (error);
12373b2aab18SMatthew Ahrens 	}
12383b2aab18SMatthew Ahrens 	return (0);
1239842727c2SChris Kirby }
1240842727c2SChris Kirby 
12414445fffbSMatthew Ahrens /* ARGSUSED */
1242842727c2SChris Kirby static int
12434445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1244842727c2SChris Kirby {
12453b2aab18SMatthew Ahrens 	nvpair_t *pair;
12463b2aab18SMatthew Ahrens 	int error;
12473b2aab18SMatthew Ahrens 
12483b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12493b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
125040a5c998SMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12513b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12523b2aab18SMatthew Ahrens 		if (error != 0)
12533b2aab18SMatthew Ahrens 			return (error);
12543b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12553b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12563b2aab18SMatthew Ahrens 		if (error != 0)
12573b2aab18SMatthew Ahrens 			return (error);
12583b2aab18SMatthew Ahrens 	}
12593b2aab18SMatthew Ahrens 	return (0);
1260842727c2SChris Kirby }
1261842727c2SChris Kirby 
1262ea8dc4b6Seschrock /*
126399d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
126499d5e173STim Haley  */
126599d5e173STim Haley static int
12664445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
126799d5e173STim Haley {
126899d5e173STim Haley 	/*
126999d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
127099d5e173STim Haley 	 * hold, destroy and release all rolled into one.
127199d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
127299d5e173STim Haley 	 */
127399d5e173STim Haley 	int error;
127499d5e173STim Haley 
127599d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
127699d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
127799d5e173STim Haley 		return (0);
127899d5e173STim Haley 
12794445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12803b2aab18SMatthew Ahrens 	if (error == 0)
12814445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12823b2aab18SMatthew Ahrens 	if (error == 0)
12834445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12843b2aab18SMatthew Ahrens 	if (error == 0)
12854445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
128699d5e173STim Haley 	return (error);
128799d5e173STim Haley }
128899d5e173STim Haley 
128999d5e173STim Haley /*
1290d78b796cSAndreas Jaekel  * Policy for allowing setting the zev callback list.
1291d78b796cSAndreas Jaekel  */
1292d78b796cSAndreas Jaekel static int
1293d78b796cSAndreas Jaekel zfs_secpolicy_set_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1294d78b796cSAndreas Jaekel {
1295d78b796cSAndreas Jaekel 	/* must be called from kernel context */
1296d78b796cSAndreas Jaekel 	if (!(zc->zc_iflags & FKIOCTL))
1297d78b796cSAndreas Jaekel 		return (SET_ERROR(EPERM));
1298d78b796cSAndreas Jaekel 	/* callback pointer must be unset (set to default value) */
1299d78b796cSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_READER);
1300d78b796cSAndreas Jaekel 	if (rz_zev_callbacks != rz_zev_default_callbacks) {
1301d78b796cSAndreas Jaekel 		rw_exit(&rz_zev_rwlock);
1302d78b796cSAndreas Jaekel 		return (SET_ERROR(EBUSY));
1303d78b796cSAndreas Jaekel 	}
1304d78b796cSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
1305d78b796cSAndreas Jaekel 	return (0);
1306d78b796cSAndreas Jaekel }
1307d78b796cSAndreas Jaekel 
1308d78b796cSAndreas Jaekel /*
1309d78b796cSAndreas Jaekel  * Policy for allowing unsetting/resetting the zev callback list.
1310d78b796cSAndreas Jaekel  */
1311d78b796cSAndreas Jaekel static int
1312d78b796cSAndreas Jaekel zfs_secpolicy_unset_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1313d78b796cSAndreas Jaekel {
1314d78b796cSAndreas Jaekel 	/* must be called from kernel context */
1315d78b796cSAndreas Jaekel 	if (!(zc->zc_iflags & FKIOCTL))
1316d78b796cSAndreas Jaekel 		return (SET_ERROR(EPERM));
1317d78b796cSAndreas Jaekel 	return (0);
1318d78b796cSAndreas Jaekel }
1319d78b796cSAndreas Jaekel 
1320d78b796cSAndreas Jaekel /*
1321fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1322fa9e4066Sahrens  */
1323fa9e4066Sahrens static int
1324478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1325fa9e4066Sahrens {
1326fa9e4066Sahrens 	char *packed;
1327fa9e4066Sahrens 	int error;
1328990b4856Slling 	nvlist_t *list = NULL;
1329fa9e4066Sahrens 
1330fa9e4066Sahrens 	/*
1331e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1332fa9e4066Sahrens 	 */
1333990b4856Slling 	if (size == 0)
1334be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1335fa9e4066Sahrens 
1336fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1337fa9e4066Sahrens 
1338478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1339478ed9adSEric Taylor 	    iflag)) != 0) {
1340fa9e4066Sahrens 		kmem_free(packed, size);
1341c71c00bbSRichard Yao 		return (SET_ERROR(EFAULT));
1342fa9e4066Sahrens 	}
1343fa9e4066Sahrens 
1344990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1345fa9e4066Sahrens 		kmem_free(packed, size);
1346fa9e4066Sahrens 		return (error);
1347fa9e4066Sahrens 	}
1348fa9e4066Sahrens 
1349fa9e4066Sahrens 	kmem_free(packed, size);
1350fa9e4066Sahrens 
1351990b4856Slling 	*nvp = list;
1352fa9e4066Sahrens 	return (0);
1353fa9e4066Sahrens }
1354fa9e4066Sahrens 
13554445fffbSMatthew Ahrens /*
13564445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13574445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13584445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13594445fffbSMatthew Ahrens  * removed.
13604445fffbSMatthew Ahrens  */
1361fa9e4066Sahrens static int
13624445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
136392241e0bSTom Erickson {
136492241e0bSTom Erickson 	size_t size;
136592241e0bSTom Erickson 
13664445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
136792241e0bSTom Erickson 
13684445fffbSMatthew Ahrens 	if (size > max) {
136992241e0bSTom Erickson 		nvpair_t *more_errors;
137092241e0bSTom Erickson 		int n = 0;
137192241e0bSTom Erickson 
13724445fffbSMatthew Ahrens 		if (max < 1024)
1373be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
137492241e0bSTom Erickson 
13754445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13764445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
137792241e0bSTom Erickson 
137892241e0bSTom Erickson 		do {
13794445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
138092241e0bSTom Erickson 			    more_errors);
13814445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
138292241e0bSTom Erickson 			n++;
13834445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13844445fffbSMatthew Ahrens 		} while (size > max);
138592241e0bSTom Erickson 
13864445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13874445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13884445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
138992241e0bSTom Erickson 	}
139092241e0bSTom Erickson 
139192241e0bSTom Erickson 	return (0);
139292241e0bSTom Erickson }
139392241e0bSTom Erickson 
139492241e0bSTom Erickson static int
1395e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1396e9dbad6fSeschrock {
1397e9dbad6fSeschrock 	char *packed = NULL;
13986e27f868SSam Falkner 	int error = 0;
1399e9dbad6fSeschrock 	size_t size;
1400e9dbad6fSeschrock 
14014445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1402e9dbad6fSeschrock 
1403e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1404be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1405e9dbad6fSeschrock 	} else {
14064445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
14076e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
14086e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1409be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
14104445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1411e9dbad6fSeschrock 	}
1412e9dbad6fSeschrock 
1413e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
14144445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1415e9dbad6fSeschrock 	return (error);
1416e9dbad6fSeschrock }
1417e9dbad6fSeschrock 
1418e9dbad6fSeschrock static int
1419af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
142014843421SMatthew Ahrens {
142114843421SMatthew Ahrens 	objset_t *os;
142214843421SMatthew Ahrens 	int error;
142314843421SMatthew Ahrens 
1424503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
14253b2aab18SMatthew Ahrens 	if (error != 0)
142614843421SMatthew Ahrens 		return (error);
1427503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1428503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1429be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1430503ad85cSMatthew Ahrens 	}
143114843421SMatthew Ahrens 
1432503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1433af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1434af4c679fSSean McEnroe 	if (*zfvp) {
1435af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
143614843421SMatthew Ahrens 	} else {
1437be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
143814843421SMatthew Ahrens 	}
1439503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1440503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
144114843421SMatthew Ahrens 	return (error);
144214843421SMatthew Ahrens }
144314843421SMatthew Ahrens 
144414843421SMatthew Ahrens /*
144514843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
144614843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1447ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1448ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
144914843421SMatthew Ahrens  */
145014843421SMatthew Ahrens static int
14511412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
145214843421SMatthew Ahrens {
145314843421SMatthew Ahrens 	int error = 0;
145414843421SMatthew Ahrens 
1455af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1456af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
145714843421SMatthew Ahrens 	if (error == 0) {
1458c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14591412a1a2SMark Shellenbaum 		    RW_READER, tag);
1460af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
146114843421SMatthew Ahrens 			/*
146214843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
146314843421SMatthew Ahrens 			 * thread should be just about to disassociate the
146414843421SMatthew Ahrens 			 * objset from the zfsvfs.
146514843421SMatthew Ahrens 			 */
1466c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1467be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
146814843421SMatthew Ahrens 		}
146914843421SMatthew Ahrens 	}
147014843421SMatthew Ahrens 	return (error);
147114843421SMatthew Ahrens }
147214843421SMatthew Ahrens 
147314843421SMatthew Ahrens static void
147414843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
147514843421SMatthew Ahrens {
1476c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
147714843421SMatthew Ahrens 
147814843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
147914843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
148014843421SMatthew Ahrens 	} else {
1481503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
148214843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
148314843421SMatthew Ahrens 	}
148414843421SMatthew Ahrens }
148514843421SMatthew Ahrens 
148614843421SMatthew Ahrens static int
1487fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1488fa9e4066Sahrens {
1489fa9e4066Sahrens 	int error;
1490990b4856Slling 	nvlist_t *config, *props = NULL;
14910a48a24eStimh 	nvlist_t *rootprops = NULL;
14920a48a24eStimh 	nvlist_t *zplprops = NULL;
1493fa9e4066Sahrens 
1494990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1495478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1496fa9e4066Sahrens 		return (error);
14972a6b87f0Sek110237 
1498990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1499478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1500478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1501990b4856Slling 		nvlist_free(config);
1502990b4856Slling 		return (error);
1503990b4856Slling 	}
1504990b4856Slling 
15050a48a24eStimh 	if (props) {
15060a48a24eStimh 		nvlist_t *nvl = NULL;
15070a48a24eStimh 		uint64_t version = SPA_VERSION;
15080a48a24eStimh 
15090a48a24eStimh 		(void) nvlist_lookup_uint64(props,
15100a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1511ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1512be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
15130a48a24eStimh 			goto pool_props_bad;
15140a48a24eStimh 		}
15150a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
15160a48a24eStimh 		if (nvl) {
15170a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
15180a48a24eStimh 			if (error != 0) {
15190a48a24eStimh 				nvlist_free(config);
15200a48a24eStimh 				nvlist_free(props);
15210a48a24eStimh 				return (error);
15220a48a24eStimh 			}
15230a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15240a48a24eStimh 		}
15250a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15260a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
15270a48a24eStimh 		    zplprops, NULL);
15283b2aab18SMatthew Ahrens 		if (error != 0)
15290a48a24eStimh 			goto pool_props_bad;
15300a48a24eStimh 	}
15310a48a24eStimh 
15324445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
15330a48a24eStimh 
15340a48a24eStimh 	/*
15350a48a24eStimh 	 * Set the remaining root properties
15360a48a24eStimh 	 */
153792241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
153892241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15390a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1540fa9e4066Sahrens 
15410a48a24eStimh pool_props_bad:
15420a48a24eStimh 	nvlist_free(rootprops);
15430a48a24eStimh 	nvlist_free(zplprops);
15442a6b87f0Sek110237 	nvlist_free(config);
1545990b4856Slling 	nvlist_free(props);
1546990b4856Slling 
1547fa9e4066Sahrens 	return (error);
1548fa9e4066Sahrens }
1549fa9e4066Sahrens 
1550fa9e4066Sahrens static int
1551fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1552fa9e4066Sahrens {
1553ecd6cf80Smarks 	int error;
1554ecd6cf80Smarks 	zfs_log_history(zc);
1555ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1556681d9761SEric Taylor 	if (error == 0)
1557681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1558ecd6cf80Smarks 	return (error);
1559fa9e4066Sahrens }
1560fa9e4066Sahrens 
1561fa9e4066Sahrens static int
1562fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1563fa9e4066Sahrens {
1564990b4856Slling 	nvlist_t *config, *props = NULL;
1565fa9e4066Sahrens 	uint64_t guid;
1566468c413aSTim Haley 	int error;
1567fa9e4066Sahrens 
1568990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1569478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1570fa9e4066Sahrens 		return (error);
1571fa9e4066Sahrens 
1572990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1573478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1574478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1575990b4856Slling 		nvlist_free(config);
1576990b4856Slling 		return (error);
1577990b4856Slling 	}
1578990b4856Slling 
1579fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1580ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1581be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1582fa9e4066Sahrens 	else
15834b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1584fa9e4066Sahrens 
15854b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15864b964adaSGeorge Wilson 		int err;
15874b964adaSGeorge Wilson 
15884b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15894b964adaSGeorge Wilson 			error = err;
15904b964adaSGeorge Wilson 	}
1591468c413aSTim Haley 
1592fa9e4066Sahrens 	nvlist_free(config);
1593fa9e4066Sahrens 
1594990b4856Slling 	nvlist_free(props);
1595990b4856Slling 
1596fa9e4066Sahrens 	return (error);
1597fa9e4066Sahrens }
1598fa9e4066Sahrens 
1599fa9e4066Sahrens static int
1600fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1601fa9e4066Sahrens {
1602ecd6cf80Smarks 	int error;
160389a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1604394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
160589a89ebfSlling 
1606ecd6cf80Smarks 	zfs_log_history(zc);
1607394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1608681d9761SEric Taylor 	if (error == 0)
1609681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1610ecd6cf80Smarks 	return (error);
1611fa9e4066Sahrens }
1612fa9e4066Sahrens 
1613fa9e4066Sahrens static int
1614fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1615fa9e4066Sahrens {
1616fa9e4066Sahrens 	nvlist_t *configs;
1617fa9e4066Sahrens 	int error;
1618fa9e4066Sahrens 
1619fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1620be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1621fa9e4066Sahrens 
1622e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1623fa9e4066Sahrens 
1624fa9e4066Sahrens 	nvlist_free(configs);
1625fa9e4066Sahrens 
1626fa9e4066Sahrens 	return (error);
1627fa9e4066Sahrens }
1628fa9e4066Sahrens 
1629ad135b5dSChristopher Siden /*
1630ad135b5dSChristopher Siden  * inputs:
1631ad135b5dSChristopher Siden  * zc_name		name of the pool
1632ad135b5dSChristopher Siden  *
1633ad135b5dSChristopher Siden  * outputs:
1634ad135b5dSChristopher Siden  * zc_cookie		real errno
1635ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1636ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1637ad135b5dSChristopher Siden  */
1638fa9e4066Sahrens static int
1639fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1640fa9e4066Sahrens {
1641fa9e4066Sahrens 	nvlist_t *config;
1642fa9e4066Sahrens 	int error;
1643ea8dc4b6Seschrock 	int ret = 0;
1644fa9e4066Sahrens 
1645e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1646e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1647fa9e4066Sahrens 
1648fa9e4066Sahrens 	if (config != NULL) {
1649e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1650fa9e4066Sahrens 		nvlist_free(config);
1651ea8dc4b6Seschrock 
1652ea8dc4b6Seschrock 		/*
1653ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1654ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1655ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1656ea8dc4b6Seschrock 		 */
1657ea8dc4b6Seschrock 		zc->zc_cookie = error;
1658fa9e4066Sahrens 	} else {
1659ea8dc4b6Seschrock 		ret = error;
1660fa9e4066Sahrens 	}
1661fa9e4066Sahrens 
1662ea8dc4b6Seschrock 	return (ret);
1663fa9e4066Sahrens }
1664fa9e4066Sahrens 
1665fa9e4066Sahrens /*
1666fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1667fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1668fa9e4066Sahrens  */
1669fa9e4066Sahrens static int
1670fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1671fa9e4066Sahrens {
1672fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1673fa9e4066Sahrens 	int error;
1674fa9e4066Sahrens 
1675990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1676478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1677fa9e4066Sahrens 		return (error);
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1680fa9e4066Sahrens 
1681fa9e4066Sahrens 	nvlist_free(tryconfig);
1682fa9e4066Sahrens 
1683fa9e4066Sahrens 	if (config == NULL)
1684be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1685fa9e4066Sahrens 
1686e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1687fa9e4066Sahrens 	nvlist_free(config);
1688fa9e4066Sahrens 
1689fa9e4066Sahrens 	return (error);
1690fa9e4066Sahrens }
1691fa9e4066Sahrens 
16923f9d6ad7SLin Ling /*
16933f9d6ad7SLin Ling  * inputs:
16943f9d6ad7SLin Ling  * zc_name              name of the pool
16953f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16963f9d6ad7SLin Ling  */
1697fa9e4066Sahrens static int
16983f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1699fa9e4066Sahrens {
1700fa9e4066Sahrens 	spa_t *spa;
1701fa9e4066Sahrens 	int error;
1702fa9e4066Sahrens 
170306eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
170406eeb2adSek110237 		return (error);
170506eeb2adSek110237 
17063f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
17073f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
17083f9d6ad7SLin Ling 	else
17093f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
171006eeb2adSek110237 
1711fa9e4066Sahrens 	spa_close(spa, FTAG);
171206eeb2adSek110237 
1713fa9e4066Sahrens 	return (error);
1714fa9e4066Sahrens }
1715fa9e4066Sahrens 
1716fa9e4066Sahrens static int
1717fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1718fa9e4066Sahrens {
1719fa9e4066Sahrens 	spa_t *spa;
1720fa9e4066Sahrens 	int error;
1721fa9e4066Sahrens 
1722fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1723fa9e4066Sahrens 	if (error == 0) {
1724fa9e4066Sahrens 		spa_freeze(spa);
1725fa9e4066Sahrens 		spa_close(spa, FTAG);
1726fa9e4066Sahrens 	}
1727fa9e4066Sahrens 	return (error);
1728fa9e4066Sahrens }
1729fa9e4066Sahrens 
1730fa9e4066Sahrens static int
1731ce0d9371SArne Jansen zfs_ioc_arc_info(zfs_cmd_t *zc)
1732ce0d9371SArne Jansen {
1733ce0d9371SArne Jansen 	int ret;
1734ce0d9371SArne Jansen 	void *buf;
1735ce0d9371SArne Jansen 	size_t sz = zc->zc_nvlist_dst_size;
1736ce0d9371SArne Jansen 	size_t returned_bytes;
1737ce0d9371SArne Jansen 
1738ce0d9371SArne Jansen 	if (zc->zc_nvlist_dst == 0)
1739ce0d9371SArne Jansen 		return (SET_ERROR(EINVAL));
1740ce0d9371SArne Jansen 
1741ce0d9371SArne Jansen 	buf = kmem_alloc(sz, KM_NOSLEEP);
1742ce0d9371SArne Jansen 	if (buf == NULL)
1743ce0d9371SArne Jansen 		return (SET_ERROR(ENOMEM));
1744ce0d9371SArne Jansen 
1745ce0d9371SArne Jansen 	ret = arc_dump(zc->zc_obj, buf, sz, &returned_bytes);
1746ce0d9371SArne Jansen 	if (ret != 0) {
1747ce0d9371SArne Jansen 		kmem_free(buf, sz);
1748ce0d9371SArne Jansen 		return (SET_ERROR(ret));
1749ce0d9371SArne Jansen 	}
1750ce0d9371SArne Jansen 
1751ce0d9371SArne Jansen 	zc->zc_nvlist_dst_filled = 1;
1752ce0d9371SArne Jansen 	ret = ddi_copyout(buf, (void *)(uintptr_t)zc->zc_nvlist_dst,
1753ce0d9371SArne Jansen 	    returned_bytes, zc->zc_iflags);
1754ce0d9371SArne Jansen 	kmem_free(buf, sz);
1755ce0d9371SArne Jansen 	if (ret != 0)
1756ce0d9371SArne Jansen 		ret = SET_ERROR(EFAULT);
1757ce0d9371SArne Jansen 
1758ce0d9371SArne Jansen 	return (ret);
1759ce0d9371SArne Jansen }
1760ce0d9371SArne Jansen 
1761ce0d9371SArne Jansen static int
1762eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1763eaca9bbdSeschrock {
1764eaca9bbdSeschrock 	spa_t *spa;
1765eaca9bbdSeschrock 	int error;
1766eaca9bbdSeschrock 
176706eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
176806eeb2adSek110237 		return (error);
176906eeb2adSek110237 
1770ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1771ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1772558d2d50Slling 		spa_close(spa, FTAG);
1773be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1774558d2d50Slling 	}
1775558d2d50Slling 
1776990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
1777eaca9bbdSeschrock 	spa_close(spa, FTAG);
177806eeb2adSek110237 
177906eeb2adSek110237 	return (error);
1780eaca9bbdSeschrock }
178106eeb2adSek110237 
178206eeb2adSek110237 static int
178306eeb2adSek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
178406eeb2adSek110237 {
178506eeb2adSek110237 	spa_t *spa;
178606eeb2adSek110237 	char *hist_buf;
178706eeb2adSek110237 	uint64_t size;
178806eeb2adSek110237 	int error;
178906eeb2adSek110237 
179006eeb2adSek110237 	if ((size = zc->zc_history_len) == 0)
1791be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
179206eeb2adSek110237 
179306eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
179406eeb2adSek110237 		return (error);
179506eeb2adSek110237 
1796e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1797d7306b64Sek110237 		spa_close(spa, FTAG);
1798be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1799d7306b64Sek110237 	}
1800d7306b64Sek110237 
180106eeb2adSek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
180206eeb2adSek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
180306eeb2adSek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
1804478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1805478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1806478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
180706eeb2adSek110237 	}
180806eeb2adSek110237 
180906eeb2adSek110237 	spa_close(spa, FTAG);
181006eeb2adSek110237 	kmem_free(hist_buf, size);
181106eeb2adSek110237 	return (error);
181206eeb2adSek110237 }
181306eeb2adSek110237 
181406eeb2adSek110237 static int
1815e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1816e9103aaeSGarrett D'Amore {
1817e9103aaeSGarrett D'Amore 	spa_t *spa;
1818e9103aaeSGarrett D'Amore 	int error;
1819e9103aaeSGarrett D'Amore 
1820e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1821e9103aaeSGarrett D'Amore 	if (error == 0) {
1822e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1823e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1824e9103aaeSGarrett D'Amore 	}
1825e9103aaeSGarrett D'Amore 	return (error);
1826e9103aaeSGarrett D'Amore }
1827e9103aaeSGarrett D'Amore 
1828e9103aaeSGarrett D'Amore static int
182955434c77Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
183055434c77Sek110237 {
18313b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
183255434c77Sek110237 }
183355434c77Sek110237 
1834503ad85cSMatthew Ahrens /*
1835503ad85cSMatthew Ahrens  * inputs:
1836503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1837503ad85cSMatthew Ahrens  * zc_obj		object to find
1838503ad85cSMatthew Ahrens  *
1839503ad85cSMatthew Ahrens  * outputs:
1840503ad85cSMatthew Ahrens  * zc_value		name of object
1841503ad85cSMatthew Ahrens  */
184255434c77Sek110237 static int
184355434c77Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
184455434c77Sek110237 {
1845503ad85cSMatthew Ahrens 	objset_t *os;
184655434c77Sek110237 	int error;
184755434c77Sek110237 
1848503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1849503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
185055434c77Sek110237 		return (error);
1851503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1852503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1853be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1854503ad85cSMatthew Ahrens 	}
1855503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
185655434c77Sek110237 	    sizeof (zc->zc_value));
1857503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
185855434c77Sek110237 
185955434c77Sek110237 	return (error);
186055434c77Sek110237 }
186155434c77Sek110237 
186299d5e173STim Haley /*
186399d5e173STim Haley  * inputs:
186499d5e173STim Haley  * zc_name		name of filesystem
186599d5e173STim Haley  * zc_obj		object to find
186699d5e173STim Haley  *
186799d5e173STim Haley  * outputs:
186899d5e173STim Haley  * zc_stat		stats on object
186999d5e173STim Haley  * zc_value		path to object
187099d5e173STim Haley  */
187199d5e173STim Haley static int
187299d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
187399d5e173STim Haley {
187499d5e173STim Haley 	objset_t *os;
187599d5e173STim Haley 	int error;
187699d5e173STim Haley 
187799d5e173STim Haley 	/* XXX reading from objset not owned */
187899d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
187999d5e173STim Haley 		return (error);
188099d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
188199d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1882be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
188399d5e173STim Haley 	}
188499d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
188599d5e173STim Haley 	    sizeof (zc->zc_value));
188699d5e173STim Haley 	dmu_objset_rele(os, FTAG);
188799d5e173STim Haley 
188899d5e173STim Haley 	return (error);
188999d5e173STim Haley }
189099d5e173STim Haley 
189155434c77Sek110237 static int
1892fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1893fa9e4066Sahrens {
1894fa9e4066Sahrens 	spa_t *spa;
1895fa9e4066Sahrens 	int error;
1896e7cbe64fSgw25295 	nvlist_t *config, **l2cache, **spares;
1897e7cbe64fSgw25295 	uint_t nl2cache = 0, nspares = 0;
1898fa9e4066Sahrens 
1899fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1900fa9e4066Sahrens 	if (error != 0)
1901fa9e4066Sahrens 		return (error);
1902fa9e4066Sahrens 
1903fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1904478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1905fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1906fa94a07fSbrendan 	    &l2cache, &nl2cache);
1907fa94a07fSbrendan 
1908e7cbe64fSgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1909e7cbe64fSgw25295 	    &spares, &nspares);
1910e7cbe64fSgw25295 
1911b1b8ab34Slling 	/*
1912b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1913e7cbe64fSgw25295 	 * Thus, can not add a device to a root pool.
1914e7cbe64fSgw25295 	 *
1915e7cbe64fSgw25295 	 * Intent log device can not be added to a rootpool because
1916e7cbe64fSgw25295 	 * during mountroot, zil is replayed, a seperated log device
1917e7cbe64fSgw25295 	 * can not be accessed during the mountroot time.
1918e7cbe64fSgw25295 	 *
1919e7cbe64fSgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
1920b1b8ab34Slling 	 */
1921b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
19221195e687SMark J Musante 		nvlist_free(config);
1923b1b8ab34Slling 		spa_close(spa, FTAG);
1924be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1925b1b8ab34Slling 	}
1926b1b8ab34Slling 
1927fa94a07fSbrendan 	if (error == 0) {
1928fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1929fa9e4066Sahrens 		nvlist_free(config);
1930fa9e4066Sahrens 	}
1931fa9e4066Sahrens 	spa_close(spa, FTAG);
1932fa9e4066Sahrens 	return (error);
1933fa9e4066Sahrens }
1934fa9e4066Sahrens 
19353f9d6ad7SLin Ling /*
19363f9d6ad7SLin Ling  * inputs:
19373f9d6ad7SLin Ling  * zc_name		name of the pool
19383f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
19393f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
19403f9d6ad7SLin Ling  */
1941fa9e4066Sahrens static int
1942fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1943fa9e4066Sahrens {
194499653d4eSeschrock 	spa_t *spa;
194599653d4eSeschrock 	int error;
194699653d4eSeschrock 
194799653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
194899653d4eSeschrock 	if (error != 0)
194999653d4eSeschrock 		return (error);
195099653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
195199653d4eSeschrock 	spa_close(spa, FTAG);
195299653d4eSeschrock 	return (error);
1953fa9e4066Sahrens }
1954fa9e4066Sahrens 
1955fa9e4066Sahrens static int
19563d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1957fa9e4066Sahrens {
1958fa9e4066Sahrens 	spa_t *spa;
1959fa9e4066Sahrens 	int error;
19603d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1961fa9e4066Sahrens 
196206eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1963fa9e4066Sahrens 		return (error);
19643d7072f8Seschrock 	switch (zc->zc_cookie) {
19653d7072f8Seschrock 	case VDEV_STATE_ONLINE:
19663d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19673d7072f8Seschrock 		break;
19683d7072f8Seschrock 
19693d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19703d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19713d7072f8Seschrock 		break;
19723d7072f8Seschrock 
19733d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1974069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1975069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1976069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1977069f55e2SEric Schrock 
1978069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19793d7072f8Seschrock 		break;
19803d7072f8Seschrock 
19813d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1982069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1983069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1984069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1985069f55e2SEric Schrock 
1986069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19873d7072f8Seschrock 		break;
19883d7072f8Seschrock 
19893d7072f8Seschrock 	default:
1990be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1991fa9e4066Sahrens 	}
19923d7072f8Seschrock 	zc->zc_cookie = newstate;
1993fa9e4066Sahrens 	spa_close(spa, FTAG);
1994fa9e4066Sahrens 	return (error);
1995fa9e4066Sahrens }
1996fa9e4066Sahrens 
1997fa9e4066Sahrens static int
1998fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1999fa9e4066Sahrens {
2000fa9e4066Sahrens 	spa_t *spa;
2001fa9e4066Sahrens 	int replacing = zc->zc_cookie;
2002fa9e4066Sahrens 	nvlist_t *config;
2003fa9e4066Sahrens 	int error;
2004fa9e4066Sahrens 
200506eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2006fa9e4066Sahrens 		return (error);
2007fa9e4066Sahrens 
2008990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2009478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
2010ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2011fa9e4066Sahrens 		nvlist_free(config);
2012fa9e4066Sahrens 	}
2013fa9e4066Sahrens 
2014fa9e4066Sahrens 	spa_close(spa, FTAG);
2015fa9e4066Sahrens 	return (error);
2016fa9e4066Sahrens }
2017fa9e4066Sahrens 
2018fa9e4066Sahrens static int
2019fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2020fa9e4066Sahrens {
2021fa9e4066Sahrens 	spa_t *spa;
2022fa9e4066Sahrens 	int error;
2023fa9e4066Sahrens 
202406eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2025fa9e4066Sahrens 		return (error);
2026fa9e4066Sahrens 
20278ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2028fa9e4066Sahrens 
2029fa9e4066Sahrens 	spa_close(spa, FTAG);
2030fa9e4066Sahrens 	return (error);
2031fa9e4066Sahrens }
2032fa9e4066Sahrens 
2033fa9e4066Sahrens static int
20341195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
20351195e687SMark J Musante {
20361195e687SMark J Musante 	spa_t *spa;
20371195e687SMark J Musante 	nvlist_t *config, *props = NULL;
20381195e687SMark J Musante 	int error;
20391195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
20401195e687SMark J Musante 
20411195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
20421195e687SMark J Musante 		return (error);
20431195e687SMark J Musante 
20441195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
20451195e687SMark J Musante 	    zc->zc_iflags, &config)) {
20461195e687SMark J Musante 		spa_close(spa, FTAG);
20471195e687SMark J Musante 		return (error);
20481195e687SMark J Musante 	}
20491195e687SMark J Musante 
20501195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
20511195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20521195e687SMark J Musante 	    zc->zc_iflags, &props))) {
20531195e687SMark J Musante 		spa_close(spa, FTAG);
20541195e687SMark J Musante 		nvlist_free(config);
20551195e687SMark J Musante 		return (error);
20561195e687SMark J Musante 	}
20571195e687SMark J Musante 
20581195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20591195e687SMark J Musante 
20601195e687SMark J Musante 	spa_close(spa, FTAG);
20611195e687SMark J Musante 
20621195e687SMark J Musante 	nvlist_free(config);
20631195e687SMark J Musante 	nvlist_free(props);
20641195e687SMark J Musante 
20651195e687SMark J Musante 	return (error);
20661195e687SMark J Musante }
20671195e687SMark J Musante 
20681195e687SMark J Musante static int
2069c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2070c67d9675Seschrock {
2071c67d9675Seschrock 	spa_t *spa;
2072e9dbad6fSeschrock 	char *path = zc->zc_value;
2073ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2074c67d9675Seschrock 	int error;
2075c67d9675Seschrock 
2076c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2077c67d9675Seschrock 	if (error != 0)
2078c67d9675Seschrock 		return (error);
2079c67d9675Seschrock 
2080c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2081c67d9675Seschrock 	spa_close(spa, FTAG);
2082c67d9675Seschrock 	return (error);
2083c67d9675Seschrock }
2084c67d9675Seschrock 
20856809eb4eSEric Schrock static int
20866809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20876809eb4eSEric Schrock {
20886809eb4eSEric Schrock 	spa_t *spa;
20896809eb4eSEric Schrock 	char *fru = zc->zc_value;
20906809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20916809eb4eSEric Schrock 	int error;
20926809eb4eSEric Schrock 
20936809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20946809eb4eSEric Schrock 	if (error != 0)
20956809eb4eSEric Schrock 		return (error);
20966809eb4eSEric Schrock 
20976809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20986809eb4eSEric Schrock 	spa_close(spa, FTAG);
20996809eb4eSEric Schrock 	return (error);
21006809eb4eSEric Schrock }
21016809eb4eSEric Schrock 
2102c67d9675Seschrock static int
2103a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2104fa9e4066Sahrens {
2105a7f53a56SChris Kirby 	int error = 0;
21067f7322feSeschrock 	nvlist_t *nv;
2107fa9e4066Sahrens 
2108a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2109fa9e4066Sahrens 
21105ad82045Snd150628 	if (zc->zc_nvlist_dst != 0 &&
211192241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2112a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2113432f72fdSahrens 		/*
2114bd00f61bSrm160521 		 * NB: zvol_get_stats() will read the objset contents,
2115432f72fdSahrens 		 * which we aren't supposed to do with a
2116745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2117432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2118503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2119432f72fdSahrens 		 */
212019b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
212119b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
212219b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
212319b94df9SMatthew Ahrens 			if (error == EIO)
212419b94df9SMatthew Ahrens 				return (error);
2125fb09f5aaSMadhav Suresh 			VERIFY0(error);
2126e7437265Sahrens 		}
2127e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
21287f7322feSeschrock 		nvlist_free(nv);
21297f7322feSeschrock 	}
21307f7322feSeschrock 
2131a7f53a56SChris Kirby 	return (error);
2132a7f53a56SChris Kirby }
2133a7f53a56SChris Kirby 
2134a7f53a56SChris Kirby /*
2135a7f53a56SChris Kirby  * inputs:
2136a7f53a56SChris Kirby  * zc_name		name of filesystem
2137a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2138a7f53a56SChris Kirby  *
2139a7f53a56SChris Kirby  * outputs:
2140a7f53a56SChris Kirby  * zc_objset_stats	stats
2141a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2142a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2143a7f53a56SChris Kirby  */
2144a7f53a56SChris Kirby static int
2145a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2146a7f53a56SChris Kirby {
21473b2aab18SMatthew Ahrens 	objset_t *os;
2148a7f53a56SChris Kirby 	int error;
2149a7f53a56SChris Kirby 
21503b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
21513b2aab18SMatthew Ahrens 	if (error == 0) {
2152a7f53a56SChris Kirby 		error = zfs_ioc_objset_stats_impl(zc, os);
2153503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
21543b2aab18SMatthew Ahrens 	}
2155a7f53a56SChris Kirby 
2156fa9e4066Sahrens 	return (error);
2157fa9e4066Sahrens }
2158fa9e4066Sahrens 
215992241e0bSTom Erickson /*
216092241e0bSTom Erickson  * inputs:
216192241e0bSTom Erickson  * zc_name		name of filesystem
216292241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
216392241e0bSTom Erickson  *
216492241e0bSTom Erickson  * outputs:
216592241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
216692241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
216792241e0bSTom Erickson  *
216892241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
216992241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
217092241e0bSTom Erickson  * local property values.
217192241e0bSTom Erickson  */
217292241e0bSTom Erickson static int
217392241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
217492241e0bSTom Erickson {
21753b2aab18SMatthew Ahrens 	int error = 0;
217692241e0bSTom Erickson 	nvlist_t *nv;
217792241e0bSTom Erickson 
217892241e0bSTom Erickson 	/*
217992241e0bSTom Erickson 	 * Without this check, we would return local property values if the
218092241e0bSTom Erickson 	 * caller has not already received properties on or after
218192241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
218292241e0bSTom Erickson 	 */
21833b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2184be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
218592241e0bSTom Erickson 
218692241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21873b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
218892241e0bSTom Erickson 		error = put_nvlist(zc, nv);
218992241e0bSTom Erickson 		nvlist_free(nv);
219092241e0bSTom Erickson 	}
219192241e0bSTom Erickson 
219292241e0bSTom Erickson 	return (error);
219392241e0bSTom Erickson }
219492241e0bSTom Erickson 
2195de8267e0Stimh static int
2196de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2197de8267e0Stimh {
2198de8267e0Stimh 	uint64_t value;
2199de8267e0Stimh 	int error;
2200de8267e0Stimh 
2201de8267e0Stimh 	/*
2202de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2203de8267e0Stimh 	 * the default value (if there is one).
2204de8267e0Stimh 	 */
2205de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2206de8267e0Stimh 		return (error);
2207de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2208de8267e0Stimh 	return (0);
2209de8267e0Stimh }
2210de8267e0Stimh 
2211de8267e0Stimh /*
2212de8267e0Stimh  * inputs:
2213de8267e0Stimh  * zc_name		name of filesystem
2214de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
2215de8267e0Stimh  *
2216de8267e0Stimh  * outputs:
2217de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2218de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
2219de8267e0Stimh  */
2220de8267e0Stimh static int
2221de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2222de8267e0Stimh {
2223de8267e0Stimh 	objset_t *os;
2224de8267e0Stimh 	int err;
2225de8267e0Stimh 
2226503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2227503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2228de8267e0Stimh 		return (err);
2229de8267e0Stimh 
2230de8267e0Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2231de8267e0Stimh 
2232de8267e0Stimh 	/*
2233de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2234745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2235745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2236de8267e0Stimh 	 */
2237de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2238de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2239de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2240de8267e0Stimh 		nvlist_t *nv;
2241de8267e0Stimh 
2242de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2243de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2244de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2245de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2246de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2247de8267e0Stimh 			err = put_nvlist(zc, nv);
2248de8267e0Stimh 		nvlist_free(nv);
2249de8267e0Stimh 	} else {
2250be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2251de8267e0Stimh 	}
2252503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2253de8267e0Stimh 	return (err);
2254de8267e0Stimh }
2255de8267e0Stimh 
225614843421SMatthew Ahrens static boolean_t
225714843421SMatthew Ahrens dataset_name_hidden(const char *name)
225814843421SMatthew Ahrens {
225914843421SMatthew Ahrens 	/*
226014843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
226114843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
226214843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
226314843421SMatthew Ahrens 	 */
226414843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
226514843421SMatthew Ahrens 		return (B_TRUE);
226614843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
226714843421SMatthew Ahrens 		return (B_TRUE);
226814843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
226914843421SMatthew Ahrens 		return (B_TRUE);
227014843421SMatthew Ahrens 	return (B_FALSE);
227114843421SMatthew Ahrens }
227214843421SMatthew Ahrens 
22733cb34c60Sahrens /*
22743cb34c60Sahrens  * inputs:
22753cb34c60Sahrens  * zc_name		name of filesystem
22763cb34c60Sahrens  * zc_cookie		zap cursor
22773cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22783cb34c60Sahrens  *
22793cb34c60Sahrens  * outputs:
22803cb34c60Sahrens  * zc_name		name of next filesystem
228114843421SMatthew Ahrens  * zc_cookie		zap cursor
22823cb34c60Sahrens  * zc_objset_stats	stats
22833cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22843cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22853cb34c60Sahrens  */
2286fa9e4066Sahrens static int
2287fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2288fa9e4066Sahrens {
228987e5029aSahrens 	objset_t *os;
2290fa9e4066Sahrens 	int error;
2291fa9e4066Sahrens 	char *p;
2292620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2293fa9e4066Sahrens 
2294620252bcSChris Kirby top:
2295503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
229687e5029aSahrens 		if (error == ENOENT)
2297be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
229887e5029aSahrens 		return (error);
2299fa9e4066Sahrens 	}
2300fa9e4066Sahrens 
2301fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2302fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2303fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2304fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2305fa9e4066Sahrens 
2306fa9e4066Sahrens 	do {
230787e5029aSahrens 		error = dmu_dir_list_next(os,
230887e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
230987e5029aSahrens 		    NULL, &zc->zc_cookie);
2310fa9e4066Sahrens 		if (error == ENOENT)
2311be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
231219b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2313503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2314fa9e4066Sahrens 
2315681d9761SEric Taylor 	/*
2316681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2317681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2318681d9761SEric Taylor 	 */
2319620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
232087e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2321620252bcSChris Kirby 		if (error == ENOENT) {
2322620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2323620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2324620252bcSChris Kirby 			goto top;
2325620252bcSChris Kirby 		}
2326620252bcSChris Kirby 	}
2327fa9e4066Sahrens 	return (error);
2328fa9e4066Sahrens }
2329fa9e4066Sahrens 
23303cb34c60Sahrens /*
23313cb34c60Sahrens  * inputs:
23323cb34c60Sahrens  * zc_name		name of filesystem
23333cb34c60Sahrens  * zc_cookie		zap cursor
23343cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
23353cb34c60Sahrens  *
23363cb34c60Sahrens  * outputs:
23373cb34c60Sahrens  * zc_name		name of next snapshot
23383cb34c60Sahrens  * zc_objset_stats	stats
23393cb34c60Sahrens  * zc_nvlist_dst	property nvlist
23403cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
23413cb34c60Sahrens  */
2342fa9e4066Sahrens static int
2343fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2344fa9e4066Sahrens {
234587e5029aSahrens 	objset_t *os;
2346fa9e4066Sahrens 	int error;
2347fa9e4066Sahrens 
2348503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
23493b2aab18SMatthew Ahrens 	if (error != 0) {
2350745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
23513b2aab18SMatthew Ahrens 	}
2352fa9e4066Sahrens 
2353b81d61a6Slling 	/*
2354b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2355b81d61a6Slling 	 * so exit immediately.
2356b81d61a6Slling 	 */
235740a5c998SMatthew Ahrens 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
235840a5c998SMatthew Ahrens 	    ZFS_MAX_DATASET_NAME_LEN) {
2359503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2360be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2361fa9e4066Sahrens 	}
2362fa9e4066Sahrens 
236387e5029aSahrens 	error = dmu_snapshot_list_next(os,
236487e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2365a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2366a7f53a56SChris Kirby 	    NULL);
2367a7f53a56SChris Kirby 
2368620252bcSChris Kirby 	if (error == 0) {
2369a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2370a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2371a7f53a56SChris Kirby 
2372a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23733b2aab18SMatthew Ahrens 		if (error == 0) {
2374a7f53a56SChris Kirby 			objset_t *ossnap;
2375a7f53a56SChris Kirby 
2376a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2377a7f53a56SChris Kirby 			if (error == 0)
2378a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2379a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2380a7f53a56SChris Kirby 		}
2381620252bcSChris Kirby 	} else if (error == ENOENT) {
2382be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2383620252bcSChris Kirby 	}
2384fa9e4066Sahrens 
2385a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23863cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23873b2aab18SMatthew Ahrens 	if (error != 0)
23883cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2389fa9e4066Sahrens 	return (error);
2390fa9e4066Sahrens }
2391fa9e4066Sahrens 
239292241e0bSTom Erickson static int
239392241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2394e9dbad6fSeschrock {
239592241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
239614843421SMatthew Ahrens 	uint64_t *valary;
239714843421SMatthew Ahrens 	unsigned int vallen;
239814843421SMatthew Ahrens 	const char *domain;
2399eeb85002STim Haley 	char *dash;
240014843421SMatthew Ahrens 	zfs_userquota_prop_t type;
240114843421SMatthew Ahrens 	uint64_t rid;
240214843421SMatthew Ahrens 	uint64_t quota;
240314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
240492241e0bSTom Erickson 	int err;
240514843421SMatthew Ahrens 
240692241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
240792241e0bSTom Erickson 		nvlist_t *attrs;
240892241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2409eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2410eeb85002STim Haley 		    &pair) != 0)
2411be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
241292241e0bSTom Erickson 	}
241392241e0bSTom Erickson 
2414eeb85002STim Haley 	/*
2415eeb85002STim Haley 	 * A correctly constructed propname is encoded as
2416eeb85002STim Haley 	 * userquota@<rid>-<domain>.
2417eeb85002STim Haley 	 */
2418eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2419eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2420eeb85002STim Haley 	    vallen != 3)
2421be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2422eeb85002STim Haley 
2423eeb85002STim Haley 	domain = dash + 1;
242414843421SMatthew Ahrens 	type = valary[0];
242514843421SMatthew Ahrens 	rid = valary[1];
242614843421SMatthew Ahrens 	quota = valary[2];
242714843421SMatthew Ahrens 
24281412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
242992241e0bSTom Erickson 	if (err == 0) {
243092241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
243114843421SMatthew Ahrens 		zfsvfs_rele(zfsvfs, FTAG);
243214843421SMatthew Ahrens 	}
243392241e0bSTom Erickson 
243492241e0bSTom Erickson 	return (err);
2435ecd6cf80Smarks }
243692241e0bSTom Erickson 
243792241e0bSTom Erickson /*
243892241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
243992241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
244092241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
244192241e0bSTom Erickson  *
2442eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
244392241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
244492241e0bSTom Erickson  */
244592241e0bSTom Erickson static int
244692241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
244792241e0bSTom Erickson     nvpair_t *pair)
244892241e0bSTom Erickson {
244992241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
245092241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
245192241e0bSTom Erickson 	uint64_t intval;
2452b5152584SMatthew Ahrens 	int err = -1;
245392241e0bSTom Erickson 
245492241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
245592241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
245692241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
245792241e0bSTom Erickson 		return (-1);
245814843421SMatthew Ahrens 	}
2459e9dbad6fSeschrock 
246092241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
246192241e0bSTom Erickson 		nvlist_t *attrs;
246292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
246392241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
246492241e0bSTom Erickson 		    &pair) == 0);
246592241e0bSTom Erickson 	}
246692241e0bSTom Erickson 
246792241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
246892241e0bSTom Erickson 		return (-1);
246992241e0bSTom Erickson 
247092241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
247192241e0bSTom Erickson 
2472e9dbad6fSeschrock 	switch (prop) {
2473e9dbad6fSeschrock 	case ZFS_PROP_QUOTA:
247492241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
2475e9dbad6fSeschrock 		break;
2476a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
24773b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
2478a9799022Sck153898 		break;
2479a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2480a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2481a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2482a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2483a2afb611SJerry Jelinek 			err = 0;
2484a2afb611SJerry Jelinek 		} else {
2485a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2486a2afb611SJerry Jelinek 		}
2487a2afb611SJerry Jelinek 		/*
2488a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2489a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2490a2afb611SJerry Jelinek 		 */
2491a2afb611SJerry Jelinek 		if (err == 0)
2492a2afb611SJerry Jelinek 			err = -1;
2493a2afb611SJerry Jelinek 		break;
2494e9dbad6fSeschrock 	case ZFS_PROP_RESERVATION:
249592241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
2496e9dbad6fSeschrock 		break;
2497a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
24983b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
2499a9799022Sck153898 		break;
2500e9dbad6fSeschrock 	case ZFS_PROP_VOLSIZE:
2501c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
2502e9dbad6fSeschrock 		break;
2503e7437265Sahrens 	case ZFS_PROP_VERSION:
250414843421SMatthew Ahrens 	{
250514843421SMatthew Ahrens 		zfsvfs_t *zfsvfs;
250614843421SMatthew Ahrens 
25071412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
250892241e0bSTom Erickson 			break;
250992241e0bSTom Erickson 
251092241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
251114843421SMatthew Ahrens 		zfsvfs_rele(zfsvfs, FTAG);
251214843421SMatthew Ahrens 
251392241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2514b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2515b16da2e2SGeorge Wilson 
2516b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2517b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2518b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2519b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
252014843421SMatthew Ahrens 		}
25214201a95eSRic Aleshire 		break;
25224201a95eSRic Aleshire 	}
2523e9dbad6fSeschrock 	default:
252492241e0bSTom Erickson 		err = -1;
25255c0b6a79SRich Morris 	}
252692241e0bSTom Erickson 
252792241e0bSTom Erickson 	return (err);
252892241e0bSTom Erickson }
252992241e0bSTom Erickson 
253092241e0bSTom Erickson /*
253192241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
25324445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
25334445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
25344445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
25354445fffbSMatthew Ahrens  * corresponding error numbers.
253692241e0bSTom Erickson  *
25374445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
25384445fffbSMatthew Ahrens  * modified.
253992241e0bSTom Erickson  */
254092241e0bSTom Erickson int
254192241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
25424445fffbSMatthew Ahrens     nvlist_t *errlist)
254392241e0bSTom Erickson {
254492241e0bSTom Erickson 	nvpair_t *pair;
254592241e0bSTom Erickson 	nvpair_t *propval;
254602e383d1STom Erickson 	int rv = 0;
254792241e0bSTom Erickson 	uint64_t intval;
254892241e0bSTom Erickson 	char *strval;
25494445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
25504445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
255192241e0bSTom Erickson 
255292241e0bSTom Erickson retry:
255392241e0bSTom Erickson 	pair = NULL;
255492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
255592241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
255692241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2557cfa69fd2STom Erickson 		int err = 0;
255892241e0bSTom Erickson 
255992241e0bSTom Erickson 		/* decode the property value */
256092241e0bSTom Erickson 		propval = pair;
256192241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
256292241e0bSTom Erickson 			nvlist_t *attrs;
25634445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2564eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2565eeb85002STim Haley 			    &propval) != 0)
2566be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
256792241e0bSTom Erickson 		}
256892241e0bSTom Erickson 
256992241e0bSTom Erickson 		/* Validate value type */
2570eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
257192241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
257292241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2573be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
257492241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
257592241e0bSTom Erickson 				if (nvpair_type(propval) !=
257692241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2577be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
257819b94df9SMatthew Ahrens 			} else {
2579be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
258092241e0bSTom Erickson 			}
2581eeb85002STim Haley 		} else if (err == 0) {
258292241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
258392241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2584be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
258592241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2586a2eea2e1Sahrens 				const char *unused;
2587a2eea2e1Sahrens 
25884445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2589e9dbad6fSeschrock 
2590e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
259191ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2592e9dbad6fSeschrock 					break;
259391ebeef5Sahrens 				case PROP_TYPE_STRING:
2594be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
259592241e0bSTom Erickson 					break;
259691ebeef5Sahrens 				case PROP_TYPE_INDEX:
2597acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
259892241e0bSTom Erickson 					    intval, &unused) != 0)
2599be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2600e9dbad6fSeschrock 					break;
2601e9dbad6fSeschrock 				default:
2602e7437265Sahrens 					cmn_err(CE_PANIC,
2603e7437265Sahrens 					    "unknown property type");
2604e9dbad6fSeschrock 				}
2605e9dbad6fSeschrock 			} else {
2606be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2607e9dbad6fSeschrock 			}
2608e9dbad6fSeschrock 		}
2609e9dbad6fSeschrock 
261092241e0bSTom Erickson 		/* Validate permissions */
261192241e0bSTom Erickson 		if (err == 0)
261292241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
261392241e0bSTom Erickson 
261492241e0bSTom Erickson 		if (err == 0) {
261592241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
261692241e0bSTom Erickson 			if (err == -1) {
261792241e0bSTom Erickson 				/*
261892241e0bSTom Erickson 				 * For better performance we build up a list of
261992241e0bSTom Erickson 				 * properties to set in a single transaction.
262092241e0bSTom Erickson 				 */
262192241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
262292241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
262392241e0bSTom Erickson 				/*
262492241e0bSTom Erickson 				 * This may be a spurious error caused by
262592241e0bSTom Erickson 				 * receiving quota and reservation out of order.
262692241e0bSTom Erickson 				 * Try again in a second pass.
262792241e0bSTom Erickson 				 */
262892241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
26295c0b6a79SRich Morris 			}
263092241e0bSTom Erickson 		}
263192241e0bSTom Erickson 
26324445fffbSMatthew Ahrens 		if (err != 0) {
26334445fffbSMatthew Ahrens 			if (errlist != NULL)
26344445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
26354445fffbSMatthew Ahrens 			rv = err;
26364445fffbSMatthew Ahrens 		}
263792241e0bSTom Erickson 	}
263892241e0bSTom Erickson 
263992241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
264092241e0bSTom Erickson 		nvl = retrynvl;
264192241e0bSTom Erickson 		goto retry;
264292241e0bSTom Erickson 	}
264392241e0bSTom Erickson 
264492241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
264592241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
264692241e0bSTom Erickson 		/*
264792241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
264892241e0bSTom Erickson 		 * can, so try setting them individually.
264992241e0bSTom Erickson 		 */
265092241e0bSTom Erickson 		pair = NULL;
265192241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
265292241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2653cfa69fd2STom Erickson 			int err = 0;
265492241e0bSTom Erickson 
265592241e0bSTom Erickson 			propval = pair;
265692241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
265792241e0bSTom Erickson 				nvlist_t *attrs;
26584445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
26594445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
26604445fffbSMatthew Ahrens 				    ZPROP_VALUE);
266192241e0bSTom Erickson 			}
266292241e0bSTom Erickson 
266392241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26644445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26653b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26663b2aab18SMatthew Ahrens 				    source, strval);
266792241e0bSTom Erickson 			} else {
26684445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26693b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26703b2aab18SMatthew Ahrens 				    intval);
267192241e0bSTom Erickson 			}
267292241e0bSTom Erickson 
267392241e0bSTom Erickson 			if (err != 0) {
26744445fffbSMatthew Ahrens 				if (errlist != NULL) {
26754445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26764445fffbSMatthew Ahrens 					    err);
26774445fffbSMatthew Ahrens 				}
26784445fffbSMatthew Ahrens 				rv = err;
267992241e0bSTom Erickson 			}
268092241e0bSTom Erickson 		}
268192241e0bSTom Erickson 	}
26825c0b6a79SRich Morris 	nvlist_free(genericnvl);
268392241e0bSTom Erickson 	nvlist_free(retrynvl);
268492241e0bSTom Erickson 
268592241e0bSTom Erickson 	return (rv);
2686e9dbad6fSeschrock }
2687e9dbad6fSeschrock 
26883cb34c60Sahrens /*
2689ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2690ea2f5b9eSMatthew Ahrens  */
2691ea2f5b9eSMatthew Ahrens static int
26924445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2693ea2f5b9eSMatthew Ahrens {
269492241e0bSTom Erickson 	nvpair_t *pair = NULL;
2695ea2f5b9eSMatthew Ahrens 	int error = 0;
2696ea2f5b9eSMatthew Ahrens 
269792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
269892241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2699ea2f5b9eSMatthew Ahrens 
2700ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
270192241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2702be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2703ea2f5b9eSMatthew Ahrens 
2704ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2705ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2706ea2f5b9eSMatthew Ahrens 			return (error);
2707ea2f5b9eSMatthew Ahrens 
2708ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2709be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2710ea2f5b9eSMatthew Ahrens 
271178f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2712ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2713ea2f5b9eSMatthew Ahrens 	}
2714ea2f5b9eSMatthew Ahrens 	return (0);
2715ea2f5b9eSMatthew Ahrens }
2716ea2f5b9eSMatthew Ahrens 
271792241e0bSTom Erickson static void
271892241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
271992241e0bSTom Erickson {
272092241e0bSTom Erickson 	nvpair_t *pair;
272192241e0bSTom Erickson 
272292241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
272392241e0bSTom Erickson 
272492241e0bSTom Erickson 	pair = NULL;
272592241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
272692241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
272792241e0bSTom Erickson 			continue;
272892241e0bSTom Erickson 
272992241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
273092241e0bSTom Erickson 	}
273192241e0bSTom Erickson }
273292241e0bSTom Erickson 
273392241e0bSTom Erickson static int
27343b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
273592241e0bSTom Erickson     nvlist_t *skipped)
273692241e0bSTom Erickson {
273792241e0bSTom Erickson 	int err = 0;
273892241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
273992241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
274092241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
274192241e0bSTom Erickson 		/*
274292241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
274392241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
274492241e0bSTom Erickson 		 */
274592241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
27463b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
27473b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
274892241e0bSTom Erickson 	}
274992241e0bSTom Erickson 	nvlist_free(cleared_props);
275092241e0bSTom Erickson 	return (err);
275192241e0bSTom Erickson }
275292241e0bSTom Erickson 
2753ea2f5b9eSMatthew Ahrens /*
27543cb34c60Sahrens  * inputs:
27553cb34c60Sahrens  * zc_name		name of filesystem
27565c0b6a79SRich Morris  * zc_value		name of property to set
27573cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
275892241e0bSTom Erickson  * zc_cookie		received properties flag
27593cb34c60Sahrens  *
276092241e0bSTom Erickson  * outputs:
276192241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
27623cb34c60Sahrens  */
2763e9dbad6fSeschrock static int
2764fa9e4066Sahrens zfs_ioc_set_prop(zfs_cmd_t *zc)
2765fa9e4066Sahrens {
2766e9dbad6fSeschrock 	nvlist_t *nvl;
276792241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
276892241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
276992241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27704445fffbSMatthew Ahrens 	nvlist_t *errors;
2771e9dbad6fSeschrock 	int error;
2772e9dbad6fSeschrock 
2773990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2774478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2775e9dbad6fSeschrock 		return (error);
2776fa9e4066Sahrens 
277792241e0bSTom Erickson 	if (received) {
2778bb0ade09Sahrens 		nvlist_t *origprops;
2779bb0ade09Sahrens 
27803b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27813b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27823b2aab18SMatthew Ahrens 			    origprops, nvl);
2783bb0ade09Sahrens 			nvlist_free(origprops);
2784bb0ade09Sahrens 		}
278592241e0bSTom Erickson 
27863b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2787bb0ade09Sahrens 	}
2788bb0ade09Sahrens 
27894445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27903b2aab18SMatthew Ahrens 	if (error == 0)
27914445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2792ecd6cf80Smarks 
279392241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
279492241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
279592241e0bSTom Erickson 	}
279692241e0bSTom Erickson 
279792241e0bSTom Erickson 	nvlist_free(errors);
2798e9dbad6fSeschrock 	nvlist_free(nvl);
2799e9dbad6fSeschrock 	return (error);
2800fa9e4066Sahrens }
2801fa9e4066Sahrens 
28023cb34c60Sahrens /*
28033cb34c60Sahrens  * inputs:
28043cb34c60Sahrens  * zc_name		name of filesystem
28053cb34c60Sahrens  * zc_value		name of property to inherit
280692241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
28073cb34c60Sahrens  *
28083cb34c60Sahrens  * outputs:		none
28093cb34c60Sahrens  */
2810fa9e4066Sahrens static int
2811e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2812e45ce728Sahrens {
281392241e0bSTom Erickson 	const char *propname = zc->zc_value;
281492241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
281592241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
281692241e0bSTom Erickson 	zprop_source_t source = (received
281792241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
281892241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
281992241e0bSTom Erickson 
282092241e0bSTom Erickson 	if (received) {
282192241e0bSTom Erickson 		nvlist_t *dummy;
282292241e0bSTom Erickson 		nvpair_t *pair;
282392241e0bSTom Erickson 		zprop_type_t type;
282492241e0bSTom Erickson 		int err;
282592241e0bSTom Erickson 
282692241e0bSTom Erickson 		/*
282792241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
282892241e0bSTom Erickson 		 * nvpair with type info.
282992241e0bSTom Erickson 		 */
283092241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
283192241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2832be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
283392241e0bSTom Erickson 
283492241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2835a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2836a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2837be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
283892241e0bSTom Erickson 		} else {
283992241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
284092241e0bSTom Erickson 		}
284192241e0bSTom Erickson 
284292241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
284392241e0bSTom Erickson 
284492241e0bSTom Erickson 		switch (type) {
284592241e0bSTom Erickson 		case PROP_TYPE_STRING:
284692241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
284792241e0bSTom Erickson 			break;
284892241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
284992241e0bSTom Erickson 		case PROP_TYPE_INDEX:
285092241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
285192241e0bSTom Erickson 			break;
285292241e0bSTom Erickson 		default:
285392241e0bSTom Erickson 			nvlist_free(dummy);
2854be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
285592241e0bSTom Erickson 		}
285692241e0bSTom Erickson 
285792241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
285892241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
285992241e0bSTom Erickson 		nvlist_free(dummy);
286092241e0bSTom Erickson 		if (err != -1)
286192241e0bSTom Erickson 			return (err); /* special property already handled */
286292241e0bSTom Erickson 	} else {
286392241e0bSTom Erickson 		/*
286492241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
286592241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
286692241e0bSTom Erickson 		 * and reservation to the received or default values even though
286792241e0bSTom Erickson 		 * they are not considered inheritable.
286892241e0bSTom Erickson 		 */
286992241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2870be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
287192241e0bSTom Erickson 	}
287292241e0bSTom Erickson 
28734445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28743b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2875e45ce728Sahrens }
2876e45ce728Sahrens 
2877e45ce728Sahrens static int
287811a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2879b1b8ab34Slling {
2880990b4856Slling 	nvlist_t *props;
2881b1b8ab34Slling 	spa_t *spa;
2882990b4856Slling 	int error;
288392241e0bSTom Erickson 	nvpair_t *pair;
2884b1b8ab34Slling 
288592241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
288692241e0bSTom Erickson 	    zc->zc_iflags, &props))
2887b1b8ab34Slling 		return (error);
2888b1b8ab34Slling 
2889379c004dSEric Schrock 	/*
2890379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2891379c004dSEric Schrock 	 * to handle the faulted case.
2892379c004dSEric Schrock 	 */
289392241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
289492241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2895379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
289692241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2897379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2898379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2899379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2900379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2901379c004dSEric Schrock 		}
2902379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2903b693757aSEric Schrock 		if (spa != NULL) {
2904b693757aSEric Schrock 			nvlist_free(props);
2905379c004dSEric Schrock 			return (0);
2906379c004dSEric Schrock 		}
2907b693757aSEric Schrock 	}
2908379c004dSEric Schrock 
2909b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2910990b4856Slling 		nvlist_free(props);
2911b1b8ab34Slling 		return (error);
2912b1b8ab34Slling 	}
2913b1b8ab34Slling 
2914990b4856Slling 	error = spa_prop_set(spa, props);
2915b1b8ab34Slling 
2916990b4856Slling 	nvlist_free(props);
2917b1b8ab34Slling 	spa_close(spa, FTAG);
2918b1b8ab34Slling 
2919b1b8ab34Slling 	return (error);
2920b1b8ab34Slling }
2921b1b8ab34Slling 
2922b1b8ab34Slling static int
292311a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2924b1b8ab34Slling {
2925b1b8ab34Slling 	spa_t *spa;
2926b1b8ab34Slling 	int error;
2927b1b8ab34Slling 	nvlist_t *nvp = NULL;
2928b1b8ab34Slling 
2929379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2930379c004dSEric Schrock 		/*
2931379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2932379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2933379c004dSEric Schrock 		 * anyway.
2934379c004dSEric Schrock 		 */
2935379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2936379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2937990b4856Slling 			error = spa_prop_get(spa, &nvp);
2938379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2939379c004dSEric Schrock 	} else {
2940379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2941379c004dSEric Schrock 		spa_close(spa, FTAG);
2942379c004dSEric Schrock 	}
2943b1b8ab34Slling 
2944b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2945b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2946b1b8ab34Slling 	else
2947be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2948b1b8ab34Slling 
2949b1b8ab34Slling 	nvlist_free(nvp);
2950b1b8ab34Slling 	return (error);
2951b1b8ab34Slling }
2952b1b8ab34Slling 
29533cb34c60Sahrens /*
29543cb34c60Sahrens  * inputs:
29553cb34c60Sahrens  * zc_name		name of filesystem
29563cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29573cb34c60Sahrens  * zc_perm_action	allow/unallow flag
29583cb34c60Sahrens  *
29593cb34c60Sahrens  * outputs:		none
29603cb34c60Sahrens  */
2961ecd6cf80Smarks static int
2962ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2963ecd6cf80Smarks {
2964ecd6cf80Smarks 	int error;
2965ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2966ecd6cf80Smarks 
2967990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2968478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2969ecd6cf80Smarks 		return (error);
2970ecd6cf80Smarks 
2971ecd6cf80Smarks 	/*
2972ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2973ecd6cf80Smarks 	 */
2974ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2975ecd6cf80Smarks 		nvlist_free(fsaclnv);
2976be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2977ecd6cf80Smarks 	}
2978ecd6cf80Smarks 
2979ecd6cf80Smarks 	/*
2980ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2981ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2982ecd6cf80Smarks 	 * the nvlist(s)
2983ecd6cf80Smarks 	 */
2984ecd6cf80Smarks 
298591ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29863b2aab18SMatthew Ahrens 	if (error != 0) {
298791ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
298891ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
298991ebeef5Sahrens 			    fsaclnv, CRED());
299091ebeef5Sahrens 		} else {
299191ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
299291ebeef5Sahrens 			    fsaclnv, CRED());
299391ebeef5Sahrens 		}
2994ecd6cf80Smarks 	}
2995ecd6cf80Smarks 
2996ecd6cf80Smarks 	if (error == 0)
2997ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2998ecd6cf80Smarks 
2999ecd6cf80Smarks 	nvlist_free(fsaclnv);
3000ecd6cf80Smarks 	return (error);
3001ecd6cf80Smarks }
3002ecd6cf80Smarks 
30033cb34c60Sahrens /*
30043cb34c60Sahrens  * inputs:
30053cb34c60Sahrens  * zc_name		name of filesystem
30063cb34c60Sahrens  *
30073cb34c60Sahrens  * outputs:
30083cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
30093cb34c60Sahrens  */
3010ecd6cf80Smarks static int
3011ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3012ecd6cf80Smarks {
3013ecd6cf80Smarks 	nvlist_t *nvp;
3014ecd6cf80Smarks 	int error;
3015ecd6cf80Smarks 
3016ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3017ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
3018ecd6cf80Smarks 		nvlist_free(nvp);
3019ecd6cf80Smarks 	}
3020ecd6cf80Smarks 
3021ecd6cf80Smarks 	return (error);
3022ecd6cf80Smarks }
3023ecd6cf80Smarks 
30243cb34c60Sahrens /*
3025fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
3026fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
3027fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
3028fa9e4066Sahrens  */
3029fa9e4066Sahrens static vfs_t *
3030fa9e4066Sahrens zfs_get_vfs(const char *resource)
3031fa9e4066Sahrens {
3032fa9e4066Sahrens 	struct vfs *vfsp;
3033fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
3034fa9e4066Sahrens 
3035fa9e4066Sahrens 	vfs_list_read_lock();
3036fa9e4066Sahrens 	vfsp = rootvfs;
3037fa9e4066Sahrens 	do {
3038fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3039fa9e4066Sahrens 			VFS_HOLD(vfsp);
3040fa9e4066Sahrens 			vfs_found = vfsp;
3041fa9e4066Sahrens 			break;
3042fa9e4066Sahrens 		}
3043fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
3044fa9e4066Sahrens 	} while (vfsp != rootvfs);
3045fa9e4066Sahrens 	vfs_list_unlock();
3046fa9e4066Sahrens 	return (vfs_found);
3047fa9e4066Sahrens }
3048fa9e4066Sahrens 
3049ecd6cf80Smarks /* ARGSUSED */
3050fa9e4066Sahrens static void
3051ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3052fa9e4066Sahrens {
3053da6c28aaSamw 	zfs_creat_t *zct = arg;
3054e7437265Sahrens 
3055de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3056da6c28aaSamw }
3057da6c28aaSamw 
3058de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3059de8267e0Stimh 
3060da6c28aaSamw /*
3061de8267e0Stimh  * inputs:
30620a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3063f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3064f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3065f7170741SWill Andrews  * createprops		list of properties requested by creator
3066da6c28aaSamw  *
3067de8267e0Stimh  * outputs:
3068de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30690a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3070da6c28aaSamw  *
3071de8267e0Stimh  * Determine the settings for utf8only, normalization and
3072de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3073de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3074de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3075de8267e0Stimh  * request settings for these properties, even if the requested
3076de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3077de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3078de8267e0Stimh  * processing.
3079da6c28aaSamw  */
3080da6c28aaSamw static int
308114843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30820a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30830a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3084da6c28aaSamw {
3085de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3086de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3087de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3088da6c28aaSamw 
3089de8267e0Stimh 	ASSERT(zplprops != NULL);
3090da6c28aaSamw 
3091c2a93d44Stimh 	/*
3092de8267e0Stimh 	 * Pull out creator prop choices, if any.
3093c2a93d44Stimh 	 */
3094de8267e0Stimh 	if (createprops) {
3095de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
30960a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
30970a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
3098de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3099de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3100de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3101de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3102de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3103de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3104de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3105de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3106de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3107de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3108de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3109da6c28aaSamw 	}
3110da6c28aaSamw 
3111c2a93d44Stimh 	/*
31120a48a24eStimh 	 * If the zpl version requested is whacky or the file system
31130a48a24eStimh 	 * or pool is version is too "young" to support normalization
31140a48a24eStimh 	 * and the creator tried to set a value for one of the props,
31150a48a24eStimh 	 * error out.
3116c2a93d44Stimh 	 */
31170a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
31180a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
31190a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
31200a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3121de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
31220a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3123be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3124c2a93d44Stimh 
3125de8267e0Stimh 	/*
3126de8267e0Stimh 	 * Put the version in the zplprops
3127de8267e0Stimh 	 */
3128de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3129de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3130de8267e0Stimh 
3131de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3132de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3133de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3134de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3135de8267e0Stimh 
3136de8267e0Stimh 	/*
3137de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3138de8267e0Stimh 	 */
3139de8267e0Stimh 	if (norm)
3140de8267e0Stimh 		u8 = 1;
3141de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3142de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3143de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3144de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3145de8267e0Stimh 
3146de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3147de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3148de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3149de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3150de8267e0Stimh 
3151ab04eb8eStimh 	if (is_ci)
3152ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3153ab04eb8eStimh 
3154da6c28aaSamw 	return (0);
3155fa9e4066Sahrens }
3156fa9e4066Sahrens 
31570a48a24eStimh static int
31580a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31590a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31600a48a24eStimh {
31610a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
31620a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31630a48a24eStimh 	objset_t *os = NULL;
316440a5c998SMatthew Ahrens 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
31650a48a24eStimh 	char *cp;
31660a586ceaSMark Shellenbaum 	spa_t *spa;
31670a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31680a48a24eStimh 	int error;
31690a48a24eStimh 
31700a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31710a48a24eStimh 	cp = strrchr(parentname, '/');
31720a48a24eStimh 	ASSERT(cp != NULL);
31730a48a24eStimh 	cp[0] = '\0';
31740a48a24eStimh 
31750a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31760a586ceaSMark Shellenbaum 		return (error);
31770a586ceaSMark Shellenbaum 
31780a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31790a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31800a586ceaSMark Shellenbaum 
31810a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31820a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31830a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31840a48a24eStimh 
31850a48a24eStimh 	/*
31860a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31870a48a24eStimh 	 */
3188503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31890a48a24eStimh 		return (error);
31900a48a24eStimh 
31910a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31920a48a24eStimh 	    zplprops, is_ci);
3193503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31940a48a24eStimh 	return (error);
31950a48a24eStimh }
31960a48a24eStimh 
31970a48a24eStimh static int
31980a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31990a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
32000a48a24eStimh {
32010a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
32020a586ceaSMark Shellenbaum 	boolean_t sa_ok;
32030a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
32040a48a24eStimh 	int error;
32050a48a24eStimh 
32060a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
32070a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
32080a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
32090a48a24eStimh 
32100a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
32110a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
32120a48a24eStimh 	return (error);
32130a48a24eStimh }
32140a48a24eStimh 
32153cb34c60Sahrens /*
32164445fffbSMatthew Ahrens  * innvl: {
32174445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
32184445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32194445fffbSMatthew Ahrens  * }
32203cb34c60Sahrens  *
32214445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32223cb34c60Sahrens  */
3223fa9e4066Sahrens static int
32244445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3225fa9e4066Sahrens {
3226fa9e4066Sahrens 	int error = 0;
32274445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3228ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3229ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
32304445fffbSMatthew Ahrens 	int32_t type32;
32314445fffbSMatthew Ahrens 	dmu_objset_type_t type;
32324445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
32334445fffbSMatthew Ahrens 
32344445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3235be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32364445fffbSMatthew Ahrens 	type = type32;
32374445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3238fa9e4066Sahrens 
3239fa9e4066Sahrens 	switch (type) {
3240fa9e4066Sahrens 	case DMU_OST_ZFS:
3241fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3242fa9e4066Sahrens 		break;
3243fa9e4066Sahrens 
3244fa9e4066Sahrens 	case DMU_OST_ZVOL:
3245fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3246fa9e4066Sahrens 		break;
3247fa9e4066Sahrens 
3248fa9e4066Sahrens 	default:
32491d452cf5Sahrens 		cbfunc = NULL;
3250e7cbe64fSgw25295 		break;
3251fa9e4066Sahrens 	}
32524445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32534445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3254be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3255fa9e4066Sahrens 
3256da6c28aaSamw 	zct.zct_props = nvprops;
3257da6c28aaSamw 
32584445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3259be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3260e9dbad6fSeschrock 
3261fa9e4066Sahrens 	if (type == DMU_OST_ZVOL) {
3262e9dbad6fSeschrock 		uint64_t volsize, volblocksize;
32635c5460e9Seschrock 
32644445fffbSMatthew Ahrens 		if (nvprops == NULL)
3265be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32664445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32674445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3268be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32695c5460e9Seschrock 
3270ecd6cf80Smarks 		if ((error = nvlist_lookup_uint64(nvprops,
3271e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32724445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3273be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3274e9dbad6fSeschrock 
3275e9dbad6fSeschrock 		if (error != 0)
3276e9dbad6fSeschrock 			volblocksize = zfs_prop_default_numeric(
3277e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
3278e9dbad6fSeschrock 
3279e9dbad6fSeschrock 		if ((error = zvol_check_volblocksize(
3280e9dbad6fSeschrock 		    volblocksize)) != 0 ||
3281e9dbad6fSeschrock 		    (error = zvol_check_volsize(volsize,
32824445fffbSMatthew Ahrens 		    volblocksize)) != 0)
32835c5460e9Seschrock 			return (error);
3284e7437265Sahrens 	} else if (type == DMU_OST_ZFS) {
3285da6c28aaSamw 		int error;
3286e7437265Sahrens 
3287de8267e0Stimh 		/*
3288da6c28aaSamw 		 * We have to have normalization and
3289da6c28aaSamw 		 * case-folding flags correct when we do the
3290da6c28aaSamw 		 * file system creation, so go figure them out
3291de8267e0Stimh 		 * now.
3292da6c28aaSamw 		 */
3293de8267e0Stimh 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
3294de8267e0Stimh 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32954445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32960a48a24eStimh 		    zct.zct_zplprops, &is_insensitive);
3297da6c28aaSamw 		if (error != 0) {
3298de8267e0Stimh 			nvlist_free(zct.zct_zplprops);
3299da6c28aaSamw 			return (error);
3300da6c28aaSamw 		}
3301da6c28aaSamw 	}
33024445fffbSMatthew Ahrens 
33034445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
3304ab04eb8eStimh 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3305de8267e0Stimh 	nvlist_free(zct.zct_zplprops);
3306e9dbad6fSeschrock 
3307e9dbad6fSeschrock 	/*
3308e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3309e9dbad6fSeschrock 	 */
3310e9dbad6fSeschrock 	if (error == 0) {
33114445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33124445fffbSMatthew Ahrens 		    nvprops, outnvl);
331392241e0bSTom Erickson 		if (error != 0)
33143b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3315e9dbad6fSeschrock 	}
3316fa9e4066Sahrens 	return (error);
3317fa9e4066Sahrens }
3318fa9e4066Sahrens 
33193cb34c60Sahrens /*
33204445fffbSMatthew Ahrens  * innvl: {
33214445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
33224445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
33234445fffbSMatthew Ahrens  * }
33243cb34c60Sahrens  *
33254445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
33263cb34c60Sahrens  */
3327fa9e4066Sahrens static int
33284445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3329fa9e4066Sahrens {
33304445fffbSMatthew Ahrens 	int error = 0;
3331bb0ade09Sahrens 	nvlist_t *nvprops = NULL;
33324445fffbSMatthew Ahrens 	char *origin_name;
3333bb0ade09Sahrens 
33344445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3335be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33364445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
33374445fffbSMatthew Ahrens 
33384445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
33394445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3340be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3341bb0ade09Sahrens 
334223962479SMarcel Telka 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3343be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33443b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
33453b2aab18SMatthew Ahrens 	if (error != 0)
33464445fffbSMatthew Ahrens 		return (error);
33474445fffbSMatthew Ahrens 
33484445fffbSMatthew Ahrens 	/*
33494445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
33504445fffbSMatthew Ahrens 	 */
33514445fffbSMatthew Ahrens 	if (error == 0) {
33524445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33534445fffbSMatthew Ahrens 		    nvprops, outnvl);
33544445fffbSMatthew Ahrens 		if (error != 0)
33553b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3356bb0ade09Sahrens 	}
3357bb0ade09Sahrens 	return (error);
33581d452cf5Sahrens }
33591d452cf5Sahrens 
33604445fffbSMatthew Ahrens /*
33614445fffbSMatthew Ahrens  * innvl: {
33624445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33634445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33644445fffbSMatthew Ahrens  * }
33654445fffbSMatthew Ahrens  *
33664445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33674445fffbSMatthew Ahrens  */
33684445fffbSMatthew Ahrens static int
33694445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
33704445fffbSMatthew Ahrens {
33714445fffbSMatthew Ahrens 	nvlist_t *snaps;
33724445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33734445fffbSMatthew Ahrens 	int error, poollen;
33744445fffbSMatthew Ahrens 	nvpair_t *pair;
33754445fffbSMatthew Ahrens 
33764445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33774445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33784445fffbSMatthew Ahrens 		return (error);
33794445fffbSMatthew Ahrens 
33804445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33814445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3382be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33834445fffbSMatthew Ahrens 
33844445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3385be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33864445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33874445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33884445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33894445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33904445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
33914445fffbSMatthew Ahrens 
33924445fffbSMatthew Ahrens 		/*
33934445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33944445fffbSMatthew Ahrens 		 * contain only valid characters.
33954445fffbSMatthew Ahrens 		 */
339678f17100SMatthew Ahrens 		if (cp == NULL ||
339778f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3398be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
33994445fffbSMatthew Ahrens 
34004445fffbSMatthew Ahrens 		/*
34014445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
34024445fffbSMatthew Ahrens 		 */
34034445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
34044445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3405be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
34064445fffbSMatthew Ahrens 
34074445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
34084445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
34094445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
34104445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
34114445fffbSMatthew Ahrens 			    == 0) {
3412be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
34134445fffbSMatthew Ahrens 			}
34144445fffbSMatthew Ahrens 		}
34154445fffbSMatthew Ahrens 	}
34164445fffbSMatthew Ahrens 
34173b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
34184445fffbSMatthew Ahrens 	return (error);
34194445fffbSMatthew Ahrens }
34204445fffbSMatthew Ahrens 
34214445fffbSMatthew Ahrens /*
34224445fffbSMatthew Ahrens  * innvl: "message" -> string
34234445fffbSMatthew Ahrens  */
34244445fffbSMatthew Ahrens /* ARGSUSED */
34254445fffbSMatthew Ahrens static int
34264445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
34274445fffbSMatthew Ahrens {
34284445fffbSMatthew Ahrens 	char *message;
34294445fffbSMatthew Ahrens 	spa_t *spa;
34304445fffbSMatthew Ahrens 	int error;
34314445fffbSMatthew Ahrens 	char *poolname;
34324445fffbSMatthew Ahrens 
34334445fffbSMatthew Ahrens 	/*
34344445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
34354445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
34364445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
34374445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
34384445fffbSMatthew Ahrens 	 * we clear the TSD here.
34394445fffbSMatthew Ahrens 	 */
34404445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
34414445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
34424445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
34434445fffbSMatthew Ahrens 	strfree(poolname);
34444445fffbSMatthew Ahrens 	if (error != 0)
34454445fffbSMatthew Ahrens 		return (error);
34464445fffbSMatthew Ahrens 
34474445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
34484445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3449be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34504445fffbSMatthew Ahrens 	}
34514445fffbSMatthew Ahrens 
34524445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34534445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3454be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34554445fffbSMatthew Ahrens 	}
34564445fffbSMatthew Ahrens 
34574445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
34584445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
34594445fffbSMatthew Ahrens 	return (error);
34604445fffbSMatthew Ahrens }
34614445fffbSMatthew Ahrens 
34623b2aab18SMatthew Ahrens /*
34633b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34643b2aab18SMatthew Ahrens  * unmount may need to write out data.
34653b2aab18SMatthew Ahrens  *
34663b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34673b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3468fc7a6e3fSWill Andrews  *
3469fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3470fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34713b2aab18SMatthew Ahrens  */
3472fc7a6e3fSWill Andrews int
34733b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34741d452cf5Sahrens {
34754445fffbSMatthew Ahrens 	vfs_t *vfsp;
34763b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3477fc7a6e3fSWill Andrews 	int err;
3478fa9e4066Sahrens 
34793b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3480fc7a6e3fSWill Andrews 		return (0);
34814445fffbSMatthew Ahrens 
34823b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
34834445fffbSMatthew Ahrens 	if (vfsp == NULL)
3484fc7a6e3fSWill Andrews 		return (0);
34854445fffbSMatthew Ahrens 
34863b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
34873b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34883b2aab18SMatthew Ahrens 
3489fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
3490fa9e4066Sahrens 	VFS_RELE(vfsp);
3491fc7a6e3fSWill Andrews 	if (err != 0)
3492fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
34934445fffbSMatthew Ahrens 
34944445fffbSMatthew Ahrens 	/*
34954445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34964445fffbSMatthew Ahrens 	 */
34973b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3498fc7a6e3fSWill Andrews 	return (0);
34993b2aab18SMatthew Ahrens }
35003b2aab18SMatthew Ahrens 
35013b2aab18SMatthew Ahrens /* ARGSUSED */
35023b2aab18SMatthew Ahrens static int
35033b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
35043b2aab18SMatthew Ahrens {
3505fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
35063b2aab18SMatthew Ahrens }
35073b2aab18SMatthew Ahrens 
35083b2aab18SMatthew Ahrens /*
35093b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
35103b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
35113b2aab18SMatthew Ahrens  * if necessary.
35123b2aab18SMatthew Ahrens  */
35133b2aab18SMatthew Ahrens void
35143b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
35153b2aab18SMatthew Ahrens {
35163b2aab18SMatthew Ahrens 	int error;
35173b2aab18SMatthew Ahrens 	objset_t *os;
35183b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
35193b2aab18SMatthew Ahrens 
35203b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
35213b2aab18SMatthew Ahrens 	if (error != 0)
35223b2aab18SMatthew Ahrens 		return;
35233b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
35243b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
352540a5c998SMatthew Ahrens 		char originname[ZFS_MAX_DATASET_NAME_LEN];
35263b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
35273b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3528fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
35293b2aab18SMatthew Ahrens 	} else {
35303b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
35313b2aab18SMatthew Ahrens 	}
35321d452cf5Sahrens }
35331d452cf5Sahrens 
35343cb34c60Sahrens /*
35354445fffbSMatthew Ahrens  * innvl: {
35364445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
35374445fffbSMatthew Ahrens  *     (optional boolean) "defer"
35384445fffbSMatthew Ahrens  * }
35393cb34c60Sahrens  *
35404445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
35414445fffbSMatthew Ahrens  *
35423cb34c60Sahrens  */
354378f17100SMatthew Ahrens /* ARGSUSED */
35441d452cf5Sahrens static int
35454445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
35461d452cf5Sahrens {
35474445fffbSMatthew Ahrens 	nvlist_t *snaps;
354819b94df9SMatthew Ahrens 	nvpair_t *pair;
35494445fffbSMatthew Ahrens 	boolean_t defer;
35501d452cf5Sahrens 
35514445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3552be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
35534445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
355419b94df9SMatthew Ahrens 
35554445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35564445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
355778f17100SMatthew Ahrens 		(void) zfs_unmount_snap(nvpair_name(pair));
355819b94df9SMatthew Ahrens 	}
355919b94df9SMatthew Ahrens 
35603b2aab18SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
35611d452cf5Sahrens }
35621d452cf5Sahrens 
35633cb34c60Sahrens /*
356478f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
356578f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
356678f17100SMatthew Ahrens  *
356778f17100SMatthew Ahrens  * innvl: {
356878f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
356978f17100SMatthew Ahrens  * }
357078f17100SMatthew Ahrens  *
357178f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
357278f17100SMatthew Ahrens  *
357378f17100SMatthew Ahrens  */
357478f17100SMatthew Ahrens /* ARGSUSED */
357578f17100SMatthew Ahrens static int
357678f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
357778f17100SMatthew Ahrens {
357878f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
357978f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
358078f17100SMatthew Ahrens 		char *snap_name;
358178f17100SMatthew Ahrens 
358278f17100SMatthew Ahrens 		/*
358378f17100SMatthew Ahrens 		 * Verify the snapshot argument.
358478f17100SMatthew Ahrens 		 */
358578f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
358678f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
358778f17100SMatthew Ahrens 
358878f17100SMatthew Ahrens 
358978f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
359078f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
359178f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
359278f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
359378f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
359478f17100SMatthew Ahrens 		}
359578f17100SMatthew Ahrens 	}
359678f17100SMatthew Ahrens 
359778f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
359878f17100SMatthew Ahrens }
359978f17100SMatthew Ahrens 
360078f17100SMatthew Ahrens /*
360178f17100SMatthew Ahrens  * innvl: {
360278f17100SMatthew Ahrens  *     property 1, property 2, ...
360378f17100SMatthew Ahrens  * }
360478f17100SMatthew Ahrens  *
360578f17100SMatthew Ahrens  * outnvl: {
360678f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
360778f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
360878f17100SMatthew Ahrens  * }
360978f17100SMatthew Ahrens  *
361078f17100SMatthew Ahrens  */
361178f17100SMatthew Ahrens static int
361278f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
361378f17100SMatthew Ahrens {
361478f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
361578f17100SMatthew Ahrens }
361678f17100SMatthew Ahrens 
361778f17100SMatthew Ahrens /*
361878f17100SMatthew Ahrens  * innvl: {
361978f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
362078f17100SMatthew Ahrens  * }
362178f17100SMatthew Ahrens  *
362278f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
362378f17100SMatthew Ahrens  *
362478f17100SMatthew Ahrens  */
362578f17100SMatthew Ahrens static int
362678f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
362778f17100SMatthew Ahrens     nvlist_t *outnvl)
362878f17100SMatthew Ahrens {
362978f17100SMatthew Ahrens 	int error, poollen;
363078f17100SMatthew Ahrens 
363178f17100SMatthew Ahrens 	poollen = strlen(poolname);
363278f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
363378f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
363478f17100SMatthew Ahrens 		const char *name = nvpair_name(pair);
363578f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
363678f17100SMatthew Ahrens 
363778f17100SMatthew Ahrens 		/*
363878f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
363978f17100SMatthew Ahrens 		 * must contain only valid characters.
364078f17100SMatthew Ahrens 		 */
364178f17100SMatthew Ahrens 		if (cp == NULL ||
364278f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
364378f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
364478f17100SMatthew Ahrens 
364578f17100SMatthew Ahrens 		/*
364678f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
364778f17100SMatthew Ahrens 		 */
364878f17100SMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
364978f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
365078f17100SMatthew Ahrens 			return (SET_ERROR(EXDEV));
365178f17100SMatthew Ahrens 	}
365278f17100SMatthew Ahrens 
365378f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
365478f17100SMatthew Ahrens 	return (error);
365578f17100SMatthew Ahrens }
365678f17100SMatthew Ahrens 
365778f17100SMatthew Ahrens /*
36583cb34c60Sahrens  * inputs:
36593cb34c60Sahrens  * zc_name		name of dataset to destroy
36603cb34c60Sahrens  * zc_objset_type	type of objset
3661842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
36623cb34c60Sahrens  *
36633cb34c60Sahrens  * outputs:		none
36643cb34c60Sahrens  */
36651d452cf5Sahrens static int
36661d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36671d452cf5Sahrens {
3668681d9761SEric Taylor 	int err;
3669fc7a6e3fSWill Andrews 
3670fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3671fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3672fc7a6e3fSWill Andrews 		if (err != 0)
3673fc7a6e3fSWill Andrews 			return (err);
3674fc7a6e3fSWill Andrews 	}
3675fa9e4066Sahrens 
36763b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
36773b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36783b2aab18SMatthew Ahrens 	else
36793b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3680681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36815c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3682681d9761SEric Taylor 	return (err);
3683fa9e4066Sahrens }
3684fa9e4066Sahrens 
36853cb34c60Sahrens /*
3686a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
36873cb34c60Sahrens  *
3688a7027df1SMatthew Ahrens  * innvl is not used.
3689a7027df1SMatthew Ahrens  *
3690a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3691a7027df1SMatthew Ahrens  * }
36923cb34c60Sahrens  */
3693a7027df1SMatthew Ahrens /* ARGSUSED */
3694fa9e4066Sahrens static int
3695a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3696fa9e4066Sahrens {
3697ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
36983b2aab18SMatthew Ahrens 	int error;
36994ccbb6e7Sahrens 
3700a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3701503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
370247f263f4Sek110237 		if (error == 0) {
370347f263f4Sek110237 			int resume_err;
370447f263f4Sek110237 
3705a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3706a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
370747f263f4Sek110237 			error = error ? error : resume_err;
370847f263f4Sek110237 		}
37094ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
37104ccbb6e7Sahrens 	} else {
3711a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
37124ccbb6e7Sahrens 	}
37134ccbb6e7Sahrens 	return (error);
3714fa9e4066Sahrens }
3715fa9e4066Sahrens 
37163b2aab18SMatthew Ahrens static int
37173b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
37183b2aab18SMatthew Ahrens {
37193b2aab18SMatthew Ahrens 	const char *snapname = arg;
372040a5c998SMatthew Ahrens 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
37213b2aab18SMatthew Ahrens 
37223b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3723fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
37243b2aab18SMatthew Ahrens }
37253b2aab18SMatthew Ahrens 
37263cb34c60Sahrens /*
37273cb34c60Sahrens  * inputs:
37283cb34c60Sahrens  * zc_name	old name of dataset
37293cb34c60Sahrens  * zc_value	new name of dataset
37303cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
37313cb34c60Sahrens  *
37323cb34c60Sahrens  * outputs:	none
37333cb34c60Sahrens  */
3734fa9e4066Sahrens static int
3735fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3736fa9e4066Sahrens {
37377f1f55eaSvb160487 	boolean_t recursive = zc->zc_cookie & 1;
37383b2aab18SMatthew Ahrens 	char *at;
3739cdf5b4caSmmusante 
3740e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3741f18faf3fSek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
374223962479SMarcel Telka 	    strchr(zc->zc_value, '%'))
3743be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3744fa9e4066Sahrens 
37453b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
37463b2aab18SMatthew Ahrens 	if (at != NULL) {
37473b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3748a0c1127bSSteven Hartland 		int error;
3749a0c1127bSSteven Hartland 
37503b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3751be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
37523b2aab18SMatthew Ahrens 		*at = '\0';
37533b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3754a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
37553b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
37563b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3757a0c1127bSSteven Hartland 			if (error != 0) {
3758a0c1127bSSteven Hartland 				*at = '@';
37593b2aab18SMatthew Ahrens 				return (error);
3760fa9e4066Sahrens 			}
3761a0c1127bSSteven Hartland 		}
3762a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3763a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3764a0c1127bSSteven Hartland 		*at = '@';
3765a0c1127bSSteven Hartland 
3766a0c1127bSSteven Hartland 		return (error);
37673b2aab18SMatthew Ahrens 	} else {
3768681d9761SEric Taylor 		if (zc->zc_objset_type == DMU_OST_ZVOL)
3769681d9761SEric Taylor 			(void) zvol_remove_minor(zc->zc_name);
37703b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
37713b2aab18SMatthew Ahrens 	}
3772fa9e4066Sahrens }
3773fa9e4066Sahrens 
377492241e0bSTom Erickson static int
377592241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
377692241e0bSTom Erickson {
377792241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
377892241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
377992241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
378092241e0bSTom Erickson 	uint64_t intval;
378192241e0bSTom Erickson 	int err;
378292241e0bSTom Erickson 
378392241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
378492241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
378592241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
378692241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
378792241e0bSTom Erickson 				return (err);
378892241e0bSTom Erickson 			return (0);
378992241e0bSTom Erickson 		}
379092241e0bSTom Erickson 
379192241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
379292241e0bSTom Erickson 			const char *perm = NULL;
379392241e0bSTom Erickson 			const char *uq_prefix =
379492241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
379592241e0bSTom Erickson 			const char *gq_prefix =
379692241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
379792241e0bSTom Erickson 
379892241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
379992241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
380092241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
380192241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
380292241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
380392241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
380492241e0bSTom Erickson 			} else {
380592241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3806be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
380792241e0bSTom Erickson 			}
380892241e0bSTom Erickson 
380992241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
381092241e0bSTom Erickson 				return (err);
381192241e0bSTom Erickson 			return (0);
381292241e0bSTom Erickson 		}
381392241e0bSTom Erickson 
3814be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
381592241e0bSTom Erickson 	}
381692241e0bSTom Erickson 
381792241e0bSTom Erickson 	if (issnap)
3818be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
381992241e0bSTom Erickson 
382092241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
382192241e0bSTom Erickson 		/*
382292241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
382392241e0bSTom Erickson 		 * format.
382492241e0bSTom Erickson 		 */
382592241e0bSTom Erickson 		nvlist_t *attrs;
382692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
382792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
382892241e0bSTom Erickson 		    &pair) == 0);
382992241e0bSTom Erickson 	}
383092241e0bSTom Erickson 
383192241e0bSTom Erickson 	/*
383292241e0bSTom Erickson 	 * Check that this value is valid for this pool version
383392241e0bSTom Erickson 	 */
383492241e0bSTom Erickson 	switch (prop) {
383592241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
383692241e0bSTom Erickson 		/*
383792241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
383892241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
383992241e0bSTom Erickson 		 * we'll catch them later.
384092241e0bSTom Erickson 		 */
3841b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
384292241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
384392241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
384492241e0bSTom Erickson 			    zfs_earlier_version(dsname,
384592241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3846be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
384792241e0bSTom Erickson 			}
384892241e0bSTom Erickson 
384992241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
385092241e0bSTom Erickson 			    zfs_earlier_version(dsname,
385192241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3852be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
385392241e0bSTom Erickson 
3854a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3855a6f561b4SSašo Kiselkov 				spa_t *spa;
3856a6f561b4SSašo Kiselkov 
3857a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3858a6f561b4SSašo Kiselkov 					return (err);
3859a6f561b4SSašo Kiselkov 
38602acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
38612acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3862a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3863be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3864a6f561b4SSašo Kiselkov 				}
3865a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3866a6f561b4SSašo Kiselkov 			}
3867a6f561b4SSašo Kiselkov 
386892241e0bSTom Erickson 			/*
386992241e0bSTom Erickson 			 * If this is a bootable dataset then
387092241e0bSTom Erickson 			 * verify that the compression algorithm
387192241e0bSTom Erickson 			 * is supported for booting. We must return
387292241e0bSTom Erickson 			 * something other than ENOTSUP since it
387392241e0bSTom Erickson 			 * implies a downrev pool version.
387492241e0bSTom Erickson 			 */
387592241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
387692241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3877be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
387892241e0bSTom Erickson 			}
387992241e0bSTom Erickson 		}
388092241e0bSTom Erickson 		break;
388192241e0bSTom Erickson 
388292241e0bSTom Erickson 	case ZFS_PROP_COPIES:
388392241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3884be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
388592241e0bSTom Erickson 		break;
388692241e0bSTom Erickson 
3887b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
3888b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
3889b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
3890b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
3891b5152584SMatthew Ahrens 			spa_t *spa;
3892b5152584SMatthew Ahrens 
3893b5152584SMatthew Ahrens 			/*
3894b5152584SMatthew Ahrens 			 * We don't allow setting the property above 1MB,
3895b5152584SMatthew Ahrens 			 * unless the tunable has been changed.
3896b5152584SMatthew Ahrens 			 */
3897b5152584SMatthew Ahrens 			if (intval > zfs_max_recordsize ||
3898b5152584SMatthew Ahrens 			    intval > SPA_MAXBLOCKSIZE)
38996de9bb56SMatthew Ahrens 				return (SET_ERROR(ERANGE));
3900b5152584SMatthew Ahrens 
3901b5152584SMatthew Ahrens 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3902b5152584SMatthew Ahrens 				return (err);
3903b5152584SMatthew Ahrens 
3904b5152584SMatthew Ahrens 			if (!spa_feature_is_enabled(spa,
3905b5152584SMatthew Ahrens 			    SPA_FEATURE_LARGE_BLOCKS)) {
3906b5152584SMatthew Ahrens 				spa_close(spa, FTAG);
3907b5152584SMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
3908b5152584SMatthew Ahrens 			}
3909b5152584SMatthew Ahrens 			spa_close(spa, FTAG);
3910b5152584SMatthew Ahrens 		}
3911b5152584SMatthew Ahrens 		break;
3912b5152584SMatthew Ahrens 
391392241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
391492241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3915be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
391692241e0bSTom Erickson 		break;
391792241e0bSTom Erickson 
391892241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
391992241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
392092241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
392192241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
392292241e0bSTom Erickson 			    zfs_earlier_version(dsname,
392392241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3924be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
392592241e0bSTom Erickson 		}
392692241e0bSTom Erickson 		break;
392745818ee1SMatthew Ahrens 
392845818ee1SMatthew Ahrens 	case ZFS_PROP_CHECKSUM:
392945818ee1SMatthew Ahrens 	case ZFS_PROP_DEDUP:
393045818ee1SMatthew Ahrens 	{
393145818ee1SMatthew Ahrens 		spa_feature_t feature;
393245818ee1SMatthew Ahrens 		spa_t *spa;
393345818ee1SMatthew Ahrens 
393445818ee1SMatthew Ahrens 		/* dedup feature version checks */
393545818ee1SMatthew Ahrens 		if (prop == ZFS_PROP_DEDUP &&
393645818ee1SMatthew Ahrens 		    zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
393745818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
393845818ee1SMatthew Ahrens 
393945818ee1SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) != 0)
394045818ee1SMatthew Ahrens 			return (SET_ERROR(EINVAL));
394145818ee1SMatthew Ahrens 
394245818ee1SMatthew Ahrens 		/* check prop value is enabled in features */
3943971640e6Silovezfs 		feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
394445818ee1SMatthew Ahrens 		if (feature == SPA_FEATURE_NONE)
394545818ee1SMatthew Ahrens 			break;
394645818ee1SMatthew Ahrens 
394745818ee1SMatthew Ahrens 		if ((err = spa_open(dsname, &spa, FTAG)) != 0)
394845818ee1SMatthew Ahrens 			return (err);
394945818ee1SMatthew Ahrens 		/*
395045818ee1SMatthew Ahrens 		 * Salted checksums are not supported on root pools.
395145818ee1SMatthew Ahrens 		 */
395245818ee1SMatthew Ahrens 		if (spa_bootfs(spa) != 0 &&
395345818ee1SMatthew Ahrens 		    intval < ZIO_CHECKSUM_FUNCTIONS &&
395445818ee1SMatthew Ahrens 		    (zio_checksum_table[intval].ci_flags &
395545818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_SALTED)) {
395645818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
395745818ee1SMatthew Ahrens 			return (SET_ERROR(ERANGE));
395845818ee1SMatthew Ahrens 		}
395945818ee1SMatthew Ahrens 		if (!spa_feature_is_enabled(spa, feature)) {
396045818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
396145818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
396245818ee1SMatthew Ahrens 		}
396345818ee1SMatthew Ahrens 		spa_close(spa, FTAG);
396445818ee1SMatthew Ahrens 		break;
396545818ee1SMatthew Ahrens 	}
396692241e0bSTom Erickson 	}
396792241e0bSTom Erickson 
396892241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
396992241e0bSTom Erickson }
397092241e0bSTom Erickson 
397192241e0bSTom Erickson /*
3972a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3973a6f561b4SSašo Kiselkov  * multiple times.
3974a6f561b4SSašo Kiselkov  */
3975a6f561b4SSašo Kiselkov static int
39763b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3977a6f561b4SSašo Kiselkov {
39783b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39792acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3980a6f561b4SSašo Kiselkov 
39812acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
3982a6f561b4SSašo Kiselkov 		return (0);
3983a6f561b4SSašo Kiselkov 	else
3984be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3985a6f561b4SSašo Kiselkov }
3986a6f561b4SSašo Kiselkov 
3987a6f561b4SSašo Kiselkov /*
3988a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3989a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3990a6f561b4SSašo Kiselkov  */
3991a6f561b4SSašo Kiselkov static void
39923b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3993a6f561b4SSašo Kiselkov {
39943b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39952acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3996a6f561b4SSašo Kiselkov 
39972acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
3998a6f561b4SSašo Kiselkov }
3999a6f561b4SSašo Kiselkov 
4000a6f561b4SSašo Kiselkov /*
40013b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
40023b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
40033b2aab18SMatthew Ahrens  * as being active.
40043b2aab18SMatthew Ahrens  */
40053b2aab18SMatthew Ahrens static int
40062acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
40073b2aab18SMatthew Ahrens {
40083b2aab18SMatthew Ahrens 	int err;
40093b2aab18SMatthew Ahrens 
40103b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
40113b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
40123b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
40137d46dc6cSMatthew Ahrens 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
40143b2aab18SMatthew Ahrens 
40153b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
40163b2aab18SMatthew Ahrens 		return (err);
40173b2aab18SMatthew Ahrens 	else
40183b2aab18SMatthew Ahrens 		return (0);
40193b2aab18SMatthew Ahrens }
40203b2aab18SMatthew Ahrens 
40213b2aab18SMatthew Ahrens /*
402292241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
402392241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
402492241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
402592241e0bSTom Erickson  *
402692241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
402792241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
402892241e0bSTom Erickson  * of all the properties that failed a permission check along with the
402992241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
403092241e0bSTom Erickson  * returned errlist.
403192241e0bSTom Erickson  *
403292241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
403392241e0bSTom Erickson  * pointed at by errlist is NULL.
403492241e0bSTom Erickson  */
403592241e0bSTom Erickson static int
403692241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4037745cd3c5Smaybee {
4038745cd3c5Smaybee 	zfs_cmd_t *zc;
403992241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
404092241e0bSTom Erickson 	nvlist_t *errors;
404192241e0bSTom Erickson 	int err, rv = 0;
4042745cd3c5Smaybee 
4043745cd3c5Smaybee 	if (props == NULL)
404492241e0bSTom Erickson 		return (0);
404592241e0bSTom Erickson 
404692241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
404792241e0bSTom Erickson 
4048745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4049745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
405092241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
405192241e0bSTom Erickson 	while (pair != NULL) {
405292241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
405392241e0bSTom Erickson 
405492241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
405592241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
40564445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
405792241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
405892241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
405992241e0bSTom Erickson 			    zc->zc_value, err) == 0);
406092241e0bSTom Erickson 		}
406192241e0bSTom Erickson 		pair = next_pair;
4062745cd3c5Smaybee 	}
4063745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
406492241e0bSTom Erickson 
406592241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
406692241e0bSTom Erickson 		nvlist_free(errors);
406792241e0bSTom Erickson 		errors = NULL;
406892241e0bSTom Erickson 	} else {
406992241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
4070745cd3c5Smaybee 	}
4071745cd3c5Smaybee 
407292241e0bSTom Erickson 	if (errlist == NULL)
407392241e0bSTom Erickson 		nvlist_free(errors);
407492241e0bSTom Erickson 	else
407592241e0bSTom Erickson 		*errlist = errors;
407692241e0bSTom Erickson 
407792241e0bSTom Erickson 	return (rv);
407892241e0bSTom Erickson }
407992241e0bSTom Erickson 
408092241e0bSTom Erickson static boolean_t
408192241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
408292241e0bSTom Erickson {
408392241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
408492241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
408592241e0bSTom Erickson 		nvlist_t *attrs;
408692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
408792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
408892241e0bSTom Erickson 		    &p1) == 0);
408992241e0bSTom Erickson 	}
409092241e0bSTom Erickson 
409192241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
409292241e0bSTom Erickson 		nvlist_t *attrs;
409392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
409492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
409592241e0bSTom Erickson 		    &p2) == 0);
409692241e0bSTom Erickson 	}
409792241e0bSTom Erickson 
409892241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
409992241e0bSTom Erickson 		return (B_FALSE);
410092241e0bSTom Erickson 
410192241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
410292241e0bSTom Erickson 		char *valstr1, *valstr2;
410392241e0bSTom Erickson 
410492241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
410592241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
410692241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
410792241e0bSTom Erickson 	} else {
410892241e0bSTom Erickson 		uint64_t intval1, intval2;
410992241e0bSTom Erickson 
411092241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
411192241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
411292241e0bSTom Erickson 		return (intval1 == intval2);
411392241e0bSTom Erickson 	}
411492241e0bSTom Erickson }
411592241e0bSTom Erickson 
411692241e0bSTom Erickson /*
411792241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
411892241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
411992241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
412092241e0bSTom Erickson  */
412192241e0bSTom Erickson static void
412292241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
412392241e0bSTom Erickson {
412492241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
412592241e0bSTom Erickson 
412692241e0bSTom Erickson 	if (origprops == NULL)
412792241e0bSTom Erickson 		return; /* all props need to be received */
412892241e0bSTom Erickson 
412992241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
413092241e0bSTom Erickson 	while (pair != NULL) {
413192241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
413292241e0bSTom Erickson 		nvpair_t *match;
413392241e0bSTom Erickson 
413492241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
413592241e0bSTom Erickson 
413692241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
413792241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
413892241e0bSTom Erickson 			goto next; /* need to set received value */
413992241e0bSTom Erickson 
414092241e0bSTom Erickson 		/* don't clear the existing received value */
414192241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
414292241e0bSTom Erickson 		/* don't bother receiving the property */
414392241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
414492241e0bSTom Erickson next:
414592241e0bSTom Erickson 		pair = next_pair;
414692241e0bSTom Erickson 	}
414792241e0bSTom Erickson }
414892241e0bSTom Erickson 
41495878fad7SDan McDonald /*
41505878fad7SDan McDonald  * Extract properties that cannot be set PRIOR to the receipt of a dataset.
41515878fad7SDan McDonald  * For example, refquota cannot be set until after the receipt of a dataset,
41525878fad7SDan McDonald  * because in replication streams, an older/earlier snapshot may exceed the
41535878fad7SDan McDonald  * refquota.  We want to receive the older/earlier snapshot, but setting
41545878fad7SDan McDonald  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
41555878fad7SDan McDonald  * the older/earlier snapshot from being received (with EDQUOT).
41565878fad7SDan McDonald  *
41575878fad7SDan McDonald  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
41585878fad7SDan McDonald  *
41595878fad7SDan McDonald  * libzfs will need to be judicious handling errors encountered by props
41605878fad7SDan McDonald  * extracted by this function.
41615878fad7SDan McDonald  */
41625878fad7SDan McDonald static nvlist_t *
41635878fad7SDan McDonald extract_delay_props(nvlist_t *props)
41645878fad7SDan McDonald {
41655878fad7SDan McDonald 	nvlist_t *delayprops;
41665878fad7SDan McDonald 	nvpair_t *nvp, *tmp;
41675878fad7SDan McDonald 	static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
41685878fad7SDan McDonald 	int i;
41695878fad7SDan McDonald 
41705878fad7SDan McDonald 	VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
41715878fad7SDan McDonald 
41725878fad7SDan McDonald 	for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
41735878fad7SDan McDonald 	    nvp = nvlist_next_nvpair(props, nvp)) {
41745878fad7SDan McDonald 		/*
41755878fad7SDan McDonald 		 * strcmp() is safe because zfs_prop_to_name() always returns
41765878fad7SDan McDonald 		 * a bounded string.
41775878fad7SDan McDonald 		 */
41785878fad7SDan McDonald 		for (i = 0; delayable[i] != 0; i++) {
41795878fad7SDan McDonald 			if (strcmp(zfs_prop_to_name(delayable[i]),
41805878fad7SDan McDonald 			    nvpair_name(nvp)) == 0) {
41815878fad7SDan McDonald 				break;
41825878fad7SDan McDonald 			}
41835878fad7SDan McDonald 		}
41845878fad7SDan McDonald 		if (delayable[i] != 0) {
41855878fad7SDan McDonald 			tmp = nvlist_prev_nvpair(props, nvp);
41865878fad7SDan McDonald 			VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
41875878fad7SDan McDonald 			VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
41885878fad7SDan McDonald 			nvp = tmp;
41895878fad7SDan McDonald 		}
41905878fad7SDan McDonald 	}
41915878fad7SDan McDonald 
41925878fad7SDan McDonald 	if (nvlist_empty(delayprops)) {
41935878fad7SDan McDonald 		nvlist_free(delayprops);
41945878fad7SDan McDonald 		delayprops = NULL;
41955878fad7SDan McDonald 	}
41965878fad7SDan McDonald 	return (delayprops);
41975878fad7SDan McDonald }
41985878fad7SDan McDonald 
419992241e0bSTom Erickson #ifdef	DEBUG
420092241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
420192241e0bSTom Erickson #endif
420292241e0bSTom Erickson 
42033cb34c60Sahrens /*
42043cb34c60Sahrens  * inputs:
42053cb34c60Sahrens  * zc_name		name of containing filesystem
42063cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
42073cb34c60Sahrens  * zc_value		name of snapshot to create
42083cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
42093cb34c60Sahrens  * zc_cookie		file descriptor to recv from
42103cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
42113cb34c60Sahrens  * zc_guid		force flag
4212c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
4213c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
42149c3fd121SMatthew Ahrens  * zc_resumable		if data is incomplete assume sender will resume
42153cb34c60Sahrens  *
42163cb34c60Sahrens  * outputs:
42173cb34c60Sahrens  * zc_cookie		number of bytes read
421892241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
421992241e0bSTom Erickson  * zc_obj		zprop_errflags_t
4220c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
42213cb34c60Sahrens  */
4222fa9e4066Sahrens static int
42233cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4224fa9e4066Sahrens {
4225fa9e4066Sahrens 	file_t *fp;
42263cb34c60Sahrens 	dmu_recv_cookie_t drc;
4227f18faf3fSek110237 	boolean_t force = (boolean_t)zc->zc_guid;
422892241e0bSTom Erickson 	int fd;
422992241e0bSTom Erickson 	int error = 0;
423092241e0bSTom Erickson 	int props_error = 0;
423192241e0bSTom Erickson 	nvlist_t *errors;
42323cb34c60Sahrens 	offset_t off;
423392241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
423492241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
42355878fad7SDan McDonald 	nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
42363b2aab18SMatthew Ahrens 	char *origin = NULL;
42373cb34c60Sahrens 	char *tosnap;
423840a5c998SMatthew Ahrens 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
423992241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
4240fa9e4066Sahrens 
42413ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4242f18faf3fSek110237 	    strchr(zc->zc_value, '@') == NULL ||
4243f18faf3fSek110237 	    strchr(zc->zc_value, '%'))
4244be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
42453ccfa83cSahrens 
42463cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
42473cb34c60Sahrens 	tosnap = strchr(tofs, '@');
424892241e0bSTom Erickson 	*tosnap++ = '\0';
42493cb34c60Sahrens 
42503cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
42513cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4252478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
42533cb34c60Sahrens 		return (error);
42543cb34c60Sahrens 
4255fa9e4066Sahrens 	fd = zc->zc_cookie;
4256fa9e4066Sahrens 	fp = getf(fd);
42573cb34c60Sahrens 	if (fp == NULL) {
42583cb34c60Sahrens 		nvlist_free(props);
4259be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
42603cb34c60Sahrens 	}
4261a2eea2e1Sahrens 
42629c3fd121SMatthew Ahrens 	errors = fnvlist_alloc();
426392241e0bSTom Erickson 
42643b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
42653b2aab18SMatthew Ahrens 		origin = zc->zc_string;
42663b2aab18SMatthew Ahrens 
42673b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
42689c3fd121SMatthew Ahrens 	    &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
42693b2aab18SMatthew Ahrens 	if (error != 0)
42703b2aab18SMatthew Ahrens 		goto out;
42713b2aab18SMatthew Ahrens 
42723b2aab18SMatthew Ahrens 	/*
42733b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
42743b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
42753b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
42763b2aab18SMatthew Ahrens 	 */
42773b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
42783b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
42793b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
42803b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
428192241e0bSTom Erickson 			first_recvd_props = B_TRUE;
428292241e0bSTom Erickson 
4283745cd3c5Smaybee 		/*
428492241e0bSTom Erickson 		 * If new received properties are supplied, they are to
428592241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
428692241e0bSTom Erickson 		 * away the existing ones.
4287745cd3c5Smaybee 		 */
42883b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
428992241e0bSTom Erickson 			nvlist_t *errlist = NULL;
429092241e0bSTom Erickson 			/*
429192241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
429292241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
429392241e0bSTom Erickson 			 *
429492241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
429592241e0bSTom Erickson 			 * special case where we blow away all local properties
429692241e0bSTom Erickson 			 * regardless.
429792241e0bSTom Erickson 			 */
429892241e0bSTom Erickson 			if (!first_recvd_props)
429992241e0bSTom Erickson 				props_reduce(props, origprops);
43003b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
430192241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
430292241e0bSTom Erickson 			nvlist_free(errlist);
4303745cd3c5Smaybee 
43043b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
430592241e0bSTom Erickson 			    first_recvd_props ? NULL : props) != 0)
430692241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
430792241e0bSTom Erickson 		} else {
430892241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
430992241e0bSTom Erickson 		}
431092241e0bSTom Erickson 	}
431192241e0bSTom Erickson 
43123b2aab18SMatthew Ahrens 	if (props != NULL) {
43133b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
43143b2aab18SMatthew Ahrens 
43153b2aab18SMatthew Ahrens 		if (props_error == 0) {
43165878fad7SDan McDonald 			delayprops = extract_delay_props(props);
431792241e0bSTom Erickson 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
43184445fffbSMatthew Ahrens 			    props, errors);
431992241e0bSTom Erickson 		}
43203b2aab18SMatthew Ahrens 	}
432192241e0bSTom Erickson 
43223cb34c60Sahrens 	off = fp->f_offset;
4323c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4324c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
43253cb34c60Sahrens 
4326f4b94bdeSMatthew Ahrens 	if (error == 0) {
4327f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4328f4b94bdeSMatthew Ahrens 
4329f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4330f4b94bdeSMatthew Ahrens 			/* online recv */
4331f4b94bdeSMatthew Ahrens 			int end_err;
4332f18faf3fSek110237 
4333503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4334f4b94bdeSMatthew Ahrens 			/*
4335f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4336f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4337f4b94bdeSMatthew Ahrens 			 */
433891948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
43395c703fceSGeorge Wilson 			if (error == 0)
43405c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4341f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4342f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4343f4b94bdeSMatthew Ahrens 		} else {
434491948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
4345f18faf3fSek110237 		}
43465878fad7SDan McDonald 
43475878fad7SDan McDonald 		/* Set delayed properties now, after we're done receiving. */
43485878fad7SDan McDonald 		if (delayprops != NULL && error == 0) {
43495878fad7SDan McDonald 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
43505878fad7SDan McDonald 			    delayprops, errors);
43515878fad7SDan McDonald 		}
43525878fad7SDan McDonald 	}
43535878fad7SDan McDonald 
43545878fad7SDan McDonald 	if (delayprops != NULL) {
43555878fad7SDan McDonald 		/*
43565878fad7SDan McDonald 		 * Merge delayed props back in with initial props, in case
43575878fad7SDan McDonald 		 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
43585878fad7SDan McDonald 		 * we have to make sure clear_received_props() includes
43595878fad7SDan McDonald 		 * the delayed properties).
43605878fad7SDan McDonald 		 *
43615878fad7SDan McDonald 		 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
43625878fad7SDan McDonald 		 * using ASSERT() will be just like a VERIFY.
43635878fad7SDan McDonald 		 */
43645878fad7SDan McDonald 		ASSERT(nvlist_merge(props, delayprops, 0) == 0);
43655878fad7SDan McDonald 		nvlist_free(delayprops);
43665878fad7SDan McDonald 	}
43675878fad7SDan McDonald 
43685878fad7SDan McDonald 	/*
43695878fad7SDan McDonald 	 * Now that all props, initial and delayed, are set, report the prop
43705878fad7SDan McDonald 	 * errors to the caller.
43715878fad7SDan McDonald 	 */
43725878fad7SDan McDonald 	if (zc->zc_nvlist_dst_size != 0 &&
43735878fad7SDan McDonald 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
43745878fad7SDan McDonald 	    put_nvlist(zc, errors) != 0)) {
43755878fad7SDan McDonald 		/*
43765878fad7SDan McDonald 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
43775878fad7SDan McDonald 		 * size or supplied an invalid address.
43785878fad7SDan McDonald 		 */
43795878fad7SDan McDonald 		props_error = SET_ERROR(EINVAL);
4380f4b94bdeSMatthew Ahrens 	}
43813cb34c60Sahrens 
43823cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
43833cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
43843cb34c60Sahrens 		fp->f_offset = off;
4385a2eea2e1Sahrens 
438692241e0bSTom Erickson #ifdef	DEBUG
438792241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
438892241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
438992241e0bSTom Erickson 		error = 1;
439092241e0bSTom Erickson 	}
439192241e0bSTom Erickson #endif
4392745cd3c5Smaybee 	/*
4393745cd3c5Smaybee 	 * On error, restore the original props.
4394745cd3c5Smaybee 	 */
43953b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
43963b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
439792241e0bSTom Erickson 			/*
439892241e0bSTom Erickson 			 * We failed to clear the received properties.
439992241e0bSTom Erickson 			 * Since we may have left a $recvd value on the
440092241e0bSTom Erickson 			 * system, we can't clear the $hasrecvd flag.
440192241e0bSTom Erickson 			 */
440292241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
440392241e0bSTom Erickson 		} else if (first_recvd_props) {
44043b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
440592241e0bSTom Erickson 		}
440692241e0bSTom Erickson 
440792241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
440892241e0bSTom Erickson 			/* We failed to stash the original properties. */
440992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
441092241e0bSTom Erickson 		}
441192241e0bSTom Erickson 
441292241e0bSTom Erickson 		/*
441392241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
441492241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
441592241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
441692241e0bSTom Erickson 		 * first new-style receive.
441792241e0bSTom Erickson 		 */
441892241e0bSTom Erickson 		if (origprops != NULL &&
441992241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
442092241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
442192241e0bSTom Erickson 		    origprops, NULL) != 0) {
442292241e0bSTom Erickson 			/*
442392241e0bSTom Erickson 			 * We stashed the original properties but failed to
442492241e0bSTom Erickson 			 * restore them.
442592241e0bSTom Erickson 			 */
442692241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
442792241e0bSTom Erickson 		}
4428745cd3c5Smaybee 	}
4429745cd3c5Smaybee out:
4430745cd3c5Smaybee 	nvlist_free(props);
4431745cd3c5Smaybee 	nvlist_free(origprops);
443292241e0bSTom Erickson 	nvlist_free(errors);
4433fa9e4066Sahrens 	releasef(fd);
443492241e0bSTom Erickson 
443592241e0bSTom Erickson 	if (error == 0)
443692241e0bSTom Erickson 		error = props_error;
443792241e0bSTom Erickson 
4438fa9e4066Sahrens 	return (error);
4439fa9e4066Sahrens }
4440fa9e4066Sahrens 
44413cb34c60Sahrens /*
44423cb34c60Sahrens  * inputs:
44433cb34c60Sahrens  * zc_name	name of snapshot to send
44443cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4445a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4446a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4447a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
444819b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
444919b94df9SMatthew Ahrens  *		output size in zc_objset_type.
4450b5152584SMatthew Ahrens  * zc_flags	lzc_send_flags
44513cb34c60Sahrens  *
445278f17100SMatthew Ahrens  * outputs:
445378f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
44543cb34c60Sahrens  */
4455fa9e4066Sahrens static int
44563cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4457fa9e4066Sahrens {
4458fa9e4066Sahrens 	int error;
44593cb34c60Sahrens 	offset_t off;
446019b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
44615d7b4d43SMatthew Ahrens 	boolean_t embedok = (zc->zc_flags & 0x1);
4462b5152584SMatthew Ahrens 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
4463fa9e4066Sahrens 
44643b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
44653b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
44663b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
44673b2aab18SMatthew Ahrens 
44683b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44693b2aab18SMatthew Ahrens 		if (error != 0)
4470fa9e4066Sahrens 			return (error);
4471fa9e4066Sahrens 
44723b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
44733b2aab18SMatthew Ahrens 		if (error != 0) {
44743b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
44753b2aab18SMatthew Ahrens 			return (error);
44763b2aab18SMatthew Ahrens 		}
44773b2aab18SMatthew Ahrens 
44783b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
4479c1379625SJustin T. Gibbs 			zc->zc_fromobj =
4480c1379625SJustin T. Gibbs 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
44813b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
44823b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
44833b2aab18SMatthew Ahrens 	}
44843b2aab18SMatthew Ahrens 
44853b2aab18SMatthew Ahrens 	if (estimate) {
44863b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
44873b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
44883b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
44893b2aab18SMatthew Ahrens 
44903b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44913b2aab18SMatthew Ahrens 		if (error != 0)
4492fa9e4066Sahrens 			return (error);
4493a7f53a56SChris Kirby 
44943b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
44953b2aab18SMatthew Ahrens 		if (error != 0) {
44963b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4497a7f53a56SChris Kirby 			return (error);
4498a7f53a56SChris Kirby 		}
4499a7f53a56SChris Kirby 
4500a7f53a56SChris Kirby 		if (zc->zc_fromobj != 0) {
45013b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
45023b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
45033b2aab18SMatthew Ahrens 			if (error != 0) {
45043b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
45053b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
4506a7f53a56SChris Kirby 				return (error);
4507a7f53a56SChris Kirby 			}
45084445fffbSMatthew Ahrens 		}
45094445fffbSMatthew Ahrens 
45104445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
451119b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
45123b2aab18SMatthew Ahrens 
45133b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
45143b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
45153b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
45163b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
451719b94df9SMatthew Ahrens 	} else {
451819b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
45193b2aab18SMatthew Ahrens 		if (fp == NULL)
4520be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4521fa9e4066Sahrens 
45223cb34c60Sahrens 		off = fp->f_offset;
45233b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4524b5152584SMatthew Ahrens 		    zc->zc_fromobj, embedok, large_block_ok,
4525b5152584SMatthew Ahrens 		    zc->zc_cookie, fp->f_vnode, &off);
4526fa9e4066Sahrens 
45273cb34c60Sahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
45283cb34c60Sahrens 			fp->f_offset = off;
4529fa9e4066Sahrens 		releasef(zc->zc_cookie);
453019b94df9SMatthew Ahrens 	}
4531fa9e4066Sahrens 	return (error);
4532fa9e4066Sahrens }
4533fa9e4066Sahrens 
45344e3c9f44SBill Pijewski /*
45354e3c9f44SBill Pijewski  * inputs:
45364e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
45374e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
45384e3c9f44SBill Pijewski  *
45394e3c9f44SBill Pijewski  * outputs:
45404e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
45414e3c9f44SBill Pijewski  */
45424e3c9f44SBill Pijewski static int
45434e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
45444e3c9f44SBill Pijewski {
45453b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
45464e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
45474e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
45484e3c9f44SBill Pijewski 	int error;
45494e3c9f44SBill Pijewski 
45503b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
45513b2aab18SMatthew Ahrens 	if (error != 0)
45524e3c9f44SBill Pijewski 		return (error);
45534e3c9f44SBill Pijewski 
45543b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
45553b2aab18SMatthew Ahrens 	if (error != 0) {
45563b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
45573b2aab18SMatthew Ahrens 		return (error);
45583b2aab18SMatthew Ahrens 	}
45593b2aab18SMatthew Ahrens 
45604e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
45614e3c9f44SBill Pijewski 
45624e3c9f44SBill Pijewski 	/*
45634e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
45644e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
45654e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
45664e3c9f44SBill Pijewski 	 * that stream.
45674e3c9f44SBill Pijewski 	 */
45684e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
45694e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
45704e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
45714e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
45724e3c9f44SBill Pijewski 			break;
45734e3c9f44SBill Pijewski 	}
45744e3c9f44SBill Pijewski 
45754e3c9f44SBill Pijewski 	if (dsp != NULL)
45764e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
45774e3c9f44SBill Pijewski 	else
4578be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
45794e3c9f44SBill Pijewski 
45804e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
45814e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
45823b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
45834e3c9f44SBill Pijewski 	return (error);
45844e3c9f44SBill Pijewski }
45854e3c9f44SBill Pijewski 
4586ea8dc4b6Seschrock static int
4587ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4588ea8dc4b6Seschrock {
4589ea8dc4b6Seschrock 	int id, error;
4590ea8dc4b6Seschrock 
4591ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4592ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4593ea8dc4b6Seschrock 
4594ea8dc4b6Seschrock 	if (error == 0)
4595ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4596ea8dc4b6Seschrock 
4597ea8dc4b6Seschrock 	return (error);
4598ea8dc4b6Seschrock }
4599ea8dc4b6Seschrock 
4600ea8dc4b6Seschrock static int
4601ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4602ea8dc4b6Seschrock {
4603ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4604ea8dc4b6Seschrock }
4605ea8dc4b6Seschrock 
4606ea8dc4b6Seschrock static int
4607ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4608ea8dc4b6Seschrock {
4609ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4610ea8dc4b6Seschrock 	int error;
4611ea8dc4b6Seschrock 
4612ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4613ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4614ea8dc4b6Seschrock 
4615ea8dc4b6Seschrock 	zc->zc_guid = id;
4616ea8dc4b6Seschrock 
4617ea8dc4b6Seschrock 	return (error);
4618ea8dc4b6Seschrock }
4619ea8dc4b6Seschrock 
4620ea8dc4b6Seschrock static int
4621ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4622ea8dc4b6Seschrock {
4623ea8dc4b6Seschrock 	spa_t *spa;
4624ea8dc4b6Seschrock 	int error;
4625e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4626ea8dc4b6Seschrock 
4627ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4628ea8dc4b6Seschrock 		return (error);
4629ea8dc4b6Seschrock 
4630e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4631ea8dc4b6Seschrock 	    &count);
4632ea8dc4b6Seschrock 	if (error == 0)
4633e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4634ea8dc4b6Seschrock 	else
4635e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4636ea8dc4b6Seschrock 
4637ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4638ea8dc4b6Seschrock 
4639ea8dc4b6Seschrock 	return (error);
4640ea8dc4b6Seschrock }
4641ea8dc4b6Seschrock 
4642ea8dc4b6Seschrock static int
4643ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4644ea8dc4b6Seschrock {
4645ea8dc4b6Seschrock 	spa_t *spa;
4646ea8dc4b6Seschrock 	vdev_t *vd;
4647bb8b5132Sek110237 	int error;
4648ea8dc4b6Seschrock 
4649b87f3af3Sperrin 	/*
4650b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4651b87f3af3Sperrin 	 */
4652b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4653b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4654b87f3af3Sperrin 	if (spa == NULL) {
4655b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4656be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4657b87f3af3Sperrin 	}
4658b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4659b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4660b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4661b87f3af3Sperrin 	}
4662468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4663b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4664b87f3af3Sperrin 
4665c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4666468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4667468c413aSTim Haley 	} else {
4668468c413aSTim Haley 		nvlist_t *policy;
4669468c413aSTim Haley 		nvlist_t *config = NULL;
4670468c413aSTim Haley 
4671468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4672be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4673468c413aSTim Haley 
4674468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4675468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4676468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4677468c413aSTim Haley 			    policy, &config);
4678468c413aSTim Haley 			if (config != NULL) {
46794b964adaSGeorge Wilson 				int err;
46804b964adaSGeorge Wilson 
46814b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
46824b964adaSGeorge Wilson 					error = err;
4683468c413aSTim Haley 				nvlist_free(config);
4684468c413aSTim Haley 			}
4685468c413aSTim Haley 			nvlist_free(policy);
4686468c413aSTim Haley 		}
4687468c413aSTim Haley 	}
4688468c413aSTim Haley 
46893b2aab18SMatthew Ahrens 	if (error != 0)
4690ea8dc4b6Seschrock 		return (error);
4691ea8dc4b6Seschrock 
46928f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4693ea8dc4b6Seschrock 
4694e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4695ea8dc4b6Seschrock 		vd = NULL;
4696c5904d13Seschrock 	} else {
4697c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4698fa94a07fSbrendan 		if (vd == NULL) {
4699e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4700ea8dc4b6Seschrock 			spa_close(spa, FTAG);
4701be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4702ea8dc4b6Seschrock 		}
4703fa94a07fSbrendan 	}
4704ea8dc4b6Seschrock 
4705e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4706ea8dc4b6Seschrock 
4707e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4708e14bb325SJeff Bonwick 
4709e14bb325SJeff Bonwick 	/*
4710e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4711e14bb325SJeff Bonwick 	 */
471254d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4713be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4714ea8dc4b6Seschrock 
4715ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4716ea8dc4b6Seschrock 
471754d692b7SGeorge Wilson 	return (error);
4718ea8dc4b6Seschrock }
4719ea8dc4b6Seschrock 
47204263d13fSGeorge Wilson static int
47214263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
47224263d13fSGeorge Wilson {
47234263d13fSGeorge Wilson 	spa_t *spa;
47244263d13fSGeorge Wilson 	int error;
47254263d13fSGeorge Wilson 
47264263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
47273b2aab18SMatthew Ahrens 	if (error != 0)
47284263d13fSGeorge Wilson 		return (error);
47294263d13fSGeorge Wilson 
47304263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4731d6afdce2SGeorge Wilson 
4732d6afdce2SGeorge Wilson 	/*
4733d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4734d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4735d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4736d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4737d6afdce2SGeorge Wilson 	 */
4738d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
47394263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4740d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4741d6afdce2SGeorge Wilson 
47424263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
47434263d13fSGeorge Wilson 	spa_close(spa, FTAG);
47444263d13fSGeorge Wilson 	return (0);
47454263d13fSGeorge Wilson }
47463cb34c60Sahrens /*
47473cb34c60Sahrens  * inputs:
47483cb34c60Sahrens  * zc_name	name of filesystem
47493cb34c60Sahrens  * zc_value	name of origin snapshot
47503cb34c60Sahrens  *
4751681d9761SEric Taylor  * outputs:
4752681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
47533cb34c60Sahrens  */
4754ea8dc4b6Seschrock static int
475599653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
475699653d4eSeschrock {
47570b69c2f0Sahrens 	char *cp;
47580b69c2f0Sahrens 
47590b69c2f0Sahrens 	/*
47600b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
47610b69c2f0Sahrens 	 * it's easier.
47620b69c2f0Sahrens 	 */
4763e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
47640b69c2f0Sahrens 	if (cp)
47650b69c2f0Sahrens 		*cp = '\0';
4766e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
47673b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4768681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
476999653d4eSeschrock }
477099653d4eSeschrock 
4771ecd6cf80Smarks /*
477214843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
477314843421SMatthew Ahrens  *
477414843421SMatthew Ahrens  * inputs:
477514843421SMatthew Ahrens  * zc_name	name of filesystem
477614843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
477714843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
477814843421SMatthew Ahrens  * zc_guid	RID/UID/GID
477914843421SMatthew Ahrens  *
478014843421SMatthew Ahrens  * outputs:
478114843421SMatthew Ahrens  * zc_cookie	property value
478214843421SMatthew Ahrens  */
478314843421SMatthew Ahrens static int
478414843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
478514843421SMatthew Ahrens {
478614843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
478714843421SMatthew Ahrens 	int error;
478814843421SMatthew Ahrens 
478914843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4790be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
479114843421SMatthew Ahrens 
47921412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
47933b2aab18SMatthew Ahrens 	if (error != 0)
479414843421SMatthew Ahrens 		return (error);
479514843421SMatthew Ahrens 
479614843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
479714843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
479814843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
479914843421SMatthew Ahrens 
480014843421SMatthew Ahrens 	return (error);
480114843421SMatthew Ahrens }
480214843421SMatthew Ahrens 
480314843421SMatthew Ahrens /*
480414843421SMatthew Ahrens  * inputs:
480514843421SMatthew Ahrens  * zc_name		name of filesystem
480614843421SMatthew Ahrens  * zc_cookie		zap cursor
480714843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
480814843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
480914843421SMatthew Ahrens  *
481014843421SMatthew Ahrens  * outputs:
481114843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
481214843421SMatthew Ahrens  * zc_cookie	zap cursor
481314843421SMatthew Ahrens  */
481414843421SMatthew Ahrens static int
481514843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
481614843421SMatthew Ahrens {
481714843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4818eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
481914843421SMatthew Ahrens 
4820eeb85002STim Haley 	if (bufsize <= 0)
4821be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4822eeb85002STim Haley 
48231412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
48243b2aab18SMatthew Ahrens 	if (error != 0)
482514843421SMatthew Ahrens 		return (error);
482614843421SMatthew Ahrens 
482714843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
482814843421SMatthew Ahrens 
482914843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
483014843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
483114843421SMatthew Ahrens 
483214843421SMatthew Ahrens 	if (error == 0) {
483314843421SMatthew Ahrens 		error = xcopyout(buf,
483414843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
483514843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
483614843421SMatthew Ahrens 	}
483714843421SMatthew Ahrens 	kmem_free(buf, bufsize);
483814843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
483914843421SMatthew Ahrens 
484014843421SMatthew Ahrens 	return (error);
484114843421SMatthew Ahrens }
484214843421SMatthew Ahrens 
484314843421SMatthew Ahrens /*
484414843421SMatthew Ahrens  * inputs:
484514843421SMatthew Ahrens  * zc_name		name of filesystem
484614843421SMatthew Ahrens  *
484714843421SMatthew Ahrens  * outputs:
484814843421SMatthew Ahrens  * none
484914843421SMatthew Ahrens  */
485014843421SMatthew Ahrens static int
485114843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
485214843421SMatthew Ahrens {
485314843421SMatthew Ahrens 	objset_t *os;
48541195e687SMark J Musante 	int error = 0;
485514843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
485614843421SMatthew Ahrens 
485714843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4858503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
485914843421SMatthew Ahrens 			/*
486014843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
486114843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
486214843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
486314843421SMatthew Ahrens 			 */
4864503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
486591948b51SKeith M Wesolowski 			if (error == 0) {
486691948b51SKeith M Wesolowski 				dmu_objset_refresh_ownership(zfsvfs->z_os,
486791948b51SKeith M Wesolowski 				    zfsvfs);
4868503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
486914843421SMatthew Ahrens 			}
487091948b51SKeith M Wesolowski 		}
487114843421SMatthew Ahrens 		if (error == 0)
487214843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
487314843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
487414843421SMatthew Ahrens 	} else {
4875503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4876503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
48773b2aab18SMatthew Ahrens 		if (error != 0)
487814843421SMatthew Ahrens 			return (error);
487914843421SMatthew Ahrens 
488014843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4881503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
488214843421SMatthew Ahrens 	}
488314843421SMatthew Ahrens 
488414843421SMatthew Ahrens 	return (error);
488514843421SMatthew Ahrens }
488614843421SMatthew Ahrens 
488714843421SMatthew Ahrens /*
4888ecd6cf80Smarks  * We don't want to have a hard dependency
4889ecd6cf80Smarks  * against some special symbols in sharefs
4890da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4891ecd6cf80Smarks  * the first file system is shared.
4892da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4893ecd6cf80Smarks  */
4894da6c28aaSamw int (*znfsexport_fs)(void *arg);
4895ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4896da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4897ecd6cf80Smarks 
4898da6c28aaSamw int zfs_nfsshare_inited;
4899da6c28aaSamw int zfs_smbshare_inited;
4900da6c28aaSamw 
4901ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4902ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4903da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4904ecd6cf80Smarks kmutex_t zfs_share_lock;
4905ecd6cf80Smarks 
4906ecd6cf80Smarks static int
4907da6c28aaSamw zfs_init_sharefs()
4908da6c28aaSamw {
4909da6c28aaSamw 	int error;
4910da6c28aaSamw 
4911da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4912da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4913da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4914da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4915da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4916be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4917da6c28aaSamw 	}
4918da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4919da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4920da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4921be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4922da6c28aaSamw 	}
4923da6c28aaSamw 	return (0);
4924da6c28aaSamw }
4925da6c28aaSamw 
4926da6c28aaSamw static int
4927ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4928ecd6cf80Smarks {
4929ecd6cf80Smarks 	int error;
4930ecd6cf80Smarks 	int opcode;
4931ecd6cf80Smarks 
4932da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4933da6c28aaSamw 	case ZFS_SHARE_NFS:
4934da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4935da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4936ecd6cf80Smarks 			mutex_enter(&zfs_share_lock);
4937da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4938da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4939ecd6cf80Smarks 				mutex_exit(&zfs_share_lock);
4940be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4941ecd6cf80Smarks 			}
4942da6c28aaSamw 			if (znfsexport_fs == NULL &&
4943da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4944da6c28aaSamw 			    ddi_modsym(nfs_mod,
4945da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4946ecd6cf80Smarks 				mutex_exit(&zfs_share_lock);
4947be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4948ecd6cf80Smarks 			}
4949da6c28aaSamw 			error = zfs_init_sharefs();
49503b2aab18SMatthew Ahrens 			if (error != 0) {
4951da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4952be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4953da6c28aaSamw 			}
4954da6c28aaSamw 			zfs_nfsshare_inited = 1;
4955da6c28aaSamw 			mutex_exit(&zfs_share_lock);
4956da6c28aaSamw 		}
4957da6c28aaSamw 		break;
4958da6c28aaSamw 	case ZFS_SHARE_SMB:
4959da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4960da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4961da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4962da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4963da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4964da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4965da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4966be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4967da6c28aaSamw 			}
4968da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4969da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4970faa1795aSjb150015 			    "smb_server_share", &error)) == NULL)) {
4971da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4972be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4973da6c28aaSamw 			}
4974da6c28aaSamw 			error = zfs_init_sharefs();
49753b2aab18SMatthew Ahrens 			if (error != 0) {
4976da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4977be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4978da6c28aaSamw 			}
4979da6c28aaSamw 			zfs_smbshare_inited = 1;
4980da6c28aaSamw 			mutex_exit(&zfs_share_lock);
4981da6c28aaSamw 		}
4982da6c28aaSamw 		break;
4983da6c28aaSamw 	default:
4984be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4985da6c28aaSamw 	}
4986ecd6cf80Smarks 
4987da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4988da6c28aaSamw 	case ZFS_SHARE_NFS:
4989da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4990da6c28aaSamw 		if (error =
4991da6c28aaSamw 		    znfsexport_fs((void *)
4992da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4993ecd6cf80Smarks 			return (error);
4994da6c28aaSamw 		break;
4995da6c28aaSamw 	case ZFS_SHARE_SMB:
4996da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4997da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4998da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4999da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5000da6c28aaSamw 		    B_TRUE: B_FALSE)) {
5001da6c28aaSamw 			return (error);
5002da6c28aaSamw 		}
5003da6c28aaSamw 		break;
5004da6c28aaSamw 	}
5005ecd6cf80Smarks 
5006da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5007da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5008ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
5009ecd6cf80Smarks 
5010da6c28aaSamw 	/*
5011da6c28aaSamw 	 * Add or remove share from sharetab
5012da6c28aaSamw 	 */
5013ecd6cf80Smarks 	error = zshare_fs(opcode,
5014ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
5015ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
5016ecd6cf80Smarks 
5017ecd6cf80Smarks 	return (error);
5018ecd6cf80Smarks 
5019ecd6cf80Smarks }
5020ecd6cf80Smarks 
5021743a77edSAlan Wright ace_t full_access[] = {
5022743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5023743a77edSAlan Wright };
5024743a77edSAlan Wright 
5025743a77edSAlan Wright /*
502699d5e173STim Haley  * inputs:
502799d5e173STim Haley  * zc_name		name of containing filesystem
502899d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
502999d5e173STim Haley  *
503099d5e173STim Haley  * outputs:
503199d5e173STim Haley  * zc_obj		next in-use object #
503299d5e173STim Haley  */
503399d5e173STim Haley static int
503499d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
503599d5e173STim Haley {
503699d5e173STim Haley 	objset_t *os = NULL;
503799d5e173STim Haley 	int error;
503899d5e173STim Haley 
503999d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
50403b2aab18SMatthew Ahrens 	if (error != 0)
504199d5e173STim Haley 		return (error);
504299d5e173STim Haley 
504399d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5044c1379625SJustin T. Gibbs 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
504599d5e173STim Haley 
504699d5e173STim Haley 	dmu_objset_rele(os, FTAG);
504799d5e173STim Haley 	return (error);
504899d5e173STim Haley }
504999d5e173STim Haley 
505099d5e173STim Haley /*
505199d5e173STim Haley  * inputs:
505299d5e173STim Haley  * zc_name		name of filesystem
505399d5e173STim Haley  * zc_value		prefix name for snapshot
505499d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
505599d5e173STim Haley  *
505699d5e173STim Haley  * outputs:
50574445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
505899d5e173STim Haley  */
505999d5e173STim Haley static int
506099d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
506199d5e173STim Haley {
506299d5e173STim Haley 	char *snap_name;
50633b2aab18SMatthew Ahrens 	char *hold_name;
506499d5e173STim Haley 	int error;
50653b2aab18SMatthew Ahrens 	minor_t minor;
506699d5e173STim Haley 
50673b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
50683b2aab18SMatthew Ahrens 	if (error != 0)
506999d5e173STim Haley 		return (error);
507099d5e173STim Haley 
50713b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
50723b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
50733b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
50743b2aab18SMatthew Ahrens 
50753b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
50763b2aab18SMatthew Ahrens 	    hold_name);
50773b2aab18SMatthew Ahrens 	if (error == 0)
50783b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
507999d5e173STim Haley 	strfree(snap_name);
50803b2aab18SMatthew Ahrens 	strfree(hold_name);
50813b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
50823b2aab18SMatthew Ahrens 	return (error);
508399d5e173STim Haley }
508499d5e173STim Haley 
508599d5e173STim Haley /*
508699d5e173STim Haley  * inputs:
508799d5e173STim Haley  * zc_name		name of "to" snapshot
508899d5e173STim Haley  * zc_value		name of "from" snapshot
508999d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
509099d5e173STim Haley  *
509199d5e173STim Haley  * outputs:
509299d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
509399d5e173STim Haley  */
509499d5e173STim Haley static int
509599d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
509699d5e173STim Haley {
509799d5e173STim Haley 	file_t *fp;
509899d5e173STim Haley 	offset_t off;
509999d5e173STim Haley 	int error;
510099d5e173STim Haley 
510199d5e173STim Haley 	fp = getf(zc->zc_cookie);
51023b2aab18SMatthew Ahrens 	if (fp == NULL)
5103be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
510499d5e173STim Haley 
510599d5e173STim Haley 	off = fp->f_offset;
510699d5e173STim Haley 
51073b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
510899d5e173STim Haley 
510999d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
511099d5e173STim Haley 		fp->f_offset = off;
511199d5e173STim Haley 	releasef(zc->zc_cookie);
511299d5e173STim Haley 
511399d5e173STim Haley 	return (error);
511499d5e173STim Haley }
511599d5e173STim Haley 
511699d5e173STim Haley /*
5117743a77edSAlan Wright  * Remove all ACL files in shares dir
5118743a77edSAlan Wright  */
5119743a77edSAlan Wright static int
5120743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5121743a77edSAlan Wright {
5122743a77edSAlan Wright 	zap_cursor_t	zc;
5123743a77edSAlan Wright 	zap_attribute_t	zap;
5124743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5125743a77edSAlan Wright 	int error;
5126743a77edSAlan Wright 
5127743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5128743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5129743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
5130743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5131743a77edSAlan Wright 		    NULL, 0)) != 0)
5132743a77edSAlan Wright 			break;
5133743a77edSAlan Wright 	}
5134743a77edSAlan Wright 	zap_cursor_fini(&zc);
5135743a77edSAlan Wright 	return (error);
5136743a77edSAlan Wright }
5137743a77edSAlan Wright 
5138743a77edSAlan Wright static int
5139743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5140743a77edSAlan Wright {
5141743a77edSAlan Wright 	vnode_t *vp;
5142743a77edSAlan Wright 	znode_t *dzp;
5143743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
5144743a77edSAlan Wright 	znode_t *sharedir;
5145743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
5146743a77edSAlan Wright 	nvlist_t *nvlist;
5147743a77edSAlan Wright 	char *src, *target;
5148743a77edSAlan Wright 	vattr_t vattr;
5149743a77edSAlan Wright 	vsecattr_t vsec;
5150743a77edSAlan Wright 	int error = 0;
5151743a77edSAlan Wright 
5152743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5153743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
5154743a77edSAlan Wright 		return (error);
5155743a77edSAlan Wright 
5156743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
5157743a77edSAlan Wright 
5158743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5159743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5160743a77edSAlan Wright 	    zc->zc_name) != 0)) {
5161743a77edSAlan Wright 		VN_RELE(vp);
5162be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5163743a77edSAlan Wright 	}
5164743a77edSAlan Wright 
5165743a77edSAlan Wright 	dzp = VTOZ(vp);
5166743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
5167743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
5168743a77edSAlan Wright 
51699e1320c0SMark Shellenbaum 	/*
51709e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
51719e1320c0SMark Shellenbaum 	 */
51729e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
51739e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
51749e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
51759e1320c0SMark Shellenbaum 
51769e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
51779e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
51789e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
51799e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
51809e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
51813b2aab18SMatthew Ahrens 		if (error != 0) {
51829e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
51839e1320c0SMark Shellenbaum 		} else {
51849e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
51859e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
51869e1320c0SMark Shellenbaum 		}
51873b2aab18SMatthew Ahrens 		if (error != 0) {
51889e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
51899e1320c0SMark Shellenbaum 			VN_RELE(vp);
51909e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
51919e1320c0SMark Shellenbaum 			return (error);
51929e1320c0SMark Shellenbaum 		}
51939e1320c0SMark Shellenbaum 	}
51949e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
51959e1320c0SMark Shellenbaum 
51969e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5197743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
51989e1320c0SMark Shellenbaum 		VN_RELE(vp);
5199743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5200743a77edSAlan Wright 		return (error);
5201743a77edSAlan Wright 	}
5202743a77edSAlan Wright 
5203743a77edSAlan Wright 	switch (zc->zc_cookie) {
5204743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5205743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5206743a77edSAlan Wright 		vattr.va_type = VREG;
5207743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5208743a77edSAlan Wright 		vattr.va_uid = 0;
5209743a77edSAlan Wright 		vattr.va_gid = 0;
5210743a77edSAlan Wright 
5211743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5212743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5213743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5214743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5215743a77edSAlan Wright 
5216743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5217743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5218743a77edSAlan Wright 		if (resourcevp)
5219743a77edSAlan Wright 			VN_RELE(resourcevp);
5220743a77edSAlan Wright 		break;
5221743a77edSAlan Wright 
5222743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5223743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5224743a77edSAlan Wright 		    NULL, 0);
5225743a77edSAlan Wright 		break;
5226743a77edSAlan Wright 
5227743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5228743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5229478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5230743a77edSAlan Wright 			VN_RELE(vp);
52318f5190a5SDan McDonald 			VN_RELE(ZTOV(sharedir));
5232743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5233743a77edSAlan Wright 			return (error);
5234743a77edSAlan Wright 		}
5235743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5236743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5237743a77edSAlan Wright 		    &target)) {
5238743a77edSAlan Wright 			VN_RELE(vp);
523989459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5240743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
52411195e687SMark J Musante 			nvlist_free(nvlist);
5242743a77edSAlan Wright 			return (error);
5243743a77edSAlan Wright 		}
5244743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5245743a77edSAlan Wright 		    kcred, NULL, 0);
5246743a77edSAlan Wright 		nvlist_free(nvlist);
5247743a77edSAlan Wright 		break;
5248743a77edSAlan Wright 
5249743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5250743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5251743a77edSAlan Wright 		break;
5252743a77edSAlan Wright 
5253743a77edSAlan Wright 	default:
5254be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5255743a77edSAlan Wright 		break;
5256743a77edSAlan Wright 	}
5257743a77edSAlan Wright 
5258743a77edSAlan Wright 	VN_RELE(vp);
5259743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5260743a77edSAlan Wright 
5261743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5262743a77edSAlan Wright 
5263743a77edSAlan Wright 	return (error);
5264743a77edSAlan Wright }
5265743a77edSAlan Wright 
5266ecd6cf80Smarks /*
52673b2aab18SMatthew Ahrens  * innvl: {
52683b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
52693b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
52703b2aab18SMatthew Ahrens  * }
5271842727c2SChris Kirby  *
52723b2aab18SMatthew Ahrens  * outnvl: {
52733b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
52743b2aab18SMatthew Ahrens  *     ...
52753b2aab18SMatthew Ahrens  * }
5276842727c2SChris Kirby  */
52773b2aab18SMatthew Ahrens /* ARGSUSED */
5278842727c2SChris Kirby static int
52793b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5280842727c2SChris Kirby {
5281752fd8daSJosef 'Jeff' Sipek 	nvpair_t *pair;
52823b2aab18SMatthew Ahrens 	nvlist_t *holds;
52833b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5284a7f53a56SChris Kirby 	int error;
5285a7f53a56SChris Kirby 	minor_t minor = 0;
5286842727c2SChris Kirby 
52873b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
52883b2aab18SMatthew Ahrens 	if (error != 0)
5289be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5290842727c2SChris Kirby 
5291752fd8daSJosef 'Jeff' Sipek 	/* make sure the user didn't pass us any invalid (empty) tags */
5292752fd8daSJosef 'Jeff' Sipek 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5293752fd8daSJosef 'Jeff' Sipek 	    pair = nvlist_next_nvpair(holds, pair)) {
5294752fd8daSJosef 'Jeff' Sipek 		char *htag;
5295752fd8daSJosef 'Jeff' Sipek 
5296752fd8daSJosef 'Jeff' Sipek 		error = nvpair_value_string(pair, &htag);
5297752fd8daSJosef 'Jeff' Sipek 		if (error != 0)
5298752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(error));
5299752fd8daSJosef 'Jeff' Sipek 
5300752fd8daSJosef 'Jeff' Sipek 		if (strlen(htag) == 0)
5301752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(EINVAL));
5302752fd8daSJosef 'Jeff' Sipek 	}
5303752fd8daSJosef 'Jeff' Sipek 
53043b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
53053b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
53063b2aab18SMatthew Ahrens 		if (error != 0)
5307a7f53a56SChris Kirby 			return (error);
5308a7f53a56SChris Kirby 	}
5309a7f53a56SChris Kirby 
53103b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
53113b2aab18SMatthew Ahrens 	if (minor != 0)
53123b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5313a7f53a56SChris Kirby 	return (error);
5314842727c2SChris Kirby }
5315842727c2SChris Kirby 
5316842727c2SChris Kirby /*
53173b2aab18SMatthew Ahrens  * innvl is not used.
5318842727c2SChris Kirby  *
53193b2aab18SMatthew Ahrens  * outnvl: {
53203b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
53213b2aab18SMatthew Ahrens  *    ...
53223b2aab18SMatthew Ahrens  * }
5323842727c2SChris Kirby  */
53243b2aab18SMatthew Ahrens /* ARGSUSED */
5325842727c2SChris Kirby static int
53263b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5327842727c2SChris Kirby {
53283b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5329842727c2SChris Kirby }
5330842727c2SChris Kirby 
5331842727c2SChris Kirby /*
53323b2aab18SMatthew Ahrens  * innvl: {
53333b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
53343b2aab18SMatthew Ahrens  *     ...
53353b2aab18SMatthew Ahrens  * }
5336842727c2SChris Kirby  *
53373b2aab18SMatthew Ahrens  * outnvl: {
53383b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
53393b2aab18SMatthew Ahrens  *     ...
53403b2aab18SMatthew Ahrens  * }
5341842727c2SChris Kirby  */
53423b2aab18SMatthew Ahrens /* ARGSUSED */
5343842727c2SChris Kirby static int
53443b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5345842727c2SChris Kirby {
53463b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5347842727c2SChris Kirby }
5348842727c2SChris Kirby 
5349842727c2SChris Kirby /*
535019b94df9SMatthew Ahrens  * inputs:
535119b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
535219b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
535319b94df9SMatthew Ahrens  *
535419b94df9SMatthew Ahrens  * outputs:
535519b94df9SMatthew Ahrens  * zc_cookie		space in bytes
535619b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
535719b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
535819b94df9SMatthew Ahrens  */
535919b94df9SMatthew Ahrens static int
536019b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
536119b94df9SMatthew Ahrens {
536219b94df9SMatthew Ahrens 	int error;
53633b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
536419b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
536519b94df9SMatthew Ahrens 
53663b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
536719b94df9SMatthew Ahrens 	if (error != 0)
536819b94df9SMatthew Ahrens 		return (error);
53693b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
53703b2aab18SMatthew Ahrens 	if (error != 0) {
53713b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
53723b2aab18SMatthew Ahrens 		return (error);
53733b2aab18SMatthew Ahrens 	}
53743b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
537519b94df9SMatthew Ahrens 	if (error != 0) {
537619b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
53773b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
537819b94df9SMatthew Ahrens 		return (error);
537919b94df9SMatthew Ahrens 	}
538019b94df9SMatthew Ahrens 
538119b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
538219b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
538319b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
538419b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
53853b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
538619b94df9SMatthew Ahrens 	return (error);
538719b94df9SMatthew Ahrens }
53883b2aab18SMatthew Ahrens 
538919b94df9SMatthew Ahrens /*
53904445fffbSMatthew Ahrens  * innvl: {
53914445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
53924445fffbSMatthew Ahrens  * }
539319b94df9SMatthew Ahrens  *
53944445fffbSMatthew Ahrens  * outnvl: {
53954445fffbSMatthew Ahrens  *     "used" -> space in bytes
53964445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
53974445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
53984445fffbSMatthew Ahrens  * }
539919b94df9SMatthew Ahrens  */
540019b94df9SMatthew Ahrens static int
54014445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
540219b94df9SMatthew Ahrens {
540319b94df9SMatthew Ahrens 	int error;
54043b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
540519b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
54064445fffbSMatthew Ahrens 	char *firstsnap;
54074445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
540819b94df9SMatthew Ahrens 
54094445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5410be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
54114445fffbSMatthew Ahrens 
54123b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
541319b94df9SMatthew Ahrens 	if (error != 0)
541419b94df9SMatthew Ahrens 		return (error);
54153b2aab18SMatthew Ahrens 
54163b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
541724218bebSAndriy Gapon 	if (error == 0 && !new->ds_is_snapshot) {
541824218bebSAndriy Gapon 		dsl_dataset_rele(new, FTAG);
541924218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
542024218bebSAndriy Gapon 	}
54213b2aab18SMatthew Ahrens 	if (error != 0) {
54223b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
54233b2aab18SMatthew Ahrens 		return (error);
54243b2aab18SMatthew Ahrens 	}
54253b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
542624218bebSAndriy Gapon 	if (error == 0 && !old->ds_is_snapshot) {
542724218bebSAndriy Gapon 		dsl_dataset_rele(old, FTAG);
542824218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
542924218bebSAndriy Gapon 	}
543019b94df9SMatthew Ahrens 	if (error != 0) {
543119b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
54323b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
543319b94df9SMatthew Ahrens 		return (error);
543419b94df9SMatthew Ahrens 	}
543519b94df9SMatthew Ahrens 
54364445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
543719b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
543819b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
54393b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
54404445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
54414445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
54424445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
544319b94df9SMatthew Ahrens 	return (error);
544419b94df9SMatthew Ahrens }
544519b94df9SMatthew Ahrens 
544619b94df9SMatthew Ahrens /*
54474445fffbSMatthew Ahrens  * innvl: {
54484445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
54494445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
5450b5152584SMatthew Ahrens  *     (optional) "largeblockok" -> (value ignored)
5451b5152584SMatthew Ahrens  *         indicates that blocks > 128KB are permitted
54525d7b4d43SMatthew Ahrens  *     (optional) "embedok" -> (value ignored)
54535d7b4d43SMatthew Ahrens  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
54549c3fd121SMatthew Ahrens  *     (optional) "resume_object" and "resume_offset" -> (uint64)
54559c3fd121SMatthew Ahrens  *         if present, resume send stream from specified object and offset.
54564445fffbSMatthew Ahrens  * }
54574445fffbSMatthew Ahrens  *
54584445fffbSMatthew Ahrens  * outnvl is unused
5459ecd6cf80Smarks  */
54604445fffbSMatthew Ahrens /* ARGSUSED */
54614445fffbSMatthew Ahrens static int
54624445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
54634445fffbSMatthew Ahrens {
54644445fffbSMatthew Ahrens 	int error;
54654445fffbSMatthew Ahrens 	offset_t off;
54663b2aab18SMatthew Ahrens 	char *fromname = NULL;
54674445fffbSMatthew Ahrens 	int fd;
5468b5152584SMatthew Ahrens 	boolean_t largeblockok;
54695d7b4d43SMatthew Ahrens 	boolean_t embedok;
54709c3fd121SMatthew Ahrens 	uint64_t resumeobj = 0;
54719c3fd121SMatthew Ahrens 	uint64_t resumeoff = 0;
54724445fffbSMatthew Ahrens 
54734445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
54744445fffbSMatthew Ahrens 	if (error != 0)
5475be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
54764445fffbSMatthew Ahrens 
54773b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
54784445fffbSMatthew Ahrens 
5479b5152584SMatthew Ahrens 	largeblockok = nvlist_exists(innvl, "largeblockok");
54805d7b4d43SMatthew Ahrens 	embedok = nvlist_exists(innvl, "embedok");
54815d7b4d43SMatthew Ahrens 
54829c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
54839c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
54849c3fd121SMatthew Ahrens 
54854445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
54863b2aab18SMatthew Ahrens 	if (fp == NULL)
5487be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
54884445fffbSMatthew Ahrens 
54894445fffbSMatthew Ahrens 	off = fp->f_offset;
54909c3fd121SMatthew Ahrens 	error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
54919c3fd121SMatthew Ahrens 	    resumeobj, resumeoff, fp->f_vnode, &off);
54924445fffbSMatthew Ahrens 
54934445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
54944445fffbSMatthew Ahrens 		fp->f_offset = off;
54954445fffbSMatthew Ahrens 	releasef(fd);
54964445fffbSMatthew Ahrens 	return (error);
54974445fffbSMatthew Ahrens }
54984445fffbSMatthew Ahrens 
54994445fffbSMatthew Ahrens /*
55004445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
55014445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
55024445fffbSMatthew Ahrens  *
55034445fffbSMatthew Ahrens  * innvl: {
5504643da460SMax Grossman  *     (optional) "from" -> full snap or bookmark name to send an incremental
5505643da460SMax Grossman  *                          from
55064445fffbSMatthew Ahrens  * }
55074445fffbSMatthew Ahrens  *
55084445fffbSMatthew Ahrens  * outnvl: {
55094445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
55104445fffbSMatthew Ahrens  * }
55114445fffbSMatthew Ahrens  */
55124445fffbSMatthew Ahrens static int
55134445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
55144445fffbSMatthew Ahrens {
55153b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
55163b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
55174445fffbSMatthew Ahrens 	int error;
55184445fffbSMatthew Ahrens 	char *fromname;
55194445fffbSMatthew Ahrens 	uint64_t space;
55204445fffbSMatthew Ahrens 
55213b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
55223b2aab18SMatthew Ahrens 	if (error != 0)
55234445fffbSMatthew Ahrens 		return (error);
55244445fffbSMatthew Ahrens 
55253b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
55263b2aab18SMatthew Ahrens 	if (error != 0) {
55273b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
55283b2aab18SMatthew Ahrens 		return (error);
55293b2aab18SMatthew Ahrens 	}
55303b2aab18SMatthew Ahrens 
5531643da460SMax Grossman 	error = nvlist_lookup_string(innvl, "from", &fromname);
55324445fffbSMatthew Ahrens 	if (error == 0) {
5533643da460SMax Grossman 		if (strchr(fromname, '@') != NULL) {
5534643da460SMax Grossman 			/*
5535643da460SMax Grossman 			 * If from is a snapshot, hold it and use the more
5536643da460SMax Grossman 			 * efficient dmu_send_estimate to estimate send space
5537643da460SMax Grossman 			 * size using deadlists.
5538643da460SMax Grossman 			 */
5539643da460SMax Grossman 			dsl_dataset_t *fromsnap;
55403b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5541643da460SMax Grossman 			if (error != 0)
5542643da460SMax Grossman 				goto out;
5543643da460SMax Grossman 			error = dmu_send_estimate(tosnap, fromsnap, &space);
5544643da460SMax Grossman 			dsl_dataset_rele(fromsnap, FTAG);
5545643da460SMax Grossman 		} else if (strchr(fromname, '#') != NULL) {
5546643da460SMax Grossman 			/*
5547643da460SMax Grossman 			 * If from is a bookmark, fetch the creation TXG of the
5548643da460SMax Grossman 			 * snapshot it was created from and use that to find
5549643da460SMax Grossman 			 * blocks that were born after it.
5550643da460SMax Grossman 			 */
5551643da460SMax Grossman 			zfs_bookmark_phys_t frombm;
5552643da460SMax Grossman 
5553643da460SMax Grossman 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
5554643da460SMax Grossman 			    &frombm);
5555643da460SMax Grossman 			if (error != 0)
5556643da460SMax Grossman 				goto out;
5557643da460SMax Grossman 			error = dmu_send_estimate_from_txg(tosnap,
5558643da460SMax Grossman 			    frombm.zbm_creation_txg, &space);
5559643da460SMax Grossman 		} else {
5560643da460SMax Grossman 			/*
5561643da460SMax Grossman 			 * from is not properly formatted as a snapshot or
5562643da460SMax Grossman 			 * bookmark
5563643da460SMax Grossman 			 */
5564643da460SMax Grossman 			error = SET_ERROR(EINVAL);
5565643da460SMax Grossman 			goto out;
55664445fffbSMatthew Ahrens 		}
5567643da460SMax Grossman 	} else {
5568643da460SMax Grossman 		// If estimating the size of a full send, use dmu_send_estimate
5569643da460SMax Grossman 		error = dmu_send_estimate(tosnap, NULL, &space);
55704445fffbSMatthew Ahrens 	}
55714445fffbSMatthew Ahrens 
55724445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
55734445fffbSMatthew Ahrens 
5574643da460SMax Grossman out:
55753b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
55763b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
55774445fffbSMatthew Ahrens 	return (error);
55784445fffbSMatthew Ahrens }
55794445fffbSMatthew Ahrens 
5580d78b796cSAndreas Jaekel static int
5581d78b796cSAndreas Jaekel zfs_ioc_set_zev_callbacks(const char *unused, nvlist_t *innvl,
5582d78b796cSAndreas Jaekel     nvlist_t *outnvl)
5583d78b796cSAndreas Jaekel {
5584d78b796cSAndreas Jaekel 	int error;
5585d78b796cSAndreas Jaekel 	uint64_t cb_addr;
5586d78b796cSAndreas Jaekel 	/*
5587d78b796cSAndreas Jaekel 	 * Our secpolicy for this op makes sure it's called in
5588d78b796cSAndreas Jaekel 	 * kernel context, and that no other callbacks have
5589d78b796cSAndreas Jaekel 	 * been registered, yet.
5590d78b796cSAndreas Jaekel 	 */
5591d78b796cSAndreas Jaekel 	error = nvlist_lookup_uint64(innvl, "callbacks", &cb_addr);
5592d78b796cSAndreas Jaekel 	if (error != 0) {
5593d78b796cSAndreas Jaekel 		cmn_err(CE_WARN, "set_zev_callbacks nvlist lookup failed (%d)",
5594d78b796cSAndreas Jaekel 		    error);
5595d78b796cSAndreas Jaekel 		return (error);
5596d78b796cSAndreas Jaekel 	}
5597d78b796cSAndreas Jaekel 	/* cb_addr is always a kernel memory address */
5598d78b796cSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_WRITER);
5599d78b796cSAndreas Jaekel 	if (rz_zev_callbacks != rz_zev_default_callbacks) {
5600d78b796cSAndreas Jaekel 		rw_exit(&rz_zev_rwlock);
5601d78b796cSAndreas Jaekel 		return (EBUSY);
5602d78b796cSAndreas Jaekel 	}
5603d78b796cSAndreas Jaekel 	rz_zev_callbacks = (void *)(uintptr_t)cb_addr;
5604d78b796cSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
5605d78b796cSAndreas Jaekel 	return (0);
5606d78b796cSAndreas Jaekel }
5607d78b796cSAndreas Jaekel 
5608d78b796cSAndreas Jaekel static int
5609d78b796cSAndreas Jaekel zfs_ioc_unset_zev_callbacks(const char *unused, nvlist_t *innvl,
5610d78b796cSAndreas Jaekel     nvlist_t *outnvl)
5611d78b796cSAndreas Jaekel {
5612d78b796cSAndreas Jaekel 	/*
5613d78b796cSAndreas Jaekel 	 * Our secpolicy for this op makes sure it's called in
5614d78b796cSAndreas Jaekel 	 * kernel context.
5615d78b796cSAndreas Jaekel 	 */
5616d78b796cSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_WRITER);
5617d78b796cSAndreas Jaekel 	rz_zev_callbacks = rz_zev_default_callbacks;
5618d78b796cSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
5619d78b796cSAndreas Jaekel 	/* after mutex release, no thread is using the old table anymore. */
5620d78b796cSAndreas Jaekel 	return (0);
5621d78b796cSAndreas Jaekel }
5622d78b796cSAndreas Jaekel 
56234445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
56244445fffbSMatthew Ahrens 
56254445fffbSMatthew Ahrens static void
56264445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56274445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
56284445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
56294445fffbSMatthew Ahrens {
56304445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
56314445fffbSMatthew Ahrens 
56324445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
56334445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
56344445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
56354445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
56364445fffbSMatthew Ahrens 
56374445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
56384445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
56394445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
56404445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
56414445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
56424445fffbSMatthew Ahrens }
56434445fffbSMatthew Ahrens 
56444445fffbSMatthew Ahrens /*
56454445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
56464445fffbSMatthew Ahrens  * each argument to this function.
56474445fffbSMatthew Ahrens  */
56484445fffbSMatthew Ahrens static void
56494445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
56504445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
56514445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
56524445fffbSMatthew Ahrens     boolean_t allow_log)
56534445fffbSMatthew Ahrens {
56544445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
56554445fffbSMatthew Ahrens 
56564445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
56574445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
56584445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
56594445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
56604445fffbSMatthew Ahrens 
56614445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
56624445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
56634445fffbSMatthew Ahrens 
56644445fffbSMatthew Ahrens 	vec->zvec_name = name;
56654445fffbSMatthew Ahrens 	vec->zvec_func = func;
56664445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
56674445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
56684445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
56694445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
56704445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
56714445fffbSMatthew Ahrens }
56724445fffbSMatthew Ahrens 
56734445fffbSMatthew Ahrens static void
56744445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56754445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
56764445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
56774445fffbSMatthew Ahrens {
56784445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56794445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
56804445fffbSMatthew Ahrens }
56814445fffbSMatthew Ahrens 
56824445fffbSMatthew Ahrens static void
56834445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56844445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
56854445fffbSMatthew Ahrens {
56864445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56874445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
56884445fffbSMatthew Ahrens }
56894445fffbSMatthew Ahrens 
56904445fffbSMatthew Ahrens static void
56914445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
56924445fffbSMatthew Ahrens {
56934445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
56944445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
56954445fffbSMatthew Ahrens }
56964445fffbSMatthew Ahrens 
56974445fffbSMatthew Ahrens static void
56984445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56994445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
57004445fffbSMatthew Ahrens {
57014445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
57024445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
57034445fffbSMatthew Ahrens }
57044445fffbSMatthew Ahrens 
57054445fffbSMatthew Ahrens static void
57064445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
57074445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
57084445fffbSMatthew Ahrens {
57094445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
57104445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
57114445fffbSMatthew Ahrens }
57124445fffbSMatthew Ahrens 
57134445fffbSMatthew Ahrens static void
57144445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
57154445fffbSMatthew Ahrens {
57164445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
57174445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
57184445fffbSMatthew Ahrens }
57194445fffbSMatthew Ahrens 
57204445fffbSMatthew Ahrens static void
57214445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
57224445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
57234445fffbSMatthew Ahrens {
57244445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
57254445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
57264445fffbSMatthew Ahrens }
57274445fffbSMatthew Ahrens 
57284445fffbSMatthew Ahrens static void
57294445fffbSMatthew Ahrens zfs_ioctl_init(void)
57304445fffbSMatthew Ahrens {
57314445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
57324445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
57334445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57344445fffbSMatthew Ahrens 
57354445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
57364445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
57374445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
57384445fffbSMatthew Ahrens 
57394445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
57404445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
57414445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57424445fffbSMatthew Ahrens 
57434445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
57444445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
57454445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57464445fffbSMatthew Ahrens 
57474445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
57484445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
57494445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57504445fffbSMatthew Ahrens 
57514445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
57524445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
57534445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57544445fffbSMatthew Ahrens 
57554445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
57564445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
57574445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57584445fffbSMatthew Ahrens 
57594445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
57604445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
57614445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57624445fffbSMatthew Ahrens 
57633b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
57643b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
57653b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57663b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
57673b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
57683b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
57693b2aab18SMatthew Ahrens 
57703b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
57713b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
57723b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
57733b2aab18SMatthew Ahrens 
5774d78b796cSAndreas Jaekel 	zfs_ioctl_register("set_zev_callbacks", ZFS_IOC_SET_ZEV_CALLBACKS,
5775d78b796cSAndreas Jaekel 	    zfs_ioc_set_zev_callbacks, zfs_secpolicy_set_zev_callbacks, NO_NAME,
5776d78b796cSAndreas Jaekel 	    POOL_CHECK_NONE, B_TRUE, B_FALSE);
5777d78b796cSAndreas Jaekel 
5778d78b796cSAndreas Jaekel 	zfs_ioctl_register("unset_zev_callbacks", ZFS_IOC_UNSET_ZEV_CALLBACKS,
5779d78b796cSAndreas Jaekel 	    zfs_ioc_unset_zev_callbacks, zfs_secpolicy_unset_zev_callbacks,
5780d78b796cSAndreas Jaekel 	    NO_NAME, POOL_CHECK_NONE, B_TRUE, B_FALSE);
5781a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5782a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5783a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5784a7027df1SMatthew Ahrens 
578578f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
578678f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
578778f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
578878f17100SMatthew Ahrens 
578978f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
579078f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
579178f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
579278f17100SMatthew Ahrens 
579378f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
579478f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
579578f17100SMatthew Ahrens 	    POOL_NAME,
579678f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
579778f17100SMatthew Ahrens 
57984445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
57994445fffbSMatthew Ahrens 
58004445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
58014445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
58024445fffbSMatthew Ahrens 
5803ce0d9371SArne Jansen 	zfs_ioctl_register_legacy(ZFS_IOC_ARC_INFO, zfs_ioc_arc_info,
5804ce0d9371SArne Jansen 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5805ce0d9371SArne Jansen 
58064445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
58074445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58084445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
58094445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
58104445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
58114445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
58124445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
58134445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
58144445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
58154445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
58164445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
58174445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
58184445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
58194445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
58204445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
58214445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
58224445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
58234445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
58244445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
58254445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
58264445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
58274445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
58284445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
58294445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
58304445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
58314445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
58324445fffbSMatthew Ahrens 
58334445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
58344445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
58354445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
58364445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
58374445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
58384445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
58394445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
58404445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
58414445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
58424445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
58434445fffbSMatthew Ahrens 
58444445fffbSMatthew Ahrens 	/*
58454445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
58464445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
58474445fffbSMatthew Ahrens 	 * does the logging of those commands.
58484445fffbSMatthew Ahrens 	 */
58494445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
58504445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
58514445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
58524445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
58534445fffbSMatthew Ahrens 
58544445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
58554445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
58564445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
58574445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
58584445fffbSMatthew Ahrens 
58594445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
58604445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
58614445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
58624445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
58634445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
58644445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
58654445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
58664445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
58674445fffbSMatthew Ahrens 
58684445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
58694445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58704445fffbSMatthew Ahrens 
58714445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
587222e30981SGeorge Wilson 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
58734445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
58744445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
58754445fffbSMatthew Ahrens 
58764445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
58774445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
58784445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
58794445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
58804445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
58814445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
58824445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
58834445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
58844445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
58854445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
58864445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
58874445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
58884445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
58894445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
58904445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
58914445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
58924445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
58934445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
58944445fffbSMatthew Ahrens 
58954445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
58964445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
58974445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
58984445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
58994445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
59004445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
59014445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
59024445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
59034445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
59044445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
59054445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
59064445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
59074445fffbSMatthew Ahrens 
59084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
59094445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
59104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
59114445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
59124445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
59134445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
59144445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
59154445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
59164445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
59174445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
59184445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
59194445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
59204445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
59214445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
59224445fffbSMatthew Ahrens 
59234445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
59244445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
59254445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
59264445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
59274445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
59284445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
59294445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
59304445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
59314445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
59324445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
59334445fffbSMatthew Ahrens }
5934fa9e4066Sahrens 
593554d692b7SGeorge Wilson int
5936f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5937f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
593854d692b7SGeorge Wilson {
593954d692b7SGeorge Wilson 	spa_t *spa;
594054d692b7SGeorge Wilson 	int error;
594154d692b7SGeorge Wilson 
594254d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
594354d692b7SGeorge Wilson 
5944f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5945f9af39baSGeorge Wilson 		return (0);
5946f9af39baSGeorge Wilson 
594714843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
594854d692b7SGeorge Wilson 	if (error == 0) {
5949f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5950be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5951f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5952be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
595354d692b7SGeorge Wilson 		spa_close(spa, FTAG);
595454d692b7SGeorge Wilson 	}
595554d692b7SGeorge Wilson 	return (error);
595654d692b7SGeorge Wilson }
595754d692b7SGeorge Wilson 
5958c99e4bdcSChris Kirby /*
5959c99e4bdcSChris Kirby  * Find a free minor number.
5960c99e4bdcSChris Kirby  */
5961c99e4bdcSChris Kirby minor_t
5962c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5963c99e4bdcSChris Kirby {
5964c99e4bdcSChris Kirby 	static minor_t last_minor;
5965c99e4bdcSChris Kirby 	minor_t m;
5966c99e4bdcSChris Kirby 
5967c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5968c99e4bdcSChris Kirby 
5969c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5970c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5971c99e4bdcSChris Kirby 			m = 1;
5972c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5973c99e4bdcSChris Kirby 			last_minor = m;
5974c99e4bdcSChris Kirby 			return (m);
5975c99e4bdcSChris Kirby 		}
5976c99e4bdcSChris Kirby 	}
5977c99e4bdcSChris Kirby 
5978c99e4bdcSChris Kirby 	return (0);
5979c99e4bdcSChris Kirby }
5980c99e4bdcSChris Kirby 
5981c99e4bdcSChris Kirby static int
5982c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5983c99e4bdcSChris Kirby {
5984c99e4bdcSChris Kirby 	minor_t minor;
5985c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5986c99e4bdcSChris Kirby 
5987c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5988c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5989c99e4bdcSChris Kirby 
5990c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5991c99e4bdcSChris Kirby 	if (minor == 0)
5992be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5993c99e4bdcSChris Kirby 
5994c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5995be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5996c99e4bdcSChris Kirby 
5997c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5998c99e4bdcSChris Kirby 
5999c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
6000c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
6001c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6002c99e4bdcSChris Kirby 
6003c99e4bdcSChris Kirby 	return (0);
6004c99e4bdcSChris Kirby }
6005c99e4bdcSChris Kirby 
6006c99e4bdcSChris Kirby static void
6007c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6008c99e4bdcSChris Kirby {
6009c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6010c99e4bdcSChris Kirby 
6011c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
6012c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
6013c99e4bdcSChris Kirby }
6014c99e4bdcSChris Kirby 
6015c99e4bdcSChris Kirby void *
6016c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6017c99e4bdcSChris Kirby {
6018c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
6019c99e4bdcSChris Kirby 
6020c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
6021c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
6022c99e4bdcSChris Kirby 		return (NULL);
6023c99e4bdcSChris Kirby 
6024c99e4bdcSChris Kirby 	return (zp->zss_data);
6025c99e4bdcSChris Kirby }
6026c99e4bdcSChris Kirby 
6027c99e4bdcSChris Kirby static int
6028c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6029c99e4bdcSChris Kirby {
6030c99e4bdcSChris Kirby 	int error = 0;
6031c99e4bdcSChris Kirby 
6032c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
6033c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
6034c99e4bdcSChris Kirby 
6035c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
6036c99e4bdcSChris Kirby 	if (flag & FEXCL) {
6037c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
6038c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
6039c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6040c99e4bdcSChris Kirby 	}
6041c99e4bdcSChris Kirby 
6042c99e4bdcSChris Kirby 	return (error);
6043c99e4bdcSChris Kirby }
6044c99e4bdcSChris Kirby 
6045c99e4bdcSChris Kirby static int
6046c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6047c99e4bdcSChris Kirby {
6048c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
6049c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
6050c99e4bdcSChris Kirby 
6051c99e4bdcSChris Kirby 	if (minor == 0)
6052c99e4bdcSChris Kirby 		return (0);
6053c99e4bdcSChris Kirby 
6054c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
6055c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6056c99e4bdcSChris Kirby 	if (zo == NULL) {
6057c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6058c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
6059c99e4bdcSChris Kirby 	}
6060c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
6061c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
6062c99e4bdcSChris Kirby 
6063c99e4bdcSChris Kirby 	return (0);
6064c99e4bdcSChris Kirby }
6065c99e4bdcSChris Kirby 
6066fa9e4066Sahrens static int
6067fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6068fa9e4066Sahrens {
6069fa9e4066Sahrens 	zfs_cmd_t *zc;
60704445fffbSMatthew Ahrens 	uint_t vecnum;
60714445fffbSMatthew Ahrens 	int error, rc, len;
6072c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
60734445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
60744445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
60754445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
6076fa9e4066Sahrens 
6077c99e4bdcSChris Kirby 	if (minor != 0 &&
6078c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6079fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6080fa9e4066Sahrens 
60814445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
608291ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6083fa9e4066Sahrens 
60844445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6085be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
60864445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
6087fa9e4066Sahrens 
6088fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6089fa9e4066Sahrens 
6090478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
60914445fffbSMatthew Ahrens 	if (error != 0) {
6092be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
60934445fffbSMatthew Ahrens 		goto out;
60944445fffbSMatthew Ahrens 	}
6095fa9e4066Sahrens 
60964445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
60974445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
60984445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
60994445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
61004445fffbSMatthew Ahrens 		if (error != 0)
61014445fffbSMatthew Ahrens 			goto out;
61024445fffbSMatthew Ahrens 	}
6103fa9e4066Sahrens 
6104fa9e4066Sahrens 	/*
6105fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
6106fa9e4066Sahrens 	 * the lower layers.
6107fa9e4066Sahrens 	 */
6108fa9e4066Sahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
61094445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
6110e7437265Sahrens 	case POOL_NAME:
6111fa9e4066Sahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6112be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
61134445fffbSMatthew Ahrens 		else
611454d692b7SGeorge Wilson 			error = pool_status_check(zc->zc_name,
61154445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
6116fa9e4066Sahrens 		break;
6117fa9e4066Sahrens 
6118e7437265Sahrens 	case DATASET_NAME:
611923962479SMarcel Telka 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6120be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
61214445fffbSMatthew Ahrens 		else
612254d692b7SGeorge Wilson 			error = pool_status_check(zc->zc_name,
61234445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
6124fa9e4066Sahrens 		break;
61255ad82045Snd150628 
6126e7437265Sahrens 	case NO_NAME:
61275ad82045Snd150628 		break;
6128fa9e4066Sahrens 	}
61294445fffbSMatthew Ahrens 
61304445fffbSMatthew Ahrens 
61314445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
61324445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
61334445fffbSMatthew Ahrens 
61344445fffbSMatthew Ahrens 	if (error != 0)
61354445fffbSMatthew Ahrens 		goto out;
61364445fffbSMatthew Ahrens 
61374445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
613878f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
61394445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
61404445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
61414445fffbSMatthew Ahrens 
61424445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
61434445fffbSMatthew Ahrens 		nvlist_t *outnvl;
61444445fffbSMatthew Ahrens 		int puterror = 0;
61454445fffbSMatthew Ahrens 		spa_t *spa;
61464445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
61474445fffbSMatthew Ahrens 
61484445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
61494445fffbSMatthew Ahrens 
61504445fffbSMatthew Ahrens 		/*
61514445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
61524445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
61534445fffbSMatthew Ahrens 		 */
61544445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
61554445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
61564445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
61574445fffbSMatthew Ahrens 			    vec->zvec_name);
61584445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
61594445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
61604445fffbSMatthew Ahrens 				    innvl);
61614445fffbSMatthew Ahrens 			}
6162fa9e4066Sahrens 		}
6163fa9e4066Sahrens 
61644445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
61654445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6166fa9e4066Sahrens 
61674445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
61684445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
61694445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
61704445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
61714445fffbSMatthew Ahrens 				    outnvl);
61724445fffbSMatthew Ahrens 			}
61734445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
61744445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
61754445fffbSMatthew Ahrens 		}
61764445fffbSMatthew Ahrens 		fnvlist_free(lognv);
61774445fffbSMatthew Ahrens 
61784445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
61794445fffbSMatthew Ahrens 			int smusherror = 0;
61804445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
61814445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
61824445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
61834445fffbSMatthew Ahrens 			}
61844445fffbSMatthew Ahrens 			if (smusherror == 0)
61854445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
61864445fffbSMatthew Ahrens 		}
61874445fffbSMatthew Ahrens 
61884445fffbSMatthew Ahrens 		if (puterror != 0)
61894445fffbSMatthew Ahrens 			error = puterror;
61904445fffbSMatthew Ahrens 
61914445fffbSMatthew Ahrens 		nvlist_free(outnvl);
61924445fffbSMatthew Ahrens 	} else {
61934445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
61944445fffbSMatthew Ahrens 	}
61954445fffbSMatthew Ahrens 
61964445fffbSMatthew Ahrens out:
61974445fffbSMatthew Ahrens 	nvlist_free(innvl);
6198478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
61994445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
6200be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
62014445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
62024445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
62034445fffbSMatthew Ahrens 		if (s != NULL)
62044445fffbSMatthew Ahrens 			strfree(s);
62054445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
62064445fffbSMatthew Ahrens 	} else {
62074445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
62084445fffbSMatthew Ahrens 			strfree(saved_poolname);
6209ecd6cf80Smarks 	}
6210fa9e4066Sahrens 
6211fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
6212fa9e4066Sahrens 	return (error);
6213fa9e4066Sahrens }
6214fa9e4066Sahrens 
6215fa9e4066Sahrens static int
6216fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6217fa9e4066Sahrens {
6218fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
6219fa9e4066Sahrens 		return (DDI_FAILURE);
6220fa9e4066Sahrens 
6221fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6222fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
6223fa9e4066Sahrens 		return (DDI_FAILURE);
6224fa9e4066Sahrens 
6225fa9e4066Sahrens 	zfs_dip = dip;
6226fa9e4066Sahrens 
6227fa9e4066Sahrens 	ddi_report_dev(dip);
6228fa9e4066Sahrens 
6229fa9e4066Sahrens 	return (DDI_SUCCESS);
6230fa9e4066Sahrens }
6231fa9e4066Sahrens 
6232fa9e4066Sahrens static int
6233fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6234fa9e4066Sahrens {
6235fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
6236fa9e4066Sahrens 		return (DDI_FAILURE);
6237fa9e4066Sahrens 
6238fa9e4066Sahrens 	if (cmd != DDI_DETACH)
6239fa9e4066Sahrens 		return (DDI_FAILURE);
6240fa9e4066Sahrens 
6241fa9e4066Sahrens 	zfs_dip = NULL;
6242fa9e4066Sahrens 
6243fa9e4066Sahrens 	ddi_prop_remove_all(dip);
6244fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
6245fa9e4066Sahrens 
6246fa9e4066Sahrens 	return (DDI_SUCCESS);
6247fa9e4066Sahrens }
6248fa9e4066Sahrens 
6249fa9e4066Sahrens /*ARGSUSED*/
6250fa9e4066Sahrens static int
6251fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6252fa9e4066Sahrens {
6253fa9e4066Sahrens 	switch (infocmd) {
6254fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
6255fa9e4066Sahrens 		*result = zfs_dip;
6256fa9e4066Sahrens 		return (DDI_SUCCESS);
6257fa9e4066Sahrens 
6258fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
6259a0965f35Sbonwick 		*result = (void *)0;
6260fa9e4066Sahrens 		return (DDI_SUCCESS);
6261fa9e4066Sahrens 	}
6262fa9e4066Sahrens 
6263fa9e4066Sahrens 	return (DDI_FAILURE);
6264fa9e4066Sahrens }
6265fa9e4066Sahrens 
6266fa9e4066Sahrens /*
6267fa9e4066Sahrens  * OK, so this is a little weird.
6268fa9e4066Sahrens  *
6269fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
6270fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6271fa9e4066Sahrens  *
6272fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
6273fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
6274fa9e4066Sahrens  */
6275fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
6276c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
6277c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
6278fa9e4066Sahrens 	zvol_strategy,	/* strategy */
6279fa9e4066Sahrens 	nodev,		/* print */
6280e7cbe64fSgw25295 	zvol_dump,	/* dump */
6281fa9e4066Sahrens 	zvol_read,	/* read */
6282fa9e4066Sahrens 	zvol_write,	/* write */
6283fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
6284fa9e4066Sahrens 	nodev,		/* devmap */
6285fa9e4066Sahrens 	nodev,		/* mmap */
6286fa9e4066Sahrens 	nodev,		/* segmap */
6287fa9e4066Sahrens 	nochpoll,	/* poll */
6288fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
6289fa9e4066Sahrens 	NULL,		/* streamtab */
6290fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
6291fa9e4066Sahrens 	CB_REV,		/* version */
6292feb08c6bSbillm 	nodev,		/* async read */
6293feb08c6bSbillm 	nodev,		/* async write */
6294fa9e4066Sahrens };
6295fa9e4066Sahrens 
6296fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
6297fa9e4066Sahrens 	DEVO_REV,	/* version */
6298fa9e4066Sahrens 	0,		/* refcnt */
6299fa9e4066Sahrens 	zfs_info,	/* info */
6300fa9e4066Sahrens 	nulldev,	/* identify */
6301fa9e4066Sahrens 	nulldev,	/* probe */
6302fa9e4066Sahrens 	zfs_attach,	/* attach */
6303fa9e4066Sahrens 	zfs_detach,	/* detach */
6304fa9e4066Sahrens 	nodev,		/* reset */
6305fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
630619397407SSherry Moore 	NULL,		/* no bus operations */
630719397407SSherry Moore 	NULL,		/* power */
630819397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
6309fa9e4066Sahrens };
6310fa9e4066Sahrens 
6311fa9e4066Sahrens static struct modldrv zfs_modldrv = {
631219397407SSherry Moore 	&mod_driverops,
631319397407SSherry Moore 	"ZFS storage pool",
6314e9dbad6fSeschrock 	&zfs_dev_ops
6315fa9e4066Sahrens };
6316fa9e4066Sahrens 
6317fa9e4066Sahrens static struct modlinkage modlinkage = {
6318fa9e4066Sahrens 	MODREV_1,
6319fa9e4066Sahrens 	(void *)&zfs_modlfs,
6320fa9e4066Sahrens 	(void *)&zfs_modldrv,
6321fa9e4066Sahrens 	NULL
6322fa9e4066Sahrens };
6323fa9e4066Sahrens 
63244445fffbSMatthew Ahrens static void
63254445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
63264445fffbSMatthew Ahrens {
63274445fffbSMatthew Ahrens 	char *poolname = arg;
63284445fffbSMatthew Ahrens 	strfree(poolname);
63294445fffbSMatthew Ahrens }
6330ec533521Sfr157268 
6331fa9e4066Sahrens int
6332fa9e4066Sahrens _init(void)
6333fa9e4066Sahrens {
6334fa9e4066Sahrens 	int error;
6335fa9e4066Sahrens 
6336fa9e4066Sahrens 	spa_init(FREAD | FWRITE);
6337fa9e4066Sahrens 	zfs_init();
6338fa9e4066Sahrens 	zvol_init();
63394445fffbSMatthew Ahrens 	zfs_ioctl_init();
6340d78b796cSAndreas Jaekel 	rz_zev_init();
6341fa9e4066Sahrens 
6342a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
6343a0965f35Sbonwick 		zvol_fini();
6344a0965f35Sbonwick 		zfs_fini();
6345a0965f35Sbonwick 		spa_fini();
6346a0965f35Sbonwick 		return (error);
6347a0965f35Sbonwick 	}
6348a0965f35Sbonwick 
6349ec533521Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
63504445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
63514445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6352ec533521Sfr157268 
6353a0965f35Sbonwick 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6354a0965f35Sbonwick 	ASSERT(error == 0);
6355ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6356a0965f35Sbonwick 
6357fa9e4066Sahrens 	return (0);
6358fa9e4066Sahrens }
6359fa9e4066Sahrens 
6360fa9e4066Sahrens int
6361fa9e4066Sahrens _fini(void)
6362fa9e4066Sahrens {
6363fa9e4066Sahrens 	int error;
6364fa9e4066Sahrens 
6365ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6366be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6367fa9e4066Sahrens 
6368fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
6369fa9e4066Sahrens 		return (error);
6370fa9e4066Sahrens 
6371d78b796cSAndreas Jaekel 	rz_zev_fini();
6372fa9e4066Sahrens 	zvol_fini();
6373fa9e4066Sahrens 	zfs_fini();
6374fa9e4066Sahrens 	spa_fini();
6375da6c28aaSamw 	if (zfs_nfsshare_inited)
6376ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
6377da6c28aaSamw 	if (zfs_smbshare_inited)
6378da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
6379da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
6380ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
6381fa9e4066Sahrens 
6382ec533521Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
6383fa9e4066Sahrens 	ldi_ident_release(zfs_li);
6384fa9e4066Sahrens 	zfs_li = NULL;
6385ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
6386fa9e4066Sahrens 
6387fa9e4066Sahrens 	return (error);
6388fa9e4066Sahrens }
6389fa9e4066Sahrens 
6390fa9e4066Sahrens int
6391fa9e4066Sahrens _info(struct modinfo *modinfop)
6392fa9e4066Sahrens {
6393fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
6394fa9e4066Sahrens }
6395