xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 0d8fa8f8)
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.
24*0d8fa8f8SMartin Matuska  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
251df56adaSMartin Matuska  * Portions Copyright 2011 Martin Matuska
265878fad7SDan McDonald  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
27752fd8daSJosef 'Jeff' Sipek  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
28a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
296de9bb56SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
30a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
32c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
334445fffbSMatthew Ahrens  */
344445fffbSMatthew Ahrens 
354445fffbSMatthew Ahrens /*
364445fffbSMatthew Ahrens  * ZFS ioctls.
374445fffbSMatthew Ahrens  *
384445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
394445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
404445fffbSMatthew Ahrens  *
414445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
424445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
434445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
444445fffbSMatthew Ahrens  *
454445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
464445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
474445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
484445fffbSMatthew Ahrens  *
494445fffbSMatthew Ahrens  * The registration arguments are as follows:
504445fffbSMatthew Ahrens  *
514445fffbSMatthew Ahrens  * const char *name
524445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
534445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
544445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
554445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
564445fffbSMatthew Ahrens  *
574445fffbSMatthew Ahrens  * zfs_ioc_t ioc
584445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
594445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
604445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
614445fffbSMatthew Ahrens  *
624445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
634445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
644445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
654445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
664445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
674445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
684445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
694445fffbSMatthew Ahrens  *
704445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
714445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
724445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
734445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
744445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
754445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
764445fffbSMatthew Ahrens  *   doesn't have invalid characters).
774445fffbSMatthew Ahrens  *
784445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
794445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
804445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
814445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
824445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
834445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
844445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
854445fffbSMatthew Ahrens  *
864445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
874445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
884445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
894445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
904445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
914445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
924445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
934445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
944445fffbSMatthew Ahrens  *
954445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
964445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
974445fffbSMatthew Ahrens  *
984445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
994445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
1004445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
1014445fffbSMatthew Ahrens  *   will be called with the following arguments:
1024445fffbSMatthew Ahrens  *
1034445fffbSMatthew Ahrens  *   const char *name
1044445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1054445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1064445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1074445fffbSMatthew Ahrens  *
1084445fffbSMatthew Ahrens  *   nvlist_t *innvl
1094445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1104445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1114445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1124445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1134445fffbSMatthew Ahrens  *
1144445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1154445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1164445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1174445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1184445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1194445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1204445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1214445fffbSMatthew Ahrens  *
1224445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1234445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1244445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1254445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1264445fffbSMatthew Ahrens  *         the ioctl again.
1274445fffbSMatthew Ahrens  *
1284445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1294445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1304445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1314445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1324445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1334445fffbSMatthew Ahrens  *         deserialization failing.
134e9103aaeSGarrett D'Amore  */
135fa9e4066Sahrens 
136fa9e4066Sahrens #include <sys/types.h>
137fa9e4066Sahrens #include <sys/param.h>
138fa9e4066Sahrens #include <sys/errno.h>
139fa9e4066Sahrens #include <sys/uio.h>
140fa9e4066Sahrens #include <sys/buf.h>
141fa9e4066Sahrens #include <sys/modctl.h>
142fa9e4066Sahrens #include <sys/open.h>
143fa9e4066Sahrens #include <sys/file.h>
144fa9e4066Sahrens #include <sys/kmem.h>
145fa9e4066Sahrens #include <sys/conf.h>
146fa9e4066Sahrens #include <sys/cmn_err.h>
147fa9e4066Sahrens #include <sys/stat.h>
148fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1494201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
150da6c28aaSamw #include <sys/zfs_znode.h>
151fa9e4066Sahrens #include <sys/zap.h>
152fa9e4066Sahrens #include <sys/spa.h>
153b1b8ab34Slling #include <sys/spa_impl.h>
154fa9e4066Sahrens #include <sys/vdev.h>
1554201a95eSRic Aleshire #include <sys/priv_impl.h>
156fa9e4066Sahrens #include <sys/dmu.h>
157fa9e4066Sahrens #include <sys/dsl_dir.h>
158fa9e4066Sahrens #include <sys/dsl_dataset.h>
159fa9e4066Sahrens #include <sys/dsl_prop.h>
160ecd6cf80Smarks #include <sys/dsl_deleg.h>
161ecd6cf80Smarks #include <sys/dmu_objset.h>
1624e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1633b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
164fa9e4066Sahrens #include <sys/ddi.h>
165fa9e4066Sahrens #include <sys/sunddi.h>
166fa9e4066Sahrens #include <sys/sunldi.h>
167fa9e4066Sahrens #include <sys/policy.h>
168fa9e4066Sahrens #include <sys/zone.h>
169fa9e4066Sahrens #include <sys/nvpair.h>
170fa9e4066Sahrens #include <sys/pathname.h>
171fa9e4066Sahrens #include <sys/mount.h>
172fa9e4066Sahrens #include <sys/sdt.h>
173fa9e4066Sahrens #include <sys/fs/zfs.h>
174fa9e4066Sahrens #include <sys/zfs_ctldir.h>
175da6c28aaSamw #include <sys/zfs_dir.h>
176c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
177a2eea2e1Sahrens #include <sys/zvol.h>
1783f9d6ad7SLin Ling #include <sys/dsl_scan.h>
179ecd6cf80Smarks #include <sharefs/share.h>
180f18faf3fSek #include <sys/dmu_objset.h>
1813b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1823b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18378f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1843b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
185a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
18645818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
187fa9e4066Sahrens 
188fa9e4066Sahrens #include "zfs_namecheck.h"
189e9dbad6fSeschrock #include "zfs_prop.h"
190ecd6cf80Smarks #include "zfs_deleg.h"
1910a586ceaSMark Shellenbaum #include "zfs_comutil.h"
192fa9e4066Sahrens 
193fa9e4066Sahrens extern struct modlfs zfs_modlfs;
194fa9e4066Sahrens 
195fa9e4066Sahrens extern void zfs_init(void);
196fa9e4066Sahrens extern void zfs_fini(void);
197fa9e4066Sahrens 
198fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
199fa9e4066Sahrens dev_info_t *zfs_dip;
200fa9e4066Sahrens 
2014445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2024445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2034445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2044445fffbSMatthew Ahrens 
2054445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2064445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2074445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
208fa9e4066Sahrens 
20954d692b7SGeorge Wilson typedef enum {
21054d692b7SGeorge Wilson 	NO_NAME,
21154d692b7SGeorge Wilson 	POOL_NAME,
21254d692b7SGeorge Wilson 	DATASET_NAME
21354d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21454d692b7SGeorge Wilson 
215f9af39baSGeorge Wilson typedef enum {
216f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
217f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2184445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
219f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
220f9af39baSGeorge Wilson 
221fa9e4066Sahrens typedef struct zfs_ioc_vec {
2224445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
223fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
224fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
22554d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2264445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
227f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2284445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2294445fffbSMatthew Ahrens 	const char		*zvec_name;
230fa9e4066Sahrens } zfs_ioc_vec_t;
231fa9e4066Sahrens 
23214843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
23314843421SMatthew Ahrens static const char *userquota_perms[] = {
23414843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23514843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23614843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23714843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23814843421SMatthew Ahrens };
23914843421SMatthew Ahrens 
24014843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
24192241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
24292241e0bSTom Erickson     cred_t *cr);
24392241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24492241e0bSTom Erickson     nvlist_t **errors);
2450a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2460a48a24eStimh     boolean_t *);
2474445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2484445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2490a48a24eStimh 
2502acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
251a6f561b4SSašo Kiselkov 
252fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
253fa9e4066Sahrens void
254fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
255fa9e4066Sahrens {
256fa9e4066Sahrens 	const char *newfile;
2573f9d6ad7SLin Ling 	char buf[512];
258fa9e4066Sahrens 	va_list adx;
259fa9e4066Sahrens 
260fa9e4066Sahrens 	/*
261fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
262fa9e4066Sahrens 	 */
263fa9e4066Sahrens 	newfile = strrchr(file, '/');
264fa9e4066Sahrens 	if (newfile != NULL) {
265fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
266fa9e4066Sahrens 	} else {
267fa9e4066Sahrens 		newfile = file;
268fa9e4066Sahrens 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	va_start(adx, fmt);
271fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
272fa9e4066Sahrens 	va_end(adx);
273fa9e4066Sahrens 
274fa9e4066Sahrens 	/*
275fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
276fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
277fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
278fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
279fa9e4066Sahrens 	 * arg0 = file name
280fa9e4066Sahrens 	 * arg1 = function name
281fa9e4066Sahrens 	 * arg2 = line number
282fa9e4066Sahrens 	 * arg3 = message
283fa9e4066Sahrens 	 */
284fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
285fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
286fa9e4066Sahrens }
287fa9e4066Sahrens 
288ecd6cf80Smarks static void
289228975ccSek history_str_free(char *buf)
290228975ccSek {
291228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
292228975ccSek }
293228975ccSek 
294228975ccSek static char *
295228975ccSek history_str_get(zfs_cmd_t *zc)
296ecd6cf80Smarks {
29740feaa91Sahrens 	char *buf;
298ecd6cf80Smarks 
299ecd6cf80Smarks 	if (zc->zc_history == NULL)
300228975ccSek 		return (NULL);
301e7437265Sahrens 
302ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
303ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
304ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
305228975ccSek 		history_str_free(buf);
306228975ccSek 		return (NULL);
307ecd6cf80Smarks 	}
308ecd6cf80Smarks 
309ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
310ecd6cf80Smarks 
311228975ccSek 	return (buf);
312228975ccSek }
313ecd6cf80Smarks 
31415e6edf1Sgw /*
31515e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
31615e6edf1Sgw  */
31715e6edf1Sgw static boolean_t
31815e6edf1Sgw zfs_is_bootfs(const char *name)
31915e6edf1Sgw {
320503ad85cSMatthew Ahrens 	objset_t *os;
32115e6edf1Sgw 
322503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
323503ad85cSMatthew Ahrens 		boolean_t ret;
324b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
325503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
326503ad85cSMatthew Ahrens 		return (ret);
32715e6edf1Sgw 	}
328503ad85cSMatthew Ahrens 	return (B_FALSE);
32915e6edf1Sgw }
33015e6edf1Sgw 
331c2a93d44Stimh /*
332f7170741SWill Andrews  * Return non-zero if the spa version is less than requested version.
333c2a93d44Stimh  */
334da6c28aaSamw static int
3350a48a24eStimh zfs_earlier_version(const char *name, int version)
336da6c28aaSamw {
337da6c28aaSamw 	spa_t *spa;
338da6c28aaSamw 
339da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
340da6c28aaSamw 		if (spa_version(spa) < version) {
341da6c28aaSamw 			spa_close(spa, FTAG);
342da6c28aaSamw 			return (1);
343da6c28aaSamw 		}
344da6c28aaSamw 		spa_close(spa, FTAG);
345da6c28aaSamw 	}
346da6c28aaSamw 	return (0);
347da6c28aaSamw }
348da6c28aaSamw 
3499e6eda55Smarks /*
350745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3519e6eda55Smarks  */
352745cd3c5Smaybee static boolean_t
353745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3549e6eda55Smarks {
3559e6eda55Smarks 	objset_t *os;
356745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3579e6eda55Smarks 
358503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
359745cd3c5Smaybee 		uint64_t zplversion;
3609e6eda55Smarks 
361503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
362503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
363503ad85cSMatthew Ahrens 			return (B_TRUE);
364503ad85cSMatthew Ahrens 		}
365503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
366745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
367745cd3c5Smaybee 			rc = zplversion < version;
368503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3699e6eda55Smarks 	}
3709e6eda55Smarks 	return (rc);
3719e6eda55Smarks }
3729e6eda55Smarks 
373228975ccSek static void
374228975ccSek zfs_log_history(zfs_cmd_t *zc)
375228975ccSek {
376228975ccSek 	spa_t *spa;
377228975ccSek 	char *buf;
378ecd6cf80Smarks 
379228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
380228975ccSek 		return;
381228975ccSek 
382228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
383228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3844445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
385228975ccSek 		spa_close(spa, FTAG);
386228975ccSek 	}
387228975ccSek 	history_str_free(buf);
388ecd6cf80Smarks }
389ecd6cf80Smarks 
390fa9e4066Sahrens /*
391fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
392fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
393fa9e4066Sahrens  */
394fa9e4066Sahrens /* ARGSUSED */
395fa9e4066Sahrens static int
3964445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
397fa9e4066Sahrens {
398fa9e4066Sahrens 	return (0);
399fa9e4066Sahrens }
400fa9e4066Sahrens 
401fa9e4066Sahrens /*
402fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
403fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
404fa9e4066Sahrens  */
405fa9e4066Sahrens /* ARGSUSED */
406fa9e4066Sahrens static int
4074445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
408fa9e4066Sahrens {
409fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
410ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
411fa9e4066Sahrens 		return (0);
412fa9e4066Sahrens 
413be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416fa9e4066Sahrens static int
417a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
418fa9e4066Sahrens {
419fa9e4066Sahrens 	int writable = 1;
420fa9e4066Sahrens 
421fa9e4066Sahrens 	/*
422fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
423fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
424fa9e4066Sahrens 	 */
425fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
426fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
427be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
428fa9e4066Sahrens 
429fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
430fa9e4066Sahrens 		/*
431fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
432fa9e4066Sahrens 		 * global zone.
433fa9e4066Sahrens 		 */
434fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
435be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
436fa9e4066Sahrens 	} else {
437fa9e4066Sahrens 		/*
438fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
439fa9e4066Sahrens 		 */
440fa9e4066Sahrens 		if (!zoned)
441be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
442fa9e4066Sahrens 
443fa9e4066Sahrens 		/* must be writable by this zone */
444fa9e4066Sahrens 		if (!writable)
445be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
446fa9e4066Sahrens 	}
447fa9e4066Sahrens 	return (0);
448fa9e4066Sahrens }
449fa9e4066Sahrens 
450a7f53a56SChris Kirby static int
451a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
452a7f53a56SChris Kirby {
453a7f53a56SChris Kirby 	uint64_t zoned;
454a7f53a56SChris Kirby 
455a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
456be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
457a7f53a56SChris Kirby 
458a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
459a7f53a56SChris Kirby }
460a7f53a56SChris Kirby 
461a7f53a56SChris Kirby static int
462a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
463a7f53a56SChris Kirby {
464a7f53a56SChris Kirby 	uint64_t zoned;
465a7f53a56SChris Kirby 
4663b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
467be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
468a7f53a56SChris Kirby 
469a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
470a7f53a56SChris Kirby }
471a7f53a56SChris Kirby 
4724445fffbSMatthew Ahrens static int
4733b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4743b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
475fa9e4066Sahrens {
476fa9e4066Sahrens 	int error;
477fa9e4066Sahrens 
47819b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
479ecd6cf80Smarks 	if (error == 0) {
480ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4813b2aab18SMatthew Ahrens 		if (error != 0)
4824445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
483ecd6cf80Smarks 	}
484ecd6cf80Smarks 	return (error);
485ecd6cf80Smarks }
486ecd6cf80Smarks 
4874445fffbSMatthew Ahrens static int
4883b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
489a7f53a56SChris Kirby {
490a7f53a56SChris Kirby 	int error;
4913b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4923b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
493a7f53a56SChris Kirby 
4943b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
4953b2aab18SMatthew Ahrens 	if (error != 0)
4963b2aab18SMatthew Ahrens 		return (error);
4973b2aab18SMatthew Ahrens 
4983b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
4993b2aab18SMatthew Ahrens 	if (error != 0) {
5003b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5013b2aab18SMatthew Ahrens 		return (error);
502a7f53a56SChris Kirby 	}
5033b2aab18SMatthew Ahrens 
5043b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5053b2aab18SMatthew Ahrens 
5063b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5073b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
508a7f53a56SChris Kirby 	return (error);
509a7f53a56SChris Kirby }
510a7f53a56SChris Kirby 
5114201a95eSRic Aleshire /*
5124201a95eSRic Aleshire  * Policy for setting the security label property.
5134201a95eSRic Aleshire  *
5144201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5154201a95eSRic Aleshire  */
5164201a95eSRic Aleshire static int
51792241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5184201a95eSRic Aleshire {
5194201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5204201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5214201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5224201a95eSRic Aleshire 	uint64_t	zoned;
5234201a95eSRic Aleshire 	int		needed_priv = -1;
5244201a95eSRic Aleshire 	int		error;
5254201a95eSRic Aleshire 
5264201a95eSRic Aleshire 	/* First get the existing dataset label. */
5274201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5284201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5293b2aab18SMatthew Ahrens 	if (error != 0)
530be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5314201a95eSRic Aleshire 
5324201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5334201a95eSRic Aleshire 		new_default = TRUE;
5344201a95eSRic Aleshire 
5354201a95eSRic Aleshire 	/* The label must be translatable */
5364201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
537be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5384201a95eSRic Aleshire 
5394201a95eSRic Aleshire 	/*
5404201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5414201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5424201a95eSRic Aleshire 	 * are needed.
5434201a95eSRic Aleshire 	 */
5444201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5454201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
546be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5474201a95eSRic Aleshire 		return (0);
5484201a95eSRic Aleshire 	}
5494201a95eSRic Aleshire 
5504201a95eSRic Aleshire 	/*
5514201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5524201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5534201a95eSRic Aleshire 	 * global zone.
5544201a95eSRic Aleshire 	 */
5554201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5564201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
557be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5584201a95eSRic Aleshire 	if (!zoned) {
5594201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
560be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5614201a95eSRic Aleshire 	}
5624201a95eSRic Aleshire 
5634201a95eSRic Aleshire 	/*
5644201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5654201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5664201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5674201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5684201a95eSRic Aleshire 	 */
5694201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
57092241e0bSTom Erickson 		objset_t *os;
57192241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
57292241e0bSTom Erickson 
5734201a95eSRic Aleshire 		/*
5744201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5754201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5764201a95eSRic Aleshire 		 */
5774201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
57892241e0bSTom Erickson 		    setsl_tag, &os);
5793b2aab18SMatthew Ahrens 		if (error != 0)
580be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5814201a95eSRic Aleshire 
58292241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
58392241e0bSTom Erickson 
5844201a95eSRic Aleshire 		if (new_default) {
5854201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5864201a95eSRic Aleshire 			goto out_check;
5874201a95eSRic Aleshire 		}
5884201a95eSRic Aleshire 
5894201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
590be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5914201a95eSRic Aleshire 
5924201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
5934201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5944201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
5954201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
5964201a95eSRic Aleshire 	} else {
5974201a95eSRic Aleshire 		/* dataset currently has a default label */
5984201a95eSRic Aleshire 		if (!new_default)
5994201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6004201a95eSRic Aleshire 	}
6014201a95eSRic Aleshire 
6024201a95eSRic Aleshire out_check:
6034201a95eSRic Aleshire 	if (needed_priv != -1)
6044201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6054201a95eSRic Aleshire 	return (0);
6064201a95eSRic Aleshire }
6074201a95eSRic Aleshire 
608ecd6cf80Smarks static int
60992241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
61092241e0bSTom Erickson     cred_t *cr)
611ecd6cf80Smarks {
61292241e0bSTom Erickson 	char *strval;
61392241e0bSTom Erickson 
614ecd6cf80Smarks 	/*
615ecd6cf80Smarks 	 * Check permissions for special properties.
616ecd6cf80Smarks 	 */
617ecd6cf80Smarks 	switch (prop) {
618ecd6cf80Smarks 	case ZFS_PROP_ZONED:
619ecd6cf80Smarks 		/*
620ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
621ecd6cf80Smarks 		 */
622ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
623be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
624ecd6cf80Smarks 		break;
625ecd6cf80Smarks 
626ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
627a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
628a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
629ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
630ecd6cf80Smarks 			uint64_t zoned;
631ecd6cf80Smarks 			char setpoint[MAXNAMELEN];
632ecd6cf80Smarks 			/*
633ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
634a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
635ecd6cf80Smarks 			 * the thing they own.
636ecd6cf80Smarks 			 */
63792241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
638ecd6cf80Smarks 			    setpoint))
639be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
64092241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
641be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
642ecd6cf80Smarks 		}
643db870a07Sahrens 		break;
6444201a95eSRic Aleshire 
6454201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6464201a95eSRic Aleshire 		if (!is_system_labeled())
647be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
64892241e0bSTom Erickson 
64992241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
65092241e0bSTom Erickson 			int err;
65192241e0bSTom Erickson 
65292241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
65392241e0bSTom Erickson 			if (err != 0)
65492241e0bSTom Erickson 				return (err);
65592241e0bSTom Erickson 		}
6564201a95eSRic Aleshire 		break;
657ecd6cf80Smarks 	}
658ecd6cf80Smarks 
65992241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
660ecd6cf80Smarks }
661ecd6cf80Smarks 
6624445fffbSMatthew Ahrens /* ARGSUSED */
6634445fffbSMatthew Ahrens static int
6644445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
665ecd6cf80Smarks {
666ecd6cf80Smarks 	int error;
667ecd6cf80Smarks 
668ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6693b2aab18SMatthew Ahrens 	if (error != 0)
670fa9e4066Sahrens 		return (error);
671fa9e4066Sahrens 
672ecd6cf80Smarks 	/*
673ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
674ecd6cf80Smarks 	 * dsl_deleg_can_allow()
675ecd6cf80Smarks 	 */
676ecd6cf80Smarks 	return (0);
677ecd6cf80Smarks }
678ecd6cf80Smarks 
6794445fffbSMatthew Ahrens /* ARGSUSED */
6804445fffbSMatthew Ahrens static int
6814445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
682ecd6cf80Smarks {
683681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
684681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
685ecd6cf80Smarks }
686ecd6cf80Smarks 
6874445fffbSMatthew Ahrens /* ARGSUSED */
6884445fffbSMatthew Ahrens static int
6894445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
690ecd6cf80Smarks {
691a7f53a56SChris Kirby 	dsl_pool_t *dp;
692a7f53a56SChris Kirby 	dsl_dataset_t *ds;
693a7f53a56SChris Kirby 	char *cp;
694a7f53a56SChris Kirby 	int error;
695a7f53a56SChris Kirby 
696a7f53a56SChris Kirby 	/*
697a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
698a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
699a7f53a56SChris Kirby 	 */
700a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
701a7f53a56SChris Kirby 	if (cp == NULL)
702be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7033b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7043b2aab18SMatthew Ahrens 	if (error != 0)
705a7f53a56SChris Kirby 		return (error);
706a7f53a56SChris Kirby 
707a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7083b2aab18SMatthew Ahrens 	if (error != 0) {
7093b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
710a7f53a56SChris Kirby 		return (error);
7113b2aab18SMatthew Ahrens 	}
712a7f53a56SChris Kirby 
713a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
714a7f53a56SChris Kirby 
715a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
716a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
717a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7183b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
719a7f53a56SChris Kirby 
720a7f53a56SChris Kirby 	return (error);
721ecd6cf80Smarks }
722ecd6cf80Smarks 
7234445fffbSMatthew Ahrens /* ARGSUSED */
724743a77edSAlan Wright static int
7254445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7264445fffbSMatthew Ahrens {
7274445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7284445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7294445fffbSMatthew Ahrens }
7304445fffbSMatthew Ahrens 
7314445fffbSMatthew Ahrens /* ARGSUSED */
7324445fffbSMatthew Ahrens static int
7334445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
734743a77edSAlan Wright {
735743a77edSAlan Wright 	vnode_t *vp;
736743a77edSAlan Wright 	int error;
737743a77edSAlan Wright 
738743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
739743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
740743a77edSAlan Wright 		return (error);
741743a77edSAlan Wright 
742743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
743743a77edSAlan Wright 
744743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
745743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
746743a77edSAlan Wright 	    zc->zc_name) != 0)) {
747743a77edSAlan Wright 		VN_RELE(vp);
748be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
749743a77edSAlan Wright 	}
750743a77edSAlan Wright 
751743a77edSAlan Wright 	VN_RELE(vp);
752743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
753743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
754743a77edSAlan Wright }
755743a77edSAlan Wright 
756ecd6cf80Smarks int
7574445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
758ecd6cf80Smarks {
759ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
760be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
761ecd6cf80Smarks 
7623cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
763ecd6cf80Smarks 		return (0);
764ecd6cf80Smarks 	} else {
7654445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
766743a77edSAlan Wright 	}
767743a77edSAlan Wright }
768ecd6cf80Smarks 
769743a77edSAlan Wright int
7704445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
771743a77edSAlan Wright {
772743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
773be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
774ecd6cf80Smarks 
775743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
776743a77edSAlan Wright 		return (0);
777743a77edSAlan Wright 	} else {
7784445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
779ecd6cf80Smarks 	}
780fa9e4066Sahrens }
781fa9e4066Sahrens 
782fa9e4066Sahrens static int
783ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
784fa9e4066Sahrens {
785fa9e4066Sahrens 	char *cp;
786fa9e4066Sahrens 
787fa9e4066Sahrens 	/*
788fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
789fa9e4066Sahrens 	 */
790ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
791ecd6cf80Smarks 	cp = strrchr(parent, '@');
792fa9e4066Sahrens 	if (cp != NULL) {
793fa9e4066Sahrens 		cp[0] = '\0';
794fa9e4066Sahrens 	} else {
795ecd6cf80Smarks 		cp = strrchr(parent, '/');
796fa9e4066Sahrens 		if (cp == NULL)
797be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
798fa9e4066Sahrens 		cp[0] = '\0';
799ecd6cf80Smarks 	}
800ecd6cf80Smarks 
801ecd6cf80Smarks 	return (0);
802ecd6cf80Smarks }
803ecd6cf80Smarks 
804ecd6cf80Smarks int
805ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
806ecd6cf80Smarks {
807ecd6cf80Smarks 	int error;
808ecd6cf80Smarks 
809ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
810ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
811ecd6cf80Smarks 		return (error);
812ecd6cf80Smarks 
813ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
814ecd6cf80Smarks }
815ecd6cf80Smarks 
8164445fffbSMatthew Ahrens /* ARGSUSED */
817ecd6cf80Smarks static int
8184445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
819ecd6cf80Smarks {
820ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
821ecd6cf80Smarks }
822ecd6cf80Smarks 
823cbf6f6aaSWilliam Gorrell /*
824cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8254445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
826cbf6f6aaSWilliam Gorrell  */
8274445fffbSMatthew Ahrens /* ARGSUSED */
828cbf6f6aaSWilliam Gorrell static int
8294445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
830cbf6f6aaSWilliam Gorrell {
8314445fffbSMatthew Ahrens 	nvlist_t *snaps;
8324445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8334445fffbSMatthew Ahrens 	int error = 0;
834cbf6f6aaSWilliam Gorrell 
8354445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
836be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8374445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8384445fffbSMatthew Ahrens 	    pair = nextpair) {
8394445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
84078f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
84178f17100SMatthew Ahrens 		if (error == ENOENT) {
8424445fffbSMatthew Ahrens 			/*
8434445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8444445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8454445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8464445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8474445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8484445fffbSMatthew Ahrens 			 * checked for permission).
8494445fffbSMatthew Ahrens 			 */
8504445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8514445fffbSMatthew Ahrens 			error = 0;
8524445fffbSMatthew Ahrens 		}
8534445fffbSMatthew Ahrens 		if (error != 0)
8544445fffbSMatthew Ahrens 			break;
8554445fffbSMatthew Ahrens 	}
856cbf6f6aaSWilliam Gorrell 
857cbf6f6aaSWilliam Gorrell 	return (error);
858cbf6f6aaSWilliam Gorrell }
859cbf6f6aaSWilliam Gorrell 
860ecd6cf80Smarks int
861ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
862ecd6cf80Smarks {
86392241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
864ecd6cf80Smarks 	int	error;
865ecd6cf80Smarks 
866ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
867ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
868ecd6cf80Smarks 		return (error);
869ecd6cf80Smarks 
870ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
871ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
872ecd6cf80Smarks 		return (error);
873ecd6cf80Smarks 
874ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
875ecd6cf80Smarks 	    sizeof (parentname))) != 0)
876ecd6cf80Smarks 		return (error);
877ecd6cf80Smarks 
878ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
879ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
880ecd6cf80Smarks 		return (error);
881ecd6cf80Smarks 
882ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
883ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
884ecd6cf80Smarks 		return (error);
885ecd6cf80Smarks 
886ecd6cf80Smarks 	return (error);
887ecd6cf80Smarks }
888ecd6cf80Smarks 
8894445fffbSMatthew Ahrens /* ARGSUSED */
890ecd6cf80Smarks static int
8914445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
892ecd6cf80Smarks {
893ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
894ecd6cf80Smarks }
895ecd6cf80Smarks 
8964445fffbSMatthew Ahrens /* ARGSUSED */
897ecd6cf80Smarks static int
8984445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
899ecd6cf80Smarks {
9003b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9013b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
902ecd6cf80Smarks 	int error;
903ecd6cf80Smarks 
904ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
905ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9063b2aab18SMatthew Ahrens 	if (error != 0)
9073b2aab18SMatthew Ahrens 		return (error);
9083b2aab18SMatthew Ahrens 
9093b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9103b2aab18SMatthew Ahrens 	if (error != 0)
911ecd6cf80Smarks 		return (error);
912ecd6cf80Smarks 
9133b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
914ecd6cf80Smarks 
915ecd6cf80Smarks 	if (error == 0) {
9163b2aab18SMatthew Ahrens 		char parentname[MAXNAMELEN];
9173b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
918ecd6cf80Smarks 		dsl_dir_t *dd;
9193b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
920ecd6cf80Smarks 
921745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
922c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9233b2aab18SMatthew Ahrens 		if (error != 0) {
9243b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9253b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
926ecd6cf80Smarks 			return (error);
927ecd6cf80Smarks 		}
928ecd6cf80Smarks 
9293b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
930ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
931ecd6cf80Smarks 
9323b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9333b2aab18SMatthew Ahrens 		if (error == 0) {
9343b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
935ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9363b2aab18SMatthew Ahrens 		}
9373b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9383b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
939ecd6cf80Smarks 	}
9403b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
941ecd6cf80Smarks 	return (error);
942ecd6cf80Smarks }
943ecd6cf80Smarks 
9444445fffbSMatthew Ahrens /* ARGSUSED */
945ecd6cf80Smarks static int
9464445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
947ecd6cf80Smarks {
948ecd6cf80Smarks 	int error;
949ecd6cf80Smarks 
950ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
951ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
952ecd6cf80Smarks 		return (error);
953ecd6cf80Smarks 
954ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
955ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
956ecd6cf80Smarks 		return (error);
957ecd6cf80Smarks 
958ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
959ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
960ecd6cf80Smarks }
961ecd6cf80Smarks 
962ecd6cf80Smarks int
963ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
964ecd6cf80Smarks {
965681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
966681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
967ecd6cf80Smarks }
968ecd6cf80Smarks 
9694445fffbSMatthew Ahrens /*
9704445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9714445fffbSMatthew Ahrens  */
9724445fffbSMatthew Ahrens /* ARGSUSED */
973ecd6cf80Smarks static int
9744445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
975ecd6cf80Smarks {
9764445fffbSMatthew Ahrens 	nvlist_t *snaps;
977d5285caeSGeorge Wilson 	int error = 0;
9784445fffbSMatthew Ahrens 	nvpair_t *pair;
979ecd6cf80Smarks 
9804445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
981be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9824445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9834445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9844445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9854445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9864445fffbSMatthew Ahrens 
9874445fffbSMatthew Ahrens 		if (atp == NULL) {
988be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9894445fffbSMatthew Ahrens 			break;
9904445fffbSMatthew Ahrens 		}
9914445fffbSMatthew Ahrens 		*atp = '\0';
9924445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
9934445fffbSMatthew Ahrens 		*atp = '@';
9944445fffbSMatthew Ahrens 		if (error != 0)
9954445fffbSMatthew Ahrens 			break;
9964445fffbSMatthew Ahrens 	}
9974445fffbSMatthew Ahrens 	return (error);
998ecd6cf80Smarks }
999ecd6cf80Smarks 
100078f17100SMatthew Ahrens /*
100178f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
100278f17100SMatthew Ahrens  */
100378f17100SMatthew Ahrens /* ARGSUSED */
100478f17100SMatthew Ahrens static int
100578f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
100678f17100SMatthew Ahrens {
100778f17100SMatthew Ahrens 	int error = 0;
100878f17100SMatthew Ahrens 
100978f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
101078f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
101178f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
101278f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
101378f17100SMatthew Ahrens 
101478f17100SMatthew Ahrens 		if (hashp == NULL) {
101578f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
101678f17100SMatthew Ahrens 			break;
101778f17100SMatthew Ahrens 		}
101878f17100SMatthew Ahrens 		*hashp = '\0';
101978f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
102078f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
102178f17100SMatthew Ahrens 		*hashp = '#';
102278f17100SMatthew Ahrens 		if (error != 0)
102378f17100SMatthew Ahrens 			break;
102478f17100SMatthew Ahrens 	}
102578f17100SMatthew Ahrens 	return (error);
102678f17100SMatthew Ahrens }
102778f17100SMatthew Ahrens 
102878f17100SMatthew Ahrens /* ARGSUSED */
102978f17100SMatthew Ahrens static int
103078f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
103178f17100SMatthew Ahrens {
103278f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
103378f17100SMatthew Ahrens 	int error = 0;
103478f17100SMatthew Ahrens 
103578f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
103678f17100SMatthew Ahrens 	    pair = nextpair) {
103778f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
103878f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
103978f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
104078f17100SMatthew Ahrens 
104178f17100SMatthew Ahrens 		if (hashp == NULL) {
104278f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
104378f17100SMatthew Ahrens 			break;
104478f17100SMatthew Ahrens 		}
104578f17100SMatthew Ahrens 
104678f17100SMatthew Ahrens 		*hashp = '\0';
104778f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
104878f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
104978f17100SMatthew Ahrens 		*hashp = '#';
105078f17100SMatthew Ahrens 		if (error == ENOENT) {
105178f17100SMatthew Ahrens 			/*
105278f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
105378f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
105478f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
105578f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
105678f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
105778f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
105878f17100SMatthew Ahrens 			 */
105978f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
106078f17100SMatthew Ahrens 			error = 0;
106178f17100SMatthew Ahrens 		}
106278f17100SMatthew Ahrens 		if (error != 0)
106378f17100SMatthew Ahrens 			break;
106478f17100SMatthew Ahrens 	}
106578f17100SMatthew Ahrens 
106678f17100SMatthew Ahrens 	return (error);
106778f17100SMatthew Ahrens }
106878f17100SMatthew Ahrens 
10694445fffbSMatthew Ahrens /* ARGSUSED */
1070ecd6cf80Smarks static int
10714445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10724445fffbSMatthew Ahrens {
10734445fffbSMatthew Ahrens 	/*
10744445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10754445fffbSMatthew Ahrens 	 * to log to.
10764445fffbSMatthew Ahrens 	 */
10774445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1078be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10794445fffbSMatthew Ahrens 	return (0);
10804445fffbSMatthew Ahrens }
10814445fffbSMatthew Ahrens 
10824445fffbSMatthew Ahrens static int
10834445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1084ecd6cf80Smarks {
108592241e0bSTom Erickson 	char	parentname[MAXNAMELEN];
108692241e0bSTom Erickson 	int	error;
10874445fffbSMatthew Ahrens 	char	*origin;
1088ecd6cf80Smarks 
1089ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1090ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1091ecd6cf80Smarks 		return (error);
1092fa9e4066Sahrens 
10934445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
10944445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
10954445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
10964445fffbSMatthew Ahrens 		return (error);
1097fa9e4066Sahrens 
1098ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1099ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1100ecd6cf80Smarks 		return (error);
1101ecd6cf80Smarks 
11024445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11034445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1104fa9e4066Sahrens }
1105fa9e4066Sahrens 
1106fa9e4066Sahrens /*
1107fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1108fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1109fa9e4066Sahrens  */
1110fa9e4066Sahrens /* ARGSUSED */
1111fa9e4066Sahrens static int
11124445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1113fa9e4066Sahrens {
1114fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1115be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1116fa9e4066Sahrens 
1117fa9e4066Sahrens 	return (0);
1118fa9e4066Sahrens }
1119fa9e4066Sahrens 
112099d5e173STim Haley /*
112199d5e173STim Haley  * Policy for object to name lookups.
112299d5e173STim Haley  */
112399d5e173STim Haley /* ARGSUSED */
112499d5e173STim Haley static int
11254445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
112699d5e173STim Haley {
112799d5e173STim Haley 	int error;
112899d5e173STim Haley 
112999d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
113099d5e173STim Haley 		return (0);
113199d5e173STim Haley 
113299d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
113399d5e173STim Haley 	return (error);
113499d5e173STim Haley }
113599d5e173STim Haley 
1136ea8dc4b6Seschrock /*
1137ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1138ea8dc4b6Seschrock  */
1139ea8dc4b6Seschrock /* ARGSUSED */
1140ea8dc4b6Seschrock static int
11414445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1142ea8dc4b6Seschrock {
1143ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1144ea8dc4b6Seschrock }
1145ea8dc4b6Seschrock 
11464445fffbSMatthew Ahrens /* ARGSUSED */
1147e45ce728Sahrens static int
11484445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1149e45ce728Sahrens {
1150e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1151e45ce728Sahrens 
1152990b4856Slling 	if (prop == ZPROP_INVAL) {
1153e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1154be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1155e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1156e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1157e45ce728Sahrens 	} else {
115892241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
115992241e0bSTom Erickson 		    NULL, cr));
1160e45ce728Sahrens 	}
1161e45ce728Sahrens }
1162e45ce728Sahrens 
116314843421SMatthew Ahrens static int
11644445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
116514843421SMatthew Ahrens {
11664445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
116714843421SMatthew Ahrens 	if (err)
116814843421SMatthew Ahrens 		return (err);
116914843421SMatthew Ahrens 
117014843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1171be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
117214843421SMatthew Ahrens 
117314843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
117414843421SMatthew Ahrens 		/*
117514843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
117614843421SMatthew Ahrens 		 * themself, allow it.
117714843421SMatthew Ahrens 		 */
117814843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
117914843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
118014843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
118114843421SMatthew Ahrens 				return (0);
118214843421SMatthew Ahrens 		} else {
118314843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
118414843421SMatthew Ahrens 				return (0);
118514843421SMatthew Ahrens 		}
118614843421SMatthew Ahrens 	}
118714843421SMatthew Ahrens 
118814843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
118914843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
119014843421SMatthew Ahrens }
119114843421SMatthew Ahrens 
119214843421SMatthew Ahrens static int
11934445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
119414843421SMatthew Ahrens {
11954445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
119614843421SMatthew Ahrens 	if (err)
119714843421SMatthew Ahrens 		return (err);
119814843421SMatthew Ahrens 
119914843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1200be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
120114843421SMatthew Ahrens 
120214843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
120314843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
120414843421SMatthew Ahrens }
120514843421SMatthew Ahrens 
12064445fffbSMatthew Ahrens /* ARGSUSED */
120714843421SMatthew Ahrens static int
12084445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120914843421SMatthew Ahrens {
121092241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
121192241e0bSTom Erickson 	    NULL, cr));
121214843421SMatthew Ahrens }
121314843421SMatthew Ahrens 
12144445fffbSMatthew Ahrens /* ARGSUSED */
1215842727c2SChris Kirby static int
12164445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1217842727c2SChris Kirby {
12183b2aab18SMatthew Ahrens 	nvpair_t *pair;
12193b2aab18SMatthew Ahrens 	nvlist_t *holds;
12203b2aab18SMatthew Ahrens 	int error;
12213b2aab18SMatthew Ahrens 
12223b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12233b2aab18SMatthew Ahrens 	if (error != 0)
1224be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12253b2aab18SMatthew Ahrens 
12263b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12273b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
12283b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
12293b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12303b2aab18SMatthew Ahrens 		if (error != 0)
12313b2aab18SMatthew Ahrens 			return (error);
12323b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12333b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12343b2aab18SMatthew Ahrens 		if (error != 0)
12353b2aab18SMatthew Ahrens 			return (error);
12363b2aab18SMatthew Ahrens 	}
12373b2aab18SMatthew Ahrens 	return (0);
1238842727c2SChris Kirby }
1239842727c2SChris Kirby 
12404445fffbSMatthew Ahrens /* ARGSUSED */
1241842727c2SChris Kirby static int
12424445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1243842727c2SChris Kirby {
12443b2aab18SMatthew Ahrens 	nvpair_t *pair;
12453b2aab18SMatthew Ahrens 	int error;
12463b2aab18SMatthew Ahrens 
12473b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12483b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
12493b2aab18SMatthew Ahrens 		char fsname[MAXNAMELEN];
12503b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12513b2aab18SMatthew Ahrens 		if (error != 0)
12523b2aab18SMatthew Ahrens 			return (error);
12533b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12543b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12553b2aab18SMatthew Ahrens 		if (error != 0)
12563b2aab18SMatthew Ahrens 			return (error);
12573b2aab18SMatthew Ahrens 	}
12583b2aab18SMatthew Ahrens 	return (0);
1259842727c2SChris Kirby }
1260842727c2SChris Kirby 
126199d5e173STim Haley /*
126299d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
126399d5e173STim Haley  */
126499d5e173STim Haley static int
12654445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
126699d5e173STim Haley {
126799d5e173STim Haley 	/*
126899d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
126999d5e173STim Haley 	 * hold, destroy and release all rolled into one.
127099d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
127199d5e173STim Haley 	 */
127299d5e173STim Haley 	int error;
127399d5e173STim Haley 
127499d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
127599d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
127699d5e173STim Haley 		return (0);
127799d5e173STim Haley 
12784445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12793b2aab18SMatthew Ahrens 	if (error == 0)
12804445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12813b2aab18SMatthew Ahrens 	if (error == 0)
12824445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12833b2aab18SMatthew Ahrens 	if (error == 0)
12844445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
128599d5e173STim Haley 	return (error);
128699d5e173STim Haley }
128799d5e173STim Haley 
1288fa9e4066Sahrens /*
1289fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1290fa9e4066Sahrens  */
1291fa9e4066Sahrens static int
1292478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1293fa9e4066Sahrens {
1294fa9e4066Sahrens 	char *packed;
1295fa9e4066Sahrens 	int error;
1296990b4856Slling 	nvlist_t *list = NULL;
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	/*
1299e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1300fa9e4066Sahrens 	 */
1301990b4856Slling 	if (size == 0)
1302be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1305fa9e4066Sahrens 
1306478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1307478ed9adSEric Taylor 	    iflag)) != 0) {
1308fa9e4066Sahrens 		kmem_free(packed, size);
1309c71c00bbSRichard Yao 		return (SET_ERROR(EFAULT));
1310fa9e4066Sahrens 	}
1311fa9e4066Sahrens 
1312990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1313fa9e4066Sahrens 		kmem_free(packed, size);
1314fa9e4066Sahrens 		return (error);
1315fa9e4066Sahrens 	}
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	kmem_free(packed, size);
1318fa9e4066Sahrens 
1319990b4856Slling 	*nvp = list;
1320fa9e4066Sahrens 	return (0);
1321fa9e4066Sahrens }
1322fa9e4066Sahrens 
13234445fffbSMatthew Ahrens /*
13244445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13254445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13264445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13274445fffbSMatthew Ahrens  * removed.
13284445fffbSMatthew Ahrens  */
132992241e0bSTom Erickson static int
13304445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
133192241e0bSTom Erickson {
133292241e0bSTom Erickson 	size_t size;
133392241e0bSTom Erickson 
13344445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
133592241e0bSTom Erickson 
13364445fffbSMatthew Ahrens 	if (size > max) {
133792241e0bSTom Erickson 		nvpair_t *more_errors;
133892241e0bSTom Erickson 		int n = 0;
133992241e0bSTom Erickson 
13404445fffbSMatthew Ahrens 		if (max < 1024)
1341be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
134292241e0bSTom Erickson 
13434445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13444445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
134592241e0bSTom Erickson 
134692241e0bSTom Erickson 		do {
13474445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
134892241e0bSTom Erickson 			    more_errors);
13494445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
135092241e0bSTom Erickson 			n++;
13514445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13524445fffbSMatthew Ahrens 		} while (size > max);
135392241e0bSTom Erickson 
13544445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13554445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13564445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
135792241e0bSTom Erickson 	}
135892241e0bSTom Erickson 
135992241e0bSTom Erickson 	return (0);
136092241e0bSTom Erickson }
136192241e0bSTom Erickson 
1362e9dbad6fSeschrock static int
1363e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1364e9dbad6fSeschrock {
1365e9dbad6fSeschrock 	char *packed = NULL;
13666e27f868SSam Falkner 	int error = 0;
1367e9dbad6fSeschrock 	size_t size;
1368e9dbad6fSeschrock 
13694445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1370e9dbad6fSeschrock 
1371e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1372be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1373e9dbad6fSeschrock 	} else {
13744445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
13756e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
13766e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1377be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
13784445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1379e9dbad6fSeschrock 	}
1380e9dbad6fSeschrock 
1381e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
13824445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1383e9dbad6fSeschrock 	return (error);
1384e9dbad6fSeschrock }
1385e9dbad6fSeschrock 
138614843421SMatthew Ahrens static int
1387af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
138814843421SMatthew Ahrens {
138914843421SMatthew Ahrens 	objset_t *os;
139014843421SMatthew Ahrens 	int error;
139114843421SMatthew Ahrens 
1392503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
13933b2aab18SMatthew Ahrens 	if (error != 0)
139414843421SMatthew Ahrens 		return (error);
1395503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1396503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1397be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1398503ad85cSMatthew Ahrens 	}
139914843421SMatthew Ahrens 
1400503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1401af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1402af4c679fSSean McEnroe 	if (*zfvp) {
1403af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
140414843421SMatthew Ahrens 	} else {
1405be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
140614843421SMatthew Ahrens 	}
1407503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1408503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
140914843421SMatthew Ahrens 	return (error);
141014843421SMatthew Ahrens }
141114843421SMatthew Ahrens 
141214843421SMatthew Ahrens /*
141314843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
141414843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1415ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1416ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
141714843421SMatthew Ahrens  */
141814843421SMatthew Ahrens static int
14191412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
142014843421SMatthew Ahrens {
142114843421SMatthew Ahrens 	int error = 0;
142214843421SMatthew Ahrens 
1423af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1424af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
142514843421SMatthew Ahrens 	if (error == 0) {
1426c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14271412a1a2SMark Shellenbaum 		    RW_READER, tag);
1428af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
142914843421SMatthew Ahrens 			/*
143014843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
143114843421SMatthew Ahrens 			 * thread should be just about to disassociate the
143214843421SMatthew Ahrens 			 * objset from the zfsvfs.
143314843421SMatthew Ahrens 			 */
1434c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1435be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
143614843421SMatthew Ahrens 		}
143714843421SMatthew Ahrens 	}
143814843421SMatthew Ahrens 	return (error);
143914843421SMatthew Ahrens }
144014843421SMatthew Ahrens 
144114843421SMatthew Ahrens static void
144214843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
144314843421SMatthew Ahrens {
1444c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
144514843421SMatthew Ahrens 
144614843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
144714843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
144814843421SMatthew Ahrens 	} else {
1449503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
145014843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
145114843421SMatthew Ahrens 	}
145214843421SMatthew Ahrens }
145314843421SMatthew Ahrens 
1454fa9e4066Sahrens static int
1455fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1456fa9e4066Sahrens {
1457fa9e4066Sahrens 	int error;
1458990b4856Slling 	nvlist_t *config, *props = NULL;
14590a48a24eStimh 	nvlist_t *rootprops = NULL;
14600a48a24eStimh 	nvlist_t *zplprops = NULL;
1461fa9e4066Sahrens 
1462990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1463478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1464fa9e4066Sahrens 		return (error);
14652a6b87f0Sek 
1466990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1467478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1468478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1469990b4856Slling 		nvlist_free(config);
1470990b4856Slling 		return (error);
1471990b4856Slling 	}
1472990b4856Slling 
14730a48a24eStimh 	if (props) {
14740a48a24eStimh 		nvlist_t *nvl = NULL;
14750a48a24eStimh 		uint64_t version = SPA_VERSION;
14760a48a24eStimh 
14770a48a24eStimh 		(void) nvlist_lookup_uint64(props,
14780a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1479ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1480be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
14810a48a24eStimh 			goto pool_props_bad;
14820a48a24eStimh 		}
14830a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
14840a48a24eStimh 		if (nvl) {
14850a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
14860a48a24eStimh 			if (error != 0) {
14870a48a24eStimh 				nvlist_free(config);
14880a48a24eStimh 				nvlist_free(props);
14890a48a24eStimh 				return (error);
14900a48a24eStimh 			}
14910a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
14920a48a24eStimh 		}
14930a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
14940a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
14950a48a24eStimh 		    zplprops, NULL);
14963b2aab18SMatthew Ahrens 		if (error != 0)
14970a48a24eStimh 			goto pool_props_bad;
14980a48a24eStimh 	}
14990a48a24eStimh 
15004445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
15010a48a24eStimh 
15020a48a24eStimh 	/*
15030a48a24eStimh 	 * Set the remaining root properties
15040a48a24eStimh 	 */
150592241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
150692241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15070a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1508fa9e4066Sahrens 
15090a48a24eStimh pool_props_bad:
15100a48a24eStimh 	nvlist_free(rootprops);
15110a48a24eStimh 	nvlist_free(zplprops);
1512fa9e4066Sahrens 	nvlist_free(config);
15130a48a24eStimh 	nvlist_free(props);
1514990b4856Slling 
1515fa9e4066Sahrens 	return (error);
1516fa9e4066Sahrens }
1517fa9e4066Sahrens 
1518fa9e4066Sahrens static int
1519fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1520fa9e4066Sahrens {
1521ecd6cf80Smarks 	int error;
1522ecd6cf80Smarks 	zfs_log_history(zc);
1523ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1524681d9761SEric Taylor 	if (error == 0)
1525681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1526ecd6cf80Smarks 	return (error);
1527fa9e4066Sahrens }
1528fa9e4066Sahrens 
1529fa9e4066Sahrens static int
1530fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1531fa9e4066Sahrens {
1532990b4856Slling 	nvlist_t *config, *props = NULL;
1533fa9e4066Sahrens 	uint64_t guid;
1534468c413aSTim Haley 	int error;
1535fa9e4066Sahrens 
1536990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1537478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1538990b4856Slling 		return (error);
1539990b4856Slling 
1540990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1541478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1542478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1543990b4856Slling 		nvlist_free(config);
1544fa9e4066Sahrens 		return (error);
1545990b4856Slling 	}
1546fa9e4066Sahrens 
1547fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1548ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1549be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1550fa9e4066Sahrens 	else
15514b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1552fa9e4066Sahrens 
15534b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15544b964adaSGeorge Wilson 		int err;
15554b964adaSGeorge Wilson 
15564b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15574b964adaSGeorge Wilson 			error = err;
15584b964adaSGeorge Wilson 	}
1559468c413aSTim Haley 
1560fa9e4066Sahrens 	nvlist_free(config);
1561fa9e4066Sahrens 
1562aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1563990b4856Slling 
1564fa9e4066Sahrens 	return (error);
1565fa9e4066Sahrens }
1566fa9e4066Sahrens 
1567fa9e4066Sahrens static int
1568fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1569fa9e4066Sahrens {
1570ecd6cf80Smarks 	int error;
157189a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1572394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
157389a89ebfSlling 
1574ecd6cf80Smarks 	zfs_log_history(zc);
1575394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1576681d9761SEric Taylor 	if (error == 0)
1577681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1578ecd6cf80Smarks 	return (error);
1579fa9e4066Sahrens }
1580fa9e4066Sahrens 
1581fa9e4066Sahrens static int
1582fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1583fa9e4066Sahrens {
1584fa9e4066Sahrens 	nvlist_t *configs;
1585fa9e4066Sahrens 	int error;
1586fa9e4066Sahrens 
1587fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1588be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1589fa9e4066Sahrens 
1590e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1591fa9e4066Sahrens 
1592fa9e4066Sahrens 	nvlist_free(configs);
1593fa9e4066Sahrens 
1594fa9e4066Sahrens 	return (error);
1595fa9e4066Sahrens }
1596fa9e4066Sahrens 
1597ad135b5dSChristopher Siden /*
1598ad135b5dSChristopher Siden  * inputs:
1599ad135b5dSChristopher Siden  * zc_name		name of the pool
1600ad135b5dSChristopher Siden  *
1601ad135b5dSChristopher Siden  * outputs:
1602ad135b5dSChristopher Siden  * zc_cookie		real errno
1603ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1604ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1605ad135b5dSChristopher Siden  */
1606fa9e4066Sahrens static int
1607fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1608fa9e4066Sahrens {
1609fa9e4066Sahrens 	nvlist_t *config;
1610fa9e4066Sahrens 	int error;
1611ea8dc4b6Seschrock 	int ret = 0;
1612fa9e4066Sahrens 
1613e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1614e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1615fa9e4066Sahrens 
1616fa9e4066Sahrens 	if (config != NULL) {
1617e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1618fa9e4066Sahrens 		nvlist_free(config);
1619ea8dc4b6Seschrock 
1620ea8dc4b6Seschrock 		/*
1621ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1622ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1623ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1624ea8dc4b6Seschrock 		 */
1625ea8dc4b6Seschrock 		zc->zc_cookie = error;
1626fa9e4066Sahrens 	} else {
1627ea8dc4b6Seschrock 		ret = error;
1628fa9e4066Sahrens 	}
1629fa9e4066Sahrens 
1630ea8dc4b6Seschrock 	return (ret);
1631fa9e4066Sahrens }
1632fa9e4066Sahrens 
1633fa9e4066Sahrens /*
1634fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1635fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1636fa9e4066Sahrens  */
1637fa9e4066Sahrens static int
1638fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1639fa9e4066Sahrens {
1640fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1641fa9e4066Sahrens 	int error;
1642fa9e4066Sahrens 
1643990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1644478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1645fa9e4066Sahrens 		return (error);
1646fa9e4066Sahrens 
1647fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1648fa9e4066Sahrens 
1649fa9e4066Sahrens 	nvlist_free(tryconfig);
1650fa9e4066Sahrens 
1651fa9e4066Sahrens 	if (config == NULL)
1652be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1653fa9e4066Sahrens 
1654e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1655fa9e4066Sahrens 	nvlist_free(config);
1656fa9e4066Sahrens 
1657fa9e4066Sahrens 	return (error);
1658fa9e4066Sahrens }
1659fa9e4066Sahrens 
16603f9d6ad7SLin Ling /*
16613f9d6ad7SLin Ling  * inputs:
16623f9d6ad7SLin Ling  * zc_name              name of the pool
16633f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16643f9d6ad7SLin Ling  */
1665fa9e4066Sahrens static int
16663f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1667fa9e4066Sahrens {
1668fa9e4066Sahrens 	spa_t *spa;
1669fa9e4066Sahrens 	int error;
1670fa9e4066Sahrens 
167106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
167206eeb2adSek 		return (error);
167306eeb2adSek 
16743f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
16753f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
16763f9d6ad7SLin Ling 	else
16773f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
167806eeb2adSek 
167906eeb2adSek 	spa_close(spa, FTAG);
168006eeb2adSek 
1681fa9e4066Sahrens 	return (error);
1682fa9e4066Sahrens }
1683fa9e4066Sahrens 
1684fa9e4066Sahrens static int
1685fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1686fa9e4066Sahrens {
1687fa9e4066Sahrens 	spa_t *spa;
1688fa9e4066Sahrens 	int error;
1689fa9e4066Sahrens 
1690fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1691fa9e4066Sahrens 	if (error == 0) {
1692fa9e4066Sahrens 		spa_freeze(spa);
1693fa9e4066Sahrens 		spa_close(spa, FTAG);
1694fa9e4066Sahrens 	}
1695fa9e4066Sahrens 	return (error);
1696fa9e4066Sahrens }
1697fa9e4066Sahrens 
1698eaca9bbdSeschrock static int
1699eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1700eaca9bbdSeschrock {
1701eaca9bbdSeschrock 	spa_t *spa;
1702eaca9bbdSeschrock 	int error;
1703eaca9bbdSeschrock 
170406eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
170506eeb2adSek 		return (error);
170606eeb2adSek 
1707ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1708ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1709558d2d50Slling 		spa_close(spa, FTAG);
1710be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1711558d2d50Slling 	}
1712558d2d50Slling 
1713990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
171406eeb2adSek 	spa_close(spa, FTAG);
171506eeb2adSek 
171606eeb2adSek 	return (error);
171706eeb2adSek }
171806eeb2adSek 
171906eeb2adSek static int
172006eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
172106eeb2adSek {
172206eeb2adSek 	spa_t *spa;
172306eeb2adSek 	char *hist_buf;
172406eeb2adSek 	uint64_t size;
172506eeb2adSek 	int error;
172606eeb2adSek 
172706eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1728be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
172906eeb2adSek 
173006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
173106eeb2adSek 		return (error);
173206eeb2adSek 
1733e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1734d7306b64Sek 		spa_close(spa, FTAG);
1735be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1736d7306b64Sek 	}
1737d7306b64Sek 
173806eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
173906eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
174006eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1741478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1742478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1743478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
174406eeb2adSek 	}
174506eeb2adSek 
174606eeb2adSek 	spa_close(spa, FTAG);
174706eeb2adSek 	kmem_free(hist_buf, size);
174806eeb2adSek 	return (error);
174906eeb2adSek }
175006eeb2adSek 
1751e9103aaeSGarrett D'Amore static int
1752e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1753e9103aaeSGarrett D'Amore {
1754e9103aaeSGarrett D'Amore 	spa_t *spa;
1755e9103aaeSGarrett D'Amore 	int error;
1756e9103aaeSGarrett D'Amore 
1757e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1758e9103aaeSGarrett D'Amore 	if (error == 0) {
1759e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1760e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1761e9103aaeSGarrett D'Amore 	}
1762e9103aaeSGarrett D'Amore 	return (error);
1763e9103aaeSGarrett D'Amore }
1764e9103aaeSGarrett D'Amore 
176555434c77Sek static int
176655434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
176755434c77Sek {
17683b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
176955434c77Sek }
177055434c77Sek 
1771503ad85cSMatthew Ahrens /*
1772503ad85cSMatthew Ahrens  * inputs:
1773503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1774503ad85cSMatthew Ahrens  * zc_obj		object to find
1775503ad85cSMatthew Ahrens  *
1776503ad85cSMatthew Ahrens  * outputs:
1777503ad85cSMatthew Ahrens  * zc_value		name of object
1778503ad85cSMatthew Ahrens  */
177955434c77Sek static int
178055434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
178155434c77Sek {
1782503ad85cSMatthew Ahrens 	objset_t *os;
178355434c77Sek 	int error;
178455434c77Sek 
1785503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1786503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
178755434c77Sek 		return (error);
1788503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1789503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1790be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1791503ad85cSMatthew Ahrens 	}
1792503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
179355434c77Sek 	    sizeof (zc->zc_value));
1794503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
179555434c77Sek 
179655434c77Sek 	return (error);
179755434c77Sek }
179855434c77Sek 
179999d5e173STim Haley /*
180099d5e173STim Haley  * inputs:
180199d5e173STim Haley  * zc_name		name of filesystem
180299d5e173STim Haley  * zc_obj		object to find
180399d5e173STim Haley  *
180499d5e173STim Haley  * outputs:
180599d5e173STim Haley  * zc_stat		stats on object
180699d5e173STim Haley  * zc_value		path to object
180799d5e173STim Haley  */
180899d5e173STim Haley static int
180999d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
181099d5e173STim Haley {
181199d5e173STim Haley 	objset_t *os;
181299d5e173STim Haley 	int error;
181399d5e173STim Haley 
181499d5e173STim Haley 	/* XXX reading from objset not owned */
181599d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
181699d5e173STim Haley 		return (error);
181799d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
181899d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1819be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
182099d5e173STim Haley 	}
182199d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
182299d5e173STim Haley 	    sizeof (zc->zc_value));
182399d5e173STim Haley 	dmu_objset_rele(os, FTAG);
182499d5e173STim Haley 
182599d5e173STim Haley 	return (error);
182699d5e173STim Haley }
182799d5e173STim Haley 
1828fa9e4066Sahrens static int
1829fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1830fa9e4066Sahrens {
1831fa9e4066Sahrens 	spa_t *spa;
1832fa9e4066Sahrens 	int error;
1833e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1834e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1835fa9e4066Sahrens 
1836fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1837fa9e4066Sahrens 	if (error != 0)
1838fa9e4066Sahrens 		return (error);
1839fa9e4066Sahrens 
1840fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1841478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1842fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1843fa94a07fSbrendan 	    &l2cache, &nl2cache);
1844fa94a07fSbrendan 
1845e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1846e7cbe64fSgw 	    &spares, &nspares);
1847e7cbe64fSgw 
1848b1b8ab34Slling 	/*
1849b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1850e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1851e7cbe64fSgw 	 *
1852e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1853e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1854e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1855e7cbe64fSgw 	 *
1856e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1857b1b8ab34Slling 	 */
1858b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
18591195e687SMark J Musante 		nvlist_free(config);
1860b1b8ab34Slling 		spa_close(spa, FTAG);
1861be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1862b1b8ab34Slling 	}
1863b1b8ab34Slling 
1864fa94a07fSbrendan 	if (error == 0) {
1865fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1866fa9e4066Sahrens 		nvlist_free(config);
1867fa9e4066Sahrens 	}
1868fa9e4066Sahrens 	spa_close(spa, FTAG);
1869fa9e4066Sahrens 	return (error);
1870fa9e4066Sahrens }
1871fa9e4066Sahrens 
18723f9d6ad7SLin Ling /*
18733f9d6ad7SLin Ling  * inputs:
18743f9d6ad7SLin Ling  * zc_name		name of the pool
18753f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
18763f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
18773f9d6ad7SLin Ling  */
1878fa9e4066Sahrens static int
1879fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1880fa9e4066Sahrens {
188199653d4eSeschrock 	spa_t *spa;
188299653d4eSeschrock 	int error;
188399653d4eSeschrock 
188499653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
188599653d4eSeschrock 	if (error != 0)
188699653d4eSeschrock 		return (error);
188799653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
188899653d4eSeschrock 	spa_close(spa, FTAG);
188999653d4eSeschrock 	return (error);
1890fa9e4066Sahrens }
1891fa9e4066Sahrens 
1892fa9e4066Sahrens static int
18933d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1894fa9e4066Sahrens {
1895fa9e4066Sahrens 	spa_t *spa;
1896fa9e4066Sahrens 	int error;
18973d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1898fa9e4066Sahrens 
189906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1900fa9e4066Sahrens 		return (error);
19013d7072f8Seschrock 	switch (zc->zc_cookie) {
19023d7072f8Seschrock 	case VDEV_STATE_ONLINE:
19033d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19043d7072f8Seschrock 		break;
1905fa9e4066Sahrens 
19063d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19073d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19083d7072f8Seschrock 		break;
1909fa9e4066Sahrens 
19103d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1911069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1912069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1913069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1914069f55e2SEric Schrock 
1915069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19163d7072f8Seschrock 		break;
19173d7072f8Seschrock 
19183d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1919069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1920069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1921069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1922069f55e2SEric Schrock 
1923069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19243d7072f8Seschrock 		break;
19253d7072f8Seschrock 
19263d7072f8Seschrock 	default:
1927be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
19283d7072f8Seschrock 	}
19293d7072f8Seschrock 	zc->zc_cookie = newstate;
1930fa9e4066Sahrens 	spa_close(spa, FTAG);
1931fa9e4066Sahrens 	return (error);
1932fa9e4066Sahrens }
1933fa9e4066Sahrens 
1934fa9e4066Sahrens static int
1935fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1936fa9e4066Sahrens {
1937fa9e4066Sahrens 	spa_t *spa;
1938fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1939fa9e4066Sahrens 	nvlist_t *config;
1940fa9e4066Sahrens 	int error;
1941fa9e4066Sahrens 
194206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1943fa9e4066Sahrens 		return (error);
1944fa9e4066Sahrens 
1945990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1946478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1947ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1948fa9e4066Sahrens 		nvlist_free(config);
1949fa9e4066Sahrens 	}
1950fa9e4066Sahrens 
1951fa9e4066Sahrens 	spa_close(spa, FTAG);
1952fa9e4066Sahrens 	return (error);
1953fa9e4066Sahrens }
1954fa9e4066Sahrens 
1955fa9e4066Sahrens static int
1956fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1957fa9e4066Sahrens {
1958fa9e4066Sahrens 	spa_t *spa;
1959fa9e4066Sahrens 	int error;
1960fa9e4066Sahrens 
196106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1962fa9e4066Sahrens 		return (error);
1963fa9e4066Sahrens 
19648ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	spa_close(spa, FTAG);
1967fa9e4066Sahrens 	return (error);
1968fa9e4066Sahrens }
1969fa9e4066Sahrens 
19701195e687SMark J Musante static int
19711195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
19721195e687SMark J Musante {
19731195e687SMark J Musante 	spa_t *spa;
19741195e687SMark J Musante 	nvlist_t *config, *props = NULL;
19751195e687SMark J Musante 	int error;
19761195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
19771195e687SMark J Musante 
19781195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
19791195e687SMark J Musante 		return (error);
19801195e687SMark J Musante 
19811195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
19821195e687SMark J Musante 	    zc->zc_iflags, &config)) {
19831195e687SMark J Musante 		spa_close(spa, FTAG);
19841195e687SMark J Musante 		return (error);
19851195e687SMark J Musante 	}
19861195e687SMark J Musante 
19871195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
19881195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19891195e687SMark J Musante 	    zc->zc_iflags, &props))) {
19901195e687SMark J Musante 		spa_close(spa, FTAG);
19911195e687SMark J Musante 		nvlist_free(config);
19921195e687SMark J Musante 		return (error);
19931195e687SMark J Musante 	}
19941195e687SMark J Musante 
19951195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
19961195e687SMark J Musante 
19971195e687SMark J Musante 	spa_close(spa, FTAG);
19981195e687SMark J Musante 
19991195e687SMark J Musante 	nvlist_free(config);
20001195e687SMark J Musante 	nvlist_free(props);
20011195e687SMark J Musante 
20021195e687SMark J Musante 	return (error);
20031195e687SMark J Musante }
20041195e687SMark J Musante 
2005c67d9675Seschrock static int
2006c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2007c67d9675Seschrock {
2008c67d9675Seschrock 	spa_t *spa;
2009e9dbad6fSeschrock 	char *path = zc->zc_value;
2010ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2011c67d9675Seschrock 	int error;
2012c67d9675Seschrock 
2013c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2014c67d9675Seschrock 	if (error != 0)
2015c67d9675Seschrock 		return (error);
2016c67d9675Seschrock 
2017c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2018c67d9675Seschrock 	spa_close(spa, FTAG);
2019c67d9675Seschrock 	return (error);
2020c67d9675Seschrock }
2021c67d9675Seschrock 
20226809eb4eSEric Schrock static int
20236809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20246809eb4eSEric Schrock {
20256809eb4eSEric Schrock 	spa_t *spa;
20266809eb4eSEric Schrock 	char *fru = zc->zc_value;
20276809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20286809eb4eSEric Schrock 	int error;
20296809eb4eSEric Schrock 
20306809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20316809eb4eSEric Schrock 	if (error != 0)
20326809eb4eSEric Schrock 		return (error);
20336809eb4eSEric Schrock 
20346809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20356809eb4eSEric Schrock 	spa_close(spa, FTAG);
20366809eb4eSEric Schrock 	return (error);
20376809eb4eSEric Schrock }
20386809eb4eSEric Schrock 
2039fa9e4066Sahrens static int
2040a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2041fa9e4066Sahrens {
2042a7f53a56SChris Kirby 	int error = 0;
20437f7322feSeschrock 	nvlist_t *nv;
2044fa9e4066Sahrens 
2045a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2046fa9e4066Sahrens 
20475ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
204892241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2049a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2050432f72fdSahrens 		/*
2051bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
2052432f72fdSahrens 		 * which we aren't supposed to do with a
2053745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2054432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2055503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2056432f72fdSahrens 		 */
205719b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
205819b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
205919b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
206019b94df9SMatthew Ahrens 			if (error == EIO)
206119b94df9SMatthew Ahrens 				return (error);
2062fb09f5aaSMadhav Suresh 			VERIFY0(error);
2063e7437265Sahrens 		}
2064e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
20657f7322feSeschrock 		nvlist_free(nv);
20667f7322feSeschrock 	}
2067fa9e4066Sahrens 
2068a7f53a56SChris Kirby 	return (error);
2069a7f53a56SChris Kirby }
2070a7f53a56SChris Kirby 
2071a7f53a56SChris Kirby /*
2072a7f53a56SChris Kirby  * inputs:
2073a7f53a56SChris Kirby  * zc_name		name of filesystem
2074a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2075a7f53a56SChris Kirby  *
2076a7f53a56SChris Kirby  * outputs:
2077a7f53a56SChris Kirby  * zc_objset_stats	stats
2078a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2079a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2080a7f53a56SChris Kirby  */
2081a7f53a56SChris Kirby static int
2082a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2083a7f53a56SChris Kirby {
20843b2aab18SMatthew Ahrens 	objset_t *os;
2085a7f53a56SChris Kirby 	int error;
2086a7f53a56SChris Kirby 
20873b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
20883b2aab18SMatthew Ahrens 	if (error == 0) {
20893b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
20903b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
20913b2aab18SMatthew Ahrens 	}
2092a7f53a56SChris Kirby 
2093fa9e4066Sahrens 	return (error);
2094fa9e4066Sahrens }
2095fa9e4066Sahrens 
209692241e0bSTom Erickson /*
209792241e0bSTom Erickson  * inputs:
209892241e0bSTom Erickson  * zc_name		name of filesystem
209992241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
210092241e0bSTom Erickson  *
210192241e0bSTom Erickson  * outputs:
210292241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
210392241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
210492241e0bSTom Erickson  *
210592241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
210692241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
210792241e0bSTom Erickson  * local property values.
210892241e0bSTom Erickson  */
210992241e0bSTom Erickson static int
211092241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
211192241e0bSTom Erickson {
21123b2aab18SMatthew Ahrens 	int error = 0;
211392241e0bSTom Erickson 	nvlist_t *nv;
211492241e0bSTom Erickson 
211592241e0bSTom Erickson 	/*
211692241e0bSTom Erickson 	 * Without this check, we would return local property values if the
211792241e0bSTom Erickson 	 * caller has not already received properties on or after
211892241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
211992241e0bSTom Erickson 	 */
21203b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2121be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
212292241e0bSTom Erickson 
212392241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21243b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
212592241e0bSTom Erickson 		error = put_nvlist(zc, nv);
212692241e0bSTom Erickson 		nvlist_free(nv);
212792241e0bSTom Erickson 	}
212892241e0bSTom Erickson 
212992241e0bSTom Erickson 	return (error);
213092241e0bSTom Erickson }
213192241e0bSTom Erickson 
2132de8267e0Stimh static int
2133de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2134de8267e0Stimh {
2135de8267e0Stimh 	uint64_t value;
2136de8267e0Stimh 	int error;
2137de8267e0Stimh 
2138de8267e0Stimh 	/*
2139de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2140de8267e0Stimh 	 * the default value (if there is one).
2141de8267e0Stimh 	 */
2142de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2143de8267e0Stimh 		return (error);
2144de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2145de8267e0Stimh 	return (0);
2146de8267e0Stimh }
2147de8267e0Stimh 
21483cb34c60Sahrens /*
21493cb34c60Sahrens  * inputs:
21503cb34c60Sahrens  * zc_name		name of filesystem
2151de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
21523cb34c60Sahrens  *
21533cb34c60Sahrens  * outputs:
2154de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2155de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
21563cb34c60Sahrens  */
2157bd00f61bSrm static int
2158de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2159bd00f61bSrm {
2160de8267e0Stimh 	objset_t *os;
2161de8267e0Stimh 	int err;
2162bd00f61bSrm 
2163503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2164503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2165de8267e0Stimh 		return (err);
2166bd00f61bSrm 
2167bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2168bd00f61bSrm 
2169bd00f61bSrm 	/*
2170de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2171745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2172745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2173bd00f61bSrm 	 */
2174de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2175de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2176de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2177de8267e0Stimh 		nvlist_t *nv;
2178de8267e0Stimh 
2179de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2180de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2181de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2182de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2183de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2184de8267e0Stimh 			err = put_nvlist(zc, nv);
2185de8267e0Stimh 		nvlist_free(nv);
2186de8267e0Stimh 	} else {
2187be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2188de8267e0Stimh 	}
2189503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2190de8267e0Stimh 	return (err);
2191bd00f61bSrm }
2192bd00f61bSrm 
219314843421SMatthew Ahrens static boolean_t
219414843421SMatthew Ahrens dataset_name_hidden(const char *name)
219514843421SMatthew Ahrens {
219614843421SMatthew Ahrens 	/*
219714843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
219814843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
219914843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
220014843421SMatthew Ahrens 	 */
220114843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
220214843421SMatthew Ahrens 		return (B_TRUE);
220314843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
220414843421SMatthew Ahrens 		return (B_TRUE);
220514843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
220614843421SMatthew Ahrens 		return (B_TRUE);
220714843421SMatthew Ahrens 	return (B_FALSE);
220814843421SMatthew Ahrens }
220914843421SMatthew Ahrens 
2210de8267e0Stimh /*
2211de8267e0Stimh  * inputs:
2212de8267e0Stimh  * zc_name		name of filesystem
2213de8267e0Stimh  * zc_cookie		zap cursor
2214de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2215de8267e0Stimh  *
2216de8267e0Stimh  * outputs:
2217de8267e0Stimh  * zc_name		name of next filesystem
221814843421SMatthew Ahrens  * zc_cookie		zap cursor
2219de8267e0Stimh  * zc_objset_stats	stats
2220de8267e0Stimh  * zc_nvlist_dst	property nvlist
2221de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2222de8267e0Stimh  */
2223fa9e4066Sahrens static int
2224fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2225fa9e4066Sahrens {
222687e5029aSahrens 	objset_t *os;
2227fa9e4066Sahrens 	int error;
2228fa9e4066Sahrens 	char *p;
2229620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2230fa9e4066Sahrens 
2231620252bcSChris Kirby top:
2232503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
223387e5029aSahrens 		if (error == ENOENT)
2234be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
223587e5029aSahrens 		return (error);
2236fa9e4066Sahrens 	}
2237fa9e4066Sahrens 
2238fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2239fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2240fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2241fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2242fa9e4066Sahrens 
2243fa9e4066Sahrens 	do {
224487e5029aSahrens 		error = dmu_dir_list_next(os,
224587e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
224687e5029aSahrens 		    NULL, &zc->zc_cookie);
2247fa9e4066Sahrens 		if (error == ENOENT)
2248be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
224919b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2250503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2251fa9e4066Sahrens 
2252681d9761SEric Taylor 	/*
2253681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2254681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2255681d9761SEric Taylor 	 */
2256620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
225787e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2258620252bcSChris Kirby 		if (error == ENOENT) {
2259620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2260620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2261620252bcSChris Kirby 			goto top;
2262620252bcSChris Kirby 		}
2263620252bcSChris Kirby 	}
2264fa9e4066Sahrens 	return (error);
2265fa9e4066Sahrens }
2266fa9e4066Sahrens 
22673cb34c60Sahrens /*
22683cb34c60Sahrens  * inputs:
22693cb34c60Sahrens  * zc_name		name of filesystem
22703cb34c60Sahrens  * zc_cookie		zap cursor
22713cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
2272*0d8fa8f8SMartin Matuska  * zc_simple		when set, only name is requested
22733cb34c60Sahrens  *
22743cb34c60Sahrens  * outputs:
22753cb34c60Sahrens  * zc_name		name of next snapshot
22763cb34c60Sahrens  * zc_objset_stats	stats
22773cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22783cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22793cb34c60Sahrens  */
2280fa9e4066Sahrens static int
2281fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2282fa9e4066Sahrens {
228387e5029aSahrens 	objset_t *os;
2284fa9e4066Sahrens 	int error;
2285fa9e4066Sahrens 
2286503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
22873b2aab18SMatthew Ahrens 	if (error != 0) {
2288745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
22893b2aab18SMatthew Ahrens 	}
2290fa9e4066Sahrens 
2291b81d61a6Slling 	/*
2292b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2293b81d61a6Slling 	 * so exit immediately.
2294b81d61a6Slling 	 */
2295b81d61a6Slling 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2296503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2297be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2298fa9e4066Sahrens 	}
2299fa9e4066Sahrens 
230087e5029aSahrens 	error = dmu_snapshot_list_next(os,
230187e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2302a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2303a7f53a56SChris Kirby 	    NULL);
2304a7f53a56SChris Kirby 
2305*0d8fa8f8SMartin Matuska 	if (error == 0 && !zc->zc_simple) {
2306a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2307a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2308a7f53a56SChris Kirby 
2309a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23103b2aab18SMatthew Ahrens 		if (error == 0) {
2311a7f53a56SChris Kirby 			objset_t *ossnap;
2312a7f53a56SChris Kirby 
2313a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2314a7f53a56SChris Kirby 			if (error == 0)
2315a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2316a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2317620252bcSChris Kirby 		}
2318620252bcSChris Kirby 	} else if (error == ENOENT) {
2319be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2320620252bcSChris Kirby 	}
2321fa9e4066Sahrens 
2322a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23233cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23243b2aab18SMatthew Ahrens 	if (error != 0)
23253cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2326fa9e4066Sahrens 	return (error);
2327fa9e4066Sahrens }
2328fa9e4066Sahrens 
232992241e0bSTom Erickson static int
233092241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2331fa9e4066Sahrens {
233292241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
233392241e0bSTom Erickson 	uint64_t *valary;
233492241e0bSTom Erickson 	unsigned int vallen;
233592241e0bSTom Erickson 	const char *domain;
2336eeb85002STim Haley 	char *dash;
233792241e0bSTom Erickson 	zfs_userquota_prop_t type;
233892241e0bSTom Erickson 	uint64_t rid;
233992241e0bSTom Erickson 	uint64_t quota;
234092241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
234192241e0bSTom Erickson 	int err;
234292241e0bSTom Erickson 
234392241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
234492241e0bSTom Erickson 		nvlist_t *attrs;
234592241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2346eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2347eeb85002STim Haley 		    &pair) != 0)
2348be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
234992241e0bSTom Erickson 	}
2350e9dbad6fSeschrock 
2351ecd6cf80Smarks 	/*
2352eeb85002STim Haley 	 * A correctly constructed propname is encoded as
235392241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2354ecd6cf80Smarks 	 */
2355eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2356eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2357eeb85002STim Haley 	    vallen != 3)
2358be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2359eeb85002STim Haley 
2360eeb85002STim Haley 	domain = dash + 1;
2361eeb85002STim Haley 	type = valary[0];
2362eeb85002STim Haley 	rid = valary[1];
2363eeb85002STim Haley 	quota = valary[2];
2364e9dbad6fSeschrock 
23651412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
236692241e0bSTom Erickson 	if (err == 0) {
236792241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
236892241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
236992241e0bSTom Erickson 	}
2370e9dbad6fSeschrock 
237192241e0bSTom Erickson 	return (err);
237292241e0bSTom Erickson }
237314843421SMatthew Ahrens 
237492241e0bSTom Erickson /*
237592241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
237692241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
237792241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
237892241e0bSTom Erickson  *
2379eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
238092241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
238192241e0bSTom Erickson  */
238292241e0bSTom Erickson static int
238392241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
238492241e0bSTom Erickson     nvpair_t *pair)
238592241e0bSTom Erickson {
238692241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
238792241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
238892241e0bSTom Erickson 	uint64_t intval;
2389b5152584SMatthew Ahrens 	int err = -1;
2390fa9e4066Sahrens 
239192241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
239292241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
239392241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
239492241e0bSTom Erickson 		return (-1);
239592241e0bSTom Erickson 	}
239614843421SMatthew Ahrens 
239792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
239892241e0bSTom Erickson 		nvlist_t *attrs;
239992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
240092241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
240192241e0bSTom Erickson 		    &pair) == 0);
240292241e0bSTom Erickson 	}
2403db870a07Sahrens 
240492241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
240592241e0bSTom Erickson 		return (-1);
2406b24ab676SJeff Bonwick 
240792241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
240840feaa91Sahrens 
240992241e0bSTom Erickson 	switch (prop) {
241092241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
241192241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
241292241e0bSTom Erickson 		break;
241392241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
24143b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
241592241e0bSTom Erickson 		break;
2416a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2417a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2418a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2419a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2420a2afb611SJerry Jelinek 			err = 0;
2421a2afb611SJerry Jelinek 		} else {
2422a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2423a2afb611SJerry Jelinek 		}
2424a2afb611SJerry Jelinek 		/*
2425a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2426a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2427a2afb611SJerry Jelinek 		 */
2428a2afb611SJerry Jelinek 		if (err == 0)
2429a2afb611SJerry Jelinek 			err = -1;
2430a2afb611SJerry Jelinek 		break;
243192241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
243292241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
243392241e0bSTom Erickson 		break;
243492241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
24353b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
243692241e0bSTom Erickson 		break;
243792241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2438c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
243992241e0bSTom Erickson 		break;
244092241e0bSTom Erickson 	case ZFS_PROP_VERSION:
244192241e0bSTom Erickson 	{
244292241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
24439e6eda55Smarks 
24441412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2445b24ab676SJeff Bonwick 			break;
2446b24ab676SJeff Bonwick 
244792241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
244892241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2449d0f3f37eSMark Shellenbaum 
245092241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2451b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2452b16da2e2SGeorge Wilson 
2453b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2454b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2455b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2456b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
245740feaa91Sahrens 		}
245892241e0bSTom Erickson 		break;
2459ecd6cf80Smarks 	}
246092241e0bSTom Erickson 	default:
246192241e0bSTom Erickson 		err = -1;
246292241e0bSTom Erickson 	}
2463e9dbad6fSeschrock 
246492241e0bSTom Erickson 	return (err);
246592241e0bSTom Erickson }
2466a9799022Sck 
246792241e0bSTom Erickson /*
246892241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
24694445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
24704445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
24714445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
24724445fffbSMatthew Ahrens  * corresponding error numbers.
247392241e0bSTom Erickson  *
24744445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
24754445fffbSMatthew Ahrens  * modified.
247692241e0bSTom Erickson  */
247792241e0bSTom Erickson int
247892241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
24794445fffbSMatthew Ahrens     nvlist_t *errlist)
248092241e0bSTom Erickson {
248192241e0bSTom Erickson 	nvpair_t *pair;
248292241e0bSTom Erickson 	nvpair_t *propval;
248302e383d1STom Erickson 	int rv = 0;
248492241e0bSTom Erickson 	uint64_t intval;
248592241e0bSTom Erickson 	char *strval;
24864445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
24874445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2488a9799022Sck 
248992241e0bSTom Erickson retry:
249092241e0bSTom Erickson 	pair = NULL;
249192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
249292241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
249392241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2494cfa69fd2STom Erickson 		int err = 0;
2495e9dbad6fSeschrock 
249692241e0bSTom Erickson 		/* decode the property value */
249792241e0bSTom Erickson 		propval = pair;
249892241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
249992241e0bSTom Erickson 			nvlist_t *attrs;
25004445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2501eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2502eeb85002STim Haley 			    &propval) != 0)
2503be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
250414843421SMatthew Ahrens 		}
2505e9dbad6fSeschrock 
250692241e0bSTom Erickson 		/* Validate value type */
2507eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
250892241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
250992241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2510be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
251192241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
251292241e0bSTom Erickson 				if (nvpair_type(propval) !=
251392241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2514be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
251519b94df9SMatthew Ahrens 			} else {
2516be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
25174201a95eSRic Aleshire 			}
2518eeb85002STim Haley 		} else if (err == 0) {
251992241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
252092241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2521be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
252292241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2523a2eea2e1Sahrens 				const char *unused;
2524a2eea2e1Sahrens 
25254445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2526e9dbad6fSeschrock 
2527e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
252891ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2529e9dbad6fSeschrock 					break;
253091ebeef5Sahrens 				case PROP_TYPE_STRING:
2531be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
253292241e0bSTom Erickson 					break;
253391ebeef5Sahrens 				case PROP_TYPE_INDEX:
2534acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
253592241e0bSTom Erickson 					    intval, &unused) != 0)
2536be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2537e9dbad6fSeschrock 					break;
2538e9dbad6fSeschrock 				default:
2539e7437265Sahrens 					cmn_err(CE_PANIC,
2540e7437265Sahrens 					    "unknown property type");
2541e9dbad6fSeschrock 				}
2542e9dbad6fSeschrock 			} else {
2543be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2544e9dbad6fSeschrock 			}
2545e9dbad6fSeschrock 		}
254692241e0bSTom Erickson 
254792241e0bSTom Erickson 		/* Validate permissions */
254892241e0bSTom Erickson 		if (err == 0)
254992241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
255092241e0bSTom Erickson 
255192241e0bSTom Erickson 		if (err == 0) {
255292241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
255392241e0bSTom Erickson 			if (err == -1) {
255492241e0bSTom Erickson 				/*
255592241e0bSTom Erickson 				 * For better performance we build up a list of
255692241e0bSTom Erickson 				 * properties to set in a single transaction.
255792241e0bSTom Erickson 				 */
255892241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
255992241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
256092241e0bSTom Erickson 				/*
256192241e0bSTom Erickson 				 * This may be a spurious error caused by
256292241e0bSTom Erickson 				 * receiving quota and reservation out of order.
256392241e0bSTom Erickson 				 * Try again in a second pass.
256492241e0bSTom Erickson 				 */
256592241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
256692241e0bSTom Erickson 			}
256792241e0bSTom Erickson 		}
256892241e0bSTom Erickson 
25694445fffbSMatthew Ahrens 		if (err != 0) {
25704445fffbSMatthew Ahrens 			if (errlist != NULL)
25714445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
25724445fffbSMatthew Ahrens 			rv = err;
25734445fffbSMatthew Ahrens 		}
2574e9dbad6fSeschrock 	}
2575e9dbad6fSeschrock 
257692241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
257792241e0bSTom Erickson 		nvl = retrynvl;
257892241e0bSTom Erickson 		goto retry;
257992241e0bSTom Erickson 	}
258092241e0bSTom Erickson 
258192241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
258292241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
258392241e0bSTom Erickson 		/*
258492241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
258592241e0bSTom Erickson 		 * can, so try setting them individually.
258692241e0bSTom Erickson 		 */
258792241e0bSTom Erickson 		pair = NULL;
258892241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
258992241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2590cfa69fd2STom Erickson 			int err = 0;
259192241e0bSTom Erickson 
259292241e0bSTom Erickson 			propval = pair;
259392241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
259492241e0bSTom Erickson 				nvlist_t *attrs;
25954445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
25964445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
25974445fffbSMatthew Ahrens 				    ZPROP_VALUE);
259892241e0bSTom Erickson 			}
259992241e0bSTom Erickson 
260092241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26014445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26023b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26033b2aab18SMatthew Ahrens 				    source, strval);
260492241e0bSTom Erickson 			} else {
26054445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26063b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26073b2aab18SMatthew Ahrens 				    intval);
260892241e0bSTom Erickson 			}
260992241e0bSTom Erickson 
261092241e0bSTom Erickson 			if (err != 0) {
26114445fffbSMatthew Ahrens 				if (errlist != NULL) {
26124445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26134445fffbSMatthew Ahrens 					    err);
26144445fffbSMatthew Ahrens 				}
26154445fffbSMatthew Ahrens 				rv = err;
261692241e0bSTom Erickson 			}
261792241e0bSTom Erickson 		}
26185c0b6a79SRich Morris 	}
26195c0b6a79SRich Morris 	nvlist_free(genericnvl);
262092241e0bSTom Erickson 	nvlist_free(retrynvl);
262192241e0bSTom Erickson 
262292241e0bSTom Erickson 	return (rv);
2623fa9e4066Sahrens }
2624fa9e4066Sahrens 
2625ea2f5b9eSMatthew Ahrens /*
2626ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2627ea2f5b9eSMatthew Ahrens  */
2628ea2f5b9eSMatthew Ahrens static int
26294445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2630ea2f5b9eSMatthew Ahrens {
263192241e0bSTom Erickson 	nvpair_t *pair = NULL;
2632ea2f5b9eSMatthew Ahrens 	int error = 0;
2633ea2f5b9eSMatthew Ahrens 
263492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
263592241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2636ea2f5b9eSMatthew Ahrens 
2637ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
263892241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2639be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2640ea2f5b9eSMatthew Ahrens 
2641ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2642ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2643ea2f5b9eSMatthew Ahrens 			return (error);
2644ea2f5b9eSMatthew Ahrens 
2645ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2646be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2647ea2f5b9eSMatthew Ahrens 
264878f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2649ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2650ea2f5b9eSMatthew Ahrens 	}
2651ea2f5b9eSMatthew Ahrens 	return (0);
2652ea2f5b9eSMatthew Ahrens }
2653ea2f5b9eSMatthew Ahrens 
265492241e0bSTom Erickson static void
265592241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
265692241e0bSTom Erickson {
265792241e0bSTom Erickson 	nvpair_t *pair;
265892241e0bSTom Erickson 
265992241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
266092241e0bSTom Erickson 
266192241e0bSTom Erickson 	pair = NULL;
266292241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
266392241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
266492241e0bSTom Erickson 			continue;
266592241e0bSTom Erickson 
266692241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
266792241e0bSTom Erickson 	}
266892241e0bSTom Erickson }
266992241e0bSTom Erickson 
267092241e0bSTom Erickson static int
26713b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
267292241e0bSTom Erickson     nvlist_t *skipped)
267392241e0bSTom Erickson {
267492241e0bSTom Erickson 	int err = 0;
267592241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
267692241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
267792241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
267892241e0bSTom Erickson 		/*
267992241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
268092241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
268192241e0bSTom Erickson 		 */
268292241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
26833b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
26843b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
268592241e0bSTom Erickson 	}
268692241e0bSTom Erickson 	nvlist_free(cleared_props);
268792241e0bSTom Erickson 	return (err);
268892241e0bSTom Erickson }
268992241e0bSTom Erickson 
26903cb34c60Sahrens /*
26913cb34c60Sahrens  * inputs:
26923cb34c60Sahrens  * zc_name		name of filesystem
26935c0b6a79SRich Morris  * zc_value		name of property to set
26943cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
269592241e0bSTom Erickson  * zc_cookie		received properties flag
26963cb34c60Sahrens  *
269792241e0bSTom Erickson  * outputs:
269892241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
26993cb34c60Sahrens  */
2700fa9e4066Sahrens static int
2701e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2702fa9e4066Sahrens {
2703e9dbad6fSeschrock 	nvlist_t *nvl;
270492241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
270592241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
270692241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27074445fffbSMatthew Ahrens 	nvlist_t *errors;
2708e9dbad6fSeschrock 	int error;
2709e9dbad6fSeschrock 
2710990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2711478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2712e9dbad6fSeschrock 		return (error);
2713e9dbad6fSeschrock 
271492241e0bSTom Erickson 	if (received) {
2715bb0ade09Sahrens 		nvlist_t *origprops;
2716bb0ade09Sahrens 
27173b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27183b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27193b2aab18SMatthew Ahrens 			    origprops, nvl);
27203b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2721bb0ade09Sahrens 		}
27223b2aab18SMatthew Ahrens 
27233b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2724bb0ade09Sahrens 	}
2725bb0ade09Sahrens 
27264445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27273b2aab18SMatthew Ahrens 	if (error == 0)
27283b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
272992241e0bSTom Erickson 
273092241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
273192241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
273292241e0bSTom Erickson 	}
2733ecd6cf80Smarks 
273492241e0bSTom Erickson 	nvlist_free(errors);
2735e9dbad6fSeschrock 	nvlist_free(nvl);
2736e9dbad6fSeschrock 	return (error);
2737fa9e4066Sahrens }
2738fa9e4066Sahrens 
27393cb34c60Sahrens /*
27403cb34c60Sahrens  * inputs:
27413cb34c60Sahrens  * zc_name		name of filesystem
27423cb34c60Sahrens  * zc_value		name of property to inherit
274392241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
27443cb34c60Sahrens  *
27453cb34c60Sahrens  * outputs:		none
27463cb34c60Sahrens  */
2747e45ce728Sahrens static int
2748e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2749e45ce728Sahrens {
275092241e0bSTom Erickson 	const char *propname = zc->zc_value;
275192241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
275292241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
275392241e0bSTom Erickson 	zprop_source_t source = (received
275492241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
275592241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
275692241e0bSTom Erickson 
275792241e0bSTom Erickson 	if (received) {
275892241e0bSTom Erickson 		nvlist_t *dummy;
275992241e0bSTom Erickson 		nvpair_t *pair;
276092241e0bSTom Erickson 		zprop_type_t type;
276192241e0bSTom Erickson 		int err;
276292241e0bSTom Erickson 
276392241e0bSTom Erickson 		/*
276492241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
276592241e0bSTom Erickson 		 * nvpair with type info.
276692241e0bSTom Erickson 		 */
276792241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
276892241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2769be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
277092241e0bSTom Erickson 
277192241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2772a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2773a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2774be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
277592241e0bSTom Erickson 		} else {
277692241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
277792241e0bSTom Erickson 		}
277892241e0bSTom Erickson 
277992241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
278092241e0bSTom Erickson 
278192241e0bSTom Erickson 		switch (type) {
278292241e0bSTom Erickson 		case PROP_TYPE_STRING:
278392241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
278492241e0bSTom Erickson 			break;
278592241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
278692241e0bSTom Erickson 		case PROP_TYPE_INDEX:
278792241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
278892241e0bSTom Erickson 			break;
278992241e0bSTom Erickson 		default:
279092241e0bSTom Erickson 			nvlist_free(dummy);
2791be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
279292241e0bSTom Erickson 		}
279392241e0bSTom Erickson 
279492241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
279592241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
279692241e0bSTom Erickson 		nvlist_free(dummy);
279792241e0bSTom Erickson 		if (err != -1)
279892241e0bSTom Erickson 			return (err); /* special property already handled */
279992241e0bSTom Erickson 	} else {
280092241e0bSTom Erickson 		/*
280192241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
280292241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
280392241e0bSTom Erickson 		 * and reservation to the received or default values even though
280492241e0bSTom Erickson 		 * they are not considered inheritable.
280592241e0bSTom Erickson 		 */
280692241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2807be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
280892241e0bSTom Erickson 	}
280992241e0bSTom Erickson 
28104445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28113b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2812e45ce728Sahrens }
2813e45ce728Sahrens 
2814b1b8ab34Slling static int
281511a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2816b1b8ab34Slling {
2817990b4856Slling 	nvlist_t *props;
2818b1b8ab34Slling 	spa_t *spa;
2819990b4856Slling 	int error;
282092241e0bSTom Erickson 	nvpair_t *pair;
2821b1b8ab34Slling 
282292241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
282392241e0bSTom Erickson 	    zc->zc_iflags, &props))
2824b1b8ab34Slling 		return (error);
2825b1b8ab34Slling 
2826379c004dSEric Schrock 	/*
2827379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2828379c004dSEric Schrock 	 * to handle the faulted case.
2829379c004dSEric Schrock 	 */
283092241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
283192241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2832379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
283392241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2834379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2835379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2836379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2837379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2838379c004dSEric Schrock 		}
2839379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2840b693757aSEric Schrock 		if (spa != NULL) {
2841b693757aSEric Schrock 			nvlist_free(props);
2842379c004dSEric Schrock 			return (0);
2843b693757aSEric Schrock 		}
2844379c004dSEric Schrock 	}
2845379c004dSEric Schrock 
2846b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2847990b4856Slling 		nvlist_free(props);
2848b1b8ab34Slling 		return (error);
2849b1b8ab34Slling 	}
2850b1b8ab34Slling 
2851990b4856Slling 	error = spa_prop_set(spa, props);
2852b1b8ab34Slling 
2853990b4856Slling 	nvlist_free(props);
2854b1b8ab34Slling 	spa_close(spa, FTAG);
2855b1b8ab34Slling 
2856b1b8ab34Slling 	return (error);
2857b1b8ab34Slling }
2858b1b8ab34Slling 
2859b1b8ab34Slling static int
286011a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2861b1b8ab34Slling {
2862b1b8ab34Slling 	spa_t *spa;
2863b1b8ab34Slling 	int error;
2864b1b8ab34Slling 	nvlist_t *nvp = NULL;
2865b1b8ab34Slling 
2866379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2867379c004dSEric Schrock 		/*
2868379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2869379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2870379c004dSEric Schrock 		 * anyway.
2871379c004dSEric Schrock 		 */
2872379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2873379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2874379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2875379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2876379c004dSEric Schrock 	} else {
2877379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2878379c004dSEric Schrock 		spa_close(spa, FTAG);
2879379c004dSEric Schrock 	}
2880b1b8ab34Slling 
2881b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2882b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2883b1b8ab34Slling 	else
2884be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2885b1b8ab34Slling 
2886379c004dSEric Schrock 	nvlist_free(nvp);
2887b1b8ab34Slling 	return (error);
2888b1b8ab34Slling }
2889b1b8ab34Slling 
28903cb34c60Sahrens /*
28913cb34c60Sahrens  * inputs:
28923cb34c60Sahrens  * zc_name		name of filesystem
28933cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
28943cb34c60Sahrens  * zc_perm_action	allow/unallow flag
28953cb34c60Sahrens  *
28963cb34c60Sahrens  * outputs:		none
28973cb34c60Sahrens  */
2898ecd6cf80Smarks static int
2899ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2900ecd6cf80Smarks {
2901ecd6cf80Smarks 	int error;
2902ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2903ecd6cf80Smarks 
2904990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2905478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2906ecd6cf80Smarks 		return (error);
2907ecd6cf80Smarks 
2908ecd6cf80Smarks 	/*
2909ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2910ecd6cf80Smarks 	 */
2911ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2912ecd6cf80Smarks 		nvlist_free(fsaclnv);
2913be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2914ecd6cf80Smarks 	}
2915ecd6cf80Smarks 
2916ecd6cf80Smarks 	/*
2917ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2918ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2919ecd6cf80Smarks 	 * the nvlist(s)
2920ecd6cf80Smarks 	 */
2921ecd6cf80Smarks 
292291ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29233b2aab18SMatthew Ahrens 	if (error != 0) {
292491ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
292591ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
292691ebeef5Sahrens 			    fsaclnv, CRED());
292791ebeef5Sahrens 		} else {
292891ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
292991ebeef5Sahrens 			    fsaclnv, CRED());
293091ebeef5Sahrens 		}
2931ecd6cf80Smarks 	}
2932ecd6cf80Smarks 
2933ecd6cf80Smarks 	if (error == 0)
2934ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2935ecd6cf80Smarks 
2936ecd6cf80Smarks 	nvlist_free(fsaclnv);
2937ecd6cf80Smarks 	return (error);
2938ecd6cf80Smarks }
2939ecd6cf80Smarks 
29403cb34c60Sahrens /*
29413cb34c60Sahrens  * inputs:
29423cb34c60Sahrens  * zc_name		name of filesystem
29433cb34c60Sahrens  *
29443cb34c60Sahrens  * outputs:
29453cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29463cb34c60Sahrens  */
2947ecd6cf80Smarks static int
2948ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2949ecd6cf80Smarks {
2950ecd6cf80Smarks 	nvlist_t *nvp;
2951ecd6cf80Smarks 	int error;
2952ecd6cf80Smarks 
2953ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2954ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2955ecd6cf80Smarks 		nvlist_free(nvp);
2956ecd6cf80Smarks 	}
2957ecd6cf80Smarks 
2958ecd6cf80Smarks 	return (error);
2959ecd6cf80Smarks }
2960ecd6cf80Smarks 
2961fa9e4066Sahrens /*
2962fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2963fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2964fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2965fa9e4066Sahrens  */
2966fa9e4066Sahrens static vfs_t *
2967fa9e4066Sahrens zfs_get_vfs(const char *resource)
2968fa9e4066Sahrens {
2969fa9e4066Sahrens 	struct vfs *vfsp;
2970fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2971fa9e4066Sahrens 
2972fa9e4066Sahrens 	vfs_list_read_lock();
2973fa9e4066Sahrens 	vfsp = rootvfs;
2974fa9e4066Sahrens 	do {
2975fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2976fa9e4066Sahrens 			VFS_HOLD(vfsp);
2977fa9e4066Sahrens 			vfs_found = vfsp;
2978fa9e4066Sahrens 			break;
2979fa9e4066Sahrens 		}
2980fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2981fa9e4066Sahrens 	} while (vfsp != rootvfs);
2982fa9e4066Sahrens 	vfs_list_unlock();
2983fa9e4066Sahrens 	return (vfs_found);
2984fa9e4066Sahrens }
2985fa9e4066Sahrens 
2986ecd6cf80Smarks /* ARGSUSED */
2987fa9e4066Sahrens static void
2988ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2989fa9e4066Sahrens {
2990da6c28aaSamw 	zfs_creat_t *zct = arg;
2991da6c28aaSamw 
2992de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2993da6c28aaSamw }
2994da6c28aaSamw 
2995de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2996da6c28aaSamw 
2997da6c28aaSamw /*
2998de8267e0Stimh  * inputs:
29990a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3000f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3001f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3002f7170741SWill Andrews  * createprops		list of properties requested by creator
3003de8267e0Stimh  *
3004de8267e0Stimh  * outputs:
3005de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30060a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3007da6c28aaSamw  *
3008de8267e0Stimh  * Determine the settings for utf8only, normalization and
3009de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3010de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3011de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3012de8267e0Stimh  * request settings for these properties, even if the requested
3013de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3014de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3015de8267e0Stimh  * processing.
3016da6c28aaSamw  */
3017da6c28aaSamw static int
301814843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30190a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30200a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3021da6c28aaSamw {
3022de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3023de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3024de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3025da6c28aaSamw 
3026de8267e0Stimh 	ASSERT(zplprops != NULL);
3027da6c28aaSamw 
3028de8267e0Stimh 	/*
3029de8267e0Stimh 	 * Pull out creator prop choices, if any.
3030de8267e0Stimh 	 */
3031de8267e0Stimh 	if (createprops) {
30320a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
30330a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3034de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3035de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3036de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3037de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3038de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3039de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3040de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3041de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3042de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3043de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3044de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3045de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3046de8267e0Stimh 	}
3047da6c28aaSamw 
3048c2a93d44Stimh 	/*
30490a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30500a48a24eStimh 	 * or pool is version is too "young" to support normalization
30510a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30520a48a24eStimh 	 * error out.
3053c2a93d44Stimh 	 */
30540a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30550a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30560a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30570a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3058de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30590a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3060be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3061c2a93d44Stimh 
3062de8267e0Stimh 	/*
3063de8267e0Stimh 	 * Put the version in the zplprops
3064de8267e0Stimh 	 */
3065de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3066de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3067da6c28aaSamw 
3068de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3069de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3070de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3071de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3072da6c28aaSamw 
3073c2a93d44Stimh 	/*
3074de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3075c2a93d44Stimh 	 */
3076de8267e0Stimh 	if (norm)
3077de8267e0Stimh 		u8 = 1;
3078de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3079de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3080de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3081de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3082de8267e0Stimh 
3083de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3084de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3085de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3086de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3087c2a93d44Stimh 
3088ab04eb8eStimh 	if (is_ci)
3089ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3090ab04eb8eStimh 
3091da6c28aaSamw 	return (0);
3092fa9e4066Sahrens }
3093fa9e4066Sahrens 
30940a48a24eStimh static int
30950a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
30960a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
30970a48a24eStimh {
30980a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
30990a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31000a48a24eStimh 	objset_t *os = NULL;
31010a48a24eStimh 	char parentname[MAXNAMELEN];
31020a48a24eStimh 	char *cp;
31030a586ceaSMark Shellenbaum 	spa_t *spa;
31040a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31050a48a24eStimh 	int error;
31060a48a24eStimh 
31070a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31080a48a24eStimh 	cp = strrchr(parentname, '/');
31090a48a24eStimh 	ASSERT(cp != NULL);
31100a48a24eStimh 	cp[0] = '\0';
31110a48a24eStimh 
31120a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31130a586ceaSMark Shellenbaum 		return (error);
31140a586ceaSMark Shellenbaum 
31150a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31160a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31170a586ceaSMark Shellenbaum 
31180a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31190a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31200a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31210a48a24eStimh 
31220a48a24eStimh 	/*
31230a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31240a48a24eStimh 	 */
3125503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31260a48a24eStimh 		return (error);
31270a48a24eStimh 
31280a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31290a48a24eStimh 	    zplprops, is_ci);
3130503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31310a48a24eStimh 	return (error);
31320a48a24eStimh }
31330a48a24eStimh 
31340a48a24eStimh static int
31350a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31360a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31370a48a24eStimh {
31380a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
31390a586ceaSMark Shellenbaum 	boolean_t sa_ok;
31400a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31410a48a24eStimh 	int error;
31420a48a24eStimh 
31430a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31440a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31450a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31460a48a24eStimh 
31470a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31480a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31490a48a24eStimh 	return (error);
31500a48a24eStimh }
31510a48a24eStimh 
31523cb34c60Sahrens /*
31534445fffbSMatthew Ahrens  * innvl: {
31544445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31554445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31564445fffbSMatthew Ahrens  * }
31573cb34c60Sahrens  *
31584445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31593cb34c60Sahrens  */
3160fa9e4066Sahrens static int
31614445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3162fa9e4066Sahrens {
3163fa9e4066Sahrens 	int error = 0;
31644445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3165ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3166ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31674445fffbSMatthew Ahrens 	int32_t type32;
31684445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31694445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3170fa9e4066Sahrens 
31714445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3172be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31734445fffbSMatthew Ahrens 	type = type32;
31744445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3175fa9e4066Sahrens 
31764445fffbSMatthew Ahrens 	switch (type) {
3177fa9e4066Sahrens 	case DMU_OST_ZFS:
3178fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3179fa9e4066Sahrens 		break;
3180fa9e4066Sahrens 
3181fa9e4066Sahrens 	case DMU_OST_ZVOL:
3182fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3183fa9e4066Sahrens 		break;
3184fa9e4066Sahrens 
3185fa9e4066Sahrens 	default:
31861d452cf5Sahrens 		cbfunc = NULL;
3187e7cbe64fSgw 		break;
3188fa9e4066Sahrens 	}
31894445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
31904445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3191be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3192fa9e4066Sahrens 
3193da6c28aaSamw 	zct.zct_props = nvprops;
3194da6c28aaSamw 
31954445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3196be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31974445fffbSMatthew Ahrens 
31984445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
31994445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
32004445fffbSMatthew Ahrens 
32014445fffbSMatthew Ahrens 		if (nvprops == NULL)
3202be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32034445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32044445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3205be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3206fa9e4066Sahrens 
32074445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
32084445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32094445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3210be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32114445fffbSMatthew Ahrens 
32124445fffbSMatthew Ahrens 		if (error != 0)
32134445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
32144445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
32154445fffbSMatthew Ahrens 
32164445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
32174445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
32184445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
32194445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3220fa9e4066Sahrens 			return (error);
32214445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
32224445fffbSMatthew Ahrens 		int error;
3223ab04eb8eStimh 
32244445fffbSMatthew Ahrens 		/*
32254445fffbSMatthew Ahrens 		 * We have to have normalization and
32264445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
32274445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
32284445fffbSMatthew Ahrens 		 * now.
32294445fffbSMatthew Ahrens 		 */
32304445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
32314445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32324445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32334445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
32344445fffbSMatthew Ahrens 		if (error != 0) {
32354445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3236da6c28aaSamw 			return (error);
3237da6c28aaSamw 		}
32384445fffbSMatthew Ahrens 	}
3239ab04eb8eStimh 
32404445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
32414445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
32424445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
32435c5460e9Seschrock 
32444445fffbSMatthew Ahrens 	/*
32454445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32464445fffbSMatthew Ahrens 	 */
32474445fffbSMatthew Ahrens 	if (error == 0) {
32484445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32494445fffbSMatthew Ahrens 		    nvprops, outnvl);
32504445fffbSMatthew Ahrens 		if (error != 0)
32513b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32524445fffbSMatthew Ahrens 	}
32534445fffbSMatthew Ahrens 	return (error);
32544445fffbSMatthew Ahrens }
3255e9dbad6fSeschrock 
32564445fffbSMatthew Ahrens /*
32574445fffbSMatthew Ahrens  * innvl: {
32584445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32594445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32604445fffbSMatthew Ahrens  * }
32614445fffbSMatthew Ahrens  *
32624445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32634445fffbSMatthew Ahrens  */
32644445fffbSMatthew Ahrens static int
32654445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32664445fffbSMatthew Ahrens {
32674445fffbSMatthew Ahrens 	int error = 0;
32684445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32694445fffbSMatthew Ahrens 	char *origin_name;
3270e9dbad6fSeschrock 
32714445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3272be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32734445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3274e9dbad6fSeschrock 
32754445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32764445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3277be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3278e9dbad6fSeschrock 
32794445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3280be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32813b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
32823b2aab18SMatthew Ahrens 	if (error != 0)
32834445fffbSMatthew Ahrens 		return (error);
3284e9dbad6fSeschrock 
3285e9dbad6fSeschrock 	/*
3286e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3287e9dbad6fSeschrock 	 */
3288e9dbad6fSeschrock 	if (error == 0) {
32894445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32904445fffbSMatthew Ahrens 		    nvprops, outnvl);
329192241e0bSTom Erickson 		if (error != 0)
32923b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3293e9dbad6fSeschrock 	}
3294fa9e4066Sahrens 	return (error);
3295fa9e4066Sahrens }
3296fa9e4066Sahrens 
32973cb34c60Sahrens /*
32984445fffbSMatthew Ahrens  * innvl: {
32994445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33004445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33014445fffbSMatthew Ahrens  * }
33024445fffbSMatthew Ahrens  *
33034445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33043cb34c60Sahrens  */
3305fa9e4066Sahrens static int
33064445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3307fa9e4066Sahrens {
33084445fffbSMatthew Ahrens 	nvlist_t *snaps;
33094445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33104445fffbSMatthew Ahrens 	int error, poollen;
33114445fffbSMatthew Ahrens 	nvpair_t *pair;
3312bb0ade09Sahrens 
33134445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33144445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33154445fffbSMatthew Ahrens 		return (error);
33164445fffbSMatthew Ahrens 
33174445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33184445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3319be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33204445fffbSMatthew Ahrens 
33214445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3322be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33234445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33244445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33254445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33264445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33274445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3328bb0ade09Sahrens 
33294445fffbSMatthew Ahrens 		/*
33304445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33314445fffbSMatthew Ahrens 		 * contain only valid characters.
33324445fffbSMatthew Ahrens 		 */
333378f17100SMatthew Ahrens 		if (cp == NULL ||
333478f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3335be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3336bb0ade09Sahrens 
33374445fffbSMatthew Ahrens 		/*
33384445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
33394445fffbSMatthew Ahrens 		 */
33404445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
33414445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3342be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
33434445fffbSMatthew Ahrens 
33444445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
33454445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33464445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33474445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33484445fffbSMatthew Ahrens 			    == 0) {
3349be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33504445fffbSMatthew Ahrens 			}
33514445fffbSMatthew Ahrens 		}
33524445fffbSMatthew Ahrens 	}
3353bb0ade09Sahrens 
33543b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33554445fffbSMatthew Ahrens 	return (error);
33564445fffbSMatthew Ahrens }
33574445fffbSMatthew Ahrens 
33584445fffbSMatthew Ahrens /*
33594445fffbSMatthew Ahrens  * innvl: "message" -> string
33604445fffbSMatthew Ahrens  */
33614445fffbSMatthew Ahrens /* ARGSUSED */
33624445fffbSMatthew Ahrens static int
33634445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
33644445fffbSMatthew Ahrens {
33654445fffbSMatthew Ahrens 	char *message;
33664445fffbSMatthew Ahrens 	spa_t *spa;
33674445fffbSMatthew Ahrens 	int error;
33684445fffbSMatthew Ahrens 	char *poolname;
33694445fffbSMatthew Ahrens 
33704445fffbSMatthew Ahrens 	/*
33714445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
33724445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
33734445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
33744445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
33754445fffbSMatthew Ahrens 	 * we clear the TSD here.
33764445fffbSMatthew Ahrens 	 */
33774445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
33784445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
33794445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
33804445fffbSMatthew Ahrens 	strfree(poolname);
33814445fffbSMatthew Ahrens 	if (error != 0)
33824445fffbSMatthew Ahrens 		return (error);
33834445fffbSMatthew Ahrens 
33844445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
33854445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3386be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3387bb0ade09Sahrens 	}
3388ea2f5b9eSMatthew Ahrens 
33894445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
33904445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3391be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33924445fffbSMatthew Ahrens 	}
3393ea2f5b9eSMatthew Ahrens 
33944445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
33954445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3396bb0ade09Sahrens 	return (error);
33971d452cf5Sahrens }
3398fa9e4066Sahrens 
33993b2aab18SMatthew Ahrens /*
34003b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34013b2aab18SMatthew Ahrens  * unmount may need to write out data.
34023b2aab18SMatthew Ahrens  *
34033b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34043b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3405fc7a6e3fSWill Andrews  *
3406fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3407fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34083b2aab18SMatthew Ahrens  */
3409fc7a6e3fSWill Andrews int
34103b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34111d452cf5Sahrens {
34124445fffbSMatthew Ahrens 	vfs_t *vfsp;
34133b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3414fc7a6e3fSWill Andrews 	int err;
34151d452cf5Sahrens 
34163b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3417fc7a6e3fSWill Andrews 		return (0);
34181d452cf5Sahrens 
34193b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
34204445fffbSMatthew Ahrens 	if (vfsp == NULL)
3421fc7a6e3fSWill Andrews 		return (0);
34223b2aab18SMatthew Ahrens 
34233b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
34243b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34251d452cf5Sahrens 
3426fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
34274445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3428fc7a6e3fSWill Andrews 	if (err != 0)
3429fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
34304445fffbSMatthew Ahrens 
34314445fffbSMatthew Ahrens 	/*
34324445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34334445fffbSMatthew Ahrens 	 */
34343b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3435fc7a6e3fSWill Andrews 	return (0);
34363b2aab18SMatthew Ahrens }
34373b2aab18SMatthew Ahrens 
34383b2aab18SMatthew Ahrens /* ARGSUSED */
34393b2aab18SMatthew Ahrens static int
34403b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
34413b2aab18SMatthew Ahrens {
3442fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
34433b2aab18SMatthew Ahrens }
34443b2aab18SMatthew Ahrens 
34453b2aab18SMatthew Ahrens /*
34463b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34473b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34483b2aab18SMatthew Ahrens  * if necessary.
34493b2aab18SMatthew Ahrens  */
34503b2aab18SMatthew Ahrens void
34513b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34523b2aab18SMatthew Ahrens {
34533b2aab18SMatthew Ahrens 	int error;
34543b2aab18SMatthew Ahrens 	objset_t *os;
34553b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34563b2aab18SMatthew Ahrens 
34573b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
34583b2aab18SMatthew Ahrens 	if (error != 0)
34593b2aab18SMatthew Ahrens 		return;
34603b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
34613b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
34623b2aab18SMatthew Ahrens 		char originname[MAXNAMELEN];
34633b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
34643b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3465fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
34663b2aab18SMatthew Ahrens 	} else {
34673b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34683b2aab18SMatthew Ahrens 	}
34691d452cf5Sahrens }
34701d452cf5Sahrens 
34713cb34c60Sahrens /*
34724445fffbSMatthew Ahrens  * innvl: {
34734445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34744445fffbSMatthew Ahrens  *     (optional boolean) "defer"
34754445fffbSMatthew Ahrens  * }
34764445fffbSMatthew Ahrens  *
34774445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34783cb34c60Sahrens  *
34793cb34c60Sahrens  */
348078f17100SMatthew Ahrens /* ARGSUSED */
34811d452cf5Sahrens static int
34824445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34831d452cf5Sahrens {
34844445fffbSMatthew Ahrens 	nvlist_t *snaps;
348519b94df9SMatthew Ahrens 	nvpair_t *pair;
34864445fffbSMatthew Ahrens 	boolean_t defer;
34871d452cf5Sahrens 
34884445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3489be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34904445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
349119b94df9SMatthew Ahrens 
34924445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
34934445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
349478f17100SMatthew Ahrens 		(void) zfs_unmount_snap(nvpair_name(pair));
349578f17100SMatthew Ahrens 	}
349678f17100SMatthew Ahrens 
349778f17100SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
349878f17100SMatthew Ahrens }
349978f17100SMatthew Ahrens 
350078f17100SMatthew Ahrens /*
350178f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
350278f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
350378f17100SMatthew Ahrens  *
350478f17100SMatthew Ahrens  * innvl: {
350578f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
350678f17100SMatthew Ahrens  * }
350778f17100SMatthew Ahrens  *
350878f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
350978f17100SMatthew Ahrens  *
351078f17100SMatthew Ahrens  */
351178f17100SMatthew Ahrens /* ARGSUSED */
351278f17100SMatthew Ahrens static int
351378f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
351478f17100SMatthew Ahrens {
351578f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
351678f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
351778f17100SMatthew Ahrens 		char *snap_name;
351878f17100SMatthew Ahrens 
351978f17100SMatthew Ahrens 		/*
352078f17100SMatthew Ahrens 		 * Verify the snapshot argument.
352178f17100SMatthew Ahrens 		 */
352278f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
352378f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
352478f17100SMatthew Ahrens 
352578f17100SMatthew Ahrens 
352678f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
352778f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
352878f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
352978f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
353078f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
353178f17100SMatthew Ahrens 		}
353278f17100SMatthew Ahrens 	}
353378f17100SMatthew Ahrens 
353478f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
353578f17100SMatthew Ahrens }
353678f17100SMatthew Ahrens 
353778f17100SMatthew Ahrens /*
353878f17100SMatthew Ahrens  * innvl: {
353978f17100SMatthew Ahrens  *     property 1, property 2, ...
354078f17100SMatthew Ahrens  * }
354178f17100SMatthew Ahrens  *
354278f17100SMatthew Ahrens  * outnvl: {
354378f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
354478f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
354578f17100SMatthew Ahrens  * }
354678f17100SMatthew Ahrens  *
354778f17100SMatthew Ahrens  */
354878f17100SMatthew Ahrens static int
354978f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
355078f17100SMatthew Ahrens {
355178f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
355278f17100SMatthew Ahrens }
355378f17100SMatthew Ahrens 
355478f17100SMatthew Ahrens /*
355578f17100SMatthew Ahrens  * innvl: {
355678f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
355778f17100SMatthew Ahrens  * }
355878f17100SMatthew Ahrens  *
355978f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
356078f17100SMatthew Ahrens  *
356178f17100SMatthew Ahrens  */
356278f17100SMatthew Ahrens static int
356378f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
356478f17100SMatthew Ahrens     nvlist_t *outnvl)
356578f17100SMatthew Ahrens {
356678f17100SMatthew Ahrens 	int error, poollen;
356778f17100SMatthew Ahrens 
356878f17100SMatthew Ahrens 	poollen = strlen(poolname);
356978f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
357078f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
357119b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
357278f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
35734445fffbSMatthew Ahrens 
357419b94df9SMatthew Ahrens 		/*
357578f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
357678f17100SMatthew Ahrens 		 * must contain only valid characters.
357778f17100SMatthew Ahrens 		 */
357878f17100SMatthew Ahrens 		if (cp == NULL ||
357978f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
358078f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
358178f17100SMatthew Ahrens 
358278f17100SMatthew Ahrens 		/*
358378f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
358419b94df9SMatthew Ahrens 		 */
35854445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
358678f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
3587be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
358819b94df9SMatthew Ahrens 	}
358919b94df9SMatthew Ahrens 
359078f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
359178f17100SMatthew Ahrens 	return (error);
35921d452cf5Sahrens }
35931d452cf5Sahrens 
35943cb34c60Sahrens /*
35953cb34c60Sahrens  * inputs:
35963cb34c60Sahrens  * zc_name		name of dataset to destroy
35973cb34c60Sahrens  * zc_objset_type	type of objset
3598842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
35993cb34c60Sahrens  *
36003cb34c60Sahrens  * outputs:		none
36013cb34c60Sahrens  */
36021d452cf5Sahrens static int
36031d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36041d452cf5Sahrens {
3605681d9761SEric Taylor 	int err;
3606fc7a6e3fSWill Andrews 
3607fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3608fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3609fc7a6e3fSWill Andrews 		if (err != 0)
3610fc7a6e3fSWill Andrews 			return (err);
3611fc7a6e3fSWill Andrews 	}
3612fa9e4066Sahrens 
36133b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
36143b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36153b2aab18SMatthew Ahrens 	else
36163b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3617681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36185c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3619681d9761SEric Taylor 	return (err);
3620fa9e4066Sahrens }
3621fa9e4066Sahrens 
36223cb34c60Sahrens /*
3623a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
36243cb34c60Sahrens  *
3625a7027df1SMatthew Ahrens  * innvl is not used.
3626a7027df1SMatthew Ahrens  *
3627a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3628a7027df1SMatthew Ahrens  * }
36293cb34c60Sahrens  */
3630a7027df1SMatthew Ahrens /* ARGSUSED */
3631fa9e4066Sahrens static int
3632a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3633fa9e4066Sahrens {
3634ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
36353b2aab18SMatthew Ahrens 	int error;
3636ae46e4c7SMatthew Ahrens 
3637a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3638503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
363947f263f4Sek 		if (error == 0) {
364047f263f4Sek 			int resume_err;
36414ccbb6e7Sahrens 
3642a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3643a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
364447f263f4Sek 			error = error ? error : resume_err;
364547f263f4Sek 		}
36464ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
36474ccbb6e7Sahrens 	} else {
3648a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
36494ccbb6e7Sahrens 	}
36503b2aab18SMatthew Ahrens 	return (error);
36513b2aab18SMatthew Ahrens }
36524ccbb6e7Sahrens 
36533b2aab18SMatthew Ahrens static int
36543b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
36553b2aab18SMatthew Ahrens {
36563b2aab18SMatthew Ahrens 	const char *snapname = arg;
36573b2aab18SMatthew Ahrens 	char fullname[MAXNAMELEN];
3658ae46e4c7SMatthew Ahrens 
36593b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3660fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
3661fa9e4066Sahrens }
3662fa9e4066Sahrens 
36633cb34c60Sahrens /*
36643cb34c60Sahrens  * inputs:
36653cb34c60Sahrens  * zc_name	old name of dataset
36663cb34c60Sahrens  * zc_value	new name of dataset
36673cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
36683cb34c60Sahrens  *
36693cb34c60Sahrens  * outputs:	none
36703cb34c60Sahrens  */
3671fa9e4066Sahrens static int
3672fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3673fa9e4066Sahrens {
36747f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
36753b2aab18SMatthew Ahrens 	char *at;
3676cdf5b4caSmmusante 
3677e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3678f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3679f18faf3fSek 	    strchr(zc->zc_value, '%'))
3680be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3681fa9e4066Sahrens 
36823b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
36833b2aab18SMatthew Ahrens 	if (at != NULL) {
36843b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3685a0c1127bSSteven Hartland 		int error;
3686a0c1127bSSteven Hartland 
36873b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3688be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
36893b2aab18SMatthew Ahrens 		*at = '\0';
36903b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3691a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
36923b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
36933b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3694a0c1127bSSteven Hartland 			if (error != 0) {
3695a0c1127bSSteven Hartland 				*at = '@';
36963b2aab18SMatthew Ahrens 				return (error);
3697a0c1127bSSteven Hartland 			}
36983b2aab18SMatthew Ahrens 		}
3699a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3700a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3701a0c1127bSSteven Hartland 		*at = '@';
3702a0c1127bSSteven Hartland 
3703a0c1127bSSteven Hartland 		return (error);
37043b2aab18SMatthew Ahrens 	} else {
37053b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZVOL)
37063b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
37073b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3708fa9e4066Sahrens 	}
3709fa9e4066Sahrens }
3710fa9e4066Sahrens 
371192241e0bSTom Erickson static int
371292241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
371392241e0bSTom Erickson {
371492241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
371592241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
371692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
371792241e0bSTom Erickson 	uint64_t intval;
371892241e0bSTom Erickson 	int err;
371992241e0bSTom Erickson 
372092241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
372192241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
372292241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
372392241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
372492241e0bSTom Erickson 				return (err);
372592241e0bSTom Erickson 			return (0);
372692241e0bSTom Erickson 		}
372792241e0bSTom Erickson 
372892241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
372992241e0bSTom Erickson 			const char *perm = NULL;
373092241e0bSTom Erickson 			const char *uq_prefix =
373192241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
373292241e0bSTom Erickson 			const char *gq_prefix =
373392241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
373492241e0bSTom Erickson 
373592241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
373692241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
373792241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
373892241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
373992241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
374092241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
374192241e0bSTom Erickson 			} else {
374292241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3743be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
374492241e0bSTom Erickson 			}
374592241e0bSTom Erickson 
374692241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
374792241e0bSTom Erickson 				return (err);
374892241e0bSTom Erickson 			return (0);
374992241e0bSTom Erickson 		}
375092241e0bSTom Erickson 
3751be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
375292241e0bSTom Erickson 	}
375392241e0bSTom Erickson 
375492241e0bSTom Erickson 	if (issnap)
3755be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
375692241e0bSTom Erickson 
375792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
375892241e0bSTom Erickson 		/*
375992241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
376092241e0bSTom Erickson 		 * format.
376192241e0bSTom Erickson 		 */
376292241e0bSTom Erickson 		nvlist_t *attrs;
376392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
376492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
376592241e0bSTom Erickson 		    &pair) == 0);
376692241e0bSTom Erickson 	}
376792241e0bSTom Erickson 
376892241e0bSTom Erickson 	/*
376992241e0bSTom Erickson 	 * Check that this value is valid for this pool version
377092241e0bSTom Erickson 	 */
377192241e0bSTom Erickson 	switch (prop) {
377292241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
377392241e0bSTom Erickson 		/*
377492241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
377592241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
377692241e0bSTom Erickson 		 * we'll catch them later.
377792241e0bSTom Erickson 		 */
3778b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
377992241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
378092241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
378192241e0bSTom Erickson 			    zfs_earlier_version(dsname,
378292241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3783be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
378492241e0bSTom Erickson 			}
378592241e0bSTom Erickson 
378692241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
378792241e0bSTom Erickson 			    zfs_earlier_version(dsname,
378892241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3789be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
379092241e0bSTom Erickson 
3791a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3792a6f561b4SSašo Kiselkov 				spa_t *spa;
3793a6f561b4SSašo Kiselkov 
3794a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3795a6f561b4SSašo Kiselkov 					return (err);
3796a6f561b4SSašo Kiselkov 
37972acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
37982acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3799a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3800be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3801a6f561b4SSašo Kiselkov 				}
3802a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3803a6f561b4SSašo Kiselkov 			}
3804a6f561b4SSašo Kiselkov 
380592241e0bSTom Erickson 			/*
380692241e0bSTom Erickson 			 * If this is a bootable dataset then
380792241e0bSTom Erickson 			 * verify that the compression algorithm
380892241e0bSTom Erickson 			 * is supported for booting. We must return
380992241e0bSTom Erickson 			 * something other than ENOTSUP since it
381092241e0bSTom Erickson 			 * implies a downrev pool version.
381192241e0bSTom Erickson 			 */
381292241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
381392241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3814be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
381592241e0bSTom Erickson 			}
381692241e0bSTom Erickson 		}
381792241e0bSTom Erickson 		break;
381892241e0bSTom Erickson 
381992241e0bSTom Erickson 	case ZFS_PROP_COPIES:
382092241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3821be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
382292241e0bSTom Erickson 		break;
382392241e0bSTom Erickson 
3824b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
3825b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
3826b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
3827b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
3828b5152584SMatthew Ahrens 			spa_t *spa;
3829b5152584SMatthew Ahrens 
3830b5152584SMatthew Ahrens 			/*
3831b5152584SMatthew Ahrens 			 * If this is a bootable dataset then
3832b5152584SMatthew Ahrens 			 * the we don't allow large (>128K) blocks,
3833b5152584SMatthew Ahrens 			 * because GRUB doesn't support them.
3834b5152584SMatthew Ahrens 			 */
3835b5152584SMatthew Ahrens 			if (zfs_is_bootfs(dsname) &&
3836b5152584SMatthew Ahrens 			    intval > SPA_OLD_MAXBLOCKSIZE) {
38376de9bb56SMatthew Ahrens 				return (SET_ERROR(ERANGE));
3838b5152584SMatthew Ahrens 			}
3839b5152584SMatthew Ahrens 
3840b5152584SMatthew Ahrens 			/*
3841b5152584SMatthew Ahrens 			 * We don't allow setting the property above 1MB,
3842b5152584SMatthew Ahrens 			 * unless the tunable has been changed.
3843b5152584SMatthew Ahrens 			 */
3844b5152584SMatthew Ahrens 			if (intval > zfs_max_recordsize ||
3845b5152584SMatthew Ahrens 			    intval > SPA_MAXBLOCKSIZE)
38466de9bb56SMatthew Ahrens 				return (SET_ERROR(ERANGE));
3847b5152584SMatthew Ahrens 
3848b5152584SMatthew Ahrens 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3849b5152584SMatthew Ahrens 				return (err);
3850b5152584SMatthew Ahrens 
3851b5152584SMatthew Ahrens 			if (!spa_feature_is_enabled(spa,
3852b5152584SMatthew Ahrens 			    SPA_FEATURE_LARGE_BLOCKS)) {
3853b5152584SMatthew Ahrens 				spa_close(spa, FTAG);
3854b5152584SMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
3855b5152584SMatthew Ahrens 			}
3856b5152584SMatthew Ahrens 			spa_close(spa, FTAG);
3857b5152584SMatthew Ahrens 		}
3858b5152584SMatthew Ahrens 		break;
3859b5152584SMatthew Ahrens 
386092241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
386192241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3862be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
386392241e0bSTom Erickson 		break;
386492241e0bSTom Erickson 
386592241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
386692241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
386792241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
386892241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
386992241e0bSTom Erickson 			    zfs_earlier_version(dsname,
387092241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
3871be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
387292241e0bSTom Erickson 		}
387392241e0bSTom Erickson 		break;
387445818ee1SMatthew Ahrens 
387545818ee1SMatthew Ahrens 	case ZFS_PROP_CHECKSUM:
387645818ee1SMatthew Ahrens 	case ZFS_PROP_DEDUP:
387745818ee1SMatthew Ahrens 	{
387845818ee1SMatthew Ahrens 		spa_feature_t feature;
387945818ee1SMatthew Ahrens 		spa_t *spa;
388045818ee1SMatthew Ahrens 
388145818ee1SMatthew Ahrens 		/* dedup feature version checks */
388245818ee1SMatthew Ahrens 		if (prop == ZFS_PROP_DEDUP &&
388345818ee1SMatthew Ahrens 		    zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
388445818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
388545818ee1SMatthew Ahrens 
388645818ee1SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) != 0)
388745818ee1SMatthew Ahrens 			return (SET_ERROR(EINVAL));
388845818ee1SMatthew Ahrens 
388945818ee1SMatthew Ahrens 		/* check prop value is enabled in features */
3890971640e6Silovezfs 		feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
389145818ee1SMatthew Ahrens 		if (feature == SPA_FEATURE_NONE)
389245818ee1SMatthew Ahrens 			break;
389345818ee1SMatthew Ahrens 
389445818ee1SMatthew Ahrens 		if ((err = spa_open(dsname, &spa, FTAG)) != 0)
389545818ee1SMatthew Ahrens 			return (err);
389645818ee1SMatthew Ahrens 		/*
389745818ee1SMatthew Ahrens 		 * Salted checksums are not supported on root pools.
389845818ee1SMatthew Ahrens 		 */
389945818ee1SMatthew Ahrens 		if (spa_bootfs(spa) != 0 &&
390045818ee1SMatthew Ahrens 		    intval < ZIO_CHECKSUM_FUNCTIONS &&
390145818ee1SMatthew Ahrens 		    (zio_checksum_table[intval].ci_flags &
390245818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_SALTED)) {
390345818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
390445818ee1SMatthew Ahrens 			return (SET_ERROR(ERANGE));
390545818ee1SMatthew Ahrens 		}
390645818ee1SMatthew Ahrens 		if (!spa_feature_is_enabled(spa, feature)) {
390745818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
390845818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
390945818ee1SMatthew Ahrens 		}
391045818ee1SMatthew Ahrens 		spa_close(spa, FTAG);
391145818ee1SMatthew Ahrens 		break;
391245818ee1SMatthew Ahrens 	}
391392241e0bSTom Erickson 	}
391492241e0bSTom Erickson 
391592241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
391692241e0bSTom Erickson }
391792241e0bSTom Erickson 
3918a6f561b4SSašo Kiselkov /*
3919a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
3920a6f561b4SSašo Kiselkov  * multiple times.
3921a6f561b4SSašo Kiselkov  */
3922a6f561b4SSašo Kiselkov static int
39233b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3924a6f561b4SSašo Kiselkov {
39253b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39262acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3927a6f561b4SSašo Kiselkov 
39282acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
3929a6f561b4SSašo Kiselkov 		return (0);
3930a6f561b4SSašo Kiselkov 	else
3931be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
3932a6f561b4SSašo Kiselkov }
3933a6f561b4SSašo Kiselkov 
3934a6f561b4SSašo Kiselkov /*
3935a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
3936a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
3937a6f561b4SSašo Kiselkov  */
3938a6f561b4SSašo Kiselkov static void
39393b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3940a6f561b4SSašo Kiselkov {
39413b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
39422acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
3943a6f561b4SSašo Kiselkov 
39442acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
3945a6f561b4SSašo Kiselkov }
3946a6f561b4SSašo Kiselkov 
39473b2aab18SMatthew Ahrens /*
39483b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
39493b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
39503b2aab18SMatthew Ahrens  * as being active.
39513b2aab18SMatthew Ahrens  */
39523b2aab18SMatthew Ahrens static int
39532acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
39543b2aab18SMatthew Ahrens {
39553b2aab18SMatthew Ahrens 	int err;
39563b2aab18SMatthew Ahrens 
39573b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
39583b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
39593b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
39607d46dc6cSMatthew Ahrens 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
39613b2aab18SMatthew Ahrens 
39623b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
39633b2aab18SMatthew Ahrens 		return (err);
39643b2aab18SMatthew Ahrens 	else
39653b2aab18SMatthew Ahrens 		return (0);
39663b2aab18SMatthew Ahrens }
39673b2aab18SMatthew Ahrens 
396892241e0bSTom Erickson /*
396992241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
397092241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
397192241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
397292241e0bSTom Erickson  *
397392241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
397492241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
397592241e0bSTom Erickson  * of all the properties that failed a permission check along with the
397692241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
397792241e0bSTom Erickson  * returned errlist.
397892241e0bSTom Erickson  *
397992241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
398092241e0bSTom Erickson  * pointed at by errlist is NULL.
398192241e0bSTom Erickson  */
398292241e0bSTom Erickson static int
398392241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3984745cd3c5Smaybee {
3985745cd3c5Smaybee 	zfs_cmd_t *zc;
398692241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
398792241e0bSTom Erickson 	nvlist_t *errors;
398892241e0bSTom Erickson 	int err, rv = 0;
3989745cd3c5Smaybee 
3990745cd3c5Smaybee 	if (props == NULL)
399192241e0bSTom Erickson 		return (0);
399292241e0bSTom Erickson 
399392241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
399492241e0bSTom Erickson 
3995745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3996745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
399792241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
399892241e0bSTom Erickson 	while (pair != NULL) {
399992241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
400092241e0bSTom Erickson 
400192241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
400292241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
40034445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
400492241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
400592241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
400692241e0bSTom Erickson 			    zc->zc_value, err) == 0);
400792241e0bSTom Erickson 		}
400892241e0bSTom Erickson 		pair = next_pair;
4009745cd3c5Smaybee 	}
4010745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
401192241e0bSTom Erickson 
401292241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
401392241e0bSTom Erickson 		nvlist_free(errors);
401492241e0bSTom Erickson 		errors = NULL;
401592241e0bSTom Erickson 	} else {
401692241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
401792241e0bSTom Erickson 	}
401892241e0bSTom Erickson 
401992241e0bSTom Erickson 	if (errlist == NULL)
402092241e0bSTom Erickson 		nvlist_free(errors);
402192241e0bSTom Erickson 	else
402292241e0bSTom Erickson 		*errlist = errors;
402392241e0bSTom Erickson 
402492241e0bSTom Erickson 	return (rv);
402592241e0bSTom Erickson }
402692241e0bSTom Erickson 
402792241e0bSTom Erickson static boolean_t
402892241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
402992241e0bSTom Erickson {
403092241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
403192241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
403292241e0bSTom Erickson 		nvlist_t *attrs;
403392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
403492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
403592241e0bSTom Erickson 		    &p1) == 0);
403692241e0bSTom Erickson 	}
403792241e0bSTom Erickson 
403892241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
403992241e0bSTom Erickson 		nvlist_t *attrs;
404092241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
404192241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
404292241e0bSTom Erickson 		    &p2) == 0);
404392241e0bSTom Erickson 	}
404492241e0bSTom Erickson 
404592241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
404692241e0bSTom Erickson 		return (B_FALSE);
404792241e0bSTom Erickson 
404892241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
404992241e0bSTom Erickson 		char *valstr1, *valstr2;
405092241e0bSTom Erickson 
405192241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
405292241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
405392241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
405492241e0bSTom Erickson 	} else {
405592241e0bSTom Erickson 		uint64_t intval1, intval2;
405692241e0bSTom Erickson 
405792241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
405892241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
405992241e0bSTom Erickson 		return (intval1 == intval2);
406092241e0bSTom Erickson 	}
4061745cd3c5Smaybee }
4062745cd3c5Smaybee 
406392241e0bSTom Erickson /*
406492241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
406592241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
406692241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
406792241e0bSTom Erickson  */
406892241e0bSTom Erickson static void
406992241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
407092241e0bSTom Erickson {
407192241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
407292241e0bSTom Erickson 
407392241e0bSTom Erickson 	if (origprops == NULL)
407492241e0bSTom Erickson 		return; /* all props need to be received */
407592241e0bSTom Erickson 
407692241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
407792241e0bSTom Erickson 	while (pair != NULL) {
407892241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
407992241e0bSTom Erickson 		nvpair_t *match;
408092241e0bSTom Erickson 
408192241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
408292241e0bSTom Erickson 
408392241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
408492241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
408592241e0bSTom Erickson 			goto next; /* need to set received value */
408692241e0bSTom Erickson 
408792241e0bSTom Erickson 		/* don't clear the existing received value */
408892241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
408992241e0bSTom Erickson 		/* don't bother receiving the property */
409092241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
409192241e0bSTom Erickson next:
409292241e0bSTom Erickson 		pair = next_pair;
409392241e0bSTom Erickson 	}
409492241e0bSTom Erickson }
409592241e0bSTom Erickson 
40965878fad7SDan McDonald /*
40975878fad7SDan McDonald  * Extract properties that cannot be set PRIOR to the receipt of a dataset.
40985878fad7SDan McDonald  * For example, refquota cannot be set until after the receipt of a dataset,
40995878fad7SDan McDonald  * because in replication streams, an older/earlier snapshot may exceed the
41005878fad7SDan McDonald  * refquota.  We want to receive the older/earlier snapshot, but setting
41015878fad7SDan McDonald  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
41025878fad7SDan McDonald  * the older/earlier snapshot from being received (with EDQUOT).
41035878fad7SDan McDonald  *
41045878fad7SDan McDonald  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
41055878fad7SDan McDonald  *
41065878fad7SDan McDonald  * libzfs will need to be judicious handling errors encountered by props
41075878fad7SDan McDonald  * extracted by this function.
41085878fad7SDan McDonald  */
41095878fad7SDan McDonald static nvlist_t *
41105878fad7SDan McDonald extract_delay_props(nvlist_t *props)
41115878fad7SDan McDonald {
41125878fad7SDan McDonald 	nvlist_t *delayprops;
41135878fad7SDan McDonald 	nvpair_t *nvp, *tmp;
41145878fad7SDan McDonald 	static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
41155878fad7SDan McDonald 	int i;
41165878fad7SDan McDonald 
41175878fad7SDan McDonald 	VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
41185878fad7SDan McDonald 
41195878fad7SDan McDonald 	for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
41205878fad7SDan McDonald 	    nvp = nvlist_next_nvpair(props, nvp)) {
41215878fad7SDan McDonald 		/*
41225878fad7SDan McDonald 		 * strcmp() is safe because zfs_prop_to_name() always returns
41235878fad7SDan McDonald 		 * a bounded string.
41245878fad7SDan McDonald 		 */
41255878fad7SDan McDonald 		for (i = 0; delayable[i] != 0; i++) {
41265878fad7SDan McDonald 			if (strcmp(zfs_prop_to_name(delayable[i]),
41275878fad7SDan McDonald 			    nvpair_name(nvp)) == 0) {
41285878fad7SDan McDonald 				break;
41295878fad7SDan McDonald 			}
41305878fad7SDan McDonald 		}
41315878fad7SDan McDonald 		if (delayable[i] != 0) {
41325878fad7SDan McDonald 			tmp = nvlist_prev_nvpair(props, nvp);
41335878fad7SDan McDonald 			VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
41345878fad7SDan McDonald 			VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
41355878fad7SDan McDonald 			nvp = tmp;
41365878fad7SDan McDonald 		}
41375878fad7SDan McDonald 	}
41385878fad7SDan McDonald 
41395878fad7SDan McDonald 	if (nvlist_empty(delayprops)) {
41405878fad7SDan McDonald 		nvlist_free(delayprops);
41415878fad7SDan McDonald 		delayprops = NULL;
41425878fad7SDan McDonald 	}
41435878fad7SDan McDonald 	return (delayprops);
41445878fad7SDan McDonald }
41455878fad7SDan McDonald 
414692241e0bSTom Erickson #ifdef	DEBUG
414792241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
414892241e0bSTom Erickson #endif
414992241e0bSTom Erickson 
41503cb34c60Sahrens /*
41513cb34c60Sahrens  * inputs:
41523cb34c60Sahrens  * zc_name		name of containing filesystem
41533cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
41543cb34c60Sahrens  * zc_value		name of snapshot to create
41553cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
41563cb34c60Sahrens  * zc_cookie		file descriptor to recv from
41573cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
41583cb34c60Sahrens  * zc_guid		force flag
4159c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
4160c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
41619c3fd121SMatthew Ahrens  * zc_resumable		if data is incomplete assume sender will resume
41623cb34c60Sahrens  *
41633cb34c60Sahrens  * outputs:
41643cb34c60Sahrens  * zc_cookie		number of bytes read
416592241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
416692241e0bSTom Erickson  * zc_obj		zprop_errflags_t
4167c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
41683cb34c60Sahrens  */
4169fa9e4066Sahrens static int
41703cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4171fa9e4066Sahrens {
4172fa9e4066Sahrens 	file_t *fp;
41733cb34c60Sahrens 	dmu_recv_cookie_t drc;
4174f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
417592241e0bSTom Erickson 	int fd;
417692241e0bSTom Erickson 	int error = 0;
417792241e0bSTom Erickson 	int props_error = 0;
417892241e0bSTom Erickson 	nvlist_t *errors;
41793cb34c60Sahrens 	offset_t off;
418092241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
418192241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
41825878fad7SDan McDonald 	nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
41833b2aab18SMatthew Ahrens 	char *origin = NULL;
41843cb34c60Sahrens 	char *tosnap;
41853cb34c60Sahrens 	char tofs[ZFS_MAXNAMELEN];
418692241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
4187fa9e4066Sahrens 
41883ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4189f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
4190f18faf3fSek 	    strchr(zc->zc_value, '%'))
4191be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
41923ccfa83cSahrens 
41933cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
41943cb34c60Sahrens 	tosnap = strchr(tofs, '@');
419592241e0bSTom Erickson 	*tosnap++ = '\0';
41963cb34c60Sahrens 
41973cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
41983cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4199478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
42003cb34c60Sahrens 		return (error);
42013cb34c60Sahrens 
4202fa9e4066Sahrens 	fd = zc->zc_cookie;
4203fa9e4066Sahrens 	fp = getf(fd);
42043cb34c60Sahrens 	if (fp == NULL) {
42053cb34c60Sahrens 		nvlist_free(props);
4206be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
42073cb34c60Sahrens 	}
4208f18faf3fSek 
42099c3fd121SMatthew Ahrens 	errors = fnvlist_alloc();
421092241e0bSTom Erickson 
42113b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
42123b2aab18SMatthew Ahrens 		origin = zc->zc_string;
42133b2aab18SMatthew Ahrens 
42143b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
42159c3fd121SMatthew Ahrens 	    &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
42163b2aab18SMatthew Ahrens 	if (error != 0)
42173b2aab18SMatthew Ahrens 		goto out;
42183b2aab18SMatthew Ahrens 
42193b2aab18SMatthew Ahrens 	/*
42203b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
42213b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
42223b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
42233b2aab18SMatthew Ahrens 	 */
42243b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
42253b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
42263b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
42273b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
422892241e0bSTom Erickson 			first_recvd_props = B_TRUE;
422992241e0bSTom Erickson 
4230745cd3c5Smaybee 		/*
423192241e0bSTom Erickson 		 * If new received properties are supplied, they are to
423292241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
423392241e0bSTom Erickson 		 * away the existing ones.
4234745cd3c5Smaybee 		 */
42353b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
423692241e0bSTom Erickson 			nvlist_t *errlist = NULL;
423792241e0bSTom Erickson 			/*
423892241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
423992241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
424092241e0bSTom Erickson 			 *
424192241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
424292241e0bSTom Erickson 			 * special case where we blow away all local properties
424392241e0bSTom Erickson 			 * regardless.
424492241e0bSTom Erickson 			 */
424592241e0bSTom Erickson 			if (!first_recvd_props)
424692241e0bSTom Erickson 				props_reduce(props, origprops);
42473b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
424892241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
424992241e0bSTom Erickson 			nvlist_free(errlist);
42503cb34c60Sahrens 
42513b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
42523b2aab18SMatthew Ahrens 			    first_recvd_props ? NULL : props) != 0)
425392241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
42543b2aab18SMatthew Ahrens 		} else {
425592241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
425692241e0bSTom Erickson 		}
42573b2aab18SMatthew Ahrens 	}
425892241e0bSTom Erickson 
42593b2aab18SMatthew Ahrens 	if (props != NULL) {
42603b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
42613b2aab18SMatthew Ahrens 
42623b2aab18SMatthew Ahrens 		if (props_error == 0) {
42635878fad7SDan McDonald 			delayprops = extract_delay_props(props);
42643b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
42653b2aab18SMatthew Ahrens 			    props, errors);
42663b2aab18SMatthew Ahrens 		}
426792241e0bSTom Erickson 	}
426892241e0bSTom Erickson 
42693cb34c60Sahrens 	off = fp->f_offset;
4270c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4271c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
4272a2eea2e1Sahrens 
4273f4b94bdeSMatthew Ahrens 	if (error == 0) {
4274f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4275745cd3c5Smaybee 
4276f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4277f4b94bdeSMatthew Ahrens 			/* online recv */
4278f4b94bdeSMatthew Ahrens 			int end_err;
4279745cd3c5Smaybee 
4280503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4281f4b94bdeSMatthew Ahrens 			/*
4282f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4283f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4284f4b94bdeSMatthew Ahrens 			 */
428591948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
42865c703fceSGeorge Wilson 			if (error == 0)
42875c703fceSGeorge Wilson 				error = zfs_resume_fs(zfsvfs, tofs);
4288f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4289f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4290745cd3c5Smaybee 		} else {
429191948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
42923cb34c60Sahrens 		}
42935878fad7SDan McDonald 
42945878fad7SDan McDonald 		/* Set delayed properties now, after we're done receiving. */
42955878fad7SDan McDonald 		if (delayprops != NULL && error == 0) {
42965878fad7SDan McDonald 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
42975878fad7SDan McDonald 			    delayprops, errors);
42985878fad7SDan McDonald 		}
42995878fad7SDan McDonald 	}
43005878fad7SDan McDonald 
43015878fad7SDan McDonald 	if (delayprops != NULL) {
43025878fad7SDan McDonald 		/*
43035878fad7SDan McDonald 		 * Merge delayed props back in with initial props, in case
43045878fad7SDan McDonald 		 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
43055878fad7SDan McDonald 		 * we have to make sure clear_received_props() includes
43065878fad7SDan McDonald 		 * the delayed properties).
43075878fad7SDan McDonald 		 *
43085878fad7SDan McDonald 		 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
43095878fad7SDan McDonald 		 * using ASSERT() will be just like a VERIFY.
43105878fad7SDan McDonald 		 */
43115878fad7SDan McDonald 		ASSERT(nvlist_merge(props, delayprops, 0) == 0);
43125878fad7SDan McDonald 		nvlist_free(delayprops);
43135878fad7SDan McDonald 	}
43145878fad7SDan McDonald 
43155878fad7SDan McDonald 	/*
43165878fad7SDan McDonald 	 * Now that all props, initial and delayed, are set, report the prop
43175878fad7SDan McDonald 	 * errors to the caller.
43185878fad7SDan McDonald 	 */
43195878fad7SDan McDonald 	if (zc->zc_nvlist_dst_size != 0 &&
43205878fad7SDan McDonald 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
43215878fad7SDan McDonald 	    put_nvlist(zc, errors) != 0)) {
43225878fad7SDan McDonald 		/*
43235878fad7SDan McDonald 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
43245878fad7SDan McDonald 		 * size or supplied an invalid address.
43255878fad7SDan McDonald 		 */
43265878fad7SDan McDonald 		props_error = SET_ERROR(EINVAL);
432747f263f4Sek 	}
43283cb34c60Sahrens 
43293cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
43303cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
43313cb34c60Sahrens 		fp->f_offset = off;
4332a2eea2e1Sahrens 
433392241e0bSTom Erickson #ifdef	DEBUG
433492241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
433592241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
433692241e0bSTom Erickson 		error = 1;
433792241e0bSTom Erickson 	}
433892241e0bSTom Erickson #endif
4339745cd3c5Smaybee 	/*
4340745cd3c5Smaybee 	 * On error, restore the original props.
4341745cd3c5Smaybee 	 */
43423b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
43433b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
43443b2aab18SMatthew Ahrens 			/*
43453b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
43463b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
43473b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
43483b2aab18SMatthew Ahrens 			 */
434992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
43503b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
43513b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
435292241e0bSTom Erickson 		}
435392241e0bSTom Erickson 
435492241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
435592241e0bSTom Erickson 			/* We failed to stash the original properties. */
435692241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
435792241e0bSTom Erickson 		}
435892241e0bSTom Erickson 
435992241e0bSTom Erickson 		/*
436092241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
436192241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
436292241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
436392241e0bSTom Erickson 		 * first new-style receive.
436492241e0bSTom Erickson 		 */
436592241e0bSTom Erickson 		if (origprops != NULL &&
436692241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
436792241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
436892241e0bSTom Erickson 		    origprops, NULL) != 0) {
436992241e0bSTom Erickson 			/*
437092241e0bSTom Erickson 			 * We stashed the original properties but failed to
437192241e0bSTom Erickson 			 * restore them.
437292241e0bSTom Erickson 			 */
437392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
437492241e0bSTom Erickson 		}
4375745cd3c5Smaybee 	}
4376745cd3c5Smaybee out:
4377745cd3c5Smaybee 	nvlist_free(props);
4378745cd3c5Smaybee 	nvlist_free(origprops);
437992241e0bSTom Erickson 	nvlist_free(errors);
4380fa9e4066Sahrens 	releasef(fd);
438192241e0bSTom Erickson 
438292241e0bSTom Erickson 	if (error == 0)
438392241e0bSTom Erickson 		error = props_error;
438492241e0bSTom Erickson 
4385fa9e4066Sahrens 	return (error);
4386fa9e4066Sahrens }
4387fa9e4066Sahrens 
43883cb34c60Sahrens /*
43893cb34c60Sahrens  * inputs:
43903cb34c60Sahrens  * zc_name	name of snapshot to send
43913cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4392a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4393a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4394a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
439519b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
439619b94df9SMatthew Ahrens  *		output size in zc_objset_type.
4397b5152584SMatthew Ahrens  * zc_flags	lzc_send_flags
43983cb34c60Sahrens  *
439978f17100SMatthew Ahrens  * outputs:
440078f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
44013cb34c60Sahrens  */
4402fa9e4066Sahrens static int
44033cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4404fa9e4066Sahrens {
4405fa9e4066Sahrens 	int error;
44063cb34c60Sahrens 	offset_t off;
440719b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
44085d7b4d43SMatthew Ahrens 	boolean_t embedok = (zc->zc_flags & 0x1);
4409b5152584SMatthew Ahrens 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
4410fa9e4066Sahrens 
44113b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
44123b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
44133b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4414a7f53a56SChris Kirby 
44153b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44163b2aab18SMatthew Ahrens 		if (error != 0)
4417a7f53a56SChris Kirby 			return (error);
44183b2aab18SMatthew Ahrens 
44193b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
44203b2aab18SMatthew Ahrens 		if (error != 0) {
44213b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4422fa9e4066Sahrens 			return (error);
4423fa9e4066Sahrens 		}
44243b2aab18SMatthew Ahrens 
44253b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
4426c1379625SJustin T. Gibbs 			zc->zc_fromobj =
4427c1379625SJustin T. Gibbs 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
44283b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
44293b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
44304445fffbSMatthew Ahrens 	}
44314445fffbSMatthew Ahrens 
44323b2aab18SMatthew Ahrens 	if (estimate) {
44333b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
44343b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
44353b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
44364445fffbSMatthew Ahrens 
44373b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44383b2aab18SMatthew Ahrens 		if (error != 0)
44393b2aab18SMatthew Ahrens 			return (error);
44403b2aab18SMatthew Ahrens 
44413b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
44423b2aab18SMatthew Ahrens 		if (error != 0) {
44433b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
44443b2aab18SMatthew Ahrens 			return (error);
44454445fffbSMatthew Ahrens 		}
44464445fffbSMatthew Ahrens 
44473b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
44483b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
44493b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
44503b2aab18SMatthew Ahrens 			if (error != 0) {
44513b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
44523b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
44534445fffbSMatthew Ahrens 				return (error);
44544445fffbSMatthew Ahrens 			}
44554445fffbSMatthew Ahrens 		}
4456fa9e4066Sahrens 
44574445fffbSMatthew Ahrens 		error = dmu_send_estimate(tosnap, fromsnap,
445819b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
44593b2aab18SMatthew Ahrens 
44603b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
44613b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
44623b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
44633b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
446419b94df9SMatthew Ahrens 	} else {
446519b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
44663b2aab18SMatthew Ahrens 		if (fp == NULL)
4467be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4468fa9e4066Sahrens 
446919b94df9SMatthew Ahrens 		off = fp->f_offset;
44703b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4471b5152584SMatthew Ahrens 		    zc->zc_fromobj, embedok, large_block_ok,
4472b5152584SMatthew Ahrens 		    zc->zc_cookie, fp->f_vnode, &off);
4473fa9e4066Sahrens 
447419b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
447519b94df9SMatthew Ahrens 			fp->f_offset = off;
447619b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
447719b94df9SMatthew Ahrens 	}
4478fa9e4066Sahrens 	return (error);
4479fa9e4066Sahrens }
4480fa9e4066Sahrens 
44814e3c9f44SBill Pijewski /*
44824e3c9f44SBill Pijewski  * inputs:
44834e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
44844e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
44854e3c9f44SBill Pijewski  *
44864e3c9f44SBill Pijewski  * outputs:
44874e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
44884e3c9f44SBill Pijewski  */
44894e3c9f44SBill Pijewski static int
44904e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
44914e3c9f44SBill Pijewski {
44923b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
44934e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
44944e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
44954e3c9f44SBill Pijewski 	int error;
44964e3c9f44SBill Pijewski 
44973b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
44983b2aab18SMatthew Ahrens 	if (error != 0)
44993b2aab18SMatthew Ahrens 		return (error);
45003b2aab18SMatthew Ahrens 
45013b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
45023b2aab18SMatthew Ahrens 	if (error != 0) {
45033b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
45044e3c9f44SBill Pijewski 		return (error);
45053b2aab18SMatthew Ahrens 	}
45064e3c9f44SBill Pijewski 
45074e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
45084e3c9f44SBill Pijewski 
45094e3c9f44SBill Pijewski 	/*
45104e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
45114e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
45124e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
45134e3c9f44SBill Pijewski 	 * that stream.
45144e3c9f44SBill Pijewski 	 */
45154e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
45164e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
45174e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
45184e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
45194e3c9f44SBill Pijewski 			break;
45204e3c9f44SBill Pijewski 	}
45214e3c9f44SBill Pijewski 
45224e3c9f44SBill Pijewski 	if (dsp != NULL)
45234e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
45244e3c9f44SBill Pijewski 	else
4525be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
45264e3c9f44SBill Pijewski 
45274e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
45284e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
45293b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
45304e3c9f44SBill Pijewski 	return (error);
45314e3c9f44SBill Pijewski }
45324e3c9f44SBill Pijewski 
4533ea8dc4b6Seschrock static int
4534ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4535ea8dc4b6Seschrock {
4536ea8dc4b6Seschrock 	int id, error;
4537ea8dc4b6Seschrock 
4538ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4539ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4540ea8dc4b6Seschrock 
4541ea8dc4b6Seschrock 	if (error == 0)
4542ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4543ea8dc4b6Seschrock 
4544ea8dc4b6Seschrock 	return (error);
4545ea8dc4b6Seschrock }
4546ea8dc4b6Seschrock 
4547ea8dc4b6Seschrock static int
4548ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4549ea8dc4b6Seschrock {
4550ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4551ea8dc4b6Seschrock }
4552ea8dc4b6Seschrock 
4553ea8dc4b6Seschrock static int
4554ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4555ea8dc4b6Seschrock {
4556ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4557ea8dc4b6Seschrock 	int error;
4558ea8dc4b6Seschrock 
4559ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4560ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4561ea8dc4b6Seschrock 
4562ea8dc4b6Seschrock 	zc->zc_guid = id;
4563ea8dc4b6Seschrock 
4564ea8dc4b6Seschrock 	return (error);
4565ea8dc4b6Seschrock }
4566ea8dc4b6Seschrock 
4567ea8dc4b6Seschrock static int
4568ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4569ea8dc4b6Seschrock {
4570ea8dc4b6Seschrock 	spa_t *spa;
4571ea8dc4b6Seschrock 	int error;
4572e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4573ea8dc4b6Seschrock 
4574ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4575ea8dc4b6Seschrock 		return (error);
4576ea8dc4b6Seschrock 
4577e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4578ea8dc4b6Seschrock 	    &count);
4579ea8dc4b6Seschrock 	if (error == 0)
4580e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4581ea8dc4b6Seschrock 	else
4582e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4583ea8dc4b6Seschrock 
4584ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4585ea8dc4b6Seschrock 
4586ea8dc4b6Seschrock 	return (error);
4587ea8dc4b6Seschrock }
4588ea8dc4b6Seschrock 
4589ea8dc4b6Seschrock static int
4590ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4591ea8dc4b6Seschrock {
4592ea8dc4b6Seschrock 	spa_t *spa;
4593ea8dc4b6Seschrock 	vdev_t *vd;
4594bb8b5132Sek 	int error;
4595ea8dc4b6Seschrock 
4596b87f3af3Sperrin 	/*
4597b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4598b87f3af3Sperrin 	 */
4599b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4600b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4601b87f3af3Sperrin 	if (spa == NULL) {
4602b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4603be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4604b87f3af3Sperrin 	}
4605b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4606b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4607b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4608b87f3af3Sperrin 	}
4609468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4610b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4611b87f3af3Sperrin 
4612c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4613468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4614468c413aSTim Haley 	} else {
4615468c413aSTim Haley 		nvlist_t *policy;
4616468c413aSTim Haley 		nvlist_t *config = NULL;
4617468c413aSTim Haley 
4618468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4619be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4620468c413aSTim Haley 
4621468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4622468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4623468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4624468c413aSTim Haley 			    policy, &config);
4625468c413aSTim Haley 			if (config != NULL) {
46264b964adaSGeorge Wilson 				int err;
46274b964adaSGeorge Wilson 
46284b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
46294b964adaSGeorge Wilson 					error = err;
4630468c413aSTim Haley 				nvlist_free(config);
4631468c413aSTim Haley 			}
4632468c413aSTim Haley 			nvlist_free(policy);
4633468c413aSTim Haley 		}
4634468c413aSTim Haley 	}
4635468c413aSTim Haley 
46363b2aab18SMatthew Ahrens 	if (error != 0)
4637ea8dc4b6Seschrock 		return (error);
4638ea8dc4b6Seschrock 
46398f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4640ea8dc4b6Seschrock 
4641e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4642ea8dc4b6Seschrock 		vd = NULL;
4643c5904d13Seschrock 	} else {
4644c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4645fa94a07fSbrendan 		if (vd == NULL) {
4646e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4647fa94a07fSbrendan 			spa_close(spa, FTAG);
4648be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4649fa94a07fSbrendan 		}
4650ea8dc4b6Seschrock 	}
4651ea8dc4b6Seschrock 
4652e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4653e14bb325SJeff Bonwick 
4654e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4655ea8dc4b6Seschrock 
4656e14bb325SJeff Bonwick 	/*
4657e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4658e14bb325SJeff Bonwick 	 */
465954d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4660be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4661ea8dc4b6Seschrock 
4662ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4663ea8dc4b6Seschrock 
466454d692b7SGeorge Wilson 	return (error);
4665ea8dc4b6Seschrock }
4666ea8dc4b6Seschrock 
46674263d13fSGeorge Wilson static int
46684263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
46694263d13fSGeorge Wilson {
46704263d13fSGeorge Wilson 	spa_t *spa;
46714263d13fSGeorge Wilson 	int error;
46724263d13fSGeorge Wilson 
46734263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
46743b2aab18SMatthew Ahrens 	if (error != 0)
46754263d13fSGeorge Wilson 		return (error);
46764263d13fSGeorge Wilson 
46774263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4678d6afdce2SGeorge Wilson 
4679d6afdce2SGeorge Wilson 	/*
4680d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4681d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4682d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4683d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4684d6afdce2SGeorge Wilson 	 */
4685d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
46864263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4687d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4688d6afdce2SGeorge Wilson 
46894263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
46904263d13fSGeorge Wilson 	spa_close(spa, FTAG);
46914263d13fSGeorge Wilson 	return (0);
46924263d13fSGeorge Wilson }
46933cb34c60Sahrens /*
46943cb34c60Sahrens  * inputs:
46953cb34c60Sahrens  * zc_name	name of filesystem
46963cb34c60Sahrens  * zc_value	name of origin snapshot
46973cb34c60Sahrens  *
4698681d9761SEric Taylor  * outputs:
4699681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
47003cb34c60Sahrens  */
470199653d4eSeschrock static int
470299653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
470399653d4eSeschrock {
47040b69c2f0Sahrens 	char *cp;
47050b69c2f0Sahrens 
47060b69c2f0Sahrens 	/*
47070b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
47080b69c2f0Sahrens 	 * it's easier.
47090b69c2f0Sahrens 	 */
4710e9dbad6fSeschrock 	cp = strchr(zc->zc_value, '@');
47110b69c2f0Sahrens 	if (cp)
47120b69c2f0Sahrens 		*cp = '\0';
4713e9dbad6fSeschrock 	(void) dmu_objset_find(zc->zc_value,
47143b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4715681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
471699653d4eSeschrock }
471799653d4eSeschrock 
471814843421SMatthew Ahrens /*
471914843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
472014843421SMatthew Ahrens  *
472114843421SMatthew Ahrens  * inputs:
472214843421SMatthew Ahrens  * zc_name	name of filesystem
472314843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
472414843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
472514843421SMatthew Ahrens  * zc_guid	RID/UID/GID
472614843421SMatthew Ahrens  *
472714843421SMatthew Ahrens  * outputs:
472814843421SMatthew Ahrens  * zc_cookie	property value
472914843421SMatthew Ahrens  */
473014843421SMatthew Ahrens static int
473114843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
473214843421SMatthew Ahrens {
473314843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
473414843421SMatthew Ahrens 	int error;
473514843421SMatthew Ahrens 
473614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4737be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
473814843421SMatthew Ahrens 
47391412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
47403b2aab18SMatthew Ahrens 	if (error != 0)
474114843421SMatthew Ahrens 		return (error);
474214843421SMatthew Ahrens 
474314843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
474414843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
474514843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
474614843421SMatthew Ahrens 
474714843421SMatthew Ahrens 	return (error);
474814843421SMatthew Ahrens }
474914843421SMatthew Ahrens 
475014843421SMatthew Ahrens /*
475114843421SMatthew Ahrens  * inputs:
475214843421SMatthew Ahrens  * zc_name		name of filesystem
475314843421SMatthew Ahrens  * zc_cookie		zap cursor
475414843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
475514843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
475614843421SMatthew Ahrens  *
475714843421SMatthew Ahrens  * outputs:
475814843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
475914843421SMatthew Ahrens  * zc_cookie	zap cursor
476014843421SMatthew Ahrens  */
476114843421SMatthew Ahrens static int
476214843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
476314843421SMatthew Ahrens {
476414843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
4765eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
476614843421SMatthew Ahrens 
4767eeb85002STim Haley 	if (bufsize <= 0)
4768be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
4769eeb85002STim Haley 
47701412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
47713b2aab18SMatthew Ahrens 	if (error != 0)
477214843421SMatthew Ahrens 		return (error);
477314843421SMatthew Ahrens 
477414843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
477514843421SMatthew Ahrens 
477614843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
477714843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
477814843421SMatthew Ahrens 
477914843421SMatthew Ahrens 	if (error == 0) {
478014843421SMatthew Ahrens 		error = xcopyout(buf,
478114843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
478214843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
478314843421SMatthew Ahrens 	}
478414843421SMatthew Ahrens 	kmem_free(buf, bufsize);
478514843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
478614843421SMatthew Ahrens 
478714843421SMatthew Ahrens 	return (error);
478814843421SMatthew Ahrens }
478914843421SMatthew Ahrens 
479014843421SMatthew Ahrens /*
479114843421SMatthew Ahrens  * inputs:
479214843421SMatthew Ahrens  * zc_name		name of filesystem
479314843421SMatthew Ahrens  *
479414843421SMatthew Ahrens  * outputs:
479514843421SMatthew Ahrens  * none
479614843421SMatthew Ahrens  */
479714843421SMatthew Ahrens static int
479814843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
479914843421SMatthew Ahrens {
480014843421SMatthew Ahrens 	objset_t *os;
48011195e687SMark J Musante 	int error = 0;
480214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
480314843421SMatthew Ahrens 
480414843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4805503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
480614843421SMatthew Ahrens 			/*
480714843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
480814843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
480914843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
481014843421SMatthew Ahrens 			 */
4811503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
481291948b51SKeith M Wesolowski 			if (error == 0) {
481391948b51SKeith M Wesolowski 				dmu_objset_refresh_ownership(zfsvfs->z_os,
481491948b51SKeith M Wesolowski 				    zfsvfs);
4815503ad85cSMatthew Ahrens 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
481691948b51SKeith M Wesolowski 			}
481714843421SMatthew Ahrens 		}
481814843421SMatthew Ahrens 		if (error == 0)
481914843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
482014843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
482114843421SMatthew Ahrens 	} else {
4822503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
4823503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
48243b2aab18SMatthew Ahrens 		if (error != 0)
482514843421SMatthew Ahrens 			return (error);
482614843421SMatthew Ahrens 
482714843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
4828503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
482914843421SMatthew Ahrens 	}
483014843421SMatthew Ahrens 
483114843421SMatthew Ahrens 	return (error);
483214843421SMatthew Ahrens }
483314843421SMatthew Ahrens 
4834ecd6cf80Smarks /*
4835ecd6cf80Smarks  * We don't want to have a hard dependency
4836ecd6cf80Smarks  * against some special symbols in sharefs
4837da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
4838ecd6cf80Smarks  * the first file system is shared.
4839da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
4840ecd6cf80Smarks  */
4841da6c28aaSamw int (*znfsexport_fs)(void *arg);
4842ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4843da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4844da6c28aaSamw 
4845da6c28aaSamw int zfs_nfsshare_inited;
4846da6c28aaSamw int zfs_smbshare_inited;
4847ecd6cf80Smarks 
4848ecd6cf80Smarks ddi_modhandle_t nfs_mod;
4849ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
4850da6c28aaSamw ddi_modhandle_t smbsrv_mod;
4851ecd6cf80Smarks kmutex_t zfs_share_lock;
4852ecd6cf80Smarks 
4853da6c28aaSamw static int
4854da6c28aaSamw zfs_init_sharefs()
4855da6c28aaSamw {
4856da6c28aaSamw 	int error;
4857da6c28aaSamw 
4858da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
4859da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
4860da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
4861da6c28aaSamw 	    ddi_modopen("fs/sharefs",
4862da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
4863be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4864da6c28aaSamw 	}
4865da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
4866da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4867da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4868be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
4869da6c28aaSamw 	}
4870da6c28aaSamw 	return (0);
4871da6c28aaSamw }
4872da6c28aaSamw 
4873ecd6cf80Smarks static int
4874ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
4875ecd6cf80Smarks {
4876ecd6cf80Smarks 	int error;
4877ecd6cf80Smarks 	int opcode;
4878ecd6cf80Smarks 
4879da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4880da6c28aaSamw 	case ZFS_SHARE_NFS:
4881da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4882da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
4883da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4884da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4885da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4886da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4887be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4888da6c28aaSamw 			}
4889da6c28aaSamw 			if (znfsexport_fs == NULL &&
4890da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
4891da6c28aaSamw 			    ddi_modsym(nfs_mod,
4892da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
4893da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4894be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4895da6c28aaSamw 			}
4896da6c28aaSamw 			error = zfs_init_sharefs();
48973b2aab18SMatthew Ahrens 			if (error != 0) {
4898da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4899be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4900da6c28aaSamw 			}
4901da6c28aaSamw 			zfs_nfsshare_inited = 1;
4902ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4903ecd6cf80Smarks 		}
4904da6c28aaSamw 		break;
4905da6c28aaSamw 	case ZFS_SHARE_SMB:
4906da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4907da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
4908da6c28aaSamw 			mutex_enter(&zfs_share_lock);
4909da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
4910da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
4911da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4912da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4913be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4914da6c28aaSamw 			}
4915da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4916da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4917faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
4918da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4919be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4920da6c28aaSamw 			}
4921da6c28aaSamw 			error = zfs_init_sharefs();
49223b2aab18SMatthew Ahrens 			if (error != 0) {
4923da6c28aaSamw 				mutex_exit(&zfs_share_lock);
4924be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
4925da6c28aaSamw 			}
4926da6c28aaSamw 			zfs_smbshare_inited = 1;
4927ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
4928ecd6cf80Smarks 		}
4929da6c28aaSamw 		break;
4930da6c28aaSamw 	default:
4931be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4932da6c28aaSamw 	}
4933ecd6cf80Smarks 
4934da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
4935da6c28aaSamw 	case ZFS_SHARE_NFS:
4936da6c28aaSamw 	case ZFS_UNSHARE_NFS:
4937da6c28aaSamw 		if (error =
4938da6c28aaSamw 		    znfsexport_fs((void *)
4939da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
4940da6c28aaSamw 			return (error);
4941da6c28aaSamw 		break;
4942da6c28aaSamw 	case ZFS_SHARE_SMB:
4943da6c28aaSamw 	case ZFS_UNSHARE_SMB:
4944da6c28aaSamw 		if (error = zsmbexport_fs((void *)
4945da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
4946da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4947743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
4948da6c28aaSamw 			return (error);
4949ecd6cf80Smarks 		}
4950da6c28aaSamw 		break;
4951ecd6cf80Smarks 	}
4952ecd6cf80Smarks 
4953da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4954da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4955ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
4956ecd6cf80Smarks 
4957da6c28aaSamw 	/*
4958da6c28aaSamw 	 * Add or remove share from sharetab
4959da6c28aaSamw 	 */
4960ecd6cf80Smarks 	error = zshare_fs(opcode,
4961ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4962ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
4963ecd6cf80Smarks 
4964ecd6cf80Smarks 	return (error);
4965ecd6cf80Smarks 
4966ecd6cf80Smarks }
4967ecd6cf80Smarks 
4968743a77edSAlan Wright ace_t full_access[] = {
4969743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4970743a77edSAlan Wright };
4971743a77edSAlan Wright 
497299d5e173STim Haley /*
497399d5e173STim Haley  * inputs:
497499d5e173STim Haley  * zc_name		name of containing filesystem
497599d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
497699d5e173STim Haley  *
497799d5e173STim Haley  * outputs:
497899d5e173STim Haley  * zc_obj		next in-use object #
497999d5e173STim Haley  */
498099d5e173STim Haley static int
498199d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
498299d5e173STim Haley {
498399d5e173STim Haley 	objset_t *os = NULL;
498499d5e173STim Haley 	int error;
498599d5e173STim Haley 
498699d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
49873b2aab18SMatthew Ahrens 	if (error != 0)
498899d5e173STim Haley 		return (error);
498999d5e173STim Haley 
499099d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4991c1379625SJustin T. Gibbs 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
499299d5e173STim Haley 
499399d5e173STim Haley 	dmu_objset_rele(os, FTAG);
499499d5e173STim Haley 	return (error);
499599d5e173STim Haley }
499699d5e173STim Haley 
499799d5e173STim Haley /*
499899d5e173STim Haley  * inputs:
499999d5e173STim Haley  * zc_name		name of filesystem
500099d5e173STim Haley  * zc_value		prefix name for snapshot
500199d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
500299d5e173STim Haley  *
500399d5e173STim Haley  * outputs:
50044445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
500599d5e173STim Haley  */
500699d5e173STim Haley static int
500799d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
500899d5e173STim Haley {
500999d5e173STim Haley 	char *snap_name;
50103b2aab18SMatthew Ahrens 	char *hold_name;
501199d5e173STim Haley 	int error;
50123b2aab18SMatthew Ahrens 	minor_t minor;
501399d5e173STim Haley 
50143b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
50153b2aab18SMatthew Ahrens 	if (error != 0)
501699d5e173STim Haley 		return (error);
501799d5e173STim Haley 
50183b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
50193b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
50203b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
50213b2aab18SMatthew Ahrens 
50223b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
50233b2aab18SMatthew Ahrens 	    hold_name);
50243b2aab18SMatthew Ahrens 	if (error == 0)
50253b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
502699d5e173STim Haley 	strfree(snap_name);
50273b2aab18SMatthew Ahrens 	strfree(hold_name);
50283b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
50293b2aab18SMatthew Ahrens 	return (error);
503099d5e173STim Haley }
503199d5e173STim Haley 
503299d5e173STim Haley /*
503399d5e173STim Haley  * inputs:
503499d5e173STim Haley  * zc_name		name of "to" snapshot
503599d5e173STim Haley  * zc_value		name of "from" snapshot
503699d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
503799d5e173STim Haley  *
503899d5e173STim Haley  * outputs:
503999d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
504099d5e173STim Haley  */
504199d5e173STim Haley static int
504299d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
504399d5e173STim Haley {
504499d5e173STim Haley 	file_t *fp;
504599d5e173STim Haley 	offset_t off;
504699d5e173STim Haley 	int error;
504799d5e173STim Haley 
504899d5e173STim Haley 	fp = getf(zc->zc_cookie);
50493b2aab18SMatthew Ahrens 	if (fp == NULL)
5050be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
505199d5e173STim Haley 
505299d5e173STim Haley 	off = fp->f_offset;
505399d5e173STim Haley 
50543b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
505599d5e173STim Haley 
505699d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
505799d5e173STim Haley 		fp->f_offset = off;
505899d5e173STim Haley 	releasef(zc->zc_cookie);
505999d5e173STim Haley 
506099d5e173STim Haley 	return (error);
506199d5e173STim Haley }
506299d5e173STim Haley 
5063743a77edSAlan Wright /*
5064743a77edSAlan Wright  * Remove all ACL files in shares dir
5065743a77edSAlan Wright  */
5066743a77edSAlan Wright static int
5067743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5068743a77edSAlan Wright {
5069743a77edSAlan Wright 	zap_cursor_t	zc;
5070743a77edSAlan Wright 	zap_attribute_t	zap;
5071743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5072743a77edSAlan Wright 	int error;
5073743a77edSAlan Wright 
5074743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5075743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5076743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
5077743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5078743a77edSAlan Wright 		    NULL, 0)) != 0)
5079743a77edSAlan Wright 			break;
5080743a77edSAlan Wright 	}
5081743a77edSAlan Wright 	zap_cursor_fini(&zc);
5082743a77edSAlan Wright 	return (error);
5083743a77edSAlan Wright }
5084743a77edSAlan Wright 
5085743a77edSAlan Wright static int
5086743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5087743a77edSAlan Wright {
5088743a77edSAlan Wright 	vnode_t *vp;
5089743a77edSAlan Wright 	znode_t *dzp;
5090743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
5091743a77edSAlan Wright 	znode_t *sharedir;
5092743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
5093743a77edSAlan Wright 	nvlist_t *nvlist;
5094743a77edSAlan Wright 	char *src, *target;
5095743a77edSAlan Wright 	vattr_t vattr;
5096743a77edSAlan Wright 	vsecattr_t vsec;
5097743a77edSAlan Wright 	int error = 0;
5098743a77edSAlan Wright 
5099743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5100743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
5101743a77edSAlan Wright 		return (error);
5102743a77edSAlan Wright 
5103743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
5104743a77edSAlan Wright 
5105743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5106743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5107743a77edSAlan Wright 	    zc->zc_name) != 0)) {
5108743a77edSAlan Wright 		VN_RELE(vp);
5109be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5110743a77edSAlan Wright 	}
5111743a77edSAlan Wright 
5112743a77edSAlan Wright 	dzp = VTOZ(vp);
5113743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
5114743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
5115743a77edSAlan Wright 
51169e1320c0SMark Shellenbaum 	/*
51179e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
51189e1320c0SMark Shellenbaum 	 */
51199e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
51209e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
51219e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
51229e1320c0SMark Shellenbaum 
51239e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
51249e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
51259e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
51269e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
51279e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
51283b2aab18SMatthew Ahrens 		if (error != 0) {
51299e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
51309e1320c0SMark Shellenbaum 		} else {
51319e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
51329e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
51339e1320c0SMark Shellenbaum 		}
51343b2aab18SMatthew Ahrens 		if (error != 0) {
51359e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
51369e1320c0SMark Shellenbaum 			VN_RELE(vp);
51379e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
51389e1320c0SMark Shellenbaum 			return (error);
51399e1320c0SMark Shellenbaum 		}
51409e1320c0SMark Shellenbaum 	}
51419e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
51429e1320c0SMark Shellenbaum 
51439e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5144743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
51459e1320c0SMark Shellenbaum 		VN_RELE(vp);
5146743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5147743a77edSAlan Wright 		return (error);
5148743a77edSAlan Wright 	}
5149743a77edSAlan Wright 
5150743a77edSAlan Wright 	switch (zc->zc_cookie) {
5151743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5152743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5153743a77edSAlan Wright 		vattr.va_type = VREG;
5154743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5155743a77edSAlan Wright 		vattr.va_uid = 0;
5156743a77edSAlan Wright 		vattr.va_gid = 0;
5157743a77edSAlan Wright 
5158743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5159743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5160743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5161743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5162743a77edSAlan Wright 
5163743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5164743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5165743a77edSAlan Wright 		if (resourcevp)
5166743a77edSAlan Wright 			VN_RELE(resourcevp);
5167743a77edSAlan Wright 		break;
5168743a77edSAlan Wright 
5169743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5170743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5171743a77edSAlan Wright 		    NULL, 0);
5172743a77edSAlan Wright 		break;
5173743a77edSAlan Wright 
5174743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5175743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5176478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5177743a77edSAlan Wright 			VN_RELE(vp);
51788f5190a5SDan McDonald 			VN_RELE(ZTOV(sharedir));
5179743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5180743a77edSAlan Wright 			return (error);
5181743a77edSAlan Wright 		}
5182743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5183743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5184743a77edSAlan Wright 		    &target)) {
5185743a77edSAlan Wright 			VN_RELE(vp);
518689459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5187743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
51881195e687SMark J Musante 			nvlist_free(nvlist);
5189743a77edSAlan Wright 			return (error);
5190743a77edSAlan Wright 		}
5191743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5192743a77edSAlan Wright 		    kcred, NULL, 0);
5193743a77edSAlan Wright 		nvlist_free(nvlist);
5194743a77edSAlan Wright 		break;
5195743a77edSAlan Wright 
5196743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5197743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5198743a77edSAlan Wright 		break;
5199743a77edSAlan Wright 
5200743a77edSAlan Wright 	default:
5201be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5202743a77edSAlan Wright 		break;
5203743a77edSAlan Wright 	}
5204743a77edSAlan Wright 
5205743a77edSAlan Wright 	VN_RELE(vp);
5206743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5207743a77edSAlan Wright 
5208743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5209743a77edSAlan Wright 
5210743a77edSAlan Wright 	return (error);
5211743a77edSAlan Wright }
5212743a77edSAlan Wright 
5213842727c2SChris Kirby /*
52143b2aab18SMatthew Ahrens  * innvl: {
52153b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
52163b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
52173b2aab18SMatthew Ahrens  * }
5218842727c2SChris Kirby  *
52193b2aab18SMatthew Ahrens  * outnvl: {
52203b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
52213b2aab18SMatthew Ahrens  *     ...
52223b2aab18SMatthew Ahrens  * }
5223842727c2SChris Kirby  */
52243b2aab18SMatthew Ahrens /* ARGSUSED */
5225842727c2SChris Kirby static int
52263b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5227842727c2SChris Kirby {
5228752fd8daSJosef 'Jeff' Sipek 	nvpair_t *pair;
52293b2aab18SMatthew Ahrens 	nvlist_t *holds;
52303b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5231a7f53a56SChris Kirby 	int error;
5232a7f53a56SChris Kirby 	minor_t minor = 0;
5233842727c2SChris Kirby 
52343b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
52353b2aab18SMatthew Ahrens 	if (error != 0)
5236be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5237a7f53a56SChris Kirby 
5238752fd8daSJosef 'Jeff' Sipek 	/* make sure the user didn't pass us any invalid (empty) tags */
5239752fd8daSJosef 'Jeff' Sipek 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5240752fd8daSJosef 'Jeff' Sipek 	    pair = nvlist_next_nvpair(holds, pair)) {
5241752fd8daSJosef 'Jeff' Sipek 		char *htag;
5242752fd8daSJosef 'Jeff' Sipek 
5243752fd8daSJosef 'Jeff' Sipek 		error = nvpair_value_string(pair, &htag);
5244752fd8daSJosef 'Jeff' Sipek 		if (error != 0)
5245752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(error));
5246752fd8daSJosef 'Jeff' Sipek 
5247752fd8daSJosef 'Jeff' Sipek 		if (strlen(htag) == 0)
5248752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(EINVAL));
5249752fd8daSJosef 'Jeff' Sipek 	}
5250752fd8daSJosef 'Jeff' Sipek 
52513b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
52523b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
52533b2aab18SMatthew Ahrens 		if (error != 0)
5254a7f53a56SChris Kirby 			return (error);
5255a7f53a56SChris Kirby 	}
5256a7f53a56SChris Kirby 
52573b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
52583b2aab18SMatthew Ahrens 	if (minor != 0)
52593b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5260a7f53a56SChris Kirby 	return (error);
5261842727c2SChris Kirby }
5262842727c2SChris Kirby 
5263842727c2SChris Kirby /*
52643b2aab18SMatthew Ahrens  * innvl is not used.
5265842727c2SChris Kirby  *
52663b2aab18SMatthew Ahrens  * outnvl: {
52673b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
52683b2aab18SMatthew Ahrens  *    ...
52693b2aab18SMatthew Ahrens  * }
5270842727c2SChris Kirby  */
52713b2aab18SMatthew Ahrens /* ARGSUSED */
5272842727c2SChris Kirby static int
52733b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5274842727c2SChris Kirby {
52753b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5276842727c2SChris Kirby }
5277842727c2SChris Kirby 
5278842727c2SChris Kirby /*
52793b2aab18SMatthew Ahrens  * innvl: {
52803b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
52813b2aab18SMatthew Ahrens  *     ...
52823b2aab18SMatthew Ahrens  * }
5283842727c2SChris Kirby  *
52843b2aab18SMatthew Ahrens  * outnvl: {
52853b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
52863b2aab18SMatthew Ahrens  *     ...
52873b2aab18SMatthew Ahrens  * }
5288842727c2SChris Kirby  */
52893b2aab18SMatthew Ahrens /* ARGSUSED */
5290842727c2SChris Kirby static int
52913b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5292842727c2SChris Kirby {
52933b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5294842727c2SChris Kirby }
5295842727c2SChris Kirby 
529619b94df9SMatthew Ahrens /*
529719b94df9SMatthew Ahrens  * inputs:
529819b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
529919b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
530019b94df9SMatthew Ahrens  *
530119b94df9SMatthew Ahrens  * outputs:
530219b94df9SMatthew Ahrens  * zc_cookie		space in bytes
530319b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
530419b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
530519b94df9SMatthew Ahrens  */
530619b94df9SMatthew Ahrens static int
530719b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
530819b94df9SMatthew Ahrens {
530919b94df9SMatthew Ahrens 	int error;
53103b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
531119b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
531219b94df9SMatthew Ahrens 
53133b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
531419b94df9SMatthew Ahrens 	if (error != 0)
531519b94df9SMatthew Ahrens 		return (error);
53163b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
53173b2aab18SMatthew Ahrens 	if (error != 0) {
53183b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
53193b2aab18SMatthew Ahrens 		return (error);
53203b2aab18SMatthew Ahrens 	}
53213b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
532219b94df9SMatthew Ahrens 	if (error != 0) {
532319b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
53243b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
532519b94df9SMatthew Ahrens 		return (error);
532619b94df9SMatthew Ahrens 	}
532719b94df9SMatthew Ahrens 
532819b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
532919b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
533019b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
533119b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
53323b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
533319b94df9SMatthew Ahrens 	return (error);
533419b94df9SMatthew Ahrens }
53353b2aab18SMatthew Ahrens 
533619b94df9SMatthew Ahrens /*
53374445fffbSMatthew Ahrens  * innvl: {
53384445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
53394445fffbSMatthew Ahrens  * }
534019b94df9SMatthew Ahrens  *
53414445fffbSMatthew Ahrens  * outnvl: {
53424445fffbSMatthew Ahrens  *     "used" -> space in bytes
53434445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
53444445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
53454445fffbSMatthew Ahrens  * }
534619b94df9SMatthew Ahrens  */
534719b94df9SMatthew Ahrens static int
53484445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
534919b94df9SMatthew Ahrens {
535019b94df9SMatthew Ahrens 	int error;
53513b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
535219b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
53534445fffbSMatthew Ahrens 	char *firstsnap;
53544445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
535519b94df9SMatthew Ahrens 
53564445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5357be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
53584445fffbSMatthew Ahrens 
53593b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
536019b94df9SMatthew Ahrens 	if (error != 0)
536119b94df9SMatthew Ahrens 		return (error);
53623b2aab18SMatthew Ahrens 
53633b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
536424218bebSAndriy Gapon 	if (error == 0 && !new->ds_is_snapshot) {
536524218bebSAndriy Gapon 		dsl_dataset_rele(new, FTAG);
536624218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
536724218bebSAndriy Gapon 	}
53683b2aab18SMatthew Ahrens 	if (error != 0) {
53693b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
53703b2aab18SMatthew Ahrens 		return (error);
53713b2aab18SMatthew Ahrens 	}
53723b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
537324218bebSAndriy Gapon 	if (error == 0 && !old->ds_is_snapshot) {
537424218bebSAndriy Gapon 		dsl_dataset_rele(old, FTAG);
537524218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
537624218bebSAndriy Gapon 	}
537719b94df9SMatthew Ahrens 	if (error != 0) {
537819b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
53793b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
538019b94df9SMatthew Ahrens 		return (error);
538119b94df9SMatthew Ahrens 	}
538219b94df9SMatthew Ahrens 
53834445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
538419b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
538519b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
53863b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
53874445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
53884445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
53894445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
539019b94df9SMatthew Ahrens 	return (error);
539119b94df9SMatthew Ahrens }
539219b94df9SMatthew Ahrens 
5393ecd6cf80Smarks /*
53944445fffbSMatthew Ahrens  * innvl: {
53954445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
53964445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
5397b5152584SMatthew Ahrens  *     (optional) "largeblockok" -> (value ignored)
5398b5152584SMatthew Ahrens  *         indicates that blocks > 128KB are permitted
53995d7b4d43SMatthew Ahrens  *     (optional) "embedok" -> (value ignored)
54005d7b4d43SMatthew Ahrens  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
54019c3fd121SMatthew Ahrens  *     (optional) "resume_object" and "resume_offset" -> (uint64)
54029c3fd121SMatthew Ahrens  *         if present, resume send stream from specified object and offset.
54034445fffbSMatthew Ahrens  * }
54044445fffbSMatthew Ahrens  *
54054445fffbSMatthew Ahrens  * outnvl is unused
5406ecd6cf80Smarks  */
54074445fffbSMatthew Ahrens /* ARGSUSED */
54084445fffbSMatthew Ahrens static int
54094445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
54104445fffbSMatthew Ahrens {
54114445fffbSMatthew Ahrens 	int error;
54124445fffbSMatthew Ahrens 	offset_t off;
54133b2aab18SMatthew Ahrens 	char *fromname = NULL;
54144445fffbSMatthew Ahrens 	int fd;
5415b5152584SMatthew Ahrens 	boolean_t largeblockok;
54165d7b4d43SMatthew Ahrens 	boolean_t embedok;
54179c3fd121SMatthew Ahrens 	uint64_t resumeobj = 0;
54189c3fd121SMatthew Ahrens 	uint64_t resumeoff = 0;
54194445fffbSMatthew Ahrens 
54204445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
54214445fffbSMatthew Ahrens 	if (error != 0)
5422be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
54234445fffbSMatthew Ahrens 
54243b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
54254445fffbSMatthew Ahrens 
5426b5152584SMatthew Ahrens 	largeblockok = nvlist_exists(innvl, "largeblockok");
54275d7b4d43SMatthew Ahrens 	embedok = nvlist_exists(innvl, "embedok");
54285d7b4d43SMatthew Ahrens 
54299c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
54309c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
54319c3fd121SMatthew Ahrens 
54324445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
54333b2aab18SMatthew Ahrens 	if (fp == NULL)
5434be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
54354445fffbSMatthew Ahrens 
54364445fffbSMatthew Ahrens 	off = fp->f_offset;
54379c3fd121SMatthew Ahrens 	error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
54389c3fd121SMatthew Ahrens 	    resumeobj, resumeoff, fp->f_vnode, &off);
54394445fffbSMatthew Ahrens 
54404445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
54414445fffbSMatthew Ahrens 		fp->f_offset = off;
54424445fffbSMatthew Ahrens 	releasef(fd);
54434445fffbSMatthew Ahrens 	return (error);
54444445fffbSMatthew Ahrens }
54454445fffbSMatthew Ahrens 
54464445fffbSMatthew Ahrens /*
54474445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
54484445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
54494445fffbSMatthew Ahrens  *
54504445fffbSMatthew Ahrens  * innvl: {
5451643da460SMax Grossman  *     (optional) "from" -> full snap or bookmark name to send an incremental
5452643da460SMax Grossman  *                          from
54534445fffbSMatthew Ahrens  * }
54544445fffbSMatthew Ahrens  *
54554445fffbSMatthew Ahrens  * outnvl: {
54564445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
54574445fffbSMatthew Ahrens  * }
54584445fffbSMatthew Ahrens  */
54594445fffbSMatthew Ahrens static int
54604445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
54614445fffbSMatthew Ahrens {
54623b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
54633b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
54644445fffbSMatthew Ahrens 	int error;
54654445fffbSMatthew Ahrens 	char *fromname;
54664445fffbSMatthew Ahrens 	uint64_t space;
54674445fffbSMatthew Ahrens 
54683b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
54693b2aab18SMatthew Ahrens 	if (error != 0)
54703b2aab18SMatthew Ahrens 		return (error);
54713b2aab18SMatthew Ahrens 
54723b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
54733b2aab18SMatthew Ahrens 	if (error != 0) {
54743b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
54754445fffbSMatthew Ahrens 		return (error);
54763b2aab18SMatthew Ahrens 	}
54774445fffbSMatthew Ahrens 
5478643da460SMax Grossman 	error = nvlist_lookup_string(innvl, "from", &fromname);
54794445fffbSMatthew Ahrens 	if (error == 0) {
5480643da460SMax Grossman 		if (strchr(fromname, '@') != NULL) {
5481643da460SMax Grossman 			/*
5482643da460SMax Grossman 			 * If from is a snapshot, hold it and use the more
5483643da460SMax Grossman 			 * efficient dmu_send_estimate to estimate send space
5484643da460SMax Grossman 			 * size using deadlists.
5485643da460SMax Grossman 			 */
5486643da460SMax Grossman 			dsl_dataset_t *fromsnap;
5487643da460SMax Grossman 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5488643da460SMax Grossman 			if (error != 0)
5489643da460SMax Grossman 				goto out;
5490643da460SMax Grossman 			error = dmu_send_estimate(tosnap, fromsnap, &space);
5491643da460SMax Grossman 			dsl_dataset_rele(fromsnap, FTAG);
5492643da460SMax Grossman 		} else if (strchr(fromname, '#') != NULL) {
5493643da460SMax Grossman 			/*
5494643da460SMax Grossman 			 * If from is a bookmark, fetch the creation TXG of the
5495643da460SMax Grossman 			 * snapshot it was created from and use that to find
5496643da460SMax Grossman 			 * blocks that were born after it.
5497643da460SMax Grossman 			 */
5498643da460SMax Grossman 			zfs_bookmark_phys_t frombm;
5499643da460SMax Grossman 
5500643da460SMax Grossman 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
5501643da460SMax Grossman 			    &frombm);
5502643da460SMax Grossman 			if (error != 0)
5503643da460SMax Grossman 				goto out;
5504643da460SMax Grossman 			error = dmu_send_estimate_from_txg(tosnap,
5505643da460SMax Grossman 			    frombm.zbm_creation_txg, &space);
5506643da460SMax Grossman 		} else {
5507643da460SMax Grossman 			/*
5508643da460SMax Grossman 			 * from is not properly formatted as a snapshot or
5509643da460SMax Grossman 			 * bookmark
5510643da460SMax Grossman 			 */
5511643da460SMax Grossman 			error = SET_ERROR(EINVAL);
5512643da460SMax Grossman 			goto out;
55134445fffbSMatthew Ahrens 		}
5514643da460SMax Grossman 	} else {
5515643da460SMax Grossman 		// If estimating the size of a full send, use dmu_send_estimate
5516643da460SMax Grossman 		error = dmu_send_estimate(tosnap, NULL, &space);
55174445fffbSMatthew Ahrens 	}
55184445fffbSMatthew Ahrens 
55194445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
55204445fffbSMatthew Ahrens 
5521643da460SMax Grossman out:
55223b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
55233b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
55244445fffbSMatthew Ahrens 	return (error);
55254445fffbSMatthew Ahrens }
55264445fffbSMatthew Ahrens 
55274445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
55284445fffbSMatthew Ahrens 
55294445fffbSMatthew Ahrens static void
55304445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55314445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
55324445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
55334445fffbSMatthew Ahrens {
55344445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
55354445fffbSMatthew Ahrens 
55364445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
55374445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
55384445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
55394445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
55404445fffbSMatthew Ahrens 
55414445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
55424445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
55434445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
55444445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
55454445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
55464445fffbSMatthew Ahrens }
55474445fffbSMatthew Ahrens 
55484445fffbSMatthew Ahrens /*
55494445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
55504445fffbSMatthew Ahrens  * each argument to this function.
55514445fffbSMatthew Ahrens  */
55524445fffbSMatthew Ahrens static void
55534445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
55544445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
55554445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
55564445fffbSMatthew Ahrens     boolean_t allow_log)
55574445fffbSMatthew Ahrens {
55584445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
55594445fffbSMatthew Ahrens 
55604445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
55614445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
55624445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
55634445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
55644445fffbSMatthew Ahrens 
55654445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
55664445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
55674445fffbSMatthew Ahrens 
55684445fffbSMatthew Ahrens 	vec->zvec_name = name;
55694445fffbSMatthew Ahrens 	vec->zvec_func = func;
55704445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
55714445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
55724445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
55734445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
55744445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
55754445fffbSMatthew Ahrens }
55764445fffbSMatthew Ahrens 
55774445fffbSMatthew Ahrens static void
55784445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55794445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
55804445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
55814445fffbSMatthew Ahrens {
55824445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
55834445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
55844445fffbSMatthew Ahrens }
55854445fffbSMatthew Ahrens 
55864445fffbSMatthew Ahrens static void
55874445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
55884445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
55894445fffbSMatthew Ahrens {
55904445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
55914445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
55924445fffbSMatthew Ahrens }
55934445fffbSMatthew Ahrens 
55944445fffbSMatthew Ahrens static void
55954445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
55964445fffbSMatthew Ahrens {
55974445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
55984445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
55994445fffbSMatthew Ahrens }
56004445fffbSMatthew Ahrens 
56014445fffbSMatthew Ahrens static void
56024445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56034445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
56044445fffbSMatthew Ahrens {
56054445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56064445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
56074445fffbSMatthew Ahrens }
56084445fffbSMatthew Ahrens 
56094445fffbSMatthew Ahrens static void
56104445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
56114445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
56124445fffbSMatthew Ahrens {
56134445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56144445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
56154445fffbSMatthew Ahrens }
56164445fffbSMatthew Ahrens 
56174445fffbSMatthew Ahrens static void
56184445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
56194445fffbSMatthew Ahrens {
56204445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
56214445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
56224445fffbSMatthew Ahrens }
56234445fffbSMatthew Ahrens 
56244445fffbSMatthew Ahrens static void
56254445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
56269a686fbcSPaul Dagnelie     zfs_secpolicy_func_t *secpolicy)
56274445fffbSMatthew Ahrens {
56284445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
56294445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
56304445fffbSMatthew Ahrens }
56314445fffbSMatthew Ahrens 
56324445fffbSMatthew Ahrens static void
56334445fffbSMatthew Ahrens zfs_ioctl_init(void)
56344445fffbSMatthew Ahrens {
56354445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
56364445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
56374445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56384445fffbSMatthew Ahrens 
56394445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
56404445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
56414445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
56424445fffbSMatthew Ahrens 
56434445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
56444445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
56454445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56464445fffbSMatthew Ahrens 
56474445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
56484445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
56494445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56504445fffbSMatthew Ahrens 
56514445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
56524445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
56534445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56544445fffbSMatthew Ahrens 
56554445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
56564445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
56574445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56584445fffbSMatthew Ahrens 
56594445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
56604445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
56614445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56624445fffbSMatthew Ahrens 
56634445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
56644445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
56654445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56664445fffbSMatthew Ahrens 
56673b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
56683b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
56693b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56703b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
56713b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
56723b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
56733b2aab18SMatthew Ahrens 
56743b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
56753b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
56763b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
56773b2aab18SMatthew Ahrens 
5678a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5679a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5680a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5681a7027df1SMatthew Ahrens 
568278f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
568378f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
568478f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
568578f17100SMatthew Ahrens 
568678f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
568778f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
568878f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
568978f17100SMatthew Ahrens 
569078f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
569178f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
569278f17100SMatthew Ahrens 	    POOL_NAME,
569378f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
569478f17100SMatthew Ahrens 
56954445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
56964445fffbSMatthew Ahrens 
56974445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
56984445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
56994445fffbSMatthew Ahrens 
57004445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
57014445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57024445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
57034445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
57044445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
57054445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
57064445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
57074445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
57084445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
57094445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
57104445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
57114445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
57124445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
57134445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
57144445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
57154445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
57164445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
57174445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
57184445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
57194445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
57204445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
57214445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
57224445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
57234445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
57244445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
57254445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
57264445fffbSMatthew Ahrens 
57274445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
57284445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
57294445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
57304445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
57314445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
57324445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
57334445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
57344445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
57354445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
57364445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
57374445fffbSMatthew Ahrens 
57384445fffbSMatthew Ahrens 	/*
57394445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
57404445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
57414445fffbSMatthew Ahrens 	 * does the logging of those commands.
57424445fffbSMatthew Ahrens 	 */
57434445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
57444445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
57454445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
57464445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
57474445fffbSMatthew Ahrens 
57484445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
57494445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
57504445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
57514445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
57524445fffbSMatthew Ahrens 
57534445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
57544445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
57554445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
57564445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
57574445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
57584445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
57594445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
57604445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
57614445fffbSMatthew Ahrens 
57624445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
57634445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57644445fffbSMatthew Ahrens 
57654445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
576622e30981SGeorge Wilson 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
57674445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
57684445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
57694445fffbSMatthew Ahrens 
57704445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
57714445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
57724445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
57734445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
57744445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
57754445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
57764445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
57774445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
57784445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
57794445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
57804445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
57814445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
57824445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
57834445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
57844445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
57854445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
57864445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
57874445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
57884445fffbSMatthew Ahrens 
57894445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
57904445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
57914445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
57924445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
57934445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
57944445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
57954445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
57964445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
57974445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
57984445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
57994445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
58004445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
58014445fffbSMatthew Ahrens 
58024445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
58034445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
58044445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
58054445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
58064445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
58074445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
58084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
58094445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
58104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
58114445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
58124445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
58134445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
58144445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
58154445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
58164445fffbSMatthew Ahrens 
58174445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
58184445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
58194445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
58204445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
58214445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
58224445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
58234445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58244445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
58254445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
58264445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58274445fffbSMatthew Ahrens }
5828fa9e4066Sahrens 
582954d692b7SGeorge Wilson int
5830f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5831f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
583254d692b7SGeorge Wilson {
583354d692b7SGeorge Wilson 	spa_t *spa;
583454d692b7SGeorge Wilson 	int error;
583554d692b7SGeorge Wilson 
583654d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
583754d692b7SGeorge Wilson 
5838f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
5839f9af39baSGeorge Wilson 		return (0);
5840f9af39baSGeorge Wilson 
584114843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
584254d692b7SGeorge Wilson 	if (error == 0) {
5843f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5844be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
5845f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5846be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
584754d692b7SGeorge Wilson 		spa_close(spa, FTAG);
584854d692b7SGeorge Wilson 	}
584954d692b7SGeorge Wilson 	return (error);
585054d692b7SGeorge Wilson }
585154d692b7SGeorge Wilson 
5852c99e4bdcSChris Kirby /*
5853c99e4bdcSChris Kirby  * Find a free minor number.
5854c99e4bdcSChris Kirby  */
5855c99e4bdcSChris Kirby minor_t
5856c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
5857c99e4bdcSChris Kirby {
5858c99e4bdcSChris Kirby 	static minor_t last_minor;
5859c99e4bdcSChris Kirby 	minor_t m;
5860c99e4bdcSChris Kirby 
5861c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5862c99e4bdcSChris Kirby 
5863c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
5864c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
5865c99e4bdcSChris Kirby 			m = 1;
5866c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5867c99e4bdcSChris Kirby 			last_minor = m;
5868c99e4bdcSChris Kirby 			return (m);
5869c99e4bdcSChris Kirby 		}
5870c99e4bdcSChris Kirby 	}
5871c99e4bdcSChris Kirby 
5872c99e4bdcSChris Kirby 	return (0);
5873c99e4bdcSChris Kirby }
5874c99e4bdcSChris Kirby 
5875c99e4bdcSChris Kirby static int
5876c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
5877c99e4bdcSChris Kirby {
5878c99e4bdcSChris Kirby 	minor_t minor;
5879c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
5880c99e4bdcSChris Kirby 
5881c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5882c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
5883c99e4bdcSChris Kirby 
5884c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
5885c99e4bdcSChris Kirby 	if (minor == 0)
5886be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
5887c99e4bdcSChris Kirby 
5888c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5889be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
5890c99e4bdcSChris Kirby 
5891c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
5892c99e4bdcSChris Kirby 
5893c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
5894c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
5895c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5896c99e4bdcSChris Kirby 
5897c99e4bdcSChris Kirby 	return (0);
5898c99e4bdcSChris Kirby }
5899c99e4bdcSChris Kirby 
5900c99e4bdcSChris Kirby static void
5901c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5902c99e4bdcSChris Kirby {
5903c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5904c99e4bdcSChris Kirby 
5905c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
5906c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
5907c99e4bdcSChris Kirby }
5908c99e4bdcSChris Kirby 
5909c99e4bdcSChris Kirby void *
5910c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5911c99e4bdcSChris Kirby {
5912c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
5913c99e4bdcSChris Kirby 
5914c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
5915c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
5916c99e4bdcSChris Kirby 		return (NULL);
5917c99e4bdcSChris Kirby 
5918c99e4bdcSChris Kirby 	return (zp->zss_data);
5919c99e4bdcSChris Kirby }
5920c99e4bdcSChris Kirby 
5921c99e4bdcSChris Kirby static int
5922c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5923c99e4bdcSChris Kirby {
5924c99e4bdcSChris Kirby 	int error = 0;
5925c99e4bdcSChris Kirby 
5926c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
5927c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
5928c99e4bdcSChris Kirby 
5929c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
5930c99e4bdcSChris Kirby 	if (flag & FEXCL) {
5931c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
5932c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
5933c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5934c99e4bdcSChris Kirby 	}
5935c99e4bdcSChris Kirby 
5936c99e4bdcSChris Kirby 	return (error);
5937c99e4bdcSChris Kirby }
5938c99e4bdcSChris Kirby 
5939c99e4bdcSChris Kirby static int
5940c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5941c99e4bdcSChris Kirby {
5942c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
5943c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
5944c99e4bdcSChris Kirby 
5945c99e4bdcSChris Kirby 	if (minor == 0)
5946c99e4bdcSChris Kirby 		return (0);
5947c99e4bdcSChris Kirby 
5948c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
5949c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5950c99e4bdcSChris Kirby 	if (zo == NULL) {
5951c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
5952c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
5953c99e4bdcSChris Kirby 	}
5954c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
5955c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
5956c99e4bdcSChris Kirby 
5957c99e4bdcSChris Kirby 	return (0);
5958c99e4bdcSChris Kirby }
5959c99e4bdcSChris Kirby 
5960fa9e4066Sahrens static int
5961fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5962fa9e4066Sahrens {
5963fa9e4066Sahrens 	zfs_cmd_t *zc;
59644445fffbSMatthew Ahrens 	uint_t vecnum;
59654445fffbSMatthew Ahrens 	int error, rc, len;
5966c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
59674445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
59684445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
59694445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
5970fa9e4066Sahrens 
5971c99e4bdcSChris Kirby 	if (minor != 0 &&
5972c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5973fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5974fa9e4066Sahrens 
59754445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
597691ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5977fa9e4066Sahrens 
59784445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5979be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
59804445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
5981fa9e4066Sahrens 
5982fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5983fa9e4066Sahrens 
5984478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
59854445fffbSMatthew Ahrens 	if (error != 0) {
5986be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
59874445fffbSMatthew Ahrens 		goto out;
59884445fffbSMatthew Ahrens 	}
5989fa9e4066Sahrens 
59904445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
59914445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
59924445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
59934445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
59944445fffbSMatthew Ahrens 		if (error != 0)
59954445fffbSMatthew Ahrens 			goto out;
59964445fffbSMatthew Ahrens 	}
5997fa9e4066Sahrens 
5998fa9e4066Sahrens 	/*
5999fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
6000fa9e4066Sahrens 	 * the lower layers.
6001fa9e4066Sahrens 	 */
60024445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
60034445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
60044445fffbSMatthew Ahrens 	case POOL_NAME:
60054445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6006be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
60074445fffbSMatthew Ahrens 		else
6008f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
60094445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
60104445fffbSMatthew Ahrens 		break;
6011fa9e4066Sahrens 
60124445fffbSMatthew Ahrens 	case DATASET_NAME:
60134445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6014be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
60154445fffbSMatthew Ahrens 		else
6016f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
60174445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
60184445fffbSMatthew Ahrens 		break;
60195ad82045Snd 
60204445fffbSMatthew Ahrens 	case NO_NAME:
60214445fffbSMatthew Ahrens 		break;
6022fa9e4066Sahrens 	}
6023fa9e4066Sahrens 
6024fa9e4066Sahrens 
60254445fffbSMatthew Ahrens 	if (error == 0 && !(flag & FKIOCTL))
60264445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
60274445fffbSMatthew Ahrens 
60284445fffbSMatthew Ahrens 	if (error != 0)
60294445fffbSMatthew Ahrens 		goto out;
60304445fffbSMatthew Ahrens 
60314445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
603278f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
60334445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
60344445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
60354445fffbSMatthew Ahrens 
60364445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
60374445fffbSMatthew Ahrens 		nvlist_t *outnvl;
60384445fffbSMatthew Ahrens 		int puterror = 0;
60394445fffbSMatthew Ahrens 		spa_t *spa;
60404445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
60414445fffbSMatthew Ahrens 
60424445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
60434445fffbSMatthew Ahrens 
60444445fffbSMatthew Ahrens 		/*
60454445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
60464445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
60474445fffbSMatthew Ahrens 		 */
60484445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
60494445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
60504445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
60514445fffbSMatthew Ahrens 			    vec->zvec_name);
60524445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
60534445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
60544445fffbSMatthew Ahrens 				    innvl);
60554445fffbSMatthew Ahrens 			}
60564445fffbSMatthew Ahrens 		}
60574445fffbSMatthew Ahrens 
60584445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
60594445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
60604445fffbSMatthew Ahrens 
60614445fffbSMatthew Ahrens 		if (error == 0 && vec->zvec_allow_log &&
60624445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
60634445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
60644445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
60654445fffbSMatthew Ahrens 				    outnvl);
60664445fffbSMatthew Ahrens 			}
60674445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
60684445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
60694445fffbSMatthew Ahrens 		}
60704445fffbSMatthew Ahrens 		fnvlist_free(lognv);
60714445fffbSMatthew Ahrens 
60724445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
60734445fffbSMatthew Ahrens 			int smusherror = 0;
60744445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
60754445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
60764445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
60774445fffbSMatthew Ahrens 			}
60784445fffbSMatthew Ahrens 			if (smusherror == 0)
60794445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
60804445fffbSMatthew Ahrens 		}
60814445fffbSMatthew Ahrens 
60824445fffbSMatthew Ahrens 		if (puterror != 0)
60834445fffbSMatthew Ahrens 			error = puterror;
60844445fffbSMatthew Ahrens 
60854445fffbSMatthew Ahrens 		nvlist_free(outnvl);
60864445fffbSMatthew Ahrens 	} else {
60874445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
60884445fffbSMatthew Ahrens 	}
60894445fffbSMatthew Ahrens 
60904445fffbSMatthew Ahrens out:
60914445fffbSMatthew Ahrens 	nvlist_free(innvl);
6092478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
60934445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
6094be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
60954445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
60964445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
60974445fffbSMatthew Ahrens 		if (s != NULL)
60984445fffbSMatthew Ahrens 			strfree(s);
60994445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
61004445fffbSMatthew Ahrens 	} else {
61014445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
61024445fffbSMatthew Ahrens 			strfree(saved_poolname);
6103ecd6cf80Smarks 	}
6104fa9e4066Sahrens 
6105fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
6106fa9e4066Sahrens 	return (error);
6107fa9e4066Sahrens }
6108fa9e4066Sahrens 
6109fa9e4066Sahrens static int
6110fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6111fa9e4066Sahrens {
6112fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
6113fa9e4066Sahrens 		return (DDI_FAILURE);
6114fa9e4066Sahrens 
6115fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6116fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
6117fa9e4066Sahrens 		return (DDI_FAILURE);
6118fa9e4066Sahrens 
6119fa9e4066Sahrens 	zfs_dip = dip;
6120fa9e4066Sahrens 
6121fa9e4066Sahrens 	ddi_report_dev(dip);
6122fa9e4066Sahrens 
6123fa9e4066Sahrens 	return (DDI_SUCCESS);
6124fa9e4066Sahrens }
6125fa9e4066Sahrens 
6126fa9e4066Sahrens static int
6127fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6128fa9e4066Sahrens {
6129fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
6130fa9e4066Sahrens 		return (DDI_FAILURE);
6131fa9e4066Sahrens 
6132fa9e4066Sahrens 	if (cmd != DDI_DETACH)
6133fa9e4066Sahrens 		return (DDI_FAILURE);
6134fa9e4066Sahrens 
6135fa9e4066Sahrens 	zfs_dip = NULL;
6136fa9e4066Sahrens 
6137fa9e4066Sahrens 	ddi_prop_remove_all(dip);
6138fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
6139fa9e4066Sahrens 
6140fa9e4066Sahrens 	return (DDI_SUCCESS);
6141fa9e4066Sahrens }
6142fa9e4066Sahrens 
6143fa9e4066Sahrens /*ARGSUSED*/
6144fa9e4066Sahrens static int
6145fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6146fa9e4066Sahrens {
6147fa9e4066Sahrens 	switch (infocmd) {
6148fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
6149fa9e4066Sahrens 		*result = zfs_dip;
6150fa9e4066Sahrens 		return (DDI_SUCCESS);
6151fa9e4066Sahrens 
6152fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
6153a0965f35Sbonwick 		*result = (void *)0;
6154fa9e4066Sahrens 		return (DDI_SUCCESS);
6155fa9e4066Sahrens 	}
6156fa9e4066Sahrens 
6157fa9e4066Sahrens 	return (DDI_FAILURE);
6158fa9e4066Sahrens }
6159fa9e4066Sahrens 
6160fa9e4066Sahrens /*
6161fa9e4066Sahrens  * OK, so this is a little weird.
6162fa9e4066Sahrens  *
6163fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
6164fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6165fa9e4066Sahrens  *
6166fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
6167fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
6168fa9e4066Sahrens  */
6169fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
6170c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
6171c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
6172fa9e4066Sahrens 	zvol_strategy,	/* strategy */
6173fa9e4066Sahrens 	nodev,		/* print */
6174e7cbe64fSgw 	zvol_dump,	/* dump */
6175fa9e4066Sahrens 	zvol_read,	/* read */
6176fa9e4066Sahrens 	zvol_write,	/* write */
6177fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
6178fa9e4066Sahrens 	nodev,		/* devmap */
6179fa9e4066Sahrens 	nodev,		/* mmap */
6180fa9e4066Sahrens 	nodev,		/* segmap */
6181fa9e4066Sahrens 	nochpoll,	/* poll */
6182fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
6183fa9e4066Sahrens 	NULL,		/* streamtab */
6184fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
6185fa9e4066Sahrens 	CB_REV,		/* version */
6186feb08c6bSbillm 	nodev,		/* async read */
6187feb08c6bSbillm 	nodev,		/* async write */
6188fa9e4066Sahrens };
6189fa9e4066Sahrens 
6190fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
6191fa9e4066Sahrens 	DEVO_REV,	/* version */
6192fa9e4066Sahrens 	0,		/* refcnt */
6193fa9e4066Sahrens 	zfs_info,	/* info */
6194fa9e4066Sahrens 	nulldev,	/* identify */
6195fa9e4066Sahrens 	nulldev,	/* probe */
6196fa9e4066Sahrens 	zfs_attach,	/* attach */
6197fa9e4066Sahrens 	zfs_detach,	/* detach */
6198fa9e4066Sahrens 	nodev,		/* reset */
6199fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
620019397407SSherry Moore 	NULL,		/* no bus operations */
620119397407SSherry Moore 	NULL,		/* power */
620219397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
6203fa9e4066Sahrens };
6204fa9e4066Sahrens 
6205fa9e4066Sahrens static struct modldrv zfs_modldrv = {
620619397407SSherry Moore 	&mod_driverops,
620719397407SSherry Moore 	"ZFS storage pool",
620819397407SSherry Moore 	&zfs_dev_ops
6209fa9e4066Sahrens };
6210fa9e4066Sahrens 
6211fa9e4066Sahrens static struct modlinkage modlinkage = {
6212fa9e4066Sahrens 	MODREV_1,
6213fa9e4066Sahrens 	(void *)&zfs_modlfs,
6214fa9e4066Sahrens 	(void *)&zfs_modldrv,
6215fa9e4066Sahrens 	NULL
6216fa9e4066Sahrens };
6217fa9e4066Sahrens 
62184445fffbSMatthew Ahrens static void
62194445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
62204445fffbSMatthew Ahrens {
62214445fffbSMatthew Ahrens 	char *poolname = arg;
62224445fffbSMatthew Ahrens 	strfree(poolname);
62234445fffbSMatthew Ahrens }
6224ec533521Sfr 
6225fa9e4066Sahrens int
6226fa9e4066Sahrens _init(void)
6227fa9e4066Sahrens {
6228fa9e4066Sahrens 	int error;
6229fa9e4066Sahrens 
6230a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
6231a0965f35Sbonwick 	zfs_init();
6232a0965f35Sbonwick 	zvol_init();
62334445fffbSMatthew Ahrens 	zfs_ioctl_init();
6234a0965f35Sbonwick 
6235a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
6236a0965f35Sbonwick 		zvol_fini();
6237a0965f35Sbonwick 		zfs_fini();
6238a0965f35Sbonwick 		spa_fini();
6239fa9e4066Sahrens 		return (error);
6240a0965f35Sbonwick 	}
6241fa9e4066Sahrens 
6242ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
62434445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
62444445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6245ec533521Sfr 
6246fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6247fa9e4066Sahrens 	ASSERT(error == 0);
6248ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6249fa9e4066Sahrens 
6250fa9e4066Sahrens 	return (0);
6251fa9e4066Sahrens }
6252fa9e4066Sahrens 
6253fa9e4066Sahrens int
6254fa9e4066Sahrens _fini(void)
6255fa9e4066Sahrens {
6256fa9e4066Sahrens 	int error;
6257fa9e4066Sahrens 
6258ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6259be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6260fa9e4066Sahrens 
6261fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
6262fa9e4066Sahrens 		return (error);
6263fa9e4066Sahrens 
6264fa9e4066Sahrens 	zvol_fini();
6265fa9e4066Sahrens 	zfs_fini();
6266fa9e4066Sahrens 	spa_fini();
6267da6c28aaSamw 	if (zfs_nfsshare_inited)
6268ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
6269da6c28aaSamw 	if (zfs_smbshare_inited)
6270da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
6271da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
6272ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
6273fa9e4066Sahrens 
6274ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
6275fa9e4066Sahrens 	ldi_ident_release(zfs_li);
6276fa9e4066Sahrens 	zfs_li = NULL;
6277ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
6278fa9e4066Sahrens 
6279fa9e4066Sahrens 	return (error);
6280fa9e4066Sahrens }
6281fa9e4066Sahrens 
6282fa9e4066Sahrens int
6283fa9e4066Sahrens _info(struct modinfo *modinfop)
6284fa9e4066Sahrens {
6285fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
6286fa9e4066Sahrens }
6287