xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision c8811bd3e2427dddbac6c05a59cfe117d8fea370)
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.
240d8fa8f8SMartin 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.
2845b17475SAlex Wilson  * Copyright (c) 2014, 2016 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]
33*c8811bd3SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
344445fffbSMatthew Ahrens  */
354445fffbSMatthew Ahrens 
364445fffbSMatthew Ahrens /*
374445fffbSMatthew Ahrens  * ZFS ioctls.
384445fffbSMatthew Ahrens  *
394445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
404445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
414445fffbSMatthew Ahrens  *
424445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
434445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
444445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
454445fffbSMatthew Ahrens  *
464445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
474445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
484445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
494445fffbSMatthew Ahrens  *
504445fffbSMatthew Ahrens  * The registration arguments are as follows:
514445fffbSMatthew Ahrens  *
524445fffbSMatthew Ahrens  * const char *name
534445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
544445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
554445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
564445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
574445fffbSMatthew Ahrens  *
584445fffbSMatthew Ahrens  * zfs_ioc_t ioc
594445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
604445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
614445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
624445fffbSMatthew Ahrens  *
634445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
644445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
654445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
664445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
674445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
684445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
694445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
704445fffbSMatthew Ahrens  *
714445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
724445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
734445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
744445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
754445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
764445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
774445fffbSMatthew Ahrens  *   doesn't have invalid characters).
784445fffbSMatthew Ahrens  *
794445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
804445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
814445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
824445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
834445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
844445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
854445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
864445fffbSMatthew Ahrens  *
874445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
884445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
894445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
904445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
914445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
924445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
934445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
944445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
954445fffbSMatthew Ahrens  *
964445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
974445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
984445fffbSMatthew Ahrens  *
994445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
1004445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
1014445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
1024445fffbSMatthew Ahrens  *   will be called with the following arguments:
1034445fffbSMatthew Ahrens  *
1044445fffbSMatthew Ahrens  *   const char *name
1054445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1064445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1074445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1084445fffbSMatthew Ahrens  *
1094445fffbSMatthew Ahrens  *   nvlist_t *innvl
1104445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1114445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1124445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1134445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1144445fffbSMatthew Ahrens  *
1154445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1164445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1174445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1184445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1194445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1204445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1214445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1224445fffbSMatthew Ahrens  *
1234445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1244445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1254445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1264445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1274445fffbSMatthew Ahrens  *         the ioctl again.
1284445fffbSMatthew Ahrens  *
1294445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1304445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1314445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1324445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1334445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1344445fffbSMatthew Ahrens  *         deserialization failing.
135e9103aaeSGarrett D'Amore  */
136fa9e4066Sahrens 
137fa9e4066Sahrens #include <sys/types.h>
138fa9e4066Sahrens #include <sys/param.h>
139fa9e4066Sahrens #include <sys/errno.h>
140fa9e4066Sahrens #include <sys/uio.h>
141fa9e4066Sahrens #include <sys/buf.h>
142fa9e4066Sahrens #include <sys/modctl.h>
143fa9e4066Sahrens #include <sys/open.h>
144fa9e4066Sahrens #include <sys/file.h>
145fa9e4066Sahrens #include <sys/kmem.h>
146fa9e4066Sahrens #include <sys/conf.h>
147fa9e4066Sahrens #include <sys/cmn_err.h>
148fa9e4066Sahrens #include <sys/stat.h>
149fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1504201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
151da6c28aaSamw #include <sys/zfs_znode.h>
152fa9e4066Sahrens #include <sys/zap.h>
153fa9e4066Sahrens #include <sys/spa.h>
154b1b8ab34Slling #include <sys/spa_impl.h>
155fa9e4066Sahrens #include <sys/vdev.h>
1564201a95eSRic Aleshire #include <sys/priv_impl.h>
157fa9e4066Sahrens #include <sys/dmu.h>
158fa9e4066Sahrens #include <sys/dsl_dir.h>
159fa9e4066Sahrens #include <sys/dsl_dataset.h>
160fa9e4066Sahrens #include <sys/dsl_prop.h>
161ecd6cf80Smarks #include <sys/dsl_deleg.h>
162ecd6cf80Smarks #include <sys/dmu_objset.h>
1634e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1643b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
165fa9e4066Sahrens #include <sys/ddi.h>
166fa9e4066Sahrens #include <sys/sunddi.h>
167fa9e4066Sahrens #include <sys/sunldi.h>
168fa9e4066Sahrens #include <sys/policy.h>
169fa9e4066Sahrens #include <sys/zone.h>
170fa9e4066Sahrens #include <sys/nvpair.h>
171fa9e4066Sahrens #include <sys/pathname.h>
172fa9e4066Sahrens #include <sys/mount.h>
173fa9e4066Sahrens #include <sys/sdt.h>
174fa9e4066Sahrens #include <sys/fs/zfs.h>
175fa9e4066Sahrens #include <sys/zfs_ctldir.h>
176da6c28aaSamw #include <sys/zfs_dir.h>
177c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
178a2eea2e1Sahrens #include <sys/zvol.h>
1793f9d6ad7SLin Ling #include <sys/dsl_scan.h>
180ecd6cf80Smarks #include <sharefs/share.h>
181f18faf3fSek #include <sys/dmu_objset.h>
1823b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1833b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18478f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1853b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
186a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
18745818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
188fa9e4066Sahrens 
189fa9e4066Sahrens #include "zfs_namecheck.h"
190e9dbad6fSeschrock #include "zfs_prop.h"
191ecd6cf80Smarks #include "zfs_deleg.h"
1920a586ceaSMark Shellenbaum #include "zfs_comutil.h"
193fa9e4066Sahrens 
194fa9e4066Sahrens extern struct modlfs zfs_modlfs;
195fa9e4066Sahrens 
196fa9e4066Sahrens extern void zfs_init(void);
197fa9e4066Sahrens extern void zfs_fini(void);
198fa9e4066Sahrens 
199fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
200fa9e4066Sahrens dev_info_t *zfs_dip;
201fa9e4066Sahrens 
2024445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2034445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2044445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2054445fffbSMatthew Ahrens 
2064445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2074445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2084445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
209fa9e4066Sahrens 
21054d692b7SGeorge Wilson typedef enum {
21154d692b7SGeorge Wilson 	NO_NAME,
21254d692b7SGeorge Wilson 	POOL_NAME,
21354d692b7SGeorge Wilson 	DATASET_NAME
21454d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
21554d692b7SGeorge Wilson 
216f9af39baSGeorge Wilson typedef enum {
217f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
218f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2194445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
220f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
221f9af39baSGeorge Wilson 
222fa9e4066Sahrens typedef struct zfs_ioc_vec {
2234445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
224fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
225fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
22654d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2274445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
228f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2294445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2304445fffbSMatthew Ahrens 	const char		*zvec_name;
231fa9e4066Sahrens } zfs_ioc_vec_t;
232fa9e4066Sahrens 
23314843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
23414843421SMatthew Ahrens static const char *userquota_perms[] = {
23514843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
23614843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
23714843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
23814843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
23914843421SMatthew Ahrens };
24014843421SMatthew Ahrens 
24114843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
24292241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
24392241e0bSTom Erickson     cred_t *cr);
24492241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
24592241e0bSTom Erickson     nvlist_t **errors);
2460a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2470a48a24eStimh     boolean_t *);
2484445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2494445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2500a48a24eStimh 
2512acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
252a6f561b4SSašo Kiselkov 
253fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
254fa9e4066Sahrens void
255fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	const char *newfile;
2583f9d6ad7SLin Ling 	char buf[512];
259fa9e4066Sahrens 	va_list adx;
260fa9e4066Sahrens 
261fa9e4066Sahrens 	/*
262fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
263fa9e4066Sahrens 	 */
264fa9e4066Sahrens 	newfile = strrchr(file, '/');
265fa9e4066Sahrens 	if (newfile != NULL) {
266fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
267fa9e4066Sahrens 	} else {
268fa9e4066Sahrens 		newfile = file;
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 
271fa9e4066Sahrens 	va_start(adx, fmt);
272fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
273fa9e4066Sahrens 	va_end(adx);
274fa9e4066Sahrens 
275fa9e4066Sahrens 	/*
276fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
277fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
278fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
279fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
280fa9e4066Sahrens 	 * arg0 = file name
281fa9e4066Sahrens 	 * arg1 = function name
282fa9e4066Sahrens 	 * arg2 = line number
283fa9e4066Sahrens 	 * arg3 = message
284fa9e4066Sahrens 	 */
285fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
286fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
287fa9e4066Sahrens }
288fa9e4066Sahrens 
289ecd6cf80Smarks static void
290228975ccSek history_str_free(char *buf)
291228975ccSek {
292228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
293228975ccSek }
294228975ccSek 
295228975ccSek static char *
296228975ccSek history_str_get(zfs_cmd_t *zc)
297ecd6cf80Smarks {
29840feaa91Sahrens 	char *buf;
299ecd6cf80Smarks 
300ecd6cf80Smarks 	if (zc->zc_history == NULL)
301228975ccSek 		return (NULL);
302e7437265Sahrens 
303ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
304ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
305ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
306228975ccSek 		history_str_free(buf);
307228975ccSek 		return (NULL);
308ecd6cf80Smarks 	}
309ecd6cf80Smarks 
310ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
311ecd6cf80Smarks 
312228975ccSek 	return (buf);
313228975ccSek }
314ecd6cf80Smarks 
31515e6edf1Sgw /*
31615e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
31715e6edf1Sgw  */
31815e6edf1Sgw static boolean_t
31915e6edf1Sgw zfs_is_bootfs(const char *name)
32015e6edf1Sgw {
321503ad85cSMatthew Ahrens 	objset_t *os;
32215e6edf1Sgw 
323503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
324503ad85cSMatthew Ahrens 		boolean_t ret;
325b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
326503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
327503ad85cSMatthew Ahrens 		return (ret);
32815e6edf1Sgw 	}
329503ad85cSMatthew Ahrens 	return (B_FALSE);
33015e6edf1Sgw }
33115e6edf1Sgw 
332c2a93d44Stimh /*
333f7170741SWill Andrews  * Return non-zero if the spa version is less than requested version.
334c2a93d44Stimh  */
335da6c28aaSamw static int
3360a48a24eStimh zfs_earlier_version(const char *name, int version)
337da6c28aaSamw {
338da6c28aaSamw 	spa_t *spa;
339da6c28aaSamw 
340da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
341da6c28aaSamw 		if (spa_version(spa) < version) {
342da6c28aaSamw 			spa_close(spa, FTAG);
343da6c28aaSamw 			return (1);
344da6c28aaSamw 		}
345da6c28aaSamw 		spa_close(spa, FTAG);
346da6c28aaSamw 	}
347da6c28aaSamw 	return (0);
348da6c28aaSamw }
349da6c28aaSamw 
3509e6eda55Smarks /*
351745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3529e6eda55Smarks  */
353745cd3c5Smaybee static boolean_t
354745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3559e6eda55Smarks {
3569e6eda55Smarks 	objset_t *os;
357745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3589e6eda55Smarks 
359503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
360745cd3c5Smaybee 		uint64_t zplversion;
3619e6eda55Smarks 
362503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
363503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
364503ad85cSMatthew Ahrens 			return (B_TRUE);
365503ad85cSMatthew Ahrens 		}
366503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
367745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
368745cd3c5Smaybee 			rc = zplversion < version;
369503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3709e6eda55Smarks 	}
3719e6eda55Smarks 	return (rc);
3729e6eda55Smarks }
3739e6eda55Smarks 
374228975ccSek static void
375228975ccSek zfs_log_history(zfs_cmd_t *zc)
376228975ccSek {
377228975ccSek 	spa_t *spa;
378228975ccSek 	char *buf;
379ecd6cf80Smarks 
380228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
381228975ccSek 		return;
382228975ccSek 
383228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
384228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3854445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
386228975ccSek 		spa_close(spa, FTAG);
387228975ccSek 	}
388228975ccSek 	history_str_free(buf);
389ecd6cf80Smarks }
390ecd6cf80Smarks 
391fa9e4066Sahrens /*
392fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
393fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
394fa9e4066Sahrens  */
395fa9e4066Sahrens /* ARGSUSED */
396fa9e4066Sahrens static int
3974445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
398fa9e4066Sahrens {
399fa9e4066Sahrens 	return (0);
400fa9e4066Sahrens }
401fa9e4066Sahrens 
402fa9e4066Sahrens /*
403fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
404fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
405fa9e4066Sahrens  */
406fa9e4066Sahrens /* ARGSUSED */
407fa9e4066Sahrens static int
4084445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
409fa9e4066Sahrens {
410fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
411ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
412fa9e4066Sahrens 		return (0);
413fa9e4066Sahrens 
414be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417fa9e4066Sahrens static int
418a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	int writable = 1;
421fa9e4066Sahrens 
422fa9e4066Sahrens 	/*
423fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
424fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
425fa9e4066Sahrens 	 */
426fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
427fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
428be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
429fa9e4066Sahrens 
430fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
431fa9e4066Sahrens 		/*
432fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
433fa9e4066Sahrens 		 * global zone.
434fa9e4066Sahrens 		 */
435fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
436be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
437fa9e4066Sahrens 	} else {
438fa9e4066Sahrens 		/*
439fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
440fa9e4066Sahrens 		 */
441fa9e4066Sahrens 		if (!zoned)
442be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
443fa9e4066Sahrens 
444fa9e4066Sahrens 		/* must be writable by this zone */
445fa9e4066Sahrens 		if (!writable)
446be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
447fa9e4066Sahrens 	}
448fa9e4066Sahrens 	return (0);
449fa9e4066Sahrens }
450fa9e4066Sahrens 
451a7f53a56SChris Kirby static int
452a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
453a7f53a56SChris Kirby {
454a7f53a56SChris Kirby 	uint64_t zoned;
455a7f53a56SChris Kirby 
456a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
457be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
458a7f53a56SChris Kirby 
459a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
460a7f53a56SChris Kirby }
461a7f53a56SChris Kirby 
462a7f53a56SChris Kirby static int
463a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
464a7f53a56SChris Kirby {
465a7f53a56SChris Kirby 	uint64_t zoned;
466a7f53a56SChris Kirby 
4673b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
468be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
469a7f53a56SChris Kirby 
470a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
471a7f53a56SChris Kirby }
472a7f53a56SChris Kirby 
4734445fffbSMatthew Ahrens static int
4743b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4753b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
476fa9e4066Sahrens {
477fa9e4066Sahrens 	int error;
478fa9e4066Sahrens 
47919b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
480ecd6cf80Smarks 	if (error == 0) {
481ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4823b2aab18SMatthew Ahrens 		if (error != 0)
4834445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
484ecd6cf80Smarks 	}
485ecd6cf80Smarks 	return (error);
486ecd6cf80Smarks }
487ecd6cf80Smarks 
4884445fffbSMatthew Ahrens static int
4893b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
490a7f53a56SChris Kirby {
491a7f53a56SChris Kirby 	int error;
4923b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
4933b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
494a7f53a56SChris Kirby 
49525f7d993SMatthew Ahrens 	/*
49625f7d993SMatthew Ahrens 	 * First do a quick check for root in the global zone, which
49725f7d993SMatthew Ahrens 	 * is allowed to do all write_perms.  This ensures that zfs_ioc_*
49825f7d993SMatthew Ahrens 	 * will get to handle nonexistent datasets.
49925f7d993SMatthew Ahrens 	 */
50025f7d993SMatthew Ahrens 	if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
50125f7d993SMatthew Ahrens 		return (0);
50225f7d993SMatthew Ahrens 
5033b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
5043b2aab18SMatthew Ahrens 	if (error != 0)
5053b2aab18SMatthew Ahrens 		return (error);
5063b2aab18SMatthew Ahrens 
5073b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
5083b2aab18SMatthew Ahrens 	if (error != 0) {
5093b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5103b2aab18SMatthew Ahrens 		return (error);
511a7f53a56SChris Kirby 	}
5123b2aab18SMatthew Ahrens 
5133b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5143b2aab18SMatthew Ahrens 
5153b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5163b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
517a7f53a56SChris Kirby 	return (error);
518a7f53a56SChris Kirby }
519a7f53a56SChris Kirby 
5204201a95eSRic Aleshire /*
5214201a95eSRic Aleshire  * Policy for setting the security label property.
5224201a95eSRic Aleshire  *
5234201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5244201a95eSRic Aleshire  */
5254201a95eSRic Aleshire static int
52692241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5274201a95eSRic Aleshire {
5284201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5294201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5304201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5314201a95eSRic Aleshire 	uint64_t	zoned;
5324201a95eSRic Aleshire 	int		needed_priv = -1;
5334201a95eSRic Aleshire 	int		error;
5344201a95eSRic Aleshire 
5354201a95eSRic Aleshire 	/* First get the existing dataset label. */
5364201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5374201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5383b2aab18SMatthew Ahrens 	if (error != 0)
539be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5404201a95eSRic Aleshire 
5414201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5424201a95eSRic Aleshire 		new_default = TRUE;
5434201a95eSRic Aleshire 
5444201a95eSRic Aleshire 	/* The label must be translatable */
5454201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
546be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5474201a95eSRic Aleshire 
5484201a95eSRic Aleshire 	/*
5494201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5504201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5514201a95eSRic Aleshire 	 * are needed.
5524201a95eSRic Aleshire 	 */
5534201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5544201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
555be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5564201a95eSRic Aleshire 		return (0);
5574201a95eSRic Aleshire 	}
5584201a95eSRic Aleshire 
5594201a95eSRic Aleshire 	/*
5604201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5614201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5624201a95eSRic Aleshire 	 * global zone.
5634201a95eSRic Aleshire 	 */
5644201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5654201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
566be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5674201a95eSRic Aleshire 	if (!zoned) {
5684201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
569be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5704201a95eSRic Aleshire 	}
5714201a95eSRic Aleshire 
5724201a95eSRic Aleshire 	/*
5734201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5744201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5754201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5764201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5774201a95eSRic Aleshire 	 */
5784201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
57992241e0bSTom Erickson 		objset_t *os;
58092241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
58192241e0bSTom Erickson 
5824201a95eSRic Aleshire 		/*
5834201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5844201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5854201a95eSRic Aleshire 		 */
5864201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
58792241e0bSTom Erickson 		    setsl_tag, &os);
5883b2aab18SMatthew Ahrens 		if (error != 0)
589be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5904201a95eSRic Aleshire 
59192241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
59292241e0bSTom Erickson 
5934201a95eSRic Aleshire 		if (new_default) {
5944201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
5954201a95eSRic Aleshire 			goto out_check;
5964201a95eSRic Aleshire 		}
5974201a95eSRic Aleshire 
5984201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
599be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
6004201a95eSRic Aleshire 
6014201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
6024201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
6034201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
6044201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6054201a95eSRic Aleshire 	} else {
6064201a95eSRic Aleshire 		/* dataset currently has a default label */
6074201a95eSRic Aleshire 		if (!new_default)
6084201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6094201a95eSRic Aleshire 	}
6104201a95eSRic Aleshire 
6114201a95eSRic Aleshire out_check:
6124201a95eSRic Aleshire 	if (needed_priv != -1)
6134201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6144201a95eSRic Aleshire 	return (0);
6154201a95eSRic Aleshire }
6164201a95eSRic Aleshire 
617ecd6cf80Smarks static int
61892241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
61992241e0bSTom Erickson     cred_t *cr)
620ecd6cf80Smarks {
62192241e0bSTom Erickson 	char *strval;
62292241e0bSTom Erickson 
623ecd6cf80Smarks 	/*
624ecd6cf80Smarks 	 * Check permissions for special properties.
625ecd6cf80Smarks 	 */
626ecd6cf80Smarks 	switch (prop) {
627ecd6cf80Smarks 	case ZFS_PROP_ZONED:
628ecd6cf80Smarks 		/*
629ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
630ecd6cf80Smarks 		 */
631ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
632be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
633ecd6cf80Smarks 		break;
634ecd6cf80Smarks 
635ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
636a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
637a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
638ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
639ecd6cf80Smarks 			uint64_t zoned;
6409adfa60dSMatthew Ahrens 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
641ecd6cf80Smarks 			/*
642ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
643a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
644ecd6cf80Smarks 			 * the thing they own.
645ecd6cf80Smarks 			 */
64692241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
647ecd6cf80Smarks 			    setpoint))
648be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
64992241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
650be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
651ecd6cf80Smarks 		}
652db870a07Sahrens 		break;
6534201a95eSRic Aleshire 
6544201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6554201a95eSRic Aleshire 		if (!is_system_labeled())
656be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
65792241e0bSTom Erickson 
65892241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
65992241e0bSTom Erickson 			int err;
66092241e0bSTom Erickson 
66192241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
66292241e0bSTom Erickson 			if (err != 0)
66392241e0bSTom Erickson 				return (err);
66492241e0bSTom Erickson 		}
6654201a95eSRic Aleshire 		break;
666ecd6cf80Smarks 	}
667ecd6cf80Smarks 
66892241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
669ecd6cf80Smarks }
670ecd6cf80Smarks 
6714445fffbSMatthew Ahrens /* ARGSUSED */
6724445fffbSMatthew Ahrens static int
6734445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
674ecd6cf80Smarks {
675ecd6cf80Smarks 	int error;
676ecd6cf80Smarks 
677ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6783b2aab18SMatthew Ahrens 	if (error != 0)
679fa9e4066Sahrens 		return (error);
680fa9e4066Sahrens 
681ecd6cf80Smarks 	/*
682ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
683ecd6cf80Smarks 	 * dsl_deleg_can_allow()
684ecd6cf80Smarks 	 */
685ecd6cf80Smarks 	return (0);
686ecd6cf80Smarks }
687ecd6cf80Smarks 
6884445fffbSMatthew Ahrens /* ARGSUSED */
6894445fffbSMatthew Ahrens static int
6904445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
691ecd6cf80Smarks {
692681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
693681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
694ecd6cf80Smarks }
695ecd6cf80Smarks 
6964445fffbSMatthew Ahrens /* ARGSUSED */
6974445fffbSMatthew Ahrens static int
6984445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
699ecd6cf80Smarks {
700a7f53a56SChris Kirby 	dsl_pool_t *dp;
701a7f53a56SChris Kirby 	dsl_dataset_t *ds;
702a7f53a56SChris Kirby 	char *cp;
703a7f53a56SChris Kirby 	int error;
704a7f53a56SChris Kirby 
705a7f53a56SChris Kirby 	/*
706a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
707a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
708a7f53a56SChris Kirby 	 */
709a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
710a7f53a56SChris Kirby 	if (cp == NULL)
711be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7123b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7133b2aab18SMatthew Ahrens 	if (error != 0)
714a7f53a56SChris Kirby 		return (error);
715a7f53a56SChris Kirby 
716a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7173b2aab18SMatthew Ahrens 	if (error != 0) {
7183b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
719a7f53a56SChris Kirby 		return (error);
7203b2aab18SMatthew Ahrens 	}
721a7f53a56SChris Kirby 
722a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
723a7f53a56SChris Kirby 
724a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
725a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
726a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7273b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
728a7f53a56SChris Kirby 
729a7f53a56SChris Kirby 	return (error);
730ecd6cf80Smarks }
731ecd6cf80Smarks 
7324445fffbSMatthew Ahrens /* ARGSUSED */
733743a77edSAlan Wright static int
7344445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7354445fffbSMatthew Ahrens {
7364445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7374445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7384445fffbSMatthew Ahrens }
7394445fffbSMatthew Ahrens 
7404445fffbSMatthew Ahrens /* ARGSUSED */
7414445fffbSMatthew Ahrens static int
7424445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
743743a77edSAlan Wright {
744743a77edSAlan Wright 	vnode_t *vp;
745743a77edSAlan Wright 	int error;
746743a77edSAlan Wright 
747743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
748743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
749743a77edSAlan Wright 		return (error);
750743a77edSAlan Wright 
751743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
752743a77edSAlan Wright 
753743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
754743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
755743a77edSAlan Wright 	    zc->zc_name) != 0)) {
756743a77edSAlan Wright 		VN_RELE(vp);
757be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
758743a77edSAlan Wright 	}
759743a77edSAlan Wright 
760743a77edSAlan Wright 	VN_RELE(vp);
761743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
762743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
763743a77edSAlan Wright }
764743a77edSAlan Wright 
765ecd6cf80Smarks int
7664445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
767ecd6cf80Smarks {
768ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
769be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
770ecd6cf80Smarks 
7713cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
772ecd6cf80Smarks 		return (0);
773ecd6cf80Smarks 	} else {
7744445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
775743a77edSAlan Wright 	}
776743a77edSAlan Wright }
777ecd6cf80Smarks 
778743a77edSAlan Wright int
7794445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
780743a77edSAlan Wright {
781743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
782be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
783ecd6cf80Smarks 
784743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
785743a77edSAlan Wright 		return (0);
786743a77edSAlan Wright 	} else {
7874445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
788ecd6cf80Smarks 	}
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens static int
792ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
793fa9e4066Sahrens {
794fa9e4066Sahrens 	char *cp;
795fa9e4066Sahrens 
796fa9e4066Sahrens 	/*
797fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
798fa9e4066Sahrens 	 */
799ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
800ecd6cf80Smarks 	cp = strrchr(parent, '@');
801fa9e4066Sahrens 	if (cp != NULL) {
802fa9e4066Sahrens 		cp[0] = '\0';
803fa9e4066Sahrens 	} else {
804ecd6cf80Smarks 		cp = strrchr(parent, '/');
805fa9e4066Sahrens 		if (cp == NULL)
806be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
807fa9e4066Sahrens 		cp[0] = '\0';
808ecd6cf80Smarks 	}
809ecd6cf80Smarks 
810ecd6cf80Smarks 	return (0);
811ecd6cf80Smarks }
812ecd6cf80Smarks 
813ecd6cf80Smarks int
814ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
815ecd6cf80Smarks {
816ecd6cf80Smarks 	int error;
817ecd6cf80Smarks 
818ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
819ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
820ecd6cf80Smarks 		return (error);
821ecd6cf80Smarks 
822ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
823ecd6cf80Smarks }
824ecd6cf80Smarks 
8254445fffbSMatthew Ahrens /* ARGSUSED */
826ecd6cf80Smarks static int
8274445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
828ecd6cf80Smarks {
829ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
830ecd6cf80Smarks }
831ecd6cf80Smarks 
832cbf6f6aaSWilliam Gorrell /*
833cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8344445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
835cbf6f6aaSWilliam Gorrell  */
8364445fffbSMatthew Ahrens /* ARGSUSED */
837cbf6f6aaSWilliam Gorrell static int
8384445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
839cbf6f6aaSWilliam Gorrell {
8404445fffbSMatthew Ahrens 	nvlist_t *snaps;
8414445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8424445fffbSMatthew Ahrens 	int error = 0;
843cbf6f6aaSWilliam Gorrell 
8444445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
845be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8464445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8474445fffbSMatthew Ahrens 	    pair = nextpair) {
8484445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
84978f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
85078f17100SMatthew Ahrens 		if (error == ENOENT) {
8514445fffbSMatthew Ahrens 			/*
8524445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8534445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8544445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8554445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8564445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8574445fffbSMatthew Ahrens 			 * checked for permission).
8584445fffbSMatthew Ahrens 			 */
8594445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8604445fffbSMatthew Ahrens 			error = 0;
8614445fffbSMatthew Ahrens 		}
8624445fffbSMatthew Ahrens 		if (error != 0)
8634445fffbSMatthew Ahrens 			break;
8644445fffbSMatthew Ahrens 	}
865cbf6f6aaSWilliam Gorrell 
866cbf6f6aaSWilliam Gorrell 	return (error);
867cbf6f6aaSWilliam Gorrell }
868cbf6f6aaSWilliam Gorrell 
869ecd6cf80Smarks int
870ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
871ecd6cf80Smarks {
8729adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
873ecd6cf80Smarks 	int	error;
874ecd6cf80Smarks 
875ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
876ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
877ecd6cf80Smarks 		return (error);
878ecd6cf80Smarks 
879ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
880ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
881ecd6cf80Smarks 		return (error);
882ecd6cf80Smarks 
883ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
884ecd6cf80Smarks 	    sizeof (parentname))) != 0)
885ecd6cf80Smarks 		return (error);
886ecd6cf80Smarks 
887ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
888ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
889ecd6cf80Smarks 		return (error);
890ecd6cf80Smarks 
891ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
892ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
893ecd6cf80Smarks 		return (error);
894ecd6cf80Smarks 
895ecd6cf80Smarks 	return (error);
896ecd6cf80Smarks }
897ecd6cf80Smarks 
8984445fffbSMatthew Ahrens /* ARGSUSED */
899ecd6cf80Smarks static int
9004445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
901ecd6cf80Smarks {
902ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
903ecd6cf80Smarks }
904ecd6cf80Smarks 
9054445fffbSMatthew Ahrens /* ARGSUSED */
906ecd6cf80Smarks static int
9074445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
908ecd6cf80Smarks {
9093b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9103b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
911ecd6cf80Smarks 	int error;
912ecd6cf80Smarks 
913ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
914ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9153b2aab18SMatthew Ahrens 	if (error != 0)
9163b2aab18SMatthew Ahrens 		return (error);
9173b2aab18SMatthew Ahrens 
9183b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9193b2aab18SMatthew Ahrens 	if (error != 0)
920ecd6cf80Smarks 		return (error);
921ecd6cf80Smarks 
9223b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
923ecd6cf80Smarks 
924ecd6cf80Smarks 	if (error == 0) {
9259adfa60dSMatthew Ahrens 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
9263b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
927ecd6cf80Smarks 		dsl_dir_t *dd;
9283b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
929ecd6cf80Smarks 
930745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
931c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9323b2aab18SMatthew Ahrens 		if (error != 0) {
9333b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9343b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
935ecd6cf80Smarks 			return (error);
936ecd6cf80Smarks 		}
937ecd6cf80Smarks 
9383b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
939ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
940ecd6cf80Smarks 
9413b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9423b2aab18SMatthew Ahrens 		if (error == 0) {
9433b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
944ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9453b2aab18SMatthew Ahrens 		}
9463b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9473b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
948ecd6cf80Smarks 	}
9493b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
950ecd6cf80Smarks 	return (error);
951ecd6cf80Smarks }
952ecd6cf80Smarks 
9534445fffbSMatthew Ahrens /* ARGSUSED */
954ecd6cf80Smarks static int
9554445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
956ecd6cf80Smarks {
957ecd6cf80Smarks 	int error;
958ecd6cf80Smarks 
959ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
960ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
961ecd6cf80Smarks 		return (error);
962ecd6cf80Smarks 
963ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
964ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
965ecd6cf80Smarks 		return (error);
966ecd6cf80Smarks 
967ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
968ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
969ecd6cf80Smarks }
970ecd6cf80Smarks 
971ecd6cf80Smarks int
972ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
973ecd6cf80Smarks {
974681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
975681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
976ecd6cf80Smarks }
977ecd6cf80Smarks 
9784445fffbSMatthew Ahrens /*
9794445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9804445fffbSMatthew Ahrens  */
9814445fffbSMatthew Ahrens /* ARGSUSED */
982ecd6cf80Smarks static int
9834445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
984ecd6cf80Smarks {
9854445fffbSMatthew Ahrens 	nvlist_t *snaps;
986d5285caeSGeorge Wilson 	int error = 0;
9874445fffbSMatthew Ahrens 	nvpair_t *pair;
988ecd6cf80Smarks 
9894445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
990be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9914445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
9924445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
9934445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
9944445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
9954445fffbSMatthew Ahrens 
9964445fffbSMatthew Ahrens 		if (atp == NULL) {
997be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
9984445fffbSMatthew Ahrens 			break;
9994445fffbSMatthew Ahrens 		}
10004445fffbSMatthew Ahrens 		*atp = '\0';
10014445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
10024445fffbSMatthew Ahrens 		*atp = '@';
10034445fffbSMatthew Ahrens 		if (error != 0)
10044445fffbSMatthew Ahrens 			break;
10054445fffbSMatthew Ahrens 	}
10064445fffbSMatthew Ahrens 	return (error);
1007ecd6cf80Smarks }
1008ecd6cf80Smarks 
100978f17100SMatthew Ahrens /*
101078f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
101178f17100SMatthew Ahrens  */
101278f17100SMatthew Ahrens /* ARGSUSED */
101378f17100SMatthew Ahrens static int
101478f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
101578f17100SMatthew Ahrens {
101678f17100SMatthew Ahrens 	int error = 0;
101778f17100SMatthew Ahrens 
101878f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
101978f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
102078f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
102178f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
102278f17100SMatthew Ahrens 
102378f17100SMatthew Ahrens 		if (hashp == NULL) {
102478f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
102578f17100SMatthew Ahrens 			break;
102678f17100SMatthew Ahrens 		}
102778f17100SMatthew Ahrens 		*hashp = '\0';
102878f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
102978f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
103078f17100SMatthew Ahrens 		*hashp = '#';
103178f17100SMatthew Ahrens 		if (error != 0)
103278f17100SMatthew Ahrens 			break;
103378f17100SMatthew Ahrens 	}
103478f17100SMatthew Ahrens 	return (error);
103578f17100SMatthew Ahrens }
103678f17100SMatthew Ahrens 
103778f17100SMatthew Ahrens /* ARGSUSED */
103878f17100SMatthew Ahrens static int
103978f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
104078f17100SMatthew Ahrens {
104178f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
104278f17100SMatthew Ahrens 	int error = 0;
104378f17100SMatthew Ahrens 
104478f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
104578f17100SMatthew Ahrens 	    pair = nextpair) {
104678f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
104778f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
104878f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
104978f17100SMatthew Ahrens 
105078f17100SMatthew Ahrens 		if (hashp == NULL) {
105178f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
105278f17100SMatthew Ahrens 			break;
105378f17100SMatthew Ahrens 		}
105478f17100SMatthew Ahrens 
105578f17100SMatthew Ahrens 		*hashp = '\0';
105678f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
105778f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
105878f17100SMatthew Ahrens 		*hashp = '#';
105978f17100SMatthew Ahrens 		if (error == ENOENT) {
106078f17100SMatthew Ahrens 			/*
106178f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
106278f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
106378f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
106478f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
106578f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
106678f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
106778f17100SMatthew Ahrens 			 */
106878f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
106978f17100SMatthew Ahrens 			error = 0;
107078f17100SMatthew Ahrens 		}
107178f17100SMatthew Ahrens 		if (error != 0)
107278f17100SMatthew Ahrens 			break;
107378f17100SMatthew Ahrens 	}
107478f17100SMatthew Ahrens 
107578f17100SMatthew Ahrens 	return (error);
107678f17100SMatthew Ahrens }
107778f17100SMatthew Ahrens 
10784445fffbSMatthew Ahrens /* ARGSUSED */
1079ecd6cf80Smarks static int
10804445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10814445fffbSMatthew Ahrens {
10824445fffbSMatthew Ahrens 	/*
10834445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
10844445fffbSMatthew Ahrens 	 * to log to.
10854445fffbSMatthew Ahrens 	 */
10864445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1087be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
10884445fffbSMatthew Ahrens 	return (0);
10894445fffbSMatthew Ahrens }
10904445fffbSMatthew Ahrens 
10914445fffbSMatthew Ahrens static int
10924445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1093ecd6cf80Smarks {
10949adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
109592241e0bSTom Erickson 	int	error;
10964445fffbSMatthew Ahrens 	char	*origin;
1097ecd6cf80Smarks 
1098ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1099ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1100ecd6cf80Smarks 		return (error);
1101fa9e4066Sahrens 
11024445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
11034445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
11044445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
11054445fffbSMatthew Ahrens 		return (error);
1106fa9e4066Sahrens 
1107ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1108ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1109ecd6cf80Smarks 		return (error);
1110ecd6cf80Smarks 
11114445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11124445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1113fa9e4066Sahrens }
1114fa9e4066Sahrens 
1115fa9e4066Sahrens /*
1116fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1117fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1118fa9e4066Sahrens  */
1119fa9e4066Sahrens /* ARGSUSED */
1120fa9e4066Sahrens static int
11214445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1122fa9e4066Sahrens {
1123fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1124be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1125fa9e4066Sahrens 
1126fa9e4066Sahrens 	return (0);
1127fa9e4066Sahrens }
1128fa9e4066Sahrens 
112999d5e173STim Haley /*
113099d5e173STim Haley  * Policy for object to name lookups.
113199d5e173STim Haley  */
113299d5e173STim Haley /* ARGSUSED */
113399d5e173STim Haley static int
11344445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
113599d5e173STim Haley {
113699d5e173STim Haley 	int error;
113799d5e173STim Haley 
113899d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
113999d5e173STim Haley 		return (0);
114099d5e173STim Haley 
114199d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
114299d5e173STim Haley 	return (error);
114399d5e173STim Haley }
114499d5e173STim Haley 
1145ea8dc4b6Seschrock /*
1146ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1147ea8dc4b6Seschrock  */
1148ea8dc4b6Seschrock /* ARGSUSED */
1149ea8dc4b6Seschrock static int
11504445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1151ea8dc4b6Seschrock {
1152ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1153ea8dc4b6Seschrock }
1154ea8dc4b6Seschrock 
11554445fffbSMatthew Ahrens /* ARGSUSED */
1156e45ce728Sahrens static int
11574445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1158e45ce728Sahrens {
1159e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1160e45ce728Sahrens 
1161990b4856Slling 	if (prop == ZPROP_INVAL) {
1162e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1163be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1164e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1165e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1166e45ce728Sahrens 	} else {
116792241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
116892241e0bSTom Erickson 		    NULL, cr));
1169e45ce728Sahrens 	}
1170e45ce728Sahrens }
1171e45ce728Sahrens 
117214843421SMatthew Ahrens static int
11734445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
117414843421SMatthew Ahrens {
11754445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
117614843421SMatthew Ahrens 	if (err)
117714843421SMatthew Ahrens 		return (err);
117814843421SMatthew Ahrens 
117914843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1180be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
118114843421SMatthew Ahrens 
118214843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
118314843421SMatthew Ahrens 		/*
118414843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
118514843421SMatthew Ahrens 		 * themself, allow it.
118614843421SMatthew Ahrens 		 */
118714843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
118814843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
118914843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
119014843421SMatthew Ahrens 				return (0);
119114843421SMatthew Ahrens 		} else {
119214843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
119314843421SMatthew Ahrens 				return (0);
119414843421SMatthew Ahrens 		}
119514843421SMatthew Ahrens 	}
119614843421SMatthew Ahrens 
119714843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
119814843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
119914843421SMatthew Ahrens }
120014843421SMatthew Ahrens 
120114843421SMatthew Ahrens static int
12024445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120314843421SMatthew Ahrens {
12044445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
120514843421SMatthew Ahrens 	if (err)
120614843421SMatthew Ahrens 		return (err);
120714843421SMatthew Ahrens 
120814843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1209be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
121014843421SMatthew Ahrens 
121114843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
121214843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
121314843421SMatthew Ahrens }
121414843421SMatthew Ahrens 
12154445fffbSMatthew Ahrens /* ARGSUSED */
121614843421SMatthew Ahrens static int
12174445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
121814843421SMatthew Ahrens {
121992241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
122092241e0bSTom Erickson 	    NULL, cr));
122114843421SMatthew Ahrens }
122214843421SMatthew Ahrens 
12234445fffbSMatthew Ahrens /* ARGSUSED */
1224842727c2SChris Kirby static int
12254445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1226842727c2SChris Kirby {
12273b2aab18SMatthew Ahrens 	nvpair_t *pair;
12283b2aab18SMatthew Ahrens 	nvlist_t *holds;
12293b2aab18SMatthew Ahrens 	int error;
12303b2aab18SMatthew Ahrens 
12313b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12323b2aab18SMatthew Ahrens 	if (error != 0)
1233be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12343b2aab18SMatthew Ahrens 
12353b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12363b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
12379adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12383b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12393b2aab18SMatthew Ahrens 		if (error != 0)
12403b2aab18SMatthew Ahrens 			return (error);
12413b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12423b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12433b2aab18SMatthew Ahrens 		if (error != 0)
12443b2aab18SMatthew Ahrens 			return (error);
12453b2aab18SMatthew Ahrens 	}
12463b2aab18SMatthew Ahrens 	return (0);
1247842727c2SChris Kirby }
1248842727c2SChris Kirby 
12494445fffbSMatthew Ahrens /* ARGSUSED */
1250842727c2SChris Kirby static int
12514445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1252842727c2SChris Kirby {
12533b2aab18SMatthew Ahrens 	nvpair_t *pair;
12543b2aab18SMatthew Ahrens 	int error;
12553b2aab18SMatthew Ahrens 
12563b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12573b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
12589adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12593b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12603b2aab18SMatthew Ahrens 		if (error != 0)
12613b2aab18SMatthew Ahrens 			return (error);
12623b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12633b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12643b2aab18SMatthew Ahrens 		if (error != 0)
12653b2aab18SMatthew Ahrens 			return (error);
12663b2aab18SMatthew Ahrens 	}
12673b2aab18SMatthew Ahrens 	return (0);
1268842727c2SChris Kirby }
1269842727c2SChris Kirby 
127099d5e173STim Haley /*
127199d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
127299d5e173STim Haley  */
127399d5e173STim Haley static int
12744445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
127599d5e173STim Haley {
127699d5e173STim Haley 	/*
127799d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
127899d5e173STim Haley 	 * hold, destroy and release all rolled into one.
127999d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
128099d5e173STim Haley 	 */
128199d5e173STim Haley 	int error;
128299d5e173STim Haley 
128399d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
128499d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
128599d5e173STim Haley 		return (0);
128699d5e173STim Haley 
12874445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
12883b2aab18SMatthew Ahrens 	if (error == 0)
12894445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
12903b2aab18SMatthew Ahrens 	if (error == 0)
12914445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
12923b2aab18SMatthew Ahrens 	if (error == 0)
12934445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
129499d5e173STim Haley 	return (error);
129599d5e173STim Haley }
129699d5e173STim Haley 
1297fa9e4066Sahrens /*
1298fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1299fa9e4066Sahrens  */
1300fa9e4066Sahrens static int
1301478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1302fa9e4066Sahrens {
1303fa9e4066Sahrens 	char *packed;
1304fa9e4066Sahrens 	int error;
1305990b4856Slling 	nvlist_t *list = NULL;
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	/*
1308e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1309fa9e4066Sahrens 	 */
1310990b4856Slling 	if (size == 0)
1311be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1312fa9e4066Sahrens 
1313fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1314fa9e4066Sahrens 
1315478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1316478ed9adSEric Taylor 	    iflag)) != 0) {
1317fa9e4066Sahrens 		kmem_free(packed, size);
1318c71c00bbSRichard Yao 		return (SET_ERROR(EFAULT));
1319fa9e4066Sahrens 	}
1320fa9e4066Sahrens 
1321990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1322fa9e4066Sahrens 		kmem_free(packed, size);
1323fa9e4066Sahrens 		return (error);
1324fa9e4066Sahrens 	}
1325fa9e4066Sahrens 
1326fa9e4066Sahrens 	kmem_free(packed, size);
1327fa9e4066Sahrens 
1328990b4856Slling 	*nvp = list;
1329fa9e4066Sahrens 	return (0);
1330fa9e4066Sahrens }
1331fa9e4066Sahrens 
13324445fffbSMatthew Ahrens /*
13334445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13344445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13354445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13364445fffbSMatthew Ahrens  * removed.
13374445fffbSMatthew Ahrens  */
133892241e0bSTom Erickson static int
13394445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
134092241e0bSTom Erickson {
134192241e0bSTom Erickson 	size_t size;
134292241e0bSTom Erickson 
13434445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
134492241e0bSTom Erickson 
13454445fffbSMatthew Ahrens 	if (size > max) {
134692241e0bSTom Erickson 		nvpair_t *more_errors;
134792241e0bSTom Erickson 		int n = 0;
134892241e0bSTom Erickson 
13494445fffbSMatthew Ahrens 		if (max < 1024)
1350be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
135192241e0bSTom Erickson 
13524445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13534445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
135492241e0bSTom Erickson 
135592241e0bSTom Erickson 		do {
13564445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
135792241e0bSTom Erickson 			    more_errors);
13584445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
135992241e0bSTom Erickson 			n++;
13604445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13614445fffbSMatthew Ahrens 		} while (size > max);
136292241e0bSTom Erickson 
13634445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13644445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13654445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
136692241e0bSTom Erickson 	}
136792241e0bSTom Erickson 
136892241e0bSTom Erickson 	return (0);
136992241e0bSTom Erickson }
137092241e0bSTom Erickson 
1371e9dbad6fSeschrock static int
1372e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1373e9dbad6fSeschrock {
1374e9dbad6fSeschrock 	char *packed = NULL;
13756e27f868SSam Falkner 	int error = 0;
1376e9dbad6fSeschrock 	size_t size;
1377e9dbad6fSeschrock 
13784445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1379e9dbad6fSeschrock 
1380e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1381be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1382e9dbad6fSeschrock 	} else {
13834445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
13846e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
13856e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1386be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
13874445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1388e9dbad6fSeschrock 	}
1389e9dbad6fSeschrock 
1390e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
13914445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1392e9dbad6fSeschrock 	return (error);
1393e9dbad6fSeschrock }
1394e9dbad6fSeschrock 
139514843421SMatthew Ahrens static int
1396af4c679fSSean McEnroe getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
139714843421SMatthew Ahrens {
139814843421SMatthew Ahrens 	objset_t *os;
139914843421SMatthew Ahrens 	int error;
140014843421SMatthew Ahrens 
1401503ad85cSMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
14023b2aab18SMatthew Ahrens 	if (error != 0)
140314843421SMatthew Ahrens 		return (error);
1404503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1405503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1406be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1407503ad85cSMatthew Ahrens 	}
140814843421SMatthew Ahrens 
1409503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1410af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1411af4c679fSSean McEnroe 	if (*zfvp) {
1412af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
141314843421SMatthew Ahrens 	} else {
1414be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
141514843421SMatthew Ahrens 	}
1416503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1417503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
141814843421SMatthew Ahrens 	return (error);
141914843421SMatthew Ahrens }
142014843421SMatthew Ahrens 
142114843421SMatthew Ahrens /*
142214843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
142314843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1424ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1425ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
142614843421SMatthew Ahrens  */
142714843421SMatthew Ahrens static int
14281412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
142914843421SMatthew Ahrens {
143014843421SMatthew Ahrens 	int error = 0;
143114843421SMatthew Ahrens 
1432af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1433af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
143414843421SMatthew Ahrens 	if (error == 0) {
1435c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14361412a1a2SMark Shellenbaum 		    RW_READER, tag);
1437af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
143814843421SMatthew Ahrens 			/*
143914843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
144014843421SMatthew Ahrens 			 * thread should be just about to disassociate the
144114843421SMatthew Ahrens 			 * objset from the zfsvfs.
144214843421SMatthew Ahrens 			 */
1443c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1444be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
144514843421SMatthew Ahrens 		}
144614843421SMatthew Ahrens 	}
144714843421SMatthew Ahrens 	return (error);
144814843421SMatthew Ahrens }
144914843421SMatthew Ahrens 
145014843421SMatthew Ahrens static void
145114843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
145214843421SMatthew Ahrens {
1453c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
145414843421SMatthew Ahrens 
145514843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
145614843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
145714843421SMatthew Ahrens 	} else {
1458503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
145914843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
146014843421SMatthew Ahrens 	}
146114843421SMatthew Ahrens }
146214843421SMatthew Ahrens 
1463fa9e4066Sahrens static int
1464fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1465fa9e4066Sahrens {
1466fa9e4066Sahrens 	int error;
1467990b4856Slling 	nvlist_t *config, *props = NULL;
14680a48a24eStimh 	nvlist_t *rootprops = NULL;
14690a48a24eStimh 	nvlist_t *zplprops = NULL;
1470fa9e4066Sahrens 
1471990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1472478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1473fa9e4066Sahrens 		return (error);
14742a6b87f0Sek 
1475990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1476478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1477478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1478990b4856Slling 		nvlist_free(config);
1479990b4856Slling 		return (error);
1480990b4856Slling 	}
1481990b4856Slling 
14820a48a24eStimh 	if (props) {
14830a48a24eStimh 		nvlist_t *nvl = NULL;
14840a48a24eStimh 		uint64_t version = SPA_VERSION;
14850a48a24eStimh 
14860a48a24eStimh 		(void) nvlist_lookup_uint64(props,
14870a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1488ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1489be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
14900a48a24eStimh 			goto pool_props_bad;
14910a48a24eStimh 		}
14920a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
14930a48a24eStimh 		if (nvl) {
14940a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
14950a48a24eStimh 			if (error != 0) {
14960a48a24eStimh 				nvlist_free(config);
14970a48a24eStimh 				nvlist_free(props);
14980a48a24eStimh 				return (error);
14990a48a24eStimh 			}
15000a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15010a48a24eStimh 		}
15020a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15030a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
15040a48a24eStimh 		    zplprops, NULL);
15053b2aab18SMatthew Ahrens 		if (error != 0)
15060a48a24eStimh 			goto pool_props_bad;
15070a48a24eStimh 	}
15080a48a24eStimh 
15094445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
15100a48a24eStimh 
15110a48a24eStimh 	/*
15120a48a24eStimh 	 * Set the remaining root properties
15130a48a24eStimh 	 */
151492241e0bSTom Erickson 	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
151592241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
15160a48a24eStimh 		(void) spa_destroy(zc->zc_name);
1517fa9e4066Sahrens 
15180a48a24eStimh pool_props_bad:
15190a48a24eStimh 	nvlist_free(rootprops);
15200a48a24eStimh 	nvlist_free(zplprops);
1521fa9e4066Sahrens 	nvlist_free(config);
15220a48a24eStimh 	nvlist_free(props);
1523990b4856Slling 
1524fa9e4066Sahrens 	return (error);
1525fa9e4066Sahrens }
1526fa9e4066Sahrens 
1527fa9e4066Sahrens static int
1528fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1529fa9e4066Sahrens {
1530ecd6cf80Smarks 	int error;
1531ecd6cf80Smarks 	zfs_log_history(zc);
1532ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1533681d9761SEric Taylor 	if (error == 0)
1534681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1535ecd6cf80Smarks 	return (error);
1536fa9e4066Sahrens }
1537fa9e4066Sahrens 
1538fa9e4066Sahrens static int
1539fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1540fa9e4066Sahrens {
1541990b4856Slling 	nvlist_t *config, *props = NULL;
1542fa9e4066Sahrens 	uint64_t guid;
1543468c413aSTim Haley 	int error;
1544fa9e4066Sahrens 
1545990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1546478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1547990b4856Slling 		return (error);
1548990b4856Slling 
1549990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1550478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1551478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1552990b4856Slling 		nvlist_free(config);
1553fa9e4066Sahrens 		return (error);
1554990b4856Slling 	}
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1557ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1558be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1559fa9e4066Sahrens 	else
15604b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1561fa9e4066Sahrens 
15624b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15634b964adaSGeorge Wilson 		int err;
15644b964adaSGeorge Wilson 
15654b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15664b964adaSGeorge Wilson 			error = err;
15674b964adaSGeorge Wilson 	}
1568468c413aSTim Haley 
1569fa9e4066Sahrens 	nvlist_free(config);
1570fa9e4066Sahrens 
1571aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1572990b4856Slling 
1573fa9e4066Sahrens 	return (error);
1574fa9e4066Sahrens }
1575fa9e4066Sahrens 
1576fa9e4066Sahrens static int
1577fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1578fa9e4066Sahrens {
1579ecd6cf80Smarks 	int error;
158089a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1581394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
158289a89ebfSlling 
1583ecd6cf80Smarks 	zfs_log_history(zc);
1584394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1585681d9761SEric Taylor 	if (error == 0)
1586681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1587ecd6cf80Smarks 	return (error);
1588fa9e4066Sahrens }
1589fa9e4066Sahrens 
1590fa9e4066Sahrens static int
1591fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1592fa9e4066Sahrens {
1593fa9e4066Sahrens 	nvlist_t *configs;
1594fa9e4066Sahrens 	int error;
1595fa9e4066Sahrens 
1596fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1597be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1598fa9e4066Sahrens 
1599e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1600fa9e4066Sahrens 
1601fa9e4066Sahrens 	nvlist_free(configs);
1602fa9e4066Sahrens 
1603fa9e4066Sahrens 	return (error);
1604fa9e4066Sahrens }
1605fa9e4066Sahrens 
1606ad135b5dSChristopher Siden /*
1607ad135b5dSChristopher Siden  * inputs:
1608ad135b5dSChristopher Siden  * zc_name		name of the pool
1609ad135b5dSChristopher Siden  *
1610ad135b5dSChristopher Siden  * outputs:
1611ad135b5dSChristopher Siden  * zc_cookie		real errno
1612ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1613ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1614ad135b5dSChristopher Siden  */
1615fa9e4066Sahrens static int
1616fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1617fa9e4066Sahrens {
1618fa9e4066Sahrens 	nvlist_t *config;
1619fa9e4066Sahrens 	int error;
1620ea8dc4b6Seschrock 	int ret = 0;
1621fa9e4066Sahrens 
1622e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1623e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1624fa9e4066Sahrens 
1625fa9e4066Sahrens 	if (config != NULL) {
1626e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1627fa9e4066Sahrens 		nvlist_free(config);
1628ea8dc4b6Seschrock 
1629ea8dc4b6Seschrock 		/*
1630ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1631ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1632ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1633ea8dc4b6Seschrock 		 */
1634ea8dc4b6Seschrock 		zc->zc_cookie = error;
1635fa9e4066Sahrens 	} else {
1636ea8dc4b6Seschrock 		ret = error;
1637fa9e4066Sahrens 	}
1638fa9e4066Sahrens 
1639ea8dc4b6Seschrock 	return (ret);
1640fa9e4066Sahrens }
1641fa9e4066Sahrens 
1642fa9e4066Sahrens /*
1643fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1644fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1645fa9e4066Sahrens  */
1646fa9e4066Sahrens static int
1647fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1648fa9e4066Sahrens {
1649fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1650fa9e4066Sahrens 	int error;
1651fa9e4066Sahrens 
1652990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1653478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1654fa9e4066Sahrens 		return (error);
1655fa9e4066Sahrens 
1656fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1657fa9e4066Sahrens 
1658fa9e4066Sahrens 	nvlist_free(tryconfig);
1659fa9e4066Sahrens 
1660fa9e4066Sahrens 	if (config == NULL)
1661be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1662fa9e4066Sahrens 
1663e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1664fa9e4066Sahrens 	nvlist_free(config);
1665fa9e4066Sahrens 
1666fa9e4066Sahrens 	return (error);
1667fa9e4066Sahrens }
1668fa9e4066Sahrens 
16693f9d6ad7SLin Ling /*
16703f9d6ad7SLin Ling  * inputs:
16713f9d6ad7SLin Ling  * zc_name              name of the pool
16723f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
16733f9d6ad7SLin Ling  */
1674fa9e4066Sahrens static int
16753f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1676fa9e4066Sahrens {
1677fa9e4066Sahrens 	spa_t *spa;
1678fa9e4066Sahrens 	int error;
1679fa9e4066Sahrens 
168006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
168106eeb2adSek 		return (error);
168206eeb2adSek 
16833f9d6ad7SLin Ling 	if (zc->zc_cookie == POOL_SCAN_NONE)
16843f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
16853f9d6ad7SLin Ling 	else
16863f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
168706eeb2adSek 
168806eeb2adSek 	spa_close(spa, FTAG);
168906eeb2adSek 
1690fa9e4066Sahrens 	return (error);
1691fa9e4066Sahrens }
1692fa9e4066Sahrens 
1693fa9e4066Sahrens static int
1694fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1695fa9e4066Sahrens {
1696fa9e4066Sahrens 	spa_t *spa;
1697fa9e4066Sahrens 	int error;
1698fa9e4066Sahrens 
1699fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1700fa9e4066Sahrens 	if (error == 0) {
1701fa9e4066Sahrens 		spa_freeze(spa);
1702fa9e4066Sahrens 		spa_close(spa, FTAG);
1703fa9e4066Sahrens 	}
1704fa9e4066Sahrens 	return (error);
1705fa9e4066Sahrens }
1706fa9e4066Sahrens 
1707eaca9bbdSeschrock static int
1708eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1709eaca9bbdSeschrock {
1710eaca9bbdSeschrock 	spa_t *spa;
1711eaca9bbdSeschrock 	int error;
1712eaca9bbdSeschrock 
171306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
171406eeb2adSek 		return (error);
171506eeb2adSek 
1716ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1717ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1718558d2d50Slling 		spa_close(spa, FTAG);
1719be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1720558d2d50Slling 	}
1721558d2d50Slling 
1722990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
172306eeb2adSek 	spa_close(spa, FTAG);
172406eeb2adSek 
172506eeb2adSek 	return (error);
172606eeb2adSek }
172706eeb2adSek 
172806eeb2adSek static int
172906eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
173006eeb2adSek {
173106eeb2adSek 	spa_t *spa;
173206eeb2adSek 	char *hist_buf;
173306eeb2adSek 	uint64_t size;
173406eeb2adSek 	int error;
173506eeb2adSek 
173606eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1737be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
173806eeb2adSek 
173906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
174006eeb2adSek 		return (error);
174106eeb2adSek 
1742e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1743d7306b64Sek 		spa_close(spa, FTAG);
1744be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1745d7306b64Sek 	}
1746d7306b64Sek 
174706eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
174806eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
174906eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1750478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1751478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1752478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
175306eeb2adSek 	}
175406eeb2adSek 
175506eeb2adSek 	spa_close(spa, FTAG);
175606eeb2adSek 	kmem_free(hist_buf, size);
175706eeb2adSek 	return (error);
175806eeb2adSek }
175906eeb2adSek 
1760e9103aaeSGarrett D'Amore static int
1761e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1762e9103aaeSGarrett D'Amore {
1763e9103aaeSGarrett D'Amore 	spa_t *spa;
1764e9103aaeSGarrett D'Amore 	int error;
1765e9103aaeSGarrett D'Amore 
1766e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1767e9103aaeSGarrett D'Amore 	if (error == 0) {
1768e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1769e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1770e9103aaeSGarrett D'Amore 	}
1771e9103aaeSGarrett D'Amore 	return (error);
1772e9103aaeSGarrett D'Amore }
1773e9103aaeSGarrett D'Amore 
177455434c77Sek static int
177555434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
177655434c77Sek {
17773b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
177855434c77Sek }
177955434c77Sek 
1780503ad85cSMatthew Ahrens /*
1781503ad85cSMatthew Ahrens  * inputs:
1782503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1783503ad85cSMatthew Ahrens  * zc_obj		object to find
1784503ad85cSMatthew Ahrens  *
1785503ad85cSMatthew Ahrens  * outputs:
1786503ad85cSMatthew Ahrens  * zc_value		name of object
1787503ad85cSMatthew Ahrens  */
178855434c77Sek static int
178955434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
179055434c77Sek {
1791503ad85cSMatthew Ahrens 	objset_t *os;
179255434c77Sek 	int error;
179355434c77Sek 
1794503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1795503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
179655434c77Sek 		return (error);
1797503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1798503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1799be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1800503ad85cSMatthew Ahrens 	}
1801503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
180255434c77Sek 	    sizeof (zc->zc_value));
1803503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
180455434c77Sek 
180555434c77Sek 	return (error);
180655434c77Sek }
180755434c77Sek 
180899d5e173STim Haley /*
180999d5e173STim Haley  * inputs:
181099d5e173STim Haley  * zc_name		name of filesystem
181199d5e173STim Haley  * zc_obj		object to find
181299d5e173STim Haley  *
181399d5e173STim Haley  * outputs:
181499d5e173STim Haley  * zc_stat		stats on object
181599d5e173STim Haley  * zc_value		path to object
181699d5e173STim Haley  */
181799d5e173STim Haley static int
181899d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
181999d5e173STim Haley {
182099d5e173STim Haley 	objset_t *os;
182199d5e173STim Haley 	int error;
182299d5e173STim Haley 
182399d5e173STim Haley 	/* XXX reading from objset not owned */
182499d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
182599d5e173STim Haley 		return (error);
182699d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
182799d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1828be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
182999d5e173STim Haley 	}
183099d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
183199d5e173STim Haley 	    sizeof (zc->zc_value));
183299d5e173STim Haley 	dmu_objset_rele(os, FTAG);
183399d5e173STim Haley 
183499d5e173STim Haley 	return (error);
183599d5e173STim Haley }
183699d5e173STim Haley 
1837fa9e4066Sahrens static int
1838fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1839fa9e4066Sahrens {
1840fa9e4066Sahrens 	spa_t *spa;
1841fa9e4066Sahrens 	int error;
1842e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1843e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1844fa9e4066Sahrens 
1845fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1846fa9e4066Sahrens 	if (error != 0)
1847fa9e4066Sahrens 		return (error);
1848fa9e4066Sahrens 
1849fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1850478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1851fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1852fa94a07fSbrendan 	    &l2cache, &nl2cache);
1853fa94a07fSbrendan 
1854e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1855e7cbe64fSgw 	    &spares, &nspares);
1856e7cbe64fSgw 
1857b1b8ab34Slling 	/*
1858b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1859e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1860e7cbe64fSgw 	 *
1861e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1862e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1863e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1864e7cbe64fSgw 	 *
1865e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1866b1b8ab34Slling 	 */
1867b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
18681195e687SMark J Musante 		nvlist_free(config);
1869b1b8ab34Slling 		spa_close(spa, FTAG);
1870be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1871b1b8ab34Slling 	}
1872b1b8ab34Slling 
1873fa94a07fSbrendan 	if (error == 0) {
1874fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1875fa9e4066Sahrens 		nvlist_free(config);
1876fa9e4066Sahrens 	}
1877fa9e4066Sahrens 	spa_close(spa, FTAG);
1878fa9e4066Sahrens 	return (error);
1879fa9e4066Sahrens }
1880fa9e4066Sahrens 
18813f9d6ad7SLin Ling /*
18823f9d6ad7SLin Ling  * inputs:
18833f9d6ad7SLin Ling  * zc_name		name of the pool
18843f9d6ad7SLin Ling  * zc_nvlist_conf	nvlist of devices to remove
18853f9d6ad7SLin Ling  * zc_cookie		to stop the remove?
18863f9d6ad7SLin Ling  */
1887fa9e4066Sahrens static int
1888fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1889fa9e4066Sahrens {
189099653d4eSeschrock 	spa_t *spa;
189199653d4eSeschrock 	int error;
189299653d4eSeschrock 
189399653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
189499653d4eSeschrock 	if (error != 0)
189599653d4eSeschrock 		return (error);
189699653d4eSeschrock 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
189799653d4eSeschrock 	spa_close(spa, FTAG);
189899653d4eSeschrock 	return (error);
1899fa9e4066Sahrens }
1900fa9e4066Sahrens 
1901fa9e4066Sahrens static int
19023d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1903fa9e4066Sahrens {
1904fa9e4066Sahrens 	spa_t *spa;
1905fa9e4066Sahrens 	int error;
19063d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1907fa9e4066Sahrens 
190806eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1909fa9e4066Sahrens 		return (error);
19103d7072f8Seschrock 	switch (zc->zc_cookie) {
19113d7072f8Seschrock 	case VDEV_STATE_ONLINE:
19123d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19133d7072f8Seschrock 		break;
1914fa9e4066Sahrens 
19153d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19163d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19173d7072f8Seschrock 		break;
1918fa9e4066Sahrens 
19193d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1920069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1921069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1922069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1923069f55e2SEric Schrock 
1924069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19253d7072f8Seschrock 		break;
19263d7072f8Seschrock 
19273d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1928069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1929069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1930069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1931069f55e2SEric Schrock 
1932069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19333d7072f8Seschrock 		break;
19343d7072f8Seschrock 
19353d7072f8Seschrock 	default:
1936be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
19373d7072f8Seschrock 	}
19383d7072f8Seschrock 	zc->zc_cookie = newstate;
1939fa9e4066Sahrens 	spa_close(spa, FTAG);
1940fa9e4066Sahrens 	return (error);
1941fa9e4066Sahrens }
1942fa9e4066Sahrens 
1943fa9e4066Sahrens static int
1944fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1945fa9e4066Sahrens {
1946fa9e4066Sahrens 	spa_t *spa;
1947fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1948fa9e4066Sahrens 	nvlist_t *config;
1949fa9e4066Sahrens 	int error;
1950fa9e4066Sahrens 
195106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1952fa9e4066Sahrens 		return (error);
1953fa9e4066Sahrens 
1954990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1955478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1956ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1957fa9e4066Sahrens 		nvlist_free(config);
1958fa9e4066Sahrens 	}
1959fa9e4066Sahrens 
1960fa9e4066Sahrens 	spa_close(spa, FTAG);
1961fa9e4066Sahrens 	return (error);
1962fa9e4066Sahrens }
1963fa9e4066Sahrens 
1964fa9e4066Sahrens static int
1965fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1966fa9e4066Sahrens {
1967fa9e4066Sahrens 	spa_t *spa;
1968fa9e4066Sahrens 	int error;
1969fa9e4066Sahrens 
197006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1971fa9e4066Sahrens 		return (error);
1972fa9e4066Sahrens 
19738ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1974fa9e4066Sahrens 
1975fa9e4066Sahrens 	spa_close(spa, FTAG);
1976fa9e4066Sahrens 	return (error);
1977fa9e4066Sahrens }
1978fa9e4066Sahrens 
19791195e687SMark J Musante static int
19801195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
19811195e687SMark J Musante {
19821195e687SMark J Musante 	spa_t *spa;
19831195e687SMark J Musante 	nvlist_t *config, *props = NULL;
19841195e687SMark J Musante 	int error;
19851195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
19861195e687SMark J Musante 
19871195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
19881195e687SMark J Musante 		return (error);
19891195e687SMark J Musante 
19901195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
19911195e687SMark J Musante 	    zc->zc_iflags, &config)) {
19921195e687SMark J Musante 		spa_close(spa, FTAG);
19931195e687SMark J Musante 		return (error);
19941195e687SMark J Musante 	}
19951195e687SMark J Musante 
19961195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
19971195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
19981195e687SMark J Musante 	    zc->zc_iflags, &props))) {
19991195e687SMark J Musante 		spa_close(spa, FTAG);
20001195e687SMark J Musante 		nvlist_free(config);
20011195e687SMark J Musante 		return (error);
20021195e687SMark J Musante 	}
20031195e687SMark J Musante 
20041195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20051195e687SMark J Musante 
20061195e687SMark J Musante 	spa_close(spa, FTAG);
20071195e687SMark J Musante 
20081195e687SMark J Musante 	nvlist_free(config);
20091195e687SMark J Musante 	nvlist_free(props);
20101195e687SMark J Musante 
20111195e687SMark J Musante 	return (error);
20121195e687SMark J Musante }
20131195e687SMark J Musante 
2014c67d9675Seschrock static int
2015c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2016c67d9675Seschrock {
2017c67d9675Seschrock 	spa_t *spa;
2018e9dbad6fSeschrock 	char *path = zc->zc_value;
2019ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2020c67d9675Seschrock 	int error;
2021c67d9675Seschrock 
2022c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2023c67d9675Seschrock 	if (error != 0)
2024c67d9675Seschrock 		return (error);
2025c67d9675Seschrock 
2026c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2027c67d9675Seschrock 	spa_close(spa, FTAG);
2028c67d9675Seschrock 	return (error);
2029c67d9675Seschrock }
2030c67d9675Seschrock 
20316809eb4eSEric Schrock static int
20326809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20336809eb4eSEric Schrock {
20346809eb4eSEric Schrock 	spa_t *spa;
20356809eb4eSEric Schrock 	char *fru = zc->zc_value;
20366809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20376809eb4eSEric Schrock 	int error;
20386809eb4eSEric Schrock 
20396809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20406809eb4eSEric Schrock 	if (error != 0)
20416809eb4eSEric Schrock 		return (error);
20426809eb4eSEric Schrock 
20436809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20446809eb4eSEric Schrock 	spa_close(spa, FTAG);
20456809eb4eSEric Schrock 	return (error);
20466809eb4eSEric Schrock }
20476809eb4eSEric Schrock 
2048fa9e4066Sahrens static int
2049a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2050fa9e4066Sahrens {
2051a7f53a56SChris Kirby 	int error = 0;
20527f7322feSeschrock 	nvlist_t *nv;
2053fa9e4066Sahrens 
2054a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2055fa9e4066Sahrens 
20565ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
205792241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2058a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2059432f72fdSahrens 		/*
2060bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
2061432f72fdSahrens 		 * which we aren't supposed to do with a
2062745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2063432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2064503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2065432f72fdSahrens 		 */
206619b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
206719b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
206819b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
206919b94df9SMatthew Ahrens 			if (error == EIO)
207019b94df9SMatthew Ahrens 				return (error);
2071fb09f5aaSMadhav Suresh 			VERIFY0(error);
2072e7437265Sahrens 		}
2073e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
20747f7322feSeschrock 		nvlist_free(nv);
20757f7322feSeschrock 	}
2076fa9e4066Sahrens 
2077a7f53a56SChris Kirby 	return (error);
2078a7f53a56SChris Kirby }
2079a7f53a56SChris Kirby 
2080a7f53a56SChris Kirby /*
2081a7f53a56SChris Kirby  * inputs:
2082a7f53a56SChris Kirby  * zc_name		name of filesystem
2083a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2084a7f53a56SChris Kirby  *
2085a7f53a56SChris Kirby  * outputs:
2086a7f53a56SChris Kirby  * zc_objset_stats	stats
2087a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2088a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2089a7f53a56SChris Kirby  */
2090a7f53a56SChris Kirby static int
2091a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2092a7f53a56SChris Kirby {
20933b2aab18SMatthew Ahrens 	objset_t *os;
2094a7f53a56SChris Kirby 	int error;
2095a7f53a56SChris Kirby 
20963b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
20973b2aab18SMatthew Ahrens 	if (error == 0) {
20983b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
20993b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
21003b2aab18SMatthew Ahrens 	}
2101a7f53a56SChris Kirby 
2102fa9e4066Sahrens 	return (error);
2103fa9e4066Sahrens }
2104fa9e4066Sahrens 
210592241e0bSTom Erickson /*
210692241e0bSTom Erickson  * inputs:
210792241e0bSTom Erickson  * zc_name		name of filesystem
210892241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
210992241e0bSTom Erickson  *
211092241e0bSTom Erickson  * outputs:
211192241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
211292241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
211392241e0bSTom Erickson  *
211492241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
211592241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
211692241e0bSTom Erickson  * local property values.
211792241e0bSTom Erickson  */
211892241e0bSTom Erickson static int
211992241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
212092241e0bSTom Erickson {
21213b2aab18SMatthew Ahrens 	int error = 0;
212292241e0bSTom Erickson 	nvlist_t *nv;
212392241e0bSTom Erickson 
212492241e0bSTom Erickson 	/*
212592241e0bSTom Erickson 	 * Without this check, we would return local property values if the
212692241e0bSTom Erickson 	 * caller has not already received properties on or after
212792241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
212892241e0bSTom Erickson 	 */
21293b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2130be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
213192241e0bSTom Erickson 
213292241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21333b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
213492241e0bSTom Erickson 		error = put_nvlist(zc, nv);
213592241e0bSTom Erickson 		nvlist_free(nv);
213692241e0bSTom Erickson 	}
213792241e0bSTom Erickson 
213892241e0bSTom Erickson 	return (error);
213992241e0bSTom Erickson }
214092241e0bSTom Erickson 
2141de8267e0Stimh static int
2142de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2143de8267e0Stimh {
2144de8267e0Stimh 	uint64_t value;
2145de8267e0Stimh 	int error;
2146de8267e0Stimh 
2147de8267e0Stimh 	/*
2148de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2149de8267e0Stimh 	 * the default value (if there is one).
2150de8267e0Stimh 	 */
2151de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2152de8267e0Stimh 		return (error);
2153de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2154de8267e0Stimh 	return (0);
2155de8267e0Stimh }
2156de8267e0Stimh 
21573cb34c60Sahrens /*
21583cb34c60Sahrens  * inputs:
21593cb34c60Sahrens  * zc_name		name of filesystem
2160de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
21613cb34c60Sahrens  *
21623cb34c60Sahrens  * outputs:
2163de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2164de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
21653cb34c60Sahrens  */
2166bd00f61bSrm static int
2167de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2168bd00f61bSrm {
2169de8267e0Stimh 	objset_t *os;
2170de8267e0Stimh 	int err;
2171bd00f61bSrm 
2172503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2173503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2174de8267e0Stimh 		return (err);
2175bd00f61bSrm 
2176bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2177bd00f61bSrm 
2178bd00f61bSrm 	/*
2179de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2180745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2181745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2182bd00f61bSrm 	 */
2183de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2184de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2185de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2186de8267e0Stimh 		nvlist_t *nv;
2187de8267e0Stimh 
2188de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2189de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2190de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2191de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2192de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2193de8267e0Stimh 			err = put_nvlist(zc, nv);
2194de8267e0Stimh 		nvlist_free(nv);
2195de8267e0Stimh 	} else {
2196be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2197de8267e0Stimh 	}
2198503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2199de8267e0Stimh 	return (err);
2200bd00f61bSrm }
2201bd00f61bSrm 
220214843421SMatthew Ahrens static boolean_t
220314843421SMatthew Ahrens dataset_name_hidden(const char *name)
220414843421SMatthew Ahrens {
220514843421SMatthew Ahrens 	/*
220614843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
220714843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
220814843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
220914843421SMatthew Ahrens 	 */
221014843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
221114843421SMatthew Ahrens 		return (B_TRUE);
221214843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
221314843421SMatthew Ahrens 		return (B_TRUE);
221414843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
221514843421SMatthew Ahrens 		return (B_TRUE);
221614843421SMatthew Ahrens 	return (B_FALSE);
221714843421SMatthew Ahrens }
221814843421SMatthew Ahrens 
2219de8267e0Stimh /*
2220de8267e0Stimh  * inputs:
2221de8267e0Stimh  * zc_name		name of filesystem
2222de8267e0Stimh  * zc_cookie		zap cursor
2223de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2224de8267e0Stimh  *
2225de8267e0Stimh  * outputs:
2226de8267e0Stimh  * zc_name		name of next filesystem
222714843421SMatthew Ahrens  * zc_cookie		zap cursor
2228de8267e0Stimh  * zc_objset_stats	stats
2229de8267e0Stimh  * zc_nvlist_dst	property nvlist
2230de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2231de8267e0Stimh  */
2232fa9e4066Sahrens static int
2233fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2234fa9e4066Sahrens {
223587e5029aSahrens 	objset_t *os;
2236fa9e4066Sahrens 	int error;
2237fa9e4066Sahrens 	char *p;
2238620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2239fa9e4066Sahrens 
2240620252bcSChris Kirby top:
2241503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
224287e5029aSahrens 		if (error == ENOENT)
2243be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
224487e5029aSahrens 		return (error);
2245fa9e4066Sahrens 	}
2246fa9e4066Sahrens 
2247fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2248fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2249fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2250fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2251fa9e4066Sahrens 
2252fa9e4066Sahrens 	do {
225387e5029aSahrens 		error = dmu_dir_list_next(os,
225487e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
225587e5029aSahrens 		    NULL, &zc->zc_cookie);
2256fa9e4066Sahrens 		if (error == ENOENT)
2257be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
225819b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2259503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2260fa9e4066Sahrens 
2261681d9761SEric Taylor 	/*
2262681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2263681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2264681d9761SEric Taylor 	 */
2265620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
226687e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2267620252bcSChris Kirby 		if (error == ENOENT) {
2268620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2269620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2270620252bcSChris Kirby 			goto top;
2271620252bcSChris Kirby 		}
2272620252bcSChris Kirby 	}
2273fa9e4066Sahrens 	return (error);
2274fa9e4066Sahrens }
2275fa9e4066Sahrens 
22763cb34c60Sahrens /*
22773cb34c60Sahrens  * inputs:
22783cb34c60Sahrens  * zc_name		name of filesystem
22793cb34c60Sahrens  * zc_cookie		zap cursor
22803cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
22810d8fa8f8SMartin Matuska  * zc_simple		when set, only name is requested
22823cb34c60Sahrens  *
22833cb34c60Sahrens  * outputs:
22843cb34c60Sahrens  * zc_name		name of next snapshot
22853cb34c60Sahrens  * zc_objset_stats	stats
22863cb34c60Sahrens  * zc_nvlist_dst	property nvlist
22873cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
22883cb34c60Sahrens  */
2289fa9e4066Sahrens static int
2290fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2291fa9e4066Sahrens {
229287e5029aSahrens 	objset_t *os;
2293fa9e4066Sahrens 	int error;
2294fa9e4066Sahrens 
2295503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
22963b2aab18SMatthew Ahrens 	if (error != 0) {
2297745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
22983b2aab18SMatthew Ahrens 	}
2299fa9e4066Sahrens 
2300b81d61a6Slling 	/*
2301b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2302b81d61a6Slling 	 * so exit immediately.
2303b81d61a6Slling 	 */
23049adfa60dSMatthew Ahrens 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
23059adfa60dSMatthew Ahrens 	    ZFS_MAX_DATASET_NAME_LEN) {
2306503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2307be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2308fa9e4066Sahrens 	}
2309fa9e4066Sahrens 
231087e5029aSahrens 	error = dmu_snapshot_list_next(os,
231187e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2312a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2313a7f53a56SChris Kirby 	    NULL);
2314a7f53a56SChris Kirby 
23150d8fa8f8SMartin Matuska 	if (error == 0 && !zc->zc_simple) {
2316a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2317a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2318a7f53a56SChris Kirby 
2319a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23203b2aab18SMatthew Ahrens 		if (error == 0) {
2321a7f53a56SChris Kirby 			objset_t *ossnap;
2322a7f53a56SChris Kirby 
2323a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2324a7f53a56SChris Kirby 			if (error == 0)
2325a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2326a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2327620252bcSChris Kirby 		}
2328620252bcSChris Kirby 	} else if (error == ENOENT) {
2329be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2330620252bcSChris Kirby 	}
2331fa9e4066Sahrens 
2332a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23333cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23343b2aab18SMatthew Ahrens 	if (error != 0)
23353cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2336fa9e4066Sahrens 	return (error);
2337fa9e4066Sahrens }
2338fa9e4066Sahrens 
233992241e0bSTom Erickson static int
234092241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2341fa9e4066Sahrens {
234292241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
234392241e0bSTom Erickson 	uint64_t *valary;
234492241e0bSTom Erickson 	unsigned int vallen;
234592241e0bSTom Erickson 	const char *domain;
2346eeb85002STim Haley 	char *dash;
234792241e0bSTom Erickson 	zfs_userquota_prop_t type;
234892241e0bSTom Erickson 	uint64_t rid;
234992241e0bSTom Erickson 	uint64_t quota;
235092241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
235192241e0bSTom Erickson 	int err;
235292241e0bSTom Erickson 
235392241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
235492241e0bSTom Erickson 		nvlist_t *attrs;
235592241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2356eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2357eeb85002STim Haley 		    &pair) != 0)
2358be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
235992241e0bSTom Erickson 	}
2360e9dbad6fSeschrock 
2361ecd6cf80Smarks 	/*
2362eeb85002STim Haley 	 * A correctly constructed propname is encoded as
236392241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2364ecd6cf80Smarks 	 */
2365eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2366eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2367eeb85002STim Haley 	    vallen != 3)
2368be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2369eeb85002STim Haley 
2370eeb85002STim Haley 	domain = dash + 1;
2371eeb85002STim Haley 	type = valary[0];
2372eeb85002STim Haley 	rid = valary[1];
2373eeb85002STim Haley 	quota = valary[2];
2374e9dbad6fSeschrock 
23751412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
237692241e0bSTom Erickson 	if (err == 0) {
237792241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
237892241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
237992241e0bSTom Erickson 	}
2380e9dbad6fSeschrock 
238192241e0bSTom Erickson 	return (err);
238292241e0bSTom Erickson }
238314843421SMatthew Ahrens 
238492241e0bSTom Erickson /*
238592241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
238692241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
238792241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
238892241e0bSTom Erickson  *
2389eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
239092241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
239192241e0bSTom Erickson  */
239292241e0bSTom Erickson static int
239392241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
239492241e0bSTom Erickson     nvpair_t *pair)
239592241e0bSTom Erickson {
239692241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
239792241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
239892241e0bSTom Erickson 	uint64_t intval;
2399b5152584SMatthew Ahrens 	int err = -1;
2400fa9e4066Sahrens 
240192241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
240292241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
240392241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
240492241e0bSTom Erickson 		return (-1);
240592241e0bSTom Erickson 	}
240614843421SMatthew Ahrens 
240792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
240892241e0bSTom Erickson 		nvlist_t *attrs;
240992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
241092241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
241192241e0bSTom Erickson 		    &pair) == 0);
241292241e0bSTom Erickson 	}
2413db870a07Sahrens 
241492241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
241592241e0bSTom Erickson 		return (-1);
2416b24ab676SJeff Bonwick 
241792241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
241840feaa91Sahrens 
241992241e0bSTom Erickson 	switch (prop) {
242092241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
242192241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
242292241e0bSTom Erickson 		break;
242392241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
24243b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
242592241e0bSTom Erickson 		break;
2426a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2427a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2428a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2429a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2430a2afb611SJerry Jelinek 			err = 0;
2431a2afb611SJerry Jelinek 		} else {
2432a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2433a2afb611SJerry Jelinek 		}
2434a2afb611SJerry Jelinek 		/*
2435a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2436a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2437a2afb611SJerry Jelinek 		 */
2438a2afb611SJerry Jelinek 		if (err == 0)
2439a2afb611SJerry Jelinek 			err = -1;
2440a2afb611SJerry Jelinek 		break;
244192241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
244292241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
244392241e0bSTom Erickson 		break;
244492241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
24453b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
244692241e0bSTom Erickson 		break;
244792241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2448c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
244992241e0bSTom Erickson 		break;
245092241e0bSTom Erickson 	case ZFS_PROP_VERSION:
245192241e0bSTom Erickson 	{
245292241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
24539e6eda55Smarks 
24541412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2455b24ab676SJeff Bonwick 			break;
2456b24ab676SJeff Bonwick 
245792241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
245892241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2459d0f3f37eSMark Shellenbaum 
246092241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2461b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2462b16da2e2SGeorge Wilson 
2463b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2464b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2465b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2466b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
246740feaa91Sahrens 		}
246892241e0bSTom Erickson 		break;
2469ecd6cf80Smarks 	}
247092241e0bSTom Erickson 	default:
247192241e0bSTom Erickson 		err = -1;
247292241e0bSTom Erickson 	}
2473e9dbad6fSeschrock 
247492241e0bSTom Erickson 	return (err);
247592241e0bSTom Erickson }
2476a9799022Sck 
247792241e0bSTom Erickson /*
247892241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
24794445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
24804445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
24814445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
24824445fffbSMatthew Ahrens  * corresponding error numbers.
248392241e0bSTom Erickson  *
24844445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
24854445fffbSMatthew Ahrens  * modified.
248692241e0bSTom Erickson  */
248792241e0bSTom Erickson int
248892241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
24894445fffbSMatthew Ahrens     nvlist_t *errlist)
249092241e0bSTom Erickson {
249192241e0bSTom Erickson 	nvpair_t *pair;
249292241e0bSTom Erickson 	nvpair_t *propval;
249302e383d1STom Erickson 	int rv = 0;
249492241e0bSTom Erickson 	uint64_t intval;
249592241e0bSTom Erickson 	char *strval;
24964445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
24974445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2498a9799022Sck 
249992241e0bSTom Erickson retry:
250092241e0bSTom Erickson 	pair = NULL;
250192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
250292241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
250392241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2504cfa69fd2STom Erickson 		int err = 0;
2505e9dbad6fSeschrock 
250692241e0bSTom Erickson 		/* decode the property value */
250792241e0bSTom Erickson 		propval = pair;
250892241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
250992241e0bSTom Erickson 			nvlist_t *attrs;
25104445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2511eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2512eeb85002STim Haley 			    &propval) != 0)
2513be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
251414843421SMatthew Ahrens 		}
2515e9dbad6fSeschrock 
251692241e0bSTom Erickson 		/* Validate value type */
2517eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
251892241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
251992241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2520be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
252192241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
252292241e0bSTom Erickson 				if (nvpair_type(propval) !=
252392241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2524be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
252519b94df9SMatthew Ahrens 			} else {
2526be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
25274201a95eSRic Aleshire 			}
2528eeb85002STim Haley 		} else if (err == 0) {
252992241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
253092241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2531be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
253292241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2533a2eea2e1Sahrens 				const char *unused;
2534a2eea2e1Sahrens 
25354445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2536e9dbad6fSeschrock 
2537e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
253891ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2539e9dbad6fSeschrock 					break;
254091ebeef5Sahrens 				case PROP_TYPE_STRING:
2541be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
254292241e0bSTom Erickson 					break;
254391ebeef5Sahrens 				case PROP_TYPE_INDEX:
2544acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
254592241e0bSTom Erickson 					    intval, &unused) != 0)
2546be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2547e9dbad6fSeschrock 					break;
2548e9dbad6fSeschrock 				default:
2549e7437265Sahrens 					cmn_err(CE_PANIC,
2550e7437265Sahrens 					    "unknown property type");
2551e9dbad6fSeschrock 				}
2552e9dbad6fSeschrock 			} else {
2553be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2554e9dbad6fSeschrock 			}
2555e9dbad6fSeschrock 		}
255692241e0bSTom Erickson 
255792241e0bSTom Erickson 		/* Validate permissions */
255892241e0bSTom Erickson 		if (err == 0)
255992241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
256092241e0bSTom Erickson 
256192241e0bSTom Erickson 		if (err == 0) {
256292241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
256392241e0bSTom Erickson 			if (err == -1) {
256492241e0bSTom Erickson 				/*
256592241e0bSTom Erickson 				 * For better performance we build up a list of
256692241e0bSTom Erickson 				 * properties to set in a single transaction.
256792241e0bSTom Erickson 				 */
256892241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
256992241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
257092241e0bSTom Erickson 				/*
257192241e0bSTom Erickson 				 * This may be a spurious error caused by
257292241e0bSTom Erickson 				 * receiving quota and reservation out of order.
257392241e0bSTom Erickson 				 * Try again in a second pass.
257492241e0bSTom Erickson 				 */
257592241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
257692241e0bSTom Erickson 			}
257792241e0bSTom Erickson 		}
257892241e0bSTom Erickson 
25794445fffbSMatthew Ahrens 		if (err != 0) {
25804445fffbSMatthew Ahrens 			if (errlist != NULL)
25814445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
25824445fffbSMatthew Ahrens 			rv = err;
25834445fffbSMatthew Ahrens 		}
2584e9dbad6fSeschrock 	}
2585e9dbad6fSeschrock 
258692241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
258792241e0bSTom Erickson 		nvl = retrynvl;
258892241e0bSTom Erickson 		goto retry;
258992241e0bSTom Erickson 	}
259092241e0bSTom Erickson 
259192241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
259292241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
259392241e0bSTom Erickson 		/*
259492241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
259592241e0bSTom Erickson 		 * can, so try setting them individually.
259692241e0bSTom Erickson 		 */
259792241e0bSTom Erickson 		pair = NULL;
259892241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
259992241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2600cfa69fd2STom Erickson 			int err = 0;
260192241e0bSTom Erickson 
260292241e0bSTom Erickson 			propval = pair;
260392241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
260492241e0bSTom Erickson 				nvlist_t *attrs;
26054445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
26064445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
26074445fffbSMatthew Ahrens 				    ZPROP_VALUE);
260892241e0bSTom Erickson 			}
260992241e0bSTom Erickson 
261092241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26114445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26123b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26133b2aab18SMatthew Ahrens 				    source, strval);
261492241e0bSTom Erickson 			} else {
26154445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26163b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26173b2aab18SMatthew Ahrens 				    intval);
261892241e0bSTom Erickson 			}
261992241e0bSTom Erickson 
262092241e0bSTom Erickson 			if (err != 0) {
26214445fffbSMatthew Ahrens 				if (errlist != NULL) {
26224445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26234445fffbSMatthew Ahrens 					    err);
26244445fffbSMatthew Ahrens 				}
26254445fffbSMatthew Ahrens 				rv = err;
262692241e0bSTom Erickson 			}
262792241e0bSTom Erickson 		}
26285c0b6a79SRich Morris 	}
26295c0b6a79SRich Morris 	nvlist_free(genericnvl);
263092241e0bSTom Erickson 	nvlist_free(retrynvl);
263192241e0bSTom Erickson 
263292241e0bSTom Erickson 	return (rv);
2633fa9e4066Sahrens }
2634fa9e4066Sahrens 
2635ea2f5b9eSMatthew Ahrens /*
2636ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2637ea2f5b9eSMatthew Ahrens  */
2638ea2f5b9eSMatthew Ahrens static int
26394445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2640ea2f5b9eSMatthew Ahrens {
264192241e0bSTom Erickson 	nvpair_t *pair = NULL;
2642ea2f5b9eSMatthew Ahrens 	int error = 0;
2643ea2f5b9eSMatthew Ahrens 
264492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
264592241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2646ea2f5b9eSMatthew Ahrens 
2647ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
264892241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2649be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2650ea2f5b9eSMatthew Ahrens 
2651ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2652ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2653ea2f5b9eSMatthew Ahrens 			return (error);
2654ea2f5b9eSMatthew Ahrens 
2655ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2656be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2657ea2f5b9eSMatthew Ahrens 
265878f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2659ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2660ea2f5b9eSMatthew Ahrens 	}
2661ea2f5b9eSMatthew Ahrens 	return (0);
2662ea2f5b9eSMatthew Ahrens }
2663ea2f5b9eSMatthew Ahrens 
266492241e0bSTom Erickson static void
266592241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
266692241e0bSTom Erickson {
266792241e0bSTom Erickson 	nvpair_t *pair;
266892241e0bSTom Erickson 
266992241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
267092241e0bSTom Erickson 
267192241e0bSTom Erickson 	pair = NULL;
267292241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
267392241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
267492241e0bSTom Erickson 			continue;
267592241e0bSTom Erickson 
267692241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
267792241e0bSTom Erickson 	}
267892241e0bSTom Erickson }
267992241e0bSTom Erickson 
268092241e0bSTom Erickson static int
26813b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
268292241e0bSTom Erickson     nvlist_t *skipped)
268392241e0bSTom Erickson {
268492241e0bSTom Erickson 	int err = 0;
268592241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
268692241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
268792241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
268892241e0bSTom Erickson 		/*
268992241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
269092241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
269192241e0bSTom Erickson 		 */
269292241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
26933b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
26943b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
269592241e0bSTom Erickson 	}
269692241e0bSTom Erickson 	nvlist_free(cleared_props);
269792241e0bSTom Erickson 	return (err);
269892241e0bSTom Erickson }
269992241e0bSTom Erickson 
27003cb34c60Sahrens /*
27013cb34c60Sahrens  * inputs:
27023cb34c60Sahrens  * zc_name		name of filesystem
27035c0b6a79SRich Morris  * zc_value		name of property to set
27043cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
270592241e0bSTom Erickson  * zc_cookie		received properties flag
27063cb34c60Sahrens  *
270792241e0bSTom Erickson  * outputs:
270892241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
27093cb34c60Sahrens  */
2710fa9e4066Sahrens static int
2711e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2712fa9e4066Sahrens {
2713e9dbad6fSeschrock 	nvlist_t *nvl;
271492241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
271592241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
271692241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27174445fffbSMatthew Ahrens 	nvlist_t *errors;
2718e9dbad6fSeschrock 	int error;
2719e9dbad6fSeschrock 
2720990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2721478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2722e9dbad6fSeschrock 		return (error);
2723e9dbad6fSeschrock 
272492241e0bSTom Erickson 	if (received) {
2725bb0ade09Sahrens 		nvlist_t *origprops;
2726bb0ade09Sahrens 
27273b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27283b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27293b2aab18SMatthew Ahrens 			    origprops, nvl);
27303b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2731bb0ade09Sahrens 		}
27323b2aab18SMatthew Ahrens 
27333b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2734bb0ade09Sahrens 	}
2735bb0ade09Sahrens 
27364445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27373b2aab18SMatthew Ahrens 	if (error == 0)
27383b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
273992241e0bSTom Erickson 
274092241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
274192241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
274292241e0bSTom Erickson 	}
2743ecd6cf80Smarks 
274492241e0bSTom Erickson 	nvlist_free(errors);
2745e9dbad6fSeschrock 	nvlist_free(nvl);
2746e9dbad6fSeschrock 	return (error);
2747fa9e4066Sahrens }
2748fa9e4066Sahrens 
27493cb34c60Sahrens /*
27503cb34c60Sahrens  * inputs:
27513cb34c60Sahrens  * zc_name		name of filesystem
27523cb34c60Sahrens  * zc_value		name of property to inherit
275392241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
27543cb34c60Sahrens  *
27553cb34c60Sahrens  * outputs:		none
27563cb34c60Sahrens  */
2757e45ce728Sahrens static int
2758e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2759e45ce728Sahrens {
276092241e0bSTom Erickson 	const char *propname = zc->zc_value;
276192241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
276292241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
276392241e0bSTom Erickson 	zprop_source_t source = (received
276492241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
276592241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
276692241e0bSTom Erickson 
276792241e0bSTom Erickson 	if (received) {
276892241e0bSTom Erickson 		nvlist_t *dummy;
276992241e0bSTom Erickson 		nvpair_t *pair;
277092241e0bSTom Erickson 		zprop_type_t type;
277192241e0bSTom Erickson 		int err;
277292241e0bSTom Erickson 
277392241e0bSTom Erickson 		/*
277492241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
277592241e0bSTom Erickson 		 * nvpair with type info.
277692241e0bSTom Erickson 		 */
277792241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
277892241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2779be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
278092241e0bSTom Erickson 
278192241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2782a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2783a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2784be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
278592241e0bSTom Erickson 		} else {
278692241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
278792241e0bSTom Erickson 		}
278892241e0bSTom Erickson 
278992241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
279092241e0bSTom Erickson 
279192241e0bSTom Erickson 		switch (type) {
279292241e0bSTom Erickson 		case PROP_TYPE_STRING:
279392241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
279492241e0bSTom Erickson 			break;
279592241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
279692241e0bSTom Erickson 		case PROP_TYPE_INDEX:
279792241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
279892241e0bSTom Erickson 			break;
279992241e0bSTom Erickson 		default:
280092241e0bSTom Erickson 			nvlist_free(dummy);
2801be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
280292241e0bSTom Erickson 		}
280392241e0bSTom Erickson 
280492241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
280592241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
280692241e0bSTom Erickson 		nvlist_free(dummy);
280792241e0bSTom Erickson 		if (err != -1)
280892241e0bSTom Erickson 			return (err); /* special property already handled */
280992241e0bSTom Erickson 	} else {
281092241e0bSTom Erickson 		/*
281192241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
281292241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
281392241e0bSTom Erickson 		 * and reservation to the received or default values even though
281492241e0bSTom Erickson 		 * they are not considered inheritable.
281592241e0bSTom Erickson 		 */
281692241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2817be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
281892241e0bSTom Erickson 	}
281992241e0bSTom Erickson 
28204445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28213b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2822e45ce728Sahrens }
2823e45ce728Sahrens 
2824b1b8ab34Slling static int
282511a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2826b1b8ab34Slling {
2827990b4856Slling 	nvlist_t *props;
2828b1b8ab34Slling 	spa_t *spa;
2829990b4856Slling 	int error;
283092241e0bSTom Erickson 	nvpair_t *pair;
2831b1b8ab34Slling 
283292241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
283392241e0bSTom Erickson 	    zc->zc_iflags, &props))
2834b1b8ab34Slling 		return (error);
2835b1b8ab34Slling 
2836379c004dSEric Schrock 	/*
2837379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2838379c004dSEric Schrock 	 * to handle the faulted case.
2839379c004dSEric Schrock 	 */
284092241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
284192241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2842379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
284392241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2844379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2845379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2846379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
2847379c004dSEric Schrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2848379c004dSEric Schrock 		}
2849379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2850b693757aSEric Schrock 		if (spa != NULL) {
2851b693757aSEric Schrock 			nvlist_free(props);
2852379c004dSEric Schrock 			return (0);
2853b693757aSEric Schrock 		}
2854379c004dSEric Schrock 	}
2855379c004dSEric Schrock 
2856b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2857990b4856Slling 		nvlist_free(props);
2858b1b8ab34Slling 		return (error);
2859b1b8ab34Slling 	}
2860b1b8ab34Slling 
2861990b4856Slling 	error = spa_prop_set(spa, props);
2862b1b8ab34Slling 
2863990b4856Slling 	nvlist_free(props);
2864b1b8ab34Slling 	spa_close(spa, FTAG);
2865b1b8ab34Slling 
2866b1b8ab34Slling 	return (error);
2867b1b8ab34Slling }
2868b1b8ab34Slling 
2869b1b8ab34Slling static int
287011a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2871b1b8ab34Slling {
2872b1b8ab34Slling 	spa_t *spa;
2873b1b8ab34Slling 	int error;
2874b1b8ab34Slling 	nvlist_t *nvp = NULL;
2875b1b8ab34Slling 
2876379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2877379c004dSEric Schrock 		/*
2878379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2879379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2880379c004dSEric Schrock 		 * anyway.
2881379c004dSEric Schrock 		 */
2882379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2883379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2884379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2885379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2886379c004dSEric Schrock 	} else {
2887379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2888379c004dSEric Schrock 		spa_close(spa, FTAG);
2889379c004dSEric Schrock 	}
2890b1b8ab34Slling 
2891b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2892b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2893b1b8ab34Slling 	else
2894be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2895b1b8ab34Slling 
2896379c004dSEric Schrock 	nvlist_free(nvp);
2897b1b8ab34Slling 	return (error);
2898b1b8ab34Slling }
2899b1b8ab34Slling 
29003cb34c60Sahrens /*
29013cb34c60Sahrens  * inputs:
29023cb34c60Sahrens  * zc_name		name of filesystem
29033cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29043cb34c60Sahrens  * zc_perm_action	allow/unallow flag
29053cb34c60Sahrens  *
29063cb34c60Sahrens  * outputs:		none
29073cb34c60Sahrens  */
2908ecd6cf80Smarks static int
2909ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2910ecd6cf80Smarks {
2911ecd6cf80Smarks 	int error;
2912ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2913ecd6cf80Smarks 
2914990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2915478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2916ecd6cf80Smarks 		return (error);
2917ecd6cf80Smarks 
2918ecd6cf80Smarks 	/*
2919ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2920ecd6cf80Smarks 	 */
2921ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2922ecd6cf80Smarks 		nvlist_free(fsaclnv);
2923be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2924ecd6cf80Smarks 	}
2925ecd6cf80Smarks 
2926ecd6cf80Smarks 	/*
2927ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2928ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2929ecd6cf80Smarks 	 * the nvlist(s)
2930ecd6cf80Smarks 	 */
2931ecd6cf80Smarks 
293291ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29333b2aab18SMatthew Ahrens 	if (error != 0) {
293491ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
293591ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
293691ebeef5Sahrens 			    fsaclnv, CRED());
293791ebeef5Sahrens 		} else {
293891ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
293991ebeef5Sahrens 			    fsaclnv, CRED());
294091ebeef5Sahrens 		}
2941ecd6cf80Smarks 	}
2942ecd6cf80Smarks 
2943ecd6cf80Smarks 	if (error == 0)
2944ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2945ecd6cf80Smarks 
2946ecd6cf80Smarks 	nvlist_free(fsaclnv);
2947ecd6cf80Smarks 	return (error);
2948ecd6cf80Smarks }
2949ecd6cf80Smarks 
29503cb34c60Sahrens /*
29513cb34c60Sahrens  * inputs:
29523cb34c60Sahrens  * zc_name		name of filesystem
29533cb34c60Sahrens  *
29543cb34c60Sahrens  * outputs:
29553cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29563cb34c60Sahrens  */
2957ecd6cf80Smarks static int
2958ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2959ecd6cf80Smarks {
2960ecd6cf80Smarks 	nvlist_t *nvp;
2961ecd6cf80Smarks 	int error;
2962ecd6cf80Smarks 
2963ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2964ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
2965ecd6cf80Smarks 		nvlist_free(nvp);
2966ecd6cf80Smarks 	}
2967ecd6cf80Smarks 
2968ecd6cf80Smarks 	return (error);
2969ecd6cf80Smarks }
2970ecd6cf80Smarks 
2971fa9e4066Sahrens /*
2972fa9e4066Sahrens  * Search the vfs list for a specified resource.  Returns a pointer to it
2973fa9e4066Sahrens  * or NULL if no suitable entry is found. The caller of this routine
2974fa9e4066Sahrens  * is responsible for releasing the returned vfs pointer.
2975fa9e4066Sahrens  */
2976fa9e4066Sahrens static vfs_t *
2977fa9e4066Sahrens zfs_get_vfs(const char *resource)
2978fa9e4066Sahrens {
2979fa9e4066Sahrens 	struct vfs *vfsp;
2980fa9e4066Sahrens 	struct vfs *vfs_found = NULL;
2981fa9e4066Sahrens 
2982fa9e4066Sahrens 	vfs_list_read_lock();
2983fa9e4066Sahrens 	vfsp = rootvfs;
2984fa9e4066Sahrens 	do {
2985fa9e4066Sahrens 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2986fa9e4066Sahrens 			VFS_HOLD(vfsp);
2987fa9e4066Sahrens 			vfs_found = vfsp;
2988fa9e4066Sahrens 			break;
2989fa9e4066Sahrens 		}
2990fa9e4066Sahrens 		vfsp = vfsp->vfs_next;
2991fa9e4066Sahrens 	} while (vfsp != rootvfs);
2992fa9e4066Sahrens 	vfs_list_unlock();
2993fa9e4066Sahrens 	return (vfs_found);
2994fa9e4066Sahrens }
2995fa9e4066Sahrens 
2996ecd6cf80Smarks /* ARGSUSED */
2997fa9e4066Sahrens static void
2998ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2999fa9e4066Sahrens {
3000da6c28aaSamw 	zfs_creat_t *zct = arg;
3001da6c28aaSamw 
3002de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3003da6c28aaSamw }
3004da6c28aaSamw 
3005de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3006da6c28aaSamw 
3007da6c28aaSamw /*
3008de8267e0Stimh  * inputs:
30090a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3010f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3011f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3012f7170741SWill Andrews  * createprops		list of properties requested by creator
3013de8267e0Stimh  *
3014de8267e0Stimh  * outputs:
3015de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30160a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3017da6c28aaSamw  *
3018de8267e0Stimh  * Determine the settings for utf8only, normalization and
3019de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3020de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3021de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3022de8267e0Stimh  * request settings for these properties, even if the requested
3023de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3024de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3025de8267e0Stimh  * processing.
3026da6c28aaSamw  */
3027da6c28aaSamw static int
302814843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30290a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30300a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3031da6c28aaSamw {
3032de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3033de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3034de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3035da6c28aaSamw 
3036de8267e0Stimh 	ASSERT(zplprops != NULL);
3037da6c28aaSamw 
3038de8267e0Stimh 	/*
3039de8267e0Stimh 	 * Pull out creator prop choices, if any.
3040de8267e0Stimh 	 */
3041de8267e0Stimh 	if (createprops) {
30420a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
30430a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3044de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3045de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3046de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3047de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3048de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3049de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3050de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3051de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3052de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3053de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3054de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3055de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3056de8267e0Stimh 	}
3057da6c28aaSamw 
3058c2a93d44Stimh 	/*
30590a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30600a48a24eStimh 	 * or pool is version is too "young" to support normalization
30610a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30620a48a24eStimh 	 * error out.
3063c2a93d44Stimh 	 */
30640a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30650a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30660a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30670a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3068de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30690a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3070be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3071c2a93d44Stimh 
3072de8267e0Stimh 	/*
3073de8267e0Stimh 	 * Put the version in the zplprops
3074de8267e0Stimh 	 */
3075de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3076de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3077da6c28aaSamw 
3078de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3079de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3080de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3081de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3082da6c28aaSamw 
3083c2a93d44Stimh 	/*
3084de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3085c2a93d44Stimh 	 */
3086de8267e0Stimh 	if (norm)
3087de8267e0Stimh 		u8 = 1;
3088de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3089de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3090de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3091de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3092de8267e0Stimh 
3093de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3094de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3095de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3096de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3097c2a93d44Stimh 
3098ab04eb8eStimh 	if (is_ci)
3099ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3100ab04eb8eStimh 
3101da6c28aaSamw 	return (0);
3102fa9e4066Sahrens }
3103fa9e4066Sahrens 
31040a48a24eStimh static int
31050a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31060a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31070a48a24eStimh {
31080a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
31090a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31100a48a24eStimh 	objset_t *os = NULL;
31119adfa60dSMatthew Ahrens 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
31120a48a24eStimh 	char *cp;
31130a586ceaSMark Shellenbaum 	spa_t *spa;
31140a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31150a48a24eStimh 	int error;
31160a48a24eStimh 
31170a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31180a48a24eStimh 	cp = strrchr(parentname, '/');
31190a48a24eStimh 	ASSERT(cp != NULL);
31200a48a24eStimh 	cp[0] = '\0';
31210a48a24eStimh 
31220a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31230a586ceaSMark Shellenbaum 		return (error);
31240a586ceaSMark Shellenbaum 
31250a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31260a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31270a586ceaSMark Shellenbaum 
31280a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31290a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31300a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31310a48a24eStimh 
31320a48a24eStimh 	/*
31330a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31340a48a24eStimh 	 */
3135503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31360a48a24eStimh 		return (error);
31370a48a24eStimh 
31380a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31390a48a24eStimh 	    zplprops, is_ci);
3140503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31410a48a24eStimh 	return (error);
31420a48a24eStimh }
31430a48a24eStimh 
31440a48a24eStimh static int
31450a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31460a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31470a48a24eStimh {
31480a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
31490a586ceaSMark Shellenbaum 	boolean_t sa_ok;
31500a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31510a48a24eStimh 	int error;
31520a48a24eStimh 
31530a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31540a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31550a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31560a48a24eStimh 
31570a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31580a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31590a48a24eStimh 	return (error);
31600a48a24eStimh }
31610a48a24eStimh 
31623cb34c60Sahrens /*
31634445fffbSMatthew Ahrens  * innvl: {
31644445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31654445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31664445fffbSMatthew Ahrens  * }
31673cb34c60Sahrens  *
31684445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31693cb34c60Sahrens  */
3170fa9e4066Sahrens static int
31714445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3172fa9e4066Sahrens {
3173fa9e4066Sahrens 	int error = 0;
31744445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3175ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3176ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31774445fffbSMatthew Ahrens 	int32_t type32;
31784445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31794445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3180fa9e4066Sahrens 
31814445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3182be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31834445fffbSMatthew Ahrens 	type = type32;
31844445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3185fa9e4066Sahrens 
31864445fffbSMatthew Ahrens 	switch (type) {
3187fa9e4066Sahrens 	case DMU_OST_ZFS:
3188fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3189fa9e4066Sahrens 		break;
3190fa9e4066Sahrens 
3191fa9e4066Sahrens 	case DMU_OST_ZVOL:
3192fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3193fa9e4066Sahrens 		break;
3194fa9e4066Sahrens 
3195fa9e4066Sahrens 	default:
31961d452cf5Sahrens 		cbfunc = NULL;
3197e7cbe64fSgw 		break;
3198fa9e4066Sahrens 	}
31994445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32004445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3201be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3202fa9e4066Sahrens 
3203da6c28aaSamw 	zct.zct_props = nvprops;
3204da6c28aaSamw 
32054445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3206be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32074445fffbSMatthew Ahrens 
32084445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
32094445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
32104445fffbSMatthew Ahrens 
32114445fffbSMatthew Ahrens 		if (nvprops == NULL)
3212be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32134445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32144445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3215be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3216fa9e4066Sahrens 
32174445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
32184445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32194445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3220be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32214445fffbSMatthew Ahrens 
32224445fffbSMatthew Ahrens 		if (error != 0)
32234445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
32244445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
32254445fffbSMatthew Ahrens 
32264445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
32274445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
32284445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
32294445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3230fa9e4066Sahrens 			return (error);
32314445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
32324445fffbSMatthew Ahrens 		int error;
3233ab04eb8eStimh 
32344445fffbSMatthew Ahrens 		/*
32354445fffbSMatthew Ahrens 		 * We have to have normalization and
32364445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
32374445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
32384445fffbSMatthew Ahrens 		 * now.
32394445fffbSMatthew Ahrens 		 */
32404445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
32414445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32424445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32434445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
32444445fffbSMatthew Ahrens 		if (error != 0) {
32454445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3246da6c28aaSamw 			return (error);
3247da6c28aaSamw 		}
32484445fffbSMatthew Ahrens 	}
3249ab04eb8eStimh 
32504445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
32514445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
32524445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
32535c5460e9Seschrock 
32544445fffbSMatthew Ahrens 	/*
32554445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32564445fffbSMatthew Ahrens 	 */
32574445fffbSMatthew Ahrens 	if (error == 0) {
32584445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32594445fffbSMatthew Ahrens 		    nvprops, outnvl);
32604445fffbSMatthew Ahrens 		if (error != 0)
32613b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32624445fffbSMatthew Ahrens 	}
32634445fffbSMatthew Ahrens 	return (error);
32644445fffbSMatthew Ahrens }
3265e9dbad6fSeschrock 
32664445fffbSMatthew Ahrens /*
32674445fffbSMatthew Ahrens  * innvl: {
32684445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32694445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32704445fffbSMatthew Ahrens  * }
32714445fffbSMatthew Ahrens  *
32724445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32734445fffbSMatthew Ahrens  */
32744445fffbSMatthew Ahrens static int
32754445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32764445fffbSMatthew Ahrens {
32774445fffbSMatthew Ahrens 	int error = 0;
32784445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32794445fffbSMatthew Ahrens 	char *origin_name;
3280e9dbad6fSeschrock 
32814445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3282be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32834445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3284e9dbad6fSeschrock 
32854445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32864445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3287be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3288e9dbad6fSeschrock 
32894445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3290be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32913b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
32923b2aab18SMatthew Ahrens 	if (error != 0)
32934445fffbSMatthew Ahrens 		return (error);
3294e9dbad6fSeschrock 
3295e9dbad6fSeschrock 	/*
3296e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3297e9dbad6fSeschrock 	 */
3298e9dbad6fSeschrock 	if (error == 0) {
32994445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33004445fffbSMatthew Ahrens 		    nvprops, outnvl);
330192241e0bSTom Erickson 		if (error != 0)
33023b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3303e9dbad6fSeschrock 	}
3304fa9e4066Sahrens 	return (error);
3305fa9e4066Sahrens }
3306fa9e4066Sahrens 
33073cb34c60Sahrens /*
33084445fffbSMatthew Ahrens  * innvl: {
33094445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33104445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33114445fffbSMatthew Ahrens  * }
33124445fffbSMatthew Ahrens  *
33134445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33143cb34c60Sahrens  */
3315fa9e4066Sahrens static int
33164445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3317fa9e4066Sahrens {
33184445fffbSMatthew Ahrens 	nvlist_t *snaps;
33194445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33204445fffbSMatthew Ahrens 	int error, poollen;
33214445fffbSMatthew Ahrens 	nvpair_t *pair;
3322bb0ade09Sahrens 
33234445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33244445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33254445fffbSMatthew Ahrens 		return (error);
33264445fffbSMatthew Ahrens 
33274445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33284445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3329be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33304445fffbSMatthew Ahrens 
33314445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3332be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33334445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33344445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33354445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33364445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33374445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3338bb0ade09Sahrens 
33394445fffbSMatthew Ahrens 		/*
33404445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33414445fffbSMatthew Ahrens 		 * contain only valid characters.
33424445fffbSMatthew Ahrens 		 */
334378f17100SMatthew Ahrens 		if (cp == NULL ||
334478f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3345be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3346bb0ade09Sahrens 
33474445fffbSMatthew Ahrens 		/*
33484445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
33494445fffbSMatthew Ahrens 		 */
33504445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
33514445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3352be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
33534445fffbSMatthew Ahrens 
33544445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
33554445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33564445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33574445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33584445fffbSMatthew Ahrens 			    == 0) {
3359be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33604445fffbSMatthew Ahrens 			}
33614445fffbSMatthew Ahrens 		}
33624445fffbSMatthew Ahrens 	}
3363bb0ade09Sahrens 
33643b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33654445fffbSMatthew Ahrens 	return (error);
33664445fffbSMatthew Ahrens }
33674445fffbSMatthew Ahrens 
33684445fffbSMatthew Ahrens /*
33694445fffbSMatthew Ahrens  * innvl: "message" -> string
33704445fffbSMatthew Ahrens  */
33714445fffbSMatthew Ahrens /* ARGSUSED */
33724445fffbSMatthew Ahrens static int
33734445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
33744445fffbSMatthew Ahrens {
33754445fffbSMatthew Ahrens 	char *message;
33764445fffbSMatthew Ahrens 	spa_t *spa;
33774445fffbSMatthew Ahrens 	int error;
33784445fffbSMatthew Ahrens 	char *poolname;
33794445fffbSMatthew Ahrens 
33804445fffbSMatthew Ahrens 	/*
33814445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
33824445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
33834445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
33844445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
33854445fffbSMatthew Ahrens 	 * we clear the TSD here.
33864445fffbSMatthew Ahrens 	 */
33874445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
33884445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
33894445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
33904445fffbSMatthew Ahrens 	strfree(poolname);
33914445fffbSMatthew Ahrens 	if (error != 0)
33924445fffbSMatthew Ahrens 		return (error);
33934445fffbSMatthew Ahrens 
33944445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
33954445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3396be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3397bb0ade09Sahrens 	}
3398ea2f5b9eSMatthew Ahrens 
33994445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34004445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3401be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34024445fffbSMatthew Ahrens 	}
3403ea2f5b9eSMatthew Ahrens 
34044445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
34054445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3406bb0ade09Sahrens 	return (error);
34071d452cf5Sahrens }
3408fa9e4066Sahrens 
34093b2aab18SMatthew Ahrens /*
34103b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34113b2aab18SMatthew Ahrens  * unmount may need to write out data.
34123b2aab18SMatthew Ahrens  *
34133b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34143b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3415fc7a6e3fSWill Andrews  *
3416fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3417fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34183b2aab18SMatthew Ahrens  */
3419fc7a6e3fSWill Andrews int
34203b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34211d452cf5Sahrens {
34224445fffbSMatthew Ahrens 	vfs_t *vfsp;
34233b2aab18SMatthew Ahrens 	zfsvfs_t *zfsvfs;
3424fc7a6e3fSWill Andrews 	int err;
34251d452cf5Sahrens 
34263b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3427fc7a6e3fSWill Andrews 		return (0);
34281d452cf5Sahrens 
34293b2aab18SMatthew Ahrens 	vfsp = zfs_get_vfs(snapname);
34304445fffbSMatthew Ahrens 	if (vfsp == NULL)
3431fc7a6e3fSWill Andrews 		return (0);
34323b2aab18SMatthew Ahrens 
34333b2aab18SMatthew Ahrens 	zfsvfs = vfsp->vfs_data;
34343b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34351d452cf5Sahrens 
3436fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
34374445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3438fc7a6e3fSWill Andrews 	if (err != 0)
3439fc7a6e3fSWill Andrews 		return (SET_ERROR(err));
34404445fffbSMatthew Ahrens 
34414445fffbSMatthew Ahrens 	/*
34424445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34434445fffbSMatthew Ahrens 	 */
34443b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
3445fc7a6e3fSWill Andrews 	return (0);
34463b2aab18SMatthew Ahrens }
34473b2aab18SMatthew Ahrens 
34483b2aab18SMatthew Ahrens /* ARGSUSED */
34493b2aab18SMatthew Ahrens static int
34503b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
34513b2aab18SMatthew Ahrens {
3452fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(snapname));
34533b2aab18SMatthew Ahrens }
34543b2aab18SMatthew Ahrens 
34553b2aab18SMatthew Ahrens /*
34563b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34573b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34583b2aab18SMatthew Ahrens  * if necessary.
34593b2aab18SMatthew Ahrens  */
34603b2aab18SMatthew Ahrens void
34613b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34623b2aab18SMatthew Ahrens {
34633b2aab18SMatthew Ahrens 	int error;
34643b2aab18SMatthew Ahrens 	objset_t *os;
34653b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34663b2aab18SMatthew Ahrens 
34673b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
34683b2aab18SMatthew Ahrens 	if (error != 0)
34693b2aab18SMatthew Ahrens 		return;
34703b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
34713b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
34729adfa60dSMatthew Ahrens 		char originname[ZFS_MAX_DATASET_NAME_LEN];
34733b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
34743b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3475fc7a6e3fSWill Andrews 		(void) zfs_unmount_snap(originname);
34763b2aab18SMatthew Ahrens 	} else {
34773b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
34783b2aab18SMatthew Ahrens 	}
34791d452cf5Sahrens }
34801d452cf5Sahrens 
34813cb34c60Sahrens /*
34824445fffbSMatthew Ahrens  * innvl: {
34834445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34844445fffbSMatthew Ahrens  *     (optional boolean) "defer"
34854445fffbSMatthew Ahrens  * }
34864445fffbSMatthew Ahrens  *
34874445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34883cb34c60Sahrens  *
34893cb34c60Sahrens  */
349078f17100SMatthew Ahrens /* ARGSUSED */
34911d452cf5Sahrens static int
34924445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34931d452cf5Sahrens {
34944445fffbSMatthew Ahrens 	nvlist_t *snaps;
349519b94df9SMatthew Ahrens 	nvpair_t *pair;
34964445fffbSMatthew Ahrens 	boolean_t defer;
34971d452cf5Sahrens 
34984445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3499be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
35004445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
350119b94df9SMatthew Ahrens 
35024445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35034445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
350478f17100SMatthew Ahrens 		(void) zfs_unmount_snap(nvpair_name(pair));
350578f17100SMatthew Ahrens 	}
350678f17100SMatthew Ahrens 
350778f17100SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
350878f17100SMatthew Ahrens }
350978f17100SMatthew Ahrens 
351078f17100SMatthew Ahrens /*
351178f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
351278f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
351378f17100SMatthew Ahrens  *
351478f17100SMatthew Ahrens  * innvl: {
351578f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
351678f17100SMatthew Ahrens  * }
351778f17100SMatthew Ahrens  *
351878f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
351978f17100SMatthew Ahrens  *
352078f17100SMatthew Ahrens  */
352178f17100SMatthew Ahrens /* ARGSUSED */
352278f17100SMatthew Ahrens static int
352378f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
352478f17100SMatthew Ahrens {
352578f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
352678f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
352778f17100SMatthew Ahrens 		char *snap_name;
352878f17100SMatthew Ahrens 
352978f17100SMatthew Ahrens 		/*
353078f17100SMatthew Ahrens 		 * Verify the snapshot argument.
353178f17100SMatthew Ahrens 		 */
353278f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
353378f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
353478f17100SMatthew Ahrens 
353578f17100SMatthew Ahrens 
353678f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
353778f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
353878f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
353978f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
354078f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
354178f17100SMatthew Ahrens 		}
354278f17100SMatthew Ahrens 	}
354378f17100SMatthew Ahrens 
354478f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
354578f17100SMatthew Ahrens }
354678f17100SMatthew Ahrens 
354778f17100SMatthew Ahrens /*
354878f17100SMatthew Ahrens  * innvl: {
354978f17100SMatthew Ahrens  *     property 1, property 2, ...
355078f17100SMatthew Ahrens  * }
355178f17100SMatthew Ahrens  *
355278f17100SMatthew Ahrens  * outnvl: {
355378f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
355478f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
355578f17100SMatthew Ahrens  * }
355678f17100SMatthew Ahrens  *
355778f17100SMatthew Ahrens  */
355878f17100SMatthew Ahrens static int
355978f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
356078f17100SMatthew Ahrens {
356178f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
356278f17100SMatthew Ahrens }
356378f17100SMatthew Ahrens 
356478f17100SMatthew Ahrens /*
356578f17100SMatthew Ahrens  * innvl: {
356678f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
356778f17100SMatthew Ahrens  * }
356878f17100SMatthew Ahrens  *
356978f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
357078f17100SMatthew Ahrens  *
357178f17100SMatthew Ahrens  */
357278f17100SMatthew Ahrens static int
357378f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
357478f17100SMatthew Ahrens     nvlist_t *outnvl)
357578f17100SMatthew Ahrens {
357678f17100SMatthew Ahrens 	int error, poollen;
357778f17100SMatthew Ahrens 
357878f17100SMatthew Ahrens 	poollen = strlen(poolname);
357978f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
358078f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
358119b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
358278f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
35834445fffbSMatthew Ahrens 
358419b94df9SMatthew Ahrens 		/*
358578f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
358678f17100SMatthew Ahrens 		 * must contain only valid characters.
358778f17100SMatthew Ahrens 		 */
358878f17100SMatthew Ahrens 		if (cp == NULL ||
358978f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
359078f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
359178f17100SMatthew Ahrens 
359278f17100SMatthew Ahrens 		/*
359378f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
359419b94df9SMatthew Ahrens 		 */
35954445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
359678f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
3597be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
359819b94df9SMatthew Ahrens 	}
359919b94df9SMatthew Ahrens 
360078f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
360178f17100SMatthew Ahrens 	return (error);
36021d452cf5Sahrens }
36031d452cf5Sahrens 
36043cb34c60Sahrens /*
36053cb34c60Sahrens  * inputs:
36063cb34c60Sahrens  * zc_name		name of dataset to destroy
36073cb34c60Sahrens  * zc_objset_type	type of objset
3608842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
36093cb34c60Sahrens  *
36103cb34c60Sahrens  * outputs:		none
36113cb34c60Sahrens  */
36121d452cf5Sahrens static int
36131d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
36141d452cf5Sahrens {
3615681d9761SEric Taylor 	int err;
3616fc7a6e3fSWill Andrews 
3617fc7a6e3fSWill Andrews 	if (zc->zc_objset_type == DMU_OST_ZFS) {
3618fc7a6e3fSWill Andrews 		err = zfs_unmount_snap(zc->zc_name);
3619fc7a6e3fSWill Andrews 		if (err != 0)
3620fc7a6e3fSWill Andrews 			return (err);
3621fc7a6e3fSWill Andrews 	}
3622fa9e4066Sahrens 
36233b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
36243b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
36253b2aab18SMatthew Ahrens 	else
36263b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3627681d9761SEric Taylor 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
36285c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3629681d9761SEric Taylor 	return (err);
3630fa9e4066Sahrens }
3631fa9e4066Sahrens 
36323cb34c60Sahrens /*
3633a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
36343cb34c60Sahrens  *
3635a7027df1SMatthew Ahrens  * innvl is not used.
3636a7027df1SMatthew Ahrens  *
3637a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3638a7027df1SMatthew Ahrens  * }
36393cb34c60Sahrens  */
3640a7027df1SMatthew Ahrens /* ARGSUSED */
3641fa9e4066Sahrens static int
3642a7027df1SMatthew Ahrens zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3643fa9e4066Sahrens {
3644ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
36453b2aab18SMatthew Ahrens 	int error;
3646ae46e4c7SMatthew Ahrens 
3647a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3648503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
364947f263f4Sek 		if (error == 0) {
365047f263f4Sek 			int resume_err;
36514ccbb6e7Sahrens 
3652a7027df1SMatthew Ahrens 			error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3653a7027df1SMatthew Ahrens 			resume_err = zfs_resume_fs(zfsvfs, fsname);
365447f263f4Sek 			error = error ? error : resume_err;
365547f263f4Sek 		}
36564ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
36574ccbb6e7Sahrens 	} else {
3658a7027df1SMatthew Ahrens 		error = dsl_dataset_rollback(fsname, NULL, outnvl);
36594ccbb6e7Sahrens 	}
36603b2aab18SMatthew Ahrens 	return (error);
36613b2aab18SMatthew Ahrens }
36624ccbb6e7Sahrens 
36633b2aab18SMatthew Ahrens static int
36643b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
36653b2aab18SMatthew Ahrens {
36663b2aab18SMatthew Ahrens 	const char *snapname = arg;
36679adfa60dSMatthew Ahrens 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
3668ae46e4c7SMatthew Ahrens 
36693b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3670fc7a6e3fSWill Andrews 	return (zfs_unmount_snap(fullname));
3671fa9e4066Sahrens }
3672fa9e4066Sahrens 
36733cb34c60Sahrens /*
36743cb34c60Sahrens  * inputs:
36753cb34c60Sahrens  * zc_name	old name of dataset
36763cb34c60Sahrens  * zc_value	new name of dataset
36773cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
36783cb34c60Sahrens  *
36793cb34c60Sahrens  * outputs:	none
36803cb34c60Sahrens  */
3681fa9e4066Sahrens static int
3682fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3683fa9e4066Sahrens {
36847f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
36853b2aab18SMatthew Ahrens 	char *at;
3686cdf5b4caSmmusante 
3687e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3688f18faf3fSek 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3689f18faf3fSek 	    strchr(zc->zc_value, '%'))
3690be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3691fa9e4066Sahrens 
36923b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
36933b2aab18SMatthew Ahrens 	if (at != NULL) {
36943b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3695a0c1127bSSteven Hartland 		int error;
3696a0c1127bSSteven Hartland 
36973b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3698be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
36993b2aab18SMatthew Ahrens 		*at = '\0';
37003b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZFS) {
3701a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
37023b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
37033b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3704a0c1127bSSteven Hartland 			if (error != 0) {
3705a0c1127bSSteven Hartland 				*at = '@';
37063b2aab18SMatthew Ahrens 				return (error);
3707a0c1127bSSteven Hartland 			}
37083b2aab18SMatthew Ahrens 		}
3709a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3710a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3711a0c1127bSSteven Hartland 		*at = '@';
3712a0c1127bSSteven Hartland 
3713a0c1127bSSteven Hartland 		return (error);
37143b2aab18SMatthew Ahrens 	} else {
37153b2aab18SMatthew Ahrens 		if (zc->zc_objset_type == DMU_OST_ZVOL)
37163b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
37173b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3718fa9e4066Sahrens 	}
3719fa9e4066Sahrens }
3720fa9e4066Sahrens 
372192241e0bSTom Erickson static int
372292241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
372392241e0bSTom Erickson {
372492241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
372592241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
372692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
372792241e0bSTom Erickson 	uint64_t intval;
372892241e0bSTom Erickson 	int err;
372992241e0bSTom Erickson 
373092241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
373192241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
373292241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
373392241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
373492241e0bSTom Erickson 				return (err);
373592241e0bSTom Erickson 			return (0);
373692241e0bSTom Erickson 		}
373792241e0bSTom Erickson 
373892241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
373992241e0bSTom Erickson 			const char *perm = NULL;
374092241e0bSTom Erickson 			const char *uq_prefix =
374192241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
374292241e0bSTom Erickson 			const char *gq_prefix =
374392241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
374492241e0bSTom Erickson 
374592241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
374692241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
374792241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
374892241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
374992241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
375092241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
375192241e0bSTom Erickson 			} else {
375292241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3753be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
375492241e0bSTom Erickson 			}
375592241e0bSTom Erickson 
375692241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
375792241e0bSTom Erickson 				return (err);
375892241e0bSTom Erickson 			return (0);
375992241e0bSTom Erickson 		}
376092241e0bSTom Erickson 
3761be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
376292241e0bSTom Erickson 	}
376392241e0bSTom Erickson 
376492241e0bSTom Erickson 	if (issnap)
3765be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
376692241e0bSTom Erickson 
376792241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
376892241e0bSTom Erickson 		/*
376992241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
377092241e0bSTom Erickson 		 * format.
377192241e0bSTom Erickson 		 */
377292241e0bSTom Erickson 		nvlist_t *attrs;
377392241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
377492241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
377592241e0bSTom Erickson 		    &pair) == 0);
377692241e0bSTom Erickson 	}
377792241e0bSTom Erickson 
377892241e0bSTom Erickson 	/*
377992241e0bSTom Erickson 	 * Check that this value is valid for this pool version
378092241e0bSTom Erickson 	 */
378192241e0bSTom Erickson 	switch (prop) {
378292241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
378392241e0bSTom Erickson 		/*
378492241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
378592241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
378692241e0bSTom Erickson 		 * we'll catch them later.
378792241e0bSTom Erickson 		 */
3788b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
378992241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
379092241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
379192241e0bSTom Erickson 			    zfs_earlier_version(dsname,
379292241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3793be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
379492241e0bSTom Erickson 			}
379592241e0bSTom Erickson 
379692241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
379792241e0bSTom Erickson 			    zfs_earlier_version(dsname,
379892241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3799be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
380092241e0bSTom Erickson 
3801a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
3802a6f561b4SSašo Kiselkov 				spa_t *spa;
3803a6f561b4SSašo Kiselkov 
3804a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3805a6f561b4SSašo Kiselkov 					return (err);
3806a6f561b4SSašo Kiselkov 
38072acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
38082acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
3809a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
3810be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
3811a6f561b4SSašo Kiselkov 				}
3812a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
3813a6f561b4SSašo Kiselkov 			}
3814a6f561b4SSašo Kiselkov 
381592241e0bSTom Erickson 			/*
381692241e0bSTom Erickson 			 * If this is a bootable dataset then
381792241e0bSTom Erickson 			 * verify that the compression algorithm
381892241e0bSTom Erickson 			 * is supported for booting. We must return
381992241e0bSTom Erickson 			 * something other than ENOTSUP since it
382092241e0bSTom Erickson 			 * implies a downrev pool version.
382192241e0bSTom Erickson 			 */
382292241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
382392241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
3824be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
382592241e0bSTom Erickson 			}
382692241e0bSTom Erickson 		}
382792241e0bSTom Erickson 		break;
382892241e0bSTom Erickson 
382992241e0bSTom Erickson 	case ZFS_PROP_COPIES:
383092241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3831be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
383292241e0bSTom Erickson 		break;
383392241e0bSTom Erickson 
3834b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
3835b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
3836b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
3837b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
3838b5152584SMatthew Ahrens 			spa_t *spa;
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;
41859adfa60dSMatthew Ahrens 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
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 
602545b17475SAlex Wilson 	if (error == 0)
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