xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision e2dcee5754c56d91c6e1ff847db294541069ca0d)
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
25752fd8daSJosef 'Jeff' Sipek  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
26a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
27*a1988827SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
28a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
304445fffbSMatthew Ahrens  */
314445fffbSMatthew Ahrens 
324445fffbSMatthew Ahrens /*
334445fffbSMatthew Ahrens  * ZFS ioctls.
344445fffbSMatthew Ahrens  *
354445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
364445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
374445fffbSMatthew Ahrens  *
384445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
394445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
404445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
414445fffbSMatthew Ahrens  *
424445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
434445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
444445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
454445fffbSMatthew Ahrens  *
464445fffbSMatthew Ahrens  * The registration arguments are as follows:
474445fffbSMatthew Ahrens  *
484445fffbSMatthew Ahrens  * const char *name
494445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
504445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
514445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
524445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
534445fffbSMatthew Ahrens  *
544445fffbSMatthew Ahrens  * zfs_ioc_t ioc
554445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
564445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
574445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
584445fffbSMatthew Ahrens  *
594445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
604445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
614445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
624445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
634445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
644445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
654445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
664445fffbSMatthew Ahrens  *
674445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
684445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
694445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
704445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
714445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
724445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
734445fffbSMatthew Ahrens  *   doesn't have invalid characters).
744445fffbSMatthew Ahrens  *
754445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
764445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
774445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
784445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
794445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
804445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
814445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
824445fffbSMatthew Ahrens  *
834445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
844445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
854445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
864445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
874445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
884445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
894445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
904445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
914445fffbSMatthew Ahrens  *
924445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
934445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
944445fffbSMatthew Ahrens  *
954445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
964445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
974445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
984445fffbSMatthew Ahrens  *   will be called with the following arguments:
994445fffbSMatthew Ahrens  *
1004445fffbSMatthew Ahrens  *   const char *name
1014445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1024445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1034445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1044445fffbSMatthew Ahrens  *
1054445fffbSMatthew Ahrens  *   nvlist_t *innvl
1064445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1074445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1084445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1094445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1104445fffbSMatthew Ahrens  *
1114445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1124445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1134445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1144445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1154445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1164445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1174445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1184445fffbSMatthew Ahrens  *
1194445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1204445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1214445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1224445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1234445fffbSMatthew Ahrens  *         the ioctl again.
1244445fffbSMatthew Ahrens  *
1254445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1264445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1274445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1284445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1294445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1304445fffbSMatthew Ahrens  *         deserialization failing.
131e9103aaeSGarrett D'Amore  */
132fa9e4066Sahrens 
133fa9e4066Sahrens #include <sys/types.h>
134fa9e4066Sahrens #include <sys/param.h>
135fa9e4066Sahrens #include <sys/errno.h>
136fa9e4066Sahrens #include <sys/uio.h>
137fa9e4066Sahrens #include <sys/buf.h>
138fa9e4066Sahrens #include <sys/modctl.h>
139fa9e4066Sahrens #include <sys/open.h>
140fa9e4066Sahrens #include <sys/file.h>
141fa9e4066Sahrens #include <sys/kmem.h>
142fa9e4066Sahrens #include <sys/conf.h>
143fa9e4066Sahrens #include <sys/cmn_err.h>
144fa9e4066Sahrens #include <sys/stat.h>
145fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1464201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
147da6c28aaSamw #include <sys/zfs_znode.h>
148fa9e4066Sahrens #include <sys/zap.h>
149fa9e4066Sahrens #include <sys/spa.h>
150b1b8ab34Slling #include <sys/spa_impl.h>
151fa9e4066Sahrens #include <sys/vdev.h>
1524201a95eSRic Aleshire #include <sys/priv_impl.h>
153fa9e4066Sahrens #include <sys/dmu.h>
154fa9e4066Sahrens #include <sys/dsl_dir.h>
155fa9e4066Sahrens #include <sys/dsl_dataset.h>
156fa9e4066Sahrens #include <sys/dsl_prop.h>
157ecd6cf80Smarks #include <sys/dsl_deleg.h>
158ecd6cf80Smarks #include <sys/dmu_objset.h>
1594e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1603b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
161fa9e4066Sahrens #include <sys/ddi.h>
162fa9e4066Sahrens #include <sys/sunddi.h>
163fa9e4066Sahrens #include <sys/sunldi.h>
164fa9e4066Sahrens #include <sys/policy.h>
165fa9e4066Sahrens #include <sys/zone.h>
166fa9e4066Sahrens #include <sys/nvpair.h>
167fa9e4066Sahrens #include <sys/pathname.h>
168fa9e4066Sahrens #include <sys/mount.h>
169fa9e4066Sahrens #include <sys/sdt.h>
170fa9e4066Sahrens #include <sys/fs/zfs.h>
171fa9e4066Sahrens #include <sys/zfs_ctldir.h>
172da6c28aaSamw #include <sys/zfs_dir.h>
173c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
174a2eea2e1Sahrens #include <sys/zvol.h>
1753f9d6ad7SLin Ling #include <sys/dsl_scan.h>
176ecd6cf80Smarks #include <sharefs/share.h>
177f18faf3fSek110237 #include <sys/dmu_objset.h>
1783b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1793b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18078f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1813b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
182a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
18337c9dfdcSAndreas Jaekel #include <sys/zfs_events.h>
184fa9e4066Sahrens 
185fa9e4066Sahrens #include "zfs_namecheck.h"
186e9dbad6fSeschrock #include "zfs_prop.h"
187ecd6cf80Smarks #include "zfs_deleg.h"
1880a586ceaSMark Shellenbaum #include "zfs_comutil.h"
189fa9e4066Sahrens 
190fa9e4066Sahrens extern struct modlfs zfs_modlfs;
191fa9e4066Sahrens 
192fa9e4066Sahrens extern void zfs_init(void);
193fa9e4066Sahrens extern void zfs_fini(void);
194fa9e4066Sahrens 
195fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
196fa9e4066Sahrens dev_info_t *zfs_dip;
197fa9e4066Sahrens 
1984445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
1994445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2004445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2014445fffbSMatthew Ahrens 
2024445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2034445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2044445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
205fa9e4066Sahrens 
20654d692b7SGeorge Wilson typedef enum {
207e7437265Sahrens 	NO_NAME,
208e7437265Sahrens 	POOL_NAME,
209e7437265Sahrens 	DATASET_NAME
21054d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21154d692b7SGeorge Wilson 
212f9af39baSGeorge Wilson typedef enum {
213f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
214f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2154445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
216f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
217f9af39baSGeorge Wilson 
21854d692b7SGeorge Wilson typedef struct zfs_ioc_vec {
2194445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
22054d692b7SGeorge Wilson 	zfs_ioc_func_t		*zvec_func;
22154d692b7SGeorge Wilson 	zfs_secpolicy_func_t	*zvec_secpolicy;
22254d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2234445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
224f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2254445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2264445fffbSMatthew Ahrens 	const char		*zvec_name;
227fa9e4066Sahrens } zfs_ioc_vec_t;
228fa9e4066Sahrens 
22914843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
23014843421SMatthew Ahrens static const char *userquota_perms[] = {
23114843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23214843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23314843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23414843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23514843421SMatthew Ahrens };
23614843421SMatthew Ahrens 
23714843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
23892241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
23992241e0bSTom Erickson     cred_t *cr);
24092241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24192241e0bSTom Erickson     nvlist_t **errors);
2420a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2430a48a24eStimh     boolean_t *);
2444445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2454445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2460a48a24eStimh 
2472acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
248a6f561b4SSašo Kiselkov 
249fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
250fa9e4066Sahrens void
__dprintf(const char * file,const char * func,int line,const char * fmt,...)251fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
252fa9e4066Sahrens {
253fa9e4066Sahrens 	const char *newfile;
2543f9d6ad7SLin Ling 	char buf[512];
255fa9e4066Sahrens 	va_list adx;
256fa9e4066Sahrens 
257fa9e4066Sahrens 	/*
258fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
259fa9e4066Sahrens 	 */
260fa9e4066Sahrens 	newfile = strrchr(file, '/');
261fa9e4066Sahrens 	if (newfile != NULL) {
262fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
263fa9e4066Sahrens 	} else {
264fa9e4066Sahrens 		newfile = file;
265fa9e4066Sahrens 	}
266fa9e4066Sahrens 
267fa9e4066Sahrens 	va_start(adx, fmt);
268fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
269fa9e4066Sahrens 	va_end(adx);
270fa9e4066Sahrens 
271fa9e4066Sahrens 	/*
272fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
273fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
274fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
275fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
276fa9e4066Sahrens 	 * arg0 = file name
277fa9e4066Sahrens 	 * arg1 = function name
278fa9e4066Sahrens 	 * arg2 = line number
279fa9e4066Sahrens 	 * arg3 = message
280fa9e4066Sahrens 	 */
281fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
282fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285ecd6cf80Smarks static void
history_str_free(char * buf)286228975ccSek110237 history_str_free(char *buf)
287228975ccSek110237 {
288228975ccSek110237 	kmem_free(buf, HIS_MAX_RECORD_LEN);
289228975ccSek110237 }
290228975ccSek110237 
291228975ccSek110237 static char *
history_str_get(zfs_cmd_t * zc)292228975ccSek110237 history_str_get(zfs_cmd_t *zc)
293228975ccSek110237 {
294228975ccSek110237 	char *buf;
295228975ccSek110237 
296228975ccSek110237 	if (zc->zc_history == NULL)
297228975ccSek110237 		return (NULL);
298228975ccSek110237 
299228975ccSek110237 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
300228975ccSek110237 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
301228975ccSek110237 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
302228975ccSek110237 		history_str_free(buf);
303228975ccSek110237 		return (NULL);
304228975ccSek110237 	}
305228975ccSek110237 
306228975ccSek110237 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
307228975ccSek110237 
308228975ccSek110237 	return (buf);
309228975ccSek110237 }
310228975ccSek110237 
311c2a93d44Stimh /*
31215e6edf1Sgw25295  * Check to see if the named dataset is currently defined as bootable
31315e6edf1Sgw25295  */
31415e6edf1Sgw25295 static boolean_t
zfs_is_bootfs(const char * name)31515e6edf1Sgw25295 zfs_is_bootfs(const char *name)
31615e6edf1Sgw25295 {
31715e6edf1Sgw25295 	objset_t *os;
31815e6edf1Sgw25295 
319503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
320503ad85cSMatthew Ahrens 		boolean_t ret;
321b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
322503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
32315e6edf1Sgw25295 		return (ret);
32415e6edf1Sgw25295 	}
325503ad85cSMatthew Ahrens 	return (B_FALSE);
326503ad85cSMatthew Ahrens }
32715e6edf1Sgw25295 
32815e6edf1Sgw25295 /*
329c2a93d44Stimh  * Return non-zero if the spa version is less than requested version.
330c2a93d44Stimh  */
331da6c28aaSamw static int
zfs_earlier_version(const char * name,int version)3320a48a24eStimh zfs_earlier_version(const char *name, int version)
333da6c28aaSamw {
334da6c28aaSamw 	spa_t *spa;
335da6c28aaSamw 
336da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
337da6c28aaSamw 		if (spa_version(spa) < version) {
338da6c28aaSamw 			spa_close(spa, FTAG);
339da6c28aaSamw 			return (1);
340da6c28aaSamw 		}
341da6c28aaSamw 		spa_close(spa, FTAG);
342da6c28aaSamw 	}
343da6c28aaSamw 	return (0);
344da6c28aaSamw }
345da6c28aaSamw 
3469e6eda55Smarks /*
347745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3489e6eda55Smarks  */
349745cd3c5Smaybee static boolean_t
zpl_earlier_version(const char * name,int version)350745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3519e6eda55Smarks {
3529e6eda55Smarks 	objset_t *os;
353745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3549e6eda55Smarks 
355503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
356745cd3c5Smaybee 		uint64_t zplversion;
3579e6eda55Smarks 
358503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
359503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
360503ad85cSMatthew Ahrens 			return (B_TRUE);
361503ad85cSMatthew Ahrens 		}
362503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
363745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
364745cd3c5Smaybee 			rc = zplversion < version;
365503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3669e6eda55Smarks 	}
3679e6eda55Smarks 	return (rc);
3689e6eda55Smarks }
3699e6eda55Smarks 
370228975ccSek110237 static void
zfs_log_history(zfs_cmd_t * zc)371ecd6cf80Smarks zfs_log_history(zfs_cmd_t *zc)
372ecd6cf80Smarks {
373ecd6cf80Smarks 	spa_t *spa;
37440feaa91Sahrens 	char *buf;
375ecd6cf80Smarks 
376228975ccSek110237 	if ((buf = history_str_get(zc)) == NULL)
377ecd6cf80Smarks 		return;
378ecd6cf80Smarks 
379228975ccSek110237 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
380e7437265Sahrens 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3814445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
382ecd6cf80Smarks 		spa_close(spa, FTAG);
383228975ccSek110237 	}
384228975ccSek110237 	history_str_free(buf);
385ecd6cf80Smarks }
386ecd6cf80Smarks 
387fa9e4066Sahrens /*
388fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
389fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
390fa9e4066Sahrens  */
391fa9e4066Sahrens /* ARGSUSED */
392fa9e4066Sahrens static int
zfs_secpolicy_none(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)3934445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
394fa9e4066Sahrens {
395fa9e4066Sahrens 	return (0);
396fa9e4066Sahrens }
397fa9e4066Sahrens 
398fa9e4066Sahrens /*
399fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
400fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
401fa9e4066Sahrens  */
402fa9e4066Sahrens /* ARGSUSED */
403fa9e4066Sahrens static int
zfs_secpolicy_read(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)4044445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
405fa9e4066Sahrens {
406fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
407ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
408fa9e4066Sahrens 		return (0);
409fa9e4066Sahrens 
410be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
411fa9e4066Sahrens }
412fa9e4066Sahrens 
413fa9e4066Sahrens static int
zfs_dozonecheck_impl(const char * dataset,uint64_t zoned,cred_t * cr)414a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
415fa9e4066Sahrens {
416fa9e4066Sahrens 	int writable = 1;
417fa9e4066Sahrens 
418fa9e4066Sahrens 	/*
419fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
420fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
421fa9e4066Sahrens 	 */
422fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
423fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
424be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
425fa9e4066Sahrens 
426fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
427fa9e4066Sahrens 		/*
428fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
429fa9e4066Sahrens 		 * global zone.
430fa9e4066Sahrens 		 */
431fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
432be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
433fa9e4066Sahrens 	} else {
434fa9e4066Sahrens 		/*
435fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
436fa9e4066Sahrens 		 */
437fa9e4066Sahrens 		if (!zoned)
438be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
439fa9e4066Sahrens 
440fa9e4066Sahrens 		/* must be writable by this zone */
441fa9e4066Sahrens 		if (!writable)
442be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
443fa9e4066Sahrens 	}
444fa9e4066Sahrens 	return (0);
445fa9e4066Sahrens }
446fa9e4066Sahrens 
447a7f53a56SChris Kirby static int
zfs_dozonecheck(const char * dataset,cred_t * cr)448a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
449a7f53a56SChris Kirby {
450a7f53a56SChris Kirby 	uint64_t zoned;
451a7f53a56SChris Kirby 
452a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
453be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
454a7f53a56SChris Kirby 
455a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
456a7f53a56SChris Kirby }
457a7f53a56SChris Kirby 
458a7f53a56SChris Kirby static int
zfs_dozonecheck_ds(const char * dataset,dsl_dataset_t * ds,cred_t * cr)459a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
460a7f53a56SChris Kirby {
461a7f53a56SChris Kirby 	uint64_t zoned;
462a7f53a56SChris Kirby 
4633b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
464be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
465a7f53a56SChris Kirby 
466a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
467a7f53a56SChris Kirby }
468a7f53a56SChris Kirby 
4694445fffbSMatthew Ahrens static int
zfs_secpolicy_write_perms_ds(const char * name,dsl_dataset_t * ds,const char * perm,cred_t * cr)470a7f53a56SChris Kirby zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
471a7f53a56SChris Kirby     const char *perm, cred_t *cr)
472a7f53a56SChris Kirby {
473a7f53a56SChris Kirby 	int error;
474a7f53a56SChris Kirby 
475a7f53a56SChris Kirby 	error = zfs_dozonecheck_ds(name, ds, cr);
476a7f53a56SChris Kirby 	if (error == 0) {
477a7f53a56SChris Kirby 		error = secpolicy_zfs(cr);
4783b2aab18SMatthew Ahrens 		if (error != 0)
4794445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
480a7f53a56SChris Kirby 	}
481a7f53a56SChris Kirby 	return (error);
482a7f53a56SChris Kirby }
483a7f53a56SChris Kirby 
4843b2aab18SMatthew Ahrens static int
zfs_secpolicy_write_perms(const char * name,const char * perm,cred_t * cr)4853b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
4863b2aab18SMatthew Ahrens {
4873b2aab18SMatthew Ahrens 	int error;
4883b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4893b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
4903b2aab18SMatthew Ahrens 
4913b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4923b2aab18SMatthew Ahrens 	if (error != 0)
4933b2aab18SMatthew Ahrens 		return (error);
4943b2aab18SMatthew Ahrens 
4953b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
4963b2aab18SMatthew Ahrens 	if (error != 0) {
4973b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
4983b2aab18SMatthew Ahrens 		return (error);
4993b2aab18SMatthew Ahrens 	}
5003b2aab18SMatthew Ahrens 
5013b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5023b2aab18SMatthew Ahrens 
5033b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5043b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
5053b2aab18SMatthew Ahrens 	return (error);
5063b2aab18SMatthew Ahrens }
5073b2aab18SMatthew Ahrens 
5084201a95eSRic Aleshire /*
5094201a95eSRic Aleshire  * Policy for setting the security label property.
5104201a95eSRic Aleshire  *
5114201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5124201a95eSRic Aleshire  */
5134201a95eSRic Aleshire static int
zfs_set_slabel_policy(const char * name,char * strval,cred_t * cr)51492241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5154201a95eSRic Aleshire {
5164201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5174201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5184201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5194201a95eSRic Aleshire 	uint64_t	zoned;
5204201a95eSRic Aleshire 	int		needed_priv = -1;
5214201a95eSRic Aleshire 	int		error;
5224201a95eSRic Aleshire 
5234201a95eSRic Aleshire 	/* First get the existing dataset label. */
5244201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5254201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5263b2aab18SMatthew Ahrens 	if (error != 0)
527be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5284201a95eSRic Aleshire 
5294201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5304201a95eSRic Aleshire 		new_default = TRUE;
5314201a95eSRic Aleshire 
5324201a95eSRic Aleshire 	/* The label must be translatable */
5334201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
534be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5354201a95eSRic Aleshire 
5364201a95eSRic Aleshire 	/*
5374201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5384201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5394201a95eSRic Aleshire 	 * are needed.
5404201a95eSRic Aleshire 	 */
5414201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5424201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
543be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5444201a95eSRic Aleshire 		return (0);
5454201a95eSRic Aleshire 	}
5464201a95eSRic Aleshire 
5474201a95eSRic Aleshire 	/*
5484201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5494201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5504201a95eSRic Aleshire 	 * global zone.
5514201a95eSRic Aleshire 	 */
5524201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5534201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
554be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5554201a95eSRic Aleshire 	if (!zoned) {
5564201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
557be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5584201a95eSRic Aleshire 	}
5594201a95eSRic Aleshire 
5604201a95eSRic Aleshire 	/*
5614201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5624201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5634201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5644201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5654201a95eSRic Aleshire 	 */
5664201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
56792241e0bSTom Erickson 		objset_t *os;
56892241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
56992241e0bSTom Erickson 
5704201a95eSRic Aleshire 		/*
5714201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5724201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5734201a95eSRic Aleshire 		 */
5744201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57592241e0bSTom Erickson 		    setsl_tag, &os);
5763b2aab18SMatthew Ahrens 		if (error != 0)
577be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5784201a95eSRic Aleshire 
57992241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
58092241e0bSTom Erickson 
5814201a95eSRic Aleshire 		if (new_default) {
5824201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5834201a95eSRic Aleshire 			goto out_check;
5844201a95eSRic Aleshire 		}
5854201a95eSRic Aleshire 
5864201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
587be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5884201a95eSRic Aleshire 
5894201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5904201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5914201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5924201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5934201a95eSRic Aleshire 	} else {
5944201a95eSRic Aleshire 		/* dataset currently has a default label */
5954201a95eSRic Aleshire 		if (!new_default)
5964201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5974201a95eSRic Aleshire 	}
5984201a95eSRic Aleshire 
5994201a95eSRic Aleshire out_check:
6004201a95eSRic Aleshire 	if (needed_priv != -1)
6014201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6024201a95eSRic Aleshire 	return (0);
6034201a95eSRic Aleshire }
6044201a95eSRic Aleshire 
605fa9e4066Sahrens static int
zfs_secpolicy_setprop(const char * dsname,zfs_prop_t prop,nvpair_t * propval,cred_t * cr)60692241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
60792241e0bSTom Erickson     cred_t *cr)
608fa9e4066Sahrens {
60992241e0bSTom Erickson 	char *strval;
61092241e0bSTom Erickson 
611ecd6cf80Smarks 	/*
612ecd6cf80Smarks 	 * Check permissions for special properties.
613ecd6cf80Smarks 	 */
614ecd6cf80Smarks 	switch (prop) {
615ecd6cf80Smarks 	case ZFS_PROP_ZONED:
616ecd6cf80Smarks 		/*
617ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
618ecd6cf80Smarks 		 */
619ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
620be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
621ecd6cf80Smarks 		break;
622ecd6cf80Smarks 
623ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
624a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
625a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
626ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
627ecd6cf80Smarks 			uint64_t zoned;
628*a1988827SMatthew Ahrens 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
629ecd6cf80Smarks 			/*
630ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
631a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
632ecd6cf80Smarks 			 * the thing they own.
633ecd6cf80Smarks 			 */
63492241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
635ecd6cf80Smarks 			    setpoint))
636be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
63792241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
638be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
639ecd6cf80Smarks 		}
640db870a07Sahrens 		break;
6414201a95eSRic Aleshire 
6424201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6434201a95eSRic Aleshire 		if (!is_system_labeled())
644be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64592241e0bSTom Erickson 
64692241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
64792241e0bSTom Erickson 			int err;
64892241e0bSTom Erickson 
64992241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
65092241e0bSTom Erickson 			if (err != 0)
65192241e0bSTom Erickson 				return (err);
65292241e0bSTom Erickson 		}
6534201a95eSRic Aleshire 		break;
654ecd6cf80Smarks 	}
655ecd6cf80Smarks 
65692241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
657ecd6cf80Smarks }
658ecd6cf80Smarks 
6594445fffbSMatthew Ahrens /* ARGSUSED */
6604445fffbSMatthew Ahrens static int
zfs_secpolicy_set_fsacl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6614445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
662ecd6cf80Smarks {
663ecd6cf80Smarks 	int error;
664ecd6cf80Smarks 
665ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6663b2aab18SMatthew Ahrens 	if (error != 0)
667ecd6cf80Smarks 		return (error);
668ecd6cf80Smarks 
669ecd6cf80Smarks 	/*
670ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
671ecd6cf80Smarks 	 * dsl_deleg_can_allow()
672ecd6cf80Smarks 	 */
673ecd6cf80Smarks 	return (0);
674ecd6cf80Smarks }
675ecd6cf80Smarks 
6764445fffbSMatthew Ahrens /* ARGSUSED */
6774445fffbSMatthew Ahrens static int
zfs_secpolicy_rollback(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6784445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
679ecd6cf80Smarks {
680681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
681681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
682ecd6cf80Smarks }
683ecd6cf80Smarks 
6844445fffbSMatthew Ahrens /* ARGSUSED */
6854445fffbSMatthew Ahrens static int
zfs_secpolicy_send(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)6864445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
687ecd6cf80Smarks {
688a7f53a56SChris Kirby 	dsl_pool_t *dp;
689a7f53a56SChris Kirby 	dsl_dataset_t *ds;
690a7f53a56SChris Kirby 	char *cp;
691a7f53a56SChris Kirby 	int error;
692a7f53a56SChris Kirby 
693a7f53a56SChris Kirby 	/*
694a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
695a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
696a7f53a56SChris Kirby 	 */
697a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
698a7f53a56SChris Kirby 	if (cp == NULL)
699be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7003b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7013b2aab18SMatthew Ahrens 	if (error != 0)
702a7f53a56SChris Kirby 		return (error);
703a7f53a56SChris Kirby 
704a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7053b2aab18SMatthew Ahrens 	if (error != 0) {
7063b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
707a7f53a56SChris Kirby 		return (error);
7083b2aab18SMatthew Ahrens 	}
709a7f53a56SChris Kirby 
710a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
711a7f53a56SChris Kirby 
712a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
713a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
714a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7153b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
716a7f53a56SChris Kirby 
717a7f53a56SChris Kirby 	return (error);
718ecd6cf80Smarks }
719ecd6cf80Smarks 
7204445fffbSMatthew Ahrens /* ARGSUSED */
721743a77edSAlan Wright static int
zfs_secpolicy_send_new(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7224445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7234445fffbSMatthew Ahrens {
7244445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7254445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7264445fffbSMatthew Ahrens }
7274445fffbSMatthew Ahrens 
7284445fffbSMatthew Ahrens /* ARGSUSED */
7294445fffbSMatthew Ahrens static int
zfs_secpolicy_deleg_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7304445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
731ecd6cf80Smarks {
732ecd6cf80Smarks 	vnode_t *vp;
733ecd6cf80Smarks 	int error;
734ecd6cf80Smarks 
735ecd6cf80Smarks 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
736ecd6cf80Smarks 	    NO_FOLLOW, NULL, &vp)) != 0)
737ecd6cf80Smarks 		return (error);
738ecd6cf80Smarks 
739ecd6cf80Smarks 	/* Now make sure mntpnt and dataset are ZFS */
740ecd6cf80Smarks 
741ecd6cf80Smarks 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
742ecd6cf80Smarks 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
743ecd6cf80Smarks 	    zc->zc_name) != 0)) {
744ecd6cf80Smarks 		VN_RELE(vp);
745be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
746ecd6cf80Smarks 	}
747ecd6cf80Smarks 
748ecd6cf80Smarks 	VN_RELE(vp);
749ecd6cf80Smarks 	return (dsl_deleg_access(zc->zc_name,
750ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHARE, cr));
751ecd6cf80Smarks }
752743a77edSAlan Wright 
753743a77edSAlan Wright int
zfs_secpolicy_share(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7544445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
755743a77edSAlan Wright {
756743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
757be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
758743a77edSAlan Wright 
759743a77edSAlan Wright 	if (secpolicy_nfs(cr) == 0) {
760743a77edSAlan Wright 		return (0);
761743a77edSAlan Wright 	} else {
7624445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
763743a77edSAlan Wright 	}
764743a77edSAlan Wright }
765743a77edSAlan Wright 
766743a77edSAlan Wright int
zfs_secpolicy_smb_acl(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)7674445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
768743a77edSAlan Wright {
769743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
770be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
771743a77edSAlan Wright 
772743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
773743a77edSAlan Wright 		return (0);
774743a77edSAlan Wright 	} else {
7754445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
776743a77edSAlan Wright 	}
777ecd6cf80Smarks }
778ecd6cf80Smarks 
779ecd6cf80Smarks static int
zfs_get_parent(const char * datasetname,char * parent,int parentsize)780ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
781ecd6cf80Smarks {
782fa9e4066Sahrens 	char *cp;
783fa9e4066Sahrens 
784fa9e4066Sahrens 	/*
785fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
786fa9e4066Sahrens 	 */
787ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
788ecd6cf80Smarks 	cp = strrchr(parent, '@');
789fa9e4066Sahrens 	if (cp != NULL) {
790fa9e4066Sahrens 		cp[0] = '\0';
791fa9e4066Sahrens 	} else {
792ecd6cf80Smarks 		cp = strrchr(parent, '/');
793fa9e4066Sahrens 		if (cp == NULL)
794be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
795fa9e4066Sahrens 		cp[0] = '\0';
796fa9e4066Sahrens 	}
797fa9e4066Sahrens 
798ecd6cf80Smarks 	return (0);
799ecd6cf80Smarks }
800ecd6cf80Smarks 
801ecd6cf80Smarks int
zfs_secpolicy_destroy_perms(const char * name,cred_t * cr)802ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
803ecd6cf80Smarks {
804ecd6cf80Smarks 	int error;
805ecd6cf80Smarks 
806ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
807ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
808ecd6cf80Smarks 		return (error);
809ecd6cf80Smarks 
810ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
811ecd6cf80Smarks }
812ecd6cf80Smarks 
8134445fffbSMatthew Ahrens /* ARGSUSED */
814ecd6cf80Smarks static int
zfs_secpolicy_destroy(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8154445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
816ecd6cf80Smarks {
817ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
818ecd6cf80Smarks }
819ecd6cf80Smarks 
820ecd6cf80Smarks /*
821cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8224445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
823cbf6f6aaSWilliam Gorrell  */
8244445fffbSMatthew Ahrens /* ARGSUSED */
825cbf6f6aaSWilliam Gorrell static int
zfs_secpolicy_destroy_snaps(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8264445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
827cbf6f6aaSWilliam Gorrell {
8284445fffbSMatthew Ahrens 	nvlist_t *snaps;
8294445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8304445fffbSMatthew Ahrens 	int error = 0;
831cbf6f6aaSWilliam Gorrell 
8324445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
833be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8344445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8354445fffbSMatthew Ahrens 	    pair = nextpair) {
8364445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
83778f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
83878f17100SMatthew Ahrens 		if (error == ENOENT) {
8394445fffbSMatthew Ahrens 			/*
8404445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8414445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8424445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8434445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8444445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8454445fffbSMatthew Ahrens 			 * checked for permission).
8464445fffbSMatthew Ahrens 			 */
8474445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8484445fffbSMatthew Ahrens 			error = 0;
8494445fffbSMatthew Ahrens 		}
8504445fffbSMatthew Ahrens 		if (error != 0)
8514445fffbSMatthew Ahrens 			break;
8524445fffbSMatthew Ahrens 	}
853cbf6f6aaSWilliam Gorrell 
854cbf6f6aaSWilliam Gorrell 	return (error);
855cbf6f6aaSWilliam Gorrell }
856cbf6f6aaSWilliam Gorrell 
857ecd6cf80Smarks int
zfs_secpolicy_rename_perms(const char * from,const char * to,cred_t * cr)858ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
859ecd6cf80Smarks {
860*a1988827SMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
861ecd6cf80Smarks 	int	error;
862ecd6cf80Smarks 
863ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
864ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
865ecd6cf80Smarks 		return (error);
866ecd6cf80Smarks 
867ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
868ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
869ecd6cf80Smarks 		return (error);
870ecd6cf80Smarks 
871ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
872ecd6cf80Smarks 	    sizeof (parentname))) != 0)
873ecd6cf80Smarks 		return (error);
874ecd6cf80Smarks 
875ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
876ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
877ecd6cf80Smarks 		return (error);
878ecd6cf80Smarks 
879ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
880ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
881ecd6cf80Smarks 		return (error);
882ecd6cf80Smarks 
883ecd6cf80Smarks 	return (error);
884ecd6cf80Smarks }
885ecd6cf80Smarks 
8864445fffbSMatthew Ahrens /* ARGSUSED */
887ecd6cf80Smarks static int
zfs_secpolicy_rename(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8884445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
889ecd6cf80Smarks {
890ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
891ecd6cf80Smarks }
892ecd6cf80Smarks 
8934445fffbSMatthew Ahrens /* ARGSUSED */
894ecd6cf80Smarks static int
zfs_secpolicy_promote(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)8954445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
896ecd6cf80Smarks {
8973b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
8983b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
899ecd6cf80Smarks 	int error;
900ecd6cf80Smarks 
901ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
902ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9033b2aab18SMatthew Ahrens 	if (error != 0)
904ecd6cf80Smarks 		return (error);
905ecd6cf80Smarks 
9063b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9073b2aab18SMatthew Ahrens 	if (error != 0)
9083b2aab18SMatthew Ahrens 		return (error);
9093b2aab18SMatthew Ahrens 
9103b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
911ecd6cf80Smarks 
912ecd6cf80Smarks 	if (error == 0) {
913*a1988827SMatthew Ahrens 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
9143b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
915ecd6cf80Smarks 		dsl_dir_t *dd;
9163b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
917ecd6cf80Smarks 
918745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
919c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9203b2aab18SMatthew Ahrens 		if (error != 0) {
9213b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9223b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
923ecd6cf80Smarks 			return (error);
924ecd6cf80Smarks 		}
925ecd6cf80Smarks 
9263b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
927ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
928ecd6cf80Smarks 
9293b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9303b2aab18SMatthew Ahrens 		if (error == 0) {
9313b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
932ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
933ecd6cf80Smarks 		}
9343b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9353b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
9363b2aab18SMatthew Ahrens 	}
9373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
938ecd6cf80Smarks 	return (error);
939ecd6cf80Smarks }
940ecd6cf80Smarks 
9414445fffbSMatthew Ahrens /* ARGSUSED */
942ecd6cf80Smarks static int
zfs_secpolicy_recv(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)9434445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
944ecd6cf80Smarks {
945ecd6cf80Smarks 	int error;
946ecd6cf80Smarks 
947ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
948ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
949ecd6cf80Smarks 		return (error);
950ecd6cf80Smarks 
951ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
952ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
953ecd6cf80Smarks 		return (error);
954ecd6cf80Smarks 
955ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
956ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
957ecd6cf80Smarks }
958ecd6cf80Smarks 
959ecd6cf80Smarks int
zfs_secpolicy_snapshot_perms(const char * name,cred_t * cr)960ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
961ecd6cf80Smarks {
962681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
963681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
964ecd6cf80Smarks }
965ecd6cf80Smarks 
9664445fffbSMatthew Ahrens /*
9674445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9684445fffbSMatthew Ahrens  */
9694445fffbSMatthew Ahrens /* ARGSUSED */
970ecd6cf80Smarks static int
zfs_secpolicy_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)9714445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
972ecd6cf80Smarks {
9734445fffbSMatthew Ahrens 	nvlist_t *snaps;
974d5285caeSGeorge Wilson 	int error = 0;
9754445fffbSMatthew Ahrens 	nvpair_t *pair;
976ecd6cf80Smarks 
9774445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
978be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9794445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9804445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9814445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9824445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9834445fffbSMatthew Ahrens 
9844445fffbSMatthew Ahrens 		if (atp == NULL) {
985be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9864445fffbSMatthew Ahrens 			break;
9874445fffbSMatthew Ahrens 		}
9884445fffbSMatthew Ahrens 		*atp = '\0';
9894445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
9904445fffbSMatthew Ahrens 		*atp = '@';
9914445fffbSMatthew Ahrens 		if (error != 0)
9924445fffbSMatthew Ahrens 			break;
9934445fffbSMatthew Ahrens 	}
9944445fffbSMatthew Ahrens 	return (error);
9954445fffbSMatthew Ahrens }
9964445fffbSMatthew Ahrens 
99778f17100SMatthew Ahrens /*
99878f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
99978f17100SMatthew Ahrens  */
100078f17100SMatthew Ahrens /* ARGSUSED */
100178f17100SMatthew Ahrens static int
zfs_secpolicy_bookmark(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)100278f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
100378f17100SMatthew Ahrens {
100478f17100SMatthew Ahrens 	int error = 0;
100578f17100SMatthew Ahrens 
100678f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
100778f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
100878f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
100978f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
101078f17100SMatthew Ahrens 
101178f17100SMatthew Ahrens 		if (hashp == NULL) {
101278f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
101378f17100SMatthew Ahrens 			break;
101478f17100SMatthew Ahrens 		}
101578f17100SMatthew Ahrens 		*hashp = '\0';
101678f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
101778f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
101878f17100SMatthew Ahrens 		*hashp = '#';
101978f17100SMatthew Ahrens 		if (error != 0)
102078f17100SMatthew Ahrens 			break;
102178f17100SMatthew Ahrens 	}
102278f17100SMatthew Ahrens 	return (error);
102378f17100SMatthew Ahrens }
102478f17100SMatthew Ahrens 
102578f17100SMatthew Ahrens /* ARGSUSED */
102678f17100SMatthew Ahrens static int
zfs_secpolicy_destroy_bookmarks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)102778f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
102878f17100SMatthew Ahrens {
102978f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
103078f17100SMatthew Ahrens 	int error = 0;
103178f17100SMatthew Ahrens 
103278f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
103378f17100SMatthew Ahrens 	    pair = nextpair) {
103478f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
103578f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
103678f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
103778f17100SMatthew Ahrens 
103878f17100SMatthew Ahrens 		if (hashp == NULL) {
103978f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
104078f17100SMatthew Ahrens 			break;
104178f17100SMatthew Ahrens 		}
104278f17100SMatthew Ahrens 
104378f17100SMatthew Ahrens 		*hashp = '\0';
104478f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
104578f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
104678f17100SMatthew Ahrens 		*hashp = '#';
104778f17100SMatthew Ahrens 		if (error == ENOENT) {
104878f17100SMatthew Ahrens 			/*
104978f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
105078f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
105178f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
105278f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
105378f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
105478f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
105578f17100SMatthew Ahrens 			 */
105678f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
105778f17100SMatthew Ahrens 			error = 0;
105878f17100SMatthew Ahrens 		}
105978f17100SMatthew Ahrens 		if (error != 0)
106078f17100SMatthew Ahrens 			break;
106178f17100SMatthew Ahrens 	}
106278f17100SMatthew Ahrens 
106378f17100SMatthew Ahrens 	return (error);
106478f17100SMatthew Ahrens }
106578f17100SMatthew Ahrens 
10664445fffbSMatthew Ahrens /* ARGSUSED */
10674445fffbSMatthew Ahrens static int
zfs_secpolicy_log_history(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)10684445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10694445fffbSMatthew Ahrens {
10704445fffbSMatthew Ahrens 	/*
10714445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10724445fffbSMatthew Ahrens 	 * to log to.
10734445fffbSMatthew Ahrens 	 */
10744445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1075be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10764445fffbSMatthew Ahrens 	return (0);
1077ecd6cf80Smarks }
1078ecd6cf80Smarks 
1079ecd6cf80Smarks static int
zfs_secpolicy_create_clone(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)10804445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1081ecd6cf80Smarks {
1082*a1988827SMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
1083ecd6cf80Smarks 	int	error;
10844445fffbSMatthew Ahrens 	char	*origin;
1085ecd6cf80Smarks 
1086ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1087ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1088ecd6cf80Smarks 		return (error);
1089ecd6cf80Smarks 
10904445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10914445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
1092ecd6cf80Smarks 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
1093ecd6cf80Smarks 		return (error);
1094ecd6cf80Smarks 
1095ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1096ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1097ecd6cf80Smarks 		return (error);
1098ecd6cf80Smarks 
10994445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11004445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1101fa9e4066Sahrens }
1102fa9e4066Sahrens 
1103fa9e4066Sahrens /*
1104fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1105fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1106fa9e4066Sahrens  */
1107fa9e4066Sahrens /* ARGSUSED */
1108fa9e4066Sahrens static int
zfs_secpolicy_config(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11094445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1110fa9e4066Sahrens {
1111fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1112be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1113fa9e4066Sahrens 
1114fa9e4066Sahrens 	return (0);
1115fa9e4066Sahrens }
1116fa9e4066Sahrens 
1117fa9e4066Sahrens /*
111899d5e173STim Haley  * Policy for object to name lookups.
111999d5e173STim Haley  */
112099d5e173STim Haley /* ARGSUSED */
112199d5e173STim Haley static int
zfs_secpolicy_diff(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11224445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
112399d5e173STim Haley {
112499d5e173STim Haley 	int error;
112599d5e173STim Haley 
112699d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
112799d5e173STim Haley 		return (0);
112899d5e173STim Haley 
112999d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
113099d5e173STim Haley 	return (error);
113199d5e173STim Haley }
113299d5e173STim Haley 
113399d5e173STim Haley /*
1134ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1135ea8dc4b6Seschrock  */
1136ea8dc4b6Seschrock /* ARGSUSED */
1137ea8dc4b6Seschrock static int
zfs_secpolicy_inject(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11384445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1139ea8dc4b6Seschrock {
1140ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1141ea8dc4b6Seschrock }
1142ea8dc4b6Seschrock 
11434445fffbSMatthew Ahrens /* ARGSUSED */
1144e45ce728Sahrens static int
zfs_secpolicy_inherit_prop(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11454445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1146e45ce728Sahrens {
1147e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1148e45ce728Sahrens 
1149990b4856Slling 	if (prop == ZPROP_INVAL) {
1150e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1151be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1152e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1153e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1154e45ce728Sahrens 	} else {
115592241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
115692241e0bSTom Erickson 		    NULL, cr));
1157e45ce728Sahrens 	}
1158e45ce728Sahrens }
1159e45ce728Sahrens 
116014843421SMatthew Ahrens static int
zfs_secpolicy_userspace_one(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11614445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
116214843421SMatthew Ahrens {
11634445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
116414843421SMatthew Ahrens 	if (err)
116514843421SMatthew Ahrens 		return (err);
116614843421SMatthew Ahrens 
116714843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1168be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
116914843421SMatthew Ahrens 
117014843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
117114843421SMatthew Ahrens 		/*
117214843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
117314843421SMatthew Ahrens 		 * themself, allow it.
117414843421SMatthew Ahrens 		 */
117514843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
117614843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
117714843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
117814843421SMatthew Ahrens 				return (0);
117914843421SMatthew Ahrens 		} else {
118014843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
118114843421SMatthew Ahrens 				return (0);
118214843421SMatthew Ahrens 		}
118314843421SMatthew Ahrens 	}
118414843421SMatthew Ahrens 
118514843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
118614843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
118714843421SMatthew Ahrens }
118814843421SMatthew Ahrens 
118914843421SMatthew Ahrens static int
zfs_secpolicy_userspace_many(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)11904445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
119114843421SMatthew Ahrens {
11924445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
119314843421SMatthew Ahrens 	if (err)
119414843421SMatthew Ahrens 		return (err);
119514843421SMatthew Ahrens 
119614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1197be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
119814843421SMatthew Ahrens 
119914843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
120014843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
120114843421SMatthew Ahrens }
120214843421SMatthew Ahrens 
12034445fffbSMatthew Ahrens /* ARGSUSED */
120414843421SMatthew Ahrens static int
zfs_secpolicy_userspace_upgrade(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12054445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120614843421SMatthew Ahrens {
120792241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
120892241e0bSTom Erickson 	    NULL, cr));
120914843421SMatthew Ahrens }
121014843421SMatthew Ahrens 
12114445fffbSMatthew Ahrens /* ARGSUSED */
1212842727c2SChris Kirby static int
zfs_secpolicy_hold(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12134445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1214842727c2SChris Kirby {
12153b2aab18SMatthew Ahrens 	nvpair_t *pair;
12163b2aab18SMatthew Ahrens 	nvlist_t *holds;
12173b2aab18SMatthew Ahrens 	int error;
12183b2aab18SMatthew Ahrens 
12193b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12203b2aab18SMatthew Ahrens 	if (error != 0)
1221be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12223b2aab18SMatthew Ahrens 
12233b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12243b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
1225*a1988827SMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12263b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12273b2aab18SMatthew Ahrens 		if (error != 0)
12283b2aab18SMatthew Ahrens 			return (error);
12293b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12303b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12313b2aab18SMatthew Ahrens 		if (error != 0)
12323b2aab18SMatthew Ahrens 			return (error);
12333b2aab18SMatthew Ahrens 	}
12343b2aab18SMatthew Ahrens 	return (0);
1235842727c2SChris Kirby }
1236842727c2SChris Kirby 
12374445fffbSMatthew Ahrens /* ARGSUSED */
1238842727c2SChris Kirby static int
zfs_secpolicy_release(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12394445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1240842727c2SChris Kirby {
12413b2aab18SMatthew Ahrens 	nvpair_t *pair;
12423b2aab18SMatthew Ahrens 	int error;
12433b2aab18SMatthew Ahrens 
12443b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12453b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
1246*a1988827SMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12473b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12483b2aab18SMatthew Ahrens 		if (error != 0)
12493b2aab18SMatthew Ahrens 			return (error);
12503b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12513b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12523b2aab18SMatthew Ahrens 		if (error != 0)
12533b2aab18SMatthew Ahrens 			return (error);
12543b2aab18SMatthew Ahrens 	}
12553b2aab18SMatthew Ahrens 	return (0);
1256842727c2SChris Kirby }
1257842727c2SChris Kirby 
1258ea8dc4b6Seschrock /*
125999d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
126099d5e173STim Haley  */
126199d5e173STim Haley static int
zfs_secpolicy_tmp_snapshot(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)12624445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
126399d5e173STim Haley {
126499d5e173STim Haley 	/*
126599d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
126699d5e173STim Haley 	 * hold, destroy and release all rolled into one.
126799d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
126899d5e173STim Haley 	 */
126999d5e173STim Haley 	int error;
127099d5e173STim Haley 
127199d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
127299d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
127399d5e173STim Haley 		return (0);
127499d5e173STim Haley 
12754445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12763b2aab18SMatthew Ahrens 	if (error == 0)
12774445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12783b2aab18SMatthew Ahrens 	if (error == 0)
12794445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12803b2aab18SMatthew Ahrens 	if (error == 0)
12814445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
128299d5e173STim Haley 	return (error);
128399d5e173STim Haley }
128499d5e173STim Haley 
128599d5e173STim Haley /*
128637c9dfdcSAndreas Jaekel  * Policy for allowing setting the zev callback list.
128737c9dfdcSAndreas Jaekel  */
128837c9dfdcSAndreas Jaekel static int
zfs_secpolicy_set_zev_callbacks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)128937c9dfdcSAndreas Jaekel zfs_secpolicy_set_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
129037c9dfdcSAndreas Jaekel {
129137c9dfdcSAndreas Jaekel 	/* must be called from kernel context */
129237c9dfdcSAndreas Jaekel 	if (!(zc->zc_iflags & FKIOCTL))
129337c9dfdcSAndreas Jaekel 		return (SET_ERROR(EPERM));
129437c9dfdcSAndreas Jaekel 	/* callback pointer must be unset (set to default value) */
129537c9dfdcSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_READER);
129637c9dfdcSAndreas Jaekel 	if (rz_zev_callbacks != rz_zev_default_callbacks) {
129737c9dfdcSAndreas Jaekel 		rw_exit(&rz_zev_rwlock);
129837c9dfdcSAndreas Jaekel 		return (SET_ERROR(EBUSY));
129937c9dfdcSAndreas Jaekel 	}
130037c9dfdcSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
130137c9dfdcSAndreas Jaekel 	return (0);
130237c9dfdcSAndreas Jaekel }
130337c9dfdcSAndreas Jaekel 
130437c9dfdcSAndreas Jaekel /*
130537c9dfdcSAndreas Jaekel  * Policy for allowing unsetting/resetting the zev callback list.
130637c9dfdcSAndreas Jaekel  */
130737c9dfdcSAndreas Jaekel static int
zfs_secpolicy_unset_zev_callbacks(zfs_cmd_t * zc,nvlist_t * innvl,cred_t * cr)130837c9dfdcSAndreas Jaekel zfs_secpolicy_unset_zev_callbacks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
130937c9dfdcSAndreas Jaekel {
131037c9dfdcSAndreas Jaekel 	/* must be called from kernel context */
131137c9dfdcSAndreas Jaekel 	if (!(zc->zc_iflags & FKIOCTL))
131237c9dfdcSAndreas Jaekel 		return (SET_ERROR(EPERM));
131337c9dfdcSAndreas Jaekel 	return (0);
131437c9dfdcSAndreas Jaekel }
131537c9dfdcSAndreas Jaekel 
131637c9dfdcSAndreas Jaekel /*
1317fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1318fa9e4066Sahrens  */
1319fa9e4066Sahrens static int
get_nvlist(uint64_t nvl,uint64_t size,int iflag,nvlist_t ** nvp)1320478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1321fa9e4066Sahrens {
1322fa9e4066Sahrens 	char *packed;
1323fa9e4066Sahrens 	int error;
1324990b4856Slling 	nvlist_t *list = NULL;
1325fa9e4066Sahrens 
1326fa9e4066Sahrens 	/*
1327e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1328fa9e4066Sahrens 	 */
1329990b4856Slling 	if (size == 0)
1330be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1333fa9e4066Sahrens 
1334478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1335478ed9adSEric Taylor 	    iflag)) != 0) {
1336fa9e4066Sahrens 		kmem_free(packed, size);
1337fa9e4066Sahrens 		return (error);
1338fa9e4066Sahrens 	}
1339fa9e4066Sahrens 
1340990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1341fa9e4066Sahrens 		kmem_free(packed, size);
1342fa9e4066Sahrens 		return (error);
1343fa9e4066Sahrens 	}
1344fa9e4066Sahrens 
1345fa9e4066Sahrens 	kmem_free(packed, size);
1346fa9e4066Sahrens 
1347990b4856Slling 	*nvp = list;
1348fa9e4066Sahrens 	return (0);
1349fa9e4066Sahrens }
1350fa9e4066Sahrens 
13514445fffbSMatthew Ahrens /*
13524445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13534445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13544445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13554445fffbSMatthew Ahrens  * removed.
13564445fffbSMatthew Ahrens  */
1357fa9e4066Sahrens static int
nvlist_smush(nvlist_t * errors,size_t max)13584445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
135992241e0bSTom Erickson {
136092241e0bSTom Erickson 	size_t size;
136192241e0bSTom Erickson 
13624445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
136392241e0bSTom Erickson 
13644445fffbSMatthew Ahrens 	if (size > max) {
136592241e0bSTom Erickson 		nvpair_t *more_errors;
136692241e0bSTom Erickson 		int n = 0;
136792241e0bSTom Erickson 
13684445fffbSMatthew Ahrens 		if (max < 1024)
1369be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
137092241e0bSTom Erickson 
13714445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13724445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
137392241e0bSTom Erickson 
137492241e0bSTom Erickson 		do {
13754445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
137692241e0bSTom Erickson 			    more_errors);
13774445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
137892241e0bSTom Erickson 			n++;
13794445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13804445fffbSMatthew Ahrens 		} while (size > max);
138192241e0bSTom Erickson 
13824445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13834445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13844445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
138592241e0bSTom Erickson 	}
138692241e0bSTom Erickson 
138792241e0bSTom Erickson 	return (0);
138892241e0bSTom Erickson }
138992241e0bSTom Erickson 
139092241e0bSTom Erickson static int
put_nvlist(zfs_cmd_t * zc,nvlist_t * nvl)1391e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1392e9dbad6fSeschrock {
1393e9dbad6fSeschrock 	char *packed = NULL;
13946e27f868SSam Falkner 	int error = 0;
1395e9dbad6fSeschrock 	size_t size;
1396e9dbad6fSeschrock 
13974445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1398e9dbad6fSeschrock 
1399e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1400be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1401e9dbad6fSeschrock 	} else {
14024445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
14036e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
14046e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1405be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
14064445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1407e9dbad6fSeschrock 	}
1408e9dbad6fSeschrock 
1409e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
14104445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1411e9dbad6fSeschrock 	return (error);
1412e9dbad6fSeschrock }
1413e9dbad6fSeschrock 
1414e9dbad6fSeschrock static int
getzfsvfs(const char * dsname,zfsvfs_t ** zfvp)1415af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
141614843421SMatthew Ahrens {
141714843421SMatthew Ahrens 	objset_t *os;
141814843421SMatthew Ahrens 	int error;
141914843421SMatthew Ahrens 
1420503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
14213b2aab18SMatthew Ahrens 	if (error != 0)
142214843421SMatthew Ahrens 		return (error);
1423503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1424503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1425be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1426503ad85cSMatthew Ahrens 	}
142714843421SMatthew Ahrens 
1428503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1429af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1430af4c679fSSean McEnroe 	if (*zfvp) {
1431af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
143214843421SMatthew Ahrens 	} else {
1433be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
143414843421SMatthew Ahrens 	}
1435503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1436503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
143714843421SMatthew Ahrens 	return (error);
143814843421SMatthew Ahrens }
143914843421SMatthew Ahrens 
144014843421SMatthew Ahrens /*
144114843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
144214843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1443ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1444ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
144514843421SMatthew Ahrens  */
144614843421SMatthew Ahrens static int
zfsvfs_hold(const char * name,void * tag,zfsvfs_t ** zfvp,boolean_t writer)14471412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
144814843421SMatthew Ahrens {
144914843421SMatthew Ahrens 	int error = 0;
145014843421SMatthew Ahrens 
1451af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1452af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
145314843421SMatthew Ahrens 	if (error == 0) {
1454c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14551412a1a2SMark Shellenbaum 		    RW_READER, tag);
1456af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
145714843421SMatthew Ahrens 			/*
145814843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
145914843421SMatthew Ahrens 			 * thread should be just about to disassociate the
146014843421SMatthew Ahrens 			 * objset from the zfsvfs.
146114843421SMatthew Ahrens 			 */
1462c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1463be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
146414843421SMatthew Ahrens 		}
146514843421SMatthew Ahrens 	}
146614843421SMatthew Ahrens 	return (error);
146714843421SMatthew Ahrens }
146814843421SMatthew Ahrens 
146914843421SMatthew Ahrens static void
zfsvfs_rele(zfsvfs_t * zfsvfs,void * tag)147014843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
147114843421SMatthew Ahrens {
1472c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
147314843421SMatthew Ahrens 
147414843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
147514843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
147614843421SMatthew Ahrens 	} else {
1477503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
147814843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
147914843421SMatthew Ahrens 	}
148014843421SMatthew Ahrens }
148114843421SMatthew Ahrens 
148214843421SMatthew Ahrens static int
zfs_ioc_pool_create(zfs_cmd_t * zc)1483fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1484fa9e4066Sahrens {
1485fa9e4066Sahrens 	int error;
1486990b4856Slling 	nvlist_t *config, *props = NULL;
14870a48a24eStimh 	nvlist_t *rootprops = NULL;
14880a48a24eStimh 	nvlist_t *zplprops = NULL;
1489fa9e4066Sahrens 
1490990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1491478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1492fa9e4066Sahrens 		return (error);
14932a6b87f0Sek110237 
1494990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1495478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1496478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1497990b4856Slling 		nvlist_free(config);
1498990b4856Slling 		return (error);
1499990b4856Slling 	}
1500990b4856Slling 
15010a48a24eStimh 	if (props) {
15020a48a24eStimh 		nvlist_t *nvl = NULL;
15030a48a24eStimh 		uint64_t version = SPA_VERSION;
15040a48a24eStimh 
15050a48a24eStimh 		(void) nvlist_lookup_uint64(props,
15060a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1507ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1508be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
15090a48a24eStimh 			goto pool_props_bad;
15100a48a24eStimh 		}
15110a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
15120a48a24eStimh 		if (nvl) {
15130a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
15140a48a24eStimh 			if (error != 0) {
15150a48a24eStimh 				nvlist_free(config);
15160a48a24eStimh 				nvlist_free(props);
15170a48a24eStimh 				return (error);
15180a48a24eStimh 			}
15190a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15200a48a24eStimh 		}
15210a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15220a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
15230a48a24eStimh 		    zplprops, NULL);
15243b2aab18SMatthew Ahrens 		if (error != 0)
15250a48a24eStimh 			goto pool_props_bad;
15260a48a24eStimh 	}
15270a48a24eStimh 
15284445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
15290a48a24eStimh 
15300a48a24eStimh 	/*
15310a48a24eStimh 	 * Set the remaining root properties
15320a48a24eStimh 	 */
153392241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
153492241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15350a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1536fa9e4066Sahrens 
15370a48a24eStimh pool_props_bad:
15380a48a24eStimh 	nvlist_free(rootprops);
15390a48a24eStimh 	nvlist_free(zplprops);
15402a6b87f0Sek110237 	nvlist_free(config);
1541990b4856Slling 	nvlist_free(props);
1542990b4856Slling 
1543fa9e4066Sahrens 	return (error);
1544fa9e4066Sahrens }
1545fa9e4066Sahrens 
1546fa9e4066Sahrens static int
zfs_ioc_pool_destroy(zfs_cmd_t * zc)1547fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1548fa9e4066Sahrens {
1549ecd6cf80Smarks 	int error;
1550ecd6cf80Smarks 	zfs_log_history(zc);
1551ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1552681d9761SEric Taylor 	if (error == 0)
1553681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1554ecd6cf80Smarks 	return (error);
1555fa9e4066Sahrens }
1556fa9e4066Sahrens 
1557fa9e4066Sahrens static int
zfs_ioc_pool_import(zfs_cmd_t * zc)1558fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1559fa9e4066Sahrens {
1560990b4856Slling 	nvlist_t *config, *props = NULL;
1561fa9e4066Sahrens 	uint64_t guid;
1562468c413aSTim Haley 	int error;
1563fa9e4066Sahrens 
1564990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1565478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1566fa9e4066Sahrens 		return (error);
1567fa9e4066Sahrens 
1568990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1569478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1570478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1571990b4856Slling 		nvlist_free(config);
1572990b4856Slling 		return (error);
1573990b4856Slling 	}
1574990b4856Slling 
1575fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1576ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1577be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1578fa9e4066Sahrens 	else
15794b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1580fa9e4066Sahrens 
15814b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15824b964adaSGeorge Wilson 		int err;
15834b964adaSGeorge Wilson 
15844b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15854b964adaSGeorge Wilson 			error = err;
15864b964adaSGeorge Wilson 	}
1587468c413aSTim Haley 
1588fa9e4066Sahrens 	nvlist_free(config);
1589fa9e4066Sahrens 
1590990b4856Slling 	nvlist_free(props);
1591990b4856Slling 
1592fa9e4066Sahrens 	return (error);
1593fa9e4066Sahrens }
1594fa9e4066Sahrens 
1595fa9e4066Sahrens static int
zfs_ioc_pool_export(zfs_cmd_t * zc)1596fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1597fa9e4066Sahrens {
1598ecd6cf80Smarks 	int error;
159989a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1600394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
160189a89ebfSlling 
1602ecd6cf80Smarks 	zfs_log_history(zc);
1603394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1604681d9761SEric Taylor 	if (error == 0)
1605681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1606ecd6cf80Smarks 	return (error);
1607fa9e4066Sahrens }
1608fa9e4066Sahrens 
1609fa9e4066Sahrens static int
zfs_ioc_pool_configs(zfs_cmd_t * zc)1610fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1611fa9e4066Sahrens {
1612fa9e4066Sahrens 	nvlist_t *configs;
1613fa9e4066Sahrens 	int error;
1614fa9e4066Sahrens 
1615fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1616be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1617fa9e4066Sahrens 
1618e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1619fa9e4066Sahrens 
1620fa9e4066Sahrens 	nvlist_free(configs);
1621fa9e4066Sahrens 
1622fa9e4066Sahrens 	return (error);
1623fa9e4066Sahrens }
1624fa9e4066Sahrens 
1625ad135b5dSChristopher Siden /*
1626ad135b5dSChristopher Siden  * inputs:
1627ad135b5dSChristopher Siden  * zc_name		name of the pool
1628ad135b5dSChristopher Siden  *
1629ad135b5dSChristopher Siden  * outputs:
1630ad135b5dSChristopher Siden  * zc_cookie		real errno
1631ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1632ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1633ad135b5dSChristopher Siden  */
1634fa9e4066Sahrens static int
zfs_ioc_pool_stats(zfs_cmd_t * zc)1635fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1636fa9e4066Sahrens {
1637fa9e4066Sahrens 	nvlist_t *config;
1638fa9e4066Sahrens 	int error;
1639ea8dc4b6Seschrock 	int ret = 0;
1640fa9e4066Sahrens 
1641e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1642e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1643fa9e4066Sahrens 
1644fa9e4066Sahrens 	if (config != NULL) {
1645e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1646fa9e4066Sahrens 		nvlist_free(config);
1647ea8dc4b6Seschrock 
1648ea8dc4b6Seschrock 		/*
1649ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1650ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1651ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1652ea8dc4b6Seschrock 		 */
1653ea8dc4b6Seschrock 		zc->zc_cookie = error;
1654fa9e4066Sahrens 	} else {
1655ea8dc4b6Seschrock 		ret = error;
1656fa9e4066Sahrens 	}
1657fa9e4066Sahrens 
1658ea8dc4b6Seschrock 	return (ret);
1659fa9e4066Sahrens }
1660fa9e4066Sahrens 
1661fa9e4066Sahrens /*
1662fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1663fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1664fa9e4066Sahrens  */
1665fa9e4066Sahrens static int
zfs_ioc_pool_tryimport(zfs_cmd_t * zc)1666fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1667fa9e4066Sahrens {
1668fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1669fa9e4066Sahrens 	int error;
1670fa9e4066Sahrens 
1671990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1672478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1673fa9e4066Sahrens 		return (error);
1674fa9e4066Sahrens 
1675fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1676fa9e4066Sahrens 
1677fa9e4066Sahrens 	nvlist_free(tryconfig);
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 	if (config == NULL)
1680be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1681fa9e4066Sahrens 
1682e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1683fa9e4066Sahrens 	nvlist_free(config);
1684fa9e4066Sahrens 
1685fa9e4066Sahrens 	return (error);
1686fa9e4066Sahrens }
1687fa9e4066Sahrens 
16883f9d6ad7SLin Ling /*
16893f9d6ad7SLin Ling  * inputs:
16903f9d6ad7SLin Ling  * zc_name              name of the pool
16913f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16923f9d6ad7SLin Ling  */
1693fa9e4066Sahrens static int
zfs_ioc_pool_scan(zfs_cmd_t * zc)16943f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1695fa9e4066Sahrens {
1696fa9e4066Sahrens 	spa_t *spa;
1697fa9e4066Sahrens 	int error;
1698fa9e4066Sahrens 
169906eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
170006eeb2adSek110237 		return (error);
170106eeb2adSek110237 
17023f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
17033f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
17043f9d6ad7SLin Ling 	else
17053f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
170606eeb2adSek110237 
1707fa9e4066Sahrens 	spa_close(spa, FTAG);
170806eeb2adSek110237 
1709fa9e4066Sahrens 	return (error);
1710fa9e4066Sahrens }
1711fa9e4066Sahrens 
1712fa9e4066Sahrens static int
zfs_ioc_pool_freeze(zfs_cmd_t * zc)1713fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1714fa9e4066Sahrens {
1715fa9e4066Sahrens 	spa_t *spa;
1716fa9e4066Sahrens 	int error;
1717fa9e4066Sahrens 
1718fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1719fa9e4066Sahrens 	if (error == 0) {
1720fa9e4066Sahrens 		spa_freeze(spa);
1721fa9e4066Sahrens 		spa_close(spa, FTAG);
1722fa9e4066Sahrens 	}
1723fa9e4066Sahrens 	return (error);
1724fa9e4066Sahrens }
1725fa9e4066Sahrens 
1726fa9e4066Sahrens static int
zfs_ioc_arc_info(zfs_cmd_t * zc)1727e071a233SArne Jansen zfs_ioc_arc_info(zfs_cmd_t *zc)
1728e071a233SArne Jansen {
1729e071a233SArne Jansen 	int ret;
1730e071a233SArne Jansen 	void *buf;
1731e071a233SArne Jansen 	size_t sz = zc->zc_nvlist_dst_size;
1732e071a233SArne Jansen 	size_t returned_bytes;
1733e071a233SArne Jansen 
1734e071a233SArne Jansen 	if (zc->zc_nvlist_dst == 0)
1735e071a233SArne Jansen 		return (SET_ERROR(EINVAL));
1736e071a233SArne Jansen 
1737e071a233SArne Jansen 	buf = kmem_alloc(sz, KM_NOSLEEP);
1738e071a233SArne Jansen 	if (buf == NULL)
1739e071a233SArne Jansen 		return (SET_ERROR(ENOMEM));
1740e071a233SArne Jansen 
1741e071a233SArne Jansen 	ret = arc_dump(zc->zc_obj, buf, sz, &returned_bytes);
1742e071a233SArne Jansen 	if (ret != 0) {
1743e071a233SArne Jansen 		kmem_free(buf, sz);
1744e071a233SArne Jansen 		return (SET_ERROR(ret));
1745e071a233SArne Jansen 	}
1746e071a233SArne Jansen 
1747e071a233SArne Jansen 	zc->zc_nvlist_dst_filled = 1;
1748e071a233SArne Jansen 	ret = ddi_copyout(buf, (void *)(uintptr_t)zc->zc_nvlist_dst,
1749e071a233SArne Jansen 	    returned_bytes, zc->zc_iflags);
1750e071a233SArne Jansen 	kmem_free(buf, sz);
1751e071a233SArne Jansen 	if (ret != 0)
1752e071a233SArne Jansen 		ret = SET_ERROR(EFAULT);
1753e071a233SArne Jansen 
1754e071a233SArne Jansen 	return (ret);
1755e071a233SArne Jansen }
1756e071a233SArne Jansen 
1757e071a233SArne Jansen static int
zfs_ioc_pool_upgrade(zfs_cmd_t * zc)1758eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1759eaca9bbdSeschrock {
1760eaca9bbdSeschrock 	spa_t *spa;
1761eaca9bbdSeschrock 	int error;
1762eaca9bbdSeschrock 
176306eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
176406eeb2adSek110237 		return (error);
176506eeb2adSek110237 
1766ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1767ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1768558d2d50Slling 		spa_close(spa, FTAG);
1769be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1770558d2d50Slling 	}
1771558d2d50Slling 
1772990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
1773eaca9bbdSeschrock 	spa_close(spa, FTAG);
177406eeb2adSek110237 
177506eeb2adSek110237 	return (error);
1776eaca9bbdSeschrock }
177706eeb2adSek110237 
177806eeb2adSek110237 static int
zfs_ioc_pool_get_history(zfs_cmd_t * zc)177906eeb2adSek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
178006eeb2adSek110237 {
178106eeb2adSek110237 	spa_t *spa;
178206eeb2adSek110237 	char *hist_buf;
178306eeb2adSek110237 	uint64_t size;
178406eeb2adSek110237 	int error;
178506eeb2adSek110237 
178606eeb2adSek110237 	if ((size = zc->zc_history_len) == 0)
1787be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
178806eeb2adSek110237 
178906eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
179006eeb2adSek110237 		return (error);
179106eeb2adSek110237 
1792e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1793d7306b64Sek110237 		spa_close(spa, FTAG);
1794be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1795d7306b64Sek110237 	}
1796d7306b64Sek110237 
179706eeb2adSek110237 	hist_buf = kmem_alloc(size, KM_SLEEP);
179806eeb2adSek110237 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
179906eeb2adSek110237 	    &zc->zc_history_len, hist_buf)) == 0) {
1800478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1801478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1802478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
180306eeb2adSek110237 	}
180406eeb2adSek110237 
180506eeb2adSek110237 	spa_close(spa, FTAG);
180606eeb2adSek110237 	kmem_free(hist_buf, size);
180706eeb2adSek110237 	return (error);
180806eeb2adSek110237 }
180906eeb2adSek110237 
181006eeb2adSek110237 static int
zfs_ioc_pool_reguid(zfs_cmd_t * zc)1811e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1812e9103aaeSGarrett D'Amore {
1813e9103aaeSGarrett D'Amore 	spa_t *spa;
1814e9103aaeSGarrett D'Amore 	int error;
1815e9103aaeSGarrett D'Amore 
1816e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1817e9103aaeSGarrett D'Amore 	if (error == 0) {
1818e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1819e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1820e9103aaeSGarrett D'Amore 	}
1821e9103aaeSGarrett D'Amore 	return (error);
1822e9103aaeSGarrett D'Amore }
1823e9103aaeSGarrett D'Amore 
1824e9103aaeSGarrett D'Amore static int
zfs_ioc_dsobj_to_dsname(zfs_cmd_t * zc)182555434c77Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
182655434c77Sek110237 {
18273b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
182855434c77Sek110237 }
182955434c77Sek110237 
1830503ad85cSMatthew Ahrens /*
1831503ad85cSMatthew Ahrens  * inputs:
1832503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1833503ad85cSMatthew Ahrens  * zc_obj		object to find
1834503ad85cSMatthew Ahrens  *
1835503ad85cSMatthew Ahrens  * outputs:
1836503ad85cSMatthew Ahrens  * zc_value		name of object
1837503ad85cSMatthew Ahrens  */
183855434c77Sek110237 static int
zfs_ioc_obj_to_path(zfs_cmd_t * zc)183955434c77Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
184055434c77Sek110237 {
1841503ad85cSMatthew Ahrens 	objset_t *os;
184255434c77Sek110237 	int error;
184355434c77Sek110237 
1844503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1845503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
184655434c77Sek110237 		return (error);
1847503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1848503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1849be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1850503ad85cSMatthew Ahrens 	}
1851503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
185255434c77Sek110237 	    sizeof (zc->zc_value));
1853503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
185455434c77Sek110237 
185555434c77Sek110237 	return (error);
185655434c77Sek110237 }
185755434c77Sek110237 
185899d5e173STim Haley /*
185999d5e173STim Haley  * inputs:
186099d5e173STim Haley  * zc_name		name of filesystem
186199d5e173STim Haley  * zc_obj		object to find
186299d5e173STim Haley  *
186399d5e173STim Haley  * outputs:
186499d5e173STim Haley  * zc_stat		stats on object
186599d5e173STim Haley  * zc_value		path to object
186699d5e173STim Haley  */
186799d5e173STim Haley static int
zfs_ioc_obj_to_stats(zfs_cmd_t * zc)186899d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
186999d5e173STim Haley {
187099d5e173STim Haley 	objset_t *os;
187199d5e173STim Haley 	int error;
187299d5e173STim Haley 
187399d5e173STim Haley 	/* XXX reading from objset not owned */
187499d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
187599d5e173STim Haley 		return (error);
187699d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
187799d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1878be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
187999d5e173STim Haley 	}
188099d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
188199d5e173STim Haley 	    sizeof (zc->zc_value));
188299d5e173STim Haley 	dmu_objset_rele(os, FTAG);
188399d5e173STim Haley 
188499d5e173STim Haley 	return (error);
188599d5e173STim Haley }
188699d5e173STim Haley 
188755434c77Sek110237 static int
zfs_ioc_vdev_add(zfs_cmd_t * zc)1888fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1889fa9e4066Sahrens {
1890fa9e4066Sahrens 	spa_t *spa;
1891fa9e4066Sahrens 	int error;
1892e7cbe64fSgw25295 	nvlist_t *config, **l2cache, **spares;
1893e7cbe64fSgw25295 	uint_t nl2cache = 0, nspares = 0;
1894fa9e4066Sahrens 
1895fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1896fa9e4066Sahrens 	if (error != 0)
1897fa9e4066Sahrens 		return (error);
1898fa9e4066Sahrens 
1899fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1900478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1901fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1902fa94a07fSbrendan 	    &l2cache, &nl2cache);
1903fa94a07fSbrendan 
1904e7cbe64fSgw25295 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1905e7cbe64fSgw25295 	    &spares, &nspares);
1906e7cbe64fSgw25295 
1907b1b8ab34Slling 	/*
1908b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1909e7cbe64fSgw25295 	 * Thus, can not add a device to a root pool.
1910e7cbe64fSgw25295 	 *
1911e7cbe64fSgw25295 	 * Intent log device can not be added to a rootpool because
1912e7cbe64fSgw25295 	 * during mountroot, zil is replayed, a seperated log device
1913e7cbe64fSgw25295 	 * can not be accessed during the mountroot time.
1914e7cbe64fSgw25295 	 *
1915e7cbe64fSgw25295 	 * l2cache and spare devices are ok to be added to a rootpool.
1916b1b8ab34Slling 	 */
1917b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
19181195e687SMark J Musante 		nvlist_free(config);
1919b1b8ab34Slling 		spa_close(spa, FTAG);
1920be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1921b1b8ab34Slling 	}
1922b1b8ab34Slling 
1923fa94a07fSbrendan 	if (error == 0) {
1924fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1925fa9e4066Sahrens 		nvlist_free(config);
1926fa9e4066Sahrens 	}
1927fa9e4066Sahrens 	spa_close(spa, FTAG);
1928fa9e4066Sahrens 	return (error);
1929fa9e4066Sahrens }
1930fa9e4066Sahrens 
19313f9d6ad7SLin Ling /*
19323f9d6ad7SLin Ling  * inputs:
19333f9d6ad7SLin Ling  * zc_name		name of the pool
19343f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
19353f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
19363f9d6ad7SLin Ling  */
1937fa9e4066Sahrens static int
zfs_ioc_vdev_remove(zfs_cmd_t * zc)1938fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1939fa9e4066Sahrens {
194099653d4eSeschrock 	spa_t *spa;
194199653d4eSeschrock 	int error;
194299653d4eSeschrock 
194399653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
194499653d4eSeschrock 	if (error != 0)
194599653d4eSeschrock 		return (error);
194699653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
194799653d4eSeschrock 	spa_close(spa, FTAG);
194899653d4eSeschrock 	return (error);
1949fa9e4066Sahrens }
1950fa9e4066Sahrens 
1951fa9e4066Sahrens static int
zfs_ioc_vdev_set_state(zfs_cmd_t * zc)19523d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1953fa9e4066Sahrens {
1954fa9e4066Sahrens 	spa_t *spa;
1955fa9e4066Sahrens 	int error;
19563d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1957fa9e4066Sahrens 
195806eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1959fa9e4066Sahrens 		return (error);
19603d7072f8Seschrock 	switch (zc->zc_cookie) {
19613d7072f8Seschrock 	case VDEV_STATE_ONLINE:
19623d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19633d7072f8Seschrock 		break;
19643d7072f8Seschrock 
19653d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19663d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19673d7072f8Seschrock 		break;
19683d7072f8Seschrock 
19693d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1970069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1971069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1972069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1973069f55e2SEric Schrock 
1974069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19753d7072f8Seschrock 		break;
19763d7072f8Seschrock 
19773d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1978069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1979069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1980069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1981069f55e2SEric Schrock 
1982069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19833d7072f8Seschrock 		break;
19843d7072f8Seschrock 
19853d7072f8Seschrock 	default:
1986be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1987fa9e4066Sahrens 	}
19883d7072f8Seschrock 	zc->zc_cookie = newstate;
1989fa9e4066Sahrens 	spa_close(spa, FTAG);
1990fa9e4066Sahrens 	return (error);
1991fa9e4066Sahrens }
1992fa9e4066Sahrens 
1993fa9e4066Sahrens static int
zfs_ioc_vdev_attach(zfs_cmd_t * zc)1994fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1995fa9e4066Sahrens {
1996fa9e4066Sahrens 	spa_t *spa;
1997fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1998fa9e4066Sahrens 	nvlist_t *config;
1999fa9e4066Sahrens 	int error;
2000fa9e4066Sahrens 
200106eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2002fa9e4066Sahrens 		return (error);
2003fa9e4066Sahrens 
2004990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2005478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
2006ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2007fa9e4066Sahrens 		nvlist_free(config);
2008fa9e4066Sahrens 	}
2009fa9e4066Sahrens 
2010fa9e4066Sahrens 	spa_close(spa, FTAG);
2011fa9e4066Sahrens 	return (error);
2012fa9e4066Sahrens }
2013fa9e4066Sahrens 
2014fa9e4066Sahrens static int
zfs_ioc_vdev_detach(zfs_cmd_t * zc)2015fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2016fa9e4066Sahrens {
2017fa9e4066Sahrens 	spa_t *spa;
2018fa9e4066Sahrens 	int error;
2019fa9e4066Sahrens 
202006eeb2adSek110237 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2021fa9e4066Sahrens 		return (error);
2022fa9e4066Sahrens 
20238ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2024fa9e4066Sahrens 
2025fa9e4066Sahrens 	spa_close(spa, FTAG);
2026fa9e4066Sahrens 	return (error);
2027fa9e4066Sahrens }
2028fa9e4066Sahrens 
2029fa9e4066Sahrens static int
zfs_ioc_vdev_split(zfs_cmd_t * zc)20301195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
20311195e687SMark J Musante {
20321195e687SMark J Musante 	spa_t *spa;
20331195e687SMark J Musante 	nvlist_t *config, *props = NULL;
20341195e687SMark J Musante 	int error;
20351195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
20361195e687SMark J Musante 
20371195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
20381195e687SMark J Musante 		return (error);
20391195e687SMark J Musante 
20401195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
20411195e687SMark J Musante 	    zc->zc_iflags, &config)) {
20421195e687SMark J Musante 		spa_close(spa, FTAG);
20431195e687SMark J Musante 		return (error);
20441195e687SMark J Musante 	}
20451195e687SMark J Musante 
20461195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
20471195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20481195e687SMark J Musante 	    zc->zc_iflags, &props))) {
20491195e687SMark J Musante 		spa_close(spa, FTAG);
20501195e687SMark J Musante 		nvlist_free(config);
20511195e687SMark J Musante 		return (error);
20521195e687SMark J Musante 	}
20531195e687SMark J Musante 
20541195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20551195e687SMark J Musante 
20561195e687SMark J Musante 	spa_close(spa, FTAG);
20571195e687SMark J Musante 
20581195e687SMark J Musante 	nvlist_free(config);
20591195e687SMark J Musante 	nvlist_free(props);
20601195e687SMark J Musante 
20611195e687SMark J Musante 	return (error);
20621195e687SMark J Musante }
20631195e687SMark J Musante 
20641195e687SMark J Musante static int
zfs_ioc_vdev_setpath(zfs_cmd_t * zc)2065c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2066c67d9675Seschrock {
2067c67d9675Seschrock 	spa_t *spa;
2068e9dbad6fSeschrock 	char *path = zc->zc_value;
2069ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2070c67d9675Seschrock 	int error;
2071c67d9675Seschrock 
2072c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2073c67d9675Seschrock 	if (error != 0)
2074c67d9675Seschrock 		return (error);
2075c67d9675Seschrock 
2076c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2077c67d9675Seschrock 	spa_close(spa, FTAG);
2078c67d9675Seschrock 	return (error);
2079c67d9675Seschrock }
2080c67d9675Seschrock 
20816809eb4eSEric Schrock static int
zfs_ioc_vdev_setfru(zfs_cmd_t * zc)20826809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20836809eb4eSEric Schrock {
20846809eb4eSEric Schrock 	spa_t *spa;
20856809eb4eSEric Schrock 	char *fru = zc->zc_value;
20866809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20876809eb4eSEric Schrock 	int error;
20886809eb4eSEric Schrock 
20896809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20906809eb4eSEric Schrock 	if (error != 0)
20916809eb4eSEric Schrock 		return (error);
20926809eb4eSEric Schrock 
20936809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20946809eb4eSEric Schrock 	spa_close(spa, FTAG);
20956809eb4eSEric Schrock 	return (error);
20966809eb4eSEric Schrock }
20976809eb4eSEric Schrock 
2098c67d9675Seschrock static int
zfs_ioc_objset_stats_impl(zfs_cmd_t * zc,objset_t * os)2099a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2100fa9e4066Sahrens {
2101a7f53a56SChris Kirby 	int error = 0;
21027f7322feSeschrock 	nvlist_t *nv;
2103fa9e4066Sahrens 
2104a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2105fa9e4066Sahrens 
21065ad82045Snd150628 	if (zc->zc_nvlist_dst != 0 &&
210792241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2108a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2109432f72fdSahrens 		/*
2110bd00f61bSrm160521 		 * NB: zvol_get_stats() will read the objset contents,
2111432f72fdSahrens 		 * which we aren't supposed to do with a
2112745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2113432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2114503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2115432f72fdSahrens 		 */
211619b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
211719b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
211819b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
211919b94df9SMatthew Ahrens 			if (error == EIO)
212019b94df9SMatthew Ahrens 				return (error);
2121fb09f5aaSMadhav Suresh 			VERIFY0(error);
2122e7437265Sahrens 		}
2123e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
21247f7322feSeschrock 		nvlist_free(nv);
21257f7322feSeschrock 	}
21267f7322feSeschrock 
2127a7f53a56SChris Kirby 	return (error);
2128a7f53a56SChris Kirby }
2129a7f53a56SChris Kirby 
2130a7f53a56SChris Kirby /*
2131a7f53a56SChris Kirby  * inputs:
2132a7f53a56SChris Kirby  * zc_name		name of filesystem
2133a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2134a7f53a56SChris Kirby  *
2135a7f53a56SChris Kirby  * outputs:
2136a7f53a56SChris Kirby  * zc_objset_stats	stats
2137a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2138a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2139a7f53a56SChris Kirby  */
2140a7f53a56SChris Kirby static int
zfs_ioc_objset_stats(zfs_cmd_t * zc)2141a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2142a7f53a56SChris Kirby {
21433b2aab18SMatthew Ahrens 	objset_t *os;
2144a7f53a56SChris Kirby 	int error;
2145a7f53a56SChris Kirby 
21463b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
21473b2aab18SMatthew Ahrens 	if (error == 0) {
2148a7f53a56SChris Kirby 		error = zfs_ioc_objset_stats_impl(zc, os);
2149503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
21503b2aab18SMatthew Ahrens 	}
2151a7f53a56SChris Kirby 
2152fa9e4066Sahrens 	return (error);
2153fa9e4066Sahrens }
2154fa9e4066Sahrens 
215592241e0bSTom Erickson /*
215692241e0bSTom Erickson  * inputs:
215792241e0bSTom Erickson  * zc_name		name of filesystem
215892241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
215992241e0bSTom Erickson  *
216092241e0bSTom Erickson  * outputs:
216192241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
216292241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
216392241e0bSTom Erickson  *
216492241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
216592241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
216692241e0bSTom Erickson  * local property values.
216792241e0bSTom Erickson  */
216892241e0bSTom Erickson static int
zfs_ioc_objset_recvd_props(zfs_cmd_t * zc)216992241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
217092241e0bSTom Erickson {
21713b2aab18SMatthew Ahrens 	int error = 0;
217292241e0bSTom Erickson 	nvlist_t *nv;
217392241e0bSTom Erickson 
217492241e0bSTom Erickson 	/*
217592241e0bSTom Erickson 	 * Without this check, we would return local property values if the
217692241e0bSTom Erickson 	 * caller has not already received properties on or after
217792241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
217892241e0bSTom Erickson 	 */
21793b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2180be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
218192241e0bSTom Erickson 
218292241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21833b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
218492241e0bSTom Erickson 		error = put_nvlist(zc, nv);
218592241e0bSTom Erickson 		nvlist_free(nv);
218692241e0bSTom Erickson 	}
218792241e0bSTom Erickson 
218892241e0bSTom Erickson 	return (error);
218992241e0bSTom Erickson }
219092241e0bSTom Erickson 
2191de8267e0Stimh static int
nvl_add_zplprop(objset_t * os,nvlist_t * props,zfs_prop_t prop)2192de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2193de8267e0Stimh {
2194de8267e0Stimh 	uint64_t value;
2195de8267e0Stimh 	int error;
2196de8267e0Stimh 
2197de8267e0Stimh 	/*
2198de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2199de8267e0Stimh 	 * the default value (if there is one).
2200de8267e0Stimh 	 */
2201de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2202de8267e0Stimh 		return (error);
2203de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2204de8267e0Stimh 	return (0);
2205de8267e0Stimh }
2206de8267e0Stimh 
2207de8267e0Stimh /*
2208de8267e0Stimh  * inputs:
2209de8267e0Stimh  * zc_name		name of filesystem
2210de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
2211de8267e0Stimh  *
2212de8267e0Stimh  * outputs:
2213de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2214de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
2215de8267e0Stimh  */
2216de8267e0Stimh static int
zfs_ioc_objset_zplprops(zfs_cmd_t * zc)2217de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2218de8267e0Stimh {
2219de8267e0Stimh 	objset_t *os;
2220de8267e0Stimh 	int err;
2221de8267e0Stimh 
2222503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2223503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2224de8267e0Stimh 		return (err);
2225de8267e0Stimh 
2226de8267e0Stimh 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2227de8267e0Stimh 
2228de8267e0Stimh 	/*
2229de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2230745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2231745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2232de8267e0Stimh 	 */
2233de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2234de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2235de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2236de8267e0Stimh 		nvlist_t *nv;
2237de8267e0Stimh 
2238de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2239de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2240de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2241de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2242de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2243de8267e0Stimh 			err = put_nvlist(zc, nv);
2244de8267e0Stimh 		nvlist_free(nv);
2245de8267e0Stimh 	} else {
2246be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2247de8267e0Stimh 	}
2248503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2249de8267e0Stimh 	return (err);
2250de8267e0Stimh }
2251de8267e0Stimh 
225214843421SMatthew Ahrens static boolean_t
dataset_name_hidden(const char * name)225314843421SMatthew Ahrens dataset_name_hidden(const char *name)
225414843421SMatthew Ahrens {
225514843421SMatthew Ahrens 	/*
225614843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
225714843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
225814843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
225914843421SMatthew Ahrens 	 */
226014843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
226114843421SMatthew Ahrens 		return (B_TRUE);
226214843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
226314843421SMatthew Ahrens 		return (B_TRUE);
226414843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
226514843421SMatthew Ahrens 		return (B_TRUE);
226614843421SMatthew Ahrens 	return (B_FALSE);
226714843421SMatthew Ahrens }
226814843421SMatthew Ahrens 
22693cb34c60Sahrens /*
22703cb34c60Sahrens  * inputs:
22713cb34c60Sahrens  * zc_name		name of filesystem
22723cb34c60Sahrens  * zc_cookie		zap cursor
22733cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22743cb34c60Sahrens  *
22753cb34c60Sahrens  * outputs:
22763cb34c60Sahrens  * zc_name		name of next filesystem
227714843421SMatthew Ahrens  * zc_cookie		zap cursor
22783cb34c60Sahrens  * zc_objset_stats	stats
22793cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22803cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22813cb34c60Sahrens  */
2282fa9e4066Sahrens static int
zfs_ioc_dataset_list_next(zfs_cmd_t * zc)2283fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2284fa9e4066Sahrens {
228587e5029aSahrens 	objset_t *os;
2286fa9e4066Sahrens 	int error;
2287fa9e4066Sahrens 	char *p;
2288620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2289fa9e4066Sahrens 
2290620252bcSChris Kirby top:
2291503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
229287e5029aSahrens 		if (error == ENOENT)
2293be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
229487e5029aSahrens 		return (error);
2295fa9e4066Sahrens 	}
2296fa9e4066Sahrens 
2297fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2298fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2299fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2300fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2301fa9e4066Sahrens 
2302fa9e4066Sahrens 	do {
230387e5029aSahrens 		error = dmu_dir_list_next(os,
230487e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
230587e5029aSahrens 		    NULL, &zc->zc_cookie);
2306fa9e4066Sahrens 		if (error == ENOENT)
2307be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
230819b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2309503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2310fa9e4066Sahrens 
2311681d9761SEric Taylor 	/*
2312681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2313681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2314681d9761SEric Taylor 	 */
2315620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
231687e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2317620252bcSChris Kirby 		if (error == ENOENT) {
2318620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2319620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2320620252bcSChris Kirby 			goto top;
2321620252bcSChris Kirby 		}
2322620252bcSChris Kirby 	}
2323fa9e4066Sahrens 	return (error);
2324fa9e4066Sahrens }
2325fa9e4066Sahrens 
23263cb34c60Sahrens /*
23273cb34c60Sahrens  * inputs:
23283cb34c60Sahrens  * zc_name		name of filesystem
23293cb34c60Sahrens  * zc_cookie		zap cursor
23303cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
23313cb34c60Sahrens  *
23323cb34c60Sahrens  * outputs:
23333cb34c60Sahrens  * zc_name		name of next snapshot
23343cb34c60Sahrens  * zc_objset_stats	stats
23353cb34c60Sahrens  * zc_nvlist_dst	property nvlist
23363cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
23373cb34c60Sahrens  */
2338fa9e4066Sahrens static int
zfs_ioc_snapshot_list_next(zfs_cmd_t * zc)2339fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2340fa9e4066Sahrens {
234187e5029aSahrens 	objset_t *os;
2342fa9e4066Sahrens 	int error;
2343fa9e4066Sahrens 
2344503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
23453b2aab18SMatthew Ahrens 	if (error != 0) {
2346745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
23473b2aab18SMatthew Ahrens 	}
2348fa9e4066Sahrens 
2349b81d61a6Slling 	/*
2350b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2351b81d61a6Slling 	 * so exit immediately.
2352b81d61a6Slling 	 */
2353*a1988827SMatthew Ahrens 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2354*a1988827SMatthew Ahrens 	    ZFS_MAX_DATASET_NAME_LEN) {
2355503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2356be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2357fa9e4066Sahrens 	}
2358fa9e4066Sahrens 
235987e5029aSahrens 	error = dmu_snapshot_list_next(os,
236087e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2361a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2362a7f53a56SChris Kirby 	    NULL);
2363a7f53a56SChris Kirby 
2364620252bcSChris Kirby 	if (error == 0) {
2365a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2366a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2367a7f53a56SChris Kirby 
2368a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23693b2aab18SMatthew Ahrens 		if (error == 0) {
2370a7f53a56SChris Kirby 			objset_t *ossnap;
2371a7f53a56SChris Kirby 
2372a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2373a7f53a56SChris Kirby 			if (error == 0)
2374a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2375a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2376a7f53a56SChris Kirby 		}
2377620252bcSChris Kirby 	} else if (error == ENOENT) {
2378be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2379620252bcSChris Kirby 	}
2380fa9e4066Sahrens 
2381a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23823cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23833b2aab18SMatthew Ahrens 	if (error != 0)
23843cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2385fa9e4066Sahrens 	return (error);
2386fa9e4066Sahrens }
2387fa9e4066Sahrens 
238892241e0bSTom Erickson static int
zfs_prop_set_userquota(const char * dsname,nvpair_t * pair)238992241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2390e9dbad6fSeschrock {
239192241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
239214843421SMatthew Ahrens 	uint64_t *valary;
239314843421SMatthew Ahrens 	unsigned int vallen;
239414843421SMatthew Ahrens 	const char *domain;
2395eeb85002STim Haley 	char *dash;
239614843421SMatthew Ahrens 	zfs_userquota_prop_t type;
239714843421SMatthew Ahrens 	uint64_t rid;
239814843421SMatthew Ahrens 	uint64_t quota;
239914843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
240092241e0bSTom Erickson 	int err;
240114843421SMatthew Ahrens 
240292241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
240392241e0bSTom Erickson 		nvlist_t *attrs;
240492241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2405eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2406eeb85002STim Haley 		    &pair) != 0)
2407be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
240892241e0bSTom Erickson 	}
240992241e0bSTom Erickson 
2410eeb85002STim Haley 	/*
2411eeb85002STim Haley 	 * A correctly constructed propname is encoded as
2412eeb85002STim Haley 	 * userquota@<rid>-<domain>.
2413eeb85002STim Haley 	 */
2414eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2415eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2416eeb85002STim Haley 	    vallen != 3)
2417be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2418eeb85002STim Haley 
2419eeb85002STim Haley 	domain = dash + 1;
242014843421SMatthew Ahrens 	type = valary[0];
242114843421SMatthew Ahrens 	rid = valary[1];
242214843421SMatthew Ahrens 	quota = valary[2];
242314843421SMatthew Ahrens 
24241412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
242592241e0bSTom Erickson 	if (err == 0) {
242692241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
242714843421SMatthew Ahrens 		zfsvfs_rele(zfsvfs, FTAG);
242814843421SMatthew Ahrens 	}
242992241e0bSTom Erickson 
243092241e0bSTom Erickson 	return (err);
2431ecd6cf80Smarks }
243292241e0bSTom Erickson 
243392241e0bSTom Erickson /*
243492241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
243592241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
243692241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
243792241e0bSTom Erickson  *
2438eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
243992241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
244092241e0bSTom Erickson  */
244192241e0bSTom Erickson static int
zfs_prop_set_special(const char * dsname,zprop_source_t source,nvpair_t * pair)244292241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
244392241e0bSTom Erickson     nvpair_t *pair)
244492241e0bSTom Erickson {
244592241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
244692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
244792241e0bSTom Erickson 	uint64_t intval;
2448b5152584SMatthew Ahrens 	int err = -1;
244992241e0bSTom Erickson 
245092241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
245192241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
245292241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
245392241e0bSTom Erickson 		return (-1);
245414843421SMatthew Ahrens 	}
2455e9dbad6fSeschrock 
245692241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
245792241e0bSTom Erickson 		nvlist_t *attrs;
245892241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
245992241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
246092241e0bSTom Erickson 		    &pair) == 0);
246192241e0bSTom Erickson 	}
246292241e0bSTom Erickson 
246392241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
246492241e0bSTom Erickson 		return (-1);
246592241e0bSTom Erickson 
246692241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
246792241e0bSTom Erickson 
2468e9dbad6fSeschrock 	switch (prop) {
2469e9dbad6fSeschrock 	case ZFS_PROP_QUOTA:
247092241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
2471e9dbad6fSeschrock 		break;
2472a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
24733b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
2474a9799022Sck153898 		break;
2475a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2476a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2477a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2478a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2479a2afb611SJerry Jelinek 			err = 0;
2480a2afb611SJerry Jelinek 		} else {
2481a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2482a2afb611SJerry Jelinek 		}
2483a2afb611SJerry Jelinek 		/*
2484a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2485a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2486a2afb611SJerry Jelinek 		 */
2487a2afb611SJerry Jelinek 		if (err == 0)
2488a2afb611SJerry Jelinek 			err = -1;
2489a2afb611SJerry Jelinek 		break;
2490e9dbad6fSeschrock 	case ZFS_PROP_RESERVATION:
249192241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
2492e9dbad6fSeschrock 		break;
2493a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
24943b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
2495a9799022Sck153898 		break;
2496e9dbad6fSeschrock 	case ZFS_PROP_VOLSIZE:
2497c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
2498e9dbad6fSeschrock 		break;
2499e7437265Sahrens 	case ZFS_PROP_VERSION:
250014843421SMatthew Ahrens 	{
250114843421SMatthew Ahrens 		zfsvfs_t *zfsvfs;
250214843421SMatthew Ahrens 
25031412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
250492241e0bSTom Erickson 			break;
250592241e0bSTom Erickson 
250692241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
250714843421SMatthew Ahrens 		zfsvfs_rele(zfsvfs, FTAG);
250814843421SMatthew Ahrens 
250992241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2510b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2511b16da2e2SGeorge Wilson 
2512b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2513b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2514b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2515b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
251614843421SMatthew Ahrens 		}
25174201a95eSRic Aleshire 		break;
25184201a95eSRic Aleshire 	}
2519e9dbad6fSeschrock 	default:
252092241e0bSTom Erickson 		err = -1;
25215c0b6a79SRich Morris 	}
252292241e0bSTom Erickson 
252392241e0bSTom Erickson 	return (err);
252492241e0bSTom Erickson }
252592241e0bSTom Erickson 
252692241e0bSTom Erickson /*
252792241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
25284445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
25294445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
25304445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
25314445fffbSMatthew Ahrens  * corresponding error numbers.
253292241e0bSTom Erickson  *
25334445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
25344445fffbSMatthew Ahrens  * modified.
253592241e0bSTom Erickson  */
253692241e0bSTom Erickson int
zfs_set_prop_nvlist(const char * dsname,zprop_source_t source,nvlist_t * nvl,nvlist_t * errlist)253792241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
25384445fffbSMatthew Ahrens     nvlist_t *errlist)
253992241e0bSTom Erickson {
254092241e0bSTom Erickson 	nvpair_t *pair;
254192241e0bSTom Erickson 	nvpair_t *propval;
254202e383d1STom Erickson 	int rv = 0;
254392241e0bSTom Erickson 	uint64_t intval;
254492241e0bSTom Erickson 	char *strval;
25454445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
25464445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
254792241e0bSTom Erickson 
254892241e0bSTom Erickson retry:
254992241e0bSTom Erickson 	pair = NULL;
255092241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
255192241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
255292241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2553cfa69fd2STom Erickson 		int err = 0;
255492241e0bSTom Erickson 
255592241e0bSTom Erickson 		/* decode the property value */
255692241e0bSTom Erickson 		propval = pair;
255792241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
255892241e0bSTom Erickson 			nvlist_t *attrs;
25594445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2560eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2561eeb85002STim Haley 			    &propval) != 0)
2562be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
256392241e0bSTom Erickson 		}
256492241e0bSTom Erickson 
256592241e0bSTom Erickson 		/* Validate value type */
2566eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
256792241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
256892241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2569be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
257092241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
257192241e0bSTom Erickson 				if (nvpair_type(propval) !=
257292241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2573be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
257419b94df9SMatthew Ahrens 			} else {
2575be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
257692241e0bSTom Erickson 			}
2577eeb85002STim Haley 		} else if (err == 0) {
257892241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
257992241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2580be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
258192241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2582a2eea2e1Sahrens 				const char *unused;
2583a2eea2e1Sahrens 
25844445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2585e9dbad6fSeschrock 
2586e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
258791ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2588e9dbad6fSeschrock 					break;
258991ebeef5Sahrens 				case PROP_TYPE_STRING:
2590be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
259192241e0bSTom Erickson 					break;
259291ebeef5Sahrens 				case PROP_TYPE_INDEX:
2593acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
259492241e0bSTom Erickson 					    intval, &unused) != 0)
2595be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2596e9dbad6fSeschrock 					break;
2597e9dbad6fSeschrock 				default:
2598e7437265Sahrens 					cmn_err(CE_PANIC,
2599e7437265Sahrens 					    "unknown property type");
2600e9dbad6fSeschrock 				}
2601e9dbad6fSeschrock 			} else {
2602be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2603e9dbad6fSeschrock 			}
2604e9dbad6fSeschrock 		}
2605e9dbad6fSeschrock 
260692241e0bSTom Erickson 		/* Validate permissions */
260792241e0bSTom Erickson 		if (err == 0)
260892241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
260992241e0bSTom Erickson 
261092241e0bSTom Erickson 		if (err == 0) {
261192241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
261292241e0bSTom Erickson 			if (err == -1) {
261392241e0bSTom Erickson 				/*
261492241e0bSTom Erickson 				 * For better performance we build up a list of
261592241e0bSTom Erickson 				 * properties to set in a single transaction.
261692241e0bSTom Erickson 				 */
261792241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
261892241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
261992241e0bSTom Erickson 				/*
262092241e0bSTom Erickson 				 * This may be a spurious error caused by
262192241e0bSTom Erickson 				 * receiving quota and reservation out of order.
262292241e0bSTom Erickson 				 * Try again in a second pass.
262392241e0bSTom Erickson 				 */
262492241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
26255c0b6a79SRich Morris 			}
262692241e0bSTom Erickson 		}
262792241e0bSTom Erickson 
26284445fffbSMatthew Ahrens 		if (err != 0) {
26294445fffbSMatthew Ahrens 			if (errlist != NULL)
26304445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
26314445fffbSMatthew Ahrens 			rv = err;
26324445fffbSMatthew Ahrens 		}
263392241e0bSTom Erickson 	}
263492241e0bSTom Erickson 
263592241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
263692241e0bSTom Erickson 		nvl = retrynvl;
263792241e0bSTom Erickson 		goto retry;
263892241e0bSTom Erickson 	}
263992241e0bSTom Erickson 
264092241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
264192241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
264292241e0bSTom Erickson 		/*
264392241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
264492241e0bSTom Erickson 		 * can, so try setting them individually.
264592241e0bSTom Erickson 		 */
264692241e0bSTom Erickson 		pair = NULL;
264792241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
264892241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2649cfa69fd2STom Erickson 			int err = 0;
265092241e0bSTom Erickson 
265192241e0bSTom Erickson 			propval = pair;
265292241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
265392241e0bSTom Erickson 				nvlist_t *attrs;
26544445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
26554445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
26564445fffbSMatthew Ahrens 				    ZPROP_VALUE);
265792241e0bSTom Erickson 			}
265892241e0bSTom Erickson 
265992241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26604445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26613b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26623b2aab18SMatthew Ahrens 				    source, strval);
266392241e0bSTom Erickson 			} else {
26644445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26653b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26663b2aab18SMatthew Ahrens 				    intval);
266792241e0bSTom Erickson 			}
266892241e0bSTom Erickson 
266992241e0bSTom Erickson 			if (err != 0) {
26704445fffbSMatthew Ahrens 				if (errlist != NULL) {
26714445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26724445fffbSMatthew Ahrens 					    err);
26734445fffbSMatthew Ahrens 				}
26744445fffbSMatthew Ahrens 				rv = err;
267592241e0bSTom Erickson 			}
267692241e0bSTom Erickson 		}
267792241e0bSTom Erickson 	}
26785c0b6a79SRich Morris 	nvlist_free(genericnvl);
267992241e0bSTom Erickson 	nvlist_free(retrynvl);
268092241e0bSTom Erickson 
268192241e0bSTom Erickson 	return (rv);
2682e9dbad6fSeschrock }
2683e9dbad6fSeschrock 
26843cb34c60Sahrens /*
2685ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2686ea2f5b9eSMatthew Ahrens  */
2687ea2f5b9eSMatthew Ahrens static int
zfs_check_userprops(const char * fsname,nvlist_t * nvl)26884445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2689ea2f5b9eSMatthew Ahrens {
269092241e0bSTom Erickson 	nvpair_t *pair = NULL;
2691ea2f5b9eSMatthew Ahrens 	int error = 0;
2692ea2f5b9eSMatthew Ahrens 
269392241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
269492241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2695ea2f5b9eSMatthew Ahrens 
2696ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
269792241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2698be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2699ea2f5b9eSMatthew Ahrens 
2700ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2701ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2702ea2f5b9eSMatthew Ahrens 			return (error);
2703ea2f5b9eSMatthew Ahrens 
2704ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2705be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2706ea2f5b9eSMatthew Ahrens 
270778f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2708ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2709ea2f5b9eSMatthew Ahrens 	}
2710ea2f5b9eSMatthew Ahrens 	return (0);
2711ea2f5b9eSMatthew Ahrens }
2712ea2f5b9eSMatthew Ahrens 
271392241e0bSTom Erickson static void
props_skip(nvlist_t * props,nvlist_t * skipped,nvlist_t ** newprops)271492241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
271592241e0bSTom Erickson {
271692241e0bSTom Erickson 	nvpair_t *pair;
271792241e0bSTom Erickson 
271892241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
271992241e0bSTom Erickson 
272092241e0bSTom Erickson 	pair = NULL;
272192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
272292241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
272392241e0bSTom Erickson 			continue;
272492241e0bSTom Erickson 
272592241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
272692241e0bSTom Erickson 	}
272792241e0bSTom Erickson }
272892241e0bSTom Erickson 
272992241e0bSTom Erickson static int
clear_received_props(const char * dsname,nvlist_t * props,nvlist_t * skipped)27303b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
273192241e0bSTom Erickson     nvlist_t *skipped)
273292241e0bSTom Erickson {
273392241e0bSTom Erickson 	int err = 0;
273492241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
273592241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
273692241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
273792241e0bSTom Erickson 		/*
273892241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
273992241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
274092241e0bSTom Erickson 		 */
274192241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
27423b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
27433b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
274492241e0bSTom Erickson 	}
274592241e0bSTom Erickson 	nvlist_free(cleared_props);
274692241e0bSTom Erickson 	return (err);
274792241e0bSTom Erickson }
274892241e0bSTom Erickson 
2749ea2f5b9eSMatthew Ahrens /*
27503cb34c60Sahrens  * inputs:
27513cb34c60Sahrens  * zc_name		name of filesystem
27525c0b6a79SRich Morris  * zc_value		name of property to set
27533cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
275492241e0bSTom Erickson  * zc_cookie		received properties flag
27553cb34c60Sahrens  *
275692241e0bSTom Erickson  * outputs:
275792241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
27583cb34c60Sahrens  */
2759e9dbad6fSeschrock static int
zfs_ioc_set_prop(zfs_cmd_t * zc)2760fa9e4066Sahrens zfs_ioc_set_prop(zfs_cmd_t *zc)
2761fa9e4066Sahrens {
2762e9dbad6fSeschrock 	nvlist_t *nvl;
276392241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
276492241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
276592241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27664445fffbSMatthew Ahrens 	nvlist_t *errors;
2767e9dbad6fSeschrock 	int error;
2768e9dbad6fSeschrock 
2769990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2770478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2771e9dbad6fSeschrock 		return (error);
2772fa9e4066Sahrens 
277392241e0bSTom Erickson 	if (received) {
2774bb0ade09Sahrens 		nvlist_t *origprops;
2775bb0ade09Sahrens 
27763b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27773b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27783b2aab18SMatthew Ahrens 			    origprops, nvl);
2779bb0ade09Sahrens 			nvlist_free(origprops);
2780bb0ade09Sahrens 		}
278192241e0bSTom Erickson 
27823b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2783bb0ade09Sahrens 	}
2784bb0ade09Sahrens 
27854445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27863b2aab18SMatthew Ahrens 	if (error == 0)
27874445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2788ecd6cf80Smarks 
278992241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
279092241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
279192241e0bSTom Erickson 	}
279292241e0bSTom Erickson 
279392241e0bSTom Erickson 	nvlist_free(errors);
2794e9dbad6fSeschrock 	nvlist_free(nvl);
2795e9dbad6fSeschrock 	return (error);
2796fa9e4066Sahrens }
2797fa9e4066Sahrens 
27983cb34c60Sahrens /*
27993cb34c60Sahrens  * inputs:
28003cb34c60Sahrens  * zc_name		name of filesystem
28013cb34c60Sahrens  * zc_value		name of property to inherit
280292241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
28033cb34c60Sahrens  *
28043cb34c60Sahrens  * outputs:		none
28053cb34c60Sahrens  */
2806fa9e4066Sahrens static int
zfs_ioc_inherit_prop(zfs_cmd_t * zc)2807e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2808e45ce728Sahrens {
280992241e0bSTom Erickson 	const char *propname = zc->zc_value;
281092241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
281192241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
281292241e0bSTom Erickson 	zprop_source_t source = (received
281392241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
281492241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
281592241e0bSTom Erickson 
281692241e0bSTom Erickson 	if (received) {
281792241e0bSTom Erickson 		nvlist_t *dummy;
281892241e0bSTom Erickson 		nvpair_t *pair;
281992241e0bSTom Erickson 		zprop_type_t type;
282092241e0bSTom Erickson 		int err;
282192241e0bSTom Erickson 
282292241e0bSTom Erickson 		/*
282392241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
282492241e0bSTom Erickson 		 * nvpair with type info.
282592241e0bSTom Erickson 		 */
282692241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
282792241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2828be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
282992241e0bSTom Erickson 
283092241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2831a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2832a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2833be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
283492241e0bSTom Erickson 		} else {
283592241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
283692241e0bSTom Erickson 		}
283792241e0bSTom Erickson 
283892241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
283992241e0bSTom Erickson 
284092241e0bSTom Erickson 		switch (type) {
284192241e0bSTom Erickson 		case PROP_TYPE_STRING:
284292241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
284392241e0bSTom Erickson 			break;
284492241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
284592241e0bSTom Erickson 		case PROP_TYPE_INDEX:
284692241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
284792241e0bSTom Erickson 			break;
284892241e0bSTom Erickson 		default:
284992241e0bSTom Erickson 			nvlist_free(dummy);
2850be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
285192241e0bSTom Erickson 		}
285292241e0bSTom Erickson 
285392241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
285492241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
285592241e0bSTom Erickson 		nvlist_free(dummy);
285692241e0bSTom Erickson 		if (err != -1)
285792241e0bSTom Erickson 			return (err); /* special property already handled */
285892241e0bSTom Erickson 	} else {
285992241e0bSTom Erickson 		/*
286092241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
286192241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
286292241e0bSTom Erickson 		 * and reservation to the received or default values even though
286392241e0bSTom Erickson 		 * they are not considered inheritable.
286492241e0bSTom Erickson 		 */
286592241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2866be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
286792241e0bSTom Erickson 	}
286892241e0bSTom Erickson 
28694445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28703b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2871e45ce728Sahrens }
2872e45ce728Sahrens 
2873e45ce728Sahrens static int
zfs_ioc_pool_set_props(zfs_cmd_t * zc)287411a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2875b1b8ab34Slling {
2876990b4856Slling 	nvlist_t *props;
2877b1b8ab34Slling 	spa_t *spa;
2878990b4856Slling 	int error;
287992241e0bSTom Erickson 	nvpair_t *pair;
2880b1b8ab34Slling 
288192241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
288292241e0bSTom Erickson 	    zc->zc_iflags, &props))
2883b1b8ab34Slling 		return (error);
2884b1b8ab34Slling 
2885379c004dSEric Schrock 	/*
2886379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2887379c004dSEric Schrock 	 * to handle the faulted case.
2888379c004dSEric Schrock 	 */
288992241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
289092241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2891379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
289292241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2893379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2894379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2895379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2896379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2897379c004dSEric Schrock 		}
2898379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2899b693757aSEric Schrock 		if (spa != NULL) {
2900b693757aSEric Schrock 			nvlist_free(props);
2901379c004dSEric Schrock 			return (0);
2902379c004dSEric Schrock 		}
2903b693757aSEric Schrock 	}
2904379c004dSEric Schrock 
2905b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2906990b4856Slling 		nvlist_free(props);
2907b1b8ab34Slling 		return (error);
2908b1b8ab34Slling 	}
2909b1b8ab34Slling 
2910990b4856Slling 	error = spa_prop_set(spa, props);
2911b1b8ab34Slling 
2912990b4856Slling 	nvlist_free(props);
2913b1b8ab34Slling 	spa_close(spa, FTAG);
2914b1b8ab34Slling 
2915b1b8ab34Slling 	return (error);
2916b1b8ab34Slling }
2917b1b8ab34Slling 
2918b1b8ab34Slling static int
zfs_ioc_pool_get_props(zfs_cmd_t * zc)291911a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2920b1b8ab34Slling {
2921b1b8ab34Slling 	spa_t *spa;
2922b1b8ab34Slling 	int error;
2923b1b8ab34Slling 	nvlist_t *nvp = NULL;
2924b1b8ab34Slling 
2925379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2926379c004dSEric Schrock 		/*
2927379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2928379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2929379c004dSEric Schrock 		 * anyway.
2930379c004dSEric Schrock 		 */
2931379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2932379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2933990b4856Slling 			error = spa_prop_get(spa, &nvp);
2934379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2935379c004dSEric Schrock 	} else {
2936379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2937379c004dSEric Schrock 		spa_close(spa, FTAG);
2938379c004dSEric Schrock 	}
2939b1b8ab34Slling 
2940b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2941b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2942b1b8ab34Slling 	else
2943be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2944b1b8ab34Slling 
2945b1b8ab34Slling 	nvlist_free(nvp);
2946b1b8ab34Slling 	return (error);
2947b1b8ab34Slling }
2948b1b8ab34Slling 
29493cb34c60Sahrens /*
29503cb34c60Sahrens  * inputs:
29513cb34c60Sahrens  * zc_name		name of filesystem
29523cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29533cb34c60Sahrens  * zc_perm_action	allow/unallow flag
29543cb34c60Sahrens  *
29553cb34c60Sahrens  * outputs:		none
29563cb34c60Sahrens  */
2957ecd6cf80Smarks static int
zfs_ioc_set_fsacl(zfs_cmd_t * zc)2958ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2959ecd6cf80Smarks {
2960ecd6cf80Smarks 	int error;
2961ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2962ecd6cf80Smarks 
2963990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2964478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2965ecd6cf80Smarks 		return (error);
2966ecd6cf80Smarks 
2967ecd6cf80Smarks 	/*
2968ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2969ecd6cf80Smarks 	 */
2970ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2971ecd6cf80Smarks 		nvlist_free(fsaclnv);
2972be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2973ecd6cf80Smarks 	}
2974ecd6cf80Smarks 
2975ecd6cf80Smarks 	/*
2976ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2977ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2978ecd6cf80Smarks 	 * the nvlist(s)
2979ecd6cf80Smarks 	 */
2980ecd6cf80Smarks 
298191ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29823b2aab18SMatthew Ahrens 	if (error != 0) {
298391ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
298491ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
298591ebeef5Sahrens 			    fsaclnv, CRED());
298691ebeef5Sahrens 		} else {
298791ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
298891ebeef5Sahrens 			    fsaclnv, CRED());
298991ebeef5Sahrens 		}
2990ecd6cf80Smarks 	}
2991ecd6cf80Smarks 
2992ecd6cf80Smarks 	if (error == 0)
2993ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2994ecd6cf80Smarks 
2995ecd6cf80Smarks 	nvlist_free(fsaclnv);
2996ecd6cf80Smarks 	return (error);
2997ecd6cf80Smarks }
2998ecd6cf80Smarks 
29993cb34c60Sahrens /*
30003cb34c60Sahrens  * inputs:
30013cb34c60Sahrens  * zc_name		name of filesystem
30023cb34c60Sahrens  *
30033cb34c60Sahrens  * outputs:
30043cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
30053cb34c60Sahrens  */
3006ecd6cf80Smarks static int
zfs_ioc_get_fsacl(zfs_cmd_t * zc)3007ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3008ecd6cf80Smarks {
3009ecd6cf80Smarks 	nvlist_t *nvp;
3010ecd6cf80Smarks 	int error;
3011ecd6cf80Smarks 
3012ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3013ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
3014ecd6cf80Smarks 		nvlist_free(nvp);
3015ecd6cf80Smarks 	}
3016ecd6cf80Smarks 
3017ecd6cf80Smarks 	return (error);
3018ecd6cf80Smarks }
3019ecd6cf80Smarks 
30203cb34c60Sahrens /*
3021fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
3022fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
3023fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
3024fa9e4066Sahrens  */
3025fa9e4066Sahrens static vfs_t *
zfs_get_vfs(const char * resource)3026fa9e4066Sahrens zfs_get_vfs(const char *resource)
3027fa9e4066Sahrens {
3028fa9e4066Sahrens 	struct vfs *vfsp;
3029fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
3030fa9e4066Sahrens 
3031fa9e4066Sahrens 	vfs_list_read_lock();
3032fa9e4066Sahrens 	vfsp = rootvfs;
3033fa9e4066Sahrens 	do {
3034fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3035fa9e4066Sahrens 			VFS_HOLD(vfsp);
3036fa9e4066Sahrens 			vfs_found = vfsp;
3037fa9e4066Sahrens 			break;
3038fa9e4066Sahrens 		}
3039fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
3040fa9e4066Sahrens 	} while (vfsp != rootvfs);
3041fa9e4066Sahrens 	vfs_list_unlock();
3042fa9e4066Sahrens 	return (vfs_found);
3043fa9e4066Sahrens }
3044fa9e4066Sahrens 
3045ecd6cf80Smarks /* ARGSUSED */
3046fa9e4066Sahrens static void
zfs_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)3047ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3048fa9e4066Sahrens {
3049da6c28aaSamw 	zfs_creat_t *zct = arg;
3050e7437265Sahrens 
3051de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3052da6c28aaSamw }
3053da6c28aaSamw 
3054de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3055de8267e0Stimh 
3056da6c28aaSamw /*
3057de8267e0Stimh  * inputs:
30580a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3059f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3060f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3061f7170741SWill Andrews  * createprops		list of properties requested by creator
3062da6c28aaSamw  *
3063de8267e0Stimh  * outputs:
3064de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30650a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3066da6c28aaSamw  *
3067de8267e0Stimh  * Determine the settings for utf8only, normalization and
3068de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3069de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3070de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3071de8267e0Stimh  * request settings for these properties, even if the requested
3072de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3073de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3074de8267e0Stimh  * processing.
3075da6c28aaSamw  */
3076da6c28aaSamw static int
zfs_fill_zplprops_impl(objset_t * os,uint64_t zplver,boolean_t fuids_ok,boolean_t sa_ok,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)307714843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30780a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30790a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3080da6c28aaSamw {
3081de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3082de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3083de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3084da6c28aaSamw 
3085de8267e0Stimh 	ASSERT(zplprops != NULL);
3086da6c28aaSamw 
3087c2a93d44Stimh 	/*
3088de8267e0Stimh 	 * Pull out creator prop choices, if any.
3089c2a93d44Stimh 	 */
3090de8267e0Stimh 	if (createprops) {
3091de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
30920a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
30930a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
3094de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3095de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3096de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3097de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3098de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3099de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3100de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3101de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3102de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3103de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3104de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3105da6c28aaSamw 	}
3106da6c28aaSamw 
3107c2a93d44Stimh 	/*
31080a48a24eStimh 	 * If the zpl version requested is whacky or the file system
31090a48a24eStimh 	 * or pool is version is too "young" to support normalization
31100a48a24eStimh 	 * and the creator tried to set a value for one of the props,
31110a48a24eStimh 	 * error out.
3112c2a93d44Stimh 	 */
31130a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
31140a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
31150a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
31160a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3117de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
31180a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3119be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3120c2a93d44Stimh 
3121de8267e0Stimh 	/*
3122de8267e0Stimh 	 * Put the version in the zplprops
3123de8267e0Stimh 	 */
3124de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3125de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3126de8267e0Stimh 
3127de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3128de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3129de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3130de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3131de8267e0Stimh 
3132de8267e0Stimh 	/*
3133de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3134de8267e0Stimh 	 */
3135de8267e0Stimh 	if (norm)
3136de8267e0Stimh 		u8 = 1;
3137de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3138de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3139de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3140de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3141de8267e0Stimh 
3142de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3143de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3144de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3145de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3146de8267e0Stimh 
3147ab04eb8eStimh 	if (is_ci)
3148ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3149ab04eb8eStimh 
3150da6c28aaSamw 	return (0);
3151fa9e4066Sahrens }
3152fa9e4066Sahrens 
31530a48a24eStimh static int
zfs_fill_zplprops(const char * dataset,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)31540a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31550a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31560a48a24eStimh {
31570a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
31580a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31590a48a24eStimh 	objset_t *os = NULL;
3160*a1988827SMatthew Ahrens 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
31610a48a24eStimh 	char *cp;
31620a586ceaSMark Shellenbaum 	spa_t *spa;
31630a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31640a48a24eStimh 	int error;
31650a48a24eStimh 
31660a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31670a48a24eStimh 	cp = strrchr(parentname, '/');
31680a48a24eStimh 	ASSERT(cp != NULL);
31690a48a24eStimh 	cp[0] = '\0';
31700a48a24eStimh 
31710a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31720a586ceaSMark Shellenbaum 		return (error);
31730a586ceaSMark Shellenbaum 
31740a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31750a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31760a586ceaSMark Shellenbaum 
31770a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31780a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31790a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31800a48a24eStimh 
31810a48a24eStimh 	/*
31820a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31830a48a24eStimh 	 */
3184503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31850a48a24eStimh 		return (error);
31860a48a24eStimh 
31870a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31880a48a24eStimh 	    zplprops, is_ci);
3189503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31900a48a24eStimh 	return (error);
31910a48a24eStimh }
31920a48a24eStimh 
31930a48a24eStimh static int
zfs_fill_zplprops_root(uint64_t spa_vers,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)31940a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31950a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31960a48a24eStimh {
31970a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
31980a586ceaSMark Shellenbaum 	boolean_t sa_ok;
31990a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
32000a48a24eStimh 	int error;
32010a48a24eStimh 
32020a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
32030a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
32040a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
32050a48a24eStimh 
32060a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
32070a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
32080a48a24eStimh 	return (error);
32090a48a24eStimh }
32100a48a24eStimh 
32113cb34c60Sahrens /*
32124445fffbSMatthew Ahrens  * innvl: {
32134445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
32144445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32154445fffbSMatthew Ahrens  * }
32163cb34c60Sahrens  *
32174445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32183cb34c60Sahrens  */
3219fa9e4066Sahrens static int
zfs_ioc_create(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)32204445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3221fa9e4066Sahrens {
3222fa9e4066Sahrens 	int error = 0;
32234445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3224ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3225ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
32264445fffbSMatthew Ahrens 	int32_t type32;
32274445fffbSMatthew Ahrens 	dmu_objset_type_t type;
32284445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
32294445fffbSMatthew Ahrens 
32304445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3231be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32324445fffbSMatthew Ahrens 	type = type32;
32334445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3234fa9e4066Sahrens 
3235fa9e4066Sahrens 	switch (type) {
3236fa9e4066Sahrens 	case DMU_OST_ZFS:
3237fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3238fa9e4066Sahrens 		break;
3239fa9e4066Sahrens 
3240fa9e4066Sahrens 	case DMU_OST_ZVOL:
3241fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3242fa9e4066Sahrens 		break;
3243fa9e4066Sahrens 
3244fa9e4066Sahrens 	default:
32451d452cf5Sahrens 		cbfunc = NULL;
3246e7cbe64fSgw25295 		break;
3247fa9e4066Sahrens 	}
32484445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32494445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3250be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3251fa9e4066Sahrens 
3252da6c28aaSamw 	zct.zct_props = nvprops;
3253da6c28aaSamw 
32544445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3255be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3256e9dbad6fSeschrock 
3257fa9e4066Sahrens 	if (type == DMU_OST_ZVOL) {
3258e9dbad6fSeschrock 		uint64_t volsize, volblocksize;
32595c5460e9Seschrock 
32604445fffbSMatthew Ahrens 		if (nvprops == NULL)
3261be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32624445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32634445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3264be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32655c5460e9Seschrock 
3266ecd6cf80Smarks 		if ((error = nvlist_lookup_uint64(nvprops,
3267e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32684445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3269be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3270e9dbad6fSeschrock 
3271e9dbad6fSeschrock 		if (error != 0)
3272e9dbad6fSeschrock 			volblocksize = zfs_prop_default_numeric(
3273e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
3274e9dbad6fSeschrock 
3275e9dbad6fSeschrock 		if ((error = zvol_check_volblocksize(
3276e9dbad6fSeschrock 		    volblocksize)) != 0 ||
3277e9dbad6fSeschrock 		    (error = zvol_check_volsize(volsize,
32784445fffbSMatthew Ahrens 		    volblocksize)) != 0)
32795c5460e9Seschrock 			return (error);
3280e7437265Sahrens 	} else if (type == DMU_OST_ZFS) {
3281da6c28aaSamw 		int error;
3282e7437265Sahrens 
3283de8267e0Stimh 		/*
3284da6c28aaSamw 		 * We have to have normalization and
3285da6c28aaSamw 		 * case-folding flags correct when we do the
3286da6c28aaSamw 		 * file system creation, so go figure them out
3287de8267e0Stimh 		 * now.
3288da6c28aaSamw 		 */
3289de8267e0Stimh 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
3290de8267e0Stimh 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32914445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32920a48a24eStimh 		    zct.zct_zplprops, &is_insensitive);
3293da6c28aaSamw 		if (error != 0) {
3294de8267e0Stimh 			nvlist_free(zct.zct_zplprops);
3295da6c28aaSamw 			return (error);
3296da6c28aaSamw 		}
3297da6c28aaSamw 	}
32984445fffbSMatthew Ahrens 
32994445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
3300ab04eb8eStimh 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3301de8267e0Stimh 	nvlist_free(zct.zct_zplprops);
3302e9dbad6fSeschrock 
3303e9dbad6fSeschrock 	/*
3304e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3305e9dbad6fSeschrock 	 */
3306e9dbad6fSeschrock 	if (error == 0) {
33074445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33084445fffbSMatthew Ahrens 		    nvprops, outnvl);
330992241e0bSTom Erickson 		if (error != 0)
33103b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3311e9dbad6fSeschrock 	}
3312fa9e4066Sahrens 	return (error);
3313fa9e4066Sahrens }
3314fa9e4066Sahrens 
33153cb34c60Sahrens /*
33164445fffbSMatthew Ahrens  * innvl: {
33174445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
33184445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
33194445fffbSMatthew Ahrens  * }
33203cb34c60Sahrens  *
33214445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
33223cb34c60Sahrens  */
3323fa9e4066Sahrens static int
zfs_ioc_clone(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)33244445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3325fa9e4066Sahrens {
33264445fffbSMatthew Ahrens 	int error = 0;
3327bb0ade09Sahrens 	nvlist_t *nvprops = NULL;
33284445fffbSMatthew Ahrens 	char *origin_name;
3329bb0ade09Sahrens 
33304445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3331be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33324445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
33334445fffbSMatthew Ahrens 
33344445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
33354445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3336be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3337bb0ade09Sahrens 
333854da3412SMarcel Telka 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3339be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33403b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
33413b2aab18SMatthew Ahrens 	if (error != 0)
33424445fffbSMatthew Ahrens 		return (error);
33434445fffbSMatthew Ahrens 
33444445fffbSMatthew Ahrens 	/*
33454445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
33464445fffbSMatthew Ahrens 	 */
33474445fffbSMatthew Ahrens 	if (error == 0) {
33484445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33494445fffbSMatthew Ahrens 		    nvprops, outnvl);
33504445fffbSMatthew Ahrens 		if (error != 0)
33513b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3352bb0ade09Sahrens 	}
3353bb0ade09Sahrens 	return (error);
33541d452cf5Sahrens }
33551d452cf5Sahrens 
33564445fffbSMatthew Ahrens /*
33574445fffbSMatthew Ahrens  * innvl: {
33584445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33594445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33604445fffbSMatthew Ahrens  * }
33614445fffbSMatthew Ahrens  *
33624445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33634445fffbSMatthew Ahrens  */
33644445fffbSMatthew Ahrens static int
zfs_ioc_snapshot(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)33654445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
33664445fffbSMatthew Ahrens {
33674445fffbSMatthew Ahrens 	nvlist_t *snaps;
33684445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33694445fffbSMatthew Ahrens 	int error, poollen;
33704445fffbSMatthew Ahrens 	nvpair_t *pair;
33714445fffbSMatthew Ahrens 
33724445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33734445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33744445fffbSMatthew Ahrens 		return (error);
33754445fffbSMatthew Ahrens 
33764445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33774445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3378be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33794445fffbSMatthew Ahrens 
33804445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3381be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33824445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33834445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33844445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33854445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33864445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
33874445fffbSMatthew Ahrens 
33884445fffbSMatthew Ahrens 		/*
33894445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33904445fffbSMatthew Ahrens 		 * contain only valid characters.
33914445fffbSMatthew Ahrens 		 */
339278f17100SMatthew Ahrens 		if (cp == NULL ||
339378f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3394be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
33954445fffbSMatthew Ahrens 
33964445fffbSMatthew Ahrens 		/*
33974445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
33984445fffbSMatthew Ahrens 		 */
33994445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
34004445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3401be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
34024445fffbSMatthew Ahrens 
34034445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
34044445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
34054445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
34064445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
34074445fffbSMatthew Ahrens 			    == 0) {
3408be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
34094445fffbSMatthew Ahrens 			}
34104445fffbSMatthew Ahrens 		}
34114445fffbSMatthew Ahrens 	}
34124445fffbSMatthew Ahrens 
34133b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
34144445fffbSMatthew Ahrens 	return (error);
34154445fffbSMatthew Ahrens }
34164445fffbSMatthew Ahrens 
34174445fffbSMatthew Ahrens /*
34184445fffbSMatthew Ahrens  * innvl: "message" -> string
34194445fffbSMatthew Ahrens  */
34204445fffbSMatthew Ahrens /* ARGSUSED */
34214445fffbSMatthew Ahrens static int
zfs_ioc_log_history(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)34224445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
34234445fffbSMatthew Ahrens {
34244445fffbSMatthew Ahrens 	char *message;
34254445fffbSMatthew Ahrens 	spa_t *spa;
34264445fffbSMatthew Ahrens 	int error;
34274445fffbSMatthew Ahrens 	char *poolname;
34284445fffbSMatthew Ahrens 
34294445fffbSMatthew Ahrens 	/*
34304445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
34314445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
34324445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
34334445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
34344445fffbSMatthew Ahrens 	 * we clear the TSD here.
34354445fffbSMatthew Ahrens 	 */
34364445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
34374445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
34384445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
34394445fffbSMatthew Ahrens 	strfree(poolname);
34404445fffbSMatthew Ahrens 	if (error != 0)
34414445fffbSMatthew Ahrens 		return (error);
34424445fffbSMatthew Ahrens 
34434445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
34444445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3445be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34464445fffbSMatthew Ahrens 	}
34474445fffbSMatthew Ahrens 
34484445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34494445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3450be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34514445fffbSMatthew Ahrens 	}
34524445fffbSMatthew Ahrens 
34534445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
34544445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
34554445fffbSMatthew Ahrens 	return (error);
34564445fffbSMatthew Ahrens }
34574445fffbSMatthew Ahrens 
34583b2aab18SMatthew Ahrens /*
34593b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34603b2aab18SMatthew Ahrens  * unmount may need to write out data.
34613b2aab18SMatthew Ahrens  *
34623b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34633b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3464fc7a6e3fSWill Andrews  *
3465fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3466fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34673b2aab18SMatthew Ahrens  */
3468fc7a6e3fSWill Andrews int
zfs_unmount_snap(const char * snapname)34693b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34701d452cf5Sahrens {
34714445fffbSMatthew Ahrens 	vfs_t *vfsp;
34723b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3473fc7a6e3fSWill Andrews 	int err;
3474fa9e4066Sahrens 
34753b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3476fc7a6e3fSWill Andrews 		return (0);
34774445fffbSMatthew Ahrens 
34783b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
34794445fffbSMatthew Ahrens 	if (vfsp == NULL)
3480fc7a6e3fSWill Andrews 		return (0);
34814445fffbSMatthew Ahrens 
34823b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
34833b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34843b2aab18SMatthew Ahrens 
3485fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
3486fa9e4066Sahrens 	VFS_RELE(vfsp);
3487fc7a6e3fSWill Andrews 	if (err != 0)
3488fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
34894445fffbSMatthew Ahrens 
34904445fffbSMatthew Ahrens 	/*
34914445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34924445fffbSMatthew Ahrens 	 */
34933b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3494fc7a6e3fSWill Andrews 	return (0);
34953b2aab18SMatthew Ahrens }
34963b2aab18SMatthew Ahrens 
34973b2aab18SMatthew Ahrens /* ARGSUSED */
34983b2aab18SMatthew Ahrens static int
zfs_unmount_snap_cb(const char * snapname,void * arg)34993b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
35003b2aab18SMatthew Ahrens {
3501fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
35023b2aab18SMatthew Ahrens }
35033b2aab18SMatthew Ahrens 
35043b2aab18SMatthew Ahrens /*
35053b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
35063b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
35073b2aab18SMatthew Ahrens  * if necessary.
35083b2aab18SMatthew Ahrens  */
35093b2aab18SMatthew Ahrens void
zfs_destroy_unmount_origin(const char * fsname)35103b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
35113b2aab18SMatthew Ahrens {
35123b2aab18SMatthew Ahrens 	int error;
35133b2aab18SMatthew Ahrens 	objset_t *os;
35143b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
35153b2aab18SMatthew Ahrens 
35163b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
35173b2aab18SMatthew Ahrens 	if (error != 0)
35183b2aab18SMatthew Ahrens 		return;
35193b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
35203b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3521*a1988827SMatthew Ahrens 		char originname[ZFS_MAX_DATASET_NAME_LEN];
35223b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
35233b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3524fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
35253b2aab18SMatthew Ahrens 	} else {
35263b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
35273b2aab18SMatthew Ahrens 	}
35281d452cf5Sahrens }
35291d452cf5Sahrens 
35303cb34c60Sahrens /*
35314445fffbSMatthew Ahrens  * innvl: {
35324445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
35334445fffbSMatthew Ahrens  *     (optional boolean) "defer"
35344445fffbSMatthew Ahrens  * }
35353cb34c60Sahrens  *
35364445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
35374445fffbSMatthew Ahrens  *
35383cb34c60Sahrens  */
353978f17100SMatthew Ahrens /* ARGSUSED */
35401d452cf5Sahrens static int
zfs_ioc_destroy_snaps(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)35414445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
35421d452cf5Sahrens {
35434445fffbSMatthew Ahrens 	nvlist_t *snaps;
354419b94df9SMatthew Ahrens 	nvpair_t *pair;
35454445fffbSMatthew Ahrens 	boolean_t defer;
35461d452cf5Sahrens 
35474445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3548be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
35494445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
355019b94df9SMatthew Ahrens 
35514445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35524445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
355378f17100SMatthew Ahrens 		(void) zfs_unmount_snap(nvpair_name(pair));
355419b94df9SMatthew Ahrens 	}
355519b94df9SMatthew Ahrens 
35563b2aab18SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
35571d452cf5Sahrens }
35581d452cf5Sahrens 
35593cb34c60Sahrens /*
356078f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
356178f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
356278f17100SMatthew Ahrens  *
356378f17100SMatthew Ahrens  * innvl: {
356478f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
356578f17100SMatthew Ahrens  * }
356678f17100SMatthew Ahrens  *
356778f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
356878f17100SMatthew Ahrens  *
356978f17100SMatthew Ahrens  */
357078f17100SMatthew Ahrens /* ARGSUSED */
357178f17100SMatthew Ahrens static int
zfs_ioc_bookmark(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)357278f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
357378f17100SMatthew Ahrens {
357478f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
357578f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
357678f17100SMatthew Ahrens 		char *snap_name;
357778f17100SMatthew Ahrens 
357878f17100SMatthew Ahrens 		/*
357978f17100SMatthew Ahrens 		 * Verify the snapshot argument.
358078f17100SMatthew Ahrens 		 */
358178f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
358278f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
358378f17100SMatthew Ahrens 
358478f17100SMatthew Ahrens 
358578f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
358678f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
358778f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
358878f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
358978f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
359078f17100SMatthew Ahrens 		}
359178f17100SMatthew Ahrens 	}
359278f17100SMatthew Ahrens 
359378f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
359478f17100SMatthew Ahrens }
359578f17100SMatthew Ahrens 
359678f17100SMatthew Ahrens /*
359778f17100SMatthew Ahrens  * innvl: {
359878f17100SMatthew Ahrens  *     property 1, property 2, ...
359978f17100SMatthew Ahrens  * }
360078f17100SMatthew Ahrens  *
360178f17100SMatthew Ahrens  * outnvl: {
360278f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
360378f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
360478f17100SMatthew Ahrens  * }
360578f17100SMatthew Ahrens  *
360678f17100SMatthew Ahrens  */
360778f17100SMatthew Ahrens static int
zfs_ioc_get_bookmarks(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)360878f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
360978f17100SMatthew Ahrens {
361078f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
361178f17100SMatthew Ahrens }
361278f17100SMatthew Ahrens 
361378f17100SMatthew Ahrens /*
361478f17100SMatthew Ahrens  * innvl: {
361578f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
361678f17100SMatthew Ahrens  * }
361778f17100SMatthew Ahrens  *
361878f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
361978f17100SMatthew Ahrens  *
362078f17100SMatthew Ahrens  */
362178f17100SMatthew Ahrens static int
zfs_ioc_destroy_bookmarks(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)362278f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
362378f17100SMatthew Ahrens     nvlist_t *outnvl)
362478f17100SMatthew Ahrens {
362578f17100SMatthew Ahrens 	int error, poollen;
362678f17100SMatthew Ahrens 
362778f17100SMatthew Ahrens 	poollen = strlen(poolname);
362878f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
362978f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
363078f17100SMatthew Ahrens 		const char *name = nvpair_name(pair);
363178f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
363278f17100SMatthew Ahrens 
363378f17100SMatthew Ahrens 		/*
363478f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
363578f17100SMatthew Ahrens 		 * must contain only valid characters.
363678f17100SMatthew Ahrens 		 */
363778f17100SMatthew Ahrens 		if (cp == NULL ||
363878f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
363978f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
364078f17100SMatthew Ahrens 
364178f17100SMatthew Ahrens 		/*
364278f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
364378f17100SMatthew Ahrens 		 */
364478f17100SMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
364578f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
364678f17100SMatthew Ahrens 			return (SET_ERROR(EXDEV));
364778f17100SMatthew Ahrens 	}
364878f17100SMatthew Ahrens 
364978f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
365078f17100SMatthew Ahrens 	return (error);
365178f17100SMatthew Ahrens }
365278f17100SMatthew Ahrens 
365378f17100SMatthew Ahrens /*
36543cb34c60Sahrens  * inputs:
36553cb34c60Sahrens  * zc_name		name of dataset to destroy
36563cb34c60Sahrens  * zc_objset_type	type of objset
3657842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
36583cb34c60Sahrens  *
36593cb34c60Sahrens  * outputs:		none
36603cb34c60Sahrens  */
36611d452cf5Sahrens static int
zfs_ioc_destroy(zfs_cmd_t * zc)36621d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36631d452cf5Sahrens {
3664681d9761SEric Taylor 	int err;
3665fc7a6e3fSWill Andrews 
3666fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3667fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3668fc7a6e3fSWill Andrews 		if (err != 0)
3669fc7a6e3fSWill Andrews 			return (err);
3670fc7a6e3fSWill Andrews 	}
3671fa9e4066Sahrens 
36723b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
36733b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36743b2aab18SMatthew Ahrens 	else
36753b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3676681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36775c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3678681d9761SEric Taylor 	return (err);
3679fa9e4066Sahrens }
3680fa9e4066Sahrens 
36813cb34c60Sahrens /*
3682a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
36833cb34c60Sahrens  *
3684a7027df1SMatthew Ahrens  * innvl is not used.
3685a7027df1SMatthew Ahrens  *
3686a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3687a7027df1SMatthew Ahrens  * }
36883cb34c60Sahrens  */
3689a7027df1SMatthew Ahrens /* ARGSUSED */
3690fa9e4066Sahrens static int
zfs_ioc_rollback(const char * fsname,nvlist_t * args,nvlist_t * outnvl)3691a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3692fa9e4066Sahrens {
3693ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
36943b2aab18SMatthew Ahrens 	int error;
36954ccbb6e7Sahrens 
3696a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3697503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
369847f263f4Sek110237 		if (error == 0) {
369947f263f4Sek110237 			int resume_err;
370047f263f4Sek110237 
3701a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3702a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
370347f263f4Sek110237 			error = error ? error : resume_err;
370447f263f4Sek110237 		}
37054ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
37064ccbb6e7Sahrens 	} else {
3707a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
37084ccbb6e7Sahrens 	}
37094ccbb6e7Sahrens 	return (error);
3710fa9e4066Sahrens }
3711fa9e4066Sahrens 
37123b2aab18SMatthew Ahrens static int
recursive_unmount(const char * fsname,void * arg)37133b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
37143b2aab18SMatthew Ahrens {
37153b2aab18SMatthew Ahrens 	const char *snapname = arg;
3716*a1988827SMatthew Ahrens 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
37173b2aab18SMatthew Ahrens 
37183b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3719fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
37203b2aab18SMatthew Ahrens }
37213b2aab18SMatthew Ahrens 
37223cb34c60Sahrens /*
37233cb34c60Sahrens  * inputs:
37243cb34c60Sahrens  * zc_name	old name of dataset
37253cb34c60Sahrens  * zc_value	new name of dataset
37263cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
37273cb34c60Sahrens  *
37283cb34c60Sahrens  * outputs:	none
37293cb34c60Sahrens  */
3730fa9e4066Sahrens static int
zfs_ioc_rename(zfs_cmd_t * zc)3731fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3732fa9e4066Sahrens {
37337f1f55eaSvb160487 	boolean_t recursive = zc->zc_cookie & 1;
37343b2aab18SMatthew Ahrens 	char *at;
3735cdf5b4caSmmusante 
3736e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3737f18faf3fSek110237 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
373854da3412SMarcel Telka 	    strchr(zc->zc_value, '%'))
3739be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3740fa9e4066Sahrens 
37413b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
37423b2aab18SMatthew Ahrens 	if (at != NULL) {
37433b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3744a0c1127bSSteven Hartland 		int error;
3745a0c1127bSSteven Hartland 
37463b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3747be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
37483b2aab18SMatthew Ahrens 		*at = '\0';
37493b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3750a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
37513b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
37523b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3753a0c1127bSSteven Hartland 			if (error != 0) {
3754a0c1127bSSteven Hartland 				*at = '@';
37553b2aab18SMatthew Ahrens 				return (error);
3756fa9e4066Sahrens 			}
3757a0c1127bSSteven Hartland 		}
3758a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3759a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3760a0c1127bSSteven Hartland 		*at = '@';
3761a0c1127bSSteven Hartland 
3762a0c1127bSSteven Hartland 		return (error);
37633b2aab18SMatthew Ahrens 	} else {
3764681d9761SEric Taylor 		if (zc->zc_objset_type == DMU_OST_ZVOL)
3765681d9761SEric Taylor 			(void) zvol_remove_minor(zc->zc_name);
37663b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
37673b2aab18SMatthew Ahrens 	}
3768fa9e4066Sahrens }
3769fa9e4066Sahrens 
377092241e0bSTom Erickson static int
zfs_check_settable(const char * dsname,nvpair_t * pair,cred_t * cr)377192241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
377292241e0bSTom Erickson {
377392241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
377492241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
377592241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
377692241e0bSTom Erickson 	uint64_t intval;
377792241e0bSTom Erickson 	int err;
377892241e0bSTom Erickson 
377992241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
378092241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
378192241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
378292241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
378392241e0bSTom Erickson 				return (err);
378492241e0bSTom Erickson 			return (0);
378592241e0bSTom Erickson 		}
378692241e0bSTom Erickson 
378792241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
378892241e0bSTom Erickson 			const char *perm = NULL;
378992241e0bSTom Erickson 			const char *uq_prefix =
379092241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
379192241e0bSTom Erickson 			const char *gq_prefix =
379292241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
379392241e0bSTom Erickson 
379492241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
379592241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
379692241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
379792241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
379892241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
379992241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
380092241e0bSTom Erickson 			} else {
380192241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3802be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
380392241e0bSTom Erickson 			}
380492241e0bSTom Erickson 
380592241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
380692241e0bSTom Erickson 				return (err);
380792241e0bSTom Erickson 			return (0);
380892241e0bSTom Erickson 		}
380992241e0bSTom Erickson 
3810be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
381192241e0bSTom Erickson 	}
381292241e0bSTom Erickson 
381392241e0bSTom Erickson 	if (issnap)
3814be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
381592241e0bSTom Erickson 
381692241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
381792241e0bSTom Erickson 		/*
381892241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
381992241e0bSTom Erickson 		 * format.
382092241e0bSTom Erickson 		 */
382192241e0bSTom Erickson 		nvlist_t *attrs;
382292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
382392241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
382492241e0bSTom Erickson 		    &pair) == 0);
382592241e0bSTom Erickson 	}
382692241e0bSTom Erickson 
382792241e0bSTom Erickson 	/*
382892241e0bSTom Erickson 	 * Check that this value is valid for this pool version
382992241e0bSTom Erickson 	 */
383092241e0bSTom Erickson 	switch (prop) {
383192241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
383292241e0bSTom Erickson 		/*
383392241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
383492241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
383592241e0bSTom Erickson 		 * we'll catch them later.
383692241e0bSTom Erickson 		 */
3837b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
383892241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
383992241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
384092241e0bSTom Erickson 			    zfs_earlier_version(dsname,
384192241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3842be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
384392241e0bSTom Erickson 			}
384492241e0bSTom Erickson 
384592241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
384692241e0bSTom Erickson 			    zfs_earlier_version(dsname,
384792241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3848be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
384992241e0bSTom Erickson 
3850a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3851a6f561b4SSašo Kiselkov 				spa_t *spa;
3852a6f561b4SSašo Kiselkov 
3853a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3854a6f561b4SSašo Kiselkov 					return (err);
3855a6f561b4SSašo Kiselkov 
38562acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
38572acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3858a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3859be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3860a6f561b4SSašo Kiselkov 				}
3861a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3862a6f561b4SSašo Kiselkov 			}
3863a6f561b4SSašo Kiselkov 
386492241e0bSTom Erickson 			/*
386592241e0bSTom Erickson 			 * If this is a bootable dataset then
386692241e0bSTom Erickson 			 * verify that the compression algorithm
386792241e0bSTom Erickson 			 * is supported for booting. We must return
386892241e0bSTom Erickson 			 * something other than ENOTSUP since it
386992241e0bSTom Erickson 			 * implies a downrev pool version.
387092241e0bSTom Erickson 			 */
387192241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
387292241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3873be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
387492241e0bSTom Erickson 			}
387592241e0bSTom Erickson 		}
387692241e0bSTom Erickson 		break;
387792241e0bSTom Erickson 
387892241e0bSTom Erickson 	case ZFS_PROP_COPIES:
387992241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3880be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
388192241e0bSTom Erickson 		break;
388292241e0bSTom Erickson 
388392241e0bSTom Erickson 	case ZFS_PROP_DEDUP:
388492241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3885be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
388692241e0bSTom Erickson 		break;
388792241e0bSTom Erickson 
3888b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
3889b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
3890b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
3891b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
3892b5152584SMatthew Ahrens 			spa_t *spa;
3893b5152584SMatthew Ahrens 
3894b5152584SMatthew Ahrens 			/*
3895b5152584SMatthew Ahrens 			 * If this is a bootable dataset then
3896b5152584SMatthew Ahrens 			 * the we don't allow large (>128K) blocks,
3897b5152584SMatthew Ahrens 			 * because GRUB doesn't support them.
3898b5152584SMatthew Ahrens 			 */
3899b5152584SMatthew Ahrens 			if (zfs_is_bootfs(dsname) &&
3900b5152584SMatthew Ahrens 			    intval > SPA_OLD_MAXBLOCKSIZE) {
3901b5152584SMatthew Ahrens 				return (SET_ERROR(EDOM));
3902b5152584SMatthew Ahrens 			}
3903b5152584SMatthew Ahrens 
3904b5152584SMatthew Ahrens 			/*
3905b5152584SMatthew Ahrens 			 * We don't allow setting the property above 1MB,
3906b5152584SMatthew Ahrens 			 * unless the tunable has been changed.
3907b5152584SMatthew Ahrens 			 */
3908b5152584SMatthew Ahrens 			if (intval > zfs_max_recordsize ||
3909b5152584SMatthew Ahrens 			    intval > SPA_MAXBLOCKSIZE)
3910b5152584SMatthew Ahrens 				return (SET_ERROR(EDOM));
3911b5152584SMatthew Ahrens 
3912b5152584SMatthew Ahrens 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3913b5152584SMatthew Ahrens 				return (err);
3914b5152584SMatthew Ahrens 
3915b5152584SMatthew Ahrens 			if (!spa_feature_is_enabled(spa,
3916b5152584SMatthew Ahrens 			    SPA_FEATURE_LARGE_BLOCKS)) {
3917b5152584SMatthew Ahrens 				spa_close(spa, FTAG);
3918b5152584SMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
3919b5152584SMatthew Ahrens 			}
3920b5152584SMatthew Ahrens 			spa_close(spa, FTAG);
3921b5152584SMatthew Ahrens 		}
3922b5152584SMatthew Ahrens 		break;
3923b5152584SMatthew Ahrens 
392492241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
392592241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3926be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
392792241e0bSTom Erickson 		break;
392892241e0bSTom Erickson 
392992241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
393092241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
393192241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
393292241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
393392241e0bSTom Erickson 			    zfs_earlier_version(dsname,
393492241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3935be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
393692241e0bSTom Erickson 		}
393792241e0bSTom Erickson 		break;
393892241e0bSTom Erickson 	}
393992241e0bSTom Erickson 
394092241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
394192241e0bSTom Erickson }
394292241e0bSTom Erickson 
394392241e0bSTom Erickson /*
3944a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3945a6f561b4SSašo Kiselkov  * multiple times.
3946a6f561b4SSašo Kiselkov  */
3947a6f561b4SSašo Kiselkov static int
zfs_prop_activate_feature_check(void * arg,dmu_tx_t * tx)39483b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3949a6f561b4SSašo Kiselkov {
39503b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39512acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3952a6f561b4SSašo Kiselkov 
39532acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
3954a6f561b4SSašo Kiselkov 		return (0);
3955a6f561b4SSašo Kiselkov 	else
3956be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3957a6f561b4SSašo Kiselkov }
3958a6f561b4SSašo Kiselkov 
3959a6f561b4SSašo Kiselkov /*
3960a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3961a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3962a6f561b4SSašo Kiselkov  */
3963a6f561b4SSašo Kiselkov static void
zfs_prop_activate_feature_sync(void * arg,dmu_tx_t * tx)39643b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3965a6f561b4SSašo Kiselkov {
39663b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39672acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3968a6f561b4SSašo Kiselkov 
39692acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
3970a6f561b4SSašo Kiselkov }
3971a6f561b4SSašo Kiselkov 
3972a6f561b4SSašo Kiselkov /*
39733b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
39743b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
39753b2aab18SMatthew Ahrens  * as being active.
39763b2aab18SMatthew Ahrens  */
39773b2aab18SMatthew Ahrens static int
zfs_prop_activate_feature(spa_t * spa,spa_feature_t feature)39782acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
39793b2aab18SMatthew Ahrens {
39803b2aab18SMatthew Ahrens 	int err;
39813b2aab18SMatthew Ahrens 
39823b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
39833b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
39843b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
39857d46dc6cSMatthew Ahrens 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
39863b2aab18SMatthew Ahrens 
39873b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
39883b2aab18SMatthew Ahrens 		return (err);
39893b2aab18SMatthew Ahrens 	else
39903b2aab18SMatthew Ahrens 		return (0);
39913b2aab18SMatthew Ahrens }
39923b2aab18SMatthew Ahrens 
39933b2aab18SMatthew Ahrens /*
399492241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
399592241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
399692241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
399792241e0bSTom Erickson  *
399892241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
399992241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
400092241e0bSTom Erickson  * of all the properties that failed a permission check along with the
400192241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
400292241e0bSTom Erickson  * returned errlist.
400392241e0bSTom Erickson  *
400492241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
400592241e0bSTom Erickson  * pointed at by errlist is NULL.
400692241e0bSTom Erickson  */
400792241e0bSTom Erickson static int
zfs_check_clearable(char * dataset,nvlist_t * props,nvlist_t ** errlist)400892241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4009745cd3c5Smaybee {
4010745cd3c5Smaybee 	zfs_cmd_t *zc;
401192241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
401292241e0bSTom Erickson 	nvlist_t *errors;
401392241e0bSTom Erickson 	int err, rv = 0;
4014745cd3c5Smaybee 
4015745cd3c5Smaybee 	if (props == NULL)
401692241e0bSTom Erickson 		return (0);
401792241e0bSTom Erickson 
401892241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
401992241e0bSTom Erickson 
4020745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4021745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
402292241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
402392241e0bSTom Erickson 	while (pair != NULL) {
402492241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
402592241e0bSTom Erickson 
402692241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
402792241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
40284445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
402992241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
403092241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
403192241e0bSTom Erickson 			    zc->zc_value, err) == 0);
403292241e0bSTom Erickson 		}
403392241e0bSTom Erickson 		pair = next_pair;
4034745cd3c5Smaybee 	}
4035745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
403692241e0bSTom Erickson 
403792241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
403892241e0bSTom Erickson 		nvlist_free(errors);
403992241e0bSTom Erickson 		errors = NULL;
404092241e0bSTom Erickson 	} else {
404192241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
4042745cd3c5Smaybee 	}
4043745cd3c5Smaybee 
404492241e0bSTom Erickson 	if (errlist == NULL)
404592241e0bSTom Erickson 		nvlist_free(errors);
404692241e0bSTom Erickson 	else
404792241e0bSTom Erickson 		*errlist = errors;
404892241e0bSTom Erickson 
404992241e0bSTom Erickson 	return (rv);
405092241e0bSTom Erickson }
405192241e0bSTom Erickson 
405292241e0bSTom Erickson static boolean_t
propval_equals(nvpair_t * p1,nvpair_t * p2)405392241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
405492241e0bSTom Erickson {
405592241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
405692241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
405792241e0bSTom Erickson 		nvlist_t *attrs;
405892241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
405992241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
406092241e0bSTom Erickson 		    &p1) == 0);
406192241e0bSTom Erickson 	}
406292241e0bSTom Erickson 
406392241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
406492241e0bSTom Erickson 		nvlist_t *attrs;
406592241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
406692241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
406792241e0bSTom Erickson 		    &p2) == 0);
406892241e0bSTom Erickson 	}
406992241e0bSTom Erickson 
407092241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
407192241e0bSTom Erickson 		return (B_FALSE);
407292241e0bSTom Erickson 
407392241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
407492241e0bSTom Erickson 		char *valstr1, *valstr2;
407592241e0bSTom Erickson 
407692241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
407792241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
407892241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
407992241e0bSTom Erickson 	} else {
408092241e0bSTom Erickson 		uint64_t intval1, intval2;
408192241e0bSTom Erickson 
408292241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
408392241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
408492241e0bSTom Erickson 		return (intval1 == intval2);
408592241e0bSTom Erickson 	}
408692241e0bSTom Erickson }
408792241e0bSTom Erickson 
408892241e0bSTom Erickson /*
408992241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
409092241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
409192241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
409292241e0bSTom Erickson  */
409392241e0bSTom Erickson static void
props_reduce(nvlist_t * props,nvlist_t * origprops)409492241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
409592241e0bSTom Erickson {
409692241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
409792241e0bSTom Erickson 
409892241e0bSTom Erickson 	if (origprops == NULL)
409992241e0bSTom Erickson 		return; /* all props need to be received */
410092241e0bSTom Erickson 
410192241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
410292241e0bSTom Erickson 	while (pair != NULL) {
410392241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
410492241e0bSTom Erickson 		nvpair_t *match;
410592241e0bSTom Erickson 
410692241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
410792241e0bSTom Erickson 
410892241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
410992241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
411092241e0bSTom Erickson 			goto next; /* need to set received value */
411192241e0bSTom Erickson 
411292241e0bSTom Erickson 		/* don't clear the existing received value */
411392241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
411492241e0bSTom Erickson 		/* don't bother receiving the property */
411592241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
411692241e0bSTom Erickson next:
411792241e0bSTom Erickson 		pair = next_pair;
411892241e0bSTom Erickson 	}
411992241e0bSTom Erickson }
412092241e0bSTom Erickson 
412192241e0bSTom Erickson #ifdef	DEBUG
412292241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
412392241e0bSTom Erickson #endif
412492241e0bSTom Erickson 
41253cb34c60Sahrens /*
41263cb34c60Sahrens  * inputs:
41273cb34c60Sahrens  * zc_name		name of containing filesystem
41283cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
41293cb34c60Sahrens  * zc_value		name of snapshot to create
41303cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
41313cb34c60Sahrens  * zc_cookie		file descriptor to recv from
41323cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
41333cb34c60Sahrens  * zc_guid		force flag
4134c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
4135c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
4136c1a50c7eSMatthew Ahrens  * zc_resumable		if data is incomplete assume sender will resume
41373cb34c60Sahrens  *
41383cb34c60Sahrens  * outputs:
41393cb34c60Sahrens  * zc_cookie		number of bytes read
414092241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
414192241e0bSTom Erickson  * zc_obj		zprop_errflags_t
4142c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
41433cb34c60Sahrens  */
4144fa9e4066Sahrens static int
zfs_ioc_recv(zfs_cmd_t * zc)41453cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4146fa9e4066Sahrens {
4147fa9e4066Sahrens 	file_t *fp;
41483cb34c60Sahrens 	dmu_recv_cookie_t drc;
4149f18faf3fSek110237 	boolean_t force = (boolean_t)zc->zc_guid;
415092241e0bSTom Erickson 	int fd;
415192241e0bSTom Erickson 	int error = 0;
415292241e0bSTom Erickson 	int props_error = 0;
415392241e0bSTom Erickson 	nvlist_t *errors;
41543cb34c60Sahrens 	offset_t off;
415592241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
415692241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
41573b2aab18SMatthew Ahrens 	char *origin = NULL;
41583cb34c60Sahrens 	char *tosnap;
4159*a1988827SMatthew Ahrens 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
416092241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
4161fa9e4066Sahrens 
41623ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4163f18faf3fSek110237 	    strchr(zc->zc_value, '@') == NULL ||
4164f18faf3fSek110237 	    strchr(zc->zc_value, '%'))
4165be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
41663ccfa83cSahrens 
41673cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
41683cb34c60Sahrens 	tosnap = strchr(tofs, '@');
416992241e0bSTom Erickson 	*tosnap++ = '\0';
41703cb34c60Sahrens 
41713cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
41723cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4173478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
41743cb34c60Sahrens 		return (error);
41753cb34c60Sahrens 
4176fa9e4066Sahrens 	fd = zc->zc_cookie;
4177fa9e4066Sahrens 	fp = getf(fd);
41783cb34c60Sahrens 	if (fp == NULL) {
41793cb34c60Sahrens 		nvlist_free(props);
4180be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
41813cb34c60Sahrens 	}
4182a2eea2e1Sahrens 
4183c1a50c7eSMatthew Ahrens 	errors = fnvlist_alloc();
418492241e0bSTom Erickson 
41853b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
41863b2aab18SMatthew Ahrens 		origin = zc->zc_string;
41873b2aab18SMatthew Ahrens 
41883b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
4189c1a50c7eSMatthew Ahrens 	    &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
41903b2aab18SMatthew Ahrens 	if (error != 0)
41913b2aab18SMatthew Ahrens 		goto out;
41923b2aab18SMatthew Ahrens 
41933b2aab18SMatthew Ahrens 	/*
41943b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
41953b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
41963b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
41973b2aab18SMatthew Ahrens 	 */
41983b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
41993b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
42003b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
42013b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
420292241e0bSTom Erickson 			first_recvd_props = B_TRUE;
420392241e0bSTom Erickson 
4204745cd3c5Smaybee 		/*
420592241e0bSTom Erickson 		 * If new received properties are supplied, they are to
420692241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
420792241e0bSTom Erickson 		 * away the existing ones.
4208745cd3c5Smaybee 		 */
42093b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
421092241e0bSTom Erickson 			nvlist_t *errlist = NULL;
421192241e0bSTom Erickson 			/*
421292241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
421392241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
421492241e0bSTom Erickson 			 *
421592241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
421692241e0bSTom Erickson 			 * special case where we blow away all local properties
421792241e0bSTom Erickson 			 * regardless.
421892241e0bSTom Erickson 			 */
421992241e0bSTom Erickson 			if (!first_recvd_props)
422092241e0bSTom Erickson 				props_reduce(props, origprops);
42213b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
422292241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
422392241e0bSTom Erickson 			nvlist_free(errlist);
4224745cd3c5Smaybee 
42253b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
422692241e0bSTom Erickson 			    first_recvd_props ? NULL : props) != 0)
422792241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
422892241e0bSTom Erickson 		} else {
422992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
423092241e0bSTom Erickson 		}
423192241e0bSTom Erickson 	}
423292241e0bSTom Erickson 
42333b2aab18SMatthew Ahrens 	if (props != NULL) {
42343b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
42353b2aab18SMatthew Ahrens 
42363b2aab18SMatthew Ahrens 		if (props_error == 0) {
423792241e0bSTom Erickson 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
42384445fffbSMatthew Ahrens 			    props, errors);
423992241e0bSTom Erickson 		}
42403b2aab18SMatthew Ahrens 	}
424192241e0bSTom Erickson 
42424445fffbSMatthew Ahrens 	if (zc->zc_nvlist_dst_size != 0 &&
42434445fffbSMatthew Ahrens 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
42444445fffbSMatthew Ahrens 	    put_nvlist(zc, errors) != 0)) {
42453cb34c60Sahrens 		/*
424692241e0bSTom Erickson 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
424792241e0bSTom Erickson 		 * size or supplied an invalid address.
42483cb34c60Sahrens 		 */
4249be6fd75aSMatthew Ahrens 		props_error = SET_ERROR(EINVAL);
4250745cd3c5Smaybee 	}
42513cb34c60Sahrens 
42523cb34c60Sahrens 	off = fp->f_offset;
4253c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4254c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
42553cb34c60Sahrens 
4256f4b94bdeSMatthew Ahrens 	if (error == 0) {
4257f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4258f4b94bdeSMatthew Ahrens 
4259f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4260f4b94bdeSMatthew Ahrens 			/* online recv */
4261f4b94bdeSMatthew Ahrens 			int end_err;
4262f18faf3fSek110237 
4263503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4264f4b94bdeSMatthew Ahrens 			/*
4265f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4266f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4267f4b94bdeSMatthew Ahrens 			 */
426891948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
42695c703fceSGeorge Wilson 			if (error == 0)
42705c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4271f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4272f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4273f4b94bdeSMatthew Ahrens 		} else {
427491948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
4275f18faf3fSek110237 		}
4276f4b94bdeSMatthew Ahrens 	}
42773cb34c60Sahrens 
42783cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
42793cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
42803cb34c60Sahrens 		fp->f_offset = off;
4281a2eea2e1Sahrens 
428292241e0bSTom Erickson #ifdef	DEBUG
428392241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
428492241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
428592241e0bSTom Erickson 		error = 1;
428692241e0bSTom Erickson 	}
428792241e0bSTom Erickson #endif
4288745cd3c5Smaybee 	/*
4289745cd3c5Smaybee 	 * On error, restore the original props.
4290745cd3c5Smaybee 	 */
42913b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
42923b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
429392241e0bSTom Erickson 			/*
429492241e0bSTom Erickson 			 * We failed to clear the received properties.
429592241e0bSTom Erickson 			 * Since we may have left a $recvd value on the
429692241e0bSTom Erickson 			 * system, we can't clear the $hasrecvd flag.
429792241e0bSTom Erickson 			 */
429892241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
429992241e0bSTom Erickson 		} else if (first_recvd_props) {
43003b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
430192241e0bSTom Erickson 		}
430292241e0bSTom Erickson 
430392241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
430492241e0bSTom Erickson 			/* We failed to stash the original properties. */
430592241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
430692241e0bSTom Erickson 		}
430792241e0bSTom Erickson 
430892241e0bSTom Erickson 		/*
430992241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
431092241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
431192241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
431292241e0bSTom Erickson 		 * first new-style receive.
431392241e0bSTom Erickson 		 */
431492241e0bSTom Erickson 		if (origprops != NULL &&
431592241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
431692241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
431792241e0bSTom Erickson 		    origprops, NULL) != 0) {
431892241e0bSTom Erickson 			/*
431992241e0bSTom Erickson 			 * We stashed the original properties but failed to
432092241e0bSTom Erickson 			 * restore them.
432192241e0bSTom Erickson 			 */
432292241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
432392241e0bSTom Erickson 		}
4324745cd3c5Smaybee 	}
4325745cd3c5Smaybee out:
4326745cd3c5Smaybee 	nvlist_free(props);
4327745cd3c5Smaybee 	nvlist_free(origprops);
432892241e0bSTom Erickson 	nvlist_free(errors);
4329fa9e4066Sahrens 	releasef(fd);
433092241e0bSTom Erickson 
433192241e0bSTom Erickson 	if (error == 0)
433292241e0bSTom Erickson 		error = props_error;
433392241e0bSTom Erickson 
4334fa9e4066Sahrens 	return (error);
4335fa9e4066Sahrens }
4336fa9e4066Sahrens 
43373cb34c60Sahrens /*
43383cb34c60Sahrens  * inputs:
43393cb34c60Sahrens  * zc_name	name of snapshot to send
43403cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4341a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4342a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4343a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
434419b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
434519b94df9SMatthew Ahrens  *		output size in zc_objset_type.
4346b5152584SMatthew Ahrens  * zc_flags	lzc_send_flags
43473cb34c60Sahrens  *
434878f17100SMatthew Ahrens  * outputs:
434978f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
43503cb34c60Sahrens  */
4351fa9e4066Sahrens static int
zfs_ioc_send(zfs_cmd_t * zc)43523cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4353fa9e4066Sahrens {
4354fa9e4066Sahrens 	int error;
43553cb34c60Sahrens 	offset_t off;
435619b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
43575d7b4d43SMatthew Ahrens 	boolean_t embedok = (zc->zc_flags & 0x1);
4358b5152584SMatthew Ahrens 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
4359fa9e4066Sahrens 
43603b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
43613b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
43623b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
43633b2aab18SMatthew Ahrens 
43643b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
43653b2aab18SMatthew Ahrens 		if (error != 0)
4366fa9e4066Sahrens 			return (error);
4367fa9e4066Sahrens 
43683b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
43693b2aab18SMatthew Ahrens 		if (error != 0) {
43703b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
43713b2aab18SMatthew Ahrens 			return (error);
43723b2aab18SMatthew Ahrens 		}
43733b2aab18SMatthew Ahrens 
43743b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
4375c1379625SJustin T. Gibbs 			zc->zc_fromobj =
4376c1379625SJustin T. Gibbs 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
43773b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
43783b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
43793b2aab18SMatthew Ahrens 	}
43803b2aab18SMatthew Ahrens 
43813b2aab18SMatthew Ahrens 	if (estimate) {
43823b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
43833b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
43843b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
43853b2aab18SMatthew Ahrens 
43863b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
43873b2aab18SMatthew Ahrens 		if (error != 0)
4388fa9e4066Sahrens 			return (error);
4389a7f53a56SChris Kirby 
43903b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
43913b2aab18SMatthew Ahrens 		if (error != 0) {
43923b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4393a7f53a56SChris Kirby 			return (error);
4394a7f53a56SChris Kirby 		}
4395a7f53a56SChris Kirby 
4396a7f53a56SChris Kirby 		if (zc->zc_fromobj != 0) {
43973b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
43983b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
43993b2aab18SMatthew Ahrens 			if (error != 0) {
44003b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
44013b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
4402a7f53a56SChris Kirby 				return (error);
4403a7f53a56SChris Kirby 			}
44044445fffbSMatthew Ahrens 		}
44054445fffbSMatthew Ahrens 
44064445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
440719b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
44083b2aab18SMatthew Ahrens 
44093b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
44103b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
44113b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
44123b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
441319b94df9SMatthew Ahrens 	} else {
441419b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
44153b2aab18SMatthew Ahrens 		if (fp == NULL)
4416be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4417fa9e4066Sahrens 
44183cb34c60Sahrens 		off = fp->f_offset;
44193b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4420b5152584SMatthew Ahrens 		    zc->zc_fromobj, embedok, large_block_ok,
4421b5152584SMatthew Ahrens 		    zc->zc_cookie, fp->f_vnode, &off);
4422fa9e4066Sahrens 
44233cb34c60Sahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
44243cb34c60Sahrens 			fp->f_offset = off;
4425fa9e4066Sahrens 		releasef(zc->zc_cookie);
442619b94df9SMatthew Ahrens 	}
4427fa9e4066Sahrens 	return (error);
4428fa9e4066Sahrens }
4429fa9e4066Sahrens 
44304e3c9f44SBill Pijewski /*
44314e3c9f44SBill Pijewski  * inputs:
44324e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
44334e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
44344e3c9f44SBill Pijewski  *
44354e3c9f44SBill Pijewski  * outputs:
44364e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
44374e3c9f44SBill Pijewski  */
44384e3c9f44SBill Pijewski static int
zfs_ioc_send_progress(zfs_cmd_t * zc)44394e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
44404e3c9f44SBill Pijewski {
44413b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
44424e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
44434e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
44444e3c9f44SBill Pijewski 	int error;
44454e3c9f44SBill Pijewski 
44463b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44473b2aab18SMatthew Ahrens 	if (error != 0)
44484e3c9f44SBill Pijewski 		return (error);
44494e3c9f44SBill Pijewski 
44503b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
44513b2aab18SMatthew Ahrens 	if (error != 0) {
44523b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
44533b2aab18SMatthew Ahrens 		return (error);
44543b2aab18SMatthew Ahrens 	}
44553b2aab18SMatthew Ahrens 
44564e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
44574e3c9f44SBill Pijewski 
44584e3c9f44SBill Pijewski 	/*
44594e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
44604e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
44614e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
44624e3c9f44SBill Pijewski 	 * that stream.
44634e3c9f44SBill Pijewski 	 */
44644e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
44654e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
44664e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
44674e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
44684e3c9f44SBill Pijewski 			break;
44694e3c9f44SBill Pijewski 	}
44704e3c9f44SBill Pijewski 
44714e3c9f44SBill Pijewski 	if (dsp != NULL)
44724e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
44734e3c9f44SBill Pijewski 	else
4474be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
44754e3c9f44SBill Pijewski 
44764e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
44774e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
44783b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
44794e3c9f44SBill Pijewski 	return (error);
44804e3c9f44SBill Pijewski }
44814e3c9f44SBill Pijewski 
4482ea8dc4b6Seschrock static int
zfs_ioc_inject_fault(zfs_cmd_t * zc)4483ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4484ea8dc4b6Seschrock {
4485ea8dc4b6Seschrock 	int id, error;
4486ea8dc4b6Seschrock 
4487ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4488ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4489ea8dc4b6Seschrock 
4490ea8dc4b6Seschrock 	if (error == 0)
4491ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4492ea8dc4b6Seschrock 
4493ea8dc4b6Seschrock 	return (error);
4494ea8dc4b6Seschrock }
4495ea8dc4b6Seschrock 
4496ea8dc4b6Seschrock static int
zfs_ioc_clear_fault(zfs_cmd_t * zc)4497ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4498ea8dc4b6Seschrock {
4499ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4500ea8dc4b6Seschrock }
4501ea8dc4b6Seschrock 
4502ea8dc4b6Seschrock static int
zfs_ioc_inject_list_next(zfs_cmd_t * zc)4503ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4504ea8dc4b6Seschrock {
4505ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4506ea8dc4b6Seschrock 	int error;
4507ea8dc4b6Seschrock 
4508ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4509ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4510ea8dc4b6Seschrock 
4511ea8dc4b6Seschrock 	zc->zc_guid = id;
4512ea8dc4b6Seschrock 
4513ea8dc4b6Seschrock 	return (error);
4514ea8dc4b6Seschrock }
4515ea8dc4b6Seschrock 
4516ea8dc4b6Seschrock static int
zfs_ioc_error_log(zfs_cmd_t * zc)4517ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4518ea8dc4b6Seschrock {
4519ea8dc4b6Seschrock 	spa_t *spa;
4520ea8dc4b6Seschrock 	int error;
4521e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4522ea8dc4b6Seschrock 
4523ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4524ea8dc4b6Seschrock 		return (error);
4525ea8dc4b6Seschrock 
4526e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4527ea8dc4b6Seschrock 	    &count);
4528ea8dc4b6Seschrock 	if (error == 0)
4529e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4530ea8dc4b6Seschrock 	else
4531e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4532ea8dc4b6Seschrock 
4533ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4534ea8dc4b6Seschrock 
4535ea8dc4b6Seschrock 	return (error);
4536ea8dc4b6Seschrock }
4537ea8dc4b6Seschrock 
4538ea8dc4b6Seschrock static int
zfs_ioc_clear(zfs_cmd_t * zc)4539ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4540ea8dc4b6Seschrock {
4541ea8dc4b6Seschrock 	spa_t *spa;
4542ea8dc4b6Seschrock 	vdev_t *vd;
4543bb8b5132Sek110237 	int error;
4544ea8dc4b6Seschrock 
4545b87f3af3Sperrin 	/*
4546b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4547b87f3af3Sperrin 	 */
4548b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4549b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4550b87f3af3Sperrin 	if (spa == NULL) {
4551b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4552be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4553b87f3af3Sperrin 	}
4554b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4555b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4556b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4557b87f3af3Sperrin 	}
4558468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4559b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4560b87f3af3Sperrin 
4561c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4562468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4563468c413aSTim Haley 	} else {
4564468c413aSTim Haley 		nvlist_t *policy;
4565468c413aSTim Haley 		nvlist_t *config = NULL;
4566468c413aSTim Haley 
4567468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4568be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4569468c413aSTim Haley 
4570468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4571468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4572468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4573468c413aSTim Haley 			    policy, &config);
4574468c413aSTim Haley 			if (config != NULL) {
45754b964adaSGeorge Wilson 				int err;
45764b964adaSGeorge Wilson 
45774b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
45784b964adaSGeorge Wilson 					error = err;
4579468c413aSTim Haley 				nvlist_free(config);
4580468c413aSTim Haley 			}
4581468c413aSTim Haley 			nvlist_free(policy);
4582468c413aSTim Haley 		}
4583468c413aSTim Haley 	}
4584468c413aSTim Haley 
45853b2aab18SMatthew Ahrens 	if (error != 0)
4586ea8dc4b6Seschrock 		return (error);
4587ea8dc4b6Seschrock 
45888f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4589ea8dc4b6Seschrock 
4590e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4591ea8dc4b6Seschrock 		vd = NULL;
4592c5904d13Seschrock 	} else {
4593c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4594fa94a07fSbrendan 		if (vd == NULL) {
4595e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4596ea8dc4b6Seschrock 			spa_close(spa, FTAG);
4597be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4598ea8dc4b6Seschrock 		}
4599fa94a07fSbrendan 	}
4600ea8dc4b6Seschrock 
4601e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4602ea8dc4b6Seschrock 
4603e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4604e14bb325SJeff Bonwick 
4605e14bb325SJeff Bonwick 	/*
4606e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4607e14bb325SJeff Bonwick 	 */
460854d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4609be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4610ea8dc4b6Seschrock 
4611ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4612ea8dc4b6Seschrock 
461354d692b7SGeorge Wilson 	return (error);
4614ea8dc4b6Seschrock }
4615ea8dc4b6Seschrock 
46164263d13fSGeorge Wilson static int
zfs_ioc_pool_reopen(zfs_cmd_t * zc)46174263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
46184263d13fSGeorge Wilson {
46194263d13fSGeorge Wilson 	spa_t *spa;
46204263d13fSGeorge Wilson 	int error;
46214263d13fSGeorge Wilson 
46224263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
46233b2aab18SMatthew Ahrens 	if (error != 0)
46244263d13fSGeorge Wilson 		return (error);
46254263d13fSGeorge Wilson 
46264263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4627d6afdce2SGeorge Wilson 
4628d6afdce2SGeorge Wilson 	/*
4629d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4630d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4631d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4632d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4633d6afdce2SGeorge Wilson 	 */
4634d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
46354263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4636d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4637d6afdce2SGeorge Wilson 
46384263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
46394263d13fSGeorge Wilson 	spa_close(spa, FTAG);
46404263d13fSGeorge Wilson 	return (0);
46414263d13fSGeorge Wilson }
46423cb34c60Sahrens /*
46433cb34c60Sahrens  * inputs:
46443cb34c60Sahrens  * zc_name	name of filesystem
46453cb34c60Sahrens  * zc_value	name of origin snapshot
46463cb34c60Sahrens  *
4647681d9761SEric Taylor  * outputs:
4648681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
46493cb34c60Sahrens  */
4650ea8dc4b6Seschrock static int
zfs_ioc_promote(zfs_cmd_t * zc)465199653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
465299653d4eSeschrock {
46530b69c2f0Sahrens 	char *cp;
46540b69c2f0Sahrens 
46550b69c2f0Sahrens 	/*
46560b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
46570b69c2f0Sahrens 	 * it's easier.
46580b69c2f0Sahrens 	 */
4659e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
46600b69c2f0Sahrens 	if (cp)
46610b69c2f0Sahrens 		*cp = '\0';
4662e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
46633b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4664681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
466599653d4eSeschrock }
466699653d4eSeschrock 
4667ecd6cf80Smarks /*
466814843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
466914843421SMatthew Ahrens  *
467014843421SMatthew Ahrens  * inputs:
467114843421SMatthew Ahrens  * zc_name	name of filesystem
467214843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
467314843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
467414843421SMatthew Ahrens  * zc_guid	RID/UID/GID
467514843421SMatthew Ahrens  *
467614843421SMatthew Ahrens  * outputs:
467714843421SMatthew Ahrens  * zc_cookie	property value
467814843421SMatthew Ahrens  */
467914843421SMatthew Ahrens static int
zfs_ioc_userspace_one(zfs_cmd_t * zc)468014843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
468114843421SMatthew Ahrens {
468214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
468314843421SMatthew Ahrens 	int error;
468414843421SMatthew Ahrens 
468514843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4686be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
468714843421SMatthew Ahrens 
46881412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
46893b2aab18SMatthew Ahrens 	if (error != 0)
469014843421SMatthew Ahrens 		return (error);
469114843421SMatthew Ahrens 
469214843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
469314843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
469414843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
469514843421SMatthew Ahrens 
469614843421SMatthew Ahrens 	return (error);
469714843421SMatthew Ahrens }
469814843421SMatthew Ahrens 
469914843421SMatthew Ahrens /*
470014843421SMatthew Ahrens  * inputs:
470114843421SMatthew Ahrens  * zc_name		name of filesystem
470214843421SMatthew Ahrens  * zc_cookie		zap cursor
470314843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
470414843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
470514843421SMatthew Ahrens  *
470614843421SMatthew Ahrens  * outputs:
470714843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
470814843421SMatthew Ahrens  * zc_cookie	zap cursor
470914843421SMatthew Ahrens  */
471014843421SMatthew Ahrens static int
zfs_ioc_userspace_many(zfs_cmd_t * zc)471114843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
471214843421SMatthew Ahrens {
471314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4714eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
471514843421SMatthew Ahrens 
4716eeb85002STim Haley 	if (bufsize <= 0)
4717be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4718eeb85002STim Haley 
47191412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
47203b2aab18SMatthew Ahrens 	if (error != 0)
472114843421SMatthew Ahrens 		return (error);
472214843421SMatthew Ahrens 
472314843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
472414843421SMatthew Ahrens 
472514843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
472614843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
472714843421SMatthew Ahrens 
472814843421SMatthew Ahrens 	if (error == 0) {
472914843421SMatthew Ahrens 		error = xcopyout(buf,
473014843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
473114843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
473214843421SMatthew Ahrens 	}
473314843421SMatthew Ahrens 	kmem_free(buf, bufsize);
473414843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
473514843421SMatthew Ahrens 
473614843421SMatthew Ahrens 	return (error);
473714843421SMatthew Ahrens }
473814843421SMatthew Ahrens 
473914843421SMatthew Ahrens /*
474014843421SMatthew Ahrens  * inputs:
474114843421SMatthew Ahrens  * zc_name		name of filesystem
474214843421SMatthew Ahrens  *
474314843421SMatthew Ahrens  * outputs:
474414843421SMatthew Ahrens  * none
474514843421SMatthew Ahrens  */
474614843421SMatthew Ahrens static int
zfs_ioc_userspace_upgrade(zfs_cmd_t * zc)474714843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
474814843421SMatthew Ahrens {
474914843421SMatthew Ahrens 	objset_t *os;
47501195e687SMark J Musante 	int error = 0;
475114843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
475214843421SMatthew Ahrens 
475314843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4754503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
475514843421SMatthew Ahrens 			/*
475614843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
475714843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
475814843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
475914843421SMatthew Ahrens 			 */
4760503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
476191948b51SKeith M Wesolowski 			if (error == 0) {
476291948b51SKeith M Wesolowski 				dmu_objset_refresh_ownership(zfsvfs->z_os,
476391948b51SKeith M Wesolowski 				    zfsvfs);
4764503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
476514843421SMatthew Ahrens 			}
476691948b51SKeith M Wesolowski 		}
476714843421SMatthew Ahrens 		if (error == 0)
476814843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
476914843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
477014843421SMatthew Ahrens 	} else {
4771503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4772503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
47733b2aab18SMatthew Ahrens 		if (error != 0)
477414843421SMatthew Ahrens 			return (error);
477514843421SMatthew Ahrens 
477614843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4777503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
477814843421SMatthew Ahrens 	}
477914843421SMatthew Ahrens 
478014843421SMatthew Ahrens 	return (error);
478114843421SMatthew Ahrens }
478214843421SMatthew Ahrens 
478314843421SMatthew Ahrens /*
4784ecd6cf80Smarks  * We don't want to have a hard dependency
4785ecd6cf80Smarks  * against some special symbols in sharefs
4786da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4787ecd6cf80Smarks  * the first file system is shared.
4788da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4789ecd6cf80Smarks  */
4790da6c28aaSamw int (*znfsexport_fs)(void *arg);
4791ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4792da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4793ecd6cf80Smarks 
4794da6c28aaSamw int zfs_nfsshare_inited;
4795da6c28aaSamw int zfs_smbshare_inited;
4796da6c28aaSamw 
4797ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4798ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4799da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4800ecd6cf80Smarks kmutex_t zfs_share_lock;
4801ecd6cf80Smarks 
4802ecd6cf80Smarks static int
zfs_init_sharefs()4803da6c28aaSamw zfs_init_sharefs()
4804da6c28aaSamw {
4805da6c28aaSamw 	int error;
4806da6c28aaSamw 
4807da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4808da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4809da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4810da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4811da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4812be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4813da6c28aaSamw 	}
4814da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4815da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4816da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4817be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4818da6c28aaSamw 	}
4819da6c28aaSamw 	return (0);
4820da6c28aaSamw }
4821da6c28aaSamw 
4822da6c28aaSamw static int
zfs_ioc_share(zfs_cmd_t * zc)4823ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4824ecd6cf80Smarks {
4825ecd6cf80Smarks 	int error;
4826ecd6cf80Smarks 	int opcode;
4827ecd6cf80Smarks 
4828da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4829da6c28aaSamw 	case ZFS_SHARE_NFS:
4830da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4831da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4832ecd6cf80Smarks 			mutex_enter(&zfs_share_lock);
4833da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4834da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4835ecd6cf80Smarks 				mutex_exit(&zfs_share_lock);
4836be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4837ecd6cf80Smarks 			}
4838da6c28aaSamw 			if (znfsexport_fs == NULL &&
4839da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4840da6c28aaSamw 			    ddi_modsym(nfs_mod,
4841da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4842ecd6cf80Smarks 				mutex_exit(&zfs_share_lock);
4843be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4844ecd6cf80Smarks 			}
4845da6c28aaSamw 			error = zfs_init_sharefs();
48463b2aab18SMatthew Ahrens 			if (error != 0) {
4847da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4848be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4849da6c28aaSamw 			}
4850da6c28aaSamw 			zfs_nfsshare_inited = 1;
4851da6c28aaSamw 			mutex_exit(&zfs_share_lock);
4852da6c28aaSamw 		}
4853da6c28aaSamw 		break;
4854da6c28aaSamw 	case ZFS_SHARE_SMB:
4855da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4856da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4857da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4858da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4859da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4860da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4861da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4862be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4863da6c28aaSamw 			}
4864da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4865da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4866faa1795aSjb150015 			    "smb_server_share", &error)) == NULL)) {
4867da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4868be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4869da6c28aaSamw 			}
4870da6c28aaSamw 			error = zfs_init_sharefs();
48713b2aab18SMatthew Ahrens 			if (error != 0) {
4872da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4873be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4874da6c28aaSamw 			}
4875da6c28aaSamw 			zfs_smbshare_inited = 1;
4876da6c28aaSamw 			mutex_exit(&zfs_share_lock);
4877da6c28aaSamw 		}
4878da6c28aaSamw 		break;
4879da6c28aaSamw 	default:
4880be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4881da6c28aaSamw 	}
4882ecd6cf80Smarks 
4883da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4884da6c28aaSamw 	case ZFS_SHARE_NFS:
4885da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4886da6c28aaSamw 		if (error =
4887da6c28aaSamw 		    znfsexport_fs((void *)
4888da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4889ecd6cf80Smarks 			return (error);
4890da6c28aaSamw 		break;
4891da6c28aaSamw 	case ZFS_SHARE_SMB:
4892da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4893da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4894da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4895da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4896da6c28aaSamw 		    B_TRUE: B_FALSE)) {
4897da6c28aaSamw 			return (error);
4898da6c28aaSamw 		}
4899da6c28aaSamw 		break;
4900da6c28aaSamw 	}
4901ecd6cf80Smarks 
4902da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4903da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4904ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4905ecd6cf80Smarks 
4906da6c28aaSamw 	/*
4907da6c28aaSamw 	 * Add or remove share from sharetab
4908da6c28aaSamw 	 */
4909ecd6cf80Smarks 	error = zshare_fs(opcode,
4910ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4911ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4912ecd6cf80Smarks 
4913ecd6cf80Smarks 	return (error);
4914ecd6cf80Smarks 
4915ecd6cf80Smarks }
4916ecd6cf80Smarks 
4917743a77edSAlan Wright ace_t full_access[] = {
4918743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4919743a77edSAlan Wright };
4920743a77edSAlan Wright 
4921743a77edSAlan Wright /*
492299d5e173STim Haley  * inputs:
492399d5e173STim Haley  * zc_name		name of containing filesystem
492499d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
492599d5e173STim Haley  *
492699d5e173STim Haley  * outputs:
492799d5e173STim Haley  * zc_obj		next in-use object #
492899d5e173STim Haley  */
492999d5e173STim Haley static int
zfs_ioc_next_obj(zfs_cmd_t * zc)493099d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
493199d5e173STim Haley {
493299d5e173STim Haley 	objset_t *os = NULL;
493399d5e173STim Haley 	int error;
493499d5e173STim Haley 
493599d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
49363b2aab18SMatthew Ahrens 	if (error != 0)
493799d5e173STim Haley 		return (error);
493899d5e173STim Haley 
493999d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4940c1379625SJustin T. Gibbs 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
494199d5e173STim Haley 
494299d5e173STim Haley 	dmu_objset_rele(os, FTAG);
494399d5e173STim Haley 	return (error);
494499d5e173STim Haley }
494599d5e173STim Haley 
494699d5e173STim Haley /*
494799d5e173STim Haley  * inputs:
494899d5e173STim Haley  * zc_name		name of filesystem
494999d5e173STim Haley  * zc_value		prefix name for snapshot
495099d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
495199d5e173STim Haley  *
495299d5e173STim Haley  * outputs:
49534445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
495499d5e173STim Haley  */
495599d5e173STim Haley static int
zfs_ioc_tmp_snapshot(zfs_cmd_t * zc)495699d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
495799d5e173STim Haley {
495899d5e173STim Haley 	char *snap_name;
49593b2aab18SMatthew Ahrens 	char *hold_name;
496099d5e173STim Haley 	int error;
49613b2aab18SMatthew Ahrens 	minor_t minor;
496299d5e173STim Haley 
49633b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
49643b2aab18SMatthew Ahrens 	if (error != 0)
496599d5e173STim Haley 		return (error);
496699d5e173STim Haley 
49673b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
49683b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
49693b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
49703b2aab18SMatthew Ahrens 
49713b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
49723b2aab18SMatthew Ahrens 	    hold_name);
49733b2aab18SMatthew Ahrens 	if (error == 0)
49743b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
497599d5e173STim Haley 	strfree(snap_name);
49763b2aab18SMatthew Ahrens 	strfree(hold_name);
49773b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
49783b2aab18SMatthew Ahrens 	return (error);
497999d5e173STim Haley }
498099d5e173STim Haley 
498199d5e173STim Haley /*
498299d5e173STim Haley  * inputs:
498399d5e173STim Haley  * zc_name		name of "to" snapshot
498499d5e173STim Haley  * zc_value		name of "from" snapshot
498599d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
498699d5e173STim Haley  *
498799d5e173STim Haley  * outputs:
498899d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
498999d5e173STim Haley  */
499099d5e173STim Haley static int
zfs_ioc_diff(zfs_cmd_t * zc)499199d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
499299d5e173STim Haley {
499399d5e173STim Haley 	file_t *fp;
499499d5e173STim Haley 	offset_t off;
499599d5e173STim Haley 	int error;
499699d5e173STim Haley 
499799d5e173STim Haley 	fp = getf(zc->zc_cookie);
49983b2aab18SMatthew Ahrens 	if (fp == NULL)
4999be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
500099d5e173STim Haley 
500199d5e173STim Haley 	off = fp->f_offset;
500299d5e173STim Haley 
50033b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
500499d5e173STim Haley 
500599d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
500699d5e173STim Haley 		fp->f_offset = off;
500799d5e173STim Haley 	releasef(zc->zc_cookie);
500899d5e173STim Haley 
500999d5e173STim Haley 	return (error);
501099d5e173STim Haley }
501199d5e173STim Haley 
501299d5e173STim Haley /*
5013743a77edSAlan Wright  * Remove all ACL files in shares dir
5014743a77edSAlan Wright  */
5015743a77edSAlan Wright static int
zfs_smb_acl_purge(znode_t * dzp)5016743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5017743a77edSAlan Wright {
5018743a77edSAlan Wright 	zap_cursor_t	zc;
5019743a77edSAlan Wright 	zap_attribute_t	zap;
5020743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5021743a77edSAlan Wright 	int error;
5022743a77edSAlan Wright 
5023743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5024743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5025743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
5026743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5027743a77edSAlan Wright 		    NULL, 0)) != 0)
5028743a77edSAlan Wright 			break;
5029743a77edSAlan Wright 	}
5030743a77edSAlan Wright 	zap_cursor_fini(&zc);
5031743a77edSAlan Wright 	return (error);
5032743a77edSAlan Wright }
5033743a77edSAlan Wright 
5034743a77edSAlan Wright static int
zfs_ioc_smb_acl(zfs_cmd_t * zc)5035743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5036743a77edSAlan Wright {
5037743a77edSAlan Wright 	vnode_t *vp;
5038743a77edSAlan Wright 	znode_t *dzp;
5039743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
5040743a77edSAlan Wright 	znode_t *sharedir;
5041743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
5042743a77edSAlan Wright 	nvlist_t *nvlist;
5043743a77edSAlan Wright 	char *src, *target;
5044743a77edSAlan Wright 	vattr_t vattr;
5045743a77edSAlan Wright 	vsecattr_t vsec;
5046743a77edSAlan Wright 	int error = 0;
5047743a77edSAlan Wright 
5048743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5049743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
5050743a77edSAlan Wright 		return (error);
5051743a77edSAlan Wright 
5052743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
5053743a77edSAlan Wright 
5054743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5055743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5056743a77edSAlan Wright 	    zc->zc_name) != 0)) {
5057743a77edSAlan Wright 		VN_RELE(vp);
5058be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5059743a77edSAlan Wright 	}
5060743a77edSAlan Wright 
5061743a77edSAlan Wright 	dzp = VTOZ(vp);
5062743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
5063743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
5064743a77edSAlan Wright 
50659e1320c0SMark Shellenbaum 	/*
50669e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
50679e1320c0SMark Shellenbaum 	 */
50689e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
50699e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
50709e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
50719e1320c0SMark Shellenbaum 
50729e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
50739e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
50749e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
50759e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
50769e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
50773b2aab18SMatthew Ahrens 		if (error != 0) {
50789e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
50799e1320c0SMark Shellenbaum 		} else {
50809e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
50819e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
50829e1320c0SMark Shellenbaum 		}
50833b2aab18SMatthew Ahrens 		if (error != 0) {
50849e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
50859e1320c0SMark Shellenbaum 			VN_RELE(vp);
50869e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
50879e1320c0SMark Shellenbaum 			return (error);
50889e1320c0SMark Shellenbaum 		}
50899e1320c0SMark Shellenbaum 	}
50909e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
50919e1320c0SMark Shellenbaum 
50929e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5093743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
50949e1320c0SMark Shellenbaum 		VN_RELE(vp);
5095743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5096743a77edSAlan Wright 		return (error);
5097743a77edSAlan Wright 	}
5098743a77edSAlan Wright 
5099743a77edSAlan Wright 	switch (zc->zc_cookie) {
5100743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5101743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5102743a77edSAlan Wright 		vattr.va_type = VREG;
5103743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5104743a77edSAlan Wright 		vattr.va_uid = 0;
5105743a77edSAlan Wright 		vattr.va_gid = 0;
5106743a77edSAlan Wright 
5107743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5108743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5109743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5110743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5111743a77edSAlan Wright 
5112743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5113743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5114743a77edSAlan Wright 		if (resourcevp)
5115743a77edSAlan Wright 			VN_RELE(resourcevp);
5116743a77edSAlan Wright 		break;
5117743a77edSAlan Wright 
5118743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5119743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5120743a77edSAlan Wright 		    NULL, 0);
5121743a77edSAlan Wright 		break;
5122743a77edSAlan Wright 
5123743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5124743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5125478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5126743a77edSAlan Wright 			VN_RELE(vp);
5127743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5128743a77edSAlan Wright 			return (error);
5129743a77edSAlan Wright 		}
5130743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5131743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5132743a77edSAlan Wright 		    &target)) {
5133743a77edSAlan Wright 			VN_RELE(vp);
513489459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5135743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
51361195e687SMark J Musante 			nvlist_free(nvlist);
5137743a77edSAlan Wright 			return (error);
5138743a77edSAlan Wright 		}
5139743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5140743a77edSAlan Wright 		    kcred, NULL, 0);
5141743a77edSAlan Wright 		nvlist_free(nvlist);
5142743a77edSAlan Wright 		break;
5143743a77edSAlan Wright 
5144743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5145743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5146743a77edSAlan Wright 		break;
5147743a77edSAlan Wright 
5148743a77edSAlan Wright 	default:
5149be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5150743a77edSAlan Wright 		break;
5151743a77edSAlan Wright 	}
5152743a77edSAlan Wright 
5153743a77edSAlan Wright 	VN_RELE(vp);
5154743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5155743a77edSAlan Wright 
5156743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5157743a77edSAlan Wright 
5158743a77edSAlan Wright 	return (error);
5159743a77edSAlan Wright }
5160743a77edSAlan Wright 
5161ecd6cf80Smarks /*
51623b2aab18SMatthew Ahrens  * innvl: {
51633b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
51643b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
51653b2aab18SMatthew Ahrens  * }
5166842727c2SChris Kirby  *
51673b2aab18SMatthew Ahrens  * outnvl: {
51683b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
51693b2aab18SMatthew Ahrens  *     ...
51703b2aab18SMatthew Ahrens  * }
5171842727c2SChris Kirby  */
51723b2aab18SMatthew Ahrens /* ARGSUSED */
5173842727c2SChris Kirby static int
zfs_ioc_hold(const char * pool,nvlist_t * args,nvlist_t * errlist)51743b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5175842727c2SChris Kirby {
5176752fd8daSJosef 'Jeff' Sipek 	nvpair_t *pair;
51773b2aab18SMatthew Ahrens 	nvlist_t *holds;
51783b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5179a7f53a56SChris Kirby 	int error;
5180a7f53a56SChris Kirby 	minor_t minor = 0;
5181842727c2SChris Kirby 
51823b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
51833b2aab18SMatthew Ahrens 	if (error != 0)
5184be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5185842727c2SChris Kirby 
5186752fd8daSJosef 'Jeff' Sipek 	/* make sure the user didn't pass us any invalid (empty) tags */
5187752fd8daSJosef 'Jeff' Sipek 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5188752fd8daSJosef 'Jeff' Sipek 	    pair = nvlist_next_nvpair(holds, pair)) {
5189752fd8daSJosef 'Jeff' Sipek 		char *htag;
5190752fd8daSJosef 'Jeff' Sipek 
5191752fd8daSJosef 'Jeff' Sipek 		error = nvpair_value_string(pair, &htag);
5192752fd8daSJosef 'Jeff' Sipek 		if (error != 0)
5193752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(error));
5194752fd8daSJosef 'Jeff' Sipek 
5195752fd8daSJosef 'Jeff' Sipek 		if (strlen(htag) == 0)
5196752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(EINVAL));
5197752fd8daSJosef 'Jeff' Sipek 	}
5198752fd8daSJosef 'Jeff' Sipek 
51993b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
52003b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
52013b2aab18SMatthew Ahrens 		if (error != 0)
5202a7f53a56SChris Kirby 			return (error);
5203a7f53a56SChris Kirby 	}
5204a7f53a56SChris Kirby 
52053b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
52063b2aab18SMatthew Ahrens 	if (minor != 0)
52073b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5208a7f53a56SChris Kirby 	return (error);
5209842727c2SChris Kirby }
5210842727c2SChris Kirby 
5211842727c2SChris Kirby /*
52123b2aab18SMatthew Ahrens  * innvl is not used.
5213842727c2SChris Kirby  *
52143b2aab18SMatthew Ahrens  * outnvl: {
52153b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
52163b2aab18SMatthew Ahrens  *    ...
52173b2aab18SMatthew Ahrens  * }
5218842727c2SChris Kirby  */
52193b2aab18SMatthew Ahrens /* ARGSUSED */
5220842727c2SChris Kirby static int
zfs_ioc_get_holds(const char * snapname,nvlist_t * args,nvlist_t * outnvl)52213b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5222842727c2SChris Kirby {
52233b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5224842727c2SChris Kirby }
5225842727c2SChris Kirby 
5226842727c2SChris Kirby /*
52273b2aab18SMatthew Ahrens  * innvl: {
52283b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
52293b2aab18SMatthew Ahrens  *     ...
52303b2aab18SMatthew Ahrens  * }
5231842727c2SChris Kirby  *
52323b2aab18SMatthew Ahrens  * outnvl: {
52333b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
52343b2aab18SMatthew Ahrens  *     ...
52353b2aab18SMatthew Ahrens  * }
5236842727c2SChris Kirby  */
52373b2aab18SMatthew Ahrens /* ARGSUSED */
5238842727c2SChris Kirby static int
zfs_ioc_release(const char * pool,nvlist_t * holds,nvlist_t * errlist)52393b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5240842727c2SChris Kirby {
52413b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5242842727c2SChris Kirby }
5243842727c2SChris Kirby 
5244842727c2SChris Kirby /*
524519b94df9SMatthew Ahrens  * inputs:
524619b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
524719b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
524819b94df9SMatthew Ahrens  *
524919b94df9SMatthew Ahrens  * outputs:
525019b94df9SMatthew Ahrens  * zc_cookie		space in bytes
525119b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
525219b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
525319b94df9SMatthew Ahrens  */
525419b94df9SMatthew Ahrens static int
zfs_ioc_space_written(zfs_cmd_t * zc)525519b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
525619b94df9SMatthew Ahrens {
525719b94df9SMatthew Ahrens 	int error;
52583b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
525919b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
526019b94df9SMatthew Ahrens 
52613b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
526219b94df9SMatthew Ahrens 	if (error != 0)
526319b94df9SMatthew Ahrens 		return (error);
52643b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
52653b2aab18SMatthew Ahrens 	if (error != 0) {
52663b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
52673b2aab18SMatthew Ahrens 		return (error);
52683b2aab18SMatthew Ahrens 	}
52693b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
527019b94df9SMatthew Ahrens 	if (error != 0) {
527119b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
52723b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
527319b94df9SMatthew Ahrens 		return (error);
527419b94df9SMatthew Ahrens 	}
527519b94df9SMatthew Ahrens 
527619b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
527719b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
527819b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
527919b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
52803b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
528119b94df9SMatthew Ahrens 	return (error);
528219b94df9SMatthew Ahrens }
52833b2aab18SMatthew Ahrens 
528419b94df9SMatthew Ahrens /*
52854445fffbSMatthew Ahrens  * innvl: {
52864445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
52874445fffbSMatthew Ahrens  * }
528819b94df9SMatthew Ahrens  *
52894445fffbSMatthew Ahrens  * outnvl: {
52904445fffbSMatthew Ahrens  *     "used" -> space in bytes
52914445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
52924445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
52934445fffbSMatthew Ahrens  * }
529419b94df9SMatthew Ahrens  */
529519b94df9SMatthew Ahrens static int
zfs_ioc_space_snaps(const char * lastsnap,nvlist_t * innvl,nvlist_t * outnvl)52964445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
529719b94df9SMatthew Ahrens {
529819b94df9SMatthew Ahrens 	int error;
52993b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
530019b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
53014445fffbSMatthew Ahrens 	char *firstsnap;
53024445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
530319b94df9SMatthew Ahrens 
53044445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5305be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
53064445fffbSMatthew Ahrens 
53073b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
530819b94df9SMatthew Ahrens 	if (error != 0)
530919b94df9SMatthew Ahrens 		return (error);
53103b2aab18SMatthew Ahrens 
53113b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
531224218bebSAndriy Gapon 	if (error == 0 && !new->ds_is_snapshot) {
531324218bebSAndriy Gapon 		dsl_dataset_rele(new, FTAG);
531424218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
531524218bebSAndriy Gapon 	}
53163b2aab18SMatthew Ahrens 	if (error != 0) {
53173b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
53183b2aab18SMatthew Ahrens 		return (error);
53193b2aab18SMatthew Ahrens 	}
53203b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
532124218bebSAndriy Gapon 	if (error == 0 && !old->ds_is_snapshot) {
532224218bebSAndriy Gapon 		dsl_dataset_rele(old, FTAG);
532324218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
532424218bebSAndriy Gapon 	}
532519b94df9SMatthew Ahrens 	if (error != 0) {
532619b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
53273b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
532819b94df9SMatthew Ahrens 		return (error);
532919b94df9SMatthew Ahrens 	}
533019b94df9SMatthew Ahrens 
53314445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
533219b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
533319b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
53343b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
53354445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
53364445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
53374445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
533819b94df9SMatthew Ahrens 	return (error);
533919b94df9SMatthew Ahrens }
534019b94df9SMatthew Ahrens 
534119b94df9SMatthew Ahrens /*
53424445fffbSMatthew Ahrens  * innvl: {
53434445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
53444445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
5345b5152584SMatthew Ahrens  *     (optional) "largeblockok" -> (value ignored)
5346b5152584SMatthew Ahrens  *         indicates that blocks > 128KB are permitted
53475d7b4d43SMatthew Ahrens  *     (optional) "embedok" -> (value ignored)
53485d7b4d43SMatthew Ahrens  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
5349c1a50c7eSMatthew Ahrens  *     (optional) "resume_object" and "resume_offset" -> (uint64)
5350c1a50c7eSMatthew Ahrens  *         if present, resume send stream from specified object and offset.
53514445fffbSMatthew Ahrens  * }
53524445fffbSMatthew Ahrens  *
53534445fffbSMatthew Ahrens  * outnvl is unused
5354ecd6cf80Smarks  */
53554445fffbSMatthew Ahrens /* ARGSUSED */
53564445fffbSMatthew Ahrens static int
zfs_ioc_send_new(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)53574445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
53584445fffbSMatthew Ahrens {
53594445fffbSMatthew Ahrens 	int error;
53604445fffbSMatthew Ahrens 	offset_t off;
53613b2aab18SMatthew Ahrens 	char *fromname = NULL;
53624445fffbSMatthew Ahrens 	int fd;
5363b5152584SMatthew Ahrens 	boolean_t largeblockok;
53645d7b4d43SMatthew Ahrens 	boolean_t embedok;
5365c1a50c7eSMatthew Ahrens 	uint64_t resumeobj = 0;
5366c1a50c7eSMatthew Ahrens 	uint64_t resumeoff = 0;
53674445fffbSMatthew Ahrens 
53684445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
53694445fffbSMatthew Ahrens 	if (error != 0)
5370be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
53714445fffbSMatthew Ahrens 
53723b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
53734445fffbSMatthew Ahrens 
5374b5152584SMatthew Ahrens 	largeblockok = nvlist_exists(innvl, "largeblockok");
53755d7b4d43SMatthew Ahrens 	embedok = nvlist_exists(innvl, "embedok");
53765d7b4d43SMatthew Ahrens 
5377c1a50c7eSMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5378c1a50c7eSMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5379c1a50c7eSMatthew Ahrens 
53804445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
53813b2aab18SMatthew Ahrens 	if (fp == NULL)
5382be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
53834445fffbSMatthew Ahrens 
53844445fffbSMatthew Ahrens 	off = fp->f_offset;
5385c1a50c7eSMatthew Ahrens 	error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
5386c1a50c7eSMatthew Ahrens 	    resumeobj, resumeoff, fp->f_vnode, &off);
53874445fffbSMatthew Ahrens 
53884445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
53894445fffbSMatthew Ahrens 		fp->f_offset = off;
53904445fffbSMatthew Ahrens 	releasef(fd);
53914445fffbSMatthew Ahrens 	return (error);
53924445fffbSMatthew Ahrens }
53934445fffbSMatthew Ahrens 
53944445fffbSMatthew Ahrens /*
53954445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
53964445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
53974445fffbSMatthew Ahrens  *
53984445fffbSMatthew Ahrens  * innvl: {
5399643da460SMax Grossman  *     (optional) "from" -> full snap or bookmark name to send an incremental
5400643da460SMax Grossman  *                          from
54014445fffbSMatthew Ahrens  * }
54024445fffbSMatthew Ahrens  *
54034445fffbSMatthew Ahrens  * outnvl: {
54044445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
54054445fffbSMatthew Ahrens  * }
54064445fffbSMatthew Ahrens  */
54074445fffbSMatthew Ahrens static int
zfs_ioc_send_space(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)54084445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
54094445fffbSMatthew Ahrens {
54103b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
54113b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
54124445fffbSMatthew Ahrens 	int error;
54134445fffbSMatthew Ahrens 	char *fromname;
54144445fffbSMatthew Ahrens 	uint64_t space;
54154445fffbSMatthew Ahrens 
54163b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
54173b2aab18SMatthew Ahrens 	if (error != 0)
54184445fffbSMatthew Ahrens 		return (error);
54194445fffbSMatthew Ahrens 
54203b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
54213b2aab18SMatthew Ahrens 	if (error != 0) {
54223b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
54233b2aab18SMatthew Ahrens 		return (error);
54243b2aab18SMatthew Ahrens 	}
54253b2aab18SMatthew Ahrens 
5426643da460SMax Grossman 	error = nvlist_lookup_string(innvl, "from", &fromname);
54274445fffbSMatthew Ahrens 	if (error == 0) {
5428643da460SMax Grossman 		if (strchr(fromname, '@') != NULL) {
5429643da460SMax Grossman 			/*
5430643da460SMax Grossman 			 * If from is a snapshot, hold it and use the more
5431643da460SMax Grossman 			 * efficient dmu_send_estimate to estimate send space
5432643da460SMax Grossman 			 * size using deadlists.
5433643da460SMax Grossman 			 */
5434643da460SMax Grossman 			dsl_dataset_t *fromsnap;
54353b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5436643da460SMax Grossman 			if (error != 0)
5437643da460SMax Grossman 				goto out;
5438643da460SMax Grossman 			error = dmu_send_estimate(tosnap, fromsnap, &space);
5439643da460SMax Grossman 			dsl_dataset_rele(fromsnap, FTAG);
5440643da460SMax Grossman 		} else if (strchr(fromname, '#') != NULL) {
5441643da460SMax Grossman 			/*
5442643da460SMax Grossman 			 * If from is a bookmark, fetch the creation TXG of the
5443643da460SMax Grossman 			 * snapshot it was created from and use that to find
5444643da460SMax Grossman 			 * blocks that were born after it.
5445643da460SMax Grossman 			 */
5446643da460SMax Grossman 			zfs_bookmark_phys_t frombm;
5447643da460SMax Grossman 
5448643da460SMax Grossman 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
5449643da460SMax Grossman 			    &frombm);
5450643da460SMax Grossman 			if (error != 0)
5451643da460SMax Grossman 				goto out;
5452643da460SMax Grossman 			error = dmu_send_estimate_from_txg(tosnap,
5453643da460SMax Grossman 			    frombm.zbm_creation_txg, &space);
5454643da460SMax Grossman 		} else {
5455643da460SMax Grossman 			/*
5456643da460SMax Grossman 			 * from is not properly formatted as a snapshot or
5457643da460SMax Grossman 			 * bookmark
5458643da460SMax Grossman 			 */
5459643da460SMax Grossman 			error = SET_ERROR(EINVAL);
5460643da460SMax Grossman 			goto out;
54614445fffbSMatthew Ahrens 		}
5462643da460SMax Grossman 	} else {
5463643da460SMax Grossman 		// If estimating the size of a full send, use dmu_send_estimate
5464643da460SMax Grossman 		error = dmu_send_estimate(tosnap, NULL, &space);
54654445fffbSMatthew Ahrens 	}
54664445fffbSMatthew Ahrens 
54674445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
54684445fffbSMatthew Ahrens 
5469643da460SMax Grossman out:
54703b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
54713b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
54724445fffbSMatthew Ahrens 	return (error);
54734445fffbSMatthew Ahrens }
54744445fffbSMatthew Ahrens 
547537c9dfdcSAndreas Jaekel static int
zfs_ioc_set_zev_callbacks(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)547637c9dfdcSAndreas Jaekel zfs_ioc_set_zev_callbacks(const char *unused, nvlist_t *innvl,
547737c9dfdcSAndreas Jaekel     nvlist_t *outnvl)
547837c9dfdcSAndreas Jaekel {
547937c9dfdcSAndreas Jaekel 	int error;
548037c9dfdcSAndreas Jaekel 	uint64_t cb_addr;
548137c9dfdcSAndreas Jaekel 	/*
548237c9dfdcSAndreas Jaekel 	 * Our secpolicy for this op makes sure it's called in
548337c9dfdcSAndreas Jaekel 	 * kernel context, and that no other callbacks have
548437c9dfdcSAndreas Jaekel 	 * been registered, yet.
548537c9dfdcSAndreas Jaekel 	 */
548637c9dfdcSAndreas Jaekel 	error = nvlist_lookup_uint64(innvl, "callbacks", &cb_addr);
548737c9dfdcSAndreas Jaekel 	if (error != 0) {
548837c9dfdcSAndreas Jaekel 		cmn_err(CE_WARN, "set_zev_callbacks nvlist lookup failed (%d)",
548937c9dfdcSAndreas Jaekel 		    error);
549037c9dfdcSAndreas Jaekel 		return (error);
549137c9dfdcSAndreas Jaekel 	}
549237c9dfdcSAndreas Jaekel 	/* cb_addr is always a kernel memory address */
549337c9dfdcSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_WRITER);
549437c9dfdcSAndreas Jaekel 	if (rz_zev_callbacks != rz_zev_default_callbacks) {
549537c9dfdcSAndreas Jaekel 		rw_exit(&rz_zev_rwlock);
549637c9dfdcSAndreas Jaekel 		return (EBUSY);
549737c9dfdcSAndreas Jaekel 	}
549837c9dfdcSAndreas Jaekel 	rz_zev_callbacks = (void *)(uintptr_t)cb_addr;
549937c9dfdcSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
550037c9dfdcSAndreas Jaekel 	return (0);
550137c9dfdcSAndreas Jaekel }
550237c9dfdcSAndreas Jaekel 
550337c9dfdcSAndreas Jaekel static int
zfs_ioc_unset_zev_callbacks(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)550437c9dfdcSAndreas Jaekel zfs_ioc_unset_zev_callbacks(const char *unused, nvlist_t *innvl,
550537c9dfdcSAndreas Jaekel     nvlist_t *outnvl)
550637c9dfdcSAndreas Jaekel {
550737c9dfdcSAndreas Jaekel 	/*
550837c9dfdcSAndreas Jaekel 	 * Our secpolicy for this op makes sure it's called in
550937c9dfdcSAndreas Jaekel 	 * kernel context.
551037c9dfdcSAndreas Jaekel 	 */
551137c9dfdcSAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_WRITER);
551237c9dfdcSAndreas Jaekel 	rz_zev_callbacks = rz_zev_default_callbacks;
551337c9dfdcSAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
551437c9dfdcSAndreas Jaekel 	/* after mutex release, no thread is using the old table anymore. */
551537c9dfdcSAndreas Jaekel 	return (0);
551637c9dfdcSAndreas Jaekel }
551737c9dfdcSAndreas Jaekel 
55184445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
55194445fffbSMatthew Ahrens 
55204445fffbSMatthew Ahrens static void
zfs_ioctl_register_legacy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_namecheck_t namecheck,boolean_t log_history,zfs_ioc_poolcheck_t pool_check)55214445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55224445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
55234445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
55244445fffbSMatthew Ahrens {
55254445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
55264445fffbSMatthew Ahrens 
55274445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
55284445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
55294445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
55304445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
55314445fffbSMatthew Ahrens 
55324445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
55334445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
55344445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
55354445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
55364445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
55374445fffbSMatthew Ahrens }
55384445fffbSMatthew Ahrens 
55394445fffbSMatthew Ahrens /*
55404445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
55414445fffbSMatthew Ahrens  * each argument to this function.
55424445fffbSMatthew Ahrens  */
55434445fffbSMatthew Ahrens static void
zfs_ioctl_register(const char * name,zfs_ioc_t ioc,zfs_ioc_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_namecheck_t namecheck,zfs_ioc_poolcheck_t pool_check,boolean_t smush_outnvlist,boolean_t allow_log)55444445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
55454445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
55464445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
55474445fffbSMatthew Ahrens     boolean_t allow_log)
55484445fffbSMatthew Ahrens {
55494445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
55504445fffbSMatthew Ahrens 
55514445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
55524445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
55534445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
55544445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
55554445fffbSMatthew Ahrens 
55564445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
55574445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
55584445fffbSMatthew Ahrens 
55594445fffbSMatthew Ahrens 	vec->zvec_name = name;
55604445fffbSMatthew Ahrens 	vec->zvec_func = func;
55614445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
55624445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
55634445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
55644445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
55654445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
55664445fffbSMatthew Ahrens }
55674445fffbSMatthew Ahrens 
55684445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,boolean_t log_history,zfs_ioc_poolcheck_t pool_check)55694445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55704445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
55714445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
55724445fffbSMatthew Ahrens {
55734445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
55744445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
55754445fffbSMatthew Ahrens }
55764445fffbSMatthew Ahrens 
55774445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy,zfs_ioc_poolcheck_t pool_check)55784445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55794445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
55804445fffbSMatthew Ahrens {
55814445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
55824445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
55834445fffbSMatthew Ahrens }
55844445fffbSMatthew Ahrens 
55854445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)55864445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
55874445fffbSMatthew Ahrens {
55884445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
55894445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
55904445fffbSMatthew Ahrens }
55914445fffbSMatthew Ahrens 
55924445fffbSMatthew Ahrens static void
zfs_ioctl_register_pool_meta(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)55934445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55944445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
55954445fffbSMatthew Ahrens {
55964445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
55974445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
55984445fffbSMatthew Ahrens }
55994445fffbSMatthew Ahrens 
56004445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)56014445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
56024445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
56034445fffbSMatthew Ahrens {
56044445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56054445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
56064445fffbSMatthew Ahrens }
56074445fffbSMatthew Ahrens 
56084445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_read(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)56094445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
56104445fffbSMatthew Ahrens {
56114445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
56124445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
56134445fffbSMatthew Ahrens }
56144445fffbSMatthew Ahrens 
56154445fffbSMatthew Ahrens static void
zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)56164445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56174445fffbSMatthew Ahrens 	zfs_secpolicy_func_t *secpolicy)
56184445fffbSMatthew Ahrens {
56194445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56204445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
56214445fffbSMatthew Ahrens }
56224445fffbSMatthew Ahrens 
56234445fffbSMatthew Ahrens static void
zfs_ioctl_init(void)56244445fffbSMatthew Ahrens zfs_ioctl_init(void)
56254445fffbSMatthew Ahrens {
56264445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
56274445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
56284445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56294445fffbSMatthew Ahrens 
56304445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
56314445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
56324445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
56334445fffbSMatthew Ahrens 
56344445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
56354445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
56364445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56374445fffbSMatthew Ahrens 
56384445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
56394445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
56404445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56414445fffbSMatthew Ahrens 
56424445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
56434445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
56444445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56454445fffbSMatthew Ahrens 
56464445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
56474445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
56484445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56494445fffbSMatthew Ahrens 
56504445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
56514445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
56524445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56534445fffbSMatthew Ahrens 
56544445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
56554445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
56564445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56574445fffbSMatthew Ahrens 
56583b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
56593b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
56603b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56613b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
56623b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
56633b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56643b2aab18SMatthew Ahrens 
56653b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
56663b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
56673b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56683b2aab18SMatthew Ahrens 
566937c9dfdcSAndreas Jaekel 	zfs_ioctl_register("set_zev_callbacks", ZFS_IOC_SET_ZEV_CALLBACKS,
567037c9dfdcSAndreas Jaekel 	    zfs_ioc_set_zev_callbacks, zfs_secpolicy_set_zev_callbacks, NO_NAME,
567137c9dfdcSAndreas Jaekel 	    POOL_CHECK_NONE, B_TRUE, B_FALSE);
567237c9dfdcSAndreas Jaekel 
567337c9dfdcSAndreas Jaekel 	zfs_ioctl_register("unset_zev_callbacks", ZFS_IOC_UNSET_ZEV_CALLBACKS,
567437c9dfdcSAndreas Jaekel 	    zfs_ioc_unset_zev_callbacks, zfs_secpolicy_unset_zev_callbacks,
567537c9dfdcSAndreas Jaekel 	    NO_NAME, POOL_CHECK_NONE, B_TRUE, B_FALSE);
5676a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5677a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5678a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5679a7027df1SMatthew Ahrens 
568078f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
568178f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
568278f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
568378f17100SMatthew Ahrens 
568478f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
568578f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
568678f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
568778f17100SMatthew Ahrens 
568878f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
568978f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
569078f17100SMatthew Ahrens 	    POOL_NAME,
569178f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
569278f17100SMatthew Ahrens 
56934445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
56944445fffbSMatthew Ahrens 
56954445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
56964445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
56974445fffbSMatthew Ahrens 
5698e071a233SArne Jansen 	zfs_ioctl_register_legacy(ZFS_IOC_ARC_INFO, zfs_ioc_arc_info,
5699e071a233SArne Jansen 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5700e071a233SArne Jansen 
57014445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
57024445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57034445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
57044445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
57054445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
57064445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
57074445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
57084445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
57094445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
57104445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
57114445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
57124445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
57134445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
57144445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
57154445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
57164445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
57174445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
57184445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
57194445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
57204445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
57214445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
57224445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
57234445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
57244445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
57254445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
57264445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
57274445fffbSMatthew Ahrens 
57284445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
57294445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
57304445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
57314445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
57324445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
57334445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
57344445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
57354445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
57364445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
57374445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
57384445fffbSMatthew Ahrens 
57394445fffbSMatthew Ahrens 	/*
57404445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
57414445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
57424445fffbSMatthew Ahrens 	 * does the logging of those commands.
57434445fffbSMatthew Ahrens 	 */
57444445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
57454445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
57464445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
57474445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
57484445fffbSMatthew Ahrens 
57494445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
57504445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
57514445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
57524445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
57534445fffbSMatthew Ahrens 
57544445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
57554445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
57564445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
57574445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
57584445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
57594445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
57604445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
57614445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
57624445fffbSMatthew Ahrens 
57634445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
57644445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57654445fffbSMatthew Ahrens 
57664445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
576722e30981SGeorge Wilson 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57684445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
57694445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
57704445fffbSMatthew Ahrens 
57714445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
57724445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
57734445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
57744445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
57754445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
57764445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
57774445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
57784445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
57794445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
57804445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
57814445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
57824445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
57834445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
57844445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
57854445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
57864445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
57874445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
57884445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
57894445fffbSMatthew Ahrens 
57904445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
57914445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
57924445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
57934445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
57944445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
57954445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
57964445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
57974445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
57984445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
57994445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
58004445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
58014445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
58024445fffbSMatthew Ahrens 
58034445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
58044445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
58054445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
58064445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
58074445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
58084445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
58094445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
58104445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
58114445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
58124445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
58134445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
58144445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
58154445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
58164445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
58174445fffbSMatthew Ahrens 
58184445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
58194445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
58204445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
58214445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
58224445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
58234445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
58244445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58254445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
58264445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
58274445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58284445fffbSMatthew Ahrens }
5829fa9e4066Sahrens 
583054d692b7SGeorge Wilson int
pool_status_check(const char * name,zfs_ioc_namecheck_t type,zfs_ioc_poolcheck_t check)5831f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5832f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
583354d692b7SGeorge Wilson {
583454d692b7SGeorge Wilson 	spa_t *spa;
583554d692b7SGeorge Wilson 	int error;
583654d692b7SGeorge Wilson 
583754d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
583854d692b7SGeorge Wilson 
5839f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5840f9af39baSGeorge Wilson 		return (0);
5841f9af39baSGeorge Wilson 
584214843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
584354d692b7SGeorge Wilson 	if (error == 0) {
5844f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5845be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5846f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5847be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
584854d692b7SGeorge Wilson 		spa_close(spa, FTAG);
584954d692b7SGeorge Wilson 	}
585054d692b7SGeorge Wilson 	return (error);
585154d692b7SGeorge Wilson }
585254d692b7SGeorge Wilson 
5853c99e4bdcSChris Kirby /*
5854c99e4bdcSChris Kirby  * Find a free minor number.
5855c99e4bdcSChris Kirby  */
5856c99e4bdcSChris Kirby minor_t
zfsdev_minor_alloc(void)5857c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5858c99e4bdcSChris Kirby {
5859c99e4bdcSChris Kirby 	static minor_t last_minor;
5860c99e4bdcSChris Kirby 	minor_t m;
5861c99e4bdcSChris Kirby 
5862c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5863c99e4bdcSChris Kirby 
5864c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5865c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5866c99e4bdcSChris Kirby 			m = 1;
5867c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5868c99e4bdcSChris Kirby 			last_minor = m;
5869c99e4bdcSChris Kirby 			return (m);
5870c99e4bdcSChris Kirby 		}
5871c99e4bdcSChris Kirby 	}
5872c99e4bdcSChris Kirby 
5873c99e4bdcSChris Kirby 	return (0);
5874c99e4bdcSChris Kirby }
5875c99e4bdcSChris Kirby 
5876c99e4bdcSChris Kirby static int
zfs_ctldev_init(dev_t * devp)5877c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5878c99e4bdcSChris Kirby {
5879c99e4bdcSChris Kirby 	minor_t minor;
5880c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5881c99e4bdcSChris Kirby 
5882c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5883c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5884c99e4bdcSChris Kirby 
5885c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5886c99e4bdcSChris Kirby 	if (minor == 0)
5887be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5888c99e4bdcSChris Kirby 
5889c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5890be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5891c99e4bdcSChris Kirby 
5892c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5893c99e4bdcSChris Kirby 
5894c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
5895c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
5896c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5897c99e4bdcSChris Kirby 
5898c99e4bdcSChris Kirby 	return (0);
5899c99e4bdcSChris Kirby }
5900c99e4bdcSChris Kirby 
5901c99e4bdcSChris Kirby static void
zfs_ctldev_destroy(zfs_onexit_t * zo,minor_t minor)5902c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5903c99e4bdcSChris Kirby {
5904c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5905c99e4bdcSChris Kirby 
5906c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
5907c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
5908c99e4bdcSChris Kirby }
5909c99e4bdcSChris Kirby 
5910c99e4bdcSChris Kirby void *
zfsdev_get_soft_state(minor_t minor,enum zfs_soft_state_type which)5911c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5912c99e4bdcSChris Kirby {
5913c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
5914c99e4bdcSChris Kirby 
5915c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
5916c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
5917c99e4bdcSChris Kirby 		return (NULL);
5918c99e4bdcSChris Kirby 
5919c99e4bdcSChris Kirby 	return (zp->zss_data);
5920c99e4bdcSChris Kirby }
5921c99e4bdcSChris Kirby 
5922c99e4bdcSChris Kirby static int
zfsdev_open(dev_t * devp,int flag,int otyp,cred_t * cr)5923c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5924c99e4bdcSChris Kirby {
5925c99e4bdcSChris Kirby 	int error = 0;
5926c99e4bdcSChris Kirby 
5927c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
5928c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
5929c99e4bdcSChris Kirby 
5930c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
5931c99e4bdcSChris Kirby 	if (flag & FEXCL) {
5932c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
5933c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
5934c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5935c99e4bdcSChris Kirby 	}
5936c99e4bdcSChris Kirby 
5937c99e4bdcSChris Kirby 	return (error);
5938c99e4bdcSChris Kirby }
5939c99e4bdcSChris Kirby 
5940c99e4bdcSChris Kirby static int
zfsdev_close(dev_t dev,int flag,int otyp,cred_t * cr)5941c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5942c99e4bdcSChris Kirby {
5943c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
5944c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5945c99e4bdcSChris Kirby 
5946c99e4bdcSChris Kirby 	if (minor == 0)
5947c99e4bdcSChris Kirby 		return (0);
5948c99e4bdcSChris Kirby 
5949c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5950c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5951c99e4bdcSChris Kirby 	if (zo == NULL) {
5952c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5953c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5954c99e4bdcSChris Kirby 	}
5955c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5956c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5957c99e4bdcSChris Kirby 
5958c99e4bdcSChris Kirby 	return (0);
5959c99e4bdcSChris Kirby }
5960c99e4bdcSChris Kirby 
5961fa9e4066Sahrens static int
zfsdev_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)5962fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5963fa9e4066Sahrens {
5964fa9e4066Sahrens 	zfs_cmd_t *zc;
59654445fffbSMatthew Ahrens 	uint_t vecnum;
59664445fffbSMatthew Ahrens 	int error, rc, len;
5967c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
59684445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
59694445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
59704445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
5971fa9e4066Sahrens 
5972c99e4bdcSChris Kirby 	if (minor != 0 &&
5973c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5974fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5975fa9e4066Sahrens 
59764445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
597791ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5978fa9e4066Sahrens 
59794445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5980be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
59814445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
5982fa9e4066Sahrens 
5983fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5984fa9e4066Sahrens 
5985478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
59864445fffbSMatthew Ahrens 	if (error != 0) {
5987be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
59884445fffbSMatthew Ahrens 		goto out;
59894445fffbSMatthew Ahrens 	}
5990fa9e4066Sahrens 
59914445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
59924445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
59934445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
59944445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
59954445fffbSMatthew Ahrens 		if (error != 0)
59964445fffbSMatthew Ahrens 			goto out;
59974445fffbSMatthew Ahrens 	}
5998fa9e4066Sahrens 
5999fa9e4066Sahrens 	/*
6000fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
6001fa9e4066Sahrens 	 * the lower layers.
6002fa9e4066Sahrens 	 */
6003fa9e4066Sahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
60044445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
6005e7437265Sahrens 	case POOL_NAME:
6006fa9e4066Sahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6007be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
60084445fffbSMatthew Ahrens 		else
600954d692b7SGeorge Wilson 			error = pool_status_check(zc->zc_name,
60104445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
6011fa9e4066Sahrens 		break;
6012fa9e4066Sahrens 
6013e7437265Sahrens 	case DATASET_NAME:
601454da3412SMarcel Telka 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6015be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
60164445fffbSMatthew Ahrens 		else
601754d692b7SGeorge Wilson 			error = pool_status_check(zc->zc_name,
60184445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
6019fa9e4066Sahrens 		break;
60205ad82045Snd150628 
6021e7437265Sahrens 	case NO_NAME:
60225ad82045Snd150628 		break;
6023fa9e4066Sahrens 	}
60244445fffbSMatthew Ahrens 
60254445fffbSMatthew Ahrens 
60264445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
60274445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
60284445fffbSMatthew Ahrens 
60294445fffbSMatthew Ahrens 	if (error != 0)
60304445fffbSMatthew Ahrens 		goto out;
60314445fffbSMatthew Ahrens 
60324445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
603378f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
60344445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
60354445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
60364445fffbSMatthew Ahrens 
60374445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
60384445fffbSMatthew Ahrens 		nvlist_t *outnvl;
60394445fffbSMatthew Ahrens 		int puterror = 0;
60404445fffbSMatthew Ahrens 		spa_t *spa;
60414445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
60424445fffbSMatthew Ahrens 
60434445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
60444445fffbSMatthew Ahrens 
60454445fffbSMatthew Ahrens 		/*
60464445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
60474445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
60484445fffbSMatthew Ahrens 		 */
60494445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
60504445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
60514445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
60524445fffbSMatthew Ahrens 			    vec->zvec_name);
60534445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
60544445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
60554445fffbSMatthew Ahrens 				    innvl);
60564445fffbSMatthew Ahrens 			}
6057fa9e4066Sahrens 		}
6058fa9e4066Sahrens 
60594445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
60604445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6061fa9e4066Sahrens 
60624445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
60634445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
60644445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
60654445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
60664445fffbSMatthew Ahrens 				    outnvl);
60674445fffbSMatthew Ahrens 			}
60684445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
60694445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
60704445fffbSMatthew Ahrens 		}
60714445fffbSMatthew Ahrens 		fnvlist_free(lognv);
60724445fffbSMatthew Ahrens 
60734445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
60744445fffbSMatthew Ahrens 			int smusherror = 0;
60754445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
60764445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
60774445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
60784445fffbSMatthew Ahrens 			}
60794445fffbSMatthew Ahrens 			if (smusherror == 0)
60804445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
60814445fffbSMatthew Ahrens 		}
60824445fffbSMatthew Ahrens 
60834445fffbSMatthew Ahrens 		if (puterror != 0)
60844445fffbSMatthew Ahrens 			error = puterror;
60854445fffbSMatthew Ahrens 
60864445fffbSMatthew Ahrens 		nvlist_free(outnvl);
60874445fffbSMatthew Ahrens 	} else {
60884445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
60894445fffbSMatthew Ahrens 	}
60904445fffbSMatthew Ahrens 
60914445fffbSMatthew Ahrens out:
60924445fffbSMatthew Ahrens 	nvlist_free(innvl);
6093478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
60944445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
6095be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
60964445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
60974445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
60984445fffbSMatthew Ahrens 		if (s != NULL)
60994445fffbSMatthew Ahrens 			strfree(s);
61004445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
61014445fffbSMatthew Ahrens 	} else {
61024445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
61034445fffbSMatthew Ahrens 			strfree(saved_poolname);
6104ecd6cf80Smarks 	}
6105fa9e4066Sahrens 
6106fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
6107fa9e4066Sahrens 	return (error);
6108fa9e4066Sahrens }
6109fa9e4066Sahrens 
6110fa9e4066Sahrens static int
zfs_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)6111fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6112fa9e4066Sahrens {
6113fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
6114fa9e4066Sahrens 		return (DDI_FAILURE);
6115fa9e4066Sahrens 
6116fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6117fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
6118fa9e4066Sahrens 		return (DDI_FAILURE);
6119fa9e4066Sahrens 
6120fa9e4066Sahrens 	zfs_dip = dip;
6121fa9e4066Sahrens 
6122fa9e4066Sahrens 	ddi_report_dev(dip);
6123fa9e4066Sahrens 
6124fa9e4066Sahrens 	return (DDI_SUCCESS);
6125fa9e4066Sahrens }
6126fa9e4066Sahrens 
6127fa9e4066Sahrens static int
zfs_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)6128fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6129fa9e4066Sahrens {
6130fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
6131fa9e4066Sahrens 		return (DDI_FAILURE);
6132fa9e4066Sahrens 
6133fa9e4066Sahrens 	if (cmd != DDI_DETACH)
6134fa9e4066Sahrens 		return (DDI_FAILURE);
6135fa9e4066Sahrens 
6136fa9e4066Sahrens 	zfs_dip = NULL;
6137fa9e4066Sahrens 
6138fa9e4066Sahrens 	ddi_prop_remove_all(dip);
6139fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
6140fa9e4066Sahrens 
6141fa9e4066Sahrens 	return (DDI_SUCCESS);
6142fa9e4066Sahrens }
6143fa9e4066Sahrens 
6144fa9e4066Sahrens /*ARGSUSED*/
6145fa9e4066Sahrens static int
zfs_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6146fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6147fa9e4066Sahrens {
6148fa9e4066Sahrens 	switch (infocmd) {
6149fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
6150fa9e4066Sahrens 		*result = zfs_dip;
6151fa9e4066Sahrens 		return (DDI_SUCCESS);
6152fa9e4066Sahrens 
6153fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
6154a0965f35Sbonwick 		*result = (void *)0;
6155fa9e4066Sahrens 		return (DDI_SUCCESS);
6156fa9e4066Sahrens 	}
6157fa9e4066Sahrens 
6158fa9e4066Sahrens 	return (DDI_FAILURE);
6159fa9e4066Sahrens }
6160fa9e4066Sahrens 
6161fa9e4066Sahrens /*
6162fa9e4066Sahrens  * OK, so this is a little weird.
6163fa9e4066Sahrens  *
6164fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
6165fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6166fa9e4066Sahrens  *
6167fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
6168fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
6169fa9e4066Sahrens  */
6170fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
6171c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
6172c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
6173fa9e4066Sahrens 	zvol_strategy,	/* strategy */
6174fa9e4066Sahrens 	nodev,		/* print */
6175e7cbe64fSgw25295 	zvol_dump,	/* dump */
6176fa9e4066Sahrens 	zvol_read,	/* read */
6177fa9e4066Sahrens 	zvol_write,	/* write */
6178fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
6179fa9e4066Sahrens 	nodev,		/* devmap */
6180fa9e4066Sahrens 	nodev,		/* mmap */
6181fa9e4066Sahrens 	nodev,		/* segmap */
6182fa9e4066Sahrens 	nochpoll,	/* poll */
6183fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
6184fa9e4066Sahrens 	NULL,		/* streamtab */
6185fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
6186fa9e4066Sahrens 	CB_REV,		/* version */
6187feb08c6bSbillm 	nodev,		/* async read */
6188feb08c6bSbillm 	nodev,		/* async write */
6189fa9e4066Sahrens };
6190fa9e4066Sahrens 
6191fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
6192fa9e4066Sahrens 	DEVO_REV,	/* version */
6193fa9e4066Sahrens 	0,		/* refcnt */
6194fa9e4066Sahrens 	zfs_info,	/* info */
6195fa9e4066Sahrens 	nulldev,	/* identify */
6196fa9e4066Sahrens 	nulldev,	/* probe */
6197fa9e4066Sahrens 	zfs_attach,	/* attach */
6198fa9e4066Sahrens 	zfs_detach,	/* detach */
6199fa9e4066Sahrens 	nodev,		/* reset */
6200fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
620119397407SSherry Moore 	NULL,		/* no bus operations */
620219397407SSherry Moore 	NULL,		/* power */
620319397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
6204fa9e4066Sahrens };
6205fa9e4066Sahrens 
6206fa9e4066Sahrens static struct modldrv zfs_modldrv = {
620719397407SSherry Moore 	&mod_driverops,
620819397407SSherry Moore 	"ZFS storage pool",
6209e9dbad6fSeschrock 	&zfs_dev_ops
6210fa9e4066Sahrens };
6211fa9e4066Sahrens 
6212fa9e4066Sahrens static struct modlinkage modlinkage = {
6213fa9e4066Sahrens 	MODREV_1,
6214fa9e4066Sahrens 	(void *)&zfs_modlfs,
6215fa9e4066Sahrens 	(void *)&zfs_modldrv,
6216fa9e4066Sahrens 	NULL
6217fa9e4066Sahrens };
6218fa9e4066Sahrens 
62194445fffbSMatthew Ahrens static void
zfs_allow_log_destroy(void * arg)62204445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
62214445fffbSMatthew Ahrens {
62224445fffbSMatthew Ahrens 	char *poolname = arg;
62234445fffbSMatthew Ahrens 	strfree(poolname);
62244445fffbSMatthew Ahrens }
6225ec533521Sfr157268 
6226fa9e4066Sahrens int
_init(void)6227fa9e4066Sahrens _init(void)
6228fa9e4066Sahrens {
6229fa9e4066Sahrens 	int error;
6230fa9e4066Sahrens 
6231fa9e4066Sahrens 	spa_init(FREAD | FWRITE);
6232fa9e4066Sahrens 	zfs_init();
6233fa9e4066Sahrens 	zvol_init();
62344445fffbSMatthew Ahrens 	zfs_ioctl_init();
623537c9dfdcSAndreas Jaekel 	rz_zev_init();
6236fa9e4066Sahrens 
6237a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
6238a0965f35Sbonwick 		zvol_fini();
6239a0965f35Sbonwick 		zfs_fini();
6240a0965f35Sbonwick 		spa_fini();
6241a0965f35Sbonwick 		return (error);
6242a0965f35Sbonwick 	}
6243a0965f35Sbonwick 
6244ec533521Sfr157268 	tsd_create(&zfs_fsyncer_key, NULL);
62454445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
62464445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6247ec533521Sfr157268 
6248a0965f35Sbonwick 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6249a0965f35Sbonwick 	ASSERT(error == 0);
6250ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6251a0965f35Sbonwick 
6252fa9e4066Sahrens 	return (0);
6253fa9e4066Sahrens }
6254fa9e4066Sahrens 
6255fa9e4066Sahrens int
_fini(void)6256fa9e4066Sahrens _fini(void)
6257fa9e4066Sahrens {
6258fa9e4066Sahrens 	int error;
6259fa9e4066Sahrens 
6260ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6261be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6262fa9e4066Sahrens 
6263fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
6264fa9e4066Sahrens 		return (error);
6265fa9e4066Sahrens 
626637c9dfdcSAndreas Jaekel 	rz_zev_fini();
6267fa9e4066Sahrens 	zvol_fini();
6268fa9e4066Sahrens 	zfs_fini();
6269fa9e4066Sahrens 	spa_fini();
6270da6c28aaSamw 	if (zfs_nfsshare_inited)
6271ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
6272da6c28aaSamw 	if (zfs_smbshare_inited)
6273da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
6274da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
6275ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
6276fa9e4066Sahrens 
6277ec533521Sfr157268 	tsd_destroy(&zfs_fsyncer_key);
6278fa9e4066Sahrens 	ldi_ident_release(zfs_li);
6279fa9e4066Sahrens 	zfs_li = NULL;
6280ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
6281fa9e4066Sahrens 
6282fa9e4066Sahrens 	return (error);
6283fa9e4066Sahrens }
6284fa9e4066Sahrens 
6285fa9e4066Sahrens int
_info(struct modinfo * modinfop)6286fa9e4066Sahrens _info(struct modinfo *modinfop)
6287fa9e4066Sahrens {
6288fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
6289fa9e4066Sahrens }
6290